hosted/stlinkv2: Rewrote the serial number readering logic to not violate the USB spec and properly handle libusb errors

This commit is contained in:
dragonmux 2022-04-14 13:43:11 -04:00 committed by Rachel Mant
parent 94e9281404
commit 4287f1ba0e
1 changed files with 15 additions and 4 deletions

View File

@ -523,10 +523,21 @@ int stlink_init(bmp_info_t *info)
continue;
}
char serial[64];
r = libusb_get_string_descriptor_ascii(
sl->ul_libusb_device_handle, desc.iSerialNumber,
(uint8_t*)serial,sizeof(serial));
if (r <= 0 || !strstr(serial, info->serial)) {
if (desc.iSerialNumber) {
const int result = libusb_get_string_descriptor_ascii(sl->ul_libusb_device_handle,
desc.iSerialNumber, (uint8_t *)serial, sizeof(serial));
/* If the call fails and it's not because the device gave us STALL, continue to the next one */
if (result < 0 && result != LIBUSB_ERROR_PIPE) {
libusb_close(sl->ul_libusb_device_handle);
continue;
}
else if (result <= 0)
serial[0] = '\0';
}
else
serial[0] = '\0';
/* Likewise, if the serial number returned doesn't match the one in info, go to next */
if (!strstr(serial, info->serial)) {
libusb_close(sl->ul_libusb_device_handle);
continue;
}