From 2c7ad801e7ce4eb2320ce73c49fc4b4aec3b6a88 Mon Sep 17 00:00:00 2001 From: Minchul Lee Date: Thu, 28 May 2015 15:36:48 +0900 Subject: [PATCH] modify return type of functions Change-Id: Ic15966fdeb476b82f7e8039150d61a7f6824bbd5 Signed-off-by: Minchul Lee --- lib/ic-ioty.cpp | 102 +++++++-- lib/ic-ioty.h | 3 + lib/ic-options.c | 16 +- lib/ic-query.c | 16 +- lib/ic-repr-list.c | 417 ++++++++++++++++++++++++------------ lib/ic-repr-list.h | 3 +- lib/ic-repr-obj.c | 131 +++++++---- lib/ic-repr-obj.h | 2 +- lib/ic-repr-value.c | 97 ++++++--- lib/ic-repr-value.h | 12 +- lib/ic-repr.c | 288 ++++++++++++------------- lib/ic-repr.h | 5 +- lib/ic-utils.c | 14 +- lib/include/iotcon-constant.h | 5 +- lib/include/iotcon-representation.h | 130 ++++++----- lib/include/iotcon-struct.h | 20 +- test/crud-test-client.c | 15 +- test/crud-test-server.c | 10 +- test/repr-test-client.c | 46 ++-- test/repr-test-server.c | 3 +- 20 files changed, 848 insertions(+), 487 deletions(-) diff --git a/lib/ic-ioty.cpp b/lib/ic-ioty.cpp index ccbe305..11a68d8 100755 --- a/lib/ic-ioty.cpp +++ b/lib/ic-ioty.cpp @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include #include #include #include @@ -609,29 +610,76 @@ extern "C" int ic_ioty_unregister_res(iotcon_resource_h resource_handle) return IOTCON_ERROR_NONE; } -static int _ic_ioty_generate_interface(iotcon_interface_e iface, string &interface_str) +extern "C" int ic_ioty_convert_interface_string(const char *src, iotcon_interface_e *dest) { - switch (iface) { + RETV_IF(NULL == src, IOTCON_ERROR_PARAM); + RETV_IF(NULL == dest, IOTCON_ERROR_PARAM); + + string interface_str(src); + + if (STR_EQUAL == DEFAULT_INTERFACE.compare(interface_str)) { + *dest = IOTCON_INTERFACE_DEFAULT; + } + else if (STR_EQUAL == LINK_INTERFACE.compare(interface_str)) { + *dest = IOTCON_INTERFACE_LINK; + } + else if (STR_EQUAL == BATCH_INTERFACE.compare(interface_str)) { + *dest = IOTCON_INTERFACE_BATCH; + } + else if (STR_EQUAL == GROUP_INTERFACE.compare(interface_str)) { + *dest = IOTCON_INTERFACE_GROUP; + } + else { + ERR("Invalid interface"); + *dest = IOTCON_INTERFACE_NONE; + return IOTCON_ERROR_PARAM; + } + + return IOTCON_ERROR_NONE; +} + +static int _ic_ioty_convert_interface_flag(iotcon_interface_e src, string &dest) +{ + switch (src) { case IOTCON_INTERFACE_GROUP: - interface_str = GROUP_INTERFACE; + dest = GROUP_INTERFACE; break; case IOTCON_INTERFACE_BATCH: - interface_str = BATCH_INTERFACE; + dest = BATCH_INTERFACE; break; case IOTCON_INTERFACE_LINK: - interface_str = LINK_INTERFACE; + dest = LINK_INTERFACE; break; case IOTCON_INTERFACE_DEFAULT: - case IOTCON_INTERFACE_NONE: - interface_str = DEFAULT_INTERFACE; + dest = DEFAULT_INTERFACE; break; + case IOTCON_INTERFACE_NONE: default: ERR("Invalid interface"); + dest = ""; return IOTCON_ERROR_PARAM; } return IOTCON_ERROR_NONE; } +extern "C" int ic_ioty_convert_interface_flag(iotcon_interface_e src, char **dest) +{ + FN_CALL; + int ret; + string iface_str; + + ret = _ic_ioty_convert_interface_flag(src, iface_str); + if (IOTCON_ERROR_NONE != ret) { + ERR("_ic_ioty_convert_interface_flag() Fail(%d)", ret); + *dest = NULL; + return ret; + } + + *dest = ic_utils_strdup(iface_str.c_str()); + + return IOTCON_ERROR_NONE; +} + extern "C" int ic_ioty_bind_iface_to_res(OCResourceHandle resourceHandle, iotcon_interface_e iface) { @@ -639,9 +687,9 @@ extern "C" int ic_ioty_bind_iface_to_res(OCResourceHandle resourceHandle, OCStackResult ocRet; string resource_interface; - ret = _ic_ioty_generate_interface(iface, resource_interface); + ret = _ic_ioty_convert_interface_flag(iface, resource_interface); if (IOTCON_ERROR_NONE != ret) { - ERR("_ic_ioty_generate_interface() Fail(%d)", ret); + ERR("_ic_ioty_convert_interface_flag(%d) Fail(%d)", iface, ret); return ret; } @@ -755,11 +803,18 @@ extern "C" int ic_ioty_send_notify(OCResourceHandle resHandle, struct ic_notify_ resourceResponse->setErrorCode(msg->error_code); OCRepresentation ocRep = ic_ioty_repr_parse(msg->repr); - ret = _ic_ioty_generate_interface(msg->iface, iface); - if (IOTCON_ERROR_NONE != ret) { - ERR("_ic_ioty_generate_interface() Fail(%d)", ret); - return ret; + + 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); @@ -788,10 +843,15 @@ extern "C" int ic_ioty_send_res_response_data(struct ic_resource_response *resp) pResponse->setErrorCode(resp->error_code); pResponse->setResponseResult((OCEntityHandlerResult)resp->result); - ret = _ic_ioty_generate_interface(resp->iface, iface); - if (IOTCON_ERROR_NONE != ret) { - ERR("_ic_ioty_generate_interface() Fail(%d)", ret); - return ret; + if (IOTCON_INTERFACE_NONE != resp->iface) { + ret = _ic_ioty_convert_interface_flag(resp->iface, iface); + if (IOTCON_ERROR_NONE != ret) { + ERR("_ic_ioty_convert_interface_flag(%d) Fail(%d)", resp->iface, ret); + return ret; + } + } + else { + iface = DEFAULT_INTERFACE; } pResponse->setResourceRepresentation(ocRep, iface); @@ -901,12 +961,14 @@ extern "C" int ic_ioty_find_resource(const char *host_address, const char *resou return IOTCON_ERROR_NONE; } -static void _ic_ioty_accumulate_options_vector(unsigned short id, const char *data, +static int _ic_ioty_accumulate_options_vector(unsigned short id, const char *data, void *user_data) { HeaderOptions *options = static_cast(user_data); HeaderOption::OCHeaderOption option(id, data); (*options).push_back(option); + + return IOTCON_FUNC_CONTINUE; } static OCResource::Ptr _ic_ioty_create_oc_resource(iotcon_client_h resource) @@ -963,13 +1025,15 @@ static OCResource::Ptr _ic_ioty_create_oc_resource(iotcon_client_h resource) } -static void _ic_ioty_accumulate_query_map(const char *key, const char *value, +static int _ic_ioty_accumulate_query_map(const char *key, const char *value, void *user_data) { QueryParamsMap *queryParams = static_cast(user_data); string keyStr = key; string valueStr = value; (*queryParams)[keyStr] = valueStr; + + return IOTCON_FUNC_CONTINUE; } diff --git a/lib/ic-ioty.h b/lib/ic-ioty.h index a64d24e..cc79826 100644 --- a/lib/ic-ioty.h +++ b/lib/ic-ioty.h @@ -73,5 +73,8 @@ int ic_ioty_observe(iotcon_client_h resource, iotcon_observe_type_e observe_type int ic_ioty_cancel_observe(iotcon_client_h resource); +int ic_ioty_convert_interface_flag(iotcon_interface_e src, char **dest); +int ic_ioty_convert_interface_string(const char *src, iotcon_interface_e *dest); + #endif //__IOT_CONNECTIVITY_MANAGER_INTERNAL_IOTIVITY_H__ diff --git a/lib/ic-options.c b/lib/ic-options.c index 5dde44c..00d68ea 100755 --- a/lib/ic-options.c +++ b/lib/ic-options.c @@ -108,18 +108,22 @@ API const char* iotcon_options_lookup(iotcon_options_h options, unsigned short i } -API void iotcon_options_foreach(iotcon_options_h options, iotcon_options_foreach_cb cb, - void *user_data) +API int iotcon_options_foreach(iotcon_options_h options, + iotcon_options_foreach_cb cb, void *user_data) { GHashTableIter iter; gpointer key, value; - RET_IF(NULL == options); - RET_IF(NULL == cb); + RETV_IF(NULL == options, IOTCON_ERROR_PARAM); + RETV_IF(NULL == cb, IOTCON_ERROR_PARAM); g_hash_table_iter_init(&iter, options->hash); - while (g_hash_table_iter_next(&iter, &key, &value)) - cb(GPOINTER_TO_UINT(key), value, user_data); + while (g_hash_table_iter_next(&iter, &key, &value)) { + if (false == cb(GPOINTER_TO_UINT(key), value, user_data)) + break; + } + + return IOTCON_ERROR_NONE; } diff --git a/lib/ic-query.c b/lib/ic-query.c index a5d2da4..b5b5558 100755 --- a/lib/ic-query.c +++ b/lib/ic-query.c @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include #include #include #include @@ -111,18 +112,21 @@ API const char* iotcon_query_lookup(iotcon_query_h query, const char *key) return ret; } - -API void iotcon_query_foreach(iotcon_query_h query, iotcon_query_foreach_cb cb, +API int iotcon_query_foreach(iotcon_query_h query, iotcon_query_foreach_cb cb, void *user_data) { GHashTableIter iter; gpointer key, value; - RET_IF(NULL == query); - RET_IF(NULL == cb); + RETV_IF(NULL == query, IOTCON_ERROR_PARAM); + RETV_IF(NULL == cb, IOTCON_ERROR_PARAM); g_hash_table_iter_init(&iter, query->hash); - while (g_hash_table_iter_next(&iter, &key, &value)) - cb(key, value, user_data); + while (g_hash_table_iter_next(&iter, &key, &value)) { + if (false == cb(key, value, user_data)) + break; + } + + return IOTCON_ERROR_NONE; } diff --git a/lib/ic-repr-list.c b/lib/ic-repr-list.c index 3559073..485cae0 100755 --- a/lib/ic-repr-list.c +++ b/lib/ic-repr-list.c @@ -51,7 +51,7 @@ static bool _ic_list_dec_ref_count(iotcon_list_h val) return ret; } -API iotcon_list_h iotcon_list_new(iotcon_repr_types_e type) +static iotcon_list_h _ic_list_new(iotcon_types_e type) { iotcon_list_h list; @@ -66,229 +66,299 @@ API iotcon_list_h iotcon_list_new(iotcon_repr_types_e type) return list; } +API iotcon_list_h iotcon_list_new(iotcon_types_e type) +{ + if (type < IOTCON_TYPE_INT || IOTCON_TYPE_REPR < type) { + ERR("Invalid Type(%d)", type); + return NULL; + } + + return _ic_list_new(type); +} + -API iotcon_list_h iotcon_list_insert_int(iotcon_list_h list, int val, int pos) +API int iotcon_list_insert_int(iotcon_list_h list, int val, int pos) { iotcon_value_h value; - RETV_IF(NULL == list, NULL); - RETVM_IF(IOTCON_TYPE_INT != list->type, list, "Invalid Type(%d)", list->type); + RETV_IF(NULL == list, IOTCON_ERROR_PARAM); + RETVM_IF(IOTCON_TYPE_INT != list->type, IOTCON_ERROR_PARAM, "Invalid Type(%d)", + list->type); value = ic_value_new_int(val); if (NULL == value) { ERR("ic_value_new_int(%d) Fail", val); - return list; + return IOTCON_ERROR_FAIL; } - list = ic_list_insert(list, value, pos); - - return list; + return ic_list_insert(list, value, pos); } -API iotcon_list_h iotcon_list_insert_bool(iotcon_list_h list, bool val, int pos) +API int iotcon_list_insert_bool(iotcon_list_h list, bool val, int pos) { iotcon_value_h value; - RETV_IF(NULL == list, NULL); - RETVM_IF(IOTCON_TYPE_BOOL != list->type, list, "Invalid Type(%d)", list->type); + RETV_IF(NULL == list, IOTCON_ERROR_PARAM); + RETVM_IF(IOTCON_TYPE_BOOL != list->type, IOTCON_ERROR_PARAM, "Invalid Type(%d)", + list->type); value = ic_value_new_bool(val); if (NULL == value) { ERR("ic_value_new_bool(%d) Fail", val); - return list; + return IOTCON_ERROR_FAIL; } - list = ic_list_insert(list, value, pos); - - return list; + return ic_list_insert(list, value, pos); } -API iotcon_list_h iotcon_list_insert_double(iotcon_list_h list, double val, int pos) +API int iotcon_list_insert_double(iotcon_list_h list, double val, int pos) { iotcon_value_h value; - RETV_IF(NULL == list, NULL); - RETVM_IF(IOTCON_TYPE_DOUBLE != list->type, list, "Invalid Type(%d)", list->type); + RETV_IF(NULL == list, IOTCON_ERROR_PARAM); + RETVM_IF(IOTCON_TYPE_DOUBLE != list->type, IOTCON_ERROR_PARAM, "Invalid Type(%d)", + list->type); value = ic_value_new_double(val); if (NULL == value) { ERR("ic_value_new_double(%f) Fail", val); - return list; + return IOTCON_ERROR_FAIL; } - list = ic_list_insert(list, value, pos); - - return list; + return ic_list_insert(list, value, pos); } -API iotcon_list_h iotcon_list_insert_str(iotcon_list_h list, char *val, int pos) +API int iotcon_list_insert_str(iotcon_list_h list, char *val, int pos) { iotcon_value_h value; - RETV_IF(NULL == list, NULL); - RETVM_IF(IOTCON_TYPE_STR != list->type, list, "Invalid Type(%d)", list->type); + RETV_IF(NULL == list, IOTCON_ERROR_PARAM); + RETV_IF(NULL == val, IOTCON_ERROR_PARAM); + RETVM_IF(IOTCON_TYPE_STR != list->type, IOTCON_ERROR_PARAM, "Invalid Type(%d)", + list->type); value = ic_value_new_str(val); if (NULL == value) { ERR("ic_value_new_str(%s) Fail", val); - return list; + return IOTCON_ERROR_FAIL; } - list = ic_list_insert(list, value, pos); - - return list; + return ic_list_insert(list, value, pos); } -API iotcon_list_h iotcon_list_insert_list(iotcon_list_h list, iotcon_list_h val, int pos) +API int iotcon_list_insert_list(iotcon_list_h list, iotcon_list_h val, int pos) { iotcon_value_h value; - RETV_IF(NULL == list, NULL); - RETV_IF(NULL == val, NULL); - RETVM_IF(IOTCON_TYPE_LIST != list->type, list, "Invalid Type(%d)", list->type); + RETV_IF(NULL == list, IOTCON_ERROR_PARAM); + RETV_IF(NULL == val, IOTCON_ERROR_PARAM); + RETVM_IF(IOTCON_TYPE_LIST != list->type, IOTCON_ERROR_PARAM, "Invalid Type(%d)", + list->type); value = ic_value_new_list(val); if (NULL == value) { ERR("ic_value_new_list(%p) Fail", val); - return list; + return IOTCON_ERROR_FAIL; } ic_list_inc_ref_count(val); - list = ic_list_insert(list, value, pos); - - return list; + return ic_list_insert(list, value, pos); } -API iotcon_list_h iotcon_list_insert_repr(iotcon_list_h list, iotcon_repr_h val, int pos) +API int iotcon_list_insert_repr(iotcon_list_h list, iotcon_repr_h val, int pos) { iotcon_value_h value; - RETV_IF(NULL == list, NULL); - RETV_IF(NULL == val, NULL); - RETVM_IF(IOTCON_TYPE_REPR != list->type, list, "Invalid Type(%d)", list->type); + RETV_IF(NULL == list, IOTCON_ERROR_PARAM); + RETV_IF(NULL == val, IOTCON_ERROR_PARAM); + RETVM_IF(IOTCON_TYPE_REPR != list->type, IOTCON_ERROR_PARAM, "Invalid Type(%d)", + list->type); value = ic_value_new_repr(val); if (NULL == value) { ERR("ic_value_new_repr(%p) Fail", val); - return list; + return IOTCON_ERROR_FAIL; } ic_repr_inc_ref_count(val); - list = ic_list_insert(list, value, pos); - - return list; + return ic_list_insert(list, value, pos); } -API int iotcon_list_get_nth_int(iotcon_list_h list, int index) +API int iotcon_list_get_nth_int(iotcon_list_h list, int index, int *val) { + int ival, ret; iotcon_value_h value; - RETV_IF(NULL == list, 0); - RETV_IF(NULL == list->list, 0); + RETV_IF(NULL == list, IOTCON_ERROR_PARAM); + RETV_IF(NULL == list->list, IOTCON_ERROR_PARAM); + RETV_IF(NULL == val, IOTCON_ERROR_PARAM); value = g_list_nth_data(list->list, index); if (NULL == value) { ERR("g_list_nth_data() Fail"); - return 0; + return IOTCON_ERROR_NO_DATA; + } + + ret = ic_value_get_int(value, &ival); + if (IOTCON_ERROR_NONE != ret) { + ERR("ic_value_get_int() Fail(%d)", ret); + return IOTCON_ERROR_FAIL; } - return ic_value_get_int(value); + *val = ival; + + return IOTCON_ERROR_NONE; } -API bool iotcon_list_get_nth_bool(iotcon_list_h list, int index) +API int iotcon_list_get_nth_bool(iotcon_list_h list, int index, bool *val) { + int ret; + bool bval; iotcon_value_h value; - RETV_IF(NULL == list, false); - RETV_IF(NULL == list->list, false); + RETV_IF(NULL == list, IOTCON_ERROR_PARAM); + RETV_IF(NULL == list->list, IOTCON_ERROR_PARAM); + RETV_IF(NULL == val, IOTCON_ERROR_PARAM); value = g_list_nth_data(list->list, index); if (NULL == value) { ERR("g_list_nth_data() Fail"); - return false; + return IOTCON_ERROR_NO_DATA; + } + + ret = ic_value_get_bool(value, &bval); + if (IOTCON_ERROR_NONE != ret) { + ERR("ic_value_get_bool() Fail(%d)", ret); + return IOTCON_ERROR_FAIL; } - return ic_value_get_bool(value); + *val = bval; + + return IOTCON_ERROR_NONE; } -API double iotcon_list_get_nth_double(iotcon_list_h list, int index) +API int iotcon_list_get_nth_double(iotcon_list_h list, int index, double *val) { + int ret; + double dbval; iotcon_value_h value; - RETV_IF(NULL == list, 0.0); - RETV_IF(NULL == list->list, 0.0); + RETV_IF(NULL == list, IOTCON_ERROR_PARAM); + RETV_IF(NULL == list->list, IOTCON_ERROR_PARAM); + RETV_IF(NULL == val, IOTCON_ERROR_PARAM); value = g_list_nth_data(list->list, index); if (NULL == value) { ERR("g_list_nth_data() Fail"); - return 0.0; + return IOTCON_ERROR_NO_DATA; + } + + ret = ic_value_get_double(value, &dbval); + if (IOTCON_ERROR_NONE != ret) { + ERR("ic_value_get_double() Fail(%d)", ret); + return IOTCON_ERROR_FAIL; } - return ic_value_get_double(value); + *val = dbval; + + return IOTCON_ERROR_NONE; } -API const char* iotcon_list_get_nth_str(iotcon_list_h list, int index) +API int iotcon_list_get_nth_str(iotcon_list_h list, int index, const char **val) { + int ret; + const char *strval; iotcon_value_h value; - RETV_IF(NULL == list, NULL); - RETV_IF(NULL == list->list, NULL); + RETV_IF(NULL == list, IOTCON_ERROR_PARAM); + RETV_IF(NULL == list->list, IOTCON_ERROR_PARAM); + RETV_IF(NULL == val, IOTCON_ERROR_PARAM); value = g_list_nth_data(list->list, index); if (NULL == value) { ERR("g_list_nth_data() Fail"); - return NULL; + return IOTCON_ERROR_NO_DATA; } - return ic_value_get_str(value); + ret = ic_value_get_str(value, &strval); + if (IOTCON_ERROR_NONE != ret) { + ERR("ic_value_get_str() Fail(%d)", ret); + return IOTCON_ERROR_FAIL; + } + + *val = strval; + + return IOTCON_ERROR_NONE; } -API iotcon_list_h iotcon_list_get_nth_list(iotcon_list_h list, int index) +API int iotcon_list_get_nth_list(iotcon_list_h src, int index, iotcon_list_h *dest) { + int ret; iotcon_value_h value; + iotcon_list_h list_val; - RETV_IF(NULL == list, NULL); - RETV_IF(NULL == list->list, NULL); + RETV_IF(NULL == src, IOTCON_ERROR_PARAM); + RETV_IF(NULL == src->list, IOTCON_ERROR_PARAM); + RETV_IF(NULL == dest, IOTCON_ERROR_PARAM); - value = g_list_nth_data(list->list, index); + value = g_list_nth_data(src->list, index); if (NULL == value) { ERR("g_list_nth_data() Fail"); - return NULL; + return IOTCON_ERROR_NO_DATA; + } + + ret = ic_value_get_list(value, &list_val); + if (IOTCON_ERROR_NONE != ret) { + ERR("ic_value_get_list() Fail(%d)", ret); + return IOTCON_ERROR_FAIL; } - return ic_value_get_list(value); + *dest = list_val; + + return IOTCON_ERROR_NONE; } -API iotcon_repr_h iotcon_list_get_nth_repr(iotcon_list_h list, int index) +API int iotcon_list_get_nth_repr(iotcon_list_h list, int index, iotcon_repr_h *repr) { + int ret; iotcon_value_h value; + iotcon_repr_h repr_val; - RETV_IF(NULL == list, NULL); - RETV_IF(NULL == list->list, NULL); + RETV_IF(NULL == list, IOTCON_ERROR_PARAM); + RETV_IF(NULL == list->list, IOTCON_ERROR_PARAM); + RETV_IF(NULL == repr, IOTCON_ERROR_PARAM); value = g_list_nth_data(list->list, index); if (NULL == value) { ERR("g_list_nth_data() Fail"); - return NULL; + return IOTCON_ERROR_NO_DATA; } - return ic_value_get_repr(value); + ret = ic_value_get_repr(value, &repr_val); + if (IOTCON_ERROR_NONE != ret) { + ERR("ic_value_get_list() Fail(%d)", ret); + return IOTCON_ERROR_FAIL; + } + + *repr = repr_val; + + return IOTCON_ERROR_NONE; } -static int _ic_list_del_nth_value(iotcon_list_h list, int pos, iotcon_repr_types_e value_type) +static int _ic_list_del_nth_value(iotcon_list_h list, int pos, iotcon_types_e value_type) { iotcon_value_h value; @@ -393,18 +463,21 @@ API int iotcon_list_del_nth_repr(iotcon_list_h list, int pos) } -API int iotcon_list_get_type(iotcon_list_h list) +API int iotcon_list_get_type(iotcon_list_h list, int *type) { RETV_IF(NULL == list, IOTCON_ERROR_PARAM); + RETV_IF(NULL == type, IOTCON_ERROR_PARAM); - return list->type; + *type = list->type; + + return IOTCON_ERROR_NONE; } -API int iotcon_list_get_length(iotcon_list_h list) +API unsigned int iotcon_list_get_length(iotcon_list_h list) { - RETV_IF(NULL == list, IOTCON_ERROR_PARAM); - RETV_IF(NULL == list->list, IOTCON_ERROR_PARAM); + RETV_IF(NULL == list, 0); + RETV_IF(NULL == list->list, 0); return g_list_length(list->list); } @@ -420,130 +493,162 @@ int ic_list_remove(iotcon_list_h list, iotcon_value_h val) } -iotcon_list_h ic_list_insert(iotcon_list_h list, iotcon_value_h value, int pos) +int ic_list_insert(iotcon_list_h list, iotcon_value_h value, int pos) { - RETV_IF(NULL == list, NULL); + RETV_IF(NULL == list, IOTCON_ERROR_PARAM); list->list = g_list_insert(list->list, value, pos); - if (NULL == list->list) { - ERR("g_list_insert() Fail"); - return list; - } - return list; + return IOTCON_ERROR_NONE; } -API void iotcon_list_foreach_int(iotcon_list_h list, iotcon_list_int_fn fn, void *user_data) +API int iotcon_list_foreach_int(iotcon_list_h list, iotcon_list_int_fn fn, + void *user_data) { GList *cur; int index = 0; ic_basic_s *real = NULL; - RETM_IF(NULL == list, "IOTCON_ERROR_PARAM(NULL == list)"); - RETM_IF(NULL == fn, "IOTCON_ERROR_PARAM(NULL == fn)"); + RETV_IF(NULL == list, IOTCON_ERROR_PARAM); + RETVM_IF(IOTCON_TYPE_INT != list->type, IOTCON_ERROR_PARAM, "Invalid Type(%d)", + list->type); + RETV_IF(NULL == fn, IOTCON_ERROR_PARAM); cur = list->list; while (cur) { GList *next = cur->next; real = cur->data; - (*fn)(index, real->val.i, user_data); + if (IOTCON_FUNC_STOP == fn(index, real->val.i, user_data)) + break; index++; cur = next; } + + return IOTCON_ERROR_NONE; } -API void iotcon_list_foreach_bool(iotcon_list_h list, iotcon_list_bool_fn fn, void *user_data) +API int iotcon_list_foreach_bool(iotcon_list_h list, iotcon_list_bool_fn fn, + void *user_data) { GList *cur; int index = 0; ic_basic_s *real = NULL; - RETM_IF(NULL == list, "IOTCON_ERROR_PARAM(NULL == list)"); - RETM_IF(NULL == fn, "IOTCON_ERROR_PARAM(NULL == fn)"); + RETV_IF(NULL == list, IOTCON_ERROR_PARAM); + RETVM_IF(IOTCON_TYPE_BOOL != list->type, IOTCON_ERROR_PARAM, "Invalid Type(%d)", + list->type); + RETV_IF(NULL == fn, IOTCON_ERROR_PARAM); cur = list->list; while (cur) { GList *next = cur->next; real = cur->data; - (*fn)(index, real->val.b, user_data); + if (IOTCON_FUNC_STOP == fn(index, real->val.b, user_data)) + break; index++; cur = next; } + + return IOTCON_ERROR_NONE; } -API void iotcon_list_foreach_double(iotcon_list_h list, iotcon_list_double_fn fn, void *user_data) +API int iotcon_list_foreach_double(iotcon_list_h list, iotcon_list_double_fn fn, + void *user_data) { GList *cur; int index = 0; ic_basic_s *real = NULL; - RETM_IF(NULL == list, "IOTCON_ERROR_PARAM(NULL == list)"); - RETM_IF(NULL == fn, "IOTCON_ERROR_PARAM(NULL == fn)"); + RETV_IF(NULL == list, IOTCON_ERROR_PARAM); + RETVM_IF(IOTCON_TYPE_DOUBLE != list->type, IOTCON_ERROR_PARAM, "Invalid Type(%d)", + list->type); + RETV_IF(NULL == fn, IOTCON_ERROR_PARAM); cur = list->list; while (cur) { GList *next = cur->next; real = cur->data; - (*fn)(index, real->val.d, user_data); + if (IOTCON_FUNC_STOP == fn(index, real->val.d, user_data)) + break; index++; cur = next; } + + return IOTCON_ERROR_NONE; } -API void iotcon_list_foreach_str(iotcon_list_h list, iotcon_list_str_fn fn, void *user_data) +API int iotcon_list_foreach_str(iotcon_list_h list, iotcon_list_str_fn fn, + void *user_data) { GList *cur; int index = 0; ic_basic_s *real = NULL; - RETM_IF(NULL == list, "IOTCON_ERROR_PARAM(NULL == list)"); - RETM_IF(NULL == fn, "IOTCON_ERROR_PARAM(NULL == fn)"); + RETV_IF(NULL == list, IOTCON_ERROR_PARAM); + RETVM_IF(IOTCON_TYPE_STR != list->type, IOTCON_ERROR_PARAM, "Invalid Type(%d)", + list->type); + RETV_IF(NULL == fn, IOTCON_ERROR_PARAM); cur = list->list; while (cur) { GList *next = cur->next; real = cur->data; - (*fn)(index, real->val.s, user_data); + if (IOTCON_FUNC_STOP == fn(index, real->val.s, user_data)) + break; index++; cur = next; } + + return IOTCON_ERROR_NONE; } -API void iotcon_list_list_foreach(iotcon_list_h list, iotcon_list_list_fn fn, void *user_data) +API int iotcon_list_foreach_list(iotcon_list_h list, iotcon_list_list_fn fn, + void *user_data) { int index = 0; GList *cur = NULL; ic_val_list_s *real = NULL; - RETM_IF(NULL == list, "IOTCON_ERROR_PARAM(NULL == list)"); - RETM_IF(NULL == fn, "IOTCON_ERROR_PARAM(NULL == fn)"); + RETV_IF(NULL == list, IOTCON_ERROR_PARAM); + RETVM_IF(IOTCON_TYPE_LIST != list->type, IOTCON_ERROR_PARAM, "Invalid Type(%d)", + list->type); + RETV_IF(NULL == fn, IOTCON_ERROR_PARAM); cur = list->list; while (cur) { GList *next = cur->next; real = cur->data; - (*fn)(index, real->list, user_data); + if (IOTCON_FUNC_STOP == fn(index, real->list, user_data)) + break; index++; cur = next; } + + return IOTCON_ERROR_NONE; } -API void iotcon_list_foreach_repr(iotcon_list_h list, iotcon_list_repr_fn fn, void *user_data) +API int iotcon_list_foreach_repr(iotcon_list_h list, iotcon_list_repr_fn fn, void *user_data) { int index = 0; GList *cur = NULL; ic_val_repr_s *real = NULL; - RETM_IF(NULL == list, "IOTCON_ERROR_PARAM(NULL == list)"); - RETM_IF(NULL == fn, "IOTCON_ERROR_PARAM(NULL == fn)"); + + RETV_IF(NULL == list, IOTCON_ERROR_PARAM); + RETVM_IF(IOTCON_TYPE_REPR != list->type, IOTCON_ERROR_PARAM, "Invalid Type(%d)", + list->type); + RETV_IF(NULL == fn, IOTCON_ERROR_PARAM);; cur = list->list; while (cur) { GList *next = cur->next; real = cur->data; - (*fn)(index, real->repr, user_data); + if (IOTCON_FUNC_STOP == fn(index, real->repr, user_data)) + break; index++; cur = next; } + + return IOTCON_ERROR_NONE; } @@ -562,8 +667,7 @@ static iotcon_value_h _ic_list_get_nth_value(iotcon_list_h list, int index) */ JsonArray* ic_list_to_json(iotcon_list_h list) { - int i; - int count = 0; + int i, ret, count; JsonArray *parray = NULL; JsonNode *child_node = NULL; JsonObject *child_obj = NULL; @@ -597,7 +701,13 @@ JsonArray* ic_list_to_json(iotcon_list_h list) json_array_add_element(parray, child_node); break; case IOTCON_TYPE_LIST: - child_list = ic_value_get_list(child_value); + ret = ic_value_get_list(child_value, &child_list); + if (IOTCON_ERROR_NONE != ret) { + ERR("ic_value_get_list() Fail(%d)", ret); + json_array_unref(parray); + return NULL; + } + child_array = ic_list_to_json(child_list); if (NULL == child_array) { ERR("ic_list_to_json(child_list) Fail"); @@ -609,7 +719,13 @@ JsonArray* ic_list_to_json(iotcon_list_h list) json_array_add_element(parray, child_node); break; case IOTCON_TYPE_REPR: - child_repr = ic_value_get_repr(child_value); + ret = ic_value_get_repr(child_value, &child_repr); + if (IOTCON_ERROR_NONE != ret) { + ERR("ic_value_get_repr() Fail(%d)", ret); + json_array_unref(parray); + return NULL; + } + child_obj = ic_obj_to_json(child_repr); if (NULL == child_obj) { ERR("ic_obj_to_json(child_repr) Fail"); @@ -630,10 +746,10 @@ JsonArray* ic_list_to_json(iotcon_list_h list) */ iotcon_list_h ic_list_from_json(JsonArray *parray) { - int i; + int i, ret; int count = json_array_get_length(parray); - iotcon_list_h list = iotcon_list_new(IOTCON_TYPE_NONE); + iotcon_list_h list = _ic_list_new(IOTCON_TYPE_NONE); /* DBG("array count(%d)", count); */ for (i = 0; i < count; i++) { @@ -654,7 +770,13 @@ iotcon_list_h ic_list_from_json(JsonArray *parray) return NULL; } - list = ic_list_insert(list, value, -1); + ret = ic_list_insert(list, value, -1); + if (IOTCON_ERROR_NONE != ret) { + ERR("ic_list_insert() Fail(%d)", ret); + ic_value_free(value); + iotcon_list_free(list); + return NULL; + } list->type = real->type; } else if (JSON_NODE_HOLDS_ARRAY(child_node)) { @@ -680,7 +802,13 @@ iotcon_list_h ic_list_from_json(JsonArray *parray) return NULL; } - list = ic_list_insert(list, value, -1); + ret = ic_list_insert(list, value, -1); + if (IOTCON_ERROR_NONE != ret) { + ERR("ic_list_insert() Fail(%d)", ret); + iotcon_list_free(parsed_list); + iotcon_list_free(list); + return NULL; + } list->type = IOTCON_TYPE_LIST; } else if (JSON_NODE_HOLDS_OBJECT(child_node)) { @@ -703,9 +831,16 @@ iotcon_list_h ic_list_from_json(JsonArray *parray) ERR("ic_value_new_repr(%p) Fail", ret_repr); iotcon_repr_free(ret_repr); iotcon_list_free(list); + return NULL; } - list = ic_list_insert(list, value, -1); + ret = ic_list_insert(list, value, -1); + if (IOTCON_ERROR_NONE != ret) { + ERR("ic_list_insert() Fail(%d)", ret); + iotcon_repr_free(ret_repr); + iotcon_list_free(list); + return NULL; + } list->type = IOTCON_TYPE_REPR; } } @@ -733,7 +868,7 @@ API void iotcon_list_free(iotcon_list_h list) static int _ic_list_clone_value(iotcon_list_h list, iotcon_list_h ret_list) { - int i, count; + int i, ret, count; iotcon_value_h value, copied_value; count = g_list_length(list->list); @@ -750,7 +885,12 @@ static int _ic_list_clone_value(iotcon_list_h list, iotcon_list_h ret_list) return IOTCON_ERROR_FAIL; } - ic_list_insert(ret_list, copied_value, -1); + ret = ic_list_insert(ret_list, copied_value, -1); + if (IOTCON_ERROR_NONE != ret) { + ERR("ic_list_insert() Fail"); + ic_value_free(copied_value); + return IOTCON_ERROR_FAIL; + } } return IOTCON_ERROR_NONE; @@ -759,15 +899,16 @@ static int _ic_list_clone_value(iotcon_list_h list, iotcon_list_h ret_list) static int _ic_list_clone_list(iotcon_list_h list, iotcon_list_h ret_list) { - int i, count; + int i, ret, count; + iotcon_value_h value; iotcon_list_h list_val, copied_list; count = g_list_length(list->list); for (i = 0; i < count; i++) { - list_val = iotcon_list_get_nth_list(list, i); - if (NULL != list_val) { - ERR("iotcon_list_get_nth_list() Fail"); + ret = iotcon_list_get_nth_list(list, i, &list_val); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_list_get_nth_list() Fail(%d)", ret); return IOTCON_ERROR_FAIL; } @@ -784,7 +925,12 @@ static int _ic_list_clone_list(iotcon_list_h list, iotcon_list_h ret_list) return IOTCON_ERROR_FAIL; } - ret_list = ic_list_insert(ret_list, value, -1); + ret = ic_list_insert(ret_list, value, -1); + if (IOTCON_ERROR_NONE != ret) { + ERR("ic_list_insert(%d) Fail", ret); + ic_value_free(value); + return IOTCON_ERROR_FAIL; + } } return IOTCON_ERROR_NONE; @@ -793,14 +939,14 @@ static int _ic_list_clone_list(iotcon_list_h list, iotcon_list_h ret_list) static int _ic_list_clone_repr(iotcon_list_h list, iotcon_list_h ret_list) { - int i, count; + int i, ret, count; iotcon_value_h value; iotcon_repr_h repr_val, copied_repr; count = g_list_length(list->list); for (i = 0; i < count; i++) { - repr_val = iotcon_list_get_nth_repr(list, i); - if (NULL == repr_val) { + ret = iotcon_list_get_nth_repr(list, i, &repr_val); + if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_list_get_nth_repr() Fail"); return IOTCON_ERROR_FAIL; } @@ -818,7 +964,12 @@ static int _ic_list_clone_repr(iotcon_list_h list, iotcon_list_h ret_list) return IOTCON_ERROR_FAIL; } - ret_list = ic_list_insert(ret_list, value, -1); + ret = ic_list_insert(ret_list, value, -1); + if (IOTCON_ERROR_NONE != ret) { + ERR("ic_list_insert(%d) Fail", ret); + ic_value_free(value); + return IOTCON_ERROR_FAIL; + } } return IOTCON_ERROR_NONE; diff --git a/lib/ic-repr-list.h b/lib/ic-repr-list.h index df9cd3c..4af567d 100755 --- a/lib/ic-repr-list.h +++ b/lib/ic-repr-list.h @@ -23,11 +23,12 @@ struct ic_list_s { int type; + int ref_count; GList *list; }; int ic_list_remove(iotcon_list_h list, iotcon_value_h val); -iotcon_list_h ic_list_insert(iotcon_list_h list, iotcon_value_h value, int pos); +int ic_list_insert(iotcon_list_h list, iotcon_value_h value, int pos); JsonArray* ic_list_to_json(iotcon_list_h list); iotcon_list_h ic_list_from_json(JsonArray *parray); diff --git a/lib/ic-repr-obj.c b/lib/ic-repr-obj.c index 2c0e006..88bf741 100755 --- a/lib/ic-repr-obj.c +++ b/lib/ic-repr-obj.c @@ -26,7 +26,7 @@ #include "ic-repr-obj.h" int ic_obj_del_value(iotcon_repr_h repr, const char *key, - iotcon_repr_types_e value_type) + iotcon_types_e value_type) { gboolean ret = FALSE; iotcon_value_h value = NULL; @@ -54,23 +54,26 @@ int ic_obj_del_value(iotcon_repr_h repr, const char *key, return IOTCON_ERROR_NONE; } -API int iotcon_repr_get_int(iotcon_repr_h repr, const char *key) +API int iotcon_repr_get_int(iotcon_repr_h repr, const char *key, int *val) { iotcon_value_h value; - RETV_IF(NULL == repr, 0); - RETV_IF(NULL == key, 0); + RETV_IF(NULL == repr, IOTCON_ERROR_PARAM); + RETV_IF(NULL == key, IOTCON_ERROR_PARAM); + RETV_IF(NULL == val, IOTCON_ERROR_PARAM); value = g_hash_table_lookup(repr->hash_table, key); if (NULL == value) { ERR("g_hash_table_lookup() Fail"); - return 0; + return IOTCON_ERROR_NO_DATA; } ic_basic_s *real = (ic_basic_s*)value; - RETV_IF(IOTCON_TYPE_INT != real->type, 0); + RETV_IF(IOTCON_TYPE_INT != real->type, IOTCON_ERROR_FAIL); - return real->val.i; + *val = real->val.i; + + return IOTCON_ERROR_NONE; } API int iotcon_repr_set_int(iotcon_repr_h repr, const char *key, int val) @@ -105,24 +108,27 @@ API int iotcon_repr_del_int(iotcon_repr_h repr, const char *key) return ret; } -API bool iotcon_repr_get_bool(iotcon_repr_h repr, const char *key) +API int iotcon_repr_get_bool(iotcon_repr_h repr, const char *key, bool *val) { ic_basic_s *real = NULL; iotcon_value_h value = NULL; - RETV_IF(NULL == repr, false); - RETV_IF(NULL == key, false); + RETV_IF(NULL == repr, IOTCON_ERROR_PARAM); + RETV_IF(NULL == key, IOTCON_ERROR_PARAM); + RETV_IF(NULL == val, IOTCON_ERROR_PARAM); value = g_hash_table_lookup(repr->hash_table, key); if (NULL == value) { ERR("g_hash_table_lookup() Fail"); - return false; + return IOTCON_ERROR_NO_DATA; } real = (ic_basic_s*)value; - RETV_IF(IOTCON_TYPE_BOOL != real->type, false); + RETV_IF(IOTCON_TYPE_BOOL != real->type, IOTCON_ERROR_FAIL); + + *val = real->val.b; - return real->val.b; + return IOTCON_ERROR_NONE; } API int iotcon_repr_set_bool(iotcon_repr_h repr, const char *key, bool val) @@ -154,24 +160,27 @@ API int iotcon_repr_del_bool(iotcon_repr_h repr, const char *key) return ret; } -API double iotcon_repr_get_double(iotcon_repr_h repr, const char *key) +API int iotcon_repr_get_double(iotcon_repr_h repr, const char *key, double *val) { ic_basic_s *real = NULL; iotcon_value_h value = NULL; - RETV_IF(NULL == repr, 0.0); - RETV_IF(NULL == key, 0.0); + RETV_IF(NULL == repr, IOTCON_ERROR_PARAM); + RETV_IF(NULL == key, IOTCON_ERROR_PARAM); + RETV_IF(NULL == val, IOTCON_ERROR_PARAM); value = g_hash_table_lookup(repr->hash_table, key); if (NULL == value) { ERR("g_hash_table_lookup() Fail"); - return 0; + return IOTCON_ERROR_NO_DATA; } real = (ic_basic_s*)value; - RETV_IF(IOTCON_TYPE_DOUBLE != real->type, 0.0); + RETV_IF(IOTCON_TYPE_DOUBLE != real->type, IOTCON_ERROR_FAIL); + + *val = real->val.d; - return real->val.d; + return IOTCON_ERROR_NONE; } API int iotcon_repr_set_double(iotcon_repr_h repr, const char *key, double val) @@ -206,24 +215,27 @@ API int iotcon_repr_del_double(iotcon_repr_h repr, const char *key) return ret; } -API char* iotcon_repr_get_str(iotcon_repr_h repr, const char *key) +API int iotcon_repr_get_str(iotcon_repr_h repr, const char *key, char **val) { ic_basic_s *real = NULL; iotcon_value_h value = NULL; - RETV_IF(NULL == repr, NULL); - RETV_IF(NULL == key, NULL); + RETV_IF(NULL == repr, IOTCON_ERROR_PARAM); + RETV_IF(NULL == key, IOTCON_ERROR_PARAM); + RETV_IF(NULL == val, IOTCON_ERROR_PARAM); value = g_hash_table_lookup(repr->hash_table, key); if (NULL == value) { ERR("g_hash_table_lookup() Fail"); - return NULL; + return IOTCON_ERROR_NO_DATA; } real = (ic_basic_s*)value; - RETV_IF(IOTCON_TYPE_STR != real->type, NULL); + RETV_IF(IOTCON_TYPE_STR != real->type, IOTCON_ERROR_FAIL); - return real->val.s; + *val = real->val.s; + + return IOTCON_ERROR_NONE; } API int iotcon_repr_set_str(iotcon_repr_h repr, const char *key, char *val) @@ -232,6 +244,7 @@ API int iotcon_repr_set_str(iotcon_repr_h repr, const char *key, char *val) RETV_IF(NULL == repr, IOTCON_ERROR_PARAM); RETV_IF(NULL == key, IOTCON_ERROR_PARAM); + RETV_IF(NULL == val, IOTCON_ERROR_PARAM); value = ic_value_new_str(val); if (NULL == value) { @@ -309,24 +322,27 @@ API int iotcon_repr_del_null(iotcon_repr_h repr, const char *key) return ret; } -API iotcon_list_h iotcon_repr_get_list(iotcon_repr_h repr, const char *key) +API int iotcon_repr_get_list(iotcon_repr_h repr, const char *key, iotcon_list_h *list) { iotcon_value_h value = NULL; ic_val_list_s *real = NULL; - RETV_IF(NULL == repr, NULL); - RETV_IF(NULL == key, NULL); + RETV_IF(NULL == repr, IOTCON_ERROR_PARAM); + RETV_IF(NULL == key, IOTCON_ERROR_PARAM); + RETV_IF(NULL == list, IOTCON_ERROR_PARAM); value = g_hash_table_lookup(repr->hash_table, key); if (NULL == value) { ERR("g_hash_table_lookup() Fail"); - return NULL; + return IOTCON_ERROR_NO_DATA; } real = (ic_val_list_s*)value; - RETV_IF(IOTCON_TYPE_LIST != real->type, NULL); + RETV_IF(IOTCON_TYPE_LIST != real->type, IOTCON_ERROR_FAIL); - return real->list; + *list = real->list; + + return IOTCON_ERROR_NONE; } API int iotcon_repr_set_list(iotcon_repr_h repr, const char *key, iotcon_list_h list) @@ -335,6 +351,7 @@ API int iotcon_repr_set_list(iotcon_repr_h repr, const char *key, iotcon_list_h RETV_IF(NULL == repr, IOTCON_ERROR_PARAM); RETV_IF(NULL == key, IOTCON_ERROR_PARAM); + RETV_IF(NULL == list, IOTCON_ERROR_PARAM); value = ic_value_new_list(list); if (NULL == value) { @@ -362,24 +379,28 @@ API int iotcon_repr_del_list(iotcon_repr_h repr, const char *key) return ret; } -API iotcon_repr_h iotcon_repr_get_repr(iotcon_repr_h repr, const char *key) + +API int iotcon_repr_get_repr(iotcon_repr_h src, const char *key, iotcon_repr_h *dest) { ic_val_repr_s *real = NULL; iotcon_value_h value = NULL; - RETV_IF(NULL == repr, NULL); - RETV_IF(NULL == key, NULL); + RETV_IF(NULL == src, IOTCON_ERROR_PARAM); + RETV_IF(NULL == key, IOTCON_ERROR_PARAM); + RETV_IF(NULL == dest, IOTCON_ERROR_PARAM); - value = g_hash_table_lookup(repr->hash_table, key); + value = g_hash_table_lookup(src->hash_table, key); if (NULL == value) { ERR("g_hash_table_lookup() Fail"); - return NULL; + return IOTCON_ERROR_NO_DATA; } real = (ic_val_repr_s*)value; - RETV_IF(IOTCON_TYPE_REPR != real->type, NULL); + RETV_IF(IOTCON_TYPE_REPR != real->type, IOTCON_ERROR_FAIL); + + *dest = real->repr; - return real->repr; + return IOTCON_ERROR_NONE; } API int iotcon_repr_set_repr(iotcon_repr_h repr, const char *key, iotcon_repr_h val) @@ -416,6 +437,24 @@ API int iotcon_repr_del_repr(iotcon_repr_h repr, const char *key) return ret; } +API int iotcon_repr_get_type(iotcon_repr_h repr, const char *key, int *type) +{ + iotcon_value_h value = NULL; + + RETV_IF(NULL == repr, IOTCON_ERROR_PARAM); + RETV_IF(NULL == key, IOTCON_ERROR_PARAM); + RETV_IF(NULL == type, IOTCON_ERROR_PARAM); + + value = g_hash_table_lookup(repr->hash_table, key); + if (NULL == value) { + ERR("g_hash_table_lookup() Fail"); + return IOTCON_ERROR_NO_DATA; + } + *type = value->type; + + return IOTCON_ERROR_NONE; +} + int ic_obj_set_value(iotcon_repr_h repr, const char *key, iotcon_value_h value) { RETV_IF(NULL == repr, IOTCON_ERROR_PARAM); @@ -431,7 +470,7 @@ static inline int _ic_obj_to_json(GHashTable *hash, iotcon_str_list_s *key_list, unsigned int index, JsonObject *json_obj) { FN_CALL; - int type; + int type, ret; const char *key; iotcon_repr_h child_repr = NULL; iotcon_list_h child_list = NULL; @@ -465,7 +504,12 @@ static inline int _ic_obj_to_json(GHashTable *hash, iotcon_str_list_s *key_list, json_object_set_member(json_obj, key, child_node); break; case IOTCON_TYPE_LIST: - child_list = ic_value_get_list(value); + ret = ic_value_get_list(value, &child_list); + if (IOTCON_ERROR_NONE != ret) { + ERR("ic_value_get_list() Fail(%d)", ret); + return IOTCON_ERROR_FAIL; + } + child_array = ic_list_to_json(child_list); if (NULL == child_array) { ERR("ic_list_to_json() Fail"); @@ -476,7 +520,12 @@ static inline int _ic_obj_to_json(GHashTable *hash, iotcon_str_list_s *key_list, json_object_set_member(json_obj, key, child_node); break; case IOTCON_TYPE_REPR: - child_repr = ic_value_get_repr(value); + ret = ic_value_get_repr(value, &child_repr); + if (IOTCON_ERROR_NONE != ret) { + ERR("ic_value_get_repr() Fail(%d)", ret); + return IOTCON_ERROR_FAIL; + } + child_obj = ic_obj_to_json(child_repr); if (NULL == child_obj) { ERR("ic_obj_to_json() Fail"); diff --git a/lib/ic-repr-obj.h b/lib/ic-repr-obj.h index 636f26d..a9ffbad 100644 --- a/lib/ic-repr-obj.h +++ b/lib/ic-repr-obj.h @@ -22,7 +22,7 @@ #include "iotcon-constant.h" int ic_obj_del_value(iotcon_repr_h repr, const char *key, - iotcon_repr_types_e value_type); + iotcon_types_e value_type); int ic_repr_obj_get_value(iotcon_repr_h repr, const char *key, iotcon_value_h *retval); int ic_obj_set_value(iotcon_repr_h repr, const char *key, iotcon_value_h value); diff --git a/lib/ic-repr-value.c b/lib/ic-repr-value.c index a82e6f1..3c41c7d 100644 --- a/lib/ic-repr-value.c +++ b/lib/ic-repr-value.c @@ -166,72 +166,89 @@ iotcon_value_h ic_value_new_repr(iotcon_repr_h val) return (iotcon_value_h)value; } -int ic_value_get_int(iotcon_value_h value) +int ic_value_get_int(iotcon_value_h value, int *val) { ic_basic_s *real = (ic_basic_s*)value; - RETV_IF(NULL == value, 0); - RETVM_IF(IOTCON_TYPE_INT != real->type, 0, "IOTCON_ERROR_PARAM(%d)", real->type); + RETV_IF(NULL == value, IOTCON_ERROR_PARAM); + RETVM_IF(IOTCON_TYPE_INT != real->type, IOTCON_ERROR_PARAM, "Invalid Type(%d)", + real->type); - return real->val.i; + *val = real->val.i; + + return IOTCON_ERROR_NONE; } -bool ic_value_get_bool(iotcon_value_h value) +int ic_value_get_bool(iotcon_value_h value, bool *val) { ic_basic_s *real = (ic_basic_s*)value; - RETV_IF(NULL == value, false); - RETVM_IF(IOTCON_TYPE_BOOL != real->type, false, "IOTCON_ERROR_PARAM(%d)", real->type); + RETV_IF(NULL == value, IOTCON_ERROR_PARAM); + RETVM_IF(IOTCON_TYPE_BOOL != real->type, IOTCON_ERROR_PARAM, "Invalid Type(%d)", + real->type); + + *val = real->val.b; - return real->val.b; + return IOTCON_ERROR_NONE; } -double ic_value_get_double(iotcon_value_h value) +int ic_value_get_double(iotcon_value_h value, double *val) { ic_basic_s *real = (ic_basic_s*)value; - RETV_IF(NULL == value, 0.0); - RETVM_IF(IOTCON_TYPE_DOUBLE != real->type, 0.0, "IOTCON_ERROR_PARAM(%d)", real->type); + RETV_IF(NULL == value, IOTCON_ERROR_PARAM); + RETVM_IF(IOTCON_TYPE_DOUBLE != real->type, IOTCON_ERROR_PARAM, "Invalid Type(%d)", + real->type); - return real->val.d; + *val = real->val.d; + + return IOTCON_ERROR_NONE; } -const char* ic_value_get_str(iotcon_value_h value) +int ic_value_get_str(iotcon_value_h value, const char **val) { ic_basic_s *real = (ic_basic_s*)value; - RETV_IF(NULL == value, NULL); - RETVM_IF(IOTCON_TYPE_STR != real->type, NULL, "IOTCON_ERROR_PARAM(%d)", real->type); + RETV_IF(NULL == value, IOTCON_ERROR_PARAM); + RETVM_IF(IOTCON_TYPE_STR != real->type, IOTCON_ERROR_PARAM, "Invalid Type(%d)", + real->type); - return real->val.s; + *val = real->val.s; + + return IOTCON_ERROR_NONE; } -iotcon_list_h ic_value_get_list(iotcon_value_h value) +int ic_value_get_list(iotcon_value_h value, iotcon_list_h *list) { ic_val_list_s *real = (ic_val_list_s*)value; - RETV_IF(NULL == value, NULL); - RETVM_IF(IOTCON_TYPE_LIST != real->type, NULL, "IOTCON_ERROR_PARAM(%d)", real->type); + RETV_IF(NULL == value, IOTCON_ERROR_PARAM); + RETVM_IF(IOTCON_TYPE_LIST != real->type, IOTCON_ERROR_PARAM, "Invalid Type(%d)", + real->type); - return real->list; -} + *list = real->list; + return IOTCON_ERROR_NONE; +} -iotcon_repr_h ic_value_get_repr(iotcon_value_h value) +int ic_value_get_repr(iotcon_value_h value, iotcon_repr_h *repr) { ic_val_repr_s *real = (ic_val_repr_s*)value; - RETV_IF(NULL == value, NULL); - RETVM_IF(IOTCON_TYPE_REPR != real->type, NULL, "IOTCON_ERROR_PARAM(%d)", real->type); + RETV_IF(NULL == value, IOTCON_ERROR_PARAM); + RETVM_IF(IOTCON_TYPE_REPR != real->type, IOTCON_ERROR_PARAM, "Invalid Type(%d)", + real->type); - return real->repr; + *repr = real->repr; + + return IOTCON_ERROR_NONE; } /* * A general result : 1 * : true -* : 5.5 +* : 5.5 * : "Hello" */ JsonNode* ic_value_to_json(iotcon_value_h value) @@ -329,7 +346,12 @@ API iotcon_value_h ic_value_from_json(JsonNode *node) 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; RET_IF(NULL == data); @@ -338,17 +360,34 @@ void ic_value_free(gpointer data) int type = value->type; switch (type) { case IOTCON_TYPE_STR: - free((char*)ic_value_get_str(value)); + ret = ic_value_get_str(value, &str); + if (IOTCON_ERROR_NONE != ret) { + ERR("ic_value_get_str() Fail(%d)", ret); + break; + } + free((char*)str); case IOTCON_TYPE_INT: case IOTCON_TYPE_BOOL: case IOTCON_TYPE_DOUBLE: case IOTCON_TYPE_NULL: break; case IOTCON_TYPE_LIST: - iotcon_list_free(ic_value_get_list(value)); + DBG("value is list"); + ret = ic_value_get_list(value, &list); + if (IOTCON_ERROR_NONE != ret) { + ERR("ic_value_get_list() Fail(%d)", ret); + break; + } + iotcon_list_free(list); break; case IOTCON_TYPE_REPR: - iotcon_repr_free(ic_value_get_repr(value)); + DBG("value is Repr"); + ret = ic_value_get_repr(value, &repr); + if (IOTCON_ERROR_NONE != ret) { + ERR("ic_value_get_repr() Fail(%d)", ret); + break; + } + iotcon_repr_free(repr); break; default: ERR("Invalid type(%d)", type); diff --git a/lib/ic-repr-value.h b/lib/ic-repr-value.h index 16cabe6..6b92cef 100755 --- a/lib/ic-repr-value.h +++ b/lib/ic-repr-value.h @@ -54,12 +54,12 @@ iotcon_value_h ic_value_new_list(iotcon_list_h val); iotcon_value_h ic_value_new_repr(iotcon_repr_h val); -int ic_value_get_int(iotcon_value_h value); -bool ic_value_get_bool(iotcon_value_h value); -double ic_value_get_double(iotcon_value_h value); -const char* ic_value_get_str(iotcon_value_h value); -iotcon_list_h ic_value_get_list(iotcon_value_h value); -iotcon_repr_h ic_value_get_repr(iotcon_value_h value); +int ic_value_get_int(iotcon_value_h value, int *val); +int ic_value_get_bool(iotcon_value_h value, bool *val); +int ic_value_get_double(iotcon_value_h value, double *val); +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); +int ic_value_get_repr(iotcon_value_h value, iotcon_repr_h *repr); JsonNode* ic_value_to_json(iotcon_value_h value); diff --git a/lib/ic-repr.c b/lib/ic-repr.c index 6b0eb76..577e562 100644 --- a/lib/ic-repr.c +++ b/lib/ic-repr.c @@ -24,6 +24,7 @@ #include "iotcon-representation.h" #include "ic-common.h" #include "ic-utils.h" +#include "ic-ioty.h" #include "ic-repr-list.h" #include "ic-repr-value.h" #include "ic-repr-obj.h" @@ -64,143 +65,76 @@ API iotcon_repr_h iotcon_repr_new() return NULL; } - ret_val->hash_table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, + ret_val->hash_table = g_hash_table_new_full(g_str_hash, g_str_equal, free, ic_value_free); ic_repr_inc_ref_count(ret_val); return ret_val; } -API const char* iotcon_repr_get_uri(iotcon_repr_h repr) -{ - RETV_IF(NULL == repr, NULL); - - return repr->uri; -} - -API int iotcon_repr_set_uri(iotcon_repr_h repr, const char *uri) +API int iotcon_repr_get_uri(iotcon_repr_h repr, const char **uri) { RETV_IF(NULL == repr, IOTCON_ERROR_PARAM); RETV_IF(NULL == uri, IOTCON_ERROR_PARAM); - repr->uri = ic_utils_strdup(uri); - if (NULL == repr->uri) { - ERR("ic_utils_strdup() Fail"); - return IOTCON_ERROR_MEMORY; - } + *uri = repr->uri; return IOTCON_ERROR_NONE; } -API int iotcon_repr_del_uri(iotcon_repr_h repr) +API int iotcon_repr_set_uri(iotcon_repr_h repr, const char *uri) { RETV_IF(NULL == repr, IOTCON_ERROR_PARAM); free(repr->uri); repr->uri = NULL; - return IOTCON_ERROR_NONE; -} - -API void iotcon_repr_get_resource_types(iotcon_repr_h repr, iotcon_resourcetype_fn fn, - void *user_data) -{ - RET_IF(NULL == repr); - - /* (GFunc) : fn needs to use "const" qualifier */ - g_list_foreach(repr->res_types, (GFunc)fn, user_data); -} - -API int iotcon_repr_get_resource_types_count(iotcon_repr_h repr) -{ - RETV_IF(NULL == repr, IOTCON_ERROR_PARAM); - - return g_list_length(repr->res_types); -} - -API int iotcon_repr_append_resource_types(iotcon_repr_h repr, const char *type) -{ - char *res_type = NULL; - - RETV_IF(NULL == repr, IOTCON_ERROR_PARAM); - RETV_IF(NULL == type, IOTCON_ERROR_PARAM); - - res_type = ic_utils_strdup(type); - if (NULL == res_type) { - ERR("ic_utils_strdup() Fail"); - return IOTCON_ERROR_MEMORY; + if (NULL != uri) { + repr->uri = ic_utils_strdup(uri); + if (NULL == repr->uri) { + ERR("ic_utils_strdup() Fail"); + return IOTCON_ERROR_MEMORY; + } } - repr->res_types = g_list_append(repr->res_types, res_type); - return IOTCON_ERROR_NONE; } -API int iotcon_repr_del_resource_types(iotcon_repr_h repr, const char *type) +API int iotcon_repr_get_resource_types(iotcon_repr_h repr, iotcon_str_list_s **types) { - GList *cur = NULL; RETV_IF(NULL == repr, IOTCON_ERROR_PARAM); - RETV_IF(NULL == type, IOTCON_ERROR_PARAM); - RETV_IF(NULL == repr->res_types, IOTCON_ERROR_PARAM); - - cur = g_list_find_custom(repr->res_types, type, (GCompareFunc)g_strcmp0); - if (NULL == cur) { - WARN("g_list_find(%s) returns NULL", type); - return IOTCON_ERROR_NO_DATA; - } + RETV_IF(NULL == types, IOTCON_ERROR_PARAM); - repr->res_types = g_list_delete_link(repr->res_types, cur); + *types = repr->res_types; return IOTCON_ERROR_NONE; } -API void iotcon_repr_get_resource_interfaces(iotcon_repr_h repr, iotcon_interface_fn fn, - void *user_data) +API int iotcon_repr_set_resource_types(iotcon_repr_h repr, iotcon_str_list_s *types) { - RET_IF(NULL == repr); + RETV_IF(NULL == repr, IOTCON_ERROR_PARAM); - /* (GFunc) : fn needs to use "const" qualifier */ - g_list_foreach(repr->interfaces, (GFunc)fn, user_data); -} + iotcon_str_list_free(repr->res_types); + repr->res_types = NULL; -API int iotcon_repr_get_resource_interfaces_count(iotcon_repr_h repr) -{ - RETV_IF(NULL == repr, IOTCON_ERROR_PARAM); + if (NULL != types) + repr->res_types = iotcon_str_list_clone(types); - return g_list_length(repr->interfaces); + return IOTCON_ERROR_NONE; } -API int iotcon_repr_append_resource_interfaces(iotcon_repr_h repr, const char *iface) +API int iotcon_repr_get_resource_interfaces(iotcon_repr_h repr) { - char *res_interface = NULL; - - RETV_IF(NULL == repr, IOTCON_ERROR_PARAM); - RETV_IF(NULL == iface, IOTCON_ERROR_PARAM); + RETV_IF(NULL == repr, IOTCON_INTERFACE_NONE); - res_interface = ic_utils_strdup(iface); - if (NULL == res_interface) { - ERR("ic_utils_strdup() Fail"); - return IOTCON_ERROR_MEMORY; - } - - repr->interfaces = g_list_append(repr->interfaces, res_interface); - return IOTCON_ERROR_NONE; + return repr->interfaces; } -API int iotcon_repr_del_resource_interface(iotcon_repr_h repr, const char *iface) +API int iotcon_repr_set_resource_interfaces(iotcon_repr_h repr, int ifaces) { - GList *cur = NULL; RETV_IF(NULL == repr, IOTCON_ERROR_PARAM); - RETV_IF(NULL == iface, IOTCON_ERROR_PARAM); - RETV_IF(NULL == repr->interfaces, IOTCON_ERROR_PARAM); - cur = g_list_find_custom(repr->interfaces, iface, (GCompareFunc)g_strcmp0); - if (NULL == cur) { - WARN("g_list_find(%s) returns NULL", iface); - return IOTCON_ERROR_NO_DATA; - } - - repr->interfaces = g_list_delete_link(repr->interfaces, cur); + repr->interfaces = ifaces; return IOTCON_ERROR_NONE; } @@ -216,29 +150,46 @@ API int iotcon_repr_append_child(iotcon_repr_h parent, iotcon_repr_h child) return IOTCON_ERROR_NONE; } -API void iotcon_repr_get_children(iotcon_repr_h parent, iotcon_children_fn fn, +API int iotcon_repr_get_children(iotcon_repr_h parent, iotcon_children_fn fn, void *user_data) { - RET_IF(NULL == parent); + GList *list, *next; + + RETV_IF(NULL == parent, IOTCON_ERROR_PARAM); + RETV_IF(NULL == fn, IOTCON_ERROR_PARAM); + + list = parent->children; + while (list) { + next = list->next; + if (IOTCON_FUNC_STOP == fn(list->data, user_data)) + break; + list = next; + } - /* (GFunc) : fn needs to use "const" qualifier */ - g_list_foreach(parent->children, (GFunc)fn, user_data); + return IOTCON_ERROR_NONE; } -API int iotcon_repr_get_children_count(iotcon_repr_h parent) +API unsigned int iotcon_repr_get_children_count(iotcon_repr_h parent) { - RETV_IF(NULL == parent, IOTCON_ERROR_PARAM); - RETV_IF(NULL == parent->children, IOTCON_ERROR_PARAM); + RETV_IF(NULL == parent, 0); + RETV_IF(NULL == parent->children, 0); return g_list_length(parent->children); } -API iotcon_repr_h iotcon_repr_get_nth_child(iotcon_repr_h parent, int index) +API int iotcon_repr_get_nth_child(iotcon_repr_h parent, int index, iotcon_repr_h *child) { - RETV_IF(NULL == parent, NULL); - RETV_IF(NULL == parent->children, NULL); + RETV_IF(NULL == parent, IOTCON_ERROR_PARAM); + RETV_IF(NULL == parent->children, IOTCON_ERROR_PARAM); + RETV_IF(NULL == child, IOTCON_ERROR_PARAM); - return g_list_nth_data(parent->children, index); + *child = g_list_nth_data(parent->children, index); + if (NULL == *child) { + ERR("g_list_nth_data() Fail"); + return IOTCON_ERROR_NO_DATA; + } + + return IOTCON_ERROR_NONE; } API iotcon_str_list_s* iotcon_repr_get_key_list(iotcon_repr_h repr) @@ -265,18 +216,12 @@ API int iotcon_repr_get_keys_count(iotcon_repr_h repr) return g_hash_table_size(repr->hash_table); } -void _ic_repr_get_res_type_fn(const char *res_type, void *user_data) +static int _ic_repr_get_res_type_fn(const char *res_type, void *user_data) { JsonArray *rt_array = user_data; json_array_add_string_element(rt_array, res_type); - DBG("res_type(%s)", res_type); -} -void _ic_repr_get_res_interface_fn(const char *res_if, void *user_data) -{ - JsonArray *if_array = user_data; - json_array_add_string_element(if_array, res_if); - DBG("res_if(%s)", res_if); + return IOTCON_FUNC_CONTINUE; } /* @@ -286,52 +231,77 @@ void _ic_repr_get_res_interface_fn(const char *res_if, void *user_data) static JsonObject* _ic_repr_data_generate_json(iotcon_repr_h cur_repr, unsigned int child_index) { - FN_CALL; + int i, ret, ifaces; + char *iface_str; + const char *uri; JsonObject *repr_obj = NULL; unsigned int rt_count = 0; - unsigned int if_count = 0; JsonObject *prop_obj = NULL; + iotcon_str_list_s *resource_types = NULL; RETV_IF(NULL == cur_repr, NULL); if (0 < iotcon_repr_get_keys_count(cur_repr)) { repr_obj = ic_obj_to_json(cur_repr); if (NULL == repr_obj) { - ERR("ic_obj_to_json() Fail()"); + ERR("ic_obj_to_json() Fail"); json_object_unref(repr_obj); return NULL; } } - else + else { repr_obj = json_object_new(); + } if (cur_repr->uri) { - const char *uri = iotcon_repr_get_uri(cur_repr); + iotcon_repr_get_uri(cur_repr, &uri); json_object_set_string_member(repr_obj, IOTCON_KEY_URI, uri); } - if (cur_repr->res_types) { - rt_count = iotcon_repr_get_resource_types_count(cur_repr); - } - if (cur_repr->interfaces) { - if_count = iotcon_repr_get_resource_interfaces_count(cur_repr); - } + if (cur_repr->res_types) + rt_count = iotcon_str_list_length(cur_repr->res_types); - if (0 < rt_count || 0 < if_count) { + if (0 < rt_count || IOTCON_INTERFACE_NONE != cur_repr->interfaces) { prop_obj = json_object_new(); json_object_set_object_member(repr_obj, IOTCON_KEY_PROPERTY, prop_obj); } if (0 < rt_count) { JsonArray *rt_array = json_array_new(); - iotcon_repr_get_resource_types(cur_repr, _ic_repr_get_res_type_fn, rt_array); + + ret = iotcon_repr_get_resource_types(cur_repr, &resource_types); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_repr_get_resource_types() Fail(%d)", ret); + json_object_unref(repr_obj); + return NULL; + } + + ret = iotcon_str_list_foreach(resource_types, _ic_repr_get_res_type_fn, + rt_array); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_str_list_foreach() Fail"); + json_object_unref(repr_obj); + return NULL; + } json_object_set_array_member(prop_obj, IOTCON_KEY_RESOURCETYPES, rt_array); } - if (0 < if_count) { + if (IOTCON_INTERFACE_NONE != cur_repr->interfaces) { JsonArray *if_array = json_array_new(); - iotcon_repr_get_resource_interfaces(cur_repr, _ic_repr_get_res_interface_fn, - if_array); + ifaces = iotcon_repr_get_resource_interfaces(cur_repr); + for (i = 1; i < IOTCON_INTERFACE_MAX; i = i << 1) { + if (IOTCON_INTERFACE_NONE == (ifaces & i)) /* this interface not exist */ + continue; + ret = ic_ioty_convert_interface_flag((ifaces & i), &iface_str); + if (IOTCON_ERROR_NONE != ret) { + ERR("ic_ioty_convert_interface_flag(%d) Fail(%d)", i, ret); + json_object_unref(repr_obj); + json_array_unref(if_array); + return NULL; + } + json_array_add_string_element(if_array, iface_str); + } + json_object_set_array_member(prop_obj, IOTCON_KEY_INTERFACES, if_array); } @@ -342,6 +312,7 @@ static JsonObject* _ic_repr_data_generate_json(iotcon_repr_h cur_repr, static JsonObject* _ic_repr_data_generate_parent(iotcon_repr_h cur_repr, unsigned int child_index) { + FN_CALL; JsonObject *obj = _ic_repr_data_generate_json(cur_repr, child_index); if (NULL == obj) { ERR("_ic_repr_data_generate_json() Fail"); @@ -354,6 +325,7 @@ static JsonObject* _ic_repr_data_generate_parent(iotcon_repr_h cur_repr, static JsonObject* _ic_repr_data_generate_child(iotcon_repr_h cur_repr, unsigned int child_index) { + FN_CALL; JsonObject *obj = _ic_repr_data_generate_json(cur_repr, child_index); if (NULL == obj) { ERR("_ic_repr_data_generate_json() Fail"); @@ -397,7 +369,7 @@ static JsonObject* _ic_repr_generate_json(iotcon_repr_h repr) json_array_add_object_element(root_array, repr_obj); for (child_index = 0; child_index < child_count; child_index++) { - child_repr = iotcon_repr_get_nth_child(repr, child_index); + iotcon_repr_get_nth_child(repr, child_index, &child_repr); repr_obj = _ic_repr_data_generate_child(child_repr, child_index); if (NULL == repr_obj) { ERR("_ic_repr_data_generate_child() Fali(NULL == repr_obj)"); @@ -442,7 +414,10 @@ char* ic_repr_generate_json(iotcon_repr_h repr, bool set_pretty) */ iotcon_repr_h ic_repr_parse_json(const char *json_string) { + const char *iface_str = NULL; + const char *rtype_str = NULL; const char *uri_value = NULL; + iotcon_interface_e iface_flag; RETV_IF(NULL == json_string, NULL); @@ -484,23 +459,32 @@ iotcon_repr_h ic_repr_parse_json(const char *json_string) IOTCON_KEY_PROPERTY); if (json_object_has_member(property_obj, IOTCON_KEY_RESOURCETYPES)) { + iotcon_str_list_s *res_types = NULL; JsonArray *rt_array = json_object_get_array_member(property_obj, IOTCON_KEY_RESOURCETYPES); - unsigned int rt_count = json_array_get_length(rt_array); unsigned int rt_index = 0; + unsigned int rt_count = json_array_get_length(rt_array); + for (rt_index = 0; rt_index < rt_count; rt_index++) { - iotcon_repr_append_resource_types(repr, - json_array_get_string_element(rt_array, rt_index)); + rtype_str = json_array_get_string_element(rt_array, rt_index); + res_types = iotcon_str_list_append(res_types, rtype_str); } + iotcon_repr_set_resource_types(repr, res_types); } if (json_object_has_member(property_obj, IOTCON_KEY_INTERFACES)) { JsonArray *if_array = json_object_get_array_member(property_obj, IOTCON_KEY_INTERFACES); unsigned int if_count = json_array_get_length(if_array); - unsigned int if_index = 0; - for (if_index = 0; if_index < if_count; if_index++) - iotcon_repr_append_resource_interfaces(repr, - json_array_get_string_element(if_array, if_index)); + unsigned int if_index; + int ifaces = IOTCON_INTERFACE_NONE; + + for (if_index = 0; if_index < if_count; if_index++) { + iface_str = json_array_get_string_element(if_array, if_index); + ret = ic_ioty_convert_interface_string(iface_str, &iface_flag); + ifaces |= iface_flag; + } + iotcon_repr_set_resource_interfaces(repr, ifaces); + } } @@ -517,10 +501,10 @@ iotcon_repr_h ic_repr_parse_json(const char *json_string) return repr; } + API void iotcon_repr_free(iotcon_repr_h repr) { FN_CALL; - int ref_count; RET_IF(NULL == repr); if (false == _ic_repr_dec_ref_count(repr)) @@ -530,8 +514,10 @@ API void iotcon_repr_free(iotcon_repr_h repr) /* (GDestroyNotify) : iotcon_repr_h is proper type than gpointer */ g_list_free_full(repr->children, (GDestroyNotify)iotcon_repr_free); - g_list_free_full(repr->interfaces, g_free); - g_list_free_full(repr->res_types, g_free); + + /* repr->res_types COULD be not null */ + if (repr->res_types) + iotcon_str_list_free(repr->res_types); g_hash_table_destroy(repr->hash_table); free(repr); @@ -541,7 +527,7 @@ API void iotcon_repr_free(iotcon_repr_h repr) static void _ic_repr_obj_clone(char *key, iotcon_value_h src_val, iotcon_repr_h dest_repr) { FN_CALL; - int type = IOTCON_TYPE_NONE; + int type, ret; iotcon_value_h value, copied_val; iotcon_list_h child_list, copied_list; iotcon_repr_h child_repr, copied_repr; @@ -561,7 +547,12 @@ static void _ic_repr_obj_clone(char *key, iotcon_value_h src_val, iotcon_repr_h ic_obj_set_value(dest_repr, ic_utils_strdup(key), copied_val); break; case IOTCON_TYPE_LIST: - child_list = ic_value_get_list(src_val); + ret = ic_value_get_list(src_val, &child_list); + if (IOTCON_ERROR_NONE != ret) { + ERR("ic_value_get_list() Fail(%d)", ret); + return; + } + copied_list = ic_list_clone(child_list); if (NULL == copied_list) { ERR("ic_list_clone() Fail"); @@ -578,7 +569,12 @@ static void _ic_repr_obj_clone(char *key, iotcon_value_h src_val, iotcon_repr_h ic_obj_set_value(dest_repr, key, value); break; case IOTCON_TYPE_REPR: - child_repr = ic_value_get_repr(src_val); + ret = ic_value_get_repr(src_val, &child_repr); + if (IOTCON_ERROR_NONE != ret) { + ERR("ic_value_get_repr() Fail(%d)", ret); + return; + } + copied_repr = iotcon_repr_clone(child_repr); if (NULL == copied_repr) { ERR("ic_list_clone() Fail"); @@ -599,12 +595,6 @@ static void _ic_repr_obj_clone(char *key, iotcon_value_h src_val, iotcon_repr_h } } -static gpointer _ic_repr_copy_str(gconstpointer src, gpointer data) -{ - FN_CALL; - return ic_utils_strdup(src); -} - static gpointer _ic_repr_copy_repr(gconstpointer src, gpointer data) { FN_CALL; @@ -622,8 +612,8 @@ API iotcon_repr_h iotcon_repr_clone(const iotcon_repr_h src) dest = iotcon_repr_new(); if (src->uri) dest->uri = strdup(src->uri); - dest->interfaces = g_list_copy_deep(src->interfaces, _ic_repr_copy_str, NULL); - dest->res_types = g_list_copy_deep(src->res_types, _ic_repr_copy_str, NULL); + dest->interfaces = src->interfaces; + dest->res_types = iotcon_str_list_clone(src->res_types); dest->children = g_list_copy_deep(src->children, _ic_repr_copy_repr, NULL); g_hash_table_foreach(src->hash_table, (GHFunc)_ic_repr_obj_clone, dest); diff --git a/lib/ic-repr.h b/lib/ic-repr.h index 879bd90..27802b5 100755 --- a/lib/ic-repr.h +++ b/lib/ic-repr.h @@ -31,10 +31,11 @@ struct ic_repr_s { char *uri; + int ref_count; + int interfaces; GHashTable *hash_table; GList *children; - GList *res_types; - GList *interfaces; + iotcon_str_list_s *res_types; }; /** diff --git a/lib/ic-utils.c b/lib/ic-utils.c index a85ff92..95a155e 100755 --- a/lib/ic-utils.c +++ b/lib/ic-utils.c @@ -17,7 +17,7 @@ #include #include -#include "iotcon-struct.h" +#include "iotcon.h" #include "ic-common.h" #include "ic-utils.h" @@ -151,16 +151,18 @@ API unsigned int iotcon_str_list_length(iotcon_str_list_s *str_list) return length; } - -API void iotcon_str_list_foreach(iotcon_str_list_s *str_list, - iotcon_string_foreach_cb cb, void *user_data) +API int iotcon_str_list_foreach(iotcon_str_list_s *str_list, iotcon_string_foreach_cb cb, + void *user_data) { - RET_IF(NULL == str_list); + RETV_IF(NULL == str_list, IOTCON_ERROR_PARAM); while (str_list) { - cb(str_list->string, user_data); + if (false == cb(str_list->string, user_data)) + break; str_list = str_list->next; } + + return IOTCON_ERROR_NONE; } diff --git a/lib/include/iotcon-constant.h b/lib/include/iotcon-constant.h index 5dbe37d..988b882 100755 --- a/lib/include/iotcon-constant.h +++ b/lib/include/iotcon-constant.h @@ -46,6 +46,9 @@ #define IOTCON_CONTAINED_RESOURCES_MAX 5 */ +#define IOTCON_FUNC_STOP 0 +#define IOTCON_FUNC_CONTINUE 1 + /** * @brief Action associated with observation */ @@ -120,6 +123,6 @@ typedef enum { IOTCON_TYPE_NULL, IOTCON_TYPE_LIST, IOTCON_TYPE_REPR, -} iotcon_repr_types_e; +} iotcon_types_e; #endif /* __IOT_CONNECTIVITY_MANAGER_CONSTANT_H__ */ diff --git a/lib/include/iotcon-representation.h b/lib/include/iotcon-representation.h index 8601e4b..7e82758 100755 --- a/lib/include/iotcon-representation.h +++ b/lib/include/iotcon-representation.h @@ -24,43 +24,48 @@ iotcon_repr_h iotcon_repr_new(); void iotcon_repr_free(iotcon_repr_h repr); iotcon_repr_h iotcon_repr_clone(const iotcon_repr_h src); -int iotcon_repr_set_uri(iotcon_repr_h repr, const char *uri); -const char* iotcon_repr_get_uri(iotcon_repr_h repr); -int iotcon_repr_del_uri(iotcon_repr_h repr); - /** * @ingroup CAPI_IOT_CONNECTIVITY_MODULE * @brief Appends resource type name. * @since_tizen 3.0 - * @remarks Duplicate type names are allowed. + * @remarks Stored string is replaced with @a uri. If @a uri is NULL, stored string is set + * by NULL. * * @param[in] repr The handle to the Representation - * @param[in] type The resource type + * @param[in] uri The URI of resource * * @return 0 on success, otherwise a negative error value. * @retval #IOTCON_ERROR_NONE Successful * @retval #IOTCON_ERROR_MEMORY Out of memory * @retval #IOTCON_ERROR_PARAM Invalid parameter */ -int iotcon_repr_append_resource_types(iotcon_repr_h repr, const char *type); -typedef void (*iotcon_resourcetype_fn)(const char *res_type, void *user_data); -void iotcon_repr_get_resource_types(iotcon_repr_h repr, iotcon_resourcetype_fn fn, - void *user_data); -int iotcon_repr_get_resource_types_count(iotcon_repr_h repr); -int iotcon_repr_del_resource_types(iotcon_repr_h repr, const char *type); +int iotcon_repr_set_uri(iotcon_repr_h repr, const char *uri); +int iotcon_repr_get_uri(iotcon_repr_h repr, const char **uri); -int iotcon_repr_append_resource_interfaces(iotcon_repr_h repr, const char *iface); -typedef void (*iotcon_interface_fn)(const char *res_if, void *user_data); -void iotcon_repr_get_resource_interfaces(iotcon_repr_h repr, iotcon_interface_fn fn, - void *user_data); -int iotcon_repr_get_resource_interfaces_count(iotcon_repr_h repr); -int iotcon_repr_del_resource_interfaces(iotcon_repr_h repr, const char *type); +/** + * @ingroup CAPI_IOT_CONNECTIVITY_MODULE + * @brief Sets resource type list to the Representation. + * @since_tizen 3.0 + * @remarks Stored list is replaced with @a types. If @a types is NULL, stored list is set + * by NULL. + * @param[in] repr The handle to the Representation + * @param[in] types The resource type list + * + * @return 0 on success, otherwise a negative error value. + * @retval #IOTCON_ERROR_NONE Successful + * @retval #IOTCON_ERROR_PARAM Invalid parameter + */ +int iotcon_repr_set_resource_types(iotcon_repr_h repr, iotcon_str_list_s *types); +int iotcon_repr_get_resource_types(iotcon_repr_h repr, iotcon_str_list_s **types); + +int iotcon_repr_set_resource_interfaces(iotcon_repr_h repr, int ifaces); +int iotcon_repr_get_resource_interfaces(iotcon_repr_h repr); /** * @ingroup CAPI_IOT_CONNECTIVITY_MODULE * @brief Sets int value. * @since_tizen 3.0 - * @remarks Stored value is replaced with @a ival. + * @remarks Stored value is replaced with @a val. * * @param[in] repr The handle to the Representation * @param[in] type The resource type @@ -78,12 +83,12 @@ int iotcon_repr_set_list(iotcon_repr_h repr, const char *key, iotcon_list_h list int iotcon_repr_set_repr(iotcon_repr_h dest, const char *key, iotcon_repr_h src); int iotcon_repr_set_null(iotcon_repr_h repr, const char *key); -int iotcon_repr_get_int(iotcon_repr_h repr, const char *key); -bool iotcon_repr_get_bool(iotcon_repr_h repr, const char *key); -double iotcon_repr_get_double(iotcon_repr_h repr, const char *key); -char* iotcon_repr_get_str(iotcon_repr_h repr, const char *key); -iotcon_list_h iotcon_repr_get_list(iotcon_repr_h repr, const char *key); -iotcon_repr_h iotcon_repr_get_repr(iotcon_repr_h repr, const char *key); +int iotcon_repr_get_int(iotcon_repr_h repr, const char *key, int *val); +int iotcon_repr_get_bool(iotcon_repr_h repr, const char *key, bool *val); +int iotcon_repr_get_double(iotcon_repr_h repr, const char *key, double *val); +int iotcon_repr_get_str(iotcon_repr_h repr, const char *key, char **val); +int iotcon_repr_get_list(iotcon_repr_h repr, const char *key, iotcon_list_h *list); +int iotcon_repr_get_repr(iotcon_repr_h src, const char *key, iotcon_repr_h *dest); bool iotcon_repr_is_null(iotcon_repr_h repr, const char *key); int iotcon_repr_del_int(iotcon_repr_h repr, const char *key); @@ -94,19 +99,21 @@ int iotcon_repr_del_list(iotcon_repr_h repr, const char *key); int iotcon_repr_del_repr(iotcon_repr_h repr, const char *key); int iotcon_repr_del_null(iotcon_repr_h repr, const char *key); +int iotcon_repr_get_type(iotcon_repr_h repr, const char *key, int *type); + int iotcon_repr_append_child(iotcon_repr_h parent, iotcon_repr_h child); -typedef void (*iotcon_children_fn)(iotcon_repr_h child, void *user_data); -void iotcon_repr_get_children(iotcon_repr_h parent, iotcon_children_fn fn, +typedef bool (*iotcon_children_fn)(iotcon_repr_h child, void *user_data); +int iotcon_repr_get_children(iotcon_repr_h parent, iotcon_children_fn fn, void *user_data); -int iotcon_repr_get_children_count(iotcon_repr_h parent); -iotcon_repr_h iotcon_repr_get_nth_child(iotcon_repr_h parent, int index); +unsigned int iotcon_repr_get_children_count(iotcon_repr_h parent); +int iotcon_repr_get_nth_child(iotcon_repr_h parent, int index, iotcon_repr_h *child); iotcon_str_list_s* iotcon_repr_get_key_list(iotcon_repr_h repr); int iotcon_repr_get_keys_count(iotcon_repr_h repr); char* iotcon_repr_generate_json(iotcon_repr_h repr); -iotcon_list_h iotcon_list_new(iotcon_repr_types_e type); +iotcon_list_h iotcon_list_new(iotcon_types_e type); void iotcon_list_free(iotcon_list_h list); /** @@ -122,19 +129,19 @@ void iotcon_list_free(iotcon_list_h list); * * @return the (possibly changed) start of the list, otherwise a null pointer on failure */ -iotcon_list_h iotcon_list_insert_int(iotcon_list_h list, int val, int pos); -iotcon_list_h iotcon_list_insert_bool(iotcon_list_h list, bool val, int pos); -iotcon_list_h iotcon_list_insert_double(iotcon_list_h list, double val, int pos); -iotcon_list_h iotcon_list_insert_str(iotcon_list_h list, char *val, int pos); -iotcon_list_h iotcon_list_insert_list(iotcon_list_h list, iotcon_list_h val, int pos); -iotcon_list_h iotcon_list_insert_repr(iotcon_list_h list, iotcon_repr_h val, int pos); - -int iotcon_list_get_nth_int(iotcon_list_h list, int index); -bool iotcon_list_get_nth_bool(iotcon_list_h list, int index); -double iotcon_list_get_nth_double(iotcon_list_h list, int index); -const char* iotcon_list_get_nth_str(iotcon_list_h list, int index); -iotcon_list_h iotcon_list_get_nth_list(iotcon_list_h list, int index); -iotcon_repr_h iotcon_list_get_nth_repr(iotcon_list_h list, int index); +int iotcon_list_insert_int(iotcon_list_h list, int val, int pos); +int iotcon_list_insert_bool(iotcon_list_h list, bool val, int pos); +int iotcon_list_insert_double(iotcon_list_h list, double val, int pos); +int iotcon_list_insert_str(iotcon_list_h list, char *val, int pos); +int iotcon_list_insert_list(iotcon_list_h list, iotcon_list_h val, int pos); +int iotcon_list_insert_repr(iotcon_list_h list, iotcon_repr_h val, int pos); + +int iotcon_list_get_nth_int(iotcon_list_h list, int index, int *val); +int iotcon_list_get_nth_bool(iotcon_list_h list, int index, bool *val); +int iotcon_list_get_nth_double(iotcon_list_h list, int index, double *val); +int iotcon_list_get_nth_str(iotcon_list_h list, int index, const char **val); +int iotcon_list_get_nth_list(iotcon_list_h src, int index, iotcon_list_h *dest); +int iotcon_list_get_nth_repr(iotcon_list_h list, int index, iotcon_repr_h *repr); int iotcon_list_del_nth_int(iotcon_list_h list, int pos); int iotcon_list_del_nth_bool(iotcon_list_h list, int pos); @@ -143,20 +150,27 @@ int iotcon_list_del_nth_str(iotcon_list_h list, int pos); int iotcon_list_del_nth_list(iotcon_list_h list, int pos); int iotcon_list_del_nth_repr(iotcon_list_h list, int pos); -int iotcon_list_get_type(iotcon_list_h list); -int iotcon_list_get_length(iotcon_list_h list); - -typedef void (*iotcon_list_int_fn)(int index, const int value, void *user_data); -void iotcon_list_foreach_int(iotcon_list_h list, iotcon_list_int_fn fn, void *user_data); -typedef void (*iotcon_list_bool_fn)(int index, const bool value, void *user_data); -void iotcon_list_foreach_bool(iotcon_list_h list, iotcon_list_bool_fn fn, void *user_data); -typedef void (*iotcon_list_double_fn)(int index, const double value, void *user_data); -void iotcon_list_foreach_double(iotcon_list_h list, iotcon_list_double_fn fn, void *user_data); -typedef void (*iotcon_list_str_fn)(int index, const char *value, void *user_data); -void iotcon_list_foreach_str(iotcon_list_h list, iotcon_list_str_fn fn, void *user_data); -typedef void (*iotcon_list_list_fn)(int index, iotcon_list_h value, void *user_data); -void iotcon_list_list_foreach(iotcon_list_h list, iotcon_list_list_fn fn, void *user_data); -typedef void (*iotcon_list_repr_fn)(int index, iotcon_repr_h value, void *user_data); -void iotcon_list_foreach_repr(iotcon_list_h list, iotcon_list_repr_fn fn, void *user_data); +int iotcon_list_get_type(iotcon_list_h list, int *type); +unsigned int iotcon_list_get_length(iotcon_list_h list); + +typedef int (*iotcon_list_int_fn)(int index, const int value, + void *user_data); +int iotcon_list_foreach_int(iotcon_list_h list, iotcon_list_int_fn fn, void *user_data); +typedef int (*iotcon_list_bool_fn)(int index, const bool value, + void *user_data); +int iotcon_list_foreach_bool(iotcon_list_h list, iotcon_list_bool_fn fn, void *user_data); +typedef int (*iotcon_list_double_fn)(int index, const double value, + void *user_data); +int iotcon_list_foreach_double(iotcon_list_h list, iotcon_list_double_fn fn, + void *user_data); +typedef int (*iotcon_list_str_fn)(int index, const char *value, + void *user_data); +int iotcon_list_foreach_str(iotcon_list_h list, iotcon_list_str_fn fn, void *user_data); +typedef int (*iotcon_list_list_fn)(int index, iotcon_list_h value, + void *user_data); +int iotcon_list_foreach_list(iotcon_list_h list, iotcon_list_list_fn fn, void *user_data); +typedef int (*iotcon_list_repr_fn)(int index, iotcon_repr_h value, + void *user_data); +int iotcon_list_foreach_repr(iotcon_list_h list, iotcon_list_repr_fn fn, void *user_data); #endif /* __IOT_CONNECTIVITY_MANAGER_REPRESENTATION_H__ */ diff --git a/lib/include/iotcon-struct.h b/lib/include/iotcon-struct.h index ad771a3..a2ed4d0 100755 --- a/lib/include/iotcon-struct.h +++ b/lib/include/iotcon-struct.h @@ -17,7 +17,9 @@ #define __IOT_CONNECTIVITY_MANAGER_STRUCT_H__ #include -#include +#include + +#include "iotcon-constant.h" typedef struct ic_value_s* iotcon_value_h; typedef struct ic_list_s* iotcon_list_h; @@ -41,10 +43,10 @@ int iotcon_options_insert(iotcon_options_h options, unsigned short id, const char *data); int iotcon_options_delete(iotcon_options_h options, unsigned short id); const char* iotcon_options_lookup(iotcon_options_h options, unsigned short id); -typedef void (*iotcon_options_foreach_cb)(unsigned short id, const char *data, +typedef int (*iotcon_options_foreach_cb)(unsigned short id, const char *data, + void *user_data); +int iotcon_options_foreach(iotcon_options_h options, iotcon_options_foreach_cb cb, void *user_data); -void iotcon_options_foreach(iotcon_options_h options, - iotcon_options_foreach_cb cb, void *user_data); typedef struct ic_query* iotcon_query_h; @@ -53,9 +55,9 @@ void iotcon_query_free(iotcon_query_h query); int iotcon_query_insert(iotcon_query_h query, const char *key, const char *value); int iotcon_query_delete(iotcon_query_h query, const char *key); const char* iotcon_query_lookup(iotcon_query_h query, const char *key); -typedef void (*iotcon_query_foreach_cb)(const char *key, const char *value, +typedef int (*iotcon_query_foreach_cb)(const char *key, const char *value, void *user_data); -void iotcon_query_foreach(iotcon_query_h query, iotcon_query_foreach_cb cb, +int iotcon_query_foreach(iotcon_query_h query, iotcon_query_foreach_cb cb, void *user_data); iotcon_query_h iotcon_query_clone(iotcon_query_h query); @@ -119,9 +121,9 @@ iotcon_str_list_s* iotcon_str_list_remove(iotcon_str_list_s *str_list, const char *string); iotcon_str_list_s* iotcon_str_list_clone(iotcon_str_list_s *str_list); unsigned int iotcon_str_list_length(iotcon_str_list_s *str_list); -typedef void (*iotcon_string_foreach_cb)(const char *string, void *user_data); -void iotcon_str_list_foreach(iotcon_str_list_s *str_list, - iotcon_string_foreach_cb cb, void *user_data); +typedef int (*iotcon_string_foreach_cb)(const char *string, void *user_data); +int iotcon_str_list_foreach(iotcon_str_list_s *str_list, iotcon_string_foreach_cb cb, + void *user_data); const char* iotcon_str_list_nth_data(iotcon_str_list_s *str_list, unsigned int n); #endif /* __IOT_CONNECTIVITY_MANAGER_STRUCT_H__ */ diff --git a/test/crud-test-client.c b/test/crud-test-client.c index c6e0c3b..00f6013 100755 --- a/test/crud-test-client.c +++ b/test/crud-test-client.c @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - +#include #include #include @@ -64,7 +64,7 @@ static void _on_post(iotcon_options_h header_options, iotcon_repr_h recv_repr, _print_repr_info(recv_repr); - created_uri = iotcon_repr_get_str(recv_repr, "createduri"); + iotcon_repr_get_str(recv_repr, "createduri", &created_uri); if (created_uri) { DBG("New resource created : %s", created_uri); @@ -120,11 +120,13 @@ static void _on_get(iotcon_options_h header_options, iotcon_repr_h recv_repr, iotcon_query_free(query_params); } -static void _get_res_type_fn(const char *string, void *user_data) +static int _get_res_type_fn(const char *string, void *user_data) { char *resource_uri = user_data; DBG("[%s] resource type : %s", resource_uri, string); + + return IOTCON_FUNC_CONTINUE; } static void _presence_handler(int result, unsigned int nonce, @@ -138,6 +140,7 @@ static void _presence_handler(int result, unsigned int nonce, static void _found_resource(iotcon_client_h resource, void *user_data) { + int ret; const char *resource_uri = NULL; const char *resource_host = NULL; iotcon_str_list_s *resource_types = NULL; @@ -170,8 +173,12 @@ static void _found_resource(iotcon_client_h resource, void *user_data) /* get the resource types */ resource_types = iotcon_client_get_types(resource); - iotcon_str_list_foreach(resource_types, _get_res_type_fn, + ret = iotcon_str_list_foreach(resource_types, _get_res_type_fn, (void *)resource_uri); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_str_list_foreach() Fail(%d)", ret); + return; + } iotcon_subscribe_presence(resource_host, "core.door", _presence_handler, NULL); diff --git a/test/crud-test-server.c b/test/crud-test-server.c index cb99c8b..d17e00a 100644 --- a/test/crud-test-server.c +++ b/test/crud-test-server.c @@ -106,12 +106,14 @@ static void _request_handler_get(iotcon_response_h response) static void _request_handler_put(iotcon_request_h request, iotcon_response_h response) { + bool bval; iotcon_repr_h req_repr = NULL; iotcon_repr_h resp_repr = NULL; INFO("PUT request"); req_repr = iotcon_request_get_representation(request); - my_door.state = iotcon_repr_get_bool(req_repr, "opened"); + iotcon_repr_get_bool(req_repr, "opened", &bval); + my_door.state = bval; _check_door_state(); @@ -175,10 +177,12 @@ static void _request_handler_delete(iotcon_response_h response) g_timeout_add_seconds(5, _notifier, door_handle); } -static void query_cb(const char *key, const char *value, void *user_data) +static int _query_cb(const char *key, const char *value, void *user_data) { INFO("key : %s", key); INFO("value : %s", value); + + return IOTCON_FUNC_CONTINUE; } static void _request_handler(iotcon_request_h request, void *user_data) @@ -193,7 +197,7 @@ static void _request_handler(iotcon_request_h request, void *user_data) query = iotcon_request_get_query(request); if (query) - iotcon_query_foreach(query, query_cb, NULL); + iotcon_query_foreach(query, _query_cb, NULL); request_type = iotcon_request_get_request_type(request); if (NULL == request_type) { diff --git a/test/repr-test-client.c b/test/repr-test-client.c index 0f920f2..bef1dfe 100644 --- a/test/repr-test-client.c +++ b/test/repr-test-client.c @@ -25,28 +25,38 @@ const char* const room_uri = "/a/room"; iotcon_client_h room_resource = NULL; -void _get_int_list_fn(int index, const int value, void *user_data) +static int _get_int_list_fn(int index, const int value, void *user_data) { DBG("%d°C", value); + + return IOTCON_FUNC_CONTINUE; } static void _on_get(iotcon_repr_h recv_repr, int response_result) { - int i, children_count; + int i, ret; + unsigned int children_count; + const char *uri; iotcon_repr_h child_repr; iotcon_list_h list; iotcon_str_list_s *key_list = NULL; - RETM_IF(IOTCON_RESPONSE_RESULT_OK != response_result, "_on_get_1st Response error(%d)", response_result); + RETM_IF(IOTCON_RESPONSE_RESULT_OK != response_result, + "_on_get Response error(%d)", response_result); INFO("GET request was successful"); DBG("[ parent representation ]"); - DBG("uri : %s", iotcon_repr_get_uri(recv_repr)); + iotcon_repr_get_uri(recv_repr, &uri); + if (uri) + DBG("uri : %s", uri); key_list = iotcon_repr_get_key_list(recv_repr); if (key_list) { - DBG("name : %s", iotcon_repr_get_str(recv_repr, "name")); + char *str; + iotcon_repr_get_str(recv_repr, "name", &str); + if (str) + DBG("name : %s", str); - list = iotcon_repr_get_list(recv_repr, "today_temp"); + iotcon_repr_get_list(recv_repr, "today_temp", &list); DBG("today's temperature :"); iotcon_list_foreach_int(list, _get_int_list_fn, NULL); @@ -59,21 +69,31 @@ static void _on_get(iotcon_repr_h recv_repr, int response_result) DBG("[ child representation ]"); const char *uri; - child_repr = iotcon_repr_get_nth_child(recv_repr, i); - uri = iotcon_repr_get_uri(child_repr); - DBG("uri : %s", uri); + ret = iotcon_repr_get_nth_child(recv_repr, i, &child_repr); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_repr_get_nth_child(%d) Fail(%d)", i, ret); + continue; + } + + iotcon_repr_get_uri(child_repr, &uri); + if (uri) + DBG("uri : %s", uri); if (!strcmp("/a/light", uri)) { key_list = iotcon_repr_get_key_list(child_repr); if (key_list) { - DBG("brightness : %d", iotcon_repr_get_int(child_repr, "brightness")); + int brightness; + iotcon_repr_get_int(child_repr, "brightness", &brightness); + DBG("brightness : %d", brightness); iotcon_str_list_free(key_list); } } else if (!strcmp("/a/switch", uri)) { key_list = iotcon_repr_get_key_list(child_repr); if (key_list) { - DBG("switch : %d", iotcon_repr_get_bool(child_repr, "switch")); + bool bswitch; + iotcon_repr_get_bool(child_repr, "switch", &bswitch); + DBG("switch : %d", bswitch); iotcon_str_list_free(key_list); } } @@ -102,11 +122,13 @@ static void _on_get_1st(iotcon_options_h header_options, iotcon_repr_h recv_repr iotcon_query_free(query_params); } -static void _get_res_type_fn(const char *string, void *user_data) +static int _get_res_type_fn(const char *string, void *user_data) { char *resource_uri = user_data; DBG("[%s] resource type : %s", resource_uri, string); + + return IOTCON_FUNC_CONTINUE; } static void _found_resource(iotcon_client_h resource, void *user_data) diff --git a/test/repr-test-server.c b/test/repr-test-server.c index 4638f83..b03ae34 100644 --- a/test/repr-test-server.c +++ b/test/repr-test-server.c @@ -46,13 +46,14 @@ static void _light_request_handler_get(iotcon_response_h response) iotcon_repr_free(resp_repr); } -void _query_foreach_cb(const char *key, const char *value, void *user_data) +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; } + return IOTCON_FUNC_CONTINUE; } static void _room_request_handler_get(iotcon_request_h request, -- 2.7.4