uart: try to open uart based on uart.ini 53/260753/2 accepted/tizen/unified/20210705.125135 submit/tizen/20210705.023617
authorYoungjae Cho <y0.cho@samsung.com>
Fri, 2 Jul 2021 06:19:20 +0000 (15:19 +0900)
committerYoungjae Cho <y0.cho@samsung.com>
Fri, 2 Jul 2021 06:55:13 +0000 (15:55 +0900)
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 <y0.cho@samsung.com>
src/peripheral_uart.c

index 11a5e9a..ab1dcaa 100644 (file)
@@ -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))