]> git.ipfire.org Git - thirdparty/cups.git/commitdiff
Some USB printers (notably DYMO printers) report a bogus serial number in their
authorMichael R Sweet <michael.r.sweet@gmail.com>
Mon, 19 Apr 2021 12:44:48 +0000 (08:44 -0400)
committerMichael R Sweet <michael.r.sweet@gmail.com>
Mon, 19 Apr 2021 12:44:48 +0000 (08:44 -0400)
IEEE-1284 device ID.  This change forces the USB backend to prefer the USB
serial number string over the one in the 1284 ID so that we can properly handle
printing to different USB-connected printers of the same model.

backend/usb-darwin.c

index 3a8feffb65b26d3160cdc11b2a59e9cfb2b69143..ee75b9e0d0c978ef2e52fe8dc9d92eeafcb42176 100644 (file)
@@ -1669,11 +1669,34 @@ static CFStringRef copy_printer_interface_deviceid(printer_interface_t printer,
                                        CFStringAppendFormat(extras, NULL, CFSTR("MDL:%@;"), model);
                        }
 
-                       if (serial == NULL && desc.iSerialNumber != 0)
+                       if (desc.iSerialNumber != 0)
                        {
-                               serial = copy_printer_interface_indexed_description(printer, desc.iSerialNumber, kUSBLanguageEnglish);
-                               if (serial && CFStringGetLength(serial) > 0)
-                                       CFStringAppendFormat(extras, NULL, CFSTR("SERN:%@;"), serial);
+                               // Always look at the USB serial number since some printers
+                               // incorrectly include a bogus static serial number in their
+                               // IEEE-1284 device ID string...
+                               CFStringRef userial = copy_printer_interface_indexed_description(printer, desc.iSerialNumber, kUSBLanguageEnglish);
+                               if (userial && CFStringGetLength(userial) > 0 && (serial == NULL || CFStringCompare(serial, userial, kCFCompareCaseInsensitive) != kCFCompareEqualTo))
+                               {
+                                       if (serial != NULL)
+                                       {
+                                               // 1284 serial number doesn't match USB serial number, so  replace the existing SERN: in device ID
+                                               CFRange range = CFStringFind(ret, serial, 0);
+                                               CFMutableStringRef deviceIDString = CFStringCreateMutableCopy(NULL, 0, ret);
+                                               CFStringReplace(deviceIDString, range, userial);
+                                               CFRelease(ret);
+                                               ret = deviceIDString;
+
+                                               CFRelease(serial);
+                                       }
+                                       else
+                                       {
+                                               // No 1284 serial number so add SERN: with USB serial number to device ID
+                                               CFStringAppendFormat(extras, NULL, CFSTR("SERN:%@;"), userial);
+                                       }
+                                       serial = userial;
+                               }
+                               else if (userial != NULL)
+                                       CFRelease(userial);
                        }
 
                        if (ret != NULL)
@@ -1692,18 +1715,18 @@ static CFStringRef copy_printer_interface_deviceid(printer_interface_t printer,
 
        if (ret != NULL)
        {
-       /* Remove special characters from the serial number */
-       CFRange range = (serial != NULL ? CFStringFind(serial, CFSTR("+"), 0) : CFRangeMake(0, 0));
-       if (range.length == 1)
-       {
-               range = CFStringFind(ret, serial, 0);
+               /* Remove special characters from the serial number */
+               CFRange range = (serial != NULL ? CFStringFind(serial, CFSTR("+"), 0) : CFRangeMake(0, 0));
+               if (range.length == 1)
+               {
+                       range = CFStringFind(ret, serial, 0);
 
-               CFMutableStringRef deviceIDString = CFStringCreateMutableCopy(NULL, 0, ret);
-               CFRelease(ret);
+                       CFMutableStringRef deviceIDString = CFStringCreateMutableCopy(NULL, 0, ret);
+                       CFRelease(ret);
 
-               ret = deviceIDString;
-               CFStringFindAndReplace(deviceIDString, CFSTR("+"), CFSTR(""), range, 0);
-       }
+                       ret = deviceIDString;
+                       CFStringFindAndReplace(deviceIDString, CFSTR("+"), CFSTR(""), range, 0);
+               }
        }
 
        if (manufacturer != NULL)