Disable UI if protocol was mismatch

This commit is contained in:
gb 2023-04-13 15:05:31 +08:00
parent a88adbadfe
commit 02864b4c55
6 changed files with 21 additions and 20 deletions

View File

@ -667,9 +667,6 @@ void CDlgScanner::set_device(usb::LPUSBPNP pnp)
KillTimer(TIMER_ID_REFRESH_BULK);
((CButton*)GetDlgItem(IDC_CHECK_AUTO))->SetCheck(BST_UNCHECKED);
enable_buttons(pnp != NULL);
GetDlgItem(IDC_CHECK_AUTO)->EnableWindow(pnp != NULL);
GetDlgItem(IDC_BUTTON_RESET_BULK)->EnableWindow(pnp != NULL);
GetDlgItem(IDC_BUTTON_REFRESH)->EnableWindow(pnp != NULL);
if (scanner_)
{
@ -767,15 +764,18 @@ void CDlgScanner::set_device(usb::LPUSBPNP pnp)
else
{
wchar_t buf[128] = { 0 };
uint8_t h = 0, l = 0;
uint16_t ver = 0;
swprintf_s(buf, _countof(buf) - 1, L"%04X:%04X", pnp->vid, pnp->pid);
::SetWindowTextW(m_hWnd, buf);
err = scanner_->get_protocol_version(&h, &l);
if (err)
err = scanner_->get_protocol_version(&ver);
if (err || ver != PROTOCOL_VER)
{
msg_box(m_hWnd, MB_OK, L"Unsupported Scanner", L"Failed to get protocol version with error %d.", err);
if(err)
msg_box(m_hWnd, MB_OK, L"Unsupported Scanner", L"Failed to get protocol version with error %d.", err);
else
msg_box(m_hWnd, MB_OK, L"Unsupported Scanner", L"Protocol version is mismatch: expect %u.%u but return %u.%u", HIBYTE(PROTOCOL_VER), LOBYTE(PROTOCOL_VER), HIBYTE(ver), LOBYTE(ver));
scanner_->release();
scanner_ = NULL;
enable_buttons(false);
@ -784,7 +784,7 @@ void CDlgScanner::set_device(usb::LPUSBPNP pnp)
{
scanner_->set_image_receiver(img_keeper);
swprintf_s(buf, _countof(buf) - 1, L"%u.%u", h, l);
swprintf_s(buf, _countof(buf) - 1, L"%u.%u", HIBYTE(ver), LOBYTE(ver));
SetDlgItemTextW(IDC_EDIT_PROTOCOL_VER, buf);
refresh_bulk_status();
@ -815,6 +815,9 @@ void CDlgScanner::set_device(usb::LPUSBPNP pnp)
{
::SetDlgItemTextW(m_hWnd, IDC_BUTTON_SCAN, L"Scan");
}
GetDlgItem(IDC_CHECK_AUTO)->EnableWindow(scanner_ != NULL);
GetDlgItem(IDC_BUTTON_RESET_BULK)->EnableWindow(scanner_ != NULL);
GetDlgItem(IDC_BUTTON_REFRESH)->EnableWindow(scanner_ != NULL);
}
void CDlgScanner::get_option(const char* name, void* value, size_t size)
{

View File

@ -415,12 +415,12 @@ int scanner_handler::wait_result(cmd_result* reply)
}
}
int scanner_handler::get_protocol_version(uint8_t* h, uint8_t* l)
int scanner_handler::get_protocol_version(uint16_t* ver)
{
if (!is_scanner_available())
return ENODEV;
return usb_->get_peer_protocol_version(h, l);
return usb_->get_peer_protocol_version(ver);
}
int scanner_handler::get_scanner_status(LPEP0REPLYSTATUS status)
{

View File

@ -101,7 +101,7 @@ protected:
// I/O ...
public:
// following methods transferred by EP0
int get_protocol_version(uint8_t* h, uint8_t* l);
int get_protocol_version(uint16_t* ver);
int get_scanner_status(LPEP0REPLYSTATUS status);
int restart_peer_bulk(uint32_t timeout = 1000/*ms*/);

View File

@ -479,20 +479,18 @@ dyn_mem_ptr async_usb_host::handle_data_in(dyn_mem_ptr& data, uint32_t* used, pa
}
}
int async_usb_host::get_peer_protocol_version(uint8_t* main, uint8_t* sub)
int async_usb_host::get_peer_protocol_version(uint16_t* ver)
{
uint16_t ver = 0;
uint16_t v = 0;
int err = libusb_control_transfer(usb_handle_, LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_IN
, USB_REQ_EP0_GET_PROTO_VER, 0, 0
, (unsigned char*)&ver, sizeof(ver)
, (unsigned char*)&v, sizeof(v)
, 1000);
if (main)
*main = (ver >> 8) & 0x0ff;
if (sub)
*sub = ver & 0x0ff;
if (ver)
*ver = v;
return err == sizeof(ver) ? 0 : EFAULT;
return err == sizeof(v) ? 0 : EFAULT;
}
int async_usb_host::get_peer_status(LPEP0REPLYSTATUS status)
{

View File

@ -77,7 +77,7 @@ public:
uint8_t& encrypt_data(void);
public:
int get_peer_protocol_version(uint8_t* main, uint8_t* sub);
int get_peer_protocol_version(uint16_t* ver);
int get_peer_status(LPEP0REPLYSTATUS status);
int restart_peer_bulk(uint32_t timeout = 1000/*ms*/);
int set_gadget_encrypting_method(uint32_t cmd_enc = ENCRYPT_CMD_NONE, uint32_t payload_enc = ENCRYPT_NONE, uint8_t enc_data = 0);