]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - backend/snmp-supplies.c
Import CUPS v1.7.1
[thirdparty/cups.git] / backend / snmp-supplies.c
index 75dc9dc7a1b001ddec0e2aa8d68849c42790a3e4..568af85862903e28930ff1d948416208269ae63f 100644 (file)
@@ -1,9 +1,9 @@
 /*
- * "$Id$"
+ * "$Id: snmp-supplies.c 11319 2013-09-27 16:18:26Z msweet $"
  *
  *   SNMP supplies functions for CUPS.
  *
- *   Copyright 2008-2012 by Apple Inc.
+ *   Copyright 2008-2013 by Apple Inc.
  *
  *   These coded instructions, statements, and computer programs are the
  *   property of Apple Inc. and are protected by Federal copyright
@@ -49,6 +49,9 @@
 #define CUPS_CLEANER_NEAR_EOL  0x0400  /* Proposed JPS3 */
 #define CUPS_CLEANER_LIFE_OVER 0x0800  /* Proposed JPS3 */
 
+#define CUPS_SNMP_NONE         0x0000
+#define CUPS_SNMP_CAPACITY     0x0001  /* Supply levels reported as percentages */
+
 
 /*
  * Local structures...
@@ -59,6 +62,7 @@ typedef struct                                /**** Printer supply data ****/
   char name[CUPS_SNMP_MAX_STRING],     /* Name of supply */
        color[8];                       /* Color: "#RRGGBB" or "none" */
   int  colorant,                       /* Colorant index */
+       sclass,                         /* Supply class */
        type,                           /* Supply type */
        max_capacity,                   /* Maximum capacity */
        level;                          /* Current level value */
@@ -79,6 +83,8 @@ static http_addr_t    current_addr;   /* Current address */
 static int             current_state = -1;
                                        /* Current device state bits */
 static int             charset = -1;   /* Character set for supply names */
+static unsigned                quirks = CUPS_SNMP_NONE;
+                                       /* Quirks we have to work around */
 static int             num_supplies = 0;
                                        /* Number of supplies found */
 static backend_supplies_t supplies[CUPS_MAX_SUPPLIES];
@@ -143,6 +149,13 @@ static const int   prtMarkerSuppliesMaxCapacity[] =
                        (sizeof(prtMarkerSuppliesMaxCapacity) /
                         sizeof(prtMarkerSuppliesMaxCapacity[0]));
                                        /* Offset to supply index */
+static const int       prtMarkerSuppliesClass[] =
+                       { CUPS_OID_prtMarkerSuppliesClass, -1 },
+                                       /* Class OID */
+                       prtMarkerSuppliesClassOffset =
+                       (sizeof(prtMarkerSuppliesClass) /
+                        sizeof(prtMarkerSuppliesClass[0]));
+                                       /* Offset to supply index */
 static const int       prtMarkerSuppliesType[] =
                        { CUPS_OID_prtMarkerSuppliesType, -1 },
                                        /* Type OID */
@@ -158,7 +171,7 @@ static const int    prtMarkerSuppliesSupplyUnit[] =
                         sizeof(prtMarkerSuppliesSupplyUnit[0]));
                                        /* Offset to supply index */
 
-static const backend_state_t const printer_states[] =
+static const backend_state_t printer_states[] =
                        {
                          /* { CUPS_TC_lowPaper, "media-low-report" }, */
                          { CUPS_TC_noPaper | CUPS_TC_inputTrayEmpty, "media-empty-warning" },
@@ -175,7 +188,7 @@ static const backend_state_t const printer_states[] =
                          { CUPS_TC_outputFull, "output-area-full-warning" }
                        };
 
