summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoyeon <boyeon.son@samsung.com>2019-08-07 20:14:59 +0900
committerBoyeon <boyeon.son@samsung.com>2019-08-13 13:35:10 +0900
commit19cc6f0a2e32b77506aaf102b7412d68bef01cec (patch)
tree4b9b2a6c3785493b781a9ca2dcb8a9a22b45d5d2
parent9571c753b3c1f227a2d7a8ee0a77196c55732c23 (diff)
downloadst-things-light-19cc6f0a2e32b77506aaf102b7412d68bef01cec.tar.gz
st-things-light-19cc6f0a2e32b77506aaf102b7412d68bef01cec.tar.bz2
st-things-light-19cc6f0a2e32b77506aaf102b7412d68bef01cec.zip
Add a new branch for basic app (without ST)
Change-Id: I9fbd5c256fa967b3e0b01d53c771f2558848bfb4
-rw-r--r--.gitignore1
-rw-r--r--inc/log.h2
-rwxr-xr-xsrc/controller.c542
3 files changed, 14 insertions, 531 deletions
diff --git a/.gitignore b/.gitignore
index 61124d7..5f489ce 100644
--- a/.gitignore
+++ b/.gitignore
@@ -5,3 +5,4 @@
/res/privatekey.der
/crash-info/
/.package-stamp
+/res/
diff --git a/inc/log.h b/inc/log.h
index eb89231..e9e2b41 100644
--- a/inc/log.h
+++ b/inc/log.h
@@ -27,7 +27,7 @@ extern "C" {
#ifdef LOG_TAG
#undef LOG_TAG
#endif
-#define LOG_TAG "MOTION_APP"
+#define LOG_TAG "SMART_LIGHT"
#define _E(fmt, args...) dlog_print(DLOG_ERROR, LOG_TAG, "%s : %s(%d) > "fmt"\n", rindex(__FILE__, '/') + 1, __func__, __LINE__, ##args)
#define _W(fmt, args...) dlog_print(DLOG_WARN, LOG_TAG, "%s : %s(%d) > "fmt"\n", rindex(__FILE__, '/') + 1, __func__, __LINE__, ##args)
diff --git a/src/controller.c b/src/controller.c
index b8d8985..ba706bf 100755
--- a/src/controller.c
+++ b/src/controller.c
@@ -26,85 +26,30 @@
#include "resource/resource_led.h"
// Timer duration
-#define TIMER_GATHER_INTERVAL (5.0f)
+#define TIMER_GATHER_INTERVAL (2.0f)
// Motion sensor information
-#define SENSOR_MOTION_GPIO_NUMBER (46)
+#define SENSOR_MOTION_GPIO_NUMBER (18)
// LED sensor information
-#define SENSOR_LED_GPIO_NUMBER (130)
-#define SENSOR_LED_ON "on"
-#define SENSOR_LED_OFF "off"
-
-// For using SmartThings SDK
-#define USE_ST_SDK
-#ifdef USE_ST_SDK
-#include "smartthings.h"
-#include "smartthings_resource.h"
-#include "smartthings_payload.h"
-
-// Certification file and private key file stored in the resource directory
-#define CERT_FILE "certificate.pem"
-#define PRIV_FILE "privatekey.der"
-
-// URI and key information
-#define SENSOR_MOTION_URI "/capability/motionSensor/main/0"
-#define SENSOR_MOTION_KEY "value"
-#define SENSOR_LED_URI "/capability/switch/main/0"
-#define SENSOR_LED_KEY "power"
-#endif /* USE_ST_SDK */
+#define SENSOR_LED_GPIO_NUMBER (24)
typedef struct app_data_s {
Ecore_Timer *getter_timer;
- uint32_t motion_data;
- int led_data;
-#ifdef USE_ST_SDK
- smartthings_h st_master_h;
- smartthings_resource_h st_res_h;
- smartthings_status_e status;
-#endif /* USE_ST_SDK */
} app_data;
-static app_data *g_ad = NULL;
-
-#ifdef USE_ST_SDK
-static const char * _resource_error_to_str(smartthings_resource_error_e error)
-{
- const char *err_str = NULL;
+static int _set_led_data(app_data *ad, int state) {
+ int ret = 0;
- 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;
+ ret = resource_write_led(SENSOR_LED_GPIO_NUMBER, state);
+ if (ret != 0) {
+ _E("cannot write led data");
+ return -1;
}
+ _I("LED : %d",state);
- return err_str;
+ return 0;
}
-#endif
static Eina_Bool _get_motion_sensor_data(void *user_data)
{
@@ -122,472 +67,16 @@ static Eina_Bool _get_motion_sensor_data(void *user_data)
_E("cannot read data from the infrared motion sensor");
return ECORE_CALLBACK_CANCEL;
}
- ad->motion_data = value;
_D("Detected motion value is: %u", value);
-#ifdef USE_ST_SDK
- int error = SMARTTHINGS_RESOURCE_ERROR_NONE;
- smartthings_payload_h resp_payload = NULL;
-
- if (ad->status != SMARTTHINGS_STATUS_REGISTERED_TO_CLOUD)
- return ECORE_CALLBACK_RENEW;
-
- error = smartthings_payload_create(&resp_payload);
- if (error != SMARTTHINGS_RESOURCE_ERROR_NONE || !resp_payload) {
- _E("smartthings_payload_create() failed, [%s]",
- _resource_error_to_str(error));
- return ECORE_CALLBACK_CANCEL;
- }
-
- error = smartthings_payload_set_bool(resp_payload, SENSOR_MOTION_KEY, (bool)ad->motion_data);
- if (error != SMARTTHINGS_RESOURCE_ERROR_NONE)
- _E("smartthings_payload_set_bool() failed, [%s]",
- _resource_error_to_str(error));
-
- error = smartthings_resource_notify(ad->st_res_h, SENSOR_MOTION_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");
-#endif
+ _set_led_data(ad, value);
return ECORE_CALLBACK_RENEW;
}
-static int _set_led_data(app_data *ad, int state) {
- int ret = 0;
-
- ad->led_data = state;
- ret = resource_write_led(SENSOR_LED_GPIO_NUMBER, state);
- if (ret != 0) {
- _E("cannot write led data");
- return -1;
- }
- _I("LED : %d",state);
-
- return 0;
-}
-
-#ifdef USE_ST_SDK
-/* SmartThings resource functions */
-static bool _handle_get_motion(smartthings_payload_h resp_payload, void *user_data)
-{
- int error = SMARTTHINGS_RESOURCE_ERROR_NONE;
- app_data *ad = user_data;
-
- retv_if(!ad, false);
-
- _D("Received a GET request for MOTION");
-
- error = smartthings_payload_set_bool(resp_payload, SENSOR_MOTION_KEY, (bool)ad->motion_data);
- if (error != SMARTTHINGS_RESOURCE_ERROR_NONE)
- _E("smartthings_payload_set_bool() failed, [%s]",
- _resource_error_to_str(error));
-
- return true;
-}
-
-static bool _handle_get_led(smartthings_payload_h resp_payload, void *user_data)
-{
- app_data *ad = user_data;
- int error = SMARTTHINGS_RESOURCE_ERROR_NONE;
- char *str = NULL;
-
- retv_if(!ad, false);
-
- _D("Received a GET request for LED");
-
- if (ad->led_data)
- str = SENSOR_LED_ON;
- else
- str = SENSOR_LED_OFF;
-
- error = smartthings_payload_set_string(resp_payload, SENSOR_LED_KEY, str);
- if (error != SMARTTHINGS_RESOURCE_ERROR_NONE)
- _E("smartthings_payload_set_string() failed, [%s]",
- _resource_error_to_str(error));
-
- _D("Power : %s", str);
-
- return true;
-}
-
-static bool _handle_set_led(smartthings_payload_h payload, smartthings_payload_h resp_payload, void *user_data)
-{
- app_data *ad = user_data;
- char *str = NULL;
- char *res_str = NULL;
- int ret = 0;
- int error = SMARTTHINGS_RESOURCE_ERROR_NONE;
-
- retv_if(!ad, false);
-
- _D("Received a SET request");
-
- error = smartthings_payload_get_string(payload, SENSOR_LED_KEY, &str);
- if (error != SMARTTHINGS_RESOURCE_ERROR_NONE)
- _E("smartthings_payload_get_string() failed, [%s]",
- _resource_error_to_str(error));
-
- if (strncmp(str, SENSOR_LED_ON, strlen(SENSOR_LED_ON))) {
- ret = _set_led_data(ad, 0);
- res_str = SENSOR_LED_OFF;
- } else {
- ret = _set_led_data(ad, 1);
- res_str = SENSOR_LED_ON;
- }
-
- free(str);
- if (ret != 0) {
- _E("cannot set LED");
- return false;
- }
-
- error = smartthings_payload_set_string(resp_payload, SENSOR_LED_KEY, res_str);
- if (error != SMARTTHINGS_RESOURCE_ERROR_NONE)
- _E("smartthings_payload_set_string() failed, [%s]",
- _resource_error_to_str(error));
-
- return true;
-}
-
-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;
-
- _D("request on %s, type[%d], id[%d]", uri, req_type, req_id);
-
- error = smartthings_payload_create(&resp_payload);
- if (error != SMARTTHINGS_RESOURCE_ERROR_NONE || ! resp_payload)
- _E("smartthings_payload_create() failed, [%s]",
- _resource_error_to_str(error));
-
- if (req_type == SMARTTHINGS_RESOURCE_REQUEST_GET) {
- if (!strncmp(uri, SENSOR_MOTION_URI, strlen(SENSOR_MOTION_URI)))
- result = _handle_get_motion(resp_payload, user_data);
- else if (!strncmp(uri, SENSOR_LED_URI, strlen(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 (!strncmp(uri, SENSOR_LED_URI, strlen(SENSOR_LED_URI)))
- result = _handle_set_led(payload, resp_payload, user_data);
- else
- _E("No matching Resource uri to get");
- } else {
- _E("Invalid request type - %d", req_type);
- smartthings_payload_destroy(resp_payload);
- return;
- }
-
- error = smartthings_resource_send_response(handle, req_id, uri, resp_payload, result);
- if (error != SMARTTHINGS_RESOURCE_ERROR_NONE) {
- _E("smartthings_resource_send_response() failed, [%s]",
- _resource_error_to_str(error));
- smartthings_payload_destroy(resp_payload);
- 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 void _resource_connection_status_cb(
- smartthings_resource_h handle,
- smartthings_resource_connection_status_e status, void *user_data)
-{
- app_data *ad = user_data;
-
- _D("status=[%d]", status);
-
- ret_if(!ad);
-
- if (status == SMARTTHINGS_RESOURCE_CONNECTION_STATUS_CONNECTED) {
- int error = SMARTTHINGS_RESOURCE_ERROR_NONE;
- error = smartthings_resource_set_request_cb(handle, _request_cb, ad);
- if (error != SMARTTHINGS_RESOURCE_ERROR_NONE) {
- _E("smartthings_resource_set_request_cb() is failed");
- return;
- }
- } else {
- _E("connection failed");
- }
-}
-
-static int _init_resource(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;
- }
-
- ad->st_res_h = st_res_h;
-
- return 0;
-}
-
-static int _fini_resource(app_data *ad)
-{
- retv_if(!ad, -1);
-
- if (!ad->st_res_h)
- return 0;
-
- smartthings_resource_unset_request_cb(ad->st_res_h);
- smartthings_resource_deinitialize(ad->st_res_h);
-
- ad->st_res_h = NULL;
-
- return 0;
-}
-
-/* SmartThings master functions */
-static const char *__master_error_to_str(smartthings_error_e error)
-{
- 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 _user_confirm_cb(smartthings_h handle, void *user_data)
-{
- if (smartthings_send_user_confirm(handle, true) != 0)
- _E("smartthings_send_user_confirm() is failed");
-}
-
-static void _reset_confirm_cb(smartthings_h handle, void *user_data)
-{
- if (smartthings_send_reset_confirm(handle, true) != 0)
- _E("smartthings_send_reset_confirm() is failed");
-}
-
-static void _reset_result_cb(smartthings_h handle, bool result, void *user_data)
-{
- _I("reset result = [%d]", result);
-}
-
-static void _thing_status_cb(
- smartthings_h handle, smartthings_status_e status, void *user_data)
-{
- app_data *ad = user_data;
- _D("status: [%d]", status);
- ad->status = status;
-}
-
-static void _things_connection_status_cb(
- smartthings_h handle, smartthings_connection_status_e status,
- void *user_data)
-{
- _D("status = [%d]", 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_set_device_property() 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;
- }
-
- 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;
- }
-
- 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;
- }
-
- err = smartthings_set_reset_result_cb(handle, _reset_result_cb, NULL);
- if (err) {
- _E("smartthings_set_reset_result_cb() is failed, [%s]",
- __master_error_to_str(err));
- return;
- }
-
- err = smartthings_set_status_changed_cb(handle, _thing_status_cb, user_data);
- if (err) {
- _E("smartthings_set_status_changed_callback() is failed, [%s]",
- __master_error_to_str(err));
- return;
- }
-
- err = smartthings_start(handle);
- if (err) {
- _E("smartthings_start() is failed, [%s]",
- __master_error_to_str(err));
- return;
- }
-
- 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;
- }
-
- if (is_es_completed == true) {
- _I("Easysetup is already done");
- return;
- }
-
- _I("Easysetup is starting now");
- 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("connection failed");
- }
-}
-
-static int _init_master(app_data *ad)
-{
- int err = 0;
- smartthings_h st_handle = NULL;
-
- retv_if(!ad, -1);
-
- if (ad->st_master_h) {
- _I("Already initialized!");
- return 0;
- }
-
- err = smartthings_initialize(&st_handle, _things_connection_status_cb, ad);
- if (err) {
- _E("smartthings_initialize() is failed, [%s]",
- __master_error_to_str(err));
- return -1;
- }
-
- ad->st_master_h = st_handle;
-
- return 0;
-}
-
-int _fini_master(app_data *ad)
-{
- retv_if(!ad, -1);
-
- if (!ad->st_master_h) {
- _I("handle is already NULL");
- return 0;
- }
-
- 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);
-
- 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;
-}
-#endif /* USE_ST_SDK */
-
static bool service_app_create(void *user_data)
{
-#ifdef USE_ST_SDK
- app_data *ad = user_data;
-
- if (_init_master(ad))
- return false;
-
- if (_init_resource(ad)) {
- _fini_master(ad);
- return false;
- }
-#endif
-
return true;
}
@@ -609,11 +98,6 @@ static void service_app_terminate(void *user_data)
{
app_data *ad = user_data;
-#ifdef USE_ST_SDK
- _fini_resource(ad);
- _fini_master(ad);
-#endif
-
// Turn off LED light with __set_led()
_set_led_data(ad, 0);
@@ -635,8 +119,6 @@ int main(int argc, char *argv[])
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;