From: Michael R Sweet Date: Thu, 15 Apr 2021 20:23:16 +0000 (-0400) Subject: Prefer USB serial number over 1284 serial number (#170) X-Git-Tag: v2.4b1~140 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fb3bdc46eb606e50f78ba53bbbd44a07a781e141;p=thirdparty%2Fcups.git Prefer USB serial number over 1284 serial number (#170) --- diff --git a/backend/usb-libusb.c b/backend/usb-libusb.c index 0f81f0b23f..7855058a26 100644 --- a/backend/usb-libusb.c +++ b/backend/usb-libusb.c @@ -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