diff --git a/hgdriver/hgdev/scanner_manager.cpp b/hgdriver/hgdev/scanner_manager.cpp index ef264fd..6193aca 100644 --- a/hgdriver/hgdev/scanner_manager.cpp +++ b/hgdriver/hgdev/scanner_manager.cpp @@ -103,6 +103,10 @@ void hg_scanner_mgr::clear(void) { if (hg_scanner_mgr::inst_) { + { + std::lock_guard lock(hg_scanner_mgr::inst_->mutex_dev_); + hg_scanner_mgr::event_callback_ = nullptr; + } delete hg_scanner_mgr::inst_; hg_scanner_mgr::inst_ = NULL; } @@ -131,6 +135,7 @@ void hg_scanner_mgr::on_hgscanner_pnp(usb_event ev, libusb_device* device, int v scanner_handle h = NULL; unsigned int len = sizeof(de); std::string name(""), type(""); + std::lock_guard lock(mutex_dev_); sprintf(model, "%x", pid); sprintf(vendor, "%x", vid); @@ -158,7 +163,6 @@ void hg_scanner_mgr::on_hgscanner_pnp(usb_event ev, libusb_device* device, int v if (index != -1) { - std::lock_guard lock(mutex_dev_); bool add = true; for (size_t i = 0; i < online_devices_.size(); ++i) { @@ -216,7 +220,6 @@ void hg_scanner_mgr::on_hgscanner_pnp(usb_event ev, libusb_device* device, int v } else if (ev == USB_EVENT_DEVICE_LEFT) { - std::lock_guard lock(mutex_dev_); std::vector::iterator it = std::find(online_devices_.begin(), online_devices_.end(), device); if (it != online_devices_.end()) { diff --git a/hgdriver/hgdev/usb_manager.cpp b/hgdriver/hgdev/usb_manager.cpp index 2a4c75a..456f376 100644 --- a/hgdriver/hgdev/usb_manager.cpp +++ b/hgdriver/hgdev/usb_manager.cpp @@ -408,7 +408,7 @@ bool usb_manager::get_device_info(libusb_device* device, usb_dev* devinfo) devinfo->ver = descriptor.bcdUSB; devinfo->vid = descriptor.idVendor; devinfo->pid = descriptor.idProduct; - devinfo->addr = libusb_get_device_address(device); + devinfo->addr = 0; // libusb_get_device_address(device); - initialize at open return true; } @@ -541,7 +541,7 @@ int usb_manager::register_hotplug(usb_event_handler cb, void* user) } init_notify_thread(); - return 0; + return HG_ERR_OK; } int usb_manager::open(libusb_device* device, usb_io** usbio, std::string* msg) { @@ -614,6 +614,8 @@ bool usb_io::make_singleton(void) key <<= 16; key |= dev_info_.pid; key <<= 16; + if (dev_info_.addr == 0) + dev_info_.addr = libusb_get_device_address(dev_info_.device); key |= dev_info_.addr; if (singleton_) singleton_->release(); diff --git a/hgsane/sane_hg_mdw.cpp b/hgsane/sane_hg_mdw.cpp index cea8186..1588672 100644 --- a/hgsane/sane_hg_mdw.cpp +++ b/hgsane/sane_hg_mdw.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #ifdef WIN32 #include #include @@ -491,6 +492,7 @@ namespace local_utility static sane_callback cb_ui_ = NULL; static void* cb_ui_parm_ = NULL; static SANE_Auth_Callback cb_auth_ = NULL; + static std::mutex cb_lock_; static std::string sane_event(SANE_Event ev) { @@ -516,6 +518,8 @@ namespace local_utility } int ui_cb(scanner_handle dev, int code, void* data, unsigned int* len, void* unused) { + std::lock_guard lck(cb_lock_); + if (SANE_EVENT_SUPPORT_ASYNC_IO == code) return cb_ui_ ? HG_ERR_OK : HG_ERR_DEVICE_NOT_SUPPORT; @@ -556,6 +560,7 @@ namespace local_utility } void stop_work(void) { + std::lock_guard lck(cb_lock_); cb_ui_ = NULL; cb_ui_parm_ = NULL; cb_auth_ = NULL; @@ -1085,11 +1090,11 @@ std::string hg_sane_middleware::option_value_2_string(SANE_Value_Type type, void ret = *(SANE_Bool*)val ? "true" : "false"; break; case SANE_TYPE_INT: - sprintf(buf, "%d ", *(SANE_Word*)val); + sprintf(buf, "%d", *(SANE_Word*)val); ret = buf; break; case SANE_TYPE_FIXED: - sprintf(buf, "%f ", hg_sane_middleware::sane_fixed_2_double(*(SANE_Word*)val)); + sprintf(buf, "%f", hg_sane_middleware::sane_fixed_2_double(*(SANE_Word*)val)); ret = buf; break; case SANE_TYPE_STRING: