Add util function to read USB descriptor string
Use it from accept_device() to simplify (at the cost an additional allocation for each serial, but it is not important). It will also be useful in other functions in further commits. PR #2974 <https://github.com/Genymobile/scrcpy/pull/2974>
This commit is contained in:
parent
bbef426a4b
commit
1ab3692f3d
1 changed files with 25 additions and 9 deletions
|
@ -9,6 +9,26 @@ log_libusb_error(enum libusb_error errcode) {
|
||||||
LOGW("libusb error: %s", libusb_strerror(errcode));
|
LOGW("libusb error: %s", libusb_strerror(errcode));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static char *
|
||||||
|
read_string(libusb_device_handle *handle, uint8_t desc_index) {
|
||||||
|
char buffer[128];
|
||||||
|
int result =
|
||||||
|
libusb_get_string_descriptor_ascii(handle, desc_index,
|
||||||
|
(unsigned char *) buffer,
|
||||||
|
sizeof(buffer));
|
||||||
|
if (result < 0) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
assert((size_t) result <= sizeof(buffer));
|
||||||
|
|
||||||
|
// When non-negative, 'result' contains the number of bytes written
|
||||||
|
char *s = malloc(result + 1);
|
||||||
|
memcpy(s, buffer, result);
|
||||||
|
s[result] = '\0';
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
accept_device(libusb_device *device, const char *serial) {
|
accept_device(libusb_device *device, const char *serial) {
|
||||||
// Do not log any USB error in this function, it is expected that many USB
|
// Do not log any USB error in this function, it is expected that many USB
|
||||||
|
@ -26,19 +46,15 @@ accept_device(libusb_device *device, const char *serial) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
char buffer[128];
|
char *device_serial = read_string(handle, desc.iSerialNumber);
|
||||||
result = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber,
|
|
||||||
(unsigned char *) buffer,
|
|
||||||
sizeof(buffer));
|
|
||||||
libusb_close(handle);
|
libusb_close(handle);
|
||||||
if (result < 0) {
|
if (!device_serial) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
buffer[sizeof(buffer) - 1] = '\0'; // just in case
|
bool matches = !strcmp(serial, device_serial);
|
||||||
|
free(device_serial);
|
||||||
// Accept the device if its serial matches
|
return matches;
|
||||||
return !strcmp(buffer, serial);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static libusb_device *
|
static libusb_device *
|
||||||
|
|
Loading…
Reference in a new issue