1/* 2 * Copyright (C) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15#ifndef HDC_CLIENT_H 16#define HDC_CLIENT_H 17#include "host_common.h" 18 19extern std::map<std::string, std::string> g_lists; 20extern bool g_show; 21 22namespace Hdc { 23// Avoiding Circular dependency 24class HdcHostApp; 25 26class HdcClient : public HdcChannelBase { 27public: 28 HdcClient(const bool serverOrClient, const string &addrString, uv_loop_t *loopMainIn, bool checkVersion = false); 29 virtual ~HdcClient(); 30 int Initial(const string &connectKeyIn); 31 int ExecuteCommand(const string &commandIn); 32 int CtrlServiceWork(const char *commandIn); 33 34protected: 35private: 36 static void DoCtrlServiceWork(uv_check_t *handle); 37 static void Connect(uv_connect_t *connection, int status); 38 static void AllocStdbuf(uv_handle_t *handle, size_t sizeWanted, uv_buf_t *buf); 39 static void ReadStd(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf); 40 static void CommandWorker(uv_timer_t *handle); 41 static void RetryTcpConnectWorker(uv_timer_t *handle); 42 int ConnectServerForClient(const char *ip, uint16_t port); 43 int ReadChannel(HChannel hChannel, uint8_t *buf, const int bytesIO) override; 44 int PreHandshake(HChannel hChannel, const uint8_t *buf); 45 string AutoConnectKey(string &doCommand, const string &preConnectKey) const; 46 uint32_t GetLastPID(); 47 bool StartServer(const string &cmd); 48 bool KillServer(const string &cmd); 49 void BindLocalStd(); 50 void BindLocalStd(HChannel hChannel); 51 void ModifyTty(bool setOrRestore, uv_tty_t *tty); 52 void NotifyInstanceChannelFree(HChannel hChannel) override; 53 bool IsOffset(uint16_t cmd); 54 HTaskInfo GetRemoteTaskInfo(HChannel hChannel); 55 bool WaitFor(const string &str); 56 string ListTargetsAll(const string &str); 57 void UpdateList(const string &str); 58 59#ifdef _WIN32 60 static string GetHilogPath(); 61 void RunCommandWin32(const string& cmd); 62#else 63 static void RunCommand(const string& cmd); 64#endif 65 void RunExecuteCommand(const string& cmd); 66 67#ifndef _WIN32 68 termios terminalState; 69#endif 70 string connectKey; 71 string command; 72 uint16_t debugRetryCount; 73 bool bShellInteractive = false; 74 uv_timer_t waitTimeDoCmd; 75 uv_check_t ctrlServerWork; 76 HChannel channel; 77 std::unique_ptr<HdcFile> fileTask; 78 std::unique_ptr<HdcHostApp> appTask; 79 bool isCheckVersionCmd = false; 80 bool isIpV4 = true; 81 struct sockaddr_in destv4; 82 struct sockaddr_in6 dest; 83 uv_timer_t retryTcpConnTimer; 84 uint16_t tcpConnectRetryCount = 0; 85}; 86} // namespace Hdc 87#endif 88