Split USB initialization and connection

This will allow to execute other USB calls (retrieving the device list
for example) before connecting to the selected device.

PR #2974 <https://github.com/Genymobile/scrcpy/pull/2974>
otg
Romain Vimont 2 years ago
parent 2114f48185
commit bbef426a4b

@ -424,9 +424,17 @@ scrcpy(struct scrcpy_options *options) {
goto end;
}
ok = sc_usb_init(&s->usb, serial);
ok = sc_usb_init(&s->usb);
if (!ok) {
LOGE("Failed to initialized USB device");
LOGE("Failed to initialize USB");
sc_acksync_destroy(&s->acksync);
goto aoa_hid_end;
}
ok = sc_usb_connect(&s->usb, serial);
if (!ok) {
LOGE("Failed to connect to USB device %s", serial);
sc_usb_destroy(&s->usb);
sc_acksync_destroy(&s->acksync);
goto aoa_hid_end;
}
@ -434,6 +442,7 @@ scrcpy(struct scrcpy_options *options) {
ok = sc_aoa_init(&s->aoa, &s->usb, &s->acksync);
if (!ok) {
LOGE("Failed to enable HID over AOA");
sc_usb_disconnect(&s->usb);
sc_usb_destroy(&s->usb);
sc_acksync_destroy(&s->acksync);
goto aoa_hid_end;
@ -461,6 +470,7 @@ scrcpy(struct scrcpy_options *options) {
if (!need_aoa || !sc_aoa_start(&s->aoa)) {
sc_acksync_destroy(&s->acksync);
sc_usb_disconnect(&s->usb);
sc_usb_destroy(&s->usb);
sc_aoa_destroy(&s->aoa);
goto aoa_hid_end;
@ -650,6 +660,7 @@ end:
if (aoa_hid_initialized) {
sc_aoa_join(&s->aoa);
sc_aoa_destroy(&s->aoa);
sc_usb_disconnect(&s->usb);
sc_usb_destroy(&s->usb);
}
#endif

@ -76,18 +76,23 @@ sc_usb_open_handle(libusb_device *device) {
}
bool
sc_usb_init(struct sc_usb *usb, const char *serial) {
assert(serial);
sc_usb_init(struct sc_usb *usb) {
usb->handle = NULL;
return libusb_init(&usb->context) == LIBUSB_SUCCESS;
}
// There is only one device, initialize the context here
if (libusb_init(&usb->context) != LIBUSB_SUCCESS) {
return false;
}
void
sc_usb_destroy(struct sc_usb *usb) {
libusb_exit(usb->context);
}
bool
sc_usb_connect(struct sc_usb *usb, const char *serial) {
assert(serial);
libusb_device *device = sc_usb_find_device(usb, serial);
if (!device) {
LOGW("USB device %s not found", serial);
libusb_exit(usb->context);
return false;
}
@ -95,7 +100,6 @@ sc_usb_init(struct sc_usb *usb, const char *serial) {
libusb_unref_device(device);
if (!usb->handle) {
LOGW("Could not open USB device %s", serial);
libusb_exit(usb->context);
return false;
}
@ -103,7 +107,6 @@ sc_usb_init(struct sc_usb *usb, const char *serial) {
}
void
sc_usb_destroy(struct sc_usb *usb) {
sc_usb_disconnect(struct sc_usb *usb) {
libusb_close(usb->handle);
libusb_exit(usb->context);
}

@ -12,9 +12,15 @@ struct sc_usb {
};
bool
sc_usb_init(struct sc_usb *usb, const char *serial);
sc_usb_init(struct sc_usb *usb);
void
sc_usb_destroy(struct sc_usb *usb);
bool
sc_usb_connect(struct sc_usb *usb, const char *serial);
void
sc_usb_disconnect(struct sc_usb *usb);
#endif

Loading…
Cancel
Save