]> git.ipfire.org Git - thirdparty/cups.git/blame - backend/snmp-supplies.c
Update copyrights and license text on files that were missed.
[thirdparty/cups.git] / backend / snmp-supplies.c
CommitLineData
568fa3fa 1/*
7e86f2f6 2 * SNMP supplies functions for CUPS.
568fa3fa 3 *
53f8d64f 4 * Copyright © 2008-2015 by Apple Inc.
568fa3fa 5 *
53f8d64f
MS
6 * Licensed under Apache License v2.0. See the file "LICENSE" for more
7 * information.
568fa3fa
MS
8 */
9
10/*
11 * Include necessary headers.
12 */
13
14#include "backend-private.h"
f787e1e3 15#include <cups/ppd-private.h>
568fa3fa
MS
16#include <cups/array.h>
17
18
19/*
20 * Local constants...
21 */
22
23#define CUPS_MAX_SUPPLIES 32 /* Maximum number of supplies for a printer */
68b10830 24#define CUPS_SUPPLY_TIMEOUT 2.0 /* Timeout for SNMP lookups */
568fa3fa 25
82cc1f9a
MS
26#define CUPS_DEVELOPER_LOW 0x0001
27#define CUPS_DEVELOPER_EMPTY 0x0002
28#define CUPS_MARKER_SUPPLY_LOW 0x0004
29#define CUPS_MARKER_SUPPLY_EMPTY 0x0008
30#define CUPS_OPC_NEAR_EOL 0x0010
31#define CUPS_OPC_LIFE_OVER 0x0020
32#define CUPS_TONER_LOW 0x0040
33#define CUPS_TONER_EMPTY 0x0080
34#define CUPS_WASTE_ALMOST_FULL 0x0100
35#define CUPS_WASTE_FULL 0x0200
36#define CUPS_CLEANER_NEAR_EOL 0x0400 /* Proposed JPS3 */
37#define CUPS_CLEANER_LIFE_OVER 0x0800 /* Proposed JPS3 */
c8fef167 38
a29fd7dd
MS
39#define CUPS_SNMP_NONE 0x0000
40#define CUPS_SNMP_CAPACITY 0x0001 /* Supply levels reported as percentages */
41
568fa3fa
MS
42
43/*
44 * Local structures...
45 */
46
745129be 47typedef struct /**** Printer supply data ****/
568fa3fa
MS
48{
49 char name[CUPS_SNMP_MAX_STRING], /* Name of supply */
50 color[8]; /* Color: "#RRGGBB" or "none" */
51 int colorant, /* Colorant index */
f7e3306a 52 sclass, /* Supply class */
568fa3fa
MS
53 type, /* Supply type */
54 max_capacity, /* Maximum capacity */
55 level; /* Current level value */
56} backend_supplies_t;
57
745129be
MS
58typedef struct /**** Printer state table ****/
59{
60 int bit; /* State bit */
61 const char *keyword; /* IPP printer-state-reasons keyword */
62} backend_state_t;
63
568fa3fa
MS
64
65/*
66 * Local globals...
67 */
68
69static http_addr_t current_addr; /* Current address */
745129be
MS
70static int current_state = -1;
71 /* Current device state bits */
68b10830 72static int charset = -1; /* Character set for supply names */
a29fd7dd
MS
73static unsigned quirks = CUPS_SNMP_NONE;
74 /* Quirks we have to work around */
c8fef167 75static int num_supplies = 0;
568fa3fa
MS
76 /* Number of supplies found */
77static backend_supplies_t supplies[CUPS_MAX_SUPPLIES];
78 /* Supply information */
c8fef167
MS
79static int supply_state = -1;
80 /* Supply state info */
568fa3fa
MS
81
82static const int hrDeviceDescr[] =
83 { CUPS_OID_hrDeviceDescr, 1, -1 };
84 /* Device description OID */
85static const int hrPrinterStatus[] =
86 { CUPS_OID_hrPrinterStatus, 1, -1 };
87 /* Current state OID */
88static const int hrPrinterDetectedErrorState[] =
89 { CUPS_OID_hrPrinterDetectedErrorState, 1, -1 };
90 /* Current printer state bits OID */
68b10830
MS
91static const int prtGeneralCurrentLocalization[] =
92 { CUPS_OID_prtGeneralCurrentLocalization, 1, -1 };
93static const int prtLocalizationCharacterSet[] =
94 { CUPS_OID_prtLocalizationCharacterSet, 1, 1, -1 },
95 prtLocalizationCharacterSetOffset =
96 (sizeof(prtLocalizationCharacterSet) /
97 sizeof(prtLocalizationCharacterSet[0]));
568fa3fa
MS
98static const int prtMarkerColorantValue[] =
99 { CUPS_OID_prtMarkerColorantValue, -1 },
100 /* Colorant OID */
101 prtMarkerColorantValueOffset =
102 (sizeof(prtMarkerColorantValue) /
103 sizeof(prtMarkerColorantValue[0]));
104 /* Offset to colorant index */
105static const int prtMarkerLifeCount[] =
106 { CUPS_OID_prtMarkerLifeCount, 1, 1, -1 };
107 /* Page counter OID */
108static const int prtMarkerSuppliesEntry[] =
109 { CUPS_OID_prtMarkerSuppliesEntry, -1 };
110 /* Supplies OID */
111static const int prtMarkerSuppliesColorantIndex[] =
112 { CUPS_OID_prtMarkerSuppliesColorantIndex, -1 },
113 /* Colorant index OID */
114 prtMarkerSuppliesColorantIndexOffset =
115 (sizeof(prtMarkerSuppliesColorantIndex) /
116 sizeof(prtMarkerSuppliesColorantIndex[0]));
117 /* Offset to supply index */
118static const int prtMarkerSuppliesDescription[] =
119 { CUPS_OID_prtMarkerSuppliesDescription, -1 },
120 /* Description OID */
121 prtMarkerSuppliesDescriptionOffset =
122 (sizeof(prtMarkerSuppliesDescription) /
123 sizeof(prtMarkerSuppliesDescription[0]));
124 /* Offset to supply index */
125static const int prtMarkerSuppliesLevel[] =
126 { CUPS_OID_prtMarkerSuppliesLevel, -1 },
127 /* Level OID */
128 prtMarkerSuppliesLevelOffset =
129 (sizeof(prtMarkerSuppliesLevel) /
130 sizeof(prtMarkerSuppliesLevel[0]));
131 /* Offset to supply index */
132static const int prtMarkerSuppliesMaxCapacity[] =
133 { CUPS_OID_prtMarkerSuppliesMaxCapacity, -1 },
134 /* Max capacity OID */
135 prtMarkerSuppliesMaxCapacityOffset =
136 (sizeof(prtMarkerSuppliesMaxCapacity) /
137 sizeof(prtMarkerSuppliesMaxCapacity[0]));
138 /* Offset to supply index */
f7e3306a
MS
139static const int prtMarkerSuppliesClass[] =
140 { CUPS_OID_prtMarkerSuppliesClass, -1 },
141 /* Class OID */
142 prtMarkerSuppliesClassOffset =
143 (sizeof(prtMarkerSuppliesClass) /
144 sizeof(prtMarkerSuppliesClass[0]));
145 /* Offset to supply index */
568fa3fa
MS
146static const int prtMarkerSuppliesType[] =
147 { CUPS_OID_prtMarkerSuppliesType, -1 },
148 /* Type OID */
149 prtMarkerSuppliesTypeOffset =
150 (sizeof(prtMarkerSuppliesType) /
151 sizeof(prtMarkerSuppliesType[0]));
152 /* Offset to supply index */
82cc1f9a
MS
153static const int prtMarkerSuppliesSupplyUnit[] =
154 { CUPS_OID_prtMarkerSuppliesSupplyUnit, -1 },
155 /* Units OID */
156 prtMarkerSuppliesSupplyUnitOffset =
157 (sizeof(prtMarkerSuppliesSupplyUnit) /
158 sizeof(prtMarkerSuppliesSupplyUnit[0]));
159 /* Offset to supply index */
568fa3fa 160
c1420c87 161static const backend_state_t printer_states[] =
745129be 162 {
3e7fe0ca 163 /* { CUPS_TC_lowPaper, "media-low-report" }, */
745129be 164 { CUPS_TC_noPaper | CUPS_TC_inputTrayEmpty, "media-empty-warning" },
c8fef167
MS
165 /* { CUPS_TC_lowToner, "toner-low-report" }, */ /* now use prtMarkerSupplies */
166 /* { CUPS_TC_noToner, "toner-empty-warning" }, */ /* now use prtMarkerSupplies */
745129be
MS
167 { CUPS_TC_doorOpen, "door-open-report" },
168 { CUPS_TC_jammed, "media-jam-warning" },
169 /* { CUPS_TC_offline, "offline-report" }, */ /* unreliable */
e07d4801 170 /* { CUPS_TC_serviceRequested | CUPS_TC_overduePreventMaint, "service-needed-warning" }, */ /* unreliable */
745129be
MS
171 { CUPS_TC_inputTrayMissing, "input-tray-missing-warning" },
172 { CUPS_TC_outputTrayMissing, "output-tray-missing-warning" },
173 { CUPS_TC_markerSupplyMissing, "marker-supply-missing-warning" },
174 { CUPS_TC_outputNearFull, "output-area-almost-full-report" },
175 { CUPS_TC_outputFull, "output-area-full-warning" }
176 };
177
c1420c87 178static const backend_state_t supply_states[] =
c8fef167
MS
179 {
180 { CUPS_DEVELOPER_LOW, "developer-low-report" },
181 { CUPS_DEVELOPER_EMPTY, "developer-empty-warning" },
182 { CUPS_MARKER_SUPPLY_LOW, "marker-supply-low-report" },
183 { CUPS_MARKER_SUPPLY_EMPTY, "marker-supply-empty-warning" },
c8fef167
MS
184 { CUPS_OPC_NEAR_EOL, "opc-near-eol-report" },
185 { CUPS_OPC_LIFE_OVER, "opc-life-over-warning" },
186 { CUPS_TONER_LOW, "toner-low-report" },
82cc1f9a
MS
187 { CUPS_TONER_EMPTY, "toner-empty-warning" },
188 { CUPS_WASTE_ALMOST_FULL, "waste-receptacle-almost-full-report" },
189 { CUPS_WASTE_FULL, "waste-receptacle-full-warning" },
190 { CUPS_CLEANER_NEAR_EOL, "cleaner-life-almost-over-report" },
191 { CUPS_CLEANER_LIFE_OVER, "cleaner-life-over-warning" },
c8fef167
MS
192 };
193
568fa3fa
MS
194
195/*
196 * Local functions...
197 */
198
199static void backend_init_supplies(int snmp_fd, http_addr_t *addr);
200static void backend_walk_cb(cups_snmp_t *packet, void *data);
68b10830
MS
201static void utf16_to_utf8(cups_utf8_t *dst, const unsigned char *src,
202 size_t srcsize, size_t dstsize, int le);
568fa3fa
MS
203
204
205/*
206 * 'backendSNMPSupplies()' - Get the current supplies for a device.
207 */
208
209int /* O - 0 on success, -1 on error */
210backendSNMPSupplies(
211 int snmp_fd, /* I - SNMP socket */
212 http_addr_t *addr, /* I - Printer address */
213 int *page_count, /* O - Page count */
214 int *printer_state) /* O - Printer state */
215{
216 if (!httpAddrEqual(addr, &current_addr))
217 backend_init_supplies(snmp_fd, addr);
218 else if (num_supplies > 0)
7a14d768 219 _cupsSNMPWalk(snmp_fd, &current_addr, CUPS_SNMP_VERSION_1,
68b10830
MS
220 _cupsSNMPDefaultCommunity(), prtMarkerSuppliesLevel,
221 CUPS_SUPPLY_TIMEOUT, backend_walk_cb, NULL);
568fa3fa
MS
222
223 if (page_count)
224 *page_count = -1;
225
226 if (printer_state)
227 *printer_state = -1;
228
229 if (num_supplies > 0)
230 {
745129be 231 int i, /* Looping var */
c8fef167 232 percent, /* Percent full */
745129be 233 new_state, /* New state value */
c8fef167
MS
234 change_state, /* State change */
235 new_supply_state = 0; /* Supply state */
568fa3fa
MS
236 char value[CUPS_MAX_SUPPLIES * 4],
237 /* marker-levels value string */
238 *ptr; /* Pointer into value string */
239 cups_snmp_t packet; /* SNMP response packet */
568fa3fa
MS
240
241 /*
242 * Generate the marker-levels value string...
243 */
244
245 for (i = 0, ptr = value; i < num_supplies; i ++, ptr += strlen(ptr))
246 {
a4845881 247 if (supplies[i].max_capacity > 0 && supplies[i].level >= 0)
07ed0e9a 248 percent = 100 * supplies[i].level / supplies[i].max_capacity;
a29fd7dd
MS
249 else if (supplies[i].level >= 0 && supplies[i].level <= 100 &&
250 (quirks & CUPS_SNMP_CAPACITY))
251 percent = supplies[i].level;
07ed0e9a
MS
252 else
253 percent = 50;
c8fef167 254
f7e3306a
MS
255 if (supplies[i].sclass == CUPS_TC_receptacleThatIsFilled)
256 percent = 100 - percent;
257
a4845881 258 if (percent <= 5)
c8fef167
MS
259 {
260 switch (supplies[i].type)
261 {
262 case CUPS_TC_toner :
263 case CUPS_TC_tonerCartridge :
264 if (percent <= 1)
265 new_supply_state |= CUPS_TONER_EMPTY;
266 else
267 new_supply_state |= CUPS_TONER_LOW;
268 break;
c8fef167
MS
269 case CUPS_TC_ink :
270 case CUPS_TC_inkCartridge :
271 case CUPS_TC_inkRibbon :
272 case CUPS_TC_solidWax :
273 case CUPS_TC_ribbonWax :
274 if (percent <= 1)
275 new_supply_state |= CUPS_MARKER_SUPPLY_EMPTY;
276 else
277 new_supply_state |= CUPS_MARKER_SUPPLY_LOW;
278 break;
279 case CUPS_TC_developer :
280 if (percent <= 1)
281 new_supply_state |= CUPS_DEVELOPER_EMPTY;
282 else
283 new_supply_state |= CUPS_DEVELOPER_LOW;
284 break;
285 case CUPS_TC_coronaWire :
286 case CUPS_TC_fuser :
287 case CUPS_TC_opc :
288 case CUPS_TC_transferUnit :
289 if (percent <= 1)
290 new_supply_state |= CUPS_OPC_LIFE_OVER;
291 else
292 new_supply_state |= CUPS_OPC_NEAR_EOL;
293 break;
bf766888 294#if 0 /* Because no two vendors report waste containers the same, disable SNMP reporting of same */
82cc1f9a
MS
295 case CUPS_TC_wasteInk :
296 case CUPS_TC_wastePaper :
297 case CUPS_TC_wasteToner :
298 case CUPS_TC_wasteWater :
299 case CUPS_TC_wasteWax :
300 if (percent <= 1)
301 new_supply_state |= CUPS_WASTE_FULL;
302 else
303 new_supply_state |= CUPS_WASTE_ALMOST_FULL;
304 break;
bf766888 305#endif /* 0 */
82cc1f9a
MS
306 case CUPS_TC_cleanerUnit :
307 case CUPS_TC_fuserCleaningPad :
308 if (percent <= 1)
309 new_supply_state |= CUPS_CLEANER_LIFE_OVER;
310 else
311 new_supply_state |= CUPS_CLEANER_NEAR_EOL;
312 break;
c8fef167
MS
313 }
314 }
315
568fa3fa
MS
316 if (i)
317 *ptr++ = ',';
318
a29fd7dd
MS
319 if ((supplies[i].max_capacity > 0 || (quirks & CUPS_SNMP_CAPACITY)) &&
320 supplies[i].level >= 0)
7e86f2f6 321 snprintf(ptr, sizeof(value) - (size_t)(ptr - value), "%d", percent);
06d4e77b 322 else
7e86f2f6 323 strlcpy(ptr, "-1", sizeof(value) - (size_t)(ptr - value));
568fa3fa
MS
324 }
325
326 fprintf(stderr, "ATTR: marker-levels=%s\n", value);
327
c8fef167
MS
328 if (supply_state < 0)
329 change_state = 0xffff;
330 else
331 change_state = supply_state ^ new_supply_state;
332
333 fprintf(stderr, "DEBUG: new_supply_state=%x, change_state=%x\n",
334 new_supply_state, change_state);
335
336 for (i = 0;
337 i < (int)(sizeof(supply_states) / sizeof(supply_states[0]));
338 i ++)
339 if (change_state & supply_states[i].bit)
340 {
341 fprintf(stderr, "STATE: %c%s\n",
342 (new_supply_state & supply_states[i].bit) ? '+' : '-',
343 supply_states[i].keyword);
344 }
345
346 supply_state = new_supply_state;
347
568fa3fa
MS
348 /*
349 * Get the current printer status bits...
350 */
351
7a14d768
MS
352 if (!_cupsSNMPWrite(snmp_fd, addr, CUPS_SNMP_VERSION_1,
353 _cupsSNMPDefaultCommunity(), CUPS_ASN1_GET_REQUEST, 1,
568fa3fa
MS
354 hrPrinterDetectedErrorState))
355 return (-1);
356
68b10830 357 if (!_cupsSNMPRead(snmp_fd, &packet, CUPS_SUPPLY_TIMEOUT) ||
568fa3fa
MS
358 packet.object_type != CUPS_ASN1_OCTET_STRING)
359 return (-1);
360
18ecb428
MS
361 if (packet.object_value.string.num_bytes == 2)
362 new_state = (packet.object_value.string.bytes[0] << 8) |
363 packet.object_value.string.bytes[1];
ef55b745
MS
364 else if (packet.object_value.string.num_bytes == 1)
365 new_state = (packet.object_value.string.bytes[0] << 8);
18ecb428
MS
366 else
367 new_state = 0;
568fa3fa 368
745129be
MS
369 if (current_state < 0)
370 change_state = 0xffff;
568fa3fa 371 else
745129be 372 change_state = current_state ^ new_state;
568fa3fa 373
c8fef167
MS
374 fprintf(stderr, "DEBUG: new_state=%x, change_state=%x\n", new_state,
375 change_state);
229681c1 376
745129be
MS
377 for (i = 0;
378 i < (int)(sizeof(printer_states) / sizeof(printer_states[0]));
379 i ++)
380 if (change_state & printer_states[i].bit)
229681c1 381 {
c8fef167
MS
382 fprintf(stderr, "STATE: %c%s\n",
383 (new_state & printer_states[i].bit) ? '+' : '-',
384 printer_states[i].keyword);
229681c1 385 }
568fa3fa 386
745129be 387 current_state = new_state;
568fa3fa
MS
388
389 /*
390 * Get the current printer state...
391 */
392
393 if (printer_state)
394 {
7a14d768
MS
395 if (!_cupsSNMPWrite(snmp_fd, addr, CUPS_SNMP_VERSION_1,
396 _cupsSNMPDefaultCommunity(), CUPS_ASN1_GET_REQUEST, 1,
568fa3fa
MS
397 hrPrinterStatus))
398 return (-1);
399
68b10830 400 if (!_cupsSNMPRead(snmp_fd, &packet, CUPS_SUPPLY_TIMEOUT) ||
568fa3fa
MS
401 packet.object_type != CUPS_ASN1_INTEGER)
402 return (-1);
403
404 *printer_state = packet.object_value.integer;
405 }
406
407 /*
408 * Get the current page count...
409 */
410
411 if (page_count)
412 {
7a14d768
MS
413 if (!_cupsSNMPWrite(snmp_fd, addr, CUPS_SNMP_VERSION_1,
414 _cupsSNMPDefaultCommunity(), CUPS_ASN1_GET_REQUEST, 1,
568fa3fa
MS
415 prtMarkerLifeCount))
416 return (-1);
417
68b10830 418 if (!_cupsSNMPRead(snmp_fd, &packet, CUPS_SUPPLY_TIMEOUT) ||
568fa3fa
MS
419 packet.object_type != CUPS_ASN1_COUNTER)
420 return (-1);
421
422 *page_count = packet.object_value.counter;
423 }
424
425 return (0);
426 }
427 else
428 return (-1);
429}
430
431
432/*
433 * 'backend_init_supplies()' - Initialize the supplies list.
434 */
435
436static void
437backend_init_supplies(
438 int snmp_fd, /* I - SNMP socket */
439 http_addr_t *addr) /* I - Printer address */
440{
441 int i, /* Looping var */
442 type; /* Current marker type */
443 cups_file_t *cachefile; /* Cache file */
444 const char *cachedir; /* CUPS_CACHEDIR value */
445 char addrstr[1024], /* Address string */
446 cachefilename[1024], /* Cache filename */
447 description[CUPS_SNMP_MAX_STRING],
448 /* Device description string */
12f89d24 449 value[CUPS_MAX_SUPPLIES * (CUPS_SNMP_MAX_STRING * 4 + 3)],
568fa3fa
MS
450 /* Value string */
451 *ptr, /* Pointer into value string */
452 *name_ptr; /* Pointer into name string */
453 cups_snmp_t packet; /* SNMP response packet */
58dc1933
MS
454 ppd_file_t *ppd; /* PPD file for this queue */
455 ppd_attr_t *ppdattr; /* cupsSNMPSupplies attribute */
568fa3fa
MS
456 static const char * const types[] = /* Supply types */
457 {
458 "other",
459 "unknown",
460 "toner",
5a9febac 461 "waste-toner",
568fa3fa 462 "ink",
5a9febac
MS
463 "ink-cartridge",
464 "ink-ribbon",
465 "waste-ink",
568fa3fa
MS
466 "opc",
467 "developer",
5a9febac
MS
468 "fuser-oil",
469 "solid-wax",
470 "ribbon-wax",
471 "waste-wax",
568fa3fa 472 "fuser",
5a9febac
MS
473 "corona-wire",
474 "fuser-oil-wick",
475 "cleaner-unit",
476 "fuser-cleaning-pad",
477 "transfer-unit",
478 "toner-cartridge",
479 "fuser-oiler",
568fa3fa 480 "water",
5a9febac
MS
481 "waste-water",
482 "glue-water-additive",
483 "waste-paper",
484 "binding-supply",
485 "banding-supply",
486 "stitching-wire",
487 "shrink-wrap",
488 "paper-wrap",
568fa3fa
MS
489 "staples",
490 "inserts",
491 "covers"
492 };
493
494
495 /*
496 * Reset state information...
497 */
498
745129be
MS
499 current_addr = *addr;
500 current_state = -1;
501 num_supplies = -1;
68b10830 502 charset = -1;
568fa3fa
MS
503
504 memset(supplies, 0, sizeof(supplies));
505
58dc1933
MS
506 /*
507 * See if we should be getting supply levels via SNMP...
508 */
509
f14324a7
MS
510 if ((ppd = ppdOpenFile(getenv("PPD"))) == NULL ||
511 ((ppdattr = ppdFindAttr(ppd, "cupsSNMPSupplies", NULL)) != NULL &&
88f9aafc 512 ppdattr->value && _cups_strcasecmp(ppdattr->value, "true")))
58dc1933
MS
513 {
514 ppdClose(ppd);
515 return;
516 }
517
a29fd7dd
MS
518 if ((ppdattr = ppdFindAttr(ppd, "cupsSNMPQuirks", NULL)) != NULL)
519 {
520 if (!_cups_strcasecmp(ppdattr->value, "capacity"))
521 quirks |= CUPS_SNMP_CAPACITY;
522 }
523
58dc1933
MS
524 ppdClose(ppd);
525
568fa3fa
MS
526 /*
527 * Get the device description...
528 */
529
7a14d768
MS
530 if (!_cupsSNMPWrite(snmp_fd, addr, CUPS_SNMP_VERSION_1,
531 _cupsSNMPDefaultCommunity(), CUPS_ASN1_GET_REQUEST, 1,
568fa3fa
MS
532 hrDeviceDescr))
533 return;
534
68b10830 535 if (!_cupsSNMPRead(snmp_fd, &packet, CUPS_SUPPLY_TIMEOUT) ||
568fa3fa
MS
536 packet.object_type != CUPS_ASN1_OCTET_STRING)
537 {
538 strlcpy(description, "Unknown", sizeof(description));
539 num_supplies = 0;
540 }
541 else
d1c13e16
MS
542 strlcpy(description, (char *)packet.object_value.string.bytes,
543 sizeof(description));
568fa3fa 544
68b10830
MS
545 fprintf(stderr, "DEBUG2: hrDeviceDesc=\"%s\"\n", description);
546
568fa3fa
MS
547 /*
548 * See if we have already queried this device...
549 */
550
551 httpAddrString(addr, addrstr, sizeof(addrstr));
552
553 if ((cachedir = getenv("CUPS_CACHEDIR")) == NULL)
554 cachedir = CUPS_CACHEDIR;
555
556 snprintf(cachefilename, sizeof(cachefilename), "%s/%s.snmp", cachedir,
557 addrstr);
558
559 if ((cachefile = cupsFileOpen(cachefilename, "r")) != NULL)
560 {
561 /*
562 * Yes, read the cache file:
563 *
f7e3306a 564 * 3 num_supplies charset
568fa3fa
MS
565 * device description
566 * supply structures...
567 */
568
569 if (cupsFileGets(cachefile, value, sizeof(value)))
570 {
f7e3306a 571 if (sscanf(value, "3 %d%d", &num_supplies, &charset) == 2 &&
568fa3fa
MS
572 num_supplies <= CUPS_MAX_SUPPLIES &&
573 cupsFileGets(cachefile, value, sizeof(value)))
574 {
568fa3fa
MS
575 if (!strcmp(description, value))
576 cupsFileRead(cachefile, (char *)supplies,
7e86f2f6 577 (size_t)num_supplies * sizeof(backend_supplies_t));
568fa3fa 578 else
68b10830 579 {
568fa3fa 580 num_supplies = -1;
68b10830
MS
581 charset = -1;
582 }
568fa3fa
MS
583 }
584 else
68b10830 585 {
568fa3fa 586 num_supplies = -1;
68b10830
MS
587 charset = -1;
588 }
568fa3fa
MS
589 }
590
591 cupsFileClose(cachefile);
592 }
593
594 /*
595 * If the cache information isn't correct, scan for supplies...
596 */
597
68b10830
MS
598 if (charset < 0)
599 {
600 /*
601 * Get the configured character set...
602 */
603
604 int oid[CUPS_SNMP_MAX_OID]; /* OID for character set */
605
606
607 if (!_cupsSNMPWrite(snmp_fd, &current_addr, CUPS_SNMP_VERSION_1,
608 _cupsSNMPDefaultCommunity(), CUPS_ASN1_GET_REQUEST, 1,
609 prtGeneralCurrentLocalization))
610 return;
611
612 if (!_cupsSNMPRead(snmp_fd, &packet, CUPS_SUPPLY_TIMEOUT) ||
613 packet.object_type != CUPS_ASN1_INTEGER)
614 {
615 fprintf(stderr,
616 "DEBUG: prtGeneralCurrentLocalization type is %x, expected %x!\n",
617 packet.object_type, CUPS_ASN1_INTEGER);
618 return;
619 }
620
621 fprintf(stderr, "DEBUG2: prtGeneralCurrentLocalization=%d\n",
622 packet.object_value.integer);
623
624 _cupsSNMPCopyOID(oid, prtLocalizationCharacterSet, CUPS_SNMP_MAX_OID);
625 oid[prtLocalizationCharacterSetOffset - 2] = packet.object_value.integer;
626
627
628 if (!_cupsSNMPWrite(snmp_fd, &current_addr, CUPS_SNMP_VERSION_1,
629 _cupsSNMPDefaultCommunity(), CUPS_ASN1_GET_REQUEST, 1,
630 oid))
631 return;
632
633 if (!_cupsSNMPRead(snmp_fd, &packet, CUPS_SUPPLY_TIMEOUT) ||
634 packet.object_type != CUPS_ASN1_INTEGER)
635 {
636 fprintf(stderr,
637 "DEBUG: prtLocalizationCharacterSet type is %x, expected %x!\n",
638 packet.object_type, CUPS_ASN1_INTEGER);
639 return;
640 }
641
642 fprintf(stderr, "DEBUG2: prtLocalizationCharacterSet=%d\n",
643 packet.object_value.integer);
644 charset = packet.object_value.integer;
645 }
646
568fa3fa
MS
647 if (num_supplies < 0)
648 {
649 /*
650 * Walk the printer configuration information...
651 */
652
7a14d768 653 _cupsSNMPWalk(snmp_fd, &current_addr, CUPS_SNMP_VERSION_1,
68b10830
MS
654 _cupsSNMPDefaultCommunity(), prtMarkerSuppliesEntry,
655 CUPS_SUPPLY_TIMEOUT, backend_walk_cb, NULL);
568fa3fa
MS
656 }
657
658 /*
659 * Save the cached information...
660 */
661
662 if (num_supplies < 0)
663 num_supplies = 0;
664
665 if ((cachefile = cupsFileOpen(cachefilename, "w")) != NULL)
666 {
f7e3306a 667 cupsFilePrintf(cachefile, "3 %d %d\n", num_supplies, charset);
568fa3fa
MS
668 cupsFilePrintf(cachefile, "%s\n", description);
669
670 if (num_supplies > 0)
671 cupsFileWrite(cachefile, (char *)supplies,
7e86f2f6 672 (size_t)num_supplies * sizeof(backend_supplies_t));
568fa3fa
MS
673
674 cupsFileClose(cachefile);
675 }
676
677 if (num_supplies <= 0)
678 return;
679
680 /*
681 * Get the colors...
682 */
683
684 for (i = 0; i < num_supplies; i ++)
5a9febac 685 strlcpy(supplies[i].color, "none", sizeof(supplies[i].color));
568fa3fa 686
7a14d768 687 _cupsSNMPWalk(snmp_fd, &current_addr, CUPS_SNMP_VERSION_1,
68b10830
MS
688 _cupsSNMPDefaultCommunity(), prtMarkerColorantValue,
689 CUPS_SUPPLY_TIMEOUT, backend_walk_cb, NULL);
568fa3fa
MS
690
691 /*
692 * Output the marker-colors attribute...
693 */
694
695 for (i = 0, ptr = value; i < num_supplies; i ++, ptr += strlen(ptr))
696 {
697 if (i)
698 *ptr++ = ',';
699
7e86f2f6 700 strlcpy(ptr, supplies[i].color, sizeof(value) - (size_t)(ptr - value));
568fa3fa
MS
701 }
702
703 fprintf(stderr, "ATTR: marker-colors=%s\n", value);
704
705 /*
12f89d24
MS
706 * Output the marker-names attribute (the double quoting is necessary to deal
707 * with embedded quotes and commas in the marker names...)
568fa3fa
MS
708 */
709
710 for (i = 0, ptr = value; i < num_supplies; i ++)
711 {
712 if (i)
713 *ptr++ = ',';
714
12f89d24 715 *ptr++ = '\'';
568fa3fa
MS
716 *ptr++ = '\"';
717 for (name_ptr = supplies[i].name; *name_ptr;)
718 {
12f89d24
MS
719 if (*name_ptr == '\\' || *name_ptr == '\"' || *name_ptr == '\'')
720 {
721 *ptr++ = '\\';
722 *ptr++ = '\\';
568fa3fa 723 *ptr++ = '\\';
12f89d24 724 }
568fa3fa
MS
725
726 *ptr++ = *name_ptr++;
727 }
728 *ptr++ = '\"';
12f89d24 729 *ptr++ = '\'';
568fa3fa
MS
730 }
731
732 *ptr = '\0';
733
734 fprintf(stderr, "ATTR: marker-names=%s\n", value);
735
736 /*
737 * Output the marker-types attribute...
738 */
739
740 for (i = 0, ptr = value; i < num_supplies; i ++, ptr += strlen(ptr))
741 {
742 if (i)
743 *ptr++ = ',';
744
745 type = supplies[i].type;
746
747 if (type < CUPS_TC_other || type > CUPS_TC_covers)
7e86f2f6 748 strlcpy(ptr, "unknown", sizeof(value) - (size_t)(ptr - value));
568fa3fa 749 else
7e86f2f6 750 strlcpy(ptr, types[type - CUPS_TC_other], sizeof(value) - (size_t)(ptr - value));
568fa3fa
MS
751 }
752
753 fprintf(stderr, "ATTR: marker-types=%s\n", value);
754}
755
756
757/*
68b10830 758 * 'backend_walk_cb()' - Interpret the supply value responses.
568fa3fa
MS
759 */
760
761static void
762backend_walk_cb(cups_snmp_t *packet, /* I - SNMP packet */
763 void *data) /* I - User data (unused) */
764{
765 int i, j, k; /* Looping vars */
12f89d24 766 static const char * const colors[][2] =
568fa3fa 767 { /* Standard color names */
12f89d24
MS
768 { "black", "#000000" },
769 { "blue", "#0000FF" },
770 { "brown", "#A52A2A" },
771 { "cyan", "#00FFFF" },
772 { "dark-gray", "#404040" },
773 { "dark gray", "#404040" },
774 { "dark-yellow", "#FFCC00" },
775 { "dark yellow", "#FFCC00" },
776 { "gold", "#FFD700" },
777 { "gray", "#808080" },
778 { "green", "#00FF00" },
779 { "light-black", "#606060" },
780 { "light black", "#606060" },
781 { "light-cyan", "#E0FFFF" },
782 { "light cyan", "#E0FFFF" },
783 { "light-gray", "#D3D3D3" },
784 { "light gray", "#D3D3D3" },
785 { "light-magenta", "#FF77FF" },
786 { "light magenta", "#FF77FF" },
787 { "magenta", "#FF00FF" },
788 { "orange", "#FFA500" },
789 { "red", "#FF0000" },
790 { "silver", "#C0C0C0" },
791 { "white", "#FFFFFF" },
792 { "yellow", "#FFFF00" }
568fa3fa
MS
793 };
794
795
796 (void)data;
797
7a14d768 798 if (_cupsSNMPIsOIDPrefixed(packet, prtMarkerColorantValue) &&
568fa3fa
MS
799 packet->object_type == CUPS_ASN1_OCTET_STRING)
800 {
801 /*
802 * Get colorant...
803 */
804
805 i = packet->object_name[prtMarkerColorantValueOffset];
806
807 fprintf(stderr, "DEBUG2: prtMarkerColorantValue.1.%d = \"%s\"\n", i,
d1c13e16 808 (char *)packet->object_value.string.bytes);
568fa3fa
MS
809
810 for (j = 0; j < num_supplies; j ++)
811 if (supplies[j].colorant == i)
812 {
813 for (k = 0; k < (int)(sizeof(colors) / sizeof(colors[0])); k ++)
12f89d24
MS
814 if (!_cups_strcasecmp(colors[k][0],
815 (char *)packet->object_value.string.bytes))
568fa3fa 816 {
5a9febac 817 strlcpy(supplies[j].color, colors[k][1], sizeof(supplies[j].color));
568fa3fa
MS
818 break;
819 }
820 }
821 }
7a14d768 822 else if (_cupsSNMPIsOIDPrefixed(packet, prtMarkerSuppliesColorantIndex))
568fa3fa
MS
823 {
824 /*
825 * Get colorant index...
826 */
827
828 i = packet->object_name[prtMarkerSuppliesColorantIndexOffset];
829 if (i < 1 || i > CUPS_MAX_SUPPLIES ||
830 packet->object_type != CUPS_ASN1_INTEGER)
831 return;
832
833 fprintf(stderr, "DEBUG2: prtMarkerSuppliesColorantIndex.1.%d = %d\n", i,
834 packet->object_value.integer);
835
836 if (i > num_supplies)
837 num_supplies = i;
838
839 supplies[i - 1].colorant = packet->object_value.integer;
840 }
7a14d768 841 else if (_cupsSNMPIsOIDPrefixed(packet, prtMarkerSuppliesDescription))
568fa3fa
MS
842 {
843 /*
844 * Get supply name/description...
845 */
846
847 i = packet->object_name[prtMarkerSuppliesDescriptionOffset];
848 if (i < 1 || i > CUPS_MAX_SUPPLIES ||
849 packet->object_type != CUPS_ASN1_OCTET_STRING)
850 return;
851
568fa3fa
MS
852 if (i > num_supplies)
853 num_supplies = i;
854
68b10830
MS
855 switch (charset)
856 {
857 case CUPS_TC_csASCII :
858 case CUPS_TC_csUTF8 :
859 case CUPS_TC_csUnicodeASCII :
860 strlcpy(supplies[i - 1].name,
861 (char *)packet->object_value.string.bytes,
862 sizeof(supplies[0].name));
863 break;
864
865 case CUPS_TC_csISOLatin1 :
866 case CUPS_TC_csUnicodeLatin1 :
867 cupsCharsetToUTF8((cups_utf8_t *)supplies[i - 1].name,
868 (char *)packet->object_value.string.bytes,
869 sizeof(supplies[0].name), CUPS_ISO8859_1);
870 break;
871
872 case CUPS_TC_csShiftJIS :
a2326b5b 873 case CUPS_TC_csWindows31J : /* Close enough for our purposes */
68b10830
MS
874 cupsCharsetToUTF8((cups_utf8_t *)supplies[i - 1].name,
875 (char *)packet->object_value.string.bytes,
876 sizeof(supplies[0].name), CUPS_JIS_X0213);
877 break;
878
879 case CUPS_TC_csUCS4 :
880 case CUPS_TC_csUTF32 :
881 case CUPS_TC_csUTF32BE :
882 case CUPS_TC_csUTF32LE :
883 cupsUTF32ToUTF8((cups_utf8_t *)supplies[i - 1].name,
884 (cups_utf32_t *)packet->object_value.string.bytes,
885 sizeof(supplies[0].name));
886 break;
887
888 case CUPS_TC_csUnicode :
889 case CUPS_TC_csUTF16BE :
890 case CUPS_TC_csUTF16LE :
891 utf16_to_utf8((cups_utf8_t *)supplies[i - 1].name,
892 packet->object_value.string.bytes,
893 packet->object_value.string.num_bytes,
894 sizeof(supplies[0].name), charset == CUPS_TC_csUTF16LE);
895 break;
896
897 default :
898 /*
899 * If we get here, the printer is using an unknown character set and
900 * we just want to copy characters that look like ASCII...
901 */
902
903 {
904 char *src, *dst; /* Pointers into strings */
905
68b10830
MS
906 /*
907 * Loop safe because both the object_value and supplies char arrays
908 * are CUPS_SNMP_MAX_STRING elements long.
909 */
910
911 for (src = (char *)packet->object_value.string.bytes,
912 dst = supplies[i - 1].name;
913 *src;
914 src ++)
915 {
916 if ((*src & 0x80) || *src < ' ' || *src == 0x7f)
917 *dst++ = '?';
918 else
919 *dst++ = *src;
920 }
921
922 *dst = '\0';
923 }
924 break;
925 }
926
927 fprintf(stderr, "DEBUG2: prtMarkerSuppliesDescription.1.%d = \"%s\"\n", i,
928 supplies[i - 1].name);
929
568fa3fa 930 }
7a14d768 931 else if (_cupsSNMPIsOIDPrefixed(packet, prtMarkerSuppliesLevel))
568fa3fa
MS
932 {
933 /*
934 * Get level...
935 */
936
937 i = packet->object_name[prtMarkerSuppliesLevelOffset];
938 if (i < 1 || i > CUPS_MAX_SUPPLIES ||
939 packet->object_type != CUPS_ASN1_INTEGER)
940 return;
941
942 fprintf(stderr, "DEBUG2: prtMarkerSuppliesLevel.1.%d = %d\n", i,
943 packet->object_value.integer);
944
945 if (i > num_supplies)
946 num_supplies = i;
947
948 supplies[i - 1].level = packet->object_value.integer;
949 }
a29fd7dd
MS
950 else if (_cupsSNMPIsOIDPrefixed(packet, prtMarkerSuppliesMaxCapacity) &&
951 !(quirks & CUPS_SNMP_CAPACITY))
568fa3fa
MS
952 {
953 /*
954 * Get max capacity...
955 */
956
957 i = packet->object_name[prtMarkerSuppliesMaxCapacityOffset];
958 if (i < 1 || i > CUPS_MAX_SUPPLIES ||
959 packet->object_type != CUPS_ASN1_INTEGER)
960 return;
961
962 fprintf(stderr, "DEBUG2: prtMarkerSuppliesMaxCapacity.1.%d = %d\n", i,
963 packet->object_value.integer);
964
965 if (i > num_supplies)
966 num_supplies = i;
967
82cc1f9a
MS
968 if (supplies[i - 1].max_capacity == 0 &&
969 packet->object_value.integer > 0)
970 supplies[i - 1].max_capacity = packet->object_value.integer;
568fa3fa 971 }
f7e3306a
MS
972 else if (_cupsSNMPIsOIDPrefixed(packet, prtMarkerSuppliesClass))
973 {
974 /*
975 * Get marker class...
976 */
977
978 i = packet->object_name[prtMarkerSuppliesClassOffset];
979 if (i < 1 || i > CUPS_MAX_SUPPLIES ||
980 packet->object_type != CUPS_ASN1_INTEGER)
981 return;
982
983 fprintf(stderr, "DEBUG2: prtMarkerSuppliesClass.1.%d = %d\n", i,
984 packet->object_value.integer);
985
986 if (i > num_supplies)
987 num_supplies = i;
988
989 supplies[i - 1].sclass = packet->object_value.integer;
990 }
7a14d768 991 else if (_cupsSNMPIsOIDPrefixed(packet, prtMarkerSuppliesType))
568fa3fa
MS
992 {
993 /*
994 * Get marker type...
995 */
996
997 i = packet->object_name[prtMarkerSuppliesTypeOffset];
998 if (i < 1 || i > CUPS_MAX_SUPPLIES ||
999 packet->object_type != CUPS_ASN1_INTEGER)
1000 return;
1001
1002 fprintf(stderr, "DEBUG2: prtMarkerSuppliesType.1.%d = %d\n", i,
1003 packet->object_value.integer);
1004
1005 if (i > num_supplies)
1006 num_supplies = i;
1007
1008 supplies[i - 1].type = packet->object_value.integer;
1009 }
82cc1f9a
MS
1010 else if (_cupsSNMPIsOIDPrefixed(packet, prtMarkerSuppliesSupplyUnit))
1011 {
1012 /*
1013 * Get units for capacity...
1014 */
1015
1016 i = packet->object_name[prtMarkerSuppliesSupplyUnitOffset];
1017 if (i < 1 || i > CUPS_MAX_SUPPLIES ||
1018 packet->object_type != CUPS_ASN1_INTEGER)
1019 return;
1020
1021 fprintf(stderr, "DEBUG2: prtMarkerSuppliesSupplyUnit.1.%d = %d\n", i,
1022 packet->object_value.integer);
1023
1024 if (i > num_supplies)
1025 num_supplies = i;
1026
1027 if (packet->object_value.integer == CUPS_TC_percent)
1028 supplies[i - 1].max_capacity = 100;
1029 }
568fa3fa
MS
1030}
1031
1032
68b10830
MS
1033/*
1034 * 'utf16_to_utf8()' - Convert UTF-16 text to UTF-8.
1035 */
1036
1037static void
1038utf16_to_utf8(
1039 cups_utf8_t *dst, /* I - Destination buffer */
1040 const unsigned char *src, /* I - Source string */
1041 size_t srcsize, /* I - Size of source string */
1042 size_t dstsize, /* I - Size of destination buffer */
1043 int le) /* I - Source is little-endian? */
1044{
1045 cups_utf32_t ch, /* Current character */
1046 temp[CUPS_SNMP_MAX_STRING],
1047 /* UTF-32 string */
1048 *ptr; /* Pointer into UTF-32 string */
1049
1050
1051 for (ptr = temp; srcsize >= 2;)
1052 {
1053 if (le)
7e86f2f6 1054 ch = (cups_utf32_t)(src[0] | (src[1] << 8));
68b10830 1055 else
7e86f2f6 1056 ch = (cups_utf32_t)((src[0] << 8) | src[1]);
68b10830
MS
1057
1058 src += 2;
1059 srcsize -= 2;
1060
1061 if (ch >= 0xd800 && ch <= 0xdbff && srcsize >= 2)
1062 {
1063 /*
1064 * Multi-word UTF-16 char...
1065 */
1066
7e86f2f6 1067 cups_utf32_t lch; /* Lower word */
68b10830
MS
1068
1069
1070 if (le)
7e86f2f6 1071 lch = (cups_utf32_t)(src[0] | (src[1] << 8));
68b10830 1072 else
7e86f2f6 1073 lch = (cups_utf32_t)((src[0] << 8) | src[1]);
68b10830
MS
1074
1075 if (lch >= 0xdc00 && lch <= 0xdfff)
1076 {
1077 src += 2;
1078 srcsize -= 2;
1079
1080 ch = (((ch & 0x3ff) << 10) | (lch & 0x3ff)) + 0x10000;
1081 }
1082 }
1083
1084 if (ptr < (temp + CUPS_SNMP_MAX_STRING - 1))
1085 *ptr++ = ch;
1086 }
1087
1088 *ptr = '\0';
1089
7e86f2f6 1090 cupsUTF32ToUTF8(dst, temp, (int)dstsize);
68b10830 1091}