From c4fb9a724b2aef028ce2ade0f36229d967f933c0 Mon Sep 17 00:00:00 2001 From: "jino.cho" Date: Tue, 16 May 2017 19:55:27 +0900 Subject: [PATCH 01/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.34.1 From 872d1500364bd2b3adb07e22a0098b54aaf750a4 Mon Sep 17 00:00:00 2001 From: "jino.cho" Date: Tue, 16 May 2017 20:49:28 +0900 Subject: [PATCH 02/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 @@ -33,6 +33,13 @@ struct _peripheral_i2c_s { uint handle; }; +/** + * @brief Internal struct for pwm context + */ +struct _peripheral_pwm_s { + uint handle; +}; + /** * @brief Internal struct for uart context */ 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.34.1 From 73f9d24446cd04692720dd8325c6ec13155aef58 Mon Sep 17 00:00:00 2001 From: "jino.cho" Date: Wed, 17 May 2017 12:30:55 +0900 Subject: [PATCH 03/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.34.1 From e2a5bafd94274c32d0c6fdd5e043161ce124ba19 Mon Sep 17 00:00:00 2001 From: "jino.cho" Date: Fri, 12 May 2017 15:47:40 +0900 Subject: [PATCH 04/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.34.1 From 7de767ffe6e464f2c2914a5c881c42f9877f5c15 Mon Sep 17 00:00:00 2001 From: "jino.cho" Date: Thu, 18 May 2017 14:04:57 +0900 Subject: [PATCH 05/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.34.1 From 628bec3eec313f4980b038e2541d3fc394559784 Mon Sep 17 00:00:00 2001 From: "jino.cho" Date: Thu, 18 May 2017 14:59:20 +0900 Subject: [PATCH 06/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.34.1 From da06ae69ee8437ec55f1fc9da472980cf178204d Mon Sep 17 00:00:00 2001 From: "jino.cho" Date: Thu, 18 May 2017 15:28:19 +0900 Subject: [PATCH 07/16] Fix gdbus error log Change-Id: Ib2831b01f1080dba1204940c43f23bd02278d98f Signed-off-by: jino.cho --- src/peripheral_gdbus_pwm.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/peripheral_gdbus_pwm.c b/src/peripheral_gdbus_pwm.c index cd4f26e..8b3268c 100644 --- a/src/peripheral_gdbus_pwm.c +++ b/src/peripheral_gdbus_pwm.c @@ -65,7 +65,7 @@ int peripheral_gdbus_pwm_open(peripheral_pwm_h pwm, int device, int channel) &ret, NULL, &error) == FALSE) { - _E("Error in %s() : %s\n", __func__, error->message); + _E("%s", error->message); g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } @@ -87,7 +87,7 @@ int peripheral_gdbus_pwm_close(peripheral_pwm_h pwm) &ret, NULL, &error) == FALSE) { - _E("Error in %s() : %s\n", __func__, error->message); + _E("%s", error->message); g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } @@ -110,7 +110,7 @@ int peripheral_gdbus_pwm_set_period(peripheral_pwm_h pwm, int period) &ret, NULL, &error) == FALSE) { - _E("Error in %s() : %s\n", __func__, error->message); + _E("%s", error->message); g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } @@ -133,7 +133,7 @@ int peripheral_gdbus_pwm_get_period(peripheral_pwm_h pwm, int *period) &ret, NULL, &error) == FALSE) { - _E("Error in %s() : %s\n", __func__, error->message); + _E("%s", error->message); g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } @@ -156,7 +156,7 @@ int peripheral_gdbus_pwm_set_duty_cycle(peripheral_pwm_h pwm, int duty_cycle) &ret, NULL, &error) == FALSE) { - _E("Error in %s() : %s\n", __func__, error->message); + _E("%s", error->message); g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } @@ -179,7 +179,7 @@ int peripheral_gdbus_pwm_get_duty_cycle(peripheral_pwm_h pwm, int *duty_cycle) &ret, NULL, &error) == FALSE) { - _E("Error in %s() : %s\n", __func__, error->message); + _E("%s", error->message); g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } @@ -202,7 +202,7 @@ int peripheral_gdbus_pwm_set_enable(peripheral_pwm_h pwm, bool enable) &ret, NULL, &error) == FALSE) { - _E("Error in %s() : %s\n", __func__, error->message); + _E("%s", error->message); g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } -- 2.34.1 From e963f85fcbaee117416c6f9d1671a2e354e388b8 Mon Sep 17 00:00:00 2001 From: "jino.cho" Date: Thu, 18 May 2017 15:38:42 +0900 Subject: [PATCH 08/16] Modify peripheral_gdbus_pwm.c - Change return type to peripheral_error_e - Remove TODO comment Change-Id: Iec750952c3d2ee827ef4a830b3bac81c9373068b Signed-off-by: jino.cho --- src/peripheral_gdbus_pwm.c | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/src/peripheral_gdbus_pwm.c b/src/peripheral_gdbus_pwm.c index 8b3268c..4d1e396 100644 --- a/src/peripheral_gdbus_pwm.c +++ b/src/peripheral_gdbus_pwm.c @@ -52,11 +52,10 @@ void pwm_proxy_deinit() int peripheral_gdbus_pwm_open(peripheral_pwm_h pwm, int device, int channel) { GError *error = NULL; - gint32 ret = PERIPHERAL_ERROR_NONE; + peripheral_error_e 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, @@ -76,11 +75,10 @@ int peripheral_gdbus_pwm_open(peripheral_pwm_h pwm, int device, int channel) int peripheral_gdbus_pwm_close(peripheral_pwm_h pwm) { GError *error = NULL; - gint32 ret = PERIPHERAL_ERROR_NONE; + peripheral_error_e 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, pwm->handle, @@ -98,11 +96,10 @@ int peripheral_gdbus_pwm_close(peripheral_pwm_h pwm) int peripheral_gdbus_pwm_set_period(peripheral_pwm_h pwm, int period) { GError *error = NULL; - gint32 ret = PERIPHERAL_ERROR_NONE; + peripheral_error_e 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, pwm->handle, @@ -121,11 +118,10 @@ int peripheral_gdbus_pwm_set_period(peripheral_pwm_h pwm, int period) int peripheral_gdbus_pwm_get_period(peripheral_pwm_h pwm, int *period) { GError *error = NULL; - gint32 ret = PERIPHERAL_ERROR_NONE; + peripheral_error_e 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, pwm->handle, @@ -144,11 +140,10 @@ 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) { GError *error = NULL; - gint32 ret = PERIPHERAL_ERROR_NONE; + peripheral_error_e 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, pwm->handle, @@ -167,11 +162,10 @@ 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) { GError *error = NULL; - gint32 ret = PERIPHERAL_ERROR_NONE; + peripheral_error_e 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, pwm->handle, @@ -190,11 +184,10 @@ int peripheral_gdbus_pwm_get_duty_cycle(peripheral_pwm_h pwm, int *duty_cycle) int peripheral_gdbus_pwm_set_enable(peripheral_pwm_h pwm, bool enable) { GError *error = NULL; - gint32 ret = PERIPHERAL_ERROR_NONE; + peripheral_error_e 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, pwm->handle, -- 2.34.1 From dedd27d82ae980ad39cbce270ac40d7ada4979a3 Mon Sep 17 00:00:00 2001 From: "jino.cho" Date: Thu, 18 May 2017 15:47:53 +0900 Subject: [PATCH 09/16] Add pwm set_polarity & get_polarity functions Change-Id: Ia68fab71850f2ef0b68e6c3f22310dbb13484e8f Signed-off-by: jino.cho --- include/peripheral_gdbus_pwm.h | 2 ++ include/peripheral_io.h | 12 ++++++++++ src/peripheral_gdbus_pwm.c | 44 ++++++++++++++++++++++++++++++++++ src/peripheral_io.xml | 10 ++++++++ src/peripheral_pwm.c | 14 +++++++++++ 5 files changed, 82 insertions(+) diff --git a/include/peripheral_gdbus_pwm.h b/include/peripheral_gdbus_pwm.h index 59f9d38..3fc3cab 100644 --- a/include/peripheral_gdbus_pwm.h +++ b/include/peripheral_gdbus_pwm.h @@ -26,6 +26,8 @@ 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_polarity(peripheral_pwm_h pwm, peripheral_pwm_polarity_e polarity); +int peripheral_gdbus_pwm_get_polarity(peripheral_pwm_h pwm, peripheral_pwm_polarity_e *polarity); int peripheral_gdbus_pwm_set_enable(peripheral_pwm_h pwm, bool enable); int peripheral_gdbus_pwm_get_enable(peripheral_pwm_h pwm, bool *enable); diff --git a/include/peripheral_io.h b/include/peripheral_io.h index f5bb401..1000993 100644 --- a/include/peripheral_io.h +++ b/include/peripheral_io.h @@ -375,6 +375,14 @@ int peripheral_i2c_write(peripheral_i2c_h i2c, uint8_t *data, int length); */ typedef struct _peripheral_pwm_s *peripheral_pwm_h; +/** + * @brief Enumeration for Polarity. + */ +typedef enum { + PERIPHERAL_PWM_POLARITY_NORMAL = 0, + PERIPHERAL_PWM_POLARITY_INVERSED, +} peripheral_pwm_polarity_e; + int peripheral_pwm_open(int device, int channel, peripheral_pwm_h *pwm); int peripheral_pwm_close(peripheral_pwm_h pwm); @@ -388,6 +396,10 @@ 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_polarity(peripheral_pwm_h pwm, peripheral_pwm_polarity_e polarity); + +int peripheral_pwm_get_polarity(peripheral_pwm_h pwm, peripheral_pwm_polarity_e *polarity); + int peripheral_pwm_set_enable(peripheral_pwm_h pwm, bool 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 4d1e396..1467c8f 100644 --- a/src/peripheral_gdbus_pwm.c +++ b/src/peripheral_gdbus_pwm.c @@ -181,6 +181,50 @@ int peripheral_gdbus_pwm_get_duty_cycle(peripheral_pwm_h pwm, int *duty_cycle) return ret; } +int peripheral_gdbus_pwm_set_polarity(peripheral_pwm_h pwm, peripheral_pwm_polarity_e polarity) +{ + GError *error = NULL; + peripheral_error_e ret = PERIPHERAL_ERROR_NONE; + + if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + if (peripheral_io_gdbus_pwm_call_set_polarity_sync( + pwm_proxy, + pwm->handle, + polarity, + &ret, + NULL, + &error) == FALSE) { + _E("%s", error->message); + g_error_free(error); + return PERIPHERAL_ERROR_UNKNOWN; + } + + return ret; +} + +int peripheral_gdbus_pwm_get_polarity(peripheral_pwm_h pwm, peripheral_pwm_polarity_e *polarity) +{ + 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_polarity_sync( + pwm_proxy, + pwm->handle, + (gint*)polarity, + &ret, + NULL, + &error) == FALSE) { + _E("%s", error->message); + g_error_free(error); + return PERIPHERAL_ERROR_UNKNOWN; + } + + return ret; +} + int peripheral_gdbus_pwm_set_enable(peripheral_pwm_h pwm, bool enable) { GError *error = NULL; diff --git a/src/peripheral_io.xml b/src/peripheral_io.xml index 57ad78d..abc3679 100644 --- a/src/peripheral_io.xml +++ b/src/peripheral_io.xml @@ -113,6 +113,16 @@ + + + + + + + + + + diff --git a/src/peripheral_pwm.c b/src/peripheral_pwm.c index ecb9b4a..354ad80 100644 --- a/src/peripheral_pwm.c +++ b/src/peripheral_pwm.c @@ -99,6 +99,20 @@ 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_polarity(peripheral_pwm_h pwm, peripheral_pwm_polarity_e polarity) +{ + if (pwm == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + + return peripheral_gdbus_pwm_set_polarity(pwm, polarity); +} + +int peripheral_pwm_get_polarity(peripheral_pwm_h pwm, peripheral_pwm_polarity_e *polarity) +{ + if (pwm == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + + return peripheral_gdbus_pwm_get_polarity(pwm, polarity); +} + int peripheral_pwm_set_enable(peripheral_pwm_h pwm, bool enable) { if (pwm == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; -- 2.34.1 From fe3a9f4a5a465ac3097d2afd7170d25d2acae913 Mon Sep 17 00:00:00 2001 From: "jino.cho" Date: Thu, 18 May 2017 18:36:20 +0900 Subject: [PATCH 10/16] Replace checking validation of the pwm device and channel Change-Id: I13a2b3b14dec162e6abc140def5bb3c4000f9622 Signed-off-by: jino.cho --- src/peripheral_pwm.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/peripheral_pwm.c b/src/peripheral_pwm.c index 354ad80..607c8f8 100644 --- a/src/peripheral_pwm.c +++ b/src/peripheral_pwm.c @@ -24,13 +24,12 @@ #include "peripheral_common.h" #include "peripheral_internal.h" -int peripheral_pwm_open(int device, int channel, peripheral_pwm_h* pwm) +int peripheral_pwm_open(int device, int channel, peripheral_pwm_h *pwm) { peripheral_pwm_h handle; int ret = PERIPHERAL_ERROR_NONE; - assert(device >= 0); - assert(channel >= 0); + if (device < 0 || channel < 0) return PERIPHERAL_ERROR_INVALID_PARAMETER; /* Initialize */ handle = (peripheral_pwm_h)calloc(1, sizeof(struct _peripheral_pwm_s)); -- 2.34.1 From beba91c7725e702664b95039532771ee894ee7ca Mon Sep 17 00:00:00 2001 From: Hyeongsik Min Date: Fri, 19 May 2017 19:22:11 +0900 Subject: [PATCH 11/16] Fix possible memory leak in peripheral_pwm_close Free pwm handle regardless of return value of peripheral_gdbus_pwm_close(). Change-Id: Iaaa85b757f96851eac186c11efbdd8fa8e32bb98 Signed-off-by: Hyeongsik Min --- src/peripheral_pwm.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/peripheral_pwm.c b/src/peripheral_pwm.c index 607c8f8..7e98766 100644 --- a/src/peripheral_pwm.c +++ b/src/peripheral_pwm.c @@ -59,13 +59,11 @@ int peripheral_pwm_close(peripheral_pwm_h pwm) if (pwm == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; - ret = peripheral_gdbus_pwm_close(pwm); - pwm_proxy_deinit(); + if ((ret = peripheral_gdbus_pwm_close(pwm)) < 0) + _E("Failed to close PWM device, continuing anyway"); - if (ret == PERIPHERAL_ERROR_NONE) { - free(pwm); - pwm = NULL; - } + pwm_proxy_deinit(); + free(pwm); return ret; } -- 2.34.1 From 7f8f05b74cd0f367233b0d0917fd91482e614680 Mon Sep 17 00:00:00 2001 From: Hyeongsik Min Date: Mon, 22 May 2017 20:10:43 +0900 Subject: [PATCH 12/16] Fix reference counting bug Change-Id: If8be15c2815a11cc63d477e0af7fa79729a62966 Signed-off-by: Hyeongsik Min --- src/peripheral_gdbus_gpio.c | 5 +++-- src/peripheral_gdbus_i2c.c | 5 +++-- src/peripheral_gdbus_pwm.c | 5 +++-- src/peripheral_gdbus_uart.c | 5 +++-- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/peripheral_gdbus_gpio.c b/src/peripheral_gdbus_gpio.c index 2027f3d..1c0e90f 100644 --- a/src/peripheral_gdbus_gpio.c +++ b/src/peripheral_gdbus_gpio.c @@ -32,8 +32,10 @@ void gpio_proxy_init(void) { GError *error = NULL; - if (gpio_proxy != NULL) + if (gpio_proxy != NULL) { + g_object_ref(gpio_proxy); return; + } gpio_proxy = peripheral_io_gdbus_gpio_proxy_new_for_bus_sync( G_BUS_TYPE_SYSTEM, @@ -58,7 +60,6 @@ void gpio_proxy_deinit() { if (gpio_proxy) { g_object_unref(gpio_proxy); - gpio_proxy = NULL; } } diff --git a/src/peripheral_gdbus_i2c.c b/src/peripheral_gdbus_i2c.c index ce42ce5..2e176ef 100644 --- a/src/peripheral_gdbus_i2c.c +++ b/src/peripheral_gdbus_i2c.c @@ -29,8 +29,10 @@ void i2c_proxy_init(void) { GError *error = NULL; - if (i2c_proxy != NULL) + if (i2c_proxy != NULL) { + g_object_ref(i2c_proxy); return; + } i2c_proxy = peripheral_io_gdbus_i2c_proxy_new_for_bus_sync( G_BUS_TYPE_SYSTEM, @@ -45,7 +47,6 @@ void i2c_proxy_deinit() { if (i2c_proxy) { g_object_unref(i2c_proxy); - i2c_proxy = NULL; } } diff --git a/src/peripheral_gdbus_pwm.c b/src/peripheral_gdbus_pwm.c index 1467c8f..31b2a78 100644 --- a/src/peripheral_gdbus_pwm.c +++ b/src/peripheral_gdbus_pwm.c @@ -29,8 +29,10 @@ void pwm_proxy_init(void) { GError *error = NULL; - if (pwm_proxy != NULL) + if (pwm_proxy != NULL) { + g_object_ref(pwm_proxy); return; + } pwm_proxy = peripheral_io_gdbus_pwm_proxy_new_for_bus_sync( G_BUS_TYPE_SYSTEM, @@ -45,7 +47,6 @@ void pwm_proxy_deinit() { if (pwm_proxy) { g_object_unref(pwm_proxy); - pwm_proxy = NULL; } } diff --git a/src/peripheral_gdbus_uart.c b/src/peripheral_gdbus_uart.c index fc94a89..cf51b22 100644 --- a/src/peripheral_gdbus_uart.c +++ b/src/peripheral_gdbus_uart.c @@ -29,8 +29,10 @@ void uart_proxy_init(void) { GError *error = NULL; - if (uart_proxy != NULL) + if (uart_proxy != NULL) { + g_object_ref(uart_proxy); return; + } uart_proxy = peripheral_io_gdbus_uart_proxy_new_for_bus_sync( G_BUS_TYPE_SYSTEM, @@ -45,7 +47,6 @@ void uart_proxy_deinit() { if (uart_proxy) { g_object_unref(uart_proxy); - uart_proxy = NULL; } } -- 2.34.1 From 4f70ec201719542d860af4cd100774a05c6ad7c2 Mon Sep 17 00:00:00 2001 From: Sungguk Na Date: Thu, 25 May 2017 14:14:22 +0900 Subject: [PATCH 13/16] Additional fix to handle reference count of proxy Proxy object should be NULL after proxy deinit, but g_object_unref just make free the object and do not make it NULL Change-Id: I5ca2c14606527f5610181d6470d099fcf681c8cd Signed-off-by: Sungguk Na --- src/peripheral_gdbus_gpio.c | 2 ++ src/peripheral_gdbus_i2c.c | 2 ++ src/peripheral_gdbus_pwm.c | 2 ++ src/peripheral_gdbus_uart.c | 2 ++ 4 files changed, 8 insertions(+) diff --git a/src/peripheral_gdbus_gpio.c b/src/peripheral_gdbus_gpio.c index 1c0e90f..0bbe4f7 100644 --- a/src/peripheral_gdbus_gpio.c +++ b/src/peripheral_gdbus_gpio.c @@ -60,6 +60,8 @@ void gpio_proxy_deinit() { if (gpio_proxy) { g_object_unref(gpio_proxy); + if (!G_IS_OBJECT(gpio_proxy)) + gpio_proxy = NULL; } } diff --git a/src/peripheral_gdbus_i2c.c b/src/peripheral_gdbus_i2c.c index 2e176ef..c30399b 100644 --- a/src/peripheral_gdbus_i2c.c +++ b/src/peripheral_gdbus_i2c.c @@ -47,6 +47,8 @@ void i2c_proxy_deinit() { if (i2c_proxy) { g_object_unref(i2c_proxy); + if (!G_IS_OBJECT(i2c_proxy)) + i2c_proxy = NULL; } } diff --git a/src/peripheral_gdbus_pwm.c b/src/peripheral_gdbus_pwm.c index 31b2a78..a3b8b51 100644 --- a/src/peripheral_gdbus_pwm.c +++ b/src/peripheral_gdbus_pwm.c @@ -47,6 +47,8 @@ void pwm_proxy_deinit() { if (pwm_proxy) { g_object_unref(pwm_proxy); + if (!G_IS_OBJECT(pwm_proxy)) + pwm_proxy = NULL; } } diff --git a/src/peripheral_gdbus_uart.c b/src/peripheral_gdbus_uart.c index cf51b22..0a80e72 100644 --- a/src/peripheral_gdbus_uart.c +++ b/src/peripheral_gdbus_uart.c @@ -47,6 +47,8 @@ void uart_proxy_deinit() { if (uart_proxy) { g_object_unref(uart_proxy); + if (!G_IS_OBJECT(uart_proxy)) + uart_proxy = NULL; } } -- 2.34.1 From 46b2e09e69cce2257a90efdf5b67a5c3ce3c6f41 Mon Sep 17 00:00:00 2001 From: Hyeongsik Min Date: Wed, 24 May 2017 16:31:03 +0900 Subject: [PATCH 14/16] Add GPIO unit test This patch also implements menu for each tc module and utilizes glib IO channel to receive user input. Change-Id: Iedce7feab1887c822b172c30aba58b0f0eab959c Signed-off-by: Hyeongsik Min --- test/peripheral-io-test.c | 696 ++++++++++++++++++++++++++++++-------- 1 file changed, 548 insertions(+), 148 deletions(-) diff --git a/test/peripheral-io-test.c b/test/peripheral-io-test.c index a787e10..163ffa5 100644 --- a/test/peripheral-io-test.c +++ b/test/peripheral-io-test.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd. + * 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. @@ -14,28 +14,59 @@ * limitations under the License. */ -#include "peripheral_io.h" - #include #include #include +#include #include -extern int gpio_test(); -extern int i2c_test(); -extern int adc_test(); +#include "peripheral_io.h" + +#define BUFFER_LEN 32 -GMainLoop *loop; +typedef struct { + const char *tc_name; + int tc_code; + int (*tc_func)(void); +} tc_table_t; + +tc_table_t *tc_table; + +GMainLoop *main_loop; +GList *gpio_list; +GList *i2c_list; +GList *pwm_list; +GList *adc_list; +GList *uart_list; +GList *spi_list; + +int read_int_input(int *input) +{ + char buf[BUFFER_LEN]; + int rv; + + rv = read(0, buf, BUFFER_LEN); + + /* Ignore Enter without value */ + if (*buf == '\n' || *buf == '\r') { + printf("No input value\n"); + return -1; + } + + if (rv < 0) return -1; + *input = atoi(buf); + + return 0; +} -int gpio_test(void) +int gpio_led_test(void) { int num; int cnt = 0; peripheral_gpio_h handle = NULL; - printf("artik5 : 135 \n"); - printf("artik10 : 22 \n"); - printf(">> PIN NUMBER : "); + printf(" %s()\n", __func__); + printf("Enter GPIO pin number "); if (scanf("%d", &num) < 0) return 0; @@ -52,13 +83,13 @@ int gpio_test(void) } while (cnt++ < 5) { - printf("write~\n"); + printf("Writing..\n"); peripheral_gpio_write(handle, 1); sleep(1); peripheral_gpio_write(handle, 0); sleep(1); } - printf("write finish\n"); + printf("Write finish\n"); peripheral_gpio_close(handle); return 1; @@ -74,70 +105,66 @@ void gpio_irq_test_isr(void *user_data) peripheral_gpio_get_pin(gpio, &pin); - printf("gpio_irq_test_isr: GPIO %d interrupt occurs.\n", pin); + printf("%s: GPIO %d interrupt occurs.\n", __func__, pin); } -void *gpio_irq_test_thread(void *data) +int gpio_irq_register(void) { - 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; + int pin, ret; - printf("artik710 : 27 \n"); - printf(">> PIN NUMBER : "); + printf(" %s()\n", __func__); + printf("Enter gpio pin number\n"); - if (scanf("%d", &num) < 0) - return 0; + if (read_int_input(&pin) < 0) + return -1; - if (peripheral_gpio_open(num, &gpio) != PERIPHERAL_ERROR_NONE) { - printf("test dev is null\n"); - return 0; + if ((ret = peripheral_gpio_open(pin, &gpio)) < 0) { + printf(">>>>> Failed to open GPIO pin, ret : %d\n", ret); + return -1; } + gpio_list = g_list_append(gpio_list, gpio); - if (peripheral_gpio_set_direction(gpio, PERIPHERAL_GPIO_DIRECTION_IN) != 0) { - printf("test set direction error!!!"); + if ((ret = peripheral_gpio_set_direction(gpio, PERIPHERAL_GPIO_DIRECTION_IN)) < 0) { + printf(">>>>> Failed to set direction, ret : %d\n", ret); goto error; } + peripheral_gpio_set_edge_mode(gpio, PERIPHERAL_GPIO_EDGE_BOTH); + peripheral_gpio_register_cb(gpio, gpio_irq_test_isr, gpio); - printf(">> Select Edge Mode (0 = None, 1 = Falling, 2 = Rising, 3 = Both) : "); - if (scanf("%d", &num) < 0) - return 0; + return 0; - if (num >= 0 && num <= 3) - edge = num; +error: + gpio_list = g_list_remove(gpio_list, gpio); + peripheral_gpio_close(gpio); + return -1; +} - peripheral_gpio_set_edge_mode( gpio, edge); - peripheral_gpio_register_cb(gpio, gpio_irq_test_isr, gpio); +int gpio_irq_unregister(void) +{ + peripheral_gpio_h gpio; + int pin, ret; + int gpio_test_get_handle_by_pin(int pin, peripheral_gpio_h *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); + printf(" %s()\n", __func__); + printf("Enter gpio pin number\n"); - g_thread_join(test_thread); - if (loop != NULL) - g_main_loop_unref(loop); + if (read_int_input(&pin) < 0) + return -1; - return 0; + if (gpio_test_get_handle_by_pin(pin, &gpio) < 0) { + printf(">>>>> cannot find handle. please open the gpio pin.\n"); + return -1; + } -error: + if ((ret = peripheral_gpio_unregister_cb(gpio)) < 0) { + printf(">>>>> failed to unregister callback function, ret : %d\n", ret); + return -1; + } + + gpio_list = g_list_remove(gpio_list, gpio); peripheral_gpio_close(gpio); + return 0; } @@ -149,66 +176,45 @@ error: #define GY30_READ_INTENSITY(buf) ((buf[0] << 8 | buf[1]) / 1.2) -int i2c_test(void) +int i2c_gy30_test(void) { int cnt = 0; - int bus_num; + int bus_num, ret; unsigned char buf[10]; peripheral_i2c_h i2c; - printf(">> I2C bus number : "); + printf(" %s()\n", __func__); + printf("Enter I2C bus number : "); if (scanf("%d", &bus_num) < 0) - return 0; + return -1; - if ((peripheral_i2c_open(bus_num, GY30_ADDR, &i2c)) != 0) { - printf("Failed to open I2C communication\n"); - return 0; + if ((ret = peripheral_i2c_open(bus_num, GY30_ADDR, &i2c)) < 0) { + printf("Failed to open I2C communication, ret : %d\n", ret); + return -1; } buf[0] = GY30_CONT_HIGH_RES_MODE; - if (peripheral_i2c_write(i2c, buf, 1) != 0) { - printf("Failed to write\n"); + if ((ret = peripheral_i2c_write(i2c, buf, 1)) < 0) { + printf("Failed to write, ret : %d\n", ret); goto error; } while (cnt++ < 15) { int result; sleep(1); - peripheral_i2c_read(i2c, buf, 2); + ret = peripheral_i2c_read(i2c, buf, 2); + if (ret < 0) + printf("Failed to read, ret : %d\n", ret); result = GY30_READ_INTENSITY(buf); - printf("Result [%d]\n", result); + printf("Light intensity : %d\n", result); } peripheral_i2c_close(i2c); - return 1; + return 0; error: peripheral_i2c_close(i2c); - return 0; -} - -int adc_test(void) -{ -#if 0 - int channel = 0; - int data = 0; - adc_context_h dev = NULL; - - printf(">>channel :"); - scanf("%d", &channel); - - dev = peripheral_adc_open(channel); - - if (!dev) { - printf("open error!\n"); - return 1; - } - - peripheral_adc_read(dev, &data); - - peripheral_adc_close(dev); -#endif - return 1; + return -1; } int pwm_test_led(void) @@ -222,7 +228,7 @@ int pwm_test_led(void) int get_period, get_duty_cycle; peripheral_pwm_h dev; - printf("<<< pwm_test >>>\n"); + printf(" %s()\n", __func__); peripheral_pwm_open(device, channel, &dev); peripheral_pwm_set_period(dev, period); /* period: nanosecond */ @@ -263,7 +269,7 @@ int pwm_test_motor(void) int degree[3] = {0, 45, 90}; peripheral_pwm_h dev; - printf("<<< pwm_test_motor >>>\n"); + printf(" %s()\n", __func__); peripheral_pwm_open(device, channel, &dev); for (cnt = 0; cnt < 5; cnt++) { @@ -305,25 +311,27 @@ int uart_test_accelerometer(void) int loop = 100; unsigned char buf[1024]; - printf("<<< uart_test_accelerometer >>>\n"); - printf("artik710 : 4 \n"); - printf(">> PORT NUMBER : "); + printf(" %s()\n", __func__); + printf("Enter port number"); if (scanf("%d", &port) < 0) - return 0; + return -1; 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; @@ -347,65 +355,457 @@ int uart_test_accelerometer(void) usleep(100000); } + peripheral_uart_close(uart); + return 0; + out: peripheral_uart_close(uart); err_open: - printf(">> ret = %d\n", ret); + return -1; +} + +int gpio_test_get_handle_by_pin(int pin, peripheral_gpio_h *gpio) +{ + peripheral_gpio_h handle; + GList *cursor; + int cur_pin; + + cursor = gpio_list; + while (cursor) { + handle = (peripheral_gpio_h)cursor->data; + peripheral_gpio_get_pin(handle, &cur_pin); + if (pin == cur_pin) + break; + cursor = g_list_next(cursor); + } + if (!cursor) return -1; + + *gpio = handle; + + return 0; +} + +int print_gpio_handle(void) +{ + peripheral_gpio_h gpio; + GList *cursor; + peripheral_gpio_direction_e direction; + peripheral_gpio_edge_e edge; + int pin, value; + char *dir_str, *edge_str; + + printf("--- GPIO handle info. -------------------------------------------\n"); + printf(" No Pin Direction Value Edge mode\n"); + + cursor = gpio_list; + while (cursor) { + gpio = (peripheral_gpio_h)cursor->data; + + if (peripheral_gpio_get_pin(gpio, &pin) < 0) + continue; + if (peripheral_gpio_get_direction(gpio, &direction) < 0) + continue; + if (peripheral_gpio_get_edge_mode(gpio, &edge) < 0) + continue; + + if (direction == PERIPHERAL_GPIO_DIRECTION_IN) + dir_str = "IN"; + else if (direction == PERIPHERAL_GPIO_DIRECTION_OUT_LOW) + dir_str = "OUT_LOW"; + else if (direction == PERIPHERAL_GPIO_DIRECTION_OUT_HIGH) + dir_str = "OUT_HIGH"; + else + dir_str = "UNKNOWN"; + + if (edge == PERIPHERAL_GPIO_EDGE_NONE) + edge_str = "NONE"; + else if (edge == PERIPHERAL_GPIO_EDGE_RISING) + edge_str = "RISING"; + else if (edge == PERIPHERAL_GPIO_EDGE_FALLING) + edge_str = "FALLING"; + else if (edge == PERIPHERAL_GPIO_EDGE_BOTH) + edge_str = "BOTH"; + else + edge_str = "UNKNOWN"; + + if (direction == PERIPHERAL_GPIO_DIRECTION_IN) { + if (peripheral_gpio_read(gpio, &value) < 0) + continue; + printf("%8d%8d%12s%8d%12s\n", g_list_position(gpio_list, cursor), + pin, dir_str, value, edge_str); + } else + printf("%8d%8d%12s%20s\n", g_list_position(gpio_list, cursor), + pin, dir_str, edge_str); + + cursor = g_list_next(cursor); + } + printf("-----------------------------------------------------------------\n"); + + return 0; +} + +int gpio_test_open(void) +{ + peripheral_gpio_h gpio; + int pin, ret; + + printf("%s\n", __func__); + printf("Enter GPIO pin number\n"); + + if (read_int_input(&pin) < 0) + return -1; + + if ((ret = peripheral_gpio_open(pin, &gpio)) < 0) { + printf(">>>>> GPIO open failed, ret : %d\n", ret); + return -1; + } + gpio_list = g_list_append(gpio_list, gpio); + print_gpio_handle(); + + return 0; +} + +int gpio_test_close(void) +{ + peripheral_gpio_h gpio; + int pin, ret; + + printf("%s\n", __func__); + printf("Enter GPIO pin number\n"); + + if (read_int_input(&pin) < 0) + return -1; + + if (gpio_test_get_handle_by_pin(pin, &gpio) < 0) { + printf(">>>>> Cannot find handle. Please open the GPIO pin\n"); + return -1; + } + gpio_list = g_list_remove(gpio_list, gpio); + + if ((ret = peripheral_gpio_close(gpio)) < 0) { + printf(">>>>> GPIO close failed, ret : %d\n", ret); + return -1; + } + print_gpio_handle(); + + return 0; +} + +int gpio_test_set_direction(void) +{ + peripheral_gpio_h gpio; + int pin, ret; + int direction; + + printf("%s\n", __func__); + printf("Enter gpio pin number\n"); + + if (read_int_input(&pin) < 0) + return -1; + + if (gpio_test_get_handle_by_pin(pin, &gpio) < 0) { + printf(">>>>> Cannot find handle. Please open the GPIO pin\n"); + return -1; + } + + printf("Enter direction (0:IN, 1:OUT_LOW, 2:OUT_HIGH)\n"); + if (read_int_input(&direction) < 0) + return -1; + + if (direction > PERIPHERAL_GPIO_DIRECTION_OUT_HIGH || + direction < PERIPHERAL_GPIO_DIRECTION_IN) { + printf(">>>>> Wrong input value\n"); + return -1; + } + + if ((ret = peripheral_gpio_set_direction(gpio, (peripheral_gpio_direction_e)direction)) < 0) { + printf(">>>>> Failed to set direction, ret : %d\n", ret); + return -1; + } + + return 0; +} + +int gpio_test_write(void) +{ + peripheral_gpio_h gpio; + int pin, ret; + int value; + + printf("%s\n", __func__); + printf("Enter gpio pin number\n"); + + if (read_int_input(&pin) < 0) + return -1; + + if (gpio_test_get_handle_by_pin(pin, &gpio) < 0) { + printf(">>>>> Cannot find handle. Please open the GPIO pin\n"); + return -1; + } + + printf("Enter value (0:LOW, 1:HIGH)\n"); + if (read_int_input(&value) < 0) + return -1; + + if (value < 0 || value > 1) { + printf(">>>>> Wrong input value\n"); + return -1; + } + + if ((ret = peripheral_gpio_write(gpio, value)) < 0) { + printf(">>>>> Failed to write value, ret : %d\n", ret); + return -1; + } + + return 0; +} + +int gpio_test_set_edge_mode(void) +{ + peripheral_gpio_h gpio; + int pin, ret; + int edge; + + printf("%s\n", __func__); + printf("Enter gpio pin number\n"); + + if (read_int_input(&pin) < 0) + return -1; + + if (gpio_test_get_handle_by_pin(pin, &gpio) < 0) { + printf(">>>>> Cannot find handle. Please open the GPIO pin\n"); + return -1; + } + + printf("Enter edge mode (0:NONE, 1:RISING, 2:FALLING, 3:BOTH)\n"); + if (read_int_input(&edge) < 0) + return -1; + + if (edge < PERIPHERAL_GPIO_EDGE_NONE || edge > PERIPHERAL_GPIO_EDGE_BOTH) { + printf(">>>>> Wrong input value\n"); + return -1; + } + + if ((ret = peripheral_gpio_set_edge_mode(gpio, (peripheral_gpio_edge_e)edge)) < 0) { + printf(">>>>> Failed to set edge mode, ret : %d\n", ret); + return -1; + } + + return 0; +} + +int gpio_test_set_register_cb(void) +{ + peripheral_gpio_h gpio; + int pin, ret; + + printf("%s\n", __func__); + printf("Enter gpio pin number\n"); + + if (read_int_input(&pin) < 0) + return -1; + + if (gpio_test_get_handle_by_pin(pin, &gpio) < 0) { + printf(">>>>> Cannot find handle. Please open the GPIO pin\n"); + return -1; + } + + if ((ret = peripheral_gpio_register_cb(gpio, gpio_irq_test_isr, gpio)) < 0) { + printf(">>>>> Failed to register callback function, ret : %d\n", ret); + return -1; + } + return 0; +} + +int gpio_test_set_unregister_cb(void) +{ + peripheral_gpio_h gpio; + int pin, ret; + + printf("%s\n", __func__); + printf("Enter gpio pin number\n"); + + if (read_int_input(&pin) < 0) + return -1; + + if (gpio_test_get_handle_by_pin(pin, &gpio) < 0) { + printf(">>>>> Cannot find handle. Please open the GPIO pin\n"); + return -1; + } + + if ((ret = peripheral_gpio_unregister_cb(gpio)) < 0) { + printf(">>>>> failed to unregister callback function, ret : %d\n", ret); + return -1; + } + return 0; } +int enter_main(void); + +tc_table_t gpio_tc_table[] = { + {"Open GPIO pin", 1, gpio_test_open}, + {"Close GPIO pin", 2, gpio_test_close}, + {"Set direction GPIO pin", 3, gpio_test_set_direction}, + {"Write value to GPIO pin", 4, gpio_test_write}, + {"Set edge mode", 5, gpio_test_set_edge_mode}, + {"Register callback", 6, gpio_test_set_register_cb}, + {"Unregister callback", 7, gpio_test_set_unregister_cb}, + {"Print GPIO handle", 9, print_gpio_handle}, + {"Go back to main", 0, enter_main}, + {NULL, 0, NULL}, +}; + +int enter_gpio_test(void) +{ + tc_table = gpio_tc_table; + print_gpio_handle(); + + return 0; +} + +int enter_i2c_test(void) +{ + return 0; +} + +int enter_pwm_test(void) +{ + return 0; +} + +int enter_adc_test(void) +{ + return 0; +} + +int enter_uart_test(void) +{ + return 0; +} + +int enter_spi_test(void) +{ + return 0; +} + +int terminate_test(void) +{ + int ret = 0; + + printf("Terminate test\n"); + g_main_loop_quit(main_loop); + + exit(1); + + return ret; +} + +tc_table_t main_tc_table[] = { + {"GPIO Test Menu", 1, enter_gpio_test}, + {"I2C Test Menu", 2, enter_i2c_test}, + {"PWM Test Menu", 3, enter_pwm_test}, + {"ADC Test Menu", 4, enter_adc_test}, + {"UART Test Menu", 5, enter_uart_test}, + {"SPI Test Menu", 6, enter_spi_test}, + {"[Preset Test] GPIO LED", 11, gpio_led_test}, + {"[Preset Test] I2C GY30 Light sensor", 12, i2c_gy30_test}, + {"[Preset Test] PWM LED", 14, pwm_test_led}, + {"[Preset Test] PWM Motor", 15, pwm_test_motor}, + {"[Preset Test] Uart Accelerometer", 16, uart_test_accelerometer}, + {"[Preset Test] GPIO IRQ register", 17, gpio_irq_register}, + {"[Preset Test] GPIO IRQ unregister", 18, gpio_irq_unregister}, + {"Exit Test", 0, terminate_test}, + {NULL, 0, NULL}, +}; + +int enter_main(void) +{ + tc_table = main_tc_table; + + return 0; +} + +static int test_input_callback(void *data) +{ + long test_id = (long)data; + int ret = PERIPHERAL_ERROR_NONE; + int i = 0; + + while (tc_table[i].tc_name) { + if (tc_table[i].tc_code == test_id) { + ret = tc_table[i].tc_func(); + + if (ret != PERIPHERAL_ERROR_NONE) + printf(">>>>> Test Error Returned !!! : %d\n", ret); + + break; + } + i++; + } + if (!tc_table[i].tc_name) { + printf(">>>>> Wrong input value!\n"); + return -1; + } + + return 0; +} + +static void print_tc_usage(void) +{ + int i = 0; + + printf("===========================================\n"); + while (tc_table[i].tc_name) { + printf(" %2d : %s\n", tc_table[i].tc_code, tc_table[i].tc_name); + i++; + } + printf("===========================================\n"); + printf("Enter TC number\n"); +} + +static gboolean key_event_cb(GIOChannel *chan, GIOCondition cond, gpointer data) +{ + char buf[BUFFER_LEN] = {0,}; + long test_id; + int rv = 0; + + memset(buf, 0, sizeof(buf)); + + rv = read(0, buf, BUFFER_LEN); + + if (*buf == '\n' || *buf == '\r') { + print_tc_usage(); + return TRUE; + } + + if (rv < 0) return FALSE; + + test_id = atoi(buf); + + test_input_callback((void *)test_id); + print_tc_usage(); + + return TRUE; +} + int main(int argc, char **argv) { - int num = 1; - int ret; + GIOChannel *key_io; - printf("===================\n"); - printf(" test Menu\n"); - printf("===================\n"); - printf(" 1. GPIO Test\n"); - printf(" 2. I2C Test\n"); - printf(" 3. pwm led test\n"); - printf(" 4. pwm motor test\n"); - printf(" 5. uart accelerometer test\n"); + main_loop = g_main_loop_new(NULL, FALSE); + key_io = g_io_channel_unix_new(0); - 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"); + tc_table = main_tc_table; - if (scanf("%d", &num) < 0) - return 0; + print_tc_usage(); + g_io_add_watch(key_io, (G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL), key_event_cb, NULL); - switch (num) { - case 1: - ret = gpio_test(); - break; - case 2: - ret = i2c_test(); - break; - case 3: - ret = pwm_test_led(); - break; - case 4: - ret = pwm_test_motor(); - break; - case 5: - ret = uart_test_accelerometer(); - break; - case 11: - ret = gpio_irq_test(); - break; - case 12: - ret = i2c_test(); - break; - case 14: - ret = adc_test(); - break; - default: - printf("Not support \n"); - } - printf(" return : %d\n", ret); + g_main_loop_run(main_loop); - return 1; + g_io_channel_unref(key_io); + g_main_loop_unref(main_loop); + + return 0; } -- 2.34.1 From 26272320cf0cf2d34c7272d72f62e95c18907fa9 Mon Sep 17 00:00:00 2001 From: Sungguk Na Date: Thu, 25 May 2017 19:34:13 +0900 Subject: [PATCH 15/16] Change argument of gpio gdbus method - The gpio methods will pass handle instead of device informations. - Some arguments of gpio handle are removed. Change-Id: I6a0910cbd5e03878c17e5c6426850dbd2865ada4 Signed-off-by: Sungguk Na --- include/peripheral_internal.h | 3 +-- src/peripheral_gdbus_gpio.c | 25 ++++++++++--------------- src/peripheral_gpio.c | 8 -------- src/peripheral_io.xml | 21 ++++++++++----------- 4 files changed, 21 insertions(+), 36 deletions(-) diff --git a/include/peripheral_internal.h b/include/peripheral_internal.h index 85263aa..34f09b0 100644 --- a/include/peripheral_internal.h +++ b/include/peripheral_internal.h @@ -22,8 +22,7 @@ */ struct _peripheral_gpio_s { int pin; - peripheral_gpio_direction_e direction; - peripheral_gpio_edge_e edge; + uint handle; }; /** diff --git a/src/peripheral_gdbus_gpio.c b/src/peripheral_gdbus_gpio.c index 0bbe4f7..721b3d4 100644 --- a/src/peripheral_gdbus_gpio.c +++ b/src/peripheral_gdbus_gpio.c @@ -89,8 +89,7 @@ int peripheral_gdbus_gpio_open(peripheral_gpio_h gpio) if (peripheral_io_gdbus_gpio_call_open_sync( gpio_proxy, gpio->pin, - (gint*)&gpio->edge, - (gint*)&gpio->direction, + &gpio->handle, &ret, NULL, &error) == FALSE) { @@ -111,7 +110,7 @@ int peripheral_gdbus_gpio_close(peripheral_gpio_h gpio) if (peripheral_io_gdbus_gpio_call_close_sync( gpio_proxy, - gpio->pin, + gpio->handle, &ret, NULL, &error) == FALSE) { @@ -132,7 +131,7 @@ int peripheral_gdbus_gpio_get_direction(peripheral_gpio_h gpio, peripheral_gpio_ if (peripheral_io_gdbus_gpio_call_get_direction_sync( gpio_proxy, - gpio->pin, + gpio->handle, (gint*)direction, &ret, NULL, @@ -141,7 +140,6 @@ int peripheral_gdbus_gpio_get_direction(peripheral_gpio_h gpio, peripheral_gpio_ g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } - gpio->direction = *direction; return ret; } @@ -155,7 +153,7 @@ int peripheral_gdbus_gpio_set_direction(peripheral_gpio_h gpio, peripheral_gpio_ if (peripheral_io_gdbus_gpio_call_set_direction_sync( gpio_proxy, - gpio->pin, + gpio->handle, direction, &ret, NULL, @@ -164,7 +162,6 @@ int peripheral_gdbus_gpio_set_direction(peripheral_gpio_h gpio, peripheral_gpio_ g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } - gpio->direction = direction; return ret; } @@ -178,7 +175,7 @@ int peripheral_gdbus_gpio_read(peripheral_gpio_h gpio, int *value) if (peripheral_io_gdbus_gpio_call_read_sync( gpio_proxy, - gpio->pin, + gpio->handle, value, &ret, NULL, @@ -200,7 +197,7 @@ int peripheral_gdbus_gpio_write(peripheral_gpio_h gpio, int value) if (peripheral_io_gdbus_gpio_call_write_sync( gpio_proxy, - gpio->pin, + gpio->handle, value, &ret, NULL, @@ -222,7 +219,7 @@ int peripheral_gdbus_gpio_get_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_ if (peripheral_io_gdbus_gpio_call_get_edge_mode_sync( gpio_proxy, - gpio->pin, + gpio->handle, (int*)edge, &ret, NULL, @@ -231,7 +228,6 @@ int peripheral_gdbus_gpio_get_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_ g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } - gpio->edge = *edge; return ret; } @@ -245,7 +241,7 @@ int peripheral_gdbus_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_ if (peripheral_io_gdbus_gpio_call_set_edge_mode_sync( gpio_proxy, - gpio->pin, + gpio->handle, edge, &ret, NULL, @@ -254,7 +250,6 @@ int peripheral_gdbus_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_ g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } - gpio->edge = edge; return ret; } @@ -268,7 +263,7 @@ int peripheral_gdbus_gpio_register_cb(peripheral_gpio_h gpio, gpio_isr_cb callba if (peripheral_io_gdbus_gpio_call_register_irq_sync( gpio_proxy, - gpio->pin, + gpio->handle, &ret, NULL, &error) == FALSE) { @@ -289,7 +284,7 @@ int peripheral_gdbus_gpio_unregister_cb(peripheral_gpio_h gpio) if (peripheral_io_gdbus_gpio_call_unregister_irq_sync( gpio_proxy, - gpio->pin, + gpio->handle, &ret, NULL, &error) == FALSE) { diff --git a/src/peripheral_gpio.c b/src/peripheral_gpio.c index 2b3bd20..e49477c 100644 --- a/src/peripheral_gpio.c +++ b/src/peripheral_gpio.c @@ -176,8 +176,6 @@ int peripheral_gpio_get_direction(peripheral_gpio_h gpio, peripheral_gpio_direct return PERIPHERAL_ERROR_INVALID_PARAMETER; ret = peripheral_gdbus_gpio_get_direction(gpio, direction); - if (ret == PERIPHERAL_ERROR_NONE) - gpio->direction = (*direction); return ret; } @@ -199,8 +197,6 @@ int peripheral_gpio_set_direction(peripheral_gpio_h gpio, peripheral_gpio_direct /* call gpio_set_direction */ ret = peripheral_gdbus_gpio_set_direction(gpio, direction); - if (ret == PERIPHERAL_ERROR_NONE) - gpio->direction = direction; return ret; } @@ -251,8 +247,6 @@ int peripheral_gpio_get_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e return PERIPHERAL_ERROR_INVALID_PARAMETER; ret = peripheral_gdbus_gpio_get_edge_mode(gpio, edge); - if (ret == PERIPHERAL_ERROR_NONE) - gpio->edge = (*edge); return ret; } @@ -273,8 +267,6 @@ int peripheral_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e /* call gpio_set_edge_mode */ ret = peripheral_gdbus_gpio_set_edge_mode(gpio, edge); - if (ret == PERIPHERAL_ERROR_NONE) - gpio->edge = edge; return ret; } diff --git a/src/peripheral_io.xml b/src/peripheral_io.xml index abc3679..2480f92 100644 --- a/src/peripheral_io.xml +++ b/src/peripheral_io.xml @@ -3,50 +3,49 @@ - - + - + - + - + - + - + - + - + - + - + -- 2.34.1 From 1afae1b2e619a3cfca9697930daac0a16443efc1 Mon Sep 17 00:00:00 2001 From: "jino.cho" Date: Thu, 18 May 2017 19:25:15 +0900 Subject: [PATCH 16/16] Add description for pwm APIs This patch Adds description for pwm APIs. Change-Id: I1dc40bf40e4405616c5f719d905680bf31d783e0 Signed-off-by: jino.cho --- include/peripheral_io.h | 157 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 156 insertions(+), 1 deletion(-) diff --git a/include/peripheral_io.h b/include/peripheral_io.h index 1000993..c68ff98 100644 --- a/include/peripheral_io.h +++ b/include/peripheral_io.h @@ -383,25 +383,180 @@ typedef enum { PERIPHERAL_PWM_POLARITY_INVERSED, } peripheral_pwm_polarity_e; +/** + * @brief Initializes(export) pwm device and creates pwm handle. + * @since_tizen 4.0 + * + * @param[in] device The pwm chip number + * @param[in] channel The pwm channel number to control + * @param[out] pwm The pwm 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_RESOURCE_BUSY Device is in use + * @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_pwm_close() + */ int peripheral_pwm_open(int device, int channel, peripheral_pwm_h *pwm); +/** + * @brief Destory the pwm handle and finalize(unexport) the pwm device. + * @since_tizen 4.0 + * + * @param[in] pwm The handle to the pwm 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_INVALID_OPERATION Invalid access + * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error + * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed + * + * @see peripheral_pwm_open() + */ int peripheral_pwm_close(peripheral_pwm_h pwm); - +/** + * @brief Sets Period of the pwm device. + * @since_tizen 4.0 + * + * @param[in] pwm The handle to the pwm device + * @param[in] period The total period of the PWM signal (in nanoseconds) + * + * @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_INVALID_OPERATION Invalid access + * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error + * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed + */ int peripheral_pwm_set_period(peripheral_pwm_h pwm, int period); +/** + * @brief Gets Period of the pwm device. + * @since_tizen 4.0 + * + * @param[in] pwm The handle to the pwm device + * @param[out] period The total period of the PWM signal (in nanoseconds) + * + * @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_INVALID_OPERATION Invalid access + * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error + * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed + */ int peripheral_pwm_get_period(peripheral_pwm_h pwm, int *period); +/** + * @brief Sets Duty Cycle of the pwm device. + * @since_tizen 4.0 + * + * @param[in] pwm The handle to the pwm device + * @param[in] duty_cycle The active time of the pwm signal (in nanoseconds) + * + * @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_INVALID_OPERATION Invalid access + * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error + * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed + */ int peripheral_pwm_set_duty_cycle(peripheral_pwm_h pwm, int duty_cycle); +/** + * @brief Gets Duty Cycle of the pwm device. + * @since_tizen 4.0 + * + * @param[in] pwm The handle to the pwm device + * @param[out] duty_cycle The active time of the pwm signal (in nanoseconds) + * + * @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_INVALID_OPERATION Invalid access + * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error + * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed + */ int peripheral_pwm_get_duty_cycle(peripheral_pwm_h pwm, int *duty_cycle); +/** + * @brief Sets Polarity of the pwm device. + * @since_tizen 4.0 + * + * @param[in] pwm The handle to the pwm device + * @param[in] polarity The polarity of the pwm signal + * + * @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_INVALID_OPERATION Invalid access + * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error + * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed + */ int peripheral_pwm_set_polarity(peripheral_pwm_h pwm, peripheral_pwm_polarity_e polarity); +/** + * @brief Gets Polarity of the pwm device. + * @since_tizen 4.0 + * + * @param[in] pwm The handle to the pwm device + * @param[out] polarity The polarity of the pwm signal + * + * @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_INVALID_OPERATION Invalid access + * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error + * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed + */ int peripheral_pwm_get_polarity(peripheral_pwm_h pwm, peripheral_pwm_polarity_e *polarity); +/** + * @brief Enable of the pwm device. + * @since_tizen 4.0 + * + * @param[in] pwm The handle to the pwm device + * @param[in] enable Enable/disable the pwm signal + * true - enable + * false - disable + * @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_INVALID_OPERATION Invalid access + * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error + * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed + */ int peripheral_pwm_set_enable(peripheral_pwm_h pwm, bool enable); +/** + * @brief Gets Enable status of the pwm device. + * @since_tizen 4.0 + * + * @param[in] pwm The handle to the pwm device + * @param[out] enable Enable/disable the pwm signal + * + * @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_INVALID_OPERATION Invalid access + * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error + * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed + */ int peripheral_pwm_get_enable(peripheral_pwm_h pwm, bool *enable); /** -- 2.34.1