Modify reference count(repr, state, list)
authoryoungman <yman.jung@samsung.com>
Tue, 22 Dec 2015 02:36:21 +0000 (11:36 +0900)
committerYoungjae Shin <yj99.shin@samsung.com>
Tue, 22 Dec 2015 08:21:50 +0000 (17:21 +0900)
Change-Id: I05e6b99a5b60fac1e09c308666b63e07721a3fcc
Signed-off-by: youngman <yman.jung@samsung.com>
lib/icl-list.c
lib/icl-list.h
lib/icl-lite-resource.c
lib/icl-payload.c
lib/icl-remote-resource.c
lib/icl-representation.c
lib/icl-representation.h
lib/icl-response.c
lib/icl-state.c
lib/icl-state.h
lib/icl-value.c

index f656367..a03c268 100644 (file)
 #include "icl-value.h"
 #include "icl-list.h"
 
-void icl_list_inc_ref_count(iotcon_list_h val)
+iotcon_list_h icl_list_ref(iotcon_list_h list)
 {
-       RET_IF(NULL == val);
-       RETM_IF(val->ref_count < 0, "Invalid Count(%d)", val->ref_count);
-
-       val->ref_count++;
-}
-
-static bool _icl_list_dec_ref_count(iotcon_list_h val)
-{
-       RETV_IF(NULL == val, false);
-       RETVM_IF(val->ref_count <= 0, false, "Invalid Count(%d)", val->ref_count);
+       RETV_IF(NULL == list, NULL);
+       RETV_IF(list->ref_count <= 0, NULL);
 
-       val->ref_count--;
-       if (0 == val->ref_count)
-               return true;
+       list->ref_count++;
 
-       return false;
+       return list;
 }
 
-static int _icl_list_create(iotcon_type_e type, iotcon_list_h *ret_list)
-{
-       iotcon_list_h list;
-
-       list = calloc(1, sizeof(struct icl_list_s));
-       if (NULL == list) {
-               ERR("calloc() Fail(%d)", errno);
-               return IOTCON_ERROR_OUT_OF_MEMORY;
-       }
-       icl_list_inc_ref_count(list);
-       list->type = type;
-
-       *ret_list = list;
-
-       return IOTCON_ERROR_NONE;
-}
 
 API int iotcon_list_create(iotcon_type_e type, iotcon_list_h *ret_list)
 {
-       int ret;
        iotcon_list_h list;
 
        RETV_IF(NULL == ret_list, IOTCON_ERROR_INVALID_PARAMETER);
@@ -73,12 +46,15 @@ API int iotcon_list_create(iotcon_type_e type, iotcon_list_h *ret_list)
                return IOTCON_ERROR_INVALID_TYPE;
        }
 
-       ret = _icl_list_create(type, &list);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("_icl_list_create() Fail");
-               return ret;
+       list = calloc(1, sizeof(struct icl_list_s));
+       if (NULL == list) {
+               ERR("calloc() Fail(%d)", errno);
+               return IOTCON_ERROR_OUT_OF_MEMORY;
        }
 
+       list->ref_count = 1;
+       list->type = type;
+
        *ret_list = list;
 
        return IOTCON_ERROR_NONE;
@@ -173,8 +149,6 @@ API int iotcon_list_add_list(iotcon_list_h list, iotcon_list_h val, int pos)
                return IOTCON_ERROR_OUT_OF_MEMORY;
        }
 
-       icl_list_inc_ref_count(val);
-
        return icl_list_insert(list, value, pos);
 }
 
@@ -193,7 +167,6 @@ API int iotcon_list_add_state(iotcon_list_h list, iotcon_state_h val, int pos)
                ERR("icl_value_create_state(%p) Fail", val);
                return IOTCON_ERROR_OUT_OF_MEMORY;
        }
-       icl_state_inc_ref_count(val);
 
        return icl_list_insert(list, value, pos);
 }
