From fa9c36057d082414262a185813565abc8874862e Mon Sep 17 00:00:00 2001 From: Youngjae Cho Date: Fri, 2 Jul 2021 15:19:20 +0900 Subject: [PATCH] uart: try to open uart based on uart.ini Now try once to find which devpath corresponds to a port number. It is specified in /hal/etc/peripheral-io/uart.ini. For example, if the uart.ini contains [UART] 0=/dev/ttyS0 then it returns "/dev/ttyS0" as devpath of the port number 0. If the above process fail, follow normal opening routine. Change-Id: Icec9d6601387f7f135b6e1a8692915553014ae8c Signed-off-by: Youngjae Cho --- src/peripheral_uart.c | 45 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 5 deletions(-) diff --git a/src/peripheral_uart.c b/src/peripheral_uart.c index 11a5e9a..ab1dcaa 100644 --- a/src/peripheral_uart.c +++ b/src/peripheral_uart.c @@ -41,6 +41,8 @@ FEATURE("http://tizen.org/feature/peripheral_io.uart") #define DEV_PATH_FMT_MAX_SIZE sizeof("/dev/ttyXXX" MAX_d_FMT) #define DEV_PATH_FMT "%s%d" +#define UART_INI_PATH "/hal/etc/peripheral-io/uart.ini" + /** * @brief Internal struct for uart context */ @@ -65,6 +67,35 @@ static inline void cleanup_handlep(peripheral_uart_h *handle) } } +static int peripheral_uart_find_devpath(int port, char *buffer, int len) +{ + FILE *fp; + char line[128]; + int key; + char value[22]; + + fp = fopen(UART_INI_PATH, "r"); + if (!fp) + return -1; + + while (fgets(line, sizeof(line), fp)) { + if (sscanf(line, "%d=%21s", &key, value) != 2) + continue; + + if (key != port) + continue; + + strncpy(buffer, value, len); + fclose(fp); + + return 0; + } + + fclose(fp); + + return -1; +} + /** * @brief Initializes uart communication and creates uart handle. */ @@ -88,13 +119,17 @@ int peripheral_uart_open_flags(int port, peripheral_open_flags_e flags, peripher "/dev/ttySAC", }; - size_t index; + size_t index = 0; + int retval; char path[DEV_PATH_FMT_MAX_SIZE] = {0, }; /* space for /dev/ttyXXX%d */ - for (index = 0; index < ARRAY_SIZE(sysfs_uart_path); index++) { - snprintf(path, sizeof path, DEV_PATH_FMT, sysfs_uart_path[index], port); - if (access(path, F_OK) == 0) - break; + retval = peripheral_uart_find_devpath(port, path, DEV_PATH_FMT_MAX_SIZE); + if (retval < 0 || access(path, F_OK) != 0) { + for (index = 0; index < ARRAY_SIZE(sysfs_uart_path); index++) { + snprintf(path, sizeof path, DEV_PATH_FMT, sysfs_uart_path[index], port); + if (access(path, F_OK) == 0) + break; + } } if (index == ARRAY_SIZE(sysfs_uart_path)) -- 2.34.1