summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/controller.c296
1 files changed, 296 insertions, 0 deletions
diff --git a/src/controller.c b/src/controller.c
new file mode 100644
index 0000000..32a7039
--- /dev/null
+++ b/src/controller.c
@@ -0,0 +1,296 @@
+/*
+ * 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 <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 "resource.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_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 (1)
+
+#define SENSOR_THRESHOLD_CO2 (500)
+
+//#define USE_ST_SDK
+
+typedef struct app_data_s {
+ Ecore_Timer *getter_co2;
+ unsigned int co2_val;
+ pthread_mutex_t mutex;
+} app_data;
+
+static app_data *g_ad = NULL;
+
+#ifdef TEST_RANDOM_VAL_GEN
+static int rand_resource_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 Eina_Bool __get_co2(void *data)
+{
+
+ int ret = 0;
+ unsigned int value = 0;
+
+ app_data *ad = data;
+
+ retv_if(!ad, false);
+
+#ifdef TEST_RANDOM_VAL_GEN
+ ret = rand_resource_read_co2_sensor(&value);
+#else /* TEST_RANDOM_VAL_GEN */
+ ret = resource_read_co2_sensor(SENSOR_CH_CO2, &value);
+#endif /* TEST_RANDOM_VAL_GEN */
+ retv_if(ret != 0, ECORE_CALLBACK_RENEW);
+
+ _D("co2 value - %u", value);
+
+ pthread_mutex_lock(&ad->mutex);
+ ad->co2_val = value;
+ pthread_mutex_unlock(&ad->mutex);
+
+#ifdef USE_ST_SDK
+ if (value >= SENSOR_THRESHOLD_CO2)
+ st_things_notify_observers(SENSOR_URI_CO2);
+#endif
+
+ return ECORE_CALLBACK_RENEW;
+}
+
+void gathering_start(void *data)
+{
+ app_data *ad = data;
+
+ ad->getter_co2 = ecore_timer_add(1.0f, __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 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)
+{
+ _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;
+ pthread_mutex_lock(&ad->mutex);
+ value = ad->co2_val;
+ pthread_mutex_unlock(&ad->mutex);
+ resp_rep->set_int_value(resp_rep, SENSOR_KEY_CO2, value);
+ }
+ if (req_msg->has_property_key(req_msg, SENSOR_KEY_RANGE)) {
+ 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 bool handle_set_request(st_things_set_request_message_s* req_msg, st_things_representation_s* resp_rep)
+{
+ _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;
+ }
+
+ 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;
+ }
+
+ _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;
+}
+
+static int __things_deinit(void)
+{
+ st_things_deinitialize();
+ return 0;
+}
+
+static int __things_start(void)
+{
+ st_things_start();
+ return 0;
+}
+
+static int __things_stop(void)
+{
+ st_things_stop();
+ return 0;
+}
+#endif /* USE_ST_SDK */
+
+static bool service_app_create(void *user_data)
+{
+ FN_CALL;
+
+#ifdef USE_ST_SDK
+ if (__things_init())
+ return false;
+#endif
+
+ FN_END;
+
+ return true;
+}
+
+static void service_app_control(app_control_h app_control, void *user_data)
+{
+ FN_CALL;
+
+#ifdef USE_ST_SDK
+ __things_start();
+#else
+ gathering_start(user_data);
+#endif
+
+}
+
+static void service_app_terminate(void *user_data)
+{
+ FN_CALL;
+ app_data *ad = (app_data *)user_data;
+
+ if (ad->getter_co2)
+ ecore_timer_del(ad->getter_co2);
+
+ resource_close_all();
+
+#ifdef USE_ST_SDK
+ __things_stop();
+ __things_deinit();
+#endif
+
+ free(ad);
+}
+
+int main(int argc, char *argv[])
+{
+ FN_CALL;
+
+ app_data *ad = NULL;
+ service_app_lifecycle_callback_s event_callback;
+
+ ad = calloc(1, sizeof(app_data));
+ retv_if(!ad, -1);
+
+ pthread_mutex_init(&ad->mutex, NULL);
+ g_ad = ad;
+
+ event_callback.create = service_app_create;
+ event_callback.terminate = service_app_terminate;
+ event_callback.app_control = service_app_control;
+
+ return service_app_main(argc, argv, &event_callback, ad);
+}