From 4ea46551279ac45d89dd471187664b9dad5cee54 Mon Sep 17 00:00:00 2001 From: Minchul Lee Date: Wed, 20 May 2015 16:16:44 +0900 Subject: [PATCH] (1) add representation delete and clone functions (2) modify list foreach functions (3) modify comment style (4) add doxygen comment for the terms "set" and "append" Change-Id: I94983217c69a86bf9cd614c00a9ec2234ee7bf56 Signed-off-by: Minchul Lee --- lib/ic-common.h | 2 +- lib/ic-handler.h | 2 +- lib/ic-ioty-repr.cpp | 17 +- lib/ic-ioty-repr.h | 2 +- lib/ic-ioty.cpp | 112 +++--- lib/ic-ioty.h | 2 +- lib/ic-log.h | 2 +- lib/ic-options.c | 10 +- lib/ic-options.h | 2 +- lib/ic-repr-list.c | 734 ++++++++++++++++++++++++++++-------- lib/ic-repr-list.h | 10 +- lib/ic-repr-obj.c | 620 ++++++++++++++++++++++++++++++ lib/ic-repr-obj.h | 30 ++ lib/ic-repr-value.c | 359 +++++++++++------- lib/ic-repr-value.h | 33 +- lib/ic-repr.c | 710 +++++++++++----------------------- lib/ic-repr.h | 8 +- lib/ic-struct.h | 2 +- lib/ic-utils.h | 2 +- lib/ic.c | 96 ++--- lib/include/iotcon-constant.h | 2 +- lib/include/iotcon-errors.h | 34 +- lib/include/iotcon-representation.h | 145 +++++++ lib/include/iotcon-struct.h | 6 +- lib/include/iotcon.h | 102 +---- test/crud-test-server.c | 15 +- test/test-log.h | 2 +- 27 files changed, 2004 insertions(+), 1057 deletions(-) create mode 100644 lib/ic-repr-obj.c create mode 100644 lib/ic-repr-obj.h create mode 100644 lib/include/iotcon-representation.h diff --git a/lib/ic-common.h b/lib/ic-common.h index 87711f4..81a77f8 100644 --- a/lib/ic-common.h +++ b/lib/ic-common.h @@ -23,4 +23,4 @@ #endif #define API __attribute__((visibility("default"))) -#endif //__IOT_CONNECTIVITY_MANAGER_INTERNAL_COMMON_H__ +#endif /* __IOT_CONNECTIVITY_MANAGER_INTERNAL_COMMON_H__ */ diff --git a/lib/ic-handler.h b/lib/ic-handler.h index 729988e..f4d530d 100644 --- a/lib/ic-handler.h +++ b/lib/ic-handler.h @@ -22,4 +22,4 @@ resource_handler_s* ic_get_resource_handler_data(void *handle); void ic_get_device_info_handler(iotcon_device_info_s *info); -#endif //__IOT_CONNECTIVITY_MANAGER_INTERNAL_HANDLER_H__ +#endif /* __IOT_CONNECTIVITY_MANAGER_INTERNAL_HANDLER_H__ */ diff --git a/lib/ic-ioty-repr.cpp b/lib/ic-ioty-repr.cpp index d27af9e..553264a 100644 --- a/lib/ic-ioty-repr.cpp +++ b/lib/ic-ioty-repr.cpp @@ -31,7 +31,7 @@ using namespace std; static iotcon_repr_h _ic_ioty_repr_create_repr(const OCRepresentation& ocRep) { FN_CALL; - // incoming json format example : {"href":"/a/address","rep":{"text":"Hello world"}} + string jsonStr = ocRep.getJSONRepresentation(); iotcon_repr_h repr = ic_repr_parse_json(jsonStr.c_str()); @@ -54,6 +54,13 @@ static iotcon_repr_h _ic_ioty_repr_create_child(const OCRepresentation& ocRep) return _ic_ioty_repr_create_repr(ocRep); } + +/* + * A general input : {oc:[{"href":"/a/parent","rep":{"string":"Hello","intlist":[1,2,3]}, + * "prop":{"rt":["core.light"],"if":["oc.mi.def"]}}, + * {"href":"/a/child","rep":{"string":"World","double_val":5.7}, + * "prop":{"rt":["core.light"],"if":["oc.mi.def"]}}]} + */ iotcon_repr_h ic_ioty_repr_generate_repr(const OCRepresentation& ocRep) { FN_CALL; @@ -61,6 +68,11 @@ iotcon_repr_h ic_ioty_repr_generate_repr(const OCRepresentation& ocRep) OCRepresentation ocChild; iotcon_repr_h repr_parent = _ic_ioty_repr_create_parent(ocRep); + if (NULL == repr_parent) { + ERR("_ic_ioty_repr_create_parent() Fail"); + iotcon_repr_free(repr_parent); + return NULL; + } vector childList = ocRep.getChildren(); @@ -85,8 +97,7 @@ OCRepresentation ic_ioty_repr_parse(iotcon_repr_h repr) OCRepresentation ocRep; MessageContainer info; - // TODO: It's better that iotcon_repr_h is changed to - // OCRepresentation at once. + /* TODO: It's better that iotcon_repr_h is changed to OCRepresentation at once. */ char *repr_json = ic_repr_generate_json(repr, false); try { diff --git a/lib/ic-ioty-repr.h b/lib/ic-ioty-repr.h index 6e8a1eb..4cd531b 100644 --- a/lib/ic-ioty-repr.h +++ b/lib/ic-ioty-repr.h @@ -23,5 +23,5 @@ void ic_ioty_repr_found_device_cb(const OC::OCRepresentation& ocRep); iotcon_repr_h ic_ioty_repr_generate_repr(const OC::OCRepresentation& ocRep); OC::OCRepresentation ic_ioty_repr_parse(iotcon_repr_h repr); -#endif //__IOT_CONNECTIVITY_MANAGER_INTERNAL_IOTIVITY_REPRESENTATION_H__ +#endif /* __IOT_CONNECTIVITY_MANAGER_INTERNAL_IOTIVITY_REPRESENTATION_H__ */ diff --git a/lib/ic-ioty.cpp b/lib/ic-ioty.cpp index 977865b..048fb0d 100644 --- a/lib/ic-ioty.cpp +++ b/lib/ic-ioty.cpp @@ -57,10 +57,10 @@ namespace icIotivityHandler { if (OC_STACK_OK != result) { ERR("subscribePresence() result Fail(%d)", result); - ret = IOTCON_ERR_IOTIVITY; + ret = IOTCON_ERROR_IOTIVITY; } else { - ret = IOTCON_ERR_NONE; + ret = IOTCON_ERROR_NONE; } if (presence_handler) @@ -135,9 +135,9 @@ namespace icIotivityHandler { iotcon_repr_h repr = NULL; if (OC_STACK_OK == eCode) - res = IOTCON_ERR_NONE; + res = IOTCON_ERROR_NONE; else - res = IOTCON_ERR_IOTIVITY; + res = IOTCON_ERROR_IOTIVITY; options = iotcon_options_new(); for (HeaderOption::OCHeaderOption option : headerOptions) { @@ -177,9 +177,9 @@ namespace icIotivityHandler { iotcon_repr_h repr = NULL; if (OC_STACK_OK == eCode) - res = IOTCON_ERR_NONE; + res = IOTCON_ERROR_NONE; else - res = IOTCON_ERR_IOTIVITY; + res = IOTCON_ERROR_IOTIVITY; options = iotcon_options_new(); for (HeaderOption::OCHeaderOption option : headerOptions) { @@ -219,9 +219,9 @@ namespace icIotivityHandler { iotcon_repr_h repr = NULL; if (OC_STACK_OK == eCode || OC_STACK_RESOURCE_CREATED == eCode) - res = IOTCON_ERR_NONE; + res = IOTCON_ERROR_NONE; else - res = IOTCON_ERR_IOTIVITY; + res = IOTCON_ERROR_IOTIVITY; options = iotcon_options_new(); for (HeaderOption::OCHeaderOption option : headerOptions) { @@ -258,9 +258,9 @@ namespace icIotivityHandler { iotcon_options_h options; if (OC_STACK_OK == eCode || OC_STACK_RESOURCE_DELETED == eCode) - res = IOTCON_ERR_NONE; + res = IOTCON_ERROR_NONE; else - res = IOTCON_ERR_IOTIVITY; + res = IOTCON_ERROR_IOTIVITY; options = iotcon_options_new(); for (HeaderOption::OCHeaderOption option : headerOptions) { @@ -297,9 +297,9 @@ namespace icIotivityHandler { iotcon_repr_h repr = NULL; if (OC_STACK_OK == eCode) - res = IOTCON_ERR_NONE; + res = IOTCON_ERROR_NONE; else - res = IOTCON_ERR_IOTIVITY; + res = IOTCON_ERROR_IOTIVITY; options = iotcon_options_new(); for (HeaderOption::OCHeaderOption option : headerOptions) { @@ -473,15 +473,15 @@ extern "C" int ic_ioty_unregister_res(const iotcon_resource_h resource_handle) OCStackResult result = unregisterResource(resourceHandle); if(OC_STACK_OK != result) { ERR("OCPlatform::unregisterResource Fail(%d)", result); - return IOTCON_ERR_IOTIVITY; + return IOTCON_ERROR_IOTIVITY; } } catch(OCException& e) { ERR("unregisterResource() Fail(%s)", e.what()); - return IOTCON_ERR_IOTIVITY; + return IOTCON_ERROR_IOTIVITY; } - return IOTCON_ERR_NONE; + return IOTCON_ERROR_NONE; } @@ -494,10 +494,10 @@ extern "C" int ic_ioty_bind_iface_to_res(const iotcon_resource_h resource_handle ret = bindInterfaceToResource(resourceHandle, interface_type); if (OC_STACK_OK != ret) { ERR("bindInterfaceToResource() Fail(%d)", ret); - return IOTCON_ERR_IOTIVITY; + return IOTCON_ERROR_IOTIVITY; } - return IOTCON_ERR_NONE; + return IOTCON_ERROR_NONE; } @@ -510,10 +510,10 @@ extern "C" int ic_ioty_bind_type_to_res(const iotcon_resource_h resource_handle, ret = bindTypeToResource(resourceHandle, resource_type); if (OC_STACK_OK != ret) { ERR("bindTypeToResource() Fail(%d)", ret); - return IOTCON_ERR_IOTIVITY; + return IOTCON_ERROR_IOTIVITY; } - return IOTCON_ERR_NONE; + return IOTCON_ERROR_NONE; } extern "C" int ic_ioty_bind_res(iotcon_resource_h parent, iotcon_resource_h child) @@ -525,10 +525,10 @@ extern "C" int ic_ioty_bind_res(iotcon_resource_h parent, iotcon_resource_h chil ret = OCBindResource(p_handle, c_handle); if (OC_STACK_OK != ret) { ERR("OCBindResource() Fail(%d)", ret); - return IOTCON_ERR_IOTIVITY; + return IOTCON_ERROR_IOTIVITY; } - return IOTCON_ERR_NONE; + return IOTCON_ERROR_NONE; } extern "C" int ic_ioty_register_device_info(iotcon_device_info_s *device_info) @@ -553,10 +553,10 @@ extern "C" int ic_ioty_register_device_info(iotcon_device_info_s *device_info) ret = registerDeviceInfo(deviceInfo); if (OC_STACK_OK != ret) { ERR("registerDeviceInfo() Fail(%d)", ret); - return IOTCON_ERR_IOTIVITY; + return IOTCON_ERROR_IOTIVITY; } - return IOTCON_ERR_NONE; + return IOTCON_ERROR_NONE; } @@ -574,10 +574,10 @@ extern "C" int ic_ioty_get_device_info(char *host, char *uri) ret = getDeviceInfo(resHost, resUri, ic_ioty_repr_found_device_cb); if (OC_STACK_OK != ret) { ERR("getDeviceInfo() Fail(%d)", ret); - return IOTCON_ERR_IOTIVITY; + return IOTCON_ERROR_IOTIVITY; } - return IOTCON_ERR_NONE; + return IOTCON_ERROR_NONE; } @@ -589,13 +589,13 @@ extern "C" int ic_ioty_send_notify(struct ic_res_response_s *resp, OCResourceHandle resHandle; string interface; - RETV_IF(NULL == resp, IOTCON_ERR_PARAM); - RETV_IF(NULL == resp->repr, IOTCON_ERR_PARAM); - RETV_IF(NULL == observers, IOTCON_ERR_PARAM); + RETV_IF(NULL == resp, IOTCON_ERROR_PARAM); + RETV_IF(NULL == resp->repr, IOTCON_ERROR_PARAM); + RETV_IF(NULL == observers, IOTCON_ERROR_PARAM); resHandle = resp->resource_handle; - RETV_IF(NULL == resp->repr, IOTCON_ERR_PARAM); + RETV_IF(NULL == resp->repr, IOTCON_ERROR_PARAM); OCRepresentation ocRep = ic_ioty_repr_parse(resp->repr); @@ -630,10 +630,10 @@ extern "C" int ic_ioty_send_notify(struct ic_res_response_s *resp, ret = notifyListOfObservers(resHandle, obsIds, resourceResponse); if (OC_STACK_NO_OBSERVERS == ret) { ERR("No More observers, stopping notifications"); - return IOTCON_ERR_IOTIVITY; + return IOTCON_ERROR_IOTIVITY; } - return IOTCON_ERR_NONE; + return IOTCON_ERROR_NONE; } @@ -643,9 +643,9 @@ extern "C" int ic_ioty_send_res_response_data(struct ic_res_response_s *resp) string interface; int ret = OC_STACK_ERROR; - RETV_IF(NULL == resp, IOTCON_ERR_PARAM); + RETV_IF(NULL == resp, IOTCON_ERROR_PARAM); - RETV_IF(NULL == resp->repr, IOTCON_ERR_PARAM); + RETV_IF(NULL == resp->repr, IOTCON_ERROR_PARAM); OCRepresentation ocRep = ic_ioty_repr_parse(resp->repr); @@ -676,13 +676,13 @@ extern "C" int ic_ioty_send_res_response_data(struct ic_res_response_s *resp) ret = sendResponse(pResponse); if (OC_STACK_OK != ret) { ERR("sendResponse() Fail(%d)", ret); - return IOTCON_ERR_IOTIVITY; + return IOTCON_ERROR_IOTIVITY; } } FN_END; - return IOTCON_ERR_NONE; + return IOTCON_ERROR_NONE; } @@ -714,10 +714,10 @@ extern "C" int ic_ioty_unsubscribe_presence(iotcon_presence_h presence_handle) ret = unsubscribePresence(presence_handle); if (OC_STACK_OK != ret) { ERR("unsubscribePresence() Fail(%d)", ret); - return IOTCON_ERR_IOTIVITY; + return IOTCON_ERROR_IOTIVITY; } - return IOTCON_ERR_NONE; + return IOTCON_ERROR_NONE; } extern "C" int ic_ioty_start_presence(const unsigned int time_to_live) @@ -726,10 +726,10 @@ extern "C" int ic_ioty_start_presence(const unsigned int time_to_live) startPresence(time_to_live); } catch (OCException e) { ERR("startPresence() Fail(%s)", e.what()); - return IOTCON_ERR_IOTIVITY; + return IOTCON_ERROR_IOTIVITY; } - return IOTCON_ERR_NONE; + return IOTCON_ERROR_NONE; } extern "C" int ic_ioty_stop_presence() @@ -738,10 +738,10 @@ extern "C" int ic_ioty_stop_presence() stopPresence(); } catch (OCException e) { ERR("stopPresence() Fail(%s)", e.what()); - return IOTCON_ERR_IOTIVITY; + return IOTCON_ERROR_IOTIVITY; } - return IOTCON_ERR_NONE; + return IOTCON_ERROR_NONE; } extern "C" int ic_ioty_find_resource(const char *host, const char *resource_name, @@ -757,10 +757,10 @@ extern "C" int ic_ioty_find_resource(const char *host, const char *resource_name ret = findResource(host, resource_name, findCallback); if (OC_STACK_OK != ret) { ERR("findResource() Fail(%d)", ret); - return IOTCON_ERR_IOTIVITY; + return IOTCON_ERROR_IOTIVITY; } - return IOTCON_ERR_NONE; + return IOTCON_ERROR_NONE; } static void _change_options(unsigned short id, char *data, void *user_data) @@ -836,10 +836,10 @@ extern "C" int ic_ioty_get(iotcon_resource_s resource, ret = ocResource->get(queryParams, getCallback); if (OC_STACK_OK != ret) { ERR("get() Fail(%d)", ret); - return IOTCON_ERR_IOTIVITY; + return IOTCON_ERROR_IOTIVITY; } - return IOTCON_ERR_NONE; + return IOTCON_ERROR_NONE; } extern "C" int ic_ioty_put(iotcon_resource_s resource, @@ -879,10 +879,10 @@ extern "C" int ic_ioty_put(iotcon_resource_s resource, ret = ocResource->put(ocRep, queryParams, putCallback); if (OC_STACK_OK != ret) { ERR("put() Fail(%d)", ret); - return IOTCON_ERR_IOTIVITY; + return IOTCON_ERROR_IOTIVITY; } - return IOTCON_ERR_NONE; + return IOTCON_ERROR_NONE; } extern "C" int ic_ioty_post(iotcon_resource_s resource, @@ -920,10 +920,10 @@ extern "C" int ic_ioty_post(iotcon_resource_s resource, ret = ocResource->post(ocRep, queryParams, postCallback); if (OC_STACK_OK != ret) { ERR("post() Fail(%d)", ret); - return IOTCON_ERR_IOTIVITY; + return IOTCON_ERROR_IOTIVITY; } - return IOTCON_ERR_NONE; + return IOTCON_ERROR_NONE; } extern "C" int ic_ioty_delete_res(iotcon_resource_s resource, @@ -943,10 +943,10 @@ extern "C" int ic_ioty_delete_res(iotcon_resource_s resource, ret = ocResource->deleteResource(deleteCallback); if (OC_STACK_OK != ret) { ERR("deleteResource() Fail(%d)", ret); - return IOTCON_ERR_IOTIVITY; + return IOTCON_ERROR_IOTIVITY; } - return IOTCON_ERR_NONE; + return IOTCON_ERROR_NONE; } extern "C" int ic_ioty_observe(iotcon_resource_s *resource, @@ -981,7 +981,7 @@ extern "C" int ic_ioty_observe(iotcon_resource_s *resource, } else { ERR("Invalid observe_type"); - return IOTCON_ERR_PARAM; + return IOTCON_ERROR_PARAM; } ocResource = _create_oc_resource(*resource); @@ -998,10 +998,10 @@ extern "C" int ic_ioty_observe(iotcon_resource_s *resource, ret = ocResource->observe(observeType, queryParams, observeCallback); if (OC_STACK_OK != ret) { ERR("observe() Fail(%d)", ret); - return IOTCON_ERR_IOTIVITY; + return IOTCON_ERROR_IOTIVITY; } - return IOTCON_ERR_NONE; + return IOTCON_ERROR_NONE; } extern "C" int ic_ioty_cancel_observe(iotcon_resource_s resource) @@ -1015,8 +1015,8 @@ extern "C" int ic_ioty_cancel_observe(iotcon_resource_s resource) ret = ocResource->cancelObserve(); if (OC_STACK_OK != ret) { ERR("cancelObserve() Fail(%d)", ret); - return IOTCON_ERR_IOTIVITY; + return IOTCON_ERROR_IOTIVITY; } - return IOTCON_ERR_NONE; + return IOTCON_ERROR_NONE; } diff --git a/lib/ic-ioty.h b/lib/ic-ioty.h index de3d583..d61ec8c 100644 --- a/lib/ic-ioty.h +++ b/lib/ic-ioty.h @@ -86,4 +86,4 @@ typedef struct _iot_ctx { ic_ctx_s* ic_get_ctx(); -#endif //__IOT_CONNECTIVITY_MANAGER_INTERNAL_IOTIVITY_H__ +#endif /* __IOT_CONNECTIVITY_MANAGER_INTERNAL_IOTIVITY_H__ */ diff --git a/lib/ic-log.h b/lib/ic-log.h index cac29be..4495fd1 100644 --- a/lib/ic-log.h +++ b/lib/ic-log.h @@ -135,4 +135,4 @@ } \ } while (0) -#endif //__IOT_CONNECTIVITY_MANAGER_INTERNAL_LOG_H__ \ No newline at end of file +#endif /* __IOT_CONNECTIVITY_MANAGER_INTERNAL_LOG_H__ */ diff --git a/lib/ic-options.c b/lib/ic-options.c index 68a81e4..999d30e 100644 --- a/lib/ic-options.c +++ b/lib/ic-options.c @@ -59,28 +59,28 @@ API int iotcon_options_insert(iotcon_options_h options, const unsigned short id, FN_CALL; gpointer value; - RETVM_IF(((id < IOTCON_OPTIONID_MIN) || (IOTCON_OPTIONID_MAX < id)), IOTCON_ERR_PARAM, + RETVM_IF(((id < IOTCON_OPTIONID_MIN) || (IOTCON_OPTIONID_MAX < id)), IOTCON_ERROR_PARAM, "Invalid id(%d)", id); value = ic_utils_strdup(data); g_hash_table_insert(options->options, GUINT_TO_POINTER(id), value); - return IOTCON_ERR_NONE; + return IOTCON_ERROR_NONE; } API int iotcon_options_delete(iotcon_options_h options, const unsigned short id) { gboolean ret; - RETV_IF(NULL == options, IOTCON_ERR_PARAM); + RETV_IF(NULL == options, IOTCON_ERROR_PARAM); ret = g_hash_table_remove(options->options, GUINT_TO_POINTER(id)); if (FALSE == ret) { ERR("g_hash_table_remove() Fail"); - return IOTCON_ERR_PARAM; + return IOTCON_ERROR_PARAM; } - return IOTCON_ERR_NONE; + return IOTCON_ERROR_NONE; } API const char* iotcon_options_lookup(iotcon_options_h options, const unsigned short id) diff --git a/lib/ic-options.h b/lib/ic-options.h index ba910cc..d920630 100644 --- a/lib/ic-options.h +++ b/lib/ic-options.h @@ -20,4 +20,4 @@ void ic_options_free(iotcon_options_h options); -#endif //__IOT_CONNECTIVITY_MANAGER_INTERNAL_OPTIONS_H__ +#endif /* __IOT_CONNECTIVITY_MANAGER_INTERNAL_OPTIONS_H__ */ diff --git a/lib/ic-repr-list.c b/lib/ic-repr-list.c index 5467500..44581d3 100644 --- a/lib/ic-repr-list.c +++ b/lib/ic-repr-list.c @@ -23,6 +23,7 @@ #include "ic-common.h" #include "ic-struct.h" +#include "ic-repr-obj.h" #include "ic-repr.h" #include "ic-repr-value.h" #include "ic-repr-list.h" @@ -30,7 +31,6 @@ API iotcon_list_h iotcon_list_new(iotcon_repr_types_e type) { iotcon_list_h list; - errno = 0; list = calloc(1, sizeof(struct ic_list_s)); if (NULL == list) { @@ -43,240 +43,508 @@ API iotcon_list_h iotcon_list_new(iotcon_repr_types_e type) return list; } -API iotcon_list_h iotcon_list_append_int(iotcon_list_h list, int ival) + +API iotcon_list_h iotcon_list_insert_int(iotcon_list_h list, int val, int pos) { - int ret = 0; + iotcon_value_h value; RETV_IF(NULL == list, NULL); - RETVM_IF(IOTCON_TYPE_INT != list->type, list, "IOTCON_ERR_PARAM(%d)", list->type); + RETVM_IF(IOTCON_TYPE_INT != list->type, list, "Invalid Type(%d)", list->type); - iotcon_value_h value = ic_value_new(IOTCON_TYPE_INT); + value = ic_value_new_int(val); if (NULL == value) { - ERR("ic_value_new(IOTCON_TYPE_INT) Fail"); + ERR("ic_value_new_int(%d) Fail", val); return list; } - ret = ic_value_set_int(value, ival); - if (IOTCON_ERR_NONE != ret) { - ERR("ic_value_set_int(%d) Fail(%d)", ival, ret); - ic_repr_free_basic_value(value); - return list; - } - - list = ic_list_append(list, value); + list = ic_list_insert(list, value, pos); return list; } -API iotcon_list_h iotcon_list_append_bool(iotcon_list_h list, bool bval) + +API iotcon_list_h iotcon_list_insert_bool(iotcon_list_h list, bool val, int pos) { - int ret = 0; + iotcon_value_h value; RETV_IF(NULL == list, NULL); - RETVM_IF(IOTCON_TYPE_BOOL != list->type, list, "IOTCON_ERR_PARAM(%d)", list->type); + RETVM_IF(IOTCON_TYPE_BOOL != list->type, list, "Invalid Type(%d)", list->type); - iotcon_value_h value = ic_value_new(IOTCON_TYPE_BOOL); + value = ic_value_new_bool(val); if (NULL == value) { - ERR("ic_value_new(IOTCON_TYPE_BOOL) Fail"); + ERR("ic_value_new_bool(%d) Fail", val); return list; } - ret = ic_value_set_bool(value, bval); - if (IOTCON_ERR_NONE != ret) { - ERR("ic_value_set_bool(%d) Fail(%d)", bval, ret); - ic_repr_free_basic_value(value); - return list; - } - - list = ic_list_append(list, value); + list = ic_list_insert(list, value, pos); return list; } -API iotcon_list_h iotcon_list_append_double(iotcon_list_h list, double dbval) + +API iotcon_list_h iotcon_list_insert_double(iotcon_list_h list, double val, int pos) { - int ret = 0; + iotcon_value_h value; RETV_IF(NULL == list, NULL); - RETVM_IF(IOTCON_TYPE_DOUBLE != list->type, list, "IOTCON_ERR_PARAM(%d)", - list->type); + RETVM_IF(IOTCON_TYPE_DOUBLE != list->type, list, "Invalid Type(%d)", list->type); - iotcon_value_h value = ic_value_new(IOTCON_TYPE_DOUBLE); + value = ic_value_new_double(val); if (NULL == value) { - ERR("ic_value_new(IOTCON_TYPE_DOUBLE) Fail"); + ERR("ic_value_new_double(%f) Fail", val); return list; } - ret = ic_value_set_double(value, dbval); - if (IOTCON_ERR_NONE != ret) { - ERR("ic_value_set_double(%lf) Fail(%d)", dbval, ret); - ic_repr_free_basic_value(value); + list = ic_list_insert(list, value, pos); + + return list; +} + + +API iotcon_list_h 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); + + value = ic_value_new_str(val); + if (NULL == value) { + ERR("ic_value_new_str(%s) Fail", val); return list; } - list = ic_list_append(list, value); + list = ic_list_insert(list, value, pos); return list; } -API iotcon_list_h iotcon_list_append_str(iotcon_list_h list, const char *strval) + +API iotcon_list_h iotcon_list_insert_list(iotcon_list_h list, iotcon_list_h val, int pos) { - int ret = 0; + iotcon_value_h value; RETV_IF(NULL == list, NULL); - RETVM_IF(IOTCON_TYPE_STR != list->type, list, "IOTCON_ERR_PARAM(%d)", list->type); + RETV_IF(NULL == val, NULL); + RETVM_IF(IOTCON_TYPE_LIST != list->type, list, "Invalid Type(%d)", list->type); - iotcon_value_h value = ic_value_new(IOTCON_TYPE_STR); + value = ic_value_new_list(val); if (NULL == value) { - ERR("ic_value_new(IOTCON_TYPE_STR) Fail"); + ERR("ic_value_new_list(%p) Fail", val); return list; } - ret = ic_value_set_str(value, strval); - if (IOTCON_ERR_NONE != ret) { - ERR("ic_value_set_str(%s) Fail(%d)", strval, ret); - ic_repr_free_basic_value(value); + list = ic_list_insert(list, value, pos); + + return list; +} + + +API iotcon_list_h 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); + + value = ic_value_new_repr(val); + if (NULL == value) { + ERR("ic_value_new_repr(%p) Fail", val); return list; } - list = ic_list_append(list, value); + list = ic_list_insert(list, value, pos); return list; } -API iotcon_list_h iotcon_list_append_list(iotcon_list_h dest, iotcon_list_h src) + +API int iotcon_list_get_nth_int(iotcon_list_h list, int index) { - RETV_IF(NULL == dest, NULL); - RETV_IF(NULL == src, NULL); - RETVM_IF(IOTCON_TYPE_LIST != dest->type, dest, "IOTCON_ERR_PARAM(%d)", dest->type); + iotcon_value_h value; + + RETV_IF(NULL == list, 0); + RETV_IF(NULL == list->list, 0); - iotcon_value_h value = ic_value_new(IOTCON_TYPE_LIST); + value = g_list_nth_data(list->list, index); if (NULL == value) { - ERR("ic_value_new(IOTCON_TYPE_LIST) Fail"); - return dest; + ERR("g_list_nth_data() Fail"); + return 0; } - ic_val_list_s *real = (ic_val_list_s *)value; - real->list = src; - - dest = ic_list_append(dest, value); - - return dest; + return ic_value_get_int(value); } -API iotcon_list_h iotcon_list_append_repr(iotcon_list_h list, iotcon_repr_h repr) + +API bool iotcon_list_get_nth_bool(iotcon_list_h list, int index) { - RETV_IF(NULL == list, NULL); - RETV_IF(NULL == repr, NULL); - RETVM_IF(IOTCON_TYPE_REPR != list->type, list, "IOTCON_ERR_PARAM(%d)", list->type); + iotcon_value_h value; - iotcon_value_h value = ic_value_new(IOTCON_TYPE_REPR); + RETV_IF(NULL == list, false); + RETV_IF(NULL == list->list, false); + + value = g_list_nth_data(list->list, index); if (NULL == value) { - ERR("ic_value_new(IOTCON_TYPE_REPR) Fail"); - return list; + ERR("g_list_nth_data() Fail"); + return false; } - ic_val_repr_s *real = (ic_val_repr_s *)value; - real->repr = repr; + return ic_value_get_bool(value); +} - list = ic_list_append(list, value); - return list; +API double iotcon_list_get_nth_double(iotcon_list_h list, int index) +{ + iotcon_value_h value; + + RETV_IF(NULL == list, 0.0); + RETV_IF(NULL == list->list, 0.0); + + value = g_list_nth_data(list->list, index); + if (NULL == value) { + ERR("g_list_nth_data() Fail"); + return 0.0; + } + + return ic_value_get_double(value); } -API iotcon_repr_h iotcon_list_get_nth_repr(iotcon_list_h list, int index) + +API const char* iotcon_list_get_nth_str(iotcon_list_h list, int index) { + iotcon_value_h value; + RETV_IF(NULL == list, NULL); RETV_IF(NULL == list->list, NULL); - iotcon_value_h value = g_list_nth_data(list->list, index); + value = g_list_nth_data(list->list, index); if (NULL == value) { ERR("g_list_nth_data() Fail"); return NULL; } - ic_val_repr_s *real = (ic_val_repr_s *) value; - RETVM_IF(IOTCON_TYPE_REPR != real->type, NULL, "IOTCON_ERR_PARAM(%d)", real->type); - - return real->repr; + return ic_value_get_str(value); } -API int iotcon_list_get_length(iotcon_list_h list) + +API iotcon_list_h iotcon_list_get_nth_list(iotcon_list_h list, int index) { - RETV_IF(NULL == list, IOTCON_ERR_PARAM); - RETV_IF(NULL == list->list, IOTCON_ERR_PARAM); + iotcon_value_h value; - return g_list_length(list->list); + RETV_IF(NULL == list, NULL); + RETV_IF(NULL == list->list, NULL); + + value = g_list_nth_data(list->list, index); + if (NULL == value) { + ERR("g_list_nth_data() Fail"); + return NULL; + } + + return ic_value_get_list(value); } -API iotcon_list_h iotcon_list_get_nth_list(iotcon_list_h list, int index) + +API iotcon_repr_h iotcon_list_get_nth_repr(iotcon_list_h list, int index) { + iotcon_value_h value; + RETV_IF(NULL == list, NULL); RETV_IF(NULL == list->list, NULL); - iotcon_value_h value = g_list_nth_data(list->list, index); + value = g_list_nth_data(list->list, index); if (NULL == value) { ERR("g_list_nth_data() Fail"); return NULL; } - ic_val_list_s *real = (ic_val_list_s *) value; - RETVM_IF(IOTCON_TYPE_LIST != real->type, NULL, "IOTCON_ERR_PARAM(%d)", real->type); + return ic_value_get_repr(value); +} + + +static int _ic_list_del_nth_value(iotcon_list_h list, int pos, iotcon_repr_types_e value_type) +{ + iotcon_value_h value; - return real->list; + RETV_IF(NULL == list, IOTCON_ERROR_PARAM); + RETV_IF(NULL == list->list, IOTCON_ERROR_PARAM); + RETVM_IF(value_type != list->type, IOTCON_ERROR_PARAM, "IOTCON_ERROR_PARAM(%d)", + list->type); + + value = g_list_nth_data(list->list, pos); + if (NULL == value) { + ERR("g_list_nth_data() Fail"); + return IOTCON_ERROR_NO_DATA; + } + + if (IOTCON_TYPE_STR == value->type) { + 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; + iotcon_list_free(real->list); + } + else if (IOTCON_TYPE_REPR == value->type) { + ic_val_repr_s *real = (ic_val_repr_s*)value; + iotcon_repr_free(real->repr); + } + + ic_list_remove(list, value); + return IOTCON_ERROR_NONE; } -API iotcon_value_h iotcon_list_get_nth_value(iotcon_list_h list, int index) + +API int iotcon_list_del_nth_int(iotcon_list_h list, int pos) { - RETV_IF(NULL == list, NULL); - RETV_IF(NULL == list->list, NULL); + int ret; - return g_list_nth_data(list->list, index); + ret = _ic_list_del_nth_value(list, pos, IOTCON_TYPE_INT); + if (IOTCON_ERROR_NONE != ret) + ERR("iotcon_list_del_nth_int() Fail(%d)", ret); + + return ret; +} + + +API int iotcon_list_del_nth_bool(iotcon_list_h list, int pos) +{ + int ret; + + ret = _ic_list_del_nth_value(list, pos, IOTCON_TYPE_BOOL); + if (IOTCON_ERROR_NONE != ret) + ERR("_ic_list_del_nth_value() Fail(%d)", ret); + + return ret; +} + + +API int iotcon_list_del_nth_double(iotcon_list_h list, int pos) +{ + int ret; + + ret = _ic_list_del_nth_value(list, pos, IOTCON_TYPE_DOUBLE); + if (IOTCON_ERROR_NONE != ret) + ERR("_ic_list_del_nth_value() Fail(%d)", ret); + + return ret; +} + + +API int iotcon_list_del_nth_str(iotcon_list_h list, int pos) +{ + int ret; + + ret = _ic_list_del_nth_value(list, pos, IOTCON_TYPE_STR); + if (IOTCON_ERROR_NONE != ret) + ERR("_ic_list_del_nth_value() Fail(%d)", ret); + + return ret; +} + + +API int iotcon_list_del_nth_list(iotcon_list_h list, int pos) +{ + int ret; + + ret = _ic_list_del_nth_value(list, pos, IOTCON_TYPE_LIST); + if (IOTCON_ERROR_NONE != ret) + ERR("_ic_list_del_nth_value() Fail(%d)", ret); + + return ret; +} + + +API int iotcon_list_del_nth_repr(iotcon_list_h list, int pos) +{ + int ret; + + ret = _ic_list_del_nth_value(list, pos, IOTCON_TYPE_REPR); + if (IOTCON_ERROR_NONE != ret) + ERR("_ic_list_del_nth_value() Fail(%d)", ret); + + return ret; +} + + +API int iotcon_list_get_type(iotcon_list_h list) +{ + RETV_IF(NULL == list, IOTCON_ERROR_PARAM); + + return list->type; } + +API int iotcon_list_get_length(iotcon_list_h list) +{ + RETV_IF(NULL == list, IOTCON_ERROR_PARAM); + RETV_IF(NULL == list->list, IOTCON_ERROR_PARAM); + + return g_list_length(list->list); +} + + int ic_list_remove(iotcon_list_h list, iotcon_value_h val) { - RETV_IF(NULL == list, IOTCON_ERR_PARAM); + RETV_IF(NULL == list, IOTCON_ERROR_PARAM); list->list = g_list_remove(list->list, val); - return IOTCON_ERR_NONE; + return IOTCON_ERROR_NONE; } -iotcon_list_h ic_list_append(iotcon_list_h list, iotcon_value_h value) + +iotcon_list_h ic_list_insert(iotcon_list_h list, iotcon_value_h value, int pos) { RETV_IF(NULL == list, NULL); - list->list = g_list_append(list->list, value); + list->list = g_list_insert(list->list, value, pos); if (NULL == list->list) { - ERR("g_list_append() Fail"); + ERR("g_list_insert() Fail"); return list; } return list; } -API void iotcon_list_foreach(iotcon_list_h list, iotcon_list_fn fn, void *user_data) +API void iotcon_int_list_foreach(iotcon_list_h list, iotcon_int_list_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)"); + + cur = list->list; + while (cur) { + GList *next = cur->next; + real = cur->data; + (*fn)(index, real->val.i, user_data); + index++; + cur = next; + } +} + +API void iotcon_bool_list_foreach(iotcon_list_h list, iotcon_bool_list_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)"); + + cur = list->list; + while (cur) { + GList *next = cur->next; + real = cur->data; + (*fn)(index, real->val.b, user_data); + index++; + cur = next; + } +} + +API void iotcon_double_list_foreach(iotcon_list_h list, iotcon_double_list_fn fn, void *user_data) { - RETM_IF(NULL == list, "IOTCON_ERR_PARAM(NULL == list)"); - RETM_IF(NULL == fn, "IOTCON_ERR_PARAM(NULL == fn)"); + GList *cur; + int index = 0; + ic_basic_s *real = NULL; - g_list_foreach(list->list, (GFunc)fn, user_data); + RETM_IF(NULL == list, "IOTCON_ERROR_PARAM(NULL == list)"); + RETM_IF(NULL == fn, "IOTCON_ERROR_PARAM(NULL == fn)"); + + cur = list->list; + while (cur) { + GList *next = cur->next; + real = cur->data; + (*fn)(index, real->val.d, user_data); + index++; + cur = next; + } } -JsonArray* ic_repr_generate_json_array(iotcon_list_h list) +API void iotcon_str_list_foreach(iotcon_list_h list, iotcon_str_list_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)"); + + cur = list->list; + while (cur) { + GList *next = cur->next; + real = cur->data; + (*fn)(index, real->val.s, user_data); + index++; + cur = next; + } +} + +API void iotcon_list_list_foreach(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)"); + + cur = list->list; + while (cur) { + GList *next = cur->next; + real = cur->data; + (*fn)(index, real->list, user_data); + index++; + cur = next; + } +} + +API void iotcon_repr_list_foreach(iotcon_list_h list, iotcon_repr_list_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)"); + + cur = list->list; + while (cur) { + GList *next = cur->next; + real = cur->data; + (*fn)(index, real->repr, user_data); + index++; + cur = next; + } +} + + +static iotcon_value_h _ic_list_get_nth_value(iotcon_list_h list, int index) +{ + RETV_IF(NULL == list, NULL); + RETV_IF(NULL == list->list, NULL); + + return g_list_nth_data(list->list, index); +} + + + +/* +* A general result : [1,2,3] +*/ +JsonArray* ic_list_to_json(iotcon_list_h list) { int i; int count = 0; JsonArray *parray = NULL; - JsonNode *node_child = NULL; - int error_code = IOTCON_ERR_NONE; - - JsonObject *obj_child = NULL; - JsonArray *array_child = NULL; - iotcon_repr_h repr_child = NULL; - iotcon_list_h iotlist_child = NULL; - iotcon_value_h iotvalue_child = NULL; + JsonNode *child_node = NULL; + JsonObject *child_obj = NULL; + JsonArray *child_array = NULL; + iotcon_repr_h child_repr = NULL; + iotcon_list_h child_list = NULL; + iotcon_value_h child_value = NULL; RETV_IF(NULL == list, NULL); RETV_IF(NULL == list->list, NULL); @@ -286,134 +554,141 @@ JsonArray* ic_repr_generate_json_array(iotcon_list_h list) parray = json_array_new(); for (i = 0; i < count; i++) { - iotvalue_child = iotcon_list_get_nth_value(list, i); + child_value = _ic_list_get_nth_value(list, i); - int type = iotcon_value_get_type(iotvalue_child); + int type = child_value->type; switch (type) { case IOTCON_TYPE_INT: case IOTCON_TYPE_BOOL: case IOTCON_TYPE_DOUBLE: case IOTCON_TYPE_STR: case IOTCON_TYPE_NULL: - node_child = ic_repr_generate_json_value(iotvalue_child); - if (NULL == node_child) { - ERR("ic_repr_generate_json_value(iotvalue_child) Fail"); + child_node = ic_value_to_json(child_value); + if (NULL == child_node) { + ERR("ic_value_to_json(child_value) Fail"); json_array_unref(parray); return NULL; } - json_array_add_element(parray, node_child); + json_array_add_element(parray, child_node); break; case IOTCON_TYPE_LIST: - iotlist_child = iotcon_value_get_list(iotvalue_child); - array_child = ic_repr_generate_json_array(iotlist_child); - if (NULL == array_child) { - ERR("ic_repr_generate_json_array(list_child) Fail"); + child_list = ic_value_get_list(child_value); + child_array = ic_list_to_json(child_list); + if (NULL == child_array) { + ERR("ic_list_to_json(child_list) Fail"); json_array_unref(parray); return NULL; } - node_child = json_node_new(JSON_NODE_ARRAY); - json_node_set_array(node_child, array_child); - json_array_add_element(parray, node_child); + child_node = json_node_new(JSON_NODE_ARRAY); + json_node_set_array(child_node, child_array); + json_array_add_element(parray, child_node); break; case IOTCON_TYPE_REPR: - repr_child = iotcon_value_get_repr(iotvalue_child); - obj_child = ic_repr_generate_json_repr(repr_child, &error_code); - if (NULL == obj_child) { - ERR("ic_repr_generate_json_repr(repr_child) Fail"); + child_repr = ic_value_get_repr(child_value); + child_obj = ic_obj_to_json(child_repr); + if (NULL == child_obj) { + ERR("ic_obj_to_json(child_repr) Fail"); json_array_unref(parray); return NULL; } - node_child = json_node_new(JSON_NODE_OBJECT); - json_node_set_object(node_child, obj_child); - json_array_add_element(parray, node_child); + child_node = json_node_new(JSON_NODE_OBJECT); + json_node_set_object(child_node, child_obj); + json_array_add_element(parray, child_node); } } + return parray; } -iotcon_list_h ic_repr_parse_json_array(JsonArray *parray) +/* +* A general input : [1,2,3] +*/ +iotcon_list_h ic_list_from_json(JsonArray *parray) { int i; int count = json_array_get_length(parray); iotcon_list_h list = iotcon_list_new(IOTCON_TYPE_NONE); -// DBG("array count(%d)", count); + /* DBG("array count(%d)", count); */ for (i = 0; i < count; i++) { JsonNode *child_node = json_array_get_element(parray, i); if (JSON_NODE_HOLDS_NULL(child_node) || JSON_NODE_HOLDS_VALUE(child_node)) { - iotcon_value_h value = ic_repr_parse_json_value(child_node); + iotcon_value_h value = ic_value_from_json(child_node); if (NULL == value) { - ERR("ic_repr_parse_json_value() Fail(NULL == value)"); + ERR("ic_value_from_json() Fail"); + iotcon_list_free(list); return NULL; } 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_repr_free_basic_value(value); - iotcon_repr_free_list(list); + ic_value_free(value); + iotcon_list_free(list); return NULL; } - list = ic_list_append(list, value); + list = ic_list_insert(list, value, -1); list->type = real->type; - continue; } 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); + return NULL; + } + JsonArray *child_array = json_node_get_array(child_node); - iotcon_list_h parsed_list = ic_repr_parse_json_array(child_array); + iotcon_list_h parsed_list = ic_list_from_json(child_array); if (NULL == parsed_list) { - ERR("ic_repr_parse_json_array() Fail(NULL == parsed_list)"); + ERR("ic_list_from_json() Fail(NULL == parsed_list)"); + iotcon_list_free(list); return NULL; } - if (IOTCON_TYPE_NONE != list->type - && IOTCON_TYPE_LIST != list->type) { - ERR("Type matching Fail(%d)", list->type); - iotcon_repr_free_list(parsed_list); - iotcon_repr_free_list(list); + iotcon_value_h value = ic_value_new_list(parsed_list); + if (NULL == value) { + ERR("ic_value_new_list(%p) Fail", parsed_list); + iotcon_list_free(parsed_list); + iotcon_list_free(list); return NULL; } - iotcon_value_h value = ic_value_new(IOTCON_TYPE_LIST); - ic_value_set_list(value, parsed_list); - list = ic_list_append(list, value); + list = ic_list_insert(list, value, -1); list->type = IOTCON_TYPE_LIST; - - continue; } else if (JSON_NODE_HOLDS_OBJECT(child_node)) { - int error_code = IOTCON_ERR_NONE; + if (IOTCON_TYPE_NONE != list->type && IOTCON_TYPE_REPR != list->type) { + ERR("Type matching Fail(%d)", list->type); + iotcon_list_free(list); + return NULL; + } + JsonObject *child_obj = json_node_get_object(child_node); - JsonObject *repr_obj = json_object_get_object_member(child_obj, - IOTCON_KEY_REP); - iotcon_repr_h ret_repr = ic_repr_parse_json_obj(repr_obj, &error_code); + iotcon_repr_h ret_repr = ic_obj_from_json(child_obj); if (NULL == ret_repr) { - ERR("ic_repr_parse_json_obj() Fail(NULL == ret_repr)"); + ERR("ic_obj_from_json() Fail(NULL == ret_repr)"); + iotcon_list_free(list); return NULL; } - if (IOTCON_TYPE_NONE != list->type - && IOTCON_TYPE_REPR != list->type) { - ERR("Type matching Fail(%d)", list->type); + iotcon_value_h value = ic_value_new_repr(ret_repr); + if (NULL == value) { + ERR("ic_value_new_repr(%p) Fail", ret_repr); iotcon_repr_free(ret_repr); - iotcon_repr_free_list(list); - return NULL; + iotcon_list_free(list); } - iotcon_value_h value = ic_value_new(IOTCON_TYPE_REPR); - ic_value_set_repr(value, ret_repr); - list = ic_list_append(list, value); + list = ic_list_insert(list, value, -1); list->type = IOTCON_TYPE_REPR; - continue; } } return list; } -API void iotcon_repr_free_list(iotcon_list_h list) +API void iotcon_list_free(iotcon_list_h list) { FN_CALL; int count = 0; @@ -427,8 +702,137 @@ API void iotcon_repr_free_list(iotcon_list_h list) DBG("list count(%d)", count); cur = list->list; while (cur) { - ic_repr_free_value(cur->data); + ic_value_free(cur->data); cur = cur->next; } free(list); } + +static int _ic_list_clone_value(iotcon_list_h list, iotcon_list_h ret_list) +{ + int i, count; + iotcon_value_h value, copied_value; + + count = g_list_length(list->list); + for (i = 0; i < count; i++) { + value = _ic_list_get_nth_value(list, i); + if (list->type != value->type) { + ERR("Type Mismatching(list:%d, value:%d)", list->type, value->type); + return IOTCON_ERROR_FAIL; + } + + copied_value = ic_value_clone(value); + if (NULL == copied_value) { + ERR("ic_value_clone() Fail"); + return IOTCON_ERROR_FAIL; + } + + ic_list_insert(ret_list, copied_value, -1); + } + + return IOTCON_ERROR_NONE; +} + + +static int _ic_list_clone_list(iotcon_list_h list, iotcon_list_h ret_list) +{ + int i, count; + 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"); + return IOTCON_ERROR_FAIL; + } + + copied_list = ic_list_clone(list_val); + if (NULL == copied_list) { + ERR("ic_list_clone() Fail"); + return IOTCON_ERROR_FAIL; + } + + iotcon_list_insert_list(ret_list, copied_list, -1); + } + + return IOTCON_ERROR_NONE; +} + + +static int _ic_list_clone_repr(iotcon_list_h list, iotcon_list_h ret_list) +{ + int i, count; + 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) { + ERR("iotcon_list_get_nth_repr() Fail"); + return IOTCON_ERROR_FAIL; + } + + copied_repr = iotcon_repr_clone(repr_val); + if (NULL == copied_repr) { + ERR("_ic_repr_clone_repr() Fail"); + return IOTCON_ERROR_FAIL; + } + + iotcon_list_insert_repr(ret_list, copied_repr, -1); + } + + return IOTCON_ERROR_NONE; +} + + +iotcon_list_h ic_list_clone(iotcon_list_h list) +{ + int ret; + iotcon_list_h ret_list = NULL; + + RETV_IF(NULL == list, NULL); + RETV_IF(NULL == list->list, NULL); + + ret_list = iotcon_list_new(list->type); + if (NULL == ret_list) { + ERR("iotcon_list_new(%d) Fail", list->type); + return NULL; + } + + 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); + break; + } + + return ret_list; +} diff --git a/lib/ic-repr-list.h b/lib/ic-repr-list.h index ba662e6..54ac17c 100644 --- a/lib/ic-repr-list.h +++ b/lib/ic-repr-list.h @@ -21,9 +21,11 @@ #include "iotcon-struct.h" int ic_list_remove(iotcon_list_h list, iotcon_value_h val); -iotcon_list_h ic_list_append(iotcon_list_h list, iotcon_value_h value); +iotcon_list_h ic_list_insert(iotcon_list_h list, iotcon_value_h value, int pos); -JsonArray* ic_repr_generate_json_array(iotcon_list_h list); -iotcon_list_h ic_repr_parse_json_array(JsonArray *parray); +JsonArray* ic_list_to_json(iotcon_list_h list); +iotcon_list_h ic_list_from_json(JsonArray *parray); -#endif // __IOT_CONNECTIVITY_MANAGER_INTERNAL_REPRESENTATION_LIST_H__ +iotcon_list_h ic_list_clone(iotcon_list_h list); + +#endif /* __IOT_CONNECTIVITY_MANAGER_INTERNAL_REPRESENTATION_LIST_H__ */ diff --git a/lib/ic-repr-obj.c b/lib/ic-repr-obj.c new file mode 100644 index 0000000..12cba8b --- /dev/null +++ b/lib/ic-repr-obj.c @@ -0,0 +1,620 @@ +/* Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "iotcon.h" + +#include "ic-common.h" +#include "ic-struct.h" +#include "ic-utils.h" +#include "ic-repr-list.h" +#include "ic-repr-value.h" +#include "ic-repr.h" +#include "ic-repr-obj.h" + +int ic_obj_del_value(iotcon_repr_h repr, const char *key, + iotcon_repr_types_e value_type) +{ + gboolean ret = FALSE; + iotcon_value_h value = NULL; + + RETV_IF(NULL == repr, false); + RETV_IF(NULL == key, false); + + value = g_hash_table_lookup(repr->hash_table, key); + if (NULL == value) { + ERR("g_hash_table_lookup(%s) Fail", key); + return IOTCON_ERROR_NO_DATA; + } + + if (value_type != value->type) { + ERR("Type matching Fail(input:%d, saved:%d)", value_type, value->type); + return IOTCON_ERROR_PARAM; + } + + ret = g_hash_table_remove(repr->hash_table, key); + if (FALSE == ret) { + ERR("g_hash_table_remove(%s) Fail", key); + return IOTCON_ERROR_NO_DATA; + } + + return IOTCON_ERROR_NONE; +} + +API int iotcon_repr_get_int(iotcon_repr_h repr, const char *key) +{ + iotcon_value_h value; + + RETV_IF(NULL == repr, 0); + RETV_IF(NULL == key, 0); + + value = g_hash_table_lookup(repr->hash_table, key); + if (NULL == value) { + ERR("g_hash_table_lookup() Fail"); + return 0; + } + + ic_basic_s *real = (ic_basic_s*)value; + RETV_IF(IOTCON_TYPE_INT != real->type, 0); + + return real->val.i; +} + +API int iotcon_repr_set_int(iotcon_repr_h repr, const char *key, int val) +{ + iotcon_value_h value; + + RETV_IF(NULL == repr, IOTCON_ERROR_PARAM); + RETV_IF(NULL == key, IOTCON_ERROR_PARAM); + + value = ic_value_new_int(val); + if (NULL == value) { + ERR("ic_value_new_int(%d) Fail", val); + return IOTCON_ERROR_MEMORY; + } + + g_hash_table_replace(repr->hash_table, ic_utils_strdup(key), value); + + return IOTCON_ERROR_NONE; +} + +API int iotcon_repr_del_int(iotcon_repr_h repr, const char *key) +{ + int ret; + + RETV_IF(NULL == repr, IOTCON_ERROR_PARAM); + RETV_IF(NULL == key, IOTCON_ERROR_PARAM); + + ret = ic_obj_del_value(repr, key, IOTCON_TYPE_INT); + if (IOTCON_ERROR_NONE != ret) + ERR("ic_obj_del_value() Fail(%d)", ret); + + return ret; +} + +API bool iotcon_repr_get_bool(iotcon_repr_h repr, const char *key) +{ + ic_basic_s *real = NULL; + iotcon_value_h value = NULL; + + RETV_IF(NULL == repr, false); + RETV_IF(NULL == key, false); + + value = g_hash_table_lookup(repr->hash_table, key); + if (NULL == value) { + ERR("g_hash_table_lookup() Fail"); + return false; + } + + real = (ic_basic_s*)value; + RETV_IF(IOTCON_TYPE_BOOL != real->type, false); + + return real->val.b; +} + +API int iotcon_repr_set_bool(iotcon_repr_h repr, const char *key, bool val) +{ + iotcon_value_h value = NULL; + + RETV_IF(NULL == repr, IOTCON_ERROR_PARAM); + RETV_IF(NULL == key, IOTCON_ERROR_PARAM); + + value = ic_value_new_bool(val); + if (NULL == value) { + ERR("ic_value_new_bool(%d) Fail", val); + return IOTCON_ERROR_MEMORY; + } + + g_hash_table_replace(repr->hash_table, ic_utils_strdup(key), value); + + return IOTCON_ERROR_NONE; +} + +API int iotcon_repr_del_bool(iotcon_repr_h repr, const char *key) +{ + int ret; + + ret = ic_obj_del_value(repr, key, IOTCON_TYPE_BOOL); + if (IOTCON_ERROR_NONE != ret) + ERR("ic_obj_del_value() Fail(%d)", ret); + + return ret; +} + +API double iotcon_repr_get_double(iotcon_repr_h repr, const char *key) +{ + ic_basic_s *real = NULL; + iotcon_value_h value = NULL; + + RETV_IF(NULL == repr, 0.0); + RETV_IF(NULL == key, 0.0); + + value = g_hash_table_lookup(repr->hash_table, key); + if (NULL == value) { + ERR("g_hash_table_lookup() Fail"); + return 0; + } + + real = (ic_basic_s*)value; + RETV_IF(IOTCON_TYPE_DOUBLE != real->type, 0.0); + + return real->val.d; +} + +API int iotcon_repr_set_double(iotcon_repr_h repr, const char *key, double val) +{ + iotcon_value_h value = NULL; + + RETV_IF(NULL == repr, IOTCON_ERROR_PARAM); + RETV_IF(NULL == key, IOTCON_ERROR_PARAM); + + value = ic_value_new_double(val); + if (NULL == value) { + ERR("ic_value_new_double(%f) Fail", val); + return IOTCON_ERROR_MEMORY; + } + + g_hash_table_replace(repr->hash_table, ic_utils_strdup(key), value); + + return IOTCON_ERROR_NONE; +} + +API int iotcon_repr_del_double(iotcon_repr_h repr, const char *key) +{ + int ret; + + RETV_IF(NULL == repr, IOTCON_ERROR_PARAM); + RETV_IF(NULL == key, IOTCON_ERROR_PARAM); + + ret = ic_obj_del_value(repr, key, IOTCON_TYPE_DOUBLE); + if (IOTCON_ERROR_NONE != ret) + ERR("ic_obj_del_value() Fail(%d)", ret); + + return ret; +} + +API char* iotcon_repr_get_str(iotcon_repr_h repr, const char *key) +{ + ic_basic_s *real = NULL; + iotcon_value_h value = NULL; + + RETV_IF(NULL == repr, NULL); + RETV_IF(NULL == key, NULL); + + value = g_hash_table_lookup(repr->hash_table, key); + if (NULL == value) { + ERR("g_hash_table_lookup() Fail"); + return NULL; + } + + real = (ic_basic_s*)value; + RETV_IF(IOTCON_TYPE_STR != real->type, NULL); + + return real->val.s; +} + +API int iotcon_repr_set_str(iotcon_repr_h repr, const char *key, char *val) +{ + iotcon_value_h value = NULL; + + RETV_IF(NULL == repr, IOTCON_ERROR_PARAM); + RETV_IF(NULL == key, IOTCON_ERROR_PARAM); + + value = ic_value_new_str(val); + if (NULL == value) { + ERR("ic_value_new_str(%s) Fail", val); + return IOTCON_ERROR_MEMORY; + } + + g_hash_table_replace(repr->hash_table, ic_utils_strdup(key), value); + + return IOTCON_ERROR_NONE; +} + +API int iotcon_repr_del_str(iotcon_repr_h repr, const char *key) +{ + int ret; + + RETV_IF(NULL == repr, IOTCON_ERROR_PARAM); + RETV_IF(NULL == key, IOTCON_ERROR_PARAM); + + ret = ic_obj_del_value(repr, key, IOTCON_TYPE_STR); + if (IOTCON_ERROR_NONE != ret) + ERR("ic_obj_del_value() Fail(%d)", ret); + + return ret; +} + +API bool iotcon_repr_is_null(iotcon_repr_h repr, const char *key) +{ + ic_basic_s *real = NULL; + iotcon_value_h value = NULL; + + RETV_IF(NULL == repr, false); + RETV_IF(NULL == key, false); + + value = (iotcon_value_h) g_hash_table_lookup(repr->hash_table, key); + if (NULL == value) { + ERR("g_hash_table_lookup() Fail"); + return false; + } + + real = (ic_basic_s*)value; + + return (IOTCON_TYPE_NULL == real->type) ? true : false; +} + +API int iotcon_repr_set_null(iotcon_repr_h repr, const char *key) +{ + iotcon_value_h value = NULL; + + RETV_IF(NULL == repr, IOTCON_ERROR_PARAM); + RETV_IF(NULL == key, IOTCON_ERROR_PARAM); + + value = ic_value_new_null(); + if (NULL == value) { + ERR("ic_value_new_null() Fail"); + return IOTCON_ERROR_MEMORY; + } + + g_hash_table_replace(repr->hash_table, ic_utils_strdup(key), value); + + return IOTCON_ERROR_NONE; +} + +API int iotcon_repr_del_null(iotcon_repr_h repr, const char *key) +{ + int ret; + + RETV_IF(NULL == repr, IOTCON_ERROR_PARAM); + RETV_IF(NULL == key, IOTCON_ERROR_PARAM); + + ret = ic_obj_del_value(repr, key, IOTCON_TYPE_NULL); + if (IOTCON_ERROR_NONE != ret) + ERR("ic_obj_del_value() Fail(%d)", ret); + + return ret; +} + +API iotcon_list_h iotcon_repr_get_list(iotcon_repr_h repr, const char *key) +{ + iotcon_value_h value = NULL; + ic_val_list_s *real = NULL; + + RETV_IF(NULL == repr, NULL); + RETV_IF(NULL == key, NULL); + + value = g_hash_table_lookup(repr->hash_table, key); + if (NULL == value) { + ERR("g_hash_table_lookup() Fail"); + return NULL; + } + + real = (ic_val_list_s*)value; + RETV_IF(IOTCON_TYPE_LIST != real->type, NULL); + + return real->list; +} + +API int iotcon_repr_set_list(iotcon_repr_h repr, const char *key, iotcon_list_h list) +{ + FN_CALL; + iotcon_value_h value = NULL; + + RETV_IF(NULL == repr, IOTCON_ERROR_PARAM); + RETV_IF(NULL == key, IOTCON_ERROR_PARAM); + + value = ic_value_new_list(list); + if (NULL == value) { + ERR("ic_value_new_list() Fail"); + return IOTCON_ERROR_MEMORY; + } + + g_hash_table_replace(repr->hash_table, ic_utils_strdup(key), value); + + return IOTCON_ERROR_NONE; +} + +API int iotcon_repr_del_list(iotcon_repr_h repr, const char *key) +{ + int ret; + + RETV_IF(NULL == repr, IOTCON_ERROR_PARAM); + RETV_IF(NULL == key, IOTCON_ERROR_PARAM); + + ret = ic_obj_del_value(repr, key, IOTCON_TYPE_LIST); + if (IOTCON_ERROR_NONE != ret) + ERR("ic_obj_del_value() Fail(%d)", ret); + + return ret; +} + +API iotcon_repr_h iotcon_repr_get_repr(iotcon_repr_h repr, const char *key) +{ + ic_val_repr_s *real = NULL; + iotcon_value_h value = NULL; + + RETV_IF(NULL == repr, NULL); + RETV_IF(NULL == key, NULL); + + value = g_hash_table_lookup(repr->hash_table, key); + if (NULL == value) { + ERR("g_hash_table_lookup() Fail"); + return NULL; + } + + real = (ic_val_repr_s*)value; + RETV_IF(IOTCON_TYPE_REPR != real->type, NULL); + + return real->repr; +} + +API int iotcon_repr_set_repr(iotcon_repr_h repr, const char *key, iotcon_repr_h val) +{ + iotcon_value_h value = NULL; + + 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_repr(val); + if (NULL == value) { + ERR("ic_value_new_repr(%p) Fail", val); + return IOTCON_ERROR_MEMORY; + } + + g_hash_table_replace(repr->hash_table, ic_utils_strdup(key), value); + + return IOTCON_ERROR_NONE; +} + +API int iotcon_repr_del_repr(iotcon_repr_h repr, const char *key) +{ + int ret; + + RETV_IF(NULL == repr, IOTCON_ERROR_PARAM); + RETV_IF(NULL == key, IOTCON_ERROR_PARAM); + + ret = ic_obj_del_value(repr, key, IOTCON_TYPE_REPR); + if (IOTCON_ERROR_NONE != ret) + ERR("ic_obj_del_value() Fail(%d)", ret); + + return ret; +} + +int ic_obj_set_value(iotcon_repr_h repr, const char *key, iotcon_value_h value) +{ + RETV_IF(NULL == repr, IOTCON_ERROR_PARAM); + RETV_IF(NULL == key, IOTCON_ERROR_PARAM); + RETV_IF(NULL == value, IOTCON_ERROR_PARAM); + + g_hash_table_replace(repr->hash_table, ic_utils_strdup(key), value); + + return IOTCON_ERROR_NONE; +} + +static inline int _ic_obj_to_json(GHashTable *hash, GList *key_list, unsigned int index, + JsonObject *json_obj) +{ + FN_CALL; + int type; + char *key; + iotcon_repr_h child_repr = NULL; + iotcon_list_h child_list = NULL; + iotcon_value_h value = NULL; + + JsonObject *child_obj = NULL; + JsonNode *child_node = NULL; + JsonArray *child_array = NULL; + + RETV_IF(NULL == hash, IOTCON_ERROR_PARAM); + RETV_IF(NULL == key_list, IOTCON_ERROR_PARAM); + RETV_IF(index < 0, IOTCON_ERROR_PARAM); + + key = g_list_nth_data(key_list, index); + value = g_hash_table_lookup(hash, key); + if (NULL == value) + ERR("g_hash_table_lookup(%s) Fail", key); + + type = value->type; + switch (type) { + case IOTCON_TYPE_INT: + case IOTCON_TYPE_BOOL: + case IOTCON_TYPE_DOUBLE: + case IOTCON_TYPE_STR: + case IOTCON_TYPE_NULL: + child_node = ic_value_to_json(value); + if (NULL == child_node) { + ERR("ic_value_to_json() Fail"); + return IOTCON_ERROR_PARAM; + } + json_object_set_member(json_obj, key, child_node); + break; + case IOTCON_TYPE_LIST: + child_list = ic_value_get_list(value); + child_array = ic_list_to_json(child_list); + if (NULL == child_array) { + ERR("ic_list_to_json() Fail"); + return IOTCON_ERROR_PARAM; + } + child_node = json_node_new(JSON_NODE_ARRAY); + json_node_set_array(child_node, child_array); + json_object_set_member(json_obj, key, child_node); + break; + case IOTCON_TYPE_REPR: + child_repr = ic_value_get_repr(value); + child_obj = ic_obj_to_json(child_repr); + if (NULL == child_obj) { + ERR("ic_obj_to_json() Fail"); + return IOTCON_ERROR_PARAM; + } + child_node = json_node_new(JSON_NODE_OBJECT); + json_node_set_object(child_node, child_obj); + json_object_set_member(json_obj, key, child_node); + break; + default: + ERR("Invalid type(%d)", type); + return IOTCON_ERROR_PARAM; + } + + return IOTCON_ERROR_NONE; +} + +/* +* A general result : {"rep":{"string":"Hello","intlist":[1,2,3]}} +*/ +JsonObject* ic_obj_to_json(iotcon_repr_h repr) +{ + FN_CALL; + int ret; + unsigned int i = 0; + GList *key_list = NULL; + JsonObject *json_obj = NULL; + JsonObject *parent_obj; + + RETV_IF(NULL == repr, NULL); + + key_list = iotcon_repr_get_key_list(repr); + if (key_list) { + json_obj = json_object_new(); + ic_utils_print_str_list(key_list); + + for (i = 0; i < g_list_length(key_list); i++) { + ret = _ic_obj_to_json(repr->hash_table, key_list, i, json_obj); + if (IOTCON_ERROR_NONE != ret) { + ERR("_ic_obj_to_json() Fail(%d)", ret); + json_object_unref(json_obj); + g_list_free(key_list); + return NULL; + } + } + g_list_free(key_list); + } + + parent_obj = json_object_new(); + + if (json_obj) + json_object_set_object_member(parent_obj, IOTCON_KEY_REP, json_obj); + + return parent_obj; +} + +static inline int _ic_obj_from_json(JsonObject *obj, GList *key_list, unsigned int index, + iotcon_repr_h repr) +{ + char *key; + + RETV_IF(NULL == obj, IOTCON_ERROR_PARAM); + RETV_IF(NULL == key_list, IOTCON_ERROR_PARAM); + RETV_IF(index < 0, IOTCON_ERROR_PARAM); + RETV_IF(NULL == repr, IOTCON_ERROR_PARAM); + + key = g_list_nth_data(key_list, index); + + /* search child object recursively */ + JsonNode *child_node = json_object_get_member(obj, key); + if (NULL == child_node) { + ERR("json_object_get_member() Fail(NULL == child_node)"); + return IOTCON_ERROR_PARAM; + } + if (JSON_NODE_HOLDS_NULL(child_node) || JSON_NODE_HOLDS_VALUE(child_node)) { + iotcon_value_h value = ic_value_from_json(child_node); + if (NULL == value) { + ERR("ic_value_from_json() Fail(NULL == value)"); + return IOTCON_ERROR_PARAM; + } + ic_obj_set_value(repr, key, value); + } + else if (JSON_NODE_HOLDS_ARRAY(child_node)) { + JsonArray *child_array = json_node_get_array(child_node); + iotcon_list_h list = ic_list_from_json(child_array); + if (NULL == list) { + ERR("ic_list_from_json() Fail(NULL == list)"); + return IOTCON_ERROR_PARAM; + } + iotcon_repr_set_list(repr, key, list); + } + else if (JSON_NODE_HOLDS_OBJECT(child_node)) { + JsonObject *child_obj = json_node_get_object(child_node); + iotcon_repr_h ret_repr = ic_obj_from_json(child_obj); + if (NULL == ret_repr) { + ERR("ic_obj_from_json() Fail(NULL == ret_repr)"); + return IOTCON_ERROR_PARAM; + } + iotcon_repr_set_repr(repr, key, ret_repr); + } + else { + ERR("node type(%d) Fail", json_node_get_node_type(child_node)); + return IOTCON_ERROR_PARAM; + } + + return IOTCON_ERROR_NONE; +} + +/* +* A general input : {"rep:"{"string":"Hello","intlist":[1,2,3]}} +*/ +iotcon_repr_h ic_obj_from_json(JsonObject *json_repr) +{ + int ret; + unsigned int i = 0; + GList *key_list = NULL; + iotcon_repr_h repr = NULL; + JsonObject *obj = NULL; + + RETV_IF(NULL == json_repr, NULL); + + obj = json_object_get_object_member(json_repr, IOTCON_KEY_REP); + + key_list = json_object_get_members(obj); + + repr = iotcon_repr_new(); + if (key_list) { + ic_utils_print_str_list(key_list); + + for (i = 0; i < g_list_length(key_list); i++) { + ret = _ic_obj_from_json(obj, key_list, i, repr); + if (IOTCON_ERROR_NONE != ret) { + ERR("_ic_obj_from_json() Fail(%d)", ret); + iotcon_repr_free(repr); + g_list_free(key_list); + return NULL; + } + } + g_list_free(key_list); + } + + return repr; +} + diff --git a/lib/ic-repr-obj.h b/lib/ic-repr-obj.h new file mode 100644 index 0000000..24f665a --- /dev/null +++ b/lib/ic-repr-obj.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef __IOT_CONNECTIVITY_MANAGER_INTERNAL_REPRESENTATION_OBJECT_H__ +#define __IOT_CONNECTIVITY_MANAGER_INTERNAL_REPRESENTATION_OBJECT_H__ + +#include + +int ic_obj_del_value(iotcon_repr_h repr, const char *key, + iotcon_repr_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); + +JsonObject* ic_obj_to_json(iotcon_repr_h repr); +iotcon_repr_h ic_obj_from_json(JsonObject *obj); + +#endif /* __IOT_CONNECTIVITY_MANAGER_INTERNAL_REPRESENTATION_OBJECT_H__ */ diff --git a/lib/ic-repr-value.c b/lib/ic-repr-value.c index 4c12332..5288f9f 100644 --- a/lib/ic-repr-value.c +++ b/lib/ic-repr-value.c @@ -28,10 +28,9 @@ #include "ic-repr-list.h" #include "ic-repr-value.h" -iotcon_value_h ic_value_new(int type) +static iotcon_value_h _ic_value_new(int type) { iotcon_value_h ret_val; - errno = 0; switch (type) { case IOTCON_TYPE_INT: @@ -62,265 +61,337 @@ iotcon_value_h ic_value_new(int type) return ret_val; } -API int iotcon_value_get_type(iotcon_value_h value) + +iotcon_value_h ic_value_new_null() { - RETV_IF(NULL == value, IOTCON_ERR_PARAM); - return value->type; + iotcon_value_h value; + + value = _ic_value_new(IOTCON_TYPE_NULL); + if (NULL == value) { + ERR("_ic_value_new(NULL) Fail"); + return NULL; + } + + return value; } -API int iotcon_value_get_int(iotcon_value_h value) +iotcon_value_h ic_value_new_int(int val) { - ic_basic_s *real = (ic_basic_s*)value; + ic_basic_s *value; - RETV_IF(NULL == value, IOTCON_ERR_PARAM); - RETVM_IF(IOTCON_TYPE_INT != real->type, IOTCON_ERR_PARAM, "IOTCON_ERR_PARAM(%d)", - real->type); + value = (ic_basic_s*)_ic_value_new(IOTCON_TYPE_INT); + if (NULL == value) { + ERR("_ic_value_new(INT:%d) Fail", val); + return NULL; + } - return real->val.i; + value->val.i = val; + + return (iotcon_value_h)value; } -int ic_value_set_int(iotcon_value_h value, int ival) +iotcon_value_h ic_value_new_bool(bool val) { - ic_basic_s *real = (ic_basic_s*)value; + ic_basic_s *value; - RETV_IF(NULL == value, IOTCON_ERR_PARAM); - RETVM_IF(IOTCON_TYPE_INT != real->type, IOTCON_ERR_PARAM, "IOTCON_ERR_PARAM(%d)", - real->type); + value = (ic_basic_s*)_ic_value_new(IOTCON_TYPE_BOOL); + if (NULL == value) { + ERR("_ic_value_new(BOOL:%d) Fail", val); + return NULL; + } - real->val.i = ival; + value->val.b = val; - return IOTCON_ERR_NONE; + return (iotcon_value_h)value; } -API bool iotcon_value_get_bool(iotcon_value_h value) +iotcon_value_h ic_value_new_double(double val) { - ic_basic_s *real = (ic_basic_s*)value; + ic_basic_s *value; - RETV_IF(NULL == value, IOTCON_ERR_PARAM); - RETVM_IF(IOTCON_TYPE_BOOL != real->type, false, "IOTCON_ERR_PARAM(%d)", real->type); + value = (ic_basic_s*)_ic_value_new(IOTCON_TYPE_DOUBLE); + if (NULL == value) { + ERR("_ic_value_new(DOUBLE:%f) Fail", val); + return NULL; + } - return real->val.b; + value->val.d = val; + + return (iotcon_value_h)value; } -int ic_value_set_bool(iotcon_value_h value, bool bval) +iotcon_value_h ic_value_new_str(char *val) { - ic_basic_s *real = (ic_basic_s*)value; + ic_basic_s *value; - RETV_IF(NULL == value, IOTCON_ERR_PARAM); - RETVM_IF(IOTCON_TYPE_BOOL != real->type, IOTCON_ERR_PARAM, "IOTCON_ERR_PARAM(%d)", - real->type); + RETV_IF(NULL == val, NULL); - real->val.b = bval; + value = (ic_basic_s*)_ic_value_new(IOTCON_TYPE_STR); + if (NULL == value) { + ERR("_ic_value_new(STR:%s) Fail", val); + return NULL; + } + + value->val.s = val; - return IOTCON_ERR_NONE; + return (iotcon_value_h)value; } -API double iotcon_value_get_double(iotcon_value_h value) + +iotcon_value_h ic_value_new_list(iotcon_list_h val) { - ic_basic_s *real = (ic_basic_s*)value; + ic_val_list_s *value; - RETV_IF(NULL == value, IOTCON_ERR_PARAM); - RETVM_IF(IOTCON_TYPE_DOUBLE != real->type, 0, "IOTCON_ERR_PARAM(%d)", real->type); + value = (ic_val_list_s*)_ic_value_new(IOTCON_TYPE_LIST); + if (NULL == value) { + ERR("_ic_value_new(LIST) Fail"); + return NULL; + } - return real->val.d; + value->list = val; + + return (iotcon_value_h)value; } -int ic_value_set_double(iotcon_value_h value, double dbval) +iotcon_value_h ic_value_new_repr(iotcon_repr_h val) { - ic_basic_s *real = (ic_basic_s*)value; + ic_val_repr_s *value; - RETV_IF(NULL == value, IOTCON_ERR_PARAM); - RETVM_IF(IOTCON_TYPE_DOUBLE != real->type, IOTCON_ERR_PARAM, "IOTCON_ERR_PARAM(%d)", - real->type); + value = (ic_val_repr_s*)_ic_value_new(IOTCON_TYPE_REPR); + if (NULL == value) { + ERR("_ic_value_new(REPR) Fail"); + return NULL; + } - real->val.d = dbval; + value->repr = val; - return IOTCON_ERR_NONE; + return (iotcon_value_h)value; } -API char* iotcon_value_get_str(iotcon_value_h value) +int ic_value_get_int(iotcon_value_h value) { ic_basic_s *real = (ic_basic_s*)value; - RETV_IF(NULL == value, NULL); - RETVM_IF(IOTCON_TYPE_STR != real->type, NULL, "IOTCON_ERR_PARAM(%d)", real->type); + RETV_IF(NULL == value, 0); + RETVM_IF(IOTCON_TYPE_INT != real->type, 0, "IOTCON_ERROR_PARAM(%d)", real->type); - return real->val.s; + return real->val.i; } -int ic_value_set_str(iotcon_value_h value, const char *strval) +bool ic_value_get_bool(iotcon_value_h value) { ic_basic_s *real = (ic_basic_s*)value; - RETV_IF(NULL == value, IOTCON_ERR_PARAM); - RETVM_IF(IOTCON_TYPE_STR != real->type, IOTCON_ERR_PARAM, "IOTCON_ERR_PARAM(%d)", - real->type); - - real->val.s = ic_utils_strdup(strval); + RETV_IF(NULL == value, false); + RETVM_IF(IOTCON_TYPE_BOOL != real->type, false, "IOTCON_ERROR_PARAM(%d)", real->type); - return IOTCON_ERR_NONE; + return real->val.b; } -API iotcon_list_h iotcon_value_get_list(iotcon_value_h value) +double ic_value_get_double(iotcon_value_h value) { - ic_val_list_s *real = (ic_val_list_s*)value; + ic_basic_s *real = (ic_basic_s*)value; - RETV_IF(NULL == value, NULL); - RETVM_IF(IOTCON_TYPE_LIST != real->type, NULL, "IOTCON_ERR_PARAM(%d)", real->type); + RETV_IF(NULL == value, 0.0); + RETVM_IF(IOTCON_TYPE_DOUBLE != real->type, 0.0, "IOTCON_ERROR_PARAM(%d)", real->type); - return real->list; + return real->val.d; } -int ic_value_set_list(iotcon_value_h value, iotcon_list_h list) +const char* ic_value_get_str(iotcon_value_h value) { - ic_val_list_s *real = (ic_val_list_s*)value; - - RETV_IF(NULL == value, IOTCON_ERR_PARAM); - RETVM_IF(IOTCON_TYPE_LIST != real->type, IOTCON_ERR_PARAM, "IOTCON_ERR_PARAM(%d)", - real->type); + ic_basic_s *real = (ic_basic_s*)value; - real->list = list; + RETV_IF(NULL == value, NULL); + RETVM_IF(IOTCON_TYPE_STR != real->type, NULL, "IOTCON_ERROR_PARAM(%d)", real->type); - return IOTCON_ERR_NONE; + return real->val.s; } -API iotcon_repr_h iotcon_value_get_repr(iotcon_value_h value) + +iotcon_list_h ic_value_get_list(iotcon_value_h value) { - ic_val_repr_s *real = (ic_val_repr_s*)value; + ic_val_list_s *real = (ic_val_list_s*)value; RETV_IF(NULL == value, NULL); - RETVM_IF(IOTCON_TYPE_REPR != real->type, NULL, "IOTCON_ERR_PARAM(%d)", real->type); + RETVM_IF(IOTCON_TYPE_LIST != real->type, NULL, "IOTCON_ERROR_PARAM(%d)", real->type); - return real->repr; + return real->list; } -int ic_value_set_repr(iotcon_value_h value, iotcon_repr_h repr) + +iotcon_repr_h ic_value_get_repr(iotcon_value_h value) { ic_val_repr_s *real = (ic_val_repr_s*)value; - RETV_IF(NULL == value, IOTCON_ERR_PARAM); - RETVM_IF(IOTCON_TYPE_REPR != real->type, IOTCON_ERR_PARAM, "IOTCON_ERR_PARAM(%d)", - real->type); - - real->repr = repr; + RETV_IF(NULL == value, NULL); + RETVM_IF(IOTCON_TYPE_REPR != real->type, NULL, "IOTCON_ERROR_PARAM(%d)", real->type); - return IOTCON_ERR_NONE; + return real->repr; } -JsonNode* ic_repr_generate_json_value(iotcon_value_h value) +/* +* 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; + RETV_IF(NULL == value, NULL); - JsonNode *node = json_node_new(JSON_NODE_VALUE); - int type = iotcon_value_get_type(value); - - if (IOTCON_TYPE_INT == type) - json_node_set_int(node, iotcon_value_get_int(value)); - else if (IOTCON_TYPE_BOOL == type) - json_node_set_boolean(node, iotcon_value_get_bool(value)); - else if (IOTCON_TYPE_DOUBLE == type) - json_node_set_double(node, iotcon_value_get_double(value)); - else if (IOTCON_TYPE_STR == type) - json_node_set_string(node, iotcon_value_get_str(value)); - else if (IOTCON_TYPE_NULL == type) + node = json_node_new(JSON_NODE_VALUE); + if (NULL == node) { + ERR("json_node_new(VALUE) Fail"); + return NULL; + } + + switch (value->type) { + case IOTCON_TYPE_INT: + json_node_set_int(node, real->val.i); + break; + case IOTCON_TYPE_BOOL: + json_node_set_boolean(node, real->val.b); + break; + case IOTCON_TYPE_DOUBLE: + json_node_set_double(node, real->val.d); + break; + case IOTCON_TYPE_STR: + json_node_set_string(node, real->val.s); + break; + case IOTCON_TYPE_NULL: node = json_node_init_null(node); - else - ERR("Invalid type(%d)", type); + break; + default: + ERR("Invalid type(%d)", value->type); + break; + } return node; } -API iotcon_value_h ic_repr_parse_json_value(JsonNode *node) +/* +* A general result : 1 +* : true +* : 5.5 +* : "Hello" +*/ +API iotcon_value_h ic_value_from_json(JsonNode *node) { - iotcon_value_h value = NULL; + gint64 ival64; GType gtype = 0; + iotcon_value_h value = NULL; RETV_IF(NULL == node, NULL); if (JSON_NODE_HOLDS_NULL(node)) { - value = ic_value_new(IOTCON_TYPE_NULL); - DBG("Set null value to node"); + value = ic_value_new_null(); + if (NULL == value) + ERR("ic_value_new_null() Fail"); return value; } gtype = json_node_get_value_type(node); - if (G_TYPE_INT64 == gtype) { - gint64 ival64 = json_node_get_int(node); - if (INT_MAX < ival64 || ival64 < INT_MIN) { - ERR("integer SHOULD NOT exceeds INT_MAX or INT_MIN. ival64(%lld)", ival64); - return NULL; - } - value = ic_value_new(IOTCON_TYPE_INT); - ic_value_set_int(value, ival64); - DBG("Set int value(%d) to node", (int)ival64); - } - else if (G_TYPE_BOOLEAN == gtype) { - bool bval = json_node_get_boolean(node); - value = ic_value_new(IOTCON_TYPE_BOOL); - ic_value_set_bool(value, bval); - DBG("Set bool value(%d) to node", bval); - } - else if (G_TYPE_DOUBLE == gtype) { - double dbval = json_node_get_double(node); - value = ic_value_new(IOTCON_TYPE_DOUBLE); - ic_value_set_double(value, dbval); - DBG("Set double value(%lf) to node", dbval); - } - else if (G_TYPE_STRING == gtype) { - const char *strval = json_node_get_string(node); - value = ic_value_new(IOTCON_TYPE_STR); - ic_value_set_str(value, strval); - DBG("Set str value(%s) to node", strval); - } - else { - ERR("gtype(%d) Fail", gtype); + 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); + + DBG("Set int value(%ll) to node", (int)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; } -void ic_repr_free_value(gpointer data) +void ic_value_free(gpointer data) { FN_CALL; iotcon_value_h value = data; - int type = iotcon_value_get_type(value); + int type = value->type; switch (type) { + case IOTCON_TYPE_STR: + free((char*)ic_value_get_str(value)); case IOTCON_TYPE_INT: case IOTCON_TYPE_BOOL: case IOTCON_TYPE_DOUBLE: - case IOTCON_TYPE_STR: case IOTCON_TYPE_NULL: DBG("value is basic. type(%d)", type); - ic_repr_free_basic_value(value); break; case IOTCON_TYPE_LIST: DBG("value is list"); - iotcon_list_h iotlist_child = iotcon_value_get_list(value); - iotcon_repr_free_list(iotlist_child); + iotcon_list_free(ic_value_get_list(value)); break; case IOTCON_TYPE_REPR: - DBG("value is object"); - iotcon_repr_h repr_child = iotcon_value_get_repr(value); - iotcon_repr_free(repr_child); + DBG("value is Repr"); + iotcon_repr_free(ic_value_get_repr(value)); break; default: ERR("Invalid type(%d)", type); break; } + free(value); } -void ic_repr_free_basic_value(iotcon_value_h iotvalue) + +iotcon_value_h ic_value_clone(iotcon_value_h src) { - FN_CALL; - RET_IF(NULL == iotvalue); + iotcon_value_h dest = NULL; + ic_basic_s *real = (ic_basic_s*)src; - int type = iotcon_value_get_type(iotvalue); - DBG("type(%d)", type); + RETV_IF(NULL == src, NULL); - if (IOTCON_TYPE_STR == type) { - char *str = iotcon_value_get_str(iotvalue); - free(str); + switch (src->type) { + case IOTCON_TYPE_INT: + dest = ic_value_new_int(real->val.i); + break; + case IOTCON_TYPE_BOOL: + dest = ic_value_new_bool(real->val.b); + break; + case IOTCON_TYPE_DOUBLE: + dest = ic_value_new_double(real->val.d); + break; + case IOTCON_TYPE_STR: + dest = ic_value_new_str(ic_utils_strdup(real->val.s)); + break; + case IOTCON_TYPE_NULL: + dest = ic_value_new_null(); + break; + default: + ERR("Invalid type(%d)", src->type); + break; } - free(iotvalue); + + if (NULL == dest) + ERR("ic_value_new_xxx(%d) Fail", src->type); + + return dest; } diff --git a/lib/ic-repr-value.h b/lib/ic-repr-value.h index 790da3f..a919366 100644 --- a/lib/ic-repr-value.h +++ b/lib/ic-repr-value.h @@ -20,20 +20,27 @@ #include "iotcon-struct.h" -iotcon_value_h ic_value_new(int type); +iotcon_value_h ic_value_new_null(); +iotcon_value_h ic_value_new_int(int val); +iotcon_value_h ic_value_new_bool(bool val); +iotcon_value_h ic_value_new_double(double val); +iotcon_value_h ic_value_new_str(char *val); +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_set_int(iotcon_value_h value, int ival); -int ic_value_set_bool(iotcon_value_h value, bool bval); -int ic_value_set_double(iotcon_value_h value, double dbval); -int ic_value_set_str(iotcon_value_h value, const char *strval); -int ic_value_set_list(iotcon_value_h value, iotcon_list_h list); -int ic_value_set_repr(iotcon_value_h value, iotcon_repr_h rep); -int ic_value_set_null(iotcon_value_h value); -JsonNode* ic_repr_generate_json_value(iotcon_value_h value); -iotcon_value_h ic_repr_parse_json_value(JsonNode *node); +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); -void ic_repr_free_value(gpointer data); -void ic_repr_free_basic_value(iotcon_value_h iotvalue); -#endif // __IOT_CONNECTIVITY_MANAGER_INTERNAL_REPRESENTATION_VALUE_H__ +JsonNode* ic_value_to_json(iotcon_value_h value); +iotcon_value_h ic_value_from_json(JsonNode *node); +void ic_value_free(gpointer data); + +iotcon_value_h ic_value_clone(iotcon_value_h src); + +#endif /* __IOT_CONNECTIVITY_MANAGER_INTERNAL_REPRESENTATION_VALUE_H__ */ diff --git a/lib/ic-repr.c b/lib/ic-repr.c index 1ebd353..eece65d 100644 --- a/lib/ic-repr.c +++ b/lib/ic-repr.c @@ -27,6 +27,7 @@ #include "ic-utils.h" #include "ic-repr-list.h" #include "ic-repr-value.h" +#include "ic-repr-obj.h" #include "ic-repr.h" API iotcon_repr_h iotcon_repr_new() @@ -41,7 +42,7 @@ API iotcon_repr_h iotcon_repr_new() } ret_val->hash_table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, - ic_repr_free_value); + ic_value_free); return ret_val; } @@ -55,16 +56,25 @@ API const char* iotcon_repr_get_uri(iotcon_repr_h repr) API int iotcon_repr_set_uri(iotcon_repr_h repr, const char *uri) { - RETV_IF(NULL == repr, IOTCON_ERR_PARAM); - RETV_IF(NULL == uri, IOTCON_ERR_PARAM); + 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_ERR_MEMORY; + return IOTCON_ERROR_MEMORY; } - return IOTCON_ERR_NONE; + return IOTCON_ERROR_NONE; +} + +API int iotcon_repr_delete_uri(iotcon_repr_h repr) +{ + RETV_IF(NULL == repr, IOTCON_ERROR_PARAM); + + free(repr->uri); + + return IOTCON_ERROR_NONE; } API void iotcon_repr_get_resource_types(iotcon_repr_h repr, iotcon_resourcetype_fn fn, @@ -73,12 +83,12 @@ API void iotcon_repr_get_resource_types(iotcon_repr_h repr, iotcon_resourcetype_ RET_IF(NULL == repr); /* (GFunc) : fn needs to use "const" qualifier */ - return g_list_foreach(repr->res_types, (GFunc)fn, user_data); + 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_ERR_PARAM); + RETV_IF(NULL == repr, IOTCON_ERROR_PARAM); return g_list_length(repr->res_types); } @@ -87,18 +97,36 @@ API int iotcon_repr_append_resource_types(iotcon_repr_h repr, const char *type) { char *res_type = NULL; - RETV_IF(NULL == repr, IOTCON_ERR_PARAM); - RETV_IF(NULL == type, IOTCON_ERR_PARAM); + 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_ERR_MEMORY; + return IOTCON_ERROR_MEMORY; } repr->res_types = g_list_append(repr->res_types, res_type); - return IOTCON_ERR_NONE; + return IOTCON_ERROR_NONE; +} + +API int iotcon_repr_delete_resource_types(iotcon_repr_h repr, const char *type) +{ + 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; + } + + repr->res_types = g_list_delete_link(repr->res_types, cur); + + return IOTCON_ERROR_NONE; } API void iotcon_repr_get_resource_interfaces(iotcon_repr_h repr, iotcon_interface_fn fn, @@ -107,12 +135,12 @@ API void iotcon_repr_get_resource_interfaces(iotcon_repr_h repr, iotcon_interfac RET_IF(NULL == repr); /* (GFunc) : fn needs to use "const" qualifier */ - return g_list_foreach(repr->interfaces, (GFunc)fn, user_data); + g_list_foreach(repr->interfaces, (GFunc)fn, user_data); } API int iotcon_repr_get_resource_interfaces_count(iotcon_repr_h repr) { - RETV_IF(NULL == repr, IOTCON_ERR_PARAM); + RETV_IF(NULL == repr, IOTCON_ERROR_PARAM); return g_list_length(repr->interfaces); } @@ -121,320 +149,60 @@ API int iotcon_repr_append_resource_interfaces(iotcon_repr_h repr, const char *i { char *res_interface = NULL; - RETV_IF(NULL == repr, IOTCON_ERR_PARAM); - RETV_IF(NULL == interface, IOTCON_ERR_PARAM); + RETV_IF(NULL == repr, IOTCON_ERROR_PARAM); + RETV_IF(NULL == interface, IOTCON_ERROR_PARAM); res_interface = ic_utils_strdup(interface); if (NULL == res_interface) { ERR("ic_utils_strdup() Fail"); - return IOTCON_ERR_MEMORY; + return IOTCON_ERROR_MEMORY; } repr->interfaces = g_list_append(repr->interfaces, res_interface); - return IOTCON_ERR_NONE; -} - -API int iotcon_repr_get_int(iotcon_repr_h repr, const char *key) -{ - RETV_IF(NULL == repr, IOTCON_ERR_PARAM); - RETV_IF(NULL == key, IOTCON_ERR_PARAM); - - iotcon_value_h value = g_hash_table_lookup(repr->hash_table, key); - if(NULL == value) { - ERR("g_hash_table_lookup() Fail"); - return IOTCON_ERR_NO_DATA; - } - - ic_basic_s *real = (ic_basic_s*)value; - RETV_IF(IOTCON_TYPE_INT != real->type, IOTCON_ERR_PARAM); - - return real->val.i; -} - -API int iotcon_repr_set_int(iotcon_repr_h repr, const char *key, int ival) -{ - RETV_IF(NULL == repr, IOTCON_ERR_PARAM); - RETV_IF(NULL == key, IOTCON_ERR_PARAM); - - iotcon_value_h value = ic_value_new(IOTCON_TYPE_INT); - if (NULL == value) { - ERR("ic_value_new(IOTCON_TYPE_INT) Fail"); - return IOTCON_ERR_MEMORY; - } - - ic_basic_s *real = (ic_basic_s*)value; - - real->val.i = ival; - - g_hash_table_replace(repr->hash_table, ic_utils_strdup(key), value); - - return IOTCON_ERR_NONE; -} - -API bool iotcon_repr_get_bool(iotcon_repr_h repr, const char *key) -{ - RETV_IF(NULL == repr, false); - RETV_IF(NULL == key, false); - - iotcon_value_h value = g_hash_table_lookup(repr->hash_table, key); - if(NULL == value) { - ERR("g_hash_table_lookup() Fail"); - return false; - } - - ic_basic_s *real = (ic_basic_s*)value; - RETV_IF(IOTCON_TYPE_BOOL != real->type, false); - - return real->val.b; -} - -API int iotcon_repr_set_bool(iotcon_repr_h repr, const char *key, bool bval) -{ - RETV_IF(NULL == repr, IOTCON_ERR_PARAM); - RETV_IF(NULL == key, IOTCON_ERR_PARAM); - - iotcon_value_h value = ic_value_new(IOTCON_TYPE_BOOL); - if (NULL == value) { - ERR("ic_value_new(IOTCON_TYPE_BOOL) Fail"); - return IOTCON_ERR_MEMORY; - } - - ic_basic_s *real = (ic_basic_s*)value; - - real->val.b = bval; - - g_hash_table_replace(repr->hash_table, ic_utils_strdup(key), value); - - return IOTCON_ERR_NONE; -} - -API double iotcon_repr_get_double(iotcon_repr_h repr, const char *key) -{ - RETV_IF(NULL == repr, 0); - RETV_IF(NULL == key, 0); - - iotcon_value_h value = g_hash_table_lookup(repr->hash_table, key); - if(NULL == value) { - ERR("g_hash_table_lookup() Fail"); - return 0; - } - - ic_basic_s *real = (ic_basic_s*)value; - RETV_IF(IOTCON_TYPE_DOUBLE != real->type, 0); - - return real->val.d; -} - -API int iotcon_repr_set_double(iotcon_repr_h repr, const char *key, double dbval) -{ - RETV_IF(NULL == repr, IOTCON_ERR_PARAM); - RETV_IF(NULL == key, IOTCON_ERR_PARAM); - - iotcon_value_h value = ic_value_new(IOTCON_TYPE_DOUBLE); - if (NULL == value) { - ERR("ic_value_new(IOTCON_TYPE_DOUBLE) Fail"); - return IOTCON_ERR_MEMORY; - } - - ic_basic_s *real = (ic_basic_s*)value; - - real->val.d = dbval; - - g_hash_table_replace(repr->hash_table, ic_utils_strdup(key), value); - - return IOTCON_ERR_NONE; -} - -API char* iotcon_repr_get_str(iotcon_repr_h repr, const char *key) -{ - RETV_IF(NULL == repr, NULL); - RETV_IF(NULL == key, NULL); - - iotcon_value_h value = g_hash_table_lookup(repr->hash_table, key); - if(NULL == value) { - ERR("g_hash_table_lookup() Fail"); - return NULL; - } - - ic_basic_s *real = (ic_basic_s*)value; - RETV_IF(IOTCON_TYPE_STR != real->type, NULL); - - return real->val.s; -} - -API int iotcon_repr_set_str(iotcon_repr_h repr, const char *key, const char *strval) -{ - RETV_IF(NULL == repr, IOTCON_ERR_PARAM); - RETV_IF(NULL == key, IOTCON_ERR_PARAM); - - iotcon_value_h value = ic_value_new(IOTCON_TYPE_STR); - if (NULL == value) { - ERR("ic_value_new(IOTCON_TYPE_STR) Fail"); - return IOTCON_ERR_MEMORY; - } - - ic_basic_s *real = (ic_basic_s*)value; - - real->val.s = ic_utils_strdup(strval); - - g_hash_table_replace(repr->hash_table, ic_utils_strdup(key), value); - - return IOTCON_ERR_NONE; -} - -API bool iotcon_repr_is_null(iotcon_repr_h repr, const char *key) -{ - RETV_IF(NULL == repr, false); - RETV_IF(NULL == key, false); - - iotcon_value_h value = (iotcon_value_h) g_hash_table_lookup(repr->hash_table, key); - if (NULL == value) { - ERR("g_hash_table_lookup() Fail"); - return false; - } - - ic_basic_s *real = (ic_basic_s*)value; - - return (IOTCON_TYPE_NULL == real->type) ? true : false; -} - -API int iotcon_repr_set_null(iotcon_repr_h repr, const char *key) -{ - RETV_IF(NULL == repr, IOTCON_ERR_PARAM); - RETV_IF(NULL == key, IOTCON_ERR_PARAM); - - iotcon_value_h value = ic_value_new(IOTCON_TYPE_NULL); - if (NULL == value) { - ERR("ic_value_new(IOTCON_TYPE_NULL) Fail"); - return IOTCON_ERR_MEMORY; - } - - g_hash_table_replace(repr->hash_table, ic_utils_strdup(key), value); - - return IOTCON_ERR_NONE; -} - -API iotcon_list_h iotcon_repr_get_list(iotcon_repr_h repr, const char *key) -{ - RETV_IF(NULL == repr, NULL); - RETV_IF(NULL == key, NULL); - - iotcon_value_h value = g_hash_table_lookup(repr->hash_table, key); - if(NULL == value) { - ERR("g_hash_table_lookup() Fail"); - return NULL; - } - - ic_val_list_s *real = (ic_val_list_s*)value; - RETV_IF(IOTCON_TYPE_LIST != real->type, NULL); - - return real->list; -} - -API int iotcon_repr_replace_list(iotcon_repr_h repr, const char *key, iotcon_list_h list) -{ - RETV_IF(NULL == repr, IOTCON_ERR_PARAM); - RETV_IF(NULL == key, IOTCON_ERR_PARAM); - - iotcon_value_h value = ic_value_new(IOTCON_TYPE_LIST); - if (NULL == value) { - ERR("ic_value_new(IOTCON_TYPE_LIST) Fail"); - return IOTCON_ERR_MEMORY; - } - - ic_val_list_s *real = (ic_val_list_s*)value; - RETV_IF(IOTCON_TYPE_LIST != real->type, IOTCON_ERR_PARAM); - - real->list = list; - - g_hash_table_replace(repr->hash_table, ic_utils_strdup(key), value); - - return IOTCON_ERR_NONE; -} - -API iotcon_repr_h iotcon_repr_get_repr(iotcon_repr_h repr, - const char *key) -{ - RETV_IF(NULL == repr, NULL); - RETV_IF(NULL == key, NULL); - - iotcon_value_h value = g_hash_table_lookup(repr->hash_table, key); - if (NULL == value) { - ERR("g_hash_table_lookup() Fail"); - return NULL; - } - - ic_val_repr_s *real = (ic_val_repr_s*)value; - RETV_IF(IOTCON_TYPE_REPR != real->type, NULL); - - return real->repr; + return IOTCON_ERROR_NONE; } -API int iotcon_repr_replace_repr(iotcon_repr_h dest, const char *key, iotcon_repr_h src) +API int iotcon_repr_delete_resource_interfaces(iotcon_repr_h repr, const char *interface) { - RETV_IF(NULL == dest, IOTCON_ERR_PARAM); - RETV_IF(NULL == key, IOTCON_ERR_PARAM); - RETV_IF(NULL == src, IOTCON_ERR_PARAM); + GList *cur = NULL; + RETV_IF(NULL == repr, IOTCON_ERROR_PARAM); + RETV_IF(NULL == interface, IOTCON_ERROR_PARAM); + RETV_IF(NULL == repr->interfaces, IOTCON_ERROR_PARAM); - iotcon_value_h value = ic_value_new(IOTCON_TYPE_REPR); - if (NULL == value) { - ERR("ic_value_new(IOTCON_TYPE_REPR) Fail"); - return IOTCON_ERR_MEMORY; + cur = g_list_find_custom(repr->interfaces, interface, (GCompareFunc)g_strcmp0); + if (NULL == cur) { + WARN("g_list_find(%s) returns NULL", interface); + return IOTCON_ERROR_NO_DATA; } - ic_val_repr_s *real = (ic_val_repr_s*)value; - - real->repr = src; + repr->interfaces = g_list_delete_link(repr->interfaces, cur); - g_hash_table_replace(dest->hash_table, ic_utils_strdup(key), value); - - return IOTCON_ERR_NONE; -} - -int ic_repr_get_value(iotcon_repr_h repr, const char *key, iotcon_value_h *retval) -{ - RETV_IF(NULL == repr, IOTCON_ERR_PARAM); - RETV_IF(NULL == key, IOTCON_ERR_PARAM); - - *retval = g_hash_table_lookup(repr->hash_table, key); - if (NULL == retval) - return IOTCON_ERR_NO_DATA; - else - return IOTCON_ERR_NONE; -} - -int ic_repr_replace_value(iotcon_repr_h repr, const char *key, iotcon_value_h value) -{ - RETV_IF(NULL == repr, IOTCON_ERR_PARAM); - RETV_IF(NULL == key, IOTCON_ERR_PARAM); - RETV_IF(NULL == value, IOTCON_ERR_PARAM); - - g_hash_table_replace(repr->hash_table, ic_utils_strdup(key), value); - - return IOTCON_ERR_NONE; + return IOTCON_ERROR_NONE; } API int iotcon_repr_append_child(iotcon_repr_h parent, iotcon_repr_h child) { - RETV_IF(NULL == parent, IOTCON_ERR_PARAM); - RETV_IF(NULL == child, IOTCON_ERR_PARAM); + RETV_IF(NULL == parent, IOTCON_ERROR_PARAM); + RETV_IF(NULL == child, IOTCON_ERROR_PARAM); parent->children = g_list_append(parent->children, child); - return IOTCON_ERR_NONE; + return IOTCON_ERROR_NONE; } -API GList* iotcon_repr_get_children(iotcon_repr_h parent) +API void iotcon_repr_get_children(iotcon_repr_h parent, iotcon_children_fn fn, + void *user_data) { - RETV_IF(NULL == parent, NULL); + RET_IF(NULL == parent); - return parent->children; + /* (GFunc) : fn needs to use "const" qualifier */ + g_list_foreach(parent->children, (GFunc)fn, user_data); } API int iotcon_repr_get_children_count(iotcon_repr_h parent) { - RETV_IF(NULL == parent, IOTCON_ERR_PARAM); - RETV_IF(NULL == parent->children, IOTCON_ERR_PARAM); + RETV_IF(NULL == parent, IOTCON_ERROR_PARAM); + RETV_IF(NULL == parent->children, IOTCON_ERROR_PARAM); return g_list_length(parent->children); } @@ -457,106 +225,12 @@ API GList* iotcon_repr_get_key_list(iotcon_repr_h repr) API int iotcon_repr_get_keys_count(iotcon_repr_h repr) { - RETV_IF(NULL == repr, IOTCON_ERR_PARAM); - RETV_IF(NULL == repr->hash_table, IOTCON_ERR_PARAM); + RETV_IF(NULL == repr, IOTCON_ERROR_PARAM); + RETV_IF(NULL == repr->hash_table, IOTCON_ERROR_PARAM); return g_hash_table_size(repr->hash_table); } -API bool iotcon_repr_contains(iotcon_repr_h repr, const char *key) -{ - RETV_IF(NULL == repr, false); - RETV_IF(NULL == key, false); - - return g_hash_table_contains(repr->hash_table, key); -} - -JsonObject* ic_repr_generate_json_repr(iotcon_repr_h repr, int *err_code) -{ - char *key; - unsigned int i = 0; - GList *key_list = NULL; - JsonObject *json_obj = NULL; - int error_code = IOTCON_ERR_NONE; - - JsonNode *node_child = NULL; - JsonArray *array_child = NULL; - iotcon_list_h iotlist_child = NULL; - iotcon_repr_h repr_child = NULL; - - RETV_IF(NULL == repr, NULL); - - json_obj = json_object_new(); - key_list = iotcon_repr_get_key_list(repr); - ic_utils_print_str_list(key_list); - - for (i = 0; i < g_list_length(key_list); i++) { - int type = 0; - iotcon_value_h iotvalue_child = NULL; - - key = g_list_nth_data(key_list, i); - ic_repr_get_value(repr, key, &iotvalue_child); - type = iotcon_value_get_type(iotvalue_child); - switch (type) { - case IOTCON_TYPE_INT: - case IOTCON_TYPE_BOOL: - case IOTCON_TYPE_DOUBLE: - case IOTCON_TYPE_STR: - case IOTCON_TYPE_NULL: - node_child = ic_repr_generate_json_value(iotvalue_child); - if (NULL == node_child) { - ERR("ic_repr_generate_json_value() Fail(NULL == node_child)"); - json_object_unref(json_obj); - g_list_free(key_list); - *err_code = IOTCON_ERR_PARAM; - return NULL; - } - json_object_set_member(json_obj, key, node_child); - break; - case IOTCON_TYPE_LIST: - iotlist_child = iotcon_value_get_list(iotvalue_child); - array_child = ic_repr_generate_json_array(iotlist_child); - if (NULL == array_child) { - ERR("ic_repr_generate_json_array() Fail(NULL == array_child)"); - json_object_unref(json_obj); - g_list_free(key_list); - *err_code = IOTCON_ERR_PARAM; - return NULL; - } - node_child = json_node_new(JSON_NODE_ARRAY); - json_node_set_array(node_child, array_child); - json_object_set_member(json_obj, key, node_child); - break; - case IOTCON_TYPE_REPR: - repr_child = iotcon_value_get_repr(iotvalue_child); - JsonObject *obj = ic_repr_generate_json_repr(repr_child, &error_code); - if (NULL == obj) { - ERR("ic_repr_generate_json_repr() Fail(NULL == obj)"); - json_object_unref(json_obj); - g_list_free(key_list); - *err_code = IOTCON_ERR_PARAM; - return NULL; - } - node_child = json_node_new(JSON_NODE_OBJECT); - json_node_set_object(node_child, obj); - json_object_set_member(json_obj, key, node_child); - break; - default: - ERR("Invalid type(%d)", type); - json_object_unref(json_obj); - g_list_free(key_list); - *err_code = IOTCON_ERR_PARAM; - return NULL; - } - } - g_list_free(key_list); - - JsonObject *ret_obj = json_object_new(); - json_object_set_object_member(ret_obj, IOTCON_KEY_REP, json_obj); - - return ret_obj; -} - void _ic_repr_get_res_type_fn(const char *res_type, void *user_data) { JsonArray *rt_array = user_data; @@ -571,7 +245,11 @@ void _ic_repr_get_res_interface_fn(const char *res_if, void *user_data) DBG("res_if(%s)", res_if); } -JsonObject* _ic_repr_generate_json_foreach(iotcon_repr_h cur_repr, +/* + * A general result : {"href":"/a/parent","rep":{"string":"Hello","intlist":[1,2,3]}, + * "prop":{"rt":["core.light"],"if":["oc.mi.def"]}} + */ +static JsonObject* _ic_repr_data_generate_json(iotcon_repr_h cur_repr, unsigned int child_index) { FN_CALL; @@ -580,18 +258,18 @@ JsonObject* _ic_repr_generate_json_foreach(iotcon_repr_h cur_repr, unsigned int if_count = 0; JsonObject *prop_obj = NULL; + RETV_IF(NULL == cur_repr, NULL); + if (0 < iotcon_repr_get_keys_count(cur_repr)) { - int error_code = IOTCON_ERR_NONE; DBG("Representation has \"%s\" key", IOTCON_KEY_REP); - - repr_obj = ic_repr_generate_json_repr(cur_repr, &error_code); - if (IOTCON_ERR_NONE != error_code) { - ERR("ic_repr_generate_json_repr() Fail(%d)", error_code); + repr_obj = ic_obj_to_json(cur_repr); + if (NULL == repr_obj) { + ERR("ic_obj_to_json() Fail()"); + json_object_unref(repr_obj); return NULL; } } - - if (NULL == repr_obj) // cur_repr didn't have "rep" key and repr_obj was not made. + else repr_obj = json_object_new(); if (cur_repr->uri) { @@ -629,19 +307,48 @@ JsonObject* _ic_repr_generate_json_foreach(iotcon_repr_h cur_repr, json_object_set_array_member(prop_obj, IOTCON_KEY_INTERFACES, if_array); } + FN_END; return repr_obj; } +static JsonObject* _ic_repr_data_generate_parent(iotcon_repr_h cur_repr, + unsigned int child_index) +{ + JsonObject *obj = _ic_repr_data_generate_json(cur_repr, child_index); + if (NULL == obj) { + ERR("_ic_repr_data_generate_json() Fail"); + return NULL; + } + + return obj; +} -static JsonObject* _ic_repr_generate_json_object(iotcon_repr_h repr) +static JsonObject* _ic_repr_data_generate_child(iotcon_repr_h cur_repr, + unsigned int child_index) +{ + JsonObject *obj = _ic_repr_data_generate_json(cur_repr, child_index); + if (NULL == obj) { + ERR("_ic_repr_data_generate_json() Fail"); + return NULL; + } + + return obj; +} + +/* + * A general result : {oc:[{"href":"/a/parent","rep":{"string":"Hello","intlist":[1,2,3]}, + * "prop":{"rt":["core.light"],"if":["oc.mi.def"]}}, + * {"href":"/a/child","rep":{"string":"World","double_val":5.7}, + * "prop":{"rt":["core.light"],"if":["oc.mi.def"]}}]} + */ +static JsonObject* _ic_repr_generate_json(iotcon_repr_h repr) { JsonObject *repr_obj = NULL; JsonObject *root_obj = NULL; JsonArray *root_array = NULL; - GList *children = NULL; unsigned int child_count = 0; unsigned int child_index = 0; - iotcon_repr_h cur_repr = NULL; + iotcon_repr_h child_repr = NULL; RETV_IF(NULL == repr, NULL); @@ -649,22 +356,23 @@ static JsonObject* _ic_repr_generate_json_object(iotcon_repr_h repr) root_array = json_array_new(); if (repr->children) { - children = iotcon_repr_get_children(repr); - child_count = g_list_length(children); + child_count = iotcon_repr_get_children_count(repr); } - cur_repr = repr; - for (child_index = 0; child_index < child_count + 1; child_index++) { - if (1 <= child_index) { // child representation - cur_repr = iotcon_repr_get_nth_child(repr, child_index - 1); - DBG("[Child representation]", child_index); - } - else { - DBG("[Parent representation]"); - } - repr_obj = _ic_repr_generate_json_foreach(cur_repr, child_index); + repr_obj = _ic_repr_data_generate_parent(repr, child_index); + if (NULL == repr_obj) { + ERR("_ic_repr_data_generate_parent() Fali(NULL == repr_obj)"); + json_object_unref(root_obj); + json_array_unref(root_array); + return NULL; + } + 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); + repr_obj = _ic_repr_data_generate_child(child_repr, child_index); if (NULL == repr_obj) { - ERR("_ic_repr_generate_json_foreach() Fali(NULL == repr_obj)"); + ERR("_ic_repr_data_generate_child() Fali(NULL == repr_obj)"); json_object_unref(root_obj); json_array_unref(root_array); return NULL; @@ -682,7 +390,7 @@ char* ic_repr_generate_json(iotcon_repr_h repr, bool set_pretty) JsonNode *root_node = NULL; char *json_data = NULL; - JsonObject *obj = _ic_repr_generate_json_object(repr); + JsonObject *obj = _ic_repr_generate_json(repr); if (NULL == obj) { ERR("ic_repr_generate_json() Fail"); return NULL; @@ -700,79 +408,16 @@ char* ic_repr_generate_json(iotcon_repr_h repr, bool set_pretty) return json_data; } -iotcon_repr_h ic_repr_parse_json_obj(JsonObject *obj, int *err_code) -{ - iotcon_repr_h repr = iotcon_repr_new(); - GList *key_list = json_object_get_members(obj); - ic_utils_print_str_list(key_list); - char *key; - unsigned int i = 0; - - for (i = 0; i < g_list_length(key_list); i++) { - key = g_list_nth_data(key_list, i); - - // search child object recursively - JsonNode *child_node = json_object_get_member(obj, key); - if (NULL == child_node) { - ERR("json_object_get_member() Fail(NULL == child_node)"); - iotcon_repr_free(repr); - *err_code = IOTCON_ERR_PARAM; - return NULL; - } - if (JSON_NODE_HOLDS_NULL(child_node) || JSON_NODE_HOLDS_VALUE(child_node)) { - iotcon_value_h value = ic_repr_parse_json_value(child_node); - if (NULL == value) { - ERR("ic_repr_parse_json_value() Fail(NULL == value)"); - iotcon_repr_free(repr); - *err_code = IOTCON_ERR_PARAM; - return NULL; - } - ic_repr_replace_value(repr, key, value); - } - else if (JSON_NODE_HOLDS_ARRAY(child_node)) { - JsonArray *child_array = json_node_get_array(child_node); - iotcon_list_h list = ic_repr_parse_json_array(child_array); - if (NULL == list) { - ERR("ic_repr_parse_json_array() Fail(NULL == list)"); - iotcon_repr_free(repr); - *err_code = IOTCON_ERR_PARAM; - return NULL; - } - iotcon_repr_replace_list(repr, key, list); - } - else if (JSON_NODE_HOLDS_OBJECT(child_node)) { - int error_code = IOTCON_ERR_NONE; - JsonObject *child_obj = json_node_get_object(child_node); - JsonObject *repr_obj = json_object_get_object_member(child_obj, - IOTCON_KEY_REP); - iotcon_repr_h ret_repr = ic_repr_parse_json_obj(repr_obj, &error_code); - if (NULL == ret_repr) { - ERR("ic_repr_parse_json_obj() Fail(NULL == ret_repr)"); - iotcon_repr_free(repr); - *err_code = IOTCON_ERR_PARAM; - return NULL; - } - iotcon_repr_replace_repr(repr, key, ret_repr); - } - else { - ERR("node type(%d) Fail", json_node_get_node_type(child_node)); - iotcon_repr_free(repr); - *err_code = IOTCON_ERR_PARAM; - return NULL; - } - } - g_list_free(key_list); - - return repr; -} - +/* + * A general input : {"href":"/a/parent","rep":{"string":"Hello","intlist":[1,2,3]}, + * "prop":{"rt":["core.light"],"if":["oc.mi.def"]}} + */ iotcon_repr_h ic_repr_parse_json(const char *json_string) { const char *uri_value = NULL; RETV_IF(NULL == json_string, NULL); - // incoming json format example : {"href":"/a/address","rep":{"text":"Hello world"}} DBG("input str : %s", json_string); GError *error = NULL; @@ -790,20 +435,17 @@ iotcon_repr_h ic_repr_parse_json(const char *json_string) iotcon_repr_h repr = NULL; if (json_object_has_member(root_obj, IOTCON_KEY_REP)) { - int err_code = IOTCON_ERR_NONE; DBG("Representation has \"%s\" key", IOTCON_KEY_REP); - JsonObject *repr_obj = json_object_get_object_member(root_obj, IOTCON_KEY_REP); - repr = ic_repr_parse_json_obj(repr_obj, &err_code); - if (IOTCON_ERR_NONE != err_code) { - ERR("ic_repr_parse_json_obj() Fail(%d)", err_code); + + repr = ic_obj_from_json(root_obj); + if (NULL == repr) { + ERR("ic_obj_from_json() Fail()"); g_object_unref(parser); return NULL; } } - - // root_obj didn't have "repr" key and iotcon_repr_h was not made. - if (NULL == repr) + else repr = iotcon_repr_new(); if (json_object_has_member(root_obj, IOTCON_KEY_URI)) { @@ -876,6 +518,86 @@ API void iotcon_repr_free(iotcon_repr_h repr) FN_END; } +static void _ic_repr_obj_clone(char *key, iotcon_value_h value, + iotcon_repr_h dest_repr) +{ + FN_CALL; + int type = IOTCON_TYPE_NONE; + iotcon_value_h copied_value = NULL; + iotcon_list_h child_list = NULL; + iotcon_list_h copied_list = NULL; + iotcon_repr_h child_repr = NULL; + iotcon_repr_h copied_repr = NULL; + + type = value->type; + switch (type) { + case IOTCON_TYPE_INT: + case IOTCON_TYPE_BOOL: + case IOTCON_TYPE_DOUBLE: + case IOTCON_TYPE_STR: + case IOTCON_TYPE_NULL: + copied_value = ic_value_clone(value); + if (NULL == copied_value) { + ERR("ic_value_clone() Fail"); + return; + } + ic_obj_set_value(dest_repr, ic_utils_strdup(key), copied_value); + break; + case IOTCON_TYPE_LIST: + child_list = ic_value_get_list(value); + copied_list = ic_list_clone(child_list); + if (NULL == copied_list) { + ERR("ic_list_clone() Fail"); + return; + } + iotcon_repr_set_list(dest_repr, ic_utils_strdup(key), copied_list); + break; + case IOTCON_TYPE_REPR: + child_repr = ic_value_get_repr(value); + copied_repr = iotcon_repr_clone(child_repr); + if (NULL == copied_repr) { + ERR("ic_list_clone() Fail"); + return; + } + iotcon_repr_set_repr(dest_repr, ic_utils_strdup(key), copied_repr); + break; + default: + ERR("Invalid type(%d)", type); + return; + } +} + +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; + + return iotcon_repr_clone((iotcon_repr_h)src); +} + +API iotcon_repr_h iotcon_repr_clone(const iotcon_repr_h src) +{ + FN_CALL; + iotcon_repr_h dest = NULL; + + RETV_IF(NULL == src, NULL); + + 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->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); + + return dest; +} + API char* iotcon_repr_generate_json(iotcon_repr_h repr) { return ic_repr_generate_json(repr, TRUE); diff --git a/lib/ic-repr.h b/lib/ic-repr.h index 41e0684..2a39462 100644 --- a/lib/ic-repr.h +++ b/lib/ic-repr.h @@ -27,11 +27,6 @@ #define IOTCON_KEY_PROPERTY "prop" #define IOTCON_KEY_REP "rep" -int ic_repr_get_value(iotcon_repr_h repr, const char *key, iotcon_value_h *retval); -int ic_repr_replace_value(iotcon_repr_h repr, const char *key, iotcon_value_h value); - -JsonObject* ic_repr_generate_json_repr(iotcon_repr_h repr, int *err_code); - /** * @ingroup CAPI_IOT_CONNECTIVITY_MODULE * @brief Generates JSON string from Representation. @@ -45,7 +40,6 @@ JsonObject* ic_repr_generate_json_repr(iotcon_repr_h repr, int *err_code); */ char* ic_repr_generate_json(iotcon_repr_h repr, bool set_pretty); -iotcon_repr_h ic_repr_parse_json_obj(JsonObject *obj, int *err_code); iotcon_repr_h ic_repr_parse_json(const char *json_string); -#endif // __IOT_CONNECTIVITY_MANAGER_INTERNAL_REPRESENTATION_H__ +#endif /* __IOT_CONNECTIVITY_MANAGER_INTERNAL_REPRESENTATION_H__ */ diff --git a/lib/ic-struct.h b/lib/ic-struct.h index c074f64..e676d93 100644 --- a/lib/ic-struct.h +++ b/lib/ic-struct.h @@ -72,4 +72,4 @@ struct ic_res_response_s { iotcon_repr_h repr; }; -#endif //__IOT_CONNECTIVITY_MANAGER_INTERNAL_STRUCT_H__ +#endif /* __IOT_CONNECTIVITY_MANAGER_INTERNAL_STRUCT_H__ */ diff --git a/lib/ic-utils.h b/lib/ic-utils.h index a3884d1..2941190 100644 --- a/lib/ic-utils.h +++ b/lib/ic-utils.h @@ -21,4 +21,4 @@ char* ic_utils_strdup(const char *src); void ic_utils_print_str_list(GList *list); -#endif //__IOT_CONNECTIVITY_MANAGER_INTERNAL_UTILITY_H__ +#endif /* __IOT_CONNECTIVITY_MANAGER_INTERNAL_UTILITY_H__ */ diff --git a/lib/ic.c b/lib/ic.c index 4c69ace..6a942d6 100644 --- a/lib/ic.c +++ b/lib/ic.c @@ -130,7 +130,7 @@ API int iotcon_response_set(iotcon_response_h resp, iotcon_response_property_e p value = va_arg(args, int); if (value < IOTCON_EH_OK || IOTCON_EH_MAX <= value) { ERR("Invalid value"); - return IOTCON_ERR_PARAM; + return IOTCON_ERROR_PARAM; } resp->result = value; break; @@ -165,7 +165,7 @@ API int iotcon_response_set(iotcon_response_h resp, iotcon_response_property_e p va_end(args); - return IOTCON_ERR_NONE; + return IOTCON_ERROR_NONE; } API void iotcon_response_free(iotcon_response_h resp) @@ -266,7 +266,7 @@ API int iotcon_unregister_resource(const iotcon_resource_h resource_handle) int ret; ret = ic_ioty_unregister_res(resource_handle); - if (IOTCON_ERR_NONE != ret) + if (IOTCON_ERROR_NONE != ret) ERR("ic_ioty_bind_type_to_res() Fail(%d)", ret); g_hash_table_remove(ic_ctx.entity_cb_hash, resource_handle); @@ -282,7 +282,7 @@ API int iot_bind_interface_to_resource(iotcon_resource_h resource_handle, int ret; ret = ic_ioty_bind_iface_to_res(resource_handle, interface_type); - if (IOTCON_ERR_NONE != ret) + if (IOTCON_ERROR_NONE != ret) ERR("ic_ioty_bind_type_to_res() Fail(%d)", ret); return ret; @@ -296,7 +296,7 @@ API int iotcon_bind_type_to_resource(iotcon_resource_h resource_handle, int ret; ret = ic_ioty_bind_type_to_res(resource_handle, resource_type); - if (IOTCON_ERR_NONE != ret) + if (IOTCON_ERROR_NONE != ret) ERR("ic_ioty_bind_type_to_res() Fail(%d)", ret); return ret; @@ -309,7 +309,7 @@ API int iotcon_bind_resource(iotcon_resource_h parent, iotcon_resource_h child) int ret; ret = ic_ioty_bind_res(parent, child); - if (IOTCON_ERR_NONE != ret) + if (IOTCON_ERROR_NONE != ret) ERR("ic_ioty_bind_res() Fail(%d)", ret); return ret; @@ -323,7 +323,7 @@ API int iotcon_register_device_info(iotcon_device_info_s *device_info) int ret; ret = ic_ioty_register_device_info(device_info); - if (IOTCON_ERR_NONE != ret) + if (IOTCON_ERROR_NONE != ret) ERR("ic_ioty_register_device_info() Fail(%d)", ret); return ret; @@ -334,15 +334,15 @@ API int iotcon_subscribe_device_info(char *host, char *uri, { FN_CALL; - int ret = IOTCON_ERR_NONE; + int ret = IOTCON_ERROR_NONE; - RETV_IF(NULL == uri, IOTCON_ERR_PARAM); - RETV_IF(NULL == found_cb, IOTCON_ERR_PARAM); + RETV_IF(NULL == uri, IOTCON_ERROR_PARAM); + RETV_IF(NULL == found_cb, IOTCON_ERROR_PARAM); /* If we add device_cb to manage app's CB if we don't have any app's cb */ if (0 == g_list_length(ic_ctx.found_device_cb_lst)) { ret = ic_ioty_get_device_info(host, uri); - if (IOTCON_ERR_NONE != ret) { + if (IOTCON_ERROR_NONE != ret) { ERR("ic_ioty_get_device_info() Fail(%d)", ret); return ret; } @@ -350,7 +350,7 @@ API int iotcon_subscribe_device_info(char *host, char *uri, ic_ctx.found_device_cb_lst = g_list_append(ic_ctx.found_device_cb_lst, found_cb); - return IOTCON_ERR_NONE; + return IOTCON_ERROR_NONE; } API void iotcon_unsubscribe_device_info(char *host, char *uri, @@ -358,6 +358,10 @@ API void iotcon_unsubscribe_device_info(char *host, char *uri, { FN_CALL; + RET_IF(NULL == host); + RET_IF(NULL == uri); + RET_IF(NULL == found_cb); + GList *node = g_list_first(ic_ctx.found_device_cb_lst); while (node) { GList *next_node = node->next; @@ -377,8 +381,11 @@ API int iotcon_send_notify_response(iotcon_response_h resp, iotcon_observers obs FN_CALL; int ret; + RETV_IF(NULL == resp, IOTCON_ERROR_PARAM); + RETV_IF(NULL == observers, IOTCON_ERROR_PARAM); + ret = ic_ioty_send_notify(resp, observers); - if (IOTCON_ERR_NONE != ret) + if (IOTCON_ERROR_NONE != ret) ERR("ic_ioty_send_notify() Fail(%d)", ret); return ret; @@ -389,8 +396,10 @@ API int iotcon_send_resource_response(iotcon_response_h resp) FN_CALL; int ret; + RETV_IF(NULL == resp, IOTCON_ERROR_PARAM); + ret = ic_ioty_send_res_response_data(resp); - if (IOTCON_ERR_NONE != ret) + if (IOTCON_ERROR_NONE != ret) ERR("ic_ioty_send_res_response_data() Fail(%d)", ret); return ret; @@ -415,7 +424,7 @@ API int iotcon_unsubscribe_presence(iotcon_presence_h presence_handle) int ret; ret = ic_ioty_unsubscribe_presence(presence_handle); - if (IOTCON_ERR_NONE != ret) + if (IOTCON_ERROR_NONE != ret) ERR("ic_ioty_unsubscribe_presence() Fail(%d)", ret); return ret; @@ -428,7 +437,7 @@ API int iotcon_start_presence(const unsigned int time_to_live) int ret; ret = ic_ioty_start_presence(time_to_live); - if (IOTCON_ERR_NONE != ret) + if (IOTCON_ERROR_NONE != ret) ERR("ic_ioty_start_presence() Fail(%d)", ret); return ret; @@ -441,7 +450,7 @@ API int iotcon_stop_presence() int ret; ret = ic_ioty_stop_presence(); - if (IOTCON_ERR_NONE != ret) + if (IOTCON_ERROR_NONE != ret) ERR("ic_ioty_stop_presence() Fail(%d)", ret); return ret; @@ -452,14 +461,14 @@ API int iotcon_find_resource(const char *host, const char *resource_name, { FN_CALL; - int ret = IOTCON_ERR_NONE; + int ret = IOTCON_ERROR_NONE; - RETV_IF(NULL == host, IOTCON_ERR_PARAM); - RETV_IF(NULL == resource_name, IOTCON_ERR_PARAM); - RETV_IF(NULL == found_resource_cb, IOTCON_ERR_PARAM); + RETV_IF(NULL == host, IOTCON_ERROR_PARAM); + RETV_IF(NULL == resource_name, IOTCON_ERROR_PARAM); + RETV_IF(NULL == found_resource_cb, IOTCON_ERROR_PARAM); ret = ic_ioty_find_resource(host, resource_name, found_resource_cb, user_data); - if (IOTCON_ERR_NONE != ret) + if (IOTCON_ERROR_NONE != ret) ERR("ic_ioty_find_resource() Fail(%d)", ret); return ret; @@ -539,10 +548,12 @@ API int iotcon_get(iotcon_resource_s resource, iotcon_query query, { FN_CALL; - int ret = IOTCON_ERR_NONE; + RETV_IF(NULL == on_get_cb, IOTCON_ERROR_PARAM); + + int ret = IOTCON_ERROR_NONE; ret = ic_ioty_get(resource, query, on_get_cb, user_data); - if (IOTCON_ERR_NONE != ret) + if (IOTCON_ERROR_NONE != ret) ERR("ic_ioty_get() Fail(%d)", ret); return ret; @@ -553,31 +564,29 @@ API int iotcon_put(iotcon_resource_s resource, iotcon_repr_h repr, iotcon_query { FN_CALL; - int ret = IOTCON_ERR_NONE; + int ret = IOTCON_ERROR_NONE; - RETV_IF(NULL == repr, IOTCON_ERR_PARAM); + RETV_IF(NULL == repr, IOTCON_ERROR_PARAM); + RETV_IF(NULL == on_put_cb, IOTCON_ERROR_PARAM); ret = ic_ioty_put(resource, repr, query, on_put_cb, user_data); - if (IOTCON_ERR_NONE != ret) + if (IOTCON_ERROR_NONE != ret) ERR("ic_ioty_put() Fail(%d)", ret); return ret; } -API int iotcon_post(iotcon_resource_s resource, - iotcon_repr_h repr, - iotcon_query query, - iotcon_on_post_cb on_post_cb, - void *user_data) +API int iotcon_post(iotcon_resource_s resource, iotcon_repr_h repr, iotcon_query query, + iotcon_on_post_cb on_post_cb, void *user_data) { FN_CALL; - int ret = IOTCON_ERR_NONE; + int ret = IOTCON_ERROR_NONE; - RETV_IF(NULL == repr, IOTCON_ERR_PARAM); + RETV_IF(NULL == repr, IOTCON_ERROR_PARAM); ret = ic_ioty_post(resource, repr, query, on_post_cb, user_data); - if (IOTCON_ERR_NONE != ret) + if (IOTCON_ERROR_NONE != ret) ERR("ic_ioty_post() Fail(%d)", ret); return ret; @@ -588,10 +597,10 @@ API int iotcon_delete_resource(iotcon_resource_s resource, { FN_CALL; - int ret = IOTCON_ERR_NONE; + int ret = IOTCON_ERROR_NONE; ret = ic_ioty_delete_res(resource, on_delete_cb, user_data); - if (IOTCON_ERR_NONE != ret) + if (IOTCON_ERROR_NONE != ret) ERR("ic_ioty_delete_res() Fail(%d)", ret); return ret; @@ -605,11 +614,12 @@ API int iotcon_observe(iotcon_observe_type_e observe_type, { FN_CALL; - int ret = IOTCON_ERR_NONE; + int ret = IOTCON_ERROR_NONE; + + RETV_IF(NULL == on_observe_cb, IOTCON_ERROR_PARAM); - ret = ic_ioty_observe(resource, observe_type, query, on_observe_cb, - user_data); - if (IOTCON_ERR_NONE != ret) + ret = ic_ioty_observe(resource, observe_type, query, on_observe_cb, user_data); + if (IOTCON_ERROR_NONE != ret) ERR("ic_ioty_observe() Fail(%d)", ret); return ret; @@ -619,10 +629,10 @@ API int iotcon_cancel_observe(iotcon_resource_s resource) { FN_CALL; - int ret = IOTCON_ERR_NONE; + int ret = IOTCON_ERROR_NONE; ret = ic_ioty_cancel_observe(resource); - if (IOTCON_ERR_NONE != ret) + if (IOTCON_ERROR_NONE != ret) ERR("ic_ioty_cancel_observe() Fail(%d)", ret); return ret; diff --git a/lib/include/iotcon-constant.h b/lib/include/iotcon-constant.h index db47a23..7bde6e7 100644 --- a/lib/include/iotcon-constant.h +++ b/lib/include/iotcon-constant.h @@ -94,4 +94,4 @@ typedef enum { IOTCON_TYPE_REPR, } iotcon_repr_types_e; -#endif //__IOT_CONNECTIVITY_MANAGER_CONSTANT_H__ +#endif /* __IOT_CONNECTIVITY_MANAGER_CONSTANT_H__ */ diff --git a/lib/include/iotcon-errors.h b/lib/include/iotcon-errors.h index aefa06a..d6c5a2b 100644 --- a/lib/include/iotcon-errors.h +++ b/lib/include/iotcon-errors.h @@ -16,21 +16,27 @@ #ifndef __IOT_CONNECTIVITY_MANAGER_ERRORS_H__ #define __IOT_CONNECTIVITY_MANAGER_ERRORS_H__ +/** + * @ingroup CAPI_IOT_CONNECTIVITY_MODULE + * @brief Enumerations of Iotcon error codes. + * @since_tizen 3.0 + */ typedef enum { - IOTCON_ERR_NONE = 0, - IOTCON_ERR_MEMORY = -1, - IOTCON_ERR_GDBUS = -2, - IOTCON_ERR_ENVIRONMENT = -3, - IOTCON_ERR_SYSTEM = -4, - IOTCON_ERR_PLUGIN = -5, - IOTCON_ERR_PARAM = -6, - IOTCON_ERR_ALREADY = -7, - IOTCON_ERR_BUSY = -8, - IOTCON_ERR_NOT_SUPPORT = -9, - IOTCON_ERR_DISABLE = -10, - IOTCON_ERR_IOTIVITY = -11, - IOTCON_ERR_NO_DATA = -12 + IOTCON_ERROR_NONE = 0, /**< Successful*/ + IOTCON_ERROR_MEMORY = -1, /**< Out of memory */ + IOTCON_ERROR_GDBUS = -2, + IOTCON_ERROR_ENVIRONMENT = -3, + IOTCON_ERROR_SYSTEM = -4, + IOTCON_ERROR_PLUGIN = -5, + IOTCON_ERROR_PARAM = -6, /**< Invalid parameter */ + IOTCON_ERROR_ALREADY = -7, + IOTCON_ERROR_BUSY = -8, /**< Device or resource busy */ + IOTCON_ERROR_NOT_SUPPORT = -9, + IOTCON_ERROR_DISABLE = -10, + IOTCON_ERROR_IOTIVITY = -11, /**< Iotivity errors */ + IOTCON_ERROR_NO_DATA = -12, /**< No data available */ + IOTCON_ERROR_FAIL = -13, /**< Fail */ }iotcon_error_e; -#endif //__IOT_CONNECTIVITY_MANAGER_ERRORS_H__ +#endif /* __IOT_CONNECTIVITY_MANAGER_ERRORS_H__ */ diff --git a/lib/include/iotcon-representation.h b/lib/include/iotcon-representation.h new file mode 100644 index 0000000..8067c6c --- /dev/null +++ b/lib/include/iotcon-representation.h @@ -0,0 +1,145 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef __IOT_CONNECTIVITY_MANAGER_REPRESENTATION_H__ +#define __IOT_CONNECTIVITY_MANAGER_REPRESENTATION_H__ + +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_delete_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 to append. + * + * @param[in] repr The handle to the Representation + * @param[in] type The resource type + * + * @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_delete_resource_types(iotcon_repr_h repr, const char *type); + +int iotcon_repr_append_resource_interfaces(iotcon_repr_h repr, const char *interface); +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_delete_resource_interfaces(iotcon_repr_h repr, const char *type); + +/** + * @ingroup CAPI_IOT_CONNECTIVITY_MODULE + * @brief Sets int value. + * @since_tizen 3.0 + * @remarks Stored value is replaced with @a ival. + * + * @param[in] repr The handle to the Representation + * @param[in] type The resource type + * + * @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_set_int(iotcon_repr_h repr, const char *key, int val); +int iotcon_repr_set_bool(iotcon_repr_h repr, const char *key, bool val); +int iotcon_repr_set_double(iotcon_repr_h repr, const char *key, double val); +int iotcon_repr_set_str(iotcon_repr_h repr, const char *key, char *val); +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); +bool iotcon_repr_is_null(iotcon_repr_h repr, const char *key); + +int iotcon_repr_del_int(iotcon_repr_h repr, const char *key); +int iotcon_repr_del_bool(iotcon_repr_h repr, const char *key); +int iotcon_repr_del_double(iotcon_repr_h repr, const char *key); +int iotcon_repr_del_str(iotcon_repr_h repr, const char *key); +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_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, + 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); + +GList* 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); +void iotcon_list_free(iotcon_list_h list); + +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_del_nth_int(iotcon_list_h list, int pos); +int iotcon_list_del_nth_bool(iotcon_list_h list, int pos); +int iotcon_list_del_nth_double(iotcon_list_h list, int pos); +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_int_list_fn)(int index, const int value, void *user_data); +void iotcon_int_list_foreach(iotcon_list_h list, iotcon_int_list_fn fn, void *user_data); +typedef void (*iotcon_bool_list_fn)(int index, const bool value, void *user_data); +void iotcon_bool_list_foreach(iotcon_list_h list, iotcon_bool_list_fn fn, void *user_data); +typedef void (*iotcon_double_list_fn)(int index, const double value, void *user_data); +void iotcon_double_list_foreach(iotcon_list_h list, iotcon_double_list_fn fn, void *user_data); +typedef void (*iotcon_str_list_fn)(int index, const char *value, void *user_data); +void iotcon_str_list_foreach(iotcon_list_h list, iotcon_str_list_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_repr_list_fn)(int index, iotcon_repr_h value, void *user_data); +void iotcon_repr_list_foreach(iotcon_list_h list, iotcon_repr_list_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 7268a88..295c437 100644 --- a/lib/include/iotcon-struct.h +++ b/lib/include/iotcon-struct.h @@ -49,9 +49,9 @@ typedef struct ic_res_response_s* iotcon_response_h; * @brief observation information structure */ typedef struct { - // Action associated with observation request + /* Action associated with observation request */ iotcon_osbserve_action_e action; - // Identifier for observation being registered/deregistered + /* Identifier for observation being registered/deregistered */ iotcon_observation_id obs_id; } iotcon_observation_info_s; @@ -100,4 +100,4 @@ typedef struct char *firmware_version; char *support_url; } iotcon_device_info_s; -#endif //__IOT_CONNECTIVITY_MANAGER_STRUCT_H__ +#endif /* __IOT_CONNECTIVITY_MANAGER_STRUCT_H__ */ diff --git a/lib/include/iotcon.h b/lib/include/iotcon.h index 798fb5e..0f87df0 100644 --- a/lib/include/iotcon.h +++ b/lib/include/iotcon.h @@ -23,12 +23,12 @@ extern "C" { #include #include #include +#include void iotcon_initialize(const char *addr, unsigned short port); void iotcon_deinitialize(); -iotcon_response_h iotcon_response_new(iotcon_request_h req_h, - iotcon_resource_h res_h); +iotcon_response_h iotcon_response_new(iotcon_request_h req_h, iotcon_resource_h res_h); void iotcon_response_free(iotcon_response_h resp); typedef void (*iotcon_rest_api_handle_cb)(const iotcon_request_s *request); @@ -80,22 +80,20 @@ typedef void (*iotcon_on_put_cb)(const iotcon_options_h header_options, const iotcon_repr_h repr, const int e_code, void *user_data); typedef void (*iotcon_on_post_cb)(const iotcon_options_h header_options, const iotcon_repr_h repr, const int e_code, void *user_data); -typedef void (*iotcon_on_observe_cb)(const iotcon_options_h header_options, +typedef void (*iotcon_on_observe_cb)(const iotcon_options_h header_options, const iotcon_repr_h repr, const int e_code, const int sequence_number, void *user_data); typedef void (*iotcon_on_delete_cb)(const iotcon_options_h header_options, const int e_code, void *user_data); -iotcon_resource_s iotcon_construct_resource_object(const char *host, - const char *uri, - bool is_observable, - iotcon_resource_types resource_type, - iotcon_resource_interfaces resource_if); +iotcon_resource_s iotcon_construct_resource_object(const char *host, const char *uri, +bool is_observable, iotcon_resource_types resource_type, + iotcon_resource_interfaces resource_if); void iotcon_destruct_resource_object(iotcon_resource_s *resource); iotcon_resource_s iotcon_copy_resource(iotcon_resource_s resource); -int iotcon_get(iotcon_resource_s resource, iotcon_query query, - iotcon_on_get_cb on_get_cb, void *user_data); +int iotcon_get(iotcon_resource_s resource, iotcon_query query, iotcon_on_get_cb on_get_cb, + void *user_data); int iotcon_put(iotcon_resource_s resource, iotcon_repr_h repr, iotcon_query query, iotcon_on_put_cb on_put_cb, void *user_data); int iotcon_post(iotcon_resource_s resource, iotcon_repr_h repr, iotcon_query query, @@ -103,11 +101,8 @@ int iotcon_post(iotcon_resource_s resource, iotcon_repr_h repr, iotcon_query que int iotcon_delete_resource(iotcon_resource_s resource, iotcon_on_delete_cb on_delete_cb, void *user_data); -int iotcon_observe(iotcon_observe_type_e observe_type, - iotcon_resource_s *resource, - iotcon_query query, - iotcon_on_observe_cb on_observe_cb, - void *user_data); +int iotcon_observe(iotcon_observe_type_e observe_type, iotcon_resource_s *resource, + iotcon_query query, iotcon_on_observe_cb on_observe_cb, void *user_data); int iotcon_cancel_observe(iotcon_resource_s resource); iotcon_resource_types iotcon_resource_types_new(); @@ -127,8 +122,8 @@ int iotcon_options_insert(iotcon_options_h options, const unsigned short id, int iotcon_options_delete(iotcon_options_h options, const unsigned short id); const char* iotcon_options_lookup(iotcon_options_h options, const unsigned short id); typedef void (*iotcon_options_foreach_cb)(unsigned short id, char *data, void *user_data); -void iotcon_options_foreach(iotcon_options_h options, iotcon_options_foreach_cb foreach_cb, - void *user_data); +void iotcon_options_foreach(iotcon_options_h options, + iotcon_options_foreach_cb foreach_cb, void *user_data); iotcon_options_h iotcon_options_clone(iotcon_options_h options); iotcon_query iotcon_query_new(); @@ -151,79 +146,8 @@ iotcon_resource_interfaces iotcon_resource_get_interfaces(iotcon_resource_s reso void iotcon_resource_set_options(iotcon_resource_s *resource, iotcon_options_h header_options); -iotcon_repr_h iotcon_repr_new(); -const char* iotcon_repr_get_uri(iotcon_repr_h repr); -int iotcon_repr_set_uri(iotcon_repr_h repr, const char *uri); - -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_append_resource_types(iotcon_repr_h repr, const char *type); -int iotcon_repr_get_resource_types_count(iotcon_repr_h repr); - -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_append_resource_interfaces(iotcon_repr_h repr, const char *interface); -int iotcon_repr_get_resource_interfaces_count(iotcon_repr_h repr); - -int iotcon_repr_get_int(iotcon_repr_h repr, const char *key); -int iotcon_repr_set_int(iotcon_repr_h repr, const char *key, int ival); -bool iotcon_repr_get_bool(iotcon_repr_h repr, const char *key); -int iotcon_repr_set_bool(iotcon_repr_h repr, const char *key, bool bval); -double iotcon_repr_get_double(iotcon_repr_h repr, const char *key); -int iotcon_repr_set_double(iotcon_repr_h repr, const char *key, double dbval); -char* iotcon_repr_get_str(iotcon_repr_h repr, const char *key); -int iotcon_repr_set_str(iotcon_repr_h repr, const char *key, const char *strval); -iotcon_list_h iotcon_repr_get_list(iotcon_repr_h repr, const char *key); -int iotcon_repr_replace_list(iotcon_repr_h repr, const char *key, iotcon_list_h list); -iotcon_repr_h iotcon_repr_get_repr(iotcon_repr_h repr, const char *key); -int iotcon_repr_replace_repr(iotcon_repr_h dest, const char *key, iotcon_repr_h src); -bool iotcon_repr_is_null(iotcon_repr_h repr, const char *key); -int iotcon_repr_set_null(iotcon_repr_h repr, const char *key); - -void iotcon_repr_free(iotcon_repr_h repr); - -GList* iotcon_repr_get_children(iotcon_repr_h parent); -int iotcon_repr_get_children_count(iotcon_repr_h parent); -int iotcon_repr_append_child(iotcon_repr_h parent, iotcon_repr_h child); -iotcon_repr_h iotcon_repr_get_nth_child(iotcon_repr_h parent, int index); - -GList* iotcon_repr_get_key_list(iotcon_repr_h repr); -int iotcon_repr_get_keys_count(iotcon_repr_h repr); -bool iotcon_repr_contains(iotcon_repr_h repr, const char *key); - -void iotcon_repr_print(iotcon_repr_h repr); - -iotcon_list_h iotcon_list_new(iotcon_repr_types_e type); -iotcon_list_h iotcon_list_append_int(iotcon_list_h list, int ival); -iotcon_list_h iotcon_list_append_bool(iotcon_list_h list, bool bval); -iotcon_list_h iotcon_list_append_double(iotcon_list_h list, double dbval); -iotcon_list_h iotcon_list_append_str(iotcon_list_h list, const char *strval); -iotcon_list_h iotcon_list_append_list(iotcon_list_h dest, iotcon_list_h src); -iotcon_list_h iotcon_list_append_repr(iotcon_list_h list, iotcon_repr_h repr); -iotcon_repr_h iotcon_list_get_nth_repr(iotcon_list_h list, int index); -int iotcon_list_get_length(iotcon_list_h list); -iotcon_list_h iotcon_list_get_nth_list(iotcon_list_h list, int index); -iotcon_value_h iotcon_list_get_nth_value(iotcon_list_h list, int index); - -typedef void (*iotcon_list_fn)(iotcon_value_h value, void *user_data); -void iotcon_list_foreach(iotcon_list_h list, iotcon_list_fn fn, void *user_data); - -void iotcon_repr_free_list(iotcon_list_h list); - -int iotcon_value_get_type(iotcon_value_h value); -int iotcon_value_get_int(iotcon_value_h value); -bool iotcon_value_get_bool(iotcon_value_h value); -double iotcon_value_get_double(iotcon_value_h value); -char* iotcon_value_get_str(iotcon_value_h value); -iotcon_list_h iotcon_value_get_list(iotcon_value_h value); -iotcon_repr_h iotcon_value_get_repr(iotcon_value_h value); - -char* iotcon_repr_generate_json(iotcon_repr_h repr); - #ifdef __cplusplus } #endif -#endif //__IOT_CONNECTIVITY_MANAGER_H__ +#endif /* __IOT_CONNECTIVITY_MANAGER_H__ */ diff --git a/test/crud-test-server.c b/test/crud-test-server.c index a3b84a9..67c7aa6 100644 --- a/test/crud-test-server.c +++ b/test/crud-test-server.c @@ -39,16 +39,16 @@ static iotcon_error_e _set_door_resource() my_door.type = strdup("core.door"); if (NULL == my_door.type) { ERR("strdup(core.door) Fail"); - return IOTCON_ERR_MEMORY; + return IOTCON_ERROR_MEMORY; } my_door.uri = strdup("/a/door"); if (NULL == my_door.uri) { ERR("strdup(/a/door) Fail"); - return IOTCON_ERR_MEMORY; + return IOTCON_ERROR_MEMORY; } - return IOTCON_ERR_NONE; + return IOTCON_ERROR_NONE; } static void _check_door_state() @@ -141,14 +141,14 @@ static void _request_handler_post(iotcon_response_h response) static void _request_handler_delete(iotcon_response_h response) { - int ret = IOTCON_ERR_NONE; + int ret = IOTCON_ERROR_NONE; iotcon_repr_h resp_repr = NULL; iotcon_entity_handler_result_e result = IOTCON_EH_OK; INFO("DELETE request"); ret = iotcon_unregister_resource(new_door_handle); resp_repr = iotcon_repr_new(); - if (IOTCON_ERR_NONE == ret) + if (IOTCON_ERROR_NONE == ret) result = IOTCON_EH_RESOURCE_DELETED; else result = IOTCON_EH_ERROR; @@ -197,7 +197,7 @@ int main(int argc, char **argv) iotcon_interface_e door_interfaces = IOTCON_INTERFACE_DEFAULT; iotcon_resource_property_e resource_properties = IOTCON_DISCOVERABLE; iotcon_resource_h door_handle = NULL; - iotcon_error_e iotcon_error = IOTCON_ERR_NONE; + iotcon_error_e iotcon_error = IOTCON_ERROR_NONE; loop = g_main_loop_new(NULL, FALSE); @@ -206,7 +206,8 @@ int main(int argc, char **argv) /* set local door resource */ iotcon_error = _set_door_resource(); - if (IOTCON_ERR_NONE != iotcon_error) { + + if (IOTCON_ERROR_NONE != iotcon_error) { ERR("_set_door_resource() Fail"); return -1; } diff --git a/test/test-log.h b/test/test-log.h index 26fa292..5e1d5e4 100644 --- a/test/test-log.h +++ b/test/test-log.h @@ -126,4 +126,4 @@ } \ } while (0) -#endif //__IOT_CONNECTIVITY_MANAGER_TEST_LOG_H__ +#endif /* __IOT_CONNECTIVITY_MANAGER_TEST_LOG_H__ */ -- 2.7.4