]> git.ipfire.org Git - thirdparty/cups.git/blob - backend/ieee1284.c
f5414936742c0b9ca0feee7bd345c749ff9eb7c6
[thirdparty/cups.git] / backend / ieee1284.c
1 /*
2 * "$Id: ieee1284.c 5866 2006-08-23 03:03:49Z mike $"
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 *
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.
31 */
32
33 /*
34 * Include necessary headers.
35 */
36
37 #include "backend-private.h"
38
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 */
45
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 */
54
55
56 /*
57 * 'backendGetDeviceID()' - Get the IEEE-1284 device ID string and
58 * corresponding URI.
59 */
60
61 int /* O - 0 on success, -1 on failure */
62 backendGetDeviceID(
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 */
75 manufacturer[256], /* Manufacturer string */
76 serial_number[1024]; /* Serial number string */
77 int manulen; /* Length of manufacturer string */
78 #ifdef __linux
79 int length; /* Length of device ID info */
80 #endif /* __linux */
81 #if defined(__sun) && defined(ECPPIOC_GETDEVID)
82 struct ecpp_device_id did; /* Device ID buffer */
83 #endif /* __sun && ECPPIOC_GETDEVID */
84
85
86 DEBUG_printf(("backendGetDeviceID(fd=%d, device_id=%p, device_id_size=%d, "
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 {
100 DEBUG_puts("backendGetDeviceID: Bad args!");
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
148 printf("backendGetDeviceID: ioctl failed - %s\n", strerror(errno));
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
171 printf("backendGetDeviceID: ioctl failed - %s\n", strerror(errno));
172 # endif /* DEBUG */
173 #endif /* __sun && ECPPIOC_GETDEVID */
174
175 DEBUG_printf(("backendGetDeviceID: device_id=\"%s\"\n", device_id));
176
177 if (!*device_id)
178 return (-1);
179
180 /*
181 * Get the make and model...
182 */
183
184 backendGetMakeModel(device_id, make_model, make_model_size);
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 else if (!strcasecmp(manufacturer, "Lexmark International"))
237 strcpy(manufacturer, "Lexmark");
238 }
239 else
240 {
241 strlcpy(manufacturer, make_model, sizeof(manufacturer));
242
243 if ((delim = strchr(manufacturer, ' ')) != NULL)
244 *delim = '\0';
245 }
246
247 manulen = strlen(manufacturer);
248
249 for (uriptr = uri + strlen(uri), delim = manufacturer;
250 *delim && uriptr < (uri + uri_size - 3);
251 delim ++)
252 if (*delim == ' ')
253 {
254 *uriptr++ = '%';
255 *uriptr++ = '2';
256 *uriptr++ = '0';
257 }
258 else
259 *uriptr++ = *delim;
260
261 *uriptr++ = '/';
262
263 if (!strncasecmp(make_model, manufacturer, manulen))
264 {
265 delim = make_model + manulen;
266
267 while (isspace(*delim & 255))
268 delim ++;
269 }
270 else
271 delim = make_model;
272
273 for (; *delim && uriptr < (uri + uri_size - 3); delim ++)
274 if (*delim == ' ')
275 {
276 *uriptr++ = '%';
277 *uriptr++ = '2';
278 *uriptr++ = '0';
279 }
280 else
281 *uriptr++ = *delim;
282
283 if (serial_number[0])
284 {
285 /*
286 * Add the serial number to the URI...
287 */
288
289 strlcpy(uriptr, "?serial=", uri_size - (uriptr - uri));
290 strlcat(uriptr, serial_number, uri_size - (uriptr - uri));
291 }
292 else
293 *uriptr = '\0';
294 }
295
296 return (0);
297 }
298
299
300 /*
301 * 'backendGetMakeModel()' - Get the make and model string from the device ID.
302 */
303
304 int /* O - 0 on success, -1 on failure */
305 backendGetMakeModel(
306 const char *device_id, /* O - 1284 device ID */
307 char *make_model, /* O - Make/model */
308 int make_model_size) /* I - Size of buffer */
309 {
310 char *attr, /* 1284 attribute */
311 *delim, /* 1284 delimiter */
312 *mfg, /* Manufacturer string */
313 *mdl; /* Model string */
314
315
316 DEBUG_printf(("backendGetMakeModel(device_id=\"%s\", "
317 "make_model=%p, make_model_size=%d)\n", device_id,
318 make_model, make_model_size));
319
320 /*
321 * Range check input...
322 */
323
324 if (!device_id || !*device_id || !make_model || make_model_size < 32)
325 {
326 DEBUG_puts("backendGetMakeModel: Bad args!");
327 return (-1);
328 }
329
330 *make_model = '\0';
331
332 /*
333 * Look for the description field...
334 */
335
336 if ((attr = strstr(device_id, "DES:")) != NULL)
337 attr += 4;
338 else if ((attr = strstr(device_id, "DESCRIPTION:")) != NULL)
339 attr += 12;
340
341 if (attr)
342 {
343 /*
344 * Make sure the description contains something useful, since some
345 * printer manufacturers (HP) apparently don't follow the standards
346 * they helped to define...
347 *
348 * Here we require the description to be 8 or more characters in length,
349 * containing at least one space and one letter.
350 */
351
352 if ((delim = strchr(attr, ';')) == NULL)
353 delim = attr + strlen(attr);
354
355 if ((delim - attr) < 8)
356 attr = NULL;
357 else
358 {
359 char *ptr; /* Pointer into description */
360 int letters, /* Number of letters seen */
361 spaces; /* Number of spaces seen */
362
363
364 for (ptr = attr, letters = 0, spaces = 0; ptr < delim; ptr ++)
365 {
366 if (isspace(*ptr & 255))
367 spaces ++;
368 else if (isalpha(*ptr & 255))
369 letters ++;
370
371 if (spaces && letters)
372 break;
373 }
374
375 if (!spaces || !letters)
376 attr = NULL;
377 }
378 }
379
380 if ((mfg = strstr(device_id, "MANUFACTURER:")) != NULL)
381 mfg += 13;
382 else if ((mfg = strstr(device_id, "Manufacturer:")) != NULL)
383 mfg += 13;
384 else if ((mfg = strstr(device_id, "MFG:")) != NULL)
385 mfg += 4;
386
387 if ((mdl = strstr(device_id, "MODEL:")) != NULL)
388 mdl += 6;
389 else if ((mdl = strstr(device_id, "Model:")) != NULL)
390 mdl += 6;
391 else if ((mdl = strstr(device_id, "MDL:")) != NULL)
392 mdl += 4;
393
394 if (mdl)
395 {
396 /*
397 * Build a make-model string from the manufacturer and model attributes...
398 */
399
400 if (mfg)
401 {
402 if (!strncasecmp(mfg, "Hewlett-Packard", 15))
403 strlcpy(make_model, "HP", make_model_size);
404 else if (!strncasecmp(mfg, "Lexmark International", 21))
405 strlcpy(make_model, "Lexmark", make_model_size);
406 else
407 strlcpy(make_model, mfg, make_model_size);
408
409 if ((delim = strchr(make_model, ';')) != NULL)
410 *delim = '\0';
411
412 if (!strncasecmp(make_model, mdl, strlen(make_model)))
413 {
414 /*
415 * Just copy model string, since it has the manufacturer...
416 */
417
418 strlcpy(make_model, mdl, make_model_size);
419 }
420 else
421 {
422 /*
423 * Concatenate the make and model...
424 */
425
426 strlcat(make_model, " ", make_model_size);
427 strlcat(make_model, mdl, make_model_size);
428 }
429 }
430 else
431 {
432 /*
433 * Just copy model string, since it has the manufacturer...
434 */
435
436 strlcpy(make_model, mdl, make_model_size);
437 }
438 }
439 else if (attr)
440 {
441 /*
442 * Use description...
443 */
444
445 if (!strncasecmp(attr, "Hewlett-Packard hp ", 19))
446 {
447 /*
448 * Check for a common HP bug...
449 */
450
451 strlcpy(make_model, "HP ", make_model_size);
452 strlcpy(make_model + 3, attr + 19, make_model_size - 3);
453 }
454 else if (!strncasecmp(attr, "Hewlett-Packard ", 16))
455 {
456 strlcpy(make_model, "HP ", make_model_size);
457 strlcpy(make_model + 3, attr + 16, make_model_size - 3);
458 }
459 else
460 {
461 strlcpy(make_model, attr, make_model_size);
462 }
463 }
464 else
465 {
466 /*
467 * Use "Unknown" as the printer make and model...
468 */
469
470 strlcpy(make_model, "Unknown", make_model_size);
471 }
472
473 /*
474 * Strip trailing data...
475 */
476
477 if ((delim = strchr(make_model, ';')) != NULL)
478 *delim = '\0';
479
480 /*
481 * Strip trailing whitespace...
482 */
483
484 for (delim = make_model + strlen(make_model) - 1; delim >= make_model; delim --)
485 if (isspace(*delim & 255))
486 *delim = '\0';
487 else
488 break;
489
490 /*
491 * Return...
492 */
493
494 if (make_model[0])
495 return (0);
496 else
497 return (-1);
498 }
499
500
501 /*
502 * End of "$Id: ieee1284.c 5866 2006-08-23 03:03:49Z mike $".
503 */