From b006022faa97c4813a478ffa28e91d69e8abb39c Mon Sep 17 00:00:00 2001 From: faithwang Date: Tue, 5 Jul 2022 18:59:23 +0800 Subject: [PATCH 1/2] suitable for IDE Signed-off-by: faithwang --- src/common/channel.h | 2 +- src/common/define.h | 1 + src/common/define_plus.h | 2 ++ src/common/file.cpp | 2 ++ src/common/transfer.h | 1 - src/host/client.cpp | 31 ++++++++++++++++++++++++------- src/host/main.cpp | 3 +++ src/host/server.cpp | 9 ++++++--- src/host/server_for_client.cpp | 34 +++++++++++++++++++++------------- src/host/server_for_client.h | 2 +- 10 files changed, 61 insertions(+), 26 deletions(-) diff --git a/src/common/channel.h b/src/common/channel.h index 0db5d1bf..d15799c2 100755 --- a/src/common/channel.h +++ b/src/common/channel.h @@ -34,11 +34,11 @@ public: protected: struct ChannelHandShake { char banner[12]; // must first index - char version[BUF_SIZE_TINY] = { 0 }; union { uint32_t channelId; char connectKey[MAX_CONNECTKEY_SIZE]; }; + char version[BUF_SIZE_TINY] = { 0 }; } __attribute__((packed)); uint32_t MallocChannel(HChannel *hOutChannel); virtual int ReadChannel(HChannel hChannel, uint8_t *bufPtr, const int bytesIO) diff --git a/src/common/define.h b/src/common/define.h index 1e8f0935..dd2df2e5 100755 --- a/src/common/define.h +++ b/src/common/define.h @@ -106,6 +106,7 @@ constexpr uint16_t MAX_UART_SIZE_IOBUF = 4096; // MAX_SIZE_IOBUF; const string CMDSTR_TMODE_TCP = "tcp"; const string CMDSTR_FILE_SEND = "file send"; const string CMDSTR_FILE_RECV = "file recv"; +const string CMDSTR_FILE_REMOTE_PARAMETER = "-r"; const string CMDSTR_FORWARD_FPORT = "fport"; const string CMDSTR_FORWARD_RPORT = "rport"; const string CMDSTR_APP_INSTALL = "install"; diff --git a/src/common/define_plus.h b/src/common/define_plus.h index 221860e9..9a713ffd 100755 --- a/src/common/define_plus.h +++ b/src/common/define_plus.h @@ -469,6 +469,8 @@ struct HdcChannel { uv_tty_t stdinTty; uv_tty_t stdoutTty; char bufStd[128]; + bool bFileSend = false; + bool bFileFromClient = false; }; using HChannel = struct HdcChannel *; diff --git a/src/common/file.cpp b/src/common/file.cpp index 03d68534..61fe6387 100644 --- a/src/common/file.cpp +++ b/src/common/file.cpp @@ -93,6 +93,8 @@ bool HdcFile::SetMasterParameters(CtxFile *context, const char *command, int arg } else if (argv[i] == CMD_OPTION_MODE_SYNC) { context->fileModeSync = true; ++srcArgvIndex; + } else if (argv[i] == CMDSTR_FILE_REMOTE_PARAMETER) { + ++srcArgvIndex; } else if (argv[i][0] == '-') { LogMsg(MSG_FAIL, "Unknown file option: %s", argv[i]); return false; diff --git a/src/common/transfer.h b/src/common/transfer.h index f6ad08a4..7129941d 100644 --- a/src/common/transfer.h +++ b/src/common/transfer.h @@ -78,7 +78,6 @@ protected: bool ioFinish; void *thisClass; uint32_t lastErrno; - uv_loop_t *loop; uv_fs_t fsOpenReq; uv_fs_t fsCloseReq; diff --git a/src/host/client.cpp b/src/host/client.cpp index 0452207e..1be399a7 100755 --- a/src/host/client.cpp +++ b/src/host/client.cpp @@ -179,6 +179,12 @@ int HdcClient::ExecuteCommand(const string &commandIn) channelHostPort.c_str(), ret); return -1; } + + if (!strncmp(commandIn.c_str(), CMDSTR_FILE_SEND.c_str(), CMDSTR_FILE_SEND.size()) + || !strncmp(commandIn.c_str(), CMDSTR_FILE_RECV.c_str(), CMDSTR_FILE_RECV.size())) { + WRITE_LOG(LOG_DEBUG, "Set file send mode"); + channel->bFileSend = true; + } command = commandIn; connectKey = AutoConnectKey(command, connectKey); ConnectServerForClient(ip, port); @@ -257,7 +263,7 @@ void HdcClient::CommandWorker(uv_timer_t *handle) #endif break; } - thisClass->SendWithCmd(thisClass->channel->channelId, 0, (uint8_t *)thisClass->command.c_str(), + thisClass->Send(thisClass->channel->channelId, (uint8_t *)thisClass->command.c_str(), thisClass->command.size() + 1); } @@ -280,7 +286,7 @@ void HdcClient::ReadStd(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) if (nread <= 0) { return; // error } - thisClass->SendWithCmd(hChannel->channelId, 0, (uint8_t *)command, strlen(command)); + thisClass->Send(hChannel->channelId, (uint8_t *)command, strlen(command)); Base::ZeroArray(hChannel->bufStd); } @@ -389,7 +395,7 @@ int HdcClient::PreHandshake(HChannel hChannel, const uint8_t *buf) hChannel->handshakeOK = true; #ifdef HDC_CHANNEL_KEEP_ALIVE // Evaluation method, non long-term support - SendWithCmd(hChannel->channelId, 0, + Send(hChannel->channelId, reinterpret_cast(const_cast(CMDSTR_INNER_ENABLE_KEEPALIVE.c_str())), CMDSTR_INNER_ENABLE_KEEPALIVE.size()); #endif @@ -408,8 +414,20 @@ int HdcClient::ReadChannel(HChannel hChannel, uint8_t *buf, const int bytesIO) #endif WRITE_LOG(LOG_DEBUG, "Client ReadChannel :%d", bytesIO); - uint16_t command = *reinterpret_cast(buf); - if (command != 0) { + uint16_t command = 0; + bool bOffset = false; + if (bytesIO >= sizeof(uint16_t)) { + command = *reinterpret_cast(buf); + bOffset = (command == CMD_CHECK_SERVER) + || (command == CMD_FILE_INIT) + || (command == CMD_FILE_CHECK) + || (command == CMD_FILE_BEGIN) + || (command == CMD_FILE_DATA) + || (command == CMD_FILE_FINISH) + || (command == CMD_FILE_MODE) + || (command == CMD_DIR_MODE); + } + if ((isCheckVersionCmd || hChannel->bFileSend) && bOffset) { if (CMD_CHECK_VERSION == command) { WRITE_LOG(LOG_DEBUG, "recieve CMD_CHECK_VERSION command"); string version(reinterpret_cast(buf + sizeof(uint16_t)), bytesIO - sizeof(uint16_t)); @@ -434,8 +452,7 @@ int HdcClient::ReadChannel(HChannel hChannel, uint8_t *buf, const int bytesIO) } return 0; } - - string s(reinterpret_cast(buf + sizeof(uint16_t)), bytesIO - sizeof(uint16_t)); + string s(reinterpret_cast(buf), bytesIO); fprintf(stdout, "%s", s.c_str()); fflush(stdout); return 0; diff --git a/src/host/main.cpp b/src/host/main.cpp index d087cd52..2ad347e3 100755 --- a/src/host/main.cpp +++ b/src/host/main.cpp @@ -116,6 +116,9 @@ void AppendCwdWhenTransfer(string &outCommand) outCommand += outCommand.size() ? " -cwd " : "-cwd "; string utf8Path = Base::UnicodeToUtf8(path, true); outCommand += Base::StringFormat("\"%s\"", utf8Path.c_str()); + outCommand += " "; + // add default parameter to command + outCommand += CMDSTR_FILE_REMOTE_PARAMETER; } int SplitOptionAndCommand(int argc, const char **argv, string &outOption, string &outCommand) diff --git a/src/host/server.cpp b/src/host/server.cpp index 061c42c8..66fa05a4 100755 --- a/src/host/server.cpp +++ b/src/host/server.cpp @@ -566,7 +566,6 @@ bool HdcServer::FetchCommand(HSession hSession, const uint32_t channelId, const Base::TryCloseHandle((uv_handle_t *)&hChannel->hChildWorkTCP); // detch client channel break; } - // server directly passthrough file command to client case CMD_FILE_INIT: case CMD_FILE_CHECK: case CMD_FILE_BEGIN: @@ -574,8 +573,12 @@ bool HdcServer::FetchCommand(HSession hSession, const uint32_t channelId, const case CMD_FILE_FINISH: case CMD_FILE_MODE: case CMD_DIR_MODE: - sfc->SendToClient(hChannel, command, payload, payloadSize); - break; + if (hChannel->bFileFromClient) { + // server directly passthrough file command to client if remote file mode, else go default + WRITE_LOG(LOG_INFO, "server directly passthrough file command to client command:%u", channelId); + sfc->SendCommandToClient(hChannel, command, payload, payloadSize); + break; + } default: { HSession hSession = AdminSession(OP_QUERY, hChannel->targetSessionId, nullptr); if (!hSession) { diff --git a/src/host/server_for_client.cpp b/src/host/server_for_client.cpp index 4b9e9823..71a3fbe9 100755 --- a/src/host/server_for_client.cpp +++ b/src/host/server_for_client.cpp @@ -124,16 +124,16 @@ void HdcServerForClient::EchoClient(HChannel hChannel, MessageLevel level, const if (log.back() != '\n') { log += "\r\n"; } - SendChannelWithCmd(hChannel, 0, (uint8_t *)log.c_str(), log.size()); + SendChannel(hChannel, (uint8_t *)log.c_str(), log.size()); } void HdcServerForClient::EchoClientRaw(const HChannel hChannel, uint8_t *payload, const int payloadSize) { - SendChannelWithCmd(hChannel, 0, payload, payloadSize); + SendChannel(hChannel, payload, payloadSize); } // HdcServerForClient passthrough file command to client -void HdcServerForClient::SendToClient(const HChannel hChannel, const uint16_t commandFlag, +void HdcServerForClient::SendCommandToClient(const HChannel hChannel, const uint16_t commandFlag, uint8_t *payload, const int payloadSize) { SendChannelWithCmd(hChannel, commandFlag, payload, payloadSize); @@ -445,6 +445,16 @@ bool HdcServerForClient::TaskCommand(HChannel hChannel, void *formatCommandInput if (CMD_FILE_INIT == formatCommand->cmdFlag) { cmdFlag = "send "; sizeCmdFlag = 5; // 5: cmdFlag send size + hChannel->bFileSend = true; + int argc = 0; + char **argv = Base::SplitCommandToArgs(formatCommand->parameters.c_str(), &argc); + for (int i = 0; i < argc - CMD_ARG1_COUNT; i++) { + if (argv[i] == CMDSTR_FILE_REMOTE_PARAMETER) { + hChannel->bFileFromClient = true; + WRITE_LOG(LOG_INFO, "defaul mode file from client"); + break; + } + } } else if (CMD_FORWARD_INIT == formatCommand->cmdFlag) { cmdFlag = "fport "; sizeCmdFlag = 6; // 6: cmdFlag fport size @@ -473,7 +483,7 @@ bool HdcServerForClient::TaskCommand(HChannel hChannel, void *formatCommandInput if (!hSession) { return false; } - if (CMD_FILE_INIT == formatCommand->cmdFlag) { + if ((CMD_FILE_INIT == formatCommand->cmdFlag) && hChannel->bFileFromClient) { // file send from client mode, CMD_FILE_INIT command send back to client WRITE_LOG(LOG_INFO, "file send from client mode, CMD_FILE_INIT command send back to client"); SendChannelWithCmd(hChannel, CMD_FILE_INIT, payload, sizeSend - sizeCmdFlag); @@ -482,7 +492,7 @@ bool HdcServerForClient::TaskCommand(HChannel hChannel, void *formatCommandInput ptrServer->DispatchTaskData(hSession, hChannel->channelId, formatCommand->cmdFlag, payload, sizeSend - sizeCmdFlag); } else { // Send to Daemon-side to do - WRITE_LOG(LOG_INFO, "TaskCommand recv cmd send to daemon"); + WRITE_LOG(LOG_INFO, "TaskCommand recv cmd send to daemon cmd = %u", formatCommand->cmdFlag); SendToDaemon(hChannel, formatCommand->cmdFlag, payload, sizeSend - sizeCmdFlag); } return true; @@ -676,7 +686,7 @@ int HdcServerForClient::ReadChannel(HChannel hChannel, uint8_t *bufPtr, const in } uint16_t command = *reinterpret_cast(bufPtr); - if (command != 0) { + if (command != 0 && hChannel->bFileSend) { // server directly passthrough file command to daemon if (!SendToDaemon(hChannel, command, bufPtr + sizeof(uint16_t), bytesIO - sizeof(uint16_t))) { WRITE_LOG(LOG_FATAL, "Client ReadChannel : direct send to daemon failed"); @@ -685,12 +695,11 @@ int HdcServerForClient::ReadChannel(HChannel hChannel, uint8_t *bufPtr, const in } struct TranslateCommand::FormatCommand formatCommand = { 0 }; if (!hChannel->interactiveShellMode) { - string retEcho = String2FormatCommand((char *)bufPtr + sizeof(uint16_t), bytesIO - sizeof(uint16_t), - &formatCommand); + string retEcho = String2FormatCommand((char *)bufPtr, bytesIO, &formatCommand); if (retEcho.length()) { - if (!strcmp((char *)bufPtr + sizeof(uint16_t), CMDSTR_SOFTWARE_HELP.c_str()) - || !strcmp((char *)bufPtr + sizeof(uint16_t), CMDSTR_SOFTWARE_VERSION.c_str()) - || !strcmp((char *)bufPtr + sizeof(uint16_t), "flash")) { + if (!strcmp((char *)bufPtr, CMDSTR_SOFTWARE_HELP.c_str()) + || !strcmp((char *)bufPtr, CMDSTR_SOFTWARE_VERSION.c_str())) { + || !strcmp((char *)bufPtr, "flash")) { EchoClient(hChannel, MSG_OK, retEcho.c_str()); } else { EchoClient(hChannel, MSG_FAIL, retEcho.c_str()); @@ -702,8 +711,7 @@ int HdcServerForClient::ReadChannel(HChannel hChannel, uint8_t *bufPtr, const in return ret; } } else { - formatCommand.parameters = string(reinterpret_cast(bufPtr + sizeof(uint16_t)), - bytesIO - sizeof(uint16_t)); + formatCommand.parameters = string(reinterpret_cast(bufPtr), bytesIO); formatCommand.cmdFlag = CMD_SHELL_DATA; } diff --git a/src/host/server_for_client.h b/src/host/server_for_client.h index 42e5d56e..92395d4d 100755 --- a/src/host/server_for_client.h +++ b/src/host/server_for_client.h @@ -25,7 +25,7 @@ public: int Initial(); void EchoClient(HChannel hChannel, MessageLevel level, const char *msg, ...); void EchoClientRaw(const HChannel hChannel, uint8_t *payload, const int payloadSize); - void SendToClient(const HChannel hChannel, const uint16_t commandFlag, uint8_t *payload, const int payloadSize); + void SendCommandToClient(const HChannel hChannel, const uint16_t commandFlag, uint8_t *payload, const int payloadSize); uint16_t GetTCPListenPort(); void Stop(); -- Gitee From 28468a7a0e16f57ae675642d5c5fa3695416bd71 Mon Sep 17 00:00:00 2001 From: faithwang Date: Tue, 5 Jul 2022 20:40:56 +0800 Subject: [PATCH 2/2] update Signed-off-by: faithwang --- src/common/define.h | 3 ++- src/common/define_plus.h | 7 ++++++- src/common/session.cpp | 5 ++++- src/common/uart.cpp | 5 ++++- src/host/client.cpp | 7 ++++--- src/host/host_tcp.cpp | 3 ++- src/host/host_tcp.h | 4 ++-- src/host/host_uart.cpp | 1 + src/host/host_uart.h | 2 ++ src/host/main.cpp | 5 +++-- src/host/server.cpp | 8 ++++++-- src/host/server.h | 2 +- src/host/server_for_client.cpp | 31 ++++++++++++++++++++++--------- src/host/server_for_client.h | 4 ++-- src/host/translate.cpp | 10 +++++++--- 15 files changed, 68 insertions(+), 29 deletions(-) diff --git a/src/common/define.h b/src/common/define.h index dd2df2e5..b814f2a8 100755 --- a/src/common/define.h +++ b/src/common/define.h @@ -84,7 +84,8 @@ const string CMDSTR_GENERATE_KEY = "keygen"; const string CMDSTR_KILL_SERVER = "kserver"; const string CMDSTR_KILL_DAEMON = "kdaemon"; const string CMDSTR_LIST_TARGETS = "list targets"; -const string CMDSTR_CHECK_VERSION = "check"; +const string CMDSTR_CHECK_SERVER = "checkserver"; +const string CMDSTR_CHECK_DEVICE = "checkdevice"; const string CMDSTR_CONNECT_TARGET = "tconn"; const string CMDSTR_CONNECT_ANY = "any"; const string CMDSTR_SHELL = "shell"; diff --git a/src/common/define_plus.h b/src/common/define_plus.h index 9a713ffd..799cf3f9 100755 --- a/src/common/define_plus.h +++ b/src/common/define_plus.h @@ -150,7 +150,8 @@ enum HdcCommand { CMD_KERNEL_ECHO_RAW, CMD_KERNEL_ENABLE_KEEPALIVE, CMD_KERNEL_WAKEUP_SLAVETASK, - CMD_CHECK_VERSION, + CMD_CHECK_SERVER, + CMD_CHECK_DEVICE, // One-pass simple commands CMD_UNITY_COMMAND_HEAD = 1000, // not use CMD_UNITY_EXECUTE, @@ -349,6 +350,7 @@ struct HdcSession { bool handshakeOK; // Is an expected peer side bool isDead; bool voteReset; + bool isCheck = false; string connectKey; uint8_t connType; // ConnType uint32_t sessionId; @@ -469,6 +471,8 @@ struct HdcChannel { uv_tty_t stdinTty; uv_tty_t stdoutTty; char bufStd[128]; + bool isCheck = false; + string key; bool bFileSend = false; bool bFileFromClient = false; }; @@ -481,6 +485,7 @@ struct HdcDaemonInformation { string usbMountPoint; string devName; HSession hSession; + string version; }; using HDaemonInfo = struct HdcDaemonInformation *; diff --git a/src/common/session.cpp b/src/common/session.cpp index 89aeb924..8870a48a 100755 --- a/src/common/session.cpp +++ b/src/common/session.cpp @@ -1055,7 +1055,10 @@ bool HdcSessionBase::WorkThreadStartSession(HSession hSession) handshake.banner = HANDSHAKE_MESSAGE; handshake.sessionId = hSession->sessionId; handshake.connectKey = hSession->connectKey; - handshake.version = Base::GetVersion() + HDC_MSG_HASH; + if (!hSession->isCheck) { + handshake.version = Base::GetVersion() + HDC_MSG_HASH; + WRITE_LOG(LOG_INFO, "set version = %s", handshake.version.c_str()); + } handshake.authType = AUTH_NONE; string hs = SerialStruct::SerializeToString(handshake); #ifdef HDC_SUPPORT_UART diff --git a/src/common/uart.cpp b/src/common/uart.cpp index 03292f85..cd58d41e 100644 --- a/src/common/uart.cpp +++ b/src/common/uart.cpp @@ -731,7 +731,10 @@ void HdcUARTBase::SendPkgInUARTOutMap() } // we will ready to send the package WRITE_LOG(LOG_DEBUG, "UartPackageManager: send pkg %s", it->ToDebugString().c_str()); - SendUARTRaw(nullptr, it->msgSendBuf.data(), it->msgSendBuf.size()); + if (!SendUARTRaw(nullptr, it->msgSendBuf.data(), it->msgSendBuf.size())) { + WRITE_LOG(LOG_WARN, "SendUARTRaw failed!"); + break; + } if (it->response) { // response pkg dont need wait response again. WRITE_LOG(LOG_DEBUG, "UartPackageManager: erase pkg %s", diff --git a/src/host/client.cpp b/src/host/client.cpp index 1be399a7..711f46d5 100755 --- a/src/host/client.cpp +++ b/src/host/client.cpp @@ -148,8 +148,9 @@ string HdcClient::AutoConnectKey(string &doCommand, const string &preConnectKey) vecNoConnectKeyCommand.push_back(CMDSTR_SOFTWARE_HELP); vecNoConnectKeyCommand.push_back(CMDSTR_TARGET_DISCOVER); vecNoConnectKeyCommand.push_back(CMDSTR_LIST_TARGETS); - vecNoConnectKeyCommand.push_back(CMDSTR_CHECK_VERSION); + vecNoConnectKeyCommand.push_back(CMDSTR_CHECK_SERVER); vecNoConnectKeyCommand.push_back(CMDSTR_CONNECT_TARGET); + vecNoConnectKeyCommand.push_back(CMDSTR_CHECK_DEVICE); vecNoConnectKeyCommand.push_back(CMDSTR_KILL_SERVER); vecNoConnectKeyCommand.push_back(CMDSTR_FORWARD_FPORT + " ls"); vecNoConnectKeyCommand.push_back(CMDSTR_FORWARD_FPORT + " rm"); @@ -416,7 +417,7 @@ int HdcClient::ReadChannel(HChannel hChannel, uint8_t *buf, const int bytesIO) uint16_t command = 0; bool bOffset = false; - if (bytesIO >= sizeof(uint16_t)) { + if (bytesIO >= static_cast(sizeof(uint16_t))) { command = *reinterpret_cast(buf); bOffset = (command == CMD_CHECK_SERVER) || (command == CMD_FILE_INIT) @@ -428,7 +429,7 @@ int HdcClient::ReadChannel(HChannel hChannel, uint8_t *buf, const int bytesIO) || (command == CMD_DIR_MODE); } if ((isCheckVersionCmd || hChannel->bFileSend) && bOffset) { - if (CMD_CHECK_VERSION == command) { + if (CMD_CHECK_SERVER == command) { WRITE_LOG(LOG_DEBUG, "recieve CMD_CHECK_VERSION command"); string version(reinterpret_cast(buf + sizeof(uint16_t)), bytesIO - sizeof(uint16_t)); fprintf(stdout, "Client version:%s, server version:%s\n", Base::GetVersion().c_str(), version.c_str()); diff --git a/src/host/host_tcp.cpp b/src/host/host_tcp.cpp index 7fabe9bd..0213cddb 100644 --- a/src/host/host_tcp.cpp +++ b/src/host/host_tcp.cpp @@ -120,7 +120,7 @@ Finish: ptrConnect->FreeSession(hSession->sessionId); } -HSession HdcHostTCP::ConnectDaemon(const string &connectKey) +HSession HdcHostTCP::ConnectDaemon(const string &connectKey, bool isCheck) { char ip[BUF_SIZE_TINY] = ""; uint16_t port = 0; @@ -133,6 +133,7 @@ HSession HdcHostTCP::ConnectDaemon(const string &connectKey) if (!hSession) { return nullptr; } + hSession->isCheck = isCheck; hSession->connectKey = connectKey; struct sockaddr_in dest; uv_ip4_addr(ip, port, &dest); diff --git a/src/host/host_tcp.h b/src/host/host_tcp.h index 9cc5786e..17737c66 100644 --- a/src/host/host_tcp.h +++ b/src/host/host_tcp.h @@ -22,7 +22,7 @@ public: HdcHostTCP(const bool serverOrDaemonIn, void *ptrMainBase); virtual ~HdcHostTCP(); void FindLanDaemon(); - HSession ConnectDaemon(const string &connectKey); + HSession ConnectDaemon(const string &connectKey, bool isCheck = false); void Stop(); list lstDaemonResult; @@ -35,4 +35,4 @@ private: bool broadcastFindWorking; }; } // namespace Hdc -#endif \ No newline at end of file +#endif diff --git a/src/host/host_uart.cpp b/src/host/host_uart.cpp index 91615c3e..f5f61cf7 100644 --- a/src/host/host_uart.cpp +++ b/src/host/host_uart.cpp @@ -577,6 +577,7 @@ bool HdcHostUART::ConnectMyNeed(HUART hUART, std::string connectKey) hSession->hUART->devUartHandle = hUART->devUartHandle; #endif + hSession->isCheck = isCheck; hSession->hUART->serialPort = hUART->serialPort; WRITE_LOG(LOG_DEBUG, "%s connectkey:%s,port:%s", __FUNCTION__, hSession->connectKey.c_str(), hUART->serialPort.c_str()); diff --git a/src/host/host_uart.h b/src/host/host_uart.h index 0c806e8f..8e55d47d 100644 --- a/src/host/host_uart.h +++ b/src/host/host_uart.h @@ -43,6 +43,7 @@ public: // all the thread maybe need exit if needed. void StopSession(HSession hSession) override; HSession ConnectDaemon(const std::string &connectKey); + void SetCheckFlag(bool flag) { isCheck = flag; }; protected: virtual void OnTransferError(const HSession session) override; @@ -111,6 +112,7 @@ private: std::vector serialPortRemoved; bool uartOpened = false; std::thread sendThread; + bool isCheck; std::vector StringSplit(std::string source, std::string split = ":"); bool GetPortFromKey(const std::string &connectKey, std::string &portName, uint32_t &baudRate); diff --git a/src/host/main.cpp b/src/host/main.cpp index 2ad347e3..a54f837c 100755 --- a/src/host/main.cpp +++ b/src/host/main.cpp @@ -47,7 +47,8 @@ int IsRegisterCommand(string &outCommand, const char *cmd, const char *cmdnext) registerCommand.push_back(CMDSTR_SOFTWARE_HELP); registerCommand.push_back(CMDSTR_TARGET_DISCOVER); registerCommand.push_back(CMDSTR_LIST_TARGETS); - registerCommand.push_back(CMDSTR_CHECK_VERSION); + registerCommand.push_back(CMDSTR_CHECK_SERVER); + registerCommand.push_back(CMDSTR_CHECK_DEVICE); registerCommand.push_back(CMDSTR_CONNECT_ANY); registerCommand.push_back(CMDSTR_CONNECT_TARGET); registerCommand.push_back(CMDSTR_SHELL); @@ -189,7 +190,7 @@ int RunClientMode(string &commands, string &serverListenString, string &connectK { uv_loop_t loopMain; uv_loop_init(&loopMain); - HdcClient client(false, serverListenString, &loopMain, commands == CMDSTR_CHECK_VERSION); + HdcClient client(false, serverListenString, &loopMain, commands == CMDSTR_CHECK_SERVER); if (!commands.size()) { Base::PrintMessage("Unknown operation command..."); std::cerr << TranslateCommand::Usage(); diff --git a/src/host/server.cpp b/src/host/server.cpp index 66fa05a4..9120eacd 100755 --- a/src/host/server.cpp +++ b/src/host/server.cpp @@ -494,6 +494,8 @@ bool HdcServer::ServerSessionHandshake(HSession hSession, uint8_t *payload, int } else { hdiNew->devName = handshake.buf; } + WRITE_LOG(LOG_INFO, "handshake.version = %s", handshake.version.c_str()); + hdiNew->version = handshake.version; AdminDaemonMap(OP_UPDATE, hSession->connectKey, hdiNew); hSession->handshakeOK = true; return true; @@ -758,7 +760,7 @@ void HdcServer::CreatConnectUart(HSession hSession) } #endif // -1,has old,-2 error -int HdcServer::CreateConnect(const string &connectKey) +int HdcServer::CreateConnect(const string &connectKey, bool isCheck) { uint8_t connType = 0; if (connectKey.find(":") != std::string::npos) { // TCP @@ -789,13 +791,15 @@ int HdcServer::CreateConnect(const string &connectKey) AdminDaemonMap(OP_QUERY, connectKey, hdi); } if (!hdi || hdi->connStatus == STATUS_CONNECTED) { + WRITE_LOG(LOG_FATAL, "Connected return"); return ERR_GENERIC; } HSession hSession = nullptr; if (CONN_TCP == connType) { - hSession = clsTCPClt->ConnectDaemon(connectKey); + hSession = clsTCPClt->ConnectDaemon(connectKey, isCheck); } else if (CONN_SERIAL == connType) { #ifdef HDC_SUPPORT_UART + clsUARTClt->SetCheckFlag(isCheck); hSession = clsUARTClt->ConnectDaemon(connectKey); #endif } else { diff --git a/src/host/server.h b/src/host/server.h index c3273d21..86a923d9 100755 --- a/src/host/server.h +++ b/src/host/server.h @@ -26,7 +26,7 @@ public: virtual string AdminDaemonMap(uint8_t opType, const string &connectKey, HDaemonInfo &hDaemonInfoInOut); string AdminForwardMap(uint8_t opType, const string &taskString, HForwardInfo &hForwardInfoInOut); void CleanForwardMap(uint32_t sessionId); - int CreateConnect(const string &connectKey); + int CreateConnect(const string &connectKey, bool isCheck); bool Initial(const char *listenString); void AttachChannel(HSession hSession, const uint32_t channelId) override; void DeatchChannel(HSession hSession, const uint32_t channelId) override; diff --git a/src/host/server_for_client.cpp b/src/host/server_for_client.cpp index 71a3fbe9..75504035 100755 --- a/src/host/server_for_client.cpp +++ b/src/host/server_for_client.cpp @@ -213,8 +213,14 @@ void HdcServerForClient::OrderConnecTargetResult(uv_timer_t *req) while (true) { if (bConnectOK) { bExitRepet = true; - sRet = "Connect OK"; - thisClass->EchoClient(hChannel, MSG_OK, (char *)sRet.c_str()); + if (hChannel->isCheck) { + WRITE_LOG(LOG_INFO, "%s check device success and remove %s", __FUNCTION__, hChannel->key.c_str()); + thisClass->CommandRemoveSession(hChannel, hChannel->key.c_str()); + thisClass->EchoClient(hChannel, MSG_OK, (char *)hdi->version.c_str()); + } else { + sRet = "Connect OK"; + thisClass->EchoClient(hChannel, MSG_OK, (char *)sRet.c_str()); + } break; } else { uint16_t *bRetryCount = (uint16_t *)hChannel->bufStd; @@ -235,12 +241,12 @@ void HdcServerForClient::OrderConnecTargetResult(uv_timer_t *req) } } -bool HdcServerForClient::NewConnectTry(void *ptrServer, HChannel hChannel, const string &connectKey) +bool HdcServerForClient::NewConnectTry(void *ptrServer, HChannel hChannel, const string &connectKey, bool isCheck) { #ifdef HDC_DEBUG WRITE_LOG(LOG_ALL, "%s %s", __FUNCTION__, connectKey.c_str()); #endif - int childRet = ((HdcServer *)ptrServer)->CreateConnect(connectKey); + int childRet = ((HdcServer *)ptrServer)->CreateConnect(connectKey, isCheck); bool ret = false; if (-1 == childRet) { EchoClient(hChannel, MSG_INFO, "Target is connected, repeat operation"); @@ -377,8 +383,8 @@ bool HdcServerForClient::DoCommandLocal(HChannel hChannel, void *formatCommandIn ret = false; break; } - case CMD_CHECK_VERSION: { - WRITE_LOG(LOG_DEBUG, "CMD_CHECK_VERSION command"); + case CMD_CHECK_SERVER: { + WRITE_LOG(LOG_DEBUG, "CMD_CHECK_SERVER command"); ReportServerVersion(hChannel); ret = false; break; @@ -397,6 +403,13 @@ bool HdcServerForClient::DoCommandLocal(HChannel hChannel, void *formatCommandIn ret = NewConnectTry(ptrServer, hChannel, parameterString); break; } + case CMD_CHECK_DEVICE: { + WRITE_LOG(LOG_INFO, "%s CMD_CHECK_DEVICE %s", __FUNCTION__, parameterString); + hChannel->isCheck = true; + hChannel->key = parameterString; + ret = NewConnectTry(ptrServer, hChannel, parameterString, true); + break; + } case CMD_KERNEL_TARGET_DISCONNECT: { CommandRemoveSession(hChannel, parameterString); break; @@ -674,7 +687,7 @@ int HdcServerForClient::ChannelHandShake(HChannel hChannel, uint8_t *bufPtr, con void HdcServerForClient::ReportServerVersion(HChannel hChannel) { string version = Base::GetVersion(); - SendChannelWithCmd(hChannel, CMD_CHECK_VERSION, (uint8_t *)version.c_str(), version.size()); + SendChannelWithCmd(hChannel, CMD_CHECK_SERVER, (uint8_t *)version.c_str(), version.size()); } // Here is Server to get data, the source is the SERVER's ChildWork to send data @@ -698,14 +711,14 @@ int HdcServerForClient::ReadChannel(HChannel hChannel, uint8_t *bufPtr, const in string retEcho = String2FormatCommand((char *)bufPtr, bytesIO, &formatCommand); if (retEcho.length()) { if (!strcmp((char *)bufPtr, CMDSTR_SOFTWARE_HELP.c_str()) - || !strcmp((char *)bufPtr, CMDSTR_SOFTWARE_VERSION.c_str())) { + || !strcmp((char *)bufPtr, CMDSTR_SOFTWARE_VERSION.c_str()) || !strcmp((char *)bufPtr, "flash")) { EchoClient(hChannel, MSG_OK, retEcho.c_str()); } else { EchoClient(hChannel, MSG_FAIL, retEcho.c_str()); } } - WRITE_LOG(LOG_DEBUG, "ReadChannel command: %s", bufPtr + sizeof(uint16_t)); + WRITE_LOG(LOG_DEBUG, "ReadChannel command: %s", bufPtr); if (formatCommand.bJumpDo) { ret = -10; return ret; diff --git a/src/host/server_for_client.h b/src/host/server_for_client.h index 92395d4d..c7229220 100755 --- a/src/host/server_for_client.h +++ b/src/host/server_for_client.h @@ -37,7 +37,7 @@ private: void ReportServerVersion(HChannel hChannel); bool DoCommand(HChannel hChannel, void *formatCommandInput); void OrderFindTargets(HChannel hChannel); - bool NewConnectTry(void *ptrServer, HChannel hChannel, const string &connectKey); + bool NewConnectTry(void *ptrServer, HChannel hChannel, const string &connectKey, bool isCheck = false); static void OrderConnecTargetResult(uv_timer_t *req); bool SendToDaemon(HChannel hChannel, const uint16_t commandFlag, uint8_t *bufPtr, const int bufSize); int BindChannelToSession(HChannel hChannel, uint8_t *bufPtr, const int bytesIO); @@ -59,4 +59,4 @@ private: void *clsServer; }; } // namespace Hdc -#endif \ No newline at end of file +#endif diff --git a/src/host/translate.cpp b/src/host/translate.cpp index e9c2ccfb..32510ab1 100755 --- a/src/host/translate.cpp +++ b/src/host/translate.cpp @@ -27,7 +27,8 @@ namespace TranslateCommand { " -v/version - Print hdc version\n" " -l 0-5 - Set runtime loglevel\n" " -t connectkey - Use device with given connect key\n" - " check - check client-server version\n" + " checkserver - check client-server version\n" + " checkdevice - check server-daemon version\n" "\n" "---------------------------------component commands:-------------------------------\n" "session commands(on server):\n" @@ -228,8 +229,11 @@ namespace TranslateCommand { if (strstr(input.c_str(), " -v")) { outCmd->parameters = "v"; } - } else if (!strncmp(input.c_str(), CMDSTR_CHECK_VERSION.c_str(), CMDSTR_CHECK_VERSION.size())) { - outCmd->cmdFlag = CMD_CHECK_VERSION; + } else if (!strncmp(input.c_str(), CMDSTR_CHECK_SERVER.c_str(), CMDSTR_CHECK_SERVER.size())) { + outCmd->cmdFlag = CMD_CHECK_SERVER; + } else if (!strncmp(input.c_str(), CMDSTR_CHECK_DEVICE.c_str(), CMDSTR_CHECK_DEVICE.size())) { + outCmd->parameters = input.c_str() + CMDSTR_CHECK_DEVICE.size() + 1; // with ' ' + outCmd->cmdFlag = CMD_CHECK_DEVICE; } else if (!strcmp(input.c_str(), CMDSTR_CONNECT_ANY.c_str())) { outCmd->cmdFlag = CMD_KERNEL_TARGET_ANY; } else if (!strncmp(input.c_str(), CMDSTR_CONNECT_TARGET.c_str(), CMDSTR_CONNECT_TARGET.size())) { -- Gitee