2
0
forked from Ivasoft/DSView

update: Auto open the device when it reconnected

This commit is contained in:
dreamsourcelabTAI
2022-11-11 15:40:06 +08:00
parent 94a8e924f4
commit be2cb7f438
9 changed files with 55 additions and 25 deletions

View File

@@ -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;

View File

@@ -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;
}

View File

@@ -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;

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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);

View File

@@ -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)

View File

@@ -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);

View File

@@ -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();