• 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_SERVER_H
16 #define HDC_SERVER_H
17 #include "host_common.h"
18 
19 namespace Hdc {
20 class HdcServer : public HdcSessionBase {
21 public:
22     HdcServer(bool serverOrDaemonIn);
23     virtual ~HdcServer();
24     bool FetchCommand(HSession hSession, const uint32_t channelId, const uint16_t command, uint8_t *payload,
25                       const int payloadSize) override;
26     virtual string AdminDaemonMap(uint8_t opType, const string &connectKey, HDaemonInfo &hDaemonInfoInOut);
27     void AdminDaemonMapForWait(const string &connectKey, HDaemonInfo &hDaemonInfoInOut);
28     string AdminForwardMap(uint8_t opType, const string &taskString, HForwardInfo &hForwardInfoInOut);
29     void CleanForwardMap(uint32_t sessionId);
30     int CreateConnect(const string &connectKey, bool isCheck);
31     bool Initial(const char *listenString);
32     void AttachChannel(HSession hSession, const uint32_t channelId) override;
33     void DeatchChannel(HSession hSession, const uint32_t channelId) override;
34     void AttachChannelInnerForTcp(HSession hSession, const uint32_t channelId);
35     void DeatchChannelInnerForTcp(HSession hSession, const uint32_t channelId);
36 #ifdef HOST_OHOS
37     void AttachChannelInnerForUds(HSession hSession, const uint32_t channelId);
38     void DeatchChannelInnerForUds(HSession hSession, const uint32_t channelId);
39 #endif
40     virtual void EchoToClientsForSession(uint32_t targetSessionId, const string &echo);
41     static bool PullupServer(const char *listenString);
42     static void UsbPreConnect(uv_timer_t *handle);
43     void NotifyInstanceSessionFree(HSession hSession, bool freeOrClear) override;
44 #ifdef HOST_OHOS
45     void SessionSoftReset();
46 #endif
47 
48     HdcHostTCP *clsTCPClt;
49     HdcHostUSB *clsUSBClt;
50 #ifdef HDC_SUPPORT_UART
51     void CreatConnectUart(HSession hSession);
52     static void UartPreConnect(uv_timer_t *handle);
53     HdcHostUART *clsUARTClt = nullptr;
54 #endif
55     void *clsServerForClient;
56     std::atomic<uint32_t> lastErrorNum;
57     void PrintCmdLogEx(const string &cmdStr);
58 
59 private:
60     void ClearInstanceResource() override;
61     void BuildDaemonVisableLine(HDaemonInfo hdi, bool fullDisplay, string &out);
62     void BuildForwardVisableLine(bool fullOrSimble, HForwardInfo hfi, string &echo);
63     void ClearMapDaemonInfo();
64     bool ServerCommand(const uint32_t sessionId, const uint32_t channelId, const uint16_t command, uint8_t *bufPtr,
65                        const int size) override;
66     bool RedirectToTask(HTaskInfo hTaskInfo, HSession hSession, const uint32_t channelId, const uint16_t command,
67                         uint8_t *payload, const int payloadSize) override;
68     bool RemoveInstanceTask(const uint8_t op, HTaskInfo hTask) override;
69     void BuildForwardVisableLine(HDaemonInfo hdi, char *out, int sizeOutBuf);
70     bool HandServerAuth(HSession hSession, SessionHandShake &handshake);
71     void GetDaemonAuthType(HSession hSession, SessionHandShake &handshake);
72     string GetDaemonMapList(uint8_t opType);
73     void UpdateHdiInfo(Hdc::HdcSessionBase::SessionHandShake &handshake, HSession &hSession);
74     bool ServerSessionHandshake(HSession hSession, uint8_t *payload, int payloadSize);
75     bool ServerSSLHandshake(HSession hSession, SessionHandShake &handshake);
76     bool ServerSessionSSLInit(HSession hSession, SessionHandShake &handshake);
77     void GetDaemonMapOnlyOne(HDaemonInfo &hDaemonInfoInOut);
78     void TryStopInstance();
79     static bool PullupServerWin32(const char *path, const char *listenString);
80 
81     uv_rwlock_t daemonAdmin;
82     map<string, HDaemonInfo> mapDaemon;
83     uv_rwlock_t forwardAdmin;
84     map<string, HForwardInfo> mapForward;
85 };
86 }  // namespace Hdc
87 #endif
88