]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
f7deaa1a | 2 | * "$Id: ieee1284.c 6293 2007-02-20 13:40:55Z mike $" |
ef416fc2 | 3 | * |
4 | * IEEE-1284 support functions for the Common UNIX Printing System (CUPS). | |
5 | * | |
f7deaa1a | 6 | * Copyright 1997-2007 by Easy Software Products, all rights reserved. |
ef416fc2 | 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 | ||
f7deaa1a | 96 | if (!device_id || device_id_size < 32) |
ef416fc2 | 97 | { |
ed486911 | 98 | DEBUG_puts("backendGetDeviceID: Bad args!"); |
ef416fc2 | 99 | return (-1); |
100 | } | |
101 | ||
f7deaa1a | 102 | if (make_model) |
103 | *make_model = '\0'; | |
ef416fc2 | 104 | |
105 | if (uri) | |
106 | *uri = '\0'; | |
107 | ||
f7deaa1a | 108 | if (fd >= 0) |
ef416fc2 | 109 | { |
110 | /* | |
f7deaa1a | 111 | * Get the device ID string... |
ef416fc2 | 112 | */ |
113 | ||
f7deaa1a | 114 | *device_id = '\0'; |
ef416fc2 | 115 | |
f7deaa1a | 116 | #ifdef __linux |
117 | if (!ioctl(fd, LPIOC_GET_DEVICE_ID(device_id_size), device_id)) | |
118 | { | |
119 | /* | |
120 | * Extract the length of the device ID string from the first two | |
121 | * bytes. The 1284 spec says the length is stored MSB first... | |
122 | */ | |
ef416fc2 | 123 | |
f7deaa1a | 124 | length = (((unsigned)device_id[0] & 255) << 8) + |
125 | ((unsigned)device_id[1] & 255); | |
ef416fc2 | 126 | |
f7deaa1a | 127 | /* |
128 | * Check to see if the length is larger than our buffer; first | |
129 | * assume that the vendor incorrectly implemented the 1284 spec, | |
130 | * and then limit the length to the size of our buffer... | |
131 | */ | |
ef416fc2 | 132 | |
f7deaa1a | 133 | if (length > (device_id_size - 2)) |
134 | length = (((unsigned)device_id[1] & 255) << 8) + | |
135 | ((unsigned)device_id[0] & 255); | |
ef416fc2 | 136 | |
f7deaa1a | 137 | if (length > (device_id_size - 2)) |
138 | length = device_id_size - 2; | |
139 | ||
140 | /* | |
141 | * Copy the device ID text to the beginning of the buffer and | |
142 | * nul-terminate. | |
143 | */ | |
144 | ||
145 | memmove(device_id, device_id + 2, length); | |
146 | device_id[length] = '\0'; | |
147 | } | |
ef416fc2 | 148 | # ifdef DEBUG |
f7deaa1a | 149 | else |
150 | printf("backendGetDeviceID: ioctl failed - %s\n", strerror(errno)); | |
ef416fc2 | 151 | # endif /* DEBUG */ |
152 | #endif /* __linux */ | |
153 | ||
154 | #if defined(__sun) && defined(ECPPIOC_GETDEVID) | |
f7deaa1a | 155 | did.mode = ECPP_CENTRONICS; |
156 | did.len = device_id_size - 1; | |
157 | did.rlen = 0; | |
158 | did.addr = device_id; | |
ef416fc2 | 159 | |
f7deaa1a | 160 | if (!ioctl(fd, ECPPIOC_GETDEVID, &did)) |
161 | { | |
162 | /* | |
163 | * Nul-terminate the device ID text. | |
164 | */ | |
ef416fc2 | 165 | |
f7deaa1a | 166 | if (did.rlen < (device_id_size - 1)) |
167 | device_id[did.rlen] = '\0'; | |
168 | else | |
169 | device_id[device_id_size - 1] = '\0'; | |
170 | } | |
ef416fc2 | 171 | # ifdef DEBUG |
f7deaa1a | 172 | else |
173 | printf("backendGetDeviceID: ioctl failed - %s\n", strerror(errno)); | |
ef416fc2 | 174 | # endif /* DEBUG */ |
175 | #endif /* __sun && ECPPIOC_GETDEVID */ | |
f7deaa1a | 176 | } |
ef416fc2 | 177 | |
ed486911 | 178 | DEBUG_printf(("backendGetDeviceID: device_id=\"%s\"\n", device_id)); |
ef416fc2 | 179 | |
180 | if (!*device_id) | |
181 | return (-1); | |
182 | ||
89d46774 | 183 | /* |
184 | * Get the make and model... | |
185 | */ | |
186 | ||
f7deaa1a | 187 | if (make_model) |
188 | backendGetMakeModel(device_id, make_model, make_model_size); | |
89d46774 | 189 | |
190 | /* | |
191 | * Then generate a device URI... | |
192 | */ | |
193 | ||
194 | if (scheme && uri && uri_size > 32) | |
195 | { | |
196 | /* | |
197 | * Look for the serial number field... | |
198 | */ | |
199 | ||
200 | if ((attr = strstr(device_id, "SERN:")) != NULL) | |
201 | attr += 5; | |
202 | else if ((attr = strstr(device_id, "SERIALNUMBER:")) != NULL) | |
203 | attr += 13; | |
204 | else if ((attr = strstr(device_id, ";SN:")) != NULL) | |
205 | attr += 4; | |
206 | ||
207 | if (attr) | |
208 | { | |
209 | strlcpy(serial_number, attr, sizeof(serial_number)); | |
210 | ||
211 | if ((delim = strchr(serial_number, ';')) != NULL) | |
212 | *delim = '\0'; | |
213 | } | |
214 | else | |
215 | serial_number[0] = '\0'; | |
216 | ||
217 | /* | |
218 | * Generate the device URI from the manufacturer, make_model, and | |
219 | * serial number strings. | |
220 | */ | |
221 | ||
222 | snprintf(uri, uri_size, "%s://", scheme); | |
223 | ||
224 | if ((attr = strstr(device_id, "MANUFACTURER:")) != NULL) | |
225 | attr += 13; | |
226 | else if ((attr = strstr(device_id, "Manufacturer:")) != NULL) | |
227 | attr += 13; | |
228 | else if ((attr = strstr(device_id, "MFG:")) != NULL) | |
229 | attr += 4; | |
230 | ||
231 | if (attr) | |
232 | { | |
233 | strlcpy(manufacturer, attr, sizeof(manufacturer)); | |
234 | ||
235 | if ((delim = strchr(manufacturer, ';')) != NULL) | |
236 | *delim = '\0'; | |
237 | ||
238 | if (!strcasecmp(manufacturer, "Hewlett-Packard")) | |
239 | strcpy(manufacturer, "HP"); | |
d09495fa | 240 | else if (!strcasecmp(manufacturer, "Lexmark International")) |
241 | strcpy(manufacturer, "Lexmark"); | |
89d46774 | 242 | } |
243 | else | |
244 | { | |
245 | strlcpy(manufacturer, make_model, sizeof(manufacturer)); | |
246 | ||
247 | if ((delim = strchr(manufacturer, ' ')) != NULL) | |
248 | *delim = '\0'; | |
249 | } | |
250 | ||
251 | manulen = strlen(manufacturer); | |
252 | ||
253 | for (uriptr = uri + strlen(uri), delim = manufacturer; | |
254 | *delim && uriptr < (uri + uri_size - 3); | |
255 | delim ++) | |
256 | if (*delim == ' ') | |
257 | { | |
258 | *uriptr++ = '%'; | |
259 | *uriptr++ = '2'; | |
260 | *uriptr++ = '0'; | |
261 | } | |
262 | else | |
263 | *uriptr++ = *delim; | |
264 | ||
265 | *uriptr++ = '/'; | |
266 | ||
267 | if (!strncasecmp(make_model, manufacturer, manulen)) | |
268 | { | |
269 | delim = make_model + manulen; | |
270 | ||
271 | while (isspace(*delim & 255)) | |
272 | delim ++; | |
273 | } | |
274 | else | |
275 | delim = make_model; | |
276 | ||
277 | for (; *delim && uriptr < (uri + uri_size - 3); delim ++) | |
278 | if (*delim == ' ') | |
279 | { | |
280 | *uriptr++ = '%'; | |
281 | *uriptr++ = '2'; | |
282 | *uriptr++ = '0'; | |
283 | } | |
284 | else | |
285 | *uriptr++ = *delim; | |
286 | ||
287 | if (serial_number[0]) | |
288 | { | |
289 | /* | |
290 | * Add the serial number to the URI... | |
291 | */ | |
292 | ||
293 | strlcpy(uriptr, "?serial=", uri_size - (uriptr - uri)); | |
294 | strlcat(uriptr, serial_number, uri_size - (uriptr - uri)); | |
295 | } | |
296 | else | |
297 | *uriptr = '\0'; | |
298 | } | |
299 | ||
300 | return (0); | |
301 | } | |
89d46774 | 302 | |
303 | ||
304 | /* | |
ed486911 | 305 | * 'backendGetMakeModel()' - Get the make and model string from the device ID. |
89d46774 | 306 | */ |
307 | ||
308 | int /* O - 0 on success, -1 on failure */ | |
ed486911 | 309 | backendGetMakeModel( |
89d46774 | 310 | const char *device_id, /* O - 1284 device ID */ |
311 | char *make_model, /* O - Make/model */ | |
312 | int make_model_size) /* I - Size of buffer */ | |
313 | { | |
314 | char *attr, /* 1284 attribute */ | |
315 | *delim, /* 1284 delimiter */ | |
316 | *mfg, /* Manufacturer string */ | |
317 | *mdl; /* Model string */ | |
318 | ||
319 | ||
ed486911 | 320 | DEBUG_printf(("backendGetMakeModel(device_id=\"%s\", " |
89d46774 | 321 | "make_model=%p, make_model_size=%d)\n", device_id, |
322 | make_model, make_model_size)); | |
323 | ||
324 | /* | |
325 | * Range check input... | |
326 | */ | |
327 | ||
328 | if (!device_id || !*device_id || !make_model || make_model_size < 32) | |
329 | { | |
ed486911 | 330 | DEBUG_puts("backendGetMakeModel: Bad args!"); |
89d46774 | 331 | return (-1); |
332 | } | |
333 | ||
334 | *make_model = '\0'; | |
335 | ||
ef416fc2 | 336 | /* |
337 | * Look for the description field... | |
338 | */ | |
339 | ||
340 | if ((attr = strstr(device_id, "DES:")) != NULL) | |
341 | attr += 4; | |
342 | else if ((attr = strstr(device_id, "DESCRIPTION:")) != NULL) | |
343 | attr += 12; | |
344 | ||
345 | if (attr) | |
346 | { | |
347 | /* | |
348 | * Make sure the description contains something useful, since some | |
349 | * printer manufacturers (HP) apparently don't follow the standards | |
350 | * they helped to define... | |
351 | * | |
352 | * Here we require the description to be 8 or more characters in length, | |
353 | * containing at least one space and one letter. | |
354 | */ | |
355 | ||
356 | if ((delim = strchr(attr, ';')) == NULL) | |
357 | delim = attr + strlen(attr); | |
358 | ||
359 | if ((delim - attr) < 8) | |
360 | attr = NULL; | |
361 | else | |
362 | { | |
363 | char *ptr; /* Pointer into description */ | |
364 | int letters, /* Number of letters seen */ | |
365 | spaces; /* Number of spaces seen */ | |
366 | ||
367 | ||
368 | for (ptr = attr, letters = 0, spaces = 0; ptr < delim; ptr ++) | |
369 | { | |
370 | if (isspace(*ptr & 255)) | |
371 | spaces ++; | |
372 | else if (isalpha(*ptr & 255)) | |
373 | letters ++; | |
374 | ||
375 | if (spaces && letters) | |
376 | break; | |
377 | } | |
378 | ||
379 | if (!spaces || !letters) | |
380 | attr = NULL; | |
381 | } | |
382 | } | |
383 | ||
384 | if ((mfg = strstr(device_id, "MANUFACTURER:")) != NULL) | |
385 | mfg += 13; | |
89d46774 | 386 | else if ((mfg = strstr(device_id, "Manufacturer:")) != NULL) |
387 | mfg += 13; | |
ef416fc2 | 388 | else if ((mfg = strstr(device_id, "MFG:")) != NULL) |
389 | mfg += 4; | |
390 | ||
391 | if ((mdl = strstr(device_id, "MODEL:")) != NULL) | |
392 | mdl += 6; | |
89d46774 | 393 | else if ((mdl = strstr(device_id, "Model:")) != NULL) |
394 | mdl += 6; | |
ef416fc2 | 395 | else if ((mdl = strstr(device_id, "MDL:")) != NULL) |
396 | mdl += 4; | |
397 | ||
89d46774 | 398 | if (mdl) |
ef416fc2 | 399 | { |
400 | /* | |
89d46774 | 401 | * Build a make-model string from the manufacturer and model attributes... |
ef416fc2 | 402 | */ |
403 | ||
89d46774 | 404 | if (mfg) |
ef416fc2 | 405 | { |
89d46774 | 406 | if (!strncasecmp(mfg, "Hewlett-Packard", 15)) |
407 | strlcpy(make_model, "HP", make_model_size); | |
d09495fa | 408 | else if (!strncasecmp(mfg, "Lexmark International", 21)) |
409 | strlcpy(make_model, "Lexmark", make_model_size); | |
89d46774 | 410 | else |
411 | strlcpy(make_model, mfg, make_model_size); | |
ef416fc2 | 412 | |
89d46774 | 413 | if ((delim = strchr(make_model, ';')) != NULL) |
414 | *delim = '\0'; | |
415 | ||
416 | if (!strncasecmp(make_model, mdl, strlen(make_model))) | |
417 | { | |
418 | /* | |
419 | * Just copy model string, since it has the manufacturer... | |
420 | */ | |
421 | ||
422 | strlcpy(make_model, mdl, make_model_size); | |
423 | } | |
424 | else | |
425 | { | |
426 | /* | |
427 | * Concatenate the make and model... | |
428 | */ | |
429 | ||
430 | strlcat(make_model, " ", make_model_size); | |
431 | strlcat(make_model, mdl, make_model_size); | |
432 | } | |
ef416fc2 | 433 | } |
434 | else | |
435 | { | |
89d46774 | 436 | /* |
437 | * Just copy model string, since it has the manufacturer... | |
438 | */ | |
439 | ||
440 | strlcpy(make_model, mdl, make_model_size); | |
ef416fc2 | 441 | } |
442 | } | |
89d46774 | 443 | else if (attr) |
ef416fc2 | 444 | { |
445 | /* | |
89d46774 | 446 | * Use description... |
ef416fc2 | 447 | */ |
448 | ||
89d46774 | 449 | if (!strncasecmp(attr, "Hewlett-Packard hp ", 19)) |
ef416fc2 | 450 | { |
451 | /* | |
89d46774 | 452 | * Check for a common HP bug... |
ef416fc2 | 453 | */ |
454 | ||
89d46774 | 455 | strlcpy(make_model, "HP ", make_model_size); |
456 | strlcpy(make_model + 3, attr + 19, make_model_size - 3); | |
457 | } | |
458 | else if (!strncasecmp(attr, "Hewlett-Packard ", 16)) | |
459 | { | |
460 | strlcpy(make_model, "HP ", make_model_size); | |
461 | strlcpy(make_model + 3, attr + 16, make_model_size - 3); | |
ef416fc2 | 462 | } |
463 | else | |
464 | { | |
89d46774 | 465 | strlcpy(make_model, attr, make_model_size); |
ef416fc2 | 466 | } |
467 | } | |
468 | else | |
469 | { | |
470 | /* | |
471 | * Use "Unknown" as the printer make and model... | |
472 | */ | |
473 | ||
474 | strlcpy(make_model, "Unknown", make_model_size); | |
475 | } | |
476 | ||
89d46774 | 477 | /* |
478 | * Strip trailing data... | |
479 | */ | |
480 | ||
ef416fc2 | 481 | if ((delim = strchr(make_model, ';')) != NULL) |
482 | *delim = '\0'; | |
483 | ||
89d46774 | 484 | /* |
485 | * Strip trailing whitespace... | |
486 | */ | |
ef416fc2 | 487 | |
89d46774 | 488 | for (delim = make_model + strlen(make_model) - 1; delim >= make_model; delim --) |
489 | if (isspace(*delim & 255)) | |
490 | *delim = '\0'; | |
ef416fc2 | 491 | else |
89d46774 | 492 | break; |
ef416fc2 | 493 | |
89d46774 | 494 | /* |
495 | * Return... | |
496 | */ | |
ef416fc2 | 497 | |
89d46774 | 498 | if (make_model[0]) |
499 | return (0); | |
500 | else | |
501 | return (-1); | |
ef416fc2 | 502 | } |
503 | ||
504 | ||
505 | /* | |
f7deaa1a | 506 | * End of "$Id: ieee1284.c 6293 2007-02-20 13:40:55Z mike $". |
ef416fc2 | 507 | */ |