1 /* 2 * Copyright (C)2021-2022 Intel Corporation. 3 * SPDX-License-Identifier: BSD-3-Clause 4 */ 5 #ifndef _UART_H_ 6 #define _UART_H_ 7 #include <sys/types.h> 8 #include <stdint.h> 9 #include <sys/queue.h> 10 #include <pthread.h> 11 #include <semaphore.h> 12 #include <sys/un.h> 13 14 #define TTY_PATH_MAX 32U 15 16 #define SECOND_TO_US 1000000 17 #define WAIT_RECV (SECOND_TO_US>>2) 18 #define RETRY_RECV_TIMES 100U 19 20 struct uart_dev { 21 char tty_path[TTY_PATH_MAX]; /**< UART device name */ 22 int tty_fd; /**< the FD of opened UART device */ 23 }; 24 /** 25 * @brief Allocate UART device instance and initialize UART 26 * device according to device name 27 * 28 * @param path UART device name 29 * @return struct uart_dev* Ponit to UART device instance 30 */ 31 struct uart_dev *init_uart_dev(char *path); 32 /** 33 * @brief Close UART devcie and free UART device instance 34 * 35 * @param dev Poin to UART device instance 36 */ 37 void deinit_uart_dev(struct uart_dev *dev); 38 /** 39 * @brief Set handler to handle received message 40 */ 41 ssize_t send_message_by_uart(struct uart_dev *dev, const void *buf, size_t len); 42 /** 43 * @brief Receive message and retry RETRY_RECV_TIMES time to 44 * avoid miss message in some cases. 45 */ 46 ssize_t receive_message_by_uart(struct uart_dev *dev, void *buf, size_t len); 47 /** 48 * @brief Get the file descriptor of a UART device 49 */ get_uart_dev_fd(struct uart_dev * dev)50static inline int get_uart_dev_fd(struct uart_dev *dev) 51 { 52 return dev->tty_fd; 53 } 54 /** 55 * @brief Get the name of a UART device 56 */ get_uart_dev_path(struct uart_dev * dev)57static inline char *get_uart_dev_path(struct uart_dev *dev) 58 { 59 return dev->tty_path; 60 } 61 #endif 62 63