From 3f67fb53729f3c9996e2d669d22389e276819ab2 Mon Sep 17 00:00:00 2001 From: Hyeongsik Min Date: Thu, 20 Apr 2017 11:05:08 +0900 Subject: [PATCH 01/16] Return i2c handle through pointer argument Change-Id: If65c0330fcd61edafea318018144d463fa1d559f Signed-off-by: Hyeongsik Min --- include/peripheral_io.h | 2 +- src/peripheral_i2c.c | 30 ++++++++++++++++++------------ test/peripheral-io-test.c | 14 +++++++------- 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/include/peripheral_io.h b/include/peripheral_io.h index 44c9b59..4458ae6 100644 --- a/include/peripheral_io.h +++ b/include/peripheral_io.h @@ -288,7 +288,7 @@ int peripheral_gpio_get_pin(peripheral_gpio_h gpio, int *gpio_pin); typedef struct _peripheral_i2c_s *peripheral_i2c_h; -peripheral_i2c_h peripheral_i2c_init(int bus); +int peripheral_i2c_init(int bus, peripheral_i2c_h *i2c); int peripheral_i2c_stop(peripheral_i2c_h i2c); diff --git a/src/peripheral_i2c.c b/src/peripheral_i2c.c index 35c5d06..a63e6f8 100644 --- a/src/peripheral_i2c.c +++ b/src/peripheral_i2c.c @@ -31,33 +31,39 @@ extern "C" { #define I2C_NAME "i2c" int I2C_Addr = 0; -peripheral_i2c_h peripheral_i2c_init(int bus) +int peripheral_i2c_init(int bus, peripheral_i2c_h *i2c) { - peripheral_i2c_h i2c; + peripheral_i2c_h handle; int ret = PERIPHERAL_ERROR_NONE; - assert(bus >= 0); + if (bus < 0) + return PERIPHERAL_ERROR_INVALID_PARAMETER; /* Initialize peripheral_i2c_h */ - i2c = (peripheral_i2c_h)malloc(sizeof(struct _peripheral_i2c_s)); + handle = (peripheral_i2c_h)malloc(sizeof(struct _peripheral_i2c_s)); - if (i2c == NULL) { + if (handle == NULL) { _E("Failed to allocate peripheral_i2c_h"); - return NULL; + return PERIPHERAL_ERROR_OUT_OF_MEMORY; } - if (!get_dbus_connection()) - set_dbus_connection(); + if (!get_dbus_connection()) { + ret = set_dbus_connection(); + if (ret != PERIPHERAL_ERROR_NONE) + goto exit; + } - ret = peripheral_dbus_i2c(i2c, I2C_NAME, "INIT", bus, 0, I2C_Addr); + ret = peripheral_dbus_i2c(handle, I2C_NAME, "INIT", bus, 0, I2C_Addr); +exit: if (ret != PERIPHERAL_ERROR_NONE) { - free(i2c); _E("[PERIPHERAL] I2C init error\n"); - i2c = NULL; + free(handle); + handle = NULL; } + *i2c = handle; - return i2c; + return ret; } int peripheral_i2c_stop(peripheral_i2c_h i2c) diff --git a/test/peripheral-io-test.c b/test/peripheral-io-test.c index 857fb66..c9b4609 100644 --- a/test/peripheral-io-test.c +++ b/test/peripheral-io-test.c @@ -78,24 +78,24 @@ int i2c_test(void) int cnt = 0; int bus_num; unsigned char buf[10]; - peripheral_i2c_h dev; + peripheral_i2c_h i2c; printf(">> I2C bus number : "); if (scanf("%d", &bus_num) < 0) return 0; - if ((dev = peripheral_i2c_init(bus_num)) == NULL) { + if ((peripheral_i2c_init(bus_num, &i2c)) != 0) { printf("Failed to initialize I2C device\n"); return 0; } - if (peripheral_i2c_set_address(dev, GY30_ADDR) != 0) { + if (peripheral_i2c_set_address(i2c, GY30_ADDR) != 0) { printf("Failed to set address\n"); goto error; } buf[0] = GY30_CONT_HIGH_RES_MODE; - if (peripheral_i2c_write(dev, buf, 1) != 0) { + if (peripheral_i2c_write(i2c, buf, 1) != 0) { printf("Failed to write\n"); goto error; } @@ -103,16 +103,16 @@ int i2c_test(void) while (cnt++ < 15) { int result; sleep(1); - peripheral_i2c_read(dev, buf, 2); + peripheral_i2c_read(i2c, buf, 2); result = GY30_READ_INTENSITY(buf); printf("Result [%d]\n", result); } - peripheral_i2c_stop(dev); + peripheral_i2c_stop(i2c); return 1; error: - peripheral_i2c_stop(dev); + peripheral_i2c_stop(i2c); return 0; } -- 2.34.1 From a0b47e9c274f34a3ea4bf4bc479c6e18e4788f4e Mon Sep 17 00:00:00 2001 From: Sungguk Na Date: Fri, 21 Apr 2017 19:49:11 +0900 Subject: [PATCH 02/16] Change gdbus interface by using gdbus-codegen Change-Id: I8706817d7ad17952823fae1d1475b4dc1570f3e1 Signed-off-by: Sungguk Na --- CMakeLists.txt | 12 +- include/peripheral_dbus.h | 36 ++- src/peripheral_dbus.c | 577 +++++++++++++++++++++++++++++++------- src/peripheral_gpio.c | 65 ++--- src/peripheral_i2c.c | 53 ++-- src/peripheral_io.xml | 117 ++++++++ src/peripheral_pwm.c | 66 ++--- 7 files changed, 701 insertions(+), 225 deletions(-) create mode 100644 src/peripheral_io.xml 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 -- 2.34.1 From 1b2171a5bfe6829d0b202c070d539b616b79d187 Mon Sep 17 00:00:00 2001 From: Hyeongsik Min Date: Mon, 24 Apr 2017 13:11:26 +0900 Subject: [PATCH 03/16] Fix gdbus interface prefix and typo in xml Changed gdbus interface prefix to 'org.tizen.peripheral_io'. Change-Id: Iab717d474a9c01011fe08268999d7849433849b4 Signed-off-by: Hyeongsik Min --- CMakeLists.txt | 2 +- include/peripheral_dbus.h | 12 ++++++------ src/peripheral_io.xml | 22 +++++++++++----------- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c9d951a..45ec9ac 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,7 +21,7 @@ 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. \\ + --interface-prefix org.tizen.peripheral_io. \\ ${CMAKE_SOURCE_DIR}/src/peripheral_io.xml \\ ") diff --git a/include/peripheral_dbus.h b/include/peripheral_dbus.h index d21fb2a..557b296 100644 --- a/include/peripheral_dbus.h +++ b/include/peripheral_dbus.h @@ -18,12 +18,12 @@ #include -#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_DBUS_INTERFACE "org.tizen.peripheral_io" +#define PERIPHERAL_DBUS_PATH "/Org/Tizen/Peripheral_io" +#define PERIPHERAL_DBUS_GPIO_PATH "/Org/Tizen/Peripheral_io/Gpio" +#define PERIPHERAL_DBUS_I2C_PATH "/Org/Tizen/Peripheral_io/I2c" +#define PERIPHERAL_DBUS_PWM_PATH "/Org/Tizen/Peripheral_io/Pwm" +#define PERIPHERAL_DBUS_NAME "org.tizen.peripheral_io" #define PERIPHERAL_METHOD_GPIO "gpio" #define PERIPHERAL_METHOD_I2C "i2c" diff --git a/src/peripheral_io.xml b/src/peripheral_io.xml index 07a0067..40672ad 100644 --- a/src/peripheral_io.xml +++ b/src/peripheral_io.xml @@ -1,46 +1,46 @@ - + - + - + - + - + - + - + - + - + - + @@ -72,7 +72,7 @@ - + -- 2.34.1 From 3fe9637e30d9ed38407a95f29eb47be03369410e Mon Sep 17 00:00:00 2001 From: "jino.cho" Date: Tue, 25 Apr 2017 18:10:09 +0900 Subject: [PATCH 04/16] Add parameters to the gpio open method - add parameters to the gpio open method. - update the gpio handle when the gpio attributes are changed. Change-Id: I2c2ba69605bcc85c65beeb512c32a4b5d1453f18 Signed-off-by: jino.cho --- src/peripheral_dbus.c | 6 ++++++ src/peripheral_io.xml | 2 ++ 2 files changed, 8 insertions(+) diff --git a/src/peripheral_dbus.c b/src/peripheral_dbus.c index 67c7025..3c32644 100644 --- a/src/peripheral_dbus.c +++ b/src/peripheral_dbus.c @@ -101,6 +101,8 @@ int peripheral_dbus_gpio_open(peripheral_gpio_h gpio) if (peripheral_io_gdbus_gpio_call_open_sync( gpio_proxy, gpio->pin, + (gint*)&gpio->edge, + (gint*)&gpio->direction, &ret, NULL, &error) == FALSE) { @@ -151,6 +153,7 @@ int peripheral_dbus_gpio_get_direction(peripheral_gpio_h gpio, peripheral_gpio_d g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } + gpio->direction = *direction; return ret; } @@ -173,6 +176,7 @@ int peripheral_dbus_gpio_set_direction(peripheral_gpio_h gpio, peripheral_gpio_d g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } + gpio->direction = direction; return ret; } @@ -239,6 +243,7 @@ int peripheral_dbus_gpio_get_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_e g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } + gpio->edge = *edge; return ret; } @@ -261,6 +266,7 @@ int peripheral_dbus_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_e g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } + gpio->edge = edge; return ret; } diff --git a/src/peripheral_io.xml b/src/peripheral_io.xml index 40672ad..a99cbbc 100644 --- a/src/peripheral_io.xml +++ b/src/peripheral_io.xml @@ -3,6 +3,8 @@ + + -- 2.34.1 From 541a12a30d38f2b857f9204eb78c63b355522fec Mon Sep 17 00:00:00 2001 From: Hyeongsik Min Date: Tue, 25 Apr 2017 14:13:52 +0900 Subject: [PATCH 05/16] Remove i2c_set_address() API and rearrange header - i2c_open() will pass bus and address argument together. - Add description for i2c APIs and fix typo. Change-Id: I1313fbf1fae23d828e1ac001d20f880747404109 Signed-off-by: Hyeongsik Min --- include/peripheral_dbus.h | 6 +-- include/peripheral_io.h | 85 +++++++++++++++++++++++++++++++++------ src/peripheral_dbus.c | 34 +++------------- src/peripheral_i2c.c | 16 ++------ src/peripheral_io.xml | 10 ++--- test/peripheral-io-test.c | 13 ++---- 6 files changed, 91 insertions(+), 73 deletions(-) diff --git a/include/peripheral_dbus.h b/include/peripheral_dbus.h index 557b296..529128d 100644 --- a/include/peripheral_dbus.h +++ b/include/peripheral_dbus.h @@ -39,7 +39,6 @@ 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_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); @@ -49,9 +48,8 @@ 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_open(peripheral_i2c_h i2c, int bus, int address); +int peripheral_dbus_i2c_close(peripheral_i2c_h i2c); 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); diff --git a/include/peripheral_io.h b/include/peripheral_io.h index 4458ae6..6c62c1f 100644 --- a/include/peripheral_io.h +++ b/include/peripheral_io.h @@ -37,7 +37,7 @@ typedef enum { PERIPHERAL_ERROR_NONE = TIZEN_ERROR_NONE, /**< Successful */ PERIPHERAL_ERROR_IO_ERROR = TIZEN_ERROR_IO_ERROR, /**< I/O error */ PERIPHERAL_ERROR_OUT_OF_MEMORY = TIZEN_ERROR_OUT_OF_MEMORY, /**< Out of memory */ - PERIPHERAL_ERROR_PERMISSON_DENIED = TIZEN_ERROR_PERMISSION_DENIED, /**< Permission denied */ + PERIPHERAL_ERROR_PERMISSION_DENIED = TIZEN_ERROR_PERMISSION_DENIED, /**< Permission denied */ PERIPHERAL_ERROR_RESOURCE_BUSY = TIZEN_ERROR_RESOURCE_BUSY, /**< Device or resource busy */ PERIPHERAL_ERROR_INVALID_PARAMETER = TIZEN_ERROR_INVALID_PARAMETER, /**< Invalid parameter */ PERIPHERAL_ERROR_NO_DATA = TIZEN_ERROR_NO_DATA, /**< No data available */ @@ -49,7 +49,7 @@ typedef enum { } peripheral_error_e; /** - * @addtogroup CAPI_SYSTEM_PERPHERAL_GPIO_MODULE + * @addtogroup CAPI_SYSTEM_PERIPHERAL_GPIO_MODULE * @{ */ @@ -90,7 +90,7 @@ typedef struct _peripheral_gpio_s* peripheral_gpio_h; * @retval #PERIPHERAL_ERROR_NONE Successful * @retval #PERIPHERAL_ERROR_IO_ERROR I/O operation failed * @retval #PERIPHERAL_ERROR_OUT_OF_MEMORY Memory allocation failed - * @retval #PERIPHERAL_ERROR_PERMISSON_DENIED Permission denied + * @retval #PERIPHERAL_ERROR_PERMISSION_DENIED Permission denied * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed * @@ -282,20 +282,81 @@ int peripheral_gpio_get_pin(peripheral_gpio_h gpio, int *gpio_pin); */ /** - * @addtogroup CAPI_SYSTEM_PERPHERAL_I2C_MODULE + * @addtogroup CAPI_SYSTEM_PERIPHERAL_I2C_MODULE * @{ */ +/** + * @brief The handle to the i2c device + * @since_tizen 4.0 + */ typedef struct _peripheral_i2c_s *peripheral_i2c_h; -int peripheral_i2c_init(int bus, peripheral_i2c_h *i2c); - -int peripheral_i2c_stop(peripheral_i2c_h i2c); +/** + * @brief Initializes i2c communication and creates i2c handle. + * @since_tizen 4.0 + * + * @param[in] bus The i2c bus number that the slave device is connected + * @param[in] address The address of the slave device + * @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_OUT_OF_MEMORY Memory allocation failed + * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error + * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed + * + * @see peripheral_i2c_close() + */ +int peripheral_i2c_open(int bus, int address, peripheral_i2c_h *i2c); -int peripheral_i2c_set_address(peripheral_i2c_h i2c, int address); +/** + * @brief Destory the i2c handle and release the communication. + * @since_tizen 4.0 + * + * @param[in] i2c The i2c handle + * + * @return 0 on success, otherwise a negative error value + * @retval #PERIPHERAL_ERROR_NONE Successful + * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error + * + * @see peripheral_i2c_open() + */ +int peripheral_i2c_close(peripheral_i2c_h i2c); +/** + * @brief Reads data from the i2c device. + * @since_tizen 4.0 + * + * @param[in] i2c The handle to the i2c device + * @param[in, out] data The address of read buffer + * @param[in] length The size of data buffer (in bytes) + * + * @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_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error + */ int peripheral_i2c_read(peripheral_i2c_h i2c, uint8_t *data, int length); +/** + * @brief Write data to the i2c device. + * @since_tizen 4.0 + * + * @param[in] i2c The handle to the i2c device + * @param[in, out] data The address of buffer to write + * @param[in] length The size of data buffer (in bytes) + * + * @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_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error + */ int peripheral_i2c_write(peripheral_i2c_h i2c, uint8_t *data, int length); @@ -304,7 +365,7 @@ int peripheral_i2c_write(peripheral_i2c_h i2c, uint8_t *data, int length); */ /** - * @addtogroup CAPI_SYSTEM_PERPHERAL_PWM_MODULE + * @addtogroup CAPI_SYSTEM_PERIPHERAL_PWM_MODULE * @{ */ @@ -344,7 +405,7 @@ int peripheral_pwm_get_period(peripheral_pwm_context_h pwm, int *period); */ /** - * @addtogroup CAPI_SYSTEM_PERPHERAL_ADC_MODULE + * @addtogroup CAPI_SYSTEM_PERIPHERAL_ADC_MODULE * @{ */ @@ -375,7 +436,7 @@ int peripheral_adc_close(peripheral_adc_context_h dev); */ /** - * @addtogroup CAPI_SYSTEM_PERPHERAL_UART_MODULE + * @addtogroup CAPI_SYSTEM_PERIPHERAL_UART_MODULE * @{ */ struct _peripheral_uart_s { @@ -412,7 +473,7 @@ int peripheral_uart_write(peripheral_uart_context_h hnd, const char *buf, unsign */ /** - * @addtogroup CAPI_SYSTEM_PERPHERAL_SPI_MODULE + * @addtogroup CAPI_SYSTEM_PERIPHERAL_SPI_MODULE * @{ */ diff --git a/src/peripheral_dbus.c b/src/peripheral_dbus.c index 3c32644..9cf8af5 100644 --- a/src/peripheral_dbus.c +++ b/src/peripheral_dbus.c @@ -271,16 +271,17 @@ int peripheral_dbus_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_e return ret; } -int peripheral_dbus_i2c_init(peripheral_i2c_h i2c, int bus) +int peripheral_dbus_i2c_open(peripheral_i2c_h i2c, int bus, 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_init_sync( + if (peripheral_io_gdbus_i2c_call_open_sync( i2c_proxy, bus, + address, &i2c->fd, &ret, NULL, @@ -293,38 +294,16 @@ int peripheral_dbus_i2c_init(peripheral_i2c_h i2c, int bus) return ret; } -int peripheral_dbus_i2c_stop(peripheral_i2c_h i2c) -{ - GError *error = NULL; - 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) +int peripheral_dbus_i2c_close(peripheral_i2c_h i2c) { 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( + if (peripheral_io_gdbus_i2c_call_close_sync( i2c_proxy, i2c->fd, - address, &ret, NULL, &error) == FALSE) { @@ -333,9 +312,6 @@ int peripheral_dbus_i2c_set_address(peripheral_i2c_h i2c, int address) return PERIPHERAL_ERROR_UNKNOWN; } - if (ret != PERIPHERAL_ERROR_NONE) - _E("%s failed, ret = %d", __func__, ret); - return ret; } diff --git a/src/peripheral_i2c.c b/src/peripheral_i2c.c index 2f1981d..23a33f0 100644 --- a/src/peripheral_i2c.c +++ b/src/peripheral_i2c.c @@ -24,7 +24,7 @@ #include "peripheral_common.h" #include "peripheral_internal.h" -int peripheral_i2c_init(int bus, peripheral_i2c_h *i2c) +int peripheral_i2c_open(int bus, int address, peripheral_i2c_h *i2c) { peripheral_i2c_h handle; int ret = PERIPHERAL_ERROR_NONE; @@ -32,7 +32,6 @@ int peripheral_i2c_init(int bus, peripheral_i2c_h *i2c) if (bus < 0) return PERIPHERAL_ERROR_INVALID_PARAMETER; - /* Initialize peripheral_i2c_h */ handle = (peripheral_i2c_h)malloc(sizeof(struct _peripheral_i2c_s)); if (handle == NULL) { @@ -42,7 +41,7 @@ int peripheral_i2c_init(int bus, peripheral_i2c_h *i2c) i2c_proxy_init(); - ret = peripheral_dbus_i2c_init(handle, bus); + ret = peripheral_dbus_i2c_open(handle, bus, address); if (ret != PERIPHERAL_ERROR_NONE) { _E("[PERIPHERAL] I2C init error\n"); @@ -54,13 +53,13 @@ int peripheral_i2c_init(int bus, peripheral_i2c_h *i2c) return ret; } -int peripheral_i2c_stop(peripheral_i2c_h i2c) +int peripheral_i2c_close(peripheral_i2c_h i2c) { int ret = PERIPHERAL_ERROR_NONE; if (i2c == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; - ret = peripheral_dbus_i2c_stop(i2c); + ret = peripheral_dbus_i2c_close(i2c); gpio_proxy_deinit(); free(i2c); @@ -69,13 +68,6 @@ int peripheral_i2c_stop(peripheral_i2c_h i2c) return ret; } -int peripheral_i2c_set_address(peripheral_i2c_h i2c, int address) -{ - if (i2c == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; - - return peripheral_dbus_i2c_set_address(i2c, address); -} - int peripheral_i2c_read(peripheral_i2c_h i2c, uint8_t *data, int length) { int ret = PERIPHERAL_ERROR_NONE; diff --git a/src/peripheral_io.xml b/src/peripheral_io.xml index a99cbbc..cbda49b 100644 --- a/src/peripheral_io.xml +++ b/src/peripheral_io.xml @@ -43,18 +43,14 @@ - + + - - - - - + - diff --git a/test/peripheral-io-test.c b/test/peripheral-io-test.c index c9b4609..048d136 100644 --- a/test/peripheral-io-test.c +++ b/test/peripheral-io-test.c @@ -84,16 +84,11 @@ int i2c_test(void) if (scanf("%d", &bus_num) < 0) return 0; - if ((peripheral_i2c_init(bus_num, &i2c)) != 0) { - printf("Failed to initialize I2C device\n"); + if ((peripheral_i2c_open(bus_num, GY30_ADDR, &i2c)) != 0) { + printf("Failed to open I2C communication\n"); return 0; } - if (peripheral_i2c_set_address(i2c, GY30_ADDR) != 0) { - printf("Failed to set address\n"); - goto error; - } - buf[0] = GY30_CONT_HIGH_RES_MODE; if (peripheral_i2c_write(i2c, buf, 1) != 0) { printf("Failed to write\n"); @@ -108,11 +103,11 @@ int i2c_test(void) printf("Result [%d]\n", result); } - peripheral_i2c_stop(i2c); + peripheral_i2c_close(i2c); return 1; error: - peripheral_i2c_stop(i2c); + peripheral_i2c_close(i2c); return 0; } -- 2.34.1 From c311eda10558d36aa456172808323daa3f058975 Mon Sep 17 00:00:00 2001 From: Hyeongsik Min Date: Tue, 25 Apr 2017 14:26:16 +0900 Subject: [PATCH 06/16] Update package version to 0.0.3 Change-Id: I81b41fb565e5b83c2e680e892503e44b033ab200 Signed-off-by: Hyeongsik Min --- packaging/capi-system-peripheral-io.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/capi-system-peripheral-io.spec b/packaging/capi-system-peripheral-io.spec index 3aa5b57..91a3634 100644 --- a/packaging/capi-system-peripheral-io.spec +++ b/packaging/capi-system-peripheral-io.spec @@ -1,6 +1,6 @@ Name: capi-system-peripheral-io Summary: Tizen Peripheral Input & Output library -Version: 0.0.1 +Version: 0.0.3 Release: 0 Group: System & System Tools License: Apache-2.0 -- 2.34.1 From 8950dfd59f83e6df465786c5a841a8a7cef78f4a Mon Sep 17 00:00:00 2001 From: "jino.cho" Date: Thu, 27 Apr 2017 10:54:04 +0900 Subject: [PATCH 07/16] Decrease the reference count of the dbus proxy The resource will be destroyed when its reference count drops to zero. Change-Id: Ibaca7da33eb6538b9c1928c5b8ea2b06eaf03245 Signed-off-by: jino.cho --- src/peripheral_dbus.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/peripheral_dbus.c b/src/peripheral_dbus.c index 9cf8af5..e685b71 100644 --- a/src/peripheral_dbus.c +++ b/src/peripheral_dbus.c @@ -78,17 +78,26 @@ void pwm_proxy_init(void) void gpio_proxy_deinit() { - gpio_proxy = NULL; + if (gpio_proxy) { + g_object_unref(gpio_proxy); + gpio_proxy = NULL; + } } void i2c_proxy_deinit() { - i2c_proxy = NULL; + if (i2c_proxy) { + g_object_unref(i2c_proxy); + i2c_proxy = NULL; + } } void pwm_proxy_deinit() { - pwm_proxy = NULL; + if (pwm_proxy) { + g_object_unref(pwm_proxy); + pwm_proxy = NULL; + } } int peripheral_dbus_gpio_open(peripheral_gpio_h gpio) -- 2.34.1 From e9a0d6bce9e00a8049ed478b88f19196b7694dc9 Mon Sep 17 00:00:00 2001 From: Hyeongsik Min Date: Wed, 26 Apr 2017 20:50:23 +0900 Subject: [PATCH 08/16] Define condition checking macros(RET_IF) Change-Id: I5b39db783a6499c1c5429faa8a5777032e743306 Signed-off-by: Hyeongsik Min --- include/peripheral_common.h | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/include/peripheral_common.h b/include/peripheral_common.h index 1e5d88f..2d2f960 100644 --- a/include/peripheral_common.h +++ b/include/peripheral_common.h @@ -26,4 +26,33 @@ #define _D(fmt, arg...) LOGD(fmt, ##arg) #define _W(fmt, arg...) LOGW(fmt, ##arg) +#define RET_IF(expr) \ + do { \ + if (expr) { \ + _E("(%s)", #expr); \ + return; \ + }\ + } while(0) +#define RETV_IF(expr, val) \ + do {\ + if (expr) { \ + _E("(%s)", #expr); \ + return (val); \ + } \ + } while(0) +#define RETM_IF(expr, fmt, arg...) \ + do {\ + if (expr) { \ + _E(fmt, ##arg); \ + return; \ + }\ + } while(0) +#define RETVM_IF(expr, val, fmt, arg...) \ + do {\ + if (expr) { \ + _E(fmt, ##arg); \ + return (val); \ + } \ + } while(0) + #endif /* __PERIPHERAL_COMMON_H__ */ -- 2.34.1 From 1762a85eacbe1aa7f7e4cd82d0030d41793f7847 Mon Sep 17 00:00:00 2001 From: "jino.cho" Date: Thu, 20 Apr 2017 10:33:55 +0900 Subject: [PATCH 09/16] Support gpio interrupt The gpio interrupt should work properly if the patch for peripheral-bus is applied together. Change-Id: I1c7b0b9db563ea3b3fd222bb4b05b1dbc7b279c8 Signed-off-by: jino.cho --- include/peripheral_dbus.h | 2 + src/peripheral_dbus.c | 69 ++++++++++++++++++++++++ src/peripheral_gpio.c | 107 ++++++++++++++++++++++++++++++++++++-- src/peripheral_io.xml | 12 +++++ test/peripheral-io-test.c | 80 +++++++++++++++++++++++++++- 5 files changed, 264 insertions(+), 6 deletions(-) diff --git a/include/peripheral_dbus.h b/include/peripheral_dbus.h index 529128d..e1002fa 100644 --- a/include/peripheral_dbus.h +++ b/include/peripheral_dbus.h @@ -47,6 +47,8 @@ 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_gpio_register_cb(peripheral_gpio_h gpio, gpio_isr_cb callback, void *user_data); +int peripheral_dbus_gpio_unregister_cb(peripheral_gpio_h gpio); int peripheral_dbus_i2c_open(peripheral_i2c_h i2c, int bus, int address); int peripheral_dbus_i2c_close(peripheral_i2c_h i2c); diff --git a/src/peripheral_dbus.c b/src/peripheral_dbus.c index e685b71..3a61390 100644 --- a/src/peripheral_dbus.c +++ b/src/peripheral_dbus.c @@ -24,6 +24,9 @@ #include "peripheral_internal.h" #include "peripheral_io_gdbus.h" +extern int peripheral_gpio_isr_callback(int pin); +void handle_gpio_changed(PeripheralIoGdbusGpio *gpio, gint pin, gint state, gpointer user_data); + PeripheralIoGdbusGpio *gpio_proxy = NULL; PeripheralIoGdbusI2c *i2c_proxy = NULL; PeripheralIoGdbusPwm *pwm_proxy = NULL; @@ -42,6 +45,16 @@ void gpio_proxy_init(void) PERIPHERAL_DBUS_GPIO_PATH, NULL, &error); + if (gpio_proxy == NULL) { + _E("Can not create gpio proxy : %s", error->message); + g_error_free(error); + return; + } + + g_signal_connect(gpio_proxy, + "gpio-changed", + G_CALLBACK(handle_gpio_changed), + NULL); } void i2c_proxy_init(void) @@ -100,6 +113,20 @@ void pwm_proxy_deinit() } } +void handle_gpio_changed( + PeripheralIoGdbusGpio *gpio, + gint pin, + gint state, + gpointer user_data) +{ + if (!gpio) + return; + + _D("gpio=%d state=%d",pin, state); + + peripheral_gpio_isr_callback(pin); +} + int peripheral_dbus_gpio_open(peripheral_gpio_h gpio) { GError *error = NULL; @@ -280,6 +307,48 @@ int peripheral_dbus_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_e return ret; } +int peripheral_dbus_gpio_register_cb(peripheral_gpio_h gpio, gpio_isr_cb callback, void *user_data) +{ + GError *error = NULL; + peripheral_error_e ret = PERIPHERAL_ERROR_NONE; + + if (gpio_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + if (peripheral_io_gdbus_gpio_call_register_irq_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; + } + + return ret; +} + +int peripheral_dbus_gpio_unregister_cb(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_unregister_irq_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; + } + + return ret; +} + int peripheral_dbus_i2c_open(peripheral_i2c_h i2c, int bus, int address) { GError *error = NULL; diff --git a/src/peripheral_gpio.c b/src/peripheral_gpio.c index 5520c87..f942759 100644 --- a/src/peripheral_gpio.c +++ b/src/peripheral_gpio.c @@ -25,6 +25,88 @@ #include "peripheral_internal.h" #include "peripheral_io_gdbus.h" +typedef struct { + int pin; + gpio_isr_cb callback; + void *user_data; +} gpio_isr_data_s; + +static GList *gpio_isr_list = NULL; + +int peripheral_gpio_isr_callback(int pin) +{ + GList *link; + gpio_isr_data_s *isr_data; + + link = gpio_isr_list; + while (link) { + isr_data = (gpio_isr_data_s*)link->data; + + if (isr_data->pin == pin) { + if (isr_data->callback) + isr_data->callback(isr_data->user_data); + return PERIPHERAL_ERROR_NONE; + } + link = g_list_next(link); + } + + return PERIPHERAL_ERROR_NONE; +} + +int peripheral_gpio_isr_set(int pin, gpio_isr_cb callback, void *user_data) +{ + GList *link; + gpio_isr_data_s *isr_data = NULL; + + link = gpio_isr_list; + while (link) { + gpio_isr_data_s *tmp; + tmp = (gpio_isr_data_s*)link->data; + if (tmp->pin == pin) { + isr_data = tmp; + break; + } + link = g_list_next(link); + } + + if (isr_data == NULL) { + isr_data = (gpio_isr_data_s*)calloc(1, sizeof(gpio_isr_data_s)); + if (isr_data == NULL) { + _E("failed to allocate gpio_isr_data_s"); + return PERIPHERAL_ERROR_OUT_OF_MEMORY; + } + + gpio_isr_list = g_list_append(gpio_isr_list, isr_data); + } + + isr_data->pin = pin; + isr_data->callback = callback; + isr_data->user_data = user_data; + + return PERIPHERAL_ERROR_NONE; +} + +int peripheral_gpio_isr_unset(int pin) +{ + GList *link; + gpio_isr_data_s *isr_data; + + link = gpio_isr_list; + while (link) { + isr_data = (gpio_isr_data_s*)link->data; + + if (isr_data->pin == pin) { + gpio_isr_list = g_list_remove_link(gpio_isr_list, link); + free(link->data); + g_list_free(link); + break; + } + link = g_list_next(link); + } + + return PERIPHERAL_ERROR_NONE; +} + /** * @brief Initializes(export) gpio pin and creates gpio handle. */ @@ -202,12 +284,20 @@ int peripheral_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e */ int peripheral_gpio_register_cb(peripheral_gpio_h gpio, gpio_isr_cb callback, void *user_data) { + int ret = PERIPHERAL_ERROR_NONE; + /* check validation of gpio context handle */ if (gpio == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; - //TODO - return PERIPHERAL_ERROR_INVALID_OPERATION; + ret = peripheral_dbus_gpio_register_cb(gpio, callback, user_data); + if (ret != PERIPHERAL_ERROR_NONE) + return ret; + + /* set isr */ + ret = peripheral_gpio_isr_set(gpio->pin, callback, user_data); + + return ret; } /** @@ -215,11 +305,20 @@ int peripheral_gpio_register_cb(peripheral_gpio_h gpio, gpio_isr_cb callback, vo */ int peripheral_gpio_unregister_cb(peripheral_gpio_h gpio) { + int ret = PERIPHERAL_ERROR_NONE; + /* check validation of gpio context handle */ if (gpio == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; - //TODO - return PERIPHERAL_ERROR_INVALID_OPERATION; + + ret = peripheral_dbus_gpio_unregister_cb(gpio); + if (ret != PERIPHERAL_ERROR_NONE) + return ret; + + /* clean up isr */ + ret = peripheral_gpio_isr_unset(gpio->pin); + + return ret; } /** diff --git a/src/peripheral_io.xml b/src/peripheral_io.xml index cbda49b..ed9559c 100644 --- a/src/peripheral_io.xml +++ b/src/peripheral_io.xml @@ -41,6 +41,18 @@ + + + + + + + + + + + + diff --git a/test/peripheral-io-test.c b/test/peripheral-io-test.c index 048d136..bec2885 100644 --- a/test/peripheral-io-test.c +++ b/test/peripheral-io-test.c @@ -19,11 +19,14 @@ #include #include #include +#include extern int gpio_test(); extern int i2c_test(); extern int adc_test(); +GMainLoop *loop; + int gpio_test(void) { int num; @@ -64,6 +67,79 @@ error: return 0; } +void gpio_irq_test_isr(void *user_data) +{ + int pin; + peripheral_gpio_h gpio = user_data; + + peripheral_gpio_get_pin(gpio, &pin); + + printf("gpio_irq_test_isr: GPIO %d interrupt occurs.\n", pin); +} + +void *gpio_irq_test_thread(void *data) +{ + peripheral_gpio_h gpio = data; + int num; + + printf(">> Press any key to exit GPIO IRQ Test : \n"); + if (scanf("%d", &num) < 0) + return 0; + + peripheral_gpio_unregister_cb(gpio); + peripheral_gpio_close(gpio); + + g_main_loop_quit(loop); + return 0; +} + +int gpio_irq_test(void) +{ + GThread *test_thread; + int num; + peripheral_gpio_h gpio = NULL; + peripheral_gpio_edge_e edge = PERIPHERAL_GPIO_EDGE_NONE; + + printf("artik710 : 27 \n"); + printf(">> PIN NUMBER : "); + + if (scanf("%d", &num) < 0) + return 0; + + if (peripheral_gpio_open(num, &gpio) != PERIPHERAL_ERROR_NONE) { + printf("test dev is null\n"); + return 0; + } + + if (peripheral_gpio_set_direction(gpio, PERIPHERAL_GPIO_DIRECTION_IN) != 0) { + printf("test set direction error!!!"); + goto error; + } + + printf(">> Select Edge Mode (0 = None, 1 = Falling, 2 = Rising, 3 = Both) : "); + if (scanf("%d", &num) < 0) + return 0; + + if (num >= 0 && num <= 3) + edge = num; + + peripheral_gpio_set_edge_mode( gpio, edge); + peripheral_gpio_register_cb(gpio, gpio_irq_test_isr, gpio); + + test_thread = g_thread_new("key input thread", &gpio_irq_test_thread, gpio); + loop = g_main_loop_new(NULL, FALSE); + g_main_loop_run(loop); + + g_thread_join(test_thread); + if (loop != NULL) + g_main_loop_unref(loop); + + return 0; + +error: + peripheral_gpio_close(gpio); + return 0; +} /* Address of GY30 light sensor */ #define GY30_ADDR 0x23 @@ -233,7 +309,7 @@ int main(int argc, char **argv) printf(" 3. pwm led test\n"); printf(" 4. pwm motor test\n"); - printf(" 11. H/W IF GPIO Test\n"); + printf(" 11. GPIO Interrupt Test\n"); printf(" 12. H/W IF I2C Test\n"); printf(" 13. H/W IF PWM Test\n"); printf(" 14. H/W IF SPI Test\n"); @@ -255,7 +331,7 @@ int main(int argc, char **argv) ret = pwm_test_motor(); break; case 11: - ret = gpio_test(); + ret = gpio_irq_test(); break; case 12: ret = i2c_test(); -- 2.34.1 From 2e470160b2df2af956825f72750ab14f3f415e55 Mon Sep 17 00:00:00 2001 From: Hyeongsik Min Date: Sun, 7 May 2017 18:03:51 +0900 Subject: [PATCH 10/16] Change argument name and type of i2c gdbus method The i2c methods will pass handle instead of file descriptor. Change-Id: Ia370c2fe4f3284e8d0b8925cc7bd4bb64c5f0df4 Signed-off-by: Hyeongsik Min --- include/peripheral_internal.h | 2 +- src/peripheral_dbus.c | 10 +++++----- src/peripheral_io.xml | 8 ++++---- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/include/peripheral_internal.h b/include/peripheral_internal.h index f331b57..338f75e 100644 --- a/include/peripheral_internal.h +++ b/include/peripheral_internal.h @@ -30,7 +30,7 @@ struct _peripheral_gpio_s { * @brief Internal struct for i2c context */ struct _peripheral_i2c_s { - int fd; + uint handle; }; #endif /* __PERIPHERAL_INTERNAL_H__ */ diff --git a/src/peripheral_dbus.c b/src/peripheral_dbus.c index 3a61390..b9af3d1 100644 --- a/src/peripheral_dbus.c +++ b/src/peripheral_dbus.c @@ -122,7 +122,7 @@ void handle_gpio_changed( if (!gpio) return; - _D("gpio=%d state=%d",pin, state); + _D("gpio=%d state=%d", pin, state); peripheral_gpio_isr_callback(pin); } @@ -360,7 +360,7 @@ int peripheral_dbus_i2c_open(peripheral_i2c_h i2c, int bus, int address) i2c_proxy, bus, address, - &i2c->fd, + &i2c->handle, &ret, NULL, &error) == FALSE) { @@ -381,7 +381,7 @@ int peripheral_dbus_i2c_close(peripheral_i2c_h i2c) if (peripheral_io_gdbus_i2c_call_close_sync( i2c_proxy, - i2c->fd, + i2c->handle, &ret, NULL, &error) == FALSE) { @@ -406,7 +406,7 @@ int peripheral_dbus_i2c_read(peripheral_i2c_h i2c, uint8_t *data, int length) if (peripheral_io_gdbus_i2c_call_read_sync( i2c_proxy, - i2c->fd, + i2c->handle, length, &data_array, &ret, @@ -448,7 +448,7 @@ int peripheral_dbus_i2c_write(peripheral_i2c_h i2c, uint8_t *data, int length) if (peripheral_io_gdbus_i2c_call_write_sync( i2c_proxy, - i2c->fd, + i2c->handle, length, g_data, &ret, diff --git a/src/peripheral_io.xml b/src/peripheral_io.xml index ed9559c..ba2edef 100644 --- a/src/peripheral_io.xml +++ b/src/peripheral_io.xml @@ -58,15 +58,15 @@ - + - + - + @@ -74,7 +74,7 @@ - + -- 2.34.1 From 05a568491a938223e095bc7244384d14a2c05b32 Mon Sep 17 00:00:00 2001 From: "jino.cho" Date: Mon, 8 May 2017 17:26:27 +0900 Subject: [PATCH 11/16] Worng method was called replace gpio_proxy_deinit() to i2c_proxy_deinit() Change-Id: I7df8f0213b5e129efe6400d60d1b990e189e7bfd Signed-off-by: jino.cho --- src/peripheral_i2c.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/peripheral_i2c.c b/src/peripheral_i2c.c index 23a33f0..b871014 100644 --- a/src/peripheral_i2c.c +++ b/src/peripheral_i2c.c @@ -60,7 +60,7 @@ int peripheral_i2c_close(peripheral_i2c_h i2c) if (i2c == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; ret = peripheral_dbus_i2c_close(i2c); - gpio_proxy_deinit(); + i2c_proxy_deinit(); free(i2c); i2c = NULL; -- 2.34.1 From 552f5ec036a31bcc09e2417055274daaab046213 Mon Sep 17 00:00:00 2001 From: Hyeongsik Min Date: Mon, 8 May 2017 20:15:55 +0900 Subject: [PATCH 12/16] Refactor gdbus interface codes Split peripheral_dbus.c into separate files per low-level interface. Change-Id: Ifdc6bba7d3d4628693e041882ee88b39c763629f Signed-off-by: Hyeongsik Min --- CMakeLists.txt | 10 +- include/peripheral_common.h | 12 +- include/peripheral_dbus.h | 66 ---- include/peripheral_gdbus.h | 27 ++ include/peripheral_gdbus_gpio.h | 34 ++ include/peripheral_gdbus_i2c.h | 28 ++ include/peripheral_gdbus_pwm.h | 31 ++ src/peripheral_dbus.c | 628 -------------------------------- src/peripheral_gdbus_gpio.c | 299 +++++++++++++++ src/peripheral_gdbus_i2c.c | 163 +++++++++ src/peripheral_gdbus_pwm.c | 215 +++++++++++ src/peripheral_gpio.c | 22 +- src/peripheral_i2c.c | 10 +- src/peripheral_pwm.c | 16 +- 14 files changed, 833 insertions(+), 728 deletions(-) delete mode 100644 include/peripheral_dbus.h create mode 100644 include/peripheral_gdbus.h create mode 100644 include/peripheral_gdbus_gpio.h create mode 100644 include/peripheral_gdbus_i2c.h create mode 100644 include/peripheral_gdbus_pwm.h delete mode 100644 src/peripheral_dbus.c create mode 100644 src/peripheral_gdbus_gpio.c create mode 100644 src/peripheral_gdbus_i2c.c create mode 100644 src/peripheral_gdbus_pwm.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 45ec9ac..0bbc0bd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -42,13 +42,15 @@ SET(CMAKE_C_FLAGS_DEBUG "-O0 -g") SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -Wl,--rpath=%{_libdir}") -SET(SOURCES src/peripheral_adc.c - src/peripheral_gpio.c +SET(SOURCES src/peripheral_gpio.c + src/peripheral_i2c.c src/peripheral_pwm.c + src/peripheral_adc.c src/peripheral_uart.c - src/peripheral_dbus.c + src/peripheral_gdbus_gpio.c + src/peripheral_gdbus_i2c.c + src/peripheral_gdbus_pwm.c src/peripheral_io_gdbus.c - src/peripheral_i2c.c src/peripheral_spi.c) ADD_LIBRARY(${fw_name} SHARED ${SOURCES}) diff --git a/include/peripheral_common.h b/include/peripheral_common.h index 2d2f960..19352be 100644 --- a/include/peripheral_common.h +++ b/include/peripheral_common.h @@ -31,28 +31,28 @@ if (expr) { \ _E("(%s)", #expr); \ return; \ - }\ - } while(0) + } \ + } while (0) #define RETV_IF(expr, val) \ do {\ if (expr) { \ _E("(%s)", #expr); \ return (val); \ } \ - } while(0) + } while (0) #define RETM_IF(expr, fmt, arg...) \ do {\ if (expr) { \ _E(fmt, ##arg); \ return; \ - }\ - } while(0) + } \ + } while (0) #define RETVM_IF(expr, val, fmt, arg...) \ do {\ if (expr) { \ _E(fmt, ##arg); \ return (val); \ } \ - } while(0) + } while (0) #endif /* __PERIPHERAL_COMMON_H__ */ diff --git a/include/peripheral_dbus.h b/include/peripheral_dbus.h deleted file mode 100644 index e1002fa..0000000 --- a/include/peripheral_dbus.h +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#ifndef __PERIPHERAL_DBUS_H_ -#define __PERIPHERAL_DBUS_H_ - -#include - -#define PERIPHERAL_DBUS_INTERFACE "org.tizen.peripheral_io" -#define PERIPHERAL_DBUS_PATH "/Org/Tizen/Peripheral_io" -#define PERIPHERAL_DBUS_GPIO_PATH "/Org/Tizen/Peripheral_io/Gpio" -#define PERIPHERAL_DBUS_I2C_PATH "/Org/Tizen/Peripheral_io/I2c" -#define PERIPHERAL_DBUS_PWM_PATH "/Org/Tizen/Peripheral_io/Pwm" -#define PERIPHERAL_DBUS_NAME "org.tizen.peripheral_io" - -#define PERIPHERAL_METHOD_GPIO "gpio" -#define PERIPHERAL_METHOD_I2C "i2c" -#define PERIPHERAL_METHOD_PWM "pwm" -#define PERIPHERAL_METHOD_SPI "spi" -#define PERIPHERAL_METHOD_UART "uart" - -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_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_gpio_register_cb(peripheral_gpio_h gpio, gpio_isr_cb callback, void *user_data); -int peripheral_dbus_gpio_unregister_cb(peripheral_gpio_h gpio); - -int peripheral_dbus_i2c_open(peripheral_i2c_h i2c, int bus, int address); -int peripheral_dbus_i2c_close(peripheral_i2c_h i2c); -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/include/peripheral_gdbus.h b/include/peripheral_gdbus.h new file mode 100644 index 0000000..2195320 --- /dev/null +++ b/include/peripheral_gdbus.h @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef __PERIPHERAL_GDBUS_H__ +#define __PERIPHERAL_GDBUS_H__ + +#include + +#define PERIPHERAL_GDBUS_INTERFACE "org.tizen.peripheral_io" +#define PERIPHERAL_GDBUS_GPIO_PATH "/Org/Tizen/Peripheral_io/Gpio" +#define PERIPHERAL_GDBUS_I2C_PATH "/Org/Tizen/Peripheral_io/I2c" +#define PERIPHERAL_GDBUS_PWM_PATH "/Org/Tizen/Peripheral_io/Pwm" +#define PERIPHERAL_GDBUS_NAME "org.tizen.peripheral_io" + +#endif /* __PERIPHERAL_GDBUS_H__ */ diff --git a/include/peripheral_gdbus_gpio.h b/include/peripheral_gdbus_gpio.h new file mode 100644 index 0000000..0d23b91 --- /dev/null +++ b/include/peripheral_gdbus_gpio.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __PERIPHERAL_GDBUS_GPIO_H__ +#define __PERIPHERAL_GDBUS_GPIO_H__ + +void gpio_proxy_init(void); +void gpio_proxy_deinit(void); + +int peripheral_gdbus_gpio_open(peripheral_gpio_h gpio); +int peripheral_gdbus_gpio_close(peripheral_gpio_h gpio); +int peripheral_gdbus_gpio_get_direction(peripheral_gpio_h gpio, peripheral_gpio_direction_e *direction); +int peripheral_gdbus_gpio_set_direction(peripheral_gpio_h gpio, peripheral_gpio_direction_e direction); +int peripheral_gdbus_gpio_read(peripheral_gpio_h gpio, int *value); +int peripheral_gdbus_gpio_write(peripheral_gpio_h gpio, int value); +int peripheral_gdbus_gpio_get_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e *edge); +int peripheral_gdbus_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e edge); +int peripheral_gdbus_gpio_register_cb(peripheral_gpio_h gpio, gpio_isr_cb callback, void *user_data); +int peripheral_gdbus_gpio_unregister_cb(peripheral_gpio_h gpio); + +#endif /* __PERIPHERAL_GDBUS_GPIO_H__ */ diff --git a/include/peripheral_gdbus_i2c.h b/include/peripheral_gdbus_i2c.h new file mode 100644 index 0000000..c36cb04 --- /dev/null +++ b/include/peripheral_gdbus_i2c.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __PERIPHERAL_GDBUS_I2C_H__ +#define __PERIPHERAL_GDBUS_I2C_H__ + +void i2c_proxy_init(void); +void i2c_proxy_deinit(void); + +int peripheral_gdbus_i2c_open(peripheral_i2c_h i2c, int bus, int address); +int peripheral_gdbus_i2c_close(peripheral_i2c_h i2c); +int peripheral_gdbus_i2c_read(peripheral_i2c_h i2c, uint8_t *data, int length); +int peripheral_gdbus_i2c_write(peripheral_i2c_h i2c, uint8_t *data, int length); + +#endif /* __PERIPHERAL_GDBUS_I2C_H__ */ diff --git a/include/peripheral_gdbus_pwm.h b/include/peripheral_gdbus_pwm.h new file mode 100644 index 0000000..2d28514 --- /dev/null +++ b/include/peripheral_gdbus_pwm.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __PERIPHERAL_GDBUS_PWM_H__ +#define __PERIPHERAL_GDBUS_PWM_H__ + +void pwm_proxy_init(void); +void pwm_proxy_deinit(void); + +int peripheral_gdbus_pwm_open(peripheral_pwm_context_h dev, int device, int channel); +int peripheral_gdbus_pwm_close(peripheral_pwm_context_h dev); +int peripheral_gdbus_pwm_get_duty_cycle(peripheral_pwm_context_h dev, int *duty_cycle); +int peripheral_gdbus_pwm_set_duty_cycle(peripheral_pwm_context_h dev, int duty_cycle); +int peripheral_gdbus_pwm_get_period(peripheral_pwm_context_h dev, int *period); +int peripheral_gdbus_pwm_set_period(peripheral_pwm_context_h dev, int period); +int peripheral_gdbus_pwm_set_enable(peripheral_pwm_context_h dev, peripheral_pwm_state_e enable); + +#endif /* __PERIPHERAL_GDBUS_PWM_H__ */ diff --git a/src/peripheral_dbus.c b/src/peripheral_dbus.c deleted file mode 100644 index b9af3d1..0000000 --- a/src/peripheral_dbus.c +++ /dev/null @@ -1,628 +0,0 @@ -/* - * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include - -#include "peripheral_io.h" -#include "peripheral_dbus.h" -#include "peripheral_common.h" -#include "peripheral_internal.h" -#include "peripheral_io_gdbus.h" - -extern int peripheral_gpio_isr_callback(int pin); -void handle_gpio_changed(PeripheralIoGdbusGpio *gpio, gint pin, gint state, gpointer user_data); - -PeripheralIoGdbusGpio *gpio_proxy = NULL; -PeripheralIoGdbusI2c *i2c_proxy = NULL; -PeripheralIoGdbusPwm *pwm_proxy = NULL; - -void gpio_proxy_init(void) -{ - GError *error = NULL; - - if (gpio_proxy != NULL) - return; - - 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); - if (gpio_proxy == NULL) { - _E("Can not create gpio proxy : %s", error->message); - g_error_free(error); - return; - } - - g_signal_connect(gpio_proxy, - "gpio-changed", - G_CALLBACK(handle_gpio_changed), - NULL); -} - -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() -{ - if (gpio_proxy) { - g_object_unref(gpio_proxy); - gpio_proxy = NULL; - } -} - -void i2c_proxy_deinit() -{ - if (i2c_proxy) { - g_object_unref(i2c_proxy); - i2c_proxy = NULL; - } -} - -void pwm_proxy_deinit() -{ - if (pwm_proxy) { - g_object_unref(pwm_proxy); - pwm_proxy = NULL; - } -} - -void handle_gpio_changed( - PeripheralIoGdbusGpio *gpio, - gint pin, - gint state, - gpointer user_data) -{ - if (!gpio) - return; - - _D("gpio=%d state=%d", pin, state); - - peripheral_gpio_isr_callback(pin); -} - -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, - (gint*)&gpio->edge, - (gint*)&gpio->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_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; - } - - return ret; -} - -int peripheral_dbus_gpio_get_direction(peripheral_gpio_h gpio, peripheral_gpio_direction_e *direction) -{ - 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; - } - gpio->direction = *direction; - - return ret; -} - -int peripheral_dbus_gpio_set_direction(peripheral_gpio_h gpio, peripheral_gpio_direction_e direction) -{ - 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; - } - gpio->direction = direction; - - return ret; -} - -int peripheral_dbus_gpio_read(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_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; - } - - return ret; -} - -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; - } - - 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; - } - gpio->edge = *edge; - - 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 (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; - } - gpio->edge = edge; - - return ret; -} - -int peripheral_dbus_gpio_register_cb(peripheral_gpio_h gpio, gpio_isr_cb callback, void *user_data) -{ - GError *error = NULL; - peripheral_error_e ret = PERIPHERAL_ERROR_NONE; - - if (gpio_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; - - if (peripheral_io_gdbus_gpio_call_register_irq_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; - } - - return ret; -} - -int peripheral_dbus_gpio_unregister_cb(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_unregister_irq_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; - } - - return ret; -} - -int peripheral_dbus_i2c_open(peripheral_i2c_h i2c, int bus, 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_open_sync( - i2c_proxy, - bus, - address, - &i2c->handle, - &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_close(peripheral_i2c_h i2c) -{ - GError *error = NULL; - peripheral_error_e ret = PERIPHERAL_ERROR_NONE; - - if (i2c_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; - - if (peripheral_io_gdbus_i2c_call_close_sync( - i2c_proxy, - i2c->handle, - &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_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; - int i = 0; - - if (i2c_proxy == NULL || data == NULL) return PERIPHERAL_ERROR_UNKNOWN; - - if (peripheral_io_gdbus_i2c_call_read_sync( - i2c_proxy, - i2c->handle, - 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 (peripheral_io_gdbus_i2c_call_write_sync( - i2c_proxy, - i2c->handle, - length, - g_data, - &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_open(peripheral_pwm_context_h dev, int device, int channel) -{ - 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_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_close(peripheral_pwm_context_h dev) -{ - 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_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; - } - - 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_gdbus_gpio.c b/src/peripheral_gdbus_gpio.c new file mode 100644 index 0000000..2027f3d --- /dev/null +++ b/src/peripheral_gdbus_gpio.c @@ -0,0 +1,299 @@ +/* + * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include "peripheral_io.h" +#include "peripheral_gdbus.h" +#include "peripheral_common.h" +#include "peripheral_internal.h" +#include "peripheral_io_gdbus.h" + +extern int peripheral_gpio_isr_callback(int pin); +void handle_gpio_changed(PeripheralIoGdbusGpio *gpio, gint pin, gint state, gpointer user_data); + +PeripheralIoGdbusGpio *gpio_proxy = NULL; + +void gpio_proxy_init(void) +{ + GError *error = NULL; + + if (gpio_proxy != NULL) + return; + + gpio_proxy = peripheral_io_gdbus_gpio_proxy_new_for_bus_sync( + G_BUS_TYPE_SYSTEM, + G_DBUS_PROXY_FLAGS_NONE, + PERIPHERAL_GDBUS_NAME, + PERIPHERAL_GDBUS_GPIO_PATH, + NULL, + &error); + if (gpio_proxy == NULL) { + _E("Can not create gpio proxy : %s", error->message); + g_error_free(error); + return; + } + + g_signal_connect(gpio_proxy, + "gpio-changed", + G_CALLBACK(handle_gpio_changed), + NULL); +} + +void gpio_proxy_deinit() +{ + if (gpio_proxy) { + g_object_unref(gpio_proxy); + gpio_proxy = NULL; + } +} + +void handle_gpio_changed( + PeripheralIoGdbusGpio *gpio, + gint pin, + gint state, + gpointer user_data) +{ + if (!gpio) + return; + + _D("gpio=%d state=%d", pin, state); + + peripheral_gpio_isr_callback(pin); +} + +int peripheral_gdbus_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, + (gint*)&gpio->edge, + (gint*)&gpio->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_gdbus_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; + } + + return ret; +} + +int peripheral_gdbus_gpio_get_direction(peripheral_gpio_h gpio, peripheral_gpio_direction_e *direction) +{ + 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; + } + gpio->direction = *direction; + + return ret; +} + +int peripheral_gdbus_gpio_set_direction(peripheral_gpio_h gpio, peripheral_gpio_direction_e direction) +{ + 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; + } + gpio->direction = direction; + + return ret; +} + +int peripheral_gdbus_gpio_read(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_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; + } + + return ret; +} + +int peripheral_gdbus_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; + } + + return ret; +} + +int peripheral_gdbus_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; + } + gpio->edge = *edge; + + return ret; +} + +int peripheral_gdbus_gpio_set_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_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; + } + gpio->edge = edge; + + return ret; +} + +int peripheral_gdbus_gpio_register_cb(peripheral_gpio_h gpio, gpio_isr_cb callback, void *user_data) +{ + GError *error = NULL; + peripheral_error_e ret = PERIPHERAL_ERROR_NONE; + + if (gpio_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + if (peripheral_io_gdbus_gpio_call_register_irq_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; + } + + return ret; +} + +int peripheral_gdbus_gpio_unregister_cb(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_unregister_irq_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; + } + + return ret; +} diff --git a/src/peripheral_gdbus_i2c.c b/src/peripheral_gdbus_i2c.c new file mode 100644 index 0000000..ce42ce5 --- /dev/null +++ b/src/peripheral_gdbus_i2c.c @@ -0,0 +1,163 @@ +/* + * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include "peripheral_io.h" +#include "peripheral_gdbus.h" +#include "peripheral_common.h" +#include "peripheral_internal.h" +#include "peripheral_io_gdbus.h" + +PeripheralIoGdbusI2c *i2c_proxy = NULL; + +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_GDBUS_NAME, + PERIPHERAL_GDBUS_I2C_PATH, + NULL, + &error); +} + +void i2c_proxy_deinit() +{ + if (i2c_proxy) { + g_object_unref(i2c_proxy); + i2c_proxy = NULL; + } +} + +int peripheral_gdbus_i2c_open(peripheral_i2c_h i2c, int bus, 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_open_sync( + i2c_proxy, + bus, + address, + &i2c->handle, + &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_gdbus_i2c_close(peripheral_i2c_h i2c) +{ + GError *error = NULL; + peripheral_error_e ret = PERIPHERAL_ERROR_NONE; + + if (i2c_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + if (peripheral_io_gdbus_i2c_call_close_sync( + i2c_proxy, + i2c->handle, + &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_gdbus_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; + int i = 0; + + if (i2c_proxy == NULL || data == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + if (peripheral_io_gdbus_i2c_call_read_sync( + i2c_proxy, + i2c->handle, + 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_gdbus_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 (peripheral_io_gdbus_i2c_call_write_sync( + i2c_proxy, + i2c->handle, + length, + g_data, + &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_gdbus_pwm.c b/src/peripheral_gdbus_pwm.c new file mode 100644 index 0000000..db73dee --- /dev/null +++ b/src/peripheral_gdbus_pwm.c @@ -0,0 +1,215 @@ +/* + * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include "peripheral_io.h" +#include "peripheral_gdbus.h" +#include "peripheral_common.h" +#include "peripheral_internal.h" +#include "peripheral_io_gdbus.h" + +PeripheralIoGdbusPwm *pwm_proxy = NULL; + +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_GDBUS_NAME, + PERIPHERAL_GDBUS_PWM_PATH, + NULL, + &error); +} + +void pwm_proxy_deinit() +{ + if (pwm_proxy) { + g_object_unref(pwm_proxy); + pwm_proxy = NULL; + } +} + +int peripheral_gdbus_pwm_open(peripheral_pwm_context_h dev, int device, int channel) +{ + 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_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_gdbus_pwm_close(peripheral_pwm_context_h dev) +{ + 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_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; + } + + return ret; +} + +int peripheral_gdbus_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_gdbus_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_gdbus_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_gdbus_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_gdbus_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 f942759..2b3bd20 100644 --- a/src/peripheral_gpio.c +++ b/src/peripheral_gpio.c @@ -20,7 +20,7 @@ #include #include "peripheral_io.h" -#include "peripheral_dbus.h" +#include "peripheral_gdbus_gpio.h" #include "peripheral_common.h" #include "peripheral_internal.h" #include "peripheral_io_gdbus.h" @@ -128,7 +128,7 @@ int peripheral_gpio_open(int gpio_pin, peripheral_gpio_h *gpio) gpio_proxy_init(); - ret = peripheral_dbus_gpio_open(handle); + ret = peripheral_gdbus_gpio_open(handle); if (ret != PERIPHERAL_ERROR_NONE) { free(handle); @@ -153,7 +153,7 @@ int peripheral_gpio_close(peripheral_gpio_h gpio) return PERIPHERAL_ERROR_INVALID_PARAMETER; /* call gpio_close */ - ret = peripheral_dbus_gpio_close(gpio); + ret = peripheral_gdbus_gpio_close(gpio); if (ret) ret = TIZEN_ERROR_IO_ERROR; gpio_proxy_deinit(); @@ -175,7 +175,7 @@ int peripheral_gpio_get_direction(peripheral_gpio_h gpio, peripheral_gpio_direct if (gpio == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; - ret = peripheral_dbus_gpio_get_direction(gpio, direction); + ret = peripheral_gdbus_gpio_get_direction(gpio, direction); if (ret == PERIPHERAL_ERROR_NONE) gpio->direction = (*direction); @@ -198,7 +198,7 @@ int peripheral_gpio_set_direction(peripheral_gpio_h gpio, peripheral_gpio_direct return PERIPHERAL_ERROR_INVALID_PARAMETER; /* call gpio_set_direction */ - ret = peripheral_dbus_gpio_set_direction(gpio, direction); + ret = peripheral_gdbus_gpio_set_direction(gpio, direction); if (ret == PERIPHERAL_ERROR_NONE) gpio->direction = direction; @@ -217,7 +217,7 @@ int peripheral_gpio_read(peripheral_gpio_h gpio, int *value) return PERIPHERAL_ERROR_INVALID_PARAMETER; /* call gpio_read */ - ret = peripheral_dbus_gpio_read(gpio, value); + ret = peripheral_gdbus_gpio_read(gpio, value); return ret; } @@ -234,7 +234,7 @@ int peripheral_gpio_write(peripheral_gpio_h gpio, int value) return PERIPHERAL_ERROR_INVALID_PARAMETER; /* call gpio_write */ - ret = peripheral_dbus_gpio_write(gpio, value); + ret = peripheral_gdbus_gpio_write(gpio, value); return ret; } @@ -250,7 +250,7 @@ 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_get_edge_mode(gpio, edge); + ret = peripheral_gdbus_gpio_get_edge_mode(gpio, edge); if (ret == PERIPHERAL_ERROR_NONE) gpio->edge = (*edge); @@ -272,7 +272,7 @@ int peripheral_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e return PERIPHERAL_ERROR_INVALID_PARAMETER; /* call gpio_set_edge_mode */ - ret = peripheral_dbus_gpio_set_edge_mode(gpio, edge); + ret = peripheral_gdbus_gpio_set_edge_mode(gpio, edge); if (ret == PERIPHERAL_ERROR_NONE) gpio->edge = edge; @@ -290,7 +290,7 @@ int peripheral_gpio_register_cb(peripheral_gpio_h gpio, gpio_isr_cb callback, vo if (gpio == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; - ret = peripheral_dbus_gpio_register_cb(gpio, callback, user_data); + ret = peripheral_gdbus_gpio_register_cb(gpio, callback, user_data); if (ret != PERIPHERAL_ERROR_NONE) return ret; @@ -311,7 +311,7 @@ int peripheral_gpio_unregister_cb(peripheral_gpio_h gpio) if (gpio == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; - ret = peripheral_dbus_gpio_unregister_cb(gpio); + ret = peripheral_gdbus_gpio_unregister_cb(gpio); if (ret != PERIPHERAL_ERROR_NONE) return ret; diff --git a/src/peripheral_i2c.c b/src/peripheral_i2c.c index b871014..fee3a32 100644 --- a/src/peripheral_i2c.c +++ b/src/peripheral_i2c.c @@ -20,7 +20,7 @@ #include #include "peripheral_io.h" -#include "peripheral_dbus.h" +#include "peripheral_gdbus_i2c.h" #include "peripheral_common.h" #include "peripheral_internal.h" @@ -41,7 +41,7 @@ int peripheral_i2c_open(int bus, int address, peripheral_i2c_h *i2c) i2c_proxy_init(); - ret = peripheral_dbus_i2c_open(handle, bus, address); + ret = peripheral_gdbus_i2c_open(handle, bus, address); if (ret != PERIPHERAL_ERROR_NONE) { _E("[PERIPHERAL] I2C init error\n"); @@ -59,7 +59,7 @@ int peripheral_i2c_close(peripheral_i2c_h i2c) if (i2c == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; - ret = peripheral_dbus_i2c_close(i2c); + ret = peripheral_gdbus_i2c_close(i2c); i2c_proxy_deinit(); free(i2c); @@ -74,7 +74,7 @@ int peripheral_i2c_read(peripheral_i2c_h i2c, uint8_t *data, int length) if (i2c == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; - ret = peripheral_dbus_i2c_read(i2c, data, length); + ret = peripheral_gdbus_i2c_read(i2c, data, length); /* _D("I2C read data : "); for (int i = 0 ; i < length ; i++) @@ -87,5 +87,5 @@ int peripheral_i2c_write(peripheral_i2c_h i2c, uint8_t *data, int length) { if (i2c == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; - return peripheral_dbus_i2c_write(i2c, data, length); + return peripheral_gdbus_i2c_write(i2c, data, length); } diff --git a/src/peripheral_pwm.c b/src/peripheral_pwm.c index d4d728a..b3fa3a7 100644 --- a/src/peripheral_pwm.c +++ b/src/peripheral_pwm.c @@ -20,7 +20,7 @@ #include #include "peripheral_io.h" -#include "peripheral_dbus.h" +#include "peripheral_gdbus_pwm.h" #include "peripheral_common.h" #define PWM_ENABLE 1 @@ -47,7 +47,7 @@ peripheral_pwm_context_h peripheral_pwm_open(int device, int channel) dev->device = device; dev->channel = channel; - ret = peripheral_dbus_pwm_open(dev, device, channel); + ret = peripheral_gdbus_pwm_open(dev, device, channel); if (ret != PERIPHERAL_ERROR_NONE) { free(dev); @@ -61,7 +61,7 @@ int peripheral_pwm_close(peripheral_pwm_context_h pwm) { int ret = PERIPHERAL_ERROR_NONE; - ret = peripheral_dbus_pwm_close(pwm); + ret = peripheral_gdbus_pwm_close(pwm); pwm_proxy_deinit(); if (ret == PERIPHERAL_ERROR_NONE) { @@ -77,7 +77,7 @@ int peripheral_pwm_set_duty_cycle(peripheral_pwm_context_h pwm, int duty_cycle) { int ret = PERIPHERAL_ERROR_NONE; - ret = peripheral_dbus_pwm_set_duty_cycle(pwm, duty_cycle); + ret = peripheral_gdbus_pwm_set_duty_cycle(pwm, duty_cycle); if (ret != PERIPHERAL_ERROR_NONE) pwm->duty_cycle = duty_cycle; @@ -89,7 +89,7 @@ int peripheral_pwm_set_period(peripheral_pwm_context_h pwm, int period) { int ret = PERIPHERAL_ERROR_NONE; - ret = peripheral_dbus_pwm_set_period(pwm, period); + ret = peripheral_gdbus_pwm_set_period(pwm, period); if (ret != PERIPHERAL_ERROR_NONE) pwm->period = period; @@ -101,7 +101,7 @@ int peripheral_pwm_set_enabled(peripheral_pwm_context_h pwm, peripheral_pwm_stat { int ret = PERIPHERAL_ERROR_NONE; - ret = peripheral_dbus_pwm_set_enable(pwm, enable); + ret = peripheral_gdbus_pwm_set_enable(pwm, enable); if (ret != PERIPHERAL_ERROR_NONE) pwm->enabled = enable; @@ -121,7 +121,7 @@ int peripheral_pwm_get_duty_cycle(peripheral_pwm_context_h pwm, int *duty_cycle) { int ret = PERIPHERAL_ERROR_NONE; - ret = peripheral_dbus_pwm_get_duty_cycle(pwm, duty_cycle); + ret = peripheral_gdbus_pwm_get_duty_cycle(pwm, duty_cycle); if (ret != PERIPHERAL_ERROR_NONE) pwm->duty_cycle = *duty_cycle; @@ -133,7 +133,7 @@ int peripheral_pwm_get_period(peripheral_pwm_context_h pwm, int *period) { int ret = PERIPHERAL_ERROR_NONE; - ret = peripheral_dbus_pwm_get_period(pwm, period); + ret = peripheral_gdbus_pwm_get_period(pwm, period); if (ret != PERIPHERAL_ERROR_NONE) pwm->period = *period; -- 2.34.1 From 86db2fdda86f5c20bd043892e0dc3571c95738f4 Mon Sep 17 00:00:00 2001 From: "jino.cho" Date: Fri, 12 May 2017 15:34:54 +0900 Subject: [PATCH 13/16] Add description for uart APIs This patch Adds description for uart APIs and modify uart handle. Change-Id: I0f5387ab0441cf822cfdc792ce4da28dfef86de4 Signed-off-by: jino.cho --- include/peripheral_internal.h | 7 ++ include/peripheral_io.h | 187 +++++++++++++++++++++++++++++++--- 2 files changed, 181 insertions(+), 13 deletions(-) diff --git a/include/peripheral_internal.h b/include/peripheral_internal.h index 338f75e..290fb28 100644 --- a/include/peripheral_internal.h +++ b/include/peripheral_internal.h @@ -33,4 +33,11 @@ struct _peripheral_i2c_s { uint handle; }; +/** + * @brief Internal struct for uart context + */ +struct _peripheral_uart_s { + uint handle; +}; + #endif /* __PERIPHERAL_INTERNAL_H__ */ diff --git a/include/peripheral_io.h b/include/peripheral_io.h index 6c62c1f..568c7a3 100644 --- a/include/peripheral_io.h +++ b/include/peripheral_io.h @@ -439,34 +439,195 @@ int peripheral_adc_close(peripheral_adc_context_h dev); * @addtogroup CAPI_SYSTEM_PERIPHERAL_UART_MODULE * @{ */ -struct _peripheral_uart_s { - int fd; -}; +/** + * @brief The handle to the uart device + * @since_tizen 4.0 + */ +typedef struct _peripheral_uart_s *peripheral_uart_h; -typedef struct _peripheral_uart_s* peripheral_uart_context_h; +/** + * @brief Enumeration for Baud Rate. + */ +typedef enum { + PERIPHERAL_UART_BAUDRATE_0 = 0, + PERIPHERAL_UART_BAUDRATE_50, + PERIPHERAL_UART_BAUDRATE_75, + PERIPHERAL_UART_BAUDRATE_110, + PERIPHERAL_UART_BAUDRATE_134, + PERIPHERAL_UART_BAUDRATE_150, + PERIPHERAL_UART_BAUDRATE_200, + PERIPHERAL_UART_BAUDRATE_300, + PERIPHERAL_UART_BAUDRATE_600, + PERIPHERAL_UART_BAUDRATE_1200, + PERIPHERAL_UART_BAUDRATE_1800, + PERIPHERAL_UART_BAUDRATE_2400, + PERIPHERAL_UART_BAUDRATE_4800, + PERIPHERAL_UART_BAUDRATE_9600, + PERIPHERAL_UART_BAUDRATE_19200, + PERIPHERAL_UART_BAUDRATE_38400, + PERIPHERAL_UART_BAUDRATE_57600, + PERIPHERAL_UART_BAUDRATE_115200, + PERIPHERAL_UART_BAUDRATE_230400 +} peripheral_uart_baudrate_e; + +/** + * @brief Enumeration for Byte Size. + */ +typedef enum { + PERIPHERAL_UART_BYTESIZE_5BIT = 0, + PERIPHERAL_UART_BYTESIZE_6BIT, + PERIPHERAL_UART_BYTESIZE_7BIT, + PERIPHERAL_UART_BYTESIZE_8BIT +} peripheral_uart_bytesize_e; +/** + * @brief Enumeration for Parity Bit. + */ typedef enum { PERIPHERAL_UART_PARITY_NONE = 0, PERIPHERAL_UART_PARITY_EVEN, - PERIPHERAL_UART_PARITY_ODD, + PERIPHERAL_UART_PARITY_ODD } peripheral_uart_parity_e; -peripheral_uart_context_h peripheral_uart_init(const char *path); +/** + * @brief Enumeration for Stop Bits. + */ +typedef enum { + PERIPHERAL_UART_STOPBITS_1BIT = 0, + PERIPHERAL_UART_STOPBITS_2BIT +} peripheral_uart_stopbits_e; -int peripheral_uart_stop(peripheral_uart_context_h hnd); +/** + * @brief Initializes uart communication and creates uart handle. + * @since_tizen 4.0 + * + * @param[in] port The uart port number that the slave device is connected + * @param[out] uart The uart handle is created on success + * + * @return 0 on success, otherwise a negative error value + * @retval #PERIPHERAL_ERROR_NONE Successful + * @retval #PERIPHERAL_ERROR_IO_ERROR I/O operation failed + * @retval #PERIPHERAL_ERROR_OUT_OF_MEMORY Memory allocation failed + * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error + * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed + * + * @see peripheral_uart_close() + */ +int peripheral_uart_open(int port, peripheral_uart_h *uart); -int peripheral_uart_flush(peripheral_uart_context_h hnd); +/** + * @brief Destory the uart handle and release the communication. + * @since_tizen 4.0 + * + * @param[in] uart The handle to the uart device + * + * @return 0 on success, otherwise a negative error value + * @retval #PERIPHERAL_ERROR_NONE Successful + * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error + * + * @see peripheral_uart_open() + */ +int peripheral_uart_close(peripheral_uart_h uart); -int peripheral_uart_set_baudrate(peripheral_uart_context_h hnd, unsigned int baud); +/** + * @brief Flush all input that has received but not yet been read by the uart + * device, or all output written but not transmitted to the uart device. + * @since_tizen 4.0 + * + * @param[in] uart The uart handle + * + * @return 0 on success, otherwise a negative error value + * @retval #PERIPHERAL_ERROR_NONE Successful + * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error + */ +int peripheral_uart_flush(peripheral_uart_h uart); -int peripheral_uart_set_mode(peripheral_uart_context_h hnd, int bytesize, peripheral_uart_parity_e parity, int stopbits); +/** + * @brief Sets baudrate of the uart device. + * @since_tizen 4.0 + * + * @param[in] uart The handle to the uart device to set + * @param[in] baud Baudrate of the uart device + * + * @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_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error + * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed + */ +int peripheral_uart_set_baudrate(peripheral_uart_h uart, peripheral_uart_baudrate_e baud); -int peripheral_uart_set_flowcontrol(peripheral_uart_context_h hnd, int xonxoff, int rtscts); +/** + * @brief Sets mode of the uart device. + * @since_tizen 4.0 + * + * @param[in] uart The handle to the uart device to set + * @param[in] bytesize Byte size of the uart device + * @param[in] parity Parity bits of the uart device + * @param[in] stopbits Stop bits of the uart device + * + * @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_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error + * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed + */ +int peripheral_uart_set_mode(peripheral_uart_h uart, peripheral_uart_bytesize_e bytesize, peripheral_uart_parity_e parity, peripheral_uart_stopbits_e stopbits); -int peripheral_uart_read(peripheral_uart_context_h hnd, char *buf, unsigned int length); +/** + * @brief Sets flow control of the uart device. + * @since_tizen 4.0 + * + * @param[in] uart The handle to the uart device to set + * @param[in] xonxoff Turns a transmitter on or off + * @param[in] rtscts Turns "Request to Send/Clear to Send" on or off + * + * @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_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error + * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed + */ +int peripheral_uart_set_flowcontrol(peripheral_uart_h uart, bool xonxoff, bool rtscts); + +/** + * @brief Reads data from the uart device. + * @since_tizen 4.0 + * + * @param[in] uart The handle to the uart device + * @param[out] data The address of read buffer + * @param[out] length The size of data buffer (in bytes) + * + * @return the number of bytes read on success, otherwise a negative error value + * @retval #PERIPHERAL_ERROR_NONE Successful + * @retval #PERIPHERAL_ERROR_IO_ERROR I/O operation failed + * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error + */ +int peripheral_uart_read(peripheral_uart_h uart, char *data, int length); -int peripheral_uart_write(peripheral_uart_context_h hnd, const char *buf, unsigned int length); +/** + * @brief Write data to the uart device. + * @since_tizen 4.0 + * + * @param[in] uart The handle to the uart device + * @param[in] data The address of buffer to write + * @param[in] length The size of data (in bytes) + * + * @return the number of bytes write on success, otherwise a negative error value + * @retval #PERIPHERAL_ERROR_NONE Successful + * @retval #PERIPHERAL_ERROR_IO_ERROR I/O operation failed + * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error + */ +int peripheral_uart_write(peripheral_uart_h uart, const char *data, int length); /** * @} -- 2.34.1 From 76465a52999fcbe37dadd3d24bf29ab015501097 Mon Sep 17 00:00:00 2001 From: "jino.cho" Date: Fri, 12 May 2017 15:43:49 +0900 Subject: [PATCH 14/16] Add APIs and functions for uart This patch support uart device. And, it should work properly if the patch for peripheral-bus is applied together. Change-Id: I79777883a705824af7bf9329f3470bd51ffdfb7d Signed-off-by: jino.cho --- CMakeLists.txt | 1 + include/peripheral_gdbus.h | 1 + include/peripheral_gdbus_uart.h | 31 ++++ include/peripheral_io.h | 4 +- src/peripheral_gdbus_uart.c | 252 ++++++++++++++++++++++++++++++++ src/peripheral_io.xml | 49 +++++++ src/peripheral_uart.c | 121 +++++++++++++++ 7 files changed, 457 insertions(+), 2 deletions(-) create mode 100644 include/peripheral_gdbus_uart.h create mode 100644 src/peripheral_gdbus_uart.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 0bbc0bd..d051617 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -50,6 +50,7 @@ SET(SOURCES src/peripheral_gpio.c src/peripheral_gdbus_gpio.c src/peripheral_gdbus_i2c.c src/peripheral_gdbus_pwm.c + src/peripheral_gdbus_uart.c src/peripheral_io_gdbus.c src/peripheral_spi.c) diff --git a/include/peripheral_gdbus.h b/include/peripheral_gdbus.h index 2195320..aefe770 100644 --- a/include/peripheral_gdbus.h +++ b/include/peripheral_gdbus.h @@ -22,6 +22,7 @@ #define PERIPHERAL_GDBUS_GPIO_PATH "/Org/Tizen/Peripheral_io/Gpio" #define PERIPHERAL_GDBUS_I2C_PATH "/Org/Tizen/Peripheral_io/I2c" #define PERIPHERAL_GDBUS_PWM_PATH "/Org/Tizen/Peripheral_io/Pwm" +#define PERIPHERAL_GDBUS_UART_PATH "/Org/Tizen/Peripheral_io/Uart" #define PERIPHERAL_GDBUS_NAME "org.tizen.peripheral_io" #endif /* __PERIPHERAL_GDBUS_H__ */ diff --git a/include/peripheral_gdbus_uart.h b/include/peripheral_gdbus_uart.h new file mode 100644 index 0000000..7c7f8e7 --- /dev/null +++ b/include/peripheral_gdbus_uart.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef __PERIPHERAL_GDBUS_UART_H_ +#define __PERIPHERAL_GDBUS_UART_H_ + +void uart_proxy_init(void); +void uart_proxy_deinit(); + +int peripheral_gdbus_uart_open(peripheral_uart_h uart, int port); +int peripheral_gdbus_uart_close(peripheral_uart_h uart); +int peripheral_gdbus_uart_flush(peripheral_uart_h uart); +int peripheral_gdbus_uart_set_baudrate(peripheral_uart_h uart, peripheral_uart_baudrate_e baud); +int peripheral_gdbus_uart_set_mode(peripheral_uart_h uart, peripheral_uart_bytesize_e bytesize, peripheral_uart_parity_e parity, peripheral_uart_stopbits_e stopbits); +int peripheral_gdbus_uart_set_flowcontrol(peripheral_uart_h uart, bool xonxoff, bool rtscts); +int peripheral_gdbus_uart_read(peripheral_uart_h uart, uint8_t *data, int length); +int peripheral_gdbus_uart_write(peripheral_uart_h uart, uint8_t *data, int length); + +#endif /* __PERIPHERAL_GDBUS_UART_H_ */ diff --git a/include/peripheral_io.h b/include/peripheral_io.h index 568c7a3..a8ed110 100644 --- a/include/peripheral_io.h +++ b/include/peripheral_io.h @@ -611,7 +611,7 @@ int peripheral_uart_set_flowcontrol(peripheral_uart_h uart, bool xonxoff, bool r * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error */ -int peripheral_uart_read(peripheral_uart_h uart, char *data, int length); +int peripheral_uart_read(peripheral_uart_h uart, uint8_t *data, int length); /** * @brief Write data to the uart device. @@ -627,7 +627,7 @@ int peripheral_uart_read(peripheral_uart_h uart, char *data, int length); * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error */ -int peripheral_uart_write(peripheral_uart_h uart, const char *data, int length); +int peripheral_uart_write(peripheral_uart_h uart, uint8_t *data, int length); /** * @} diff --git a/src/peripheral_gdbus_uart.c b/src/peripheral_gdbus_uart.c new file mode 100644 index 0000000..fc94a89 --- /dev/null +++ b/src/peripheral_gdbus_uart.c @@ -0,0 +1,252 @@ +/* + * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include "peripheral_io.h" +#include "peripheral_gdbus.h" +#include "peripheral_common.h" +#include "peripheral_internal.h" +#include "peripheral_io_gdbus.h" + +PeripheralIoGdbusUart *uart_proxy = NULL; + +void uart_proxy_init(void) +{ + GError *error = NULL; + + if (uart_proxy != NULL) + return; + + uart_proxy = peripheral_io_gdbus_uart_proxy_new_for_bus_sync( + G_BUS_TYPE_SYSTEM, + G_DBUS_PROXY_FLAGS_NONE, + PERIPHERAL_GDBUS_NAME, + PERIPHERAL_GDBUS_UART_PATH, + NULL, + &error); +} + +void uart_proxy_deinit() +{ + if (uart_proxy) { + g_object_unref(uart_proxy); + uart_proxy = NULL; + } +} + +int peripheral_gdbus_uart_open(peripheral_uart_h uart, int port) +{ + GError *error = NULL; + peripheral_error_e ret = PERIPHERAL_ERROR_NONE; + + if (uart_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + if (peripheral_io_gdbus_uart_call_open_sync( + uart_proxy, + port, + &uart->handle, + &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_gdbus_uart_close(peripheral_uart_h uart) +{ + GError *error = NULL; + peripheral_error_e ret = PERIPHERAL_ERROR_NONE; + + if (uart_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + if (peripheral_io_gdbus_uart_call_close_sync( + uart_proxy, + uart->handle, + &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_gdbus_uart_flush(peripheral_uart_h uart) +{ + GError *error = NULL; + gint32 ret = PERIPHERAL_ERROR_NONE; + + if (uart_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + if (peripheral_io_gdbus_uart_call_flush_sync( + uart_proxy, + uart->handle, + &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_gdbus_uart_set_baudrate(peripheral_uart_h uart, peripheral_uart_baudrate_e baud) +{ + GError *error = NULL; + gint32 ret = PERIPHERAL_ERROR_NONE; + + if (uart_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + if (peripheral_io_gdbus_uart_call_set_baudrate_sync( + uart_proxy, + uart->handle, + baud, + &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_gdbus_uart_set_mode(peripheral_uart_h uart, peripheral_uart_bytesize_e bytesize, peripheral_uart_parity_e parity, peripheral_uart_stopbits_e stopbits) +{ + GError *error = NULL; + gint32 ret = PERIPHERAL_ERROR_NONE; + + if (uart_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + if (peripheral_io_gdbus_uart_call_set_mode_sync( + uart_proxy, + uart->handle, + bytesize, + parity, + stopbits, + &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_gdbus_uart_set_flowcontrol(peripheral_uart_h uart, bool xonxoff, bool rtscts) +{ + GError *error = NULL; + gint32 ret = PERIPHERAL_ERROR_NONE; + + if (uart_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + if (peripheral_io_gdbus_uart_call_set_flowcontrol_sync( + uart_proxy, + uart->handle, + xonxoff, + rtscts, + &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_gdbus_uart_read(peripheral_uart_h uart, uint8_t *data, int length) +{ + GError *error = NULL; + peripheral_error_e ret = PERIPHERAL_ERROR_NONE; + GVariant *data_array; + GVariantIter *iter; + guint8 str; + int i = 0; + + if (uart_proxy == NULL || data == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + if (peripheral_io_gdbus_uart_call_read_sync( + uart_proxy, + uart->handle, + 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_gdbus_uart_write(peripheral_uart_h uart, uint8_t *data, int length) +{ + GError *error = NULL; + peripheral_error_e ret = PERIPHERAL_ERROR_NONE; + GVariantBuilder *builder; + GVariant *g_data; + int i = 0; + + if (uart_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 (peripheral_io_gdbus_uart_call_write_sync( + uart_proxy, + uart->handle, + length, + g_data, + &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_io.xml b/src/peripheral_io.xml index ba2edef..c6343c4 100644 --- a/src/peripheral_io.xml +++ b/src/peripheral_io.xml @@ -124,4 +124,53 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/peripheral_uart.c b/src/peripheral_uart.c index 4082df6..66d96f0 100644 --- a/src/peripheral_uart.c +++ b/src/peripheral_uart.c @@ -13,3 +13,124 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + +#include +#include +#include +#include + +#include "peripheral_io.h" +#include "peripheral_gdbus_uart.h" +#include "peripheral_common.h" +#include "peripheral_internal.h" + +/** + * @brief Initializes uart communication and creates uart handle. + */ +int peripheral_uart_open(int port, peripheral_uart_h *uart) +{ + peripheral_uart_h handle; + int ret = PERIPHERAL_ERROR_NONE; + + if (port < 0) + return PERIPHERAL_ERROR_INVALID_PARAMETER; + + handle = (peripheral_uart_h)calloc(1, sizeof(struct _peripheral_uart_s)); + + if (handle == NULL) { + _E("Failed to allocate peripheral_uart_h"); + return PERIPHERAL_ERROR_OUT_OF_MEMORY; + } + + uart_proxy_init(); + + ret = peripheral_gdbus_uart_open(handle, port); + + if (ret != PERIPHERAL_ERROR_NONE) { + _E("[PERIPHERAL] UART open error\n"); + free(handle); + handle = NULL; + } + *uart = handle; + + return ret; +} + +/** + * @brief Destory the uart handle and release the communication. + */ +int peripheral_uart_close(peripheral_uart_h uart) +{ + int ret = PERIPHERAL_ERROR_NONE; + + if (uart == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + + ret = peripheral_gdbus_uart_close(uart); + uart_proxy_deinit(); + + free(uart); + uart = NULL; + + return ret; +} + +/** + * @brief Flush all input that has received but not yet been read by the uart + * device, or all output written but not transmitted to the uart device. + */ +int peripheral_uart_flush(peripheral_uart_h uart) +{ + if (uart == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + + return peripheral_gdbus_uart_flush(uart); +} + +/** + * @brief Sets baudrate of the uart device. + */ +int peripheral_uart_set_baudrate(peripheral_uart_h uart, peripheral_uart_baudrate_e baud) +{ + if (uart == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + + return peripheral_gdbus_uart_set_baudrate(uart, baud); +} + +/** + * @brief Sets baudrate of the uart device. + */ +int peripheral_uart_set_mode(peripheral_uart_h uart, peripheral_uart_bytesize_e bytesize, peripheral_uart_parity_e parity, peripheral_uart_stopbits_e stopbits) +{ + if (uart == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + + return peripheral_gdbus_uart_set_mode(uart, bytesize, parity, stopbits); +} + +/** + * @brief Sets baudrate of the uart device. + */ +int peripheral_uart_set_flowcontrol(peripheral_uart_h uart, bool xonxoff, bool rtscts) +{ + if (uart == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + + return peripheral_gdbus_uart_set_flowcontrol(uart, xonxoff, rtscts); +} + +/** + * @brief Reads data from the uart device. + */ +int peripheral_uart_read(peripheral_uart_h uart, uint8_t *data, int length) +{ + if (uart == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + + return peripheral_gdbus_uart_read(uart, data, length); +} + +/** + * @brief Write data to the uart device. + */ +int peripheral_uart_write(peripheral_uart_h uart, uint8_t *data, int length) +{ + if (uart == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + + return peripheral_gdbus_uart_write(uart, data, length); +} -- 2.34.1 From af3f4e67bb387d029922b18e853f3b831d467b77 Mon Sep 17 00:00:00 2001 From: "jino.cho" Date: Tue, 16 May 2017 21:02:51 +0900 Subject: [PATCH 15/16] Rename peripheral_pwm_context_h to peripheral_pwm_h Change-Id: I0e39d439d1095502b158b26a8be140aa3cad98fa Signed-off-by: jino.cho --- include/peripheral_gdbus_pwm.h | 14 +++++++------- include/peripheral_io.h | 23 ++++++++++++++--------- src/peripheral_gdbus_pwm.c | 14 +++++++------- src/peripheral_pwm.c | 22 +++++++++++----------- test/peripheral-io-test.c | 4 ++-- 5 files changed, 41 insertions(+), 36 deletions(-) diff --git a/include/peripheral_gdbus_pwm.h b/include/peripheral_gdbus_pwm.h index 2d28514..9a302ef 100644 --- a/include/peripheral_gdbus_pwm.h +++ b/include/peripheral_gdbus_pwm.h @@ -20,12 +20,12 @@ void pwm_proxy_init(void); void pwm_proxy_deinit(void); -int peripheral_gdbus_pwm_open(peripheral_pwm_context_h dev, int device, int channel); -int peripheral_gdbus_pwm_close(peripheral_pwm_context_h dev); -int peripheral_gdbus_pwm_get_duty_cycle(peripheral_pwm_context_h dev, int *duty_cycle); -int peripheral_gdbus_pwm_set_duty_cycle(peripheral_pwm_context_h dev, int duty_cycle); -int peripheral_gdbus_pwm_get_period(peripheral_pwm_context_h dev, int *period); -int peripheral_gdbus_pwm_set_period(peripheral_pwm_context_h dev, int period); -int peripheral_gdbus_pwm_set_enable(peripheral_pwm_context_h dev, peripheral_pwm_state_e enable); +int peripheral_gdbus_pwm_open(peripheral_pwm_h dev, int device, int channel); +int peripheral_gdbus_pwm_close(peripheral_pwm_h dev); +int peripheral_gdbus_pwm_get_duty_cycle(peripheral_pwm_h dev, int *duty_cycle); +int peripheral_gdbus_pwm_set_duty_cycle(peripheral_pwm_h dev, int duty_cycle); +int peripheral_gdbus_pwm_get_period(peripheral_pwm_h dev, int *period); +int peripheral_gdbus_pwm_set_period(peripheral_pwm_h dev, int period); +int peripheral_gdbus_pwm_set_enable(peripheral_pwm_h dev, peripheral_pwm_state_e enable); #endif /* __PERIPHERAL_GDBUS_PWM_H__ */ diff --git a/include/peripheral_io.h b/include/peripheral_io.h index a8ed110..a0b7fad 100644 --- a/include/peripheral_io.h +++ b/include/peripheral_io.h @@ -376,28 +376,33 @@ struct _peripheral_pwm_s { int duty_cycle; int enabled; }; -typedef struct _peripheral_pwm_s *peripheral_pwm_context_h; + +/** + * @brief The handle to the pwm device + * @since_tizen 4.0 + */ +typedef struct _peripheral_pwm_s *peripheral_pwm_h; typedef enum { PERIPHERAL_PWM_DISABLE = 0, PERIPHERAL_PWM_ENABLE, } peripheral_pwm_state_e; -peripheral_pwm_context_h peripheral_pwm_open(int device, int channel); +peripheral_pwm_h peripheral_pwm_open(int device, int channel); -int peripheral_pwm_close(peripheral_pwm_context_h pwm); +int peripheral_pwm_close(peripheral_pwm_h pwm); -int peripheral_pwm_set_duty_cycle(peripheral_pwm_context_h pwm, int duty_cycle); +int peripheral_pwm_set_duty_cycle(peripheral_pwm_h pwm, int duty_cycle); -int peripheral_pwm_set_period(peripheral_pwm_context_h pwm, int period); +int peripheral_pwm_set_period(peripheral_pwm_h pwm, int period); -int peripheral_pwm_set_enabled(peripheral_pwm_context_h pwm, peripheral_pwm_state_e enable); +int peripheral_pwm_set_enabled(peripheral_pwm_h pwm, peripheral_pwm_state_e enable); -int peripheral_pwm_is_enabled(peripheral_pwm_context_h pwm); +int peripheral_pwm_is_enabled(peripheral_pwm_h pwm); -int peripheral_pwm_get_duty_cycle(peripheral_pwm_context_h pwm, int *duty_cycle); +int peripheral_pwm_get_duty_cycle(peripheral_pwm_h pwm, int *duty_cycle); -int peripheral_pwm_get_period(peripheral_pwm_context_h pwm, int *period); +int peripheral_pwm_get_period(peripheral_pwm_h pwm, int *period); /** diff --git a/src/peripheral_gdbus_pwm.c b/src/peripheral_gdbus_pwm.c index db73dee..66883a2 100644 --- a/src/peripheral_gdbus_pwm.c +++ b/src/peripheral_gdbus_pwm.c @@ -49,7 +49,7 @@ void pwm_proxy_deinit() } } -int peripheral_gdbus_pwm_open(peripheral_pwm_context_h dev, int device, int channel) +int peripheral_gdbus_pwm_open(peripheral_pwm_h dev, int device, int channel) { GError *error = NULL; gint32 ret = PERIPHERAL_ERROR_NONE; @@ -72,7 +72,7 @@ int peripheral_gdbus_pwm_open(peripheral_pwm_context_h dev, int device, int chan return ret; } -int peripheral_gdbus_pwm_close(peripheral_pwm_context_h dev) +int peripheral_gdbus_pwm_close(peripheral_pwm_h dev) { GError *error = NULL; gint32 ret = PERIPHERAL_ERROR_NONE; @@ -95,7 +95,7 @@ int peripheral_gdbus_pwm_close(peripheral_pwm_context_h dev) return ret; } -int peripheral_gdbus_pwm_get_duty_cycle(peripheral_pwm_context_h dev, int *duty_cycle) +int peripheral_gdbus_pwm_get_duty_cycle(peripheral_pwm_h dev, int *duty_cycle) { GError *error = NULL; gint32 ret = PERIPHERAL_ERROR_NONE; @@ -118,7 +118,7 @@ int peripheral_gdbus_pwm_get_duty_cycle(peripheral_pwm_context_h dev, int *duty_ return ret; } -int peripheral_gdbus_pwm_set_duty_cycle(peripheral_pwm_context_h dev, int duty_cycle) +int peripheral_gdbus_pwm_set_duty_cycle(peripheral_pwm_h dev, int duty_cycle) { GError *error = NULL; gint32 ret = PERIPHERAL_ERROR_NONE; @@ -142,7 +142,7 @@ int peripheral_gdbus_pwm_set_duty_cycle(peripheral_pwm_context_h dev, int duty_c return ret; } -int peripheral_gdbus_pwm_get_period(peripheral_pwm_context_h dev, int *period) +int peripheral_gdbus_pwm_get_period(peripheral_pwm_h dev, int *period) { GError *error = NULL; gint32 ret = PERIPHERAL_ERROR_NONE; @@ -166,7 +166,7 @@ int peripheral_gdbus_pwm_get_period(peripheral_pwm_context_h dev, int *period) return ret; } -int peripheral_gdbus_pwm_set_period(peripheral_pwm_context_h dev, int period) +int peripheral_gdbus_pwm_set_period(peripheral_pwm_h dev, int period) { GError *error = NULL; gint32 ret = PERIPHERAL_ERROR_NONE; @@ -190,7 +190,7 @@ int peripheral_gdbus_pwm_set_period(peripheral_pwm_context_h dev, int period) return ret; } -int peripheral_gdbus_pwm_set_enable(peripheral_pwm_context_h dev, peripheral_pwm_state_e enable) +int peripheral_gdbus_pwm_set_enable(peripheral_pwm_h dev, peripheral_pwm_state_e enable) { GError *error = NULL; gint32 ret = PERIPHERAL_ERROR_NONE; diff --git a/src/peripheral_pwm.c b/src/peripheral_pwm.c index b3fa3a7..ea08352 100644 --- a/src/peripheral_pwm.c +++ b/src/peripheral_pwm.c @@ -26,19 +26,19 @@ #define PWM_ENABLE 1 #define PWM_DISABLE 0 -peripheral_pwm_context_h peripheral_pwm_open(int device, int channel) +peripheral_pwm_h peripheral_pwm_open(int device, int channel) { - peripheral_pwm_context_h dev = NULL; + peripheral_pwm_h dev = NULL; int ret = PERIPHERAL_ERROR_NONE; assert(device >= 0); assert(channel >= 0); /* Initialize */ - dev = (peripheral_pwm_context_h)malloc(sizeof(struct _peripheral_pwm_s)); + dev = (peripheral_pwm_h)malloc(sizeof(struct _peripheral_pwm_s)); if (dev == NULL) { - _E("Failed to allocate peripheral_pwm_context_h"); + _E("Failed to allocate peripheral_pwm_h"); return NULL; } @@ -57,7 +57,7 @@ peripheral_pwm_context_h peripheral_pwm_open(int device, int channel) return dev; } -int peripheral_pwm_close(peripheral_pwm_context_h pwm) +int peripheral_pwm_close(peripheral_pwm_h pwm) { int ret = PERIPHERAL_ERROR_NONE; @@ -73,7 +73,7 @@ int peripheral_pwm_close(peripheral_pwm_context_h pwm) } -int peripheral_pwm_set_duty_cycle(peripheral_pwm_context_h pwm, int duty_cycle) +int peripheral_pwm_set_duty_cycle(peripheral_pwm_h pwm, int duty_cycle) { int ret = PERIPHERAL_ERROR_NONE; @@ -85,7 +85,7 @@ int peripheral_pwm_set_duty_cycle(peripheral_pwm_context_h pwm, int duty_cycle) return ret; } -int peripheral_pwm_set_period(peripheral_pwm_context_h pwm, int period) +int peripheral_pwm_set_period(peripheral_pwm_h pwm, int period) { int ret = PERIPHERAL_ERROR_NONE; @@ -97,7 +97,7 @@ int peripheral_pwm_set_period(peripheral_pwm_context_h pwm, int period) return ret; } -int peripheral_pwm_set_enabled(peripheral_pwm_context_h pwm, peripheral_pwm_state_e enable) +int peripheral_pwm_set_enabled(peripheral_pwm_h pwm, peripheral_pwm_state_e enable) { int ret = PERIPHERAL_ERROR_NONE; @@ -109,7 +109,7 @@ int peripheral_pwm_set_enabled(peripheral_pwm_context_h pwm, peripheral_pwm_stat return PERIPHERAL_ERROR_NONE; } -int peripheral_pwm_is_enabled(peripheral_pwm_context_h pwm) +int peripheral_pwm_is_enabled(peripheral_pwm_h pwm) { if (pwm->enabled == PWM_ENABLE) return PWM_ENABLE; @@ -117,7 +117,7 @@ int peripheral_pwm_is_enabled(peripheral_pwm_context_h pwm) return PWM_DISABLE; } -int peripheral_pwm_get_duty_cycle(peripheral_pwm_context_h pwm, int *duty_cycle) +int peripheral_pwm_get_duty_cycle(peripheral_pwm_h pwm, int *duty_cycle) { int ret = PERIPHERAL_ERROR_NONE; @@ -129,7 +129,7 @@ int peripheral_pwm_get_duty_cycle(peripheral_pwm_context_h pwm, int *duty_cycle) return ret; } -int peripheral_pwm_get_period(peripheral_pwm_context_h pwm, int *period) +int peripheral_pwm_get_period(peripheral_pwm_h pwm, int *period) { int ret = PERIPHERAL_ERROR_NONE; diff --git a/test/peripheral-io-test.c b/test/peripheral-io-test.c index bec2885..21d0a8b 100644 --- a/test/peripheral-io-test.c +++ b/test/peripheral-io-test.c @@ -220,7 +220,7 @@ int pwm_test_led(void) int set_duty_cycle; int get_period, get_duty_cycle; - peripheral_pwm_context_h dev; + peripheral_pwm_h dev; printf("<<< pwm_test >>>\n"); @@ -261,7 +261,7 @@ int pwm_test_motor(void) int duty_cycle = 1500000; int cnt = 0, idx = 0; int degree[3] = {0, 45, 90}; - peripheral_pwm_context_h dev; + peripheral_pwm_h dev; printf("<<< pwm_test_motor >>>\n"); -- 2.34.1 From f4670f9a86498e01e5a88701b03f3798334bd3a6 Mon Sep 17 00:00:00 2001 From: "jino.cho" Date: Tue, 16 May 2017 19:16:42 +0900 Subject: [PATCH 16/16] Return pwm handle through pointer argument Change-Id: I3b62a841edaafe2298592132cb2fdd8ed23a9694 Signed-off-by: jino.cho --- include/peripheral_io.h | 2 +- src/peripheral_pwm.c | 8 +++++--- test/peripheral-io-test.c | 4 ++-- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/include/peripheral_io.h b/include/peripheral_io.h index a0b7fad..95bc38a 100644 --- a/include/peripheral_io.h +++ b/include/peripheral_io.h @@ -388,7 +388,7 @@ typedef enum { PERIPHERAL_PWM_ENABLE, } peripheral_pwm_state_e; -peripheral_pwm_h peripheral_pwm_open(int device, int channel); +int peripheral_pwm_open(int device, int channel, peripheral_pwm_h *pwm); int peripheral_pwm_close(peripheral_pwm_h pwm); diff --git a/src/peripheral_pwm.c b/src/peripheral_pwm.c index ea08352..9c291b0 100644 --- a/src/peripheral_pwm.c +++ b/src/peripheral_pwm.c @@ -26,7 +26,7 @@ #define PWM_ENABLE 1 #define PWM_DISABLE 0 -peripheral_pwm_h peripheral_pwm_open(int device, int channel) +int peripheral_pwm_open(int device, int channel, peripheral_pwm_h* pwm) { peripheral_pwm_h dev = NULL; int ret = PERIPHERAL_ERROR_NONE; @@ -39,7 +39,7 @@ peripheral_pwm_h peripheral_pwm_open(int device, int channel) if (dev == NULL) { _E("Failed to allocate peripheral_pwm_h"); - return NULL; + return PERIPHERAL_ERROR_OUT_OF_MEMORY; } pwm_proxy_init(); @@ -50,11 +50,13 @@ peripheral_pwm_h peripheral_pwm_open(int device, int channel) ret = peripheral_gdbus_pwm_open(dev, device, channel); if (ret != PERIPHERAL_ERROR_NONE) { + _E("PWM open error (%d, %d)", device, channel); free(dev); dev = NULL; } + *pwm = dev; - return dev; + return ret; } int peripheral_pwm_close(peripheral_pwm_h pwm) diff --git a/test/peripheral-io-test.c b/test/peripheral-io-test.c index 21d0a8b..699be4a 100644 --- a/test/peripheral-io-test.c +++ b/test/peripheral-io-test.c @@ -224,7 +224,7 @@ int pwm_test_led(void) printf("<<< pwm_test >>>\n"); - dev = peripheral_pwm_open(device, channel); + peripheral_pwm_open(device, channel, &dev); peripheral_pwm_set_period(dev, period); /* period: nanosecond */ peripheral_pwm_set_duty_cycle(dev, duty_cycle); /* duty_cycle: nanosecond */ peripheral_pwm_set_enabled(dev, 1); /* 0: disable, 1: enable */ @@ -265,7 +265,7 @@ int pwm_test_motor(void) printf("<<< pwm_test_motor >>>\n"); - dev = peripheral_pwm_open(device, channel); + peripheral_pwm_open(device, channel, &dev); for (cnt = 0; cnt < 5; cnt++) { for (idx = 0; idx < 3; idx++) { switch (degree[idx]) { -- 2.34.1