summaryrefslogtreecommitdiff
path: root/src/controller.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/controller.c')
-rw-r--r--[-rwxr-xr-x]src/controller.c703
1 files changed, 522 insertions, 181 deletions
diff --git a/src/controller.c b/src/controller.c
index 1c55e50..683294c 100755..100644
--- a/src/controller.c
+++ b/src/controller.c
@@ -18,300 +18,637 @@
#include <service_app.h>
#include <string.h>
#include <stdlib.h>
-#include <Ecore.h>
+#include <stdint.h>
+#include <glib.h>
-#include "st_things.h"
#include "log.h"
#include "resource/resource_infrared_motion_sensor.h"
#include "resource/resource_led.h"
#include "sensor-data.h"
-#define JSON_NAME "device_def.json"
+#define LED_ON "on"
+#define LED_OFF "off"
+
+#define USE_ST_SDK
+
+#ifdef USE_ST_SDK
+
+#include "smartthings.h"
+#include "smartthings_resource.h"
+#include "smartthings_payload.h"
+
+/* You have to FIX IT !!! */
+#define CERT_FILE "certificate.pem" // cert file name in 'res' directory
+#define PRIV_FILE "privatekey.der" // private key file name in 'res' directory
+
#define SENSOR_MOTION_URI "/capability/motionSensor/main/0"
#define SENSOR_MOTION_KEY "value"
+#define SENSOR_MOTION_CH 46
#define SENSOR_LED_URI "/capability/switch/main/0"
#define SENSOR_LED_KEY "power"
#define SENSOR_LED_INIT "off"
+#define SENSOR_LED_CH 130
+#define SENSOR_GATHER_INTERVAL (50) //50ms
-#define LED_ON "on"
-#define LED_OFF "off"
-
-#define USE_ST_SDK
+#endif /* USE_ST_SDK */
typedef struct app_data_s {
- Ecore_Timer *getter_motion;
+ guint getter_motion;
sensor_data *motion_data;
sensor_data *led_data;
+#ifdef USE_ST_SDK
+ smartthings_h st_master_h;
+ smartthings_resource_h st_res_h;
+ smartthings_resource_connection_status_e st_res_conn_status;
+#endif /* USE_ST_SDK */
} app_data;
static app_data *g_ad = NULL;
-static Eina_Bool __change_motion_sensor_data(void *data)
+#ifdef USE_ST_SDK
+
+/* smartthings resource functions */
+static const char *
+__resource_error_to_str(smartthings_resource_error_e error)
{
- uint32_t value = 0;
+ const char *err_str = NULL;
+
+ switch (error) {
+ case SMARTTHINGS_RESOURCE_ERROR_NONE:
+ err_str = "SMARTTHINGS_RESOURCE_ERROR_NONE";
+ break;
+ case SMARTTHINGS_RESOURCE_ERROR_INVALID_PARAMETER:
+ err_str = "SMARTTHINGS_RESOURCE_ERROR_INVALID_PARAMETER";
+ break;
+ case SMARTTHINGS_RESOURCE_ERROR_OUT_OF_MEMORY:
+ err_str = "SMARTTHINGS_RESOURCE_ERROR_OUT_OF_MEMORY";
+ break;
+ case SMARTTHINGS_RESOURCE_ERROR_PERMISSION_DENIED:
+ err_str = "SMARTTHINGS_RESOURCE_ERROR_PERMISSION_DENIED";
+ break;
+ case SMARTTHINGS_RESOURCE_ERROR_NO_DATA:
+ err_str = "SMARTTHINGS_RESOURCE_ERROR_NO_DATA";
+ break;
+ case SMARTTHINGS_RESOURCE_ERROR_NOT_SUPPORTED:
+ err_str = "SMARTTHINGS_RESOURCE_ERROR_NOT_SUPPORTED";
+ break;
+ case SMARTTHINGS_RESOURCE_ERROR_OPERATION_FAILED:
+ err_str = "SMARTTHINGS_RESOURCE_ERROR_NOT_SUPPORTED";
+ break;
+ case SMARTTHINGS_RESOURCE_ERROR_SERVICE_UNAVAILABLE:
+ err_str = "SMARTTHINGS_RESOURCE_ERROR_SERVICE_UNAVAILABLE";
+ break;
+ default:
+ err_str = "Unknown error";
+ break;
+ }
- // Get value from motion sensor
- int ret = resource_read_infrared_motion_sensor(46, &value);
+ return err_str;
+}
- if (ret != 0) _E("Cannot read sensor value");
+static bool
+handle_get_motion(smartthings_payload_h resp_payload, void *user_data)
+{
+ app_data *ad = user_data;
+ bool value = false;
- sensor_data_set_bool(g_ad->motion_data, value);
+ retv_if(!ad, false);
- _D("Detected motion value is: %d", value);
+ sensor_data_get_bool(ad->motion_data, &value);
+ smartthings_payload_set_bool(resp_payload, SENSOR_MOTION_KEY, value);
- // Notify observers of the Motion sensor resource
- st_things_notify_observers(SENSOR_MOTION_URI);
+ _D("Value : %d", value);
- return ECORE_CALLBACK_RENEW;
+ return true;
}
-static int __change_led_data(void *data, char *state) {
- int ret = 0;
- app_data *ad = data;
+static bool
+handle_get_led(smartthings_payload_h resp_payload, void *user_data)
+{
+ app_data *ad = user_data;
+ const char *str = NULL;
- retv_if(!ad, -1);
- retv_if(!ad->led_data, -1);
+ retv_if(!ad, false);
- sensor_data_set_string(g_ad->led_data, state, strlen(state));
+ sensor_data_get_string(ad->led_data, &str);
- if (0 == strcmp(state, LED_ON)) {
- ret = resource_write_led(130, 1);
- } else {
- ret = resource_write_led(130, 0);
+ if (!str) {
+ str = SENSOR_LED_INIT;
}
- retv_if(ret != 0, -1);
+ smartthings_payload_set_string(resp_payload, SENSOR_LED_KEY, str);
- // Notify observers of the LED resource
- st_things_notify_observers(SENSOR_LED_URI);
+ _D("Power : %s", str);
- return 0;
+// free(str);
+
+ return true;
}
-static bool __handle_get_request_on_motion (st_things_get_request_message_s* req_msg, st_things_representation_s* resp_rep)
+static bool
+handle_set_led(smartthings_payload_h resp_payload, void *user_data)
{
- if (req_msg->has_property_key(req_msg, SENSOR_MOTION_KEY)) {
- bool value = false;
+ app_data *ad = user_data;
+ const char *str = NULL;
+ int ret = 0;
+
+ retv_if(!ad, false);
+ _D("set [%s:%s] == %s", SENSOR_LED_URI, SENSOR_LED_KEY, str);
+
+ smartthings_payload_set_string(resp_payload, SENSOR_LED_KEY, str);
+
+// ret = __change_led_data(ad, strdup(str));
+
+ retv_if(ret != 0, false);
+
+// free(str);
+
+ return true;
+}
- sensor_data_get_bool(g_ad->motion_data, &value);
+static void
+_request_cb(smartthings_resource_h handle, int req_id,
+ const char *uri, smartthings_resource_req_type_e req_type,
+ smartthings_payload_h payload, void *user_data)
+{
+ smartthings_payload_h resp_payload = NULL;
+ bool result = false;
+ int error = SMARTTHINGS_RESOURCE_ERROR_NONE;
- // Update the response representation about the Motion sensor property which is sent to the client
- resp_rep->set_bool_value(resp_rep, SENSOR_MOTION_KEY, value);
+ _D("request on %s, type[%d], id[%d]", uri, req_type, req_id);
- _D("Value : %d", value);
+ smartthings_payload_create(&resp_payload);
+ if (!resp_payload) {
+ _E("Response payload is NULL");
+ return;
+ }
- return true;
+ if (req_type == SMARTTHINGS_RESOURCE_REQUEST_GET) {
+ if (0 == g_strcmp0(uri, SENSOR_MOTION_URI))
+ result = handle_get_motion(resp_payload, user_data);
+ else if (0 == g_strcmp0(uri, SENSOR_LED_URI))
+ result = handle_get_led(resp_payload, user_data);
+ else
+ _E("No matching Resource uri to get");
+ } else if (req_type == SMARTTHINGS_RESOURCE_REQUEST_SET) {
+ if (0 == g_strcmp0(uri, SENSOR_LED_URI))
+ result = handle_set_led(resp_payload, user_data);
+ else
+ _E("No matching Resource uri to get");
} else {
- _E("not supported property");
+ _E("Invalid request type - %d", req_type);
+ smartthings_payload_destroy(resp_payload);
+ return;
+ }
- return false;
+ error = smartthings_resource_send_response(handle, req_id, uri, resp_payload, result);
+ if (error != SMARTTHINGS_RESOURCE_ERROR_NONE) {
+ smartthings_payload_destroy(resp_payload);
+ _E("smartthings_resource_send_response() failed, [%s]",
+ __resource_error_to_str(error));
+ return;
}
+
+ if (req_type == SMARTTHINGS_RESOURCE_REQUEST_SET) {
+ error = smartthings_resource_notify(handle, uri, resp_payload);
+ if (error != SMARTTHINGS_RESOURCE_ERROR_NONE)
+ _E("smartthings_resource_notify() failed, [%s]",
+ __resource_error_to_str(error));
+ }
+
+ if (smartthings_payload_destroy(resp_payload))
+ _E("smartthings_payload_destroy failed");
+
+ return;
}
-static bool __handle_get_request_on_led (st_things_get_request_message_s* req_msg, st_things_representation_s* resp_rep)
+static void
+_resource_connection_status_cb(smartthings_resource_error_e error,
+ smartthings_resource_h handle,
+ smartthings_resource_connection_status_e status, void *user_data)
{
- if (req_msg->has_property_key(req_msg, SENSOR_LED_KEY)) {
- const char *str = NULL;
-
- sensor_data_get_string(g_ad->led_data, &str);
+ app_data *ad = user_data;
- if (!str) {
- str = SENSOR_LED_INIT;
- }
+ _D("result [%s], status=[%d]", __resource_error_to_str(error), status);
- // Update the response representation about the LED property which is sent to the client
- resp_rep->set_str_value(resp_rep, SENSOR_LED_KEY, str);
+ ret_if(!ad);
- _D("Power : %s", str);
+ ad->st_res_conn_status = status;
- return true;
+ if (status == SMARTTHINGS_RESOURCE_CONNECTION_STATUS_CONNECTED) {
+ if (smartthings_resource_set_request_cb(handle, _request_cb, ad)) {
+ _E("smartthings_resource_set_request_cb() is failed");
+ return;
+ }
} else {
- _E("not supported property");
-
- return false;
+ _E("connection failed");
}
+ return;
}
-static bool __handle_set_request_on_led (st_things_set_request_message_s* req_msg, st_things_representation_s* resp_rep)
+static int
+st_thing_notify_resource(app_data *ad, const char *uri, const char *key, sensor_data *data)
{
- int ret = 0;
- char *str = NULL;
+ smartthings_resource_h handle = NULL;
+ smartthings_payload_h payload = NULL;
+ int error = SMARTTHINGS_RESOURCE_ERROR_NONE;
+ sensor_data_type_e data_type = SENSOR_DATA_TYPE_NONE;
- if (req_msg->rep->get_str_value(req_msg->rep, SENSOR_LED_KEY, &str)) {
- retv_if(!str, false);
+ retv_if(!ad, -1);
+ retv_if(!ad->st_res_h, -1);
+ retv_if(ad->st_res_conn_status != SMARTTHINGS_RESOURCE_CONNECTION_STATUS_CONNECTED, -1);
+ retv_if(!uri, -1);
+ retv_if(!key, -1);
+ retv_if(!data, -1);
- _D("set [%s:%s] == %s", SENSOR_LED_URI, SENSOR_LED_KEY, str);
+ handle = ad->st_res_h;
- // Update the response representation about the LED property which is sent to the client
- resp_rep->set_str_value(resp_rep, SENSOR_LED_KEY, str);
+ data_type = sensor_data_get_type(data);
+ retv_if(data_type == SENSOR_DATA_TYPE_NONE, -1);
- // Turn on LED light
- ret = __change_led_data(g_ad, strdup(str));
+ smartthings_payload_create(&payload);
+ if (!payload) {
+ _E("failed to create payload is NULL");
+ return -1;
+ }
- retv_if(ret != 0, false);
- } else {
- _E("cannot get a string value");
+ switch (data_type) {
+ case SENSOR_DATA_TYPE_INT:
+ {
+ int value = 0;
+ sensor_data_get_int(data, &value);
+ smartthings_payload_set_int(payload, key, value);
+ }
+ break;
+ case SENSOR_DATA_TYPE_UINT:
+ {
+ unsigned int value = 0;
+ sensor_data_get_uint(data, &value);
+ smartthings_payload_set_int(payload, key, (int)value);
+ }
+ break;
+ case SENSOR_DATA_TYPE_BOOL:
+ {
+ bool value = 0;
+ sensor_data_get_bool(data, &value);
+ smartthings_payload_set_bool(payload, key, value);
+ }
+ break;
+ case SENSOR_DATA_TYPE_DOUBLE:
+ {
+ double value = 0;
+ sensor_data_get_double(data, &value);
+ smartthings_payload_set_double(payload, key, value);
+ }
+ break;
+ case SENSOR_DATA_TYPE_STR:
+ {
+ const char *value = NULL;
+ sensor_data_get_string(data, &value);
+ smartthings_payload_set_string(payload, key, value);
+ }
+ break;
+ case SENSOR_DATA_TYPE_NONE:
+ default:
+ _E("unsupport data type");
+ break;
+ }
- return false;
+ error = smartthings_resource_notify(handle, uri, payload);
+ if (error != SMARTTHINGS_RESOURCE_ERROR_NONE)
+ _E("smartthings_resource_notify() failed, [%s]",
+ __resource_error_to_str(error));
+
+ smartthings_payload_destroy(payload);
+
+ return 0;
+}
+
+static int st_thing_resource_init(app_data *ad)
+{
+ smartthings_resource_h st_res_h = NULL;
+ int error = 0;
+
+ retv_if(!ad, -1);
+ if (ad->st_res_h) {
+ _I("Already initialized!");
+ return 0;
+ }
+ error = smartthings_resource_initialize(&st_res_h,
+ _resource_connection_status_cb, ad);
+ if (error) {
+ _E("smartthings_resource_initialize() is failed, [%s]",
+ __resource_error_to_str(error));
+ return -1;
}
- free(str);
+ ad->st_res_h = st_res_h;
+ ad->st_res_conn_status = SMARTTHINGS_RESOURCE_CONNECTION_STATUS_DISCONNECTED;
- return true;
+ return 0;
}
-void gathering_start(void *data)
+static int st_thing_resource_fini(app_data *ad)
{
- app_data *ad = data;
- ret_if(!ad);
+ retv_if(!ad, -1);
- if (ad->getter_motion)
- ecore_timer_del(ad->getter_motion);
+ if (!ad->st_res_h)
+ return 0;
- ad->getter_motion = ecore_timer_add(1.0f, __change_motion_sensor_data, ad);
+ smartthings_resource_unset_request_cb(ad->st_res_h);
+ smartthings_resource_deinitialize(ad->st_res_h);
- if (!ad->getter_motion) {
- _E("Failed to add infrared motion getter timer");
- }
+ ad->st_res_h = NULL;
+ ad->st_res_conn_status = SMARTTHINGS_RESOURCE_CONNECTION_STATUS_DISCONNECTED;
- return;
+ return 0;
}
-static bool handle_reset_request(void)
+/* smartthings master functions */
+static const char *__master_error_to_str(smartthings_error_e error)
{
- _D("Received a request for RESET.");
- return false;
+ const char *err_str = NULL;
+
+ switch (error) {
+ case SMARTTHINGS_ERROR_NONE:
+ err_str = "SMARTTHINGS_ERROR_NONE";
+ break;
+ case SMARTTHINGS_ERROR_INVALID_PARAMETER:
+ err_str = "SMARTTHINGS_ERROR_INVALID_PARAMETER";
+ break;
+ case SMARTTHINGS_ERROR_OUT_OF_MEMORY:
+ err_str = "SMARTTHINGS_ERROR_OUT_OF_MEMORY";
+ break;
+ case SMARTTHINGS_ERROR_PERMISSION_DENIED:
+ err_str = "SMARTTHINGS_ERROR_PERMISSION_DENIED";
+ break;
+ case SMARTTHINGS_ERROR_NO_DATA:
+ err_str = "SMARTTHINGS_ERROR_NO_DATA";
+ break;
+ case SMARTTHINGS_ERROR_NOT_SUPPORTED:
+ err_str = "SMARTTHINGS_ERROR_NOT_SUPPORTED";
+ break;
+ case SMARTTHINGS_ERROR_OPERATION_FAILED:
+ err_str = "SMARTTHINGS_ERROR_OPERATION_FAILED";
+ break;
+ case SMARTTHINGS_ERROR_SERVICE_UNAVAILABLE:
+ err_str = "SMARTTHINGS_ERROR_SERVICE_UNAVAILABLE";
+ break;
+ default:
+ err_str = "Unknown error";
+ break;
+ }
+
+ return err_str;
}
-static void handle_reset_result(bool result)
+static void _user_confirm_cb(smartthings_h handle, void *user_data)
{
- _D("Reset %s.\n", result ? "succeeded" : "failed");
+ if (smartthings_send_user_confirm(handle, true) != 0)
+ _E("smartthings_send_user_confirm() is failed");
}
-static bool handle_ownership_transfer_request(void)
+static void _reset_confirm_cb(smartthings_h handle, void *user_data)
{
- _D("Received a request for Ownership-transfer.");
- return true;
+ if (smartthings_send_reset_confirm(handle, true) != 0)
+ _E("smartthings_send_reset_confirm() is failed");
}
-static void handle_things_status_change(st_things_status_e things_status)
+static void _reset_result_cb(smartthings_h handle, bool result, void *user_data)
{
- _D("Things status is changed: %d\n", things_status);
+ _I("reset result = [%d]", result);
+}
- if (things_status == ST_THINGS_STATUS_REGISTERED_TO_CLOUD)
- ecore_main_loop_thread_safe_call_async(gathering_start, g_ad);
+static void
+_thing_status_cb(
+ smartthings_h handle, smartthings_status_e status, void *user_data)
+{
+ _D("status: [%d]", status);
}
-static bool handle_get_request(st_things_get_request_message_s* req_msg, st_things_representation_s* resp_rep)
+static void
+_things_connection_status_cb(smartthings_error_e error,
+ smartthings_h handle, smartthings_connection_status_e status,
+ void *user_data)
{
- bool ret = false;
+ _D("result [%s], status = [%d]", __master_error_to_str(error), status);
+
+ if (status == SMARTTHINGS_CONNECTION_STATUS_CONNECTED) {
+ int err = 0;
+ bool is_es_completed = false;
+ const char* dev_name = "motion-light-app";
+ int wifi_mode = SMARTTHINGS_WIFI_MODE_11B
+ | SMARTTHINGS_WIFI_MODE_11G
+ | SMARTTHINGS_WIFI_MODE_11N;
+
+ int wifi_freq = SMARTTHINGS_WIFI_FREQ_24G | SMARTTHINGS_WIFI_FREQ_5G;
+
+ err = smartthings_set_device_property(
+ handle, dev_name, wifi_mode, wifi_freq);
+ if (err) {
+ _E("smartthings_initialize() is failed, [%s]",
+ __master_error_to_str(err));
+ return;
+ }
+
+ err = smartthings_set_certificate_file(handle, CERT_FILE, PRIV_FILE);
+ if (err) {
+ _E("smartthings_set_certificate_file() is failed, [%s]",
+ __master_error_to_str(err));
+ return;
+ }
- _D("resource_uri [%s]", req_msg->resource_uri);
- retv_if(!g_ad, false);
+ err = smartthings_set_user_confirm_cb(handle, _user_confirm_cb, NULL);
+ if (err) {
+ _E("smartthings_set_user_confirm_cb() is failed, [%s]",
+ __master_error_to_str(err));
+ return;
+ }
- if (0 == strcmp(req_msg->resource_uri, SENSOR_MOTION_URI)) {
- _D("query : %s, property: %s", req_msg->query, req_msg->property_key);
+ err = smartthings_set_reset_confirm_cb(handle, _reset_confirm_cb, NULL);
+ if (err) {
+ _E("smartthings_set_reset_confirm_cb() is failed, [%s]",
+ __master_error_to_str(err));
+ return;
+ }
- // Call get request function for motion sensor
- ret = __handle_get_request_on_motion(req_msg, resp_rep);
- } else if (0 == strcmp(req_msg->resource_uri, SENSOR_LED_URI)) {
- _D("query : %s, property: %s", req_msg->query, req_msg->property_key);
+ err = smartthings_set_reset_result_cb(handle, _reset_result_cb, NULL);
+ if (err) {
+ _E("smartthings_set_reset_confirm_cb() is failed, [%s]",
+ __master_error_to_str(err));
+ return;
+ }
- // Call get request function for LED
- ret = __handle_get_request_on_led(req_msg, resp_rep);
- } else {
- _E("not supported uri");
- }
+ err = smartthings_set_status_changed_cb(handle, _thing_status_cb, NULL);
+ if (err) {
+ _E("smartthings_set_status_changed_callback() is failed, [%s]",
+ __master_error_to_str(err));
+ return;
+ }
- return ret;
-}
+ err = smartthings_start(handle);
+ if (err) {
+ _E("smartthings_start() is failed, [%s]",
+ __master_error_to_str(err));
+ return;
+ }
-static bool handle_set_request(st_things_set_request_message_s* req_msg, st_things_representation_s* resp_rep)
-{
- bool ret = false;
+ err = smartthings_get_easysetup_status(handle, &is_es_completed);
+ if (err) {
+ _E("smartthings_get_easysetup_status() is failed, [%s]",
+ __master_error_to_str(err));
+ return;
+ }
- _D("resource_uri [%s]", req_msg->resource_uri);
- retv_if(!g_ad, false);
+ if (is_es_completed == true) {
+ _I("Easysetup is already done");
+ return;
+ }
- if (0 == strcmp(req_msg->resource_uri, SENSOR_LED_URI)) {
- // Call set request function for LED
- ret = __handle_set_request_on_led(req_msg, resp_rep);
+ err = smartthings_start_easysetup(handle);
+ if (err) {
+ _E("smartthings_start_easysetup() is failed, [%s]",
+ __master_error_to_str(err));
+ smartthings_stop(handle);
+ return;
+ }
} else {
- _E("not supported uri");
+ _E("connection failed");
}
-
- return ret;
+ return;
}
-static int __things_init(void)
+static int st_thing_master_init(app_data *ad)
{
- 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;
- }
+ int err = 0;
+ smartthings_h st_handle = NULL;
- app_data_path = app_get_data_path();
- if (!app_data_path) {
- _E("app_data_path is NULL!!");
- free(app_res_path);
- return -1;
+ retv_if(!ad, -1);
+
+ if (ad->st_master_h) {
+ _I("Already initialized!");
+ return 0;
}
- // Specify the read-only and read-write 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);
+ err = smartthings_initialize(&st_handle, _things_connection_status_cb, NULL);
+ if (err) {
+ _E("smartthings_initialize() is failed, [%s]",
+ __master_error_to_str(err));
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);
+ ad->st_master_h = st_handle;
- // Specify the device configuration JSON file and change the status of easysetup_complete
- if (0 != st_things_initialize(app_json_path, &easysetup_complete)) {
- _E("st_things_initialize() failed!!");
- return -1;
+ return 0;
+}
+
+int st_thing_master_fini(app_data *ad)
+{
+ retv_if(!ad, -1);
+
+ if (!ad->st_master_h) {
+ _I("handle is already NULL");
+ return 0;
}
- _D("easysetup_complete:[%d] ", easysetup_complete);
+ smartthings_unset_user_confirm_cb(ad->st_master_h);
+ smartthings_unset_reset_confirm_cb(ad->st_master_h);
+ smartthings_unset_reset_result_cb(ad->st_master_h);
+ smartthings_unset_status_changed_cb(ad->st_master_h);
- // Register callback for handling request get (handle_get_request) and request set (handle_set_request) messages
- st_things_register_request_cb(handle_get_request, handle_set_request);
- // Register callback for reset confirmation (handle_reset_request) and reset result(handle_reset_result) functions
- st_things_register_reset_cb(handle_reset_request, handle_reset_result);
- // Register callback for getting user confirmation for ownership transfer (handle_ownership_transfer_request)
- st_things_register_user_confirm_cb(handle_ownership_transfer_request);
- // Register callback for getting notified when ST Things state changes (handle_things_status_change)
- st_things_register_things_status_change_cb(handle_things_status_change);
+ smartthings_stop_easysetup(ad->st_master_h);
+ smartthings_stop(ad->st_master_h);
+
+ if (smartthings_deinitialize(ad->st_master_h) != 0) {
+ _E("smartthings_deinitialize() is failed");
+ return -1;
+ }
+ ad->st_master_h = NULL;
return 0;
}
-static int __things_deinit(void)
+#endif /* USE_ST_SDK */
+
+static gboolean __change_motion_sensor_data(gpointer user_data)
{
- st_things_deinitialize();
- return 0;
+ int ret = 0;
+ uint32_t value = 0;
+
+ app_data *ad = user_data;
+
+ if (!ad) {
+ _E("failed to get app_data");
+ service_app_exit();
+ return FALSE;
+ }
+
+ if (!ad->motion_data) {
+ _E("failed to get motion_data");
+ service_app_exit();
+ ad->getter_motion = 0;
+ return FALSE;
+ }
+
+ ret = resource_read_infrared_motion_sensor(SENSOR_MOTION_CH, &value);
+ retv_if(ret != 0, TRUE);
+
+ sensor_data_set_bool(ad->motion_data, value);
+
+ _D("Detected motion value is: %d", value);
+
+#ifdef USE_ST_SDK
+ // Notify observers of the Motion sensor resource
+ st_thing_notify_resource(ad, SENSOR_MOTION_URI, SENSOR_MOTION_KEY, ad->motion_data);
+#endif
+
+ return TRUE;
}
-static int __things_start(void)
+//static int __change_led_data(void *data, char *state) {
+// int ret = 0;
+// app_data *ad = data;
+//
+// retv_if(!ad, -1);
+// retv_if(!ad->led_data, -1);
+//
+// sensor_data_set_string(ad->led_data, state, strlen(state));
+//
+// if (0 == strcmp(state, LED_ON)) {
+// ret = resource_write_led(SENSOR_LED_CH, 1);
+// } else {
+// ret = resource_write_led(SENSOR_LED_CH, 0);
+// }
+//
+// retv_if(ret != 0, -1);
+//
+//#ifdef USE_ST_SDK
+// // Notify observers of the LED resource
+// st_thing_notify_resource(ad, SENSOR_LED_URI, SENSOR_LED_KEY, ad->motion_data);
+//#endif
+//
+// return 0;
+//}
+
+static void gathering_stop(void *data)
{
- st_things_start();
- return 0;
+ app_data *ad = data;
+ ret_if(!ad);
+
+ if (ad->getter_motion) {
+ g_source_remove(ad->getter_motion);
+ ad->getter_motion = 0;
+ }
}
-static int __things_stop(void)
+static void gathering_start(void *data)
{
- st_things_stop();
- return 0;
+ app_data *ad = data;
+ ret_if(!ad);
+ ad->getter_motion = g_timeout_add(SENSOR_GATHER_INTERVAL, __change_motion_sensor_data, ad);
+ if (!ad->getter_motion)
+ _E("Failed to add getter_motion");
}
static bool service_app_create(void *user_data)
@@ -330,33 +667,38 @@ static bool service_app_create(void *user_data)
if (!ad->led_data)
return false;
- sensor_data_set_string(g_ad->led_data, SENSOR_LED_INIT, strlen(SENSOR_LED_INIT));
+ sensor_data_set_string(ad->led_data, SENSOR_LED_INIT, strlen(SENSOR_LED_INIT));
- if (__things_init())
+#ifdef USE_ST_SDK
+ if (st_thing_master_init(ad))
return false;
+ if (st_thing_resource_init(ad)) {
+ st_thing_master_fini(ad);
+ return false;
+ }
+#endif
+
return true;
}
static void service_app_control(app_control_h app_control, void *user_data)
{
- __things_start();
+ gathering_stop(user_data);
+ gathering_start(user_data);
}
static void service_app_terminate(void *user_data)
{
app_data *ad = (app_data *)user_data;
- // Delete ecore timer
- if (ad->getter_motion)
- ecore_timer_del(ad->getter_motion);
-
- // Stop and deinitialize things
- __things_stop();
- __things_deinit();
+#ifdef USE_ST_SDK
+ st_thing_resource_fini(ad);
+ st_thing_master_fini(ad);
+#endif
// Turn off LED light with __set_led()
- __change_led_data(ad, LED_OFF);
+// __change_led_data(ad, LED_OFF);
// Free sensor Motion & LED data
sensor_data_free(ad->motion_data);
@@ -388,4 +730,3 @@ int main(int argc, char *argv[])
return service_app_main(argc, argv, &event_callback, ad);
}
-