add check device version according device configuration iProduct

This commit is contained in:
gb 2023-12-02 11:06:40 +08:00
parent 51b93c08df
commit 53d1a7184d
2 changed files with 32 additions and 1 deletions

View File

@ -73,7 +73,7 @@ int LIBUSB_CALL usb_manager::usb_pnp_callback(libusb_context* ctx, libusb_device
usb_manager* obj = (usb_manager*)monitor; usb_manager* obj = (usb_manager*)monitor;
// if (event == LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED) // 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) //else if(event == LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT)
// libusb_unref_device(device); // 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); notify_usb_event(pd, &retry);
if (retry) if (retry)
{ {
if(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now() - pd.happen_time).count() if(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now() - pd.happen_time).count()
<= 5000) <= 5000)
pnp_events_.Put(pd, sizeof(pd)); pnp_events_.Put(pd, sizeof(pd));
else
retry = false;
if(pnp_events_.Size() == 1) if(pnp_events_.Size() == 1)
this_thread::sleep_for(chrono::milliseconds(1000)); this_thread::sleep_for(chrono::milliseconds(1000));
else else
this_thread::sleep_for(chrono::milliseconds(delay)); 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; PNPDEV pd;
unsigned ms = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now() - born_).count(); unsigned ms = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now() - born_).count();
libusb_ref_device(device);
pd.ctx = ctx; pd.ctx = ctx;
pd.dev = device; pd.dev = device;
pd.event = event; pd.event = event;
@ -307,9 +313,12 @@ int usb_manager::on_usb_pnp_event(libusb_context *ctx, libusb_device *device, li
else else
{ {
bool retry = false; bool retry = false;
libusb_ref_device(device);
notify_usb_event(pd, &retry); notify_usb_event(pd, &retry);
if(retry) if(retry)
pnp_events_.Put(pd, sizeof(pd)); pnp_events_.Put(pd, sizeof(pd));
else
libusb_unref_device(device);
} }
return 0; return 0;
@ -779,6 +788,23 @@ void usb_io::init_after_open(void)
close(); close();
last_err_ = err; 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) void usb_io::open(void)
{ {
@ -1113,6 +1139,10 @@ int usb_io::get_pid(void)
{ {
return dev_info_.pid; return dev_info_.pid;
} }
int usb_io::get_ver(void)
{
return ver_;
}
void usb_io::on_disconnected(void) void usb_io::on_disconnected(void)
{ {

View File

@ -153,6 +153,7 @@ class usb_io
int last_err_; int last_err_;
std::string init_err_msg_; std::string init_err_msg_;
libusb_device *ref_device_; libusb_device *ref_device_;
int ver_ = 1;
// endpoint ports // endpoint ports
usb_manager::USBTRANSENDP endpoints_; usb_manager::USBTRANSENDP endpoints_;