From 9babe4b2d35370812e69a548426edca809d7a154 Mon Sep 17 00:00:00 2001 From: gb <741021719@qq.com> Date: Sat, 6 Aug 2022 14:21:39 +0800 Subject: [PATCH] =?UTF-8?q?fix=20bug-242:=20=E8=B6=85=E6=97=B6=E7=9A=84?= =?UTF-8?q?=E5=BC=82=E6=AD=A5=E6=93=8D=E4=BD=9C=E5=8F=96=E8=B5=B0=E5=90=8E?= =?UTF-8?q?=E7=BB=AD=E6=B6=88=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- device/win_usb/win_usb.cpp | 39 ++++++++++++++++++++++++-------------- device/win_usb/win_usb.h | 1 + 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/device/win_usb/win_usb.cpp b/device/win_usb/win_usb.cpp index bbcd34e..c3f996f 100644 --- a/device/win_usb/win_usb.cpp +++ b/device/win_usb/win_usb.cpp @@ -81,6 +81,11 @@ void ovl_cls::reset(void) memset(&ovl_, 0, sizeof(ovl_)); ovl_.hEvent = h; io_bytes_ = 0; + ResetEvent(h); +} +bool ovl_cls::is_waited(void) +{ + return WaitForSingleObject(ovl_.hEvent, 0) == WAIT_OBJECT_0; } ovl_mgr::ovl_mgr() @@ -98,9 +103,8 @@ ovl_cls* ovl_mgr::get_ovl(void) for (auto& v : ovls_) { - if (WaitForSingleObject(v->over_lapped()->hEvent, 0) == WAIT_OBJECT_0) + if (v->is_waited()) { - ResetEvent(v->over_lapped()->hEvent); o = v; o->add_ref(); o->reset(); @@ -740,7 +744,7 @@ int usb_device::transfer_bulk(unsigned endpoint, unsigned char* data, int* lengt int usb_device::transfer_control(uint8_t type, uint8_t req, uint16_t val, uint16_t ind, unsigned char* data, uint16_t len, unsigned timeout) { // return transfer bytes ... - int ret = LIBUSB_ERROR_NOT_FOUND; + int ret = 0; // LIBUSB_ERROR_NOT_FOUND; _IO_BLOCK_EX irp; if ((HANDLE)handle_ != INVALID_HANDLE_VALUE) @@ -769,17 +773,18 @@ int usb_device::transfer_control(uint8_t type, uint8_t req, uint16_t val, uint16 } else if (GetLastError() == ERROR_IO_PENDING) { - ret = WaitForSingleObject(oc->over_lapped()->hEvent, timeout) == WAIT_TIMEOUT ? LIBUSB_ERROR_TIMEOUT : *oc->io_bytes(); + ret = WaitForSingleObject(oc->over_lapped()->hEvent, timeout) == WAIT_TIMEOUT ? 0 : *oc->io_bytes(); } else { - io = GetLastError(); - if (io == ERROR_ACCESS_DENIED) - ret = LIBUSB_ERROR_ACCESS; - else if (io == ERROR_SEM_TIMEOUT) - ret = LIBUSB_ERROR_TIMEOUT; - else - ret = LIBUSB_ERROR_PIPE; + //io = GetLastError(); + //if (io == ERROR_ACCESS_DENIED) + // ret = LIBUSB_ERROR_ACCESS; + //else if (io == ERROR_SEM_TIMEOUT) + // ret = LIBUSB_ERROR_TIMEOUT; + //else + // ret = LIBUSB_ERROR_PIPE; + ret = 0; } oc->release(); } @@ -788,14 +793,15 @@ int usb_device::transfer_control(uint8_t type, uint8_t req, uint16_t val, uint16 } int usb_device::transfer_interrupt(unsigned endpoint, unsigned char* data, int* length, unsigned int timeout) { - int ret = LIBUSB_ERROR_NOT_SUPPORTED; + 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(); - if (DeviceIoControl(h, IOCTL_WAIT_ON_DEVICE_EVENT, NULL, 0, data, *length, oc->io_bytes(), oc->over_lapped())) + if (DeviceIoControl(h, IOCTL_WAIT_ON_DEVICE_EVENT, NULL, 0, data, len, oc->io_bytes(), oc->over_lapped())) { ret = LIBUSB_SUCCESS; *length = *oc->io_bytes(); @@ -812,7 +818,12 @@ int usb_device::transfer_interrupt(unsigned endpoint, unsigned char* data, int* ret = LIBUSB_SUCCESS; } else + { ret = LIBUSB_ERROR_TIMEOUT; + + // 此错误可能导致后续新产生的数据被填充进data,故取消该操作 BUG-242 + CancelIo(h); + } } else ret = LIBUSB_ERROR_PIPE; diff --git a/device/win_usb/win_usb.h b/device/win_usb/win_usb.h index b5add8d..af9573f 100644 --- a/device/win_usb/win_usb.h +++ b/device/win_usb/win_usb.h @@ -46,6 +46,7 @@ public: LPOVERLAPPED over_lapped(void); LPDWORD io_bytes(void); void reset(void); + bool is_waited(void); }; class ovl_mgr {