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 <xiangyang.wu@intel.com>
Reviewed-by: fei1.li@intel.com
This commit is contained in:
Xiangyang Wu 2021-10-19 10:08:25 +08:00 committed by wenlingz
parent 7bbd17ce80
commit 591998e956
2 changed files with 223 additions and 0 deletions

View File

@ -0,0 +1,161 @@
/*
* Copyright (C)2021 Intel Corporation
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <sys/queue.h>
#include <pthread.h>
#include <limits.h>
#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);
}
}

View File

@ -0,0 +1,62 @@
/*
* Copyright (C)2021 Intel Corporation
* SPDX-License-Identifier: BSD-3-Clause
*/
#ifndef _UART_H_
#define _UART_H_
#include <sys/types.h>
#include <stdint.h>
#include <sys/queue.h>
#include <pthread.h>
#include <semaphore.h>
#include <sys/un.h>
#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