From 7001b78c82c59c0472f4ab41129682d501644ce2 Mon Sep 17 00:00:00 2001 From: Eunyoung Lee Date: Tue, 24 Oct 2017 20:52:14 +0900 Subject: Challenge : Create server requried in Question#3 Change-Id: I9c42933156e3fced4cc7b7c5bd252a08db047bd1 --- inc/connectivity.h | 28 ++++++++++++++-------- src/connectivity.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- src/controller.c | 25 +++++++------------- 3 files changed, 93 insertions(+), 28 deletions(-) diff --git a/inc/connectivity.h b/inc/connectivity.h index b88f83a..5165dda 100644 --- a/inc/connectivity.h +++ b/inc/connectivity.h @@ -50,33 +50,43 @@ extern int connectivity_set_resource(const char *path, const char *type, connect extern void connectivity_unset_resource(connectivity_resource_s *resource); /** - * @brief Notifies specific clients that resource's attributes have changed with boolean value. + * @brief Notifies a resource's value to observed clients. * @param[in] resource_info A structure containing information about connectivity resource - * @param[in] key A new key to be added into attributes - * @param[in] value A boolean value to be added into attributes + * @param[in] key A key to be sended. + * @param[in] value A value to be sended. * @return 0 on success, otherwise a negative error value * @see If key is already exists, current value will be replaced with new value. */ extern int connectivity_notify_bool(connectivity_resource_s *resource_info, const char *key, bool value); /** - * @brief Notifies specific clients that resource's attributes have changed with int value. + * @brief Notifies a resource's value to observed clients. * @param[in] resource_info A structure containing information about connectivity resource - * @param[in] key A new key to be added into attributes - * @param[in] value A int value to be added into attributes + * @param[in] key A key to be sended. + * @param[in] value A value to be sended. * @return 0 on success, otherwise a negative error value * @see If key is already exists, current value will be replaced with new value. */ extern int connectivity_notify_int(connectivity_resource_s *resource_info, const char *key, int value); /** - * @brief Notifies specific clients that resource's attributes have changed with double value. + * @brief Notifies a resource's value to observed clients. * @param[in] resource_info A structure containing information about connectivity resource - * @param[in] key A new key to be added into attributes - * @param[in] value A double value to be added into attributes + * @param[in] key A key to be sended. + * @param[in] value A value to be sended. * @return 0 on success, otherwise a negative error value * @see If key is already exists, current value will be replaced with new value. */ extern int connectivity_notify_double(connectivity_resource_s *resource_info, const char *key, double value); +/** + * @brief Notifies a resource's value to observed clients. + * @param[in] resource_info A structure containing information about connectivity resource + * @param[in] key A key to be sended. + * @param[in] value A value to be sended. + * @return 0 on success, otherwise a negative error value + * @see If key is already exists, current value will be replaced with new value. + */ +extern int connectivity_notify_string(connectivity_resource_s *resource_info, const char *key, char *value); + #endif /* __POSITION_FINDER_CONNECTIVITY_H__ */ diff --git a/src/connectivity.c b/src/connectivity.c index 3154e2d..09b9c30 100644 --- a/src/connectivity.c +++ b/src/connectivity.c @@ -29,7 +29,7 @@ #include "log.h" #include "connectivity.h" -#define ULTRASONIC_RESOURCE_TYPE "org.tizen.door" +#define DEVICE_NAME "Dr.Evil's device" #define BUFSIZE 1024 #define URI_PATH_LEN 64 #define URI_PATH "/door/1" @@ -186,6 +186,45 @@ error: return NULL; } +static iotcon_representation_h _create_representation_with_str(connectivity_resource_s *resource_info, const char *key, char *value) +{ + iotcon_attributes_h attributes = NULL; + iotcon_representation_h representation = NULL; + char *uri_path = NULL; + int ret = -1; + + ret = iotcon_resource_get_uri_path(resource_info->res, &uri_path); + retv_if(IOTCON_ERROR_NONE != ret, NULL); + + ret = iotcon_representation_create(&representation); + retv_if(IOTCON_ERROR_NONE != ret, NULL); + + ret = iotcon_attributes_create(&attributes); + goto_if(IOTCON_ERROR_NONE != ret, error); + + ret = iotcon_representation_set_uri_path(representation, uri_path); + goto_if(IOTCON_ERROR_NONE != ret, error); + + ret = iotcon_attributes_add_str(attributes, PATH, resource_info->path); + goto_if(IOTCON_ERROR_NONE != ret, error); + + ret = iotcon_attributes_add_str(attributes, key, value); + goto_if(IOTCON_ERROR_NONE != ret, error); + + ret = iotcon_representation_set_attributes(representation, attributes); + goto_if(IOTCON_ERROR_NONE != ret, error); + + iotcon_attributes_destroy(attributes); + + return representation; + +error: + if (attributes) iotcon_attributes_destroy(attributes); + if (representation) iotcon_representation_destroy(representation); + + return NULL; +} + static void _print_iotcon_error(int err_no) { switch (err_no) { @@ -279,6 +318,31 @@ int connectivity_notify_double(connectivity_resource_s *resource_info, const cha return 0; } +int connectivity_notify_string(connectivity_resource_s *resource_info, const char *key, char *value) +{ + iotcon_representation_h representation; + int ret = -1; + + retv_if(!resource_info, -1); + retv_if(!resource_info->observers, -1); + + _D("Notify all members [%s]", value); + + representation = _create_representation_with_str(resource_info, key, value); + retv_if(!representation, -1); + + ret = iotcon_resource_notify(resource_info->res, representation, resource_info->observers, IOTCON_QOS_LOW); + if (IOTCON_ERROR_NONE != ret) { + //_I("There are some troubles for notifying value[%d]", ret); + _print_iotcon_error(ret); + return -1; + } + + _destroy_representation(representation); + + return 0; +} + static bool _query_cb(const char *key, const char *value, void *user_data) { _D("Key : [%s], Value : [%s]", key, value); @@ -430,7 +494,7 @@ int connectivity_init(void) ret = iotcon_initialize(CBOR_FILE_IN_DATA); retv_if(IOTCON_ERROR_NONE != ret, -1); - ret = iotcon_set_device_name(ULTRASONIC_RESOURCE_TYPE); + ret = iotcon_set_device_name(DEVICE_NAME); goto_if(IOTCON_ERROR_NONE != ret, error); return 0; diff --git a/src/controller.c b/src/controller.c index 7b39aa8..2189bf9 100644 --- a/src/controller.c +++ b/src/controller.c @@ -32,30 +32,21 @@ #include "controller_util.h" #include "webutil.h" -#define CONNECTIVITY_KEY "opened" -#define SENSORING_TIME_INTERVAL 1.0f -#define TRIG_PIN_NUMBER 20 -#define ECHO_PIN_NUMBER 21 +#define LOCATION_RESOURCE_TYPE "org.tizen.location" +#define LOCATION_KEY "location" +#define LOCATION_MESSAGE "Dr.Evil is located in NEWYORK" +#define NOTIFY_TIME_INTERVAL 5.0f typedef struct app_data_s { Ecore_Timer *getter_timer; connectivity_resource_s *resource_info; } app_data; -static void _ultrasonic_sensor_read_cb(float value, void *data) -{ - _I("Distance : %.2fcm", value); -} - -static Eina_Bool _control_sensors_cb(void *data) +static Eina_Bool _control_notify_cb(void *data) { app_data *ad = data; - if (resource_read_ultrasonic_sensor(TRIG_PIN_NUMBER, ECHO_PIN_NUMBER, _ultrasonic_sensor_read_cb, NULL) == -1) - _E("Failed to get a distance from Ultrasonic sensor"); - - - if (connectivity_notify_bool(ad->resource_info, CONNECTIVITY_KEY, true) == -1) + if (connectivity_notify_string(ad->resource_info, LOCATION_KEY, LOCATION_MESSAGE) == -1) _E("Cannot notify message"); return ECORE_CALLBACK_RENEW; @@ -77,14 +68,14 @@ static bool service_app_create(void *data) * Create a connectivity resource and registers the resource in server. */ controller_util_get_path(&path); - ret = connectivity_set_resource(path, "org.tizen.door", &ad->resource_info); + ret = connectivity_set_resource(path, LOCATION_RESOURCE_TYPE, &ad->resource_info); if (ret == -1) _E("Cannot broadcast resource"); /** * Creates a timer to call the given function in the given period of time. * In the control_sensors_cb(), each sensor reads the measured value or writes a specific value to the sensor. */ - ad->getter_timer = ecore_timer_add(SENSORING_TIME_INTERVAL, _control_sensors_cb, ad); + ad->getter_timer = ecore_timer_add(NOTIFY_TIME_INTERVAL, _control_notify_cb, ad); if (!ad->getter_timer) { _E("Failed to add infrared motion getter timer"); return false; -- cgit v1.2.3