summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeonghoon Park <jh1979.park@samsung.com>2019-08-26 17:29:00 +0900
committerJeonghoon Park <jh1979.park@samsung.com>2019-08-26 17:29:38 +0900
commit0b45430da216dc4265d783af260e67f6e29bd1b1 (patch)
treeb0f6f94d9b142c6cd4b0d534a7f1b0eca92a2312
parentc6e345f6d918eb75f4bdd9e8ce7cf50c3ea320c3 (diff)
downloadst-things-light-0b45430da216dc4265d783af260e67f6e29bd1b1.tar.gz
st-things-light-0b45430da216dc4265d783af260e67f6e29bd1b1.tar.bz2
st-things-light-0b45430da216dc4265d783af260e67f6e29bd1b1.zip
update interrupt handling logic
Change-Id: I338c7015fb6234b163fe2b73aa937c5beb196556
-rwxr-xr-xinc/resource/resource_infrared_motion.h13
-rwxr-xr-xsrc/resource/resource_infrared_motion.c122
2 files changed, 68 insertions, 67 deletions
diff --git a/inc/resource/resource_infrared_motion.h b/inc/resource/resource_infrared_motion.h
index 60565a7..115c634 100755
--- a/inc/resource/resource_infrared_motion.h
+++ b/inc/resource/resource_infrared_motion.h
@@ -17,21 +17,8 @@
#ifndef __RESOURCE_INFRARED_MOTION_H__
#define __RESOURCE_INFRARED_MOTION_H__
-typedef enum {
- MOTION_HANDLE_ERROR_NONE = 0, /**< Successful */
- MOTION_HANDLE_ERROR_NOT_OPEN,
- MOTION_HANDLE_ERROR_INVALID_PIN
-} resource_infrared_motion_handle_error_e;
-
typedef void (*resource_infrared_motion_interrupted_cb) (uint32_t motion_value, void *user_data);
-typedef struct resource_infrared_motion_interrupted_data_s {
- resource_infrared_motion_interrupted_cb interrupted_cb;
- void *interrupted_cb_user_data;
- uint32_t motion_value;
- int is_called_first_time;
-} resource_infrared_motion_interrupted_data;
-
/**
* @brief Reads value of gpio connected infrared motion sensor (HC-SR501)
* @param[in] pin_num The gpio pin number for the infrared motion sensor
diff --git a/src/resource/resource_infrared_motion.c b/src/resource/resource_infrared_motion.c
index 650494a..574c58f 100755
--- a/src/resource/resource_infrared_motion.c
+++ b/src/resource/resource_infrared_motion.c
@@ -15,16 +15,26 @@
*/
#include <peripheral_io.h>
-#include <stdlib.h>
-
#include "resource/resource_infrared_motion.h"
#include "log.h"
+enum {
+ MOTION_HANDLE_ERROR_NONE = 0, /**< Successful */
+ MOTION_HANDLE_ERROR_NOT_OPEN,
+ MOTION_HANDLE_ERROR_INVALID_PIN
+} motion_handle_error_e;
+
+struct resource_im_interrupted_data {
+ resource_infrared_motion_interrupted_cb interrupted_cb;
+ void *interrupted_cb_user_data;
+ uint32_t motion_value;
+};
+
static peripheral_gpio_h g_sensor_h = NULL;
static int g_pin_num = -1;
-static resource_infrared_motion_interrupted_data *g_interrupted_data = NULL;
+static struct resource_im_interrupted_data g_interrupted_data;
-int _resource_validate_infrared_motion(int pin_num)
+static int _resource_validate_infrared_motion(int pin_num)
{
int ret = MOTION_HANDLE_ERROR_NONE;
@@ -38,7 +48,7 @@ int _resource_validate_infrared_motion(int pin_num)
return ret;
}
-int resource_open_infrared_motion(int pin_num)
+static int resource_open_infrared_motion(int pin_num)
{
peripheral_gpio_h temp = NULL;
@@ -65,18 +75,21 @@ int resource_open_infrared_motion(int pin_num)
int resource_read_infrared_motion(int pin_num, uint32_t *out_value)
{
int ret = PERIPHERAL_ERROR_NONE;
-
- if (_resource_validate_infrared_motion(pin_num) == MOTION_HANDLE_ERROR_NOT_OPEN) {
- ret = resource_open_infrared_motion(pin_num);
- retv_if(ret < 0, -1);
- }
- if (_resource_validate_infrared_motion(pin_num) == MOTION_HANDLE_ERROR_INVALID_PIN) {
- _E("Invalid pin number.");
- return -1;
+ int ret_valid = MOTION_HANDLE_ERROR_NONE;
+
+ ret_valid = _resource_validate_infrared_motion(pin_num);
+ if (ret_valid != MOTION_HANDLE_ERROR_NONE) {
+ if (ret_valid == MOTION_HANDLE_ERROR_NOT_OPEN) {
+ ret = resource_open_infrared_motion(pin_num);
+ retv_if(ret != PERIPHERAL_ERROR_NONE, -1);
+ } else if (ret_valid == MOTION_HANDLE_ERROR_INVALID_PIN) {
+ _E("Invalid pin number.");
+ return -1;
+ }
}
ret = peripheral_gpio_read(g_sensor_h, out_value);
- retv_if(ret < 0, -1);
+ retv_if(ret != PERIPHERAL_ERROR_NONE, -1);
return 0;
}
@@ -87,85 +100,86 @@ void resource_close_infrared_motion(void)
_I("Infrared Motion Sensor is finishing...");
+ if (g_interrupted_data.interrupted_cb) {
+ peripheral_gpio_unset_interrupted_cb(g_sensor_h);
+ g_interrupted_data.interrupted_cb = NULL;
+ g_interrupted_data.interrupted_cb_user_data = NULL;
+ g_interrupted_data.motion_value = 0;
+ }
+
peripheral_gpio_close(g_sensor_h);
- if (g_interrupted_data != NULL) {
- free(g_interrupted_data);
- g_interrupted_data = NULL;
- }
g_sensor_h = NULL;
g_pin_num = -1;
}
void _resoucre_motion_interrupted_cb (peripheral_gpio_h gpio, peripheral_error_e error, void *user_data)
{
- int ret = PERIPHERAL_ERROR_NONE;
-
if (!g_sensor_h) return;
- if (g_interrupted_data->is_called_first_time) {
- ret = peripheral_gpio_read(g_sensor_h, &g_interrupted_data->motion_value);
- if (ret) return;
+ g_interrupted_data.motion_value = !g_interrupted_data.motion_value;
- g_interrupted_data->is_called_first_time = 0;
- } else {
- // toggle g_interrupted_data->motion_value
- g_interrupted_data->motion_value = !g_interrupted_data->motion_value;
- }
-
- g_interrupted_data->interrupted_cb(g_interrupted_data->motion_value, g_interrupted_data->interrupted_cb_user_data);
+ g_interrupted_data.interrupted_cb(g_interrupted_data.motion_value, g_interrupted_data.interrupted_cb_user_data);
}
int resource_set_interrupted_cb_infrared_motion(int pin_num, resource_infrared_motion_interrupted_cb interrupted_cb, void *user_data)
{
int ret = PERIPHERAL_ERROR_NONE;
- if (g_interrupted_data == NULL) {
- g_interrupted_data = calloc(1, sizeof(resource_infrared_motion_interrupted_data));
- }
+ int ret_valid = MOTION_HANDLE_ERROR_NONE;
- if (_resource_validate_infrared_motion(pin_num) == MOTION_HANDLE_ERROR_NOT_OPEN) {
+ ret_valid = _resource_validate_infrared_motion(pin_num);
+ if (ret_valid == MOTION_HANDLE_ERROR_NOT_OPEN) {
ret = resource_open_infrared_motion(pin_num);
- retv_if(ret < 0, -1);
- }
- if (_resource_validate_infrared_motion(pin_num) == MOTION_HANDLE_ERROR_INVALID_PIN) {
+ retv_if(ret != PERIPHERAL_ERROR_NONE, -1);
+ } else if (ret_valid == MOTION_HANDLE_ERROR_INVALID_PIN) {
_E("Invalid pin number.");
return -1;
}
- g_interrupted_data->interrupted_cb = interrupted_cb;
- g_interrupted_data->interrupted_cb_user_data = user_data;
- g_interrupted_data->motion_value = 0;
- g_interrupted_data->is_called_first_time = 1;
-
ret = peripheral_gpio_set_edge_mode(g_sensor_h, PERIPHERAL_GPIO_EDGE_BOTH);
- retv_if(ret < 0, -1);
+ retv_if(ret != PERIPHERAL_ERROR_NONE, -1);
+
+ g_interrupted_data.motion_value = 0;
+ ret = peripheral_gpio_read(g_sensor_h, &g_interrupted_data.motion_value);
+ retv_if(ret != PERIPHERAL_ERROR_NONE, -1);
- ret = peripheral_gpio_set_interrupted_cb(g_sensor_h, _resoucre_motion_interrupted_cb, g_interrupted_data);
- retv_if(ret < 0, -1);
+ ret = peripheral_gpio_set_interrupted_cb(g_sensor_h, _resoucre_motion_interrupted_cb, &g_interrupted_data);
+ goto_if(ret != PERIPHERAL_ERROR_NONE, ERROR);
+
+ g_interrupted_data.interrupted_cb = interrupted_cb;
+ g_interrupted_data.interrupted_cb_user_data = user_data;
return 0;
+
+ERROR:
+ _E("failed to read gpio");
+ peripheral_gpio_unset_interrupted_cb(g_sensor_h);
+ g_interrupted_data.interrupted_cb = NULL;
+ g_interrupted_data.interrupted_cb_user_data = NULL;
+ g_interrupted_data.motion_value = 0;
+ return -1;
}
int resource_unset_interrupted_cb_infrared_motion(int pin_num)
{
int ret = PERIPHERAL_ERROR_NONE;
+ int ret_valid = MOTION_HANDLE_ERROR_NONE;
- if (g_interrupted_data != NULL) {
- free(g_interrupted_data);
- g_interrupted_data = NULL;
- }
-
- if (_resource_validate_infrared_motion(pin_num) == MOTION_HANDLE_ERROR_NOT_OPEN) {
+ ret_valid = _resource_validate_infrared_motion(pin_num);
+ if (ret_valid == MOTION_HANDLE_ERROR_NOT_OPEN) {
_E("No open handle.");
return -1;
- }
- else if (_resource_validate_infrared_motion(pin_num) == MOTION_HANDLE_ERROR_INVALID_PIN) {
+ } else if (ret_valid == MOTION_HANDLE_ERROR_INVALID_PIN) {
_E("Invalid pin number.");
return -1;
}
ret = peripheral_gpio_unset_interrupted_cb(g_sensor_h);
- retv_if(ret < 0, -1);
+ retv_if(ret != PERIPHERAL_ERROR_NONE, -1);
+
+ g_interrupted_data.interrupted_cb = NULL;
+ g_interrupted_data.interrupted_cb_user_data = NULL;
+ g_interrupted_data.motion_value = 0;
return 0;
}