Add API function to open UART device with flags 38/260538/1 submit/tizen/20210629.011533
authorAdrian Szyndela <adrian.s@samsung.com>
Mon, 28 Jun 2021 11:43:00 +0000 (13:43 +0200)
committerAdrian Szyndela <adrian.s@samsung.com>
Mon, 28 Jun 2021 11:43:00 +0000 (13:43 +0200)
Extract translation from peripheral_open_flags to flock()'s lock type.

Change-Id: Ifcb933a70c30f077b55facd3f78ea5c3c47298e9

include/peripheral_io.h
src/common.h
src/peripheral_i2c.c
src/peripheral_uart.c

index 616d9b0..c438a23 100644 (file)
@@ -898,6 +898,33 @@ int peripheral_uart_open(int port, peripheral_uart_h *uart);
 
 /**
  * @platform
+ * @brief Opens the UART slave device.
+ * @since_tizen 6.5
+ * @privlevel platform
+ * @privilege http://tizen.org/privilege/peripheralio
+ * @remarks @a uart should be released with peripheral_uart_close()
+ *
+ * @param[in] port The UART port number that the slave device is connected
+ * @param[in] flags The flags to open call
+ * @param[out] uart The UART handle is created on success
+ *
+ * @return 0 on success, otherwise a negative error value
+ * @retval #PERIPHERAL_ERROR_NONE Successful
+ * @retval #PERIPHERAL_ERROR_IO_ERROR I/O operation failed
+ * @retval #PERIPHERAL_ERROR_NO_DEVICE Device does not exist or is removed
+ * @retval #PERIPHERAL_ERROR_OUT_OF_MEMORY Memory allocation failed
+ * @retval #PERIPHERAL_ERROR_PERMISSION_DENIED Permission denied
+ * @retval #PERIPHERAL_ERROR_RESOURCE_BUSY Device is in use
+ * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval #PERIPHERAL_ERROR_NOT_SUPPORTED Not supported
+ * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error
+ *
+ * @post peripheral_uart_close()
+ */
+int peripheral_uart_open_flags(int port, peripheral_open_flags_e flags, peripheral_uart_h *uart);
+
+/**
+ * @platform
  * @brief Closes the UART slave device.
  * @since_tizen 4.0
  * @privlevel platform
index 34bd3f2..17bacaa 100644 (file)
@@ -23,6 +23,7 @@
 #include <unistd.h>
 
 #include "log.h"
+#include "peripheral_io.h"
 
 #define MAX_ERR_LEN 255
 
@@ -100,3 +101,10 @@ typedef bool (*UdevCompareFunc)(struct udev_device *, void *);
 int peripheral_wait_for_udev(struct udev_monitor *monitor, UdevCompareFunc func, void *func_data);
 
 int peripheral_write_pin_to_file(const char *file_name, int pin);
+
+static inline int peripheral_get_lock_type(peripheral_open_flags_e flags)
+{
+       if (flags == PERIPHERAL_OPEN_FLAGS_SHARED)
+               return LOCK_SH;
+       return LOCK_EX;
+}
index 5ce29a6..ff7cefd 100644 (file)
@@ -81,8 +81,6 @@ static inline void cleanup_handlep(peripheral_i2c_h *handle) {
 int peripheral_i2c_open_flags(int bus, int address, peripheral_open_flags_e flags, peripheral_i2c_h *i2c)
 {
        int ret;
-       int lock_type = LOCK_EX;
-
        char path[DEV_PATH_FMT_MAX_SIZE] = {0, };       /* space for /dev/i2c-%d */
 
        RETVM_IF(!__is_feature_supported(), PERIPHERAL_ERROR_NOT_SUPPORTED, "I2C feature is not supported");
@@ -106,11 +104,9 @@ int peripheral_i2c_open_flags(int bus, int address, peripheral_open_flags_e flag
        ret = ioctl(handle->fd, I2C_SLAVE, address);
        CHECK_ERROR(ret);
 
-       if (flags == PERIPHERAL_OPEN_FLAGS_SHARED)
-               lock_type = LOCK_SH;
-
        ret = PERIPHERAL_ERROR_NONE;
-       TRY_FLOCK(ret, handle->fd, lock_type | LOCK_NB, "bus : %d, address : 0x%x", bus, address);
+       TRY_FLOCK(ret, handle->fd, peripheral_get_lock_type(flags) | LOCK_NB,
+                       "bus : %d, address : 0x%x", bus, address);
        CHECK_ERROR(ret != PERIPHERAL_ERROR_NONE);
 
        *i2c = handle;
index a354d2e..2357763 100644 (file)
@@ -68,11 +68,13 @@ static inline void cleanup_handlep(peripheral_uart_h *handle)
 /**
  * @brief Initializes uart communication and creates uart handle.
  */
-int peripheral_uart_open(int port, peripheral_uart_h *uart)
+int peripheral_uart_open_flags(int port, peripheral_open_flags_e flags, peripheral_uart_h *uart)
 {
        RETVM_IF(!__is_feature_supported(), PERIPHERAL_ERROR_NOT_SUPPORTED, "UART feature is not supported");
        RETVM_IF(uart == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid uart handle");
        RETVM_IF(port < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid port number");
+       RETVM_IF(flags != PERIPHERAL_OPEN_FLAGS_PRIVATE && flags != PERIPHERAL_OPEN_FLAGS_SHARED,
+                       PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid flags");
 
        __attribute__ ((cleanup(cleanup_handlep))) peripheral_uart_h handle = NULL;
        handle = (peripheral_uart_h)calloc(1, sizeof(struct _peripheral_uart_s));
@@ -103,7 +105,8 @@ int peripheral_uart_open(int port, peripheral_uart_h *uart)
        CHECK_ERROR(handle->fd < 0);
 
        int ret = PERIPHERAL_ERROR_NONE;
-       TRY_FLOCK(ret, handle->fd, LOCK_EX | LOCK_NB, "uart port: %d, path: %s", port, path);
+       TRY_FLOCK(ret, handle->fd, peripheral_get_lock_type(flags) | LOCK_NB,
+                       "uart port: %d, path: %s", port, path);
        CHECK_ERROR(ret != PERIPHERAL_ERROR_NONE);
 
        *uart = handle;
@@ -112,6 +115,14 @@ int peripheral_uart_open(int port, peripheral_uart_h *uart)
        return PERIPHERAL_ERROR_NONE;
 }
 
+/**
+ * @brief Initializes uart communication and creates uart handle.
+ */
+int peripheral_uart_open(int port, peripheral_uart_h *uart)
+{
+       return peripheral_uart_open_flags(port, PERIPHERAL_OPEN_FLAGS_PRIVATE, uart);
+}
+
 static void peripheral_uart_flush(peripheral_uart_h uart)
 {
        tcflush(uart->fd, TCIOFLUSH);