mirror of
https://github.com/Genymobile/scrcpy.git
synced 2025-12-18 22:14:20 +01:00
Wrap libusb_device
Introduce a structure to wrap a libusb_device and expose its descriptor data read during discovery. PR #2974 <https://github.com/Genymobile/scrcpy/pull/2974>
This commit is contained in:
@@ -30,7 +30,8 @@ read_string(libusb_device_handle *handle, uint8_t desc_index) {
|
||||
}
|
||||
|
||||
static bool
|
||||
accept_device(libusb_device *device, const char *serial) {
|
||||
accept_device(libusb_device *device, const char *serial,
|
||||
struct sc_usb_device *out) {
|
||||
// Do not log any USB error in this function, it is expected that many USB
|
||||
// devices available on the computer have permission restrictions
|
||||
|
||||
@@ -47,39 +48,61 @@ accept_device(libusb_device *device, const char *serial) {
|
||||
}
|
||||
|
||||
char *device_serial = read_string(handle, desc.iSerialNumber);
|
||||
libusb_close(handle);
|
||||
if (!device_serial) {
|
||||
libusb_close(handle);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool matches = !strcmp(serial, device_serial);
|
||||
free(device_serial);
|
||||
return matches;
|
||||
if (!matches) {
|
||||
free(device_serial);
|
||||
libusb_close(handle);
|
||||
return false;
|
||||
}
|
||||
|
||||
out->device = libusb_ref_device(device);
|
||||
out->serial = device_serial;
|
||||
out->vid = desc.idVendor;
|
||||
out->pid = desc.idProduct;
|
||||
out->manufacturer = read_string(handle, desc.iManufacturer);
|
||||
out->product = read_string(handle, desc.iProduct);
|
||||
|
||||
libusb_close(handle);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
libusb_device *
|
||||
sc_usb_find_device(struct sc_usb *usb, const char *serial) {
|
||||
void
|
||||
sc_usb_device_destroy(struct sc_usb_device *usb_device) {
|
||||
libusb_unref_device(usb_device->device);
|
||||
free(usb_device->serial);
|
||||
free(usb_device->manufacturer);
|
||||
free(usb_device->product);
|
||||
}
|
||||
|
||||
bool
|
||||
sc_usb_find_device(struct sc_usb *usb, const char *serial,
|
||||
struct sc_usb_device *out) {
|
||||
assert(serial);
|
||||
|
||||
libusb_device **list;
|
||||
libusb_device *result = NULL;
|
||||
ssize_t count = libusb_get_device_list(usb->context, &list);
|
||||
if (count < 0) {
|
||||
log_libusb_error((enum libusb_error) count);
|
||||
return NULL;
|
||||
return false;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < (size_t) count; ++i) {
|
||||
libusb_device *device = list[i];
|
||||
|
||||
if (accept_device(device, serial)) {
|
||||
result = libusb_ref_device(device);
|
||||
break;
|
||||
if (accept_device(device, serial, out)) {
|
||||
libusb_free_device_list(list, 1);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
libusb_free_device_list(list, 1);
|
||||
return result;
|
||||
return false;
|
||||
}
|
||||
|
||||
static libusb_device_handle *
|
||||
|
||||
Reference in New Issue
Block a user