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