diff --git a/libuuu/CMakeLists.txt b/libuuu/CMakeLists.txt index 46d48a24..720fd32e 100644 --- a/libuuu/CMakeLists.txt +++ b/libuuu/CMakeLists.txt @@ -23,6 +23,7 @@ set(SOURCES zip.cpp fat.cpp rominfo.cpp + conf.cpp ) set(generated_files_dir "${CMAKE_BINARY_DIR}/libuuu/gen") diff --git a/libuuu/cmd.cpp b/libuuu/cmd.cpp index 57f989e2..fdcfd70f 100644 --- a/libuuu/cmd.cpp +++ b/libuuu/cmd.cpp @@ -41,11 +41,13 @@ #include "buffer.h" #include "sdp.h" #include "fastboot.h" +#include "conf.h" #include #include -#include -#include +#include +#include +#include static CmdMap g_cmd_map; static CmdObjCreateMap g_cmd_create_map; @@ -235,12 +237,19 @@ int get_string_in_square_brackets(string &cmd, string &context) uint32_t str_to_uint(string &str) { - if (str.size() > 2) + uint32_t val = 0; + std::stringstream ss; + if (str.size() > 2 && + str.substr(0, 2).compare("0x") == 0) { - if (str.substr(0, 2).compare("0x") == 0) - return strtoul(str.substr(2).c_str(), NULL, 16); + ss << std::hex << str.substr(2); } - return strtoul(str.c_str(), NULL, 10); + else + { + ss << std::dec << str; + } + ss >> val; + return val; } template shared_ptr new_cmd_obj(char *p) @@ -289,6 +298,7 @@ CmdObjCreateMap::CmdObjCreateMap() (*this)["_ALL:DONE"] = new_cmd_obj; (*this)["_ALL:DELAY"] = new_cmd_obj; + (*this)["_ALL:CONFIG"] = new_cmd_obj; (*this)["_ALL:SH"] = new_cmd_obj; (*this)["_ALL:SHELL"] = new_cmd_obj; (*this)["_ALL:<"] = new_cmd_obj; @@ -415,6 +425,73 @@ int CmdDelay::run(CmdCtx *) return 0; } +int CmdConfig::parser(char * /*p*/) +{ + try + { + size_t pos = 0; + string param = get_next_param(m_cmd, pos); + + if (param.find(':') != string::npos) + param = get_next_param(m_cmd, pos); + + if (str_to_upper(param) != "CONFIG") + { + string err = "CmdConfig::parser Uknown Commnd:"; + err += param; + set_last_err_string(err); + return -1; + } + + string args = get_next_param(m_cmd, pos); + if (str_to_upper(args) == "USB::HID::MAX_PACKET_SIZE") + { + string size = get_next_param(m_cmd, pos); + Conf::GetConf().m_USB.m_HIDMaxTransfer = str_to_uint(size); + cout << "changed USB::HID::MAX_PACKET_SIZE to " << Conf::GetConf().m_USB.m_HIDMaxTransfer << endl; + } + else if (str_to_upper(args) == "USB::HID::TIMEOUT" ) + { + string ms = get_next_param(m_cmd, pos); + Conf::GetConf().m_USB.m_HIDTimeout = str_to_uint(ms); + cout << "changed USB::HID::TIMEOUT to " << Conf::GetConf().m_USB.m_HIDTimeout << endl; + } + else if (str_to_upper(args) == "USB::BULK::MAX_PACKET_SIZE" ) + { + string size = get_next_param(m_cmd, pos); + Conf::GetConf().m_USB.m_BulkMaxTransfer = str_to_uint(size); + cout << "changed USB::BULK::MAX_PACKET_SIZE to " << Conf::GetConf().m_USB.m_BulkMaxTransfer << endl; + } + else if (str_to_upper(args) == "USB::BULK::TIMEOUT") + { + string ms = get_next_param(m_cmd, pos); + Conf::GetConf().m_USB.m_BulkTimeout = str_to_uint(ms); + cout << "changed CONF::USB::BULK::TIMEOUT to " << Conf::GetConf().m_USB.m_BulkTimeout << endl; + } + else + { + string err = "CmdConfig::parser invalid parameter:"; + err += m_cmd; + set_last_err_string(err); + return -1; + + } + } + catch (string const& except) + { + string err(" error at line : "); + err += except; + throw err; + } + return 0; +} + +int CmdConfig::run(CmdCtx *) +{ + return 0; +} + + int CmdShell::parser(char * p) { if (p) diff --git a/libuuu/cmd.h b/libuuu/cmd.h index 6f05adba..b187cd6f 100644 --- a/libuuu/cmd.h +++ b/libuuu/cmd.h @@ -35,6 +35,7 @@ #include #include #include +#include #include "liberror.h" #include "libcomm.h" @@ -107,7 +108,6 @@ class CmdBase string param; if (get_string_in_square_brackets(prot, param)) return -1; - if (!param.empty()) { size_t param_pos = 0; @@ -161,6 +161,16 @@ class CmdDelay :public CmdBase int run(CmdCtx *p); }; +class CmdConfig :public CmdBase +{ +public: + virtual int parser(char *p = NULL); + CmdConfig(char *p) :CmdBase(p) { }; + int run(CmdCtx *p); +}; + + + class CmdShell : public CmdBase { public: diff --git a/libuuu/conf.cpp b/libuuu/conf.cpp new file mode 100644 index 00000000..cbbe07e0 --- /dev/null +++ b/libuuu/conf.cpp @@ -0,0 +1,51 @@ +/* +* Copyright 2018 NXP. +* +* Redistribution and use in source and binary forms, with or without modification, +* are permitted provided that the following conditions are met: +* +* Redistributions of source code must retain the above copyright notice, this +* list of conditions and the following disclaimer. +* +* Redistributions in binary form must reproduce the above copyright notice, this +* list of conditions and the following disclaimer in the documentation and/or +* other materials provided with the distribution. +* +* Neither the name of the NXP Semiconductor nor the names of its +* contributors may be used to endorse or promote products derived from this +* software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +* POSSIBILITY OF SUCH DAMAGE. +* +*/ + + +#include "conf.h" + + +Conf::Conf() +{ + m_USB.m_BulkMaxTransfer = 0x10000; + m_USB.m_BulkTimeout = 2000; + m_USB.m_HIDMaxTransfer= 0x10000; + m_USB.m_HIDTimeout = 2000; +} + +Conf& Conf::GetConf() +{ + static Conf instance; + return instance; +} + + + diff --git a/libuuu/conf.h b/libuuu/conf.h new file mode 100644 index 00000000..49fb8502 --- /dev/null +++ b/libuuu/conf.h @@ -0,0 +1,52 @@ +/* +* Copyright 2018 NXP. +* +* Redistribution and use in source and binary forms, with or without modification, +* are permitted provided that the following conditions are met: +* +* Redistributions of source code must retain the above copyright notice, this +* list of conditions and the following disclaimer. +* +* Redistributions in binary form must reproduce the above copyright notice, this +* list of conditions and the following disclaimer in the documentation and/or +* other materials provided with the distribution. +* +* Neither the name of the NXP Semiconductor nor the names of its +* contributors may be used to endorse or promote products derived from this +* software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +* POSSIBILITY OF SUCH DAMAGE. +* +*/ + +#include +#include + + +class Conf +{ + public: + struct UsbConf + { + size_t m_BulkMaxTransfer; + uint64_t m_BulkTimeout; + size_t m_HIDMaxTransfer; + uint64_t m_HIDTimeout; + }; + public: + static Conf& GetConf(); + UsbConf m_USB; + + private: + Conf(); +}; diff --git a/libuuu/fastboot.cpp b/libuuu/fastboot.cpp index ba108036..4a379e14 100644 --- a/libuuu/fastboot.cpp +++ b/libuuu/fastboot.cpp @@ -43,6 +43,7 @@ #include #include #include "sparse.h" +#include "conf.h" int FastBoot::Transport(string cmd, void *p, size_t size, vector *input) { @@ -170,7 +171,6 @@ int FBCmd::run(CmdCtx *ctx) if (dev.open(ctx->m_dev)) return -1; - dev.m_timeout = m_timeout; FastBoot fb(&dev); string cmd; @@ -212,6 +212,10 @@ int FBCopy::parser(char *p) size_t pos = 0; string s; + + if (parser_protocal(p, pos)) + return -1; + s = get_next_param(m_cmd, pos); if (s.find(":") != s.npos) s = get_next_param(m_cmd, pos); @@ -310,11 +314,13 @@ int FBCopy::run(CmdCtx *ctx) nt.total = buff->size(); call_notify(nt); - for (i = 0; i < buff->size(); i += this->m_Maxsize_pre_cmd) + for (i = 0; + i < buff->size(); + i += Conf::GetConf().m_USB.m_BulkMaxTransfer) { size_t sz = buff->size() - i; - if (sz > m_Maxsize_pre_cmd) - sz = m_Maxsize_pre_cmd; + if (sz > Conf::GetConf().m_USB.m_BulkMaxTransfer) + sz = Conf::GetConf().m_USB.m_BulkMaxTransfer; cmd.format("donwload:%08X", sz); if (fb.Transport(cmd, buff->data() + i, sz)) @@ -515,7 +521,6 @@ int FBFlashCmd::run(CmdCtx *ctx) return -1; FastBoot fb(&dev); - dev.m_timeout = m_timeout; shared_ptr pdata = get_file_buffer(m_filename); if (pdata == NULL) diff --git a/libuuu/fastboot.h b/libuuu/fastboot.h index 6fddb4b3..80b1029a 100644 --- a/libuuu/fastboot.h +++ b/libuuu/fastboot.h @@ -136,14 +136,13 @@ class FBDownload : public CmdBase int run(CmdCtx *ctx); }; -class FBCopy : public CmdBase +class FBCopy : public FBCmd { public: string m_local_file; string m_target_file; bool m_bDownload; - size_t m_Maxsize_pre_cmd; int parser(char *p=NULL); - FBCopy(char *p) :CmdBase(p) { m_Maxsize_pre_cmd = 0x10000; }; + FBCopy(char *p) :FBCmd(p) { }; int run(CmdCtx *ctx); }; diff --git a/libuuu/sdp.cpp b/libuuu/sdp.cpp index a5041b81..38319675 100644 --- a/libuuu/sdp.cpp +++ b/libuuu/sdp.cpp @@ -355,7 +355,6 @@ int SDPJumpCmd::run(CmdCtx *ctx) int SDPBootlogCmd::run(CmdCtx *ctx) { HIDTrans dev; - dev.m_read_timeout = 2000; if (dev.open(ctx->m_dev)) return -1; @@ -383,4 +382,4 @@ int SDPBootlogCmd::run(CmdCtx *ctx) } } return 0; -} \ No newline at end of file +} diff --git a/libuuu/trans.cpp b/libuuu/trans.cpp index 94e3b4c1..af547441 100644 --- a/libuuu/trans.cpp +++ b/libuuu/trans.cpp @@ -28,11 +28,12 @@ * POSSIBILITY OF SUCH DAMAGE. * */ - +#include #include "trans.h" #include "libuuu.h" #include "liberror.h" #include "libusb.h" +#include "conf.h" extern "C" { @@ -112,26 +113,38 @@ int HIDTrans::read(void *buff, size_t size, size_t *rsize) { int ret; int actual; - ret = libusb_interrupt_transfer( - (libusb_device_handle *)m_devhandle, - 0x81, - (uint8_t*)buff, - size, - &actual, - m_read_timeout - ); - - *rsize = actual; - if (ret < 0) + for (size_t i = 0; i < size; ) { - string error; - string err; - err = "HID(R):"; - err += libusb_error_name(ret); - set_last_err_string(err); - return ret; - } + uint8_t *p = (uint8_t *)buff; + p += i; + size_t sz; + sz = size - i; + if (sz > Conf::GetConf().m_USB.m_HIDMaxTransfer) + sz = Conf::GetConf().m_USB.m_HIDMaxTransfer; + + ret = libusb_interrupt_transfer( + (libusb_device_handle *)m_devhandle, + 0x81, + p, + sz, + &actual, + Conf::GetConf().m_USB.m_HIDTimeout + ); + + *rsize = actual; + + if (ret < 0) + { + string error; + string err; + err = "HID(R):"; + err += libusb_error_name(ret); + set_last_err_string(err); + return ret; + } + i += sz; + } return 0; } @@ -140,14 +153,15 @@ int BulkTrans::write(void *buff, size_t size) { int ret; int actual_lenght; - for (size_t i = 0; i < size; i += m_MaxTransPreRequest) + int retries_on_timeout=3; + for (size_t i = 0; i < size; ) { uint8_t *p = (uint8_t *)buff; p += i; size_t sz; sz = size - i; - if (sz > m_MaxTransPreRequest) - sz = m_MaxTransPreRequest; + if (sz > Conf::GetConf().m_USB.m_BulkMaxTransfer) + sz = Conf::GetConf().m_USB.m_BulkMaxTransfer; ret = libusb_bulk_transfer( (libusb_device_handle *)m_devhandle, @@ -155,18 +169,53 @@ int BulkTrans::write(void *buff, size_t size) p, sz, &actual_lenght, - m_timeout + Conf::GetConf().m_USB.m_BulkTimeout ); if (ret < 0) { - string error; - string err; - err = "Bulk(W):"; - err += libusb_error_name(ret); - set_last_err_string(err); - return ret; + if (ret == LIBUSB_ERROR_TIMEOUT && actual_lenght != 0) + { + cerr << "warning : timeout, transferred :" + << actual_lenght + << " on " + << sz + << " after " + << Conf::GetConf().m_USB.m_BulkTimeout + << " ms" + << endl; + retries_on_timeout = 3; + } + else if (ret == LIBUSB_ERROR_TIMEOUT && actual_lenght == 0 && retries_on_timeout != 0) + { + cerr << "warning : timeout, transferred :" + << actual_lenght + << " on " + << sz + << " after " + << Conf::GetConf().m_USB.m_BulkTimeout + << " ms, retry " + << retries_on_timeout + << endl; + retries_on_timeout --; + sz = actual_lenght; + } + else + { + string error; + string err; + err = "Bulk(W):"; + err += libusb_error_name(ret); + cerr << " transferred :" << actual_lenght << " on " << sz << endl; + set_last_err_string(err); + return ret; + } + } + else + { + retries_on_timeout = 3; } + i += sz; } //Send zero package @@ -223,7 +272,7 @@ int BulkTrans::read(void *buff, size_t size, size_t *rsize) p, size, &actual_lenght, - m_timeout + Conf::GetConf().m_USB.m_BulkTimeout ); *rsize = actual_lenght; diff --git a/libuuu/trans.h b/libuuu/trans.h index ad2cb04b..e5807500 100644 --- a/libuuu/trans.h +++ b/libuuu/trans.h @@ -78,8 +78,7 @@ class HIDTrans : public USBTrans { int m_set_report; public: - int m_read_timeout; - HIDTrans() { m_set_report = 9; m_read_timeout = 1000; } + HIDTrans() { m_set_report = 9; } ~HIDTrans() { if (m_devhandle) close(); m_devhandle = NULL; } int write(void *buff, size_t size); int read(void *buff, size_t size, size_t *return_size); @@ -89,17 +88,13 @@ class BulkTrans : public USBTrans { void Init() { - m_MaxTransPreRequest = 0x100000; m_b_send_zero = 0; - m_timeout = 2000; } public: EPInfo m_ep_in; EPInfo m_ep_out; - size_t m_MaxTransPreRequest; int m_b_send_zero; - uint64_t m_timeout; BulkTrans() { Init(); diff --git a/msvc/libuuu.vcxproj b/msvc/libuuu.vcxproj index 292346e9..6c6b5ab1 100644 --- a/msvc/libuuu.vcxproj +++ b/msvc/libuuu.vcxproj @@ -33,6 +33,7 @@ + @@ -208,4 +209,4 @@ - \ No newline at end of file +