add packet GOOD_BYE; fix bugs when exiting app

This commit is contained in:
gb 2023-12-26 09:33:05 +08:00
parent fbe158c845
commit 742dcd1535
4 changed files with 20 additions and 4 deletions

View File

@ -264,13 +264,17 @@ int scanner_hw::open(std::function<IMAGE_HANDLER_PROTO> image_handler)
}
int scanner_hw::start_scan(void)
{
if(!camera_.get())
return EBADF;
camera_->start();
return 0;
}
int scanner_hw::stop_scan(void)
{
camera_->stop();
if(camera_.get())
camera_->stop();
return 0;
}

View File

@ -449,6 +449,8 @@ uint32_t async_scanner::stop(void)
{
usb_->stop();
}
return 0;
}
int async_scanner::last_error(void)
{

View File

@ -47,6 +47,7 @@ enum cancel_io
enum ep0_req
{
USB_REQ_EP0_HAND_SHAKE = 100, // hand-shake[in/out] with data LPPEERCFG, req = me, ind = 0, val = 0, len = sizeof(PEERCFG)
USB_REQ_EP0_GOOD_BYE, // disconnect, req = me, ind = 0, val = 0, len = sizeof(PEERCFG)
USB_REQ_EP0_GET_STATUS, // 获取各工作线程状态, return EP0REPLYSTATUS. req = me, ind = 0, val = bool: whether write log, len = sizeof(EP0REPLYSTATUS)
USB_REQ_EP0_CANCEL_IO, // 设置当前IO数据的有效性. req = me, ind = 0, val = 0, len = sizeof(uint32_t), discard IO data when data is CANCEL_IO_CANCEL
// work-flow: write control with 'CANCEL_IO_CANCEL', write bulk with 1 byte, write control with not 'CANCEL_IO_CANCEL' to restore

View File

@ -62,7 +62,8 @@ async_usb_gadget::async_usb_gadget(std::function<FUNCTION_PROTO_COMMAND_HANDLE>
if(err == 0)
{
thread_write_bulk(fd);
dev_->close_endpoint(EP_IND_BULK_IN);
if(run_)
dev_->close_endpoint(EP_IND_BULK_IN);
}
}
};
@ -80,7 +81,8 @@ async_usb_gadget::async_usb_gadget(std::function<FUNCTION_PROTO_COMMAND_HANDLE>
if(err == 0)
{
thread_read_bulk(fd);
dev_->close_endpoint(EP_IND_BULK_OUT);
if(run_)
dev_->close_endpoint(EP_IND_BULK_OUT);
}
}
};
@ -284,6 +286,13 @@ dyn_mem_ptr async_usb_gadget::handle_ctrl_setup(dyn_mem_ptr data)
utils::to_log(LOG_LEVEL_DEBUG, "Host process %llu connected.\n", host_pid_);
}
break;
case USB_REQ_EP0_GOOD_BYE:
{
host_pid_ = ((LPPEERCFG)(pev + 1))->pid;
utils::to_log(LOG_LEVEL_DEBUG, "Host process %llu disconnected.\n", host_pid_);
host_pid_ = 0;
}
break;
case USB_REQ_EP0_GET_STATUS:
reply = dyn_mem::memory(sizeof(struct _ep0_reply));
reply->set_len(sizeof(struct _ep0_reply));
@ -459,7 +468,7 @@ void async_usb_gadget::thread_read_ep0(void)
while(run_)
{
ret = wait_fd_event(fd);
if( ret <= 0 )
if( !run_ || ret <= 0 )
{
utils::to_log(LOG_LEVEL_ALL, "select EP0(%d) failed: %d(%s)\n", fd, errno, strerror(errno));
break;