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