4 * SNMP supplies functions for CUPS.
6 * Copyright 2008-2013 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 sclass
, /* Supply class */
66 type
, /* Supply type */
67 max_capacity
, /* Maximum capacity */
68 level
; /* Current level value */
71 typedef struct /**** Printer state table ****/
73 int bit
; /* State bit */
74 const char *keyword
; /* IPP printer-state-reasons keyword */
82 static http_addr_t current_addr
; /* Current address */
83 static int current_state
= -1;
84 /* Current device state bits */
85 static int charset
= -1; /* Character set for supply names */
86 static unsigned quirks
= CUPS_SNMP_NONE
;
87 /* Quirks we have to work around */
88 static int num_supplies
= 0;
89 /* Number of supplies found */
90 static backend_supplies_t supplies
[CUPS_MAX_SUPPLIES
];
91 /* Supply information */
92 static int supply_state
= -1;
93 /* Supply state info */
95 static const int hrDeviceDescr
[] =
96 { CUPS_OID_hrDeviceDescr
, 1, -1 };
97 /* Device description OID */
98 static const int hrPrinterStatus
[] =
99 { CUPS_OID_hrPrinterStatus
, 1, -1 };
100 /* Current state OID */
101 static const int hrPrinterDetectedErrorState
[] =
102 { CUPS_OID_hrPrinterDetectedErrorState
, 1, -1 };
103 /* Current printer state bits OID */
104 static const int prtGeneralCurrentLocalization
[] =
105 { CUPS_OID_prtGeneralCurrentLocalization
, 1, -1 };
106 static const int prtLocalizationCharacterSet
[] =
107 { CUPS_OID_prtLocalizationCharacterSet
, 1, 1, -1 },
108 prtLocalizationCharacterSetOffset
=
109 (sizeof(prtLocalizationCharacterSet
) /
110 sizeof(prtLocalizationCharacterSet
[0]));
111 static const int prtMarkerColorantValue
[] =
112 { CUPS_OID_prtMarkerColorantValue
, -1 },
114 prtMarkerColorantValueOffset
=
115 (sizeof(prtMarkerColorantValue
) /
116 sizeof(prtMarkerColorantValue
[0]));
117 /* Offset to colorant index */
118 static const int prtMarkerLifeCount
[] =
119 { CUPS_OID_prtMarkerLifeCount
, 1, 1, -1 };
120 /* Page counter OID */
121 static const int prtMarkerSuppliesEntry
[] =
122 { CUPS_OID_prtMarkerSuppliesEntry
, -1 };
124 static const int prtMarkerSuppliesColorantIndex
[] =
125 { CUPS_OID_prtMarkerSuppliesColorantIndex
, -1 },
126 /* Colorant index OID */
127 prtMarkerSuppliesColorantIndexOffset
=
128 (sizeof(prtMarkerSuppliesColorantIndex
) /
129 sizeof(prtMarkerSuppliesColorantIndex
[0]));
130 /* Offset to supply index */
131 static const int prtMarkerSuppliesDescription
[] =
132 { CUPS_OID_prtMarkerSuppliesDescription
, -1 },
133 /* Description OID */
134 prtMarkerSuppliesDescriptionOffset
=
135 (sizeof(prtMarkerSuppliesDescription
) /
136 sizeof(prtMarkerSuppliesDescription
[0]));
137 /* Offset to supply index */
138 static const int prtMarkerSuppliesLevel
[] =
139 { CUPS_OID_prtMarkerSuppliesLevel
, -1 },
141 prtMarkerSuppliesLevelOffset
=
142 (sizeof(prtMarkerSuppliesLevel
) /
143 sizeof(prtMarkerSuppliesLevel
[0]));
144 /* Offset to supply index */
145 static const int prtMarkerSuppliesMaxCapacity
[] =
146 { CUPS_OID_prtMarkerSuppliesMaxCapacity
, -1 },
147 /* Max capacity OID */
148 prtMarkerSuppliesMaxCapacityOffset
=
149 (sizeof(prtMarkerSuppliesMaxCapacity
) /
150 sizeof(prtMarkerSuppliesMaxCapacity
[0]));
151 /* Offset to supply index */
152 static const int prtMarkerSuppliesClass
[] =
153 { CUPS_OID_prtMarkerSuppliesClass
, -1 },
155 prtMarkerSuppliesClassOffset
=
156 (sizeof(prtMarkerSuppliesClass
) /
157 sizeof(prtMarkerSuppliesClass
[0]));
158 /* Offset to supply index */
159 static const int prtMarkerSuppliesType
[] =
160 { CUPS_OID_prtMarkerSuppliesType
, -1 },
162 prtMarkerSuppliesTypeOffset
=
163 (sizeof(prtMarkerSuppliesType
) /
164 sizeof(prtMarkerSuppliesType
[0]));
165 /* Offset to supply index */
166 static const int prtMarkerSuppliesSupplyUnit
[] =
167 { CUPS_OID_prtMarkerSuppliesSupplyUnit
, -1 },
169 prtMarkerSuppliesSupplyUnitOffset
=
170 (sizeof(prtMarkerSuppliesSupplyUnit
) /
171 sizeof(prtMarkerSuppliesSupplyUnit
[0]));
172 /* Offset to supply index */
174 static const backend_state_t printer_states
[] =
176 /* { CUPS_TC_lowPaper, "media-low-report" }, */
177 { CUPS_TC_noPaper
| CUPS_TC_inputTrayEmpty
, "media-empty-warning" },
178 /* { CUPS_TC_lowToner, "toner-low-report" }, */ /* now use prtMarkerSupplies */
179 /* { CUPS_TC_noToner, "toner-empty-warning" }, */ /* now use prtMarkerSupplies */
180 { CUPS_TC_doorOpen
, "door-open-report" },
181 { CUPS_TC_jammed
, "media-jam-warning" },
182 /* { CUPS_TC_offline, "offline-report" }, */ /* unreliable */
183 /* { CUPS_TC_serviceRequested | CUPS_TC_overduePreventMaint, "service-needed-warning" }, */ /* unreliable */
184 { CUPS_TC_inputTrayMissing
, "input-tray-missing-warning" },
185 { CUPS_TC_outputTrayMissing
, "output-tray-missing-warning" },
186 { CUPS_TC_markerSupplyMissing
, "marker-supply-missing-warning" },
187 { CUPS_TC_outputNearFull
, "output-area-almost-full-report" },
188 { CUPS_TC_outputFull
, "output-area-full-warning" }
191 static const backend_state_t supply_states
[] =
193 { CUPS_DEVELOPER_LOW
, "developer-low-report" },
194 { CUPS_DEVELOPER_EMPTY
, "developer-empty-warning" },
195 { CUPS_MARKER_SUPPLY_LOW
, "marker-supply-low-report" },
196 { CUPS_MARKER_SUPPLY_EMPTY
, "marker-supply-empty-warning" },
197 { CUPS_OPC_NEAR_EOL
, "opc-near-eol-report" },
198 { CUPS_OPC_LIFE_OVER
, "opc-life-over-warning" },
199 { CUPS_TONER_LOW
, "toner-low-report" },
200 { CUPS_TONER_EMPTY
, "toner-empty-warning" },
201 { CUPS_WASTE_ALMOST_FULL
, "waste-receptacle-almost-full-report" },
202 { CUPS_WASTE_FULL
, "waste-receptacle-full-warning" },
203 { CUPS_CLEANER_NEAR_EOL
, "cleaner-life-almost-over-report" },
204 { CUPS_CLEANER_LIFE_OVER
, "cleaner-life-over-warning" },
212 static void backend_init_supplies(int snmp_fd
, http_addr_t
*addr
);
213 static void backend_walk_cb(cups_snmp_t
*packet
, void *data
);
214 static void utf16_to_utf8(cups_utf8_t
*dst
, const unsigned char *src
,
215 size_t srcsize
, size_t dstsize
, int le
);
219 * 'backendSNMPSupplies()' - Get the current supplies for a device.
222 int /* O - 0 on success, -1 on error */
224 int snmp_fd
, /* I - SNMP socket */
225 http_addr_t
*addr
, /* I - Printer address */
226 int *page_count
, /* O - Page count */
227 int *printer_state
) /* O - Printer state */
229 if (!httpAddrEqual(addr
, ¤t_addr
))
230 backend_init_supplies(snmp_fd
, addr
);
231 else if (num_supplies
> 0)
232 _cupsSNMPWalk(snmp_fd
, ¤t_addr
, CUPS_SNMP_VERSION_1
,
233 _cupsSNMPDefaultCommunity(), prtMarkerSuppliesLevel
,
234 CUPS_SUPPLY_TIMEOUT
, backend_walk_cb
, NULL
);
242 if (num_supplies
> 0)
244 int i
, /* Looping var */
245 percent
, /* Percent full */
246 new_state
, /* New state value */
247 change_state
, /* State change */
248 new_supply_state
= 0; /* Supply state */
249 char value
[CUPS_MAX_SUPPLIES
* 4],
250 /* marker-levels value string */
251 *ptr
; /* Pointer into value string */
252 cups_snmp_t packet
; /* SNMP response packet */
255 * Generate the marker-levels value string...
258 for (i
= 0, ptr
= value
; i
< num_supplies
; i
++, ptr
+= strlen(ptr
))
260 if (supplies
[i
].max_capacity
> 0 && supplies
[i
].level
>= 0)
261 percent
= 100 * supplies
[i
].level
/ supplies
[i
].max_capacity
;
262 else if (supplies
[i
].level
>= 0 && supplies
[i
].level
<= 100 &&
263 (quirks
& CUPS_SNMP_CAPACITY
))
264 percent
= supplies
[i
].level
;
268 if (supplies
[i
].sclass
== CUPS_TC_receptacleThatIsFilled
)
269 percent
= 100 - percent
;
273 switch (supplies
[i
].type
)
276 case CUPS_TC_tonerCartridge
:
278 new_supply_state
|= CUPS_TONER_EMPTY
;
280 new_supply_state
|= CUPS_TONER_LOW
;
283 case CUPS_TC_inkCartridge
:
284 case CUPS_TC_inkRibbon
:
285 case CUPS_TC_solidWax
:
286 case CUPS_TC_ribbonWax
:
288 new_supply_state
|= CUPS_MARKER_SUPPLY_EMPTY
;
290 new_supply_state
|= CUPS_MARKER_SUPPLY_LOW
;
292 case CUPS_TC_developer
:
294 new_supply_state
|= CUPS_DEVELOPER_EMPTY
;
296 new_supply_state
|= CUPS_DEVELOPER_LOW
;
298 case CUPS_TC_coronaWire
:
301 case CUPS_TC_transferUnit
:
303 new_supply_state
|= CUPS_OPC_LIFE_OVER
;
305 new_supply_state
|= CUPS_OPC_NEAR_EOL
;
307 case CUPS_TC_wasteInk
:
308 case CUPS_TC_wastePaper
:
309 case CUPS_TC_wasteToner
:
310 case CUPS_TC_wasteWater
:
311 case CUPS_TC_wasteWax
:
313 new_supply_state
|= CUPS_WASTE_FULL
;
315 new_supply_state
|= CUPS_WASTE_ALMOST_FULL
;
317 case CUPS_TC_cleanerUnit
:
318 case CUPS_TC_fuserCleaningPad
:
320 new_supply_state
|= CUPS_CLEANER_LIFE_OVER
;
322 new_supply_state
|= CUPS_CLEANER_NEAR_EOL
;
330 if ((supplies
[i
].max_capacity
> 0 || (quirks
& CUPS_SNMP_CAPACITY
)) &&
331 supplies
[i
].level
>= 0)
332 snprintf(ptr
, sizeof(value
) - (ptr
- value
), "%d", percent
);
334 strlcpy(ptr
, "-1", sizeof(value
) - (ptr
- value
));
337 fprintf(stderr
, "ATTR: marker-levels=%s\n", value
);
339 if (supply_state
< 0)
340 change_state
= 0xffff;
342 change_state
= supply_state
^ new_supply_state
;
344 fprintf(stderr
, "DEBUG: new_supply_state=%x, change_state=%x\n",
345 new_supply_state
, change_state
);
348 i
< (int)(sizeof(supply_states
) / sizeof(supply_states
[0]));
350 if (change_state
& supply_states
[i
].bit
)
352 fprintf(stderr
, "STATE: %c%s\n",
353 (new_supply_state
& supply_states
[i
].bit
) ? '+' : '-',
354 supply_states
[i
].keyword
);
357 supply_state
= new_supply_state
;
360 * Get the current printer status bits...
363 if (!_cupsSNMPWrite(snmp_fd
, addr
, CUPS_SNMP_VERSION_1
,
364 _cupsSNMPDefaultCommunity(), CUPS_ASN1_GET_REQUEST
, 1,
365 hrPrinterDetectedErrorState
))
368 if (!_cupsSNMPRead(snmp_fd
, &packet
, CUPS_SUPPLY_TIMEOUT
) ||
369 packet
.object_type
!= CUPS_ASN1_OCTET_STRING
)
372 if (packet
.object_value
.string
.num_bytes
== 2)
373 new_state
= (packet
.object_value
.string
.bytes
[0] << 8) |
374 packet
.object_value
.string
.bytes
[1];
375 else if (packet
.object_value
.string
.num_bytes
== 1)
376 new_state
= (packet
.object_value
.string
.bytes
[0] << 8);
380 if (current_state
< 0)
381 change_state
= 0xffff;
383 change_state
= current_state
^ new_state
;
385 fprintf(stderr
, "DEBUG: new_state=%x, change_state=%x\n", new_state
,
389 i
< (int)(sizeof(printer_states
) / sizeof(printer_states
[0]));
391 if (change_state
& printer_states
[i
].bit
)
393 fprintf(stderr
, "STATE: %c%s\n",
394 (new_state
& printer_states
[i
].bit
) ? '+' : '-',
395 printer_states
[i
].keyword
);
398 current_state
= new_state
;
401 * Get the current printer state...
406 if (!_cupsSNMPWrite(snmp_fd
, addr
, CUPS_SNMP_VERSION_1
,
407 _cupsSNMPDefaultCommunity(), CUPS_ASN1_GET_REQUEST
, 1,
411 if (!_cupsSNMPRead(snmp_fd
, &packet
, CUPS_SUPPLY_TIMEOUT
) ||
412 packet
.object_type
!= CUPS_ASN1_INTEGER
)
415 *printer_state
= packet
.object_value
.integer
;
419 * Get the current page count...
424 if (!_cupsSNMPWrite(snmp_fd
, addr
, CUPS_SNMP_VERSION_1
,
425 _cupsSNMPDefaultCommunity(), CUPS_ASN1_GET_REQUEST
, 1,
429 if (!_cupsSNMPRead(snmp_fd
, &packet
, CUPS_SUPPLY_TIMEOUT
) ||
430 packet
.object_type
!= CUPS_ASN1_COUNTER
)
433 *page_count
= packet
.object_value
.counter
;
444 * 'backend_init_supplies()' - Initialize the supplies list.
448 backend_init_supplies(
449 int snmp_fd
, /* I - SNMP socket */
450 http_addr_t
*addr
) /* I - Printer address */
452 int i
, /* Looping var */
453 type
; /* Current marker type */
454 cups_file_t
*cachefile
; /* Cache file */
455 const char *cachedir
; /* CUPS_CACHEDIR value */
456 char addrstr
[1024], /* Address string */
457 cachefilename
[1024], /* Cache filename */
458 description
[CUPS_SNMP_MAX_STRING
],
459 /* Device description string */
460 value
[CUPS_MAX_SUPPLIES
* (CUPS_SNMP_MAX_STRING
* 4 + 3)],
462 *ptr
, /* Pointer into value string */
463 *name_ptr
; /* Pointer into name string */
464 cups_snmp_t packet
; /* SNMP response packet */
465 ppd_file_t
*ppd
; /* PPD file for this queue */
466 ppd_attr_t
*ppdattr
; /* cupsSNMPSupplies attribute */
467 static const char * const types
[] = /* Supply types */
487 "fuser-cleaning-pad",
493 "glue-water-additive",
507 * Reset state information...
510 current_addr
= *addr
;
515 memset(supplies
, 0, sizeof(supplies
));
518 * See if we should be getting supply levels via SNMP...
521 if ((ppd
= ppdOpenFile(getenv("PPD"))) == NULL
||
522 ((ppdattr
= ppdFindAttr(ppd
, "cupsSNMPSupplies", NULL
)) != NULL
&&
523 ppdattr
->value
&& _cups_strcasecmp(ppdattr
->value
, "true")))
529 if ((ppdattr
= ppdFindAttr(ppd
, "cupsSNMPQuirks", NULL
)) != NULL
)
531 if (!_cups_strcasecmp(ppdattr
->value
, "capacity"))
532 quirks
|= CUPS_SNMP_CAPACITY
;
538 * Get the device description...
541 if (!_cupsSNMPWrite(snmp_fd
, addr
, CUPS_SNMP_VERSION_1
,
542 _cupsSNMPDefaultCommunity(), CUPS_ASN1_GET_REQUEST
, 1,
546 if (!_cupsSNMPRead(snmp_fd
, &packet
, CUPS_SUPPLY_TIMEOUT
) ||
547 packet
.object_type
!= CUPS_ASN1_OCTET_STRING
)
549 strlcpy(description
, "Unknown", sizeof(description
));
553 strlcpy(description
, (char *)packet
.object_value
.string
.bytes
,
554 sizeof(description
));
556 fprintf(stderr
, "DEBUG2: hrDeviceDesc=\"%s\"\n", description
);
559 * See if we have already queried this device...
562 httpAddrString(addr
, addrstr
, sizeof(addrstr
));
564 if ((cachedir
= getenv("CUPS_CACHEDIR")) == NULL
)
565 cachedir
= CUPS_CACHEDIR
;
567 snprintf(cachefilename
, sizeof(cachefilename
), "%s/%s.snmp", cachedir
,
570 if ((cachefile
= cupsFileOpen(cachefilename
, "r")) != NULL
)
573 * Yes, read the cache file:
575 * 3 num_supplies charset
577 * supply structures...
580 if (cupsFileGets(cachefile
, value
, sizeof(value
)))
582 if (sscanf(value
, "3 %d%d", &num_supplies
, &charset
) == 2 &&
583 num_supplies
<= CUPS_MAX_SUPPLIES
&&
584 cupsFileGets(cachefile
, value
, sizeof(value
)))
586 if (!strcmp(description
, value
))
587 cupsFileRead(cachefile
, (char *)supplies
,
588 num_supplies
* sizeof(backend_supplies_t
));
602 cupsFileClose(cachefile
);
606 * If the cache information isn't correct, scan for supplies...
612 * Get the configured character set...
615 int oid
[CUPS_SNMP_MAX_OID
]; /* OID for character set */
618 if (!_cupsSNMPWrite(snmp_fd
, ¤t_addr
, CUPS_SNMP_VERSION_1
,
619 _cupsSNMPDefaultCommunity(), CUPS_ASN1_GET_REQUEST
, 1,
620 prtGeneralCurrentLocalization
))
623 if (!_cupsSNMPRead(snmp_fd
, &packet
, CUPS_SUPPLY_TIMEOUT
) ||
624 packet
.object_type
!= CUPS_ASN1_INTEGER
)
627 "DEBUG: prtGeneralCurrentLocalization type is %x, expected %x!\n",
628 packet
.object_type
, CUPS_ASN1_INTEGER
);
632 fprintf(stderr
, "DEBUG2: prtGeneralCurrentLocalization=%d\n",
633 packet
.object_value
.integer
);
635 _cupsSNMPCopyOID(oid
, prtLocalizationCharacterSet
, CUPS_SNMP_MAX_OID
);
636 oid
[prtLocalizationCharacterSetOffset
- 2] = packet
.object_value
.integer
;
639 if (!_cupsSNMPWrite(snmp_fd
, ¤t_addr
, CUPS_SNMP_VERSION_1
,
640 _cupsSNMPDefaultCommunity(), CUPS_ASN1_GET_REQUEST
, 1,
644 if (!_cupsSNMPRead(snmp_fd
, &packet
, CUPS_SUPPLY_TIMEOUT
) ||
645 packet
.object_type
!= CUPS_ASN1_INTEGER
)
648 "DEBUG: prtLocalizationCharacterSet type is %x, expected %x!\n",
649 packet
.object_type
, CUPS_ASN1_INTEGER
);
653 fprintf(stderr
, "DEBUG2: prtLocalizationCharacterSet=%d\n",
654 packet
.object_value
.integer
);
655 charset
= packet
.object_value
.integer
;
658 if (num_supplies
< 0)
661 * Walk the printer configuration information...
664 _cupsSNMPWalk(snmp_fd
, ¤t_addr
, CUPS_SNMP_VERSION_1
,
665 _cupsSNMPDefaultCommunity(), prtMarkerSuppliesEntry
,
666 CUPS_SUPPLY_TIMEOUT
, backend_walk_cb
, NULL
);
670 * Save the cached information...
673 if (num_supplies
< 0)
676 if ((cachefile
= cupsFileOpen(cachefilename
, "w")) != NULL
)
678 cupsFilePrintf(cachefile
, "3 %d %d\n", num_supplies
, charset
);
679 cupsFilePrintf(cachefile
, "%s\n", description
);
681 if (num_supplies
> 0)
682 cupsFileWrite(cachefile
, (char *)supplies
,
683 num_supplies
* sizeof(backend_supplies_t
));
685 cupsFileClose(cachefile
);
688 if (num_supplies
<= 0)
695 for (i
= 0; i
< num_supplies
; i
++)
696 strlcpy(supplies
[i
].color
, "none", sizeof(supplies
[i
].color
));
698 _cupsSNMPWalk(snmp_fd
, ¤t_addr
, CUPS_SNMP_VERSION_1
,
699 _cupsSNMPDefaultCommunity(), prtMarkerColorantValue
,
700 CUPS_SUPPLY_TIMEOUT
, backend_walk_cb
, NULL
);
703 * Output the marker-colors attribute...
706 for (i
= 0, ptr
= value
; i
< num_supplies
; i
++, ptr
+= strlen(ptr
))
711 strlcpy(ptr
, supplies
[i
].color
, sizeof(value
) - (ptr
- value
));
714 fprintf(stderr
, "ATTR: marker-colors=%s\n", value
);
717 * Output the marker-names attribute (the double quoting is necessary to deal
718 * with embedded quotes and commas in the marker names...)
721 for (i
= 0, ptr
= value
; i
< num_supplies
; i
++)
728 for (name_ptr
= supplies
[i
].name
; *name_ptr
;)
730 if (*name_ptr
== '\\' || *name_ptr
== '\"' || *name_ptr
== '\'')
737 *ptr
++ = *name_ptr
++;
745 fprintf(stderr
, "ATTR: marker-names=%s\n", value
);
748 * Output the marker-types attribute...
751 for (i
= 0, ptr
= value
; i
< num_supplies
; i
++, ptr
+= strlen(ptr
))
756 type
= supplies
[i
].type
;
758 if (type
< CUPS_TC_other
|| type
> CUPS_TC_covers
)
759 strlcpy(ptr
, "unknown", sizeof(value
) - (ptr
- value
));
761 strlcpy(ptr
, types
[type
- CUPS_TC_other
], sizeof(value
) - (ptr
- value
));
764 fprintf(stderr
, "ATTR: marker-types=%s\n", value
);
769 * 'backend_walk_cb()' - Interpret the supply value responses.
773 backend_walk_cb(cups_snmp_t
*packet
, /* I - SNMP packet */
774 void *data
) /* I - User data (unused) */
776 int i
, j
, k
; /* Looping vars */
777 static const char * const colors
[][2] =
778 { /* Standard color names */
779 { "black", "#000000" },
780 { "blue", "#0000FF" },
781 { "brown", "#A52A2A" },
782 { "cyan", "#00FFFF" },
783 { "dark-gray", "#404040" },
784 { "dark gray", "#404040" },
785 { "dark-yellow", "#FFCC00" },
786 { "dark yellow", "#FFCC00" },
787 { "gold", "#FFD700" },
788 { "gray", "#808080" },
789 { "green", "#00FF00" },
790 { "light-black", "#606060" },
791 { "light black", "#606060" },
792 { "light-cyan", "#E0FFFF" },
793 { "light cyan", "#E0FFFF" },
794 { "light-gray", "#D3D3D3" },
795 { "light gray", "#D3D3D3" },
796 { "light-magenta", "#FF77FF" },
797 { "light magenta", "#FF77FF" },
798 { "magenta", "#FF00FF" },
799 { "orange", "#FFA500" },
800 { "red", "#FF0000" },
801 { "silver", "#C0C0C0" },
802 { "white", "#FFFFFF" },
803 { "yellow", "#FFFF00" }
809 if (_cupsSNMPIsOIDPrefixed(packet
, prtMarkerColorantValue
) &&
810 packet
->object_type
== CUPS_ASN1_OCTET_STRING
)
816 i
= packet
->object_name
[prtMarkerColorantValueOffset
];
818 fprintf(stderr
, "DEBUG2: prtMarkerColorantValue.1.%d = \"%s\"\n", i
,
819 (char *)packet
->object_value
.string
.bytes
);
821 for (j
= 0; j
< num_supplies
; j
++)
822 if (supplies
[j
].colorant
== i
)
824 for (k
= 0; k
< (int)(sizeof(colors
) / sizeof(colors
[0])); k
++)
825 if (!_cups_strcasecmp(colors
[k
][0],
826 (char *)packet
->object_value
.string
.bytes
))
828 strlcpy(supplies
[j
].color
, colors
[k
][1], sizeof(supplies
[j
].color
));
833 else if (_cupsSNMPIsOIDPrefixed(packet
, prtMarkerSuppliesColorantIndex
))
836 * Get colorant index...
839 i
= packet
->object_name
[prtMarkerSuppliesColorantIndexOffset
];
840 if (i
< 1 || i
> CUPS_MAX_SUPPLIES
||
841 packet
->object_type
!= CUPS_ASN1_INTEGER
)
844 fprintf(stderr
, "DEBUG2: prtMarkerSuppliesColorantIndex.1.%d = %d\n", i
,
845 packet
->object_value
.integer
);
847 if (i
> num_supplies
)
850 supplies
[i
- 1].colorant
= packet
->object_value
.integer
;
852 else if (_cupsSNMPIsOIDPrefixed(packet
, prtMarkerSuppliesDescription
))
855 * Get supply name/description...
858 i
= packet
->object_name
[prtMarkerSuppliesDescriptionOffset
];
859 if (i
< 1 || i
> CUPS_MAX_SUPPLIES
||
860 packet
->object_type
!= CUPS_ASN1_OCTET_STRING
)
863 if (i
> num_supplies
)
868 case CUPS_TC_csASCII
:
869 case CUPS_TC_csUTF8
:
870 case CUPS_TC_csUnicodeASCII
:
871 strlcpy(supplies
[i
- 1].name
,
872 (char *)packet
->object_value
.string
.bytes
,
873 sizeof(supplies
[0].name
));
876 case CUPS_TC_csISOLatin1
:
877 case CUPS_TC_csUnicodeLatin1
:
878 cupsCharsetToUTF8((cups_utf8_t
*)supplies
[i
- 1].name
,
879 (char *)packet
->object_value
.string
.bytes
,
880 sizeof(supplies
[0].name
), CUPS_ISO8859_1
);
883 case CUPS_TC_csShiftJIS
:
884 case CUPS_TC_csWindows31J
: /* Close enough for our purposes */
885 cupsCharsetToUTF8((cups_utf8_t
*)supplies
[i
- 1].name
,
886 (char *)packet
->object_value
.string
.bytes
,
887 sizeof(supplies
[0].name
), CUPS_JIS_X0213
);
890 case CUPS_TC_csUCS4
:
891 case CUPS_TC_csUTF32
:
892 case CUPS_TC_csUTF32BE
:
893 case CUPS_TC_csUTF32LE
:
894 cupsUTF32ToUTF8((cups_utf8_t
*)supplies
[i
- 1].name
,
895 (cups_utf32_t
*)packet
->object_value
.string
.bytes
,
896 sizeof(supplies
[0].name
));
899 case CUPS_TC_csUnicode
:
900 case CUPS_TC_csUTF16BE
:
901 case CUPS_TC_csUTF16LE
:
902 utf16_to_utf8((cups_utf8_t
*)supplies
[i
- 1].name
,
903 packet
->object_value
.string
.bytes
,
904 packet
->object_value
.string
.num_bytes
,
905 sizeof(supplies
[0].name
), charset
== CUPS_TC_csUTF16LE
);
910 * If we get here, the printer is using an unknown character set and
911 * we just want to copy characters that look like ASCII...
915 char *src
, *dst
; /* Pointers into strings */
918 * Loop safe because both the object_value and supplies char arrays
919 * are CUPS_SNMP_MAX_STRING elements long.
922 for (src
= (char *)packet
->object_value
.string
.bytes
,
923 dst
= supplies
[i
- 1].name
;
927 if ((*src
& 0x80) || *src
< ' ' || *src
== 0x7f)
938 fprintf(stderr
, "DEBUG2: prtMarkerSuppliesDescription.1.%d = \"%s\"\n", i
,
939 supplies
[i
- 1].name
);
942 else if (_cupsSNMPIsOIDPrefixed(packet
, prtMarkerSuppliesLevel
))
948 i
= packet
->object_name
[prtMarkerSuppliesLevelOffset
];
949 if (i
< 1 || i
> CUPS_MAX_SUPPLIES
||
950 packet
->object_type
!= CUPS_ASN1_INTEGER
)
953 fprintf(stderr
, "DEBUG2: prtMarkerSuppliesLevel.1.%d = %d\n", i
,
954 packet
->object_value
.integer
);
956 if (i
> num_supplies
)
959 supplies
[i
- 1].level
= packet
->object_value
.integer
;
961 else if (_cupsSNMPIsOIDPrefixed(packet
, prtMarkerSuppliesMaxCapacity
) &&
962 !(quirks
& CUPS_SNMP_CAPACITY
))
965 * Get max capacity...
968 i
= packet
->object_name
[prtMarkerSuppliesMaxCapacityOffset
];
969 if (i
< 1 || i
> CUPS_MAX_SUPPLIES
||
970 packet
->object_type
!= CUPS_ASN1_INTEGER
)
973 fprintf(stderr
, "DEBUG2: prtMarkerSuppliesMaxCapacity.1.%d = %d\n", i
,
974 packet
->object_value
.integer
);
976 if (i
> num_supplies
)
979 if (supplies
[i
- 1].max_capacity
== 0 &&
980 packet
->object_value
.integer
> 0)
981 supplies
[i
- 1].max_capacity
= packet
->object_value
.integer
;
983 else if (_cupsSNMPIsOIDPrefixed(packet
, prtMarkerSuppliesClass
))
986 * Get marker class...
989 i
= packet
->object_name
[prtMarkerSuppliesClassOffset
];
990 if (i
< 1 || i
> CUPS_MAX_SUPPLIES
||
991 packet
->object_type
!= CUPS_ASN1_INTEGER
)
994 fprintf(stderr
, "DEBUG2: prtMarkerSuppliesClass.1.%d = %d\n", i
,
995 packet
->object_value
.integer
);
997 if (i
> num_supplies
)
1000 supplies
[i
- 1].sclass
= packet
->object_value
.integer
;
1002 else if (_cupsSNMPIsOIDPrefixed(packet
, prtMarkerSuppliesType
))
1005 * Get marker type...
1008 i
= packet
->object_name
[prtMarkerSuppliesTypeOffset
];
1009 if (i
< 1 || i
> CUPS_MAX_SUPPLIES
||
1010 packet
->object_type
!= CUPS_ASN1_INTEGER
)
1013 fprintf(stderr
, "DEBUG2: prtMarkerSuppliesType.1.%d = %d\n", i
,
1014 packet
->object_value
.integer
);
1016 if (i
> num_supplies
)
1019 supplies
[i
- 1].type
= packet
->object_value
.integer
;
1021 else if (_cupsSNMPIsOIDPrefixed(packet
, prtMarkerSuppliesSupplyUnit
))
1024 * Get units for capacity...
1027 i
= packet
->object_name
[prtMarkerSuppliesSupplyUnitOffset
];
1028 if (i
< 1 || i
> CUPS_MAX_SUPPLIES
||
1029 packet
->object_type
!= CUPS_ASN1_INTEGER
)
1032 fprintf(stderr
, "DEBUG2: prtMarkerSuppliesSupplyUnit.1.%d = %d\n", i
,
1033 packet
->object_value
.integer
);
1035 if (i
> num_supplies
)
1038 if (packet
->object_value
.integer
== CUPS_TC_percent
)
1039 supplies
[i
- 1].max_capacity
= 100;
1045 * 'utf16_to_utf8()' - Convert UTF-16 text to UTF-8.
1050 cups_utf8_t
*dst
, /* I - Destination buffer */
1051 const unsigned char *src
, /* I - Source string */
1052 size_t srcsize
, /* I - Size of source string */
1053 size_t dstsize
, /* I - Size of destination buffer */
1054 int le
) /* I - Source is little-endian? */
1056 cups_utf32_t ch
, /* Current character */
1057 temp
[CUPS_SNMP_MAX_STRING
],
1059 *ptr
; /* Pointer into UTF-32 string */
1062 for (ptr
= temp
; srcsize
>= 2;)
1065 ch
= src
[0] | (src
[1] << 8);
1067 ch
= (src
[0] << 8) | src
[1];
1072 if (ch
>= 0xd800 && ch
<= 0xdbff && srcsize
>= 2)
1075 * Multi-word UTF-16 char...
1078 int lch
; /* Lower word */
1082 lch
= src
[0] | (src
[1] << 8);
1084 lch
= (src
[0] << 8) | src
[1];
1086 if (lch
>= 0xdc00 && lch
<= 0xdfff)
1091 ch
= (((ch
& 0x3ff) << 10) | (lch
& 0x3ff)) + 0x10000;
1095 if (ptr
< (temp
+ CUPS_SNMP_MAX_STRING
- 1))
1101 cupsUTF32ToUTF8(dst
, temp
, dstsize
);