]>
git.ipfire.org Git - thirdparty/cups.git/blob - backend/ieee1284.c
2 * "$Id: ieee1284.c 4903 2006-01-10 20:02:46Z 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 * get_device_id() - Get the IEEE-1284 device ID string and corresponding
33 * Include necessary headers.
36 #include <cups/debug.h>
38 # include <sys/ioctl.h>
39 # include <linux/lp.h>
40 # define IOCNR_GET_DEVICE_ID 1
41 # define LPIOC_GET_DEVICE_ID(len) _IOC(_IOC_READ, 'P', IOCNR_GET_DEVICE_ID, len)
46 # include <sys/ecppio.h>
48 # include <sys/ioccom.h>
49 # include <sys/ecppsys.h>
55 * 'get_device_id()' - Get the IEEE-1284 device ID string and
59 int /* O - 0 on success, -1 on failure */
61 int fd
, /* I - File descriptor */
62 char *device_id
, /* O - 1284 device ID */
63 int device_id_size
, /* I - Size of buffer */
64 char *make_model
, /* O - Make/model */
65 int make_model_size
, /* I - Size of buffer */
66 const char *scheme
, /* I - URI scheme */
67 char *uri
, /* O - Device URI */
68 int uri_size
) /* I - Size of buffer */
70 char *attr
, /* 1284 attribute */
71 *delim
, /* 1284 delimiter */
72 *uriptr
, /* Pointer into URI */
73 *mfg
, /* Manufacturer string */
74 *mdl
, /* Model string */
75 serial_number
[1024]; /* Serial number string */
77 int length
; /* Length of device ID info */
80 struct ecpp_device_id did
; /* Device ID buffer */
83 DEBUG_printf(("get_device_id(fd=%d, device_id=%p, device_id_size=%d, "
84 "make_model=%p, make_model_size=%d, scheme=\"%s\", "
85 "uri=%p, uri_size=%d)\n", fd
, device_id
, device_id_size
,
86 make_model
, make_model_size
, scheme
? scheme
: "(null)",
90 * Range check input...
94 !device_id
|| device_id_size
< 32 ||
95 !make_model
|| make_model_size
< 32)
97 DEBUG_puts("get_device_id: Bad args!");
108 * Get the device ID string...
112 if (!ioctl(fd
, LPIOC_GET_DEVICE_ID(device_id_size
), device_id
))
115 * Extract the length of the device ID string from the first two
116 * bytes. The 1284 spec says the length is stored MSB first...
119 length
= (((unsigned)device_id
[0] & 255) << 8) +
120 ((unsigned)device_id
[1] & 255);
123 * Check to see if the length is larger than our buffer; first
124 * assume that the vendor incorrectly implemented the 1284 spec,
125 * and then limit the length to the size of our buffer...
128 if (length
> (device_id_size
- 2))
129 length
= (((unsigned)device_id
[1] & 255) << 8) +
130 ((unsigned)device_id
[0] & 255);
132 if (length
> (device_id_size
- 2))
133 length
= device_id_size
- 2;
136 * Copy the device ID text to the beginning of the buffer and
140 memmove(device_id
, device_id
+ 2, length
);
141 device_id
[length
] = '\0';
145 printf("get_device_id: ioctl failed - %s\n", strerror(errno
));
149 #if defined(__sun) && defined(ECPPIOC_GETDEVID)
150 did
.mode
= ECPP_CENTRONICS
;
151 did
.len
= device_id_size
- 1;
153 did
.addr
= device_id
;
155 if (!ioctl(fd
, ECPPIOC_GETDEVID
, &did
))
158 * Nul-terminate the device ID text.
161 if (did
.rlen
< (device_id_size
- 1))
162 device_id
[did
.rlen
] = '\0';
164 device_id
[device_id_size
- 1] = '\0';
168 printf("get_device_id: ioctl failed - %s\n", strerror(errno
));
170 #endif /* __sun && ECPPIOC_GETDEVID */
172 DEBUG_printf(("get_device_id: device_id=\"%s\"\n", device_id
));
178 * Look for the description field...
181 if ((attr
= strstr(device_id
, "DES:")) != NULL
)
183 else if ((attr
= strstr(device_id
, "DESCRIPTION:")) != NULL
)
189 * Make sure the description contains something useful, since some
190 * printer manufacturers (HP) apparently don't follow the standards
191 * they helped to define...
193 * Here we require the description to be 8 or more characters in length,
194 * containing at least one space and one letter.
197 if ((delim
= strchr(attr
, ';')) == NULL
)
198 delim
= attr
+ strlen(attr
);
200 if ((delim
- attr
) < 8)
204 char *ptr
; /* Pointer into description */
205 int letters
, /* Number of letters seen */
206 spaces
; /* Number of spaces seen */
209 for (ptr
= attr
, letters
= 0, spaces
= 0; ptr
< delim
; ptr
++)
211 if (isspace(*ptr
& 255))
213 else if (isalpha(*ptr
& 255))
216 if (spaces
&& letters
)
220 if (!spaces
|| !letters
)
225 if ((mfg
= strstr(device_id
, "MANUFACTURER:")) != NULL
)
227 else if ((mfg
= strstr(device_id
, "MFG:")) != NULL
)
230 if ((mdl
= strstr(device_id
, "MODEL:")) != NULL
)
232 else if ((mdl
= strstr(device_id
, "MDL:")) != NULL
)
241 if (!strncasecmp(attr
, "Hewlett-Packard hp ", 19))
244 * Check for a common HP bug...
247 strlcpy(make_model
, "HP ", make_model_size
);
248 strlcpy(make_model
+ 3, attr
+ 19, make_model_size
- 3);
250 else if (!strncasecmp(attr
, "Hewlett-Packard ", 16))
252 strlcpy(make_model
, "HP ", make_model_size
);
253 strlcpy(make_model
+ 3, attr
+ 16, make_model_size
- 3);
257 strlcpy(make_model
, attr
, make_model_size
);
263 * Build a make-model string from the manufacturer and model attributes...
266 if (!strncasecmp(mfg
, "Hewlett-Packard", 15))
267 strlcpy(make_model
, "HP", make_model_size
);
269 strlcpy(make_model
, mfg
, make_model_size
);
271 if ((delim
= strchr(make_model
, ';')) != NULL
)
274 if (!strncasecmp(make_model
, mdl
, strlen(make_model
)))
277 * Just copy model string, since it has the manufacturer...
280 strlcpy(make_model
, mdl
, make_model_size
);
285 * Concatenate the make and model...
288 strlcat(make_model
, " ", make_model_size
);
289 strlcat(make_model
, mdl
, make_model_size
);
295 * Use "Unknown" as the printer make and model...
298 strlcpy(make_model
, "Unknown", make_model_size
);
301 if ((delim
= strchr(make_model
, ';')) != NULL
)
304 if (scheme
&& uri
&& uri_size
> 32)
307 * Look for the serial number field...
310 if ((attr
= strstr(device_id
, "SERN:")) != NULL
)
312 else if ((attr
= strstr(device_id
, "SERIALNUMBER:")) != NULL
)
314 else if ((attr
= strstr(device_id
, ";SN:")) != NULL
)
319 strlcpy(serial_number
, attr
, sizeof(serial_number
));
321 if ((delim
= strchr(serial_number
, ';')) != NULL
)
325 serial_number
[0] = '\0';
328 * Generate the device URI from the make_model and serial number strings.
331 snprintf(uri
, uri_size
, "%s://", scheme
);
332 for (uriptr
= uri
+ strlen(uri
), delim
= make_model
;
333 *delim
&& uriptr
< (uri
+ uri_size
- 1);
344 for (; *delim
&& uriptr
< (uri
+ uri_size
- 3); delim
++)
356 if (serial_number
[0])
359 * Add the serial number to the URI...
362 strlcat(uri
, "?serial=", uri_size
);
363 strlcat(uri
, serial_number
, uri_size
);
372 * End of "$Id: ieee1284.c 4903 2006-01-10 20:02:46Z mike $".