• 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 #ifdef __OHOS__
44     static void RetryUdsConnectWorker(uv_timer_t *handle);
45     static void ConnectUds(uv_connect_t *connection, int status);
46     int ConnectUdsServerForClient();
47 #endif
48     int ConnectServerForClient(const char *ip, uint16_t port);
49     int ReadChannel(HChannel hChannel, uint8_t *buf, const int bytesIO) override;
50     int PreHandshake(HChannel hChannel, const uint8_t *buf);
51     string AutoConnectKey(string &doCommand, const string &preConnectKey) const;
52     uint32_t GetLastPID();
53     bool StartServer(const string &cmd);
54     bool KillServer(const string &cmd);
55     void ChannelCtrlServerStart(const char *listenString);
56     void BindLocalStd();
57     void BindLocalStd(HChannel hChannel);
58     void ModifyTty(bool setOrRestore, uv_tty_t *tty);
59     void NotifyInstanceChannelFree(HChannel hChannel) override;
60     bool IsOffset(uint16_t cmd);
61     HTaskInfo GetRemoteTaskInfo(HChannel hChannel);
62     bool WaitFor(const string &str);
63     string ListTargetsAll(const string &str);
64     void UpdateList(const string &str);
65     bool KillMethodByUv(bool isStart);
66 
67 #ifdef _WIN32
68     static string GetHilogPath();
69     void RunCommandWin32(const string& cmd);
70     bool CreatePipePair(HANDLE *hParentRead, HANDLE *hSubWrite, HANDLE *hSubRead, HANDLE *hParentWrite,
71         SECURITY_ATTRIBUTES *sa);
72     bool CreateChildProcess(HANDLE hSubWrite, HANDLE hSubRead, PROCESS_INFORMATION *pi, const string& cmd);
73 #else
74     static void RunCommand(const string& cmd);
75 #endif
76     void RunExecuteCommand(const string& cmd);
77 
78 #ifndef _WIN32
79     termios terminalState;
80 #endif
81     string connectKey;
82     string command;
83     uint16_t debugRetryCount;
84     bool bShellInteractive = false;
85     uv_timer_t waitTimeDoCmd;
86     uv_check_t ctrlServerWork;
87     HChannel channel;
88     std::unique_ptr<HdcFile> fileTask;
89     std::unique_ptr<HdcHostApp> appTask;
90     bool isCheckVersionCmd = false;
91     bool isIpV4 = true;
92     struct sockaddr_in destv4;
93     struct sockaddr_in6 dest;
94     uv_timer_t retryTcpConnTimer;
95     uint16_t tcpConnectRetryCount = 0;
96 #ifdef __OHOS__
97     string serverAddress;
98     uv_timer_t retryUdsConnTimer;
99     uint16_t udsConnectRetryCount = 0;
100 #endif
101 };
102 }  // namespace Hdc
103 #endif
104