add gathering stop function
[apps/native/st-things-co2-meter.git] / src / co2.c
index fcb882f..82d164f 100644 (file)
--- a/src/co2.c
+++ b/src/co2.c
@@ -1,18 +1,20 @@
-/*
- * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+/* ****************************************************************
  *
- * Licensed under the Flora License, Version 1.1 (the License);
+ * Copyright 2017 Samsung Electronics All Rights Reserved.
+ *
+ * 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://floralicense.org/license/
+ *      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,
+ * 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 <tizen.h>
 #include <service_app.h>
 #include "sensor-data.h"
 #include "co2-sensor.h"
 
-//#define TEST_RANDOM_VAL_GEN
-#ifdef TEST_RANDOM_VAL_GEN
-#include <time.h>
-#include <stdlib.h>
-#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 (5)
+#define SENSOR_GATHER_COUNT (60)
 
 //#define USE_ST_SDK
 
@@ -53,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;
@@ -91,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++;
@@ -108,17 +77,13 @@ static Eina_Bool __get_co2(void *data)
                unsigned int avg = 0;
                avg = sum/SENSOR_GATHER_COUNT;
 
-               _D("co2 avg value - %u", avg);
-               _D("co2 avg voltage - %.2lfv", co2_sensor_value_to_voltage(avg));
+               _D("co2 avg - [%u], [%u ppm]", avg, co2_sensor_value_to_ppm(avg));
 
                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;
        }
@@ -126,19 +91,25 @@ static Eina_Bool __get_co2(void *data)
        return ECORE_CALLBACK_RENEW;
 }
 
-void gathering_start(void *data)
+static void gathering_stop(void *data)
 {
        app_data *ad = data;
        ret_if(!ad);
 
-       if (ad->getter_co2)
+       if (ad->getter_co2) {
                ecore_timer_del(ad->getter_co2);
+               ad->getter_co2 = NULL;
+       }
+}
+
+static void gathering_start(void *data)
+{
+       app_data *ad = data;
+       ret_if(!ad);
 
        ad->getter_co2 = ecore_timer_add(SENSOR_GATHER_INTERVAL, __get_co2, ad);
        if (!ad->getter_co2)
                _E("Failed to add getter_co2");
-
-       return;
 }
 
 #ifdef USE_ST_SDK
@@ -165,6 +136,8 @@ static void handle_things_status_change(st_things_status_e things_status)
 
        if (things_status == ST_THINGS_STATUS_REGISTERED_TO_CLOUD)
                ecore_main_loop_thread_safe_call_async(gathering_start, g_ad);
+       else
+               ecore_main_loop_thread_safe_call_async(gathering_stop, g_ad);
 }
 
 static bool handle_get_request(st_things_get_request_message_s* req_msg, st_things_representation_s* resp_rep)
@@ -280,8 +253,10 @@ static bool service_app_create(void *user_data)
 static void service_app_control(app_control_h app_control, void *user_data)
 {
 #ifdef USE_ST_SDK
+       __things_stop();
        __things_start();
 #else
+       gathering_stop(user_data);
        gathering_start(user_data);
 #endif
 }
@@ -290,8 +265,8 @@ static void service_app_terminate(void *user_data)
 {
        app_data *ad = (app_data *)user_data;
 
-       if (ad->getter_co2)
-               ecore_timer_del(ad->getter_co2);
+       if (!ad)
+               return;
 
 #ifdef USE_ST_SDK
        __things_stop();