From ae5c4e63c3e9b7217ee1598ec68dacd172cd1f8f Mon Sep 17 00:00:00 2001 From: dreamsourcelabTAI Date: Thu, 11 May 2023 10:36:52 +0800 Subject: [PATCH] The device can only be accessed by one process --- DSView/pv/mainwindow.cpp | 8 ++++---- DSView/pv/sigsession.cpp | 8 ++++++++ lang/cn/msg.json | 4 ++++ lang/en/msg.json | 4 ++++ libsigrok4DSL/hardware/DSL/dsl.c | 8 +++++--- libsigrok4DSL/lib_main.c | 26 ++++++++++++++++++++------ 6 files changed, 45 insertions(+), 13 deletions(-) diff --git a/DSView/pv/mainwindow.cpp b/DSView/pv/mainwindow.cpp index 43cc8d57..2e49e98b 100644 --- a/DSView/pv/mainwindow.cpp +++ b/DSView/pv/mainwindow.cpp @@ -1837,20 +1837,20 @@ namespace pv case DSV_MSG_NEW_USB_DEVICE: { + _sampling_bar->update_device_list(); + if (_session->get_device()->is_demo() == false) { QString msgText = L_S(STR_PAGE_MSG, S_ID(IDS_MSG_TO_SWITCH_DEVICE), "To switch the new device?"); - if (MsgBox::Confirm(msgText) == false){ - _sampling_bar->update_device_list(); // Update the list only. + if (MsgBox::Confirm(msgText) == false){ return; } } // The store confirm is not processed. if (_is_save_confirm_msg){ - _is_auto_switch_device = true; - _sampling_bar->update_device_list(); + _is_auto_switch_device = true; return; } diff --git a/DSView/pv/sigsession.cpp b/DSView/pv/sigsession.cpp index 17416664..1aa2bad8 100644 --- a/DSView/pv/sigsession.cpp +++ b/DSView/pv/sigsession.cpp @@ -225,6 +225,8 @@ namespace pv _device_status = ST_INIT; + ds_device_handle old_dev = _device_agent.handle(); + if (ds_active_device(dev_handle) != SR_OK) { dsv_err("%s", "Switch device error!"); @@ -258,6 +260,12 @@ namespace pv delay_prop_msg(strMsg); } + if (_device_agent.handle() != dev_handle && old_dev != NULL_HANDLE) + { + QString strMsg = L_S(STR_PAGE_MSG, S_ID(IDS_MSG_DEVICE_BUSY_SWITCH_FAILED), "Device is busy!"); + MsgBox::Show("",strMsg); + } + return true; } diff --git a/lang/cn/msg.json b/lang/cn/msg.json index 132fe0d7..22def5d1 100644 --- a/lang/cn/msg.json +++ b/lang/cn/msg.json @@ -366,5 +366,9 @@ { "id": "IDS_MSG_TO_RECONNECT_FOR_FIRMWARE", "text": "固件版本不对,请重新连接设备!" + }, + { + "id": "IDS_MSG_DEVICE_BUSY_SWITCH_FAILED", + "text": "设置在使用中,切换失败!" } ] \ No newline at end of file diff --git a/lang/en/msg.json b/lang/en/msg.json index 6efb4cc2..d5a55921 100644 --- a/lang/en/msg.json +++ b/lang/en/msg.json @@ -367,5 +367,9 @@ { "id": "IDS_MSG_TO_RECONNECT_FOR_FIRMWARE", "text": "The firmware version is incorrect, please reconnect the device!" + }, + { + "id": "IDS_MSG_DEVICE_BUSY_SWITCH_FAILED", + "text": "The device is busy,switch failed!" } ] \ No newline at end of file diff --git a/libsigrok4DSL/hardware/DSL/dsl.c b/libsigrok4DSL/hardware/DSL/dsl.c index 9b71b1b7..9e544bba 100644 --- a/libsigrok4DSL/hardware/DSL/dsl.c +++ b/libsigrok4DSL/hardware/DSL/dsl.c @@ -1826,7 +1826,8 @@ SR_PRIV int dsl_dev_open(struct sr_dev_driver *di, struct sr_dev_inst *sdi, gboo assert(usb->devhdl); ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE); - if (ret != 0) { + + if (ret != LIBUSB_SUCCESS) { switch(ret) { case LIBUSB_ERROR_BUSY: sr_err("%s: Unable to claim USB interface. Another " @@ -1848,12 +1849,13 @@ SR_PRIV int dsl_dev_open(struct sr_dev_driver *di, struct sr_dev_inst *sdi, gboo break; } - if (ret != 0 && fdError == 1){ + if (ret != LIBUSB_SUCCESS && fdError == 1){ sr_err("%s: Unable to claim interface, the second time: %s.", __func__, libusb_error_name(ret)); } - if (ret != 0){ + if (ret != LIBUSB_SUCCESS){ + dsl_dev_close(sdi); return SR_ERR; } } diff --git a/libsigrok4DSL/lib_main.c b/libsigrok4DSL/lib_main.c index 675388f7..cfc8e003 100644 --- a/libsigrok4DSL/lib_main.c +++ b/libsigrok4DSL/lib_main.c @@ -341,12 +341,15 @@ SR_API int ds_active_device(ds_device_handle handle) struct sr_dev_inst *dev; int bFind = 0; int ret; + struct sr_dev_inst *old_dev; if (handle == NULL_HANDLE) { return SR_ERR_ARG; } + ret = SR_OK; + old_dev = NULL; sr_info("%s", "Start activating device."); @@ -371,6 +374,7 @@ SR_API int ds_active_device(ds_device_handle handle) { sr_info("Close the previous device \"%s\"", lib_ctx.actived_device_instance->name); close_device_instance(lib_ctx.actived_device_instance); + old_dev = lib_ctx.actived_device_instance; } } @@ -391,14 +395,25 @@ SR_API int ds_active_device(ds_device_handle handle) else sr_info("device name: \"%s\".", dev->name); - if (open_device_instance(dev) == SR_OK) + ret = open_device_instance(dev); + + if (ret == SR_OK) { lib_ctx.actived_device_instance = dev; } else - { - sr_err("%s", "Open device error!"); - ret = SR_ERR_CALL_STATUS; + { + if (old_dev != NULL && old_dev != dev){ + sr_err("%s", "Open device error! the current device switch failed."); + lib_ctx.actived_device_instance = old_dev; + ret = open_device_instance(old_dev); + } + else if(lib_ctx.device_list != NULL){ + old_dev = lib_ctx.device_list->data; + sr_err("%s", "Open device error! the current device switch to demo."); + lib_ctx.actived_device_instance = old_dev; + ret = open_device_instance(old_dev); + } } break; } @@ -1493,8 +1508,7 @@ static int open_device_instance(struct sr_dev_inst *dev) if (driver_ins->dev_open) { - driver_ins->dev_open(dev); - return SR_OK; + return driver_ins->dev_open(dev); } return SR_ERR_CALL_STATUS;