增加设备配置获取接口;增加memory数据块发送进度通知

This commit is contained in:
gb 2023-12-16 15:29:59 +08:00
parent a364fd68a0
commit cad9d653c8
9 changed files with 91 additions and 42 deletions

View File

@ -396,7 +396,10 @@ device_option* hg_scanner::get_device_opt(void)
return dev_opts_;
}
int hg_scanner::get_peer_config(LPPEERCFG cfg)
{
return scanner_->get_peer_config(cfg);
}
int hg_scanner::file_transfer(const char* local, const char* remote, bool to_remote)
{
auto prog = [this](uint64_t total, uint64_t txed, uint32_t err, void* user) -> int

View File

@ -125,5 +125,6 @@ public:
// advanced functions ...
public:
device_option* get_device_opt(void);
int get_peer_config(LPPEERCFG cfg);
int file_transfer(const char* local, const char* remote, bool to_remote); // get_value("tx-prog", (double*)percent) to query progress
};

View File

@ -476,13 +476,26 @@ void async_usb_host::stop_worker_threads(void)
#endif
}
int async_usb_host::bulk_write_buf(uint8_t* buf, int* len, int io_size)
int async_usb_host::bulk_write_buf(uint8_t* buf, int* len, int io_size, data_source_ptr prog)
{
int total = 0,
l = io_size <= *len ? io_size : *len,
s = 0,
err = 0,
to = 0;
uint64_t all = *len,
cur = 0;
auto real_prog = [&](uint64_t total, uint64_t cur, uint32_t err) -> int
{
return prog->notify_progress(total, cur, err);
};
auto empty_prog = [&](uint64_t total, uint64_t cur, uint32_t err) -> int
{
return 0;
};
std::function<int(uint64_t, uint64_t, uint32_t)> progr = empty_prog;
if (prog)
progr = real_prog;
while (1)
{
@ -500,6 +513,10 @@ int async_usb_host::bulk_write_buf(uint8_t* buf, int* len, int io_size)
std::this_thread::sleep_for(std::chrono::milliseconds(to * 100));
continue;
}
{
cur = total;
progr(all, cur, err);
}
break;
}
@ -510,6 +527,10 @@ int async_usb_host::bulk_write_buf(uint8_t* buf, int* len, int io_size)
}
total += s;
{
cur = total;
progr(all, cur, err);
}
if (total >= *len)
break;
@ -532,7 +553,7 @@ int async_usb_host::inner_write_bulk(data_source_ptr data, dyn_mem_ptr mem, int
writing_ = true;
if (data->is_memory_block())
{
err = bulk_write_buf(data->ptr(), &total, bulk_size);
err = bulk_write_buf(data->ptr(), &total, bulk_size, data);
}
else
{
@ -594,6 +615,10 @@ uint16_t async_usb_host::get_protocol_version(void)
{
return peer_cfg_.ver;
}
uint32_t async_usb_host::get_io_buffer_size(void)
{
return peer_cfg_.io_size;
}
int async_usb_host::get_peer_status(LPEP0REPLYSTATUS status)
{
SIMPLE_LOCK(io_lock_);
@ -618,28 +643,41 @@ int async_usb_host::reset_peer(uint32_t timeout)
, 1000);
}
tc.reset();
while ((err = get_peer_status(&status)) == 0)
if (err == 0)
{
if (status.in_status == WORKER_STATUS_IDLE && status.out_status == WORKER_STATUS_BUSY && status.task_cnt == 0
&& status.task_required_bytes == 0 && status.packets_to_sent == 0)
break;
std::this_thread::sleep_for(std::chrono::milliseconds(50));
std::this_thread::sleep_for(std::chrono::milliseconds(5));
if (tc.elapse_ms() > timeout)
dyn_mem_ptr empty = dyn_mem::memory(peer_cfg_.io_size);
empty->set_len(peer_cfg_.io_size);
post_2_write_bulk_thread(empty);
empty->release();
tc.reset();
while ((err = get_peer_status(&status)) == 0)
{
err = ETIMEDOUT;
break;
}
}
if (status.in_status == WORKER_STATUS_IDLE && status.out_status == WORKER_STATUS_IDLE && status.task_cnt == 0
&& status.task_required_bytes == 0 && status.packets_to_sent == 0)
break;
cancel = 0;
{
SIMPLE_LOCK(io_lock_);
libusb_control_transfer(usb_handle_, LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_IN
, USB_REQ_EP0_CANCEL_IO, 0, 0
, (unsigned char*)&cancel, sizeof(cancel)
, 1000);
std::this_thread::sleep_for(std::chrono::milliseconds(5));
if (tc.elapse_ms() > timeout)
{
err = ETIMEDOUT;
break;
}
}
if (err == 0)
{
cancel = 0;
{
SIMPLE_LOCK(io_lock_);
libusb_control_transfer(usb_handle_, LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_IN
, USB_REQ_EP0_CANCEL_IO, 0, 0
, (unsigned char*)&cancel, sizeof(cancel)
, 1000);
}
}
}
return err;

View File

