diff options
Diffstat (limited to 'src/illuminance_sensor.c')
-rw-r--r-- | src/illuminance_sensor.c | 528 |
1 files changed, 363 insertions, 165 deletions
diff --git a/src/illuminance_sensor.c b/src/illuminance_sensor.c index 93dd58a..f76d2fd 100644 --- a/src/illuminance_sensor.c +++ b/src/illuminance_sensor.c @@ -14,274 +14,472 @@ * limitations under the License. */ +#include <tizen.h> #include <service_app.h> #include <stdio.h> -#include "st_things.h" -#include "log.h" +#include <string.h> +#include <stdlib.h> #include <Ecore.h> -#define _DEBUG_PRINT_ -#ifdef _DEBUG_PRINT_ -#include <sys/time.h> -#endif +#include "log.h" -#define SENSOR_EVENT_INTERVAL (0.250f) // sensor event timer : 250ms interval -#define JSON_PATH "device_def.json" -#define MUTEX_LOCK pthread_mutex_lock(&mutex) -#define MUTEX_UNLOCK pthread_mutex_unlock(&mutex) +#include "smartthings.h" +#include "smartthings_resource.h" +#include "smartthings_payload.h" +// Duration for a timer +#define TIMER_SENSOR_INTERVAL (.250f) Ecore_Timer *sensor_event_timer = NULL; -pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; -static const char *RES_CAPABILITY_ILLUMINANCEMEASUREMENT_MAIN_0 = "/capability/illuminanceMeasurement/main/0"; -static const char *RES_CAPABILITY_SWITCH_MAIN_0 = "/capability/switch/main/0"; -static int g_lux_value = 0; -static bool g_switch_status; +#define THING_RESOURCE_FILE_NAME "resource.json" -/* resource handler */ -extern int resource_open_i2c_handle(void); -extern int resource_close_i2c_handle(void); -extern int resource_read_illuminance_sensor(uint16_t *out_value); +static smartthings_resource_h st_handle = NULL; +static bool is_init = false; +static bool is_resource_init = false; +static smartthings_h st_h; + +smartthings_status_e st_things_status = -1; + +static const char *RES_CAPABILITY_SWITCH_MAIN_0 = "/capability/switch/main/0"; +static const char *RES_CAPABILITY_ILLUMINANCEMEASUREMENT_MAIN_0 = "/capability/illuminanceMeasurement/main/0"; +static const char *PROP_ILLUMINANCE = "illuminance"; /* get and set request handlers */ -extern bool handle_get_request_on_resource_capability_illuminancemeasurement(st_things_get_request_message_s* req_msg, st_things_representation_s* resp_rep); -extern bool handle_get_request_on_resource_capability_switch(st_things_get_request_message_s* req_msg, st_things_representation_s* resp_rep); -extern bool handle_set_request_on_resource_capability_switch(st_things_set_request_message_s* req_msg, st_things_representation_s* resp_rep); +extern bool handle_get_request_on_resource_capability_switch_main_0(smartthings_payload_h resp_payload, void *user_data); +extern bool handle_set_request_on_resource_capability_switch_main_0(smartthings_payload_h payload, smartthings_payload_h resp_payload, void *user_data); +extern bool handle_get_request_on_resource_capability_illuminancemeasurement_main_0(smartthings_payload_h resp_payload, void *user_data); + +extern int resource_read_illuminance_sensor(uint16_t *out_value); +extern int resource_close_illuminance_sensor(void); -bool get_switch_status(void) +static Eina_Bool _get_sensor_data(void *user_data) { - bool status = false; + int ret = 0; + uint16_t value = 0; - MUTEX_LOCK; - status = g_switch_status; - MUTEX_UNLOCK; + int error = SMARTTHINGS_RESOURCE_ERROR_NONE; + smartthings_payload_h resp_payload = NULL; - return status; -} + if (st_things_status != SMARTTHINGS_STATUS_REGISTERED_TO_CLOUD) + return ECORE_CALLBACK_RENEW; -void set_switch_status(bool status) -{ - MUTEX_LOCK; - g_switch_status = status; - MUTEX_UNLOCK; + if (!st_handle) { + _E("st_handle is NULL"); + return ECORE_CALLBACK_RENEW; + } -} + ret = resource_read_illuminance_sensor(&value); + if (ret != 0) { + _E("cannot read data from the sensor"); + return ECORE_CALLBACK_CANCEL; + } -uint16_t get_sensor_value(void) -{ - uint16_t value = 0; + _D("Detected sensor value is: %u", value); - MUTEX_LOCK; - value = g_lux_value; - MUTEX_UNLOCK; + error = smartthings_payload_create(&resp_payload); + if (error != SMARTTHINGS_RESOURCE_ERROR_NONE || !resp_payload) { + _E("smartthings_payload_create() failed, [%d]", error); + return ECORE_CALLBACK_CANCEL; + } - return value; + error = smartthings_payload_set_int(resp_payload, PROP_ILLUMINANCE, (int)value); + if (error != SMARTTHINGS_RESOURCE_ERROR_NONE) + _E("smartthings_payload_set_int() failed, [%d]", error); + + error = smartthings_resource_notify(st_handle, RES_CAPABILITY_ILLUMINANCEMEASUREMENT_MAIN_0, resp_payload); + if (error != SMARTTHINGS_RESOURCE_ERROR_NONE) + _E("smartthings_resource_notify() failed, [%d]", error); + + if (smartthings_payload_destroy(resp_payload)) + _E("smartthings_payload_destroy() failed"); + + return ECORE_CALLBACK_RENEW; } -static void set_sensor_value(uint16_t value) +void _send_response_result_cb(smartthings_resource_error_e result, void *user_data) { - MUTEX_LOCK; - g_lux_value = value; - MUTEX_UNLOCK; + _D("app_control reply callback for send_response : result=[%d]", result); } -static Eina_Bool _sensor_interval_event_cb(void *data) +void _notify_result_cb(smartthings_resource_error_e result, void *user_data) { - uint16_t sensor_value; - int ret = 0; - bool switch_status = false; + _D("app_control reply callback for notify : result=[%d]", result); +} - // read sensor value in lux - ret = resource_read_illuminance_sensor(&sensor_value); - if (ret != 0) { - ERR("Got invalid sensor value"); - // cancle periodic event timer operation - return ECORE_CALLBACK_CANCEL; - } else { - // save sensor value to global variable - set_sensor_value(sensor_value); - - #ifdef _DEBUG_PRINT_ - struct timeval __tv; - gettimeofday(&__tv, NULL); - INFO("[%d.%06d] Lux=[%d]Lux", __tv.tv_sec, __tv.tv_usec, sensor_value); - #endif - - // send notification when switch is on state. - switch_status = get_switch_status(); - if (switch_status) { - // send notification to cloud server - st_things_notify_observers(RES_CAPABILITY_ILLUMINANCEMEASUREMENT_MAIN_0); +void _request_cb(smartthings_resource_h st_h, int req_id, const char *uri, smartthings_resource_req_type_e req_type, + smartthings_payload_h payload, void *user_data) +{ + START; + + smartthings_payload_h resp_payload = NULL; + + smartthings_payload_create(&resp_payload); + if (!resp_payload) { + _E("Response payload is NULL"); + return; + } + + bool result = false; + + if (req_type == SMARTTHINGS_RESOURCE_REQUEST_GET) { + if (0 == strncmp(uri, RES_CAPABILITY_SWITCH_MAIN_0, strlen(RES_CAPABILITY_SWITCH_MAIN_0))) { + result = handle_get_request_on_resource_capability_switch_main_0(resp_payload, user_data); + } + if (0 == strncmp(uri, RES_CAPABILITY_ILLUMINANCEMEASUREMENT_MAIN_0, strlen(RES_CAPABILITY_ILLUMINANCEMEASUREMENT_MAIN_0))) { + result = handle_get_request_on_resource_capability_illuminancemeasurement_main_0(resp_payload, user_data); } + } else if (req_type == SMARTTHINGS_RESOURCE_REQUEST_SET) { + if (0 == strncmp(uri, RES_CAPABILITY_SWITCH_MAIN_0, strlen(RES_CAPABILITY_SWITCH_MAIN_0))) { + result = handle_set_request_on_resource_capability_switch_main_0(payload, resp_payload, user_data); + } + } else { + _E("Invalid request type"); + smartthings_payload_destroy(resp_payload); + END; + return; } - // reset event timer periodic - return ECORE_CALLBACK_RENEW; + + int error = SMARTTHINGS_RESOURCE_ERROR_NONE; + + error = smartthings_resource_send_response(st_h, req_id, uri, resp_payload, result); + if (error != SMARTTHINGS_RESOURCE_ERROR_NONE) { + smartthings_payload_destroy(resp_payload); + _E("smartthings_resource_send_response() failed, err=[%d]", error); + END; + return; + } + + if (req_type == SMARTTHINGS_RESOURCE_REQUEST_SET) { + error = smartthings_resource_notify(st_h, uri, resp_payload); + if (error != SMARTTHINGS_RESOURCE_ERROR_NONE) { + _E("smartthings_resource_notify() failed, err=[%d]", error); + } + } + + if (smartthings_payload_destroy(resp_payload) != 0) { + _E("smartthings_payload_destroy failed"); + } + + END; + return; } -/* handle : for getting request on resources */ -bool handle_get_request(st_things_get_request_message_s* req_msg, st_things_representation_s* resp_rep) +static void _resource_connection_status_cb(smartthings_resource_h handle, smartthings_resource_connection_status_e status, void *user_data) { - //DBG("resource_uri [%s]", req_msg->resource_uri); + START; - if (0 == strcmp(req_msg->resource_uri, RES_CAPABILITY_ILLUMINANCEMEASUREMENT_MAIN_0)) { - return handle_get_request_on_resource_capability_illuminancemeasurement(req_msg, resp_rep); - } - if (0 == strcmp(req_msg->resource_uri, RES_CAPABILITY_SWITCH_MAIN_0)) { - return handle_get_request_on_resource_capability_switch(req_msg, resp_rep); + if (status == SMARTTHINGS_RESOURCE_CONNECTION_STATUS_CONNECTED) { + if (smartthings_resource_set_request_cb(st_handle, _request_cb, NULL) != 0) { + _E("smartthings_resource_set_request_cb() is failed"); + return; + } + } else { + _I("connection failed, status=[%d]", status); } - ERR("not supported uri"); - return false; + END; + return; } -/* handle : for setting request on resources */ -bool handle_set_request(st_things_set_request_message_s* req_msg, st_things_representation_s* resp_rep) +int init_resource_app() { - //DBG("resource_uri [%s]", req_msg->resource_uri); + START; + + if (is_resource_init) { + _I("Already initialized!"); + return 0; + } - if (0 == strcmp(req_msg->resource_uri, RES_CAPABILITY_SWITCH_MAIN_0)) { - return handle_set_request_on_resource_capability_switch(req_msg, resp_rep); + if (smartthings_resource_initialize(&st_handle, _resource_connection_status_cb, NULL) != 0) { + _E("smartthings_resource_initialize() is failed"); + goto _out; } - ERR("not supported uri"); - return false; + is_resource_init = true; + + END; + return 0; + +_out : + END; + return -1; } -/* callback functions */ -bool handle_reset_request(void) +int deinit_resource_app() { - DBG("Received a request for RESET."); - return true; + START; + + if (!st_handle) + return 0; + + smartthings_resource_unset_request_cb(st_handle); + + if (smartthings_resource_deinitialize(st_handle) != 0) + return -1; + + is_resource_init = false; + + END; + return 0; } -void handle_reset_result(bool result) + +void _user_confirm_cb(smartthings_h handle, void *user_data) { - DBG("Reset %s.\n", result ? "succeeded" : "failed"); + START; + + if (smartthings_send_user_confirm(handle, true) != 0) + _E("smartthings_send_user_confirm() is failed"); + + END; + return; } -bool handle_ownership_transfer_request(void) +void _reset_confirm_cb(smartthings_h handle, void *user_data) { - DBG("Received a request for Ownership-transfer."); - return true; + START; + + if (smartthings_send_reset_confirm(handle, true) != 0) + _E("smartthings_send_reset_confirm() is failed"); + + END; + return; } -void handle_things_status_change(st_things_status_e things_status) +static void _reset_result_cb(smartthings_h handle, bool result, void *user_data) { - DBG("Things status is changed: %d\n", things_status); + START; + + _I("reset result = [%d]", result); + + END; + return; } -bool init_user() +static void _thing_status_cb(smartthings_h handle, smartthings_status_e status, void *user_data) { - // initialize mutex - pthread_mutex_init(&mutex, NULL); - - // create timer event function - sensor_event_timer = ecore_timer_add(SENSOR_EVENT_INTERVAL, _sensor_interval_event_cb, NULL); - if (!sensor_event_timer) { - ERR("Failed to add sensor_event_timer"); - return false; + START; + + _D("Received status changed cb : status = [%d]", status); + st_things_status = status; + + switch (status) { + case SMARTTHINGS_STATUS_NOT_READY: + _I("status: [%d] [%s]", status, "SMARTTHINGS_STATUS_NOT_READY"); + break; + case SMARTTHINGS_STATUS_INIT: + _I("status: [%d] [%s]", status, "SMARTTHINGS_STATUS_INIT"); + break; + case SMARTTHINGS_STATUS_ES_STARTED: + _I("status: [%d] [%s]", status, "SMARTTHINGS_STATUS_ES_STARTED"); + break; + case SMARTTHINGS_STATUS_ES_DONE: + _I("status: [%d] [%s]", status, "SMARTTHINGS_STATUS_ES_DONE"); + break; + case SMARTTHINGS_STATUS_ES_FAILED_ON_OWNERSHIP_TRANSFER: + _I("status: [%d] [%s]", status, "SMARTTHINGS_STATUS_ES_FAILED_ON_OWNERSHIP_TRANSFER"); + break; + case SMARTTHINGS_STATUS_CONNECTING_TO_AP: + _I("status: [%d] [%s]", status, "SMARTTHINGS_STATUS_CONNECTING_TO_AP"); + break; + case SMARTTHINGS_STATUS_CONNECTED_TO_AP: + _I("status: [%d] [%s]", status, "SMARTTHINGS_STATUS_CONNECTED_TO_AP"); + break; + case SMARTTHINGS_STATUS_CONNECTING_TO_AP_FAILED: + _I("status: [%d] [%s]", status, "SMARTTHINGS_STATUS_CONNECTING_TO_AP_FAILED"); + break; + case SMARTTHINGS_STATUS_REGISTERING_TO_CLOUD: + _I("status: [%d] [%s]", status, "SMARTTHINGS_STATUS_REGISTERING_TO_CLOUD"); + break; + case SMARTTHINGS_STATUS_REGISTERED_TO_CLOUD: + _I("status: [%d] [%s]", status, "SMARTTHINGS_STATUS_REGISTERED_TO_CLOUD"); + break; + case SMARTTHINGS_STATUS_REGISTERING_FAILED_ON_SIGN_IN: + _I("status: [%d] [%s]", status, "SMARTTHINGS_STATUS_REGISTERING_FAILED_ON_SIGN_IN"); + break; + case SMARTTHINGS_STATUS_REGISTERING_FAILED_ON_PUB_RES: + _I("status: [%d] [%s]", status, "SMARTTHINGS_STATUS_REGISTERING_FAILED_ON_PUB_RES"); + break; + default: + _E("status: [%d][%s]", status, "Unknown Status"); + break; } - - return true; + END; + return; } -/* initialize */ -void init_thing() +static void _things_connection_status_cb(smartthings_h handle, smartthings_connection_status_e status, void *user_data) { - FN_CALL; - static bool binitialized = false; - if (binitialized) { - DBG("Already initialized!!"); - return; + START; + + _D("Received connection status changed cb : status = [%d]", status); + + if (status == SMARTTHINGS_CONNECTION_STATUS_CONNECTED) { + const char* dev_name = "IoT Test Device"; + 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; + + if (smartthings_set_device_property(handle, dev_name, wifi_mode, wifi_freq) != 0) { + _E("smartthings_initialize() is failed"); + return; + } + + if (smartthings_set_certificate_file(handle, "certificate.pem", "privatekey.der") != 0) { + _E("smartthings_set_certificate_file() is failed"); + return; + } + + if (smartthings_set_user_confirm_cb(st_h, _user_confirm_cb, NULL) != 0) { + _E("smartthings_set_user_confirm_cb() is failed"); + return; + } + + if (smartthings_set_reset_confirm_cb(handle, _reset_confirm_cb, NULL) != 0) { + _E("smartthings_set_reset_confirm_cb() is failed"); + return; + } + + if (smartthings_set_reset_result_cb(handle, _reset_result_cb, NULL) != 0) { + _E("smartthings_set_reset_confirm_cb() is failed"); + return; + } + + if (smartthings_set_status_changed_cb(handle, _thing_status_cb, NULL) != 0) { + _E("smartthings_set_status_changed_callback() is failed"); + return; + } + + if (smartthings_start(handle) != 0) { + _E("smartthings_start() is failed"); + return; + } + + bool is_es_completed = false; + if (smartthings_get_easysetup_status(handle, &is_es_completed) != 0) { + _E("smartthings_get_easysetup_status() is failed"); + return; + } + + if (is_es_completed == true) { + _I("Easysetup is already done"); + return; + } + + if (smartthings_start_easysetup(handle) != 0) { + _E("smartthings_start_easysetup() is failed"); + return; + } + } else { + _E("connection failed, status=[%d]", status); } - bool easysetup_complete = false; + END; + return; +} - char app_json_path[128] = {0,}; - char *app_res_path = NULL; - char *app_data_path = NULL; +int init_master_app() +{ + START; - app_res_path = app_get_resource_path(); - if (!app_res_path) { - ERR("app_res_path is NULL!!"); - return; + if (is_init) { + _I("Already initialized!"); + END; + return 0; } - app_data_path = app_get_data_path(); - if (!app_data_path) { - ERR("app_data_path is NULL!!"); - free(app_res_path); - return; + if (smartthings_initialize(&st_h, _things_connection_status_cb, NULL) != 0) { + _E("smartthings_initialize() is failed"); + goto _out; } - snprintf(app_json_path, sizeof(app_json_path), "%s/%s", app_res_path, JSON_PATH); + is_init = true; - if (0 != st_things_set_configuration_prefix_path((const char *)app_res_path, (const char *)app_data_path)) { - ERR("st_things_set_configuration_prefix_path() failed!!"); - free(app_res_path); - free(app_data_path); - return; - } + END; + return 0; - free(app_res_path); - free(app_data_path); +_out : + END; + return -1; +} - if (0 != st_things_initialize(app_json_path, &easysetup_complete)) { - ERR("st_things_initialize() failed!!"); - return; - } +int deinit_master_app() +{ + START; - binitialized = true; - init_user(); + is_init = false; - DBG("easysetup_complete:[%d] ", easysetup_complete); + if (!st_h) { + _I("handle is already NULL"); + END; + return 0; + } - 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); + smartthings_unset_user_confirm_cb(st_h); + smartthings_unset_reset_confirm_cb(st_h); + smartthings_unset_reset_result_cb(st_h); + smartthings_unset_status_changed_cb(st_h); - st_things_start(); + if (smartthings_deinitialize(st_h) != 0) { + _E("smartthings_deinitialize() is failed"); + END; + return -1; + } - FN_END; + END; + return 0; } static bool service_app_create(void *user_data) { - INFO("open I2C handle"); - resource_open_i2c_handle(); + if (sensor_event_timer) + ecore_timer_del(sensor_event_timer); + + sensor_event_timer = ecore_timer_add(TIMER_SENSOR_INTERVAL, _get_sensor_data, NULL); + if (!sensor_event_timer) + _E("Failed to add a timer"); + return true; } static void service_app_terminate(void *user_data) { - // clear timer event + // clear timer resource if (sensor_event_timer) { ecore_timer_del(sensor_event_timer); sensor_event_timer = NULL; } - INFO("close I2C handle"); - resource_close_i2c_handle(); + // close sensor resource + resource_close_illuminance_sensor(); + + /*terminate resource*/ + if (deinit_resource_app() != 0) { + _E("deinit_resource_app failed"); + } - int status = pthread_mutex_destroy(&mutex); - INFO("mutex destroy status = %d", status); + /*terminate master*/ + if (deinit_master_app() != 0) { + _E("deinit_master_app failed"); + } + + return; } static void service_app_control(app_control_h app_control, void *user_data) { if (app_control == NULL) { - ERR("app_control is NULL"); + _E("app_control is NULL"); return; } - init_thing(); + init_master_app(); + init_resource_app(); + + return; } int main(int argc, char *argv[]) { - FN_CALL; - service_app_lifecycle_callback_s event_callback; event_callback.create = service_app_create; |