4 * SNMP supplies functions for CUPS.
6 * Copyright 2008-2012 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 * backendSNMPSupplies() - Get the current supplies for a device.
19 * backend_init_supplies() - Initialize the supplies list.
20 * backend_walk_cb() - Interpret the supply value responses.
21 * utf16_to_utf8() - Convert UTF-16 text to UTF-8.
25 * Include necessary headers.
28 #include "backend-private.h"
29 #include <cups/array.h>
36 #define CUPS_MAX_SUPPLIES 32 /* Maximum number of supplies for a printer */
37 #define CUPS_SUPPLY_TIMEOUT 2.0 /* Timeout for SNMP lookups */
39 #define CUPS_DEVELOPER_LOW 0x0001
40 #define CUPS_DEVELOPER_EMPTY 0x0002
41 #define CUPS_MARKER_SUPPLY_LOW 0x0004
42 #define CUPS_MARKER_SUPPLY_EMPTY 0x0008
43 #define CUPS_OPC_NEAR_EOL 0x0010
44 #define CUPS_OPC_LIFE_OVER 0x0020
45 #define CUPS_TONER_LOW 0x0040
46 #define CUPS_TONER_EMPTY 0x0080
47 #define CUPS_WASTE_ALMOST_FULL 0x0100
48 #define CUPS_WASTE_FULL 0x0200
49 #define CUPS_CLEANER_NEAR_EOL 0x0400 /* Proposed JPS3 */
50 #define CUPS_CLEANER_LIFE_OVER 0x0800 /* Proposed JPS3 */
52 #define CUPS_SNMP_NONE 0x0000
53 #define CUPS_SNMP_CAPACITY 0x0001 /* Supply levels reported as percentages */
60 typedef struct /**** Printer supply data ****/
62 char name
[CUPS_SNMP_MAX_STRING
], /* Name of supply */
63 color
[8]; /* Color: "#RRGGBB" or "none" */
64 int colorant
, /* Colorant index */
65 type
, /* Supply type */
66 max_capacity
, /* Maximum capacity */
67 level
; /* Current level value */
70 typedef struct /**** Printer state table ****/
72 int bit
; /* State bit */
73 const char *keyword
; /* IPP printer-state-reasons keyword */
81 static http_addr_t current_addr
; /* Current address */
82 static int current_state
= -1;
83 /* Current device state bits */
84 static int charset
= -1; /* Character set for supply names */
85 static unsigned quirks
= CUPS_SNMP_NONE
;
86 /* Quirks we have to work around */
87 static int num_supplies
= 0;
88 /* Number of supplies found */
89 static backend_supplies_t supplies
[CUPS_MAX_SUPPLIES
];
90 /* Supply information */
91 static int supply_state
= -1;
92 /* Supply state info */
94 static const int hrDeviceDescr
[] =
95 { CUPS_OID_hrDeviceDescr
, 1, -1 };
96 /* Device description OID */
97 static const int hrPrinterStatus
[] =
98 { CUPS_OID_hrPrinterStatus
, 1, -1 };
99 /* Current state OID */
100 static const int hrPrinterDetectedErrorState
[] =
101 { CUPS_OID_hrPrinterDetectedErrorState
, 1, -1 };
102 /* Current printer state bits OID */
103 static const int prtGeneralCurrentLocalization
[] =
104 { CUPS_OID_prtGeneralCurrentLocalization
, 1, -1 };
105 static const int prtLocalizationCharacterSet
[] =
106 { CUPS_OID_prtLocalizationCharacterSet
, 1, 1, -1 },
107 prtLocalizationCharacterSetOffset
=
108 (sizeof(prtLocalizationCharacterSet
) /
109 sizeof(prtLocalizationCharacterSet
[0]));
110 static const int prtMarkerColorantValue
[] =
111 { CUPS_OID_prtMarkerColorantValue
, -1 },
113 prtMarkerColorantValueOffset
=
114 (sizeof(prtMarkerColorantValue
) /
115 sizeof(prtMarkerColorantValue
[0]));
116 /* Offset to colorant index */
117 static const int prtMarkerLifeCount
[] =
118 { CUPS_OID_prtMarkerLifeCount
, 1, 1, -1 };
119 /* Page counter OID */
120 static const int prtMarkerSuppliesEntry
[] =
121 { CUPS_OID_prtMarkerSuppliesEntry
, -1 };
123 static const int prtMarkerSuppliesColorantIndex
[] =
124 { CUPS_OID_prtMarkerSuppliesColorantIndex
, -1 },
125 /* Colorant index OID */
126 prtMarkerSuppliesColorantIndexOffset
=
127 (sizeof(prtMarkerSuppliesColorantIndex
) /
128 sizeof(prtMarkerSuppliesColorantIndex
[0]));
129 /* Offset to supply index */
130 static const int prtMarkerSuppliesDescription
[] =
131 { CUPS_OID_prtMarkerSuppliesDescription
, -1 },
132 /* Description OID */
133 prtMarkerSuppliesDescriptionOffset
=
134 (sizeof(prtMarkerSuppliesDescription
) /
135 sizeof(prtMarkerSuppliesDescription
[0]));
136 /* Offset to supply index */
137 static const int prtMarkerSuppliesLevel
[] =
138 { CUPS_OID_prtMarkerSuppliesLevel
, -1 },
140 prtMarkerSuppliesLevelOffset
=
141 (sizeof(prtMarkerSuppliesLevel
) /
142 sizeof(prtMarkerSuppliesLevel
[0]));
143 /* Offset to supply index */
144 static const int prtMarkerSuppliesMaxCapacity
[] =
145 { CUPS_OID_prtMarkerSuppliesMaxCapacity
, -1 },
146 /* Max capacity OID */
147 prtMarkerSuppliesMaxCapacityOffset
=
148 (sizeof(prtMarkerSuppliesMaxCapacity
) /
149 sizeof(prtMarkerSuppliesMaxCapacity
[0]));
150 /* Offset to supply index */
151 static const int prtMarkerSuppliesType
[] =
152 { CUPS_OID_prtMarkerSuppliesType
, -1 },
154 prtMarkerSuppliesTypeOffset
=
155 (sizeof(prtMarkerSuppliesType
) /
156 sizeof(prtMarkerSuppliesType
[0]));
157 /* Offset to supply index */
158 static const int prtMarkerSuppliesSupplyUnit
[] =
159 { CUPS_OID_prtMarkerSuppliesSupplyUnit
, -1 },
161 prtMarkerSuppliesSupplyUnitOffset
=
162 (sizeof(prtMarkerSuppliesSupplyUnit
) /
163 sizeof(prtMarkerSuppliesSupplyUnit
[0]));
164 /* Offset to supply index */
166 static const backend_state_t printer_states
[] =
168 /* { CUPS_TC_lowPaper, "media-low-report" }, */
169 { CUPS_TC_noPaper
| CUPS_TC_inputTrayEmpty
, "media-empty-warning" },
170 /* { CUPS_TC_lowToner, "toner-low-report" }, */ /* now use prtMarkerSupplies */
171 /* { CUPS_TC_noToner, "toner-empty-warning" }, */ /* now use prtMarkerSupplies */
172 { CUPS_TC_doorOpen
, "door-open-report" },
173 { CUPS_TC_jammed
, "media-jam-warning" },
174 /* { CUPS_TC_offline, "offline-report" }, */ /* unreliable */
175 /* { CUPS_TC_serviceRequested | CUPS_TC_overduePreventMaint, "service-needed-warning" }, */ /* unreliable */
176 { CUPS_TC_inputTrayMissing
, "input-tray-missing-warning" },
177 { CUPS_TC_outputTrayMissing
, "output-tray-missing-warning" },
178 { CUPS_TC_markerSupplyMissing
, "marker-supply-missing-warning" },
179 { CUPS_TC_outputNearFull
, "output-area-almost-full-report" },
180 { CUPS_TC_outputFull
, "output-area-full-warning" }
183 static const backend_state_t supply_states
[] =
185 { CUPS_DEVELOPER_LOW
, "developer-low-report" },
186 { CUPS_DEVELOPER_EMPTY
, "developer-empty-warning" },
187 { CUPS_MARKER_SUPPLY_LOW
, "marker-supply-low-report" },
188 { CUPS_MARKER_SUPPLY_EMPTY
, "marker-supply-empty-warning" },
189 { CUPS_OPC_NEAR_EOL
, "opc-near-eol-report" },
190 { CUPS_OPC_LIFE_OVER
, "opc-life-over-warning" },
191 { CUPS_TONER_LOW
, "toner-low-report" },
192 { CUPS_TONER_EMPTY
, "toner-empty-warning" },
193 { CUPS_WASTE_ALMOST_FULL
, "waste-receptacle-almost-full-report" },
194 { CUPS_WASTE_FULL
, "waste-receptacle-full-warning" },
195 { CUPS_CLEANER_NEAR_EOL
, "cleaner-life-almost-over-report" },
196 { CUPS_CLEANER_LIFE_OVER
, "cleaner-life-over-warning" },
204 static void backend_init_supplies(int snmp_fd
, http_addr_t
*addr
);
205 static void backend_walk_cb(cups_snmp_t
*packet
, void *data
);
206 static void utf16_to_utf8(cups_utf8_t
*dst
, const unsigned char *src
,
207 size_t srcsize
, size_t dstsize
, int le
);
211 * 'backendSNMPSupplies()' - Get the current supplies for a device.
214 int /* O - 0 on success, -1 on error */
216 int snmp_fd
, /* I - SNMP socket */
217 http_addr_t
*addr
, /* I - Printer address */
218 int *page_count
, /* O - Page count */
219 int *printer_state
) /* O - Printer state */
221 if (!httpAddrEqual(addr
, ¤t_addr
))
222 backend_init_supplies(snmp_fd
, addr
);
223 else if (num_supplies
> 0)
224 _cupsSNMPWalk(snmp_fd
, ¤t_addr
, CUPS_SNMP_VERSION_1
,
225 _cupsSNMPDefaultCommunity(), prtMarkerSuppliesLevel
,
226 CUPS_SUPPLY_TIMEOUT
, backend_walk_cb
, NULL
);
234 if (num_supplies
> 0)
236 int i
, /* Looping var */
237 percent
, /* Percent full */
238 new_state
, /* New state value */
239 change_state
, /* State change */
240 new_supply_state
= 0; /* Supply state */
241 char value
[CUPS_MAX_SUPPLIES
* 4],
242 /* marker-levels value string */
243 *ptr
; /* Pointer into value string */
244 cups_snmp_t packet
; /* SNMP response packet */
247 * Generate the marker-levels value string...
250 for (i
= 0, ptr
= value
; i
< num_supplies
; i
++, ptr
+= strlen(ptr
))
252 if (supplies
[i
].max_capacity
> 0 && supplies
[i
].level
>= 0)
253 percent
= 100 * supplies
[i
].level
/ supplies
[i
].max_capacity
;
254 else if (supplies
[i
].level
>= 0 && supplies
[i
].level
<= 100 &&
255 (quirks
& CUPS_SNMP_CAPACITY
))
256 percent
= supplies
[i
].level
;
262 switch (supplies
[i
].type
)
265 case CUPS_TC_tonerCartridge
:
267 new_supply_state
|= CUPS_TONER_EMPTY
;
269 new_supply_state
|= CUPS_TONER_LOW
;
272 case CUPS_TC_inkCartridge
:
273 case CUPS_TC_inkRibbon
:
274 case CUPS_TC_solidWax
:
275 case CUPS_TC_ribbonWax
:
277 new_supply_state
|= CUPS_MARKER_SUPPLY_EMPTY
;
279 new_supply_state
|= CUPS_MARKER_SUPPLY_LOW
;
281 case CUPS_TC_developer
:
283 new_supply_state
|= CUPS_DEVELOPER_EMPTY
;
285 new_supply_state
|= CUPS_DEVELOPER_LOW
;
287 case CUPS_TC_coronaWire
:
290 case CUPS_TC_transferUnit
:
292 new_supply_state
|= CUPS_OPC_LIFE_OVER
;
294 new_supply_state
|= CUPS_OPC_NEAR_EOL
;
296 case CUPS_TC_wasteInk
:
297 case CUPS_TC_wastePaper
:
298 case CUPS_TC_wasteToner
:
299 case CUPS_TC_wasteWater
:
300 case CUPS_TC_wasteWax
:
302 new_supply_state
|= CUPS_WASTE_FULL
;
304 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
) - (ptr
- value
), "%d", percent
);
323 strlcpy(ptr
, "-1", sizeof(value
) - (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 cups_file_t
*cachefile
; /* Cache file */
444 const char *cachedir
; /* CUPS_CACHEDIR value */
445 char addrstr
[1024], /* Address string */
446 cachefilename
[1024], /* Cache filename */
447 description
[CUPS_SNMP_MAX_STRING
],
448 /* Device description string */
449 value
[CUPS_MAX_SUPPLIES
* (CUPS_SNMP_MAX_STRING
* 4 + 3)],
451 *ptr
, /* Pointer into value string */
452 *name_ptr
; /* Pointer into name string */
453 cups_snmp_t packet
; /* SNMP response packet */
454 ppd_file_t
*ppd
; /* PPD file for this queue */
455 ppd_attr_t
*ppdattr
; /* cupsSNMPSupplies attribute */
456 static const char * const types
[] = /* Supply types */
476 "fuser-cleaning-pad",
482 "glue-water-additive",
496 * Reset state information...
499 current_addr
= *addr
;
504 memset(supplies
, 0, sizeof(supplies
));
507 * See if we should be getting supply levels via SNMP...
510 if ((ppd
= ppdOpenFile(getenv("PPD"))) == NULL
||
511 ((ppdattr
= ppdFindAttr(ppd
, "cupsSNMPSupplies", NULL
)) != NULL
&&
512 ppdattr
->value
&& _cups_strcasecmp(ppdattr
->value
, "true")))
518 if ((ppdattr
= ppdFindAttr(ppd
, "cupsSNMPQuirks", NULL
)) != NULL
)
520 if (!_cups_strcasecmp(ppdattr
->value
, "capacity"))
521 quirks
|= CUPS_SNMP_CAPACITY
;
527 * Get the device description...
530 if (!_cupsSNMPWrite(snmp_fd
, addr
, CUPS_SNMP_VERSION_1
,
531 _cupsSNMPDefaultCommunity(), CUPS_ASN1_GET_REQUEST
, 1,
535 if (!_cupsSNMPRead(snmp_fd
, &packet
, CUPS_SUPPLY_TIMEOUT
) ||
536 packet
.object_type
!= CUPS_ASN1_OCTET_STRING
)
538 strlcpy(description
, "Unknown", sizeof(description
));
542 strlcpy(description
, (char *)packet
.object_value
.string
.bytes
,
543 sizeof(description
));
545 fprintf(stderr
, "DEBUG2: hrDeviceDesc=\"%s\"\n", description
);
548 * See if we have already queried this device...
551 httpAddrString(addr
, addrstr
, sizeof(addrstr
));
553 if ((cachedir
= getenv("CUPS_CACHEDIR")) == NULL
)
554 cachedir
= CUPS_CACHEDIR
;
556 snprintf(cachefilename
, sizeof(cachefilename
), "%s/%s.snmp", cachedir
,
559 if ((cachefile
= cupsFileOpen(cachefilename
, "r")) != NULL
)
562 * Yes, read the cache file:
564 * 2 num_supplies charset
566 * supply structures...
569 if (cupsFileGets(cachefile
, value
, sizeof(value
)))
571 if (sscanf(value
, "2 %d%d", &num_supplies
, &charset
) == 2 &&
572 num_supplies
<= CUPS_MAX_SUPPLIES
&&
573 cupsFileGets(cachefile
, value
, sizeof(value
)))
575 if (!strcmp(description
, value
))
576 cupsFileRead(cachefile
, (char *)supplies
,
577 num_supplies
* sizeof(backend_supplies_t
));
591 cupsFileClose(cachefile
);
595 * If the cache information isn't correct, scan for supplies...
601 * Get the configured character set...
604 int oid
[CUPS_SNMP_MAX_OID
]; /* OID for character set */
607 if (!_cupsSNMPWrite(snmp_fd
, ¤t_addr
, CUPS_SNMP_VERSION_1
,
608 _cupsSNMPDefaultCommunity(), CUPS_ASN1_GET_REQUEST
, 1,
609 prtGeneralCurrentLocalization
))
612 if (!_cupsSNMPRead(snmp_fd
, &packet
, CUPS_SUPPLY_TIMEOUT
) ||
613 packet
.object_type
!= CUPS_ASN1_INTEGER
)
616 "DEBUG: prtGeneralCurrentLocalization type is %x, expected %x!\n",
617 packet
.object_type
, CUPS_ASN1_INTEGER
);
621 fprintf(stderr
, "DEBUG2: prtGeneralCurrentLocalization=%d\n",
622 packet
.object_value
.integer
);
624 _cupsSNMPCopyOID(oid
, prtLocalizationCharacterSet
, CUPS_SNMP_MAX_OID
);
625 oid
[prtLocalizationCharacterSetOffset
- 2] = packet
.object_value
.integer
;
628 if (!_cupsSNMPWrite(snmp_fd
, ¤t_addr
, CUPS_SNMP_VERSION_1
,
629 _cupsSNMPDefaultCommunity(), CUPS_ASN1_GET_REQUEST
, 1,
633 if (!_cupsSNMPRead(snmp_fd
, &packet
, CUPS_SUPPLY_TIMEOUT
) ||
634 packet
.object_type
!= CUPS_ASN1_INTEGER
)
637 "DEBUG: prtLocalizationCharacterSet type is %x, expected %x!\n",
638 packet
.object_type
, CUPS_ASN1_INTEGER
);
642 fprintf(stderr
, "DEBUG2: prtLocalizationCharacterSet=%d\n",
643 packet
.object_value
.integer
);
644 charset
= packet
.object_value
.integer
;
647 if (num_supplies
< 0)
650 * Walk the printer configuration information...
653 _cupsSNMPWalk(snmp_fd
, ¤t_addr
, CUPS_SNMP_VERSION_1
,
654 _cupsSNMPDefaultCommunity(), prtMarkerSuppliesEntry
,
655 CUPS_SUPPLY_TIMEOUT
, backend_walk_cb
, NULL
);
659 * Save the cached information...
662 if (num_supplies
< 0)
665 if ((cachefile
= cupsFileOpen(cachefilename
, "w")) != NULL
)
667 cupsFilePrintf(cachefile
, "2 %d %d\n", num_supplies
, charset
);
668 cupsFilePrintf(cachefile
, "%s\n", description
);
670 if (num_supplies
> 0)
671 cupsFileWrite(cachefile
, (char *)supplies
,
672 num_supplies
* sizeof(backend_supplies_t
));
674 cupsFileClose(cachefile
);
677 if (num_supplies
<= 0)
684 for (i
= 0; i
< num_supplies
; i
++)
685 strlcpy(supplies
[i
].color
, "none", sizeof(supplies
[i
].color
));
687 _cupsSNMPWalk(snmp_fd
, ¤t_addr
, CUPS_SNMP_VERSION_1
,
688 _cupsSNMPDefaultCommunity(), prtMarkerColorantValue
,
689 CUPS_SUPPLY_TIMEOUT
, backend_walk_cb
, NULL
);
692 * Output the marker-colors attribute...
695 for (i
= 0, ptr
= value
; i
< num_supplies
; i
++, ptr
+= strlen(ptr
))
700 strlcpy(ptr
, supplies
[i
].color
, sizeof(value
) - (ptr
- value
));
703 fprintf(stderr
, "ATTR: marker-colors=%s\n", value
);
706 * Output the marker-names attribute (the double quoting is necessary to deal
707 * with embedded quotes and commas in the marker names...)
710 for (i
= 0, ptr
= value
; i
< num_supplies
; i
++)
717 for (name_ptr
= supplies
[i
].name
; *name_ptr
;)
719 if (*name_ptr
== '\\' || *name_ptr
== '\"' || *name_ptr
== '\'')
726 *ptr
++ = *name_ptr
++;
734 fprintf(stderr
, "ATTR: marker-names=%s\n", value
);
737 * Output the marker-types attribute...
740 for (i
= 0, ptr
= value
; i
< num_supplies
; i
++, ptr
+= strlen(ptr
))
745 type
= supplies
[i
].type
;
747 if (type
< CUPS_TC_other
|| type
> CUPS_TC_covers
)
748 strlcpy(ptr
, "unknown", sizeof(value
) - (ptr
- value
));
750 strlcpy(ptr
, types
[type
- CUPS_TC_other
], sizeof(value
) - (ptr
- value
));
753 fprintf(stderr
, "ATTR: marker-types=%s\n", value
);
758 * 'backend_walk_cb()' - Interpret the supply value responses.
762 backend_walk_cb(cups_snmp_t
*packet
, /* I - SNMP packet */
763 void *data
) /* I - User data (unused) */
765 int i
, j
, k
; /* Looping vars */
766 static const char * const colors
[][2] =
767 { /* Standard color names */
768 { "black", "#000000" },
769 { "blue", "#0000FF" },
770 { "brown", "#A52A2A" },
771 { "cyan", "#00FFFF" },
772 { "dark-gray", "#404040" },
773 { "dark gray", "#404040" },
774 { "dark-yellow", "#FFCC00" },
775 { "dark yellow", "#FFCC00" },
776 { "gold", "#FFD700" },
777 { "gray", "#808080" },
778 { "green", "#00FF00" },
779 { "light-black", "#606060" },
780 { "light black", "#606060" },
781 { "light-cyan", "#E0FFFF" },
782 { "light cyan", "#E0FFFF" },
783 { "light-gray", "#D3D3D3" },
784 { "light gray", "#D3D3D3" },
785 { "light-magenta", "#FF77FF" },
786 { "light magenta", "#FF77FF" },
787 { "magenta", "#FF00FF" },
788 { "orange", "#FFA500" },
789 { "red", "#FF0000" },
790 { "silver", "#C0C0C0" },
791 { "white", "#FFFFFF" },
792 { "yellow", "#FFFF00" }
798 if (_cupsSNMPIsOIDPrefixed(packet
, prtMarkerColorantValue
) &&
799 packet
->object_type
== CUPS_ASN1_OCTET_STRING
)
805 i
= packet
->object_name
[prtMarkerColorantValueOffset
];
807 fprintf(stderr
, "DEBUG2: prtMarkerColorantValue.1.%d = \"%s\"\n", i
,
808 (char *)packet
->object_value
.string
.bytes
);
810 for (j
= 0; j
< num_supplies
; j
++)
811 if (supplies
[j
].colorant
== i
)
813 for (k
= 0; k
< (int)(sizeof(colors
) / sizeof(colors
[0])); k
++)
814 if (!_cups_strcasecmp(colors
[k
][0],
815 (char *)packet
->object_value
.string
.bytes
))
817 strlcpy(supplies
[j
].color
, colors
[k
][1], sizeof(supplies
[j
].color
));
822 else if (_cupsSNMPIsOIDPrefixed(packet
, prtMarkerSuppliesColorantIndex
))
825 * Get colorant index...
828 i
= packet
->object_name
[prtMarkerSuppliesColorantIndexOffset
];
829 if (i
< 1 || i
> CUPS_MAX_SUPPLIES
||
830 packet
->object_type
!= CUPS_ASN1_INTEGER
)
833 fprintf(stderr
, "DEBUG2: prtMarkerSuppliesColorantIndex.1.%d = %d\n", i
,
834 packet
->object_value
.integer
);
836 if (i
> num_supplies
)
839 supplies
[i
- 1].colorant
= packet
->object_value
.integer
;
841 else if (_cupsSNMPIsOIDPrefixed(packet
, prtMarkerSuppliesDescription
))
844 * Get supply name/description...
847 i
= packet
->object_name
[prtMarkerSuppliesDescriptionOffset
];
848 if (i
< 1 || i
> CUPS_MAX_SUPPLIES
||
849 packet
->object_type
!= CUPS_ASN1_OCTET_STRING
)
852 if (i
> num_supplies
)
857 case CUPS_TC_csASCII
:
858 case CUPS_TC_csUTF8
:
859 case CUPS_TC_csUnicodeASCII
:
860 strlcpy(supplies
[i
- 1].name
,
861 (char *)packet
->object_value
.string
.bytes
,
862 sizeof(supplies
[0].name
));
865 case CUPS_TC_csISOLatin1
:
866 case CUPS_TC_csUnicodeLatin1
:
867 cupsCharsetToUTF8((cups_utf8_t
*)supplies
[i
- 1].name
,
868 (char *)packet
->object_value
.string
.bytes
,
869 sizeof(supplies
[0].name
), CUPS_ISO8859_1
);
872 case CUPS_TC_csShiftJIS
:
873 case CUPS_TC_csWindows31J
: /* Close enough for our purposes */
874 cupsCharsetToUTF8((cups_utf8_t
*)supplies
[i
- 1].name
,
875 (char *)packet
->object_value
.string
.bytes
,
876 sizeof(supplies
[0].name
), CUPS_JIS_X0213
);
879 case CUPS_TC_csUCS4
:
880 case CUPS_TC_csUTF32
:
881 case CUPS_TC_csUTF32BE
:
882 case CUPS_TC_csUTF32LE
:
883 cupsUTF32ToUTF8((cups_utf8_t
*)supplies
[i
- 1].name
,
884 (cups_utf32_t
*)packet
->object_value
.string
.bytes
,
885 sizeof(supplies
[0].name
));
888 case CUPS_TC_csUnicode
:
889 case CUPS_TC_csUTF16BE
:
890 case CUPS_TC_csUTF16LE
:
891 utf16_to_utf8((cups_utf8_t
*)supplies
[i
- 1].name
,
892 packet
->object_value
.string
.bytes
,
893 packet
->object_value
.string
.num_bytes
,
894 sizeof(supplies
[0].name
), charset
== CUPS_TC_csUTF16LE
);
899 * If we get here, the printer is using an unknown character set and
900 * we just want to copy characters that look like ASCII...
904 char *src
, *dst
; /* Pointers into strings */
907 * Loop safe because both the object_value and supplies char arrays
908 * are CUPS_SNMP_MAX_STRING elements long.
911 for (src
= (char *)packet
->object_value
.string
.bytes
,
912 dst
= supplies
[i
- 1].name
;
916 if ((*src
& 0x80) || *src
< ' ' || *src
== 0x7f)
927 fprintf(stderr
, "DEBUG2: prtMarkerSuppliesDescription.1.%d = \"%s\"\n", i
,
928 supplies
[i
- 1].name
);
931 else if (_cupsSNMPIsOIDPrefixed(packet
, prtMarkerSuppliesLevel
))
937 i
= packet
->object_name
[prtMarkerSuppliesLevelOffset
];
938 if (i
< 1 || i
> CUPS_MAX_SUPPLIES
||
939 packet
->object_type
!= CUPS_ASN1_INTEGER
)
942 fprintf(stderr
, "DEBUG2: prtMarkerSuppliesLevel.1.%d = %d\n", i
,
943 packet
->object_value
.integer
);
945 if (i
> num_supplies
)
948 supplies
[i
- 1].level
= packet
->object_value
.integer
;
950 else if (_cupsSNMPIsOIDPrefixed(packet
, prtMarkerSuppliesMaxCapacity
) &&
951 !(quirks
& CUPS_SNMP_CAPACITY
))
954 * Get max capacity...
957 i
= packet
->object_name
[prtMarkerSuppliesMaxCapacityOffset
];
958 if (i
< 1 || i
> CUPS_MAX_SUPPLIES
||
959 packet
->object_type
!= CUPS_ASN1_INTEGER
)
962 fprintf(stderr
, "DEBUG2: prtMarkerSuppliesMaxCapacity.1.%d = %d\n", i
,
963 packet
->object_value
.integer
);
965 if (i
> num_supplies
)
968 if (supplies
[i
- 1].max_capacity
== 0 &&
969 packet
->object_value
.integer
> 0)
970 supplies
[i
- 1].max_capacity
= packet
->object_value
.integer
;
972 else if (_cupsSNMPIsOIDPrefixed(packet
, prtMarkerSuppliesType
))
978 i
= packet
->object_name
[prtMarkerSuppliesTypeOffset
];
979 if (i
< 1 || i
> CUPS_MAX_SUPPLIES
||
980 packet
->object_type
!= CUPS_ASN1_INTEGER
)
983 fprintf(stderr
, "DEBUG2: prtMarkerSuppliesType.1.%d = %d\n", i
,
984 packet
->object_value
.integer
);
986 if (i
> num_supplies
)
989 supplies
[i
- 1].type
= packet
->object_value
.integer
;
991 else if (_cupsSNMPIsOIDPrefixed(packet
, prtMarkerSuppliesSupplyUnit
))
994 * Get units for capacity...
997 i
= packet
->object_name
[prtMarkerSuppliesSupplyUnitOffset
];
998 if (i
< 1 || i
> CUPS_MAX_SUPPLIES
||
999 packet
->object_type
!= CUPS_ASN1_INTEGER
)
1002 fprintf(stderr
, "DEBUG2: prtMarkerSuppliesSupplyUnit.1.%d = %d\n", i
,
1003 packet
->object_value
.integer
);
1005 if (i
> num_supplies
)
1008 if (packet
->object_value
.integer
== CUPS_TC_percent
)
1009 supplies
[i
- 1].max_capacity
= 100;
1015 * 'utf16_to_utf8()' - Convert UTF-16 text to UTF-8.
1020 cups_utf8_t
*dst
, /* I - Destination buffer */
1021 const unsigned char *src
, /* I - Source string */
1022 size_t srcsize
, /* I - Size of source string */
1023 size_t dstsize
, /* I - Size of destination buffer */
1024 int le
) /* I - Source is little-endian? */
1026 cups_utf32_t ch
, /* Current character */
1027 temp
[CUPS_SNMP_MAX_STRING
],
1029 *ptr
; /* Pointer into UTF-32 string */
1032 for (ptr
= temp
; srcsize
>= 2;)
1035 ch
= src
[0] | (src
[1] << 8);
1037 ch
= (src
[0] << 8) | src
[1];
1042 if (ch
>= 0xd800 && ch
<= 0xdbff && srcsize
>= 2)
1045 * Multi-word UTF-16 char...
1048 int lch
; /* Lower word */
1052 lch
= src
[0] | (src
[1] << 8);
1054 lch
= (src
[0] << 8) | src
[1];
1056 if (lch
>= 0xdc00 && lch
<= 0xdfff)
1061 ch
= (((ch
& 0x3ff) << 10) | (lch
& 0x3ff)) + 0x10000;
1065 if (ptr
< (temp
+ CUPS_SNMP_MAX_STRING
- 1))
1071 cupsUTF32ToUTF8(dst
, temp
, dstsize
);