@@ -421,9 +394,10 @@ API int iotcon_list_get_length(iotcon_list_h list, unsigned int *length)
 {
        RETV_IF(NULL == list, IOTCON_ERROR_INVALID_PARAMETER);
        RETV_IF(NULL == length, IOTCON_ERROR_INVALID_PARAMETER);
-       RETV_IF(NULL == list->list, IOTCON_ERROR_INVALID_PARAMETER);
-
-       *length = g_list_length(list->list);
+       if (NULL == list->list)
+               *length = 0;
+       else
+               *length = g_list_length(list->list);
 
        return IOTCON_ERROR_NONE;
 }
@@ -610,12 +584,13 @@ static iotcon_value_h _icl_list_get_nth_value(iotcon_list_h list, int pos)
 
 API void iotcon_list_destroy(iotcon_list_h list)
 {
-       FN_CALL;
        GList *cur = NULL;
 
        RET_IF(NULL == list);
 
-       if (false == _icl_list_dec_ref_count(list))
+       list->ref_count--;
+
+       if (0 != list->ref_count)
                return;
 
        cur = list->list;
index 573c39c..92d4dc5 100644 (file)
@@ -32,6 +32,6 @@ int icl_list_insert(iotcon_list_h list, iotcon_value_h value, int pos);
 
 iotcon_list_h icl_list_clone(iotcon_list_h list);
 
-void icl_list_inc_ref_count(iotcon_list_h val);
+iotcon_list_h icl_list_ref(iotcon_list_h list);
 
 #endif /* __IOT_CONNECTIVITY_MANAGER_LIBRARY_LIST_H__ */
index 63ce405..9fbc06d 100644 (file)
@@ -49,6 +49,11 @@ static inline int _icl_lite_resource_set_state(iotcon_state_h state,
        gpointer key, value;
        iotcon_value_h res_value, src_value;
 
+       if (NULL == res_state) {
+               DBG("resource_state is NULL");
+               return IOTCON_ERROR_NONE;
+       }
+
        g_hash_table_iter_init(&iter, state->hash_table);
        while (g_hash_table_iter_next(&iter, &key, &value)) {
                res_value = g_hash_table_lookup(res_state->hash_table, key);
@@ -91,8 +96,8 @@ static int _icl_lite_resource_response_send(iotcon_representation_h repr,
        response->result = response_result;
        response->oic_request_h = oic_request_h;
        response->oic_resource_h = oic_resource_h;
-       response->repr = repr;
-       icl_representation_inc_ref_count(response->repr);
+       if (repr)
+               response->repr = icl_representation_ref(repr);
 
        ret = iotcon_response_send(response);
        if (IOTCON_ERROR_NONE != ret) {
@@ -261,7 +266,8 @@ static void _icl_lite_resource_conn_cleanup(iotcon_lite_resource_h resource)
                return;
        }
 
-       iotcon_state_destroy(resource->state);
+       if (resource->state)
+               iotcon_state_destroy(resource->state);
        free(resource->uri_path);
        free(resource);
 }
@@ -324,14 +330,8 @@ API int iotcon_lite_resource_create(const char *uri_path,
 
        resource->properties = properties;
        resource->uri_path = ic_utils_strdup(uri_path);
-
-       ret = icl_state_clone(state, &(resource->state));
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("icl_state_clone() Fail(%d)", ret);
-               free(resource->uri_path);
-               free(resource);
-               return ret;
-       }
+       if (state)
+               resource->state = icl_state_ref(state);
 
        snprintf(signal_name, sizeof(signal_name), "%s_%llx", IC_DBUS_SIGNAL_REQUEST_HANDLER,
                        signal_number);
@@ -340,7 +340,8 @@ API int iotcon_lite_resource_create(const char *uri_path,
                        _icl_lite_resource_conn_cleanup, _icl_lite_resource_request_handler);
        if (0 == sub_id) {
                ERR("icl_dbus_subscribe_signal() Fail");
-               iotcon_state_destroy(resource->state);
+               if (resource->state)
+                       iotcon_state_destroy(resource->state);
                free(resource->uri_path);
                free(resource);
                return IOTCON_ERROR_DBUS;
@@ -396,19 +397,16 @@ API int iotcon_lite_resource_update_state(iotcon_lite_resource_h resource,
                iotcon_state_h state)
 {
        int ret;
-       iotcon_state_h cloned_state = NULL;
 
        RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
 
-       ret = icl_state_clone(state, &cloned_state);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("icl_state_clone() Fail(%d)", ret);
-               return ret;
-       }
+       if (state)
+               state = icl_state_ref(state);
 
-       iotcon_state_destroy(resource->state);
+       if (resource->state)
+               iotcon_state_destroy(resource->state);
 
-       resource->state = cloned_state;
+       resource->state = state;
 
        ret = _icl_lite_resource_notify(resource);
        if (IOTCON_ERROR_NONE != ret)
index 63b9568..c90a637 100644 (file)
@@ -20,6 +20,7 @@
 #include "ic-utils.h"
 #include "icl.h"
 #include "icl-representation.h"
+#include "icl-state.h"
 #include "icl-list.h"
 #include "icl-value.h"
 #include "icl-resource-types.h"
index 2d9e1fc..9eba769 100644 (file)
@@ -438,13 +438,13 @@ API int iotcon_remote_resource_set_options(iotcon_remote_resource_h resource,
 {
        RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
 
+       if (options)
+               options = icl_options_ref(options);
+
        if (resource->header_options)
                iotcon_options_destroy(resource->header_options);
 
-       if (options)
-               resource->header_options = icl_options_ref(options);
-       else
-               resource->header_options = NULL;
+       resource->header_options = options;
 
        return IOTCON_ERROR_NONE;
 }
index e5f177e..aadaeba 100644 (file)
 #include "icl-state.h"
 #include "icl-representation.h"
 
-void icl_representation_inc_ref_count(iotcon_representation_h val)
+iotcon_representation_h icl_representation_ref(iotcon_representation_h repr)
 {
-       RET_IF(NULL == val);
-       RETM_IF(val->ref_count < 0, "Invalid Count(%d)", val->ref_count);
+       RETV_IF(NULL == repr, NULL);
+       RETV_IF(repr->ref_count <= 0, NULL);
 
-       val->ref_count++;
-}
-
-
-static bool _icl_representation_dec_ref_count(iotcon_representation_h val)
-{
-       RETV_IF(NULL == val, false);
-       RETVM_IF(val->ref_count <= 0, false, "Invalid Count(%d)", val->ref_count);
+       repr->ref_count++;
 
-       val->ref_count--;
-       if (0 == val->ref_count)
-               return true;
-
-       return false;
+       return repr;
 }
 
 
@@ -68,7 +57,7 @@ API int iotcon_representation_create(iotcon_representation_h *ret_repr)
        }
 
        repr->visibility = (ICL_VISIBILITY_REPR | ICL_VISIBILITY_PROP);
-       icl_representation_inc_ref_count(repr);
+       repr->ref_count = 1;
 
        *ret_repr = repr;
 
@@ -80,7 +69,9 @@ API void iotcon_representation_destroy(iotcon_representation_h repr)
 {
        RET_IF(NULL == repr);
 
-       if (false == _icl_representation_dec_ref_count(repr))
+       repr->ref_count--;
+
+       if (0 != repr->ref_count)
                return;
 
        free(repr->uri_path);
@@ -142,11 +133,13 @@ API int iotcon_representation_set_resource_types(iotcon_representation_h repr,
 {
        RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
 
-       iotcon_resource_types_destroy(repr->res_types);
-       repr->res_types = NULL;
-
        if (types)
-               repr->res_types = icl_resource_types_ref(types);
+               types = icl_resource_types_ref(types);
+
+       if (repr->res_types)
+               iotcon_resource_types_destroy(repr->res_types);
+
+       repr->res_types = types;
 
        return IOTCON_ERROR_NONE;
 }
@@ -180,16 +173,11 @@ API int iotcon_representation_set_state(iotcon_representation_h repr,
 {
        RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
 
-       if (state == repr->state)
-               return IOTCON_ERROR_NONE;
+       if (state)
+               state = icl_state_ref(state);
 
-       if (repr->state) {
+       if (repr->state)
                iotcon_state_destroy(repr->state);
-               repr->state = NULL;
-       }
-
-       if (state)
-               icl_state_inc_ref_count(state);
 
        repr->state = state;
 
@@ -211,11 +199,14 @@ API int iotcon_representation_get_state(iotcon_representation_h repr,
 API int iotcon_representation_add_child(iotcon_representation_h parent,
                iotcon_representation_h child)
 {
+       iotcon_representation_h repr;
+
        RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER);
        RETV_IF(NULL == child, IOTCON_ERROR_INVALID_PARAMETER);
 
-       icl_representation_inc_ref_count(child);
-       parent->children = g_list_append(parent->children, child);
+       repr = icl_representation_ref(child);
+
+       parent->children = g_list_append(parent->children, repr);
 
        return IOTCON_ERROR_NONE;
 }
@@ -229,6 +220,8 @@ API int iotcon_representation_remove_child(iotcon_representation_h parent,
 
        parent->children = g_list_remove(parent->children, child);
 
+       iotcon_representation_destroy(child);
+
        return IOTCON_ERROR_NONE;
 }
 
@@ -280,105 +273,6 @@ API int iotcon_representation_get_nth_child(iotcon_representation_h parent, int
        return IOTCON_ERROR_NONE;
 }
 
-API int iotcon_state_foreach(iotcon_state_h state, iotcon_state_cb cb, void *user_data)
-{
-       GHashTableIter iter;
-       gpointer key;
-
-       RETV_IF(NULL == state, IOTCON_ERROR_INVALID_PARAMETER);
-       RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
-
-       g_hash_table_iter_init(&iter, state->hash_table);
-       while (g_hash_table_iter_next(&iter, &key, NULL)) {
-               if (IOTCON_FUNC_STOP == cb(state, key, user_data))
-                       break;
-       }
-
-       return IOTCON_ERROR_NONE;
-}
-
-
-API int iotcon_state_get_keys_count(iotcon_state_h state, unsigned int *count)
-{
-       RETV_IF(NULL == state, IOTCON_ERROR_INVALID_PARAMETER);
-       RETV_IF(NULL == count, IOTCON_ERROR_INVALID_PARAMETER);
-       RETV_IF(NULL == state->hash_table, IOTCON_ERROR_INVALID_PARAMETER);
-
-       *count = g_hash_table_size(state->hash_table);
-
-       return IOTCON_ERROR_NONE;
-}
-
-
-void icl_state_clone_foreach(char *key, iotcon_value_h src_val, iotcon_state_h dest_state)
-{
-       FN_CALL;
-       int type, ret;
-       iotcon_value_h value, copied_val;
-       iotcon_list_h child_list, copied_list;
-       iotcon_state_h child_state;
-       iotcon_state_h copied_state = NULL;
-
-       type = src_val->type;
-       switch (type) {
-       case IOTCON_TYPE_INT:
-       case IOTCON_TYPE_BOOL:
-       case IOTCON_TYPE_DOUBLE:
-       case IOTCON_TYPE_STR:
-       case IOTCON_TYPE_NULL:
-               copied_val = icl_value_clone(src_val);
-               if (NULL == copied_val) {
-                       ERR("icl_value_clone() Fail");
-                       return;
-               }
-
-               icl_state_set_value(dest_state, key, copied_val);
-               break;
-       case IOTCON_TYPE_LIST:
-               ret = icl_value_get_list(src_val, &child_list);
-               if (IOTCON_ERROR_NONE != ret) {
-                       ERR("icl_value_get_list() Fail(%d)", ret);
-                       return;
-               }
-
-               copied_list = icl_list_clone(child_list);
-               if (NULL == copied_list) {
-                       ERR("icl_list_clone() Fail");
-                       return;
-               }
-
-               value = icl_value_create_list(copied_list);
-               if (NULL == value) {
-                       ERR("icl_value_create_list() Fail");
-                       iotcon_list_destroy(copied_list);
-                       return;
-               }
-
-               icl_state_set_value(dest_state, key, value);
-               break;
-       case IOTCON_TYPE_STATE:
-               ret = icl_value_get_state(src_val, &child_state);
-               if (IOTCON_ERROR_NONE != ret) {
-                       ERR("icl_value_get_state() Fail(%d)", ret);
-                       return;
-               }
-
-               g_hash_table_foreach(child_state->hash_table, (GHFunc)icl_state_clone_foreach,
-                               copied_state);
-
-               value = icl_value_create_state(copied_state);
-               if (NULL == value) {
-                       ERR("icl_value_create_state(%p) Fail", copied_state);
-                       return;
-               }
-
-               icl_state_set_value(dest_state, key, value);
-               break;
-       default:
-               ERR("Invalid type(%d)", type);
-               return;
-       }
-}
 
 API int iotcon_representation_clone(const iotcon_representation_h src,
                iotcon_representation_h *dest)
index 71e5381..a862a31 100644 (file)
@@ -36,8 +36,6 @@ struct icl_representation_s {
        struct icl_state_s *state;
 };
 
-void icl_representation_inc_ref_count(iotcon_representation_h val);
-
-void icl_state_clone_foreach(char *key, iotcon_value_h src_val, iotcon_state_h dest_state);
+iotcon_representation_h icl_representation_ref(iotcon_representation_h repr);
 
 #endif /* __IOT_CONNECTIVITY_MANAGER_LIBRARY_REPRESENTATION_H__ */
index 25474b5..05d4104 100644 (file)
@@ -118,12 +118,15 @@ API int iotcon_response_set_representation(iotcon_response_h resp,
 {
        RETV_IF(NULL == resp, IOTCON_ERROR_INVALID_PARAMETER);
 
+       resp->iface = iface;
+
+       if (repr)
+               repr = icl_representation_ref(repr);
+
        if (resp->repr)
                iotcon_representation_destroy(resp->repr);
-       resp->iface = iface;
+
        resp->repr = repr;
-       if (repr)
-               icl_representation_inc_ref_count(resp->repr);
 
        return IOTCON_ERROR_NONE;
 }
@@ -134,13 +137,13 @@ API int iotcon_response_set_options(iotcon_response_h resp,
 {
        RETV_IF(NULL == resp, IOTCON_ERROR_INVALID_PARAMETER);
 
+       if (options)
+               options = icl_options_ref(options);
+
        if (resp->header_options)
                iotcon_options_destroy(resp->header_options);
 
-       if (options)
-               resp->header_options = icl_options_ref(options);
-       else
-               resp->header_options = NULL;
+       resp->header_options = options;
 
        return IOTCON_ERROR_NONE;
 }
index 6fafe97..02356c8 100644 (file)
 #include "icl-representation.h"
 #include "icl-state.h"
 
-void icl_state_inc_ref_count(iotcon_state_h val)
+iotcon_state_h icl_state_ref(iotcon_state_h state)
 {
-       RET_IF(NULL == val);
-       RETM_IF(val->ref_count < 0, "Invalid Count(%d)", val->ref_count);
+       RETV_IF(NULL == state, NULL);
+       RETV_IF(state->ref_count <= 0, NULL);
 
-       val->ref_count++;
-}
-
-
-static bool _icl_state_dec_ref_count(iotcon_state_h val)
-{
-       RETV_IF(NULL == val, false);
-       RETVM_IF(val->ref_count <= 0, false, "Invalid Count(%d)", val->ref_count);
+       state->ref_count++;
 
-       val->ref_count--;
-       if (0 == val->ref_count)
-               return true;
-
-       return false;
+       return state;
 }
 
 
@@ -61,7 +50,7 @@ API int iotcon_state_create(iotcon_state_h *ret_state)
 
        state->hash_table = g_hash_table_new_full(g_str_hash, g_str_equal, free,
                        icl_value_destroy);
-       icl_state_inc_ref_count(state);
+       state->ref_count = 1;
 
        *ret_state = state;
 
@@ -73,28 +62,15 @@ API void iotcon_state_destroy(iotcon_state_h state)
 {
        RET_IF(NULL == state);
 
-       if (false == _icl_state_dec_ref_count(state))
+       state->ref_count--;
+
+       if (0 != state->ref_count)
                return;
 
        g_hash_table_destroy(state->hash_table);
        free(state);
 }
 
-API int iotcon_state_clone(iotcon_state_h state, iotcon_state_h *state_clone)
-{
-       int ret;
-       RETV_IF(NULL == state, IOTCON_ERROR_INVALID_PARAMETER);
-       RETV_IF(NULL == state_clone, IOTCON_ERROR_INVALID_PARAMETER);
-
-       ret = icl_state_clone(state, state_clone);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("icl_state_clone() Fail(%d)", ret);
-               return ret;
-       }
-
-       return IOTCON_ERROR_NONE;
-}
-
 
 int icl_state_del_value(iotcon_state_h state, const char *key)
 {
@@ -387,7 +363,6 @@ API int iotcon_state_add_list(iotcon_state_h state, const char *key, iotcon_list
                ERR("icl_value_create_list() Fail");
                return IOTCON_ERROR_OUT_OF_MEMORY;
        }
-       icl_list_inc_ref_count(list);
 
        g_hash_table_replace(state->hash_table, ic_utils_strdup(key), value);
 
@@ -433,7 +408,6 @@ API int iotcon_state_add_state(iotcon_state_h state, const char *key, iotcon_sta
                ERR("icl_value_create_state(%p) Fail", val);
                return IOTCON_ERROR_OUT_OF_MEMORY;
        }
-       icl_state_inc_ref_count(val);
 
        g_hash_table_replace(state->hash_table, ic_utils_strdup(key), value);
 
@@ -471,27 +445,128 @@ int icl_state_set_value(iotcon_state_h state, const char *key, iotcon_value_h va
 }
 
 
-int icl_state_clone(iotcon_state_h src, iotcon_state_h *dest)
+API int iotcon_state_get_keys_count(iotcon_state_h state, unsigned int *count)
+{
+       RETV_IF(NULL == state, IOTCON_ERROR_INVALID_PARAMETER);
+       RETV_IF(NULL == count, IOTCON_ERROR_INVALID_PARAMETER);
+       RETV_IF(NULL == state->hash_table, IOTCON_ERROR_INVALID_PARAMETER);
+
+       *count = g_hash_table_size(state->hash_table);
+
+       return IOTCON_ERROR_NONE;
+}
+
+
+API int iotcon_state_clone(iotcon_state_h state, iotcon_state_h *state_clone)
 {
        int ret;
 
-       iotcon_state_h cloned_state = NULL;
+       iotcon_state_h temp = NULL;
 
-       RETV_IF(NULL == src, IOTCON_ERROR_INVALID_PARAMETER);
-       RETV_IF(NULL == dest, IOTCON_ERROR_INVALID_PARAMETER);
+       RETV_IF(NULL == state, IOTCON_ERROR_INVALID_PARAMETER);
+       RETV_IF(NULL == state_clone, IOTCON_ERROR_INVALID_PARAMETER);
 
-       if (src->hash_table) {
-               ret = iotcon_state_create(&cloned_state);
+       if (state->hash_table) {
+               ret = iotcon_state_create(&temp);
                if (IOTCON_ERROR_NONE != ret) {
                        ERR("iotcon_state_create() Fail(%d)", ret);
                        return ret;
                }
 
-               g_hash_table_foreach(src->hash_table, (GHFunc)icl_state_clone_foreach,
-                               cloned_state);
+               g_hash_table_foreach(state->hash_table, (GHFunc)icl_state_clone_foreach, temp);
+       }
+
+       *state_clone = temp;
+
+       return IOTCON_ERROR_NONE;
+}
+
+
+void icl_state_clone_foreach(char *key, iotcon_value_h src_val, iotcon_state_h dest_state)
+{
+       FN_CALL;
+       int type, ret;
+       iotcon_value_h value, copied_val;
+       iotcon_list_h child_list, copied_list;
+       iotcon_state_h child_state;
+       iotcon_state_h copied_state = NULL;
+
+       type = src_val->type;
+       switch (type) {
+       case IOTCON_TYPE_INT:
+       case IOTCON_TYPE_BOOL:
+       case IOTCON_TYPE_DOUBLE:
+       case IOTCON_TYPE_STR:
+       case IOTCON_TYPE_NULL:
+               copied_val = icl_value_clone(src_val);
+               if (NULL == copied_val) {
+                       ERR("icl_value_clone() Fail");
+                       return;
+               }
+
+               icl_state_set_value(dest_state, key, copied_val);
+               break;
+       case IOTCON_TYPE_LIST:
+               ret = icl_value_get_list(src_val, &child_list);
+               if (IOTCON_ERROR_NONE != ret) {
+                       ERR("icl_value_get_list() Fail(%d)", ret);
+                       return;
+               }
+
+               copied_list = icl_list_clone(child_list);
+               if (NULL == copied_list) {
+                       ERR("icl_list_clone() Fail");
+                       return;
+               }
+
+               value = icl_value_create_list(copied_list);
+               if (NULL == value) {
+                       ERR("icl_value_create_list() Fail");
+                       iotcon_list_destroy(copied_list);
+                       return;
+               }
+
+               icl_state_set_value(dest_state, key, value);
+               break;
+       case IOTCON_TYPE_STATE:
+               ret = icl_value_get_state(src_val, &child_state);
+               if (IOTCON_ERROR_NONE != ret) {
+                       ERR("icl_value_get_state() Fail(%d)", ret);
+                       return;
+               }
+
+               g_hash_table_foreach(child_state->hash_table, (GHFunc)icl_state_clone_foreach,
+                               copied_state);
+
+               value = icl_value_create_state(copied_state);
+               if (NULL == value) {
+                       ERR("icl_value_create_state(%p) Fail", copied_state);
+                       return;
+               }
+
+               icl_state_set_value(dest_state, key, value);
+               break;
+       default:
+               ERR("Invalid type(%d)", type);
+               return;
        }
+}
 
-       *dest = cloned_state;
+
+API int iotcon_state_foreach(iotcon_state_h state, iotcon_state_cb cb, void *user_data)
+{
+       GHashTableIter iter;
+       gpointer key;
+
+       RETV_IF(NULL == state, IOTCON_ERROR_INVALID_PARAMETER);
+       RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
+
+       g_hash_table_iter_init(&iter, state->hash_table);
+       while (g_hash_table_iter_next(&iter, &key, NULL)) {
+               if (IOTCON_FUNC_STOP == cb(state, key, user_data))
+                       break;
+       }
 
        return IOTCON_ERROR_NONE;
 }
+
index 6e2aad5..e3ed4d2 100644 (file)
 #include "icl-value.h"
 #include "icl-representation.h"
 
-void icl_state_inc_ref_count(iotcon_state_h val);
-
 int icl_state_del_value(iotcon_state_h state, const char *key);
 
 int icl_state_set_value(iotcon_state_h state, const char *key, iotcon_value_h value);
 
-int icl_state_clone(iotcon_state_h src, iotcon_state_h *dest);
+void icl_state_clone_foreach(char *key, iotcon_value_h src_val,
+               iotcon_state_h dest_state);
+
+iotcon_state_h icl_state_ref(iotcon_state_h state);
 
 #endif /* __IOT_CONNECTIVITY_MANAGER_LIBRARY_STATE_H__ */
index aa7a1c4..58c08aa 100644 (file)
@@ -23,6 +23,7 @@
 #include "icl.h"
 #include "icl-representation.h"
 #include "icl-list.h"
+#include "icl-state.h"
 #include "icl-value.h"
 
 static iotcon_value_h _icl_value_create(int type)
@@ -145,7 +146,7 @@ iotcon_value_h icl_value_create_list(iotcon_list_h val)
                return NULL;
        }
 
-       value->list = val;
+       value->list = icl_list_ref(val);
 
        return (iotcon_value_h)value;
 }
@@ -160,7 +161,7 @@ iotcon_value_h icl_value_create_state(iotcon_state_h val)
                return NULL;
        }
 
-       value->state = val;
+       value->state = icl_state_ref(val);
 
        return (iotcon_value_h)value;
 }