• 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_SERVERFORCLIENT_H
16 #define HDC_SERVERFORCLIENT_H
17 #include "host_common.h"
18 #include "translate.h"
19 
20 namespace Hdc {
21 class HdcServerForClient : public HdcChannelBase {
22 public:
23     HdcServerForClient(const bool serverOrClient, const string &addrString, void *pClsServer, uv_loop_t *loopMainIn);
24     virtual ~HdcServerForClient();
25     int Initial();
26     void EchoClient(HChannel hChannel, MessageLevel level, const char *msg, ...);
27     void EchoClientRaw(const HChannel hChannel, uint8_t *payload, const int payloadSize);
28     void SendCommandToClient(const HChannel hChannel, const uint16_t commandFlag, uint8_t *payload,
29                              const int payloadSize);
30     uint16_t GetTCPListenPort();
31     void Stop();
32 
33 protected:
34 private:
35 #ifdef __OHOS__
36     static void AcceptUdsClient(uv_stream_t *server, int status);
37     bool SetUdsListen();
38 #endif
39     static void AcceptClient(uv_stream_t *server, int status);
40     bool SetTCPListen();
41     int ReadChannel(HChannel hChannel, uint8_t *bufPtr, const int bytesIO) override;
42     void ReportServerVersion(HChannel hChannel);
43     bool DoCommand(HChannel hChannel, void *formatCommandInput, HDaemonInfo &hdi);
44     void OrderFindTargets(HChannel hChannel);
45     bool NewConnectTry(void *ptrServer, HChannel hChannel, const string &connectKey, bool isCheck = false);
46     static void OrderConnecTargetResult(uv_timer_t *req);
47     bool SendToDaemon(HChannel hChannel, const uint16_t commandFlag, uint8_t *bufPtr, const int bufSize);
48     int BindChannelToSession(HChannel hChannel, uint8_t *bufPtr, const int bytesIO);
49     bool CheckAutoFillTarget(HChannel hChannel);
50     bool CommandRemoveSession(HChannel hChannel, const char *connectKey);
51     bool CommandRemoveForward(const string &forwardKey);
52     bool RemoveFportkey(const string &forwardKey);
53     bool DoCommandLocal(HChannel hChannel, void *formatCommandInput);
54     bool DoCommandRemote(HChannel hChannel, void *formatCommandInput);
55     void GetTargetList(HChannel hChannel, void *formatCommandInput);
56     bool GetAnyTarget(HChannel hChannel);
57     bool WaitForAny(HChannel hChannel);
58     bool RemoveForward(HChannel hChannel, const char *parameterString);
59     bool TaskCommand(HChannel hChannel, void *formatCommandInput);
60     void HandleRemote(HChannel hChannel, string &parameters, RemoteType flag);
61     int ChannelHandShake(HChannel hChannel, uint8_t *bufPtr, const int bytesIO);
62     bool ChannelSendSessionCtrlMsg(vector<uint8_t> &ctrlMsg, uint32_t sessionId) override;
63     HSession FindAliveSession(uint32_t sessionId);
64     HSession FindAliveSessionFromDaemonMap(const HChannel hChannel);
65     string GetErrorString(uint32_t errorCode);
66     void PrintLastError(HChannel HChannel);
67     bool CommandMatchDaemonFeature(uint16_t cmdFlag, const HDaemonInfo &hdi);
68 #ifdef __OHOS__
69     bool IsServerTransfer(HChannel hChannel, uint16_t cmdFlag, string &parameters);
70 #endif
71 
72 #ifdef __OHOS__
73     uv_pipe_t udsListen;
74 #endif
75     uv_tcp_t tcpListen;
76     void *clsServer;
77     const std::set<uint16_t> FEATURE_CHECK_SET = {
78         CMD_UNITY_EXECUTE_EX,
79     }; // feature cmdFlag
80     const std::unordered_map<uint32_t, std::string> ErrorStringEnglish = {
81 #ifdef _WIN32
82         {0x000005, "Failed to load the authorization pub key!\r\n"
83             "Please check the public key directory:[%USERPROFILE%\\.harmony]\r\n"
84             "Alternatively, delete the public key directory and re-run the command.\r\n"
85             "Delete command: \"del %USERPROFILE%\\.harmony\""},
86 #else
87         {0x000005, "Failed to load the authorization pub key!\r\n"
88             "Please check the public key directory:[~/.harmony]\r\n"
89             "Alternatively, delete the public key directory and re-run the command.\r\n"
90             "Delete command: \"rm -r ~/.harmony\""},
91 #endif
92         {0x002103, "Failed to start the HDC server process!\r\n"
93             "Please check the HDC server process port is occupied or used as an exception port.\r\n"
94             "Alternatively, change the OHOS_HDC_SERVER_PORT environment variable and re-run the command."},
95         {0xFFFFFF, "Unknown error"},
96     };
97 };
98 }  // namespace Hdc
99 #endif
100