4 * SNMP supplies functions for CUPS.
6 * Copyright 2008-2014 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/array.h>
29 #define CUPS_MAX_SUPPLIES 32 /* Maximum number of supplies for a printer */
30 #define CUPS_SUPPLY_TIMEOUT 2.0 /* Timeout for SNMP lookups */
32 #define CUPS_DEVELOPER_LOW 0x0001
33 #define CUPS_DEVELOPER_EMPTY 0x0002
34 #define CUPS_MARKER_SUPPLY_LOW 0x0004
35 #define CUPS_MARKER_SUPPLY_EMPTY 0x0008
36 #define CUPS_OPC_NEAR_EOL 0x0010
37 #define CUPS_OPC_LIFE_OVER 0x0020
38 #define CUPS_TONER_LOW 0x0040
39 #define CUPS_TONER_EMPTY 0x0080
40 #define CUPS_WASTE_ALMOST_FULL 0x0100
41 #define CUPS_WASTE_FULL 0x0200
42 #define CUPS_CLEANER_NEAR_EOL 0x0400 /* Proposed JPS3 */
43 #define CUPS_CLEANER_LIFE_OVER 0x0800 /* Proposed JPS3 */
45 #define CUPS_SNMP_NONE 0x0000
46 #define CUPS_SNMP_CAPACITY 0x0001 /* Supply levels reported as percentages */
53 typedef struct /**** Printer supply data ****/
55 char name
[CUPS_SNMP_MAX_STRING
], /* Name of supply */
56 color
[8]; /* Color: "#RRGGBB" or "none" */
57 int colorant
, /* Colorant index */
58 sclass
, /* Supply class */
59 type
, /* Supply type */
60 max_capacity
, /* Maximum capacity */
61 level
; /* Current level value */
64 typedef struct /**** Printer state table ****/
66 int bit
; /* State bit */
67 const char *keyword
; /* IPP printer-state-reasons keyword */
75 static http_addr_t current_addr
; /* Current address */
76 static int current_state
= -1;
77 /* Current device state bits */
78 static int charset
= -1; /* Character set for supply names */
79 static unsigned quirks
= CUPS_SNMP_NONE
;
80 /* Quirks we have to work around */
81 static int num_supplies
= 0;
82 /* Number of supplies found */
83 static backend_supplies_t supplies
[CUPS_MAX_SUPPLIES
];
84 /* Supply information */
85 static int supply_state
= -1;
86 /* Supply state info */
88 static const int hrDeviceDescr
[] =
89 { CUPS_OID_hrDeviceDescr
, 1, -1 };
90 /* Device description OID */
91 static const int hrPrinterStatus
[] =
92 { CUPS_OID_hrPrinterStatus
, 1, -1 };
93 /* Current state OID */
94 static const int hrPrinterDetectedErrorState
[] =
95 { CUPS_OID_hrPrinterDetectedErrorState
, 1, -1 };
96 /* Current printer state bits OID */
97 static const int prtGeneralCurrentLocalization
[] =
98 { CUPS_OID_prtGeneralCurrentLocalization
, 1, -1 };
99 static const int prtLocalizationCharacterSet
[] =
100 { CUPS_OID_prtLocalizationCharacterSet
, 1, 1, -1 },
101 prtLocalizationCharacterSetOffset
=
102 (sizeof(prtLocalizationCharacterSet
) /
103 sizeof(prtLocalizationCharacterSet
[0]));
104 static const int prtMarkerColorantValue
[] =
105 { CUPS_OID_prtMarkerColorantValue
, -1 },
107 prtMarkerColorantValueOffset
=
108 (sizeof(prtMarkerColorantValue
) /
109 sizeof(prtMarkerColorantValue
[0]));
110 /* Offset to colorant index */
111 static const int prtMarkerLifeCount
[] =
112 { CUPS_OID_prtMarkerLifeCount
, 1, 1, -1 };
113 /* Page counter OID */
114 static const int prtMarkerSuppliesEntry
[] =
115 { CUPS_OID_prtMarkerSuppliesEntry
, -1 };
117 static const int prtMarkerSuppliesColorantIndex
[] =
118 { CUPS_OID_prtMarkerSuppliesColorantIndex
, -1 },
119 /* Colorant index OID */
120 prtMarkerSuppliesColorantIndexOffset
=
121 (sizeof(prtMarkerSuppliesColorantIndex
) /
122 sizeof(prtMarkerSuppliesColorantIndex
[0]));
123 /* Offset to supply index */
124 static const int prtMarkerSuppliesDescription
[] =
125 { CUPS_OID_prtMarkerSuppliesDescription
, -1 },
126 /* Description OID */
127 prtMarkerSuppliesDescriptionOffset
=
128 (sizeof(prtMarkerSuppliesDescription
) /
129 sizeof(prtMarkerSuppliesDescription
[0]));
130 /* Offset to supply index */
131 static const int prtMarkerSuppliesLevel
[] =
132 { CUPS_OID_prtMarkerSuppliesLevel
, -1 },
134 prtMarkerSuppliesLevelOffset
=
135 (sizeof(prtMarkerSuppliesLevel
) /
136 sizeof(prtMarkerSuppliesLevel
[0]));
137 /* Offset to supply index */
138 static const int prtMarkerSuppliesMaxCapacity
[] =
139 { CUPS_OID_prtMarkerSuppliesMaxCapacity
, -1 },
140 /* Max capacity OID */
141 prtMarkerSuppliesMaxCapacityOffset
=
142 (sizeof(prtMarkerSuppliesMaxCapacity
) /
143 sizeof(prtMarkerSuppliesMaxCapacity
[0]));
144 /* Offset to supply index */
145 static const int prtMarkerSuppliesClass
[] =
146 { CUPS_OID_prtMarkerSuppliesClass
, -1 },
148 prtMarkerSuppliesClassOffset
=
149 (sizeof(prtMarkerSuppliesClass
) /
150 sizeof(prtMarkerSuppliesClass
[0]));
151 /* Offset to supply index */
152 static const int prtMarkerSuppliesType
[] =
153 { CUPS_OID_prtMarkerSuppliesType
, -1 },
155 prtMarkerSuppliesTypeOffset
=
156 (sizeof(prtMarkerSuppliesType
) /
157 sizeof(prtMarkerSuppliesType
[0]));
158 /* Offset to supply index */
159 static const int prtMarkerSuppliesSupplyUnit
[] =
160 { CUPS_OID_prtMarkerSuppliesSupplyUnit
, -1 },
162 prtMarkerSuppliesSupplyUnitOffset
=
163 (sizeof(prtMarkerSuppliesSupplyUnit
) /
164 sizeof(prtMarkerSuppliesSupplyUnit
[0]));
165 /* Offset to supply index */
167 static const backend_state_t printer_states
[] =
169 /* { CUPS_TC_lowPaper, "media-low-report" }, */
170 { CUPS_TC_noPaper
| CUPS_TC_inputTrayEmpty
, "media-empty-warning" },
171 /* { CUPS_TC_lowToner, "toner-low-report" }, */ /* now use prtMarkerSupplies */
172 /* { CUPS_TC_noToner, "toner-empty-warning" }, */ /* now use prtMarkerSupplies */
173 { CUPS_TC_doorOpen
, "door-open-report" },
174 { CUPS_TC_jammed
, "media-jam-warning" },
175 /* { CUPS_TC_offline, "offline-report" }, */ /* unreliable */
176 /* { CUPS_TC_serviceRequested | CUPS_TC_overduePreventMaint, "service-needed-warning" }, */ /* unreliable */
177 { CUPS_TC_inputTrayMissing
, "input-tray-missing-warning" },
178 { CUPS_TC_outputTrayMissing
, "output-tray-missing-warning" },
179 { CUPS_TC_markerSupplyMissing
, "marker-supply-missing-warning" },
180 { CUPS_TC_outputNearFull
, "output-area-almost-full-report" },
181 { CUPS_TC_outputFull
, "output-area-full-warning" }
184 static const backend_state_t supply_states
[] =
186 { CUPS_DEVELOPER_LOW
, "developer-low-report" },
187 { CUPS_DEVELOPER_EMPTY
, "developer-empty-warning" },
188 { CUPS_MARKER_SUPPLY_LOW
, "marker-supply-low-report" },
189 { CUPS_MARKER_SUPPLY_EMPTY
, "marker-supply-empty-warning" },
190 { CUPS_OPC_NEAR_EOL
, "opc-near-eol-report" },
191 { CUPS_OPC_LIFE_OVER
, "opc-life-over-warning" },
192 { CUPS_TONER_LOW
, "toner-low-report" },
193 { CUPS_TONER_EMPTY
, "toner-empty-warning" },
194 { CUPS_WASTE_ALMOST_FULL
, "waste-receptacle-almost-full-report" },
195 { CUPS_WASTE_FULL
, "waste-receptacle-full-warning" },
196 { CUPS_CLEANER_NEAR_EOL
, "cleaner-life-almost-over-report" },
197 { CUPS_CLEANER_LIFE_OVER
, "cleaner-life-over-warning" },
205 static void backend_init_supplies(int snmp_fd
, http_addr_t
*addr
);
206 static void backend_walk_cb(cups_snmp_t
*packet
, void *data
);
207 static void utf16_to_utf8(cups_utf8_t
*dst
, const unsigned char *src
,
208 size_t srcsize
, size_t dstsize
, int le
);
212 * 'backendSNMPSupplies()' - Get the current supplies for a device.
215 int /* O - 0 on success, -1 on error */
217 int snmp_fd
, /* I - SNMP socket */
218 http_addr_t
*addr
, /* I - Printer address */
219 int *page_count
, /* O - Page count */
220 int *printer_state
) /* O - Printer state */
222 if (!httpAddrEqual(addr
, ¤t_addr
))
223 backend_init_supplies(snmp_fd
, addr
);
224 else if (num_supplies
> 0)
225 _cupsSNMPWalk(snmp_fd
, ¤t_addr
, CUPS_SNMP_VERSION_1
,
226 _cupsSNMPDefaultCommunity(), prtMarkerSuppliesLevel
,
227 CUPS_SUPPLY_TIMEOUT
, backend_walk_cb
, NULL
);
235 if (num_supplies
> 0)
237 int i
, /* Looping var */
238 percent
, /* Percent full */
239 new_state
, /* New state value */
240 change_state
, /* State change */
241 new_supply_state
= 0; /* Supply state */
242 char value
[CUPS_MAX_SUPPLIES
* 4],
243 /* marker-levels value string */
244 *ptr
; /* Pointer into value string */
245 cups_snmp_t packet
; /* SNMP response packet */
248 * Generate the marker-levels value string...
251 for (i
= 0, ptr
= value
; i
< num_supplies
; i
++, ptr
+= strlen(ptr
))
253 if (supplies
[i
].max_capacity
> 0 && supplies
[i
].level
>= 0)
254 percent
= 100 * supplies
[i
].level
/ supplies
[i
].max_capacity
;
255 else if (supplies
[i
].level
>= 0 && supplies
[i
].level
<= 100 &&
256 (quirks
& CUPS_SNMP_CAPACITY
))
257 percent
= supplies
[i
].level
;
261 if (supplies
[i
].sclass
== CUPS_TC_receptacleThatIsFilled
)
262 percent
= 100 - percent
;
266 switch (supplies
[i
].type
)
269 case CUPS_TC_tonerCartridge
:
271 new_supply_state
|= CUPS_TONER_EMPTY
;
273 new_supply_state
|= CUPS_TONER_LOW
;
276 case CUPS_TC_inkCartridge
:
277 case CUPS_TC_inkRibbon
:
278 case CUPS_TC_solidWax
:
279 case CUPS_TC_ribbonWax
:
281 new_supply_state
|= CUPS_MARKER_SUPPLY_EMPTY
;
283 new_supply_state
|= CUPS_MARKER_SUPPLY_LOW
;
285 case CUPS_TC_developer
:
287 new_supply_state
|= CUPS_DEVELOPER_EMPTY
;
289 new_supply_state
|= CUPS_DEVELOPER_LOW
;
291 case CUPS_TC_coronaWire
:
294 case CUPS_TC_transferUnit
:
296 new_supply_state
|= CUPS_OPC_LIFE_OVER
;
298 new_supply_state
|= CUPS_OPC_NEAR_EOL
;
300 case CUPS_TC_wasteInk
:
301 case CUPS_TC_wastePaper
:
302 case CUPS_TC_wasteToner
:
303 case CUPS_TC_wasteWater
:
304 case CUPS_TC_wasteWax
:
306 new_supply_state
|= CUPS_WASTE_FULL
;
308 new_supply_state
|= CUPS_WASTE_ALMOST_FULL
;
310 case CUPS_TC_cleanerUnit
:
311 case CUPS_TC_fuserCleaningPad
:
313 new_supply_state
|= CUPS_CLEANER_LIFE_OVER
;
315 new_supply_state
|= CUPS_CLEANER_NEAR_EOL
;
323 if ((supplies
[i
].max_capacity
> 0 || (quirks
& CUPS_SNMP_CAPACITY
)) &&
324 supplies
[i
].level
>= 0)
325 snprintf(ptr
, sizeof(value
) - (size_t)(ptr
- value
), "%d", percent
);
327 strlcpy(ptr
, "-1", sizeof(value
) - (size_t)(ptr
- value
));
330 fprintf(stderr
, "ATTR: marker-levels=%s\n", value
);
332 if (supply_state
< 0)
333 change_state
= 0xffff;
335 change_state
= supply_state
^ new_supply_state
;
337 fprintf(stderr
, "DEBUG: new_supply_state=%x, change_state=%x\n",
338 new_supply_state
, change_state
);
341 i
< (int)(sizeof(supply_states
) / sizeof(supply_states
[0]));
343 if (change_state
& supply_states
[i
].bit
)
345 fprintf(stderr
, "STATE: %c%s\n",
346 (new_supply_state
& supply_states
[i
].bit
) ? '+' : '-',
347 supply_states
[i
].keyword
);
350 supply_state
= new_supply_state
;
353 * Get the current printer status bits...
356 if (!_cupsSNMPWrite(snmp_fd
, addr
, CUPS_SNMP_VERSION_1
,
357 _cupsSNMPDefaultCommunity(), CUPS_ASN1_GET_REQUEST
, 1,
358 hrPrinterDetectedErrorState
))
361 if (!_cupsSNMPRead(snmp_fd
, &packet
, CUPS_SUPPLY_TIMEOUT
) ||
362 packet
.object_type
!= CUPS_ASN1_OCTET_STRING
)
365 if (packet
.object_value
.string
.num_bytes
== 2)
366 new_state
= (packet
.object_value
.string
.bytes
[0] << 8) |
367 packet
.object_value
.string
.bytes
[1];
368 else if (packet
.object_value
.string
.num_bytes
== 1)
369 new_state
= (packet
.object_value
.string
.bytes
[0] << 8);
373 if (current_state
< 0)
374 change_state
= 0xffff;
376 change_state
= current_state
^ new_state
;
378 fprintf(stderr
, "DEBUG: new_state=%x, change_state=%x\n", new_state
,
382 i
< (int)(sizeof(printer_states
) / sizeof(printer_states
[0]));
384 if (change_state
& printer_states
[i
].bit
)
386 fprintf(stderr
, "STATE: %c%s\n",
387 (new_state
& printer_states
[i
].bit
) ? '+' : '-',
388 printer_states
[i
].keyword
);
391 current_state
= new_state
;
394 * Get the current printer state...
399 if (!_cupsSNMPWrite(snmp_fd
, addr
, CUPS_SNMP_VERSION_1
,
400 _cupsSNMPDefaultCommunity(), CUPS_ASN1_GET_REQUEST
, 1,
404 if (!_cupsSNMPRead(snmp_fd
, &packet
, CUPS_SUPPLY_TIMEOUT
) ||
405 packet
.object_type
!= CUPS_ASN1_INTEGER
)
408 *printer_state
= packet
.object_value
.integer
;
412 * Get the current page count...
417 if (!_cupsSNMPWrite(snmp_fd
, addr
, CUPS_SNMP_VERSION_1
,
418 _cupsSNMPDefaultCommunity(), CUPS_ASN1_GET_REQUEST
, 1,
422 if (!_cupsSNMPRead(snmp_fd
, &packet
, CUPS_SUPPLY_TIMEOUT
) ||
423 packet
.object_type
!= CUPS_ASN1_COUNTER
)
426 *page_count
= packet
.object_value
.counter
;
437 * 'backend_init_supplies()' - Initialize the supplies list.
441 backend_init_supplies(
442 int snmp_fd
, /* I - SNMP socket */
443 http_addr_t
*addr
) /* I - Printer address */
445 int i
, /* Looping var */
446 type
; /* Current marker type */
447 cups_file_t
*cachefile
; /* Cache file */
448 const char *cachedir
; /* CUPS_CACHEDIR value */
449 char addrstr
[1024], /* Address string */
450 cachefilename
[1024], /* Cache filename */
451 description
[CUPS_SNMP_MAX_STRING
],
452 /* Device description string */
453 value
[CUPS_MAX_SUPPLIES
* (CUPS_SNMP_MAX_STRING
* 4 + 3)],
455 *ptr
, /* Pointer into value string */
456 *name_ptr
; /* Pointer into name string */
457 cups_snmp_t packet
; /* SNMP response packet */
458 ppd_file_t
*ppd
; /* PPD file for this queue */
459 ppd_attr_t
*ppdattr
; /* cupsSNMPSupplies attribute */
460 static const char * const types
[] = /* Supply types */
480 "fuser-cleaning-pad",
486 "glue-water-additive",
500 * Reset state information...
503 current_addr
= *addr
;
508 memset(supplies
, 0, sizeof(supplies
));
511 * See if we should be getting supply levels via SNMP...
514 if ((ppd
= ppdOpenFile(getenv("PPD"))) == NULL
||
515 ((ppdattr
= ppdFindAttr(ppd
, "cupsSNMPSupplies", NULL
)) != NULL
&&
516 ppdattr
->value
&& _cups_strcasecmp(ppdattr
->value
, "true")))
522 if ((ppdattr
= ppdFindAttr(ppd
, "cupsSNMPQuirks", NULL
)) != NULL
)
524 if (!_cups_strcasecmp(ppdattr
->value
, "capacity"))
525 quirks
|= CUPS_SNMP_CAPACITY
;
531 * Get the device description...
534 if (!_cupsSNMPWrite(snmp_fd
, addr
, CUPS_SNMP_VERSION_1
,
535 _cupsSNMPDefaultCommunity(), CUPS_ASN1_GET_REQUEST
, 1,
539 if (!_cupsSNMPRead(snmp_fd
, &packet
, CUPS_SUPPLY_TIMEOUT
) ||
540 packet
.object_type
!= CUPS_ASN1_OCTET_STRING
)
542 strlcpy(description
, "Unknown", sizeof(description
));
546 strlcpy(description
, (char *)packet
.object_value
.string
.bytes
,
547 sizeof(description
));
549 fprintf(stderr
, "DEBUG2: hrDeviceDesc=\"%s\"\n", description
);
552 * See if we have already queried this device...
555 httpAddrString(addr
, addrstr
, sizeof(addrstr
));
557 if ((cachedir
= getenv("CUPS_CACHEDIR")) == NULL
)
558 cachedir
= CUPS_CACHEDIR
;
560 snprintf(cachefilename
, sizeof(cachefilename
), "%s/%s.snmp", cachedir
,
563 if ((cachefile
= cupsFileOpen(cachefilename
, "r")) != NULL
)
566 * Yes, read the cache file:
568 * 3 num_supplies charset
570 * supply structures...
573 if (cupsFileGets(cachefile
, value
, sizeof(value
)))
575 if (sscanf(value
, "3 %d%d", &num_supplies
, &charset
) == 2 &&
576 num_supplies
<= CUPS_MAX_SUPPLIES
&&
577 cupsFileGets(cachefile
, value
, sizeof(value
)))
579 if (!strcmp(description
, value
))
580 cupsFileRead(cachefile
, (char *)supplies
,
581 (size_t)num_supplies
* sizeof(backend_supplies_t
));
595 cupsFileClose(cachefile
);
599 * If the cache information isn't correct, scan for supplies...
605 * Get the configured character set...
608 int oid
[CUPS_SNMP_MAX_OID
]; /* OID for character set */
611 if (!_cupsSNMPWrite(snmp_fd
, ¤t_addr
, CUPS_SNMP_VERSION_1
,
612 _cupsSNMPDefaultCommunity(), CUPS_ASN1_GET_REQUEST
, 1,
613 prtGeneralCurrentLocalization
))
616 if (!_cupsSNMPRead(snmp_fd
, &packet
, CUPS_SUPPLY_TIMEOUT
) ||
617 packet
.object_type
!= CUPS_ASN1_INTEGER
)
620 "DEBUG: prtGeneralCurrentLocalization type is %x, expected %x!\n",
621 packet
.object_type
, CUPS_ASN1_INTEGER
);
625 fprintf(stderr
, "DEBUG2: prtGeneralCurrentLocalization=%d\n",
626 packet
.object_value
.integer
);
628 _cupsSNMPCopyOID(oid
, prtLocalizationCharacterSet
, CUPS_SNMP_MAX_OID
);
629 oid
[prtLocalizationCharacterSetOffset
- 2] = packet
.object_value
.integer
;
632 if (!_cupsSNMPWrite(snmp_fd
, ¤t_addr
, CUPS_SNMP_VERSION_1
,
633 _cupsSNMPDefaultCommunity(), CUPS_ASN1_GET_REQUEST
, 1,
637 if (!_cupsSNMPRead(snmp_fd
, &packet
, CUPS_SUPPLY_TIMEOUT
) ||
638 packet
.object_type
!= CUPS_ASN1_INTEGER
)
641 "DEBUG: prtLocalizationCharacterSet type is %x, expected %x!\n",
642 packet
.object_type
, CUPS_ASN1_INTEGER
);
646 fprintf(stderr
, "DEBUG2: prtLocalizationCharacterSet=%d\n",
647 packet
.object_value
.integer
);
648 charset
= packet
.object_value
.integer
;
651 if (num_supplies
< 0)
654 * Walk the printer configuration information...
657 _cupsSNMPWalk(snmp_fd
, ¤t_addr
, CUPS_SNMP_VERSION_1
,
658 _cupsSNMPDefaultCommunity(), prtMarkerSuppliesEntry
,
659 CUPS_SUPPLY_TIMEOUT
, backend_walk_cb
, NULL
);
663 * Save the cached information...
666 if (num_supplies
< 0)
669 if ((cachefile
= cupsFileOpen(cachefilename
, "w")) != NULL
)
671 cupsFilePrintf(cachefile
, "3 %d %d\n", num_supplies
, charset
);
672 cupsFilePrintf(cachefile
, "%s\n", description
);
674 if (num_supplies
> 0)
675 cupsFileWrite(cachefile
, (char *)supplies
,
676 (size_t)num_supplies
* sizeof(backend_supplies_t
));
678 cupsFileClose(cachefile
);
681 if (num_supplies
<= 0)
688 for (i
= 0; i
< num_supplies
; i
++)
689 strlcpy(supplies
[i
].color
, "none", sizeof(supplies
[i
].color
));
691 _cupsSNMPWalk(snmp_fd
, ¤t_addr
, CUPS_SNMP_VERSION_1
,
692 _cupsSNMPDefaultCommunity(), prtMarkerColorantValue
,
693 CUPS_SUPPLY_TIMEOUT
, backend_walk_cb
, NULL
);
696 * Output the marker-colors attribute...
699 for (i
= 0, ptr
= value
; i
< num_supplies
; i
++, ptr
+= strlen(ptr
))
704 strlcpy(ptr
, supplies
[i
].color
, sizeof(value
) - (size_t)(ptr
- value
));
707 fprintf(stderr
, "ATTR: marker-colors=%s\n", value
);
710 * Output the marker-names attribute (the double quoting is necessary to deal
711 * with embedded quotes and commas in the marker names...)
714 for (i
= 0, ptr
= value
; i
< num_supplies
; i
++)
721 for (name_ptr
= supplies
[i
].name
; *name_ptr
;)
723 if (*name_ptr
== '\\' || *name_ptr
== '\"' || *name_ptr
== '\'')
730 *ptr
++ = *name_ptr
++;
738 fprintf(stderr
, "ATTR: marker-names=%s\n", value
);
741 * Output the marker-types attribute...
744 for (i
= 0, ptr
= value
; i
< num_supplies
; i
++, ptr
+= strlen(ptr
))
749 type
= supplies
[i
].type
;
751 if (type
< CUPS_TC_other
|| type
> CUPS_TC_covers
)
752 strlcpy(ptr
, "unknown", sizeof(value
) - (size_t)(ptr
- value
));
754 strlcpy(ptr
, types
[type
- CUPS_TC_other
], sizeof(value
) - (size_t)(ptr
- value
));
757 fprintf(stderr
, "ATTR: marker-types=%s\n", value
);
762 * 'backend_walk_cb()' - Interpret the supply value responses.
766 backend_walk_cb(cups_snmp_t
*packet
, /* I - SNMP packet */
767 void *data
) /* I - User data (unused) */
769 int i
, j
, k
; /* Looping vars */
770 static const char * const colors
[][2] =
771 { /* Standard color names */
772 { "black", "#000000" },
773 { "blue", "#0000FF" },
774 { "brown", "#A52A2A" },
775 { "cyan", "#00FFFF" },
776 { "dark-gray", "#404040" },
777 { "dark gray", "#404040" },
778 { "dark-yellow", "#FFCC00" },
779 { "dark yellow", "#FFCC00" },
780 { "gold", "#FFD700" },
781 { "gray", "#808080" },
782 { "green", "#00FF00" },
783 { "light-black", "#606060" },
784 { "light black", "#606060" },
785 { "light-cyan", "#E0FFFF" },
786 { "light cyan", "#E0FFFF" },
787 { "light-gray", "#D3D3D3" },
788 { "light gray", "#D3D3D3" },
789 { "light-magenta", "#FF77FF" },
790 { "light magenta", "#FF77FF" },
791 { "magenta", "#FF00FF" },
792 { "orange", "#FFA500" },
793 { "red", "#FF0000" },
794 { "silver", "#C0C0C0" },
795 { "white", "#FFFFFF" },
796 { "yellow", "#FFFF00" }
802 if (_cupsSNMPIsOIDPrefixed(packet
, prtMarkerColorantValue
) &&
803 packet
->object_type
== CUPS_ASN1_OCTET_STRING
)
809 i
= packet
->object_name
[prtMarkerColorantValueOffset
];
811 fprintf(stderr
, "DEBUG2: prtMarkerColorantValue.1.%d = \"%s\"\n", i
,
812 (char *)packet
->object_value
.string
.bytes
);
814 for (j
= 0; j
< num_supplies
; j
++)
815 if (supplies
[j
].colorant
== i
)
817 for (k
= 0; k
< (int)(sizeof(colors
) / sizeof(colors
[0])); k
++)
818 if (!_cups_strcasecmp(colors
[k
][0],
819 (char *)packet
->object_value
.string
.bytes
))
821 strlcpy(supplies
[j
].color
, colors
[k
][1], sizeof(supplies
[j
].color
));
826 else if (_cupsSNMPIsOIDPrefixed(packet
, prtMarkerSuppliesColorantIndex
))
829 * Get colorant index...
832 i
= packet
->object_name
[prtMarkerSuppliesColorantIndexOffset
];
833 if (i
< 1 || i
> CUPS_MAX_SUPPLIES
||
834 packet
->object_type
!= CUPS_ASN1_INTEGER
)
837 fprintf(stderr
, "DEBUG2: prtMarkerSuppliesColorantIndex.1.%d = %d\n", i
,
838 packet
->object_value
.integer
);
840 if (i
> num_supplies
)
843 supplies
[i
- 1].colorant
= packet
->object_value
.integer
;
845 else if (_cupsSNMPIsOIDPrefixed(packet
, prtMarkerSuppliesDescription
))
848 * Get supply name/description...
851 i
= packet
->object_name
[prtMarkerSuppliesDescriptionOffset
];
852 if (i
< 1 || i
> CUPS_MAX_SUPPLIES
||
853 packet
->object_type
!= CUPS_ASN1_OCTET_STRING
)
856 if (i
> num_supplies
)
861 case CUPS_TC_csASCII
:
862 case CUPS_TC_csUTF8
:
863 case CUPS_TC_csUnicodeASCII
:
864 strlcpy(supplies
[i
- 1].name
,
865 (char *)packet
->object_value
.string
.bytes
,
866 sizeof(supplies
[0].name
));
869 case CUPS_TC_csISOLatin1
:
870 case CUPS_TC_csUnicodeLatin1
:
871 cupsCharsetToUTF8((cups_utf8_t
*)supplies
[i
- 1].name
,
872 (char *)packet
->object_value
.string
.bytes
,
873 sizeof(supplies
[0].name
), CUPS_ISO8859_1
);
876 case CUPS_TC_csShiftJIS
:
877 case CUPS_TC_csWindows31J
: /* Close enough for our purposes */
878 cupsCharsetToUTF8((cups_utf8_t
*)supplies
[i
- 1].name
,
879 (char *)packet
->object_value
.string
.bytes
,
880 sizeof(supplies
[0].name
), CUPS_JIS_X0213
);
883 case CUPS_TC_csUCS4
:
884 case CUPS_TC_csUTF32
:
885 case CUPS_TC_csUTF32BE
:
886 case CUPS_TC_csUTF32LE
:
887 cupsUTF32ToUTF8((cups_utf8_t
*)supplies
[i
- 1].name
,
888 (cups_utf32_t
*)packet
->object_value
.string
.bytes
,
889 sizeof(supplies
[0].name
));
892 case CUPS_TC_csUnicode
:
893 case CUPS_TC_csUTF16BE
:
894 case CUPS_TC_csUTF16LE
:
895 utf16_to_utf8((cups_utf8_t
*)supplies
[i
- 1].name
,
896 packet
->object_value
.string
.bytes
,
897 packet
->object_value
.string
.num_bytes
,
898 sizeof(supplies
[0].name
), charset
== CUPS_TC_csUTF16LE
);
903 * If we get here, the printer is using an unknown character set and
904 * we just want to copy characters that look like ASCII...
908 char *src
, *dst
; /* Pointers into strings */
911 * Loop safe because both the object_value and supplies char arrays
912 * are CUPS_SNMP_MAX_STRING elements long.
915 for (src
= (char *)packet
->object_value
.string
.bytes
,
916 dst
= supplies
[i
- 1].name
;
920 if ((*src
& 0x80) || *src
< ' ' || *src
== 0x7f)
931 fprintf(stderr
, "DEBUG2: prtMarkerSuppliesDescription.1.%d = \"%s\"\n", i
,
932 supplies
[i
- 1].name
);
935 else if (_cupsSNMPIsOIDPrefixed(packet
, prtMarkerSuppliesLevel
))
941 i
= packet
->object_name
[prtMarkerSuppliesLevelOffset
];
942 if (i
< 1 || i
> CUPS_MAX_SUPPLIES
||
943 packet
->object_type
!= CUPS_ASN1_INTEGER
)
946 fprintf(stderr
, "DEBUG2: prtMarkerSuppliesLevel.1.%d = %d\n", i
,
947 packet
->object_value
.integer
);
949 if (i
> num_supplies
)
952 supplies
[i
- 1].level
= packet
->object_value
.integer
;
954 else if (_cupsSNMPIsOIDPrefixed(packet
, prtMarkerSuppliesMaxCapacity
) &&
955 !(quirks
& CUPS_SNMP_CAPACITY
))
958 * Get max capacity...
961 i
= packet
->object_name
[prtMarkerSuppliesMaxCapacityOffset
];
962 if (i
< 1 || i
> CUPS_MAX_SUPPLIES
||
963 packet
->object_type
!= CUPS_ASN1_INTEGER
)
966 fprintf(stderr
, "DEBUG2: prtMarkerSuppliesMaxCapacity.1.%d = %d\n", i
,
967 packet
->object_value
.integer
);
969 if (i
> num_supplies
)
972 if (supplies
[i
- 1].max_capacity
== 0 &&
973 packet
->object_value
.integer
> 0)
974 supplies
[i
- 1].max_capacity
= packet
->object_value
.integer
;
976 else if (_cupsSNMPIsOIDPrefixed(packet
, prtMarkerSuppliesClass
))
979 * Get marker class...
982 i
= packet
->object_name
[prtMarkerSuppliesClassOffset
];
983 if (i
< 1 || i
> CUPS_MAX_SUPPLIES
||
984 packet
->object_type
!= CUPS_ASN1_INTEGER
)
987 fprintf(stderr
, "DEBUG2: prtMarkerSuppliesClass.1.%d = %d\n", i
,
988 packet
->object_value
.integer
);
990 if (i
> num_supplies
)
993 supplies
[i
- 1].sclass
= packet
->object_value
.integer
;
995 else if (_cupsSNMPIsOIDPrefixed(packet
, prtMarkerSuppliesType
))
1001 i
= packet
->object_name
[prtMarkerSuppliesTypeOffset
];
1002 if (i
< 1 || i
> CUPS_MAX_SUPPLIES
||
1003 packet
->object_type
!= CUPS_ASN1_INTEGER
)
1006 fprintf(stderr
, "DEBUG2: prtMarkerSuppliesType.1.%d = %d\n", i
,
1007 packet
->object_value
.integer
);
1009 if (i
> num_supplies
)
1012 supplies
[i
- 1].type
= packet
->object_value
.integer
;
1014 else if (_cupsSNMPIsOIDPrefixed(packet
, prtMarkerSuppliesSupplyUnit
))
1017 * Get units for capacity...
1020 i
= packet
->object_name
[prtMarkerSuppliesSupplyUnitOffset
];
1021 if (i
< 1 || i
> CUPS_MAX_SUPPLIES
||
1022 packet
->object_type
!= CUPS_ASN1_INTEGER
)
1025 fprintf(stderr
, "DEBUG2: prtMarkerSuppliesSupplyUnit.1.%d = %d\n", i
,
1026 packet
->object_value
.integer
);
1028 if (i
> num_supplies
)
1031 if (packet
->object_value
.integer
== CUPS_TC_percent
)
1032 supplies
[i
- 1].max_capacity
= 100;
1038 * 'utf16_to_utf8()' - Convert UTF-16 text to UTF-8.
1043 cups_utf8_t
*dst
, /* I - Destination buffer */
1044 const unsigned char *src
, /* I - Source string */
1045 size_t srcsize
, /* I - Size of source string */
1046 size_t dstsize
, /* I - Size of destination buffer */
1047 int le
) /* I - Source is little-endian? */
1049 cups_utf32_t ch
, /* Current character */
1050 temp
[CUPS_SNMP_MAX_STRING
],
1052 *ptr
; /* Pointer into UTF-32 string */
1055 for (ptr
= temp
; srcsize
>= 2;)
1058 ch
= (cups_utf32_t
)(src
[0] | (src
[1] << 8));
1060 ch
= (cups_utf32_t
)((src
[0] << 8) | src
[1]);
1065 if (ch
>= 0xd800 && ch
<= 0xdbff && srcsize
>= 2)
1068 * Multi-word UTF-16 char...
1071 cups_utf32_t lch
; /* Lower word */
1075 lch
= (cups_utf32_t
)(src
[0] | (src
[1] << 8));
1077 lch
= (cups_utf32_t
)((src
[0] << 8) | src
[1]);
1079 if (lch
>= 0xdc00 && lch
<= 0xdfff)
1084 ch
= (((ch
& 0x3ff) << 10) | (lch
& 0x3ff)) + 0x10000;
1088 if (ptr
< (temp
+ CUPS_SNMP_MAX_STRING
- 1))
1094 cupsUTF32ToUTF8(dst
, temp
, (int)dstsize
);