rename modules
[apps/native/st-things-co2-meter.git] / src / co2.c
index 9a9ae5c..895e82b 100644 (file)
--- a/src/co2.c
+++ b/src/co2.c
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Flora License, Version 1.1 (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/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * 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 <co2-sensor.h>
 #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 "thing.h"
 #include "log.h"
+#include "sensor-data.h"
+#include "co2-sensor.h"
 
-static bool service_app_create(void *user_data)
+//#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_PATH "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 USE_ST_SDK
+
+typedef struct app_data_s {
+       Ecore_Timer *getter_co2;
+       sensor_data *co2_data;
+} app_data;
+
+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_co2_sensor(unsigned int *out_value)
 {
-       FN_CALL;
+       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;
+       unsigned int value = 0;
+       static unsigned int sum = 0;
+       static unsigned int count = 0;
+
+       app_data *ad = data;
+
+       if (!ad) {
+               _E("failed to get app_data");
+               service_app_exit();
+       }
+
+       if (!ad->co2_data) {
+               _E("failed to get co2_data");
+               service_app_exit();
+       }
 
+       ret = read_co2_sensor(&value);
+       retv_if(ret != 0, ECORE_CALLBACK_RENEW);
+
+       count++;
+       sum += value;
+
+       if (count == SENSOR_GATHER_COUNT) {
+               unsigned int avg = 0;
+               avg = sum/SENSOR_GATHER_COUNT;
+
+               _D("co2 avg value - %u", 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
+               }
+
+               count = 0;
+               sum = 0;
+       }
+
+       return ECORE_CALLBACK_RENEW;
+}
+
+void gathering_start(void *data)
+{
+       app_data *ad = data;
+       ret_if(!ad);
+
+       if (ad->getter_co2)
+               ecore_timer_del(ad->getter_co2);
+
+       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
+static bool handle_reset_request(void)
+{
+       _D("Received a request for RESET.");
+       return false;
+}
+
+static void handle_reset_result(bool result)
+{
+       _D("Reset %s.\n", result ? "succeeded" : "failed");
+}
+
+static bool handle_ownership_transfer_request(void)
+{
+       _D("Received a request for Ownership-transfer.");
        return true;
 }
 
-static void service_app_terminate(void *user_data)
+static void handle_things_status_change(st_things_status_e things_status)
+{
+       _D("Things status is changed: %d\n", things_status);
+
+       if (things_status == ST_THINGS_STATUS_REGISTERED_TO_CLOUD)
+               ecore_main_loop_thread_safe_call_async(gathering_start, g_ad);
+}
+
+static bool handle_get_request(st_things_get_request_message_s* req_msg, st_things_representation_s* resp_rep)
 {
-       FN_CALL;
+       _D("resource_uri [%s]", req_msg->resource_uri);
+       retv_if(!g_ad, false);
+
+       if (0 == strcmp(req_msg->resource_uri, SENSOR_URI_CO2)) {
+               if (req_msg->has_property_key(req_msg, SENSOR_KEY_CO2)) {
+                       unsigned int value = 0;
+                       sensor_data_get_uint(g_ad->co2_data, &value);
+                       resp_rep->set_int_value(resp_rep, SENSOR_KEY_CO2, value);
+               }
+               if (req_msg->has_property_key(req_msg, SENSOR_KEY_RANGE)) {
+                       const double range[2] = { 0.0, 1024.0 };
+                       resp_rep->set_double_array_value(resp_rep, SENSOR_KEY_RANGE, range, 2);
+               }
+               return true;
+       }
+       _E("not supported uri");
+       return false;
 }
 
-static void service_app_control(app_control_h app_control, void *user_data)
+static bool handle_set_request(st_things_set_request_message_s* req_msg, st_things_representation_s* resp_rep)
 {
-       FN_CALL;
-       if (app_control == NULL) {
-               ERR("app_control is NULL");
-               return;
+       _D("resource_uri [%s]", req_msg->resource_uri);
+    return false;
+}
+
+static int __things_init(void)
+{
+       bool easysetup_complete = false;
+       char app_json_path[128] = {'\0', };
+       char *app_res_path = NULL;
+       char *app_data_path = NULL;
+
+       app_res_path = app_get_resource_path();
+       if (!app_res_path) {
+               _E("app_res_path is NULL!!");
+               return -1;
+       }
+
+       app_data_path = app_get_data_path();
+       if (!app_data_path) {
+               _E("app_data_path is NULL!!");
+               free(app_res_path);
+               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;
        }
 
-       int ret;
-       char *value = NULL;
-       ret = app_control_get_extra_data(app_control, "cmd", &value);
-       DBG("value: [%s]", value);
-       if (value == NULL)
-               init_thing();
-       else
-               ERR("Unknown command");
+       free(app_res_path);
+       free(app_data_path);
+
+       if (0 != st_things_initialize(app_json_path, &easysetup_complete)) {
+               _E("st_things_initialize() failed!!");
+               return -1;
+       }
 
-       free(value);
+       _D("easysetup_complete:[%d] ", easysetup_complete);
+
+       st_things_register_request_cb(handle_get_request, handle_set_request);
+       st_things_register_reset_cb(handle_reset_request, handle_reset_result);
+       st_things_register_user_confirm_cb(handle_ownership_transfer_request);
+       st_things_register_things_status_change_cb(handle_things_status_change);
+
+       return 0;
 }
 
-int main(int argc, char *argv[])
+static int __things_deinit(void)
 {
-       FN_CALL;
+       st_things_deinitialize();
+       return 0;
+}
+
+static int __things_start(void)
+{
+       st_things_start();
+       return 0;
+}
 
-       char ad[50] = {0,};
+static int __things_stop(void)
+{
+       st_things_stop();
+       return 0;
+}
+#endif /* USE_ST_SDK */
+
+static bool service_app_create(void *user_data)
+{
+       app_data *ad = (app_data *)user_data;
+
+       ad->co2_data = sensor_data_new(SENSOR_DATA_TYPE_UINT);
+       if (!ad->co2_data)
+               return false;
+
+#ifdef USE_ST_SDK
+       if (__things_init())
+               return false;
+#endif
+
+       return true;
+}
+
+static void service_app_control(app_control_h app_control, void *user_data)
+{
+#ifdef USE_ST_SDK
+       __things_start();
+#else
+       gathering_start(user_data);
+#endif
+}
+
+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);
+
+#ifdef USE_ST_SDK
+       __things_stop();
+       __things_deinit();
+#endif
+
+       sensor_data_free(ad->co2_data);
+       co2_sensor_close();
+       free(ad);
+
+       FN_END;
+}
+
+int main(int argc, char *argv[])
+{
+       app_data *ad = NULL;
        service_app_lifecycle_callback_s event_callback;
 
+       ad = calloc(1, sizeof(app_data));
+       retv_if(!ad, -1);
+
+       g_ad = ad;
+
        event_callback.create = service_app_create;
        event_callback.terminate = service_app_terminate;
        event_callback.app_control = service_app_control;