• 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_HOST_UART_H
16 #define HDC_HOST_UART_H
17 #include <condition_variable>
18 #include "host_common.h"
19 #ifdef HOST_MINGW
20 #include "tchar.h"
21 #include "windows.h"
22 #include <setupapi.h>
23 #include <winnt.h>
24 #elif defined(HOST_LINUX)||defined(HOST_MAC)
25 #include <fcntl.h> // open close
26 #include <pthread.h>
27 #include <termios.h> // truct termios
28 #endif
29 
30 namespace Hdc {
31 class HdcServer;
32 
33 class HdcHostUART : public HdcUARTBase {
34 public:
35     explicit HdcHostUART(HdcServer &, ExternInterface & = HdcUARTBase::defaultInterface);
36     ~HdcHostUART();
37     int Initial();
38     virtual void Stop();
39     HSession ConnectDaemonByUart(const HSession hSession,
40                                  [[maybe_unused]] const HDaemonInfo = nullptr);
41 
42     // logic layer will free the session
43     // all the thread maybe need exit if needed.
44     void StopSession(HSession hSession) override;
45     HSession ConnectDaemon(const std::string &connectKey);
SetCheckFlag(bool flag)46     void SetCheckFlag(bool flag) { isCheck = flag; };
47 
48 protected:
49     virtual void OnTransferError(const HSession session) override;
50     virtual HSession GetSession(const uint32_t sessionId, bool create) override;
51     virtual void Restartession(const HSession session) override;
52 
53 private:
54     enum UartCheckStatus {
55         HOST_UART_EMPTY = 0, // default value
56         HOST_UART_IGNORE = 1,
57         HOST_UART_READY,
58         HOST_UART_REGISTER,
59     };
60     // review maybe merge to base ?
61     virtual bool StartUartSendThread();
62     virtual bool StartUartReadThread(HSession hSession);
63 
64     size_t SendUARTDev(HSession hSession, uint8_t *data, const size_t length);
UvWatchUartDevPlugin(uv_timer_t * handle)65     static inline void UvWatchUartDevPlugin(uv_timer_t *handle)
66     {
67         if (handle != nullptr) {
68             HdcHostUART *thisClass = static_cast<HdcHostUART *>(handle->data);
69             if (thisClass != nullptr) {
70                 thisClass->WatchUartDevPlugin();
71                 return;
72             }
73         }
74         WRITE_LOG(LOG_FATAL, "%s have not got correct class parameter", __FUNCTION__);
75     };
76     virtual void WatchUartDevPlugin();
77     void KickoutZombie(HSession hSession);
78     virtual void UpdateUARTDaemonInfo(const std::string &connectKey, HSession hSession, ConnStatus connStatus);
79     bool ConnectMyNeed(HUART hUART, std::string connectKey = "");
80     virtual int OpenSerialPort(const std::string &portName = "");
81     virtual void CloseSerialPort(const HUART hUART);
82     virtual RetErrCode StartupUARTWork();
83 
84     // we use this function check if the uart read nothing in a timeout
85     // if not , that means this uart maybe not our daemon
86     // More importantly, the bootloader will output data. We use this to detect whether it is the
87     // bootloader stage.
88     virtual bool WaitUartIdle(HdcUART &uart, bool retry = true);
89     virtual void SendUartSoftReset(HSession hSession, uint32_t sessionId) override;
90 
91     virtual bool EnumSerialPort(bool &portChange);
92     virtual bool IsDeviceOpened(const HdcUART &uart);
93     virtual bool NeedStop(const HSession hSession);
94     virtual void UartReadThread(HSession hSession);
95 #ifdef HOST_MINGW
96     int WinSetSerial(HUART hUART, string serialPort, int byteSize, int eqBaudRate);
97     bool enumDetailsSerialPorts(bool *portChange);
98     static constexpr uint8_t PORT_NAME_LEN = 10;
99     static constexpr uint8_t PORT_NUM = 100;
100 #elif defined(HOST_LINUX)||defined(HOST_MAC)
101     void EnumLinuxSerialPort(bool *PortStatusChange);
102 #endif
103     virtual void UartWriteThread();
104 
105     uv_timer_t devUartWatcher;
106     std::mutex semUartDevCheck;
107     uint32_t baudRate = 0;
108     const int intervalDevCheck = 3000;
109     std::map<string, UartCheckStatus> mapIgnoreDevice;
110     std::unordered_set<std::string> connectedPorts;
111     std::vector<string> serialPortInfo;
112     std::vector<string> serialPortRemoved;
113     bool uartOpened = false;
114     std::thread sendThread;
115     bool isCheck = false;
116 
117     std::vector<std::string> StringSplit(std::string source, std::string split = ":");
118     bool GetPortFromKey(const std::string &connectKey, std::string &portName, uint32_t &baudRate);
119     HdcServer &server;
120 };
121 } // namespace Hdc
122 #endif
123