From d9b34db8bdb7f624e34f5af2976233c52aa9c95f Mon Sep 17 00:00:00 2001 From: Jin Yoon Date: Fri, 14 Jul 2017 18:52:48 +0900 Subject: [PATCH] New API for CC Pattern's Connectivity Change-Id: I0fce5ad528adef459afe5e8cecef92b1453dc808 --- inc/connectivity.h | 3 ++- inc/connectivity_internal.h | 6 +++--- src/connectivity.c | 43 +++++++++++++++++++++++++++++-------------- src/control.c | 11 ++++++++++- 4 files changed, 44 insertions(+), 19 deletions(-) diff --git a/inc/connectivity.h b/inc/connectivity.h index 00e3a2b..820dc3f 100644 --- a/inc/connectivity.h +++ b/inc/connectivity.h @@ -25,7 +25,8 @@ #include "connectivity_internal.h" typedef struct _connectivity_resource_s connectivity_resource_s; +typedef void (*connectivity_observe_resource_cb)(connectivity_resource_s *resource_info, void *resource_data, void *user_data); -extern int connectivity_find_resource(void); +extern int connectivity_observe_resource(connectivity_observe_resource_cb cb, void *user_data); #endif /* __POSITION_FINDER_CONNECTIVITY_H__ */ diff --git a/inc/connectivity_internal.h b/inc/connectivity_internal.h index af21943..ff4abe7 100644 --- a/inc/connectivity_internal.h +++ b/inc/connectivity_internal.h @@ -19,10 +19,10 @@ * limitations under the License. */ -#ifndef __POSITION_FINDER_CONNECTIVITY_H__ -#define __POSITION_FINDER_CONNECTIVITY_H__ +#ifndef __POSITION_FINDER_CONNECTIVITY_INTERNAL_H__ +#define __POSITION_FINDER_CONNECTIVITY_INTERNAL_H__ extern int connectivity_init(void); extern int connectivity_fini(void); -#endif /* __POSITION_FINDER_CONNECTIVITY_H__ */ +#endif /* __POSITION_FINDER_CONNECTIVITY_INTERNAL_H__ */ diff --git a/src/connectivity.c b/src/connectivity.c index 8b29131..bce0117 100644 --- a/src/connectivity.c +++ b/src/connectivity.c @@ -28,6 +28,12 @@ #define DOOR_RESOURCE_URI_PREFIX "/door" #define DOOR_RESOURCE_TYPE "org.tizen.door" +struct _connectivity_observe_resource_cb_s { + connectivity_observe_resource_cb cb; + void *user_data; +}; +typedef struct _connectivity_observe_resource_cb_s connectivity_observe_resource_cb_s; + struct _connectivity_resource_s { char *device_id; char *host_address; @@ -35,6 +41,7 @@ struct _connectivity_resource_s { char *uri_path; iotcon_presence_h presence; iotcon_remote_resource_h resource; + connectivity_observe_resource_cb_s *cb_info; }; static struct { @@ -53,7 +60,11 @@ static void _observe_cb(iotcon_remote_resource_h resource, iotcon_error_e err, i iotcon_attributes_h attributes = NULL; iotcon_representation_h repr = NULL; iotcon_response_result_e response_result; + connectivity_resource_s *resource_info = user_data; + ret_if(!resource_info); + ret_if(!resource_info->cb_info); + ret_if(!resource_info->cb_info->cb); ret_if(IOTCON_ERROR_NONE != err); ret = iotcon_response_get_result(response, &response_result); @@ -76,6 +87,7 @@ static void _observe_cb(iotcon_remote_resource_h resource, iotcon_error_e err, i ret = iotcon_attributes_get_bool(attributes, "opened", &opened); ret_if(IOTCON_ERROR_NONE != ret); + resource_info->cb_info->cb(resource_info, (void *)(int) opened, resource_info->cb_info->user_data); if (opened) _I("[Door] opened."); else _I("[Door] closed."); } @@ -551,7 +563,7 @@ static int _register_observe(connectivity_resource_s *info) IOTCON_OBSERVE_IGNORE_OUT_OF_ORDER, NULL, _observe_cb, - NULL); + info); retv_if(IOTCON_ERROR_NONE != ret, -1); return 0; @@ -592,7 +604,6 @@ static bool _found_resource_cb(iotcon_remote_resource_h resource, iotcon_error_e int ret = -1; connectivity_resource_s *info = NULL; - _D("HELLO"); /* We should know where we get wrong results */ /* Can attackers send wrong messages to stop this? */ retv_if(IOTCON_ERROR_NONE != result, IOTCON_FUNC_STOP); @@ -605,10 +616,8 @@ static bool _found_resource_cb(iotcon_remote_resource_h resource, iotcon_error_e if (1 == ret) return IOTCON_FUNC_CONTINUE; info = calloc(1, sizeof(connectivity_resource_s)); - retv_if(!info, IOTCON_FUNC_CONTINUE); - - ret = _add_resource_info_into_list(info); - goto_if(-1 == ret, error); + retv_if(!info, -1); + info->cb_info = user_data; ret = _retrieve_resource_info(resource, info); goto_if(-1 == ret, error); @@ -626,10 +635,12 @@ static bool _found_resource_cb(iotcon_remote_resource_h resource, iotcon_error_e _E("Cannot request query"); } + ret = _add_resource_info_into_list(info); + goto_if(-1 == ret, error); + return IOTCON_FUNC_CONTINUE; error: - connectivity_info.list = eina_list_remove(connectivity_info.list, info); if (info) _free_resource_info(info); return IOTCON_FUNC_CONTINUE; } @@ -655,31 +666,35 @@ int connectivity_fini(void) return 0; } -int connectivity_find_resource(void) +int connectivity_observe_resource(connectivity_observe_resource_cb cb, void *user_data) { int ret = -1; iotcon_query_h query = NULL; + connectivity_observe_resource_cb_s *cb_info = NULL; - _D("HELLO"); + cb_info = calloc(1, sizeof(connectivity_observe_resource_cb_s)); + retv_if(!cb_info, -1); + cb_info->cb = cb; + cb_info->user_data = user_data; ret = iotcon_query_create(&query); - retv_if(IOTCON_ERROR_NONE != ret, -1); + goto_if(IOTCON_ERROR_NONE != ret, error); ret = iotcon_query_set_resource_type(query, DOOR_RESOURCE_TYPE); goto_if(IOTCON_ERROR_NONE != ret, error); ret = iotcon_find_resource(IOTCON_MULTICAST_ADDRESS, IOTCON_CONNECTIVITY_IP | IOTCON_CONNECTIVITY_PREFER_UDP, - query, - _found_resource_cb, - NULL); + query, + _found_resource_cb, + cb_info); goto_if(IOTCON_ERROR_NONE != ret, error); - _D("HELLO"); iotcon_query_destroy(query); return 0; error: iotcon_query_destroy(query); + if (cb_info) free(cb_info); return -1; } diff --git a/src/control.c b/src/control.c index d308805..7fda7f2 100644 --- a/src/control.c +++ b/src/control.c @@ -28,13 +28,22 @@ static void _start_internal_function(void); static void _stop_internal_function(void); +void _observe_resource_cb(connectivity_resource_s *resource_info, void *resource_data, void *user_data) +{ + int opened = (int) resource_data; + + if (opened) _I("[Door] opened."); + else _I("[Door] closed."); +} + bool service_app_create(void *data) { int ret = -1; _start_internal_function(); - connectivity_find_resource(); + ret = connectivity_observe_resource(_observe_resource_cb, NULL); + retv_if(ret == -1, false); return true; } -- 2.7.4