restore EP0 checking thread and fix reseting-bulk; reset bulk at openning if first bulk communication failed with error TIMEOUTED

This commit is contained in:
gb 2023-04-08 14:04:40 +08:00
parent 4d65a65568
commit 14e5778fa3
4 changed files with 49 additions and 30 deletions

View File

@ -55,7 +55,7 @@ async_usb_gadget::async_usb_gadget(usb_gadget* gadget, std::function<FUNCTION_PR
threads_->thread_new(&async_usb_gadget::thread_pump_task); threads_->thread_new(&async_usb_gadget::thread_pump_task);
thread_ep0_id_ = threads_->thread_new(&async_usb_gadget::thread_read_ep0); thread_ep0_id_ = threads_->thread_new(&async_usb_gadget::thread_read_ep0);
//threads_->thread_new(&async_usb_gadget::thread_check_ep0_status); threads_->thread_new(&async_usb_gadget::thread_check_ep0_status);
} }
async_usb_gadget::~async_usb_gadget() async_usb_gadget::~async_usb_gadget()
@ -546,7 +546,7 @@ void async_usb_gadget::thread_read_bulk(void)
// log_cls::log(LOG_LEVEL_ALL, "read bulk with 0 byte - %u time(s).\n", cnt_0); // log_cls::log(LOG_LEVEL_ALL, "read bulk with 0 byte - %u time(s).\n", cnt_0);
// cnt_0 = 0; // cnt_0 = 0;
// } // }
log_cls::log(LOG_LEVEL_ALL, "thread_pump_task do reset-bulk ...\n"); log_cls::log(LOG_LEVEL_ALL, "thread_read_bulk do reset-bulk ...\n");
buf->release(); buf->release();
if(!run_) if(!run_)
break; break;
@ -786,6 +786,7 @@ void async_usb_gadget::thread_pump_task(void)
} }
void async_usb_gadget::thread_restart_bulk(void) void async_usb_gadget::thread_restart_bulk(void)
{ {
log_cls::log(LOG_LEVEL_ALL, "reset bulk: 1/5 - clear command queue and sent queue ...\n");
dyn_mem_ptr data; dyn_mem_ptr data;
while(cmd_que_.take(data, false)) while(cmd_que_.take(data, false))
data->release(); data->release();
@ -794,17 +795,25 @@ void async_usb_gadget::thread_restart_bulk(void)
while(sent_que_.take(src, false)) while(sent_que_.take(src, false))
src->release(); src->release();
sent_que_.quit(); log_cls::log(LOG_LEVEL_ALL, "reset bulk: 2/5 - waiting for IN-endpoint to be idle ...\n");
cmd_que_.quit(); while (status_.in_status != BULK_STATUS_IDLE)
{
sent_que_.quit();
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
log_cls::log(LOG_LEVEL_ALL, "reset bulk: 3/5 - waiting for task queue to be idle ...\n");
while(status_.task_cmd)
{
cmd_que_.quit();
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
log_cls::log(LOG_LEVEL_ALL, "reset bulk: 4/5 - waiting for OUT-endpoint to be idle(wait data) ...\n");
// close_bulk(); while(status_.out_status != BULK_STATUS_IO)
while(status_.in_status != BULK_STATUS_IDLE || status_.out_status != BULK_STATUS_IO || status_.task_cmd)
std::this_thread::sleep_for(std::chrono::milliseconds(10)); std::this_thread::sleep_for(std::chrono::milliseconds(10));
// open_bulk();
// cmd_que_.quit();
reset_bulk_ = false; reset_bulk_ = false;
log_cls::log(LOG_LEVEL_ALL, "reset bulk completed.\n"); log_cls::log(LOG_LEVEL_ALL, "reset bulk: 5/5 - completed.\n");
} }
int async_usb_gadget::stop(void) int async_usb_gadget::stop(void)

View File

@ -709,7 +709,14 @@ void CDlgScanner::set_device(usb::LPUSBPNP pnp)
RECT r = { 0 }; RECT r = { 0 };
std::string all(""); std::string all("");
scanner_->option_get_all(all); if (scanner_->option_get_all(all) == ETIMEDOUT)
{
// reset it ...
msg_box(m_hWnd, MB_OK, L"Bulk Trouble", L"Bulk communication TIMEOUTED, will try reset bulk ...");
scanner_->reset_message_que();
refresh_bulk_status();
scanner_->option_get_all(all);
}
scanner_handler::reorder_device_config_json(all); scanner_handler::reorder_device_config_json(all);
sane::reset_opts(all.c_str()); sane::reset_opts(all.c_str());

View File

@ -186,7 +186,7 @@ scanner_handler::scanner_handler(void) : usb_(nullptr), status_(SCANNER_STATUS_N
*more = dynamic_cast<packet_data_base_ptr>(receiver); *more = dynamic_cast<packet_data_base_ptr>(receiver);
} }
} }
else if (pack->cmd == PACK_CMD_SCAN_STOP_ROGER) else if (pack->cmd == PACK_CMD_SCAN_FINISHED_ROGER)
{ {
data_holder_ptr receiver = img_receiver_(NULL, pack->data); data_holder_ptr receiver = img_receiver_(NULL, pack->data);
if (receiver) if (receiver)

View File

@ -287,10 +287,10 @@ void async_usb_host::thread_pump_task(void)
if (dh->is_complete() || err) if (dh->is_complete() || err)
{ {
reply = dyn_mem::memory(sizeof(PACK_BASE)); //reply = dyn_mem::memory(sizeof(PACK_BASE));
pack = (LPPACK_BASE)reply->ptr(); //pack = (LPPACK_BASE)reply->ptr();
BASE_PACKET_REPLY(*pack, dh->get_packet_command() + 1, dh->get_packet_id(), err); //BASE_PACKET_REPLY(*pack, dh->get_packet_command() + 1, dh->get_packet_id(), err);
reply->set_len(sizeof(PACK_BASE)); //reply->set_len(sizeof(PACK_BASE));
dh->release(); dh->release();
dh = nullptr; dh = nullptr;
@ -348,24 +348,27 @@ int async_usb_host::bulk_write_buf(uint8_t* buf, int* len)
s = 0, s = 0,
err = 0; err = 0;
while ((err = libusb_bulk_transfer(usb_handle_, bulk_out_.port, buf, l, &s, 1000)) == 0) do
{ {
if (cancel_write_) while ((err = libusb_bulk_transfer(usb_handle_, bulk_out_.port, buf, l, &s, 1000)) == 0)
{ {
err = ECANCELED; if (cancel_write_)
break; {
err = ECANCELED;
break;
}
total += s;
if (total >= *len)
break;
buf += s;
if (*len - total < bulk_size)
l = *len - total;
else
l = bulk_size;
} }
} while (err == LIBUSB_ERROR_INTERRUPTED); // should pay more attention to this error !!!
total += s;
if (total >= *len)
break;
buf += s;
if (*len - total < bulk_size)
l = *len - total;
else
l = bulk_size;
}
*len = total; *len = total;
return err; return err;