]>
git.ipfire.org Git - thirdparty/cups.git/blob - backend/ieee1284.c
d623b293df3b0f4362bf0781344fd8577651d3e1
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");
239 strlcpy(manufacturer
, make_model
, sizeof(manufacturer
));
241 if ((delim
= strchr(manufacturer
, ' ')) != NULL
)
245 manulen
= strlen(manufacturer
);
247 for (uriptr
= uri
+ strlen(uri
), delim
= manufacturer
;
248 *delim
&& uriptr
< (uri
+ uri_size
- 3);
261 if (!strncasecmp(make_model
, manufacturer
, manulen
))
263 delim
= make_model
+ manulen
;
265 while (isspace(*delim
& 255))
271 for (; *delim
&& uriptr
< (uri
+ uri_size
- 3); delim
++)
281 if (serial_number
[0])
284 * Add the serial number to the URI...
287 strlcpy(uriptr
, "?serial=", uri_size
- (uriptr
- uri
));
288 strlcat(uriptr
, serial_number
, uri_size
- (uriptr
- uri
));
299 * 'backendGetMakeModel()' - Get the make and model string from the device ID.
302 int /* O - 0 on success, -1 on failure */
304 const char *device_id
, /* O - 1284 device ID */
305 char *make_model
, /* O - Make/model */
306 int make_model_size
) /* I - Size of buffer */
308 char *attr
, /* 1284 attribute */
309 *delim
, /* 1284 delimiter */
310 *mfg
, /* Manufacturer string */
311 *mdl
; /* Model string */
314 DEBUG_printf(("backendGetMakeModel(device_id=\"%s\", "
315 "make_model=%p, make_model_size=%d)\n", device_id
,
316 make_model
, make_model_size
));
319 * Range check input...
322 if (!device_id
|| !*device_id
|| !make_model
|| make_model_size
< 32)
324 DEBUG_puts("backendGetMakeModel: Bad args!");
331 * Look for the description field...
334 if ((attr
= strstr(device_id
, "DES:")) != NULL
)
336 else if ((attr
= strstr(device_id
, "DESCRIPTION:")) != NULL
)
342 * Make sure the description contains something useful, since some
343 * printer manufacturers (HP) apparently don't follow the standards
344 * they helped to define...
346 * Here we require the description to be 8 or more characters in length,
347 * containing at least one space and one letter.
350 if ((delim
= strchr(attr
, ';')) == NULL
)
351 delim
= attr
+ strlen(attr
);
353 if ((delim
- attr
) < 8)
357 char *ptr
; /* Pointer into description */
358 int letters
, /* Number of letters seen */
359 spaces
; /* Number of spaces seen */
362 for (ptr
= attr
, letters
= 0, spaces
= 0; ptr
< delim
; ptr
++)
364 if (isspace(*ptr
& 255))
366 else if (isalpha(*ptr
& 255))
369 if (spaces
&& letters
)
373 if (!spaces
|| !letters
)
378 if ((mfg
= strstr(device_id
, "MANUFACTURER:")) != NULL
)
380 else if ((mfg
= strstr(device_id
, "Manufacturer:")) != NULL
)
382 else if ((mfg
= strstr(device_id
, "MFG:")) != NULL
)
385 if ((mdl
= strstr(device_id
, "MODEL:")) != NULL
)
387 else if ((mdl
= strstr(device_id
, "Model:")) != NULL
)
389 else if ((mdl
= strstr(device_id
, "MDL:")) != NULL
)
395 * Build a make-model string from the manufacturer and model attributes...
400 if (!strncasecmp(mfg
, "Hewlett-Packard", 15))
401 strlcpy(make_model
, "HP", make_model_size
);
403 strlcpy(make_model
, mfg
, make_model_size
);
405 if ((delim
= strchr(make_model
, ';')) != NULL
)
408 if (!strncasecmp(make_model
, mdl
, strlen(make_model
)))
411 * Just copy model string, since it has the manufacturer...
414 strlcpy(make_model
, mdl
, make_model_size
);
419 * Concatenate the make and model...
422 strlcat(make_model
, " ", make_model_size
);
423 strlcat(make_model
, mdl
, make_model_size
);
429 * Just copy model string, since it has the manufacturer...
432 strlcpy(make_model
, mdl
, make_model_size
);
441 if (!strncasecmp(attr
, "Hewlett-Packard hp ", 19))
444 * Check for a common HP bug...
447 strlcpy(make_model
, "HP ", make_model_size
);
448 strlcpy(make_model
+ 3, attr
+ 19, make_model_size
- 3);
450 else if (!strncasecmp(attr
, "Hewlett-Packard ", 16))
452 strlcpy(make_model
, "HP ", make_model_size
);
453 strlcpy(make_model
+ 3, attr
+ 16, make_model_size
- 3);
457 strlcpy(make_model
, attr
, make_model_size
);
463 * Use "Unknown" as the printer make and model...
466 strlcpy(make_model
, "Unknown", make_model_size
);
470 * Strip trailing data...
473 if ((delim
= strchr(make_model
, ';')) != NULL
)
477 * Strip trailing whitespace...
480 for (delim
= make_model
+ strlen(make_model
) - 1; delim
>= make_model
; delim
--)
481 if (isspace(*delim
& 255))