• 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 
16 #ifndef HDC_UART_H
17 #define HDC_UART_H
18 
19 #include <cassert>
20 #include <chrono>
21 #include <cinttypes>
22 #include <cstdarg>
23 #include <cstdio>
24 #include <cstdlib>
25 #include <ctime>
26 #include <fcntl.h>
27 #include <functional>
28 #include <numeric>
29 #include <sstream>
30 #include <sys/types.h>
31 #include <unordered_set>
32 #include <unistd.h>
33 #include <vector>
34 
35 #ifdef HOST_MINGW
36 #include "tchar.h"
37 #include "windows.h"
38 #include <setupapi.h>
39 #include <winnt.h>
40 #else
41 #include <fcntl.h> // open close
42 #include <pthread.h>
43 #include <termios.h> // truct termios
44 #endif
45 
46 enum UartTimeConst {
47     UV_TIMEOUT = 10,
48     UV_REPEAT = 100,
49     TIMEOUTS_R_INTERALTIMEOUT = 1000,
50     TIMEOUTS_R_TOALTIMEOUTMULTIPLIER = 500,
51     TIMEOUTS_R_TIMEOUTCONSTANT = 5000
52 };
53 enum UartSetSerialNBits {
54     UART_BIT1 = 7,
55     UART_BIT2 = 8
56 };
57 enum UartSetSerialNSpeed {
58     UART_SPEED2400 = 2400,
59     UART_SPEED4800 = 4800,
60     UART_SPEED9600 = 9600,
61     UART_SPEED115200 = 115200,
62     UART_SPEED921600 = 921600,
63     UART_SPEED1500000 = 1500000
64 };
65 enum UartSetSerialNStop {
66     UART_STOP1 = 1,
67     UART_STOP2 = 2
68 };
69 
70 const std::string CMDSTR_TMODE_UART = "uart";
71 const std::string UART_HDC_NODE = "/dev/ttyS4";
72 const std::string CONSOLE_ACTIVE_NODE = "/sys/class/tty/console/active";
73 constexpr int UART_IO_WAIT_TIME_100 = 100;
74 constexpr int UART_IO_WAIT_TIME = 1000;
75 
76 const int ERR_GENERIC = -1;
77 const int ERR_SUCCESS = 0;
78 constexpr uint16_t MAX_UART_SIZE_IOBUF = 4096; // MAX_SIZE_IOBUF;
79 constexpr size_t MAX_READ_BUFFER = MAX_UART_SIZE_IOBUF * 10;
80 constexpr int WAIT_RESPONSE_TIME_OUT_MS = 1000; // 1000ms
81 constexpr int READ_GIVE_UP_TIME_OUT_TIME_MS = 500; // 500ms
82 constexpr uint16_t BUF_SIZE_DEFAULT = 1024;
83 constexpr uint32_t DEFAULT_BAUD_RATE_VALUE = 1500000;
84 
85 #ifdef HOST_MINGW
86 HANDLE WinOpenSerialPort(std::string portName);
87 bool WinSetSerialPort(HANDLE devUartHandle, std::string serialport, int byteSize, int baudRate);
88 bool WinCloseSerialPort(HANDLE& handle);
89 ssize_t WinReadUartDev(HANDLE handle, std::vector<uint8_t> &readBuf, size_t expectedSize, OVERLAPPED &overRead);
90 ssize_t WinWriteUartDev(HANDLE handle, uint8_t *data, const size_t length, OVERLAPPED &ovWrite);
91 
92 #else
93 int GetUartSpeed(int speed);
94 int GetUartBits(int bits);
95 int OpenSerialPort(std::string portName);
96 int SetSerial(int fd, int nSpeed, int nBits, char nEvent, int nStop);
97 #endif
98 
99 ssize_t ReadUartDev(int handle, std::vector<uint8_t> &readBuf, size_t expectedSize);
100 ssize_t WriteUartDev(int handle, uint8_t *data, const size_t length);
101 bool CloseSerialPort(int& handle);
102 int CloseFd(int &fd);
103 
104 #endif
105