From be2cb7f438c42094455014136fb913c3b485b9f6 Mon Sep 17 00:00:00 2001 From: dreamsourcelabTAI Date: Fri, 11 Nov 2022 15:40:06 +0800 Subject: [PATCH] update: Auto open the device when it reconnected --- libsigrok4DSL/dsdevice.c | 5 ++-- libsigrok4DSL/hardware/DSL/dscope.c | 4 +-- libsigrok4DSL/hardware/DSL/dsl.c | 12 ++++++--- libsigrok4DSL/hardware/DSL/dslogic.c | 5 ++-- libsigrok4DSL/hardware/common/ezusb.c | 3 ++- libsigrok4DSL/hardware/common/usb.c | 4 +-- libsigrok4DSL/lib_main.c | 36 +++++++++++++++++++++------ libsigrok4DSL/libsigrok-internal.h | 9 +++++-- libsigrok4DSL/libsigrok.h | 2 +- 9 files changed, 55 insertions(+), 25 deletions(-) diff --git a/libsigrok4DSL/dsdevice.c b/libsigrok4DSL/dsdevice.c index de19b5ac..6f030df1 100644 --- a/libsigrok4DSL/dsdevice.c +++ b/libsigrok4DSL/dsdevice.c @@ -255,8 +255,7 @@ SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi) } /** @private */ -SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus, - uint8_t address, struct libusb_device_handle *hdl) +SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus, uint8_t address) { struct sr_usb_dev_inst *udi; @@ -267,7 +266,7 @@ SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus, udi->bus = bus; udi->address = address; - udi->devhdl = hdl; + udi->devhdl = NULL; udi->usb_dev = NULL; return udi; diff --git a/libsigrok4DSL/hardware/DSL/dscope.c b/libsigrok4DSL/hardware/DSL/dscope.c index 62f011ea..e415d181 100644 --- a/libsigrok4DSL/hardware/DSL/dscope.c +++ b/libsigrok4DSL/hardware/DSL/dscope.c @@ -328,7 +328,7 @@ static GSList *scan(GSList *options) /* Already has the firmware, so fix the new address. */ sr_info("Found a DSCope device,name:\"%s\",handle:%p", prof->model, device_handle); - usb_dev_info = sr_usb_dev_inst_new(bus, address, NULL); + usb_dev_info = sr_usb_dev_inst_new(bus, address); usb_dev_info->usb_dev = device_handle; sdi->conn = usb_dev_info; sdi->status = SR_ST_INACTIVE; @@ -354,7 +354,7 @@ static GSList *scan(GSList *options) "device %d.", devcnt); g_free(firmware); - usb_dev_info = sr_usb_dev_inst_new(bus, 0xff, NULL); + usb_dev_info = sr_usb_dev_inst_new(bus, 0xff); usb_dev_info->usb_dev = device_handle; sdi->conn = usb_dev_info; } diff --git a/libsigrok4DSL/hardware/DSL/dsl.c b/libsigrok4DSL/hardware/DSL/dsl.c index 2165a24d..a07a222b 100644 --- a/libsigrok4DSL/hardware/DSL/dsl.c +++ b/libsigrok4DSL/hardware/DSL/dsl.c @@ -326,6 +326,7 @@ static int hw_dev_open(struct sr_dev_driver *di, struct sr_dev_inst *sdi) libusb_error_name(ret), dev_handel); return SR_ERR; } + //sr_info("------------Open returns the libusb_device_handle: %p, struct:%p", usb->devhdl, usb); if (usb->address == 0xff){ /* @@ -1928,8 +1929,12 @@ SR_PRIV int dsl_dev_close(struct sr_dev_inst *sdi) sr_info("%s: Closing device %d on %d.%d interface %d.", sdi->driver->name, sdi->index, usb->bus, usb->address, USB_INTERFACE); - libusb_release_interface(usb->devhdl, USB_INTERFACE); - libusb_close(usb->devhdl); + if (usb->devhdl != NULL){ + libusb_release_interface(usb->devhdl, USB_INTERFACE); + libusb_close(usb->devhdl); + } + //sr_info("------------Close the libusb_device_handle:%p, struct:%p", usb->devhdl, usb); + usb->devhdl = NULL; sdi->status = SR_ST_INACTIVE; @@ -1951,7 +1956,8 @@ SR_PRIV int dsl_dev_acquisition_stop(const struct sr_dev_inst *sdi, void *cb_dat if (!devc->abort) { devc->abort = TRUE; dsl_wr_reg(sdi, CTR0_ADDR, bmFORCE_RDY); - } else if (devc->status == DSL_FINISH) { + } + else if (devc->status == DSL_FINISH) { /* Stop GPIF acquisition */ wr_cmd.header.dest = DSL_CTL_STOP; wr_cmd.header.size = 0; diff --git a/libsigrok4DSL/hardware/DSL/dslogic.c b/libsigrok4DSL/hardware/DSL/dslogic.c index d4221179..1e7f2a18 100644 --- a/libsigrok4DSL/hardware/DSL/dslogic.c +++ b/libsigrok4DSL/hardware/DSL/dslogic.c @@ -429,8 +429,7 @@ static GSList *scan(GSList *options) /* Already has the firmware, so fix the new address. */ sr_info("Found a DSLogic device,name:\"%s\",handle:%p", prof->model,device_handle); - usb_dev_info = sr_usb_dev_inst_new(bus, address, NULL); - + usb_dev_info = sr_usb_dev_inst_new(bus, address); usb_dev_info->usb_dev = device_handle; sdi->conn = usb_dev_info; sdi->status = SR_ST_INACTIVE; @@ -456,7 +455,7 @@ static GSList *scan(GSList *options) "device %d.", devcnt); g_free(firmware); - usb_dev_info = sr_usb_dev_inst_new(bus, 0xff, NULL); + usb_dev_info = sr_usb_dev_inst_new(bus, 0xff); usb_dev_info->usb_dev = device_handle; sdi->conn = usb_dev_info; } diff --git a/libsigrok4DSL/hardware/common/ezusb.c b/libsigrok4DSL/hardware/common/ezusb.c index 49a31d0f..60a0408b 100644 --- a/libsigrok4DSL/hardware/common/ezusb.c +++ b/libsigrok4DSL/hardware/common/ezusb.c @@ -137,7 +137,8 @@ SR_PRIV int ezusb_upload_firmware(libusb_device *dev, int configuration, if ((ezusb_reset(hdl, 0)) < 0) return SR_ERR; - libusb_close(hdl); + if (hdl != NULL) + libusb_close(hdl); return SR_OK; } diff --git a/libsigrok4DSL/hardware/common/usb.c b/libsigrok4DSL/hardware/common/usb.c index f8c800a6..7d5a21a7 100644 --- a/libsigrok4DSL/hardware/common/usb.c +++ b/libsigrok4DSL/hardware/common/usb.c @@ -125,7 +125,7 @@ SR_PRIV GSList *sr_usb_find(libusb_context *usb_ctx, const char *conn) "%d.%d).", des.idVendor, des.idProduct, b, a); usb = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]), - libusb_get_device_address(devlist[i]), NULL); + libusb_get_device_address(devlist[i])); usb->usb_dev = devlist[i]; devices = g_slist_append(devices, usb); @@ -181,7 +181,7 @@ SR_PRIV GSList *sr_usb_find_usbtmc(libusb_context *usb_ctx) libusb_get_device_address(devlist[i])); usb = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]), - libusb_get_device_address(devlist[i]), NULL); + libusb_get_device_address(devlist[i])); usb->usb_dev = devlist[i]; devices = g_slist_append(devices, usb); diff --git a/libsigrok4DSL/lib_main.c b/libsigrok4DSL/lib_main.c index e5e69878..714d5a4c 100644 --- a/libsigrok4DSL/lib_main.c +++ b/libsigrok4DSL/lib_main.c @@ -36,6 +36,9 @@ #define MAX_DEVCIE_LIST_LENGTH 20 +// Wait the device reconnect for 500ms. +#define CHECK_DEV_RECONNECT_TIMES 5 + #undef LOG_PREFIX #define LOG_PREFIX "lib_main: " @@ -61,6 +64,8 @@ struct sr_lib_context int callback_thread_count; int is_delay_destory_actived_device; int is_stop_by_detached; + int transaction_id; + int transaction_command; }; static void hotplug_event_listen_callback(struct libusb_context *ctx, struct libusb_device *dev, int event); @@ -94,6 +99,8 @@ static struct sr_lib_context lib_ctx = { .callback_thread_count = 0, .is_delay_destory_actived_device = 0, .is_stop_by_detached = 0, + .transaction_id = 0, + .transaction_command = DEV_TRANS_NONE, }; /** @@ -778,7 +785,7 @@ END: } /** - * Stop collect data + * Stop collect data, but not close the device. */ SR_API int ds_stop_collect() { @@ -1118,7 +1125,7 @@ static int update_device_handle(struct libusb_device *old_dev, struct libusb_dev // Release the old device and the resource. if (dev == lib_ctx.actived_device_instance) { - sr_info("%s", "Release the old device's resource."); + sr_info("%s", "----------To release the old device's resource."); close_device_instance(dev); } @@ -1130,11 +1137,10 @@ static int update_device_handle(struct libusb_device *old_dev, struct libusb_dev dev->handle = new_dev; bFind = 1; - // Reopen the device. - if (dev == lib_ctx.actived_device_instance) - { - sr_info("%s", "Reopen the current device."); - open_device_instance(dev); + // Reopen the device by transaction. + if (dev == lib_ctx.actived_device_instance){ + lib_ctx.transaction_id++; + lib_ctx.transaction_command = DEV_TRANS_OPEN; } break; } @@ -1341,6 +1347,8 @@ static void usb_hotplug_process_proc() { sr_info("%s", "Hotplug thread start!"); + int cur_trans_id = DEV_TRANS_NONE; + while (!lib_ctx.lib_exit_flag) { sr_hotplug_wait_timout(lib_ctx.sr_ctx); @@ -1361,13 +1369,25 @@ static void usb_hotplug_process_proc() { lib_ctx.check_reconnect_times++; // 500ms - if (lib_ctx.check_reconnect_times == 5) + if (lib_ctx.check_reconnect_times == CHECK_DEV_RECONNECT_TIMES) { // Device loose contact,wait for it reconnection timeout. lib_ctx.detach_event_flag = 1; // use detach event lib_ctx.is_waitting_reconnect = 0; } } + + if (lib_ctx.transaction_id != cur_trans_id) + { + cur_trans_id = lib_ctx.transaction_id; + + if (lib_ctx.transaction_command == DEV_TRANS_OPEN){ + if (lib_ctx.actived_device_instance != NULL){ + sr_info("%s", "----------To reopen the current device."); + open_device_instance(lib_ctx.actived_device_instance); + } + } + } } if (lib_ctx.callback_thread_count > 0) diff --git a/libsigrok4DSL/libsigrok-internal.h b/libsigrok4DSL/libsigrok-internal.h index c772a8c1..3291b197 100644 --- a/libsigrok4DSL/libsigrok-internal.h +++ b/libsigrok4DSL/libsigrok-internal.h @@ -56,6 +56,12 @@ #define DS_VENDOR_ID 0x2A0E +enum device_transaction_types +{ + DEV_TRANS_NONE = 0, + DEV_TRANS_OPEN = 1 +}; + /** global variable */ extern char DS_RES_PATH[500]; extern struct ds_trigger *trigger; @@ -263,8 +269,7 @@ SR_PRIV struct sr_dev_inst *sr_dev_inst_new(int mode, int status, SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi); /* USB-specific instances */ -SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus, - uint8_t address, struct libusb_device_handle *hdl); +SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus, uint8_t address); SR_PRIV GSList *sr_usb_find_usbtmc(libusb_context *usb_ctx); SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb); diff --git a/libsigrok4DSL/libsigrok.h b/libsigrok4DSL/libsigrok.h index 43904cd1..04cc64c6 100644 --- a/libsigrok4DSL/libsigrok.h +++ b/libsigrok4DSL/libsigrok.h @@ -1428,7 +1428,7 @@ SR_API int ds_get_actived_device_mode(); SR_API int ds_start_collect(); /** - * Stop collect data + * Stop collect data, but not close the device. */ SR_API int ds_stop_collect();