diff --git a/src/common/auth.cpp b/src/common/auth.cpp index 31b6d350f64607606231ac98d9923183d775dac2..4419a58df7d30e1679ee17b2c8b87999ee3633d6 100644 --- a/src/common/auth.cpp +++ b/src/common/auth.cpp @@ -20,7 +20,7 @@ #include using namespace Hdc; -#define BIGNUMTOBIT 32 +#define BIGNUMTOBIT 32 namespace HdcAuth { // ---------------------------------------Cheat compiler--------------------------------------------------------- @@ -188,7 +188,7 @@ bool GenerateKey(const char *file) BN_set_word(exponent, RSA_F4); RSA_generate_key_ex(rsa, 2048, exponent, nullptr); EVP_PKEY_set1_RSA(publicKey, rsa); - old_mask = umask(077); // 077:permission + old_mask = umask(077); // 077:permission fKey = fopen(file, "w"); if (!fKey) { @@ -248,15 +248,13 @@ int GetUserKeyPath(string &path) const char hdcKeyFile[] = "hdckey"; char buf[BUF_SIZE_DEFAULT]; size_t len = BUF_SIZE_DEFAULT; - char sep[2] = ""; - sep[0] = PREF_SEPARATOR; // $home if (uv_os_homedir(buf, &len) < 0) return false; path = string(buf) + string(sep) + string(harmoneyPath) + string(sep); if (stat(path.c_str(), &status)) { uv_fs_t req; - uv_fs_mkdir(nullptr, &req, path.c_str(), 0750, nullptr); // 0750:permission + uv_fs_mkdir(nullptr, &req, path.c_str(), 0750, nullptr); // 0750:permission uv_fs_req_cleanup(&req); if (req.result < 0) { WRITE_LOG(LOG_DEBUG, "Cannot mkdir '%s'", path.c_str()); diff --git a/src/common/base.cpp b/src/common/base.cpp index ec077d92b183b4d13a457ca90418573dd55679a2..c81fcc757999a8b81903865c135a45378d9fb2a4 100644 --- a/src/common/base.cpp +++ b/src/common/base.cpp @@ -195,8 +195,7 @@ namespace Base { WRITE_LOG(LOG_WARN, "SetTcpOptions nullptr Ptr"); return; } - int timeout = GLOBAL_TIMEOUT; - uv_tcp_keepalive(tcpHandle, 1, timeout / 2); + uv_tcp_keepalive(tcpHandle, 1, GLOBAL_TIMEOUT); // if MAX_SIZE_IOBUF==5k,bufMaxSize at least 40k. It must be set to io 8 times is more appropriate, // otherwise asynchronous IO is too fast, a lot of IO is wasted on IOloop, transmission speed will decrease int bufMaxSize = GetMaxBufSize() * maxBufFactor; @@ -575,7 +574,7 @@ namespace Base { #endif #else string sKey = key; - string sBuf = "getparam " + sKey; + string sBuf = "param get " + sKey; RunPipeComand(sBuf.c_str(), value, sizeOutBuf, true); #endif value[sizeOutBuf - 1] = '\0'; @@ -690,7 +689,8 @@ namespace Base { WRITE_LOG(LOG_FATAL, "Tmppath failed"); return ERR_API_FAIL; } - if (snprintf_s(bufPath, sizeof(bufPath), sizeof(bufPath) - 1, "%s/%s.pid", buf, procname) < 0) { + if (snprintf_s(bufPath, sizeof(bufPath), sizeof(bufPath) - 1, "%s%c.%s.pid", buf, Base::GetPathSep(), procname) + < 0) { return ERR_BUF_OVERFLOW; } int pid = static_cast(getpid()); @@ -698,7 +698,8 @@ namespace Base { return ERR_BUF_OVERFLOW; } // no need to CanonicalizeSpecPath, else not work - int fd = open(bufPath, O_RDWR | O_CREAT, (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)); + umask(0); + int fd = open(bufPath, O_RDWR | O_CREAT, 0666); // 0666:permission if (fd < 0) { WRITE_LOG(LOG_FATAL, "Open mutex file \"%s\" failed!!!Errno:%d\n", buf, errno); return ERR_FILE_OPEN; @@ -789,14 +790,19 @@ namespace Base { return (((uint64_t)ntohl(val)) << 32) + ntohl(val >> 32); } - string GetFullFilePath(const string &s) - { // cannot use s.rfind(std::filesystem::path::preferred_separator + char GetPathSep() + { #ifdef _WIN32 const char sep = '\\'; #else const char sep = '/'; #endif - size_t i = s.rfind(sep, s.length()); + return sep; + } + + string GetFullFilePath(const string &s) + { // cannot use s.rfind(std::filesystem::path::preferred_separator + size_t i = s.rfind(GetPathSep(), s.length()); if (i != string::npos) { return (s.substr(i + 1, s.length() - i)); } @@ -1166,8 +1172,9 @@ namespace Base { vector Md5Sum(uint8_t *buf, int size) { vector ret; - if (buf != nullptr && size > 0) { // dummy for security temp - ret.push_back(0); + uint8_t md5Hash[MD5_DIGEST_LENGTH] = { 0 }; + if (EVP_Digest(buf, size, md5Hash, NULL, EVP_md5(), NULL)) { + ret.insert(ret.begin(), md5Hash, md5Hash + sizeof(md5Hash)); } return ret; } @@ -1184,5 +1191,18 @@ namespace Base { #endif return false; } + + bool IsAbsolutePath(string &path) + { + bool ret = false; +#ifdef _WIN32 + // shlwapi.h PathIsRelativeA not link in harmony project + // c:\ or UNC path '\\hostname\share\file' + ret = path.find(":\\") == 1 || path.find("\\\\") == 0; +#else + ret = path[0] == '/'; +#endif + return ret; + } } } // namespace Hdc diff --git a/src/common/base.h b/src/common/base.h index 13a759f47941adba80cebc7d5167a05bdcca7ad1..74a1b336b8dccda3a26aa32c66685da5df67b8a2 100644 --- a/src/common/base.h +++ b/src/common/base.h @@ -136,6 +136,8 @@ namespace Base { uv_os_sock_t DuplicateUvSocket(uv_tcp_t *tcp); vector Md5Sum(uint8_t *buf, int size); bool IsRoot(); + char GetPathSep(); + bool IsAbsolutePath(string &path); } // namespace base } // namespace Hdc diff --git a/src/common/channel.cpp b/src/common/channel.cpp index fa9a801a273142eaaec3637b55b326649c1c353f..8d944ac52c3e54f4e7c392496a5d2f022448e17c 100644 --- a/src/common/channel.cpp +++ b/src/common/channel.cpp @@ -101,6 +101,7 @@ void HdcChannelBase::ReadStream(uv_stream_t *tcp, ssize_t nread, const uv_buf_t { int size = 0; int indexBuf = 0; + int childRet = 0; bool needExit = false; HChannel hChannel = (HChannel)tcp->data; HdcChannelBase *thisClass = (HdcChannelBase *)hChannel->clsChannel; @@ -129,9 +130,12 @@ void HdcChannelBase::ReadStream(uv_stream_t *tcp, ssize_t nread, const uv_buf_t if (hChannel->availTailIndex - DWORD_SERIALIZE_SIZE < size) { break; } - if (thisClass->ReadChannel(hChannel, (uint8_t *)hChannel->ioBuf + DWORD_SERIALIZE_SIZE + indexBuf, size) < 0) { - needExit = true; - break; + childRet = thisClass->ReadChannel(hChannel, (uint8_t *)hChannel->ioBuf + DWORD_SERIALIZE_SIZE + indexBuf, size); + if (childRet < 0) { + if (!hChannel->keepAlive) { + needExit = true; + break; + } } // update io hChannel->availTailIndex -= (DWORD_SERIALIZE_SIZE + size); diff --git a/src/common/define.h b/src/common/define.h index 7b1f6396c42872e158383b9adc951a9f5f55ab22..20ba51818db3d8a799ca141a5c7585246c39ff96 100644 --- a/src/common/define.h +++ b/src/common/define.h @@ -20,9 +20,9 @@ namespace Hdc { // ############################## config ####################################### constexpr uint8_t MINOR_TIMEOUT = 5; constexpr uint8_t SIZE_THREAD_POOL = 16; -constexpr uint8_t GLOBAL_TIMEOUT = 60; +constexpr uint8_t GLOBAL_TIMEOUT = 30; constexpr uint16_t DEFAULT_PORT = 8710; -constexpr uint16_t MAX_SIZE_IOBUF = 5120; // USB EP block max size about 10k +constexpr uint16_t MAX_SIZE_IOBUF = 5120; // USB EP block max size about 10k, USBFFS_BULKSIZE 16384 constexpr bool ENABLE_IO_CHECKSUM = false; const string DEFAULT_SERVER_ADDR = "127.0.0.1:8710"; @@ -46,7 +46,7 @@ constexpr uint16_t UV_DEFAULT_INTERVAL = 250; // ms constexpr uint16_t VER_PROTOCOL = 0x01; constexpr uint16_t EXTRA_ALLOC_SIZE = 2048; // double-word(hex)=[0]major[1][2]minor[3][4]version[5]fix(a-p)[6][7]reserve -constexpr uint32_t HDC_VERSION_NUMBER = 0x10101500; // 1.1.1b=0x10101100 +constexpr uint32_t HDC_VERSION_NUMBER = 0x10101700; // 1.1.1b=0x10101100 constexpr uint32_t HDC_BUF_MAX_BYTES = 1024000000; const string WHITE_SPACES = " \t\n\r"; @@ -88,10 +88,6 @@ const string CMDSTR_APP_INSTALL = "install"; const string CMDSTR_APP_UNINSTALL = "uninstall"; const string CMDSTR_APP_SIDELOAD = "sideload"; const string CMDSTR_LIST_JDWP = "jpid"; -#ifdef _WIN32 -constexpr char PREF_SEPARATOR = '\\'; -#else -constexpr char PREF_SEPARATOR = '/'; -#endif +const string CMDSTR_INNER_ENABLE_KEEPALIVE = "alive"; } // namespace Hdc #endif // HDC_DEFINE_H diff --git a/src/common/define_plus.h b/src/common/define_plus.h index 0d94c23bd7617ad8b7a634c8a7c68197390b2e92..2327346f3950143308b23cf4b6b4d2b42bda76d5 100644 --- a/src/common/define_plus.h +++ b/src/common/define_plus.h @@ -113,6 +113,7 @@ enum HdcCommand { CMD_KERNEL_TARGET_DISCONNECT, CMD_KERNEL_ECHO, CMD_KERNEL_ECHO_RAW, + CMD_KERNEL_ENABLE_KEEPALIVE, // One-pass simple commands CMD_UNITY_EXECUTE = 1000, CMD_UNITY_REMOUNT, @@ -290,6 +291,7 @@ struct HdcChannel { bool serverOrClient; // client's channel/ server's channel bool childCleared; bool interactiveShellMode; // Is shell interactive mode + bool keepAlive; // channel will not auto-close by server std::atomic sendRef; uint32_t targetSessionId; // child work diff --git a/src/common/file.cpp b/src/common/file.cpp index ab105434a4302832d91314773b9dc2a5002aaf71..4e3cd9059c6eac7a9cab950a127dd0d8aefe4424 100644 --- a/src/common/file.cpp +++ b/src/common/file.cpp @@ -34,9 +34,6 @@ void HdcFile::StopTask() singalStop = true; }; -// Send supported below styles -// send|recv path/filename path/filename -// send|recv filename path bool HdcFile::BeginTransfer(CtxFile *context, const string &command) { int argc = 0; @@ -49,7 +46,7 @@ bool HdcFile::BeginTransfer(CtxFile *context, const string &command) } return false; } - if (!GetLocalRemotePath(context, command.c_str(), argc, argv)) { + if (!SetMasterParameters(context, command.c_str(), argc, argv)) { delete[]((char *)argv); return false; } @@ -67,12 +64,13 @@ bool HdcFile::BeginTransfer(CtxFile *context, const string &command) return ret; } -bool HdcFile::GetLocalRemotePath(CtxFile *context, const char *command, int argc, char **argv) +bool HdcFile::SetMasterParameters(CtxFile *context, const char *command, int argc, char **argv) { int srcArgvIndex = 0; const string CMD_OPTION_TSTMP = "-a"; const string CMD_OPTION_SYNC = "-sync"; const string CMD_OPTION_ZIP = "-z"; + for (int i = 0; i < argc - CMD_ARG1_COUNT; i++) { if (argv[i] == CMD_OPTION_ZIP) { context->transferConfig.compressType = COMPRESS_LZ4; @@ -81,8 +79,15 @@ bool HdcFile::GetLocalRemotePath(CtxFile *context, const char *command, int argc context->transferConfig.updateIfNew = true; ++srcArgvIndex; } else if (argv[i] == CMD_OPTION_TSTMP) { + // The time zone difference may cause the display time on the PC and the + // device to differ by several hours + // + // ls -al --full-time context->transferConfig.holdTimestamp = true; ++srcArgvIndex; + } else if (argv[i] == CMD_OPTION_CLIENTCWD) { + context->transferConfig.clientCwd = argv[i + 1]; + srcArgvIndex += CMD_ARG1_COUNT; // skip 2args } else if (argv[i][0] == '-') { LogMsg(MSG_FAIL, "Unknow file option: %s", argv[i]); return false; @@ -90,6 +95,10 @@ bool HdcFile::GetLocalRemotePath(CtxFile *context, const char *command, int argc } context->remotePath = argv[argc - 1]; context->localPath = argv[argc - 2]; + if (taskInfo->serverOrDaemon) { + // master and server + ExtractRelativePath(context->transferConfig.clientCwd, context->localPath); + } if (!Base::CheckDirectoryOrPath(context->localPath.c_str(), true, true)) { LogMsg(MSG_FAIL, "Src not exist, path: %s", context->localPath.c_str()); return false; @@ -131,7 +140,7 @@ bool HdcFile::SlaveCheck(uint8_t *payload, const int payloadSize) ctxNow.master = false; ctxNow.fsOpenReq.data = &ctxNow; // check path - childRet = SmartSlavePath(ctxNow.localPath, stat.optionalName.c_str()); + childRet = SmartSlavePath(stat.clientCwd, ctxNow.localPath, stat.optionalName.c_str()); if (childRet && ctxNow.transferConfig.updateIfNew) { // file exist and option need update // if is newer uv_fs_t fs; diff --git a/src/common/file.h b/src/common/file.h index a36e4239471f571d9c0b301e8e334e85f092635d..230d3f50a829213ebe4cb1613796488bb066423d 100644 --- a/src/common/file.h +++ b/src/common/file.h @@ -32,7 +32,7 @@ private: void WhenTransferFinish(CtxFile *context); bool BeginTransfer(CtxFile *context, const string &command); void TransferSummary(CtxFile *context); - bool GetLocalRemotePath(CtxFile *context, const char *command, int argc, char **argv); + bool SetMasterParameters(CtxFile *context, const char *command, int argc, char **argv); }; } // namespace Hdc diff --git a/src/common/forward.cpp b/src/common/forward.cpp index de24a63b936a1450bce424843b5840ac8d637393..b096cbb996a91ec2b9fe562707bbaf080d6ae026 100644 --- a/src/common/forward.cpp +++ b/src/common/forward.cpp @@ -227,16 +227,10 @@ void HdcForwardBase::AllocForwardBuf(uv_handle_t *handle, size_t sizeSuggested, } } -/// issue [6:50:12][0xac5d2920][D] Error ReadForwardBuf error:95 nread:-4095 file:end of file index:897090 -int g_index = 0; void HdcForwardBase::ReadForwardBuf(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) { HCtxForward ctx = (HCtxForward)stream->data; - g_index += nread; - // issue here if (nread < 0) { - WRITE_LOG(LOG_DEBUG, "Error ReadForwardBuf error:%d nread:%d file:%s index:%d", errno, nread, - uv_strerror(nread), g_index); ctx->thisClass->FreeContext(ctx, 0, true); return; } @@ -315,6 +309,9 @@ bool HdcForwardBase::DetechForwardType(HCtxForward ctxPoint) } else if (sFType == "dev") { ctxPoint->type = FORWARD_DEVICE; } else if (sFType == "localabstract") { + // daemon shell: /system/bin/socat abstract-listen:linux-abstract - + // daemon shell: /system/bin/socat - abstract-connect:linux-abstract + // host: hdc_std fport tcp:8080 localabstract:linux-abstract ctxPoint->type = FORWARD_ABSTRACT; } else if (sFType == "localreserved") { sNodeCfg = HARMONY_RESERVED_SOCKET_PREFIX + sNodeCfg; @@ -377,6 +374,41 @@ bool HdcForwardBase::SetupDevicePoint(HCtxForward ctxPoint) return true; } +bool HdcForwardBase::LocalAbstractConnect(uv_pipe_t *pipe, string &sNodeCfg) +{ + bool abstractRet = false; +#ifndef _WIN32 + int s = 0; + do { + if ((s = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0) { + break; + } + fcntl(s, F_SETFD, FD_CLOEXEC); + struct sockaddr_un addr; + Base::ZeroStruct(addr); + int addrLen = sNodeCfg.size() + offsetof(struct sockaddr_un, sun_path) + 1; + addr.sun_family = AF_LOCAL; + addr.sun_path[0] = 0; + + if (memcpy_s(addr.sun_path + 1, sizeof(addr.sun_path) - 1, sNodeCfg.c_str(), sNodeCfg.size()) != EOK) { + break; + }; + // local connect, ignore timeout + if (connect(s, (struct sockaddr *)&addr, addrLen) < 0) { + break; + } + if (uv_pipe_open(pipe, s)) { + break; + } + abstractRet = true; + } while (false); + if (!abstractRet && s > 0) { + close(s); + } +#endif + return abstractRet; +} + bool HdcForwardBase::SetupFilePoint(HCtxForward ctxPoint) { string &sNodeCfg = ctxPoint->localArgs[1]; @@ -397,7 +429,16 @@ bool HdcForwardBase::SetupFilePoint(HCtxForward ctxPoint) } else { uv_connect_t *connect = new uv_connect_t(); connect->data = ctxPoint; - uv_pipe_connect(connect, &ctxPoint->pipe, sNodeCfg.c_str(), ConnectTarget); + if (ctxPoint->type == FORWARD_ABSTRACT) { + bool abstractRet = LocalAbstractConnect(&ctxPoint->pipe, sNodeCfg); + SetupPointContinue(ctxPoint, abstractRet ? 0 : -1); + if (!abstractRet) { + ctxPoint->lastError = "LocalAbstractConnect failed"; + return false; + } + } else { + uv_pipe_connect(connect, &ctxPoint->pipe, sNodeCfg.c_str(), ConnectTarget); + } } return true; } diff --git a/src/common/forward.h b/src/common/forward.h index 5bc1cfb6ed461f1f9edc9574bd7daec89330a01f..9baf47025fbc5c97b5964152bbf0641027316ae6 100644 --- a/src/common/forward.h +++ b/src/common/forward.h @@ -93,6 +93,7 @@ private: bool SetupFilePoint(HCtxForward ctxPoint); bool ForwardCommandDispatch(const uint16_t command, uint8_t *payload, const int payloadSize); bool CommandForwardCheckResult(HCtxForward ctx, uint8_t *payload); + bool LocalAbstractConnect(uv_pipe_t *pipe, string &sNodeCfg); map mapCtxPoint; string taskCommand; diff --git a/src/common/serial_struct.h b/src/common/serial_struct.h index 70f75989951d9d1b4f1f833b7c386e93c34e4f96..b151f9dc98bf91d154f9f4ac2466ed7c4afdc09b 100644 --- a/src/common/serial_struct.h +++ b/src/common/serial_struct.h @@ -30,6 +30,9 @@ namespace SerialStruct { constexpr int fieldEight = 8; constexpr int fieldNine = 9; constexpr int fieldTen = 10; + constexpr int field11 = 11; + constexpr int field12 = 12; + constexpr int field13 = 13; template<> struct Descriptor { static auto type() @@ -43,7 +46,10 @@ namespace SerialStruct { Field("updateIfNew"), Field("compressType"), Field("holdTimestamp"), - Field("functionName")); + Field("functionName"), + Field("clientCwd"), + Field("reserve1"), + Field("reserve2")); } }; diff --git a/src/common/transfer.cpp b/src/common/transfer.cpp index d8ca298fa49ae8d7c60d295db1b5c625144b5c64..fc983cfd2db372298f325dfbcadb4559136426ae 100644 --- a/src/common/transfer.cpp +++ b/src/common/transfer.cpp @@ -20,6 +20,8 @@ #endif namespace Hdc { +constexpr uint64_t HDC_TIME_CONVERT_BASE = 1000000000; + HdcTransferBase::HdcTransferBase(HTaskInfo hTaskInfo) : HdcTaskBase(hTaskInfo) { @@ -47,6 +49,7 @@ bool HdcTransferBase::ResetCtx(CtxFile *context, bool full) context->remotePath = ""; context->transferBegin = 0; context->taskQueue.clear(); + Base::ZeroStruct(ctxNow.transferConfig); } return true; } @@ -111,17 +114,16 @@ void HdcTransferBase::OnFileClose(uv_fs_t *req) void HdcTransferBase::SetFileTime(CtxFile *context) { - if (context->transferConfig.holdTimestamp) { + if (!context->transferConfig.holdTimestamp) { return; } if (!context->transferConfig.mtime) { return; } uv_fs_t fs; - // clang-format off - uv_fs_futime(nullptr, &fs, context->fsOpenReq.result, context->transferConfig.atime, - context->transferConfig.mtime, nullptr); - // clang-format on + double aTimeSec = static_cast(context->transferConfig.atime) / HDC_TIME_CONVERT_BASE; + double mTimeSec = static_cast(context->transferConfig.mtime) / HDC_TIME_CONVERT_BASE; + uv_fs_futime(nullptr, &fs, context->fsOpenReq.result, aTimeSec, mTimeSec, nullptr); uv_fs_req_cleanup(&fs); } @@ -245,8 +247,10 @@ void HdcTransferBase::OnFileOpen(uv_fs_t *req) TransferConfig &st = context->transferConfig; st.fileSize = fs.statbuf.st_size; st.optionalName = context->localName; - st.atime = fs.statbuf.st_atim.tv_sec; - st.mtime = fs.statbuf.st_mtim.tv_sec; + if (st.holdTimestamp) { + st.atime = fs.statbuf.st_atim.tv_sec * HDC_TIME_CONVERT_BASE + fs.statbuf.st_atim.tv_nsec; + st.mtime = fs.statbuf.st_mtim.tv_sec * HDC_TIME_CONVERT_BASE + fs.statbuf.st_mtim.tv_nsec; + } st.path = context->remotePath; // update ctxNow=context child value context->fileSize = st.fileSize; @@ -311,8 +315,12 @@ int HdcTransferBase::GetSubFiles(const char *path, string filter, vector // https://en.cppreference.com/w/cpp/filesystem/is_directory // return true if file exist, false if file not exist -bool HdcTransferBase::SmartSlavePath(string &localPath, const char *optName) +bool HdcTransferBase::SmartSlavePath(string &cwd, string &localPath, const char *optName) { + if (taskInfo->serverOrDaemon) { + // slave and server + ExtractRelativePath(cwd, localPath); + } if (Base::CheckDirectoryOrPath(localPath.c_str(), true, false)) { return true; } @@ -320,7 +328,7 @@ bool HdcTransferBase::SmartSlavePath(string &localPath, const char *optName) int r = uv_fs_lstat(nullptr, &req, localPath.c_str(), nullptr); uv_fs_req_cleanup(&req); if (r == 0 && req.statbuf.st_mode & S_IFDIR) { // is dir - localPath = Base::StringFormat("%s%c%s", localPath.c_str(), PREF_SEPARATOR, optName); + localPath = Base::StringFormat("%s%c%s", localPath.c_str(), Base::GetPathSep(), optName); } return false; } @@ -398,4 +406,12 @@ bool HdcTransferBase::CommandDispatch(const uint16_t command, uint8_t *payload, } return ret; } + +void HdcTransferBase::ExtractRelativePath(string &cwd, string &path) +{ + bool absPath = Base::IsAbsolutePath(path); + if (!absPath) { + path = cwd + path; + } +} } // namespace Hdc diff --git a/src/common/transfer.h b/src/common/transfer.h index 9fbc18ffaa6ff8d17a2b684f127e8b8b42e029b1..3b660cf777b9c63e9bec2bf009c473d41c1e988d 100644 --- a/src/common/transfer.h +++ b/src/common/transfer.h @@ -23,8 +23,8 @@ public: // used for child class struct TransferConfig { uint64_t fileSize; - uint64_t atime; - uint64_t mtime; + uint64_t atime; // ns + uint64_t mtime; // ns string options; string path; string optionalName; @@ -32,6 +32,9 @@ public: uint8_t compressType; bool holdTimestamp; string functionName; + string clientCwd; + string reserve1; + string reserve2; }; // used for HdcTransferBase. just base class use, not public struct TransferPayload { @@ -85,12 +88,14 @@ protected: } bool MatchPackageExtendName(string fileName, string extName); bool ResetCtx(CtxFile *context, bool full = false); - bool SmartSlavePath(string &localPath, const char *optName); + bool SmartSlavePath(string &cwd, string &localPath, const char *optName); void SetFileTime(CtxFile *context); + void ExtractRelativePath(string &cwd, string &path); CtxFile ctxNow; uint16_t commandBegin; uint16_t commandData; + const string CMD_OPTION_CLIENTCWD = "-cwd"; private: // dynamic IO context diff --git a/src/common/usb.cpp b/src/common/usb.cpp index ecd6bd23e9d5ac4736a99d57774df07de174c835..db39a445a2cde8f65bce30c6ccf7cfa4c78b616f 100644 --- a/src/common/usb.cpp +++ b/src/common/usb.cpp @@ -141,4 +141,4 @@ int HdcUSBBase::SendToHdcStream(HSession hSession, uv_stream_t *stream, uint8_t } return ret; } -} +} \ No newline at end of file diff --git a/src/daemon/daemon.cpp b/src/daemon/daemon.cpp index eb8ba1a815a7dfb4b65afd82118e7fdb44ddc66e..b3a28a440a645cf13d83eb475504ce0e4620126a 100644 --- a/src/daemon/daemon.cpp +++ b/src/daemon/daemon.cpp @@ -244,7 +244,7 @@ bool HdcDaemon::FetchCommand(HSession hSession, const uint32_t channelId, const } case CMD_KERNEL_CHANNEL_CLOSE: { // Daemon is only cleaning up the Channel task ClearOwnTasks(hSession, channelId); - if (*payload) { + if (*payload == 1) { --(*payload); Send(hSession->sessionId, channelId, CMD_KERNEL_CHANNEL_CLOSE, payload, 1); } @@ -293,7 +293,7 @@ bool HdcDaemon::ServerCommand(const uint32_t sessionId, const uint32_t channelId void HdcDaemon::JdwpNewFileDescriptor(const uint8_t *buf, const int bytesIO) { uint32_t pid = *(uint32_t *)(buf + 1); - uint32_t fd = *(uint32_t *)(buf + 5); // 5 : fd offset + uint32_t fd = *(uint32_t *)(buf + 5); // 5 : fd offset ((HdcJdwp *)clsJdwp)->SendJdwpNewFD(pid, fd); }; } // namespace Hdc diff --git a/src/daemon/daemon_app.cpp b/src/daemon/daemon_app.cpp index 817a62b5b98bebb8d5869e27eb98b4021e510237..4f04dc93eabd5712d724b7b876309bc2b055203b 100644 --- a/src/daemon/daemon_app.cpp +++ b/src/daemon/daemon_app.cpp @@ -65,7 +65,7 @@ bool HdcDaemonApp::CommandDispatch(const uint16_t command, uint8_t *payload, con size_t size = 256; uv_os_tmpdir(tmpPath, &size); dstPath = tmpPath; - dstPath += PREF_SEPARATOR; + dstPath += Base::GetPathSep(); #endif dstPath += ctxNow.transferConfig.optionalName; ctxNow.localPath = dstPath; @@ -148,4 +148,4 @@ void HdcDaemonApp::WhenTransferFinish(CtxFile *context) } else { } }; -} +} \ No newline at end of file diff --git a/src/daemon/daemon_usb.cpp b/src/daemon/daemon_usb.cpp index 0bf6316a5ffe378d18986add4c2b47c9694c0967..6f5a33b66defeb839c5ea13a0d60a4071e1b9090 100644 --- a/src/daemon/daemon_usb.cpp +++ b/src/daemon/daemon_usb.cpp @@ -249,7 +249,7 @@ int HdcDaemonUSB::SendUSBIOSync(HSession hSession, HUSB hMainUSB, const uint8_t WRITE_LOG(LOG_DEBUG, "BulkinWrite write EINTR, try again"); continue; } else { - WRITE_LOG(LOG_DEBUG, "BulkinWrite write fatal errno %d", err); + WRITE_LOG(LOG_FATAL, "BulkinWrite write fatal errno %d", err); isAlive = false; } break; diff --git a/src/daemon/jdwp.cpp b/src/daemon/jdwp.cpp index 8d8e99572f7d2b996e8100a468e4eeaaccac60d5..7a3b152636c0e44c78919b16e56fdef307a66a1f 100644 --- a/src/daemon/jdwp.cpp +++ b/src/daemon/jdwp.cpp @@ -80,40 +80,31 @@ void HdcJdwp::FreeContext(HCtxJdwp ctx) void HdcJdwp::ReadStream(uv_stream_t *pipe, ssize_t nread, const uv_buf_t *buf) { - bool ret = false; + bool ret = true; HCtxJdwp ctxJdwp = (HCtxJdwp)pipe->data; HdcJdwp *thisClass = (HdcJdwp *)ctxJdwp->thisClass; - char temp[5]; + char *p = ctxJdwp->buf; uint32_t pid = 0; - int offset = 0; - if (nread > 0) { - ctxJdwp->bufIndex += nread; - } - while (offset < ctxJdwp->bufIndex) { - if (nread == UV_ENOBUFS) { // It is definite enough, usually only 4 bytes - WRITE_LOG(LOG_DEBUG, "HdcJdwp::ReadStream IOBuf max"); - break; - } else if (nread <= 0 || nread != 4) { // 4 : 4 bytes - WRITE_LOG(LOG_DEBUG, "HdcJdwp::ReadStream program exit pid:%d", ctxJdwp->pid); - break; - } - int errCode = memcpy_s(temp, sizeof(temp), ctxJdwp->buf + offset, 4); // 4 : 4 bytes - if (errCode != EOK) { - break; - } - temp[4] = 0; // 4 : pid length - if (sscanf_s(temp, "%04x", &pid) != 1) { - WRITE_LOG(LOG_WARN, "could not decode PID number: '%s'", temp); - break; + if (nread == UV_ENOBUFS) { // It is definite enough, usually only 4 bytes + ret = false; + WRITE_LOG(LOG_DEBUG, "HdcJdwp::ReadStream IOBuf max"); + } else if (nread == 0) { + return; + } else if (nread < 0 || nread != 4) { // 4 : 4 bytes + ret = false; + WRITE_LOG(LOG_DEBUG, "HdcJdwp::ReadStream program exit pid:%d", ctxJdwp->pid); + } + if (ret) { + pid = atoi(p); + if (pid > 0) { + WRITE_LOG(LOG_DEBUG, "JDWP accept pid:%d", pid); + ctxJdwp->pid = pid; + thisClass->AdminContext(OP_ADD, pid, ctxJdwp); + ret = true; } - WRITE_LOG(LOG_DEBUG, "JDWP accept pid:%d", pid); - ctxJdwp->pid = pid; - thisClass->AdminContext(OP_ADD, pid, ctxJdwp); - offset += 4; // 4 : 4 bytes - ret = true; - break; // just 4bytes, now finish } + Base::ZeroArray(ctxJdwp->buf); if (!ret) { thisClass->FreeContext(ctxJdwp); } @@ -135,53 +126,32 @@ void HdcJdwp::AcceptClient(uv_stream_t *server, int status) } auto funAlloc = [](uv_handle_t *handle, size_t sizeSuggested, uv_buf_t *buf) -> void { HCtxJdwp ctxJdwp = (HCtxJdwp)handle->data; - buf->base = (char *)ctxJdwp->buf + ctxJdwp->bufIndex; - buf->len = sizeof(ctxJdwp->buf) - ctxJdwp->bufIndex; + buf->base = (char *)ctxJdwp->buf ; + buf->len = sizeof(ctxJdwp->buf); }; uv_read_start((uv_stream_t *)&ctxJdwp->pipe, funAlloc, ReadStream); } -// https://andycong.top/2020/03/27/libuv%E5%A4%9A%E7%BA%BF%E7%A8%8B%E4%B8%AD%E4%BD%BF%E7%94%A8uv-accept/ +// Test bash connnet(UNIX-domain sockets):nc -U path/jdwp-control < hexpid.file +// Test uv connect(pipe): 'uv_pipe_connect' bool HdcJdwp::JdwpListen() { #ifdef HDC_PCDEBUG // if test, canbe enable return true; - // nc -U path/jdwp-control < hexpid.file const char jdwpCtrlName[] = { 'j', 'd', 'w', 'p', '-', 'c', 'o', 'n', 't', 'r', 'o', 'l', 0 }; unlink(jdwpCtrlName); #else const char jdwpCtrlName[] = { '\0', 'j', 'd', 'w', 'p', '-', 'c', 'o', 'n', 't', 'r', 'o', 'l', 0 }; #endif - struct sockaddr_un addr; - socklen_t addrlen; - int s; - int pathlen = sizeof(jdwpCtrlName) - 1; - bool ret = false; const int DEFAULT_BACKLOG = 4; - - Base::ZeroStruct(addr); - addr.sun_family = AF_UNIX; - if (memcpy_s(addr.sun_path, sizeof(addr.sun_path), jdwpCtrlName, pathlen) != EOK) { - return false; - } - s = socket(AF_UNIX, SOCK_STREAM, 0); - WRITE_LOG(LOG_DEBUG, "JdwpListen begin"); - if (s < 0) { - WRITE_LOG(LOG_WARN, "could not create vm debug control socket. %d: %s", errno, strerror(errno)); - return false; - } - fcntl(s, F_SETFD, FD_CLOEXEC); + bool ret = false; while (true) { - addrlen = (pathlen + sizeof(addr.sun_family)); - if (bind(s, (struct sockaddr *)&addr, addrlen) < 0) { - WRITE_LOG(LOG_WARN, "could not bind vm debug control socket: %d: %s", errno, strerror(errno)); - break; - } uv_pipe_init(loop, &listenPipe, 0); listenPipe.data = this; - if (uv_pipe_open(&listenPipe, s)) { - break; + if ((uv_pipe_bind(&listenPipe, jdwpCtrlName))) { + WRITE_LOG(LOG_WARN, "Bind error : %d: %s", errno, strerror(errno)); + return 1; } if (uv_listen((uv_stream_t *)&listenPipe, DEFAULT_BACKLOG, AcceptClient)) { break; @@ -190,9 +160,7 @@ bool HdcJdwp::JdwpListen() ret = true; break; } - if (!ret) { - close(s); - } + // listenPipe close by stop return ret; } @@ -308,4 +276,4 @@ int HdcJdwp::Initial() } return RET_SUCCESS; } -} +} \ No newline at end of file diff --git a/src/daemon/jdwp.h b/src/daemon/jdwp.h index d885f6bc78a38c98b56d3d30c77419fe5c268cce..93b3b9021e9a26f26cb18c90750b98cc5cb1d511 100644 --- a/src/daemon/jdwp.h +++ b/src/daemon/jdwp.h @@ -35,9 +35,7 @@ private: uv_pipe_t pipe; HdcJdwp *thisClass; bool finish; - uint8_t buf[16]; // read read 4 bytes ascii - uint8_t bufIndex; - + char buf[sizeof(uint32_t)]; uint8_t dummy; uv_tcp_t jvmTCP; }; diff --git a/src/host/client.cpp b/src/host/client.cpp index 0878fe4a06f62cd612a9721614d978bd4e6be15a..5c8994097545607d7f64cf1aa16b94adf3cec7f3 100644 --- a/src/host/client.cpp +++ b/src/host/client.cpp @@ -46,7 +46,7 @@ uint32_t HdcClient::GetLastPID() WRITE_LOG(LOG_FATAL, "Tmppath failed"); return 0; } - string path = Base::StringFormat("%s/%s.pid", bufPath, SERVER_NAME.c_str()); + string path = Base::StringFormat("%s%c.%s.pid", bufPath, Base::GetPathSep(), SERVER_NAME.c_str()); Base::ReadBinFile(path.c_str(), (void **)&pidBuf, BUF_SIZE_TINY); int pid = atoi(pidBuf); // pid maybe 0 return pid; @@ -70,7 +70,7 @@ bool HdcClient::StartKillServer(const char *cmd, bool startOrKill) uv_kill(pid, SIGN_NUM); } } - HdcServer::CheckToPullUptrServer(channelHostPort.c_str()); + HdcServer::PullupServer(channelHostPort.c_str()); } else { if (isNowRunning && pid) { uv_kill(pid, SIGN_NUM); @@ -80,7 +80,7 @@ bool HdcClient::StartKillServer(const char *cmd, bool startOrKill) if (!strstr(cmd, " -r")) { return true; } - HdcServer::CheckToPullUptrServer(channelHostPort.c_str()); + HdcServer::PullupServer(channelHostPort.c_str()); } return true; } @@ -319,6 +319,11 @@ int HdcClient::PreHandshake(HChannel hChannel, const uint8_t *buf) } Send(hChannel->channelId, reinterpret_cast(hShake), sizeof(ChannelHandShake)); hChannel->handshakeOK = true; +#ifdef HDC_CHANNEL_KEEP_ALIVE + // Evaluation method, non long-term support + Send(hChannel->channelId, reinterpret_cast(CMDSTR_INNER_ENABLE_KEEPALIVE.c_str()), + CMDSTR_INNER_ENABLE_KEEPALIVE.size()); +#endif return RET_SUCCESS; } diff --git a/src/host/client.h b/src/host/client.h index 6d315d549b4d9582e57f4bce39f9100ed56157d6..2bfe2a55b918c7ed8d603563f15cad3dec4be312 100644 --- a/src/host/client.h +++ b/src/host/client.h @@ -32,7 +32,7 @@ private: static void AllocStdbuf(uv_handle_t *handle, size_t sizeWanted, uv_buf_t *buf); static void ReadStd(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf); static void CommandWorker(uv_timer_t *handle); - int ConnectServerForClient(const char *stringIP, uint16_t port); + int ConnectServerForClient(const char *ip, uint16_t port); int ReadChannel(HChannel hChannel, uint8_t *buf, const int bytesIO); int PreHandshake(HChannel hChannel, const uint8_t *buf); string AutoConnectKey(string &doCommand, const string &preConnectKey) const; diff --git a/src/host/host_app.cpp b/src/host/host_app.cpp index 7d88c5d51f8fa2f6acb1e398f7bf45e0e0e3872f..938af0056d684db7eb31947290b960c1344f5e97 100644 --- a/src/host/host_app.cpp +++ b/src/host/host_app.cpp @@ -37,13 +37,19 @@ bool HdcHostApp::BeginInstall(CtxFile *context, const char *command) } for (int i = 0; i < argc; ++i) { - if (!strncmp(argv[i], "-", 1)) { + if (!strcmp(argv[i], CMD_OPTION_CLIENTCWD.c_str())) { + if (i + 1 < argc) { + context->transferConfig.clientCwd = argv[i + 1]; + i += 1; // add content index + } + } else if (!strncmp(argv[i], "-", 1)) { if (options.size()) { options += " "; } options += argv[i]; } else { - const char *path = argv[i]; + string path = argv[i]; + ExtractRelativePath(context->transferConfig.clientCwd, path); if (MatchPackageExtendName(path, ".hap")) { context->taskQueue.push_back(path); } else { diff --git a/src/host/host_usb.cpp b/src/host/host_usb.cpp index e9380e17dfc0e32577c0a48a19f49dbfcd37725b..2b9a9f6d979a3e0bd00f8755b25420253b293c5e 100644 --- a/src/host/host_usb.cpp +++ b/src/host/host_usb.cpp @@ -131,7 +131,7 @@ bool HdcHostUSB::DetectMyNeed(libusb_device *device, string &sn) uv_timer_init(&hdcServer->loopMain, waitTimeDoCmd); waitTimeDoCmd->data = hSession; constexpr uint16_t PRECONNECT_INTERVAL = 3000; - uv_timer_start(waitTimeDoCmd, hdcServer->UsbPreConnect, GLOBAL_TIMEOUT, PRECONNECT_INTERVAL); + uv_timer_start(waitTimeDoCmd, hdcServer->UsbPreConnect, 0, PRECONNECT_INTERVAL); mapIgnoreDevice[sn] = HOST_USB_REGISTER; ret = true; delete hUSB; @@ -167,10 +167,19 @@ void HdcHostUSB::RemoveIgnoreDevice(string &mountInfo) { if (mapIgnoreDevice.count(mountInfo)) { mapIgnoreDevice.erase(mountInfo); - WRITE_LOG(LOG_DEBUG, "Remove %s from mapIgnoreDevice", mountInfo.c_str()); } } +void HdcHostUSB::ReviewUsbNodeLater(string &nodeKey) +{ + HdcServer *hdcServer = (HdcServer *)clsMainBase; + // add to ignore list + mapIgnoreDevice[nodeKey] = HOST_USB_IGNORE; + int delayRemoveFromList = intervalDevCheck * MINOR_TIMEOUT; // wait little time for daemon reinit + Base::DelayDo(&hdcServer->loopMain, delayRemoveFromList, 0, nodeKey, nullptr, + [this](const uint8_t flag, string &msg, const void *) -> void { RemoveIgnoreDevice(msg); }); +} + void HdcHostUSB::WatchDevPlugin(uv_timer_t *handle) { HdcHostUSB *thisClass = (HdcHostUSB *)handle->data; @@ -196,9 +205,7 @@ void HdcHostUSB::WatchDevPlugin(uv_timer_t *handle) } string sn = szTmpKey; if (!thisClass->DetectMyNeed(dev, sn)) { - // add to ignore device - thisClass->mapIgnoreDevice[szTmpKey] = HOST_USB_IGNORE; - WRITE_LOG(LOG_DEBUG, "Add %s to ignore list", szTmpKey.c_str()); + thisClass->ReviewUsbNodeLater(szTmpKey); } } libusb_free_device_list(devs, 1); @@ -206,17 +213,10 @@ void HdcHostUSB::WatchDevPlugin(uv_timer_t *handle) int HdcHostUSB::StartupUSBWork() { - // LIBUSB_HOTPLUG_NO_FLAGS = 0,//Only the registered callback function will only be called when the plug is - // inserted. LIBUSB_HOTPLUG_ENUMERATE = 1<<0,//The program load initialization before the device has been - // inserted, and the registered callback function is called (execution, scanning) + // Because libusb(winusb backend) does not support hotplug under win32, we use list mode for all platforms WRITE_LOG(LOG_DEBUG, "USBHost loopfind mode"); devListWatcher.data = this; -#ifdef HDC_PCDEBUG - constexpr int interval = 500; -#else - constexpr int interval = 3000; -#endif - uv_timer_start(&devListWatcher, WatchDevPlugin, 0, interval); + uv_timer_start(&devListWatcher, WatchDevPlugin, 0, intervalDevCheck); // Running pendding in independent threads does not significantly improve the efficiency usbWork.data = ctxUSB; uv_idle_start(&usbWork, PenddingUSBIO); @@ -236,7 +236,6 @@ int HdcHostUSB::CheckDescriptor(HUSB hUSB) WRITE_LOG(LOG_DEBUG, "CheckDescriptor libusb_get_device_descriptor failed"); return -1; } - WRITE_LOG(LOG_DEBUG, "CheckDescriptor busid:%d devid:%d", curBus, curDev); // Get the serial number of the device, if there is no serial number, use the ID number to replace // If the device is not in time, occasionally can't get it, this is determined by the external factor, cannot be // changed. LIBUSB_SUCCESS @@ -335,6 +334,7 @@ void LIBUSB_CALL HdcHostUSB::ReadUSBBulkCallback(struct libusb_transfer *transfe HUSB hUSB = hSession->hUSB; bool bOK = false; int childRet = 0; + constexpr int infinity = 0; // ignore timeout while (true) { if (!thisClass->modRunning || (hSession->isDead && 0 == hSession->sendRef)) break; @@ -351,7 +351,7 @@ void LIBUSB_CALL HdcHostUSB::ReadUSBBulkCallback(struct libusb_transfer *transfe hUSB->lockDeviceHandle.lock(); // loop self libusb_fill_bulk_transfer(transfer, hUSB->devHandle, hUSB->epDevice, hUSB->bufDevice, hUSB->sizeEpBuf, - ReadUSBBulkCallback, hSession, 0); // no user data + ReadUSBBulkCallback, hSession, infinity); childRet = libusb_submit_transfer(transfer); hUSB->lockDeviceHandle.unlock(); if (childRet < 0) { @@ -377,9 +377,9 @@ void HdcHostUSB::RegisterReadCallback(HSession hSession) } hSession->hUSB->transferRecv->user_data = hSession; hUSB->lockDeviceHandle.lock(); + // first bulk-read must be Okay and no timeout, otherwise failed. libusb_fill_bulk_transfer(hSession->hUSB->transferRecv, hUSB->devHandle, hUSB->epDevice, hUSB->bufDevice, - hUSB->sizeEpBuf, // Note: in_buffer is where input data - ReadUSBBulkCallback, hSession, 0); // no user data + hUSB->sizeEpBuf, ReadUSBBulkCallback, hSession, GLOBAL_TIMEOUT * TIME_BASE); int childRet = libusb_submit_transfer(hSession->hUSB->transferRecv); hUSB->lockDeviceHandle.unlock(); if (childRet == 0) { @@ -423,14 +423,6 @@ void LIBUSB_CALL HdcHostUSB::WriteUSBBulkCallback(struct libusb_transfer *transf USBHead *usbHead = reinterpret_cast(transfer->buffer); HSession hSession = reinterpret_cast(transfer->user_data); HdcSessionBase *server = reinterpret_cast(hSession->classInstance); - uint16_t zeroMask = hSession->hUSB->wMaxPacketSize - 1; - if (transfer->length != 0 && zeroMask != 0 && (transfer->length & zeroMask) == 0) { - // Zero packet - transfer->length = 0; - if (libusb_submit_transfer(transfer) == 0) { - return; - } - } if (usbHead->option & USB_OPTION_TAIL) { --hSession->sendRef; } @@ -452,7 +444,6 @@ int HdcHostUSB::SendUSBRaw(HSession hSession, uint8_t *data, const int length) int ret = ERR_GENERIC; int childRet = -1; HUSB hUSB = hSession->hUSB; - constexpr int retryTimeout = GLOBAL_TIMEOUT * TIME_BASE; while (true) { if (memcpy_s(hUSB->bufHost, length, data, length) != EOK) { ret = ERR_BUF_COPY; @@ -462,7 +453,7 @@ int HdcHostUSB::SendUSBRaw(HSession hSession, uint8_t *data, const int length) std::unique_lock lock(hUSB->lockSend); hUSB->sendIOComplete = false; libusb_fill_bulk_transfer(hUSB->transferSend, hUSB->devHandle, hUSB->epHost, hUSB->bufHost, length, - WriteUSBBulkCallback, hSession, retryTimeout); + WriteUSBBulkCallback, hSession, GLOBAL_TIMEOUT * TIME_BASE); childRet = libusb_submit_transfer(hUSB->transferSend); hUSB->lockDeviceHandle.unlock(); if (childRet < 0) { diff --git a/src/host/host_usb.h b/src/host/host_usb.h index 4304da18e0ede6a22f1ed755fc86c4ad560c8fc1..edafd789cb934d587519687262ab1a96dade9647 100644 --- a/src/host/host_usb.h +++ b/src/host/host_usb.h @@ -52,11 +52,13 @@ private: void RestoreHdcProtocol(HUSB hUsb, const uint8_t *buf, int bufSize); void UpdateUSBDaemonInfo(HUSB hUSB, HSession hSession, uint8_t connStatus); void RegisterReadCallback(HSession hSession); + void ReviewUsbNodeLater(string &nodeKey); uv_idle_t usbWork; libusb_context *ctxUSB; uv_timer_t devListWatcher; map mapIgnoreDevice; + const int intervalDevCheck = 3000; private: }; diff --git a/src/host/main.cpp b/src/host/main.cpp index 80d8c2caef06c6684fc4e26681cb4c2181861ac5..14c39c391ddb77378f04b512e141cc2a44004627 100644 --- a/src/host/main.cpp +++ b/src/host/main.cpp @@ -79,6 +79,23 @@ int IsRegisterCommand(string &outCommand, const char *cmd, const char *cmdnext) return 0; } +void AppendCwdWhenTransfer(string &outCommand) +{ + if (outCommand != CMDSTR_FILE_SEND && outCommand != CMDSTR_FILE_RECV && outCommand != CMDSTR_APP_INSTALL + && outCommand != CMDSTR_APP_SIDELOAD) { + return; + } + char path[PATH_MAX] = ""; + size_t size = sizeof(path); + if (uv_cwd(path, &size) < 0) + return; + if (path[strlen(path) - 1] != Base::GetPathSep()) { + path[strlen(path)] = Base::GetPathSep(); + } + outCommand += outCommand.size() ? " -cwd " : "-cwd "; + outCommand += path; +} + int SplitOptionAndCommand(int argc, const char **argv, string &outOption, string &outCommand) { bool foundCommand = false; @@ -92,6 +109,7 @@ int SplitOptionAndCommand(int argc, const char **argv, string &outOption, string if (resultChild == 2) { ++i; } + AppendCwdWhenTransfer(outCommand); continue; } } @@ -157,7 +175,7 @@ int RunClientMode(string &commands, string &serverListenString, string &connectK if (isPullServer && serverListenString == DEFAULT_SERVER_ADDR && Base::ProgramMutex(SERVER_NAME.c_str(), true) == 0) { // default pullup, just default listenstr.If want to customer listen-string, please use 'hdc -m -s lanip:port' - HdcServer::CheckToPullUptrServer(DEFAULT_SERVER_ADDR.c_str()); + HdcServer::PullupServer(DEFAULT_SERVER_ADDR.c_str()); uv_sleep(300); // give time to start serverForClient,at least 200ms } client.Initial(connectKey); @@ -206,7 +224,7 @@ bool GetCommandlineOptions(int optArgc, const char *optArgv[]) int ch = 0; bool needExit = false; opterr = 0; - // get option paraments first + // get option parameters first while ((ch = getopt(optArgc, (char *const *)optArgv, "hvpfms:d:t:l:")) != -1) { switch (ch) { case 'h': { @@ -265,7 +283,7 @@ bool GetCommandlineOptions(int optArgc, const char *optArgv[]) } else if (optarg[0] == 'u') { g_isTCPorUSB = false; } else { - Base::PrintMessage("Unknow debug paraments"); + Base::PrintMessage("Unknown debug parameters"); needExit = true; return needExit; } @@ -274,7 +292,7 @@ bool GetCommandlineOptions(int optArgc, const char *optArgv[]) case '?': break; default: { - Base::PrintMessage("Unknow paraments"); + Base::PrintMessage("Unknown parameters"); needExit = true; return needExit; } diff --git a/src/host/server.cpp b/src/host/server.cpp index a62a8515682812ec90ee7b35bb109680bb946924..e50f7f538d3f85368e3491dbcfbf20fd2be98090 100644 --- a/src/host/server.cpp +++ b/src/host/server.cpp @@ -81,7 +81,7 @@ bool HdcServer::Initial(const char *listenString) return true; } -bool HdcServer::CheckToPullUpServerWin32(const char *path, const char *listenString) +bool HdcServer::PullupServerWin32(const char *path, const char *listenString) { #ifdef _WIN32 char buf[BUF_SIZE_SMALL] = ""; @@ -116,7 +116,7 @@ bool HdcServer::CheckToPullUpServerWin32(const char *path, const char *listenStr } // Only detects that the default call is in the loop address, the other tubes are not -bool HdcServer::CheckToPullUptrServer(const char *listenString) +bool HdcServer::PullupServer(const char *listenString) { char path[BUF_SIZE_SMALL] = ""; size_t nPathSize = sizeof(path); @@ -127,7 +127,7 @@ bool HdcServer::CheckToPullUptrServer(const char *listenString) } #ifdef _WIN32 - if (!CheckToPullUpServerWin32(path, listenString)) { + if (!PullupServerWin32(path, listenString)) { return false; } #else @@ -435,7 +435,7 @@ bool HdcServer::ServerSessionHandshake(HSession hSession, uint8_t *payload, int } bool HdcServer::FetchCommand(HSession hSession, const uint32_t channelId, const uint16_t command, uint8_t *payload, - int payloadSize) + const int payloadSize) { bool ret = true; HdcServerForClient *pSfc = static_cast(clsServerForClient); @@ -471,13 +471,10 @@ bool HdcServer::FetchCommand(HSession hSession, const uint32_t channelId, const case CMD_KERNEL_CHANNEL_CLOSE: { WRITE_LOG(LOG_DEBUG, "CMD_KERNEL_CHANNEL_CLOSE channelid:%d", channelId); ClearOwnTasks(hSession, channelId); - auto funcChannleClose = [](uv_handle_t *handle) -> void { - HChannel hChannel = (HChannel)handle->data; - HdcServerForClient *sfc = static_cast(hChannel->clsChannel); - sfc->FreeChannel(hChannel->channelId); - }; - Base::TryCloseHandle((uv_handle_t *)&hChannel->hChildWorkTCP, funcChannleClose); - if (*payload) { + // Forcibly closing the tcp handle here may result in incomplete data reception on the client side + HdcServerForClient *sfc = static_cast(hChannel->clsChannel); + sfc->FreeChannel(hChannel->channelId); + if (*payload == 1) { --(*payload); Send(hSession->sessionId, channelId, CMD_KERNEL_CHANNEL_CLOSE, payload, 1); } diff --git a/src/host/server.h b/src/host/server.h index 3ac2ecc3134e32324603e7081c37fa664aa9224c..e1e66810a7ae39264f6251294d21f9c6b3df489a 100644 --- a/src/host/server.h +++ b/src/host/server.h @@ -29,7 +29,7 @@ public: bool Initial(const char *listenString); void AttachChannel(HSession hSession, const uint32_t channelId); void DeatchChannel(HSession hSession, const uint32_t channelId); - static bool CheckToPullUptrServer(const char *listenString); + static bool PullupServer(const char *listenString); static void UsbPreConnect(uv_timer_t *handle); void NotifyInstanceSessionFree(HSession hSession, bool freeOrClear); @@ -39,7 +39,7 @@ public: private: void ClearInstanceResource(); - void BuildDaemonVisableLine(HDaemonInfo hDaemonInfoInOut, bool fullDisplay, string &out); + void BuildDaemonVisableLine(HDaemonInfo hdi, bool fullDisplay, string &out); void BuildForwardVisableLine(bool fullOrSimble, HForwardInfo hfi, string &echo); void ClearMapDaemonInfo(); bool ServerCommand(const uint32_t sessionId, const uint32_t channelId, const uint16_t command, uint8_t *bufPtr, @@ -53,7 +53,7 @@ private: bool ServerSessionHandshake(HSession hSession, uint8_t *payload, int payloadSize); void GetDaemonMapOnlyOne(HDaemonInfo &hDaemonInfoInOut); void TryStopInstance(); - static bool CheckToPullUpServerWin32(const char *path, const char *listenString); + static bool PullupServerWin32(const char *path, const char *listenString); uv_rwlock_t daemonAdmin; map mapDaemon; diff --git a/src/host/server_for_client.cpp b/src/host/server_for_client.cpp index 44745368abff1921aff6a000c98f9fe9a242a0d1..18abf088c5b23164ec89de1b5b2ff2b35e0d6699 100644 --- a/src/host/server_for_client.cpp +++ b/src/host/server_for_client.cpp @@ -278,7 +278,7 @@ void HdcServerForClient::GetTargetList(HChannel hChannel, void *formatCommandInp TranslateCommand::FormatCommand *formatCommand = (TranslateCommand::FormatCommand *)formatCommandInput; HdcServer *ptrServer = (HdcServer *)clsServer; uint16_t cmd = OP_GET_STRLIST; - if (formatCommand->paraments == "v") { + if (formatCommand->parameters == "v") { cmd = OP_GET_STRLIST_FULL; } HDaemonInfo hdi = nullptr; @@ -312,11 +312,11 @@ bool HdcServerForClient::GetAnyTarget(HChannel hChannel) return ret; } -bool HdcServerForClient::RemoveForward(HChannel hChannel, const char *paramentString) +bool HdcServerForClient::RemoveForward(HChannel hChannel, const char *parameterString) { HdcServer *ptrServer = (HdcServer *)clsServer; - if (paramentString == nullptr) { // remove all - HForwardInfo hfi = nullptr; // dummy + if (parameterString == nullptr) { // remove all + HForwardInfo hfi = nullptr; // dummy string echo = ptrServer->AdminForwardMap(OP_GET_STRLIST, "", hfi); if (!echo.length()) { return false; @@ -329,8 +329,8 @@ bool HdcServerForClient::RemoveForward(HChannel hChannel, const char *paramentSt } } } else { // remove single - if (!CommandRemoveForward(paramentString)) { - EchoClient(hChannel, MSG_FAIL, "Remove forward ruler failed,ruler:%s", paramentString); + if (!CommandRemoveForward(parameterString)) { + EchoClient(hChannel, MSG_FAIL, "Remove forward ruler failed,ruler:%s", parameterString); } } return true; @@ -340,7 +340,7 @@ bool HdcServerForClient::DoCommandLocal(HChannel hChannel, void *formatCommandIn { TranslateCommand::FormatCommand *formatCommand = (TranslateCommand::FormatCommand *)formatCommandInput; HdcServer *ptrServer = (HdcServer *)clsServer; - const char *paramentString = formatCommand->paraments.c_str(); + const char *parameterString = formatCommand->parameters.c_str(); bool ret = false; // Main thread command, direct Listen main thread switch (formatCommand->cmdFlag) { @@ -359,11 +359,11 @@ bool HdcServerForClient::DoCommandLocal(HChannel hChannel, void *formatCommandIn break; } case CMD_KERNEL_TARGET_CONNECT: { - ret = NewConnectTry(ptrServer, hChannel, paramentString); + ret = NewConnectTry(ptrServer, hChannel, parameterString); break; } case CMD_KERNEL_TARGET_DISCONNECT: { - CommandRemoveSession(hChannel, paramentString); + CommandRemoveSession(hChannel, parameterString); break; } case CMD_KERNEL_SERVER_KILL: { @@ -383,7 +383,13 @@ bool HdcServerForClient::DoCommandLocal(HChannel hChannel, void *formatCommandIn break; } case CMD_FORWARD_REMOVE: { - RemoveForward(hChannel, paramentString); + RemoveForward(hChannel, parameterString); + break; + } + case CMD_KERNEL_ENABLE_KEEPALIVE: { + // just use for 'list targets' now + hChannel->keepAlive = true; + ret = true; break; } default: { @@ -398,38 +404,38 @@ bool HdcServerForClient::TaskCommand(HChannel hChannel, void *formatCommandInput { TranslateCommand::FormatCommand *formatCommand = (TranslateCommand::FormatCommand *)formatCommandInput; HdcServer *ptrServer = (HdcServer *)clsServer; - int sizeSend = formatCommand->paraments.size(); + int sizeSend = formatCommand->parameters.size(); string cmdFlag; uint8_t sizeCmdFlag = 0; if (CMD_FILE_INIT == formatCommand->cmdFlag) { cmdFlag = "send "; - sizeCmdFlag = 5; // 5: cmdFlag send size + sizeCmdFlag = 5; // 5: cmdFlag send size } else if (CMD_FORWARD_INIT == formatCommand->cmdFlag) { cmdFlag = "fport "; - sizeCmdFlag = 6; // 6: cmdFlag fport size + sizeCmdFlag = 6; // 6: cmdFlag fport size } else if (CMD_APP_INIT == formatCommand->cmdFlag) { cmdFlag = "install "; - sizeCmdFlag = 8; // 8: cmdFlag install size + sizeCmdFlag = 8; // 8: cmdFlag install size } else if (CMD_APP_UNINSTALL == formatCommand->cmdFlag) { cmdFlag = "uninstall "; - sizeCmdFlag = 10; // 10: cmdFlag uninstall size + sizeCmdFlag = 10; // 10: cmdFlag uninstall size } else if (CMD_UNITY_BUGREPORT_INIT == formatCommand->cmdFlag) { cmdFlag = "bugreport "; - sizeCmdFlag = 10; // 10: cmdFlag bugreport size + sizeCmdFlag = 10; // 10: cmdFlag bugreport size } else if (CMD_APP_SIDELOAD == formatCommand->cmdFlag) { cmdFlag = "sideload "; sizeCmdFlag = 9; } - if (!strncmp(formatCommand->paraments.c_str(), cmdFlag.c_str(), sizeCmdFlag)) { // local do + uint8_t *payload = reinterpret_cast(const_cast(formatCommand->parameters.c_str())) + sizeCmdFlag; + if (!strncmp(formatCommand->parameters.c_str(), cmdFlag.c_str(), sizeCmdFlag)) { // local do HSession hSession = FindAliveSession(hChannel->targetSessionId); if (!hSession) { return false; } - ptrServer->DispatchTaskData(hSession, hChannel->channelId, formatCommand->cmdFlag, - (uint8_t *)formatCommand->paraments.c_str() + sizeCmdFlag, sizeSend - sizeCmdFlag); + ptrServer->DispatchTaskData(hSession, hChannel->channelId, formatCommand->cmdFlag, payload, + sizeSend - sizeCmdFlag); } else { // Send to Daemon-side to do - SendToDaemon(hChannel, formatCommand->cmdFlag, (uint8_t *)formatCommand->paraments.c_str() + sizeCmdFlag, - sizeSend - sizeCmdFlag); + SendToDaemon(hChannel, formatCommand->cmdFlag, payload, sizeSend - sizeCmdFlag); } return true; } @@ -438,7 +444,7 @@ bool HdcServerForClient::DoCommandRemote(HChannel hChannel, void *formatCommandI { TranslateCommand::FormatCommand *formatCommand = (TranslateCommand::FormatCommand *)formatCommandInput; bool ret = false; - int sizeSend = formatCommand->paraments.size(); + int sizeSend = formatCommand->parameters.size(); string cmdFlag; switch (formatCommand->cmdFlag) { // Some simple commands only need to forward the instruction, no need to start Task @@ -452,7 +458,8 @@ bool HdcServerForClient::DoCommandRemote(HChannel hChannel, void *formatCommandI case CMD_UNITY_HILOG: case CMD_UNITY_ROOTRUN: case CMD_UNITY_JPID: { - if (!SendToDaemon(hChannel, formatCommand->cmdFlag, (uint8_t *)formatCommand->paraments.c_str(), + if (!SendToDaemon(hChannel, formatCommand->cmdFlag, + reinterpret_cast(const_cast(formatCommand->parameters.c_str())), sizeSend)) { break; } @@ -623,7 +630,7 @@ int HdcServerForClient::ReadChannel(HChannel hChannel, uint8_t *bufPtr, const in return ret; } } else { - formatCommand.paraments = string((char *)bufPtr, bytesIO); + formatCommand.parameters = string(reinterpret_cast(bufPtr), bytesIO); formatCommand.cmdFlag = CMD_SHELL_DATA; } if (!DoCommand(hChannel, &formatCommand)) { diff --git a/src/host/server_for_client.h b/src/host/server_for_client.h index 0b9da576b2dd0dcc851495b29ee0cac505fb393c..22ca06e258c95416212e552d36e843d5523a9170 100644 --- a/src/host/server_for_client.h +++ b/src/host/server_for_client.h @@ -46,7 +46,7 @@ private: bool DoCommandRemote(HChannel hChannel, void *formatCommandInput); void GetTargetList(HChannel hChannel, void *formatCommandInput); bool GetAnyTarget(HChannel hChannel); - bool RemoveForward(HChannel hChannel, const char *paramentString); + bool RemoveForward(HChannel hChannel, const char *parameterString); bool TaskCommand(HChannel hChannel, void *formatCommandInput); int ChannelHandShake(HChannel hChannel, uint8_t *bufPtr, const int bytesIO); bool ChannelSendSessionCtrlMsg(vector &ctrlMsg, uint32_t sessionId); diff --git a/src/host/translate.cpp b/src/host/translate.cpp index be05056ae3a098181d6072bd0766011abc060ad7..b335f74aba0038c9eeec3716f82febff1ec87857 100644 --- a/src/host/translate.cpp +++ b/src/host/translate.cpp @@ -95,20 +95,20 @@ namespace TranslateCommand { string TargetConnect(FormatCommand *outCmd) { string stringError; - if (Base::StringEndsWith(outCmd->paraments, " -remove")) { - outCmd->paraments = outCmd->paraments.substr(0, outCmd->paraments.size() - 8); + if (Base::StringEndsWith(outCmd->parameters, " -remove")) { + outCmd->parameters = outCmd->parameters.substr(0, outCmd->parameters.size() - 8); outCmd->cmdFlag = CMD_KERNEL_TARGET_DISCONNECT; } else { outCmd->cmdFlag = CMD_KERNEL_TARGET_CONNECT; - if (outCmd->paraments.size() > 22) { // 22: tcp max=21,USB max=8bytes + if (outCmd->parameters.size() > 22) { // 22: tcp max=21,USB max=8bytes stringError = "Error connect key's size"; outCmd->bJumpDo = true; } } - if (outCmd->paraments.find(":") != std::string::npos) { + if (outCmd->parameters.find(":") != std::string::npos) { // tcp mode - string ip = outCmd->paraments.substr(0, outCmd->paraments.find(":")); - string sport = outCmd->paraments.substr(outCmd->paraments.find(":") + 1); + string ip = outCmd->parameters.substr(0, outCmd->parameters.find(":")); + string sport = outCmd->parameters.substr(outCmd->parameters.find(":") + 1); int port = std::stoi(sport); sockaddr_in addr; if ((port <= 0 || port > MAX_IP_PORT) || uv_ip4_addr(ip.c_str(), port, &addr) < 0) { @@ -128,7 +128,7 @@ namespace TranslateCommand { } else if (!strncmp(pExtra, "rm", 2)) { outCmd->cmdFlag = CMD_FORWARD_REMOVE; if (strcmp(pExtra, "rm")) { - outCmd->paraments = input + 9; + outCmd->parameters = input + 9; } } else { const char *p = input + 6; @@ -140,7 +140,7 @@ namespace TranslateCommand { } // clang-format on outCmd->cmdFlag = CMD_FORWARD_INIT; - outCmd->paraments = input; + outCmd->parameters = input; } return stringError; } @@ -149,12 +149,12 @@ namespace TranslateCommand { { string stringError; outCmd->cmdFlag = CMD_UNITY_RUNMODE; - outCmd->paraments = input + CMDSTR_TARGET_MODE.size() + 1; // with ' ' - if (!strncmp(outCmd->paraments.c_str(), "port", 4) - && !strcmp(outCmd->paraments.c_str(), CMDSTR_TMODE_USB.c_str())) { + outCmd->parameters = input + CMDSTR_TARGET_MODE.size() + 1; // with ' ' + if (!strncmp(outCmd->parameters.c_str(), "port", 4) + && !strcmp(outCmd->parameters.c_str(), CMDSTR_TMODE_USB.c_str())) { stringError = "Error tmode command"; outCmd->bJumpDo = true; - } else if (!strncmp(outCmd->paraments.c_str(), "port ", 5)) { + } else if (!strncmp(outCmd->parameters.c_str(), "port ", 5)) { int port = atoi(input + 4); if (port > MAX_IP_PORT || port <= 0) { stringError = "Incorrect port range"; @@ -169,12 +169,12 @@ namespace TranslateCommand { string stringError; outCmd->cmdFlag = CMD_UNITY_REBOOT; if (strcmp(input, CMDSTR_TARGET_REBOOT.c_str())) { - outCmd->paraments = input + 12; - if (outCmd->paraments != "-bootloader" && outCmd->paraments != "-recovery") { + outCmd->parameters = input + 12; + if (outCmd->parameters != "-bootloader" && outCmd->parameters != "-recovery") { stringError = "Error reboot paramenter"; outCmd->bJumpDo = true; } else { - outCmd->paraments.erase(outCmd->paraments.begin()); + outCmd->parameters.erase(outCmd->parameters.begin()); } } return stringError; @@ -182,92 +182,97 @@ namespace TranslateCommand { // command input // client side:Enter string data formatting conversion to module see internal processing command - string String2FormatCommand(const char *input, int sizeInput, FormatCommand *outCmd) + string String2FormatCommand(const char *inputRaw, int sizeInputRaw, FormatCommand *outCmd) { string stringError; - outCmd->inputRaw = input; - if (!strcmp(input, CMDSTR_SOFTWARE_HELP.c_str())) { + string input = string(inputRaw, sizeInputRaw); + if (!strcmp(input.c_str(), CMDSTR_SOFTWARE_HELP.c_str())) { outCmd->cmdFlag = CMD_KERNEL_HELP; stringError = Usage(); outCmd->bJumpDo = true; - } else if (!strcmp(input, CMDSTR_SOFTWARE_VERSION.c_str())) { + } else if (!strcmp(input.c_str(), CMDSTR_SOFTWARE_VERSION.c_str())) { outCmd->cmdFlag = CMD_KERNEL_HELP; stringError = Base::GetVersion(); outCmd->bJumpDo = true; - } else if (!strcmp(input, CMDSTR_TARGET_DISCOVER.c_str())) { + } else if (!strcmp(input.c_str(), CMDSTR_TARGET_DISCOVER.c_str())) { outCmd->cmdFlag = CMD_KERNEL_TARGET_DISCOVER; - } else if (!strncmp(input, CMDSTR_LIST_TARGETS.c_str(), CMDSTR_LIST_TARGETS.size())) { + } else if (!strncmp(input.c_str(), CMDSTR_LIST_TARGETS.c_str(), CMDSTR_LIST_TARGETS.size())) { outCmd->cmdFlag = CMD_KERNEL_TARGET_LIST; - if (strstr(input, " -v")) { - outCmd->paraments = "v"; + if (strstr(input.c_str(), " -v")) { + outCmd->parameters = "v"; } - } else if (!strcmp(input, CMDSTR_CONNECT_ANY.c_str())) { + } else if (!strcmp(input.c_str(), CMDSTR_CONNECT_ANY.c_str())) { outCmd->cmdFlag = CMD_KERNEL_TARGET_ANY; - } else if (!strncmp(input, CMDSTR_CONNECT_TARGET.c_str(), CMDSTR_CONNECT_TARGET.size())) { - outCmd->paraments = input + CMDSTR_CONNECT_TARGET.size() + 1; // with ' ' + } else if (!strncmp(input.c_str(), CMDSTR_CONNECT_TARGET.c_str(), CMDSTR_CONNECT_TARGET.size())) { + outCmd->parameters = input.c_str() + CMDSTR_CONNECT_TARGET.size() + 1; // with ' ' stringError = TargetConnect(outCmd); - } else if (!strncmp(input, (CMDSTR_SHELL + " ").c_str(), CMDSTR_SHELL.size() + 1)) { + } else if (!strncmp(input.c_str(), (CMDSTR_SHELL + " ").c_str(), CMDSTR_SHELL.size() + 1)) { outCmd->cmdFlag = CMD_UNITY_EXECUTE; - outCmd->paraments = input + CMDSTR_SHELL.size() + 1; - } else if (!strcmp(input, CMDSTR_SHELL.c_str())) { + outCmd->parameters = input.c_str() + CMDSTR_SHELL.size() + 1; + } else if (!strcmp(input.c_str(), CMDSTR_SHELL.c_str())) { outCmd->cmdFlag = CMD_SHELL_INIT; - } else if (!strncmp(input, CMDSTR_FILE_SEND.c_str(), CMDSTR_FILE_SEND.size()) - || !strncmp(input, CMDSTR_FILE_RECV.c_str(), CMDSTR_FILE_RECV.size())) { + } else if (!strncmp(input.c_str(), CMDSTR_FILE_SEND.c_str(), CMDSTR_FILE_SEND.size()) + || !strncmp(input.c_str(), CMDSTR_FILE_RECV.c_str(), CMDSTR_FILE_RECV.size())) { outCmd->cmdFlag = CMD_FILE_INIT; - outCmd->paraments = input + 5; // 5: CMDSTR_FORWARD_FPORT CMDSTR_FORWARD_RPORT size - } else if (!strncmp(input, string(CMDSTR_FORWARD_FPORT + " ").c_str(), CMDSTR_FORWARD_FPORT.size() + 1) - || !strncmp(input, string(CMDSTR_FORWARD_RPORT + " ").c_str(), CMDSTR_FORWARD_RPORT.size() + 1)) { - stringError = ForwardPort(input, outCmd); - } else if (!strcmp(input, CMDSTR_KILL_SERVER.c_str())) { + outCmd->parameters = input.c_str() + 5; // 5: CMDSTR_FORWARD_FPORT CMDSTR_FORWARD_RPORT size + } else if (!strncmp(input.c_str(), string(CMDSTR_FORWARD_FPORT + " ").c_str(), CMDSTR_FORWARD_FPORT.size() + 1) + || !strncmp(input.c_str(), string(CMDSTR_FORWARD_RPORT + " ").c_str(), + CMDSTR_FORWARD_RPORT.size() + 1)) { + stringError = ForwardPort(input.c_str(), outCmd); + } else if (!strcmp(input.c_str(), CMDSTR_KILL_SERVER.c_str())) { outCmd->cmdFlag = CMD_KERNEL_SERVER_KILL; - } else if (!strcmp(input, CMDSTR_KILL_DAEMON.c_str())) { + } else if (!strcmp(input.c_str(), CMDSTR_KILL_DAEMON.c_str())) { outCmd->cmdFlag = CMD_UNITY_TERMINATE; - outCmd->paraments = "0"; - } else if (!strncmp(input, CMDSTR_APP_INSTALL.c_str(), CMDSTR_APP_INSTALL.size())) { + outCmd->parameters = "0"; + } else if (!strncmp(input.c_str(), CMDSTR_APP_INSTALL.c_str(), CMDSTR_APP_INSTALL.size())) { outCmd->cmdFlag = CMD_APP_INIT; - outCmd->paraments = input; - } else if (!strncmp(input, CMDSTR_APP_UNINSTALL.c_str(), CMDSTR_APP_UNINSTALL.size())) { + outCmd->parameters = input; + } else if (!strncmp(input.c_str(), CMDSTR_APP_UNINSTALL.c_str(), CMDSTR_APP_UNINSTALL.size())) { outCmd->cmdFlag = CMD_APP_UNINSTALL; - outCmd->paraments = input; - if (outCmd->paraments.size() > 512 || outCmd->paraments.size() < 4) { + outCmd->parameters = input; + if (outCmd->parameters.size() > 512 || outCmd->parameters.size() < 4) { stringError = "Package's path incorrect"; outCmd->bJumpDo = true; } - } else if (!strcmp(input, CMDSTR_TARGET_MOUNT.c_str())) { + } else if (!strcmp(input.c_str(), CMDSTR_TARGET_MOUNT.c_str())) { outCmd->cmdFlag = CMD_UNITY_REMOUNT; - } else if (!strcmp(input, CMDSTR_LIST_JDWP.c_str())) { + } else if (!strcmp(input.c_str(), CMDSTR_LIST_JDWP.c_str())) { outCmd->cmdFlag = CMD_UNITY_JPID; - } else if (!strncmp(input, CMDSTR_TARGET_REBOOT.c_str(), CMDSTR_TARGET_REBOOT.size())) { - TargetReboot(input, outCmd); - } else if (!strncmp(input, CMDSTR_TARGET_MODE.c_str(), CMDSTR_TARGET_MODE.size())) { - RunMode(input, outCmd); - } else if (!strcmp(input, CMDSTR_CONNECT_ANY.c_str())) { + } else if (!strncmp(input.c_str(), CMDSTR_TARGET_REBOOT.c_str(), CMDSTR_TARGET_REBOOT.size())) { + TargetReboot(input.c_str(), outCmd); + } else if (!strncmp(input.c_str(), CMDSTR_TARGET_MODE.c_str(), CMDSTR_TARGET_MODE.size())) { + RunMode(input.c_str(), outCmd); + } else if (!strcmp(input.c_str(), CMDSTR_CONNECT_ANY.c_str())) { outCmd->cmdFlag = CMD_KERNEL_TARGET_ANY; - } else if (!strncmp(input, CMDSTR_HILOG.c_str(), CMDSTR_HILOG.size())) { + } else if (!strncmp(input.c_str(), CMDSTR_HILOG.c_str(), CMDSTR_HILOG.size())) { outCmd->cmdFlag = CMD_UNITY_HILOG; - if (strstr(input, " -v")) { - outCmd->paraments = "v"; + if (strstr(input.c_str(), " -v")) { + outCmd->parameters = "v"; } - } else if (!strncmp(input, CMDSTR_STARTUP_MODE.c_str(), CMDSTR_STARTUP_MODE.size())) { + } else if (!strncmp(input.c_str(), CMDSTR_STARTUP_MODE.c_str(), CMDSTR_STARTUP_MODE.size())) { outCmd->cmdFlag = CMD_UNITY_ROOTRUN; - if (strstr(input, " -r")) { - outCmd->paraments = "r"; + if (strstr(input.c_str(), " -r")) { + outCmd->parameters = "r"; } - } else if (!strncmp(input, CMDSTR_APP_SIDELOAD.c_str(), CMDSTR_APP_SIDELOAD.size())) { - if (strlen(input) == CMDSTR_APP_SIDELOAD.size()) { + } else if (!strncmp(input.c_str(), CMDSTR_APP_SIDELOAD.c_str(), CMDSTR_APP_SIDELOAD.size())) { + if (strlen(input.c_str()) == CMDSTR_APP_SIDELOAD.size()) { stringError = "Incorrect command, please with local path"; outCmd->bJumpDo = true; } outCmd->cmdFlag = CMD_APP_SIDELOAD; - outCmd->paraments = input; - } else if (!strncmp(input, CMDSTR_BUGREPORT.c_str(), CMDSTR_BUGREPORT.size())) { + outCmd->parameters = input; + } else if (!strncmp(input.c_str(), CMDSTR_BUGREPORT.c_str(), CMDSTR_BUGREPORT.size())) { outCmd->cmdFlag = CMD_UNITY_BUGREPORT_INIT; - outCmd->paraments = input; - if (outCmd->paraments.size() == CMDSTR_BUGREPORT.size()) { - outCmd->paraments += " "; + outCmd->parameters = input; + if (outCmd->parameters.size() == CMDSTR_BUGREPORT.size()) { + outCmd->parameters += " "; } + } + // Inner command, protocol uses only + else if (input == CMDSTR_INNER_ENABLE_KEEPALIVE) { + outCmd->cmdFlag = CMD_KERNEL_ENABLE_KEEPALIVE; } else { - stringError = "Unknow command..."; + stringError = "Unknown command..."; outCmd->bJumpDo = true; } // nl @@ -276,11 +281,5 @@ namespace TranslateCommand { } return stringError; }; - - // IDE plugin input to private command - int LibraryCallEntryPoint(void *hwnd, void *hinst, uint16_t callMethod, char *lpcmdStringLine, int nCmdShow) - { - return 0; - }; } } // namespace Hdc diff --git a/src/host/translate.h b/src/host/translate.h index 9021fc8f43a0b56bb7a98bb9ccc7b8277b70ed33..96128e50d6b078ee510fa15fc1c5ff137fe4726d 100644 --- a/src/host/translate.h +++ b/src/host/translate.h @@ -20,15 +20,12 @@ namespace Hdc { namespace TranslateCommand { struct FormatCommand { uint16_t cmdFlag; - string paraments; - string inputRaw; + string parameters; bool bJumpDo; }; - string String2FormatCommand(const char *input, int sizeInput, FormatCommand *outCmd); + string String2FormatCommand(const char *inputRaw, int sizeInputRaw, FormatCommand *outCmd); string Usage(); - - int LibraryCallEntryPoint(void *hwnd, void *hinst, uint16_t callMethod, char *lpcmdStringLine, int nCmdShow); } } #endif \ No newline at end of file