]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
f7faf1f5 | 2 | * "$Id: ieee1284.c 5591 2006-05-26 19:51:59Z mike $" |
ef416fc2 | 3 | * |
4 | * IEEE-1284 support functions for the Common UNIX Printing System (CUPS). | |
5 | * | |
6 | * Copyright 1997-2006 by Easy Software Products, all rights reserved. | |
7 | * | |
8 | * These coded instructions, statements, and computer programs are the | |
9 | * property of Easy Software Products and are protected by Federal | |
10 | * copyright law. Distribution and use rights are outlined in the file | |
11 | * "LICENSE" which should have been included with this file. If this | |
12 | * file is missing or damaged please contact Easy Software Products | |
13 | * at: | |
14 | * | |
15 | * Attn: CUPS Licensing Information | |
16 | * Easy Software Products | |
17 | * 44141 Airport View Drive, Suite 204 | |
18 | * Hollywood, Maryland 20636 USA | |
19 | * | |
20 | * Voice: (301) 373-9600 | |
21 | * EMail: cups-info@cups.org | |
22 | * WWW: http://www.cups.org | |
23 | * | |
24 | * This file is subject to the Apple OS-Developed Software exception. | |
25 | * | |
26 | * Contents: | |
27 | * | |
ed486911 | 28 | * backendGetDeviceID() - Get the IEEE-1284 device ID string and |
29 | * corresponding URI. | |
30 | * backendGetMakeModel() - Get the make and model string from the device ID. | |
ef416fc2 | 31 | */ |
32 | ||
33 | /* | |
34 | * Include necessary headers. | |
35 | */ | |
36 | ||
ed486911 | 37 | #include "backend-private.h" |
ef416fc2 | 38 | |
ed486911 | 39 | #ifdef __linux |
40 | # include <sys/ioctl.h> | |
41 | # include <linux/lp.h> | |
42 | # define IOCNR_GET_DEVICE_ID 1 | |
43 | # define LPIOC_GET_DEVICE_ID(len) _IOC(_IOC_READ, 'P', IOCNR_GET_DEVICE_ID, len) | |
44 | #endif /* __linux */ | |
89d46774 | 45 | |
ed486911 | 46 | #ifdef __sun |
47 | # ifdef __sparc | |
48 | # include <sys/ecppio.h> | |
49 | # else | |
50 | # include <sys/ioccom.h> | |
51 | # include <sys/ecppsys.h> | |
52 | # endif /* __sparc */ | |
53 | #endif /* __sun */ | |
ef416fc2 | 54 | |
55 | ||
56 | /* | |
ed486911 | 57 | * 'backendGetDeviceID()' - Get the IEEE-1284 device ID string and |
58 | * corresponding URI. | |
ef416fc2 | 59 | */ |
60 | ||
61 | int /* O - 0 on success, -1 on failure */ | |
ed486911 | 62 | backendGetDeviceID( |
ef416fc2 | 63 | int fd, /* I - File descriptor */ |
64 | char *device_id, /* O - 1284 device ID */ | |
65 | int device_id_size, /* I - Size of buffer */ | |
66 | char *make_model, /* O - Make/model */ | |
67 | int make_model_size, /* I - Size of buffer */ | |
68 | const char *scheme, /* I - URI scheme */ | |
69 | char *uri, /* O - Device URI */ | |
70 | int uri_size) /* I - Size of buffer */ | |
71 | { | |
72 | char *attr, /* 1284 attribute */ | |
73 | *delim, /* 1284 delimiter */ | |
74 | *uriptr, /* Pointer into URI */ | |
89d46774 | 75 | manufacturer[256], /* Manufacturer string */ |
ef416fc2 | 76 | serial_number[1024]; /* Serial number string */ |
89d46774 | 77 | int manulen; /* Length of manufacturer string */ |
ef416fc2 | 78 | #ifdef __linux |
79 | int length; /* Length of device ID info */ | |
80 | #endif /* __linux */ | |
b423cd4c | 81 | #if defined(__sun) && defined(ECPPIOC_GETDEVID) |
ef416fc2 | 82 | struct ecpp_device_id did; /* Device ID buffer */ |
b423cd4c | 83 | #endif /* __sun && ECPPIOC_GETDEVID */ |
ef416fc2 | 84 | |
89d46774 | 85 | |
ed486911 | 86 | DEBUG_printf(("backendGetDeviceID(fd=%d, device_id=%p, device_id_size=%d, " |
ef416fc2 | 87 | "make_model=%p, make_model_size=%d, scheme=\"%s\", " |
88 | "uri=%p, uri_size=%d)\n", fd, device_id, device_id_size, | |
89 | make_model, make_model_size, scheme ? scheme : "(null)", | |
90 | uri, uri_size)); | |
91 | ||
92 | /* | |
93 | * Range check input... | |
94 | */ | |
95 | ||
96 | if (fd < 0 || | |
97 | !device_id || device_id_size < 32 || | |
98 | !make_model || make_model_size < 32) | |
99 | { | |
ed486911 | 100 | DEBUG_puts("backendGetDeviceID: Bad args!"); |
ef416fc2 | 101 | return (-1); |
102 | } | |
103 | ||
104 | *device_id = '\0'; | |
105 | *make_model = '\0'; | |
106 | ||
107 | if (uri) | |
108 | *uri = '\0'; | |
109 | ||
110 | /* | |
111 | * Get the device ID string... | |
112 | */ | |
113 | ||
114 | #ifdef __linux | |
115 | if (!ioctl(fd, LPIOC_GET_DEVICE_ID(device_id_size), device_id)) | |
116 | { | |
117 | /* | |
118 | * Extract the length of the device ID string from the first two | |
119 | * bytes. The 1284 spec says the length is stored MSB first... | |
120 | */ | |
121 | ||
122 | length = (((unsigned)device_id[0] & 255) << 8) + | |
123 | ((unsigned)device_id[1] & 255); | |
124 | ||
125 | /* | |
126 | * Check to see if the length is larger than our buffer; first | |
127 | * assume that the vendor incorrectly implemented the 1284 spec, | |
128 | * and then limit the length to the size of our buffer... | |
129 | */ | |
130 | ||
131 | if (length > (device_id_size - 2)) | |
132 | length = (((unsigned)device_id[1] & 255) << 8) + | |
133 | ((unsigned)device_id[0] & 255); | |
134 | ||
135 | if (length > (device_id_size - 2)) | |
136 | length = device_id_size - 2; | |
137 | ||
138 | /* | |
139 | * Copy the device ID text to the beginning of the buffer and | |
140 | * nul-terminate. | |
141 | */ | |
142 | ||
143 | memmove(device_id, device_id + 2, length); | |
144 | device_id[length] = '\0'; | |
145 | } | |
146 | # ifdef DEBUG | |
147 | else | |
ed486911 | 148 | printf("backendGetDeviceID: ioctl failed - %s\n", strerror(errno)); |
ef416fc2 | 149 | # endif /* DEBUG */ |
150 | #endif /* __linux */ | |
151 | ||
152 | #if defined(__sun) && defined(ECPPIOC_GETDEVID) | |
153 | did.mode = ECPP_CENTRONICS; | |
154 | did.len = device_id_size - 1; | |
155 | did.rlen = 0; | |
156 | did.addr = device_id; | |
157 | ||
158 | if (!ioctl(fd, ECPPIOC_GETDEVID, &did)) | |
159 | { | |
160 | /* | |
161 | * Nul-terminate the device ID text. | |
162 | */ | |
163 | ||
164 | if (did.rlen < (device_id_size - 1)) | |
165 | device_id[did.rlen] = '\0'; | |
166 | else | |
167 | device_id[device_id_size - 1] = '\0'; | |
168 | } | |
169 | # ifdef DEBUG | |
170 | else | |
ed486911 | 171 | printf("backendGetDeviceID: ioctl failed - %s\n", strerror(errno)); |
ef416fc2 | 172 | # endif /* DEBUG */ |
173 | #endif /* __sun && ECPPIOC_GETDEVID */ | |
174 | ||
ed486911 | 175 | DEBUG_printf(("backendGetDeviceID: device_id=\"%s\"\n", device_id)); |
ef416fc2 | 176 | |
177 | if (!*device_id) | |
178 | return (-1); | |
179 | ||
89d46774 | 180 | /* |
181 | * Get the make and model... | |
182 | */ | |
183 | ||
ed486911 | 184 | backendGetMakeModel(device_id, make_model, make_model_size); |
89d46774 | 185 | |
186 | /* | |
187 | * Then generate a device URI... | |
188 | */ | |
189 | ||
190 | if (scheme && uri && uri_size > 32) | |
191 | { | |
192 | /* | |
193 | * Look for the serial number field... | |
194 | */ | |
195 | ||
196 | if ((attr = strstr(device_id, "SERN:")) != NULL) | |
197 | attr += 5; | |
198 | else if ((attr = strstr(device_id, "SERIALNUMBER:")) != NULL) | |
199 | attr += 13; | |
200 | else if ((attr = strstr(device_id, ";SN:")) != NULL) | |
201 | attr += 4; | |
202 | ||
203 | if (attr) | |
204 | { | |
205 | strlcpy(serial_number, attr, sizeof(serial_number)); | |
206 | ||
207 | if ((delim = strchr(serial_number, ';')) != NULL) | |
208 | *delim = '\0'; | |
209 | } | |
210 | else | |
211 | serial_number[0] = '\0'; | |
212 | ||
213 | /* | |
214 | * Generate the device URI from the manufacturer, make_model, and | |
215 | * serial number strings. | |
216 | */ | |
217 | ||
218 | snprintf(uri, uri_size, "%s://", scheme); | |
219 | ||
220 | if ((attr = strstr(device_id, "MANUFACTURER:")) != NULL) | |
221 | attr += 13; | |
222 | else if ((attr = strstr(device_id, "Manufacturer:")) != NULL) | |
223 | attr += 13; | |
224 | else if ((attr = strstr(device_id, "MFG:")) != NULL) | |
225 | attr += 4; | |
226 | ||
227 | if (attr) | |
228 | { | |
229 | strlcpy(manufacturer, attr, sizeof(manufacturer)); | |
230 | ||
231 | if ((delim = strchr(manufacturer, ';')) != NULL) | |
232 | *delim = '\0'; | |
233 | ||
234 | if (!strcasecmp(manufacturer, "Hewlett-Packard")) | |
235 | strcpy(manufacturer, "HP"); | |
236 | } | |
237 | else | |
238 | { | |
239 | strlcpy(manufacturer, make_model, sizeof(manufacturer)); | |
240 | ||
241 | if ((delim = strchr(manufacturer, ' ')) != NULL) | |
242 | *delim = '\0'; | |
243 | } | |
244 | ||
245 | manulen = strlen(manufacturer); | |
246 | ||
247 | for (uriptr = uri + strlen(uri), delim = manufacturer; | |
248 | *delim && uriptr < (uri + uri_size - 3); | |
249 | delim ++) | |
250 | if (*delim == ' ') | |
251 | { | |
252 | *uriptr++ = '%'; | |
253 | *uriptr++ = '2'; | |
254 | *uriptr++ = '0'; | |
255 | } | |
256 | else | |
257 | *uriptr++ = *delim; | |
258 | ||
259 | *uriptr++ = '/'; | |
260 | ||
261 | if (!strncasecmp(make_model, manufacturer, manulen)) | |
262 | { | |
263 | delim = make_model + manulen; | |
264 | ||
265 | while (isspace(*delim & 255)) | |
266 | delim ++; | |
267 | } | |
268 | else | |
269 | delim = make_model; | |
270 | ||
271 | for (; *delim && uriptr < (uri + uri_size - 3); delim ++) | |
272 | if (*delim == ' ') | |
273 | { | |
274 | *uriptr++ = '%'; | |
275 | *uriptr++ = '2'; | |
276 | *uriptr++ = '0'; | |
277 | } | |
278 | else | |
279 | *uriptr++ = *delim; | |
280 | ||
281 | if (serial_number[0]) | |
282 | { | |
283 | /* | |
284 | * Add the serial number to the URI... | |
285 | */ | |
286 | ||
287 | strlcpy(uriptr, "?serial=", uri_size - (uriptr - uri)); | |
288 | strlcat(uriptr, serial_number, uri_size - (uriptr - uri)); | |
289 | } | |
290 | else | |
291 | *uriptr = '\0'; | |
292 | } | |
293 | ||
294 | return (0); | |
295 | } | |
89d46774 | 296 | |
297 | ||
298 | /* | |
ed486911 | 299 | * 'backendGetMakeModel()' - Get the make and model string from the device ID. |
89d46774 | 300 | */ |
301 | ||
302 | int /* O - 0 on success, -1 on failure */ | |
ed486911 | 303 | backendGetMakeModel( |
89d46774 | 304 | const char *device_id, /* O - 1284 device ID */ |
305 | char *make_model, /* O - Make/model */ | |
306 | int make_model_size) /* I - Size of buffer */ | |
307 | { | |
308 | char *attr, /* 1284 attribute */ | |
309 | *delim, /* 1284 delimiter */ | |
310 | *mfg, /* Manufacturer string */ | |
311 | *mdl; /* Model string */ | |
312 | ||
313 | ||
ed486911 | 314 | DEBUG_printf(("backendGetMakeModel(device_id=\"%s\", " |
89d46774 | 315 | "make_model=%p, make_model_size=%d)\n", device_id, |
316 | make_model, make_model_size)); | |
317 | ||
318 | /* | |
319 | * Range check input... | |
320 | */ | |
321 | ||
322 | if (!device_id || !*device_id || !make_model || make_model_size < 32) | |
323 | { | |
ed486911 | 324 | DEBUG_puts("backendGetMakeModel: Bad args!"); |
89d46774 | 325 | return (-1); |
326 | } | |
327 | ||
328 | *make_model = '\0'; | |
329 | ||
ef416fc2 | 330 | /* |
331 | * Look for the description field... | |
332 | */ | |
333 | ||
334 | if ((attr = strstr(device_id, "DES:")) != NULL) | |
335 | attr += 4; | |
336 | else if ((attr = strstr(device_id, "DESCRIPTION:")) != NULL) | |
337 | attr += 12; | |
338 | ||
339 | if (attr) | |
340 | { | |
341 | /* | |
342 | * Make sure the description contains something useful, since some | |
343 | * printer manufacturers (HP) apparently don't follow the standards | |
344 | * they helped to define... | |
345 | * | |
346 | * Here we require the description to be 8 or more characters in length, | |
347 | * containing at least one space and one letter. | |
348 | */ | |
349 | ||
350 | if ((delim = strchr(attr, ';')) == NULL) | |
351 | delim = attr + strlen(attr); | |
352 | ||
353 | if ((delim - attr) < 8) | |
354 | attr = NULL; | |
355 | else | |
356 | { | |
357 | char *ptr; /* Pointer into description */ | |
358 | int letters, /* Number of letters seen */ | |
359 | spaces; /* Number of spaces seen */ | |
360 | ||
361 | ||
362 | for (ptr = attr, letters = 0, spaces = 0; ptr < delim; ptr ++) | |
363 | { | |
364 | if (isspace(*ptr & 255)) | |
365 | spaces ++; | |
366 | else if (isalpha(*ptr & 255)) | |
367 | letters ++; | |
368 | ||
369 | if (spaces && letters) | |
370 | break; | |
371 | } | |
372 | ||
373 | if (!spaces || !letters) | |
374 | attr = NULL; | |
375 | } | |
376 | } | |
377 | ||
378 | if ((mfg = strstr(device_id, "MANUFACTURER:")) != NULL) | |
379 | mfg += 13; | |
89d46774 | 380 | else if ((mfg = strstr(device_id, "Manufacturer:")) != NULL) |
381 | mfg += 13; | |
ef416fc2 | 382 | else if ((mfg = strstr(device_id, "MFG:")) != NULL) |
383 | mfg += 4; | |
384 | ||
385 | if ((mdl = strstr(device_id, "MODEL:")) != NULL) | |
386 | mdl += 6; | |
89d46774 | 387 | else if ((mdl = strstr(device_id, "Model:")) != NULL) |
388 | mdl += 6; | |
ef416fc2 | 389 | else if ((mdl = strstr(device_id, "MDL:")) != NULL) |
390 | mdl += 4; | |
391 | ||
89d46774 | 392 | if (mdl) |
ef416fc2 | 393 | { |
394 | /* | |
89d46774 | 395 | * Build a make-model string from the manufacturer and model attributes... |
ef416fc2 | 396 | */ |
397 | ||
89d46774 | 398 | if (mfg) |
ef416fc2 | 399 | { |
89d46774 | 400 | if (!strncasecmp(mfg, "Hewlett-Packard", 15)) |
401 | strlcpy(make_model, "HP", make_model_size); | |
402 | else | |
403 | strlcpy(make_model, mfg, make_model_size); | |
ef416fc2 | 404 | |
89d46774 | 405 | if ((delim = strchr(make_model, ';')) != NULL) |
406 | *delim = '\0'; | |
407 | ||
408 | if (!strncasecmp(make_model, mdl, strlen(make_model))) | |
409 | { | |
410 | /* | |
411 | * Just copy model string, since it has the manufacturer... | |
412 | */ | |
413 | ||
414 | strlcpy(make_model, mdl, make_model_size); | |
415 | } | |
416 | else | |
417 | { | |
418 | /* | |
419 | * Concatenate the make and model... | |
420 | */ | |
421 | ||
422 | strlcat(make_model, " ", make_model_size); | |
423 | strlcat(make_model, mdl, make_model_size); | |
424 | } | |
ef416fc2 | 425 | } |
426 | else | |
427 | { | |
89d46774 | 428 | /* |
429 | * Just copy model string, since it has the manufacturer... | |
430 | */ | |
431 | ||
432 | strlcpy(make_model, mdl, make_model_size); | |
ef416fc2 | 433 | } |
434 | } | |
89d46774 | 435 | else if (attr) |
ef416fc2 | 436 | { |
437 | /* | |
89d46774 | 438 | * Use description... |
ef416fc2 | 439 | */ |
440 | ||
89d46774 | 441 | if (!strncasecmp(attr, "Hewlett-Packard hp ", 19)) |
ef416fc2 | 442 | { |
443 | /* | |
89d46774 | 444 | * Check for a common HP bug... |
ef416fc2 | 445 | */ |
446 | ||
89d46774 | 447 | strlcpy(make_model, "HP ", make_model_size); |
448 | strlcpy(make_model + 3, attr + 19, make_model_size - 3); | |
449 | } | |
450 | else if (!strncasecmp(attr, "Hewlett-Packard ", 16)) | |
451 | { | |
452 | strlcpy(make_model, "HP ", make_model_size); | |
453 | strlcpy(make_model + 3, attr + 16, make_model_size - 3); | |
ef416fc2 | 454 | } |
455 | else | |
456 | { | |
89d46774 | 457 | strlcpy(make_model, attr, make_model_size); |
ef416fc2 | 458 | } |
459 | } | |
460 | else | |
461 | { | |
462 | /* | |
463 | * Use "Unknown" as the printer make and model... | |
464 | */ | |
465 | ||
466 | strlcpy(make_model, "Unknown", make_model_size); | |
467 | } | |
468 | ||
89d46774 | 469 | /* |
470 | * Strip trailing data... | |
471 | */ | |
472 | ||
ef416fc2 | 473 | if ((delim = strchr(make_model, ';')) != NULL) |
474 | *delim = '\0'; | |
475 | ||
89d46774 | 476 | /* |
477 | * Strip trailing whitespace... | |
478 | */ | |
ef416fc2 | 479 | |
89d46774 | 480 | for (delim = make_model + strlen(make_model) - 1; delim >= make_model; delim --) |
481 | if (isspace(*delim & 255)) | |
482 | *delim = '\0'; | |
ef416fc2 | 483 | else |
89d46774 | 484 | break; |
ef416fc2 | 485 | |
89d46774 | 486 | /* |
487 | * Return... | |
488 | */ | |
ef416fc2 | 489 | |
89d46774 | 490 | if (make_model[0]) |
491 | return (0); | |
492 | else | |
493 | return (-1); | |
ef416fc2 | 494 | } |
495 | ||
496 | ||
497 | /* | |
f7faf1f5 | 498 | * End of "$Id: ieee1284.c 5591 2006-05-26 19:51:59Z mike $". |
ef416fc2 | 499 | */ |