From 8a2841d86ad1d9702547b625b7bb87fa04ea0b7d Mon Sep 17 00:00:00 2001 From: Karol Lewandowski Date: Tue, 1 Jun 2021 11:48:02 +0200 Subject: [PATCH] Add API function to open i2c device with flags Flags are going to be checked on the service side. Change-Id: Ie7ee17a06ad76a7366236122a1a42c29dbbc56bd --- include/gdbus/peripheral_gdbus_i2c.h | 1 + include/peripheral_io.h | 37 +++++++++++++++++++++++++++++++ src/gdbus/peripheral_gdbus_i2c.c | 42 ++++++++++++++++++++++++++++++++++++ src/gdbus/peripheral_io.xml | 8 +++++++ src/peripheral_i2c.c | 27 +++++++++++++++++++++++ 5 files changed, 115 insertions(+) diff --git a/include/gdbus/peripheral_gdbus_i2c.h b/include/gdbus/peripheral_gdbus_i2c.h index e1b1722..643d7c8 100644 --- a/include/gdbus/peripheral_gdbus_i2c.h +++ b/include/gdbus/peripheral_gdbus_i2c.h @@ -20,6 +20,7 @@ #include "peripheral_gdbus_common.h" int peripheral_gdbus_i2c_open(peripheral_i2c_h i2c, int bus, int address); +int peripheral_gdbus_i2c_open_flags(peripheral_i2c_h i2c, int bus, int address, int flags); int peripheral_gdbus_i2c_close(peripheral_i2c_h i2c); #endif /* __PERIPHERAL_GDBUS_I2C_H__ */ diff --git a/include/peripheral_io.h b/include/peripheral_io.h index 59187db..616d9b0 100644 --- a/include/peripheral_io.h +++ b/include/peripheral_io.h @@ -342,6 +342,43 @@ typedef struct _peripheral_i2c_s *peripheral_i2c_h; int peripheral_i2c_open(int bus, int address, peripheral_i2c_h *i2c); /** + * @brief Enumeration for open flags. + * @since_tizen 6.5 + */ +typedef enum { + PERIPHERAL_OPEN_FLAGS_PRIVATE = 0, /**< Exclusive access to device */ + PERIPHERAL_OPEN_FLAGS_SHARED = 1, /**< Shared access to device */ +} peripheral_open_flags_e; + +/** + * @platform + * @brief Opens an I2C slave device. + * @since_tizen 6.5 + * @privlevel platform + * @privilege http://tizen.org/privilege/peripheralio + * @remarks @a i2c should be released with peripheral_i2c_close() + * + * @param[in] bus The I2C bus number that the slave device is connected + * @param[in] address The address of the slave device + * @param[in] flags The flags to open call + * @param[out] i2c The I2C 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_i2c_close() + */ +int peripheral_i2c_open_flags(int bus, int address, peripheral_open_flags_e flags, peripheral_i2c_h *i2c); + +/** * @platform * @brief Closes an I2C slave device. * @since_tizen 4.0 diff --git a/src/gdbus/peripheral_gdbus_i2c.c b/src/gdbus/peripheral_gdbus_i2c.c index f410f3e..fe3a5f7 100644 --- a/src/gdbus/peripheral_gdbus_i2c.c +++ b/src/gdbus/peripheral_gdbus_i2c.c @@ -101,6 +101,48 @@ int peripheral_gdbus_i2c_open(peripheral_i2c_h i2c, int bus, int address) return ret; } +int peripheral_gdbus_i2c_open_flags(peripheral_i2c_h i2c, int bus, int address, int flags) +{ + int ret; + GError *error = NULL; + GUnixFDList *fd_list = NULL; + + ret = __i2c_proxy_init(); + if (ret != PERIPHERAL_ERROR_NONE) + return ret; + + if (peripheral_io_gdbus_i2c_call_open_flags_sync( + i2c_proxy, + bus, + address, + flags, + NULL, + &i2c->handle, + &ret, + &fd_list, + NULL, + &error) == FALSE) { + _E("Failed to request daemon to i2c open : %s", error->message); + g_error_free(error); + return PERIPHERAL_ERROR_IO_ERROR; + } + + // TODO : If ret is not PERIPHERAL_ERROR_NONE, fd list it NULL from daemon. + if (ret != PERIPHERAL_ERROR_NONE) + return ret; + + i2c->fd = g_unix_fd_list_get(fd_list, I2C_FD_INDEX, &error); + if (i2c->fd < 0) { + _E("Failed to get fd for i2c : %s", error->message); + g_error_free(error); + ret = PERIPHERAL_ERROR_IO_ERROR; + } + + g_object_unref(fd_list); + + return ret; +} + int peripheral_gdbus_i2c_close(peripheral_i2c_h i2c) { RETVM_IF(i2c_proxy == NULL, PERIPHERAL_ERROR_IO_ERROR, "I2c proxy is NULL"); diff --git a/src/gdbus/peripheral_io.xml b/src/gdbus/peripheral_io.xml index db72361..c868cdf 100644 --- a/src/gdbus/peripheral_io.xml +++ b/src/gdbus/peripheral_io.xml @@ -20,6 +20,14 @@ + + + + + + + + diff --git a/src/peripheral_i2c.c b/src/peripheral_i2c.c index 84bca0d..5f493f2 100644 --- a/src/peripheral_i2c.c +++ b/src/peripheral_i2c.c @@ -83,6 +83,33 @@ int peripheral_i2c_open(int bus, int address, peripheral_i2c_h *i2c) return ret; } +int peripheral_i2c_open_flags(int bus, int address, peripheral_open_flags_e flags, peripheral_i2c_h *i2c) +{ + peripheral_i2c_h handle; + int ret = PERIPHERAL_ERROR_NONE; + + RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "I2C feature is not supported"); + RETVM_IF(i2c == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid i2c handle"); + RETVM_IF(bus < 0 || address < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter"); + + handle = (peripheral_i2c_h)malloc(sizeof(struct _peripheral_i2c_s)); + if (handle == NULL) { + _E("Failed to allocate peripheral_i2c_h"); + return PERIPHERAL_ERROR_OUT_OF_MEMORY; + } + + ret = peripheral_gdbus_i2c_open_flags(handle, bus, address, flags); + if (ret != PERIPHERAL_ERROR_NONE) { + _E("Failed to open i2c communication, ret : %d", ret); + free(handle); + handle = NULL; + } + + *i2c = handle; + + return ret; +} + int peripheral_i2c_close(peripheral_i2c_h i2c) { int ret = PERIPHERAL_ERROR_NONE; -- 2.7.4