xref: /haiku/src/tests/add-ons/print/transports/main.cpp (revision 308f0e195dc9a71620970f8ebfe6e02fe6e27ddc)
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 = -1;
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:\n", transport);
27 
28 	directory_which which[] = {
29 		B_USER_ADDONS_DIRECTORY,
30 		B_COMMON_ADDONS_DIRECTORY,
31 		B_SYSTEM_ADDONS_DIRECTORY
32 	};
33 	BPath path;
34 	for (uint32 i = 0; i <sizeof(which) / sizeof(which[0]); i++) {
35 		if (find_directory(which[i], &path, false) != B_OK)
36 			continue;
37 
38 		path.Append("Print/transport");
39 		path.Append(transport);
40 
41 		printf("\t%s ?\n", path.Path());
42 		addon = load_add_on(path.Path());
43 		if (addon >= B_OK)
44 			break;
45 	}
46 
47 	if (addon == B_ERROR) {
48 		// failed to load transport add-on
49 		printf("Failed to load \"%s\" print transport add-on!\n", transport);
50 		return -1;
51 	}
52 
53 	printf("Add-on %d = \"%s\" loaded from %s.\n", (int) addon,
54 		transport, 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 }
115