xref: /haiku/src/tests/add-ons/print/transports/main.cpp (revision 4f00613311d0bd6b70fa82ce19931c41f071ea4e)
1 #include <stdio.h>
2 
3 #include <Application.h>
4 #include <Directory.h>
5 #include <FindDirectory.h>
6 #include <Path.h>
7 #include <File.h>
8 #include <image.h>
9 #include <DataIO.h>
10 #include <Message.h>
11 
12 int main (int argc, char *argv[])
13 {
14 	image_id addon;
15 	char *transport;
16 
17 	if (argc < 2) {
18 		printf("Usage: %s <transport add-on name> [<file to print>]\n", argv[0]);
19 		return B_ERROR;
20 	}
21 
22 	new BApplication("application/x-vnd-OBOS-print_transport_tester");
23 
24 	transport = argv[1];
25 
26 	printf("Looking for %s transport addon:", transport);
27 
28 	// try first in user add-ons directory
29 	printf("In user add-ons directory...\n");
30 
31 	BPath path;
32 	find_directory(B_USER_ADDONS_DIRECTORY, &path);
33 	path.Append("Print/transport");
34 	path.Append(transport);
35 
36 	addon = load_add_on(path.Path());
37 	if (addon < 0) {
38 		// on failure try in system add-ons directory
39 
40 		printf("In system add-ons directory...\n");
41 		find_directory(B_BEOS_ADDONS_DIRECTORY, &path);
42 		path.Append("Print/transport");
43 		path.Append(transport);
44 
45 		addon = load_add_on(path.Path());
46 	}
47 
48 	if (addon < 0) {
49 		// failed to load transport add-on
50 		printf("Failed to load %s print transport add-on!\n", transport);
51 		return B_ERROR;
52 	}
53 
54 	printf("Loaded from %s\n", path.Path());
55 
56 	// get init & exit proc
57 	BDataIO* (*init_proc)(BMessage*);
58 	void (*exit_proc)(void);
59 
60 	get_image_symbol(addon, "init_transport", B_SYMBOL_TYPE_TEXT, (void **) &init_proc);
61 	get_image_symbol(addon, "exit_transport", B_SYMBOL_TYPE_TEXT, (void **) &exit_proc);
62 
63 	if (init_proc == NULL || exit_proc == NULL) {
64 		// transport add-on has not the proper interface
65 		printf("Invalid print transport add-on API!\n");
66 		unload_add_on(addon);
67 		return B_ERROR;
68 	}
69 
70 	printf("Initing %s: ", transport);
71 
72 	// now, initialize the transport add-on
73 	// request BDataIO object from transport add-on
74 	BMessage msg('TRIN');
75 	// TODO: create on the fly a temporary printer folder for testing purpose only
76 	msg.AddString("printer_file", "/boot/home/config/settings/printers/test");
77 	BDataIO *io = (*init_proc)(&msg);
78 
79 	if (io) {
80 		printf("done.\nTransport parameters msg =>\n");
81 		msg.PrintToStream();
82 	} else
83 		printf("failed!\n");
84 
85 
86 	if (argc > 2) {
87 		BFile data(argv[2], B_READ_ONLY);
88 		if (data.InitCheck() == B_OK) {
89 			uint8 buffer[B_PAGE_SIZE];
90 			ssize_t total = 0;
91 			ssize_t sz;
92 
93 			printf("Sending data read from %s file...\n", argv[2]);
94 			while((sz = data.Read(buffer, sizeof(buffer))) > 0) {
95 				if (io->Write(buffer, sz) < 0) {
96 					printf("Error writting on the print transport stream!\n");
97 					break;
98 				}
99 				total += sz;
100 			}	// while
101 			printf("%ld data bytes sent.\n", total);
102 		}	// data valid file
103 	}	// optional data file
104 
105 	if (exit_proc) {
106 		printf("Exiting %s...\n", transport);
107 		(*exit_proc)();
108 	}
109 
110 	unload_add_on(addon);
111 	printf("%s unloaded.\n", transport);
112 
113 	return B_OK;
114 }