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 19 extern std::map<std::string, std::string> g_lists; 20 extern bool g_show; 21 22 namespace Hdc { 23 // Avoiding Circular dependency 24 class HdcHostApp; 25 26 class HdcClient : public HdcChannelBase { 27 public: 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 34 protected: 35 private: 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 int ConnectServerForClient(const char *ip, uint16_t port); 42 int ReadChannel(HChannel hChannel, uint8_t *buf, const int bytesIO) override; 43 int PreHandshake(HChannel hChannel, const uint8_t *buf); 44 string AutoConnectKey(string &doCommand, const string &preConnectKey) const; 45 uint32_t GetLastPID(); 46 bool StartKillServer(const char *cmd, bool startOrKill); 47 void BindLocalStd(); 48 void BindLocalStd(HChannel hChannel); 49 void ModifyTty(bool setOrRestore, uv_tty_t *tty); 50 void NotifyInstanceChannelFree(HChannel hChannel) override; 51 bool IsOffset(uint16_t cmd); 52 HTaskInfo GetRemoteTaskInfo(HChannel hChannel); 53 bool WaitFor(const string &str); 54 string ListTargetsAll(const string &str); 55 void UpdateList(const string &str); 56 57 #ifdef _WIN32 58 static string GetHilogPath(); 59 void RunCommandWin32(const string& cmd); 60 #else 61 static void RunCommand(const string& cmd); 62 #endif 63 void RunExecuteCommand(const string& cmd); 64 65 #ifndef _WIN32 66 termios terminalState; 67 #endif 68 string connectKey; 69 string command; 70 uint16_t debugRetryCount; 71 bool bShellInteractive = false; 72 uv_timer_t waitTimeDoCmd; 73 uv_check_t ctrlServerWork; 74 HChannel channel; 75 std::unique_ptr<HdcFile> fileTask; 76 std::unique_ptr<HdcHostApp> appTask; 77 bool isCheckVersionCmd = false; 78 }; 79 } // namespace Hdc 80 #endif 81