]> git.ipfire.org Git - thirdparty/cups.git/commitdiff
Prefer USB serial number over 1284 serial number (#170)
authorMichael R Sweet <michael.r.sweet@gmail.com>
Thu, 15 Apr 2021 20:23:16 +0000 (16:23 -0400)
committerMichael R Sweet <michael.r.sweet@gmail.com>
Thu, 15 Apr 2021 20:23:16 +0000 (16:23 -0400)
backend/usb-libusb.c

index 0f81f0b23faa7f7e2ac53f3da1bf5ce6a7ea42a7..7855058a26518bb9500009903bbea1af98439424 100644 (file)
@@ -1260,9 +1260,10 @@ make_device_uri(
   const char   *mfg,                   /* Manufacturer */
                *mdl,                   /* Model */
                *des = NULL,            /* Description */
-               *sern;                  /* Serial number */
+               *sern = NULL;           /* Serial number */
   size_t       mfglen;                 /* Length of manufacturer string */
-  char         tempmfg[256],           /* Temporary manufacturer string */
+  char         tempmdl[256],           /* Temporary model string */
+               tempmfg[256],           /* Temporary manufacturer string */
                tempsern[256],          /* Temporary serial number string */
                *tempptr;               /* Pointer into temp string */
 
@@ -1273,16 +1274,11 @@ make_device_uri(
 
   num_values = _cupsGet1284Values(device_id, &values);
 
-  if ((sern = cupsGetOption("SERIALNUMBER", num_values, values)) == NULL)
-    if ((sern = cupsGetOption("SERN", num_values, values)) == NULL)
-      sern = cupsGetOption("SN", num_values, values);
+  memset(&devdesc, 0, sizeof(devdesc));
 
-  if ((!sern || !*sern) && ((libusb_get_device_descriptor(printer->device, &devdesc) >= 0) && devdesc.iSerialNumber))
+  if (libusb_get_device_descriptor(printer->device, &devdesc) >= 0 && devdesc.iSerialNumber)
   {
-   /*
-    * Try getting the serial number from the device itself...
-    */
-
+    // Try getting the serial number from the device itself...
     int length = libusb_get_string_descriptor_ascii(printer->handle, devdesc.iSerialNumber, (unsigned char *)tempsern, sizeof(tempsern) - 1);
     if (length > 0)
     {
@@ -1291,11 +1287,40 @@ make_device_uri(
     }
   }
 
+  if (!sern)
+  {
+    // Fall back on serial number from IEEE-1284 device ID, which on some
+    // printers (Issue #170) is a bogus hardcoded number.
+    if ((sern = cupsGetOption("SERIALNUMBER", num_values, values)) == NULL)
+      if ((sern = cupsGetOption("SERN", num_values, values)) == NULL)
+       sern = cupsGetOption("SN", num_values, values);
+  }
+
   if ((mfg = cupsGetOption("MANUFACTURER", num_values, values)) == NULL)
-    mfg = cupsGetOption("MFG", num_values, values);
+  {
+    if ((mfg = cupsGetOption("MFG", num_values, values)) == NULL && devdesc.iManufacturer)
+    {
+      int length = libusb_get_string_descriptor_ascii(printer->handle, devdesc.iManufacturer, (unsigned char *)tempmfg, sizeof(tempmfg) - 1);
+      if (length > 0)
+      {
+       tempmfg[length] = '\0';
+       mfg             = tempmfg;
+      }
+    }
+  }
 
   if ((mdl = cupsGetOption("MODEL", num_values, values)) == NULL)
-    mdl = cupsGetOption("MDL", num_values, values);
+  {
+    if ((mdl = cupsGetOption("MDL", num_values, values)) == NULL && devdesc.iProduct)
+    {
+      int length = libusb_get_string_descriptor_ascii(printer->handle, devdesc.iProduct, (unsigned char *)tempmdl, sizeof(tempmdl) - 1);
+      if (length > 0)
+      {
+       tempmdl[length] = '\0';
+       mdl             = tempmdl;
+      }
+    }
+  }
 
  /*
   * To maintain compatibility with the original character device backend on