handle: do not management fd in handle 41/163241/3
authorSegwon <segwon.han@samsung.com>
Fri, 8 Dec 2017 05:59:35 +0000 (14:59 +0900)
committerSegwon <segwon.han@samsung.com>
Fri, 8 Dec 2017 08:42:19 +0000 (17:42 +0900)
 - create a fd list in the interface files
 - Do not need to have fd until Lib request to close

Change-Id: I758edbf9f0d5007bb74926a37c5d8ba3e858936d
Signed-off-by: Segwon <segwon.han@samsung.com>
21 files changed:
include/handle/peripheral_handle.h
include/interface/peripheral_interface_gpio.h
include/interface/peripheral_interface_i2c.h
include/interface/peripheral_interface_pwm.h
include/interface/peripheral_interface_spi.h
include/interface/peripheral_interface_uart.h
src/gdbus/peripheral_gdbus_gpio.c
src/gdbus/peripheral_gdbus_i2c.c
src/gdbus/peripheral_gdbus_pwm.c
src/gdbus/peripheral_gdbus_spi.c
src/gdbus/peripheral_gdbus_uart.c
src/handle/peripheral_handle_gpio.c
src/handle/peripheral_handle_i2c.c
src/handle/peripheral_handle_pwm.c
src/handle/peripheral_handle_spi.c
src/handle/peripheral_handle_uart.c
src/interface/peripheral_interface_gpio.c
src/interface/peripheral_interface_i2c.c
src/interface/peripheral_interface_pwm.c
src/interface/peripheral_interface_spi.c
src/interface/peripheral_interface_uart.c

