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     virtual void EchoToClientsForSession(uint32_t targetSessionId, const string &echo);
35     static bool PullupServer(const char *listenString);
36     static void UsbPreConnect(uv_timer_t *handle);
37     void NotifyInstanceSessionFree(HSession hSession, bool freeOrClear) override;
38 
39     HdcHostTCP *clsTCPClt;
40     HdcHostUSB *clsUSBClt;
41 #ifdef HDC_SUPPORT_UART
42     void CreatConnectUart(HSession hSession);
43     static void UartPreConnect(uv_timer_t *handle);
44     HdcHostUART *clsUARTClt = nullptr;
45 #endif
46     void *clsServerForClient;
47 
48 private:
49     void ClearInstanceResource() override;
50     void BuildDaemonVisableLine(HDaemonInfo hdi, bool fullDisplay, string &out);
51     void BuildForwardVisableLine(bool fullOrSimble, HForwardInfo hfi, string &echo);
52     void ClearMapDaemonInfo();
53     bool ServerCommand(const uint32_t sessionId, const uint32_t channelId, const uint16_t command, uint8_t *bufPtr,
54                        const int size) override;
55     bool RedirectToTask(HTaskInfo hTaskInfo, HSession hSession, const uint32_t channelId, const uint16_t command,
56                         uint8_t *payload, const int payloadSize) override;
57     bool RemoveInstanceTask(const uint8_t op, HTaskInfo hTask) override;
58     void BuildForwardVisableLine(HDaemonInfo hdi, char *out, int sizeOutBuf);
59     bool HandServerAuth(HSession hSession, SessionHandShake &handshake);
60     void GetDaemonAuthType(HSession hSession, SessionHandShake &handshake);
61     string GetDaemonMapList(uint8_t opType);
62     void UpdateHdiInfo(Hdc::HdcSessionBase::SessionHandShake &handshake, const string &connectKey);
63     bool ServerSessionHandshake(HSession hSession, uint8_t *payload, int payloadSize);
64     void GetDaemonMapOnlyOne(HDaemonInfo &hDaemonInfoInOut);
65     void TryStopInstance();
66     static bool PullupServerWin32(const char *path, const char *listenString);
67 
68     uv_rwlock_t daemonAdmin;
69     map<string, HDaemonInfo> mapDaemon;
70     uv_rwlock_t forwardAdmin;
71     map<string, HForwardInfo> mapForward;
72 };
73 }  // namespace Hdc
74 #endif
75