diff options
author | Jin Yoon <jinny.yoon@samsung.com> | 2017-08-01 05:11:34 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@review.ap-northeast-2.compute.internal> | 2017-08-01 05:11:34 +0000 |
commit | 4709d4e72c37622918bf09708c142375aeb198d5 (patch) | |
tree | 21d6fbb402e9f799b1fafbef3695443746d2f6db | |
parent | f71834f94aac05a54b64a983e6c6bc7789054843 (diff) | |
parent | 7f33b3e9f1d4a4cb2fede2b4e4d72f6c90d9e4a1 (diff) | |
download | position-finder-server-4709d4e72c37622918bf09708c142375aeb198d5.tar.gz position-finder-server-4709d4e72c37622918bf09708c142375aeb198d5.tar.bz2 position-finder-server-4709d4e72c37622918bf09708c142375aeb198d5.zip |
Merge "Add connectivity APIs for various data types"
-rw-r--r-- | inc/connectivity.h | 4 | ||||
-rw-r--r-- | src/connectivity.c | 169 | ||||
-rw-r--r-- | src/controller.c | 85 |
3 files changed, 86 insertions, 172 deletions
diff --git a/inc/connectivity.h b/inc/connectivity.h index 001bdf5..7b21043 100644 --- a/inc/connectivity.h +++ b/inc/connectivity.h @@ -34,6 +34,8 @@ typedef struct connectivity_resource connectivity_resource_s; extern int connectivity_set_resource(const char *uri_path, const char *type, connectivity_resource_s **out_resource_info); extern void connectivity_unset_resource(connectivity_resource_s *resource); -extern int connectivity_notify(connectivity_resource_s *resource_info, int value); +extern int connectivity_notify_bool(connectivity_resource_s *resource_info, const char *key, bool value); +extern int connectivity_notify_int(connectivity_resource_s *resource_info, const char *key, int value); +extern int connectivity_notify_double(connectivity_resource_s *resource_info, const char *key, double value); #endif /* __POSITION_FINDER_CONNECTIVITY_H__ */ diff --git a/src/connectivity.c b/src/connectivity.c index 0073de1..eb720ec 100644 --- a/src/connectivity.c +++ b/src/connectivity.c @@ -20,6 +20,7 @@ */ #include <stdlib.h> +#include <stdbool.h> #include <glib.h> #include <Eina.h> @@ -32,7 +33,6 @@ #define ULTRASONIC_RESOURCE_2_URI "/door/2" #define ULTRASONIC_RESOURCE_TYPE "org.tizen.door" -static bool _resource_created; static void _request_resource_handler(iotcon_resource_h resource, iotcon_request_h request, void *user_data); static int _send_response(iotcon_request_h request, iotcon_representation_h representation, iotcon_response_result_e result) @@ -67,7 +67,7 @@ static void _destroy_representation(iotcon_representation_h representation) iotcon_representation_destroy(representation); } -static iotcon_representation_h _create_representation_with_attribute(iotcon_resource_h res, bool value) +static iotcon_representation_h _create_representation_with_bool(iotcon_resource_h res, const char *key, bool value) { iotcon_attributes_h attributes = NULL; iotcon_representation_h representation = NULL; @@ -103,84 +103,79 @@ error: return NULL; } -static int _handle_get_request(iotcon_resource_h res, iotcon_request_h request) +static iotcon_representation_h _create_representation_with_int(iotcon_resource_h res, const char *key, int value) { - iotcon_representation_h representation; + iotcon_attributes_h attributes = NULL; + iotcon_representation_h representation = NULL; + char *uri_path = NULL; int ret = -1; - int value = 1; - /* FIXME : We need to check the value of sensors */ - representation = _create_representation_with_attribute(res, (bool)value); - retv_if(!representation, -1); + ret = iotcon_resource_get_uri_path(res, &uri_path); + retv_if(IOTCON_ERROR_NONE != ret, NULL); - ret = _send_response(request, representation, IOTCON_RESPONSE_OK); - goto_if(0 != ret, error); + ret = iotcon_representation_create(&representation); + retv_if(IOTCON_ERROR_NONE != ret, NULL); - _destroy_representation(representation); + ret = iotcon_attributes_create(&attributes); + goto_if(IOTCON_ERROR_NONE != ret, error); - return 0; + ret = iotcon_representation_set_uri_path(representation, uri_path); + goto_if(IOTCON_ERROR_NONE != ret, error); -error: - _destroy_representation(representation); - return -1; -} + ret = iotcon_attributes_add_int(attributes, "opened", value); + goto_if(IOTCON_ERROR_NONE != ret, error); -static int _get_value_from_representation(iotcon_representation_h representation, bool *value) -{ - iotcon_attributes_h attributes; - int ret = -1; + ret = iotcon_representation_set_attributes(representation, attributes); + goto_if(IOTCON_ERROR_NONE != ret, error); - ret = iotcon_representation_get_attributes(representation, &attributes); - retv_if(IOTCON_ERROR_NONE != ret, -1); + iotcon_attributes_destroy(attributes); - ret = iotcon_attributes_get_bool(attributes, "opened", value); - retv_if(IOTCON_ERROR_NONE != ret, -1); + return representation; - return 0; -} +error: + if (attributes) iotcon_attributes_destroy(attributes); + if (representation) iotcon_representation_destroy(representation); -static int _set_value_into_thing(iotcon_representation_h representation, bool value) -{ - /* FIXME : We need to set the value into the thing */ - return 0; + return NULL; } -static int _handle_put_request(connectivity_resource_s *resource_info, iotcon_request_h request) +static iotcon_representation_h _create_representation_with_double(iotcon_resource_h res, const char *key, double value) { - iotcon_representation_h req_repr, resp_repr; + iotcon_attributes_h attributes = NULL; + iotcon_representation_h representation = NULL; + char *uri_path = NULL; int ret = -1; - bool value = false; - - _D("PUT request"); - ret = iotcon_request_get_representation(request, &req_repr); - retv_if(IOTCON_ERROR_NONE != ret, -1); + ret = iotcon_resource_get_uri_path(res, &uri_path); + retv_if(IOTCON_ERROR_NONE != ret, NULL); - ret = _get_value_from_representation(req_repr, &value); - retv_if(0 != ret, -1); + ret = iotcon_representation_create(&representation); + retv_if(IOTCON_ERROR_NONE != ret, NULL); - ret = _set_value_into_thing(req_repr, value); - retv_if(0 != ret, -1); + ret = iotcon_attributes_create(&attributes); + goto_if(IOTCON_ERROR_NONE != ret, error); - resp_repr = _create_representation_with_attribute(resource_info->res, (bool)value); - retv_if(NULL == resp_repr, -1); + ret = iotcon_representation_set_uri_path(representation, uri_path); + goto_if(IOTCON_ERROR_NONE != ret, error); - ret = _send_response(request, resp_repr, IOTCON_RESPONSE_OK); - goto_if(0 != ret, error); + ret = iotcon_attributes_add_double(attributes, "opened", value); + goto_if(IOTCON_ERROR_NONE != ret, error); - ret = iotcon_resource_notify(resource_info->res, resp_repr, resource_info->observers, IOTCON_QOS_HIGH); + ret = iotcon_representation_set_attributes(representation, attributes); goto_if(IOTCON_ERROR_NONE != ret, error); - _destroy_representation(resp_repr); + iotcon_attributes_destroy(attributes); - return 0; + return representation; error: - _destroy_representation(resp_repr); - return -1; + if (attributes) iotcon_attributes_destroy(attributes); + if (representation) iotcon_representation_destroy(representation); + + return NULL; } -int connectivity_notify(connectivity_resource_s *resource_info, int value) +int connectivity_notify_bool(connectivity_resource_s *resource_info, const char *key, bool value) { iotcon_representation_h representation; int ret = -1; @@ -190,7 +185,7 @@ int connectivity_notify(connectivity_resource_s *resource_info, int value) _D("Notify the value[%d]", value); - representation = _create_representation_with_attribute(resource_info->res, (bool)value); + representation = _create_representation_with_bool(resource_info->res, key, value); retv_if(!representation, -1); ret = iotcon_resource_notify(resource_info->res, representation, resource_info->observers, IOTCON_QOS_HIGH); @@ -201,66 +196,44 @@ int connectivity_notify(connectivity_resource_s *resource_info, int value) return 0; } -static int _handle_post_request(connectivity_resource_s *resource_info, iotcon_request_h request) +int connectivity_notify_int(connectivity_resource_s *resource_info, const char *key, int value) { - iotcon_attributes_h resp_attributes = NULL; - iotcon_representation_h resp_repr = NULL; - connectivity_resource_s *new_resource_info = NULL; + iotcon_representation_h representation; int ret = -1; - _D("POST request"); - - if (_resource_created) { - _E("Resource(%s) is already created", ULTRASONIC_RESOURCE_2_URI); - return -1; - } - - new_resource_info = calloc(1, sizeof(connectivity_resource_s)); - retv_if(!new_resource_info, -1); + retv_if(!resource_info, -1); + retv_if(!resource_info->observers, -1); - ret = connectivity_set_resource(ULTRASONIC_RESOURCE_2_URI, ULTRASONIC_RESOURCE_TYPE, &new_resource_info); - retv_if(0 != ret, -1); + _D("Notify the value[%d]", value); - _resource_created = true; + representation = _create_representation_with_int(resource_info->res, key, value); + retv_if(!representation, -1); - ret = iotcon_representation_create(&resp_repr); + ret = iotcon_resource_notify(resource_info->res, representation, resource_info->observers, IOTCON_QOS_HIGH); retv_if(IOTCON_ERROR_NONE != ret, -1); - ret = iotcon_attributes_create(&resp_attributes); - goto_if(IOTCON_ERROR_NONE != ret, error); - - ret = iotcon_attributes_add_str(resp_attributes, "createduripath", ULTRASONIC_RESOURCE_2_URI); - goto_if(IOTCON_ERROR_NONE != ret, error); - - ret = iotcon_representation_set_attributes(resp_repr, resp_attributes); - goto_if(IOTCON_ERROR_NONE != ret, error); - - iotcon_attributes_destroy(resp_attributes); - - ret = _send_response(request, resp_repr, IOTCON_RESPONSE_RESOURCE_CREATED); - goto_if(0 != ret, error); - - iotcon_representation_destroy(resp_repr); + _destroy_representation(representation); return 0; - -error: - if (resp_attributes) iotcon_attributes_destroy(resp_attributes); - iotcon_representation_destroy(resp_repr); - return -1; } -static int _handle_delete_request(iotcon_resource_h resource, iotcon_request_h request) +int connectivity_notify_double(connectivity_resource_s *resource_info, const char *key, double value) { + iotcon_representation_h representation; int ret = -1; - _D("DELETE request"); + retv_if(!resource_info, -1); + retv_if(!resource_info->observers, -1); + + _D("Notify the value[%f]", value); - ret = iotcon_resource_destroy(resource); + representation = _create_representation_with_double(resource_info->res, key, value); + retv_if(!representation, -1); + + ret = iotcon_resource_notify(resource_info->res, representation, resource_info->observers, IOTCON_QOS_HIGH); retv_if(IOTCON_ERROR_NONE != ret, -1); - ret = _send_response(request, NULL, IOTCON_RESPONSE_RESOURCE_DELETED); - retv_if(0 != ret, -1); + _destroy_representation(representation); return 0; } @@ -295,16 +268,16 @@ static int _handle_request_by_crud_type(iotcon_request_h request, connectivity_r switch (type) { case IOTCON_REQUEST_GET: - ret = _handle_get_request(resource_info->res, request); + _I("Do not support 'get' query"); break; case IOTCON_REQUEST_PUT: - ret = _handle_put_request(resource_info, request); + _I("Do not support 'put' query"); break; case IOTCON_REQUEST_POST: - ret = _handle_post_request(resource_info, request); + _I("Do not support 'post' query"); break; case IOTCON_REQUEST_DELETE: - ret = _handle_delete_request(resource_info->res, request); + _I("Do not support 'delete' query"); break; default: _E("Cannot reach here"); diff --git a/src/controller.c b/src/controller.c index 2a54bb3..20388a7 100644 --- a/src/controller.c +++ b/src/controller.c @@ -32,27 +32,22 @@ #include "connectivity.h" #include "controller.h" -#define I2C_BUS_1 0x1 -#define GPIO_NOT_USED -1 #define GPIO_ULTRASONIC_TRIG_NUM_1 20 #define GPIO_ULTRASONIC_ECHO_NUM_1 21 -#define GPIO_INFRARED_MOTION_NUM_1 4 -#define I2C_ILLUMINANCE_FIRST_PIN_1 3 -#define USE_MULTIPLE_SENSOR 0 #define MULTIPLE_SENSOR_NUMBER 5 +#define CONNECTIVITY_KEY "opened" typedef struct app_data_s { - Ecore_Timer *getter_timer[PIN_MAX]; + Ecore_Timer *getter_timer; connectivity_resource_s *resource_info; } app_data; -static Eina_Bool _infrared_motion_getter_timer(void *data) +static Eina_Bool control_read_sensors_cb(void *data) { -#if USE_MULTIPLE_SENSOR + int value[MULTIPLE_SENSOR_NUMBER] = { 0, }; + int total = 0; int gpio_num[MULTIPLE_SENSOR_NUMBER] = { 5, 6, 13, 19, 26 }; int i = 0; - int value[MULTIPLE_SENSOR_NUMBER] = { 0, }; - int detected = 0; app_data *ad = data; for (i = 0; i < MULTIPLE_SENSOR_NUMBER; i++) { @@ -60,86 +55,30 @@ static Eina_Bool _infrared_motion_getter_timer(void *data) _E("Failed to get Infrared Motion value [GPIO:%d]", gpio_num[i]); continue; } - detected |= value[i]; + total |= value[i]; } - _I("[5:%d][6:%d][13:%d][19:%d][26:%d]", value[0], value[1], value[2], value[3], value[4]); + _I("[5:%d] | [6:%d] | [13:%d] | [19:%d] | [26:%d] = [Total:%d]", value[0], value[1], value[2], value[3], value[4], total); - if (connectivity_notify(ad->resource_info, detected) == -1) + if (connectivity_notify_bool(ad->resource_info, CONNECTIVITY_KEY, total) == -1) _E("Cannot notify message"); -#else - int value = 0; - - retv_if(resource_read_infrared_motion_sensor(GPIO_INFRARED_MOTION_NUM_1, &value) == -1, ECORE_CALLBACK_CANCEL); - _I("Infrared Motion Value is [%d]", value); -#endif - - return ECORE_CALLBACK_RENEW; -} - -#if (!USE_MULTIPLE_SENSOR) -static void ultrasonic_sensor_read_cb(double value, void *data) -{ - _I("Distance : %.2fcm", value); -} - -static Eina_Bool _ultrasonic_getter_timer(void *data) -{ - double value = 0; - - retv_if(resource_read_ultrasonic_sensor(GPIO_ULTRASONIC_TRIG_NUM_1, GPIO_ULTRASONIC_ECHO_NUM_1, ultrasonic_sensor_read_cb, NULL) == -1, ECORE_CALLBACK_CANCEL); return ECORE_CALLBACK_RENEW; } -static Eina_Bool _illuminance_getter_timer(void *data) -{ - int value = 0; - - retv_if(resource_read_illuminance_sensor(I2C_BUS_1, &value) == -1, ECORE_CALLBACK_CANCEL); - _I("Illuminance sensor is [%d lux]", value); - - return ECORE_CALLBACK_RENEW; -} -#endif - static bool service_app_create(void *data) { app_data *ad = data; int ret = -1; controller_init_internal_functions(); - ret = connectivity_set_resource("/door/1", "org.tizen.door", &ad->resource_info); if (ret == -1) _E("Cannot broadcast resource"); -#if USE_MULTIPLE_SENSOR - ad->getter_timer[GPIO_INFRARED_MOTION_NUM_1] = ecore_timer_add(3.0f, _infrared_motion_getter_timer, ad); - if (!ad->getter_timer[GPIO_INFRARED_MOTION_NUM_1]) { + ad->getter_timer = ecore_timer_add(0.5f, control_read_sensors_cb, ad); + if (!ad->getter_timer) { _E("Failed to add infrared motion getter timer"); return false; } -#else - /* One Pin Sensor */ - ad->getter_timer[GPIO_INFRARED_MOTION_NUM_1] = ecore_timer_add(1.0f, _infrared_motion_getter_timer, ad); - if (!ad->getter_timer[GPIO_INFRARED_MOTION_NUM_1]) { - _D("Failed to add infrared motion getter timer"); - return false; - } - - /* Two Pins Sensor */ - ad->getter_timer[GPIO_ULTRASONIC_TRIG_NUM_1] = ecore_timer_add(1.0f, _ultrasonic_getter_timer, ad); - if (!ad->getter_timer[GPIO_ULTRASONIC_TRIG_NUM_1]) { - _D("Failed to add ultra sonic getter timer"); - return false; - } - - /* I2C Protocol Sensor */ - ad->getter_timer[I2C_ILLUMINANCE_FIRST_PIN_1] = ecore_timer_add(1.0f, _illuminance_getter_timer, ad); - if (!ad->getter_timer[I2C_ILLUMINANCE_FIRST_PIN_1]) { - _D("Failed to add illuminance getter timer"); - return false; - } -#endif return true; } @@ -149,8 +88,8 @@ static void service_app_terminate(void *data) app_data *ad = (app_data *)data; for (int i = 0; i < PIN_MAX; i++) { - if (ad->getter_timer[i]) { - ecore_timer_del(ad->getter_timer[i]); + if (ad->getter_timer) { + ecore_timer_del(ad->getter_timer); } } |