@ -66,7 +66,7 @@ class async_usb_host : public refer
void create_worker_threads(void);
void stop_worker_threads(void);
int bulk_write_buf(uint8_t* buf, int* len, int io_size); // return error code
int bulk_write_buf(uint8_t* buf, int* len, int io_size, data_source_ptr prog = nullptr); // return error code
int inner_write_bulk(data_source_ptr data, dyn_mem_ptr mem/*to load data in if data was not memory*/, int bulk_size); // return error code
void post_2_write_bulk_thread(data_source_ptr data);
dyn_mem_ptr handle_data_in(dyn_mem_ptr& data, uint32_t* used, packet_data_base_ptr* more);
@ -92,6 +92,7 @@ public:
public:
int get_peer_config(LPPEERCFG cfg);
uint16_t get_protocol_version(void);
uint32_t get_io_buffer_size(void);
int get_peer_status(LPEP0REPLYSTATUS status);
int reset_peer(uint32_t timeout = 2000/*ms*/);
int set_gadget_encrypting_method(uint32_t cmd_enc = ENCRYPT_CMD_NONE, uint32_t payload_enc = ENCRYPT_NONE, uint8_t enc_data = 0);

View File

@ -314,10 +314,13 @@ int scanner_handler::wait_result(cmd_result* reply)
}
}
int scanner_handler::get_protocol_version(uint16_t* ver)
int scanner_handler::get_peer_config(LPPEERCFG cfg)
{
if (ver)
*ver = usb_->get_protocol_version();
if (cfg)
{
cfg->io_size = usb_->get_io_buffer_size();
cfg->ver = usb_->get_protocol_version();
}
return 0;
}

View File

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

View File

@ -392,8 +392,8 @@ void usb_manager::notify_usb_event(PNPDEV& pd, bool* retry)
sprintf(buf, "0x%x", pd.event);
evstr = buf;
}
utils::to_log(LOG_LEVEL_DEBUG, "USB%u.%x of pid:vid(%x:%x) event(%s) received.\n"
, HIBYTE(ud.ver), LOBYTE(ud.ver) / 0x10, ud.pid, ud.vid, evstr.c_str());
utils::to_log(LOG_LEVEL_DEBUG, "USB%u.%x of vid:pid(%04x:%04x) event(%s) received.\n"
, HIBYTE(ud.ver), LOBYTE(ud.ver) / 0x10, ud.vid, ud.pid, evstr.c_str());
if (ev != USB_EVENT_NULL)
{
usb_event_handler cb(&usb_manager::usb_event_handle);

View File

@ -36,19 +36,12 @@ static sys_info g_si;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
packet_data_base::packet_data_base() : pack_cmd_(0), pack_id_(0)
, progress_notify_(PROGRESS_NOTIFYER()), user_data_(nullptr)
{}
{
set_progress_notify();
}
packet_data_base::~packet_data_base()
{}
int packet_data_base::notify_progress(uint64_t total, uint64_t cur_size, uint32_t err)
{
if (progress_notify_)
return progress_notify_(total, cur_size, err, user_data_);
else
return ENOENT;
}
void packet_data_base::set_packet_param(uint32_t cmd, uint32_t id)
{
pack_cmd_ = cmd;
@ -73,9 +66,18 @@ uint32_t packet_data_base::get_session_id(void)
void packet_data_base::set_progress_notify(PROGRESS_NOTIFYER notify, void* param)
{
progress_notify_ = notify;
auto empty_notifyer = [&](uint64_t, uint64_t, uint32_t, void*) -> int
{
return 0;
};
progress_notify_ = notify ? notify : empty_notifyer;
user_data_ = param;
}
int packet_data_base::notify_progress(uint64_t total, uint64_t cur_size, uint32_t err)
{
return progress_notify_(total, cur_size, err, user_data_);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
@ -857,12 +859,13 @@ dyn_mem_ptr dyn_mem_pool::take(void)
if(!pool_[rpos_])
{
utils::to_log(LOG_LEVEL_DEBUG, "memory pool pointer = %u.\n", wpos_);
chronograph watch;
do
{
std::this_thread::sleep_for(std::chrono::milliseconds(1));
} while (run_ && !pool_[rpos_]);
utils::to_log(LOG_LEVEL_DEBUG, "Waiting for taking memory pool took %ums at %u.\n", watch.elapse_ms(), rpos_);
} while (run_ && !pool_[rpos_]);
utils::to_log(LOG_LEVEL_DEBUG, "Waiting for taking memory pool took %ums at %u (write pos = %u).\n", watch.elapse_ms(), rpos_, wpos_);
}
if(pool_[rpos_])
{

View File

@ -34,7 +34,6 @@ public:
protected:
virtual ~packet_data_base();
int notify_progress(uint64_t total, uint64_t cur_size, uint32_t err);
public:
void set_packet_param(uint32_t cmd, uint32_t id);
@ -45,6 +44,7 @@ public:
uint32_t get_session_id(void);
void set_progress_notify(PROGRESS_NOTIFYER notify = PROGRESS_NOTIFYER(), void* param = nullptr);
int notify_progress(uint64_t total, uint64_t cur_size, uint32_t err);
};
class file_map : public refer