diff --git a/device/win_usb/win_usb.cpp b/device/win_usb/win_usb.cpp index 7a73369..8bdc277 100644 --- a/device/win_usb/win_usb.cpp +++ b/device/win_usb/win_usb.cpp @@ -76,6 +76,20 @@ std::wstring ansi2unicode(const char* ansi, UINT cp = CP_ACP) return unic; } +std::string unicode2ansi(const wchar_t* unic, UINT cp = CP_ACP) +{ + int len = WideCharToMultiByte(cp, 0, unic, lstrlenW(unic), NULL, 0, NULL, NULL); + char *buf = new char[len + 4]; + + memset(buf, 0, len + 4); + len = WideCharToMultiByte(cp, 0, unic, lstrlenW(unic), buf, len, NULL, NULL); + buf[len--] = 0; + + std::string ansi(buf); + delete[] buf; + + return ansi; +} ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // OVERLAPPED ... ovl_cls::ovl_cls() : ref_(1), io_bytes_(0) @@ -377,6 +391,16 @@ bool usb_device::init(void) COPY_MEMBER(dev_desc_, &info->ConnectionInfo->DeviceDescriptor, iSerialNumber); COPY_MEMBER(dev_desc_, &info->ConnectionInfo->DeviceDescriptor, bNumConfigurations); } + PSTRING_DESCRIPTOR_NODE strdesc = info->StringDescs; + while (strdesc) + { + STRCFG sc; + sc.id = strdesc->DescriptorIndex; + sc.val = strdesc->StringDescriptor ? unicode2ansi(strdesc->StringDescriptor->bString) : ""; + str_cfg_.push_back(sc); + + strdesc = strdesc->Next; + } if (info->ConfigDesc) { libusb_config_descriptor* desc = NULL; @@ -459,6 +483,7 @@ void usb_device::clear(void) { close(); + str_cfg_.clear(); if (dev_desc_) delete dev_desc_; dev_desc_ = NULL; @@ -524,6 +549,23 @@ int usb_device::get_config_descriptor(int index, libusb_config_descriptor** desc return LIBUSB_SUCCESS; } +int usb_device::get_config_string(uint8_t desc_index, unsigned char* data, int length) +{ + for (auto& v : str_cfg_) + { + if (v.id == desc_index) + { + if (length < v.val.length()) + memcpy(data, v.val.c_str(), length); + else + strcpy((char*)data, v.val.c_str()); + + return LIBUSB_SUCCESS; + } + } + + return LIBUSB_ERROR_NOT_FOUND; +} int usb_device::open(libusb_device_handle** dev_handle) { if (handle_) @@ -1475,4 +1517,9 @@ uint8_t LIBUSB_CALL libusb_get_device_address(libusb_device* device) return ((usb_device*)device)->address(); } +int LIBUSB_CALL libusb_get_string_descriptor_ascii(libusb_device_handle* dev_handle, + uint8_t desc_index, unsigned char* data, int length) +{ + return ((usb_device*)dev_handle)->get_config_string(desc_index, data, length); +} diff --git a/device/win_usb/win_usb.h b/device/win_usb/win_usb.h index 1a3f3e9..564d13c 100644 --- a/device/win_usb/win_usb.h +++ b/device/win_usb/win_usb.h @@ -100,6 +100,13 @@ class usb_device // consider as libusb_device libusb_device_descriptor* dev_desc_; std::vector cfg_desc_; + typedef struct _str_cfg + { + int id; + std::string val; + }STRCFG; + std::vector str_cfg_; + typedef struct _usb_pipe { UCHAR address; @@ -145,6 +152,7 @@ public: void clear(void); int get_descriptor(libusb_device_descriptor* desc); int get_config_descriptor(int index, libusb_config_descriptor** desc); + int get_config_string(uint8_t desc_index, unsigned char* data, int length); int open(libusb_device_handle** dev_handle); int close(void); int set_timeout(unsigned milliseconds); diff --git a/sane/scanner.cpp b/sane/scanner.cpp index fcc0c02..09b5ea7 100644 --- a/sane/scanner.cpp +++ b/sane/scanner.cpp @@ -2810,8 +2810,8 @@ COM_API_IMPLEMENT(scanner, int, start(void)) thread_starting_->join(); #ifdef START_SCAN_IN_THREAD thread_starting_.reset(new std::thread(&scanner::thread_start, this)); - get_scanned_images(-1); // block until image arrived or scan finished - ret = err_; + ret = get_scanned_images(-1); // block until image arrived or scan finished + ret = ret > 0 ? 0 : err_; // received images, ensure return success. BUG-809 #else ret = thread_start(); #endif