From 591998e956f2f54f7e33c9e8c9a653a78b7afcb6 Mon Sep 17 00:00:00 2001 From: Xiangyang Wu Date: Tue, 19 Oct 2021 10:08:25 +0800 Subject: [PATCH] misc: life_mngr: add uart module In the uart module, the following functions are implemented: - init_uart_dev Allocate UART device instance and initialize UART device according to device name. - deinit_uart_dev Close UART devcie and free UART device instance. - send_message_by_uart Set handler to handle received message. - receive_message_by_uart Receive message and retry RETRY_RECV_TIMES time to avoid miss message in some cases. - get_uart_dev_fd Get the file descriptor of a UART device - get_uart_dev_path Get the name of a UART device v1-->v2: Update sync logic between uart in service VM and uart in user VM, lifecycle manager will not depend on VM boot order. Add code comments. v2-->v3: This module only includes UART device operations, move other logic into uart channel module. v3-->v4: Add parameters check for interface and refine some names. v4-->v5: Refine interface parameter type, error value. v5-->v6: Update condition check format of deinit_uart_dev. Tracked-On: #6652 Signed-off-by: Xiangyang Wu Reviewed-by: fei1.li@intel.com --- misc/services/life_mngr/uart.c | 161 +++++++++++++++++++++++++++++++++ misc/services/life_mngr/uart.h | 62 +++++++++++++ 2 files changed, 223 insertions(+) create mode 100644 misc/services/life_mngr/uart.c create mode 100644 misc/services/life_mngr/uart.h diff --git a/misc/services/life_mngr/uart.c b/misc/services/life_mngr/uart.c new file mode 100644 index 000000000..325acbc94 --- /dev/null +++ b/misc/services/life_mngr/uart.c @@ -0,0 +1,161 @@ +/* + * Copyright (C)2021 Intel Corporation + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "uart.h" +#include "uart_channel.h" +#include "log.h" +#include "config.h" + +/* it read from uart, and if end is '\0' or '\n' or len = buff-len it will return */ +static ssize_t try_receive_message_by_uart(int fd, void *buffer, size_t buf_len) +{ + ssize_t rc = 0U, count = 0U; + char *tmp; + + do { + rc = read(fd, buffer + count, buf_len - count); + if (rc > 0) { + count += rc; + tmp = (char *)buffer; + if ((tmp[count - 1] == '\0') || (tmp[count - 1] == '\n') + || (count == buf_len)) { + if (tmp[count - 1] == '\n') + tmp[count - 1] = '\0'; + break; + } + } + } while (rc > 0U); + + return count; +} +/** + * @brief Receive message and retry RETRY_RECV_TIMES time if + * message is missed in some cases. + */ +ssize_t receive_message_by_uart(struct uart_dev *dev, void *buf, size_t len) +{ + ssize_t count = 0; + unsigned int retry_times = RETRY_RECV_TIMES; + + if ((dev == NULL) || (buf == NULL) || (len == 0)) + return -EINVAL; + do { + usleep(WAIT_RECV); + count = try_receive_message_by_uart(dev->tty_fd, buf, len); + retry_times--; + } while ((count == 0) && (retry_times != 0U)); + return count; +} +ssize_t send_message_by_uart(struct uart_dev *dev, const void *buf, size_t len) +{ + ssize_t ret = 0; + + if ((dev == NULL) || (buf == NULL) || (len == 0)) + return -EINVAL; + ret = write(dev->tty_fd, buf, len); + + return ret; +} +static int set_tty_attr(int fd, int baudrate) +{ + struct termios tty; + + if (tcgetattr(fd, &tty) < 0) { + LOG_PRINTF("Error from tcgetattr: %s\n", strerror(errno)); + return errno; + } + + cfsetospeed(&tty, (speed_t)baudrate); + cfsetispeed(&tty, (speed_t)baudrate); + + /* set input-mode */ + tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | + ISTRIP | INLCR | IGNCR | ICRNL | IXON); + + /* set output-mode */ + tty.c_oflag &= ~OPOST; + + /* set control-mode */ + tty.c_cflag |= (CLOCAL | CREAD | CS8); + tty.c_cflag &= ~(CSIZE | PARENB | CSTOPB | CRTSCTS); + + /* set local-mode */ + tty.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN); + + /* block until one char read, set next char's timeout */ + tty.c_cc[VMIN] = 1; + tty.c_cc[VTIME] = 1; + + tcflush(fd, TCIOFLUSH); + + if (tcsetattr(fd, TCSANOW, &tty) != 0) { + LOG_PRINTF("Error from tcsetattr: %s\n", strerror(errno)); + return errno; + } + return 0; +} +static int tty_listen_setup(const char *tty_path) +{ + int fd = -1; + int ret; + + fd = open(tty_path, O_RDWR | O_NOCTTY | O_SYNC | O_NONBLOCK); + if (fd < 0) { + fprintf(stderr, "Can't open file %s\n", tty_path); + return errno; + } + ret = set_tty_attr(fd, B115200); + if (ret != 0) { + close(fd); + return ret; + } + LOG_PRINTF("Open tty device:%s, fd=%d\n", tty_path, fd); + return fd; +} + +struct uart_dev *init_uart_dev(char *path) +{ + struct uart_dev *dev; + + if (path == NULL) + return NULL; + LOG_PRINTF("UART device name:%s\n", path); + dev = calloc(1, sizeof(*dev)); + if (!dev) { + LOG_PRINTF("Failed to alloc mem for uart device %s\n", path); + return NULL; + } + if (strlen(path) < TTY_PATH_MAX) + memcpy(dev->tty_path, path, strlen(path)); + + dev->tty_fd = tty_listen_setup(dev->tty_path); + if (dev->tty_fd < 0) { + LOG_PRINTF("Failed to setup uart device %s\n", path); + free(dev); + return NULL; + } + return dev; +} +void deinit_uart_dev(struct uart_dev *dev) +{ + if (dev != NULL) { + LOG_PRINTF("Close device: %s\n", dev->tty_path); + close(dev->tty_fd); + dev->tty_fd = -1; + free(dev); + } +} + diff --git a/misc/services/life_mngr/uart.h b/misc/services/life_mngr/uart.h new file mode 100644 index 000000000..957ee3513 --- /dev/null +++ b/misc/services/life_mngr/uart.h @@ -0,0 +1,62 @@ +/* + * Copyright (C)2021 Intel Corporation + * SPDX-License-Identifier: BSD-3-Clause + */ +#ifndef _UART_H_ +#define _UART_H_ +#include +#include +#include +#include +#include +#include + +#define TTY_PATH_MAX 32U + +#define SCECOND_TO_US 1000000 +#define WAIT_RECV (SCECOND_TO_US>>2) +#define RETRY_RECV_TIMES 20U + +struct uart_dev { + char tty_path[TTY_PATH_MAX]; /**< UART device name */ + int tty_fd; /**< the FD of opened UART device */ +}; +/** + * @brief Allocate UART device instance and initialize UART + * device according to device name + * + * @param path UART device name + * @return struct uart_dev* Ponit to UART device instance + */ +struct uart_dev *init_uart_dev(char *path); +/** + * @brief Close UART devcie and free UART device instance + * + * @param dev Poin to UART device instance + */ +void deinit_uart_dev(struct uart_dev *dev); +/** + * @brief Set handler to handle received message + */ +ssize_t send_message_by_uart(struct uart_dev *dev, const void *buf, size_t len); +/** + * @brief Receive message and retry RETRY_RECV_TIMES time to + * avoid miss message in some cases. + */ +ssize_t receive_message_by_uart(struct uart_dev *dev, void *buf, size_t len); +/** + * @brief Get the file descriptor of a UART device + */ +static inline int get_uart_dev_fd(struct uart_dev *dev) +{ + return dev->tty_fd; +} +/** + * @brief Get the name of a UART device + */ +static inline char *get_uart_dev_path(struct uart_dev *dev) +{ + return dev->tty_path; +} +#endif +