From e9a0d6bce9e00a8049ed478b88f19196b7694dc9 Mon Sep 17 00:00:00 2001 From: Hyeongsik Min Date: Wed, 26 Apr 2017 20:50:23 +0900 Subject: [PATCH 01/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.7.4 From 1762a85eacbe1aa7f7e4cd82d0030d41793f7847 Mon Sep 17 00:00:00 2001 From: "jino.cho" Date: Thu, 20 Apr 2017 10:33:55 +0900 Subject: [PATCH 02/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.7.4 From 2e470160b2df2af956825f72750ab14f3f415e55 Mon Sep 17 00:00:00 2001 From: Hyeongsik Min Date: Sun, 7 May 2017 18:03:51 +0900 Subject: [PATCH 03/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.7.4 From 05a568491a938223e095bc7244384d14a2c05b32 Mon Sep 17 00:00:00 2001 From: "jino.cho" Date: Mon, 8 May 2017 17:26:27 +0900 Subject: [PATCH 04/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.7.4 From 552f5ec036a31bcc09e2417055274daaab046213 Mon Sep 17 00:00:00 2001 From: Hyeongsik Min Date: Mon, 8 May 2017 20:15:55 +0900 Subject: [PATCH 05/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.7.4 From 86db2fdda86f5c20bd043892e0dc3571c95738f4 Mon Sep 17 00:00:00 2001 From: "jino.cho" Date: Fri, 12 May 2017 15:34:54 +0900 Subject: [PATCH 06/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.7.4 From 76465a52999fcbe37dadd3d24bf29ab015501097 Mon Sep 17 00:00:00 2001 From: "jino.cho" Date: Fri, 12 May 2017 15:43:49 +0900 Subject: [PATCH 07/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.7.4 From af3f4e67bb387d029922b18e853f3b831d467b77 Mon Sep 17 00:00:00 2001 From: "jino.cho" Date: Tue, 16 May 2017 21:02:51 +0900 Subject: [PATCH 08/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.7.4 From f4670f9a86498e01e5a88701b03f3798334bd3a6 Mon Sep 17 00:00:00 2001 From: "jino.cho" Date: Tue, 16 May 2017 19:16:42 +0900 Subject: [PATCH 09/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.7.4 From 6b081a0e2716e7168170725770067202d94b1a2d Mon Sep 17 00:00:00 2001 From: "jino.cho" Date: Tue, 16 May 2017 19:29:20 +0900 Subject: [PATCH 10/16] Rearrange the order of pwm API functions Change-Id: I2adeabd3eb2b085fc2e672e9f4c66436b9acbe30 Signed-off-by: jino.cho --- include/peripheral_gdbus_pwm.h | 6 +++--- include/peripheral_io.h | 8 ++++---- src/peripheral_gdbus_pwm.c | 25 ++++++++++++----------- src/peripheral_pwm.c | 46 +++++++++++++++++++++--------------------- 4 files changed, 43 insertions(+), 42 deletions(-) diff --git a/include/peripheral_gdbus_pwm.h b/include/peripheral_gdbus_pwm.h index 9a302ef..2f0be40 100644 --- a/include/peripheral_gdbus_pwm.h +++ b/include/peripheral_gdbus_pwm.h @@ -22,10 +22,10 @@ void pwm_proxy_deinit(void); 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_get_period(peripheral_pwm_h dev, int *period); +int peripheral_gdbus_pwm_set_duty_cycle(peripheral_pwm_h dev, int duty_cycle); +int peripheral_gdbus_pwm_get_duty_cycle(peripheral_pwm_h dev, int *duty_cycle); 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 95bc38a..e9ee0ea 100644 --- a/include/peripheral_io.h +++ b/include/peripheral_io.h @@ -392,18 +392,18 @@ int peripheral_pwm_open(int device, int channel, peripheral_pwm_h *pwm); int peripheral_pwm_close(peripheral_pwm_h pwm); -int peripheral_pwm_set_duty_cycle(peripheral_pwm_h pwm, int duty_cycle); int peripheral_pwm_set_period(peripheral_pwm_h pwm, int period); -int peripheral_pwm_set_enabled(peripheral_pwm_h pwm, peripheral_pwm_state_e enable); +int peripheral_pwm_get_period(peripheral_pwm_h pwm, int *period); -int peripheral_pwm_is_enabled(peripheral_pwm_h pwm); +int peripheral_pwm_set_duty_cycle(peripheral_pwm_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_h pwm, int *period); +int peripheral_pwm_set_enabled(peripheral_pwm_h pwm, peripheral_pwm_state_e enable); +int peripheral_pwm_is_enabled(peripheral_pwm_h pwm); /** * @} diff --git a/src/peripheral_gdbus_pwm.c b/src/peripheral_gdbus_pwm.c index 66883a2..86962a6 100644 --- a/src/peripheral_gdbus_pwm.c +++ b/src/peripheral_gdbus_pwm.c @@ -95,7 +95,7 @@ int peripheral_gdbus_pwm_close(peripheral_pwm_h dev) return ret; } -int peripheral_gdbus_pwm_get_duty_cycle(peripheral_pwm_h dev, int *duty_cycle) +int peripheral_gdbus_pwm_set_period(peripheral_pwm_h dev, int period) { GError *error = NULL; gint32 ret = PERIPHERAL_ERROR_NONE; @@ -103,11 +103,11 @@ int peripheral_gdbus_pwm_get_duty_cycle(peripheral_pwm_h dev, int *duty_cycle) if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; /* TODO: Need to reorganize arguments */ - if (peripheral_io_gdbus_pwm_call_get_duty_cycle_sync( + if (peripheral_io_gdbus_pwm_call_set_period_sync( pwm_proxy, dev->device, dev->channel, - duty_cycle, + period, &ret, NULL, &error) == FALSE) { @@ -118,7 +118,8 @@ int peripheral_gdbus_pwm_get_duty_cycle(peripheral_pwm_h dev, int *duty_cycle) return ret; } -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) { GError *error = NULL; gint32 ret = PERIPHERAL_ERROR_NONE; @@ -126,11 +127,11 @@ int peripheral_gdbus_pwm_set_duty_cycle(peripheral_pwm_h dev, int duty_cycle) if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; /* TODO: Need to reorganize arguments */ - if (peripheral_io_gdbus_pwm_call_set_duty_cycle_sync( + if (peripheral_io_gdbus_pwm_call_get_period_sync( pwm_proxy, dev->device, dev->channel, - duty_cycle, + period, &ret, NULL, &error) == FALSE) { @@ -142,7 +143,7 @@ int peripheral_gdbus_pwm_set_duty_cycle(peripheral_pwm_h dev, int duty_cycle) return ret; } -int peripheral_gdbus_pwm_get_period(peripheral_pwm_h dev, int *period) +int peripheral_gdbus_pwm_set_duty_cycle(peripheral_pwm_h dev, int duty_cycle) { GError *error = NULL; gint32 ret = PERIPHERAL_ERROR_NONE; @@ -150,11 +151,11 @@ int peripheral_gdbus_pwm_get_period(peripheral_pwm_h dev, int *period) if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; /* TODO: Need to reorganize arguments */ - if (peripheral_io_gdbus_pwm_call_get_period_sync( + if (peripheral_io_gdbus_pwm_call_set_duty_cycle_sync( pwm_proxy, dev->device, dev->channel, - period, + duty_cycle, &ret, NULL, &error) == FALSE) { @@ -166,7 +167,7 @@ int peripheral_gdbus_pwm_get_period(peripheral_pwm_h dev, int *period) return ret; } -int peripheral_gdbus_pwm_set_period(peripheral_pwm_h dev, int period) +int peripheral_gdbus_pwm_get_duty_cycle(peripheral_pwm_h dev, int *duty_cycle) { GError *error = NULL; gint32 ret = PERIPHERAL_ERROR_NONE; @@ -174,11 +175,11 @@ int peripheral_gdbus_pwm_set_period(peripheral_pwm_h dev, int period) if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; /* TODO: Need to reorganize arguments */ - if (peripheral_io_gdbus_pwm_call_set_period_sync( + if (peripheral_io_gdbus_pwm_call_get_duty_cycle_sync( pwm_proxy, dev->device, dev->channel, - period, + duty_cycle, &ret, NULL, &error) == FALSE) { diff --git a/src/peripheral_pwm.c b/src/peripheral_pwm.c index 9c291b0..a7c4206 100644 --- a/src/peripheral_pwm.c +++ b/src/peripheral_pwm.c @@ -74,49 +74,40 @@ int peripheral_pwm_close(peripheral_pwm_h pwm) return ret; } - -int peripheral_pwm_set_duty_cycle(peripheral_pwm_h pwm, int duty_cycle) +int peripheral_pwm_set_period(peripheral_pwm_h pwm, int period) { int ret = PERIPHERAL_ERROR_NONE; - ret = peripheral_gdbus_pwm_set_duty_cycle(pwm, duty_cycle); + ret = peripheral_gdbus_pwm_set_period(pwm, period); if (ret != PERIPHERAL_ERROR_NONE) - pwm->duty_cycle = duty_cycle; + pwm->period = period; return ret; } -int peripheral_pwm_set_period(peripheral_pwm_h pwm, int period) +int peripheral_pwm_get_period(peripheral_pwm_h pwm, int *period) { int ret = PERIPHERAL_ERROR_NONE; - ret = peripheral_gdbus_pwm_set_period(pwm, period); + ret = peripheral_gdbus_pwm_get_period(pwm, period); if (ret != PERIPHERAL_ERROR_NONE) - pwm->period = period; + pwm->period = *period; return ret; } -int peripheral_pwm_set_enabled(peripheral_pwm_h pwm, peripheral_pwm_state_e enable) +int peripheral_pwm_set_duty_cycle(peripheral_pwm_h pwm, int duty_cycle) { int ret = PERIPHERAL_ERROR_NONE; - ret = peripheral_gdbus_pwm_set_enable(pwm, enable); + ret = peripheral_gdbus_pwm_set_duty_cycle(pwm, duty_cycle); if (ret != PERIPHERAL_ERROR_NONE) - pwm->enabled = enable; - - return PERIPHERAL_ERROR_NONE; -} + pwm->duty_cycle = duty_cycle; -int peripheral_pwm_is_enabled(peripheral_pwm_h pwm) -{ - if (pwm->enabled == PWM_ENABLE) - return PWM_ENABLE; - else - return PWM_DISABLE; + return ret; } int peripheral_pwm_get_duty_cycle(peripheral_pwm_h pwm, int *duty_cycle) @@ -131,14 +122,23 @@ int peripheral_pwm_get_duty_cycle(peripheral_pwm_h pwm, int *duty_cycle) return ret; } -int peripheral_pwm_get_period(peripheral_pwm_h pwm, int *period) +int peripheral_pwm_set_enabled(peripheral_pwm_h pwm, peripheral_pwm_state_e enable) { int ret = PERIPHERAL_ERROR_NONE; - ret = peripheral_gdbus_pwm_get_period(pwm, period); + ret = peripheral_gdbus_pwm_set_enable(pwm, enable); if (ret != PERIPHERAL_ERROR_NONE) - pwm->period = *period; + pwm->enabled = enable; - return ret; + return PERIPHERAL_ERROR_NONE; +} + +int peripheral_pwm_is_enabled(peripheral_pwm_h pwm) +{ + if (pwm->enabled == PWM_ENABLE) + return PWM_ENABLE; + else + return PWM_DISABLE; } + -- 2.7.4 From c4fb9a724b2aef028ce2ade0f36229d967f933c0 Mon Sep 17 00:00:00 2001 From: "jino.cho" Date: Tue, 16 May 2017 19:55:27 +0900 Subject: [PATCH 11/16] Change parameter name dev to pwm Change-Id: Ia015376523dcc505d355b22b9d40eaa51a0308bc Signed-off-by: jino.cho --- include/peripheral_gdbus_pwm.h | 14 +++++++------- src/peripheral_gdbus_pwm.c | 38 +++++++++++++++++++------------------- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/include/peripheral_gdbus_pwm.h b/include/peripheral_gdbus_pwm.h index 2f0be40..9b6dc02 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_h dev, int device, int channel); -int peripheral_gdbus_pwm_close(peripheral_pwm_h dev); -int peripheral_gdbus_pwm_set_period(peripheral_pwm_h dev, int period); -int peripheral_gdbus_pwm_get_period(peripheral_pwm_h dev, int *period); -int peripheral_gdbus_pwm_set_duty_cycle(peripheral_pwm_h dev, int duty_cycle); -int peripheral_gdbus_pwm_get_duty_cycle(peripheral_pwm_h dev, int *duty_cycle); -int peripheral_gdbus_pwm_set_enable(peripheral_pwm_h dev, peripheral_pwm_state_e enable); +int peripheral_gdbus_pwm_open(peripheral_pwm_h pwm, int device, int channel); +int peripheral_gdbus_pwm_close(peripheral_pwm_h pwm); +int peripheral_gdbus_pwm_set_period(peripheral_pwm_h pwm, int period); +int peripheral_gdbus_pwm_get_period(peripheral_pwm_h pwm, int *period); +int peripheral_gdbus_pwm_set_duty_cycle(peripheral_pwm_h pwm, int duty_cycle); +int peripheral_gdbus_pwm_get_duty_cycle(peripheral_pwm_h pwm, int *duty_cycle); +int peripheral_gdbus_pwm_set_enable(peripheral_pwm_h pwm, peripheral_pwm_state_e enable); #endif /* __PERIPHERAL_GDBUS_PWM_H__ */ diff --git a/src/peripheral_gdbus_pwm.c b/src/peripheral_gdbus_pwm.c index 86962a6..76cc7a9 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_h dev, int device, int channel) +int peripheral_gdbus_pwm_open(peripheral_pwm_h pwm, int device, int channel) { GError *error = NULL; gint32 ret = PERIPHERAL_ERROR_NONE; @@ -72,7 +72,7 @@ int peripheral_gdbus_pwm_open(peripheral_pwm_h dev, int device, int channel) return ret; } -int peripheral_gdbus_pwm_close(peripheral_pwm_h dev) +int peripheral_gdbus_pwm_close(peripheral_pwm_h pwm) { GError *error = NULL; gint32 ret = PERIPHERAL_ERROR_NONE; @@ -82,8 +82,8 @@ int peripheral_gdbus_pwm_close(peripheral_pwm_h dev) /* TODO: Need to reorganize arguments */ if (peripheral_io_gdbus_pwm_call_close_sync( pwm_proxy, - dev->device, - dev->channel, + pwm->device, + pwm->channel, &ret, NULL, &error) == FALSE) { @@ -95,7 +95,7 @@ int peripheral_gdbus_pwm_close(peripheral_pwm_h dev) return ret; } -int peripheral_gdbus_pwm_set_period(peripheral_pwm_h dev, int period) +int peripheral_gdbus_pwm_set_period(peripheral_pwm_h pwm, int period) { GError *error = NULL; gint32 ret = PERIPHERAL_ERROR_NONE; @@ -105,8 +105,8 @@ int peripheral_gdbus_pwm_set_period(peripheral_pwm_h dev, int period) /* TODO: Need to reorganize arguments */ if (peripheral_io_gdbus_pwm_call_set_period_sync( pwm_proxy, - dev->device, - dev->channel, + pwm->device, + pwm->channel, period, &ret, NULL, @@ -119,7 +119,7 @@ int peripheral_gdbus_pwm_set_period(peripheral_pwm_h dev, int period) return ret; } -int peripheral_gdbus_pwm_get_period(peripheral_pwm_h dev, int *period) +int peripheral_gdbus_pwm_get_period(peripheral_pwm_h pwm, int *period) { GError *error = NULL; gint32 ret = PERIPHERAL_ERROR_NONE; @@ -129,8 +129,8 @@ int peripheral_gdbus_pwm_get_period(peripheral_pwm_h dev, int *period) /* TODO: Need to reorganize arguments */ if (peripheral_io_gdbus_pwm_call_get_period_sync( pwm_proxy, - dev->device, - dev->channel, + pwm->device, + pwm->channel, period, &ret, NULL, @@ -143,7 +143,7 @@ int peripheral_gdbus_pwm_get_period(peripheral_pwm_h dev, int *period) return ret; } -int peripheral_gdbus_pwm_set_duty_cycle(peripheral_pwm_h dev, int duty_cycle) +int peripheral_gdbus_pwm_set_duty_cycle(peripheral_pwm_h pwm, int duty_cycle) { GError *error = NULL; gint32 ret = PERIPHERAL_ERROR_NONE; @@ -153,8 +153,8 @@ int peripheral_gdbus_pwm_set_duty_cycle(peripheral_pwm_h dev, int duty_cycle) /* TODO: Need to reorganize arguments */ if (peripheral_io_gdbus_pwm_call_set_duty_cycle_sync( pwm_proxy, - dev->device, - dev->channel, + pwm->device, + pwm->channel, duty_cycle, &ret, NULL, @@ -167,7 +167,7 @@ int peripheral_gdbus_pwm_set_duty_cycle(peripheral_pwm_h dev, int duty_cycle) return ret; } -int peripheral_gdbus_pwm_get_duty_cycle(peripheral_pwm_h dev, int *duty_cycle) +int peripheral_gdbus_pwm_get_duty_cycle(peripheral_pwm_h pwm, int *duty_cycle) { GError *error = NULL; gint32 ret = PERIPHERAL_ERROR_NONE; @@ -177,8 +177,8 @@ int peripheral_gdbus_pwm_get_duty_cycle(peripheral_pwm_h dev, int *duty_cycle) /* TODO: Need to reorganize arguments */ if (peripheral_io_gdbus_pwm_call_get_duty_cycle_sync( pwm_proxy, - dev->device, - dev->channel, + pwm->device, + pwm->channel, duty_cycle, &ret, NULL, @@ -191,7 +191,7 @@ int peripheral_gdbus_pwm_get_duty_cycle(peripheral_pwm_h dev, int *duty_cycle) return ret; } -int peripheral_gdbus_pwm_set_enable(peripheral_pwm_h dev, peripheral_pwm_state_e enable) +int peripheral_gdbus_pwm_set_enable(peripheral_pwm_h pwm, peripheral_pwm_state_e enable) { GError *error = NULL; gint32 ret = PERIPHERAL_ERROR_NONE; @@ -201,8 +201,8 @@ int peripheral_gdbus_pwm_set_enable(peripheral_pwm_h dev, peripheral_pwm_state_e /* TODO: Need to reorganize arguments */ if (peripheral_io_gdbus_pwm_call_set_enable_sync( pwm_proxy, - dev->device, - dev->channel, + pwm->device, + pwm->channel, enable, &ret, NULL, -- 2.7.4 From 872d1500364bd2b3adb07e22a0098b54aaf750a4 Mon Sep 17 00:00:00 2001 From: "jino.cho" Date: Tue, 16 May 2017 20:49:28 +0900 Subject: [PATCH 12/16] Change argument of pwm gdbus method - The pwm methods will pass handle instead of device informations. - The pwm enable(set/get) functions were modified. Change-Id: I54aa6444f6d22f1ba9ee1bf30391d2c04ff9a4f7 Signed-off-by: jino.cho --- include/peripheral_gdbus_pwm.h | 1 + include/peripheral_internal.h | 7 ++++ include/peripheral_io.h | 12 ++----- src/peripheral_gdbus_pwm.c | 46 ++++++++++++++++++-------- src/peripheral_io.xml | 40 +++++++++++----------- src/peripheral_pwm.c | 75 +++++++++++++----------------------------- test/peripheral-io-test.c | 8 ++--- 7 files changed, 89 insertions(+), 100 deletions(-) diff --git a/include/peripheral_gdbus_pwm.h b/include/peripheral_gdbus_pwm.h index 9b6dc02..7096af2 100644 --- a/include/peripheral_gdbus_pwm.h +++ b/include/peripheral_gdbus_pwm.h @@ -27,5 +27,6 @@ int peripheral_gdbus_pwm_get_period(peripheral_pwm_h pwm, int *period); int peripheral_gdbus_pwm_set_duty_cycle(peripheral_pwm_h pwm, int duty_cycle); int peripheral_gdbus_pwm_get_duty_cycle(peripheral_pwm_h pwm, int *duty_cycle); int peripheral_gdbus_pwm_set_enable(peripheral_pwm_h pwm, peripheral_pwm_state_e enable); +int peripheral_gdbus_pwm_get_enable(peripheral_pwm_h pwm, peripheral_pwm_state_e *enable); #endif /* __PERIPHERAL_GDBUS_PWM_H__ */ diff --git a/include/peripheral_internal.h b/include/peripheral_internal.h index 290fb28..85263aa 100644 --- a/include/peripheral_internal.h +++ b/include/peripheral_internal.h @@ -34,6 +34,13 @@ struct _peripheral_i2c_s { }; /** + * @brief Internal struct for pwm context + */ +struct _peripheral_pwm_s { + uint handle; +}; + +/** * @brief Internal struct for uart context */ struct _peripheral_uart_s { diff --git a/include/peripheral_io.h b/include/peripheral_io.h index e9ee0ea..c6838e7 100644 --- a/include/peripheral_io.h +++ b/include/peripheral_io.h @@ -369,14 +369,6 @@ int peripheral_i2c_write(peripheral_i2c_h i2c, uint8_t *data, int length); * @{ */ -struct _peripheral_pwm_s { - int device; - int channel; - int period; - int duty_cycle; - int enabled; -}; - /** * @brief The handle to the pwm device * @since_tizen 4.0 @@ -401,9 +393,9 @@ int peripheral_pwm_set_duty_cycle(peripheral_pwm_h pwm, int duty_cycle); int peripheral_pwm_get_duty_cycle(peripheral_pwm_h pwm, int *duty_cycle); -int peripheral_pwm_set_enabled(peripheral_pwm_h pwm, peripheral_pwm_state_e enable); +int peripheral_pwm_set_enable(peripheral_pwm_h pwm, peripheral_pwm_state_e enable); -int peripheral_pwm_is_enabled(peripheral_pwm_h pwm); +int peripheral_pwm_get_enable(peripheral_pwm_h pwm, peripheral_pwm_state_e *enable); /** * @} diff --git a/src/peripheral_gdbus_pwm.c b/src/peripheral_gdbus_pwm.c index 76cc7a9..76f4a5b 100644 --- a/src/peripheral_gdbus_pwm.c +++ b/src/peripheral_gdbus_pwm.c @@ -61,6 +61,7 @@ int peripheral_gdbus_pwm_open(peripheral_pwm_h pwm, int device, int channel) pwm_proxy, device, channel, + &pwm->handle, &ret, NULL, &error) == FALSE) { @@ -82,8 +83,7 @@ int peripheral_gdbus_pwm_close(peripheral_pwm_h pwm) /* TODO: Need to reorganize arguments */ if (peripheral_io_gdbus_pwm_call_close_sync( pwm_proxy, - pwm->device, - pwm->channel, + pwm->handle, &ret, NULL, &error) == FALSE) { @@ -105,8 +105,7 @@ int peripheral_gdbus_pwm_set_period(peripheral_pwm_h pwm, int period) /* TODO: Need to reorganize arguments */ if (peripheral_io_gdbus_pwm_call_set_period_sync( pwm_proxy, - pwm->device, - pwm->channel, + pwm->handle, period, &ret, NULL, @@ -129,9 +128,8 @@ int peripheral_gdbus_pwm_get_period(peripheral_pwm_h pwm, int *period) /* TODO: Need to reorganize arguments */ if (peripheral_io_gdbus_pwm_call_get_period_sync( pwm_proxy, - pwm->device, - pwm->channel, - period, + pwm->handle, + (gint*)period, &ret, NULL, &error) == FALSE) { @@ -153,8 +151,7 @@ int peripheral_gdbus_pwm_set_duty_cycle(peripheral_pwm_h pwm, int duty_cycle) /* TODO: Need to reorganize arguments */ if (peripheral_io_gdbus_pwm_call_set_duty_cycle_sync( pwm_proxy, - pwm->device, - pwm->channel, + pwm->handle, duty_cycle, &ret, NULL, @@ -177,9 +174,8 @@ int peripheral_gdbus_pwm_get_duty_cycle(peripheral_pwm_h pwm, int *duty_cycle) /* TODO: Need to reorganize arguments */ if (peripheral_io_gdbus_pwm_call_get_duty_cycle_sync( pwm_proxy, - pwm->device, - pwm->channel, - duty_cycle, + pwm->handle, + (gint*)duty_cycle, &ret, NULL, &error) == FALSE) { @@ -201,8 +197,7 @@ int peripheral_gdbus_pwm_set_enable(peripheral_pwm_h pwm, peripheral_pwm_state_e /* TODO: Need to reorganize arguments */ if (peripheral_io_gdbus_pwm_call_set_enable_sync( pwm_proxy, - pwm->device, - pwm->channel, + pwm->handle, enable, &ret, NULL, @@ -214,3 +209,26 @@ int peripheral_gdbus_pwm_set_enable(peripheral_pwm_h pwm, peripheral_pwm_state_e return ret; } + +int peripheral_gdbus_pwm_get_enable(peripheral_pwm_h pwm, bool *enable) +{ + GError *error = NULL; + peripheral_error_e ret = PERIPHERAL_ERROR_NONE; + + if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + if (peripheral_io_gdbus_pwm_call_get_enable_sync( + pwm_proxy, + pwm->handle, + (gint*)enable, + &ret, + NULL, + &error) == FALSE) { + _E("%s", 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 c6343c4..285c6f5 100644 --- a/src/peripheral_io.xml +++ b/src/peripheral_io.xml @@ -86,43 +86,43 @@ + - - - - - - - - + - - - - + + + - - + - - - - + + + + + + + + - - + + + + + + diff --git a/src/peripheral_pwm.c b/src/peripheral_pwm.c index a7c4206..45f29fc 100644 --- a/src/peripheral_pwm.c +++ b/src/peripheral_pwm.c @@ -22,39 +22,37 @@ #include "peripheral_io.h" #include "peripheral_gdbus_pwm.h" #include "peripheral_common.h" +#include "peripheral_internal.h" #define PWM_ENABLE 1 #define PWM_DISABLE 0 int peripheral_pwm_open(int device, int channel, peripheral_pwm_h* pwm) { - peripheral_pwm_h dev = NULL; + peripheral_pwm_h handle; int ret = PERIPHERAL_ERROR_NONE; assert(device >= 0); assert(channel >= 0); /* Initialize */ - dev = (peripheral_pwm_h)malloc(sizeof(struct _peripheral_pwm_s)); + handle = (peripheral_pwm_h)calloc(1, sizeof(struct _peripheral_pwm_s)); - if (dev == NULL) { + if (handle == NULL) { _E("Failed to allocate peripheral_pwm_h"); return PERIPHERAL_ERROR_OUT_OF_MEMORY; } pwm_proxy_init(); - dev->device = device; - dev->channel = channel; - - ret = peripheral_gdbus_pwm_open(dev, device, channel); + ret = peripheral_gdbus_pwm_open(handle, device, channel); if (ret != PERIPHERAL_ERROR_NONE) { _E("PWM open error (%d, %d)", device, channel); - free(dev); - dev = NULL; + free(handle); + handle = NULL; } - *pwm = dev; + *pwm = handle; return ret; } @@ -76,69 +74,42 @@ int peripheral_pwm_close(peripheral_pwm_h pwm) int peripheral_pwm_set_period(peripheral_pwm_h pwm, int period) { - int ret = PERIPHERAL_ERROR_NONE; - - ret = peripheral_gdbus_pwm_set_period(pwm, period); + if (pwm == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; - if (ret != PERIPHERAL_ERROR_NONE) - pwm->period = period; - - return ret; + return peripheral_gdbus_pwm_set_period(pwm, period); } int peripheral_pwm_get_period(peripheral_pwm_h pwm, int *period) { - int ret = PERIPHERAL_ERROR_NONE; + if (pwm == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; - ret = peripheral_gdbus_pwm_get_period(pwm, period); - - if (ret != PERIPHERAL_ERROR_NONE) - pwm->period = *period; - - return ret; + return peripheral_gdbus_pwm_get_period(pwm, period); } int peripheral_pwm_set_duty_cycle(peripheral_pwm_h pwm, int duty_cycle) { - int ret = PERIPHERAL_ERROR_NONE; - - ret = peripheral_gdbus_pwm_set_duty_cycle(pwm, duty_cycle); + if (pwm == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; - if (ret != PERIPHERAL_ERROR_NONE) - pwm->duty_cycle = duty_cycle; - - return ret; + return peripheral_gdbus_pwm_set_duty_cycle(pwm, duty_cycle); } int peripheral_pwm_get_duty_cycle(peripheral_pwm_h pwm, int *duty_cycle) { - int ret = PERIPHERAL_ERROR_NONE; + if (pwm == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; - ret = peripheral_gdbus_pwm_get_duty_cycle(pwm, duty_cycle); - - if (ret != PERIPHERAL_ERROR_NONE) - pwm->duty_cycle = *duty_cycle; - - return ret; + return peripheral_gdbus_pwm_get_duty_cycle(pwm, duty_cycle); } -int peripheral_pwm_set_enabled(peripheral_pwm_h pwm, peripheral_pwm_state_e enable) +int peripheral_pwm_set_enable(peripheral_pwm_h pwm, peripheral_pwm_state_e enable) { - int ret = PERIPHERAL_ERROR_NONE; - - ret = peripheral_gdbus_pwm_set_enable(pwm, enable); - - if (ret != PERIPHERAL_ERROR_NONE) - pwm->enabled = enable; + if (pwm == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; - return PERIPHERAL_ERROR_NONE; + return peripheral_gdbus_pwm_set_enable(pwm, enable); } -int peripheral_pwm_is_enabled(peripheral_pwm_h pwm) +int peripheral_pwm_get_enable(peripheral_pwm_h pwm, peripheral_pwm_state_e *enable) { - if (pwm->enabled == PWM_ENABLE) - return PWM_ENABLE; - else - return PWM_DISABLE; -} + if (pwm == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + return peripheral_gdbus_pwm_get_enable(pwm, enable); +} diff --git a/test/peripheral-io-test.c b/test/peripheral-io-test.c index 699be4a..dae9170 100644 --- a/test/peripheral-io-test.c +++ b/test/peripheral-io-test.c @@ -227,7 +227,7 @@ int pwm_test_led(void) 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 */ + peripheral_pwm_set_enable(dev, 1); /* 0: disable, 1: enable */ while (cnt < 5) { for (set_duty_cycle = period; set_duty_cycle > 0; set_duty_cycle -= 50) { @@ -248,7 +248,7 @@ int pwm_test_led(void) } cnt++; } - peripheral_pwm_set_enabled(dev, 0); /* 0: disable, 1: enable */ + peripheral_pwm_set_enable(dev, 0); /* 0: disable, 1: enable */ peripheral_pwm_close(dev); return 0; @@ -285,12 +285,12 @@ int pwm_test_motor(void) printf("set degree: %d\n", degree[idx]); peripheral_pwm_set_period(dev, period); peripheral_pwm_set_duty_cycle(dev, duty_cycle); - peripheral_pwm_set_enabled(dev, 1); /* 0: disable, 1: enable */ + peripheral_pwm_set_enable(dev, 1); /* 0: disable, 1: enable */ usleep(500000); } } - peripheral_pwm_set_enabled(dev, 0); /* 0: disable, 1: enable */ + peripheral_pwm_set_enable(dev, 0); /* 0: disable, 1: enable */ peripheral_pwm_close(dev); return 0; -- 2.7.4 From 73f9d24446cd04692720dd8325c6ec13155aef58 Mon Sep 17 00:00:00 2001 From: "jino.cho" Date: Wed, 17 May 2017 12:30:55 +0900 Subject: [PATCH 13/16] Change data type for parameter to a boolean The parameter('enable') have only two state just like enabled/disabled. Therefore, change it to a boolean. Change-Id: Icefe77a4b62d3e63b882c9106a48134f9036fc16 Signed-off-by: jino.cho --- include/peripheral_gdbus_pwm.h | 4 ++-- include/peripheral_io.h | 9 ++------- src/peripheral_gdbus_pwm.c | 2 +- src/peripheral_io.xml | 4 ++-- src/peripheral_pwm.c | 4 ++-- 5 files changed, 9 insertions(+), 14 deletions(-) diff --git a/include/peripheral_gdbus_pwm.h b/include/peripheral_gdbus_pwm.h index 7096af2..59f9d38 100644 --- a/include/peripheral_gdbus_pwm.h +++ b/include/peripheral_gdbus_pwm.h @@ -26,7 +26,7 @@ int peripheral_gdbus_pwm_set_period(peripheral_pwm_h pwm, int period); int peripheral_gdbus_pwm_get_period(peripheral_pwm_h pwm, int *period); int peripheral_gdbus_pwm_set_duty_cycle(peripheral_pwm_h pwm, int duty_cycle); int peripheral_gdbus_pwm_get_duty_cycle(peripheral_pwm_h pwm, int *duty_cycle); -int peripheral_gdbus_pwm_set_enable(peripheral_pwm_h pwm, peripheral_pwm_state_e enable); -int peripheral_gdbus_pwm_get_enable(peripheral_pwm_h pwm, peripheral_pwm_state_e *enable); +int peripheral_gdbus_pwm_set_enable(peripheral_pwm_h pwm, bool enable); +int peripheral_gdbus_pwm_get_enable(peripheral_pwm_h pwm, bool *enable); #endif /* __PERIPHERAL_GDBUS_PWM_H__ */ diff --git a/include/peripheral_io.h b/include/peripheral_io.h index c6838e7..f5bb401 100644 --- a/include/peripheral_io.h +++ b/include/peripheral_io.h @@ -375,11 +375,6 @@ int peripheral_i2c_write(peripheral_i2c_h i2c, uint8_t *data, int length); */ typedef struct _peripheral_pwm_s *peripheral_pwm_h; -typedef enum { - PERIPHERAL_PWM_DISABLE = 0, - PERIPHERAL_PWM_ENABLE, -} peripheral_pwm_state_e; - int peripheral_pwm_open(int device, int channel, peripheral_pwm_h *pwm); int peripheral_pwm_close(peripheral_pwm_h pwm); @@ -393,9 +388,9 @@ int peripheral_pwm_set_duty_cycle(peripheral_pwm_h pwm, int duty_cycle); int peripheral_pwm_get_duty_cycle(peripheral_pwm_h pwm, int *duty_cycle); -int peripheral_pwm_set_enable(peripheral_pwm_h pwm, peripheral_pwm_state_e enable); +int peripheral_pwm_set_enable(peripheral_pwm_h pwm, bool enable); -int peripheral_pwm_get_enable(peripheral_pwm_h pwm, peripheral_pwm_state_e *enable); +int peripheral_pwm_get_enable(peripheral_pwm_h pwm, bool *enable); /** * @} diff --git a/src/peripheral_gdbus_pwm.c b/src/peripheral_gdbus_pwm.c index 76f4a5b..cd4f26e 100644 --- a/src/peripheral_gdbus_pwm.c +++ b/src/peripheral_gdbus_pwm.c @@ -187,7 +187,7 @@ int peripheral_gdbus_pwm_get_duty_cycle(peripheral_pwm_h pwm, int *duty_cycle) return ret; } -int peripheral_gdbus_pwm_set_enable(peripheral_pwm_h pwm, peripheral_pwm_state_e enable) +int peripheral_gdbus_pwm_set_enable(peripheral_pwm_h pwm, bool enable) { GError *error = NULL; gint32 ret = PERIPHERAL_ERROR_NONE; diff --git a/src/peripheral_io.xml b/src/peripheral_io.xml index 285c6f5..57ad78d 100644 --- a/src/peripheral_io.xml +++ b/src/peripheral_io.xml @@ -115,12 +115,12 @@ - + - + diff --git a/src/peripheral_pwm.c b/src/peripheral_pwm.c index 45f29fc..07c4001 100644 --- a/src/peripheral_pwm.c +++ b/src/peripheral_pwm.c @@ -100,14 +100,14 @@ int peripheral_pwm_get_duty_cycle(peripheral_pwm_h pwm, int *duty_cycle) return peripheral_gdbus_pwm_get_duty_cycle(pwm, duty_cycle); } -int peripheral_pwm_set_enable(peripheral_pwm_h pwm, peripheral_pwm_state_e enable) +int peripheral_pwm_set_enable(peripheral_pwm_h pwm, bool enable) { if (pwm == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; return peripheral_gdbus_pwm_set_enable(pwm, enable); } -int peripheral_pwm_get_enable(peripheral_pwm_h pwm, peripheral_pwm_state_e *enable) +int peripheral_pwm_get_enable(peripheral_pwm_h pwm, bool *enable) { if (pwm == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; -- 2.7.4 From e2a5bafd94274c32d0c6fdd5e043161ce124ba19 Mon Sep 17 00:00:00 2001 From: "jino.cho" Date: Fri, 12 May 2017 15:47:40 +0900 Subject: [PATCH 14/16] Test function for uart API The function reads data stream from uart port. Change-Id: Ic85657939aaa8da4ed3914c9fa4e50388b81627b Signed-off-by: jino.cho --- test/peripheral-io-test.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/test/peripheral-io-test.c b/test/peripheral-io-test.c index dae9170..a787e10 100644 --- a/test/peripheral-io-test.c +++ b/test/peripheral-io-test.c @@ -296,6 +296,65 @@ int pwm_test_motor(void) return 0; } + +int uart_test_accelerometer(void) +{ + peripheral_uart_h uart; + int ret; + int port; + int loop = 100; + unsigned char buf[1024]; + + printf("<<< uart_test_accelerometer >>>\n"); + printf("artik710 : 4 \n"); + printf(">> PORT NUMBER : "); + + if (scanf("%d", &port) < 0) + return 0; + + ret = peripheral_uart_open(port, &uart); + if (ret < 0) + goto err_open; + ret = peripheral_uart_set_baudrate(uart, PERIPHERAL_UART_BAUDRATE_4800); + if (ret < 0) + goto out; + ret = peripheral_uart_set_mode(uart, + PERIPHERAL_UART_BYTESIZE_8BIT, + PERIPHERAL_UART_PARITY_NONE, + PERIPHERAL_UART_STOPBITS_1BIT); + if (ret < 0) + goto out; + ret = peripheral_uart_set_flowcontrol(uart, true, false); + if (ret < 0) + goto out; + + sleep(1); + ret = peripheral_uart_flush(uart); + if (ret < 0) + goto out; + + while (loop--) { + ret = peripheral_uart_read(uart, buf, 13); + if (ret < 0) { + if (ret == PERIPHERAL_ERROR_NO_DATA) + printf("No data to read (%d)\n", ret); + else + printf("Failed to read (%d)\n", ret); + continue; + } + buf[ret] = 0; + printf("%s", buf); + usleep(100000); + } + +out: + peripheral_uart_close(uart); + +err_open: + printf(">> ret = %d\n", ret); + return 0; +} + int main(int argc, char **argv) { int num = 1; @@ -308,6 +367,7 @@ int main(int argc, char **argv) printf(" 2. I2C Test\n"); printf(" 3. pwm led test\n"); printf(" 4. pwm motor test\n"); + printf(" 5. uart accelerometer test\n"); printf(" 11. GPIO Interrupt Test\n"); printf(" 12. H/W IF I2C Test\n"); @@ -330,6 +390,9 @@ int main(int argc, char **argv) case 4: ret = pwm_test_motor(); break; + case 5: + ret = uart_test_accelerometer(); + break; case 11: ret = gpio_irq_test(); break; -- 2.7.4 From 7de767ffe6e464f2c2914a5c881c42f9877f5c15 Mon Sep 17 00:00:00 2001 From: "jino.cho" Date: Thu, 18 May 2017 14:04:57 +0900 Subject: [PATCH 15/16] Remove unused defines Change-Id: Ic330c835875625d782db9cee8e9b6b957443dff9 Signed-off-by: jino.cho --- src/peripheral_pwm.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/peripheral_pwm.c b/src/peripheral_pwm.c index 07c4001..2b9c1a9 100644 --- a/src/peripheral_pwm.c +++ b/src/peripheral_pwm.c @@ -24,9 +24,6 @@ #include "peripheral_common.h" #include "peripheral_internal.h" -#define PWM_ENABLE 1 -#define PWM_DISABLE 0 - int peripheral_pwm_open(int device, int channel, peripheral_pwm_h* pwm) { peripheral_pwm_h handle; -- 2.7.4 From 628bec3eec313f4980b038e2541d3fc394559784 Mon Sep 17 00:00:00 2001 From: "jino.cho" Date: Thu, 18 May 2017 14:59:20 +0900 Subject: [PATCH 16/16] Check validation of pwm handle in the peripheral_pwm_close() Change-Id: I0b8ae08011188087819365a2dc5cd40bb6b14f39 Signed-off-by: jino.cho --- src/peripheral_pwm.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/peripheral_pwm.c b/src/peripheral_pwm.c index 2b9c1a9..ecb9b4a 100644 --- a/src/peripheral_pwm.c +++ b/src/peripheral_pwm.c @@ -58,6 +58,8 @@ int peripheral_pwm_close(peripheral_pwm_h pwm) { int ret = PERIPHERAL_ERROR_NONE; + if (pwm == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + ret = peripheral_gdbus_pwm_close(pwm); pwm_proxy_deinit(); -- 2.7.4