xref: /haiku/src/add-ons/print/transports/usb_port/USBTransport.cpp (revision 1214ef1b2100f2b3299fc9d8d6142e46f70a4c3f)
1 /*****************************************************************************/
2 // USB port transport add-on,
3 // changes by Andreas Benzler, Philippe Houdoin
4 //
5 // Original from Parallel
6 // port transport add-on.
7 //
8 // Author
9 //   Michael Pfeiffer
10 //
11 // This application and all source files used in its construction, except
12 // where noted, are licensed under the MIT License, and have been written
13 // and are:
14 //
15 // Copyright (c) 2001-2004 OpenBeOS Project
16 //
17 // Permission is hereby granted, free of charge, to any person obtaining a
18 // copy of this software and associated documentation files (the "Software"),
19 // to deal in the Software without restriction, including without limitation
20 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
21 // and/or sell copies of the Software, and to permit persons to whom the
22 // Software is furnished to do so, subject to the following conditions:
23 //
24 // The above copyright notice and this permission notice shall be included
25 // in all copies or substantial portions of the Software.
26 //
27 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
28 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
30 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
32 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
33 // DEALINGS IN THE SOFTWARE.
34 /*****************************************************************************/
35 
36 
37 #include <stdio.h>
38 #include <unistd.h>
39 #include <string.h>
40 
41 #include <StorageKit.h>
42 #include <SupportKit.h>
43 
44 #include <USB_printer.h>
45 
46 #include "PrintTransportAddOn.h"
47 
48 
49 class USBTransport : public BDataIO
50 {
51 public:
52 	USBTransport(BDirectory *printer, BMessage *msg);
53 	~USBTransport();
54 
55 	status_t InitCheck() { return fFile > -1 ? B_OK : B_ERROR; };
56 
57 	ssize_t Read(void *buffer, size_t size);
58 	ssize_t Write(const void *buffer, size_t size);
59 
60 private:
61 	int fFile;
62 };
63 
64 
65 // Implementation of transport add-on interface
66 
67 BDataIO *
68 instantiate_transport(BDirectory *printer, BMessage *msg)
69 {
70 	USBTransport * transport = new USBTransport(printer, msg);
71 	if (transport->InitCheck() == B_OK)
72 		return transport;
73 
74 	delete transport;
75 	return NULL;
76 }
77 
78 
79 // Implementation of USBTransport
80 
81 USBTransport::USBTransport(BDirectory *printer, BMessage *msg)
82 	: fFile(-1)
83 {
84 	char device_id[USB_PRINTER_DEVICE_ID_LENGTH + 1];
85 	char name[USB_PRINTER_DEVICE_ID_LENGTH + 1];
86 	char *desc;
87 	char *value;
88 	int ret;
89 	bool bidirectional = true;
90 	char *next_token;
91 
92 	// We support only one USB printer, so does BeOS R5.
93 	fFile = open("/dev/printer/usb/0", O_RDWR | O_EXCL | O_BINARY, 0);
94 	if (fFile < 0) {
95 		// Try unidirectional access mode
96 		bidirectional = false;
97 		fFile = open("/dev/printer/usb/0", O_WRONLY | O_EXCL | O_BINARY, 0);
98 	}
99 
100 	if (fFile < 0)
101 		return;
102 
103 	// Get printer's DEVICE ID string
104 	ret = ioctl(fFile, USB_PRINTER_GET_DEVICE_ID, device_id, sizeof(device_id));
105 	if (ret < 0) {
106 		close(fFile);
107 		fFile = -1;
108 		return;
109 	}
110 
111 	if (! msg)
112 		// Caller don't care about transport init message output content...
113 		return;
114 
115 	// Fill up the message
116 	msg->what = 'okok';
117 
118 	msg->AddBool("bidirectional", bidirectional);
119 	msg->AddString("device_id", device_id);
120 
121 	// parse and split the device_id string into separate parameters
122 	desc = strtok_r(device_id, ":", &next_token);
123 	while (desc) {
124 		snprintf(name, sizeof(name), "DEVID:%s", desc);
125 		value = strtok_r(NULL, ";", &next_token);
126 		if (!value)
127 			break;
128 		msg->AddString(name, value);
129 
130 		// next device descriptor
131 		desc = strtok_r(NULL, ":", &next_token);
132 	}
133 }
134 
135 
136 USBTransport::~USBTransport()
137 {
138 	if (fFile > -1)
139 		close(fFile);
140 }
141 
142 
143 ssize_t
144 USBTransport::Read(void *buffer, size_t size)
145 {
146 	return read(fFile, buffer, size);
147 }
148 
149 
150 ssize_t
151 USBTransport::Write(const void *buffer, size_t size)
152 {
153 	return write(fFile, buffer, size);
154 }
155 
156