From 2a06f89870778a2acda17817f45fe96464d4fcb1 Mon Sep 17 00:00:00 2001 From: Jeonghoon Park Date: Tue, 14 Aug 2018 19:05:59 +0900 Subject: [PATCH] remove random gen, add more error handling --- src/co2.c | 53 ++++++++++------------------------------------------- 1 file changed, 10 insertions(+), 43 deletions(-) diff --git a/src/co2.c b/src/co2.c index dc623f6..43f57f5 100644 --- a/src/co2.c +++ b/src/co2.c @@ -27,22 +27,12 @@ #include "sensor-data.h" #include "co2-sensor.h" -//#define TEST_RANDOM_VAL_GEN -#ifdef TEST_RANDOM_VAL_GEN -#include -#include -#define RAND_VAL_MIN 0 -#define RAND_VAL_MAX_CO2 1023 -#endif /* TEST_RANDOM_VAL_GEN */ - #define JSON_NAME "device_def.json" #define SENSOR_URI_CO2 "/capability/airQualitySensor/main/0" #define SENSOR_KEY_CO2 "airQuality" #define SENSOR_KEY_RANGE "range" #define SENSOR_CH_CO2 (0) -#define SENSOR_THRESHOLD_CO2 (650) - #define SENSOR_GATHER_INTERVAL (0.05f) #define SENSOR_GATHER_COUNT (60) @@ -55,32 +45,6 @@ typedef struct app_data_s { static app_data *g_ad = NULL; -#ifdef TEST_RANDOM_VAL_GEN -static int rand_read_co2_sensor(unsigned int *out_value) -{ - unsigned int val = 0; - - val = RAND_VAL_MIN + rand() / (RAND_MAX / (RAND_VAL_MAX_CO2 - RAND_VAL_MIN + 1) + 1); - *out_value = val; - - return 0; -} -#endif - -static int read_sensor(unsigned int *out_value) -{ - int ret = 0; - retv_if(!out_value, -1); - -#ifdef TEST_RANDOM_VAL_GEN - ret = rand_read_co2_sensor(out_value); -#else /* TEST_RANDOM_VAL_GEN */ - ret = co2_sensor_read(SENSOR_CH_CO2, out_value); -#endif /* TEST_RANDOM_VAL_GEN */ - - return ret; -} - static Eina_Bool __get_co2(void *data) { int ret = 0; @@ -93,14 +57,17 @@ static Eina_Bool __get_co2(void *data) if (!ad) { _E("failed to get app_data"); service_app_exit(); + return ECORE_CALLBACK_CANCEL; } if (!ad->co2_data) { _E("failed to get co2_data"); service_app_exit(); + ad->getter_co2 = NULL; + return ECORE_CALLBACK_CANCEL; } - ret = read_sensor(&value); + ret = co2_sensor_read(SENSOR_CH_CO2, &value); retv_if(ret != 0, ECORE_CALLBACK_RENEW); count++; @@ -114,12 +81,9 @@ static Eina_Bool __get_co2(void *data) sensor_data_set_uint(ad->co2_data, avg); - if (avg <= SENSOR_THRESHOLD_CO2) { - #ifdef USE_ST_SDK - st_things_notify_observers(SENSOR_URI_CO2); - #endif - } - +#ifdef USE_ST_SDK + st_things_notify_observers(SENSOR_URI_CO2); +#endif count = 0; sum = 0; } @@ -291,6 +255,9 @@ static void service_app_terminate(void *user_data) { app_data *ad = (app_data *)user_data; + if (!ad) + return; + if (ad->getter_co2) ecore_timer_del(ad->getter_co2); -- 2.7.4