BULK_OUT读数据状态,定义为IDLE;memory数据源发送时,增加进度通知

This commit is contained in:
gb 2023-12-16 15:27:40 +08:00
parent 137ba57a05
commit 04bf018274
6 changed files with 76 additions and 17 deletions

View File

@ -36,19 +36,12 @@ static sys_info g_si;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// //
packet_data_base::packet_data_base() : pack_cmd_(0), pack_id_(0) 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() 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) void packet_data_base::set_packet_param(uint32_t cmd, uint32_t id)
{ {
pack_cmd_ = cmd; 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) 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; 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_]) if(!pool_[rpos_])
{ {
utils::to_log(LOG_LEVEL_DEBUG, "memory pool pointer = %u.\n", wpos_);
chronograph watch; chronograph watch;
do do
{ {
std::this_thread::sleep_for(std::chrono::milliseconds(1)); std::this_thread::sleep_for(std::chrono::milliseconds(1));
} while (run_ && !pool_[rpos_]); } while (run_ && !pool_[rpos_]);
utils::to_log(LOG_LEVEL_DEBUG, "Waiting for taking memory pool took %ums at %u.\n", watch.elapse_ms(), 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_]) if(pool_[rpos_])
{ {

View File

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

View File

@ -688,6 +688,9 @@ int usb_device::close_device(void)
} }
int usb_device::pull_up(std::string* msg) int usb_device::pull_up(std::string* msg)
{ {
if(on_)
return 0;
{ {
int dst = open(udc_.c_str(), O_RDWR); int dst = open(udc_.c_str(), O_RDWR);
if(dst == -1) if(dst == -1)
@ -700,6 +703,7 @@ int usb_device::pull_up(std::string* msg)
if(l == dwc3_.length()) if(l == dwc3_.length())
{ {
utils::to_log(LOG_LEVEL_DEBUG, "pull up device by write '%s' to '%s' directly success.\n", dwc3_.c_str(), udc_.c_str()); utils::to_log(LOG_LEVEL_DEBUG, "pull up device by write '%s' to '%s' directly success.\n", dwc3_.c_str(), udc_.c_str());
on_ = true;
return 0; return 0;
} }
else else
@ -711,6 +715,9 @@ int usb_device::pull_up(std::string* msg)
int err = 0; int err = 0;
std::string info(utils::get_command_result(cmd.c_str(), -1, &err)); std::string info(utils::get_command_result(cmd.c_str(), -1, &err));
if(err == 0)
on_ = true;
if(msg) if(msg)
*msg = std::move(info); *msg = std::move(info);
@ -718,10 +725,37 @@ int usb_device::pull_up(std::string* msg)
} }
int usb_device::pull_down(void) int usb_device::pull_down(void)
{ {
if(!on_)
return 0;
{
int dst = open(udc_.c_str(), O_RDWR);
if(dst == -1)
utils::to_log(LOG_LEVEL_DEBUG, "Try write empty to '%s' failed(open): %s\n", udc_.c_str(), strerror(errno));
else
{
int e = 0;
int l = write(dst, &e, 1),
err = errno;
close(dst);
if(l == 1)
{
utils::to_log(LOG_LEVEL_DEBUG, "pull down device by write '' to '%s' directly success.\n", udc_.c_str());
std::this_thread::sleep_for(std::chrono::seconds(3));
on_ = false;
return 0;
}
else
utils::to_log(LOG_LEVEL_DEBUG, "Try write '' to '%s' failed(write): %s\n", udc_.c_str(), strerror(errno));
}
}
std::string cmd("echo " + pwd_ + " | sudo -S sh -c \"echo " + "''" + " > " + udc_ + "\""); std::string cmd("echo " + pwd_ + " | sudo -S sh -c \"echo " + "''" + " > " + udc_ + "\"");
int err = 0; int err = 0;
utils::get_command_result(cmd.c_str(), -1, &err); utils::get_command_result(cmd.c_str(), -1, &err);
if(err == 0)
on_ = false;
return err; return err;
} }

View File

@ -20,6 +20,7 @@ class usb_device : public refer
std::string ep_path_[EP_NB_OF_DESCRIPTORS]; std::string ep_path_[EP_NB_OF_DESCRIPTORS];
ffs_strings ffs_strs_; ffs_strings ffs_strs_;
bool ffs_mode_ = true; bool ffs_mode_ = true;
bool on_ = false;
size_t max_packet_ = 512; size_t max_packet_ = 512;
void fill_if_descriptor(bool ffs_mode, usb_gadget * usbctx, struct usb_interface_descriptor * desc); void fill_if_descriptor(bool ffs_mode, usb_gadget * usbctx, struct usb_interface_descriptor * desc);

View File

@ -319,10 +319,23 @@ dyn_mem_ptr async_usb_gadget::handle_ctrl_setup(dyn_mem_ptr data)
return reply; return reply;
} }
int async_usb_gadget::inner_write_bulk_memory(int fd, uint8_t* buf, uint32_t* len, uint32_t bulk_size) int async_usb_gadget::inner_write_bulk_memory(int fd, uint8_t* buf, uint32_t* len, uint32_t bulk_size, data_source_ptr prog)
{ {
int w = 0, to = 0, err = 0, total = *len, off = 0, int w = 0, to = 0, err = 0, total = *len, off = 0,
size = *len; size = *len;
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(!cancel_io_) while(!cancel_io_)
{ {
@ -341,12 +354,20 @@ int async_usb_gadget::inner_write_bulk_memory(int fd, uint8_t* buf, uint32_t* le
continue; continue;
} }
err = errno; err = errno;
{
cur = off;
progr(all, cur, err);
}
break; break;
} }
to = 0; to = 0;
off += w; off += w;
want_bytes_in_ -= w; want_bytes_in_ -= w;
{
cur = off;
progr(all, cur, err);
}
if(off >= total) if(off >= total)
break; break;
if(off + bulk_size < total) if(off + bulk_size < total)
@ -497,10 +518,10 @@ void async_usb_gadget::thread_read_bulk(int fd)
continue; continue;
} }
statu_out_ = WORKER_STATUS_BUSY;
buf->set_session_id(session_id_); buf->set_session_id(session_id_);
l = read(fd, buf->ptr(), bulk_size);
statu_out_ = WORKER_STATUS_IDLE; statu_out_ = WORKER_STATUS_IDLE;
l = read(fd, buf->ptr(), bulk_size);
statu_out_ = WORKER_STATUS_BUSY;
if (!run_) if (!run_)
{ {
buf->release(); buf->release();

View File

@ -100,7 +100,7 @@ class async_usb_gadget : public refer
dyn_mem_ptr handle_ctrl_message(dyn_mem_ptr data); dyn_mem_ptr handle_ctrl_message(dyn_mem_ptr data);
dyn_mem_ptr handle_ctrl_setup(dyn_mem_ptr data); // user command ... dyn_mem_ptr handle_ctrl_setup(dyn_mem_ptr data); // user command ...
int inner_write_bulk_memory(int fd, uint8_t* buf, uint32_t* len/*in - to sent; out - real sent*/, uint32_t bulk_size); // return error code int inner_write_bulk_memory(int fd, uint8_t* buf, uint32_t* len/*in - to sent; out - real sent*/, uint32_t bulk_size, data_source_ptr prog = nullptr); // return error code
int inner_write_bulk(int fd, data_source_ptr data, dyn_mem_ptr mem/*to load data in if data was not memory*/, uint32_t bulk_size); // return error int inner_write_bulk(int fd, data_source_ptr data, dyn_mem_ptr mem/*to load data in if data was not memory*/, uint32_t bulk_size); // return error
dyn_mem_ptr handle_bulk_command(dyn_mem_ptr data, uint32_t* used, packet_data_base_ptr* pkd); dyn_mem_ptr handle_bulk_command(dyn_mem_ptr data, uint32_t* used, packet_data_base_ptr* pkd);