-/*
- * 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 <stdio.h>
#include <string.h>
#include <stdlib.h>
-#include <pthread.h>
#include <Ecore.h>
#include "st_things.h"
#include "sensor-data.h"
#include "co2-sensor.h"
-#define JSON_PATH "device_def.json"
+#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_REF_VOLTAGE (5)
-#define SENSOR_VALUE_MAX (1024)
-#define SENSOR_CO2_V_ZP (0.471)
-#define SENSOR_CO2_V_RA (0.030)
-
-#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
static app_data *g_ad = NULL;
-static double __value_to_voltage(unsigned int value)
-{
- double v = 0;
-
- v = (double)value * SENSOR_REF_VOLTAGE / SENSOR_VALUE_MAX;
-
- return v;
-}
-
static Eina_Bool __get_co2(void *data)
{
int ret = 0;
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 = co2_sensor_read(SENSOR_CH_CO2, &value);
unsigned int avg = 0;
avg = sum/SENSOR_GATHER_COUNT;
- _D("co2 avg value - %u", avg);
- _D("co2 avg voltage - %.2lfv", __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;
}
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
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)
return -1;
}
- snprintf(app_json_path, sizeof(app_json_path), "%s%s", app_res_path, JSON_PATH);
-
if (0 != st_things_set_configuration_prefix_path(app_res_path, app_data_path)) {
_E("st_things_set_configuration_prefix_path() failed!!");
free(app_res_path);
free(app_data_path);
return -1;
}
+ free(app_data_path);
+ snprintf(app_json_path, sizeof(app_json_path), "%s%s", app_res_path, JSON_NAME);
free(app_res_path);
- free(app_data_path);
if (0 != st_things_initialize(app_json_path, &easysetup_complete)) {
_E("st_things_initialize() failed!!");
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
}
{
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();