4 * SNMP supplies functions for CUPS.
6 * Copyright 2008-2015 by Apple Inc.
8 * These coded instructions, statements, and computer programs are the
9 * property of Apple Inc. and are protected by Federal copyright
10 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
11 * "LICENSE" which should have been included with this file. If this
12 * file is missing or damaged, see the license at "http://www.cups.org/".
14 * This file is subject to the Apple OS-Developed Software exception.
18 * Include necessary headers.
21 #include "backend-private.h"
22 #include <cups/ppd-private.h>
23 #include <cups/array.h>
30 #define CUPS_MAX_SUPPLIES 32 /* Maximum number of supplies for a printer */
31 #define CUPS_SUPPLY_TIMEOUT 2.0 /* Timeout for SNMP lookups */
33 #define CUPS_DEVELOPER_LOW 0x0001
34 #define CUPS_DEVELOPER_EMPTY 0x0002
35 #define CUPS_MARKER_SUPPLY_LOW 0x0004
36 #define CUPS_MARKER_SUPPLY_EMPTY 0x0008
37 #define CUPS_OPC_NEAR_EOL 0x0010
38 #define CUPS_OPC_LIFE_OVER 0x0020
39 #define CUPS_TONER_LOW 0x0040
40 #define CUPS_TONER_EMPTY 0x0080
41 #define CUPS_WASTE_ALMOST_FULL 0x0100
42 #define CUPS_WASTE_FULL 0x0200
43 #define CUPS_CLEANER_NEAR_EOL 0x0400 /* Proposed JPS3 */
44 #define CUPS_CLEANER_LIFE_OVER 0x0800 /* Proposed JPS3 */
46 #define CUPS_SNMP_NONE 0x0000
47 #define CUPS_SNMP_CAPACITY 0x0001 /* Supply levels reported as percentages */
54 typedef struct /**** Printer supply data ****/
56 char name
[CUPS_SNMP_MAX_STRING
], /* Name of supply */
57 color
[8]; /* Color: "#RRGGBB" or "none" */
58 int colorant
, /* Colorant index */
59 sclass
, /* Supply class */
60 type
, /* Supply type */
61 max_capacity
, /* Maximum capacity */
62 level
; /* Current level value */
65 typedef struct /**** Printer state table ****/
67 int bit
; /* State bit */
68 const char *keyword
; /* IPP printer-state-reasons keyword */
76 static http_addr_t current_addr
; /* Current address */
77 static int current_state
= -1;
78 /* Current device state bits */
79 static int charset
= -1; /* Character set for supply names */
80 static unsigned quirks
= CUPS_SNMP_NONE
;
81 /* Quirks we have to work around */
82 static int num_supplies
= 0;
83 /* Number of supplies found */
84 static backend_supplies_t supplies
[CUPS_MAX_SUPPLIES
];
85 /* Supply information */
86 static int supply_state
= -1;
87 /* Supply state info */
89 static const int hrDeviceDescr
[] =
90 { CUPS_OID_hrDeviceDescr
, 1, -1 };
91 /* Device description OID */
92 static const int hrPrinterStatus
[] =
93 { CUPS_OID_hrPrinterStatus
, 1, -1 };
94 /* Current state OID */
95 static const int hrPrinterDetectedErrorState
[] =
96 { CUPS_OID_hrPrinterDetectedErrorState
, 1, -1 };
97 /* Current printer state bits OID */
98 static const int prtGeneralCurrentLocalization
[] =
99 { CUPS_OID_prtGeneralCurrentLocalization
, 1, -1 };
100 static const int prtLocalizationCharacterSet
[] =
101 { CUPS_OID_prtLocalizationCharacterSet
, 1, 1, -1 },
102 prtLocalizationCharacterSetOffset
=
103 (sizeof(prtLocalizationCharacterSet
) /
104 sizeof(prtLocalizationCharacterSet
[0]));
105 static const int prtMarkerColorantValue
[] =
106 { CUPS_OID_prtMarkerColorantValue
, -1 },
108 prtMarkerColorantValueOffset
=
109 (sizeof(prtMarkerColorantValue
) /
110 sizeof(prtMarkerColorantValue
[0]));
111 /* Offset to colorant index */
112 static const int prtMarkerLifeCount
[] =
113 { CUPS_OID_prtMarkerLifeCount
, 1, 1, -1 };
114 /* Page counter OID */
115 static const int prtMarkerSuppliesEntry
[] =
116 { CUPS_OID_prtMarkerSuppliesEntry
, -1 };
118 static const int prtMarkerSuppliesColorantIndex
[] =
119 { CUPS_OID_prtMarkerSuppliesColorantIndex
, -1 },
120 /* Colorant index OID */
121 prtMarkerSuppliesColorantIndexOffset
=
122 (sizeof(prtMarkerSuppliesColorantIndex
) /
123 sizeof(prtMarkerSuppliesColorantIndex
[0]));
124 /* Offset to supply index */
125 static const int prtMarkerSuppliesDescription
[] =
126 { CUPS_OID_prtMarkerSuppliesDescription
, -1 },
127 /* Description OID */
128 prtMarkerSuppliesDescriptionOffset
=
129 (sizeof(prtMarkerSuppliesDescription
) /
130 sizeof(prtMarkerSuppliesDescription
[0]));
131 /* Offset to supply index */
132 static const int prtMarkerSuppliesLevel
[] =
133 { CUPS_OID_prtMarkerSuppliesLevel
, -1 },
135 prtMarkerSuppliesLevelOffset
=
136 (sizeof(prtMarkerSuppliesLevel
) /
137 sizeof(prtMarkerSuppliesLevel
[0]));
138 /* Offset to supply index */
139 static const int prtMarkerSuppliesMaxCapacity
[] =
140 { CUPS_OID_prtMarkerSuppliesMaxCapacity
, -1 },
141 /* Max capacity OID */
142 prtMarkerSuppliesMaxCapacityOffset
=
143 (sizeof(prtMarkerSuppliesMaxCapacity
) /
144 sizeof(prtMarkerSuppliesMaxCapacity
[0]));
145 /* Offset to supply index */
146 static const int prtMarkerSuppliesClass
[] =
147 { CUPS_OID_prtMarkerSuppliesClass
, -1 },
149 prtMarkerSuppliesClassOffset
=
150 (sizeof(prtMarkerSuppliesClass
) /
151 sizeof(prtMarkerSuppliesClass
[0]));
152 /* Offset to supply index */
153 static const int prtMarkerSuppliesType
[] =
154 { CUPS_OID_prtMarkerSuppliesType
, -1 },
156 prtMarkerSuppliesTypeOffset
=
157 (sizeof(prtMarkerSuppliesType
) /
158 sizeof(prtMarkerSuppliesType
[0]));
159 /* Offset to supply index */
160 static const int prtMarkerSuppliesSupplyUnit
[] =
161 { CUPS_OID_prtMarkerSuppliesSupplyUnit
, -1 },
163 prtMarkerSuppliesSupplyUnitOffset
=
164 (sizeof(prtMarkerSuppliesSupplyUnit
) /
165 sizeof(prtMarkerSuppliesSupplyUnit
[0]));
166 /* Offset to supply index */
168 static const backend_state_t printer_states
[] =
170 /* { CUPS_TC_lowPaper, "media-low-report" }, */
171 { CUPS_TC_noPaper
| CUPS_TC_inputTrayEmpty
, "media-empty-warning" },
172 /* { CUPS_TC_lowToner, "toner-low-report" }, */ /* now use prtMarkerSupplies */
173 /* { CUPS_TC_noToner, "toner-empty-warning" }, */ /* now use prtMarkerSupplies */
174 { CUPS_TC_doorOpen
, "door-open-report" },
175 { CUPS_TC_jammed
, "media-jam-warning" },
176 /* { CUPS_TC_offline, "offline-report" }, */ /* unreliable */
177 /* { CUPS_TC_serviceRequested | CUPS_TC_overduePreventMaint, "service-needed-warning" }, */ /* unreliable */
178 { CUPS_TC_inputTrayMissing
, "input-tray-missing-warning" },
179 { CUPS_TC_outputTrayMissing
, "output-tray-missing-warning" },
180 { CUPS_TC_markerSupplyMissing
, "marker-supply-missing-warning" },
181 { CUPS_TC_outputNearFull
, "output-area-almost-full-report" },
182 { CUPS_TC_outputFull
, "output-area-full-warning" }
185 static const backend_state_t supply_states
[] =
187 { CUPS_DEVELOPER_LOW
, "developer-low-report" },
188 { CUPS_DEVELOPER_EMPTY
, "developer-empty-warning" },
189 { CUPS_MARKER_SUPPLY_LOW
, "marker-supply-low-report" },
190 { CUPS_MARKER_SUPPLY_EMPTY
, "marker-supply-empty-warning" },
191 { CUPS_OPC_NEAR_EOL
, "opc-near-eol-report" },
192 { CUPS_OPC_LIFE_OVER
, "opc-life-over-warning" },
193 { CUPS_TONER_LOW
, "toner-low-report" },
194 { CUPS_TONER_EMPTY
, "toner-empty-warning" },
195 { CUPS_WASTE_ALMOST_FULL
, "waste-receptacle-almost-full-report" },
196 { CUPS_WASTE_FULL
, "waste-receptacle-full-warning" },
197 { CUPS_CLEANER_NEAR_EOL
, "cleaner-life-almost-over-report" },
198 { CUPS_CLEANER_LIFE_OVER
, "cleaner-life-over-warning" },
206 static void backend_init_supplies(int snmp_fd
, http_addr_t
*addr
);
207 static void backend_walk_cb(cups_snmp_t
*packet
, void *data
);
208 static void utf16_to_utf8(cups_utf8_t
*dst
, const unsigned char *src
,
209 size_t srcsize
, size_t dstsize
, int le
);
213 * 'backendSNMPSupplies()' - Get the current supplies for a device.
216 int /* O - 0 on success, -1 on error */
218 int snmp_fd
, /* I - SNMP socket */
219 http_addr_t
*addr
, /* I - Printer address */
220 int *page_count
, /* O - Page count */
221 int *printer_state
) /* O - Printer state */
223 if (!httpAddrEqual(addr
, ¤t_addr
))
224 backend_init_supplies(snmp_fd
, addr
);
225 else if (num_supplies
> 0)
226 _cupsSNMPWalk(snmp_fd
, ¤t_addr
, CUPS_SNMP_VERSION_1
,
227 _cupsSNMPDefaultCommunity(), prtMarkerSuppliesLevel
,
228 CUPS_SUPPLY_TIMEOUT
, backend_walk_cb
, NULL
);
236 if (num_supplies
> 0)
238 int i
, /* Looping var */
239 percent
, /* Percent full */
240 new_state
, /* New state value */
241 change_state
, /* State change */
242 new_supply_state
= 0; /* Supply state */
243 char value
[CUPS_MAX_SUPPLIES
* 4],
244 /* marker-levels value string */
245 *ptr
; /* Pointer into value string */
246 cups_snmp_t packet
; /* SNMP response packet */
249 * Generate the marker-levels value string...
252 for (i
= 0, ptr
= value
; i
< num_supplies
; i
++, ptr
+= strlen(ptr
))
254 if (supplies
[i
].max_capacity
> 0 && supplies
[i
].level
>= 0)
255 percent
= 100 * supplies
[i
].level
/ supplies
[i
].max_capacity
;
256 else if (supplies
[i
].level
>= 0 && supplies
[i
].level
<= 100 &&
257 (quirks
& CUPS_SNMP_CAPACITY
))
258 percent
= supplies
[i
].level
;
262 if (supplies
[i
].sclass
== CUPS_TC_receptacleThatIsFilled
)
263 percent
= 100 - percent
;
267 switch (supplies
[i
].type
)
270 case CUPS_TC_tonerCartridge
:
272 new_supply_state
|= CUPS_TONER_EMPTY
;
274 new_supply_state
|= CUPS_TONER_LOW
;
277 case CUPS_TC_inkCartridge
:
278 case CUPS_TC_inkRibbon
:
279 case CUPS_TC_solidWax
:
280 case CUPS_TC_ribbonWax
:
282 new_supply_state
|= CUPS_MARKER_SUPPLY_EMPTY
;
284 new_supply_state
|= CUPS_MARKER_SUPPLY_LOW
;
286 case CUPS_TC_developer
:
288 new_supply_state
|= CUPS_DEVELOPER_EMPTY
;
290 new_supply_state
|= CUPS_DEVELOPER_LOW
;
292 case CUPS_TC_coronaWire
:
295 case CUPS_TC_transferUnit
:
297 new_supply_state
|= CUPS_OPC_LIFE_OVER
;
299 new_supply_state
|= CUPS_OPC_NEAR_EOL
;
301 #if 0 /* Because no two vendors report waste containers the same, disable SNMP reporting of same */
302 case CUPS_TC_wasteInk
:
303 case CUPS_TC_wastePaper
:
304 case CUPS_TC_wasteToner
:
305 case CUPS_TC_wasteWater
:
306 case CUPS_TC_wasteWax
:
308 new_supply_state
|= CUPS_WASTE_FULL
;
310 new_supply_state
|= CUPS_WASTE_ALMOST_FULL
;
313 case CUPS_TC_cleanerUnit
:
314 case CUPS_TC_fuserCleaningPad
:
316 new_supply_state
|= CUPS_CLEANER_LIFE_OVER
;
318 new_supply_state
|= CUPS_CLEANER_NEAR_EOL
;
326 if ((supplies
[i
].max_capacity
> 0 || (quirks
& CUPS_SNMP_CAPACITY
)) &&
327 supplies
[i
].level
>= 0)
328 snprintf(ptr
, sizeof(value
) - (size_t)(ptr
- value
), "%d", percent
);
330 strlcpy(ptr
, "-1", sizeof(value
) - (size_t)(ptr
- value
));
333 fprintf(stderr
, "ATTR: marker-levels=%s\n", value
);
335 if (supply_state
< 0)
336 change_state
= 0xffff;
338 change_state
= supply_state
^ new_supply_state
;
340 fprintf(stderr
, "DEBUG: new_supply_state=%x, change_state=%x\n",
341 new_supply_state
, change_state
);
344 i
< (int)(sizeof(supply_states
) / sizeof(supply_states
[0]));
346 if (change_state
& supply_states
[i
].bit
)
348 fprintf(stderr
, "STATE: %c%s\n",
349 (new_supply_state
& supply_states
[i
].bit
) ? '+' : '-',
350 supply_states
[i
].keyword
);
353 supply_state
= new_supply_state
;
356 * Get the current printer status bits...
359 if (!_cupsSNMPWrite(snmp_fd
, addr
, CUPS_SNMP_VERSION_1
,
360 _cupsSNMPDefaultCommunity(), CUPS_ASN1_GET_REQUEST
, 1,
361 hrPrinterDetectedErrorState
))
364 if (!_cupsSNMPRead(snmp_fd
, &packet
, CUPS_SUPPLY_TIMEOUT
) ||
365 packet
.object_type
!= CUPS_ASN1_OCTET_STRING
)
368 if (packet
.object_value
.string
.num_bytes
== 2)
369 new_state
= (packet
.object_value
.string
.bytes
[0] << 8) |
370 packet
.object_value
.string
.bytes
[1];
371 else if (packet
.object_value
.string
.num_bytes
== 1)
372 new_state
= (packet
.object_value
.string
.bytes
[0] << 8);
376 if (current_state
< 0)
377 change_state
= 0xffff;
379 change_state
= current_state
^ new_state
;
381 fprintf(stderr
, "DEBUG: new_state=%x, change_state=%x\n", new_state
,
385 i
< (int)(sizeof(printer_states
) / sizeof(printer_states
[0]));
387 if (change_state
& printer_states
[i
].bit
)
389 fprintf(stderr
, "STATE: %c%s\n",
390 (new_state
& printer_states
[i
].bit
) ? '+' : '-',
391 printer_states
[i
].keyword
);
394 current_state
= new_state
;
397 * Get the current printer state...
402 if (!_cupsSNMPWrite(snmp_fd
, addr
, CUPS_SNMP_VERSION_1
,
403 _cupsSNMPDefaultCommunity(), CUPS_ASN1_GET_REQUEST
, 1,
407 if (!_cupsSNMPRead(snmp_fd
, &packet
, CUPS_SUPPLY_TIMEOUT
) ||
408 packet
.object_type
!= CUPS_ASN1_INTEGER
)
411 *printer_state
= packet
.object_value
.integer
;
415 * Get the current page count...
420 if (!_cupsSNMPWrite(snmp_fd
, addr
, CUPS_SNMP_VERSION_1
,
421 _cupsSNMPDefaultCommunity(), CUPS_ASN1_GET_REQUEST
, 1,
425 if (!_cupsSNMPRead(snmp_fd
, &packet
, CUPS_SUPPLY_TIMEOUT
) ||
426 packet
.object_type
!= CUPS_ASN1_COUNTER
)
429 *page_count
= packet
.object_value
.counter
;
440 * 'backend_init_supplies()' - Initialize the supplies list.
444 backend_init_supplies(
445 int snmp_fd
, /* I - SNMP socket */
446 http_addr_t
*addr
) /* I - Printer address */
448 int i
, /* Looping var */
449 type
; /* Current marker type */
450 cups_file_t
*cachefile
; /* Cache file */
451 const char *cachedir
; /* CUPS_CACHEDIR value */
452 char addrstr
[1024], /* Address string */
453 cachefilename
[1024], /* Cache filename */
454 description
[CUPS_SNMP_MAX_STRING
],
455 /* Device description string */
456 value
[CUPS_MAX_SUPPLIES
* (CUPS_SNMP_MAX_STRING
* 4 + 3)],
458 *ptr
, /* Pointer into value string */
459 *name_ptr
; /* Pointer into name string */
460 cups_snmp_t packet
; /* SNMP response packet */
461 ppd_file_t
*ppd
; /* PPD file for this queue */
462 ppd_attr_t
*ppdattr
; /* cupsSNMPSupplies attribute */
463 static const char * const types
[] = /* Supply types */
483 "fuser-cleaning-pad",
489 "glue-water-additive",
503 * Reset state information...
506 current_addr
= *addr
;
511 memset(supplies
, 0, sizeof(supplies
));
514 * See if we should be getting supply levels via SNMP...
517 if ((ppd
= ppdOpenFile(getenv("PPD"))) == NULL
||
518 ((ppdattr
= ppdFindAttr(ppd
, "cupsSNMPSupplies", NULL
)) != NULL
&&
519 ppdattr
->value
&& _cups_strcasecmp(ppdattr
->value
, "true")))
525 if ((ppdattr
= ppdFindAttr(ppd
, "cupsSNMPQuirks", NULL
)) != NULL
)
527 if (!_cups_strcasecmp(ppdattr
->value
, "capacity"))
528 quirks
|= CUPS_SNMP_CAPACITY
;
534 * Get the device description...
537 if (!_cupsSNMPWrite(snmp_fd
, addr
, CUPS_SNMP_VERSION_1
,
538 _cupsSNMPDefaultCommunity(), CUPS_ASN1_GET_REQUEST
, 1,
542 if (!_cupsSNMPRead(snmp_fd
, &packet
, CUPS_SUPPLY_TIMEOUT
) ||
543 packet
.object_type
!= CUPS_ASN1_OCTET_STRING
)
545 strlcpy(description
, "Unknown", sizeof(description
));
549 strlcpy(description
, (char *)packet
.object_value
.string
.bytes
,
550 sizeof(description
));
552 fprintf(stderr
, "DEBUG2: hrDeviceDesc=\"%s\"\n", description
);
555 * See if we have already queried this device...
558 httpAddrString(addr
, addrstr
, sizeof(addrstr
));
560 if ((cachedir
= getenv("CUPS_CACHEDIR")) == NULL
)
561 cachedir
= CUPS_CACHEDIR
;
563 snprintf(cachefilename
, sizeof(cachefilename
), "%s/%s.snmp", cachedir
,
566 if ((cachefile
= cupsFileOpen(cachefilename
, "r")) != NULL
)
569 * Yes, read the cache file:
571 * 3 num_supplies charset
573 * supply structures...
576 if (cupsFileGets(cachefile
, value
, sizeof(value
)))
578 if (sscanf(value
, "3 %d%d", &num_supplies
, &charset
) == 2 &&
579 num_supplies
<= CUPS_MAX_SUPPLIES
&&
580 cupsFileGets(cachefile
, value
, sizeof(value
)))
582 if (!strcmp(description
, value
))
583 cupsFileRead(cachefile
, (char *)supplies
,
584 (size_t)num_supplies
* sizeof(backend_supplies_t
));
598 cupsFileClose(cachefile
);
602 * If the cache information isn't correct, scan for supplies...
608 * Get the configured character set...
611 int oid
[CUPS_SNMP_MAX_OID
]; /* OID for character set */
614 if (!_cupsSNMPWrite(snmp_fd
, ¤t_addr
, CUPS_SNMP_VERSION_1
,
615 _cupsSNMPDefaultCommunity(), CUPS_ASN1_GET_REQUEST
, 1,
616 prtGeneralCurrentLocalization
))
619 if (!_cupsSNMPRead(snmp_fd
, &packet
, CUPS_SUPPLY_TIMEOUT
) ||
620 packet
.object_type
!= CUPS_ASN1_INTEGER
)
623 "DEBUG: prtGeneralCurrentLocalization type is %x, expected %x!\n",
624 packet
.object_type
, CUPS_ASN1_INTEGER
);
628 fprintf(stderr
, "DEBUG2: prtGeneralCurrentLocalization=%d\n",
629 packet
.object_value
.integer
);
631 _cupsSNMPCopyOID(oid
, prtLocalizationCharacterSet
, CUPS_SNMP_MAX_OID
);
632 oid
[prtLocalizationCharacterSetOffset
- 2] = packet
.object_value
.integer
;
635 if (!_cupsSNMPWrite(snmp_fd
, ¤t_addr
, CUPS_SNMP_VERSION_1
,
636 _cupsSNMPDefaultCommunity(), CUPS_ASN1_GET_REQUEST
, 1,
640 if (!_cupsSNMPRead(snmp_fd
, &packet
, CUPS_SUPPLY_TIMEOUT
) ||
641 packet
.object_type
!= CUPS_ASN1_INTEGER
)
644 "DEBUG: prtLocalizationCharacterSet type is %x, expected %x!\n",
645 packet
.object_type
, CUPS_ASN1_INTEGER
);
649 fprintf(stderr
, "DEBUG2: prtLocalizationCharacterSet=%d\n",
650 packet
.object_value
.integer
);
651 charset
= packet
.object_value
.integer
;
654 if (num_supplies
< 0)
657 * Walk the printer configuration information...
660 _cupsSNMPWalk(snmp_fd
, ¤t_addr
, CUPS_SNMP_VERSION_1
,
661 _cupsSNMPDefaultCommunity(), prtMarkerSuppliesEntry
,
662 CUPS_SUPPLY_TIMEOUT
, backend_walk_cb
, NULL
);
666 * Save the cached information...
669 if (num_supplies
< 0)
672 if ((cachefile
= cupsFileOpen(cachefilename
, "w")) != NULL
)
674 cupsFilePrintf(cachefile
, "3 %d %d\n", num_supplies
, charset
);
675 cupsFilePrintf(cachefile
, "%s\n", description
);
677 if (num_supplies
> 0)
678 cupsFileWrite(cachefile
, (char *)supplies
,
679 (size_t)num_supplies
* sizeof(backend_supplies_t
));
681 cupsFileClose(cachefile
);
684 if (num_supplies
<= 0)
691 for (i
= 0; i
< num_supplies
; i
++)
692 strlcpy(supplies
[i
].color
, "none", sizeof(supplies
[i
].color
));
694 _cupsSNMPWalk(snmp_fd
, ¤t_addr
, CUPS_SNMP_VERSION_1
,
695 _cupsSNMPDefaultCommunity(), prtMarkerColorantValue
,
696 CUPS_SUPPLY_TIMEOUT
, backend_walk_cb
, NULL
);
699 * Output the marker-colors attribute...
702 for (i
= 0, ptr
= value
; i
< num_supplies
; i
++, ptr
+= strlen(ptr
))
707 strlcpy(ptr
, supplies
[i
].color
, sizeof(value
) - (size_t)(ptr
- value
));
710 fprintf(stderr
, "ATTR: marker-colors=%s\n", value
);
713 * Output the marker-names attribute (the double quoting is necessary to deal
714 * with embedded quotes and commas in the marker names...)
717 for (i
= 0, ptr
= value
; i
< num_supplies
; i
++)
724 for (name_ptr
= supplies
[i
].name
; *name_ptr
;)
726 if (*name_ptr
== '\\' || *name_ptr
== '\"' || *name_ptr
== '\'')
733 *ptr
++ = *name_ptr
++;
741 fprintf(stderr
, "ATTR: marker-names=%s\n", value
);
744 * Output the marker-types attribute...
747 for (i
= 0, ptr
= value
; i
< num_supplies
; i
++, ptr
+= strlen(ptr
))
752 type
= supplies
[i
].type
;
754 if (type
< CUPS_TC_other
|| type
> CUPS_TC_covers
)
755 strlcpy(ptr
, "unknown", sizeof(value
) - (size_t)(ptr
- value
));
757 strlcpy(ptr
, types
[type
- CUPS_TC_other
], sizeof(value
) - (size_t)(ptr
- value
));
760 fprintf(stderr
, "ATTR: marker-types=%s\n", value
);
765 * 'backend_walk_cb()' - Interpret the supply value responses.
769 backend_walk_cb(cups_snmp_t
*packet
, /* I - SNMP packet */
770 void *data
) /* I - User data (unused) */
772 int i
, j
, k
; /* Looping vars */
773 static const char * const colors
[][2] =
774 { /* Standard color names */
775 { "black", "#000000" },
776 { "blue", "#0000FF" },
777 { "brown", "#A52A2A" },
778 { "cyan", "#00FFFF" },
779 { "dark-gray", "#404040" },
780 { "dark gray", "#404040" },
781 { "dark-yellow", "#FFCC00" },
782 { "dark yellow", "#FFCC00" },
783 { "gold", "#FFD700" },
784 { "gray", "#808080" },
785 { "green", "#00FF00" },
786 { "light-black", "#606060" },
787 { "light black", "#606060" },
788 { "light-cyan", "#E0FFFF" },
789 { "light cyan", "#E0FFFF" },
790 { "light-gray", "#D3D3D3" },
791 { "light gray", "#D3D3D3" },
792 { "light-magenta", "#FF77FF" },
793 { "light magenta", "#FF77FF" },
794 { "magenta", "#FF00FF" },
795 { "orange", "#FFA500" },
796 { "red", "#FF0000" },
797 { "silver", "#C0C0C0" },
798 { "white", "#FFFFFF" },
799 { "yellow", "#FFFF00" }
805 if (_cupsSNMPIsOIDPrefixed(packet
, prtMarkerColorantValue
) &&
806 packet
->object_type
== CUPS_ASN1_OCTET_STRING
)
812 i
= packet
->object_name
[prtMarkerColorantValueOffset
];
814 fprintf(stderr
, "DEBUG2: prtMarkerColorantValue.1.%d = \"%s\"\n", i
,
815 (char *)packet
->object_value
.string
.bytes
);
817 for (j
= 0; j
< num_supplies
; j
++)
818 if (supplies
[j
].colorant
== i
)
820 for (k
= 0; k
< (int)(sizeof(colors
) / sizeof(colors
[0])); k
++)
821 if (!_cups_strcasecmp(colors
[k
][0],
822 (char *)packet
->object_value
.string
.bytes
))
824 strlcpy(supplies
[j
].color
, colors
[k
][1], sizeof(supplies
[j
].color
));
829 else if (_cupsSNMPIsOIDPrefixed(packet
, prtMarkerSuppliesColorantIndex
))
832 * Get colorant index...
835 i
= packet
->object_name
[prtMarkerSuppliesColorantIndexOffset
];
836 if (i
< 1 || i
> CUPS_MAX_SUPPLIES
||
837 packet
->object_type
!= CUPS_ASN1_INTEGER
)
840 fprintf(stderr
, "DEBUG2: prtMarkerSuppliesColorantIndex.1.%d = %d\n", i
,
841 packet
->object_value
.integer
);
843 if (i
> num_supplies
)
846 supplies
[i
- 1].colorant
= packet
->object_value
.integer
;
848 else if (_cupsSNMPIsOIDPrefixed(packet
, prtMarkerSuppliesDescription
))
851 * Get supply name/description...
854 i
= packet
->object_name
[prtMarkerSuppliesDescriptionOffset
];
855 if (i
< 1 || i
> CUPS_MAX_SUPPLIES
||
856 packet
->object_type
!= CUPS_ASN1_OCTET_STRING
)
859 if (i
> num_supplies
)
864 case CUPS_TC_csASCII
:
865 case CUPS_TC_csUTF8
:
866 case CUPS_TC_csUnicodeASCII
:
867 strlcpy(supplies
[i
- 1].name
,
868 (char *)packet
->object_value
.string
.bytes
,
869 sizeof(supplies
[0].name
));
872 case CUPS_TC_csISOLatin1
:
873 case CUPS_TC_csUnicodeLatin1
:
874 cupsCharsetToUTF8((cups_utf8_t
*)supplies
[i
- 1].name
,
875 (char *)packet
->object_value
.string
.bytes
,
876 sizeof(supplies
[0].name
), CUPS_ISO8859_1
);
879 case CUPS_TC_csShiftJIS
:
880 case CUPS_TC_csWindows31J
: /* Close enough for our purposes */
881 cupsCharsetToUTF8((cups_utf8_t
*)supplies
[i
- 1].name
,
882 (char *)packet
->object_value
.string
.bytes
,
883 sizeof(supplies
[0].name
), CUPS_JIS_X0213
);
886 case CUPS_TC_csUCS4
:
887 case CUPS_TC_csUTF32
:
888 case CUPS_TC_csUTF32BE
:
889 case CUPS_TC_csUTF32LE
:
890 cupsUTF32ToUTF8((cups_utf8_t
*)supplies
[i
- 1].name
,
891 (cups_utf32_t
*)packet
->object_value
.string
.bytes
,
892 sizeof(supplies
[0].name
));
895 case CUPS_TC_csUnicode
:
896 case CUPS_TC_csUTF16BE
:
897 case CUPS_TC_csUTF16LE
:
898 utf16_to_utf8((cups_utf8_t
*)supplies
[i
- 1].name
,
899 packet
->object_value
.string
.bytes
,
900 packet
->object_value
.string
.num_bytes
,
901 sizeof(supplies
[0].name
), charset
== CUPS_TC_csUTF16LE
);
906 * If we get here, the printer is using an unknown character set and
907 * we just want to copy characters that look like ASCII...
911 char *src
, *dst
; /* Pointers into strings */
914 * Loop safe because both the object_value and supplies char arrays
915 * are CUPS_SNMP_MAX_STRING elements long.
918 for (src
= (char *)packet
->object_value
.string
.bytes
,
919 dst
= supplies
[i
- 1].name
;
923 if ((*src
& 0x80) || *src
< ' ' || *src
== 0x7f)
934 fprintf(stderr
, "DEBUG2: prtMarkerSuppliesDescription.1.%d = \"%s\"\n", i
,
935 supplies
[i
- 1].name
);
938 else if (_cupsSNMPIsOIDPrefixed(packet
, prtMarkerSuppliesLevel
))
944 i
= packet
->object_name
[prtMarkerSuppliesLevelOffset
];
945 if (i
< 1 || i
> CUPS_MAX_SUPPLIES
||
946 packet
->object_type
!= CUPS_ASN1_INTEGER
)
949 fprintf(stderr
, "DEBUG2: prtMarkerSuppliesLevel.1.%d = %d\n", i
,
950 packet
->object_value
.integer
);
952 if (i
> num_supplies
)
955 supplies
[i
- 1].level
= packet
->object_value
.integer
;
957 else if (_cupsSNMPIsOIDPrefixed(packet
, prtMarkerSuppliesMaxCapacity
) &&
958 !(quirks
& CUPS_SNMP_CAPACITY
))
961 * Get max capacity...
964 i
= packet
->object_name
[prtMarkerSuppliesMaxCapacityOffset
];
965 if (i
< 1 || i
> CUPS_MAX_SUPPLIES
||
966 packet
->object_type
!= CUPS_ASN1_INTEGER
)
969 fprintf(stderr
, "DEBUG2: prtMarkerSuppliesMaxCapacity.1.%d = %d\n", i
,
970 packet
->object_value
.integer
);
972 if (i
> num_supplies
)
975 if (supplies
[i
- 1].max_capacity
== 0 &&
976 packet
->object_value
.integer
> 0)
977 supplies
[i
- 1].max_capacity
= packet
->object_value
.integer
;
979 else if (_cupsSNMPIsOIDPrefixed(packet
, prtMarkerSuppliesClass
))
982 * Get marker class...
985 i
= packet
->object_name
[prtMarkerSuppliesClassOffset
];
986 if (i
< 1 || i
> CUPS_MAX_SUPPLIES
||
987 packet
->object_type
!= CUPS_ASN1_INTEGER
)
990 fprintf(stderr
, "DEBUG2: prtMarkerSuppliesClass.1.%d = %d\n", i
,
991 packet
->object_value
.integer
);
993 if (i
> num_supplies
)
996 supplies
[i
- 1].sclass
= packet
->object_value
.integer
;
998 else if (_cupsSNMPIsOIDPrefixed(packet
, prtMarkerSuppliesType
))
1001 * Get marker type...
1004 i
= packet
->object_name
[prtMarkerSuppliesTypeOffset
];
1005 if (i
< 1 || i
> CUPS_MAX_SUPPLIES
||
1006 packet
->object_type
!= CUPS_ASN1_INTEGER
)
1009 fprintf(stderr
, "DEBUG2: prtMarkerSuppliesType.1.%d = %d\n", i
,
1010 packet
->object_value
.integer
);
1012 if (i
> num_supplies
)
1015 supplies
[i
- 1].type
= packet
->object_value
.integer
;
1017 else if (_cupsSNMPIsOIDPrefixed(packet
, prtMarkerSuppliesSupplyUnit
))
1020 * Get units for capacity...
1023 i
= packet
->object_name
[prtMarkerSuppliesSupplyUnitOffset
];
1024 if (i
< 1 || i
> CUPS_MAX_SUPPLIES
||
1025 packet
->object_type
!= CUPS_ASN1_INTEGER
)
1028 fprintf(stderr
, "DEBUG2: prtMarkerSuppliesSupplyUnit.1.%d = %d\n", i
,
1029 packet
->object_value
.integer
);
1031 if (i
> num_supplies
)
1034 if (packet
->object_value
.integer
== CUPS_TC_percent
)
1035 supplies
[i
- 1].max_capacity
= 100;
1041 * 'utf16_to_utf8()' - Convert UTF-16 text to UTF-8.
1046 cups_utf8_t
*dst
, /* I - Destination buffer */
1047 const unsigned char *src
, /* I - Source string */
1048 size_t srcsize
, /* I - Size of source string */
1049 size_t dstsize
, /* I - Size of destination buffer */
1050 int le
) /* I - Source is little-endian? */
1052 cups_utf32_t ch
, /* Current character */
1053 temp
[CUPS_SNMP_MAX_STRING
],
1055 *ptr
; /* Pointer into UTF-32 string */
1058 for (ptr
= temp
; srcsize
>= 2;)
1061 ch
= (cups_utf32_t
)(src
[0] | (src
[1] << 8));
1063 ch
= (cups_utf32_t
)((src
[0] << 8) | src
[1]);
1068 if (ch
>= 0xd800 && ch
<= 0xdbff && srcsize
>= 2)
1071 * Multi-word UTF-16 char...
1074 cups_utf32_t lch
; /* Lower word */
1078 lch
= (cups_utf32_t
)(src
[0] | (src
[1] << 8));
1080 lch
= (cups_utf32_t
)((src
[0] << 8) | src
[1]);
1082 if (lch
>= 0xdc00 && lch
<= 0xdfff)
1087 ch
= (((ch
& 0x3ff) << 10) | (lch
& 0x3ff)) + 0x10000;
1091 if (ptr
< (temp
+ CUPS_SNMP_MAX_STRING
- 1))
1097 cupsUTF32ToUTF8(dst
, temp
, (int)dstsize
);