2 * SNMP supplies functions for CUPS.
4 * Copyright 2008-2015 by Apple Inc.
6 * These coded instructions, statements, and computer programs are the
7 * property of Apple Inc. and are protected by Federal copyright
8 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
9 * "LICENSE" which should have been included with this file. If this
10 * file is missing or damaged, see the license at "http://www.cups.org/".
14 * Include necessary headers.
17 #include "backend-private.h"
18 #include <cups/ppd-private.h>
19 #include <cups/array.h>
26 #define CUPS_MAX_SUPPLIES 32 /* Maximum number of supplies for a printer */
27 #define CUPS_SUPPLY_TIMEOUT 2.0 /* Timeout for SNMP lookups */
29 #define CUPS_DEVELOPER_LOW 0x0001
30 #define CUPS_DEVELOPER_EMPTY 0x0002
31 #define CUPS_MARKER_SUPPLY_LOW 0x0004
32 #define CUPS_MARKER_SUPPLY_EMPTY 0x0008
33 #define CUPS_OPC_NEAR_EOL 0x0010
34 #define CUPS_OPC_LIFE_OVER 0x0020
35 #define CUPS_TONER_LOW 0x0040
36 #define CUPS_TONER_EMPTY 0x0080
37 #define CUPS_WASTE_ALMOST_FULL 0x0100
38 #define CUPS_WASTE_FULL 0x0200
39 #define CUPS_CLEANER_NEAR_EOL 0x0400 /* Proposed JPS3 */
40 #define CUPS_CLEANER_LIFE_OVER 0x0800 /* Proposed JPS3 */
42 #define CUPS_SNMP_NONE 0x0000
43 #define CUPS_SNMP_CAPACITY 0x0001 /* Supply levels reported as percentages */
50 typedef struct /**** Printer supply data ****/
52 char name
[CUPS_SNMP_MAX_STRING
], /* Name of supply */
53 color
[8]; /* Color: "#RRGGBB" or "none" */
54 int colorant
, /* Colorant index */
55 sclass
, /* Supply class */
56 type
, /* Supply type */
57 max_capacity
, /* Maximum capacity */
58 level
; /* Current level value */
61 typedef struct /**** Printer state table ****/
63 int bit
; /* State bit */
64 const char *keyword
; /* IPP printer-state-reasons keyword */
72 static http_addr_t current_addr
; /* Current address */
73 static int current_state
= -1;
74 /* Current device state bits */
75 static int charset
= -1; /* Character set for supply names */
76 static unsigned quirks
= CUPS_SNMP_NONE
;
77 /* Quirks we have to work around */
78 static int num_supplies
= 0;
79 /* Number of supplies found */
80 static backend_supplies_t supplies
[CUPS_MAX_SUPPLIES
];
81 /* Supply information */
82 static int supply_state
= -1;
83 /* Supply state info */
85 static const int hrDeviceDescr
[] =
86 { CUPS_OID_hrDeviceDescr
, 1, -1 };
87 /* Device description OID */
88 static const int hrPrinterStatus
[] =
89 { CUPS_OID_hrPrinterStatus
, 1, -1 };
90 /* Current state OID */
91 static const int hrPrinterDetectedErrorState
[] =
92 { CUPS_OID_hrPrinterDetectedErrorState
, 1, -1 };
93 /* Current printer state bits OID */
94 static const int prtGeneralCurrentLocalization
[] =
95 { CUPS_OID_prtGeneralCurrentLocalization
, 1, -1 };
96 static const int prtLocalizationCharacterSet
[] =
97 { CUPS_OID_prtLocalizationCharacterSet
, 1, 1, -1 },
98 prtLocalizationCharacterSetOffset
=
99 (sizeof(prtLocalizationCharacterSet
) /
100 sizeof(prtLocalizationCharacterSet
[0]));
101 static const int prtMarkerColorantValue
[] =
102 { CUPS_OID_prtMarkerColorantValue
, -1 },
104 prtMarkerColorantValueOffset
=
105 (sizeof(prtMarkerColorantValue
) /
106 sizeof(prtMarkerColorantValue
[0]));
107 /* Offset to colorant index */
108 static const int prtMarkerLifeCount
[] =
109 { CUPS_OID_prtMarkerLifeCount
, 1, 1, -1 };
110 /* Page counter OID */
111 static const int prtMarkerSuppliesEntry
[] =
112 { CUPS_OID_prtMarkerSuppliesEntry
, -1 };
114 static const int prtMarkerSuppliesColorantIndex
[] =
115 { CUPS_OID_prtMarkerSuppliesColorantIndex
, -1 },
116 /* Colorant index OID */
117 prtMarkerSuppliesColorantIndexOffset
=
118 (sizeof(prtMarkerSuppliesColorantIndex
) /
119 sizeof(prtMarkerSuppliesColorantIndex
[0]));
120 /* Offset to supply index */
121 static const int prtMarkerSuppliesDescription
[] =
122 { CUPS_OID_prtMarkerSuppliesDescription
, -1 },
123 /* Description OID */
124 prtMarkerSuppliesDescriptionOffset
=
125 (sizeof(prtMarkerSuppliesDescription
) /
126 sizeof(prtMarkerSuppliesDescription
[0]));
127 /* Offset to supply index */
128 static const int prtMarkerSuppliesLevel
[] =
129 { CUPS_OID_prtMarkerSuppliesLevel
, -1 },
131 prtMarkerSuppliesLevelOffset
=
132 (sizeof(prtMarkerSuppliesLevel
) /
133 sizeof(prtMarkerSuppliesLevel
[0]));
134 /* Offset to supply index */
135 static const int prtMarkerSuppliesMaxCapacity
[] =
136 { CUPS_OID_prtMarkerSuppliesMaxCapacity
, -1 },
137 /* Max capacity OID */
138 prtMarkerSuppliesMaxCapacityOffset
=
139 (sizeof(prtMarkerSuppliesMaxCapacity
) /
140 sizeof(prtMarkerSuppliesMaxCapacity
[0]));
141 /* Offset to supply index */
142 static const int prtMarkerSuppliesClass
[] =
143 { CUPS_OID_prtMarkerSuppliesClass
, -1 },
145 prtMarkerSuppliesClassOffset
=
146 (sizeof(prtMarkerSuppliesClass
) /
147 sizeof(prtMarkerSuppliesClass
[0]));
148 /* Offset to supply index */
149 static const int prtMarkerSuppliesType
[] =
150 { CUPS_OID_prtMarkerSuppliesType
, -1 },
152 prtMarkerSuppliesTypeOffset
=
153 (sizeof(prtMarkerSuppliesType
) /
154 sizeof(prtMarkerSuppliesType
[0]));
155 /* Offset to supply index */
156 static const int prtMarkerSuppliesSupplyUnit
[] =
157 { CUPS_OID_prtMarkerSuppliesSupplyUnit
, -1 },
159 prtMarkerSuppliesSupplyUnitOffset
=
160 (sizeof(prtMarkerSuppliesSupplyUnit
) /
161 sizeof(prtMarkerSuppliesSupplyUnit
[0]));
162 /* Offset to supply index */
164 static const backend_state_t printer_states
[] =
166 /* { CUPS_TC_lowPaper, "media-low-report" }, */
167 { CUPS_TC_noPaper
| CUPS_TC_inputTrayEmpty
, "media-empty-warning" },
168 /* { CUPS_TC_lowToner, "toner-low-report" }, */ /* now use prtMarkerSupplies */
169 /* { CUPS_TC_noToner, "toner-empty-warning" }, */ /* now use prtMarkerSupplies */
170 { CUPS_TC_doorOpen
, "door-open-report" },
171 { CUPS_TC_jammed
, "media-jam-warning" },
172 /* { CUPS_TC_offline, "offline-report" }, */ /* unreliable */
173 /* { CUPS_TC_serviceRequested | CUPS_TC_overduePreventMaint, "service-needed-warning" }, */ /* unreliable */
174 { CUPS_TC_inputTrayMissing
, "input-tray-missing-warning" },
175 { CUPS_TC_outputTrayMissing
, "output-tray-missing-warning" },
176 { CUPS_TC_markerSupplyMissing
, "marker-supply-missing-warning" },
177 { CUPS_TC_outputNearFull
, "output-area-almost-full-report" },
178 { CUPS_TC_outputFull
, "output-area-full-warning" }
181 static const backend_state_t supply_states
[] =
183 { CUPS_DEVELOPER_LOW
, "developer-low-report" },
184 { CUPS_DEVELOPER_EMPTY
, "developer-empty-warning" },
185 { CUPS_MARKER_SUPPLY_LOW
, "marker-supply-low-report" },
186 { CUPS_MARKER_SUPPLY_EMPTY
, "marker-supply-empty-warning" },
187 { CUPS_OPC_NEAR_EOL
, "opc-near-eol-report" },
188 { CUPS_OPC_LIFE_OVER
, "opc-life-over-warning" },
189 { CUPS_TONER_LOW
, "toner-low-report" },
190 { CUPS_TONER_EMPTY
, "toner-empty-warning" },
191 { CUPS_WASTE_ALMOST_FULL
, "waste-receptacle-almost-full-report" },
192 { CUPS_WASTE_FULL
, "waste-receptacle-full-warning" },
193 { CUPS_CLEANER_NEAR_EOL
, "cleaner-life-almost-over-report" },
194 { CUPS_CLEANER_LIFE_OVER
, "cleaner-life-over-warning" },
202 static void backend_init_supplies(int snmp_fd
, http_addr_t
*addr
);
203 static void backend_walk_cb(cups_snmp_t
*packet
, void *data
);
204 static void utf16_to_utf8(cups_utf8_t
*dst
, const unsigned char *src
,
205 size_t srcsize
, size_t dstsize
, int le
);
209 * 'backendSNMPSupplies()' - Get the current supplies for a device.
212 int /* O - 0 on success, -1 on error */
214 int snmp_fd
, /* I - SNMP socket */
215 http_addr_t
*addr
, /* I - Printer address */
216 int *page_count
, /* O - Page count */
217 int *printer_state
) /* O - Printer state */
219 if (!httpAddrEqual(addr
, ¤t_addr
))
220 backend_init_supplies(snmp_fd
, addr
);
221 else if (num_supplies
> 0)
222 _cupsSNMPWalk(snmp_fd
, ¤t_addr
, CUPS_SNMP_VERSION_1
,
223 _cupsSNMPDefaultCommunity(), prtMarkerSuppliesLevel
,
224 CUPS_SUPPLY_TIMEOUT
, backend_walk_cb
, NULL
);
232 if (num_supplies
> 0)
234 int i
, /* Looping var */
235 percent
, /* Percent full */
236 new_state
, /* New state value */
237 change_state
, /* State change */
238 new_supply_state
= 0; /* Supply state */
239 char value
[CUPS_MAX_SUPPLIES
* 4],
240 /* marker-levels value string */
241 *ptr
; /* Pointer into value string */
242 cups_snmp_t packet
; /* SNMP response packet */
245 * Generate the marker-levels value string...
248 for (i
= 0, ptr
= value
; i
< num_supplies
; i
++, ptr
+= strlen(ptr
))
250 if (supplies
[i
].max_capacity
> 0 && supplies
[i
].level
>= 0)
251 percent
= 100 * supplies
[i
].level
/ supplies
[i
].max_capacity
;
252 else if (supplies
[i
].level
>= 0 && supplies
[i
].level
<= 100 &&
253 (quirks
& CUPS_SNMP_CAPACITY
))
254 percent
= supplies
[i
].level
;
258 if (supplies
[i
].sclass
== CUPS_TC_receptacleThatIsFilled
)
259 percent
= 100 - percent
;
263 switch (supplies
[i
].type
)
266 case CUPS_TC_tonerCartridge
:
268 new_supply_state
|= CUPS_TONER_EMPTY
;
270 new_supply_state
|= CUPS_TONER_LOW
;
273 case CUPS_TC_inkCartridge
:
274 case CUPS_TC_inkRibbon
:
275 case CUPS_TC_solidWax
:
276 case CUPS_TC_ribbonWax
:
278 new_supply_state
|= CUPS_MARKER_SUPPLY_EMPTY
;
280 new_supply_state
|= CUPS_MARKER_SUPPLY_LOW
;
282 case CUPS_TC_developer
:
284 new_supply_state
|= CUPS_DEVELOPER_EMPTY
;
286 new_supply_state
|= CUPS_DEVELOPER_LOW
;
288 case CUPS_TC_coronaWire
:
291 case CUPS_TC_transferUnit
:
293 new_supply_state
|= CUPS_OPC_LIFE_OVER
;
295 new_supply_state
|= CUPS_OPC_NEAR_EOL
;
297 #if 0 /* Because no two vendors report waste containers the same, disable SNMP reporting of same */
298 case CUPS_TC_wasteInk
:
299 case CUPS_TC_wastePaper
:
300 case CUPS_TC_wasteToner
:
301 case CUPS_TC_wasteWater
:
302 case CUPS_TC_wasteWax
:
304 new_supply_state
|= CUPS_WASTE_FULL
;
306 new_supply_state
|= CUPS_WASTE_ALMOST_FULL
;
309 case CUPS_TC_cleanerUnit
:
310 case CUPS_TC_fuserCleaningPad
:
312 new_supply_state
|= CUPS_CLEANER_LIFE_OVER
;
314 new_supply_state
|= CUPS_CLEANER_NEAR_EOL
;
322 if ((supplies
[i
].max_capacity
> 0 || (quirks
& CUPS_SNMP_CAPACITY
)) &&
323 supplies
[i
].level
>= 0)
324 snprintf(ptr
, sizeof(value
) - (size_t)(ptr
- value
), "%d", percent
);
326 strlcpy(ptr
, "-1", sizeof(value
) - (size_t)(ptr
- value
));
329 fprintf(stderr
, "ATTR: marker-levels=%s\n", value
);
331 if (supply_state
< 0)
332 change_state
= 0xffff;
334 change_state
= supply_state
^ new_supply_state
;
336 fprintf(stderr
, "DEBUG: new_supply_state=%x, change_state=%x\n",
337 new_supply_state
, change_state
);
340 i
< (int)(sizeof(supply_states
) / sizeof(supply_states
[0]));
342 if (change_state
& supply_states
[i
].bit
)
344 fprintf(stderr
, "STATE: %c%s\n",
345 (new_supply_state
& supply_states
[i
].bit
) ? '+' : '-',
346 supply_states
[i
].keyword
);
349 supply_state
= new_supply_state
;
352 * Get the current printer status bits...
355 if (!_cupsSNMPWrite(snmp_fd
, addr
, CUPS_SNMP_VERSION_1
,
356 _cupsSNMPDefaultCommunity(), CUPS_ASN1_GET_REQUEST
, 1,
357 hrPrinterDetectedErrorState
))
360 if (!_cupsSNMPRead(snmp_fd
, &packet
, CUPS_SUPPLY_TIMEOUT
) ||
361 packet
.object_type
!= CUPS_ASN1_OCTET_STRING
)
364 if (packet
.object_value
.string
.num_bytes
== 2)
365 new_state
= (packet
.object_value
.string
.bytes
[0] << 8) |
366 packet
.object_value
.string
.bytes
[1];
367 else if (packet
.object_value
.string
.num_bytes
== 1)
368 new_state
= (packet
.object_value
.string
.bytes
[0] << 8);
372 if (current_state
< 0)
373 change_state
= 0xffff;
375 change_state
= current_state
^ new_state
;
377 fprintf(stderr
, "DEBUG: new_state=%x, change_state=%x\n", new_state
,
381 i
< (int)(sizeof(printer_states
) / sizeof(printer_states
[0]));
383 if (change_state
& printer_states
[i
].bit
)
385 fprintf(stderr
, "STATE: %c%s\n",
386 (new_state
& printer_states
[i
].bit
) ? '+' : '-',
387 printer_states
[i
].keyword
);
390 current_state
= new_state
;
393 * Get the current printer state...
398 if (!_cupsSNMPWrite(snmp_fd
, addr
, CUPS_SNMP_VERSION_1
,
399 _cupsSNMPDefaultCommunity(), CUPS_ASN1_GET_REQUEST
, 1,
403 if (!_cupsSNMPRead(snmp_fd
, &packet
, CUPS_SUPPLY_TIMEOUT
) ||
404 packet
.object_type
!= CUPS_ASN1_INTEGER
)
407 *printer_state
= packet
.object_value
.integer
;
411 * Get the current page count...
416 if (!_cupsSNMPWrite(snmp_fd
, addr
, CUPS_SNMP_VERSION_1
,
417 _cupsSNMPDefaultCommunity(), CUPS_ASN1_GET_REQUEST
, 1,
421 if (!_cupsSNMPRead(snmp_fd
, &packet
, CUPS_SUPPLY_TIMEOUT
) ||
422 packet
.object_type
!= CUPS_ASN1_COUNTER
)
425 *page_count
= packet
.object_value
.counter
;
436 * 'backend_init_supplies()' - Initialize the supplies list.
440 backend_init_supplies(
441 int snmp_fd
, /* I - SNMP socket */
442 http_addr_t
*addr
) /* I - Printer address */
444 int i
, /* Looping var */
445 type
; /* Current marker type */
446 cups_file_t
*cachefile
; /* Cache file */
447 const char *cachedir
; /* CUPS_CACHEDIR value */
448 char addrstr
[1024], /* Address string */
449 cachefilename
[1024], /* Cache filename */
450 description
[CUPS_SNMP_MAX_STRING
],
451 /* Device description string */
452 value
[CUPS_MAX_SUPPLIES
* (CUPS_SNMP_MAX_STRING
* 4 + 3)],
454 *ptr
, /* Pointer into value string */
455 *name_ptr
; /* Pointer into name string */
456 cups_snmp_t packet
; /* SNMP response packet */
457 ppd_file_t
*ppd
; /* PPD file for this queue */
458 ppd_attr_t
*ppdattr
; /* cupsSNMPSupplies attribute */
459 static const char * const types
[] = /* Supply types */
479 "fuser-cleaning-pad",
485 "glue-water-additive",
499 * Reset state information...
502 current_addr
= *addr
;
507 memset(supplies
, 0, sizeof(supplies
));
510 * See if we should be getting supply levels via SNMP...
513 if ((ppd
= ppdOpenFile(getenv("PPD"))) == NULL
||
514 ((ppdattr
= ppdFindAttr(ppd
, "cupsSNMPSupplies", NULL
)) != NULL
&&
515 ppdattr
->value
&& _cups_strcasecmp(ppdattr
->value
, "true")))
521 if ((ppdattr
= ppdFindAttr(ppd
, "cupsSNMPQuirks", NULL
)) != NULL
)
523 if (!_cups_strcasecmp(ppdattr
->value
, "capacity"))
524 quirks
|= CUPS_SNMP_CAPACITY
;
530 * Get the device description...
533 if (!_cupsSNMPWrite(snmp_fd
, addr
, CUPS_SNMP_VERSION_1
,
534 _cupsSNMPDefaultCommunity(), CUPS_ASN1_GET_REQUEST
, 1,
538 if (!_cupsSNMPRead(snmp_fd
, &packet
, CUPS_SUPPLY_TIMEOUT
) ||
539 packet
.object_type
!= CUPS_ASN1_OCTET_STRING
)
541 strlcpy(description
, "Unknown", sizeof(description
));
545 strlcpy(description
, (char *)packet
.object_value
.string
.bytes
,
546 sizeof(description
));
548 fprintf(stderr
, "DEBUG2: hrDeviceDesc=\"%s\"\n", description
);
551 * See if we have already queried this device...
554 httpAddrString(addr
, addrstr
, sizeof(addrstr
));
556 if ((cachedir
= getenv("CUPS_CACHEDIR")) == NULL
)
557 cachedir
= CUPS_CACHEDIR
;
559 snprintf(cachefilename
, sizeof(cachefilename
), "%s/%s.snmp", cachedir
,
562 if ((cachefile
= cupsFileOpen(cachefilename
, "r")) != NULL
)
565 * Yes, read the cache file:
567 * 3 num_supplies charset
569 * supply structures...
572 if (cupsFileGets(cachefile
, value
, sizeof(value
)))
574 if (sscanf(value
, "3 %d%d", &num_supplies
, &charset
) == 2 &&
575 num_supplies
<= CUPS_MAX_SUPPLIES
&&
576 cupsFileGets(cachefile
, value
, sizeof(value
)))
578 if (!strcmp(description
, value
))
579 cupsFileRead(cachefile
, (char *)supplies
,
580 (size_t)num_supplies
* sizeof(backend_supplies_t
));
594 cupsFileClose(cachefile
);
598 * If the cache information isn't correct, scan for supplies...
604 * Get the configured character set...
607 int oid
[CUPS_SNMP_MAX_OID
]; /* OID for character set */
610 if (!_cupsSNMPWrite(snmp_fd
, ¤t_addr
, CUPS_SNMP_VERSION_1
,
611 _cupsSNMPDefaultCommunity(), CUPS_ASN1_GET_REQUEST
, 1,
612 prtGeneralCurrentLocalization
))
615 if (!_cupsSNMPRead(snmp_fd
, &packet
, CUPS_SUPPLY_TIMEOUT
) ||
616 packet
.object_type
!= CUPS_ASN1_INTEGER
)
619 "DEBUG: prtGeneralCurrentLocalization type is %x, expected %x!\n",
620 packet
.object_type
, CUPS_ASN1_INTEGER
);
624 fprintf(stderr
, "DEBUG2: prtGeneralCurrentLocalization=%d\n",
625 packet
.object_value
.integer
);
627 _cupsSNMPCopyOID(oid
, prtLocalizationCharacterSet
, CUPS_SNMP_MAX_OID
);
628 oid
[prtLocalizationCharacterSetOffset
- 2] = packet
.object_value
.integer
;
631 if (!_cupsSNMPWrite(snmp_fd
, ¤t_addr
, CUPS_SNMP_VERSION_1
,
632 _cupsSNMPDefaultCommunity(), CUPS_ASN1_GET_REQUEST
, 1,
636 if (!_cupsSNMPRead(snmp_fd
, &packet
, CUPS_SUPPLY_TIMEOUT
) ||
637 packet
.object_type
!= CUPS_ASN1_INTEGER
)
640 "DEBUG: prtLocalizationCharacterSet type is %x, expected %x!\n",
641 packet
.object_type
, CUPS_ASN1_INTEGER
);
645 fprintf(stderr
, "DEBUG2: prtLocalizationCharacterSet=%d\n",
646 packet
.object_value
.integer
);
647 charset
= packet
.object_value
.integer
;
650 if (num_supplies
< 0)
653 * Walk the printer configuration information...
656 _cupsSNMPWalk(snmp_fd
, ¤t_addr
, CUPS_SNMP_VERSION_1
,
657 _cupsSNMPDefaultCommunity(), prtMarkerSuppliesEntry
,
658 CUPS_SUPPLY_TIMEOUT
, backend_walk_cb
, NULL
);
662 * Save the cached information...
665 if (num_supplies
< 0)
668 if ((cachefile
= cupsFileOpen(cachefilename
, "w")) != NULL
)
670 cupsFilePrintf(cachefile
, "3 %d %d\n", num_supplies
, charset
);
671 cupsFilePrintf(cachefile
, "%s\n", description
);
673 if (num_supplies
> 0)
674 cupsFileWrite(cachefile
, (char *)supplies
,
675 (size_t)num_supplies
* sizeof(backend_supplies_t
));
677 cupsFileClose(cachefile
);
680 if (num_supplies
<= 0)
687 for (i
= 0; i
< num_supplies
; i
++)
688 strlcpy(supplies
[i
].color
, "none", sizeof(supplies
[i
].color
));
690 _cupsSNMPWalk(snmp_fd
, ¤t_addr
, CUPS_SNMP_VERSION_1
,
691 _cupsSNMPDefaultCommunity(), prtMarkerColorantValue
,
692 CUPS_SUPPLY_TIMEOUT
, backend_walk_cb
, NULL
);
695 * Output the marker-colors attribute...
698 for (i
= 0, ptr
= value
; i
< num_supplies
; i
++, ptr
+= strlen(ptr
))
703 strlcpy(ptr
, supplies
[i
].color
, sizeof(value
) - (size_t)(ptr
- value
));
706 fprintf(stderr
, "ATTR: marker-colors=%s\n", value
);
709 * Output the marker-names attribute (the double quoting is necessary to deal
710 * with embedded quotes and commas in the marker names...)
713 for (i
= 0, ptr
= value
; i
< num_supplies
; i
++)
720 for (name_ptr
= supplies
[i
].name
; *name_ptr
;)
722 if (*name_ptr
== '\\' || *name_ptr
== '\"' || *name_ptr
== '\'')
729 *ptr
++ = *name_ptr
++;
737 fprintf(stderr
, "ATTR: marker-names=%s\n", value
);
740 * Output the marker-types attribute...
743 for (i
= 0, ptr
= value
; i
< num_supplies
; i
++, ptr
+= strlen(ptr
))
748 type
= supplies
[i
].type
;
750 if (type
< CUPS_TC_other
|| type
> CUPS_TC_covers
)
751 strlcpy(ptr
, "unknown", sizeof(value
) - (size_t)(ptr
- value
));
753 strlcpy(ptr
, types
[type
- CUPS_TC_other
], sizeof(value
) - (size_t)(ptr
- value
));
756 fprintf(stderr
, "ATTR: marker-types=%s\n", value
);
761 * 'backend_walk_cb()' - Interpret the supply value responses.
765 backend_walk_cb(cups_snmp_t
*packet
, /* I - SNMP packet */
766 void *data
) /* I - User data (unused) */
768 int i
, j
, k
; /* Looping vars */
769 static const char * const colors
[][2] =
770 { /* Standard color names */
771 { "black", "#000000" },
772 { "blue", "#0000FF" },
773 { "brown", "#A52A2A" },
774 { "cyan", "#00FFFF" },
775 { "dark-gray", "#404040" },
776 { "dark gray", "#404040" },
777 { "dark-yellow", "#FFCC00" },
778 { "dark yellow", "#FFCC00" },
779 { "gold", "#FFD700" },
780 { "gray", "#808080" },
781 { "green", "#00FF00" },
782 { "light-black", "#606060" },
783 { "light black", "#606060" },
784 { "light-cyan", "#E0FFFF" },
785 { "light cyan", "#E0FFFF" },
786 { "light-gray", "#D3D3D3" },
787 { "light gray", "#D3D3D3" },
788 { "light-magenta", "#FF77FF" },
789 { "light magenta", "#FF77FF" },
790 { "magenta", "#FF00FF" },
791 { "orange", "#FFA500" },
792 { "red", "#FF0000" },
793 { "silver", "#C0C0C0" },
794 { "white", "#FFFFFF" },
795 { "yellow", "#FFFF00" }
801 if (_cupsSNMPIsOIDPrefixed(packet
, prtMarkerColorantValue
) &&
802 packet
->object_type
== CUPS_ASN1_OCTET_STRING
)
808 i
= packet
->object_name
[prtMarkerColorantValueOffset
];
810 fprintf(stderr
, "DEBUG2: prtMarkerColorantValue.1.%d = \"%s\"\n", i
,
811 (char *)packet
->object_value
.string
.bytes
);
813 for (j
= 0; j
< num_supplies
; j
++)
814 if (supplies
[j
].colorant
== i
)
816 for (k
= 0; k
< (int)(sizeof(colors
) / sizeof(colors
[0])); k
++)
817 if (!_cups_strcasecmp(colors
[k
][0],
818 (char *)packet
->object_value
.string
.bytes
))
820 strlcpy(supplies
[j
].color
, colors
[k
][1], sizeof(supplies
[j
].color
));
825 else if (_cupsSNMPIsOIDPrefixed(packet
, prtMarkerSuppliesColorantIndex
))
828 * Get colorant index...
831 i
= packet
->object_name
[prtMarkerSuppliesColorantIndexOffset
];
832 if (i
< 1 || i
> CUPS_MAX_SUPPLIES
||
833 packet
->object_type
!= CUPS_ASN1_INTEGER
)
836 fprintf(stderr
, "DEBUG2: prtMarkerSuppliesColorantIndex.1.%d = %d\n", i
,
837 packet
->object_value
.integer
);
839 if (i
> num_supplies
)
842 supplies
[i
- 1].colorant
= packet
->object_value
.integer
;
844 else if (_cupsSNMPIsOIDPrefixed(packet
, prtMarkerSuppliesDescription
))
847 * Get supply name/description...
850 i
= packet
->object_name
[prtMarkerSuppliesDescriptionOffset
];
851 if (i
< 1 || i
> CUPS_MAX_SUPPLIES
||
852 packet
->object_type
!= CUPS_ASN1_OCTET_STRING
)
855 if (i
> num_supplies
)
860 case CUPS_TC_csASCII
:
861 case CUPS_TC_csUTF8
:
862 case CUPS_TC_csUnicodeASCII
:
863 strlcpy(supplies
[i
- 1].name
,
864 (char *)packet
->object_value
.string
.bytes
,
865 sizeof(supplies
[0].name
));
868 case CUPS_TC_csISOLatin1
:
869 case CUPS_TC_csUnicodeLatin1
:
870 cupsCharsetToUTF8((cups_utf8_t
*)supplies
[i
- 1].name
,
871 (char *)packet
->object_value
.string
.bytes
,
872 sizeof(supplies
[0].name
), CUPS_ISO8859_1
);
875 case CUPS_TC_csShiftJIS
:
876 case CUPS_TC_csWindows31J
: /* Close enough for our purposes */
877 cupsCharsetToUTF8((cups_utf8_t
*)supplies
[i
- 1].name
,
878 (char *)packet
->object_value
.string
.bytes
,
879 sizeof(supplies
[0].name
), CUPS_JIS_X0213
);
882 case CUPS_TC_csUCS4
:
883 case CUPS_TC_csUTF32
:
884 case CUPS_TC_csUTF32BE
:
885 case CUPS_TC_csUTF32LE
:
886 cupsUTF32ToUTF8((cups_utf8_t
*)supplies
[i
- 1].name
,
887 (cups_utf32_t
*)packet
->object_value
.string
.bytes
,
888 sizeof(supplies
[0].name
));
891 case CUPS_TC_csUnicode
:
892 case CUPS_TC_csUTF16BE
:
893 case CUPS_TC_csUTF16LE
:
894 utf16_to_utf8((cups_utf8_t
*)supplies
[i
- 1].name
,
895 packet
->object_value
.string
.bytes
,
896 packet
->object_value
.string
.num_bytes
,
897 sizeof(supplies
[0].name
), charset
== CUPS_TC_csUTF16LE
);
902 * If we get here, the printer is using an unknown character set and
903 * we just want to copy characters that look like ASCII...
907 char *src
, *dst
; /* Pointers into strings */
910 * Loop safe because both the object_value and supplies char arrays
911 * are CUPS_SNMP_MAX_STRING elements long.
914 for (src
= (char *)packet
->object_value
.string
.bytes
,
915 dst
= supplies
[i
- 1].name
;
919 if ((*src
& 0x80) || *src
< ' ' || *src
== 0x7f)
930 fprintf(stderr
, "DEBUG2: prtMarkerSuppliesDescription.1.%d = \"%s\"\n", i
,
931 supplies
[i
- 1].name
);
934 else if (_cupsSNMPIsOIDPrefixed(packet
, prtMarkerSuppliesLevel
))
940 i
= packet
->object_name
[prtMarkerSuppliesLevelOffset
];
941 if (i
< 1 || i
> CUPS_MAX_SUPPLIES
||
942 packet
->object_type
!= CUPS_ASN1_INTEGER
)
945 fprintf(stderr
, "DEBUG2: prtMarkerSuppliesLevel.1.%d = %d\n", i
,
946 packet
->object_value
.integer
);
948 if (i
> num_supplies
)
951 supplies
[i
- 1].level
= packet
->object_value
.integer
;
953 else if (_cupsSNMPIsOIDPrefixed(packet
, prtMarkerSuppliesMaxCapacity
) &&
954 !(quirks
& CUPS_SNMP_CAPACITY
))
957 * Get max capacity...
960 i
= packet
->object_name
[prtMarkerSuppliesMaxCapacityOffset
];
961 if (i
< 1 || i
> CUPS_MAX_SUPPLIES
||
962 packet
->object_type
!= CUPS_ASN1_INTEGER
)
965 fprintf(stderr
, "DEBUG2: prtMarkerSuppliesMaxCapacity.1.%d = %d\n", i
,
966 packet
->object_value
.integer
);
968 if (i
> num_supplies
)
971 if (supplies
[i
- 1].max_capacity
== 0 &&
972 packet
->object_value
.integer
> 0)
973 supplies
[i
- 1].max_capacity
= packet
->object_value
.integer
;
975 else if (_cupsSNMPIsOIDPrefixed(packet
, prtMarkerSuppliesClass
))
978 * Get marker class...
981 i
= packet
->object_name
[prtMarkerSuppliesClassOffset
];
982 if (i
< 1 || i
> CUPS_MAX_SUPPLIES
||
983 packet
->object_type
!= CUPS_ASN1_INTEGER
)
986 fprintf(stderr
, "DEBUG2: prtMarkerSuppliesClass.1.%d = %d\n", i
,
987 packet
->object_value
.integer
);
989 if (i
> num_supplies
)
992 supplies
[i
- 1].sclass
= packet
->object_value
.integer
;
994 else if (_cupsSNMPIsOIDPrefixed(packet
, prtMarkerSuppliesType
))
1000 i
= packet
->object_name
[prtMarkerSuppliesTypeOffset
];
1001 if (i
< 1 || i
> CUPS_MAX_SUPPLIES
||
1002 packet
->object_type
!= CUPS_ASN1_INTEGER
)
1005 fprintf(stderr
, "DEBUG2: prtMarkerSuppliesType.1.%d = %d\n", i
,
1006 packet
->object_value
.integer
);
1008 if (i
> num_supplies
)
1011 supplies
[i
- 1].type
= packet
->object_value
.integer
;
1013 else if (_cupsSNMPIsOIDPrefixed(packet
, prtMarkerSuppliesSupplyUnit
))
1016 * Get units for capacity...
1019 i
= packet
->object_name
[prtMarkerSuppliesSupplyUnitOffset
];
1020 if (i
< 1 || i
> CUPS_MAX_SUPPLIES
||
1021 packet
->object_type
!= CUPS_ASN1_INTEGER
)
1024 fprintf(stderr
, "DEBUG2: prtMarkerSuppliesSupplyUnit.1.%d = %d\n", i
,
1025 packet
->object_value
.integer
);
1027 if (i
> num_supplies
)
1030 if (packet
->object_value
.integer
== CUPS_TC_percent
)
1031 supplies
[i
- 1].max_capacity
= 100;
1037 * 'utf16_to_utf8()' - Convert UTF-16 text to UTF-8.
1042 cups_utf8_t
*dst
, /* I - Destination buffer */
1043 const unsigned char *src
, /* I - Source string */
1044 size_t srcsize
, /* I - Size of source string */
1045 size_t dstsize
, /* I - Size of destination buffer */
1046 int le
) /* I - Source is little-endian? */
1048 cups_utf32_t ch
, /* Current character */
1049 temp
[CUPS_SNMP_MAX_STRING
],
1051 *ptr
; /* Pointer into UTF-32 string */
1054 for (ptr
= temp
; srcsize
>= 2;)
1057 ch
= (cups_utf32_t
)(src
[0] | (src
[1] << 8));
1059 ch
= (cups_utf32_t
)((src
[0] << 8) | src
[1]);
1064 if (ch
>= 0xd800 && ch
<= 0xdbff && srcsize
>= 2)
1067 * Multi-word UTF-16 char...
1070 cups_utf32_t lch
; /* Lower word */
1074 lch
= (cups_utf32_t
)(src
[0] | (src
[1] << 8));
1076 lch
= (cups_utf32_t
)((src
[0] << 8) | src
[1]);
1078 if (lch
>= 0xdc00 && lch
<= 0xdfff)
1083 ch
= (((ch
& 0x3ff) << 10) | (lch
& 0x3ff)) + 0x10000;
1087 if (ptr
< (temp
+ CUPS_SNMP_MAX_STRING
- 1))
1093 cupsUTF32ToUTF8(dst
, temp
, (int)dstsize
);