Add API function to open i2c device with flags 34/259134/3 accepted/tizen/unified/20210607.124325 submit/tizen/20210607.045229
authorKarol Lewandowski <k.lewandowsk@samsung.com>
Tue, 1 Jun 2021 09:48:02 +0000 (11:48 +0200)
committerKarol Lewandowski <k.lewandowsk@samsung.com>
Wed, 2 Jun 2021 22:03:58 +0000 (00:03 +0200)
Flags are going to be checked on the service side.

Change-Id: Ie7ee17a06ad76a7366236122a1a42c29dbbc56bd

include/gdbus/peripheral_gdbus_i2c.h
include/peripheral_io.h
src/gdbus/peripheral_gdbus_i2c.c
src/gdbus/peripheral_io.xml
src/peripheral_i2c.c

index e1b1722..643d7c8 100644 (file)
@@ -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__ */
index 59187db..616d9b0 100644 (file)
@@ -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
index f410f3e..fe3a5f7 100644 (file)
@@ -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");
index db72361..c868cdf 100644 (file)
                        <arg type="u" name="handle" direction="out"/>
                        <arg type="i" name="result" direction="out"/>
                </method>
+               <method name="OpenFlags">
+                       <annotation name="org.gtk.GDBus.C.UnixFD" value="true"/>
+                       <arg type="i" name="bus" direction="in"/>
+                       <arg type="i" name="address" direction="in"/>
+                       <arg type="i" name="flags" direction="in"/>
+                       <arg type="u" name="handle" direction="out"/>
+                       <arg type="i" name="result" direction="out"/>
+               </method>
                <method name="Close">
                        <arg type="u" name="handle" direction="in"/>
                        <arg type="i" name="result" direction="out"/>
index 84bca0d..5f493f2 100644 (file)
@@ -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;