-static const backend_state_t const supply_states[] =
+static const backend_state_t supply_states[] =
                        {
                          { CUPS_DEVELOPER_LOW, "developer-low-report" },
                          { CUPS_DEVELOPER_EMPTY, "developer-empty-warning" },
@@ -246,9 +259,15 @@ backendSNMPSupplies(
     {
       if (supplies[i].max_capacity > 0 && supplies[i].level >= 0)
        percent = 100 * supplies[i].level / supplies[i].max_capacity;
+      else if (supplies[i].level >= 0 && supplies[i].level <= 100 &&
+               (quirks & CUPS_SNMP_CAPACITY))
+        percent = supplies[i].level;
       else
         percent = 50;
 
+      if (supplies[i].sclass == CUPS_TC_receptacleThatIsFilled)
+        percent = 100 - percent;
+
       if (percent <= 5)
       {
         switch (supplies[i].type)
@@ -308,10 +327,11 @@ backendSNMPSupplies(
       if (i)
         *ptr++ = ',';
 
-      if (supplies[i].max_capacity > 0 && supplies[i].level >= 0)
-        sprintf(ptr, "%d", percent);
+      if ((supplies[i].max_capacity > 0 || (quirks & CUPS_SNMP_CAPACITY)) &&
+          supplies[i].level >= 0)
+        snprintf(ptr, sizeof(value) - (ptr - value), "%d", percent);
       else
-        strcpy(ptr, "-1");
+        strlcpy(ptr, "-1", sizeof(value) - (ptr - value));
     }
 
     fprintf(stderr, "ATTR: marker-levels=%s\n", value);
@@ -449,34 +469,34 @@ backend_init_supplies(
                  "other",
                  "unknown",
                  "toner",
-                 "wasteToner",
+                 "waste-toner",
                  "ink",
-                 "inkCartridge",
-                 "inkRibbon",
-                 "wasteInk",
+                 "ink-cartridge",
+                 "ink-ribbon",
+                 "waste-ink",
                  "opc",
                  "developer",
-                 "fuserOil",
-                 "solidWax",
-                 "ribbonWax",
-                 "wasteWax",
+                 "fuser-oil",
+                 "solid-wax",
+                 "ribbon-wax",
+                 "waste-wax",
                  "fuser",
-                 "coronaWire",
-                 "fuserOilWick",
-                 "cleanerUnit",
-                 "fuserCleaningPad",
-                 "transferUnit",
-                 "tonerCartridge",
-                 "fuserOiler",
+                 "corona-wire",
+                 "fuser-oil-wick",
+                 "cleaner-unit",
+                 "fuser-cleaning-pad",
+                 "transfer-unit",
+                 "toner-cartridge",
+                 "fuser-oiler",
                  "water",
-                 "wasteWater",
-                 "glueWaterAdditive",
-                 "wastePaper",
-                 "bindingSupply",
-                 "bandingSupply",
-                 "stitchingWire",
-                 "shrinkWrap",
-                 "paperWrap",
+                 "waste-water",
+                 "glue-water-additive",
+                 "waste-paper",
+                 "binding-supply",
+                 "banding-supply",
+                 "stitching-wire",
+                 "shrink-wrap",
+                 "paper-wrap",
                  "staples",
                  "inserts",
                  "covers"
@@ -506,6 +526,12 @@ backend_init_supplies(
     return;
   }
 
+  if ((ppdattr = ppdFindAttr(ppd, "cupsSNMPQuirks", NULL)) != NULL)
+  {
+    if (!_cups_strcasecmp(ppdattr->value, "capacity"))
+      quirks |= CUPS_SNMP_CAPACITY;
+  }
+
   ppdClose(ppd);
 
  /*
@@ -546,14 +572,14 @@ backend_init_supplies(
    /*
     * Yes, read the cache file:
     *
-    *     2 num_supplies charset
+    *     3 num_supplies charset
     *     device description
     *     supply structures...
     */
 
     if (cupsFileGets(cachefile, value, sizeof(value)))
     {
-      if (sscanf(value, "2 %d%d", &num_supplies, &charset) == 2 &&
+      if (sscanf(value, "3 %d%d", &num_supplies, &charset) == 2 &&
           num_supplies <= CUPS_MAX_SUPPLIES &&
           cupsFileGets(cachefile, value, sizeof(value)))
       {
@@ -649,7 +675,7 @@ backend_init_supplies(
 
   if ((cachefile = cupsFileOpen(cachefilename, "w")) != NULL)
   {
-    cupsFilePrintf(cachefile, "2 %d %d\n", num_supplies, charset);
+    cupsFilePrintf(cachefile, "3 %d %d\n", num_supplies, charset);
     cupsFilePrintf(cachefile, "%s\n", description);
 
     if (num_supplies > 0)
@@ -667,7 +693,7 @@ backend_init_supplies(
   */
 
   for (i = 0; i < num_supplies; i ++)
-    strcpy(supplies[i].color, "none");
+    strlcpy(supplies[i].color, "none", sizeof(supplies[i].color));
 
   _cupsSNMPWalk(snmp_fd, &current_addr, CUPS_SNMP_VERSION_1,
                 _cupsSNMPDefaultCommunity(), prtMarkerColorantValue,
@@ -682,7 +708,7 @@ backend_init_supplies(
     if (i)
       *ptr++ = ',';
 
-    strcpy(ptr, supplies[i].color);
+    strlcpy(ptr, supplies[i].color, sizeof(value) - (ptr - value));
   }
 
   fprintf(stderr, "ATTR: marker-colors=%s\n", value);
@@ -730,9 +756,9 @@ backend_init_supplies(
     type = supplies[i].type;
 
     if (type < CUPS_TC_other || type > CUPS_TC_covers)
-      strcpy(ptr, "unknown");
+      strlcpy(ptr, "unknown", sizeof(value) - (ptr - value));
     else
-      strcpy(ptr, types[type - CUPS_TC_other]);
+      strlcpy(ptr, types[type - CUPS_TC_other], sizeof(value) - (ptr - value));
   }
 
   fprintf(stderr, "ATTR: marker-types=%s\n", value);
@@ -799,7 +825,7 @@ backend_walk_cb(cups_snmp_t *packet,        /* I - SNMP packet */
          if (!_cups_strcasecmp(colors[k][0],
                                (char *)packet->object_value.string.bytes))
          {
-           strcpy(supplies[j].color, colors[k][1]);
+           strlcpy(supplies[j].color, colors[k][1], sizeof(supplies[j].color));
            break;
          }
       }
@@ -932,7 +958,8 @@ backend_walk_cb(cups_snmp_t *packet,        /* I - SNMP packet */
 
     supplies[i - 1].level = packet->object_value.integer;
   }
-  else if (_cupsSNMPIsOIDPrefixed(packet, prtMarkerSuppliesMaxCapacity))
+  else if (_cupsSNMPIsOIDPrefixed(packet, prtMarkerSuppliesMaxCapacity) &&
+           !(quirks & CUPS_SNMP_CAPACITY))
   {
    /*
     * Get max capacity...
@@ -953,6 +980,25 @@ backend_walk_cb(cups_snmp_t *packet,       /* I - SNMP packet */
         packet->object_value.integer > 0)
       supplies[i - 1].max_capacity = packet->object_value.integer;
   }
+  else if (_cupsSNMPIsOIDPrefixed(packet, prtMarkerSuppliesClass))
+  {
+   /*
+    * Get marker class...
+    */
+
+    i = packet->object_name[prtMarkerSuppliesClassOffset];
+    if (i < 1 || i > CUPS_MAX_SUPPLIES ||
+        packet->object_type != CUPS_ASN1_INTEGER)
+      return;
+
+    fprintf(stderr, "DEBUG2: prtMarkerSuppliesClass.1.%d = %d\n", i,
+            packet->object_value.integer);
+
+    if (i > num_supplies)
+      num_supplies = i;
+
+    supplies[i - 1].sclass = packet->object_value.integer;
+  }
   else if (_cupsSNMPIsOIDPrefixed(packet, prtMarkerSuppliesType))
   {
    /*
@@ -1057,5 +1103,5 @@ utf16_to_utf8(
 
 
 /*
- * End of "$Id$".
+ * End of "$Id: snmp-supplies.c 11319 2013-09-27 16:18:26Z msweet $".
  */