From: Sungguk Na Date: Fri, 21 Apr 2017 10:49:11 +0000 (+0900) Subject: Change gdbus interface by using gdbus-codegen X-Git-Tag: submit/tizen/20170426.072552~4 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=a0b47e9c274f34a3ea4bf4bc479c6e18e4788f4e;p=platform%2Fcore%2Fapi%2Fperipheral-io.git Change gdbus interface by using gdbus-codegen Change-Id: I8706817d7ad17952823fae1d1475b4dc1570f3e1 Signed-off-by: Sungguk Na --- diff --git a/CMakeLists.txt b/CMakeLists.txt index a31234b..c9d951a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,13 +9,22 @@ SET(fw_name "${project_prefix}-${service}-${submodule}") PROJECT(${fw_name}) -SET(dependents "dlog glib-2.0 gio-2.0 capi-base-common") +SET(dependents "dlog glib-2.0 gio-2.0 gio-unix-2.0 capi-base-common") SET(pc_dependents "capi-base-common") SET(CMAKE_INSTALL_PREFIX ${prefix}) SET(PREFIX $(CMAKE_INSTALL_PREFIX)) SET(VERSION ${version}) +FIND_PROGRAM(GDBUS_CODEGEN NAMES gdbus-codegen) +EXEC_PROGRAM(${GDBUS_CODEGEN} ARGS + " \\ + --generate-c-code ${CMAKE_SOURCE_DIR}/src/peripheral_io_gdbus \\ + --c-namespace PeripheralIoGdbus \\ + --interface-prefix org.tizen.system.peripheral_io. \\ + ${CMAKE_SOURCE_DIR}/src/peripheral_io.xml \\ + ") + SET(INC_DIR include) INCLUDE_DIRECTORIES(${INC_DIR}) @@ -38,6 +47,7 @@ SET(SOURCES src/peripheral_adc.c src/peripheral_pwm.c src/peripheral_uart.c src/peripheral_dbus.c + src/peripheral_io_gdbus.c src/peripheral_i2c.c src/peripheral_spi.c) diff --git a/include/peripheral_dbus.h b/include/peripheral_dbus.h index 1136792..d21fb2a 100644 --- a/include/peripheral_dbus.h +++ b/include/peripheral_dbus.h @@ -20,6 +20,9 @@ #define PERIPHERAL_DBUS_INTERFACE "org.tizen.system.peripheral_io" #define PERIPHERAL_DBUS_PATH "/Org/Tizen/System/Peripheral_io" +#define PERIPHERAL_DBUS_GPIO_PATH "/Org/Tizen/System/Peripheral_io/Gpio" +#define PERIPHERAL_DBUS_I2C_PATH "/Org/Tizen/System/Peripheral_io/I2c" +#define PERIPHERAL_DBUS_PWM_PATH "/Org/Tizen/System/Peripheral_io/Pwm" #define PERIPHERAL_DBUS_NAME "org.tizen.system.peripheral_io" #define PERIPHERAL_METHOD_GPIO "gpio" @@ -28,13 +31,36 @@ #define PERIPHERAL_METHOD_SPI "spi" #define PERIPHERAL_METHOD_UART "uart" -int set_dbus_connection(void); -void unset_dbus_connection(void); -GDBusConnection *get_dbus_connection(void); +void gpio_proxy_init(void); +void i2c_proxy_init(void); +void pwm_proxy_init(void); +void gpio_proxy_deinit(); +void i2c_proxy_deinit(); +void pwm_proxy_deinit(); int peripheral_dbus_gpio(peripheral_gpio_h gpio, char * sensorid, char *funcname, int write_value, int *read_value); -int peripheral_dbus_i2c(peripheral_i2c_h i2c, char * sensorid, char *funcname, int value, unsigned char *data, int addr); -int peripheral_dbus_pwm(peripheral_pwm_context_h dev, char * sensorid, char *funcname); +int peripheral_dbus_gpio_open(peripheral_gpio_h gpio); +int peripheral_dbus_gpio_close(peripheral_gpio_h gpio); +int peripheral_dbus_gpio_get_direction(peripheral_gpio_h gpio, peripheral_gpio_direction_e *direction); +int peripheral_dbus_gpio_set_direction(peripheral_gpio_h gpio, peripheral_gpio_direction_e direction); +int peripheral_dbus_gpio_read(peripheral_gpio_h gpio, int *value); +int peripheral_dbus_gpio_write(peripheral_gpio_h gpio, int value); +int peripheral_dbus_gpio_get_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e *edge); +int peripheral_dbus_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e edge); + +int peripheral_dbus_i2c_init(peripheral_i2c_h i2c, int bus); +int peripheral_dbus_i2c_stop(peripheral_i2c_h i2c); +int peripheral_dbus_i2c_set_address(peripheral_i2c_h i2c, int address); +int peripheral_dbus_i2c_read(peripheral_i2c_h i2c, uint8_t *data, int length); +int peripheral_dbus_i2c_write(peripheral_i2c_h i2c, uint8_t *data, int length); + +int peripheral_dbus_pwm_open(peripheral_pwm_context_h dev, int device, int channel); +int peripheral_dbus_pwm_close(peripheral_pwm_context_h dev); +int peripheral_dbus_pwm_get_duty_cycle(peripheral_pwm_context_h dev, int *duty_cycle); +int peripheral_dbus_pwm_set_duty_cycle(peripheral_pwm_context_h dev, int duty_cycle); +int peripheral_dbus_pwm_get_period(peripheral_pwm_context_h dev, int *period); +int peripheral_dbus_pwm_set_period(peripheral_pwm_context_h dev, int period); +int peripheral_dbus_pwm_set_enable(peripheral_pwm_context_h dev, peripheral_pwm_state_e enable); #endif /* __PERIPHERAL_DBUS_H_ */ diff --git a/src/peripheral_dbus.c b/src/peripheral_dbus.c index e90997a..67c7025 100644 --- a/src/peripheral_dbus.c +++ b/src/peripheral_dbus.c @@ -22,166 +22,547 @@ #include "peripheral_dbus.h" #include "peripheral_common.h" #include "peripheral_internal.h" +#include "peripheral_io_gdbus.h" -GDBusConnection *connection = NULL; +PeripheralIoGdbusGpio *gpio_proxy = NULL; +PeripheralIoGdbusI2c *i2c_proxy = NULL; +PeripheralIoGdbusPwm *pwm_proxy = NULL; -int set_dbus_connection(void) +void gpio_proxy_init(void) { GError *error = NULL; - if (connection) - return PERIPHERAL_ERROR_NONE; + if (gpio_proxy != NULL) + return; - connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error); - if (error) { - _E("gdbus error occurred (%s)", error->message); + gpio_proxy = peripheral_io_gdbus_gpio_proxy_new_for_bus_sync( + G_BUS_TYPE_SYSTEM, + G_DBUS_PROXY_FLAGS_NONE, + PERIPHERAL_DBUS_NAME, + PERIPHERAL_DBUS_GPIO_PATH, + NULL, + &error); +} + +void i2c_proxy_init(void) +{ + GError *error = NULL; + + if (i2c_proxy != NULL) + return; + + i2c_proxy = peripheral_io_gdbus_i2c_proxy_new_for_bus_sync( + G_BUS_TYPE_SYSTEM, + G_DBUS_PROXY_FLAGS_NONE, + PERIPHERAL_DBUS_NAME, + PERIPHERAL_DBUS_I2C_PATH, + NULL, + &error); +} + +void pwm_proxy_init(void) +{ + GError *error = NULL; + + if (pwm_proxy != NULL) + return; + + pwm_proxy = peripheral_io_gdbus_pwm_proxy_new_for_bus_sync( + G_BUS_TYPE_SYSTEM, + G_DBUS_PROXY_FLAGS_NONE, + PERIPHERAL_DBUS_NAME, + PERIPHERAL_DBUS_PWM_PATH, + NULL, + &error); +} + +void gpio_proxy_deinit() +{ + gpio_proxy = NULL; +} + +void i2c_proxy_deinit() +{ + i2c_proxy = NULL; +} + +void pwm_proxy_deinit() +{ + pwm_proxy = NULL; +} + +int peripheral_dbus_gpio_open(peripheral_gpio_h gpio) +{ + GError *error = NULL; + gint32 ret = PERIPHERAL_ERROR_NONE; + + if (gpio_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + if (peripheral_io_gdbus_gpio_call_open_sync( + gpio_proxy, + gpio->pin, + &ret, + NULL, + &error) == FALSE) { + _E("Error in %s() : %s\n", __func__, error->message); g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } - if (!connection) { - _E("Failed to get gdbus connection "); + return ret; +} + +int peripheral_dbus_gpio_close(peripheral_gpio_h gpio) +{ + GError *error = NULL; + peripheral_error_e ret = PERIPHERAL_ERROR_NONE; + + if (gpio_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + if (peripheral_io_gdbus_gpio_call_close_sync( + gpio_proxy, + gpio->pin, + &ret, + NULL, + &error) == FALSE) { + _E("Error in %s() : %s\n", __func__, error->message); + g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; - } else { - //Sets whether the process should be terminated when connection is closed by the remote peer - g_dbus_connection_set_exit_on_close(connection, FALSE); //FALSE shareable connection is NOT closed by the remote peer } - return PERIPHERAL_ERROR_NONE; + return ret; } -void unset_dbus_connection(void) +int peripheral_dbus_gpio_get_direction(peripheral_gpio_h gpio, peripheral_gpio_direction_e *direction) { - if (connection) { - g_object_unref(connection); - connection = NULL; + GError *error = NULL; + peripheral_error_e ret = PERIPHERAL_ERROR_NONE; + + if (gpio_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + if (peripheral_io_gdbus_gpio_call_get_direction_sync( + gpio_proxy, + gpio->pin, + (gint*)direction, + &ret, + NULL, + &error) == FALSE) { + _E("Error in %s() : %s\n", __func__, error->message); + g_error_free(error); + return PERIPHERAL_ERROR_UNKNOWN; } + + return ret; } -GDBusConnection *get_dbus_connection(void) +int peripheral_dbus_gpio_set_direction(peripheral_gpio_h gpio, peripheral_gpio_direction_e direction) { - return connection; + GError *error = NULL; + peripheral_error_e ret = PERIPHERAL_ERROR_NONE; + + if (gpio_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + if (peripheral_io_gdbus_gpio_call_set_direction_sync( + gpio_proxy, + gpio->pin, + direction, + &ret, + NULL, + &error) == FALSE) { + _E("Error in %s() : %s\n", __func__, error->message); + g_error_free(error); + return PERIPHERAL_ERROR_UNKNOWN; + } + + return ret; } -int peripheral_dbus_gpio(peripheral_gpio_h gpio, char * sensorid, char *funcname, int write_value, int *read_value) +int peripheral_dbus_gpio_read(peripheral_gpio_h gpio, int *value) { GError *error = NULL; + peripheral_error_e ret = PERIPHERAL_ERROR_NONE; - GVariant *ret_value = NULL; - gint32 read = 0; - gint32 ret = PERIPHERAL_ERROR_NONE; + if (gpio_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; - error = NULL; + if (peripheral_io_gdbus_gpio_call_read_sync( + gpio_proxy, + gpio->pin, + value, + &ret, + NULL, + &error) == FALSE) { + _E("Error in %s() : %s\n", __func__, error->message); + g_error_free(error); + return PERIPHERAL_ERROR_UNKNOWN; + } - ret_value = g_dbus_connection_call_sync(connection, - PERIPHERAL_DBUS_NAME, - PERIPHERAL_DBUS_PATH, - PERIPHERAL_DBUS_INTERFACE, - sensorid, - g_variant_new("(siiii)", funcname, gpio->pin, gpio->direction, gpio->edge, write_value), - G_VARIANT_TYPE("(iiiii)"), - G_DBUS_CALL_FLAGS_NONE, - -1, - NULL, - &error); + return ret; +} - if (ret_value == NULL) { - g_printerr("Error invoking %s () : %s\n", sensorid, error->message); +int peripheral_dbus_gpio_write(peripheral_gpio_h gpio, int value) +{ + GError *error = NULL; + peripheral_error_e ret = PERIPHERAL_ERROR_NONE; + + if (gpio_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + if (peripheral_io_gdbus_gpio_call_write_sync( + gpio_proxy, + gpio->pin, + value, + &ret, + NULL, + &error) == FALSE) { + _E("Error in %s() : %s\n", __func__, error->message); g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } - g_variant_get(ret_value, "(iiiii)", &gpio->pin, &gpio->direction, &gpio->edge, &read, &ret); - g_variant_unref(ret_value); + return ret; +} + +int peripheral_dbus_gpio_get_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e *edge) +{ + GError *error = NULL; + peripheral_error_e ret = PERIPHERAL_ERROR_NONE; + + if (gpio_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + if (peripheral_io_gdbus_gpio_call_get_edge_mode_sync( + gpio_proxy, + gpio->pin, + (int*)edge, + &ret, + NULL, + &error) == FALSE) { + _E("Error in %s() : %s\n", __func__, error->message); + g_error_free(error); + return PERIPHERAL_ERROR_UNKNOWN; + } + + return ret; +} + +int peripheral_dbus_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e edge) +{ + GError *error = NULL; + peripheral_error_e ret = PERIPHERAL_ERROR_NONE; - if (read_value != 0) - (*read_value) = read; + if (gpio_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + if (peripheral_io_gdbus_gpio_call_set_edge_mode_sync( + gpio_proxy, + gpio->pin, + edge, + &ret, + NULL, + &error) == FALSE) { + _E("Error in %s() : %s\n", __func__, error->message); + g_error_free(error); + return PERIPHERAL_ERROR_UNKNOWN; + } return ret; +} +int peripheral_dbus_i2c_init(peripheral_i2c_h i2c, int bus) +{ + GError *error = NULL; + peripheral_error_e ret = PERIPHERAL_ERROR_NONE; + + if (i2c_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + if (peripheral_io_gdbus_i2c_call_init_sync( + i2c_proxy, + bus, + &i2c->fd, + &ret, + NULL, + &error) == FALSE) { + _E("Error in %s() : %s\n", __func__, error->message); + g_error_free(error); + return PERIPHERAL_ERROR_UNKNOWN; + } + + return ret; } -int peripheral_dbus_i2c(peripheral_i2c_h i2c, char * sensorid, char *funcname, int value, unsigned char * data, int addr) +int peripheral_dbus_i2c_stop(peripheral_i2c_h i2c) { GError *error = NULL; - GVariant *ret_value = NULL; - gint32 ret = PERIPHERAL_ERROR_NONE; - GVariantBuilder *builder; + peripheral_error_e ret = PERIPHERAL_ERROR_NONE; + + if (i2c_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + if (peripheral_io_gdbus_i2c_call_stop_sync( + i2c_proxy, + i2c->fd, + &ret, + NULL, + &error) == FALSE) { + _E("Error in %s() : %s\n", __func__, error->message); + g_error_free(error); + return PERIPHERAL_ERROR_UNKNOWN; + } + + return ret; +} + +int peripheral_dbus_i2c_set_address(peripheral_i2c_h i2c, int address) +{ + GError *error = NULL; + peripheral_error_e ret = PERIPHERAL_ERROR_NONE; + + if (i2c_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + if (peripheral_io_gdbus_i2c_call_set_address_sync( + i2c_proxy, + i2c->fd, + address, + &ret, + NULL, + &error) == FALSE) { + _E("Error in %s() : %s\n", __func__, error->message); + g_error_free(error); + return PERIPHERAL_ERROR_UNKNOWN; + } + + if (ret != PERIPHERAL_ERROR_NONE) + _E("%s failed, ret = %d", __func__, ret); + + return ret; +} + +int peripheral_dbus_i2c_read(peripheral_i2c_h i2c, uint8_t *data, int length) +{ + GError *error = NULL; + peripheral_error_e ret = PERIPHERAL_ERROR_NONE; + GVariant *data_array; + GVariantIter *iter; guint8 str; - GVariantIter *ret_data; - - builder = g_variant_builder_new(G_VARIANT_TYPE("ay")); - if (data == NULL) { - g_variant_builder_add(builder, "y", 0x10); - g_variant_builder_add(builder, "y", 0x00); - } else { - int i = 0; - for (i = 0; i < value; i++) - g_variant_builder_add(builder, "y", data[i]); - g_variant_builder_add(builder, "y", 0x00); - } - - ret_value = g_dbus_connection_call_sync(connection, - PERIPHERAL_DBUS_NAME, - PERIPHERAL_DBUS_PATH, - PERIPHERAL_DBUS_INTERFACE, - sensorid, - g_variant_new("(siiayi)", funcname, value, i2c->fd, builder, addr), - G_VARIANT_TYPE("(iayi)"), - G_DBUS_CALL_FLAGS_NONE, - -1, - NULL, - &error); + int i = 0; + + if (i2c_proxy == NULL || data == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + if (peripheral_io_gdbus_i2c_call_read_sync( + i2c_proxy, + i2c->fd, + length, + &data_array, + &ret, + NULL, + &error) == FALSE) { + _E("Error in %s() : %s\n", __func__, error->message); + g_error_free(error); + return PERIPHERAL_ERROR_UNKNOWN; + } + + g_variant_get(data_array, "a(y)", &iter); + while (g_variant_iter_loop(iter, "(y)", &str)) { + data[i] = str; + if (i++ == length) break; + } + g_variant_iter_free(iter); + + return ret; +} +int peripheral_dbus_i2c_write(peripheral_i2c_h i2c, uint8_t *data, int length) +{ + GError *error = NULL; + peripheral_error_e ret = PERIPHERAL_ERROR_NONE; + GVariantBuilder *builder; + GVariant *g_data; + int i = 0; + + if (i2c_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + builder = g_variant_builder_new(G_VARIANT_TYPE("a(y)")); + + for (i = 0; i < length; i++) + g_variant_builder_add(builder, "(y)", data[i]); + g_variant_builder_add(builder, "(y)", 0x00); + + g_data = g_variant_new("a(y)", builder); g_variant_builder_unref(builder); - if (ret_value == NULL) { - g_printerr("Error invoking %s () : %s\n", sensorid, error->message); + if (peripheral_io_gdbus_i2c_call_write_sync( + i2c_proxy, + i2c->fd, + length, + g_data, + &ret, + NULL, + &error) == FALSE) { + _E("Error in %s() : %s\n", __func__, error->message); g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } - g_variant_get(ret_value, "(iayi)", &(i2c->fd), &ret_data, &ret); - g_variant_unref(ret_value); + return ret; +} + +int peripheral_dbus_pwm_open(peripheral_pwm_context_h dev, int device, int channel) +{ + GError *error = NULL; + gint32 ret = PERIPHERAL_ERROR_NONE; - if (data != NULL) { - int i = 0; - while (g_variant_iter_loop(ret_data, "y", &str)) { - data[i] = str; - i++; - if (i == value) - break; - } + if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + /* TODO: Need to reorganize arguments */ + if (peripheral_io_gdbus_pwm_call_open_sync( + pwm_proxy, + device, + channel, + &ret, + NULL, + &error) == FALSE) { + _E("Error in %s() : %s\n", __func__, error->message); + g_error_free(error); + return PERIPHERAL_ERROR_UNKNOWN; } return ret; } -int peripheral_dbus_pwm(peripheral_pwm_context_h dev, char * sensorid, char *funcname) +int peripheral_dbus_pwm_close(peripheral_pwm_context_h dev) { GError *error = NULL; - GVariant *ret_value = NULL; gint32 ret = PERIPHERAL_ERROR_NONE; - ret_value = g_dbus_connection_call_sync(connection, - PERIPHERAL_DBUS_NAME, - PERIPHERAL_DBUS_PATH, - PERIPHERAL_DBUS_INTERFACE, - sensorid, - g_variant_new("(siiiii)", funcname, dev->device, dev->channel, dev->period, dev->duty_cycle, dev->enabled), - G_VARIANT_TYPE("(iii)"), - G_DBUS_CALL_FLAGS_NONE, - -1, - NULL, - &error); - if (ret_value == NULL) { - g_printerr("Error invoking %s () : %s\n", sensorid, error->message); + if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + /* TODO: Need to reorganize arguments */ + if (peripheral_io_gdbus_pwm_call_close_sync( + pwm_proxy, + dev->device, + dev->channel, + &ret, + NULL, + &error) == FALSE) { + _E("Error in %s() : %s\n", __func__, error->message); g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } - g_variant_get(ret_value, "(iii)", &dev->period, &dev->duty_cycle, &ret); - g_variant_unref(ret_value); + return ret; +} + +int peripheral_dbus_pwm_get_duty_cycle(peripheral_pwm_context_h dev, int *duty_cycle) +{ + GError *error = NULL; + gint32 ret = PERIPHERAL_ERROR_NONE; + + if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + /* TODO: Need to reorganize arguments */ + if (peripheral_io_gdbus_pwm_call_get_duty_cycle_sync( + pwm_proxy, + dev->device, + dev->channel, + duty_cycle, + &ret, + NULL, + &error) == FALSE) { + _E("Error in %s() : %s\n", __func__, error->message); + g_error_free(error); + return PERIPHERAL_ERROR_UNKNOWN; + } + + return ret; +} +int peripheral_dbus_pwm_set_duty_cycle(peripheral_pwm_context_h dev, int duty_cycle) +{ + GError *error = NULL; + gint32 ret = PERIPHERAL_ERROR_NONE; + + if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + /* TODO: Need to reorganize arguments */ + if (peripheral_io_gdbus_pwm_call_set_duty_cycle_sync( + pwm_proxy, + dev->device, + dev->channel, + duty_cycle, + &ret, + NULL, + &error) == FALSE) { + _E("Error in %s() : %s\n", __func__, error->message); + g_error_free(error); + return PERIPHERAL_ERROR_UNKNOWN; + } + + return ret; +} + +int peripheral_dbus_pwm_get_period(peripheral_pwm_context_h dev, int *period) +{ + GError *error = NULL; + gint32 ret = PERIPHERAL_ERROR_NONE; + + if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + /* TODO: Need to reorganize arguments */ + if (peripheral_io_gdbus_pwm_call_get_period_sync( + pwm_proxy, + dev->device, + dev->channel, + period, + &ret, + NULL, + &error) == FALSE) { + _E("Error in %s() : %s\n", __func__, error->message); + g_error_free(error); + return PERIPHERAL_ERROR_UNKNOWN; + } + + return ret; +} + +int peripheral_dbus_pwm_set_period(peripheral_pwm_context_h dev, int period) +{ + GError *error = NULL; + gint32 ret = PERIPHERAL_ERROR_NONE; + + if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + /* TODO: Need to reorganize arguments */ + if (peripheral_io_gdbus_pwm_call_set_period_sync( + pwm_proxy, + dev->device, + dev->channel, + period, + &ret, + NULL, + &error) == FALSE) { + _E("Error in %s() : %s\n", __func__, error->message); + g_error_free(error); + return PERIPHERAL_ERROR_UNKNOWN; + } + + return ret; +} + +int peripheral_dbus_pwm_set_enable(peripheral_pwm_context_h dev, peripheral_pwm_state_e enable) +{ + GError *error = NULL; + gint32 ret = PERIPHERAL_ERROR_NONE; + + if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + /* TODO: Need to reorganize arguments */ + if (peripheral_io_gdbus_pwm_call_set_enable_sync( + pwm_proxy, + dev->device, + dev->channel, + enable, + &ret, + NULL, + &error) == FALSE) { + _E("Error in %s() : %s\n", __func__, error->message); + g_error_free(error); + return PERIPHERAL_ERROR_UNKNOWN; + } return ret; } diff --git a/src/peripheral_gpio.c b/src/peripheral_gpio.c index 074ee17..5520c87 100644 --- a/src/peripheral_gpio.c +++ b/src/peripheral_gpio.c @@ -23,13 +23,11 @@ #include "peripheral_dbus.h" #include "peripheral_common.h" #include "peripheral_internal.h" +#include "peripheral_io_gdbus.h" /** * @brief Initializes(export) gpio pin and creates gpio handle. */ - -#define GPIO_NAME "gpio" - int peripheral_gpio_open(int gpio_pin, peripheral_gpio_h *gpio) { int ret = PERIPHERAL_ERROR_NONE; @@ -46,15 +44,10 @@ int peripheral_gpio_open(int gpio_pin, peripheral_gpio_h *gpio) } handle->pin = gpio_pin; - if (!get_dbus_connection()) { - ret = set_dbus_connection(); - if (ret != PERIPHERAL_ERROR_NONE) - goto exit; - } + gpio_proxy_init(); - ret = peripheral_dbus_gpio(handle, GPIO_NAME, "OPEN", 0 , 0); + ret = peripheral_dbus_gpio_open(handle); -exit: if (ret != PERIPHERAL_ERROR_NONE) { free(handle); handle = NULL; @@ -78,9 +71,10 @@ int peripheral_gpio_close(peripheral_gpio_h gpio) return PERIPHERAL_ERROR_INVALID_PARAMETER; /* call gpio_close */ - ret = peripheral_dbus_gpio(gpio, GPIO_NAME, "CLOSE", 0 , 0); + ret = peripheral_dbus_gpio_close(gpio); if (ret) ret = TIZEN_ERROR_IO_ERROR; + gpio_proxy_deinit(); free(gpio); gpio = NULL; @@ -99,10 +93,9 @@ int peripheral_gpio_get_direction(peripheral_gpio_h gpio, peripheral_gpio_direct if (gpio == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; - ret = peripheral_dbus_gpio(gpio, GPIO_NAME, "GET_DIR", 0 , 0); - + ret = peripheral_dbus_gpio_get_direction(gpio, direction); if (ret == PERIPHERAL_ERROR_NONE) - (*direction) = gpio->direction; + gpio->direction = (*direction); return ret; } @@ -119,15 +112,13 @@ int peripheral_gpio_set_direction(peripheral_gpio_h gpio, peripheral_gpio_direct if (gpio == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; - if (direction > PERIPHERAL_GPIO_DIRECTION_OUT_HIGH) { - ret = PERIPHERAL_ERROR_INVALID_PARAMETER; - } else { - if (gpio->direction != direction) { - gpio->direction = direction; - ret = peripheral_dbus_gpio(gpio, GPIO_NAME, "SET_DIR", 0 , 0); - } - } + if (direction > PERIPHERAL_GPIO_DIRECTION_OUT_HIGH) + return PERIPHERAL_ERROR_INVALID_PARAMETER; + /* call gpio_set_direction */ + ret = peripheral_dbus_gpio_set_direction(gpio, direction); + if (ret == PERIPHERAL_ERROR_NONE) + gpio->direction = direction; return ret; } @@ -135,9 +126,8 @@ int peripheral_gpio_set_direction(peripheral_gpio_h gpio, peripheral_gpio_direct /** * @brief Reads value of the gpio. */ -int peripheral_gpio_read(peripheral_gpio_h gpio, int *val) +int peripheral_gpio_read(peripheral_gpio_h gpio, int *value) { - int value = 0; int ret = PERIPHERAL_ERROR_NONE; /* check validation of gpio context handle */ @@ -145,8 +135,7 @@ int peripheral_gpio_read(peripheral_gpio_h gpio, int *val) return PERIPHERAL_ERROR_INVALID_PARAMETER; /* call gpio_read */ - ret = peripheral_dbus_gpio(gpio, GPIO_NAME, "READ", 0, &value); - *val = value; + ret = peripheral_dbus_gpio_read(gpio, value); return ret; } @@ -162,11 +151,8 @@ int peripheral_gpio_write(peripheral_gpio_h gpio, int value) if (gpio == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; - ret = peripheral_dbus_gpio(gpio, GPIO_NAME, "WRITE", value , 0); /* call gpio_write */ - - if (ret != PERIPHERAL_ERROR_NONE) - return ret; + ret = peripheral_dbus_gpio_write(gpio, value); return ret; } @@ -182,10 +168,9 @@ int peripheral_gpio_get_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e if (gpio == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; - ret = peripheral_dbus_gpio(gpio, GPIO_NAME, "GET_EDGE", 0 , 0); - + ret = peripheral_dbus_gpio_get_edge_mode(gpio, edge); if (ret == PERIPHERAL_ERROR_NONE) - (*edge) = gpio->edge; + gpio->edge = (*edge); return ret; } @@ -201,15 +186,13 @@ int peripheral_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e if (gpio == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; - if (edge > PERIPHERAL_GPIO_EDGE_BOTH) { - ret = PERIPHERAL_ERROR_INVALID_PARAMETER; - } else { - if (gpio->edge != edge) { - gpio->edge = edge; - ret = peripheral_dbus_gpio(gpio, GPIO_NAME, "SET_EDGE", 0 , 0); - } - } + if (edge > PERIPHERAL_GPIO_EDGE_BOTH) + return PERIPHERAL_ERROR_INVALID_PARAMETER; + /* call gpio_set_edge_mode */ + ret = peripheral_dbus_gpio_set_edge_mode(gpio, edge); + if (ret == PERIPHERAL_ERROR_NONE) + gpio->edge = edge; return ret; } diff --git a/src/peripheral_i2c.c b/src/peripheral_i2c.c index a63e6f8..2f1981d 100644 --- a/src/peripheral_i2c.c +++ b/src/peripheral_i2c.c @@ -24,13 +24,6 @@ #include "peripheral_common.h" #include "peripheral_internal.h" -#ifdef __cplusplus -extern "C" { -#endif - -#define I2C_NAME "i2c" -int I2C_Addr = 0; - int peripheral_i2c_init(int bus, peripheral_i2c_h *i2c) { peripheral_i2c_h handle; @@ -47,15 +40,10 @@ int peripheral_i2c_init(int bus, peripheral_i2c_h *i2c) return PERIPHERAL_ERROR_OUT_OF_MEMORY; } - if (!get_dbus_connection()) { - ret = set_dbus_connection(); - if (ret != PERIPHERAL_ERROR_NONE) - goto exit; - } + i2c_proxy_init(); - ret = peripheral_dbus_i2c(handle, I2C_NAME, "INIT", bus, 0, I2C_Addr); + ret = peripheral_dbus_i2c_init(handle, bus); -exit: if (ret != PERIPHERAL_ERROR_NONE) { _E("[PERIPHERAL] I2C init error\n"); free(handle); @@ -69,38 +57,43 @@ exit: int peripheral_i2c_stop(peripheral_i2c_h i2c) { int ret = PERIPHERAL_ERROR_NONE; - /* Free peripheral_i2c_h */ - if (i2c != NULL) { - ret = peripheral_dbus_i2c(i2c, I2C_NAME, "STOP", 0, 0, I2C_Addr); + if (i2c == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; - free(i2c); - i2c = NULL; - } + ret = peripheral_dbus_i2c_stop(i2c); + gpio_proxy_deinit(); + + free(i2c); + i2c = NULL; return ret; } int peripheral_i2c_set_address(peripheral_i2c_h i2c, int address) { - /* Set the i2c slave address */ + if (i2c == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; - //I2C_Addr = address; - return peripheral_dbus_i2c(i2c, I2C_NAME, "SET_ADDR", address, 0, I2C_Addr); + return peripheral_dbus_i2c_set_address(i2c, address); } int peripheral_i2c_read(peripheral_i2c_h i2c, uint8_t *data, int length) { - /* Read i2c data */ - return peripheral_dbus_i2c(i2c, I2C_NAME, "READ", length, data, I2C_Addr); + int ret = PERIPHERAL_ERROR_NONE; + + if (i2c == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + + ret = peripheral_dbus_i2c_read(i2c, data, length); + /* + _D("I2C read data : "); + for (int i = 0 ; i < length ; i++) + _D("[%02x]", data[i]); + */ + return ret; } int peripheral_i2c_write(peripheral_i2c_h i2c, uint8_t *data, int length) { - /* Write i2c data */ - return peripheral_dbus_i2c(i2c, I2C_NAME, "WRITE", length, data, I2C_Addr); -} + if (i2c == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; -#ifdef __cplusplus + return peripheral_dbus_i2c_write(i2c, data, length); } -#endif diff --git a/src/peripheral_io.xml b/src/peripheral_io.xml new file mode 100644 index 0000000..07a0067 --- /dev/null +++ b/src/peripheral_io.xml @@ -0,0 +1,117 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/peripheral_pwm.c b/src/peripheral_pwm.c index 3d6e950..d4d728a 100644 --- a/src/peripheral_pwm.c +++ b/src/peripheral_pwm.c @@ -23,14 +23,9 @@ #include "peripheral_dbus.h" #include "peripheral_common.h" -#define PWM_NAME "pwm" #define PWM_ENABLE 1 #define PWM_DISABLE 0 -#ifdef __cplusplus -extern "C" { -#endif - peripheral_pwm_context_h peripheral_pwm_open(int device, int channel) { peripheral_pwm_context_h dev = NULL; @@ -47,13 +42,12 @@ peripheral_pwm_context_h peripheral_pwm_open(int device, int channel) return NULL; } - if (!get_dbus_connection()) - set_dbus_connection(); + pwm_proxy_init(); dev->device = device; dev->channel = channel; - ret = peripheral_dbus_pwm(dev, PWM_NAME, "OPEN"); + ret = peripheral_dbus_pwm_open(dev, device, channel); if (ret != PERIPHERAL_ERROR_NONE) { free(dev); @@ -67,7 +61,8 @@ int peripheral_pwm_close(peripheral_pwm_context_h pwm) { int ret = PERIPHERAL_ERROR_NONE; - ret = peripheral_dbus_pwm(pwm, PWM_NAME, "CLOSE"); + ret = peripheral_dbus_pwm_close(pwm); + pwm_proxy_deinit(); if (ret == PERIPHERAL_ERROR_NONE) { free(pwm); @@ -82,16 +77,10 @@ int peripheral_pwm_set_duty_cycle(peripheral_pwm_context_h pwm, int duty_cycle) { int ret = PERIPHERAL_ERROR_NONE; - if (pwm->duty_cycle != duty_cycle) { - int duty_value = 0; + ret = peripheral_dbus_pwm_set_duty_cycle(pwm, duty_cycle); - duty_value = pwm->duty_cycle; + if (ret != PERIPHERAL_ERROR_NONE) pwm->duty_cycle = duty_cycle; - ret = peripheral_dbus_pwm(pwm, PWM_NAME, "SET_DUTY"); - - if (ret != PERIPHERAL_ERROR_NONE) - pwm->duty_cycle = duty_value; - } return ret; } @@ -100,16 +89,10 @@ int peripheral_pwm_set_period(peripheral_pwm_context_h pwm, int period) { int ret = PERIPHERAL_ERROR_NONE; - if (pwm->period != period) { - int period_value = 0; + ret = peripheral_dbus_pwm_set_period(pwm, period); - period_value = pwm->period; + if (ret != PERIPHERAL_ERROR_NONE) pwm->period = period; - ret = peripheral_dbus_pwm(pwm, PWM_NAME, "SET_PERIOD"); - - if (ret != PERIPHERAL_ERROR_NONE) - pwm->period = period_value; - } return ret; } @@ -118,16 +101,10 @@ int peripheral_pwm_set_enabled(peripheral_pwm_context_h pwm, peripheral_pwm_stat { int ret = PERIPHERAL_ERROR_NONE; - if (pwm->enabled != enable) { - int enable_value = 0; + ret = peripheral_dbus_pwm_set_enable(pwm, enable); - enable_value = pwm->enabled; + if (ret != PERIPHERAL_ERROR_NONE) pwm->enabled = enable; - ret = peripheral_dbus_pwm(pwm, PWM_NAME, "SET_ENABLE"); - - if (ret != PERIPHERAL_ERROR_NONE) - pwm->enabled = enable_value; - } return PERIPHERAL_ERROR_NONE; } @@ -138,20 +115,16 @@ int peripheral_pwm_is_enabled(peripheral_pwm_context_h pwm) return PWM_ENABLE; else return PWM_DISABLE; - } int peripheral_pwm_get_duty_cycle(peripheral_pwm_context_h pwm, int *duty_cycle) { - int duty_value = 0; int ret = PERIPHERAL_ERROR_NONE; - duty_value = pwm->duty_cycle; - - ret = peripheral_dbus_pwm(pwm, PWM_NAME, "GET_DUTY"); + ret = peripheral_dbus_pwm_get_duty_cycle(pwm, duty_cycle); - (*duty_cycle) = pwm->duty_cycle; - pwm->duty_cycle = duty_value; + if (ret != PERIPHERAL_ERROR_NONE) + pwm->duty_cycle = *duty_cycle; return ret; } @@ -159,18 +132,11 @@ int peripheral_pwm_get_duty_cycle(peripheral_pwm_context_h pwm, int *duty_cycle) int peripheral_pwm_get_period(peripheral_pwm_context_h pwm, int *period) { int ret = PERIPHERAL_ERROR_NONE; - int period_value = 0; - period_value = pwm->period; + ret = peripheral_dbus_pwm_get_period(pwm, period); - ret = peripheral_dbus_pwm(pwm, PWM_NAME, "GET_PERIOD"); - - (*period) = pwm->period; - pwm->period = period_value; + if (ret != PERIPHERAL_ERROR_NONE) + pwm->period = *period; return ret; } - -#ifdef __cplusplus -} -#endif