• 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     static void AcceptClient(uv_stream_t *server, int status);
36     bool SetTCPListen();
37     int ReadChannel(HChannel hChannel, uint8_t *bufPtr, const int bytesIO) override;
38     void ReportServerVersion(HChannel hChannel);
39     bool DoCommand(HChannel hChannel, void *formatCommandInput, HDaemonInfo &hdi);
40     void OrderFindTargets(HChannel hChannel);
41     bool NewConnectTry(void *ptrServer, HChannel hChannel, const string &connectKey, bool isCheck = false);
42     static void OrderConnecTargetResult(uv_timer_t *req);
43     bool SendToDaemon(HChannel hChannel, const uint16_t commandFlag, uint8_t *bufPtr, const int bufSize);
44     int BindChannelToSession(HChannel hChannel, uint8_t *bufPtr, const int bytesIO);
45     bool CheckAutoFillTarget(HChannel hChannel);
46     bool CommandRemoveSession(HChannel hChannel, const char *connectKey);
47     bool CommandRemoveForward(const string &forwardKey);
48     bool RemoveFportkey(const string &forwardKey);
49     bool DoCommandLocal(HChannel hChannel, void *formatCommandInput);
50     bool DoCommandRemote(HChannel hChannel, void *formatCommandInput);
51     void GetTargetList(HChannel hChannel, void *formatCommandInput);
52     bool GetAnyTarget(HChannel hChannel);
53     bool WaitForAny(HChannel hChannel);
54     bool RemoveForward(HChannel hChannel, const char *parameterString);
55     bool TaskCommand(HChannel hChannel, void *formatCommandInput);
56     void HandleRemote(HChannel hChannel, string &parameters, RemoteType flag);
57     int ChannelHandShake(HChannel hChannel, uint8_t *bufPtr, const int bytesIO);
58     bool ChannelSendSessionCtrlMsg(vector<uint8_t> &ctrlMsg, uint32_t sessionId) override;
59     HSession FindAliveSession(uint32_t sessionId);
60     HSession FindAliveSessionFromDaemonMap(const HChannel hChannel);
61     string GetErrorString(uint32_t errorCode);
62     void PrintLastError(HChannel HChannel);
63     bool CommandMatchDaemonFeature(uint16_t cmdFlag, const HDaemonInfo &hdi);
64 
65     uv_tcp_t tcpListen;
66     void *clsServer;
67     const std::set<uint16_t> FEATURE_CHECK_SET = {
68         CMD_UNITY_EXECUTE_EX,
69     }; // feature cmdFlag
70     const std::unordered_map<uint32_t, std::string> ErrorStringEnglish = {
71 #ifdef _WIN32
72         {0x000005, "Failed to load the authorization pub key!\r\n"
73             "Please check the public key directory:[%USERPROFILE%\\.harmony]\r\n"
74             "Alternatively, delete the public key directory and re-run the command.\r\n"
75             "Delete command: \"del %USERPROFILE%\\.harmony\""},
76 #else
77         {0x000005, "Failed to load the authorization pub key!\r\n"
78             "Please check the public key directory:[~/.harmony]\r\n"
79             "Alternatively, delete the public key directory and re-run the command.\r\n"
80             "Delete command: \"rm -r ~/.harmony\""},
81 #endif
82         {0x002103, "Failed to start the HDC server process!\r\n"
83             "Please check the HDC server process port is occupied or used as an exception port.\r\n"
84             "Alternatively, change the OHOS_HDC_SERVER_PORT environment variable and re-run the command."},
85         {0xFFFFFF, "Unknown error"},
86     };
87 };
88 }  // namespace Hdc
89 #endif
90