diff --git a/device/win_usb/win_usb.cpp b/device/win_usb/win_usb.cpp index 621d565..bfc80fc 100644 --- a/device/win_usb/win_usb.cpp +++ b/device/win_usb/win_usb.cpp @@ -545,6 +545,8 @@ bool usb_device::is_online(void) void usb_device::set_online(bool online) { online_ = online; + if (!online_) + reg_key_ = ""; } uint8_t usb_device::port(void) { @@ -552,6 +554,10 @@ uint8_t usb_device::port(void) init(); return port_; } +std::string usb_device::reg_path(void) +{ + return reg_key_; +} bool usb_device::init(void) { @@ -1068,7 +1074,7 @@ void usb_monitor::notify_usb_event(usb_device*& dev, bool arrive) if (devices_[i]->is_online()) { VLOG_MINI_1(LOG_LEVEL_DEBUG_INFO, "%s is already in device queue and received ARRIVE again, discard this event.\n", dev->name().c_str()); - dev->release(); + // dev->release(); return; } else @@ -1090,31 +1096,43 @@ void usb_monitor::notify_usb_event(usb_device*& dev, bool arrive) } else { + bool discard = false; for (size_t i = 0; i < devices_.size(); ++i) { - if (!(devices_[i]->id() == id)) + if (devices_[i]->id().vid != id.vid || devices_[i]->id().pid != id.pid) continue; - - dev->release(); - dev = devices_[i]; - dev->add_ref(); - if (dev->is_open()) + if (devices_[i]->reg_path().empty() || usb_device::usb_scan_name(devices_[i]->reg_path().c_str()).empty()) { - if (dev->is_online()) - dev->set_online(false); - else - { - VLOG_MINI_1(LOG_LEVEL_DEBUG_INFO, "%s is already offline and received LEAVE again, discard this event.\n", dev->name().c_str()); - dev->release(); - return; - } - } - else - { - devices_.erase(devices_.begin() + i); dev->release(); + dev = devices_[i]; + dev->add_ref(); + // if (dev->is_open()) + { + if (dev->is_online()) + { + dev->set_online(false); + discard = false; + break; + } + else + { + // VLOG_MINI_1(LOG_LEVEL_DEBUG_INFO, "%s is already offline and received LEAVE again, discard this event.\n", dev->name().c_str()); + // dev->release(); + // return; + discard = true; + } + } + //else + //{ + // devices_.erase(devices_.begin() + i); + // dev->release(); + //} } - break; + } + if (discard) + { + VLOG_MINI_1(LOG_LEVEL_DEBUG_INFO, "%s is already offline and received LEAVE again, discard this event.\n", dev->name().c_str()); + return; } } diff --git a/device/win_usb/win_usb.h b/device/win_usb/win_usb.h index 4c50f5b..06121ba 100644 --- a/device/win_usb/win_usb.h +++ b/device/win_usb/win_usb.h @@ -140,6 +140,7 @@ public: bool is_online(void); void set_online(bool online); uint8_t port(void); + std::string reg_path(void); bool init(void); void clear(void);