From 42b2954e8bff0902b14408f0670461dd34795f2e Mon Sep 17 00:00:00 2001 From: youngman Date: Mon, 15 Jun 2015 16:43:55 +0900 Subject: [PATCH] Attach "coap://" automatically. & Divide "notify"(all and list) Change-Id: I18193f0b85139aaa174e0c3cce1ec521a14fc167 Signed-off-by: youngman --- lib/icl-client.c | 3 +- lib/icl-device.c | 1 - lib/icl-ioty-repr.cpp | 3 +- lib/icl-ioty.cpp | 159 ++++++++++++++++++++++-------------------- lib/icl-ioty.h | 5 +- lib/icl-repr-list.c | 88 +++++++++++------------ lib/icl-repr-obj.c | 23 +++--- lib/icl-repr-value.c | 112 ++++++++++++++--------------- lib/icl-repr.c | 13 ++-- lib/icl-request.h | 1 - lib/icl-resource-types.c | 4 +- lib/icl.c | 43 ++++++------ lib/include/iotcon-constant.h | 2 +- lib/include/iotcon-struct.h | 2 +- lib/include/iotcon.h | 3 +- test/crud-test-client.c | 14 ++-- test/crud-test-server.c | 14 +++- test/device-test-client.c | 2 +- test/device-test-server.c | 2 +- test/repr-test-client.c | 14 ++-- test/repr-test-server.c | 13 ++-- test/{test-log.h => test.h} | 19 ++--- 22 files changed, 274 insertions(+), 266 deletions(-) rename test/{test-log.h => test.h} (88%) diff --git a/lib/icl-client.c b/lib/icl-client.c index 6a5b286..1698b87 100644 --- a/lib/icl-client.c +++ b/lib/icl-client.c @@ -27,8 +27,7 @@ #include "icl-resource-types.h" #include "icl-client.h" -/* host address should begin with "coap://" - * The length of resource_type should be less than or equal to 61. +/* The length of resource_type should be less than or equal to 61. * If resource_type is NULL, then All resources in host are discovered. */ API int iotcon_find_resource(const char *host_addr, const char *resource_type, iotcon_found_resource_cb cb, void *user_data) diff --git a/lib/icl-device.c b/lib/icl-device.c index b8f1702..56d6a0d 100644 --- a/lib/icl-device.c +++ b/lib/icl-device.c @@ -49,7 +49,6 @@ API int iotcon_register_device_info(iotcon_device_info_s device_info) } -/* host_address should begin with "coap://" */ API int iotcon_get_device_info(const char *host_address, iotcon_device_info_cb cb, void *user_data) { diff --git a/lib/icl-ioty-repr.cpp b/lib/icl-ioty-repr.cpp index f611a8e..36cf72e 100644 --- a/lib/icl-ioty-repr.cpp +++ b/lib/icl-ioty-repr.cpp @@ -114,8 +114,7 @@ int ic_ioty_repr_parse(iotcon_repr_h repr, OCRepresentation &ocRep) for (; itr != back; ++itr) ocRep.addChild(*itr); - } - else { + } else { ERR("Invalid parameter(%s)", repr_json); ret = IOTCON_ERROR_INVALID_PARAMETER; } diff --git a/lib/icl-ioty.cpp b/lib/icl-ioty.cpp index 4971f24..78d212c 100644 --- a/lib/icl-ioty.cpp +++ b/lib/icl-ioty.cpp @@ -32,7 +32,8 @@ extern "C" { #include "icl-ioty.h" } -#define IC_UNICAST_RESOURCE_DISCOVERY ":5683/oc/core" +#define IC_COAP "coap://" +#define IC_UNICAST_RESOURCE_DISCOVERY "/oc/core" #define IC_MULTICAST_RESOURCE_DISCOVERY "/oc/core" #define IC_DEVICE_DISCOVERY "/oc/core/d" @@ -102,6 +103,7 @@ namespace icIotivityHandler { { struct ic_remote_resource resource_s = {0}; resource_s.types = NULL; + string resource_host; vector resource_types = resource->getResourceTypes(); if (0 < resource_types.size()) { @@ -116,7 +118,11 @@ namespace icIotivityHandler { } resource_s.uri = ic_utils_strdup(resource->uri().c_str()); - resource_s.host = ic_utils_strdup(resource->host().c_str()); + + resource_host = resource->host(); + resource_host.erase(0, strlen(IC_COAP)); + resource_s.host = ic_utils_strdup(resource_host.c_str()); + resource_s.is_observable = resource->isObservable(); vector resource_interfaces = resource->getResourceInterfaces(); @@ -166,8 +172,7 @@ namespace icIotivityHandler { if (OC_STACK_OK == eCode) { res = IOTCON_RESPONSE_RESULT_OK; - } - else { + } else { ERR("get() Fail(%d)", eCode); res = IOTCON_RESPONSE_RESULT_ERROR; } @@ -211,8 +216,7 @@ namespace icIotivityHandler { if (OC_STACK_OK == eCode) { res = IOTCON_RESPONSE_RESULT_OK; - } - else { + } else { ERR("put() Fail(%d)", eCode); res = IOTCON_RESPONSE_RESULT_ERROR; } @@ -256,11 +260,9 @@ namespace icIotivityHandler { if (OC_STACK_OK == eCode) { res = IOTCON_RESPONSE_RESULT_OK; - } - else if (OC_STACK_RESOURCE_CREATED == eCode) { + } else if (OC_STACK_RESOURCE_CREATED == eCode) { res = IOTCON_RESPONSE_RESULT_RESOURCE_CREATED; - } - else { + } else { ERR("post() Fail(%d)", eCode); res = IOTCON_RESPONSE_RESULT_ERROR; } @@ -301,11 +303,9 @@ namespace icIotivityHandler { if (OC_STACK_OK == eCode) { res = IOTCON_RESPONSE_RESULT_OK; - } - else if (OC_STACK_RESOURCE_DELETED == eCode) { + } else if (OC_STACK_RESOURCE_DELETED == eCode) { res = IOTCON_RESPONSE_RESULT_RESOURCE_DELETED; - } - else { + } else { ERR("deleteResource() Fail(%d)", eCode); res = IOTCON_RESPONSE_RESULT_ERROR; } @@ -346,8 +346,7 @@ namespace icIotivityHandler { if (OC_STACK_OK == eCode) { res = IOTCON_RESPONSE_RESULT_OK; - } - else { + } else { ERR("observe() Fail(%d)", eCode); res = IOTCON_RESPONSE_RESULT_ERROR; } @@ -543,8 +542,8 @@ static OCEntityHandlerResult _ic_ioty_request_handler( return OC_EH_ERROR; } - request_s.request_handle = (iotcon_request_h)request->getRequestHandle(); - request_s.resource_handle = (iotcon_client_h)request->getResourceHandle(); + request_s.request_handle = request->getRequestHandle(); + request_s.resource_handle = request->getResourceHandle(); ObservationInfo observationInfo = request->getObservationInfo(); request_s.observation_info.action = (iotcon_observe_action_e)observationInfo.action; @@ -552,12 +551,10 @@ static OCEntityHandlerResult _ic_ioty_request_handler( DBG("obs_info.obsId=%d", observationInfo.obsId); /* call handler_cb */ - if (temp_res->cb) { + if (temp_res->cb) temp_res->cb(&request_s, temp_res->user_data); - } - else { + else WARN("temp_res->request_handler_cb is null"); - } free(request_s.uri); @@ -591,16 +588,13 @@ extern "C" OCResourceHandle ic_ioty_register_res(const char *uri, if (IOTCON_INTERFACE_DEFAULT & ifaces) { resInterface = DEFAULT_INTERFACE; ifaces ^= IOTCON_INTERFACE_DEFAULT; - } - else if (IOTCON_INTERFACE_LINK & ifaces) { + } else if (IOTCON_INTERFACE_LINK & ifaces) { resInterface = LINK_INTERFACE; ifaces ^= IOTCON_INTERFACE_LINK; - } - else if (IOTCON_INTERFACE_BATCH & ifaces) { + } else if (IOTCON_INTERFACE_BATCH & ifaces) { resInterface = BATCH_INTERFACE; ifaces ^= IOTCON_INTERFACE_BATCH; - } - else if (IOTCON_INTERFACE_GROUP & ifaces) { + } else if (IOTCON_INTERFACE_GROUP & ifaces) { resInterface = GROUP_INTERFACE; ifaces ^= IOTCON_INTERFACE_GROUP; } @@ -635,7 +629,7 @@ extern "C" int ic_ioty_unregister_res(iotcon_resource_h resource_handle) OCResourceHandle resourceHandle = resource_handle; OCStackResult result = unregisterResource(resourceHandle); - if(OC_STACK_OK != result) { + if (OC_STACK_OK != result) { ERR("unregisterResource Fail(%d)", result); return IOTCON_ERROR_IOTIVITY; } @@ -652,17 +646,13 @@ extern "C" int ic_ioty_convert_interface_string(const char *src, iotcon_interfac if (IC_STR_EQUAL == DEFAULT_INTERFACE.compare(interface_str)) { *dest = IOTCON_INTERFACE_DEFAULT; - } - else if (IC_STR_EQUAL == LINK_INTERFACE.compare(interface_str)) { + } else if (IC_STR_EQUAL == LINK_INTERFACE.compare(interface_str)) { *dest = IOTCON_INTERFACE_LINK; - } - else if (IC_STR_EQUAL == BATCH_INTERFACE.compare(interface_str)) { + } else if (IC_STR_EQUAL == BATCH_INTERFACE.compare(interface_str)) { *dest = IOTCON_INTERFACE_BATCH; - } - else if (IC_STR_EQUAL == GROUP_INTERFACE.compare(interface_str)) { + } else if (IC_STR_EQUAL == GROUP_INTERFACE.compare(interface_str)) { *dest = IOTCON_INTERFACE_GROUP; - } - else { + } else { ERR("Invalid interface"); *dest = IOTCON_INTERFACE_NONE; return IOTCON_ERROR_INVALID_PARAMETER; @@ -810,7 +800,7 @@ extern "C" int ic_ioty_get_device_info(const char *host_address, iotcon_device_info_cb cb, void *user_data) { OCStackResult ret; - string resHost = host_address + string(IC_DEVICE_DISCOVERY); + string resHost = string(IC_COAP) + host_address + string(IC_DEVICE_DISCOVERY); shared_ptr object = make_shared(cb, user_data); @@ -829,8 +819,8 @@ extern "C" int ic_ioty_get_device_info(const char *host_address, } -extern "C" int ic_ioty_send_notify(OCResourceHandle resHandle, struct ic_notify_msg *msg, - iotcon_observers_h observers) +extern "C" int ic_ioty_notify_list_of_observers(OCResourceHandle resHandle, + struct ic_notify_msg *msg, iotcon_observers_h observers) { int ret; OCStackResult ocRet; @@ -838,7 +828,7 @@ extern "C" int ic_ioty_send_notify(OCResourceHandle resHandle, struct ic_notify_ OCRepresentation ocRep; string iface; - GList *node = g_list_first((GList*)observers); + GList *node = g_list_first((GList *)observers); while (node) { int obs_id = GPOINTER_TO_UINT(node->data); obsIds.push_back(obs_id); @@ -847,29 +837,34 @@ extern "C" int ic_ioty_send_notify(OCResourceHandle resHandle, struct ic_notify_ } shared_ptr resourceResponse(new OCResourceResponse()); - resourceResponse->setErrorCode(msg->error_code); - ret = ic_ioty_repr_parse(msg->repr, ocRep); - if (IOTCON_ERROR_NONE != ret) { - ERR("ic_ioty_repr_parse() Fail(%d)", ret); - return ret; - } + if (msg) { + resourceResponse->setErrorCode(msg->error_code); - if (IOTCON_INTERFACE_NONE != msg->iface) { - ret = _ic_ioty_convert_interface_flag(msg->iface, iface); + ret = ic_ioty_repr_parse(msg->repr, ocRep); if (IOTCON_ERROR_NONE != ret) { - ERR("_ic_ioty_convert_interface_flag(%d) Fail(%d)", msg->iface, ret); + ERR("ic_ioty_repr_parse() Fail(%d)", ret); return ret; } - } - else { - iface = DEFAULT_INTERFACE; - } - resourceResponse->setResourceRepresentation(ocRep, iface); + if (IOTCON_INTERFACE_NONE != msg->iface) { + ret = _ic_ioty_convert_interface_flag(msg->iface, iface); + if (IOTCON_ERROR_NONE != ret) { + ERR("_ic_ioty_convert_interface_flag(%d) Fail(%d)", msg->iface, ret); + return ret; + } + } else { + iface = DEFAULT_INTERFACE; + } + + resourceResponse->setResourceRepresentation(ocRep, iface); + } ocRet = notifyListOfObservers(resHandle, obsIds, resourceResponse); - if (OC_STACK_OK != ocRet) { + if (OC_STACK_NO_OBSERVERS == ocRet) { + WARN("No observers. Stop notifying"); + return IOTCON_ERROR_NONE; + } else if (OC_STACK_OK != ocRet) { ERR("notifyListOfObservers() Fail(%d)", ocRet); return IOTCON_ERROR_IOTIVITY; } @@ -878,6 +873,23 @@ extern "C" int ic_ioty_send_notify(OCResourceHandle resHandle, struct ic_notify_ } +extern "C" int ic_ioty_notify_all(OCResourceHandle resHandle) +{ + OCStackResult ocRet; + + ocRet = notifyAllObservers(resHandle); + if (OC_STACK_NO_OBSERVERS == ocRet) { + WARN("No observers. Stop notifying"); + return IOTCON_ERROR_NONE; + } else if (OC_STACK_OK != ocRet) { + ERR("notifyAllObservers() Fail(%d)", ocRet); + return IOTCON_ERROR_IOTIVITY; + } + + return IOTCON_ERROR_NONE; +} + + extern "C" int ic_ioty_send_res_response_data(struct ic_resource_response *resp) { FN_CALL; @@ -905,8 +917,7 @@ extern "C" int ic_ioty_send_res_response_data(struct ic_resource_response *resp) ERR("_ic_ioty_convert_interface_flag(%d) Fail(%d)", resp->iface, ret); return ret; } - } - else { + } else { iface = DEFAULT_INTERFACE; } @@ -932,6 +943,7 @@ extern "C" const iotcon_presence_h ic_ioty_subscribe_presence(const char *host_a { OCStackResult ret; iotcon_presence_h presence_handle = NULL; + string host; shared_ptr object = make_shared(cb, user_data); @@ -939,8 +951,8 @@ extern "C" const iotcon_presence_h ic_ioty_subscribe_presence(const char *host_a = bind(&icIotivityHandler::presenceObject::presenceHandler, object, placeholders::_1, placeholders::_2, placeholders::_3); - ret = subscribePresence(presence_handle, host_address, resource_type, - subscribeCallback); + host = string(IC_COAP) + string(host_address); + ret = subscribePresence(presence_handle, host, resource_type, subscribeCallback); if (OC_STACK_OK != ret) { ERR("subscribePresence() Fail(%d)", ret); @@ -995,6 +1007,8 @@ extern "C" int ic_ioty_find_resource(const char *host_address, const char *resou OCStackResult ret; ostringstream resource_name; + resource_name << IC_COAP; + if (IC_STR_EQUAL == strcmp(IOTCON_MULTICAST_ADDRESS, host_address)) resource_name << host_address << IC_MULTICAST_RESOURCE_DISCOVERY; else @@ -1051,16 +1065,15 @@ static OCResource::Ptr _ic_ioty_create_oc_resource(iotcon_client_h resource) RETV_IF(NULL == resource->uri, NULL); RETV_IF(NULL == resource->types, NULL); - host = resource->host; + host = string(IC_COAP) + string(resource->host); uri = resource->uri; iotcon_resource_types_foreach(resource->types, _ic_ioty_accumulate_res_types, - (void*)&resource_types); + (void *)&resource_types); if (IOTCON_INTERFACE_NONE == resource->ifaces) { resource_ifs.push_back(DEFAULT_INTERFACE); - } - else { + } else { if (IOTCON_INTERFACE_DEFAULT & resource->ifaces) resource_ifs.push_back(DEFAULT_INTERFACE); @@ -1079,7 +1092,7 @@ static OCResource::Ptr _ic_ioty_create_oc_resource(iotcon_client_h resource) if (resource->header_options) { iotcon_options_foreach(resource->header_options, - _ic_ioty_accumulate_options_vector, (void*)&header_options); + _ic_ioty_accumulate_options_vector, (void *)&header_options); ocResource->setHeaderOptions(header_options); } @@ -1137,7 +1150,7 @@ extern "C" int ic_ioty_put(iotcon_client_h resource, iotcon_repr_h repr, QueryParamsMap queryParams; if (query) - iotcon_query_foreach(query, _ic_ioty_accumulate_query_map, (void*)&queryParams); + iotcon_query_foreach(query, _ic_ioty_accumulate_query_map, (void *)&queryParams); ret = ic_ioty_repr_parse(repr, ocRep); if (IOTCON_ERROR_NONE != ret) { @@ -1172,7 +1185,7 @@ extern "C" int ic_ioty_post(iotcon_client_h resource, iotcon_repr_h repr, OCResource::Ptr ocResource; if (query) - iotcon_query_foreach(query, _ic_ioty_accumulate_query_map, (void*)&queryParams); + iotcon_query_foreach(query, _ic_ioty_accumulate_query_map, (void *)&queryParams); ret = ic_ioty_repr_parse(repr, ocRep); if (IOTCON_ERROR_NONE != ret) { @@ -1231,15 +1244,13 @@ extern "C" int ic_ioty_observe(iotcon_client_h resource, QueryParamsMap queryParams; if (query) - iotcon_query_foreach(query, _ic_ioty_accumulate_query_map, (void*)&queryParams); + iotcon_query_foreach(query, _ic_ioty_accumulate_query_map, (void *)&queryParams); if (IOTCON_OBSERVE == observe_type) { observeType = ObserveType::Observe; - } - else if (IOTCON_OBSERVE_ALL == observe_type) { + } else if (IOTCON_OBSERVE_ALL == observe_type) { observeType = ObserveType::ObserveAll; - } - else { + } else { ERR("Invalid observe_type"); return IOTCON_ERROR_INVALID_PARAMETER; } @@ -1248,7 +1259,7 @@ extern "C" int ic_ioty_observe(iotcon_client_h resource, resource_handle *obs_h = new resource_handle(); obs_h->ocResource = ocResource; - resource->observe_handle = (void*)obs_h; + resource->observe_handle = (void *)obs_h; shared_ptr object = make_shared(cb, user_data); @@ -1273,7 +1284,7 @@ extern "C" int ic_ioty_cancel_observe(iotcon_client_h resource) delete (resource_handle *)resource->observe_handle; resource->observe_handle = NULL; - ret = ocResource->cancelObserve(); + ret = ocResource->cancelObserve(QualityOfService::HighQos); if (OC_STACK_OK != ret) { ERR("cancelObserve() Fail(%d)", ret); return IOTCON_ERROR_IOTIVITY; diff --git a/lib/icl-ioty.h b/lib/icl-ioty.h index fa51691..2d28546 100644 --- a/lib/icl-ioty.h +++ b/lib/icl-ioty.h @@ -59,8 +59,11 @@ int ic_ioty_register_device_info(iotcon_device_info_s device_info); int ic_ioty_get_device_info(const char *host_address, iotcon_device_info_cb cb, void *user_data); -int ic_ioty_send_notify(void *resource, struct ic_notify_msg *msg, +int ic_ioty_notify_list_of_observers(void *resource, struct ic_notify_msg *msg, iotcon_observers_h observers); + +int ic_ioty_notify_all(void *resource); + int ic_ioty_send_res_response_data(struct ic_resource_response *resp); const iotcon_presence_h ic_ioty_subscribe_presence(const char *host_address, diff --git a/lib/icl-repr-list.c b/lib/icl-repr-list.c index 1810d92..999e1d5 100644 --- a/lib/icl-repr-list.c +++ b/lib/icl-repr-list.c @@ -374,15 +374,13 @@ static int _ic_list_del_nth_value(iotcon_list_h list, int pos, iotcon_types_e va } if (IOTCON_TYPE_STR == value->type) { - ic_basic_s *real = (ic_basic_s*)value; + ic_basic_s *real = (ic_basic_s *)value; free(real->val.s); - } - else if (IOTCON_TYPE_LIST == value->type) { - ic_val_list_s *real = (ic_val_list_s*)value; + } else if (IOTCON_TYPE_LIST == value->type) { + ic_val_list_s *real = (ic_val_list_s *)value; iotcon_list_free(real->list); - } - else if (IOTCON_TYPE_REPR == value->type) { - ic_val_repr_s *real = (ic_val_repr_s*)value; + } else if (IOTCON_TYPE_REPR == value->type) { + ic_val_repr_s *real = (ic_val_repr_s *)value; iotcon_repr_free(real->repr); } @@ -663,8 +661,8 @@ static iotcon_value_h _ic_list_get_nth_value(iotcon_list_h list, int pos) /* -* A general result : [1,2,3] -*/ + * A general result : [1,2,3] + */ JsonArray* ic_list_to_json(iotcon_list_h list) { int i, ret, count; @@ -742,8 +740,8 @@ JsonArray* ic_list_to_json(iotcon_list_h list) } /* -* A general input : [1,2,3] -*/ + * A general input : [1,2,3] + */ iotcon_list_h ic_list_from_json(JsonArray *parray) { int i, ret; @@ -765,7 +763,7 @@ iotcon_list_h ic_list_from_json(JsonArray *parray) return NULL; } - ic_basic_s *real = (ic_basic_s*)value; + ic_basic_s *real = (ic_basic_s *)value; if (IOTCON_TYPE_NONE != list->type && list->type != real->type) { ERR("Type matching Fail(list:%d,value:%d)", list->type, real->type); ic_value_free(value); @@ -781,8 +779,7 @@ iotcon_list_h ic_list_from_json(JsonArray *parray) return NULL; } list->type = real->type; - } - else if (JSON_NODE_HOLDS_ARRAY(child_node)) { + } else if (JSON_NODE_HOLDS_ARRAY(child_node)) { if (IOTCON_TYPE_NONE != list->type && IOTCON_TYPE_LIST != list->type) { ERR("Type matching Fail(%d)", list->type); iotcon_list_free(list); @@ -813,8 +810,7 @@ iotcon_list_h ic_list_from_json(JsonArray *parray) return NULL; } list->type = IOTCON_TYPE_LIST; - } - else if (JSON_NODE_HOLDS_OBJECT(child_node)) { + } else if (JSON_NODE_HOLDS_OBJECT(child_node)) { if (IOTCON_TYPE_NONE != list->type && IOTCON_TYPE_REPR != list->type) { ERR("Type matching Fail(%d)", list->type); iotcon_list_free(list); @@ -998,38 +994,38 @@ iotcon_list_h ic_list_clone(iotcon_list_h list) } switch (list->type) { - case IOTCON_TYPE_INT: - case IOTCON_TYPE_BOOL: - case IOTCON_TYPE_DOUBLE: - case IOTCON_TYPE_STR: - case IOTCON_TYPE_NULL: - ret = _ic_list_clone_value(list, ret_list); - if (IOTCON_ERROR_NONE != ret) { - ERR("_ic_list_clone_value() Fail(%d)", ret); - iotcon_list_free(ret_list); - return NULL; - } - break; - case IOTCON_TYPE_LIST: - ret = _ic_list_clone_list(list, ret_list); - if (IOTCON_ERROR_NONE != ret) { - ERR("_ic_list_clone_list() Fail(%d)", ret); - iotcon_list_free(ret_list); - return NULL; - } - break; - case IOTCON_TYPE_REPR: - ret = _ic_list_clone_repr(list, ret_list); - if (IOTCON_ERROR_NONE != ret) { - ERR("_ic_list_clone_repr() Fail(%d)", ret); - iotcon_list_free(ret_list); - return NULL; - } - break; - default: - ERR("Invalid type(%d)", list->type); + case IOTCON_TYPE_INT: + case IOTCON_TYPE_BOOL: + case IOTCON_TYPE_DOUBLE: + case IOTCON_TYPE_STR: + case IOTCON_TYPE_NULL: + ret = _ic_list_clone_value(list, ret_list); + if (IOTCON_ERROR_NONE != ret) { + ERR("_ic_list_clone_value() Fail(%d)", ret); + iotcon_list_free(ret_list); + return NULL; + } + break; + case IOTCON_TYPE_LIST: + ret = _ic_list_clone_list(list, ret_list); + if (IOTCON_ERROR_NONE != ret) { + ERR("_ic_list_clone_list() Fail(%d)", ret); iotcon_list_free(ret_list); return NULL; + } + break; + case IOTCON_TYPE_REPR: + ret = _ic_list_clone_repr(list, ret_list); + if (IOTCON_ERROR_NONE != ret) { + ERR("_ic_list_clone_repr() Fail(%d)", ret); + iotcon_list_free(ret_list); + return NULL; + } + break; + default: + ERR("Invalid type(%d)", list->type); + iotcon_list_free(ret_list); + return NULL; } return ret_list; diff --git a/lib/icl-repr-obj.c b/lib/icl-repr-obj.c index be9db54..d9a4390 100644 --- a/lib/icl-repr-obj.c +++ b/lib/icl-repr-obj.c @@ -68,7 +68,7 @@ API int iotcon_repr_get_int(iotcon_repr_h repr, const char *key, int *val) return IOTCON_ERROR_NO_DATA; } - ic_basic_s *real = (ic_basic_s*)value; + ic_basic_s *real = (ic_basic_s *)value; if (IOTCON_TYPE_INT != real->type) { ERR("Invalid Type(%d)", real->type); return IOTCON_ERROR_INVALID_TYPE; @@ -126,7 +126,7 @@ API int iotcon_repr_get_bool(iotcon_repr_h repr, const char *key, bool *val) return IOTCON_ERROR_NO_DATA; } - real = (ic_basic_s*)value; + real = (ic_basic_s *)value; if (IOTCON_TYPE_BOOL != real->type) { ERR("Invalid Type(%d)", real->type); return IOTCON_ERROR_INVALID_TYPE; @@ -181,7 +181,7 @@ API int iotcon_repr_get_double(iotcon_repr_h repr, const char *key, double *val) return IOTCON_ERROR_NO_DATA; } - real = (ic_basic_s*)value; + real = (ic_basic_s *)value; if (IOTCON_TYPE_DOUBLE != real->type) { ERR("Invalid Type(%d)", real->type); return IOTCON_ERROR_INVALID_TYPE; @@ -239,7 +239,7 @@ API int iotcon_repr_get_str(iotcon_repr_h repr, const char *key, char **val) return IOTCON_ERROR_NO_DATA; } - real = (ic_basic_s*)value; + real = (ic_basic_s *)value; if (IOTCON_TYPE_STR != real->type) { ERR("Invalid Type(%d)", real->type); return IOTCON_ERROR_INVALID_TYPE; @@ -297,7 +297,7 @@ API bool iotcon_repr_is_null(iotcon_repr_h repr, const char *key) return false; } - real = (ic_basic_s*)value; + real = (ic_basic_s *)value; return (IOTCON_TYPE_NULL == real->type) ? true : false; } @@ -349,7 +349,7 @@ API int iotcon_repr_get_list(iotcon_repr_h repr, const char *key, iotcon_list_h return IOTCON_ERROR_NO_DATA; } - real = (ic_val_list_s*)value; + real = (ic_val_list_s *)value; if (IOTCON_TYPE_LIST != real->type) { ERR("Invalid Type(%d)", real->type); return IOTCON_ERROR_INVALID_TYPE; @@ -410,7 +410,7 @@ API int iotcon_repr_get_repr(iotcon_repr_h src, const char *key, iotcon_repr_h * return IOTCON_ERROR_NO_DATA; } - real = (ic_val_repr_s*)value; + real = (ic_val_repr_s *)value; if (IOTCON_TYPE_REPR != real->type) { ERR("Invalid Type(%d)", real->type); return IOTCON_ERROR_INVALID_TYPE; @@ -616,8 +616,7 @@ static inline int _ic_obj_from_json(JsonObject *obj, GList *key_list, unsigned i return IOTCON_ERROR_INVALID_PARAMETER; } ic_obj_set_value(ret_repr, key, value); - } - else if (JSON_NODE_HOLDS_ARRAY(child_node)) { + } else if (JSON_NODE_HOLDS_ARRAY(child_node)) { iotcon_value_h value; JsonArray *child_array = json_node_get_array(child_node); @@ -635,8 +634,7 @@ static inline int _ic_obj_from_json(JsonObject *obj, GList *key_list, unsigned i } ic_obj_set_value(ret_repr, key, value); - } - else if (JSON_NODE_HOLDS_OBJECT(child_node)) { + } else if (JSON_NODE_HOLDS_OBJECT(child_node)) { iotcon_value_h value; JsonObject *child_obj = json_node_get_object(child_node); @@ -654,8 +652,7 @@ static inline int _ic_obj_from_json(JsonObject *obj, GList *key_list, unsigned i } ic_obj_set_value(ret_repr, key, value); - } - else { + } else { ERR("node type(%d) Fail", json_node_get_node_type(child_node)); return IOTCON_ERROR_INVALID_PARAMETER; } diff --git a/lib/icl-repr-value.c b/lib/icl-repr-value.c index f2e63d2..45a6bd3 100644 --- a/lib/icl-repr-value.c +++ b/lib/icl-repr-value.c @@ -77,7 +77,7 @@ iotcon_value_h ic_value_new_int(int val) { ic_basic_s *value; - value = (ic_basic_s*)_ic_value_new(IOTCON_TYPE_INT); + value = (ic_basic_s *)_ic_value_new(IOTCON_TYPE_INT); if (NULL == value) { ERR("_ic_value_new(INT:%d) Fail", val); return NULL; @@ -92,7 +92,7 @@ iotcon_value_h ic_value_new_bool(bool val) { ic_basic_s *value; - value = (ic_basic_s*)_ic_value_new(IOTCON_TYPE_BOOL); + value = (ic_basic_s *)_ic_value_new(IOTCON_TYPE_BOOL); if (NULL == value) { ERR("_ic_value_new(BOOL:%d) Fail", val); return NULL; @@ -107,7 +107,7 @@ iotcon_value_h ic_value_new_double(double val) { ic_basic_s *value; - value = (ic_basic_s*)_ic_value_new(IOTCON_TYPE_DOUBLE); + value = (ic_basic_s *)_ic_value_new(IOTCON_TYPE_DOUBLE); if (NULL == value) { ERR("_ic_value_new(DOUBLE:%f) Fail", val); return NULL; @@ -124,7 +124,7 @@ iotcon_value_h ic_value_new_str(char *val) RETV_IF(NULL == val, NULL); - value = (ic_basic_s*)_ic_value_new(IOTCON_TYPE_STR); + value = (ic_basic_s *)_ic_value_new(IOTCON_TYPE_STR); if (NULL == value) { ERR("_ic_value_new(STR:%s) Fail", val); return NULL; @@ -140,7 +140,7 @@ iotcon_value_h ic_value_new_list(iotcon_list_h val) { ic_val_list_s *value; - value = (ic_val_list_s*)_ic_value_new(IOTCON_TYPE_LIST); + value = (ic_val_list_s *)_ic_value_new(IOTCON_TYPE_LIST); if (NULL == value) { ERR("_ic_value_new(LIST) Fail"); return NULL; @@ -155,7 +155,7 @@ iotcon_value_h ic_value_new_repr(iotcon_repr_h val) { ic_val_repr_s *value; - value = (ic_val_repr_s*)_ic_value_new(IOTCON_TYPE_REPR); + value = (ic_val_repr_s *)_ic_value_new(IOTCON_TYPE_REPR); if (NULL == value) { ERR("_ic_value_new(REPR) Fail"); return NULL; @@ -168,7 +168,7 @@ iotcon_value_h ic_value_new_repr(iotcon_repr_h val) int ic_value_get_int(iotcon_value_h value, int *val) { - ic_basic_s *real = (ic_basic_s*)value; + ic_basic_s *real = (ic_basic_s *)value; RETV_IF(NULL == value, IOTCON_ERROR_INVALID_PARAMETER); RETVM_IF(IOTCON_TYPE_INT != real->type, IOTCON_ERROR_INVALID_PARAMETER, @@ -181,7 +181,7 @@ int ic_value_get_int(iotcon_value_h value, int *val) int ic_value_get_bool(iotcon_value_h value, bool *val) { - ic_basic_s *real = (ic_basic_s*)value; + ic_basic_s *real = (ic_basic_s *)value; RETV_IF(NULL == value, IOTCON_ERROR_INVALID_PARAMETER); RETVM_IF(IOTCON_TYPE_BOOL != real->type, IOTCON_ERROR_INVALID_PARAMETER, @@ -194,7 +194,7 @@ int ic_value_get_bool(iotcon_value_h value, bool *val) int ic_value_get_double(iotcon_value_h value, double *val) { - ic_basic_s *real = (ic_basic_s*)value; + ic_basic_s *real = (ic_basic_s *)value; RETV_IF(NULL == value, IOTCON_ERROR_INVALID_PARAMETER); RETVM_IF(IOTCON_TYPE_DOUBLE != real->type, IOTCON_ERROR_INVALID_PARAMETER, @@ -207,7 +207,7 @@ int ic_value_get_double(iotcon_value_h value, double *val) int ic_value_get_str(iotcon_value_h value, const char **val) { - ic_basic_s *real = (ic_basic_s*)value; + ic_basic_s *real = (ic_basic_s *)value; RETV_IF(NULL == value, IOTCON_ERROR_INVALID_PARAMETER); RETVM_IF(IOTCON_TYPE_STR != real->type, IOTCON_ERROR_INVALID_PARAMETER, @@ -221,7 +221,7 @@ int ic_value_get_str(iotcon_value_h value, const char **val) int ic_value_get_list(iotcon_value_h value, iotcon_list_h *list) { - ic_val_list_s *real = (ic_val_list_s*)value; + ic_val_list_s *real = (ic_val_list_s *)value; RETV_IF(NULL == value, IOTCON_ERROR_INVALID_PARAMETER); RETVM_IF(IOTCON_TYPE_LIST != real->type, IOTCON_ERROR_INVALID_PARAMETER, @@ -234,7 +234,7 @@ int ic_value_get_list(iotcon_value_h value, iotcon_list_h *list) int ic_value_get_repr(iotcon_value_h value, iotcon_repr_h *repr) { - ic_val_repr_s *real = (ic_val_repr_s*)value; + ic_val_repr_s *real = (ic_val_repr_s *)value; RETV_IF(NULL == value, IOTCON_ERROR_INVALID_PARAMETER); RETVM_IF(IOTCON_TYPE_REPR != real->type, IOTCON_ERROR_INVALID_PARAMETER, @@ -246,15 +246,15 @@ int ic_value_get_repr(iotcon_value_h value, iotcon_repr_h *repr) } /* -* A general result : 1 -* : true -* : 5.5 -* : "Hello" -*/ + * A general result : 1 + * : true + * : 5.5 + * : "Hello" + */ JsonNode* ic_value_to_json(iotcon_value_h value) { JsonNode *node; - ic_basic_s *real = (ic_basic_s*)value; + ic_basic_s *real = (ic_basic_s *)value; RETV_IF(NULL == value, NULL); @@ -292,11 +292,11 @@ JsonNode* ic_value_to_json(iotcon_value_h value) } /* -* A general result : 1 -* : true -* : 5.5 -* : "Hello" -*/ + * A general result : 1 + * : true + * : 5.5 + * : "Hello" + */ API iotcon_value_h ic_value_from_json(JsonNode *node) { gint64 ival64; @@ -314,34 +314,34 @@ API iotcon_value_h ic_value_from_json(JsonNode *node) gtype = json_node_get_value_type(node); switch (gtype) { - case G_TYPE_INT64: - ival64 = json_node_get_int(node); - if (INT_MAX < ival64 || ival64 < INT_MIN) { - ERR("value SHOULD NOT exceeds the integer range. ival64(%lld)", ival64); - return NULL; - } - value = ic_value_new_int(ival64); - if (NULL == value) - ERR("ic_value_new_int(%ll) Fail", ival64); - break; - case G_TYPE_BOOLEAN: - value = ic_value_new_bool(json_node_get_boolean(node)); - if (NULL == value) - ERR("ic_value_new_bool() Fail"); - break; - case G_TYPE_DOUBLE: - value = ic_value_new_double(json_node_get_double(node)); - if (NULL == value) - ERR("ic_value_new_double() Fail"); - break; - case G_TYPE_STRING: - value = ic_value_new_str(ic_utils_strdup(json_node_get_string(node))); - if (NULL == value) - ERR("ic_value_new_str() Fail"); - break; - default: - ERR("Invalid type(%d)", gtype); - break; + case G_TYPE_INT64: + ival64 = json_node_get_int(node); + if (INT_MAX < ival64 || ival64 < INT_MIN) { + ERR("value SHOULD NOT exceeds the integer range. ival64(%lld)", ival64); + return NULL; + } + value = ic_value_new_int(ival64); + if (NULL == value) + ERR("ic_value_new_int(%ll) Fail", ival64); + break; + case G_TYPE_BOOLEAN: + value = ic_value_new_bool(json_node_get_boolean(node)); + if (NULL == value) + ERR("ic_value_new_bool() Fail"); + break; + case G_TYPE_DOUBLE: + value = ic_value_new_double(json_node_get_double(node)); + if (NULL == value) + ERR("ic_value_new_double() Fail"); + break; + case G_TYPE_STRING: + value = ic_value_new_str(ic_utils_strdup(json_node_get_string(node))); + if (NULL == value) + ERR("ic_value_new_str() Fail"); + break; + default: + ERR("Invalid type(%d)", gtype); + break; } return value; @@ -351,7 +351,6 @@ void ic_value_free(gpointer data) { FN_CALL; int ret; - const char *str; iotcon_value_h value; iotcon_list_h list; iotcon_repr_h repr; @@ -363,12 +362,7 @@ void ic_value_free(gpointer data) int type = value->type; switch (type) { case IOTCON_TYPE_STR: - ret = ic_value_get_str(value, &str); - if (IOTCON_ERROR_NONE != ret) { - ERR("ic_value_get_str() Fail(%d)", ret); - break; - } - free((char*)str); + free(((ic_basic_s *)value)->val.s); case IOTCON_TYPE_INT: case IOTCON_TYPE_BOOL: case IOTCON_TYPE_DOUBLE: @@ -403,7 +397,7 @@ void ic_value_free(gpointer data) iotcon_value_h ic_value_clone(iotcon_value_h src) { iotcon_value_h dest = NULL; - ic_basic_s *real = (ic_basic_s*)src; + ic_basic_s *real = (ic_basic_s *)src; RETV_IF(NULL == src, NULL); diff --git a/lib/icl-repr.c b/lib/icl-repr.c index dd56c57..f264d70 100644 --- a/lib/icl-repr.c +++ b/lib/icl-repr.c @@ -92,7 +92,7 @@ API int iotcon_repr_set_uri(iotcon_repr_h repr, const char *uri) repr->uri = NULL; if (NULL == uri) - return IOTCON_ERROR_NONE; + return IOTCON_ERROR_INVALID_PARAMETER; repr->uri = strdup(uri); if (NULL == repr->uri) { @@ -256,8 +256,7 @@ static JsonObject* _ic_repr_data_generate_json(iotcon_repr_h cur_repr, json_object_unref(repr_obj); return NULL; } - } - else { + } else { repr_obj = json_object_new(); } @@ -363,9 +362,8 @@ static JsonObject* _ic_repr_generate_json(iotcon_repr_h repr) root_obj = json_object_new(); root_array = json_array_new(); - if (repr->children) { + if (repr->children) child_count = iotcon_repr_get_children_count(repr); - } repr_obj = _ic_repr_data_generate_parent(repr, child_index); if (NULL == repr_obj) { @@ -405,7 +403,7 @@ char* ic_repr_generate_json(iotcon_repr_h repr, bool set_pretty) } JsonGenerator *gen = json_generator_new(); -#if JSON_CHECK_VERSION(0,14,0) +#if JSON_CHECK_VERSION(0, 14, 0) json_generator_set_pretty(gen, set_pretty); #endif @@ -455,8 +453,7 @@ iotcon_repr_h ic_repr_parse_json(const char *json_string) g_object_unref(parser); return NULL; } - } - else { + } else { repr = iotcon_repr_new(); } diff --git a/lib/icl-request.h b/lib/icl-request.h index 17629fa..51f974c 100644 --- a/lib/icl-request.h +++ b/lib/icl-request.h @@ -32,7 +32,6 @@ struct ic_resource_request { char *uri; iotcon_options_h header_options; iotcon_query_h query; - int request_handler_flag; struct ic_observe_info observation_info; iotcon_repr_h repr; oc_request_h request_handle; diff --git a/lib/icl-resource-types.c b/lib/icl-resource-types.c index bee4254..6166e73 100644 --- a/lib/icl-resource-types.c +++ b/lib/icl-resource-types.c @@ -149,7 +149,7 @@ API int iotcon_resource_types_foreach(iotcon_resource_types_h types, RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER); for (node = types->type_list; node; node = node->next) { - if (IOTCON_FUNC_STOP == cb((const char*)node->data, user_data)) + if (IOTCON_FUNC_STOP == cb((const char *)node->data, user_data)) break; } @@ -159,7 +159,7 @@ API int iotcon_resource_types_foreach(iotcon_resource_types_h types, API iotcon_resource_types_h iotcon_resource_types_clone(iotcon_resource_types_h types) { - GList * node; + GList *node; char *resource_type; iotcon_resource_types_h clone; diff --git a/lib/icl.c b/lib/icl.c index 59e66d3..d129f32 100644 --- a/lib/icl.c +++ b/lib/icl.c @@ -48,7 +48,7 @@ static void _free_resource(gpointer data) free(resource); } -/* Host address should begin with "coap://" */ + API void iotcon_initialize(const char *addr, unsigned short port) { FN_CALL; @@ -60,7 +60,7 @@ API void iotcon_initialize(const char *addr, unsigned short port) ic_request_cb_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, _free_resource); -#if !GLIB_CHECK_VERSION(2,35,0) +#if !GLIB_CHECK_VERSION(2, 35, 0) g_type_init(); #endif ic_is_init = true; @@ -239,9 +239,9 @@ API int iotcon_unbind_resource(iotcon_resource_h parent, iotcon_resource_h child if (child == parent->children[i]) parent->children[i] = NULL; } - } - else + } else { ERR("ic_ioty_unbind_res() Fail(%d)", ret); + } return ret; } @@ -293,18 +293,6 @@ API int iotcon_resource_get_uri(iotcon_resource_h resource, char **uri) /* The content of the resource should not be freed by user. */ -API int iotcon_resource_get_host(iotcon_resource_h resource, char **host) -{ - RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER); - RETV_IF(NULL == host, IOTCON_ERROR_INVALID_PARAMETER); - - *host = resource->host; - - return IOTCON_ERROR_NONE; -} - - -/* The content of the resource should not be freed by user. */ API int iotcon_resource_get_types(iotcon_resource_h resource, iotcon_resource_types_h *types) { RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER); @@ -372,7 +360,7 @@ API iotcon_presence_h iotcon_subscribe_presence(const char *host_address, RETV_IF(NULL == host_address, NULL); RETV_IF(NULL == cb, NULL); - if (resource_type &&(IOTCON_RESOURCE_TYPE_LENGTH_MAX < strlen(resource_type))) { + if (resource_type && (IOTCON_RESOURCE_TYPE_LENGTH_MAX < strlen(resource_type))) { ERR("The length of resource_type(%s) is invalid", resource_type); return NULL; } @@ -416,6 +404,7 @@ API iotcon_notimsg_h iotcon_notimsg_new(iotcon_repr_h repr, iotcon_interface_e i } msg->repr = repr; + ic_repr_inc_ref_count(msg->repr); msg->iface = iface; msg->error_code = 200; @@ -432,19 +421,31 @@ API void iotcon_notimsg_free(iotcon_notimsg_h msg) } -API int iotcon_notify(iotcon_resource_h resource, iotcon_notimsg_h msg, +API int iotcon_notify_list_of_observers(iotcon_resource_h resource, iotcon_notimsg_h msg, iotcon_observers_h observers) { int ret; RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER); RETV_IF(NULL == observers, IOTCON_ERROR_INVALID_PARAMETER); - RETV_IF(NULL == msg, IOTCON_ERROR_INVALID_PARAMETER); - RETV_IF(NULL == msg->repr, IOTCON_ERROR_INVALID_PARAMETER); - ret = ic_ioty_send_notify(resource->handle, msg, observers); + ret = ic_ioty_notify_list_of_observers(resource->handle, msg, observers); if (IOTCON_ERROR_NONE != ret) ERR("ic_ioty_send_notify() Fail(%d)", ret); return ret; } + + +API int iotcon_notify_all(iotcon_resource_h resource) +{ + int ret; + + RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER); + + ret = ic_ioty_notify_all(resource->handle); + if (IOTCON_ERROR_NONE != ret) + ERR("ic_ioty_send_notify_all() Fail(%d)", ret); + + return ret; +} diff --git a/lib/include/iotcon-constant.h b/lib/include/iotcon-constant.h index d05317d..df89821 100644 --- a/lib/include/iotcon-constant.h +++ b/lib/include/iotcon-constant.h @@ -18,7 +18,7 @@ #define IOTCON_ALL_INTERFACES "0.0.0.0" #define IOTCON_RANDOM_PORT 0 -#define IOTCON_MULTICAST_ADDRESS "coap://224.0.1.187" +#define IOTCON_MULTICAST_ADDRESS "224.0.1.187" /** * @brief HeaderOption range from 2048 to 3000 diff --git a/lib/include/iotcon-struct.h b/lib/include/iotcon-struct.h index 1a95151..9b48c7d 100644 --- a/lib/include/iotcon-struct.h +++ b/lib/include/iotcon-struct.h @@ -102,7 +102,6 @@ int iotcon_resource_get_number_of_children(iotcon_resource_h resource, int *numb int iotcon_resource_get_nth_child(iotcon_resource_h parent, int index, iotcon_resource_h *child); int iotcon_resource_get_uri(iotcon_resource_h resource, char **uri); -int iotcon_resource_get_host(iotcon_resource_h resource, char **host); int iotcon_resource_get_types(iotcon_resource_h resource, iotcon_resource_types_h *types); int iotcon_resource_get_interfaces(iotcon_resource_h resource, int *ifaces); int iotcon_resource_is_observable(iotcon_resource_h resource, bool *observable); @@ -116,6 +115,7 @@ int iotcon_client_is_observable(iotcon_client_h resource, bool *observable); int iotcon_client_set_options(iotcon_client_h resource, iotcon_options_h header_options); typedef struct ic_resource_request* iotcon_request_h; +int iotcon_request_get_uri(iotcon_request_h request, char **uri); int iotcon_request_get_representation(iotcon_request_h request, iotcon_repr_h *repr); int iotcon_request_get_types(iotcon_request_h request, int *types); int iotcon_request_get_options(iotcon_request_h request, iotcon_options_h *options); diff --git a/lib/include/iotcon.h b/lib/include/iotcon.h index f31d81e..99df88e 100644 --- a/lib/include/iotcon.h +++ b/lib/include/iotcon.h @@ -82,8 +82,9 @@ int iotcon_response_send(iotcon_response_h resp); iotcon_notimsg_h iotcon_notimsg_new(iotcon_repr_h repr, iotcon_interface_e iface); void iotcon_notimsg_free(iotcon_notimsg_h msg); -int iotcon_notify(iotcon_resource_h resource, iotcon_notimsg_h msg, +int iotcon_notify_list_of_observers(iotcon_resource_h resource, iotcon_notimsg_h msg, iotcon_observers_h observers); +int iotcon_notify_all(iotcon_resource_h resource); typedef void (*iotcon_on_cru_cb)(iotcon_options_h header_options, iotcon_repr_h repr, int response_result, void *user_data); diff --git a/test/crud-test-client.c b/test/crud-test-client.c index f278cdd..76770ee 100644 --- a/test/crud-test-client.c +++ b/test/crud-test-client.c @@ -18,11 +18,9 @@ #include #include -#include "test-log.h" +#include "test.h" -#define CRUD_MAX_BUFFER_SIZE (256) - -const char* const door_uri = "/a/door"; +const char * const door_uri = "/a/door"; iotcon_client_h door_resource = NULL; @@ -36,6 +34,12 @@ static void _on_observe(iotcon_options_h header_options, iotcon_repr_h recv_repr int response_result, int sequence_number, void *user_data) { INFO("_on_observe"); + + static int i = 0; + i++; + + if (2 == i) + iotcon_observer_stop(door_resource); } static void _on_delete(iotcon_options_h header_options, int response_result, @@ -219,7 +223,7 @@ static void _found_resource(iotcon_client_h resource, void *user_data) iotcon_subscribe_presence(resource_host, "core.door", _presence_handler, NULL); - if (!strcmp(door_uri, resource_uri)) { + if (TEST_STR_EQUAL == strcmp(door_uri, resource_uri)) { door_resource = iotcon_client_clone(resource); iotcon_query_h query = iotcon_query_new(); diff --git a/test/crud-test-server.c b/test/crud-test-server.c index 9527cd8..369210b 100644 --- a/test/crud-test-server.c +++ b/test/crud-test-server.c @@ -19,7 +19,7 @@ #include #include -#include "test-log.h" +#include "test.h" /* Door Resource */ typedef struct _door_resource_s { @@ -177,13 +177,13 @@ static void _request_handler_post(iotcon_response_h response) static gboolean _notifier(gpointer user_data) { static int i = 0; - if ((5 == i++) && !(observers)) + if ((5 == i++) || !(observers)) return FALSE; INFO("NOTIFY!"); iotcon_repr_h repr = iotcon_repr_new(); iotcon_notimsg_h msg = iotcon_notimsg_new(repr, IOTCON_INTERFACE_DEFAULT); - iotcon_notify(user_data, msg, observers); + iotcon_notify_list_of_observers(user_data, msg, observers); iotcon_repr_free(repr); @@ -267,6 +267,7 @@ static void _request_handler(iotcon_request_h request, void *user_data) ERR("iotcon_request_get_observer_action() Fail(%d)", ret); return; } + if (IOTCON_OBSERVE_REGISTER == observer_action) { ret = iotcon_request_get_observer_id(request, &observer_id); if (IOTCON_ERROR_NONE != ret) { @@ -274,6 +275,13 @@ static void _request_handler(iotcon_request_h request, void *user_data) return; } observers = iotcon_observers_append(observers, observer_id); + } else if (IOTCON_OBSERVE_DEREGISTER == observer_action) { + ret = iotcon_request_get_observer_id(request, &observer_id); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_request_get_observer_id() Fail(%d)", ret); + return; + } + observers = iotcon_observers_remove(observers, observer_id); } } } diff --git a/test/device-test-client.c b/test/device-test-client.c index 69a25eb..c7a95ef 100644 --- a/test/device-test-client.c +++ b/test/device-test-client.c @@ -1,6 +1,6 @@ #include #include -#include "test-log.h" +#include "test.h" static void _get_device_info(iotcon_device_info_s info, void *user_data) { diff --git a/test/device-test-server.c b/test/device-test-server.c index 324c4e7..4a046b0 100644 --- a/test/device-test-server.c +++ b/test/device-test-server.c @@ -1,6 +1,6 @@ #include #include -#include "test-log.h" +#include "test.h" int main() { diff --git a/test/repr-test-client.c b/test/repr-test-client.c index afda58a..1017726 100644 --- a/test/repr-test-client.c +++ b/test/repr-test-client.c @@ -17,11 +17,9 @@ #include #include #include -#include "test-log.h" +#include "test.h" -#define CRUD_MAX_BUFFER_SIZE (256) - -const char* const room_uri = "/a/room"; +const char * const room_uri = "/a/room"; iotcon_client_h room_resource = NULL; @@ -83,14 +81,14 @@ static void _on_get(iotcon_repr_h recv_repr, int response_result) DBG("uri : %s", uri); - if (!strcmp("/a/light", uri)) { + if (TEST_STR_EQUAL == strcmp("/a/light", uri)) { key_count = iotcon_repr_get_keys_count(child_repr); if (key_count) { int brightness; iotcon_repr_get_int(child_repr, "brightness", &brightness); DBG("brightness : %d", brightness); } - } else if (!strcmp("/a/switch", uri)) { + } else if (TEST_STR_EQUAL == strcmp("/a/switch", uri)) { key_count = iotcon_repr_get_keys_count(child_repr); if (key_count) { bool bswitch; @@ -181,9 +179,9 @@ static void _found_resource(iotcon_client_h resource, void *user_data) ERR("iotcon_client_get_types() Fail(%d)", ret); return; } - iotcon_resource_types_foreach(resource_types, _get_res_type_fn, (void*)resource_uri); + iotcon_resource_types_foreach(resource_types, _get_res_type_fn, (void *)resource_uri); - if (!strcmp(room_uri, resource_uri)) { + if (TEST_STR_EQUAL == strcmp(room_uri, resource_uri)) { /* copy resource to use elsewhere */ room_resource = iotcon_client_clone(resource); diff --git a/test/repr-test-server.c b/test/repr-test-server.c index 38eae19..c94f1f2 100644 --- a/test/repr-test-server.c +++ b/test/repr-test-server.c @@ -18,7 +18,7 @@ #include #include #include -#include "test-log.h" +#include "test.h" static void _room_request_handler(iotcon_request_h request, void *user_data); @@ -50,9 +50,9 @@ static int _query_foreach_cb(const char *key, const char *value, void *user_data { char **interface_str = user_data; - if (!strcmp("if", key)) { - *interface_str = (char*)value; - } + if (TEST_STR_EQUAL == strcmp("if", key)) + *interface_str = (char *)value; + return IOTCON_FUNC_CONTINUE; } @@ -113,11 +113,10 @@ static void _room_request_handler_get(iotcon_request_h request, if (query) iotcon_query_foreach(query, _query_foreach_cb, &query_str); - if (query_str && !strcmp("oc.mi.b", query_str)) { + if (query_str && (TEST_STR_EQUAL == strcmp("oc.mi.b", query_str))) { DBG("operation for BATCH interface"); interface = IOTCON_INTERFACE_BATCH; - } - else { + } else { DBG("operation for DEFAULT interface"); interface = IOTCON_INTERFACE_DEFAULT; } diff --git a/test/test-log.h b/test/test.h similarity index 88% rename from test/test-log.h rename to test/test.h index 5e1d5e4..844787b 100644 --- a/test/test-log.h +++ b/test/test.h @@ -13,14 +13,17 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef __IOT_CONNECTIVITY_MANAGER_TEST_LOG_H__ -#define __IOT_CONNECTIVITY_MANAGER_TEST_LOG_H__ +#ifndef __IOT_CONNECTIVITY_MANAGER_TEST_H__ +#define __IOT_CONNECTIVITY_MANAGER_TEST_H__ -#define ICTEST_LOGRED "\033[0;31m" -#define ICTEST_LOGGREEN "\033[0;32m" -#define ICTEST_LOGBROWN "\033[0;33m" -#define ICTEST_LOGBLUE "\033[0;34m" -#define ICTEST_LOGEND "\033[0;m" +/* for strcmp() */ +#define TEST_STR_EQUAL 0 + +#define ICTEST_LOGRED "\033[0;31m" +#define ICTEST_LOGGREEN "\033[0;32m" +#define ICTEST_LOGBROWN "\033[0;33m" +#define ICTEST_LOGBLUE "\033[0;34m" +#define ICTEST_LOGEND "\033[0;m" #undef _DBG #undef _INFO @@ -126,4 +129,4 @@ } \ } while (0) -#endif /* __IOT_CONNECTIVITY_MANAGER_TEST_LOG_H__ */ +#endif /* __IOT_CONNECTIVITY_MANAGER_TEST_H__ */ -- 2.7.4