]>
git.ipfire.org Git - thirdparty/cups.git/blob - backend/ieee1284.c
2 * "$Id: ieee1284.c 6293 2007-02-20 13:40:55Z mike $"
4 * IEEE-1284 support functions for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
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
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
24 * This file is subject to the Apple OS-Developed Software exception.
28 * backendGetDeviceID() - Get the IEEE-1284 device ID string and
30 * backendGetMakeModel() - Get the make and model string from the device ID.
34 * Include necessary headers.
37 #include "backend-private.h"
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)
48 # include <sys/ecppio.h>
50 # include <sys/ioccom.h>
51 # include <sys/ecppsys.h>
57 * 'backendGetDeviceID()' - Get the IEEE-1284 device ID string and
61 int /* O - 0 on success, -1 on failure */
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 */
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 */
79 int length
; /* Length of device ID info */
81 #if defined(__sun) && defined(ECPPIOC_GETDEVID)
82 struct ecpp_device_id did
; /* Device ID buffer */
83 #endif /* __sun && ECPPIOC_GETDEVID */
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)",
93 * Range check input...
96 if (!device_id
|| device_id_size
< 32)
98 DEBUG_puts("backendGetDeviceID: Bad args!");
111 * Get the device ID string...
117 if (!ioctl(fd
, LPIOC_GET_DEVICE_ID(device_id_size
), device_id
))
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...
124 length
= (((unsigned)device_id
[0] & 255) << 8) +
125 ((unsigned)device_id
[1] & 255);
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...
133 if (length
> (device_id_size
- 2))
134 length
= (((unsigned)device_id
[1] & 255) << 8) +
135 ((unsigned)device_id
[0] & 255);
137 if (length
> (device_id_size
- 2))
138 length
= device_id_size
- 2;
141 * Copy the device ID text to the beginning of the buffer and
145 memmove(device_id
, device_id
+ 2, length
);
146 device_id
[length
] = '\0';
150 printf("backendGetDeviceID: ioctl failed - %s\n", strerror(errno
));
154 #if defined(__sun) && defined(ECPPIOC_GETDEVID)
155 did
.mode
= ECPP_CENTRONICS
;
156 did
.len
= device_id_size
- 1;
158 did
.addr
= device_id
;
160 if (!ioctl(fd
, ECPPIOC_GETDEVID
, &did
))
163 * Nul-terminate the device ID text.
166 if (did
.rlen
< (device_id_size
- 1))
167 device_id
[did
.rlen
] = '\0';
169 device_id
[device_id_size
- 1] = '\0';
173 printf("backendGetDeviceID: ioctl failed - %s\n", strerror(errno
));
175 #endif /* __sun && ECPPIOC_GETDEVID */
178 DEBUG_printf(("backendGetDeviceID: device_id=\"%s\"\n", device_id
));
184 * Get the make and model...
188 backendGetMakeModel(device_id
, make_model
, make_model_size
);
191 * Then generate a device URI...
194 if (scheme
&& uri
&& uri_size
> 32)
197 * Look for the serial number field...
200 if ((attr
= strstr(device_id
, "SERN:")) != NULL
)
202 else if ((attr
= strstr(device_id
, "SERIALNUMBER:")) != NULL
)
204 else if ((attr
= strstr(device_id
, ";SN:")) != NULL
)
209 strlcpy(serial_number
, attr
, sizeof(serial_number
));
211 if ((delim
= strchr(serial_number
, ';')) != NULL
)
215 serial_number
[0] = '\0';
218 * Generate the device URI from the manufacturer, make_model, and
219 * serial number strings.
222 snprintf(uri
, uri_size
, "%s://", scheme
);
224 if ((attr
= strstr(device_id
, "MANUFACTURER:")) != NULL
)
226 else if ((attr
= strstr(device_id
, "Manufacturer:")) != NULL
)
228 else if ((attr
= strstr(device_id
, "MFG:")) != NULL
)
233 strlcpy(manufacturer
, attr
, sizeof(manufacturer
));
235 if ((delim
= strchr(manufacturer
, ';')) != NULL
)
238 if (!strcasecmp(manufacturer
, "Hewlett-Packard"))
239 strcpy(manufacturer
, "HP");
240 else if (!strcasecmp(manufacturer
, "Lexmark International"))
241 strcpy(manufacturer
, "Lexmark");
245 strlcpy(manufacturer
, make_model
, sizeof(manufacturer
));
247 if ((delim
= strchr(manufacturer
, ' ')) != NULL
)
251 manulen
= strlen(manufacturer
);
253 for (uriptr
= uri
+ strlen(uri
), delim
= manufacturer
;
254 *delim
&& uriptr
< (uri
+ uri_size
- 3);
267 if (!strncasecmp(make_model
, manufacturer
, manulen
))
269 delim
= make_model
+ manulen
;
271 while (isspace(*delim
& 255))
277 for (; *delim
&& uriptr
< (uri
+ uri_size
- 3); delim
++)
287 if (serial_number
[0])
290 * Add the serial number to the URI...
293 strlcpy(uriptr
, "?serial=", uri_size
- (uriptr
- uri
));
294 strlcat(uriptr
, serial_number
, uri_size
- (uriptr
- uri
));
305 * 'backendGetMakeModel()' - Get the make and model string from the device ID.
308 int /* O - 0 on success, -1 on failure */
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 */
314 char *attr
, /* 1284 attribute */
315 *delim
, /* 1284 delimiter */
316 *mfg
, /* Manufacturer string */
317 *mdl
; /* Model string */
320 DEBUG_printf(("backendGetMakeModel(device_id=\"%s\", "
321 "make_model=%p, make_model_size=%d)\n", device_id
,
322 make_model
, make_model_size
));
325 * Range check input...
328 if (!device_id
|| !*device_id
|| !make_model
|| make_model_size
< 32)
330 DEBUG_puts("backendGetMakeModel: Bad args!");
337 * Look for the description field...
340 if ((attr
= strstr(device_id
, "DES:")) != NULL
)
342 else if ((attr
= strstr(device_id
, "DESCRIPTION:")) != NULL
)
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...
352 * Here we require the description to be 8 or more characters in length,
353 * containing at least one space and one letter.
356 if ((delim
= strchr(attr
, ';')) == NULL
)
357 delim
= attr
+ strlen(attr
);
359 if ((delim
- attr
) < 8)
363 char *ptr
; /* Pointer into description */
364 int letters
, /* Number of letters seen */
365 spaces
; /* Number of spaces seen */
368 for (ptr
= attr
, letters
= 0, spaces
= 0; ptr
< delim
; ptr
++)
370 if (isspace(*ptr
& 255))
372 else if (isalpha(*ptr
& 255))
375 if (spaces
&& letters
)
379 if (!spaces
|| !letters
)
384 if ((mfg
= strstr(device_id
, "MANUFACTURER:")) != NULL
)
386 else if ((mfg
= strstr(device_id
, "Manufacturer:")) != NULL
)
388 else if ((mfg
= strstr(device_id
, "MFG:")) != NULL
)
391 if ((mdl
= strstr(device_id
, "MODEL:")) != NULL
)
393 else if ((mdl
= strstr(device_id
, "Model:")) != NULL
)
395 else if ((mdl
= strstr(device_id
, "MDL:")) != NULL
)
401 * Build a make-model string from the manufacturer and model attributes...
406 if (!strncasecmp(mfg
, "Hewlett-Packard", 15))
407 strlcpy(make_model
, "HP", make_model_size
);
408 else if (!strncasecmp(mfg
, "Lexmark International", 21))
409 strlcpy(make_model
, "Lexmark", make_model_size
);
411 strlcpy(make_model
, mfg
, make_model_size
);
413 if ((delim
= strchr(make_model
, ';')) != NULL
)
416 if (!strncasecmp(make_model
, mdl
, strlen(make_model
)))
419 * Just copy model string, since it has the manufacturer...
422 strlcpy(make_model
, mdl
, make_model_size
);
427 * Concatenate the make and model...
430 strlcat(make_model
, " ", make_model_size
);
431 strlcat(make_model
, mdl
, make_model_size
);
437 * Just copy model string, since it has the manufacturer...
440 strlcpy(make_model
, mdl
, make_model_size
);
449 if (!strncasecmp(attr
, "Hewlett-Packard hp ", 19))
452 * Check for a common HP bug...
455 strlcpy(make_model
, "HP ", make_model_size
);
456 strlcpy(make_model
+ 3, attr
+ 19, make_model_size
- 3);
458 else if (!strncasecmp(attr
, "Hewlett-Packard ", 16))
460 strlcpy(make_model
, "HP ", make_model_size
);
461 strlcpy(make_model
+ 3, attr
+ 16, make_model_size
- 3);
465 strlcpy(make_model
, attr
, make_model_size
);
471 * Use "Unknown" as the printer make and model...
474 strlcpy(make_model
, "Unknown", make_model_size
);
478 * Strip trailing data...
481 if ((delim
= strchr(make_model
, ';')) != NULL
)
485 * Strip trailing whitespace...
488 for (delim
= make_model
+ strlen(make_model
) - 1; delim
>= make_model
; delim
--)
489 if (isspace(*delim
& 255))
506 * End of "$Id: ieee1284.c 6293 2007-02-20 13:40:55Z mike $".