回退 win_usb.cpp
This commit is contained in:
parent
63acafc433
commit
f0b05abd2e
|
@ -10,7 +10,6 @@
|
|||
//#include <winioctl.h>
|
||||
#include <usbscan.h>
|
||||
#include <Dbt.h>
|
||||
#include <ntstatus.h> // for STATUS_CANCELLED - 0xC0000120
|
||||
|
||||
#pragma comment(lib, "setupapi.lib")
|
||||
#pragma comment(lib, "hid.lib")
|
||||
|
@ -51,16 +50,16 @@ std::string u2utf8(const wchar_t* u)
|
|||
#if defined(OEM_NONE) || defined(OEM_LISCHENG) || defined(OEM_HANWANG) || defined(OEM_ZHONGJING)
|
||||
return hg_log::u2utf8(u);
|
||||
#else
|
||||
int len = WideCharToMultiByte(CP_UTF8, 0, u, lstrlenW(u), NULL, 0, NULL, NULL);
|
||||
char *ansi = new char[len + 4];
|
||||
int len = WideCharToMultiByte(CP_UTF8, 0, u, lstrlenW(u), NULL, 0, NULL, NULL);
|
||||
char* ansi = new char[len + 4];
|
||||
|
||||
len = WideCharToMultiByte(CP_UTF8, 0, u, lstrlenW(u), ansi, len, NULL, NULL);
|
||||
ansi[len--] = 0;
|
||||
|
||||
std::string utf8(ansi);
|
||||
delete[] ansi;
|
||||
len = WideCharToMultiByte(CP_UTF8, 0, u, lstrlenW(u), ansi, len, NULL, NULL);
|
||||
ansi[len--] = 0;
|
||||
|
||||
return utf8;
|
||||
std::string utf8(ansi);
|
||||
delete[] ansi;
|
||||
|
||||
return utf8;
|
||||
#endif
|
||||
}
|
||||
std::wstring ansi2unicode(const char* ansi, UINT cp = CP_ACP)
|
||||
|
@ -79,7 +78,7 @@ std::wstring ansi2unicode(const char* ansi, UINT cp = CP_ACP)
|
|||
}
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// OVERLAPPED ...
|
||||
ovl_cls::ovl_cls(uint32_t type) : ref_(1), io_bytes_(0), type_(type)
|
||||
ovl_cls::ovl_cls() : ref_(1), io_bytes_(0)
|
||||
{
|
||||
memset(&ovl_, 0, sizeof(ovl_));
|
||||
ovl_.hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
|
||||
|
@ -110,14 +109,6 @@ bool ovl_cls::is_waited(void)
|
|||
{
|
||||
return WaitForSingleObject(ovl_.hEvent, 0) == WAIT_OBJECT_0;
|
||||
}
|
||||
void ovl_cls::notify(void)
|
||||
{
|
||||
SetEvent(ovl_.hEvent);
|
||||
}
|
||||
uint32_t ovl_cls::type(void)
|
||||
{
|
||||
return type_;
|
||||
}
|
||||
|
||||
ovl_mgr::ovl_mgr()
|
||||
{}
|
||||
|
@ -127,14 +118,14 @@ ovl_mgr::~ovl_mgr()
|
|||
v->release();
|
||||
}
|
||||
|
||||
ovl_cls* ovl_mgr::get_ovl(uint32_t type)
|
||||
ovl_cls* ovl_mgr::get_ovl(void)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(lock_);
|
||||
ovl_cls* o = NULL;
|
||||
|
||||
for (auto& v : ovls_)
|
||||
{
|
||||
if (v->is_waited() && v->type() == type)
|
||||
if (v->is_waited())
|
||||
{
|
||||
o = v;
|
||||
o->add_ref();
|
||||
|
@ -145,23 +136,18 @@ ovl_cls* ovl_mgr::get_ovl(uint32_t type)
|
|||
|
||||
if (!o)
|
||||
{
|
||||
o = new ovl_cls(type);
|
||||
o = new ovl_cls();
|
||||
ovls_.push_back(o);
|
||||
o->add_ref();
|
||||
}
|
||||
|
||||
return o;
|
||||
}
|
||||
void ovl_mgr::notify_all(void)
|
||||
{
|
||||
for (auto& v : ovls_)
|
||||
v->notify();
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// usb_device ...
|
||||
usb_device::usb_device(const USBDEV& dev) : ref_(1), udev_(dev), is_ok_(false)
|
||||
, dev_desc_(NULL), handle_(NULL), online_(true), timout_ms_(1000)
|
||||
, dev_desc_(NULL), handle_(NULL), online_(true), timout_ms_(1000)
|
||||
{
|
||||
memset(&guid_, 0, sizeof(guid_));
|
||||
}
|
||||
|
@ -200,7 +186,7 @@ int usb_device::set_timeout(HANDLE h)
|
|||
DWORD cbr = 0;
|
||||
|
||||
uto.TimeoutEvent = uto.TimeoutRead = uto.TimeoutWrite = (timout_ms_ + 500) / 1000;
|
||||
|
||||
|
||||
return DeviceIoControl(h, IOCTL_SET_TIMEOUT, &uto, sizeof(uto), NULL, 0, &cbr, NULL) ? LIBUSB_SUCCESS : LIBUSB_ERROR_IO;
|
||||
|
||||
//COMMTIMEOUTS to = { 0 };
|
||||
|
@ -213,40 +199,40 @@ int usb_device::set_timeout(HANDLE h)
|
|||
|
||||
void usb_device::vid_pid_from_name(const char* name, int* vid, int* pid)
|
||||
{
|
||||
// name: \\?\usb#vid_3072&pid_0239#01234567aabbccddee#{a5dcbf10-6530-11d2-901f-00c04fb951ed}
|
||||
std::string s(name ? name : "");
|
||||
size_t pos = 0;
|
||||
// name: \\?\usb#vid_3072&pid_0239#01234567aabbccddee#{a5dcbf10-6530-11d2-901f-00c04fb951ed}
|
||||
std::string s(name ? name : "");
|
||||
size_t pos = 0;
|
||||
|
||||
std::transform(s.begin(), s.end(), s.begin(), tolower);
|
||||
pos = s.find("vid_");
|
||||
if (pos != std::string::npos && vid)
|
||||
*vid = usb_device::from_hex_string(s.c_str() + pos + 4);
|
||||
std::transform(s.begin(), s.end(), s.begin(), tolower);
|
||||
pos = s.find("vid_");
|
||||
if (pos != std::string::npos && vid)
|
||||
*vid = usb_device::from_hex_string(s.c_str() + pos + 4);
|
||||
|
||||
pos = s.find("pid_");
|
||||
if (pos != std::string::npos && pid)
|
||||
*pid = usb_device::from_hex_string(s.c_str() + pos + 4);
|
||||
pos = s.find("pid_");
|
||||
if (pos != std::string::npos && pid)
|
||||
*pid = usb_device::from_hex_string(s.c_str() + pos + 4);
|
||||
}
|
||||
DWORD usb_device::from_hex_string(const char* hex_str)
|
||||
{
|
||||
DWORD v = 0;
|
||||
DWORD v = 0;
|
||||
|
||||
for (int i = 0; hex_str[i]; ++i)
|
||||
{
|
||||
DWORD now = 0;
|
||||
if (hex_str[i] >= '0' && hex_str[i] <= '9')
|
||||
now = hex_str[i] - '0';
|
||||
else if (hex_str[i] >= 'a' && hex_str[i] <= 'f')
|
||||
now = hex_str[i] - 'a' + 10;
|
||||
else if (hex_str[i] >= 'A' && hex_str[i] <= 'F')
|
||||
now = hex_str[i] - 'A' + 10;
|
||||
else
|
||||
break;
|
||||
for (int i = 0; hex_str[i]; ++i)
|
||||
{
|
||||
DWORD now = 0;
|
||||
if (hex_str[i] >= '0' && hex_str[i] <= '9')
|
||||
now = hex_str[i] - '0';
|
||||
else if (hex_str[i] >= 'a' && hex_str[i] <= 'f')
|
||||
now = hex_str[i] - 'a' + 10;
|
||||
else if (hex_str[i] >= 'A' && hex_str[i] <= 'F')
|
||||
now = hex_str[i] - 'A' + 10;
|
||||
else
|
||||
break;
|
||||
|
||||
v <<= 4;
|
||||
v += now;
|
||||
}
|
||||
v <<= 4;
|
||||
v += now;
|
||||
}
|
||||
|
||||
return v;
|
||||
return v;
|
||||
}
|
||||
std::string usb_device::name_without_guid(const char* name)
|
||||
{
|
||||
|
@ -269,7 +255,7 @@ std::string usb_device::usb_scan_name(const char* reg_key)
|
|||
{
|
||||
char val[256] = { 0 };
|
||||
DWORD len = _countof(val) - 1,
|
||||
type = REG_SZ;
|
||||
type = REG_SZ;
|
||||
if (RegQueryValueExW(key, L"CreateFileName", NULL, &type, (LPBYTE)val, &len) == ERROR_SUCCESS)
|
||||
{
|
||||
val[len] = 0;
|
||||
|
@ -316,7 +302,7 @@ GUID usb_device::guid(void)
|
|||
}
|
||||
bool usb_device::is_ok(void)
|
||||
{
|
||||
return is_ok_;
|
||||
return is_ok_;
|
||||
}
|
||||
bool usb_device::is_open(void)
|
||||
{
|
||||
|
@ -495,47 +481,47 @@ void usb_device::clear(void)
|
|||
}
|
||||
int usb_device::get_descriptor(libusb_device_descriptor* desc)
|
||||
{
|
||||
if (dev_desc_)
|
||||
memcpy(desc, dev_desc_, sizeof(*desc));
|
||||
else
|
||||
{
|
||||
char cls[128] = { 0 };
|
||||
if (dev_desc_)
|
||||
memcpy(desc, dev_desc_, sizeof(*desc));
|
||||
else
|
||||
{
|
||||
char cls[128] = { 0 };
|
||||
|
||||
SetupDiGetClassDescriptionA(&guid_, cls, _countof(cls) - 1, NULL);
|
||||
std::transform(cls, cls + lstrlenA(cls), cls, tolower);
|
||||
if (strcmp(cls, "usb") == 0)
|
||||
desc->bDeviceClass = libusb_class_code::LIBUSB_CLASS_HUB;
|
||||
else if (strcmp(cls, "image") == 0)
|
||||
desc->bDeviceClass = libusb_class_code::LIBUSB_CLASS_IMAGE;
|
||||
else if (strcmp(cls, "hidclass") == 0)
|
||||
desc->bDeviceClass = libusb_class_code::LIBUSB_CLASS_HID;
|
||||
else
|
||||
desc->bDeviceClass = libusb_class_code::LIBUSB_CLASS_VENDOR_SPEC;
|
||||
desc->idVendor = udev_.vid;
|
||||
desc->idProduct = udev_.pid;
|
||||
desc->bcdUSB = 0x200; // USB2.0 ?
|
||||
desc->bcdDevice = 0; // ?
|
||||
desc->bDescriptorType = libusb_descriptor_type::LIBUSB_DT_DEVICE;
|
||||
desc->bDeviceProtocol = 0;
|
||||
desc->bDeviceSubClass = libusb_class_code::LIBUSB_CLASS_IMAGE;
|
||||
desc->bLength = sizeof(*desc);
|
||||
desc->bMaxPacketSize0 = 512;
|
||||
desc->bNumConfigurations = 1; // ?
|
||||
desc->iManufacturer = 0;
|
||||
desc->iSerialNumber = 0;
|
||||
desc->iProduct = 0;
|
||||
}
|
||||
|
||||
return LIBUSB_SUCCESS;
|
||||
SetupDiGetClassDescriptionA(&guid_, cls, _countof(cls) - 1, NULL);
|
||||
std::transform(cls, cls + lstrlenA(cls), cls, tolower);
|
||||
if (strcmp(cls, "usb") == 0)
|
||||
desc->bDeviceClass = libusb_class_code::LIBUSB_CLASS_HUB;
|
||||
else if (strcmp(cls, "image") == 0)
|
||||
desc->bDeviceClass = libusb_class_code::LIBUSB_CLASS_IMAGE;
|
||||
else if (strcmp(cls, "hidclass") == 0)
|
||||
desc->bDeviceClass = libusb_class_code::LIBUSB_CLASS_HID;
|
||||
else
|
||||
desc->bDeviceClass = libusb_class_code::LIBUSB_CLASS_VENDOR_SPEC;
|
||||
desc->idVendor = udev_.vid;
|
||||
desc->idProduct = udev_.pid;
|
||||
desc->bcdUSB = 0x200; // USB2.0 ?
|
||||
desc->bcdDevice = 0; // ?
|
||||
desc->bDescriptorType = libusb_descriptor_type::LIBUSB_DT_DEVICE;
|
||||
desc->bDeviceProtocol = 0;
|
||||
desc->bDeviceSubClass = libusb_class_code::LIBUSB_CLASS_IMAGE;
|
||||
desc->bLength = sizeof(*desc);
|
||||
desc->bMaxPacketSize0 = 512;
|
||||
desc->bNumConfigurations = 1; // ?
|
||||
desc->iManufacturer = 0;
|
||||
desc->iSerialNumber = 0;
|
||||
desc->iProduct = 0;
|
||||
}
|
||||
|
||||
return LIBUSB_SUCCESS;
|
||||
}
|
||||
int usb_device::get_config_descriptor(int index, libusb_config_descriptor** desc)
|
||||
{
|
||||
if (index >= 0 && index < cfg_desc_.size())
|
||||
*desc = cfg_desc_[index];
|
||||
else
|
||||
return LIBUSB_ERROR_INVALID_PARAM;
|
||||
if (index >= 0 && index < cfg_desc_.size())
|
||||
*desc = cfg_desc_[index];
|
||||
else
|
||||
return LIBUSB_ERROR_INVALID_PARAM;
|
||||
|
||||
return LIBUSB_SUCCESS;
|
||||
return LIBUSB_SUCCESS;
|
||||
}
|
||||
int usb_device::open(libusb_device_handle** dev_handle)
|
||||
{
|
||||
|
@ -545,14 +531,20 @@ int usb_device::open(libusb_device_handle** dev_handle)
|
|||
if (!dev_desc_)
|
||||
init();
|
||||
|
||||
HANDLE h = INVALID_HANDLE_VALUE;
|
||||
HANDLE h = open_usb(udev_.name.c_str());
|
||||
if (h == INVALID_HANDLE_VALUE)
|
||||
{
|
||||
*dev_handle = NULL;
|
||||
|
||||
return online_ ? LIBUSB_ERROR_IO : LIBUSB_ERROR_NO_DEVICE;
|
||||
}
|
||||
|
||||
USBSCAN_PIPE_CONFIGURATION upc = { 0 };
|
||||
DWORD cbr = 0;
|
||||
std::string fmt("\\%d"), root("");
|
||||
|
||||
if (udev_.driver_key.length())
|
||||
root = usb_device::usb_scan_name(udev_.driver_key.c_str());//name使用root时会导致 IO一个异常
|
||||
root = usb_device::usb_scan_name(udev_.driver_key.c_str());
|
||||
if (root.empty())
|
||||
{
|
||||
VLOG_MINI_1(LOG_LEVEL_WARNING, "Cannot find '\\\\.\\Usbscan' name for '%s', try run in Administrator!\r\n", udev_.name.c_str());
|
||||
|
@ -563,13 +555,6 @@ int usb_device::open(libusb_device_handle** dev_handle)
|
|||
{
|
||||
VLOG_MINI_2(LOG_LEVEL_WARNING, "Nice: '%s' for '%s'.\r\n", root.c_str(), udev_.name.c_str());
|
||||
}
|
||||
h = open_usb(udev_.name.c_str());
|
||||
if (h == INVALID_HANDLE_VALUE)
|
||||
{
|
||||
*dev_handle = NULL;
|
||||
|
||||
return online_ ? LIBUSB_ERROR_IO : LIBUSB_ERROR_NO_DEVICE;
|
||||
}
|
||||
if (DeviceIoControl(h, IOCTL_GET_PIPE_CONFIGURATION, NULL, 0, &upc, sizeof(upc), &cbr, NULL))
|
||||
{
|
||||
int type = PIPE_TYPE::WRITE_DATA_PIPE;
|
||||
|
@ -603,7 +588,6 @@ int usb_device::close(void)
|
|||
CancelIo(pipes_[i].pipe);
|
||||
CloseHandle(pipes_[i].pipe);
|
||||
}
|
||||
ovl_mgr_.notify_all();
|
||||
pipes_.clear();
|
||||
|
||||
if (handle_)
|
||||
|
@ -643,15 +627,15 @@ int usb_device::transfer_bulk(unsigned endpoint, unsigned char* data, int* lengt
|
|||
|
||||
if (h)
|
||||
{
|
||||
ovl_cls* oc = ovl_mgr_.get_ovl(endpoint);
|
||||
ovl_cls* oc = ovl_mgr_.get_ovl();
|
||||
DWORD io = 0;
|
||||
BOOL result = FALSE;
|
||||
|
||||
|
||||
if (endpoint & BULKIN_FLAG)
|
||||
result = ReadFile(h, data, *length, oc->io_bytes(), oc->over_lapped());
|
||||
else
|
||||
{
|
||||
// oc->over_lapped()->Offset = oc->over_lapped()->OffsetHigh = -1;
|
||||
// oc->over_lapped()->Offset = oc->over_lapped()->OffsetHigh = -1;
|
||||
result = WriteFile(h, data, *length, oc->io_bytes(), oc->over_lapped());
|
||||
}
|
||||
|
||||
|
@ -668,13 +652,11 @@ int usb_device::transfer_bulk(unsigned endpoint, unsigned char* data, int* lengt
|
|||
{
|
||||
if (WaitForSingleObject(oc->over_lapped()->hEvent, timeout) == WAIT_OBJECT_0)
|
||||
{
|
||||
GetOverlappedResult(h, oc->over_lapped(), oc->io_bytes(), TRUE);
|
||||
GetOverlappedResult(h, oc->over_lapped(), oc->io_bytes(), FALSE);
|
||||
*length = *oc->io_bytes();
|
||||
if (*length == 0 && oc->over_lapped()->Internal != ERROR_SUCCESS)
|
||||
{
|
||||
ret = oc->over_lapped()->Internal == STATUS_CANCELLED ? /*LIBUSB_ERROR_TRY_AGAIN*/LIBUSB_ERROR_INTERRUPTED : oc->over_lapped()->Internal;
|
||||
if (ret == ERROR_NO_MORE_ITEMS)
|
||||
ret = LIBUSB_ERROR_TIMEOUT;
|
||||
ret = LIBUSB_ERROR_IO;
|
||||
VLOG_MINI_2(LOG_LEVEL_WARNING, "Bulk-Transfer of endpoint 0x%02x failed with code 0x%08X\n", endpoint, oc->over_lapped()->Internal);
|
||||
}
|
||||
else
|
||||
|
@ -710,7 +692,7 @@ int usb_device::transfer_control(uint8_t type, uint8_t req, uint16_t val, uint16
|
|||
if ((HANDLE)handle_ != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
DWORD io = 0;
|
||||
ovl_cls *oc = ovl_mgr_.get_ovl(0);
|
||||
ovl_cls* oc = ovl_mgr_.get_ovl();
|
||||
|
||||
irp.bmRequestType = (type >> 5) & 0x03;
|
||||
irp.bRequest = req;
|
||||
|
@ -723,7 +705,7 @@ int usb_device::transfer_control(uint8_t type, uint8_t req, uint16_t val, uint16
|
|||
{
|
||||
if (irp.fTransferDirectionIn)
|
||||
{
|
||||
GetOverlappedResult((HANDLE)handle_, oc->over_lapped(), oc->io_bytes(), TRUE);
|
||||
GetOverlappedResult((HANDLE)handle_, oc->over_lapped(), oc->io_bytes(), FALSE);
|
||||
ret = *oc->io_bytes();
|
||||
}
|
||||
else
|
||||
|
@ -756,11 +738,11 @@ int usb_device::transfer_interrupt(unsigned endpoint, unsigned char* data, int*
|
|||
int ret = LIBUSB_ERROR_NOT_SUPPORTED, len = *length;
|
||||
HANDLE h = find_pipe(endpoint, USBSCAN_PIPE_INTERRUPT);
|
||||
DWORD io = 0;
|
||||
|
||||
|
||||
*length = 0;
|
||||
if (h)
|
||||
{
|
||||
ovl_cls* oc = ovl_mgr_.get_ovl(endpoint);
|
||||
ovl_cls* oc = ovl_mgr_.get_ovl();
|
||||
if (DeviceIoControl(h, IOCTL_WAIT_ON_DEVICE_EVENT, NULL, 0, data, len, oc->io_bytes(), oc->over_lapped()))
|
||||
{
|
||||
ret = LIBUSB_SUCCESS;
|
||||
|
@ -773,7 +755,7 @@ int usb_device::transfer_interrupt(unsigned endpoint, unsigned char* data, int*
|
|||
{
|
||||
if (WaitForSingleObject(oc->over_lapped()->hEvent, timeout) == WAIT_OBJECT_0)
|
||||
{
|
||||
GetOverlappedResult(h, oc->over_lapped(), oc->io_bytes(), TRUE);
|
||||
GetOverlappedResult(h, oc->over_lapped(), oc->io_bytes(), FALSE);
|
||||
*length = *oc->io_bytes();
|
||||
ret = LIBUSB_SUCCESS;
|
||||
}
|
||||
|
@ -797,17 +779,6 @@ int usb_device::transfer_interrupt(unsigned endpoint, unsigned char* data, int*
|
|||
return ret;
|
||||
}
|
||||
|
||||
int usb_device::cancel_io(void)
|
||||
{
|
||||
for (auto& v : pipes_)
|
||||
{
|
||||
CancelIo(v.pipe);
|
||||
}
|
||||
ovl_mgr_.notify_all();
|
||||
|
||||
return LIBUSB_SUCCESS;
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// usb_monitor ...
|
||||
|
||||
|
@ -859,7 +830,7 @@ int usb_monitor::enum_usb_device(bool(__stdcall* found_usb)(LPUSBDEV dev, void*
|
|||
if (SetupDiGetDeviceRegistryPropertyW(dev_info, &sdd, SPDRP_DRIVER, NULL, (PBYTE)buf->DevicePath, len, &len)) // driver key
|
||||
dev.driver_key = u2utf8(buf->DevicePath);
|
||||
len = size;
|
||||
if(SetupDiGetDeviceRegistryPropertyW(dev_info, &sdd, SPDRP_DEVICEDESC, NULL, (PBYTE)buf->DevicePath, len, &len)) // device description
|
||||
if (SetupDiGetDeviceRegistryPropertyW(dev_info, &sdd, SPDRP_DEVICEDESC, NULL, (PBYTE)buf->DevicePath, len, &len)) // device description
|
||||
dev.desc = u2utf8(buf->DevicePath);
|
||||
len = size;
|
||||
if (SetupDiGetDeviceRegistryPropertyW(dev_info, &sdd, SPDRP_ADDRESS, NULL, (PBYTE)buf->DevicePath, len, &len)) // device description
|
||||
|
@ -981,7 +952,7 @@ void usb_monitor::notify_usb_event(usb_device*& dev, bool arrive)
|
|||
if (arrive)
|
||||
{
|
||||
bool found = false;
|
||||
for(size_t i = 0; i < devices_.size(); ++i)
|
||||
for (size_t i = 0; i < devices_.size(); ++i)
|
||||
{
|
||||
// if (devices_[i]->name() == dev->name())
|
||||
if (stricmp(usb_device::name_without_guid(devices_[i]->name().c_str()).c_str(), noguid.c_str()) == 0)
|
||||
|
@ -989,7 +960,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
|
||||
|
@ -1054,14 +1025,14 @@ int usb_monitor::on_usb_pnp(WPARAM wp, LPARAM lp)
|
|||
return wp == DBT_DEVICEQUERYREMOVE;
|
||||
|
||||
PDEV_BROADCAST_DEVICEINTERFACE_W dev = (PDEV_BROADCAST_DEVICEINTERFACE_W)lp;
|
||||
if (dev->dbcc_classguid == GUID_DEVINTERFACE_USB_DEVICE)
|
||||
{
|
||||
return wp == DBT_DEVICEQUERYREMOVE;
|
||||
}
|
||||
if (dev->dbcc_classguid == GUID_DEVINTERFACE_USB_DEVICE)
|
||||
{
|
||||
return wp == DBT_DEVICEQUERYREMOVE;
|
||||
}
|
||||
|
||||
std::string utf8(u2utf8(dev->dbcc_name));
|
||||
if (wp == DBT_DEVICEQUERYREMOVE)
|
||||
return cur_dev_name_ != utf8;
|
||||
if (wp == DBT_DEVICEQUERYREMOVE)
|
||||
return cur_dev_name_ != utf8;
|
||||
|
||||
VLOG_MINI_2(LOG_LEVEL_DEBUG_INFO, "event '%08x' of device %s\n", wp, utf8.c_str());
|
||||
|
||||
|
@ -1081,7 +1052,7 @@ int usb_monitor::on_usb_pnp(WPARAM wp, LPARAM lp)
|
|||
|
||||
usb_device* ud = new usb_device(udev);
|
||||
*ud = dev->dbcc_classguid;
|
||||
if (!PostThreadMessageW(handle_msg_id_, MSG_DEVICE_PNP, wp == DBT_DEVICEARRIVAL, (LPARAM)ud))
|
||||
if (!PostThreadMessageW(handle_msg_id_, MSG_DEVICE_PNP, wp == DBT_DEVICEARRIVAL, (LPARAM)ud))
|
||||
ud->release();
|
||||
|
||||
return ret;
|
||||
|
@ -1157,7 +1128,7 @@ void usb_monitor::thread_run_device_event_wnd(void)
|
|||
wnd_monitor_ = CreateWindowW(cls, L"usb", WS_POPUP, 0, 0, 0, 0, NULL, NULL, GetModuleHandleW(NULL), this);
|
||||
if (!IsWindow(wnd_monitor_))
|
||||
{
|
||||
if(run_)
|
||||
if (run_)
|
||||
Sleep(1000);
|
||||
|
||||
return;
|
||||
|
@ -1254,7 +1225,7 @@ int LIBUSB_CALL libusb_init(libusb_context** ctx)
|
|||
{
|
||||
if (ctx)
|
||||
*ctx = (libusb_context*)new usb_monitor();
|
||||
else if(!usb_monitor::usb_monitor_)
|
||||
else if (!usb_monitor::usb_monitor_)
|
||||
usb_monitor::usb_monitor_ = new usb_monitor();
|
||||
|
||||
return LIBUSB_SUCCESS;
|
||||
|
@ -1289,7 +1260,7 @@ void LIBUSB_CALL libusb_hotplug_deregister_callback(libusb_context* ctx, libusb_
|
|||
if (ctx)
|
||||
((usb_monitor*)ctx)->unreg_callback((usb_callback*)callback_handle);
|
||||
else if (usb_monitor::usb_monitor_)
|
||||
usb_monitor::usb_monitor_->unreg_callback((usb_callback*)callback_handle);
|
||||
usb_monitor::usb_monitor_->unreg_callback((usb_callback*)callback_handle);
|
||||
}
|
||||
int LIBUSB_CALL libusb_handle_events_timeout(libusb_context* ctx, struct timeval* tv)
|
||||
{
|
||||
|
@ -1312,21 +1283,21 @@ void LIBUSB_CALL libusb_unref_device(libusb_device* dev)
|
|||
}
|
||||
int LIBUSB_CALL libusb_get_device_descriptor(libusb_device* dev, struct libusb_device_descriptor* desc)
|
||||
{
|
||||
if (!dev)
|
||||
return LIBUSB_ERROR_INVALID_PARAM;
|
||||
if (!dev)
|
||||
return LIBUSB_ERROR_INVALID_PARAM;
|
||||
|
||||
return ((usb_device*)dev)->get_descriptor(desc);
|
||||
return ((usb_device*)dev)->get_descriptor(desc);
|
||||
}
|
||||
int LIBUSB_CALL libusb_get_config_descriptor(libusb_device* dev, uint8_t config_index, struct libusb_config_descriptor** config)
|
||||
{
|
||||
if (!dev)
|
||||
return LIBUSB_ERROR_INVALID_PARAM;
|
||||
if (!dev)
|
||||
return LIBUSB_ERROR_INVALID_PARAM;
|
||||
|
||||
return ((usb_device*)dev)->get_config_descriptor(config_index, config);
|
||||
return ((usb_device*)dev)->get_config_descriptor(config_index, config);
|
||||
}
|
||||
void LIBUSB_CALL libusb_free_config_descriptor(struct libusb_config_descriptor* config)
|
||||
{
|
||||
// because the configuration descriptor is member of device, nothing to do here ...
|
||||
// because the configuration descriptor is member of device, nothing to do here ...
|
||||
}
|
||||
|
||||
int LIBUSB_CALL libusb_reset_device(libusb_device_handle* dev_handle)
|
||||
|
@ -1490,12 +1461,9 @@ void LIBUSB_CALL libusb_quit(libusb_context* ctx)
|
|||
}
|
||||
uint8_t LIBUSB_CALL libusb_get_device_address(libusb_device* device)
|
||||
{
|
||||
if(!device)
|
||||
if (!device)
|
||||
return 0;
|
||||
|
||||
return ((usb_device*)device)->address();
|
||||
}
|
||||
int LIBUSB_CALL libusb_cancel_transfer(struct libusb_transfer* transfer/*in windows, this is a libusb_device* object*/)
|
||||
{
|
||||
return ((usb_device*)transfer)->cancel_io();
|
||||
}
|
||||
|
||||
|
|
|
@ -17,7 +17,7 @@
|
|||
// HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Class\{6bdd1fc6-810f-11d0-bec7-08002be2092f}
|
||||
#define IMAGE_CLASS_GUID "6BDD1FC6-810F-11D0-BEC7-08002BE2092F"
|
||||
#define MONITOR_WINDOW_OWNER L"monitor_wnd_owner"
|
||||
|
||||
|
||||
|
||||
typedef struct _usb_dev
|
||||
{
|
||||
|
@ -48,10 +48,9 @@ class ovl_cls// : public refer
|
|||
volatile long ref_;
|
||||
OVERLAPPED ovl_;
|
||||
DWORD io_bytes_;
|
||||
uint32_t type_;
|
||||
|
||||
public:
|
||||
ovl_cls(uint32_t type);
|
||||
ovl_cls();
|
||||
|
||||
protected:
|
||||
~ovl_cls();
|
||||
|
@ -74,8 +73,6 @@ public:
|
|||
LPDWORD io_bytes(void);
|
||||
void reset(void);
|
||||
bool is_waited(void);
|
||||
void notify(void);
|
||||
uint32_t type(void);
|
||||
};
|
||||
class ovl_mgr
|
||||
{
|
||||
|
@ -87,8 +84,7 @@ public:
|
|||
~ovl_mgr();
|
||||
|
||||
public:
|
||||
ovl_cls* get_ovl(uint32_t type);
|
||||
void notify_all(void);
|
||||
ovl_cls* get_ovl(void);
|
||||
};
|
||||
|
||||
class usb_device // consider as libusb_device
|
||||
|
@ -100,8 +96,8 @@ class usb_device // consider as libusb_device
|
|||
bool online_;
|
||||
ovl_mgr ovl_mgr_;
|
||||
|
||||
libusb_device_handle *handle_; // as file handle returned by CreateFile
|
||||
libusb_device_descriptor *dev_desc_;
|
||||
libusb_device_handle* handle_; // as file handle returned by CreateFile
|
||||
libusb_device_descriptor* dev_desc_;
|
||||
std::vector<libusb_config_descriptor*> cfg_desc_;
|
||||
|
||||
typedef struct _usb_pipe
|
||||
|
@ -119,10 +115,10 @@ class usb_device // consider as libusb_device
|
|||
public:
|
||||
usb_device(const USBDEV& dev);
|
||||
|
||||
static void vid_pid_from_name(const char* name, int *vid, int *pid); // device name like '\\?\usb#vid_3072&pid_0239#01234567aabbccddee#{a5dcbf10-6530-11d2-901f-00c04fb951ed}'
|
||||
static void vid_pid_from_name(const char* name, int* vid, int* pid); // device name like '\\?\usb#vid_3072&pid_0239#01234567aabbccddee#{a5dcbf10-6530-11d2-901f-00c04fb951ed}'
|
||||
static DWORD from_hex_string(const char* hex_str);
|
||||
static std::string name_without_guid(const char* name);
|
||||
static bool find_vid_pid_in_hub(const char* utf8_hub_path_name, int vid, int pid, int *addr/*if *addr is not -1 or NULL, search the device with vid:pid and set the address in addr if it was not null, or-else chekc the device at *addr is vid:pid or not*/, std::string* reg_key/*{6bdd1fc6-810f-11d0-bec7-08002be2092f}\\0007*/);
|
||||
static bool find_vid_pid_in_hub(const char* utf8_hub_path_name, int vid, int pid, int* addr/*if *addr is not -1 or NULL, search the device with vid:pid and set the address in addr if it was not null, or-else chekc the device at *addr is vid:pid or not*/, std::string* reg_key/*{6bdd1fc6-810f-11d0-bec7-08002be2092f}\\0007*/);
|
||||
static std::string usb_scan_name(const char* reg_key/*{6bdd1fc6-810f-11d0-bec7-08002be2092f}\\0007*/); // return \\.\Usbscan1 ...
|
||||
|
||||
long add_ref(void);
|
||||
|
@ -157,8 +153,6 @@ public:
|
|||
int transfer_bulk(unsigned endpoint, unsigned char* data, int* length, unsigned int timeout);
|
||||
int transfer_control(uint8_t type, uint8_t req, uint16_t val, uint16_t ind, unsigned char* data, uint16_t len, unsigned timeout);
|
||||
int transfer_interrupt(unsigned endpoint, unsigned char* data, int* length, unsigned int timeout);
|
||||
|
||||
int cancel_io(void);
|
||||
};
|
||||
class usb_callback
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue