(1) apply reference count concept for repr (2) modify string list
authorMinchul Lee <slotus.lee@samsung.com>
Thu, 28 May 2015 03:30:28 +0000 (12:30 +0900)
committerYoungjae Shin <yj99.shin@samsung.com>
Mon, 8 Jun 2015 10:40:57 +0000 (19:40 +0900)
Change-Id: I89734e1e8221a2a4364fb6bc4035dca8c5857bec
Signed-off-by: Minchul Lee <slotus.lee@samsung.com>
16 files changed:
lib/ic-ioty-repr.cpp
lib/ic-repr-list.c
lib/ic-repr-list.h
lib/ic-repr-obj.c [changed mode: 0644->0755]
lib/ic-repr-value.c
lib/ic-repr.c
lib/ic-repr.h
lib/ic-response.c
lib/ic.c
lib/include/iotcon-representation.h
lib/include/iotcon-struct.h
packaging/iotcon.spec
test/CMakeLists.txt
test/crud-test-client.c
test/repr-test-client.c [new file with mode: 0644]
test/repr-test-server.c [new file with mode: 0644]

index 2331ebd..4fa606a 100644 (file)
@@ -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());
 
@@ -85,7 +85,7 @@ iotcon_repr_h ic_ioty_repr_generate_repr(const OCRepresentation& ocRep)
                        return NULL;
                }
 
-               iotcon_repr_append_child(repr_parent, repr_child);
+               repr_parent->children = g_list_append(repr_parent->children, repr_child);
        }
 
        return repr_parent;
index 3c08db5..3559073 100755 (executable)
 #include "ic-repr-value.h"
 #include "ic-repr-list.h"
 
+void ic_list_inc_ref_count(iotcon_list_h val)
+{
+       RET_IF(NULL == val);
+       RETM_IF(val->ref_count < 0, "Invalid Count(%d)", val->ref_count);
+
+       val->ref_count++;
+}
+
+static bool _ic_list_dec_ref_count(iotcon_list_h val)
+{
+       bool ret;
+
+       RETV_IF(NULL == val, false);
+       RETVM_IF(val->ref_count <= 0, false, "Invalid Count(%d)", val->ref_count);
+
+       val->ref_count--;
+       if (0 == val->ref_count)
+               ret = true;
+       else
+               ret = false;
+
+       return ret;
+}
+
 API iotcon_list_h iotcon_list_new(iotcon_repr_types_e type)
 {
        iotcon_list_h list;
@@ -36,7 +60,7 @@ API iotcon_list_h iotcon_list_new(iotcon_repr_types_e type)
                ERR("calloc() Fail(%d)", errno);
                return NULL;
        }
-
+       ic_list_inc_ref_count(list);
        list->type = type;
 
        return list;
@@ -133,6 +157,8 @@ API iotcon_list_h iotcon_list_insert_list(iotcon_list_h list, iotcon_list_h val,
                return list;
        }
 
+       ic_list_inc_ref_count(val);
+
        list = ic_list_insert(list, value, pos);
 
        return list;
@@ -152,6 +178,7 @@ API iotcon_list_h iotcon_list_insert_repr(iotcon_list_h list, iotcon_repr_h val,
                ERR("ic_value_new_repr(%p) Fail", val);
                return list;
        }
+       ic_repr_inc_ref_count(val);
 
        list = ic_list_insert(list, value, pos);
 
@@ -549,7 +576,6 @@ JsonArray* ic_list_to_json(iotcon_list_h list)
        RETV_IF(NULL == list->list, NULL);
 
        count = g_list_length(list->list);
-       DBG("list count(%d)", count);
 
        parray = json_array_new();
        for (i = 0; i < count; i++) {
@@ -690,15 +716,13 @@ iotcon_list_h ic_list_from_json(JsonArray *parray)
 API void iotcon_list_free(iotcon_list_h list)
 {
        FN_CALL;
-       int count = 0;
        GList *cur = NULL;
 
        RET_IF(NULL == list);
 
-       if (list->list)
-               count = g_list_length(list->list);
+       if (false == _ic_list_dec_ref_count(list))
+               return;
 
-       DBG("list count(%d)", count);
        cur = list->list;
        while (cur) {
                ic_value_free(cur->data);
@@ -736,6 +760,7 @@ static int _ic_list_clone_value(iotcon_list_h list, iotcon_list_h ret_list)
 static int _ic_list_clone_list(iotcon_list_h list, iotcon_list_h ret_list)
 {
        int i, count;
+       iotcon_value_h value;
        iotcon_list_h list_val, copied_list;
 
        count = g_list_length(list->list);
@@ -752,7 +777,14 @@ static int _ic_list_clone_list(iotcon_list_h list, iotcon_list_h ret_list)
                        return IOTCON_ERROR_FAIL;
                }
 
-               iotcon_list_insert_list(ret_list, copied_list, -1);
+               value = ic_value_new_list(copied_list);
+               if (NULL == value) {
+                       ERR("ic_value_new_list(%p) Fail", copied_list);
+                       iotcon_list_free(copied_list);
+                       return IOTCON_ERROR_FAIL;
+               }
+
+               ret_list = ic_list_insert(ret_list, value, -1);
        }
 
        return IOTCON_ERROR_NONE;
@@ -762,6 +794,7 @@ static int _ic_list_clone_list(iotcon_list_h list, iotcon_list_h ret_list)
 static int _ic_list_clone_repr(iotcon_list_h list, iotcon_list_h ret_list)
 {
        int i, count;
+       iotcon_value_h value;
        iotcon_repr_h repr_val, copied_repr;
 
        count = g_list_length(list->list);
@@ -778,7 +811,14 @@ static int _ic_list_clone_repr(iotcon_list_h list, iotcon_list_h ret_list)
                        return IOTCON_ERROR_FAIL;
                }
 
-               iotcon_list_insert_repr(ret_list, copied_repr, -1);
+               value = ic_value_new_repr(copied_repr);
+               if (NULL == value) {
+                       ERR("ic_value_new_repr(%p) Fail", copied_repr);
+                       iotcon_repr_free(copied_repr);
+                       return IOTCON_ERROR_FAIL;
+               }
+
+               ret_list = ic_list_insert(ret_list, value, -1);
        }
 
        return IOTCON_ERROR_NONE;
@@ -830,7 +870,8 @@ iotcon_list_h ic_list_clone(iotcon_list_h list)
                        break;
                default:
                        ERR("Invalid type(%d)", list->type);
-                       break;
+                       iotcon_list_free(ret_list);
+                       return NULL;
        }
 
        return ret_list;
index 26bc6d4..df9cd3c 100755 (executable)
@@ -34,4 +34,6 @@ iotcon_list_h ic_list_from_json(JsonArray *parray);
 
 iotcon_list_h ic_list_clone(iotcon_list_h list);
 
+void ic_list_inc_ref_count(iotcon_list_h val);
+
 #endif /* __IOT_CONNECTIVITY_MANAGER_INTERNAL_REPRESENTATION_LIST_H__ */
old mode 100644 (file)
new mode 100755 (executable)
index 7ba1d0b..2c0e006
@@ -331,7 +331,6 @@ API iotcon_list_h iotcon_repr_get_list(iotcon_repr_h repr, const char *key)
 
 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);
@@ -342,6 +341,7 @@ API int iotcon_repr_set_list(iotcon_repr_h repr, const char *key, iotcon_list_h
                ERR("ic_value_new_list() Fail");
                return IOTCON_ERROR_MEMORY;
        }
+       ic_list_inc_ref_count(list);
 
        g_hash_table_replace(repr->hash_table, ic_utils_strdup(key), value);
 
@@ -395,6 +395,7 @@ API int iotcon_repr_set_repr(iotcon_repr_h repr, const char *key, iotcon_repr_h
                ERR("ic_value_new_repr(%p) Fail", val);
                return IOTCON_ERROR_MEMORY;
        }
+       ic_repr_inc_ref_count(val);
 
        g_hash_table_replace(repr->hash_table, ic_utils_strdup(key), value);
 
@@ -498,7 +499,6 @@ static inline int _ic_obj_to_json(GHashTable *hash, iotcon_str_list_s *key_list,
 */
 JsonObject* ic_obj_to_json(iotcon_repr_h repr)
 {
-       FN_CALL;
        int ret;
        unsigned int i = 0;
        iotcon_str_list_s *key_list = NULL;
@@ -532,48 +532,68 @@ JsonObject* ic_obj_to_json(iotcon_repr_h repr)
 }
 
 static inline int _ic_obj_from_json(JsonObject *obj, GList *key_list, unsigned int index,
-               iotcon_repr_h repr)
+               iotcon_repr_h ret_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);
+       RETV_IF(NULL == ret_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)");
+               ERR("json_object_get_member() Fail");
                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)");
+                       ERR("ic_value_from_json() Fail");
                        return IOTCON_ERROR_PARAM;
                }
-               ic_obj_set_value(repr, key, value);
+               ic_obj_set_value(ret_repr, key, value);
        }
        else if (JSON_NODE_HOLDS_ARRAY(child_node)) {
+               iotcon_value_h value;
                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)");
+                       ERR("ic_list_from_json() Fail");
                        return IOTCON_ERROR_PARAM;
                }
-               iotcon_repr_set_list(repr, key, list);
+
+               value = ic_value_new_list(list);
+               if (NULL == value) {
+                       ERR("ic_value_new_list() Fail");
+                       iotcon_list_free(list);
+                       return IOTCON_ERROR_MEMORY;
+               }
+
+               ic_obj_set_value(ret_repr, key, value);
        }
        else if (JSON_NODE_HOLDS_OBJECT(child_node)) {
+               iotcon_value_h value;
                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)");
+
+               iotcon_repr_h repr = ic_obj_from_json(child_obj);
+               if (NULL == repr) {
+                       ERR("ic_obj_from_json() Fail");
                        return IOTCON_ERROR_PARAM;
                }
-               iotcon_repr_set_repr(repr, key, ret_repr);
+
+               value = ic_value_new_repr(repr);
+               if (NULL == value) {
+                       ERR("ic_value_new_repr(%p) Fail", repr);
+                       iotcon_repr_free(repr);
+                       return IOTCON_ERROR_MEMORY;
+               }
+
+               ic_obj_set_value(ret_repr, key, value);
        }
        else {
                ERR("node type(%d) Fail", json_node_get_node_type(child_node));
index 32e6072..a82e6f1 100644 (file)
@@ -303,8 +303,6 @@ API iotcon_value_h ic_value_from_json(JsonNode *node)
                        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));
@@ -331,7 +329,6 @@ API iotcon_value_h ic_value_from_json(JsonNode *node)
 
 void ic_value_free(gpointer data)
 {
-       FN_CALL;
        iotcon_value_h value;
 
        RET_IF(NULL == data);
@@ -346,14 +343,11 @@ void ic_value_free(gpointer data)
        case IOTCON_TYPE_BOOL:
        case IOTCON_TYPE_DOUBLE:
        case IOTCON_TYPE_NULL:
-               DBG("value is basic. type(%d)", type);
                break;
        case IOTCON_TYPE_LIST:
-               DBG("value is list");
                iotcon_list_free(ic_value_get_list(value));
                break;
        case IOTCON_TYPE_REPR:
-               DBG("value is Repr");
                iotcon_repr_free(ic_value_get_repr(value));
                break;
        default:
index 2dc16c4..6b0eb76 100644 (file)
 #include "ic-repr-obj.h"
 #include "ic-repr.h"
 
+void ic_repr_inc_ref_count(iotcon_repr_h val)
+{
+       RET_IF(NULL == val);
+       RETM_IF(val->ref_count < 0, "Invalid Count(%d)", val->ref_count);
+
+       val->ref_count++;
+}
+
+static bool _ic_repr_dec_ref_count(iotcon_repr_h val)
+{
+       bool ret;
+
+       RETV_IF(NULL == val, -1);
+       RETVM_IF(val->ref_count <= 0, false, "Invalid Count(%d)", val->ref_count);
+
+       val->ref_count--;
+       if (0 == val->ref_count)
+               ret = true;
+       else
+               ret = false;
+
+       return ret;
+}
+
 API iotcon_repr_h iotcon_repr_new()
 {
        iotcon_repr_h ret_val;
@@ -42,6 +66,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_value_free);
+       ic_repr_inc_ref_count(ret_val);
 
        return ret_val;
 }
@@ -185,6 +210,7 @@ API int iotcon_repr_append_child(iotcon_repr_h parent, iotcon_repr_h child)
        RETV_IF(NULL == parent, IOTCON_ERROR_PARAM);
        RETV_IF(NULL == child, IOTCON_ERROR_PARAM);
 
+       ic_repr_inc_ref_count(child);
        parent->children = g_list_append(parent->children, child);
 
        return IOTCON_ERROR_NONE;
