• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     bool ChannelCtrlServer(const string &cmd, string &connectKey);
34 
35 protected:
36 private:
37     static void DoCtrlServiceWork(uv_check_t *handle);
38     static void Connect(uv_connect_t *connection, int status);
39     static void AllocStdbuf(uv_handle_t *handle, size_t sizeWanted, uv_buf_t *buf);
40     static void ReadStd(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf);
41     static void CommandWorker(uv_timer_t *handle);
42     static void RetryTcpConnectWorker(uv_timer_t *handle);
43     int ConnectServerForClient(const char *ip, uint16_t port);
44     int ReadChannel(HChannel hChannel, uint8_t *buf, const int bytesIO) override;
45     int PreHandshake(HChannel hChannel, const uint8_t *buf);
46     string AutoConnectKey(string &doCommand, const string &preConnectKey) const;
47     uint32_t GetLastPID();
48     bool StartServer(const string &cmd);
49     bool KillServer(const string &cmd);
50     void ChannelCtrlServerStart(const char *listenString);
51     void BindLocalStd();
52     void BindLocalStd(HChannel hChannel);
53     void ModifyTty(bool setOrRestore, uv_tty_t *tty);
54     void NotifyInstanceChannelFree(HChannel hChannel) override;
55     bool IsOffset(uint16_t cmd);
56     HTaskInfo GetRemoteTaskInfo(HChannel hChannel);
57     bool WaitFor(const string &str);
58     string ListTargetsAll(const string &str);
59     void UpdateList(const string &str);
60     bool KillMethodByUv(bool isStart);
61 
62 #ifdef _WIN32
63     static string GetHilogPath();
64     void RunCommandWin32(const string& cmd);
65     bool CreatePipePair(HANDLE *hParentRead, HANDLE *hSubWrite, HANDLE *hSubRead, HANDLE *hParentWrite,
66         SECURITY_ATTRIBUTES *sa);
67     bool CreateChildProcess(HANDLE hSubWrite, HANDLE hSubRead, PROCESS_INFORMATION *pi, const string& cmd);
68 #else
69     static void RunCommand(const string& cmd);
70 #endif
71     void RunExecuteCommand(const string& cmd);
72 
73 #ifndef _WIN32
74     termios terminalState;
75 #endif
76     string connectKey;
77     string command;
78     uint16_t debugRetryCount;
79     bool bShellInteractive = false;
80     uv_timer_t waitTimeDoCmd;
81     uv_check_t ctrlServerWork;
82     HChannel channel;
83     std::unique_ptr<HdcFile> fileTask;
84     std::unique_ptr<HdcHostApp> appTask;
85     bool isCheckVersionCmd = false;
86     bool isIpV4 = true;
87     struct sockaddr_in destv4;
88     struct sockaddr_in6 dest;
89     uv_timer_t retryTcpConnTimer;
90     uint16_t tcpConnectRetryCount = 0;
91 };
92 }  // namespace Hdc
93 #endif
94