2 * SNMP supplies functions for CUPS.
4 * Copyright © 2008-2015 by Apple Inc.
6 * Licensed under Apache License v2.0. See the file "LICENSE" for more
11 * Include necessary headers.
14 #include "backend-private.h"
15 #include <cups/ppd-private.h>
16 #include <cups/array.h>
23 #define CUPS_MAX_SUPPLIES 32 /* Maximum number of supplies for a printer */
24 #define CUPS_SUPPLY_TIMEOUT 2.0 /* Timeout for SNMP lookups */
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 */
39 #define CUPS_SNMP_NONE 0x0000
40 #define CUPS_SNMP_CAPACITY 0x0001 /* Supply levels reported as percentages */
47 typedef struct /**** Printer supply data ****/
49 char name
[CUPS_SNMP_MAX_STRING
], /* Name of supply */
50 color
[8]; /* Color: "#RRGGBB" or "none" */
51 int colorant
, /* Colorant index */
52 sclass
, /* Supply class */
53 type
, /* Supply type */
54 max_capacity
, /* Maximum capacity */
55 level
; /* Current level value */
58 typedef struct /**** Printer state table ****/
60 int bit
; /* State bit */
61 const char *keyword
; /* IPP printer-state-reasons keyword */
69 static http_addr_t current_addr
; /* Current address */
70 static int current_state
= -1;
71 /* Current device state bits */
72 static int charset
= -1; /* Character set for supply names */
73 static unsigned quirks
= CUPS_SNMP_NONE
;
74 /* Quirks we have to work around */
75 static int num_supplies
= 0;
76 /* Number of supplies found */
77 static backend_supplies_t supplies
[CUPS_MAX_SUPPLIES
];
78 /* Supply information */
79 static int supply_state
= -1;
80 /* Supply state info */
82 static const int hrDeviceDescr
[] =
83 { CUPS_OID_hrDeviceDescr
, 1, -1 };
84 /* Device description OID */
85 static const int hrPrinterStatus
[] =
86 { CUPS_OID_hrPrinterStatus
, 1, -1 };
87 /* Current state OID */
88 static const int hrPrinterDetectedErrorState
[] =
89 { CUPS_OID_hrPrinterDetectedErrorState
, 1, -1 };
90 /* Current printer state bits OID */
91 static const int prtGeneralCurrentLocalization
[] =
92 { CUPS_OID_prtGeneralCurrentLocalization
, 1, -1 };
93 static const int prtLocalizationCharacterSet
[] =
94 { CUPS_OID_prtLocalizationCharacterSet
, 1, 1, -1 },
95 prtLocalizationCharacterSetOffset
=
96 (sizeof(prtLocalizationCharacterSet
) /
97 sizeof(prtLocalizationCharacterSet
[0]));
98 static const int prtMarkerColorantValue
[] =
99 { CUPS_OID_prtMarkerColorantValue
, -1 },
101 prtMarkerColorantValueOffset
=
102 (sizeof(prtMarkerColorantValue
) /
103 sizeof(prtMarkerColorantValue
[0]));
104 /* Offset to colorant index */
105 static const int prtMarkerLifeCount
[] =
106 { CUPS_OID_prtMarkerLifeCount
, 1, 1, -1 };
107 /* Page counter OID */
108 static const int prtMarkerSuppliesEntry
[] =
109 { CUPS_OID_prtMarkerSuppliesEntry
, -1 };
111 static 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 */
118 static 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 */
125 static const int prtMarkerSuppliesLevel
[] =
126 { CUPS_OID_prtMarkerSuppliesLevel
, -1 },
128 prtMarkerSuppliesLevelOffset
=
129 (sizeof(prtMarkerSuppliesLevel
) /
130 sizeof(prtMarkerSuppliesLevel
[0]));
131 /* Offset to supply index */
132 static 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 */
139 static const int prtMarkerSuppliesClass
[] =
140 { CUPS_OID_prtMarkerSuppliesClass
, -1 },
142 prtMarkerSuppliesClassOffset
=
143 (sizeof(prtMarkerSuppliesClass
) /
144 sizeof(prtMarkerSuppliesClass
[0]));
145 /* Offset to supply index */
146 static const int prtMarkerSuppliesType
[] =
147 { CUPS_OID_prtMarkerSuppliesType
, -1 },
149 prtMarkerSuppliesTypeOffset
=
150 (sizeof(prtMarkerSuppliesType
) /
151 sizeof(prtMarkerSuppliesType
[0]));
152 /* Offset to supply index */
153 static const int prtMarkerSuppliesSupplyUnit
[] =
154 { CUPS_OID_prtMarkerSuppliesSupplyUnit
, -1 },
156 prtMarkerSuppliesSupplyUnitOffset
=
157 (sizeof(prtMarkerSuppliesSupplyUnit
) /
158 sizeof(prtMarkerSuppliesSupplyUnit
[0]));
159 /* Offset to supply index */
161 static const backend_state_t printer_states
[] =
163 /* { CUPS_TC_lowPaper, "media-low-report" }, */
164 { CUPS_TC_noPaper
| CUPS_TC_inputTrayEmpty
, "media-empty-warning" },
165 /* { CUPS_TC_lowToner, "toner-low-report" }, */ /* now use prtMarkerSupplies */
166 /* { CUPS_TC_noToner, "toner-empty-warning" }, */ /* now use prtMarkerSupplies */
167 { CUPS_TC_doorOpen
, "door-open-report" },
168 { CUPS_TC_jammed
, "media-jam-warning" },
169 /* { CUPS_TC_offline, "offline-report" }, */ /* unreliable */
170 /* { CUPS_TC_serviceRequested | CUPS_TC_overduePreventMaint, "service-needed-warning" }, */ /* unreliable */
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" }
178 static const backend_state_t supply_states
[] =
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" },
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" },
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" },
199 static void backend_init_supplies(int snmp_fd
, http_addr_t
*addr
);
200 static void backend_walk_cb(cups_snmp_t
*packet
, void *data
);
201 static void utf16_to_utf8(cups_utf8_t
*dst
, const unsigned char *src
,
202 size_t srcsize
, size_t dstsize
, int le
);
206 * 'backendSNMPSupplies()' - Get the current supplies for a device.
209 int /* O - 0 on success, -1 on error */
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 */
216 if (!httpAddrEqual(addr
, ¤t_addr
))
217 backend_init_supplies(snmp_fd
, addr
);
218 else if (num_supplies
> 0)
219 _cupsSNMPWalk(snmp_fd
, ¤t_addr
, CUPS_SNMP_VERSION_1
,
220 _cupsSNMPDefaultCommunity(), prtMarkerSuppliesLevel
,
221 CUPS_SUPPLY_TIMEOUT
, backend_walk_cb
, NULL
);
229 if (num_supplies
> 0)
231 int i
, /* Looping var */
232 percent
, /* Percent full */
233 new_state
, /* New state value */
234 change_state
, /* State change */
235 new_supply_state
= 0; /* Supply state */
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 */
242 * Generate the marker-levels value string...
245 for (i
= 0, ptr
= value
; i
< num_supplies
; i
++, ptr
+= strlen(ptr
))
247 if (supplies
[i
].max_capacity
> 0 && supplies
[i
].level
>= 0)
248 percent
= 100 * supplies
[i
].level
/ supplies
[i
].max_capacity
;
249 else if (supplies
[i
].level
>= 0 && supplies
[i
].level
<= 100 &&
250 (quirks
& CUPS_SNMP_CAPACITY
))
251 percent
= supplies
[i
].level
;
255 if (supplies
[i
].sclass
== CUPS_TC_receptacleThatIsFilled
)
256 percent
= 100 - percent
;
260 switch (supplies
[i
].type
)
263 case CUPS_TC_tonerCartridge
:
265 new_supply_state
|= CUPS_TONER_EMPTY
;
267 new_supply_state
|= CUPS_TONER_LOW
;
270 case CUPS_TC_inkCartridge
:
271 case CUPS_TC_inkRibbon
:
272 case CUPS_TC_solidWax
:
273 case CUPS_TC_ribbonWax
:
275 new_supply_state
|= CUPS_MARKER_SUPPLY_EMPTY
;
277 new_supply_state
|= CUPS_MARKER_SUPPLY_LOW
;
279 case CUPS_TC_developer
:
281 new_supply_state
|= CUPS_DEVELOPER_EMPTY
;
283 new_supply_state
|= CUPS_DEVELOPER_LOW
;
285 case CUPS_TC_coronaWire
:
288 case CUPS_TC_transferUnit
:
290 new_supply_state
|= CUPS_OPC_LIFE_OVER
;
292 new_supply_state
|= CUPS_OPC_NEAR_EOL
;
294 #if 0 /* Because no two vendors report waste containers the same, disable SNMP reporting of same */
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
:
301 new_supply_state
|= CUPS_WASTE_FULL
;
303 new_supply_state
|= CUPS_WASTE_ALMOST_FULL
;
306 case CUPS_TC_cleanerUnit
:
307 case CUPS_TC_fuserCleaningPad
:
309 new_supply_state
|= CUPS_CLEANER_LIFE_OVER
;
311 new_supply_state
|= CUPS_CLEANER_NEAR_EOL
;
319 if ((supplies
[i
].max_capacity
> 0 || (quirks
& CUPS_SNMP_CAPACITY
)) &&
320 supplies
[i
].level
>= 0)
321 snprintf(ptr
, sizeof(value
) - (size_t)(ptr
- value
), "%d", percent
);
323 strlcpy(ptr
, "-1", sizeof(value
) - (size_t)(ptr
- value
));
326 fprintf(stderr
, "ATTR: marker-levels=%s\n", value
);
328 if (supply_state
< 0)
329 change_state
= 0xffff;
331 change_state
= supply_state
^ new_supply_state
;
333 fprintf(stderr
, "DEBUG: new_supply_state=%x, change_state=%x\n",
334 new_supply_state
, change_state
);
337 i
< (int)(sizeof(supply_states
) / sizeof(supply_states
[0]));
339 if (change_state
& supply_states
[i
].bit
)
341 fprintf(stderr
, "STATE: %c%s\n",
342 (new_supply_state
& supply_states
[i
].bit
) ? '+' : '-',
343 supply_states
[i
].keyword
);
346 supply_state
= new_supply_state
;
349 * Get the current printer status bits...
352 if (!_cupsSNMPWrite(snmp_fd
, addr
, CUPS_SNMP_VERSION_1
,
353 _cupsSNMPDefaultCommunity(), CUPS_ASN1_GET_REQUEST
, 1,
354 hrPrinterDetectedErrorState
))
357 if (!_cupsSNMPRead(snmp_fd
, &packet
, CUPS_SUPPLY_TIMEOUT
) ||
358 packet
.object_type
!= CUPS_ASN1_OCTET_STRING
)
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];
364 else if (packet
.object_value
.string
.num_bytes
== 1)
365 new_state
= (packet
.object_value
.string
.bytes
[0] << 8);
369 if (current_state
< 0)
370 change_state
= 0xffff;
372 change_state
= current_state
^ new_state
;
374 fprintf(stderr
, "DEBUG: new_state=%x, change_state=%x\n", new_state
,
378 i
< (int)(sizeof(printer_states
) / sizeof(printer_states
[0]));
380 if (change_state
& printer_states
[i
].bit
)
382 fprintf(stderr
, "STATE: %c%s\n",
383 (new_state
& printer_states
[i
].bit
) ? '+' : '-',
384 printer_states
[i
].keyword
);
387 current_state
= new_state
;
390 * Get the current printer state...
395 if (!_cupsSNMPWrite(snmp_fd
, addr
, CUPS_SNMP_VERSION_1
,
396 _cupsSNMPDefaultCommunity(), CUPS_ASN1_GET_REQUEST
, 1,
400 if (!_cupsSNMPRead(snmp_fd
, &packet
, CUPS_SUPPLY_TIMEOUT
) ||
401 packet
.object_type
!= CUPS_ASN1_INTEGER
)
404 *printer_state
= packet
.object_value
.integer
;
408 * Get the current page count...
413 if (!_cupsSNMPWrite(snmp_fd
, addr
, CUPS_SNMP_VERSION_1
,
414 _cupsSNMPDefaultCommunity(), CUPS_ASN1_GET_REQUEST
, 1,
418 if (!_cupsSNMPRead(snmp_fd
, &packet
, CUPS_SUPPLY_TIMEOUT
) ||
419 packet
.object_type
!= CUPS_ASN1_COUNTER
)
422 *page_count
= packet
.object_value
.counter
;
433 * 'backend_init_supplies()' - Initialize the supplies list.
437 backend_init_supplies(
438 int snmp_fd
, /* I - SNMP socket */
439 http_addr_t
*addr
) /* I - Printer address */
441 int i
, /* Looping var */
442 type
; /* Current marker type */
443 const char *community
; /* SNMP community name */
444 cups_file_t
*cachefile
; /* Cache file */
445 const char *cachedir
; /* CUPS_CACHEDIR value */
446 char addrstr
[1024], /* Address string */
447 cachefilename
[1024], /* Cache filename */
448 description
[CUPS_SNMP_MAX_STRING
],
449 /* Device description string */
450 value
[CUPS_MAX_SUPPLIES
* (CUPS_SNMP_MAX_STRING
* 4 + 3)],
452 *ptr
, /* Pointer into value string */
453 *name_ptr
; /* Pointer into name string */
454 cups_snmp_t packet
; /* SNMP response packet */
455 ppd_file_t
*ppd
; /* PPD file for this queue */
456 ppd_attr_t
*ppdattr
; /* cupsSNMPSupplies attribute */
457 static const char * const types
[] = /* Supply types */
477 "fuser-cleaning-pad",
483 "glue-water-additive",
497 * Reset state information...
500 current_addr
= *addr
;
505 memset(supplies
, 0, sizeof(supplies
));
508 * See if we should be getting supply levels via SNMP...
511 community
= _cupsSNMPDefaultCommunity();
515 if ((ppd
= ppdOpenFile(getenv("PPD"))) == NULL
||
516 ((ppdattr
= ppdFindAttr(ppd
, "cupsSNMPSupplies", NULL
)) != NULL
&&
517 ppdattr
->value
&& _cups_strcasecmp(ppdattr
->value
, "true")))
523 if ((ppdattr
= ppdFindAttr(ppd
, "cupsSNMPQuirks", NULL
)) != NULL
)
525 if (!_cups_strcasecmp(ppdattr
->value
, "capacity"))
526 quirks
|= CUPS_SNMP_CAPACITY
;
532 * Get the device description...
535 if (!_cupsSNMPWrite(snmp_fd
, addr
, CUPS_SNMP_VERSION_1
,
536 community
, CUPS_ASN1_GET_REQUEST
, 1,
540 if (!_cupsSNMPRead(snmp_fd
, &packet
, CUPS_SUPPLY_TIMEOUT
) ||
541 packet
.object_type
!= CUPS_ASN1_OCTET_STRING
)
543 strlcpy(description
, "Unknown", sizeof(description
));
547 strlcpy(description
, (char *)packet
.object_value
.string
.bytes
,
548 sizeof(description
));
550 fprintf(stderr
, "DEBUG2: hrDeviceDesc=\"%s\"\n", description
);
553 * See if we have already queried this device...
556 httpAddrString(addr
, addrstr
, sizeof(addrstr
));
558 if ((cachedir
= getenv("CUPS_CACHEDIR")) == NULL
)
559 cachedir
= CUPS_CACHEDIR
;
561 snprintf(cachefilename
, sizeof(cachefilename
), "%s/%s.snmp", cachedir
,
564 if ((cachefile
= cupsFileOpen(cachefilename
, "r")) != NULL
)
567 * Yes, read the cache file:
569 * 3 num_supplies charset
571 * supply structures...
574 if (cupsFileGets(cachefile
, value
, sizeof(value
)))
576 if (sscanf(value
, "3 %d%d", &num_supplies
, &charset
) == 2 &&
577 num_supplies
<= CUPS_MAX_SUPPLIES
&&
578 cupsFileGets(cachefile
, value
, sizeof(value
)))
580 if (!strcmp(description
, value
))
581 cupsFileRead(cachefile
, (char *)supplies
,
582 (size_t)num_supplies
* sizeof(backend_supplies_t
));
596 cupsFileClose(cachefile
);
600 * If the cache information isn't correct, scan for supplies...
606 * Get the configured character set...
609 int oid
[CUPS_SNMP_MAX_OID
]; /* OID for character set */
612 if (!_cupsSNMPWrite(snmp_fd
, ¤t_addr
, CUPS_SNMP_VERSION_1
,
613 community
, CUPS_ASN1_GET_REQUEST
, 1,
614 prtGeneralCurrentLocalization
))
617 if (!_cupsSNMPRead(snmp_fd
, &packet
, CUPS_SUPPLY_TIMEOUT
) ||
618 packet
.object_type
!= CUPS_ASN1_INTEGER
)
621 "DEBUG: prtGeneralCurrentLocalization type is %x, expected %x!\n",
622 packet
.object_type
, CUPS_ASN1_INTEGER
);
626 fprintf(stderr
, "DEBUG2: prtGeneralCurrentLocalization=%d\n",
627 packet
.object_value
.integer
);
629 _cupsSNMPCopyOID(oid
, prtLocalizationCharacterSet
, CUPS_SNMP_MAX_OID
);
630 oid
[prtLocalizationCharacterSetOffset
- 2] = packet
.object_value
.integer
;
633 if (!_cupsSNMPWrite(snmp_fd
, ¤t_addr
, CUPS_SNMP_VERSION_1
,
634 community
, CUPS_ASN1_GET_REQUEST
, 1,
638 if (!_cupsSNMPRead(snmp_fd
, &packet
, CUPS_SUPPLY_TIMEOUT
) ||
639 packet
.object_type
!= CUPS_ASN1_INTEGER
)
642 "DEBUG: prtLocalizationCharacterSet type is %x, expected %x!\n",
643 packet
.object_type
, CUPS_ASN1_INTEGER
);
647 fprintf(stderr
, "DEBUG2: prtLocalizationCharacterSet=%d\n",
648 packet
.object_value
.integer
);
649 charset
= packet
.object_value
.integer
;
652 if (num_supplies
< 0)
655 * Walk the printer configuration information...
658 _cupsSNMPWalk(snmp_fd
, ¤t_addr
, CUPS_SNMP_VERSION_1
,
659 community
, prtMarkerSuppliesEntry
,
660 CUPS_SUPPLY_TIMEOUT
, backend_walk_cb
, NULL
);
664 * Save the cached information...
667 if (num_supplies
< 0)
670 if ((cachefile
= cupsFileOpen(cachefilename
, "w")) != NULL
)
672 cupsFilePrintf(cachefile
, "3 %d %d\n", num_supplies
, charset
);
673 cupsFilePrintf(cachefile
, "%s\n", description
);
675 if (num_supplies
> 0)
676 cupsFileWrite(cachefile
, (char *)supplies
,
677 (size_t)num_supplies
* sizeof(backend_supplies_t
));
679 cupsFileClose(cachefile
);
682 if (num_supplies
<= 0)
689 for (i
= 0; i
< num_supplies
; i
++)
690 strlcpy(supplies
[i
].color
, "none", sizeof(supplies
[i
].color
));
692 _cupsSNMPWalk(snmp_fd
, ¤t_addr
, CUPS_SNMP_VERSION_1
,
693 community
, prtMarkerColorantValue
,
694 CUPS_SUPPLY_TIMEOUT
, backend_walk_cb
, NULL
);
697 * Output the marker-colors attribute...
700 for (i
= 0, ptr
= value
; i
< num_supplies
; i
++, ptr
+= strlen(ptr
))
705 strlcpy(ptr
, supplies
[i
].color
, sizeof(value
) - (size_t)(ptr
- value
));
708 fprintf(stderr
, "ATTR: marker-colors=%s\n", value
);
711 * Output the marker-names attribute (the double quoting is necessary to deal
712 * with embedded quotes and commas in the marker names...)
715 for (i
= 0, ptr
= value
; i
< num_supplies
; i
++)
722 for (name_ptr
= supplies
[i
].name
; *name_ptr
;)
724 if (*name_ptr
== '\\' || *name_ptr
== '\"' || *name_ptr
== '\'')
731 *ptr
++ = *name_ptr
++;
739 fprintf(stderr
, "ATTR: marker-names=%s\n", value
);
742 * Output the marker-types attribute...
745 for (i
= 0, ptr
= value
; i
< num_supplies
; i
++, ptr
+= strlen(ptr
))
750 type
= supplies
[i
].type
;
752 if (type
< CUPS_TC_other
|| type
> CUPS_TC_covers
)
753 strlcpy(ptr
, "unknown", sizeof(value
) - (size_t)(ptr
- value
));
755 strlcpy(ptr
, types
[type
- CUPS_TC_other
], sizeof(value
) - (size_t)(ptr
- value
));
758 fprintf(stderr
, "ATTR: marker-types=%s\n", value
);
763 * 'backend_walk_cb()' - Interpret the supply value responses.
767 backend_walk_cb(cups_snmp_t
*packet
, /* I - SNMP packet */
768 void *data
) /* I - User data (unused) */
770 int i
, j
, k
; /* Looping vars */
771 static const char * const colors
[][2] =
772 { /* Standard color names */
773 { "black", "#000000" },
774 { "blue", "#0000FF" },
775 { "brown", "#A52A2A" },
776 { "cyan", "#00FFFF" },
777 { "dark-gray", "#404040" },
778 { "dark gray", "#404040" },
779 { "dark-yellow", "#FFCC00" },
780 { "dark yellow", "#FFCC00" },
781 { "gold", "#FFD700" },
782 { "gray", "#808080" },
783 { "green", "#00FF00" },
784 { "light-black", "#606060" },
785 { "light black", "#606060" },
786 { "light-cyan", "#E0FFFF" },
787 { "light cyan", "#E0FFFF" },
788 { "light-gray", "#D3D3D3" },
789 { "light gray", "#D3D3D3" },
790 { "light-magenta", "#FF77FF" },
791 { "light magenta", "#FF77FF" },
792 { "magenta", "#FF00FF" },
793 { "orange", "#FFA500" },
794 { "red", "#FF0000" },
795 { "silver", "#C0C0C0" },
796 { "white", "#FFFFFF" },
797 { "yellow", "#FFFF00" }
803 if (_cupsSNMPIsOIDPrefixed(packet
, prtMarkerColorantValue
) &&
804 packet
->object_type
== CUPS_ASN1_OCTET_STRING
)
810 i
= packet
->object_name
[prtMarkerColorantValueOffset
];
812 fprintf(stderr
, "DEBUG2: prtMarkerColorantValue.1.%d = \"%s\"\n", i
,
813 (char *)packet
->object_value
.string
.bytes
);
815 for (j
= 0; j
< num_supplies
; j
++)
816 if (supplies
[j
].colorant
== i
)
818 for (k
= 0; k
< (int)(sizeof(colors
) / sizeof(colors
[0])); k
++)
819 if (!_cups_strcasecmp(colors
[k
][0],
820 (char *)packet
->object_value
.string
.bytes
))
822 strlcpy(supplies
[j
].color
, colors
[k
][1], sizeof(supplies
[j
].color
));
827 else if (_cupsSNMPIsOIDPrefixed(packet
, prtMarkerSuppliesColorantIndex
))
830 * Get colorant index...
833 i
= packet
->object_name
[prtMarkerSuppliesColorantIndexOffset
];
834 if (i
< 1 || i
> CUPS_MAX_SUPPLIES
||
835 packet
->object_type
!= CUPS_ASN1_INTEGER
)
838 fprintf(stderr
, "DEBUG2: prtMarkerSuppliesColorantIndex.1.%d = %d\n", i
,
839 packet
->object_value
.integer
);
841 if (i
> num_supplies
)
844 supplies
[i
- 1].colorant
= packet
->object_value
.integer
;
846 else if (_cupsSNMPIsOIDPrefixed(packet
, prtMarkerSuppliesDescription
))
849 * Get supply name/description...
852 i
= packet
->object_name
[prtMarkerSuppliesDescriptionOffset
];
853 if (i
< 1 || i
> CUPS_MAX_SUPPLIES
||
854 packet
->object_type
!= CUPS_ASN1_OCTET_STRING
)
857 if (i
> num_supplies
)
862 case CUPS_TC_csASCII
:
863 case CUPS_TC_csUTF8
:
864 case CUPS_TC_csUnicodeASCII
:
865 strlcpy(supplies
[i
- 1].name
,
866 (char *)packet
->object_value
.string
.bytes
,
867 sizeof(supplies
[0].name
));
870 case CUPS_TC_csISOLatin1
:
871 case CUPS_TC_csUnicodeLatin1
:
872 cupsCharsetToUTF8((cups_utf8_t
*)supplies
[i
- 1].name
,
873 (char *)packet
->object_value
.string
.bytes
,
874 sizeof(supplies
[0].name
), CUPS_ISO8859_1
);
877 case CUPS_TC_csShiftJIS
:
878 case CUPS_TC_csWindows31J
: /* Close enough for our purposes */
879 cupsCharsetToUTF8((cups_utf8_t
*)supplies
[i
- 1].name
,
880 (char *)packet
->object_value
.string
.bytes
,
881 sizeof(supplies
[0].name
), CUPS_JIS_X0213
);
884 case CUPS_TC_csUCS4
:
885 case CUPS_TC_csUTF32
:
886 case CUPS_TC_csUTF32BE
:
887 case CUPS_TC_csUTF32LE
:
888 cupsUTF32ToUTF8((cups_utf8_t
*)supplies
[i
- 1].name
,
889 (cups_utf32_t
*)packet
->object_value
.string
.bytes
,
890 sizeof(supplies
[0].name
));
893 case CUPS_TC_csUnicode
:
894 case CUPS_TC_csUTF16BE
:
895 case CUPS_TC_csUTF16LE
:
896 utf16_to_utf8((cups_utf8_t
*)supplies
[i
- 1].name
,
897 packet
->object_value
.string
.bytes
,
898 packet
->object_value
.string
.num_bytes
,
899 sizeof(supplies
[0].name
), charset
== CUPS_TC_csUTF16LE
);
904 * If we get here, the printer is using an unknown character set and
905 * we just want to copy characters that look like ASCII...
909 char *src
, *dst
; /* Pointers into strings */
912 * Loop safe because both the object_value and supplies char arrays
913 * are CUPS_SNMP_MAX_STRING elements long.
916 for (src
= (char *)packet
->object_value
.string
.bytes
,
917 dst
= supplies
[i
- 1].name
;
921 if ((*src
& 0x80) || *src
< ' ' || *src
== 0x7f)
932 fprintf(stderr
, "DEBUG2: prtMarkerSuppliesDescription.1.%d = \"%s\"\n", i
,
933 supplies
[i
- 1].name
);
936 else if (_cupsSNMPIsOIDPrefixed(packet
, prtMarkerSuppliesLevel
))
942 i
= packet
->object_name
[prtMarkerSuppliesLevelOffset
];
943 if (i
< 1 || i
> CUPS_MAX_SUPPLIES
||
944 packet
->object_type
!= CUPS_ASN1_INTEGER
)
947 fprintf(stderr
, "DEBUG2: prtMarkerSuppliesLevel.1.%d = %d\n", i
,
948 packet
->object_value
.integer
);
950 if (i
> num_supplies
)
953 supplies
[i
- 1].level
= packet
->object_value
.integer
;
955 else if (_cupsSNMPIsOIDPrefixed(packet
, prtMarkerSuppliesMaxCapacity
) &&
956 !(quirks
& CUPS_SNMP_CAPACITY
))
959 * Get max capacity...
962 i
= packet
->object_name
[prtMarkerSuppliesMaxCapacityOffset
];
963 if (i
< 1 || i
> CUPS_MAX_SUPPLIES
||
964 packet
->object_type
!= CUPS_ASN1_INTEGER
)
967 fprintf(stderr
, "DEBUG2: prtMarkerSuppliesMaxCapacity.1.%d = %d\n", i
,
968 packet
->object_value
.integer
);
970 if (i
> num_supplies
)
973 if (supplies
[i
- 1].max_capacity
== 0 &&
974 packet
->object_value
.integer
> 0)
975 supplies
[i
- 1].max_capacity
= packet
->object_value
.integer
;
977 else if (_cupsSNMPIsOIDPrefixed(packet
, prtMarkerSuppliesClass
))
980 * Get marker class...
983 i
= packet
->object_name
[prtMarkerSuppliesClassOffset
];
984 if (i
< 1 || i
> CUPS_MAX_SUPPLIES
||
985 packet
->object_type
!= CUPS_ASN1_INTEGER
)
988 fprintf(stderr
, "DEBUG2: prtMarkerSuppliesClass.1.%d = %d\n", i
,
989 packet
->object_value
.integer
);
991 if (i
> num_supplies
)
994 supplies
[i
- 1].sclass
= packet
->object_value
.integer
;
996 else if (_cupsSNMPIsOIDPrefixed(packet
, prtMarkerSuppliesType
))
1002 i
= packet
->object_name
[prtMarkerSuppliesTypeOffset
];
1003 if (i
< 1 || i
> CUPS_MAX_SUPPLIES
||
1004 packet
->object_type
!= CUPS_ASN1_INTEGER
)
1007 fprintf(stderr
, "DEBUG2: prtMarkerSuppliesType.1.%d = %d\n", i
,
1008 packet
->object_value
.integer
);
1010 if (i
> num_supplies
)
1013 supplies
[i
- 1].type
= packet
->object_value
.integer
;
1015 else if (_cupsSNMPIsOIDPrefixed(packet
, prtMarkerSuppliesSupplyUnit
))
1018 * Get units for capacity...
1021 i
= packet
->object_name
[prtMarkerSuppliesSupplyUnitOffset
];
1022 if (i
< 1 || i
> CUPS_MAX_SUPPLIES
||
1023 packet
->object_type
!= CUPS_ASN1_INTEGER
)
1026 fprintf(stderr
, "DEBUG2: prtMarkerSuppliesSupplyUnit.1.%d = %d\n", i
,
1027 packet
->object_value
.integer
);
1029 if (i
> num_supplies
)
1032 if (packet
->object_value
.integer
== CUPS_TC_percent
)
1033 supplies
[i
- 1].max_capacity
= 100;
1039 * 'utf16_to_utf8()' - Convert UTF-16 text to UTF-8.
1044 cups_utf8_t
*dst
, /* I - Destination buffer */
1045 const unsigned char *src
, /* I - Source string */
1046 size_t srcsize
, /* I - Size of source string */
1047 size_t dstsize
, /* I - Size of destination buffer */
1048 int le
) /* I - Source is little-endian? */
1050 cups_utf32_t ch
, /* Current character */
1051 temp
[CUPS_SNMP_MAX_STRING
],
1053 *ptr
; /* Pointer into UTF-32 string */
1056 for (ptr
= temp
; srcsize
>= 2;)
1059 ch
= (cups_utf32_t
)(src
[0] | (src
[1] << 8));
1061 ch
= (cups_utf32_t
)((src
[0] << 8) | src
[1]);
1066 if (ch
>= 0xd800 && ch
<= 0xdbff && srcsize
>= 2)
1069 * Multi-word UTF-16 char...
1072 cups_utf32_t lch
; /* Lower word */
1076 lch
= (cups_utf32_t
)(src
[0] | (src
[1] << 8));
1078 lch
= (cups_utf32_t
)((src
[0] << 8) | src
[1]);
1080 if (lch
>= 0xdc00 && lch
<= 0xdfff)
1085 ch
= (((ch
& 0x3ff) << 10) | (lch
& 0x3ff)) + 0x10000;
1089 if (ptr
< (temp
+ CUPS_SNMP_MAX_STRING
- 1))
1095 cupsUTF32ToUTF8(dst
, temp
, (int)dstsize
);