]>
git.ipfire.org Git - thirdparty/cups.git/blob - backend/ieee1284.c
f5414936742c0b9ca0feee7bd345c749ff9eb7c6
2 * "$Id: ieee1284.c 5866 2006-08-23 03:03:49Z mike $"
4 * IEEE-1284 support functions for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2006 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...
97 !device_id
|| device_id_size
< 32 ||
98 !make_model
|| make_model_size
< 32)
100 DEBUG_puts("backendGetDeviceID: Bad args!");
111 * Get the device ID string...
115 if (!ioctl(fd
, LPIOC_GET_DEVICE_ID(device_id_size
), device_id
))
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...
122 length
= (((unsigned)device_id
[0] & 255) << 8) +
123 ((unsigned)device_id
[1] & 255);
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...
131 if (length
> (device_id_size
- 2))
132 length
= (((unsigned)device_id
[1] & 255) << 8) +
133 ((unsigned)device_id
[0] & 255);
135 if (length
> (device_id_size
- 2))
136 length
= device_id_size
- 2;
139 * Copy the device ID text to the beginning of the buffer and
143 memmove(device_id
, device_id
+ 2, length
);
144 device_id
[length
] = '\0';
148 printf("backendGetDeviceID: ioctl failed - %s\n", strerror(errno
));
152 #if defined(__sun) && defined(ECPPIOC_GETDEVID)
153 did
.mode
= ECPP_CENTRONICS
;
154 did
.len
= device_id_size
- 1;
156 did
.addr
= device_id
;
158 if (!ioctl(fd
, ECPPIOC_GETDEVID
, &did
))
161 * Nul-terminate the device ID text.
164 if (did
.rlen
< (device_id_size
- 1))
165 device_id
[did
.rlen
] = '\0';
167 device_id
[device_id_size
- 1] = '\0';
171 printf("backendGetDeviceID: ioctl failed - %s\n", strerror(errno
));
173 #endif /* __sun && ECPPIOC_GETDEVID */
175 DEBUG_printf(("backendGetDeviceID: device_id=\"%s\"\n", device_id
));
181 * Get the make and model...
184 backendGetMakeModel(device_id
, make_model
, make_model_size
);
187 * Then generate a device URI...
190 if (scheme
&& uri
&& uri_size
> 32)
193 * Look for the serial number field...
196 if ((attr
= strstr(device_id
, "SERN:")) != NULL
)
198 else if ((attr
= strstr(device_id
, "SERIALNUMBER:")) != NULL
)
200 else if ((attr
= strstr(device_id
, ";SN:")) != NULL
)
205 strlcpy(serial_number
, attr
, sizeof(serial_number
));
207 if ((delim
= strchr(serial_number
, ';')) != NULL
)
211 serial_number
[0] = '\0';
214 * Generate the device URI from the manufacturer, make_model, and
215 * serial number strings.
218 snprintf(uri
, uri_size
, "%s://", scheme
);
220 if ((attr
= strstr(device_id
, "MANUFACTURER:")) != NULL
)
222 else if ((attr
= strstr(device_id
, "Manufacturer:")) != NULL
)
224 else if ((attr
= strstr(device_id
, "MFG:")) != NULL
)
229 strlcpy(manufacturer
, attr
, sizeof(manufacturer
));
231 if ((delim
= strchr(manufacturer
, ';')) != NULL
)
234 if (!strcasecmp(manufacturer
, "Hewlett-Packard"))
235 strcpy(manufacturer
, "HP");
236 else if (!strcasecmp(manufacturer
, "Lexmark International"))
237 strcpy(manufacturer
, "Lexmark");
241 strlcpy(manufacturer
, make_model
, sizeof(manufacturer
));
243 if ((delim
= strchr(manufacturer
, ' ')) != NULL
)
247 manulen
= strlen(manufacturer
);
249 for (uriptr
= uri
+ strlen(uri
), delim
= manufacturer
;
250 *delim
&& uriptr
< (uri
+ uri_size
- 3);
263 if (!strncasecmp(make_model
, manufacturer
, manulen
))
265 delim
= make_model
+ manulen
;
267 while (isspace(*delim
& 255))
273 for (; *delim
&& uriptr
< (uri
+ uri_size
- 3); delim
++)
283 if (serial_number
[0])
286 * Add the serial number to the URI...
289 strlcpy(uriptr
, "?serial=", uri_size
- (uriptr
- uri
));
290 strlcat(uriptr
, serial_number
, uri_size
- (uriptr
- uri
));
301 * 'backendGetMakeModel()' - Get the make and model string from the device ID.
304 int /* O - 0 on success, -1 on failure */
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 */
310 char *attr
, /* 1284 attribute */
311 *delim
, /* 1284 delimiter */
312 *mfg
, /* Manufacturer string */
313 *mdl
; /* Model string */
316 DEBUG_printf(("backendGetMakeModel(device_id=\"%s\", "
317 "make_model=%p, make_model_size=%d)\n", device_id
,
318 make_model
, make_model_size
));
321 * Range check input...
324 if (!device_id
|| !*device_id
|| !make_model
|| make_model_size
< 32)
326 DEBUG_puts("backendGetMakeModel: Bad args!");
333 * Look for the description field...
336 if ((attr
= strstr(device_id
, "DES:")) != NULL
)
338 else if ((attr
= strstr(device_id
, "DESCRIPTION:")) != NULL
)
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...
348 * Here we require the description to be 8 or more characters in length,
349 * containing at least one space and one letter.
352 if ((delim
= strchr(attr
, ';')) == NULL
)
353 delim
= attr
+ strlen(attr
);
355 if ((delim
- attr
) < 8)
359 char *ptr
; /* Pointer into description */
360 int letters
, /* Number of letters seen */
361 spaces
; /* Number of spaces seen */
364 for (ptr
= attr
, letters
= 0, spaces
= 0; ptr
< delim
; ptr
++)
366 if (isspace(*ptr
& 255))
368 else if (isalpha(*ptr
& 255))
371 if (spaces
&& letters
)
375 if (!spaces
|| !letters
)
380 if ((mfg
= strstr(device_id
, "MANUFACTURER:")) != NULL
)
382 else if ((mfg
= strstr(device_id
, "Manufacturer:")) != NULL
)
384 else if ((mfg
= strstr(device_id
, "MFG:")) != NULL
)
387 if ((mdl
= strstr(device_id
, "MODEL:")) != NULL
)
389 else if ((mdl
= strstr(device_id
, "Model:")) != NULL
)
391 else if ((mdl
= strstr(device_id
, "MDL:")) != NULL
)
397 * Build a make-model string from the manufacturer and model attributes...
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
);
407 strlcpy(make_model
, mfg
, make_model_size
);
409 if ((delim
= strchr(make_model
, ';')) != NULL
)
412 if (!strncasecmp(make_model
, mdl
, strlen(make_model
)))
415 * Just copy model string, since it has the manufacturer...
418 strlcpy(make_model
, mdl
, make_model_size
);
423 * Concatenate the make and model...
426 strlcat(make_model
, " ", make_model_size
);
427 strlcat(make_model
, mdl
, make_model_size
);
433 * Just copy model string, since it has the manufacturer...
436 strlcpy(make_model
, mdl
, make_model_size
);
445 if (!strncasecmp(attr
, "Hewlett-Packard hp ", 19))
448 * Check for a common HP bug...
451 strlcpy(make_model
, "HP ", make_model_size
);
452 strlcpy(make_model
+ 3, attr
+ 19, make_model_size
- 3);
454 else if (!strncasecmp(attr
, "Hewlett-Packard ", 16))
456 strlcpy(make_model
, "HP ", make_model_size
);
457 strlcpy(make_model
+ 3, attr
+ 16, make_model_size
- 3);
461 strlcpy(make_model
, attr
, make_model_size
);
467 * Use "Unknown" as the printer make and model...
470 strlcpy(make_model
, "Unknown", make_model_size
);
474 * Strip trailing data...
477 if ((delim
= strchr(make_model
, ';')) != NULL
)
481 * Strip trailing whitespace...
484 for (delim
= make_model
+ strlen(make_model
) - 1; delim
>= make_model
; delim
--)
485 if (isspace(*delim
& 255))
502 * End of "$Id: ieee1284.c 5866 2006-08-23 03:03:49Z mike $".