index 871f31170ee8afe0d6b623935ec6bd39e17c27a4..60e316c7e401c704b8b95daadf91a544ded710bf 100644 (file)
@@ -41,35 +41,25 @@ typedef struct {
 
 typedef struct {
        int pin;
-       int fd_direction;
-       int fd_edge;
-       int fd_value;
 } peripheral_handle_gpio_s;
 
 typedef struct {
        int bus;
        int address;
-       int fd;
 } peripheral_handle_i2c_s;
 
 typedef struct {
        int chip;
        int pin;
-       int fd_period;
-       int fd_duty_cycle;
-       int fd_polarity;
-       int fd_enable;
 } peripheral_handle_pwm_s;
 
 typedef struct {
        int port;
-       int fd;
 } peripheral_handle_uart_s;
 
 typedef struct {
        int bus;
        int cs;
-       int fd;
 } peripheral_handle_spi_s;
 
 typedef struct {
index 10b5f54035a51fd4b0d85348593527697d140c5d..179615b7c51fa0de5648be85b4af7720e0d4d2db 100644 (file)
 #ifndef __PERIPHERAL_INTERFACE_GPIO_H__
 #define __PERIPHERAL_INTERFACE_GPIO_H__
 
+#include <gio/gunixfdlist.h>
+
 int peripheral_interface_gpio_export(int pin);
 int peripheral_interface_gpio_unexport(int pin);
 
-int peripheral_interface_gpio_fd_direction_open(int pin, int *fd_out);
-int peripheral_interface_gpio_fd_direction_close(int fd);
-
-int peripheral_interface_gpio_fd_edge_open(int pin, int *fd_out);
-int peripheral_interface_gpio_fd_edge_close(int fd);
-
-int peripheral_interface_gpio_fd_value_open(int pin, int *fd_out);
-int peripheral_interface_gpio_fd_value_close(int fd);
+int peripheral_interface_gpio_fd_list_create(int pin, GUnixFDList **list_out);
+void peripheral_interface_gpio_fd_list_destroy(GUnixFDList *list);
 
 #endif /*__PERIPHERAL_INTERFACE_GPIO_H__*/
index e0e5ecb0406c136fbe771d18eff119b1b44ea771..cf9d0f06e0a62e43d185999789b39ea154d4ee42 100644 (file)
@@ -17,7 +17,9 @@
 #ifndef __PERIPHERAL_INTERFACE_I2C_H__
 #define __PERIPHERAL_INTERFACE_I2C_H__
 
-int peripheral_interface_i2c_fd_open(int bus, int address, int *fd_out);
-int peripheral_interface_i2c_fd_close(int fd);
+#include <gio/gunixfdlist.h>
+
+int peripheral_interface_i2c_fd_list_create(int bus, int address, GUnixFDList **list_out);
+void peripheral_interface_i2c_fd_list_destroy(GUnixFDList *list);
 
 #endif /* __PERIPHERAL_INTERFACE_I2C_H__ */
index 1a257ca30422a1183a3226874f0dfc10b6b1d8d3..49bc84eff6bee887f2205bc41db7e5293432774b 100644 (file)
 #ifndef __PERIPHERAL_INTERFACE_PWM_H__
 #define __PERIPHERAL_INTERFACE_PWM_H__
 
+#include <gio/gunixfdlist.h>
+
 int peripheral_interface_pwm_export(int chip, int pin);
 int peripheral_interface_pwm_unexport(int chip, int pin);
 
-int peripheral_interface_pwm_fd_period_open(int chip, int pin, int *fd_out);
-int peripheral_interface_pwm_fd_period_close(int fd);
-
-int peripheral_interface_pwm_fd_duty_cycle_open(int chip, int pin, int *fd_out);
-int peripheral_interface_pwm_fd_duty_cycle_close(int fd);
-
-int peripheral_interface_pwm_fd_polarity_open(int chip, int pin, int *fd_out);
-int peripheral_interface_pwm_fd_polarity_close(int fd);
-
-int peripheral_interface_pwm_fd_enable_open(int chip, int pin, int *fd_out);
-int peripheral_interface_pwm_fd_enable_close(int fd);
+int peripheral_interface_pwm_fd_list_create(int chip, int pin, GUnixFDList **list_out);
+void peripheral_interface_pwm_fd_list_destroy(GUnixFDList *list);
 
 #endif /* __PERIPHERAL_INTERFACE_PWM_H__ */
index 581d68393b2b1aca7e3687b2a2423500356dcb3e..25b5a6ffc50e1c297d79d86c0a505198183101bf 100644 (file)
@@ -17,7 +17,9 @@
 #ifndef __PERIPHERAL_INTERFACE_SPI_H__
 #define __PERIPHERAL_INTERFACE_SPI_H__
 
-int peripheral_interface_spi_fd_open(int bus, int cs, int *fd_out);
-int peripheral_interface_spi_fd_close(int fd);
+#include <gio/gunixfdlist.h>
+
+int peripheral_interface_spi_fd_list_create(int bus, int cs, GUnixFDList **list_out);
+void peripheral_interface_spi_fd_list_destroy(GUnixFDList *list);
 
 #endif /* __PERIPHERAL_INTERFACE_SPI_H__ */
index aa5d5c4fdc117b51d1b09791c050df930bbfb535..c2dbb02c90ebac5c5a2d0419df954e0a5ff3b99b 100644 (file)
 #ifndef __PERIPHERAL_INTERFACE_UART_H__
 #define __PERIPHERAL_INTERFACE_UART_H__
 
-int peripheral_interface_uart_fd_open(int port, int *fd_out);
-int peripheral_interface_uart_fd_close(int fd);
+#include <gio/gunixfdlist.h>
+
+int peripheral_interface_uart_fd_list_create(int port, GUnixFDList **list_out);
+void peripheral_interface_uart_fd_list_destroy(GUnixFDList *list);
 
 #endif /* __PERIPHERAL_INTERFACE_UART_H__ */
 
index 32ae9883abd4f1c39460a3b9e6548380e025574c..20ca715fb624bfecb08ac5ec50f40bf3a64b5f06 100644 (file)
 #include "peripheral_io_gdbus.h"
 #include "peripheral_handle.h"
 #include "peripheral_handle_gpio.h"
+#include "peripheral_interface_gpio.h"
 #include "peripheral_gdbus_gpio.h"
 
 static void __gpio_on_name_vanished(GDBusConnection *connection,
-               const gchar     *name,
-               gpointer         user_data)
+               const gchar *name,
+               gpointer user_data)
 {
+       int ret = PERIPHERAL_ERROR_NONE;
+
        peripheral_h gpio_handle = (peripheral_h)user_data;
        _D("appid [%s] vanished ", name);
 
        g_bus_unwatch_name(gpio_handle->watch_id);
-       peripheral_handle_gpio_destroy(gpio_handle);
+
+       ret = peripheral_interface_gpio_unexport(gpio_handle->type.gpio.pin);
+       if (ret != PERIPHERAL_ERROR_NONE)
+               _E("Failed to unexport gpio");
+
+       ret = peripheral_handle_gpio_destroy(gpio_handle);
+       if (ret != PERIPHERAL_ERROR_NONE)
+               _E("Failed to destroy gpio handle");
 }
 
 gboolean peripheral_gdbus_gpio_open(
@@ -55,24 +65,26 @@ gboolean peripheral_gdbus_gpio_open(
                goto out;
        }
 
-       gpio_fd_list = g_unix_fd_list_new();
-       if (gpio_fd_list == NULL) {
+       ret = peripheral_interface_gpio_export(pin);
+       if (ret != PERIPHERAL_ERROR_NONE) {
+               _E("Failed to export gpio");
+               goto out;
+       }
+
+       ret = peripheral_interface_gpio_fd_list_create(pin, &gpio_fd_list);
+       if (ret != PERIPHERAL_ERROR_NONE) {
                _E("Failed to create gpio fd list");
-               ret = PERIPHERAL_ERROR_OUT_OF_MEMORY;
+               peripheral_interface_gpio_unexport(pin);
                goto out;
        }
 
        ret = peripheral_handle_gpio_create(pin, &gpio_handle, user_data);
        if (ret != PERIPHERAL_ERROR_NONE) {
-               _E("Failed to create peripheral gpio handle");
+               _E("Failed to create gpio handle");
+               peripheral_interface_gpio_unexport(pin);
                goto out;
        }
 
-       /* Do not change the order of the fd list */
-       g_unix_fd_list_append(gpio_fd_list, gpio_handle->type.gpio.fd_direction, NULL);
-       g_unix_fd_list_append(gpio_fd_list, gpio_handle->type.gpio.fd_edge, NULL);
-       g_unix_fd_list_append(gpio_fd_list, gpio_handle->type.gpio.fd_value, NULL);
-
        gpio_handle->watch_id = g_bus_watch_name(G_BUS_TYPE_SYSTEM,
                        g_dbus_method_invocation_get_sender(invocation),
                        G_BUS_NAME_WATCHER_FLAGS_NONE,
@@ -83,9 +95,7 @@ gboolean peripheral_gdbus_gpio_open(
 
 out:
        peripheral_io_gdbus_gpio_complete_open(gpio, invocation, gpio_fd_list, GPOINTER_TO_UINT(gpio_handle), ret);
-
-       if (gpio_fd_list != NULL)
-               g_object_unref(gpio_fd_list);
+       peripheral_interface_gpio_fd_list_destroy(gpio_fd_list);
 
        return true;
 }
@@ -101,7 +111,14 @@ gboolean peripheral_gdbus_gpio_close(
        peripheral_h gpio_handle = GUINT_TO_POINTER(handle);
 
        g_bus_unwatch_name(gpio_handle->watch_id);
+
+       ret = peripheral_interface_gpio_unexport(gpio_handle->type.gpio.pin);
+       if (ret != PERIPHERAL_ERROR_NONE)
+               _E("Failed to gpio unexport");
+
        ret = peripheral_handle_gpio_destroy(gpio_handle);
+       if (ret != PERIPHERAL_ERROR_NONE)
+               _E("Failed to destroy gpio handle");
 
        peripheral_io_gdbus_gpio_complete_close(gpio, invocation, ret);
 
index 4522cb63734c49e68e1d4a73e48127785463f20f..dab1d63ffedfebf81ae66b5670e4c8441601fd50 100644 (file)
 #include "peripheral_io_gdbus.h"
 #include "peripheral_handle.h"
 #include "peripheral_handle_i2c.h"
+#include "peripheral_interface_i2c.h"
 #include "peripheral_gdbus_i2c.h"
 
 static void __i2c_on_name_vanished(GDBusConnection *connection,
-               const gchar     *name,
-               gpointer         user_data)
+               const gchar *name,
+               gpointer user_data)
 {
+       int ret = PERIPHERAL_ERROR_NONE;
+
        peripheral_h i2c_handle = (peripheral_h)user_data;
        _D("appid [%s] vanished ", name);
 
        g_bus_unwatch_name(i2c_handle->watch_id);
-       peripheral_handle_i2c_destroy(i2c_handle);
+
+       ret = peripheral_handle_i2c_destroy(i2c_handle);
+       if (ret != PERIPHERAL_ERROR_NONE)
+               _E("Failed to destroy i2c handle");
 }
 
 gboolean peripheral_gdbus_i2c_open(
@@ -56,22 +62,18 @@ gboolean peripheral_gdbus_i2c_open(
                goto out;
        }
 
-       i2c_fd_list = g_unix_fd_list_new();
-       if (i2c_fd_list == NULL) {
+       ret = peripheral_interface_i2c_fd_list_create(bus, address, &i2c_fd_list);
+       if (ret != PERIPHERAL_ERROR_NONE) {
                _E("Failed to create i2c fd list");
-               ret = PERIPHERAL_ERROR_OUT_OF_MEMORY;
                goto out;
        }
 
        ret = peripheral_handle_i2c_create(bus, address, &i2c_handle, user_data);
        if (ret != PERIPHERAL_ERROR_NONE) {
-               _E("Failed to create periphreal i2c handle");
+               _E("Failed to create i2c handle");
                goto out;
        }
 
-       /* Do not change the order of the fd list */
-       g_unix_fd_list_append(i2c_fd_list, i2c_handle->type.i2c.fd, NULL);
-
        i2c_handle->watch_id = g_bus_watch_name(G_BUS_TYPE_SYSTEM,
                        g_dbus_method_invocation_get_sender(invocation),
                        G_BUS_NAME_WATCHER_FLAGS_NONE,
@@ -82,9 +84,7 @@ gboolean peripheral_gdbus_i2c_open(
 
 out:
        peripheral_io_gdbus_i2c_complete_open(i2c, invocation, i2c_fd_list, GPOINTER_TO_UINT(i2c_handle), ret);
-
-       if (i2c_fd_list != NULL)
-               g_object_unref(i2c_fd_list);
+       peripheral_interface_i2c_fd_list_destroy(i2c_fd_list);
 
        return true;
 }
@@ -100,7 +100,10 @@ gboolean peripheral_gdbus_i2c_close(
        peripheral_h i2c_handle = GUINT_TO_POINTER(handle);
 
        g_bus_unwatch_name(i2c_handle->watch_id);
+
        ret = peripheral_handle_i2c_destroy(i2c_handle);
+       if (ret != PERIPHERAL_ERROR_NONE)
+               _E("Failed to destroy i2c handle");
 
        peripheral_io_gdbus_i2c_complete_close(i2c, invocation, ret);
 
index 379dd1f35e5c74bd08710d2fbb87d6293798d361..c2994a2130a2222b1b702d3d6ae3c069a5fc3973 100644 (file)
 #include "peripheral_io_gdbus.h"
 #include "peripheral_handle.h"
 #include "peripheral_handle_pwm.h"
+#include "peripheral_interface_pwm.h"
 #include "peripheral_gdbus_pwm.h"
 
 static void __pwm_on_name_vanished(GDBusConnection *connection,
-               const gchar     *name,
-               gpointer         user_data)
+               const gchar *name,
+               gpointer user_data)
 {
+       int ret = PERIPHERAL_ERROR_NONE;
+
        peripheral_h pwm_handle = (peripheral_h)user_data;
        _D("appid [%s] vanished ", name);
 
        g_bus_unwatch_name(pwm_handle->watch_id);
-       peripheral_handle_pwm_destroy(pwm_handle);
+
+       ret = peripheral_interface_pwm_unexport(pwm_handle->type.pwm.chip, pwm_handle->type.pwm.pin);
+       if (ret != PERIPHERAL_ERROR_NONE)
+               _E("Failed to unexport pwm");
+
+       ret = peripheral_handle_pwm_destroy(pwm_handle);
+       if (ret != PERIPHERAL_ERROR_NONE)
+               _E("Failed to destroy pwm handle");
 }
 
 gboolean peripheral_gdbus_pwm_open(
@@ -56,25 +66,26 @@ gboolean peripheral_gdbus_pwm_open(
                goto out;
        }
 
-       pwm_fd_list = g_unix_fd_list_new();
-       if (pwm_fd_list == NULL) {
+       ret = peripheral_interface_pwm_export(chip, pin);
+       if (ret != PERIPHERAL_ERROR_NONE) {
+               _E("Failed to export pwm");
+               goto out;
+       }
+
+       ret = peripheral_interface_pwm_fd_list_create(chip, pin, &pwm_fd_list);
+       if (ret != PERIPHERAL_ERROR_NONE) {
                _E("Failed to create pwm fd list");
-               ret = PERIPHERAL_ERROR_OUT_OF_MEMORY;
+               peripheral_interface_pwm_unexport(chip, pin);
                goto out;
        }
 
        ret = peripheral_handle_pwm_create(chip, pin, &pwm_handle, user_data);
        if (ret != PERIPHERAL_ERROR_NONE) {
-               _E("Failed to create peripheral pwm handle");
+               _E("Failed to create pwm handle");
+               peripheral_interface_pwm_unexport(chip, pin);
                goto out;
        }
 
-       /* Do not change the order of the fd list */
-       g_unix_fd_list_append(pwm_fd_list, pwm_handle->type.pwm.fd_period, NULL);
-       g_unix_fd_list_append(pwm_fd_list, pwm_handle->type.pwm.fd_duty_cycle, NULL);
-       g_unix_fd_list_append(pwm_fd_list, pwm_handle->type.pwm.fd_polarity, NULL);
-       g_unix_fd_list_append(pwm_fd_list, pwm_handle->type.pwm.fd_enable, NULL);
-
        pwm_handle->watch_id = g_bus_watch_name(G_BUS_TYPE_SYSTEM,
                        g_dbus_method_invocation_get_sender(invocation),
                        G_BUS_NAME_WATCHER_FLAGS_NONE,
@@ -85,9 +96,7 @@ gboolean peripheral_gdbus_pwm_open(
 
 out:
        peripheral_io_gdbus_pwm_complete_open(pwm, invocation, pwm_fd_list, GPOINTER_TO_UINT(pwm_handle), ret);
-
-       if (pwm_fd_list != NULL)
-               g_object_unref(pwm_fd_list);
+       peripheral_interface_pwm_fd_list_destroy(pwm_fd_list);
 
        return true;
 }
@@ -103,7 +112,14 @@ gboolean peripheral_gdbus_pwm_close(
        peripheral_h pwm_handle = GUINT_TO_POINTER(handle);
 
        g_bus_unwatch_name(pwm_handle->watch_id);
+
+       ret = peripheral_interface_pwm_unexport(pwm_handle->type.pwm.chip, pwm_handle->type.pwm.pin);
+       if (ret != PERIPHERAL_ERROR_NONE)
+               _E("Failed to unexport pwm");
+
        ret = peripheral_handle_pwm_destroy(pwm_handle);
+       if (ret != PERIPHERAL_ERROR_NONE)
+               _E("Failed to destroy pwm handle");
 
        peripheral_io_gdbus_pwm_complete_close(pwm, invocation, ret);
 
index c395ad669323da8b9dcca51f17327bc5a2af5833..96e0b9bbfcd4ba0ccfe803957f1d0702ebcea3cd 100644 (file)
 #include "peripheral_io_gdbus.h"
 #include "peripheral_handle.h"
 #include "peripheral_handle_spi.h"
+#include "peripheral_interface_spi.h"
 #include "peripheral_gdbus_spi.h"
 
 static void __spi_on_name_vanished(GDBusConnection *connection,
-               const gchar     *name,
-               gpointer         user_data)
+               const gchar *name,
+               gpointer user_data)
 {
+       int ret = PERIPHERAL_ERROR_NONE;
+
        peripheral_h spi_handle = (peripheral_h)user_data;
        _D("appid [%s] vanished ", name);
 
        g_bus_unwatch_name(spi_handle->watch_id);
-       peripheral_handle_spi_destroy(spi_handle);
+
+       ret = peripheral_handle_spi_destroy(spi_handle);
+       if (ret != PERIPHERAL_ERROR_NONE)
+               _E("Failed to destroy spi handle");
 }
 
 gboolean peripheral_gdbus_spi_open(
@@ -56,10 +62,9 @@ gboolean peripheral_gdbus_spi_open(
                goto out;
        }
 
-       spi_fd_list = g_unix_fd_list_new();
-       if (spi_fd_list == NULL) {
+       ret = peripheral_interface_spi_fd_list_create(bus, cs, &spi_fd_list);
+       if (ret != PERIPHERAL_ERROR_NONE) {
                _E("Failed to create spi fd list");
-               ret = PERIPHERAL_ERROR_OUT_OF_MEMORY;
                goto out;
        }
 
@@ -69,9 +74,6 @@ gboolean peripheral_gdbus_spi_open(
                goto out;
        }
 
-       /* Do not change the order of the fd list */
-       g_unix_fd_list_append(spi_fd_list, spi_handle->type.spi.fd, NULL);
-
        spi_handle->watch_id = g_bus_watch_name(G_BUS_TYPE_SYSTEM,
                        g_dbus_method_invocation_get_sender(invocation),
                        G_BUS_NAME_WATCHER_FLAGS_NONE,
@@ -82,9 +84,7 @@ gboolean peripheral_gdbus_spi_open(
 
 out:
        peripheral_io_gdbus_spi_complete_open(spi, invocation, spi_fd_list, GPOINTER_TO_UINT(spi_handle), ret);
-
-       if (spi_fd_list != NULL)
-               g_object_unref(spi_fd_list);
+       peripheral_interface_spi_fd_list_destroy(spi_fd_list);
 
        return true;
 }
@@ -100,7 +100,10 @@ gboolean peripheral_gdbus_spi_close(
        peripheral_h spi_handle = GUINT_TO_POINTER(handle);
 
        g_bus_unwatch_name(spi_handle->watch_id);
+
        ret = peripheral_handle_spi_destroy(spi_handle);
+       if (ret != PERIPHERAL_ERROR_NONE)
+               _E("Failed to destroy spi handle");
 
        peripheral_io_gdbus_spi_complete_close(spi, invocation, ret);
 
index 35c1facdfb8a0d56affb1e03c14e0d591d7cf22b..fd6900660740096441a3f4e57ef6c209387b844d 100644 (file)
 #include "peripheral_io_gdbus.h"
 #include "peripheral_handle.h"
 #include "peripheral_handle_uart.h"
+#include "peripheral_interface_uart.h"
 #include "peripheral_gdbus_uart.h"
 
 static void __uart_on_name_vanished(GDBusConnection *connection,
-               const gchar     *name,
-               gpointer         user_data)
+               const gchar *name,
+               gpointer user_data)
 {
+       int ret = PERIPHERAL_ERROR_NONE;
+
        peripheral_h uart_handle = (peripheral_h)user_data;
        _D("appid [%s] vanished ", name);
 
        g_bus_unwatch_name(uart_handle->watch_id);
-       peripheral_handle_uart_destroy(uart_handle);
+
+       ret = peripheral_handle_uart_destroy(uart_handle);
+       if (ret != PERIPHERAL_ERROR_NONE)
+               _E("Failed to destroy uart handle");
 }
 
 gboolean peripheral_gdbus_uart_open(
@@ -55,10 +61,9 @@ gboolean peripheral_gdbus_uart_open(
                goto out;
        }
 
-       uart_fd_list = g_unix_fd_list_new();
-       if (uart_fd_list == NULL) {
+       ret = peripheral_interface_uart_fd_list_create(port, &uart_fd_list);
+       if (ret != PERIPHERAL_ERROR_NONE) {
                _E("Failed to create uart fd list");
-               ret = PERIPHERAL_ERROR_OUT_OF_MEMORY;
                goto out;
        }
 
@@ -68,9 +73,6 @@ gboolean peripheral_gdbus_uart_open(
                goto out;
        }
 
-       /* Do not change the order of the fd list */
-       g_unix_fd_list_append(uart_fd_list, uart_handle->type.uart.fd, NULL);
-
        uart_handle->watch_id = g_bus_watch_name(G_BUS_TYPE_SYSTEM,
                        g_dbus_method_invocation_get_sender(invocation),
                        G_BUS_NAME_WATCHER_FLAGS_NONE,
@@ -81,9 +83,7 @@ gboolean peripheral_gdbus_uart_open(
 
 out:
        peripheral_io_gdbus_uart_complete_open(uart, invocation, uart_fd_list, GPOINTER_TO_UINT(uart_handle), ret);
-
-       if (uart_fd_list != NULL)
-               g_object_unref(uart_fd_list);
+       peripheral_interface_uart_fd_list_destroy(uart_fd_list);
 
        return true;
 }
@@ -99,7 +99,10 @@ gboolean peripheral_gdbus_uart_close(
        peripheral_h uart_handle = GUINT_TO_POINTER(handle);
 
        g_bus_unwatch_name(uart_handle->watch_id);
+
        ret = peripheral_handle_uart_destroy(uart_handle);
+       if (ret != PERIPHERAL_ERROR_NONE)
+               _E("Failed to destroy uart handle");
 
        peripheral_io_gdbus_uart_complete_close(uart, invocation, ret);
 
index e075f9e28bc7abec5a51533614fa99b5d32e2a4f..7f793f53f767e53f75d9ad49278ba0ea9b342a54 100644 (file)
@@ -14,7 +14,6 @@
  * limitations under the License.
  */
 
-#include "peripheral_interface_gpio.h"
 #include "peripheral_handle_common.h"
 
 static bool __peripheral_handle_gpio_is_creatable(int pin, peripheral_info_s *info)
@@ -51,29 +50,11 @@ int peripheral_handle_gpio_destroy(peripheral_h handle)
 
        int ret = PERIPHERAL_ERROR_NONE;
 
-       ret = peripheral_interface_gpio_fd_direction_close(handle->type.gpio.fd_direction);
-       if (ret != PERIPHERAL_ERROR_NONE)
-               _E("Failed to gpio close direction fd");
-
-       ret = peripheral_interface_gpio_fd_edge_close(handle->type.gpio.fd_edge);
-       if (ret != PERIPHERAL_ERROR_NONE)
-               _E("Failed to gpio close edge fd");
-
-       peripheral_interface_gpio_fd_value_close(handle->type.gpio.fd_value);
-       if (ret != PERIPHERAL_ERROR_NONE)
-               _E("Failed to gpio close value fd");
-
-       ret = peripheral_interface_gpio_unexport(handle->type.gpio.pin);
-       if (ret != PERIPHERAL_ERROR_NONE)
-               _E("Failed to gpio unexport");
-
        ret = peripheral_handle_free(handle);
-       if (ret != PERIPHERAL_ERROR_NONE) {
+       if (ret != PERIPHERAL_ERROR_NONE)
                _E("Failed to free gpio handle");
-               return PERIPHERAL_ERROR_UNKNOWN;
-       }
 
-       return PERIPHERAL_ERROR_NONE;
+       return ret;
 }
 
 int peripheral_handle_gpio_create(int pin, peripheral_h *handle, gpointer user_data)
@@ -81,8 +62,6 @@ int peripheral_handle_gpio_create(int pin, peripheral_h *handle, gpointer user_d
        RETVM_IF(pin < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid gpio pin");
        RETVM_IF(handle == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid gpio handle");
 
-       int ret = PERIPHERAL_ERROR_NONE;
-
        peripheral_info_s *info = (peripheral_info_s*)user_data;
 
        peripheral_h gpio_handle = NULL;
@@ -102,38 +81,8 @@ int peripheral_handle_gpio_create(int pin, peripheral_h *handle, gpointer user_d
 
        gpio_handle->list = &info->gpio_list;
        gpio_handle->type.gpio.pin = pin;
-       gpio_handle->type.gpio.fd_direction = -1;
-       gpio_handle->type.gpio.fd_edge = -1;
-       gpio_handle->type.gpio.fd_value = -1;
-
-       ret = peripheral_interface_gpio_export(pin);
-       if (ret != PERIPHERAL_ERROR_NONE) {
-               _E("Failed to gpio export");
-               goto out;
-       }
-
-       ret = peripheral_interface_gpio_fd_direction_open(pin, &gpio_handle->type.gpio.fd_direction);
-       if (ret != PERIPHERAL_ERROR_NONE) {
-               _E("Failed to gpio open direction fd");
-               goto out;
-       }
-
-       ret = peripheral_interface_gpio_fd_edge_open(pin, &gpio_handle->type.gpio.fd_edge);
-       if (ret != PERIPHERAL_ERROR_NONE) {
-               _E("Failed to gpio open edge fd");
-               goto out;
-       }
-
-       ret = peripheral_interface_gpio_fd_value_open(pin, &gpio_handle->type.gpio.fd_value);
-       if (ret != PERIPHERAL_ERROR_NONE) {
-               _E("Failed to gpio open value fd");
-               goto out;
-       }
 
        *handle = gpio_handle;
-       return PERIPHERAL_ERROR_NONE;
 
-out:
-       peripheral_handle_gpio_destroy(gpio_handle);
-       return ret;
+       return PERIPHERAL_ERROR_NONE;
 }
index 075cfe4e70ab282a344ea408b4cd79c27028c2d8..3294a8b47335baa8c58fcf93c1672915ed9ebcdc 100644 (file)
@@ -14,7 +14,6 @@
  * limitations under the License.
  */
 
-#include "peripheral_interface_i2c.h"
 #include "peripheral_handle_common.h"
 
 static bool __peripheral_handle_i2c_is_creatable(int bus, int address, peripheral_info_s *info)
@@ -51,17 +50,11 @@ int peripheral_handle_i2c_destroy(peripheral_h handle)
 
        int ret = PERIPHERAL_ERROR_NONE;
 
-       ret = peripheral_interface_i2c_fd_close(handle->type.i2c.fd);
-       if (ret != PERIPHERAL_ERROR_NONE)
-               _E("Failed to i2c close fd");
-
        ret = peripheral_handle_free(handle);
-       if (ret != PERIPHERAL_ERROR_NONE) {
+       if (ret != PERIPHERAL_ERROR_NONE)
                _E("Failed to free i2c handle");
-               return PERIPHERAL_ERROR_UNKNOWN;
-       }
 
-       return PERIPHERAL_ERROR_NONE;
+       return ret;
 }
 
 int peripheral_handle_i2c_create(int bus, int address, peripheral_h *handle, gpointer user_data)
@@ -70,8 +63,6 @@ int peripheral_handle_i2c_create(int bus, int address, peripheral_h *handle, gpo
        RETVM_IF(address < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid i2c address");
        RETVM_IF(handle == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid i2c handle");
 
-       int ret = PERIPHERAL_ERROR_NONE;
-
        peripheral_info_s *info = (peripheral_info_s*)user_data;
 
        peripheral_h i2c_handle = NULL;
@@ -92,18 +83,8 @@ int peripheral_handle_i2c_create(int bus, int address, peripheral_h *handle, gpo
        i2c_handle->list = &info->i2c_list;
        i2c_handle->type.i2c.bus = bus;
        i2c_handle->type.i2c.address = address;
-       i2c_handle->type.i2c.fd = -1;
-
-       ret = peripheral_interface_i2c_fd_open(bus, address, &i2c_handle->type.i2c.fd);
-       if (ret != PERIPHERAL_ERROR_NONE) {
-               _E("Failed to i2c fd open");
-               goto out;
-       }
 
        *handle = i2c_handle;
-       return PERIPHERAL_ERROR_NONE;
 
-out:
-       peripheral_handle_i2c_destroy(i2c_handle);
-       return ret;
+       return PERIPHERAL_ERROR_NONE;
 }
index 605682ac43916d58179eca27723890b17b528d68..501245e0a19c0cdc5e7ef3758c09aefe5a3f34e5 100644 (file)
@@ -14,7 +14,6 @@
  * limitations under the License.
  */
 
-#include "peripheral_interface_pwm.h"
 #include "peripheral_handle_common.h"
 
 static bool __peripheral_handle_pwm_is_creatable(int chip, int pin, peripheral_info_s *info)
@@ -51,33 +50,11 @@ int peripheral_handle_pwm_destroy(peripheral_h handle)
 
        int ret = PERIPHERAL_ERROR_NONE;
 
-       ret = peripheral_interface_pwm_fd_period_close(handle->type.pwm.fd_period);
-       if (ret != PERIPHERAL_ERROR_NONE)
-               _E("Failed to pwm close period fd");
-
-       ret = peripheral_interface_pwm_fd_duty_cycle_close(handle->type.pwm.fd_duty_cycle);
-       if (ret != PERIPHERAL_ERROR_NONE)
-               _E("Failed to pwm close duty cycle fd");
-
-       ret = peripheral_interface_pwm_fd_polarity_close(handle->type.pwm.fd_polarity);
-       if (ret != PERIPHERAL_ERROR_NONE)
-               _E("Failed to pwm close polarity fd");
-
-       ret = peripheral_interface_pwm_fd_enable_close(handle->type.pwm.fd_enable);
-       if (ret != PERIPHERAL_ERROR_NONE)
-               _E("Failed to pwm close enable fd");
-
-       ret = peripheral_interface_pwm_unexport(handle->type.pwm.chip, handle->type.pwm.pin);
-       if (ret != PERIPHERAL_ERROR_NONE)
-               _E("Failed to pwm unexport");
-
        ret = peripheral_handle_free(handle);
-       if (ret != PERIPHERAL_ERROR_NONE) {
+       if (ret != PERIPHERAL_ERROR_NONE)
                _E("Failed to free pwm handle");
-               return PERIPHERAL_ERROR_UNKNOWN;
-       }
 
-       return PERIPHERAL_ERROR_NONE;
+       return ret;
 }
 
 int peripheral_handle_pwm_create(int chip, int pin, peripheral_h *handle, gpointer user_data)
@@ -86,8 +63,6 @@ int peripheral_handle_pwm_create(int chip, int pin, peripheral_h *handle, gpoint
        RETVM_IF(pin < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid pwm pin");
        RETVM_IF(handle == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid pwm handle");
 
-       int ret = PERIPHERAL_ERROR_NONE;
-
        peripheral_info_s *info = (peripheral_info_s*)user_data;
 
        peripheral_h pwm_handle = NULL;
@@ -102,52 +77,14 @@ int peripheral_handle_pwm_create(int chip, int pin, peripheral_h *handle, gpoint
        pwm_handle = peripheral_handle_new(&info->pwm_list);
        if (pwm_handle == NULL) {
                _E("peripheral_handle_new error");
-               ret = PERIPHERAL_ERROR_OUT_OF_MEMORY;
-               goto out;
+               return PERIPHERAL_ERROR_OUT_OF_MEMORY;
        }
 
        pwm_handle->list = &info->pwm_list;
        pwm_handle->type.pwm.chip = chip;
        pwm_handle->type.pwm.pin = pin;
-       pwm_handle->type.pwm.fd_period = -1;
-       pwm_handle->type.pwm.fd_duty_cycle = -1;
-       pwm_handle->type.pwm.fd_polarity = -1;
-       pwm_handle->type.pwm.fd_enable = -1;
-
-       ret = peripheral_interface_pwm_export(chip, pin);
-       if (ret != PERIPHERAL_ERROR_NONE) {
-               _E("Failed to pwm export");
-               goto out;
-       }
-
-       ret = peripheral_interface_pwm_fd_period_open(chip, pin, &pwm_handle->type.pwm.fd_period);
-       if (ret != PERIPHERAL_ERROR_NONE) {
-               _E("Failed to pwm open period fd");
-               goto out;
-       }
-
-       ret = peripheral_interface_pwm_fd_duty_cycle_open(chip, pin, &pwm_handle->type.pwm.fd_duty_cycle);
-       if (ret != PERIPHERAL_ERROR_NONE) {
-               _E("Failed to pwm open duty cycle fd");
-               goto out;
-       }
-
-       ret = peripheral_interface_pwm_fd_polarity_open(chip, pin, &pwm_handle->type.pwm.fd_polarity);
-       if (ret != PERIPHERAL_ERROR_NONE) {
-               _E("Failed to pwm open polarity fd");
-               goto out;
-       }
-
-       ret = peripheral_interface_pwm_fd_enable_open(chip, pin, &pwm_handle->type.pwm.fd_enable);
-       if (ret != PERIPHERAL_ERROR_NONE) {
-               _E("Failed to pwm open enable fd");
-               goto out;
-       }
 
        *handle = pwm_handle;
-       return PERIPHERAL_ERROR_NONE;
 
-out:
-       peripheral_handle_pwm_destroy(pwm_handle);
-       return ret;
+       return PERIPHERAL_ERROR_NONE;
 }
index 5d5e2ccc36b2adeaada3ebfdbed404e761c78ae2..727fde3bec61579f32cd51818adc34392eceb8eb 100644 (file)
@@ -14,7 +14,6 @@
  * limitations under the License.
  */
 
-#include "peripheral_interface_spi.h"
 #include "peripheral_handle_common.h"
 
 static bool __peripheral_handle_spi_is_creatable(int bus, int cs, peripheral_info_s *info)
@@ -51,17 +50,11 @@ int peripheral_handle_spi_destroy(peripheral_h handle)
 
        int ret = PERIPHERAL_ERROR_NONE;
 
-       ret = peripheral_interface_spi_fd_close(handle->type.spi.fd);
-       if (ret != PERIPHERAL_ERROR_NONE)
-               _E("Failed to spi close fd");
-
        ret = peripheral_handle_free(handle);
-       if (ret != PERIPHERAL_ERROR_NONE) {
+       if (ret != PERIPHERAL_ERROR_NONE)
                _E("Failed to free spi handle");
-               return PERIPHERAL_ERROR_UNKNOWN;
-       }
 
-       return PERIPHERAL_ERROR_NONE;
+       return ret;
 }
 
 int peripheral_handle_spi_create(int bus, int cs, peripheral_h *handle, gpointer user_data)
@@ -70,8 +63,6 @@ int peripheral_handle_spi_create(int bus, int cs, peripheral_h *handle, gpointer
        RETVM_IF(cs < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid spi cs");
        RETVM_IF(handle == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid spi handle");
 
-       int ret = PERIPHERAL_ERROR_NONE;
-
        peripheral_info_s *info = (peripheral_info_s*)user_data;
 
        peripheral_h spi_handle = NULL;
@@ -92,18 +83,8 @@ int peripheral_handle_spi_create(int bus, int cs, peripheral_h *handle, gpointer
        spi_handle->list = &info->spi_list;
        spi_handle->type.spi.bus = bus;
        spi_handle->type.spi.cs = cs;
-       spi_handle->type.spi.fd = -1;
-
-       ret = peripheral_interface_spi_fd_open(bus, cs, &spi_handle->type.spi.fd);
-       if (ret != PERIPHERAL_ERROR_NONE) {
-               _E("Failed to spi fd open");
-               goto out;
-       }
 
        *handle = spi_handle;
-       return PERIPHERAL_ERROR_NONE;
 
-out:
-       peripheral_handle_spi_destroy(spi_handle);
-       return ret;
+       return PERIPHERAL_ERROR_NONE;
 }
index 985764c3e331e7cf48b7255ba48cdb995ebcb48e..c6f0e0ddade5edd91828cdaa045cc4c46dbb883f 100644 (file)
@@ -14,7 +14,6 @@
  * limitations under the License.
  */
 
-#include "peripheral_interface_uart.h"
 #include "peripheral_handle_common.h"
 
 static bool __peripheral_handle_uart_is_creatable(int port, peripheral_info_s *info)
@@ -51,17 +50,11 @@ int peripheral_handle_uart_destroy(peripheral_h handle)
 
        int ret = PERIPHERAL_ERROR_NONE;
 
-       ret = peripheral_interface_uart_fd_close(handle->type.uart.fd);
-       if (ret != PERIPHERAL_ERROR_NONE)
-               _E("Failed to uart close fd");
-
        ret = peripheral_handle_free(handle);
-       if (ret != PERIPHERAL_ERROR_NONE) {
+       if (ret != PERIPHERAL_ERROR_NONE)
                _E("Failed to free uart handle");
-               return PERIPHERAL_ERROR_UNKNOWN;
-       }
 
-       return PERIPHERAL_ERROR_NONE;
+       return ret;
 }
 
 int peripheral_handle_uart_create(int port, peripheral_h *handle, gpointer user_data)
@@ -69,8 +62,6 @@ int peripheral_handle_uart_create(int port, peripheral_h *handle, gpointer user_
        RETVM_IF(port < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid uart port");
        RETVM_IF(handle == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid uart handle");
 
-       int ret = PERIPHERAL_ERROR_NONE;
-
        peripheral_info_s *info = (peripheral_info_s*)user_data;
 
        peripheral_h uart_handle = NULL;
@@ -90,18 +81,8 @@ int peripheral_handle_uart_create(int port, peripheral_h *handle, gpointer user_
 
        uart_handle->list = &info->uart_list;
        uart_handle->type.uart.port = port;
-       uart_handle->type.uart.fd = -1;
-
-       ret = peripheral_interface_uart_fd_open(port, &uart_handle->type.uart.fd);
-       if (ret != PERIPHERAL_ERROR_NONE) {
-               _E("Failed to uart fd open");
-               goto out;
-       }
 
        *handle = uart_handle;
-       return PERIPHERAL_ERROR_NONE;
 
-out:
-       peripheral_handle_uart_destroy(uart_handle);
-       return ret;
+       return PERIPHERAL_ERROR_NONE;
 }
index 8cd45a2cabb0c0969ae19d9f2f0b8651752433c1..c71358df95a7ff9f33872b4c7e33b86ee8cf432a 100644 (file)
@@ -134,7 +134,7 @@ int peripheral_interface_gpio_unexport(int pin)
        return PERIPHERAL_ERROR_NONE;
 }
 
-int peripheral_interface_gpio_fd_direction_open(int pin, int *fd_out)
+static int __peripheral_interface_gpio_fd_direction_open(int pin, int *fd_out)
 {
        RETVM_IF(pin < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid gpio pin");
        RETVM_IF(fd_out == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid fd_out for gpio direction");
@@ -151,19 +151,7 @@ int peripheral_interface_gpio_fd_direction_open(int pin, int *fd_out)
        return PERIPHERAL_ERROR_NONE;
 }
 
-int peripheral_interface_gpio_fd_direction_close(int fd)
-{
-       RETVM_IF(fd < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid fd for gpio direction");
-
-       int ret;
-
-       ret = close(fd);
-       IF_ERROR_RETURN(ret != 0);
-
-       return PERIPHERAL_ERROR_NONE;
-}
-
-int peripheral_interface_gpio_fd_edge_open(int pin, int *fd_out)
+static int __peripheral_interface_gpio_fd_edge_open(int pin, int *fd_out)
 {
        RETVM_IF(pin < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid gpio pin");
        RETVM_IF(fd_out == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid fd_out for gpio edge");
@@ -180,19 +168,7 @@ int peripheral_interface_gpio_fd_edge_open(int pin, int *fd_out)
        return PERIPHERAL_ERROR_NONE;
 }
 
-int peripheral_interface_gpio_fd_edge_close(int fd)
-{
-       RETVM_IF(fd < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid fd for gpio edge");
-
-       int ret;
-
-       ret = close(fd);
-       IF_ERROR_RETURN(ret != 0);
-
-       return PERIPHERAL_ERROR_NONE;
-}
-
-int peripheral_interface_gpio_fd_value_open(int pin, int *fd_out)
+static int __peripheral_interface_gpio_fd_value_open(int pin, int *fd_out)
 {
        RETVM_IF(pin < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid gpio pin");
        RETVM_IF(fd_out == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid fd_out for gpio value");
@@ -209,15 +185,59 @@ int peripheral_interface_gpio_fd_value_open(int pin, int *fd_out)
        return PERIPHERAL_ERROR_NONE;
 }
 
-int peripheral_interface_gpio_fd_value_close(int fd)
+int peripheral_interface_gpio_fd_list_create(int pin, GUnixFDList **list_out)
 {
-       RETVM_IF(fd < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid fd for gpio value");
+       RETVM_IF(pin < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid gpio pin");
 
        int ret;
 
-       ret = close(fd);
-       IF_ERROR_RETURN(ret != 0);
+       GUnixFDList *list = NULL;
+       int fd_direction = -1;
+       int fd_edge = -1;
+       int fd_value = -1;
 
-       return PERIPHERAL_ERROR_NONE;
+       ret = __peripheral_interface_gpio_fd_direction_open(pin, &fd_direction);
+       if (ret != PERIPHERAL_ERROR_NONE) {
+               _E("Failed to open gpio direction fd");
+               goto out;
+       }
+
+       ret = __peripheral_interface_gpio_fd_edge_open(pin, &fd_edge);
+       if (ret != PERIPHERAL_ERROR_NONE) {
+               _E("Failed to open gpio edge fd");
+               goto out;
+       }
+
+       ret = __peripheral_interface_gpio_fd_value_open(pin, &fd_value);
+       if (ret != PERIPHERAL_ERROR_NONE) {
+               _E("Failed to open gpio value fd");
+               goto out;
+       }
+
+       list = g_unix_fd_list_new();
+       if (list == NULL) {
+               _E("Failed to create gpio fd list");
+               ret = PERIPHERAL_ERROR_OUT_OF_MEMORY;
+               goto out;
+       }
+
+       /* Do not change the order of the fd list */
+       g_unix_fd_list_append(list, fd_direction, NULL);
+       g_unix_fd_list_append(list, fd_edge, NULL);
+       g_unix_fd_list_append(list, fd_value, NULL);
+
+       *list_out = list;
+
+out:
+       close(fd_direction);
+       close(fd_edge);
+       close(fd_value);
+
+       return ret;
 }
 
+void peripheral_interface_gpio_fd_list_destroy(GUnixFDList *list)
+{
+       if (list != NULL)
+               g_object_unref(list); // file descriptors in list is closed in hear.
+}
index 000aad5e736eebb5bee16603d1345f85ccb467bf..4a06dbc65656d9c19b3e76325f514d89f6cd1e64 100644 (file)
@@ -21,7 +21,7 @@
 
 #define I2C_SLAVE      0x0703  /* Use this slave address */
 
-int peripheral_interface_i2c_fd_open(int bus, int address, int *fd_out)
+static int __peripheral_interface_i2c_fd_open(int bus, int address, int *fd_out)
 {
        RETVM_IF(bus < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid i2c bus");
        RETVM_IF(address < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid i2c address");
@@ -43,14 +43,42 @@ int peripheral_interface_i2c_fd_open(int bus, int address, int *fd_out)
        return PERIPHERAL_ERROR_NONE;
 }
 
-int peripheral_interface_i2c_fd_close(int fd)
+int peripheral_interface_i2c_fd_list_create(int bus, int address, GUnixFDList **list_out)
 {
-       RETVM_IF(fd < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid fd for i2c");
+       RETVM_IF(bus < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid i2c bus");
+       RETVM_IF(address < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid i2c address");
 
        int ret;
 
-       ret = close(fd);
-       IF_ERROR_RETURN(ret != 0);
+       GUnixFDList *list = NULL;
+       int fd = -1;
 
-       return PERIPHERAL_ERROR_NONE;
+
+       ret = __peripheral_interface_i2c_fd_open(bus, address, &fd);
+       if (ret != PERIPHERAL_ERROR_NONE) {
+               _E("Failed to open i2c fd");
+       }
+
+       list = g_unix_fd_list_new();
+       if (list == NULL) {
+               _E("Failed to create i2c fd list");
+               ret = PERIPHERAL_ERROR_OUT_OF_MEMORY;
+               goto out;
+       }
+
+       /* Do not change the order of the fd list */
+       g_unix_fd_list_append(list, fd, NULL);
+
+       *list_out = list;
+
+out:
+       close(fd);
+
+       return ret;
+}
+
+void peripheral_interface_i2c_fd_list_destroy(GUnixFDList *list)
+{
+       if (list != NULL)
+               g_object_unref(list);
 }
\ No newline at end of file
index 843a88c8c8b3df97c4f7acd9f6ec75291d80bb76..d43f4a63eb9f973b36f5453f19573f6381a97156 100644 (file)
@@ -69,7 +69,7 @@ int peripheral_interface_pwm_unexport(int chip, int pin)
        return PERIPHERAL_ERROR_NONE;
 }
 
-int peripheral_interface_pwm_fd_period_open(int chip, int pin, int *fd_out)
+static int __peripheral_interface_pwm_fd_period_open(int chip, int pin, int *fd_out)
 {
        RETVM_IF(chip < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid pwm chip");
        RETVM_IF(pin < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid pwm pin");
@@ -87,19 +87,7 @@ int peripheral_interface_pwm_fd_period_open(int chip, int pin, int *fd_out)
        return PERIPHERAL_ERROR_NONE;
 }
 
-int peripheral_interface_pwm_fd_period_close(int fd)
-{
-       RETVM_IF(fd < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid fd for pwm period");
-
-       int ret;
-
-       ret = close(fd);
-       IF_ERROR_RETURN(ret != 0);
-
-       return PERIPHERAL_ERROR_NONE;
-}
-
-int peripheral_interface_pwm_fd_duty_cycle_open(int chip, int pin, int *fd_out)
+static int __peripheral_interface_pwm_fd_duty_cycle_open(int chip, int pin, int *fd_out)
 {
        RETVM_IF(chip < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid pwm chip");
        RETVM_IF(pin < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid pwm pin");
@@ -117,19 +105,7 @@ int peripheral_interface_pwm_fd_duty_cycle_open(int chip, int pin, int *fd_out)
        return PERIPHERAL_ERROR_NONE;
 }
 
-int peripheral_interface_pwm_fd_duty_cycle_close(int fd)
-{
-       RETVM_IF(fd < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid fd for pwm duty cycle");
-
-       int ret;
-
-       ret = close(fd);
-       IF_ERROR_RETURN(ret != 0);
-
-       return PERIPHERAL_ERROR_NONE;
-}
-
-int peripheral_interface_pwm_fd_polarity_open(int chip, int pin, int *fd_out)
+static int __peripheral_interface_pwm_fd_polarity_open(int chip, int pin, int *fd_out)
 {
        RETVM_IF(chip < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid pwm chip");
        RETVM_IF(pin < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid pwm pin");
@@ -147,19 +123,7 @@ int peripheral_interface_pwm_fd_polarity_open(int chip, int pin, int *fd_out)
        return PERIPHERAL_ERROR_NONE;
 }
 
-int peripheral_interface_pwm_fd_polarity_close(int fd)
-{
-       RETVM_IF(fd < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid fd for pwm polarity");
-
-       int ret;
-
-       ret = close(fd);
-       IF_ERROR_RETURN(ret != 0);
-
-       return PERIPHERAL_ERROR_NONE;
-}
-
-int peripheral_interface_pwm_fd_enable_open(int chip, int pin, int *fd_out)
+static int __peripheral_interface_pwm_fd_enable_open(int chip, int pin, int *fd_out)
 {
        RETVM_IF(chip < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid pwm chip");
        RETVM_IF(pin < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid pwm pin");
@@ -177,14 +141,69 @@ int peripheral_interface_pwm_fd_enable_open(int chip, int pin, int *fd_out)
        return PERIPHERAL_ERROR_NONE;
 }
 
-int peripheral_interface_pwm_fd_enable_close(int fd)
+int peripheral_interface_pwm_fd_list_create(int chip, int pin, GUnixFDList **list_out)
 {
-       RETVM_IF(fd < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid fd for pwm enable");
+       RETVM_IF(chip < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid pwm chip");
+       RETVM_IF(pin < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid pwm pin");
 
        int ret;
 
-       ret = close(fd);
-       IF_ERROR_RETURN(ret != 0);
+       GUnixFDList *list = NULL;
+       int fd_period = -1;
+       int fd_duty_cycle = -1;
+       int fd_polarity = -1;
+       int fd_enable = -1;
+
+       ret = __peripheral_interface_pwm_fd_period_open(chip, pin, &fd_period);
+       if (ret != PERIPHERAL_ERROR_NONE) {
+               _E("Failed to open pwm period fd");
+               goto out;
+       }
+
+       ret = __peripheral_interface_pwm_fd_duty_cycle_open(chip, pin, &fd_duty_cycle);
+       if (ret != PERIPHERAL_ERROR_NONE) {
+               _E("Failed to open pwm duty cycle fd");
+               goto out;
+       }
+
+       ret = __peripheral_interface_pwm_fd_polarity_open(chip, pin, &fd_polarity);
+       if (ret != PERIPHERAL_ERROR_NONE) {
+               _E("Failed to open pwm polarity fd");
+               goto out;
+       }
+
+       ret = __peripheral_interface_pwm_fd_enable_open(chip, pin, &fd_enable);
+       if (ret != PERIPHERAL_ERROR_NONE) {
+               _E("Failed to open pwm enable fd");
+               goto out;
+       }
+
+       list = g_unix_fd_list_new();
+       if (list == NULL) {
+               _E("Failed to create pwm fd list");
+               ret = PERIPHERAL_ERROR_OUT_OF_MEMORY;
+               goto out;
+       }
+
+       /* Do not change the order of the fd list */
+       g_unix_fd_list_append(list, fd_period, NULL);
+       g_unix_fd_list_append(list, fd_duty_cycle, NULL);
+       g_unix_fd_list_append(list, fd_polarity, NULL);
+       g_unix_fd_list_append(list, fd_enable, NULL);
+
+       *list_out = list;
+
+out:
+       close(fd_period);
+       close(fd_duty_cycle);
+       close(fd_polarity);
+       close(fd_enable);
+
+       return ret;
+}
 
-       return PERIPHERAL_ERROR_NONE;
+void peripheral_interface_pwm_fd_list_destroy(GUnixFDList *list)
+{
+       if (list != NULL)
+               g_object_unref(list); // file descriptors in list is closed in hear.
 }
\ No newline at end of file
index d19ca91958a7ed407c717d83051be64cfd7c9e92..16fea9b949e6b2af28548c7afc7ba8288e65d2b0 100644 (file)
@@ -17,7 +17,7 @@
 #include "peripheral_interface_spi.h"
 #include "peripheral_interface_common.h"
 
-int peripheral_interface_spi_fd_open(int bus, int cs, int *fd_out)
+static int __peripheral_interface_spi_fd_open(int bus, int cs, int *fd_out)
 {
        RETVM_IF(bus < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid spi bus");
        RETVM_IF(cs < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid spi cs");
@@ -36,14 +36,42 @@ int peripheral_interface_spi_fd_open(int bus, int cs, int *fd_out)
        return PERIPHERAL_ERROR_NONE;
 }
 
-int peripheral_interface_spi_fd_close(int fd)
+int peripheral_interface_spi_fd_list_create(int bus, int cs, GUnixFDList **list_out)
 {
-       RETVM_IF(fd < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid fd for spi");
+       RETVM_IF(bus < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid spi bus");
+       RETVM_IF(cs < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid spi chip select");
 
        int ret;
 
-       ret = close(fd);
-       IF_ERROR_RETURN(ret != 0);
+       GUnixFDList *list = NULL;
+       int fd = -1;
 
-       return PERIPHERAL_ERROR_NONE;
+
+       ret = __peripheral_interface_spi_fd_open(bus, cs, &fd);
+       if (ret != PERIPHERAL_ERROR_NONE) {
+               _E("Failed to open spi fd");
+       }
+
+       list = g_unix_fd_list_new();
+       if (list == NULL) {
+               _E("Failed to create spi fd list");
+               ret = PERIPHERAL_ERROR_OUT_OF_MEMORY;
+               goto out;
+       }
+
+       /* Do not change the order of the fd list */
+       g_unix_fd_list_append(list, fd, NULL);
+
+       *list_out = list;
+
+out:
+       close(fd);
+
+       return ret;
+}
+
+void peripheral_interface_spi_fd_list_destroy(GUnixFDList *list)
+{
+       if (list != NULL)
+               g_object_unref(list);
 }
\ No newline at end of file
index 7d056e70b14253266dc417e67dc212db79d00b4c..cdfe167f1adb3f0fa9efee950bc8637891b28eba 100644 (file)
@@ -23,7 +23,7 @@
 #define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0]))
 #endif
 
-int peripheral_interface_uart_fd_open(int port, int *fd_out)
+static int __peripheral_interface_uart_fd_open(int port, int *fd_out)
 {
        RETVM_IF(port < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid uart port");
        RETVM_IF(fd_out == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid fd_out for uart");
@@ -52,13 +52,41 @@ int peripheral_interface_uart_fd_open(int port, int *fd_out)
        return PERIPHERAL_ERROR_NONE;
 }
 
-int peripheral_interface_uart_fd_close(int fd)
+int peripheral_interface_uart_fd_list_create(int port, GUnixFDList **list_out)
 {
-       RETVM_IF(fd < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid fd for uart");
+       RETVM_IF(port < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid uart port");
+
        int ret;
 
-       ret = close(fd);
-       IF_ERROR_RETURN(ret != 0);
+       GUnixFDList *list = NULL;
+       int fd = -1;
 
-       return PERIPHERAL_ERROR_NONE;
+
+       ret = __peripheral_interface_uart_fd_open(port, &fd);
+       if (ret != PERIPHERAL_ERROR_NONE) {
+               _E("Failed to open uart fd");
+       }
+
+       list = g_unix_fd_list_new();
+       if (list == NULL) {
+               _E("Failed to create uart fd list");
+               ret = PERIPHERAL_ERROR_OUT_OF_MEMORY;
+               goto out;
+       }
+
+       /* Do not change the order of the fd list */
+       g_unix_fd_list_append(list, fd, NULL);
+
+       *list_out = list;
+
+out:
+       close(fd);
+
+       return ret;
+}
+
+void peripheral_interface_uart_fd_list_destroy(GUnixFDList *list)
+{
+       if (list != NULL)
+               g_object_unref(list);
 }
\ No newline at end of file