]> git.ipfire.org Git - thirdparty/cups.git/blob - backend/ieee1284.c
Full sweep of all Clang warnings, plus some bug fixes for incorrect memcpy usage.
[thirdparty/cups.git] / backend / ieee1284.c
1 /*
2 * "$Id$"
3 *
4 * IEEE-1284 support functions for CUPS.
5 *
6 * Copyright 2007-2014 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
8 *
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * "LICENSE" which should have been included with this file. If this
13 * file is missing or damaged, see the license at "http://www.cups.org/".
14 *
15 * This file is subject to the Apple OS-Developed Software exception.
16 */
17
18 /*
19 * Include necessary headers.
20 */
21
22 #include "backend-private.h"
23 #include <cups/cups-private.h>
24
25
26 /*
27 * 'backendGetDeviceID()' - Get the IEEE-1284 device ID string and
28 * corresponding URI.
29 */
30
31 int /* O - 0 on success, -1 on failure */
32 backendGetDeviceID(
33 int fd, /* I - File descriptor */
34 char *device_id, /* O - 1284 device ID */
35 int device_id_size, /* I - Size of buffer */
36 char *make_model, /* O - Make/model */
37 int make_model_size, /* I - Size of buffer */
38 const char *scheme, /* I - URI scheme */
39 char *uri, /* O - Device URI */
40 int uri_size) /* I - Size of buffer */
41 {
42 #ifdef __APPLE__ /* This function is a no-op */
43 (void)fd;
44 (void)device_id;
45 (void)device_id_size;
46 (void)make_model;
47 (void)make_model_size;
48 (void)scheme;
49 (void)uri;
50 (void)uri_size;
51
52 return (-1);
53
54 #else /* Get the device ID from the specified file descriptor... */
55 # ifdef __linux
56 int length; /* Length of device ID info */
57 int got_id = 0;
58 # endif /* __linux */
59 # if defined(__sun) && defined(ECPPIOC_GETDEVID)
60 struct ecpp_device_id did; /* Device ID buffer */
61 # endif /* __sun && ECPPIOC_GETDEVID */
62 char *ptr; /* Pointer into device ID */
63
64
65 DEBUG_printf(("backendGetDeviceID(fd=%d, device_id=%p, device_id_size=%d, "
66 "make_model=%p, make_model_size=%d, scheme=\"%s\", "
67 "uri=%p, uri_size=%d)\n", fd, device_id, device_id_size,
68 make_model, make_model_size, scheme ? scheme : "(null)",
69 uri, uri_size));
70
71 /*
72 * Range check input...
73 */
74
75 if (!device_id || device_id_size < 32)
76 {
77 DEBUG_puts("backendGetDeviceID: Bad args!");
78 return (-1);
79 }
80
81 if (make_model)
82 *make_model = '\0';
83
84 if (fd >= 0)
85 {
86 /*
87 * Get the device ID string...
88 */
89
90 *device_id = '\0';
91
92 # ifdef __linux
93 if (ioctl(fd, LPIOC_GET_DEVICE_ID(device_id_size), device_id))
94 {
95 /*
96 * Linux has to implement things differently for every device it seems.
97 * Since the standard parallel port driver does not provide a simple
98 * ioctl() to get the 1284 device ID, we have to open the "raw" parallel
99 * device corresponding to this port and do some negotiation trickery
100 * to get the current device ID.
101 */
102
103 if (uri && !strncmp(uri, "parallel:/dev/", 14))
104 {
105 char devparport[16]; /* /dev/parportN */
106 int devparportfd, /* File descriptor for raw device */
107 mode; /* Port mode */
108
109
110 /*
111 * Since the Linux parallel backend only supports 4 parallel port
112 * devices, just grab the trailing digit and use it to construct a
113 * /dev/parportN filename...
114 */
115
116 snprintf(devparport, sizeof(devparport), "/dev/parport%s",
117 uri + strlen(uri) - 1);
118
119 if ((devparportfd = open(devparport, O_RDWR | O_NOCTTY)) != -1)
120 {
121 /*
122 * Claim the device...
123 */
124
125 if (!ioctl(devparportfd, PPCLAIM))
126 {
127 fcntl(devparportfd, F_SETFL, fcntl(devparportfd, F_GETFL) | O_NONBLOCK);
128
129 mode = IEEE1284_MODE_COMPAT;
130
131 if (!ioctl(devparportfd, PPNEGOT, &mode))
132 {
133 /*
134 * Put the device into Device ID mode...
135 */
136
137 mode = IEEE1284_MODE_NIBBLE | IEEE1284_DEVICEID;
138
139 if (!ioctl(devparportfd, PPNEGOT, &mode))
140 {
141 /*
142 * Read the 1284 device ID...
143 */
144
145 if ((length = read(devparportfd, device_id,
146 device_id_size - 1)) >= 2)
147 {
148 device_id[length] = '\0';
149 got_id = 1;
150 }
151 }
152 }
153
154 /*
155 * Release the device...
156 */
157
158 ioctl(devparportfd, PPRELEASE);
159 }
160
161 close(devparportfd);
162 }
163 }
164 }
165 else
166 got_id = 1;
167
168 if (got_id)
169 {
170 /*
171 * Extract the length of the device ID string from the first two
172 * bytes. The 1284 spec says the length is stored MSB first...
173 */
174
175 length = (((unsigned)device_id[0] & 255) << 8) +
176 ((unsigned)device_id[1] & 255);
177
178 /*
179 * Check to see if the length is larger than our buffer; first
180 * assume that the vendor incorrectly implemented the 1284 spec,
181 * and then limit the length to the size of our buffer...
182 */
183
184 if (length > device_id_size || length < 14)
185 length = (((unsigned)device_id[1] & 255) << 8) +
186 ((unsigned)device_id[0] & 255);
187
188 if (length > device_id_size)
189 length = device_id_size;
190
191 /*
192 * The length field counts the number of bytes in the string
193 * including the length field itself (2 bytes). The minimum
194 * length for a valid/usable device ID is 14 bytes:
195 *
196 * <LENGTH> MFG: <MFG> ;MDL: <MDL> ;
197 * 2 + 4 + 1 + 5 + 1 + 1
198 */
199
200 if (length < 14)
201 {
202 /*
203 * Can't use this device ID, so don't try to copy it...
204 */
205
206 device_id[0] = '\0';
207 got_id = 0;
208 }
209 else
210 {
211 /*
212 * Copy the device ID text to the beginning of the buffer and
213 * nul-terminate.
214 */
215
216 length -= 2;
217
218 memmove(device_id, device_id + 2, length);
219 device_id[length] = '\0';
220 }
221 }
222 else
223 {
224 DEBUG_printf(("backendGetDeviceID: ioctl failed - %s\n",
225 strerror(errno)));
226 *device_id = '\0';
227 }
228 # endif /* __linux */
229
230 # if defined(__sun) && defined(ECPPIOC_GETDEVID)
231 did.mode = ECPP_CENTRONICS;
232 did.len = device_id_size - 1;
233 did.rlen = 0;
234 did.addr = device_id;
235
236 if (!ioctl(fd, ECPPIOC_GETDEVID, &did))
237 {
238 /*
239 * Nul-terminate the device ID text.
240 */
241
242 if (did.rlen < (device_id_size - 1))
243 device_id[did.rlen] = '\0';
244 else
245 device_id[device_id_size - 1] = '\0';
246 }
247 # ifdef DEBUG
248 else
249 DEBUG_printf(("backendGetDeviceID: ioctl failed - %s\n",
250 strerror(errno)));
251 # endif /* DEBUG */
252 # endif /* __sun && ECPPIOC_GETDEVID */
253 }
254
255 /*
256 * Check whether device ID is valid. Turn line breaks and tabs to spaces and
257 * reject device IDs with non-printable characters.
258 */
259
260 for (ptr = device_id; *ptr; ptr ++)
261 if (_cups_isspace(*ptr))
262 *ptr = ' ';
263 else if ((*ptr & 255) < ' ' || *ptr == 127)
264 {
265 DEBUG_printf(("backendGetDeviceID: Bad device_id character %d.",
266 *ptr & 255));
267 *device_id = '\0';
268 break;
269 }
270
271 DEBUG_printf(("backendGetDeviceID: device_id=\"%s\"\n", device_id));
272
273 if (scheme && uri)
274 *uri = '\0';
275
276 if (!*device_id)
277 return (-1);
278
279 /*
280 * Get the make and model...
281 */
282
283 if (make_model)
284 backendGetMakeModel(device_id, make_model, make_model_size);
285
286 /*
287 * Then generate a device URI...
288 */
289
290 if (scheme && uri && uri_size > 32)
291 {
292 int num_values; /* Number of keys and values */
293 cups_option_t *values; /* Keys and values in device ID */
294 const char *mfg, /* Manufacturer */
295 *mdl, /* Model */
296 *sern; /* Serial number */
297 char temp[256], /* Temporary manufacturer string */
298 *tempptr; /* Pointer into temp string */
299
300
301 /*
302 * Get the make, model, and serial numbers...
303 */
304
305 num_values = _cupsGet1284Values(device_id, &values);
306
307 if ((sern = cupsGetOption("SERIALNUMBER", num_values, values)) == NULL)
308 if ((sern = cupsGetOption("SERN", num_values, values)) == NULL)
309 sern = cupsGetOption("SN", num_values, values);
310
311 if ((mfg = cupsGetOption("MANUFACTURER", num_values, values)) == NULL)
312 mfg = cupsGetOption("MFG", num_values, values);
313
314 if ((mdl = cupsGetOption("MODEL", num_values, values)) == NULL)
315 mdl = cupsGetOption("MDL", num_values, values);
316
317 if (mfg)
318 {
319 if (!_cups_strcasecmp(mfg, "Hewlett-Packard"))
320 mfg = "HP";
321 else if (!_cups_strcasecmp(mfg, "Lexmark International"))
322 mfg = "Lexmark";
323 }
324 else
325 {
326 strlcpy(temp, make_model, sizeof(temp));
327
328 if ((tempptr = strchr(temp, ' ')) != NULL)
329 *tempptr = '\0';
330
331 mfg = temp;
332 }
333
334 if (!mdl)
335 mdl = "";
336
337 if (!_cups_strncasecmp(mdl, mfg, strlen(mfg)))
338 {
339 mdl += strlen(mfg);
340
341 while (isspace(*mdl & 255))
342 mdl ++;
343 }
344
345 /*
346 * Generate the device URI from the manufacturer, make_model, and
347 * serial number strings.
348 */
349
350 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, uri_size, scheme, NULL, mfg, 0,
351 "/%s%s%s", mdl, sern ? "?serial=" : "", sern ? sern : "");
352
353 cupsFreeOptions(num_values, values);
354 }
355
356 return (0);
357 #endif /* __APPLE__ */
358 }
359
360
361 /*
362 * 'backendGetMakeModel()' - Get the make and model string from the device ID.
363 */
364
365 int /* O - 0 on success, -1 on failure */
366 backendGetMakeModel(
367 const char *device_id, /* O - 1284 device ID */
368 char *make_model, /* O - Make/model */
369 size_t make_model_size) /* I - Size of buffer */
370 {
371 int num_values; /* Number of keys and values */
372 cups_option_t *values; /* Keys and values */
373 const char *mfg, /* Manufacturer string */
374 *mdl, /* Model string */
375 *des; /* Description string */
376
377
378 DEBUG_printf(("backendGetMakeModel(device_id=\"%s\", make_model=%p, make_model_size=" CUPS_LLFMT ")\n", device_id, make_model, CUPS_LLCAST make_model_size));
379
380 /*
381 * Range check input...
382 */
383
384 if (!device_id || !*device_id || !make_model || make_model_size < 32)
385 {
386 DEBUG_puts("backendGetMakeModel: Bad args!");
387 return (-1);
388 }
389
390 *make_model = '\0';
391
392 /*
393 * Look for the description field...
394 */
395
396 num_values = _cupsGet1284Values(device_id, &values);
397
398 if ((mdl = cupsGetOption("MODEL", num_values, values)) == NULL)
399 mdl = cupsGetOption("MDL", num_values, values);
400
401 if (mdl)
402 {
403 /*
404 * Build a make-model string from the manufacturer and model attributes...
405 */
406
407 if ((mfg = cupsGetOption("MANUFACTURER", num_values, values)) == NULL)
408 mfg = cupsGetOption("MFG", num_values, values);
409
410 if (!mfg || !_cups_strncasecmp(mdl, mfg, strlen(mfg)))
411 {
412 /*
413 * Just copy the model string, since it has the manufacturer...
414 */
415
416 _ppdNormalizeMakeAndModel(mdl, make_model, make_model_size);
417 }
418 else
419 {
420 /*
421 * Concatenate the make and model...
422 */
423
424 char temp[1024]; /* Temporary make and model */
425
426 snprintf(temp, sizeof(temp), "%s %s", mfg, mdl);
427
428 _ppdNormalizeMakeAndModel(temp, make_model, make_model_size);
429 }
430 }
431 else if ((des = cupsGetOption("DESCRIPTION", num_values, values)) != NULL ||
432 (des = cupsGetOption("DES", num_values, values)) != NULL)
433 {
434 /*
435 * Make sure the description contains something useful, since some
436 * printer manufacturers (HP) apparently don't follow the standards
437 * they helped to define...
438 *
439 * Here we require the description to be 8 or more characters in length,
440 * containing at least one space and one letter.
441 */
442
443 if (strlen(des) >= 8)
444 {
445 const char *ptr; /* Pointer into description */
446 int letters, /* Number of letters seen */
447 spaces; /* Number of spaces seen */
448
449
450 for (ptr = des, letters = 0, spaces = 0; *ptr; ptr ++)
451 {
452 if (isspace(*ptr & 255))
453 spaces ++;
454 else if (isalpha(*ptr & 255))
455 letters ++;
456
457 if (spaces && letters)
458 break;
459 }
460
461 if (spaces && letters)
462 _ppdNormalizeMakeAndModel(des, make_model, make_model_size);
463 }
464 }
465
466 if (!make_model[0])
467 {
468 /*
469 * Use "Unknown" as the printer make and model...
470 */
471
472 strlcpy(make_model, "Unknown", make_model_size);
473 }
474
475 cupsFreeOptions(num_values, values);
476
477 return (0);
478 }
479
480
481 /*
482 * End of "$Id$".
483 */