diff --git a/hgdriver/hgdev/usb_manager.cpp b/hgdriver/hgdev/usb_manager.cpp index e720ecf..d9f5c1f 100644 --- a/hgdriver/hgdev/usb_manager.cpp +++ b/hgdriver/hgdev/usb_manager.cpp @@ -73,7 +73,7 @@ int LIBUSB_CALL usb_manager::usb_pnp_callback(libusb_context* ctx, libusb_device usb_manager* obj = (usb_manager*)monitor; // if (event == LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED) - libusb_ref_device(device); // keep the object until handle it + //libusb_ref_device(device); // keep the object until handle it //else if(event == LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT) // libusb_unref_device(device); @@ -190,17 +190,22 @@ void usb_manager::thread_notify_usb_event() } } + libusb_ref_device(pd.dev); // for re-enter the queue pnp_events_ notify_usb_event(pd, &retry); if (retry) { if(std::chrono::duration_cast(std::chrono::system_clock::now() - pd.happen_time).count() <= 5000) pnp_events_.Put(pd, sizeof(pd)); + else + retry = false; if(pnp_events_.Size() == 1) this_thread::sleep_for(chrono::milliseconds(1000)); else this_thread::sleep_for(chrono::milliseconds(delay)); } + if (!retry) + libusb_unref_device(pd.dev); } } } @@ -295,6 +300,7 @@ int usb_manager::on_usb_pnp_event(libusb_context *ctx, libusb_device *device, li PNPDEV pd; unsigned ms = std::chrono::duration_cast(std::chrono::system_clock::now() - born_).count(); + libusb_ref_device(device); pd.ctx = ctx; pd.dev = device; pd.event = event; @@ -307,9 +313,12 @@ int usb_manager::on_usb_pnp_event(libusb_context *ctx, libusb_device *device, li else { bool retry = false; + libusb_ref_device(device); notify_usb_event(pd, &retry); if(retry) pnp_events_.Put(pd, sizeof(pd)); + else + libusb_unref_device(device); } return 0; @@ -779,6 +788,23 @@ void usb_io::init_after_open(void) close(); last_err_ = err; } + else + { + libusb_device_descriptor dd = { 0 }; + char product[128] = { 0 }; + + if (libusb_get_device_descriptor(dev_info_.device, &dd) == LIBUSB_SUCCESS) + { + libusb_get_string_descriptor_ascii(handle_, dd.iProduct, (unsigned char*)product, _countof(product) - 1); + utils::to_log(LOG_LEVEL_DEBUG, "Product: %s\n", product); + + char* v = strstr(product, "(V"); + if (v) + { + ver_ = atoi(v + 2); + } + } + } } void usb_io::open(void) { @@ -1113,6 +1139,10 @@ int usb_io::get_pid(void) { return dev_info_.pid; } +int usb_io::get_ver(void) +{ + return ver_; +} void usb_io::on_disconnected(void) { diff --git a/hgdriver/hgdev/usb_manager.h b/hgdriver/hgdev/usb_manager.h index ab04faf..1951794 100644 --- a/hgdriver/hgdev/usb_manager.h +++ b/hgdriver/hgdev/usb_manager.h @@ -153,6 +153,7 @@ class usb_io int last_err_; std::string init_err_msg_; libusb_device *ref_device_; + int ver_ = 1; // endpoint ports usb_manager::USBTRANSENDP endpoints_;