@@ -269,7 +295,6 @@ static JsonObject* _ic_repr_data_generate_json(iotcon_repr_h cur_repr,
        RETV_IF(NULL == cur_repr, NULL);
 
        if (0 < iotcon_repr_get_keys_count(cur_repr)) {
-               DBG("Representation has \"%s\" key", IOTCON_KEY_REP);
                repr_obj = ic_obj_to_json(cur_repr);
                if (NULL == repr_obj) {
                        ERR("ic_obj_to_json() Fail()");
@@ -282,16 +307,13 @@ static JsonObject* _ic_repr_data_generate_json(iotcon_repr_h cur_repr,
 
        if (cur_repr->uri) {
                const char *uri = iotcon_repr_get_uri(cur_repr);
-               DBG("Representation has \"%s\" key", IOTCON_KEY_URI);
                json_object_set_string_member(repr_obj, IOTCON_KEY_URI, uri);
        }
 
        if (cur_repr->res_types) {
-               DBG("Representation has \"%s\" key", IOTCON_KEY_RESOURCETYPES);
                rt_count = iotcon_repr_get_resource_types_count(cur_repr);
        }
        if (cur_repr->interfaces) {
-               DBG("Representation has \"%s\" key", IOTCON_KEY_INTERFACES);
                if_count = iotcon_repr_get_resource_interfaces_count(cur_repr);
        }
 
@@ -301,17 +323,15 @@ static JsonObject* _ic_repr_data_generate_json(iotcon_repr_h cur_repr,
        }
 
        if (0 < rt_count) {
-               DBG("Representation has \"%s\" key. count(%d)", IOTCON_KEY_RESOURCETYPES,
-                               rt_count);
                JsonArray *rt_array = json_array_new();
                iotcon_repr_get_resource_types(cur_repr, _ic_repr_get_res_type_fn, rt_array);
                json_object_set_array_member(prop_obj, IOTCON_KEY_RESOURCETYPES, rt_array);
        }
 
        if (0 < if_count) {
-               DBG("Representation has \"%s\" key. count(%d)", IOTCON_KEY_INTERFACES, if_count);
                JsonArray *if_array = json_array_new();
-               iotcon_repr_get_resource_interfaces(cur_repr, _ic_repr_get_res_interface_fn, if_array);
+               iotcon_repr_get_resource_interfaces(cur_repr, _ic_repr_get_res_interface_fn,
+                               if_array);
                json_object_set_array_member(prop_obj, IOTCON_KEY_INTERFACES, if_array);
        }
 
@@ -443,9 +463,6 @@ 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)) {
-
-               DBG("Representation has \"%s\" key", IOTCON_KEY_REP);
-
                repr = ic_obj_from_json(root_obj);
                if (NULL == repr) {
                        ERR("ic_obj_from_json() Fail()");
@@ -453,28 +470,23 @@ iotcon_repr_h ic_repr_parse_json(const char *json_string)
                        return NULL;
                }
        }
-       else
+       else {
                repr = iotcon_repr_new();
+       }
 
        if (json_object_has_member(root_obj, IOTCON_KEY_URI)) {
                uri_value = json_object_get_string_member(root_obj, IOTCON_KEY_URI);
-               DBG("Representation has \"%s\" key (%s)", IOTCON_KEY_URI, uri_value);
                iotcon_repr_set_uri(repr, uri_value);
        }
 
        if (json_object_has_member(root_obj, IOTCON_KEY_PROPERTY)) {
-               DBG("Representation has \"%s\" key", IOTCON_KEY_PROPERTY);
                JsonObject *property_obj = json_object_get_object_member(root_obj,
                IOTCON_KEY_PROPERTY);
 
                if (json_object_has_member(property_obj, IOTCON_KEY_RESOURCETYPES)) {
-                       DBG("Representation has \"%s\" key", IOTCON_KEY_RESOURCETYPES);
                        JsonArray *rt_array = json_object_get_array_member(property_obj,
                        IOTCON_KEY_RESOURCETYPES);
                        unsigned int rt_count = json_array_get_length(rt_array);
-
-                       DBG("rt_count:%d", rt_count);
-
                        unsigned int rt_index = 0;
                        for (rt_index = 0; rt_index < rt_count; rt_index++) {
                                iotcon_repr_append_resource_types(repr,
@@ -482,13 +494,9 @@ iotcon_repr_h ic_repr_parse_json(const char *json_string)
                        }
                }
                if (json_object_has_member(property_obj, IOTCON_KEY_INTERFACES)) {
-                       DBG("Representation has \"%s\" key", IOTCON_KEY_INTERFACES);
                        JsonArray *if_array = json_object_get_array_member(property_obj,
                        IOTCON_KEY_INTERFACES);
                        unsigned int if_count = json_array_get_length(if_array);
-
-                       DBG("if_count:%d", if_count);
-
                        unsigned int if_index = 0;
                        for (if_index = 0; if_index < if_count; if_index++)
                                iotcon_repr_append_resource_interfaces(repr,
@@ -512,8 +520,12 @@ iotcon_repr_h ic_repr_parse_json(const char *json_string)
 API void iotcon_repr_free(iotcon_repr_h repr)
 {
        FN_CALL;
+       int ref_count;
        RET_IF(NULL == repr);
 
+       if (false == _ic_repr_dec_ref_count(repr))
+               return;
+
        free(repr->uri);
 
        /* (GDestroyNotify) : iotcon_repr_h is proper type than gpointer */
@@ -526,48 +538,60 @@ 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)
+static void _ic_repr_obj_clone(char *key, iotcon_value_h src_val, iotcon_repr_h dest_repr)
 {
        FN_CALL;
        int type = IOTCON_TYPE_NONE;
-       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;
+       iotcon_value_h value, copied_val;
+       iotcon_list_h child_list, copied_list;
+       iotcon_repr_h child_repr, copied_repr;
 
-       type = value->type;
+       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_value = ic_value_clone(value);
-               if (NULL == copied_value) {
+               copied_val = ic_value_clone(src_val);
+               if (NULL == copied_val) {
                        ERR("ic_value_clone() Fail");
                        return;
                }
-               ic_obj_set_value(dest_repr, ic_utils_strdup(key), copied_value);
+               ic_obj_set_value(dest_repr, ic_utils_strdup(key), copied_val);
                break;
        case IOTCON_TYPE_LIST:
-               child_list = ic_value_get_list(value);
+               child_list = ic_value_get_list(src_val);
                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);
+
+               value = ic_value_new_list(copied_list);
+               if (NULL == value) {
+                       ERR("ic_value_new_list() Fail");
+                       iotcon_list_free(copied_list);
+                       return;
+               }
+
+               ic_obj_set_value(dest_repr, key, value);
                break;
        case IOTCON_TYPE_REPR:
-               child_repr = ic_value_get_repr(value);
+               child_repr = ic_value_get_repr(src_val);
                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);
+
+               value = ic_value_new_repr(copied_repr);
+               if (NULL == value) {
+                       ERR("ic_value_new_repr(%p) Fail", copied_repr);
+                       return;
+               }
+
+               ic_obj_set_value(dest_repr, key, value);
                break;
        default:
                ERR("Invalid type(%d)", type);
index 33a87d1..879bd90 100755 (executable)
@@ -52,4 +52,6 @@ char* ic_repr_generate_json(iotcon_repr_h repr, bool set_pretty);
 
 iotcon_repr_h ic_repr_parse_json(const char *json_string);
 
+void ic_repr_inc_ref_count(iotcon_repr_h val);
+
 #endif /* __IOT_CONNECTIVITY_MANAGER_INTERNAL_REPRESENTATION_H__ */
index 4e49e5b..450105a 100755 (executable)
@@ -21,6 +21,7 @@
 #include "ic-common.h"
 #include "ic-utils.h"
 #include "ic-ioty.h"
+#include "ic-repr.h"
 #include "ic-options.h"
 #include "ic-request.h"
 #include "ic-response.h"
@@ -76,6 +77,7 @@ API int iotcon_response_set(iotcon_response_h resp, iotcon_response_property_e p
                break;
        case IOTCON_RESPONSE_REPRESENTATION:
                resp->repr = va_arg(args, iotcon_repr_h);
+               ic_repr_inc_ref_count(resp->repr);
                break;
        case IOTCON_RESPONSE_RESULT:
                value = va_arg(args, int);
index 182a1f0..267fa34 100755 (executable)
--- a/lib/ic.c
+++ b/lib/ic.c
@@ -25,6 +25,7 @@
 #include "ic-common.h"
 #include "ic-utils.h"
 #include "ic-ioty.h"
+#include "ic-repr.h"
 #include "ic.h"
 
 /**
index 8c42ac4..8601e4b 100755 (executable)
@@ -32,7 +32,7 @@ int iotcon_repr_del_uri(iotcon_repr_h repr);
  * @ingroup CAPI_IOT_CONNECTIVITY_MODULE
  * @brief Appends resource type name.
  * @since_tizen 3.0
- * @remarks  Duplicate type names are allowed to append.
+ * @remarks  Duplicate type names are allowed.
  *
  * @param[in] repr The handle to the Representation
  * @param[in] type The resource type
@@ -109,6 +109,19 @@ 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);
 
+/**
+ * @ingroup CAPI_IOT_CONNECTIVITY_MODULE
+ * @brief Inserts integer value to list.
+ * @since_tizen 3.0
+ * @remarks If @a pos is negative, or is larger than the number of value in the list,
+ * the new value is added on to the end of the list.
+ *
+ * @param[in] list The handle to the list
+ * @param[in] val The integer
+ * @param[in] pos The position to insert integer
+ *
+ * @return the (possibly changed) start of the list, otherwise a null pointer on failure
+ */
 iotcon_list_h iotcon_list_insert_int(iotcon_list_h list, int val, int pos);
 iotcon_list_h iotcon_list_insert_bool(iotcon_list_h list, bool val, int pos);
 iotcon_list_h iotcon_list_insert_double(iotcon_list_h list, double val, int pos);
index 06f3f5e..ad771a3 100755 (executable)
@@ -101,6 +101,18 @@ const char* iotcon_device_info_get_firmware_version(iotcon_device_info_h device_
 const char* iotcon_device_info_get_support_url(iotcon_device_info_h device_info);
 
 void iotcon_str_list_free(iotcon_str_list_s *str_list);
+
+/**
+ * @ingroup CAPI_IOT_CONNECTIVITY_MODULE
+ * @brief Appends string value to list.
+ * @since_tizen 3.0
+ * @remarks  Duplicate strings are not allowed.
+ *
+ * @param[in] str_list The handle to the list
+ * @param[in] string The string
+ *
+ * @return the (possibly changed) start of the list, otherwise a null pointer on failure
+ */
 iotcon_str_list_s* iotcon_str_list_append(iotcon_str_list_s *str_list,
                const char *string);
 iotcon_str_list_s* iotcon_str_list_remove(iotcon_str_list_s *str_list,
index 28df3f5..8edfb4d 100644 (file)
@@ -50,6 +50,8 @@ MAJORVER=`echo %{version} | awk 'BEGIN {FS="."}{print $1}'`
 %{_bindir}/crud-test-server
 %{_bindir}/device-test-client
 %{_bindir}/device-test-server
+%{_bindir}/repr-test-client
+%{_bindir}/repr-test-server
 
 %files devel
 %{_libdir}/lib%{name}.so
index 0971782..127532f 100644 (file)
@@ -11,6 +11,11 @@ SET(DEVICE_TEST_SERVER "device-test-server")
 SET(DEVICE_TEST_CLIENT_SRCS "device-test-client.c")
 SET(DEVICE_TEST_SERVER_SRCS "device-test-server.c")
 
+SET(REPR_TEST_CLIENT "repr-test-client")
+SET(REPR_TEST_SERVER "repr-test-server")
+SET(REPR_TEST_CLIENT_SRCS "repr-test-client.c")
+SET(REPR_TEST_SERVER_SRCS "repr-test-server.c")
+
 pkg_check_modules(test_pkgs REQUIRED dlog glib-2.0)
 INCLUDE_DIRECTORIES(${test_pkgs_INCLUDE_DIRS})
 LINK_DIRECTORIES(${test_pkgs_LIBRARY_DIRS})
@@ -31,4 +36,10 @@ ADD_EXECUTABLE(${DEVICE_TEST_SERVER} ${DEVICE_TEST_SERVER_SRCS})
 TARGET_LINK_LIBRARIES(${DEVICE_TEST_SERVER} ${test_pkgs_LIBRARIES} ${CLIENT})
 INSTALL(TARGETS ${DEVICE_TEST_SERVER} DESTINATION ${BIN_INSTALL_DIR})
 
+ADD_EXECUTABLE(${REPR_TEST_CLIENT} ${REPR_TEST_CLIENT_SRCS})
+TARGET_LINK_LIBRARIES(${REPR_TEST_CLIENT} ${test_pkgs_LIBRARIES} ${CLIENT})
+INSTALL(TARGETS ${REPR_TEST_CLIENT} DESTINATION ${BIN_INSTALL_DIR})
 
+ADD_EXECUTABLE(${REPR_TEST_SERVER} ${REPR_TEST_SERVER_SRCS})
+TARGET_LINK_LIBRARIES(${REPR_TEST_SERVER} ${test_pkgs_LIBRARIES} ${CLIENT})
+INSTALL(TARGETS ${REPR_TEST_SERVER} DESTINATION ${BIN_INSTALL_DIR})
index 639ae24..c6e0c3b 100755 (executable)
@@ -26,21 +26,6 @@ const char* const door_uri = "/a/door";
 
 iotcon_client_h door_resource = NULL;
 
-char* _alloc_str_from_glist(GList *list)
-{
-       int i;
-       char buf[CRUD_MAX_BUFFER_SIZE] = {0};
-       char *ret_str = NULL;
-
-       for (i = 0; i < g_list_length(list); i++) {
-               char *str = g_list_nth_data(list, i);
-               strncat(buf, str, strlen(str));
-       }
-
-       ret_str = strdup(buf);
-       return ret_str;
-}
-
 void _print_repr_info(iotcon_repr_h repr)
 {
        if (0 < iotcon_repr_get_keys_count(repr))
diff --git a/test/repr-test-client.c b/test/repr-test-client.c
new file mode 100644 (file)
index 0000000..0f920f2
--- /dev/null
@@ -0,0 +1,179 @@
+/*
+ * 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 <stdlib.h>
+#include <glib.h>
+#include <iotcon.h>
+#include "test-log.h"
+
+#define CRUD_MAX_BUFFER_SIZE (256)
+
+const char* const room_uri = "/a/room";
+
+iotcon_client_h room_resource = NULL;
+
+void _get_int_list_fn(int index, const int value, void *user_data)
+{
+       DBG("%d°C", value);
+}
+
+static void _on_get(iotcon_repr_h recv_repr, int response_result)
+{
+       int i, children_count;
+       iotcon_repr_h child_repr;
+       iotcon_list_h list;
+       iotcon_str_list_s *key_list = NULL;
+
+       RETM_IF(IOTCON_RESPONSE_RESULT_OK != response_result, "_on_get_1st Response error(%d)", response_result);
+       INFO("GET request was successful");
+
+       DBG("[ parent representation ]");
+       DBG("uri : %s", iotcon_repr_get_uri(recv_repr));
+       key_list = iotcon_repr_get_key_list(recv_repr);
+       if (key_list) {
+               DBG("name : %s", iotcon_repr_get_str(recv_repr, "name"));
+
+               list = iotcon_repr_get_list(recv_repr, "today_temp");
+
+               DBG("today's temperature :");
+               iotcon_list_foreach_int(list, _get_int_list_fn, NULL);
+               iotcon_str_list_free(key_list);
+       }
+
+       children_count = iotcon_repr_get_children_count(recv_repr);
+
+       for (i = 0; i < children_count; i++) {
+               DBG("[ child representation ]");
+               const char *uri;
+
+               child_repr = iotcon_repr_get_nth_child(recv_repr, i);
+               uri = iotcon_repr_get_uri(child_repr);
+               DBG("uri : %s", uri);
+
+               if (!strcmp("/a/light", uri)) {
+                       key_list = iotcon_repr_get_key_list(child_repr);
+                       if (key_list) {
+                               DBG("brightness : %d", iotcon_repr_get_int(child_repr, "brightness"));
+                               iotcon_str_list_free(key_list);
+                       }
+               }
+               else if (!strcmp("/a/switch", uri)) {
+                       key_list = iotcon_repr_get_key_list(child_repr);
+                       if (key_list) {
+                               DBG("switch : %d", iotcon_repr_get_bool(child_repr, "switch"));
+                               iotcon_str_list_free(key_list);
+                       }
+               }
+       }
+}
+
+static void _on_get_2nd(iotcon_options_h header_options, iotcon_repr_h recv_repr,
+               int response_result, void *user_data)
+{
+       _on_get(recv_repr, response_result);
+}
+
+static void _on_get_1st(iotcon_options_h header_options, iotcon_repr_h recv_repr,
+               int response_result, void *user_data)
+{
+       iotcon_query_h query_params;
+
+       _on_get(recv_repr, response_result);
+
+       query_params = iotcon_query_new();
+       iotcon_query_insert(query_params, "if", "oc.mi.b");
+
+       /* send GET request again with BATCH interface */
+       iotcon_get(room_resource, query_params, _on_get_2nd, NULL);
+
+       iotcon_query_free(query_params);
+}
+
+static void _get_res_type_fn(const char *string, void *user_data)
+{
+       char *resource_uri = user_data;
+
+       DBG("[%s] resource type : %s", resource_uri, string);
+}
+
+static void _found_resource(iotcon_client_h resource, void *user_data)
+{
+       const char *resource_uri;
+       const char *resource_host;
+       iotcon_str_list_s *resource_types = NULL;
+       int resource_interfaces = 0;
+
+       if (resource) {
+               INFO("===== resource found =====");
+
+               /* get the resource URI */
+               resource_uri = iotcon_client_get_uri(resource);
+               if (NULL == resource_uri) {
+                       ERR("uri is NULL");
+                       return;
+               }
+
+               /* get the resource host address */
+               resource_host = iotcon_client_get_host(resource);
+               DBG("[%s] resource host : %s", resource_uri, resource_host);
+
+               /* get the resource interfaces */
+               resource_interfaces = iotcon_client_get_interfaces(resource);
+
+               if (IOTCON_INTERFACE_DEFAULT & resource_interfaces)
+                       DBG("[%s] resource interface : DEFAULT_INTERFACE", resource_uri);
+               if (IOTCON_INTERFACE_LINK & resource_interfaces)
+                       DBG("[%s] resource interface : LINK_INTERFACE", resource_uri);
+               if (IOTCON_INTERFACE_BATCH & resource_interfaces)
+                       DBG("[%s] resource interface : BATCH_INTERFACE", resource_uri);
+               if (IOTCON_INTERFACE_GROUP & resource_interfaces)
+                       DBG("[%s] resource interface : GROUP_INTERFACE", resource_uri);
+
+               /* get the resource types */
+               resource_types = iotcon_client_get_types(resource);
+               iotcon_str_list_foreach(resource_types, _get_res_type_fn, (void *)resource_uri);
+
+               if (!strcmp(room_uri, resource_uri)) {
+                       iotcon_query_h query_params;
+                       /* copy resource to use elsewhere */
+                       room_resource = iotcon_client_clone(resource);
+
+                       query_params = iotcon_query_new();
+                       /* send GET request */
+                       iotcon_get(resource, query_params, _on_get_1st, NULL);
+
+                       iotcon_query_free(query_params);
+               }
+       }
+}
+
+int main(int argc, char **argv)
+{
+       FN_CALL;
+       GMainLoop *loop;
+       loop = g_main_loop_new(NULL, FALSE);
+
+       /* initialize address and port */
+       iotcon_initialize(IOTCON_ALL_INTERFACES, IOTCON_RANDOM_PORT);
+
+       /* find room typed resources */
+       iotcon_find_resource(IOTCON_MULTICAST_ADDRESS, "core.room", &_found_resource, NULL);
+
+       g_main_loop_run(loop);
+       g_main_loop_unref(loop);
+
+       return 0;
+}
diff --git a/test/repr-test-server.c b/test/repr-test-server.c
new file mode 100644 (file)
index 0000000..4638f83
--- /dev/null
@@ -0,0 +1,280 @@
+/*
+ * 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 <stdlib.h>
+#include <stdbool.h>
+#include <glib.h>
+#include <iotcon.h>
+#include "test-log.h"
+
+static void _room_request_handler(iotcon_request_h request, void *user_data);
+
+static void _send_response(iotcon_response_h response, iotcon_repr_h repr,
+               iotcon_interface_e interface)
+{
+       iotcon_response_set(response, IOTCON_RESPONSE_REPRESENTATION, repr);
+       iotcon_response_set(response, IOTCON_RESPONSE_INTERFACE, interface);
+       iotcon_response_set(response, IOTCON_RESPONSE_RESULT, IOTCON_RESPONSE_RESULT_OK);
+
+       /* send Representation to the client */
+       iotcon_response_send(response);
+}
+
+static void _light_request_handler_get(iotcon_response_h response)
+{
+       iotcon_repr_h resp_repr;
+
+       INFO("GET request - Light");
+
+       /* create a light Representation */
+       resp_repr = iotcon_repr_new();
+
+       _send_response(response, resp_repr, IOTCON_INTERFACE_DEFAULT);
+       iotcon_repr_free(resp_repr);
+}
+
+void _query_foreach_cb(const char *key, const char *value, void *user_data)
+{
+       char **interface_str = user_data;
+
+       if (!strcmp("if", key)) {
+               *interface_str = (char*)value;
+       }
+}
+
+static void _room_request_handler_get(iotcon_request_h request,
+               iotcon_response_h response)
+{
+       iotcon_repr_h room_repr;
+       iotcon_repr_h light_repr;
+       iotcon_repr_h switch_repr;
+       iotcon_list_h temperature_list;
+
+       iotcon_query_h query;
+       char *query_str = NULL;
+
+       iotcon_interface_e interface;
+
+       INFO("GET request - Room");
+
+       /* create a room Representation */
+       room_repr = iotcon_repr_new();
+       iotcon_repr_set_uri(room_repr, "/a/room");
+       iotcon_repr_set_str(room_repr, "name", "Michael's Room");
+
+       temperature_list = iotcon_list_new(IOTCON_TYPE_INT);
+       iotcon_list_insert_int(temperature_list, 22, -1);
+       iotcon_list_insert_int(temperature_list, 23, -1);
+       iotcon_list_insert_int(temperature_list, 24, -1);
+       iotcon_list_insert_int(temperature_list, 25, -1);
+       iotcon_list_insert_int(temperature_list, 26, -1);
+       iotcon_repr_set_list(room_repr, "today_temp", temperature_list);
+
+       /* create a light Representation */
+       light_repr = iotcon_repr_new();
+       iotcon_repr_set_uri(light_repr, "/a/light");
+       iotcon_repr_set_int(light_repr, "brightness", 50);
+
+       /* create a switch Representation */
+       switch_repr = iotcon_repr_new();
+       iotcon_repr_set_uri(switch_repr, "/a/switch");
+       iotcon_repr_set_bool(switch_repr, "switch", false);
+
+       iotcon_repr_append_child(room_repr, light_repr);
+       iotcon_repr_append_child(room_repr, switch_repr);
+       iotcon_repr_free(light_repr);
+       iotcon_repr_free(switch_repr);
+
+       query = iotcon_request_get_query(request);
+       iotcon_query_foreach(query, _query_foreach_cb, &query_str);
+
+       if (query_str && !strcmp("oc.mi.b", query_str)) {
+               DBG("operation for BATCH interface");
+               interface = IOTCON_INTERFACE_BATCH;
+       }
+       else {
+               DBG("operation for DEFAULT interface");
+               interface = IOTCON_INTERFACE_DEFAULT;
+       }
+
+       _send_response(response, room_repr, interface);
+       iotcon_repr_free(room_repr);
+
+       FN_END;
+}
+
+static void _request_handler_put(iotcon_request_h request, iotcon_response_h response)
+{
+       iotcon_repr_h resp_repr = iotcon_repr_new();
+
+       INFO("PUT request");
+
+       /* do PUT operation */
+
+       _send_response(response, resp_repr, IOTCON_INTERFACE_DEFAULT);
+       iotcon_repr_free(resp_repr);
+}
+
+static void _request_handler_post(iotcon_response_h response)
+{
+       iotcon_repr_h resp_repr = iotcon_repr_new();
+
+       INFO("POST request");
+
+       /* do POST operation */
+
+       _send_response(response, resp_repr, IOTCON_INTERFACE_DEFAULT);
+       iotcon_repr_free(resp_repr);
+
+}
+
+static void _request_handler_delete(iotcon_response_h response)
+{
+       iotcon_repr_h resp_repr = iotcon_repr_new();
+
+       INFO("DELETE request");
+
+       /* do DELETE operation */
+
+       _send_response(response, resp_repr, IOTCON_INTERFACE_DEFAULT);
+       iotcon_repr_free(resp_repr);
+
+}
+
+static void _light_request_handler(iotcon_request_h request, void *user_data)
+{
+       const char *request_type = NULL;
+       int request_flag = IOTCON_INIT_FLAG;
+       iotcon_response_h response;
+       FN_CALL;
+
+       RET_IF(NULL == request);
+
+       request_type = iotcon_request_get_request_type(request);
+       if (NULL == request_type) {
+               ERR("request_type is NULL");
+               return;
+       }
+
+       request_flag = iotcon_request_get_request_handler_flag(request);
+       if (request_flag & IOTCON_CRUD_FLAG) {
+               response = iotcon_response_new(request);
+               if (NULL == response) {
+                       ERR("iotcon_response_new() Fail");
+                       return;
+               }
+
+               if (!strcmp("GET", request_type))
+                       _light_request_handler_get(response);
+
+               else if (!strcmp("PUT", request_type))
+                       _request_handler_put(request, response);
+
+               else if (!strcmp("POST", request_type))
+                       _request_handler_post(response);
+
+               else if (!strcmp("DELETE", request_type))
+                       _request_handler_delete(response);
+
+               iotcon_response_free(response);
+       }
+}
+
+static void _room_request_handler(iotcon_request_h request, void *user_data)
+{
+       const char *request_type = NULL;
+       int request_flag = IOTCON_INIT_FLAG;
+       iotcon_response_h response;
+       FN_CALL;
+
+       RET_IF(NULL == request);
+
+       request_type = iotcon_request_get_request_type(request);
+       if (NULL == request_type) {
+               ERR("request_type is NULL");
+               return;
+       }
+
+       request_flag = iotcon_request_get_request_handler_flag(request);
+       if (request_flag & IOTCON_CRUD_FLAG) {
+               response = iotcon_response_new(request);
+               if (NULL == response) {
+                       ERR("iotcon_response_new() Fail");
+                       return;
+               }
+
+               if (!strcmp("GET", request_type))
+                       _room_request_handler_get(request, response);
+
+               else if (!strcmp("PUT", request_type))
+                       _request_handler_put(request, response);
+
+               else if (!strcmp("POST", request_type))
+                       _request_handler_post(response);
+
+               else if (!strcmp("DELETE", request_type))
+                       _request_handler_delete(response);
+
+               iotcon_response_free(response);
+       }
+}
+
+int main(int argc, char **argv)
+{
+       FN_CALL;
+       GMainLoop *loop;
+       iotcon_str_list_s room_rtypes = {strdup("core.room"), NULL};
+       iotcon_str_list_s light_rtypes = {strdup("core.light"), NULL};
+       iotcon_error_e iotcon_error = IOTCON_ERROR_NONE;
+
+       loop = g_main_loop_new(NULL, FALSE);
+
+       /* initialize address and port */
+       iotcon_initialize("0.0.0.0", 0);
+
+       /* register room resource */
+       iotcon_resource_h room_handle = iotcon_register_resource("/a/room", &room_rtypes,
+                       (IOTCON_INTERFACE_DEFAULT | IOTCON_INTERFACE_BATCH),
+                       (IOTCON_DISCOVERABLE | IOTCON_OBSERVABLE), _room_request_handler,
+                       NULL);
+       if (NULL == room_handle) {
+               ERR("iotcon_register_resource() Fail");
+               return -1;
+       }
+
+       /* register room resource */
+       iotcon_resource_h light_handle = iotcon_register_resource("/a/light", &light_rtypes,
+                       (IOTCON_INTERFACE_DEFAULT | IOTCON_INTERFACE_BATCH),
+                       (IOTCON_DISCOVERABLE | IOTCON_OBSERVABLE), _light_request_handler,
+                       NULL);
+       if (NULL == light_handle) {
+               ERR("iotcon_register_resource() Fail");
+               return -1;
+       }
+
+       iotcon_error = iotcon_bind_resource(room_handle, light_handle);
+       if (IOTCON_ERROR_NONE != iotcon_error) {
+               ERR("iotcon_bind_resource() Fail");
+               return -1;
+       }
+
+       g_main_loop_run(loop);
+       g_main_loop_unref(loop);
+
+       iotcon_unregister_resource(room_handle);
+
+       return 0;
+}