From 04bf01827488f05a57d1b53fdfad39d479916539 Mon Sep 17 00:00:00 2001 From: gb <741021719@qq.com> Date: Sat, 16 Dec 2023 15:27:40 +0800 Subject: [PATCH] =?UTF-8?q?BULK=5FOUT=E8=AF=BB=E6=95=B0=E6=8D=AE=E7=8A=B6?= =?UTF-8?q?=E6=80=81=EF=BC=8C=E5=AE=9A=E4=B9=89=E4=B8=BAIDLE=EF=BC=9Bmemor?= =?UTF-8?q?y=E6=95=B0=E6=8D=AE=E6=BA=90=E5=8F=91=E9=80=81=E6=97=B6?= =?UTF-8?q?=EF=BC=8C=E5=A2=9E=E5=8A=A0=E8=BF=9B=E5=BA=A6=E9=80=9A=E7=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sdk/base/data.cpp | 27 +++++++++++++++------------ sdk/base/data.h | 2 +- usb/usb_dev.cpp | 34 ++++++++++++++++++++++++++++++++++ usb/usb_dev.h | 1 + usb/usb_io.cpp | 27 ++++++++++++++++++++++++--- usb/usb_io.h | 2 +- 6 files changed, 76 insertions(+), 17 deletions(-) diff --git a/sdk/base/data.cpp b/sdk/base/data.cpp index 3201ece..2af22e5 100644 --- a/sdk/base/data.cpp +++ b/sdk/base/data.cpp @@ -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_); + 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_]) { diff --git a/sdk/base/data.h b/sdk/base/data.h index c717d42..1dd5fde 100644 --- a/sdk/base/data.h +++ b/sdk/base/data.h @@ -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 diff --git a/usb/usb_dev.cpp b/usb/usb_dev.cpp index 8bcf264..fbb4889 100644 --- a/usb/usb_dev.cpp +++ b/usb/usb_dev.cpp @@ -688,6 +688,9 @@ int usb_device::close_device(void) } int usb_device::pull_up(std::string* msg) { + if(on_) + return 0; + { int dst = open(udc_.c_str(), O_RDWR); if(dst == -1) @@ -700,6 +703,7 @@ int usb_device::pull_up(std::string* msg) 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()); + on_ = true; return 0; } else @@ -711,6 +715,9 @@ int usb_device::pull_up(std::string* msg) int err = 0; std::string info(utils::get_command_result(cmd.c_str(), -1, &err)); + if(err == 0) + on_ = true; + if(msg) *msg = std::move(info); @@ -718,10 +725,37 @@ int usb_device::pull_up(std::string* msg) } 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_ + "\""); int err = 0; utils::get_command_result(cmd.c_str(), -1, &err); + if(err == 0) + on_ = false; return err; } diff --git a/usb/usb_dev.h b/usb/usb_dev.h index 4a96559..ef4577e 100644 --- a/usb/usb_dev.h +++ b/usb/usb_dev.h @@ -20,6 +20,7 @@ class usb_device : public refer std::string ep_path_[EP_NB_OF_DESCRIPTORS]; ffs_strings ffs_strs_; bool ffs_mode_ = true; + bool on_ = false; size_t max_packet_ = 512; void fill_if_descriptor(bool ffs_mode, usb_gadget * usbctx, struct usb_interface_descriptor * desc); diff --git a/usb/usb_io.cpp b/usb/usb_io.cpp index 9ff0b60..dfeadff 100644 --- a/usb/usb_io.cpp +++ b/usb/usb_io.cpp @@ -319,10 +319,23 @@ dyn_mem_ptr async_usb_gadget::handle_ctrl_setup(dyn_mem_ptr data) 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, 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 progr = empty_prog; + if (prog) + progr = real_prog; while(!cancel_io_) { @@ -341,12 +354,20 @@ int async_usb_gadget::inner_write_bulk_memory(int fd, uint8_t* buf, uint32_t* le continue; } err = errno; + { + cur = off; + progr(all, cur, err); + } break; } to = 0; off += w; want_bytes_in_ -= w; + { + cur = off; + progr(all, cur, err); + } if(off >= total) break; if(off + bulk_size < total) @@ -497,10 +518,10 @@ void async_usb_gadget::thread_read_bulk(int fd) continue; } - statu_out_ = WORKER_STATUS_BUSY; buf->set_session_id(session_id_); - l = read(fd, buf->ptr(), bulk_size); statu_out_ = WORKER_STATUS_IDLE; + l = read(fd, buf->ptr(), bulk_size); + statu_out_ = WORKER_STATUS_BUSY; if (!run_) { buf->release(); diff --git a/usb/usb_io.h b/usb/usb_io.h index b80f223..cfcdf81 100644 --- a/usb/usb_io.h +++ b/usb/usb_io.h @@ -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_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 dyn_mem_ptr handle_bulk_command(dyn_mem_ptr data, uint32_t* used, packet_data_base_ptr* pkd);