回退 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")
|
||||
|
@ -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,18 +136,13 @@ 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 ...
|
||||
|
@ -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,7 +627,7 @@ 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;
|
||||
|
||||
|
@ -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
|
||||
|
@ -760,7 +742,7 @@ int usb_device::transfer_interrupt(unsigned endpoint, unsigned char* data, int*
|
|||
*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 ...
|
||||
|
||||
|
@ -1495,7 +1466,4 @@ uint8_t LIBUSB_CALL libusb_get_device_address(libusb_device* device)
|
|||
|
||||
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();
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
@ -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