Resource Encapsulation
authoryoungman <yman.jung@samsung.com>
Tue, 20 Oct 2015 11:13:37 +0000 (20:13 +0900)
committeryoungman <yman.jung@samsung.com>
Tue, 3 Nov 2015 11:08:20 +0000 (20:08 +0900)
Change-Id: Iebed2bcc56ef15598c25c436ec7ec362c2d7444f
Signed-off-by: youngman <yman.jung@samsung.com>
39 files changed:
daemon/icd-ioty-ocprocess.c
daemon/icd-ioty.c
daemon/icd-payload.c
daemon/icd-payload.h
lib/icl-dbus-type.c
lib/icl-dbus.c
lib/icl-lite-resource.c [new file with mode: 0644]
lib/icl-payload.c
lib/icl-payload.h
lib/icl-presence.c
lib/icl-remote-resource-caching.c [new file with mode: 0644]
lib/icl-remote-resource-crud.c
lib/icl-remote-resource-monitoring.c [new file with mode: 0644]
lib/icl-remote-resource.c
lib/icl-remote-resource.h
lib/icl-repr-list.c
lib/icl-repr-state.c
lib/icl-repr-state.h
lib/icl-repr.c
lib/icl-repr.h
lib/icl-resource.c
lib/icl-resource.h
lib/include/iotcon-constant.h
lib/include/iotcon-struct.h
lib/include/iotcon.h
packaging/iotcon.spec
test/CMakeLists.txt
test/crud-test-client.c [deleted file]
test/crud-test-server.c [deleted file]
test/device-test-client.c [deleted file]
test/iotcon-test-basic-client.c [new file with mode: 0644]
test/iotcon-test-basic-server.c [new file with mode: 0644]
test/iotcon-test-device-client.c [new file with mode: 0644]
test/iotcon-test-encap-client.c [new file with mode: 0644]
test/iotcon-test-encap-server.c [new file with mode: 0644]
test/iotcon-test-repr-client.c [new file with mode: 0644]
test/iotcon-test-repr-server.c [new file with mode: 0644]
test/repr-test-client.c [deleted file]
test/repr-test-server.c [deleted file]

index 4e21f5e850f11e949b1ab594539393b9e36a2375..74f8637363176c7a4ac00e65b4358c0de106ec06 100644 (file)
@@ -525,12 +525,38 @@ static int _ocprocess_worker(_ocprocess_cb cb, int type, OCPayload *payload, int
 }
 
 
+static int _ocprocess_parse_oic_result(OCStackResult result)
+{
+       int res;
+
+       switch (result) {
+       case OC_STACK_OK:
+               res = IOTCON_RESPONSE_RESULT_OK;
+               break;
+       case OC_STACK_RESOURCE_CREATED:
+               res = IOTCON_RESPONSE_RESULT_RESOURCE_CREATED;
+               break;
+       case OC_STACK_RESOURCE_DELETED:
+               res = IOTCON_RESPONSE_RESULT_RESOURCE_DELETED;
+               break;
+       case OC_STACK_UNAUTHORIZED_REQ:
+               res = IOTCON_RESPONSE_RESULT_FORBIDDEN;
+               break;
+       default:
+               WARN("response error(%d)", result);
+               res = IOTCON_RESPONSE_RESULT_ERROR;
+               break;
+       }
+
+       return res;
+}
+
+
 OCStackApplicationResult icd_ioty_ocprocess_get_cb(void *ctx, OCDoHandle handle,
                OCClientResponse *resp)
 {
        FN_CALL;
        int ret, res;
-       OCStackResult result;
        GVariantBuilder *options;
 
        RETV_IF(NULL == ctx, OC_STACK_DELETE_TRANSACTION);
@@ -541,16 +567,10 @@ OCStackApplicationResult icd_ioty_ocprocess_get_cb(void *ctx, OCDoHandle handle,
                return OC_STACK_DELETE_TRANSACTION;
        }
 
-       result = resp->result;
-       if (OC_STACK_OK == result) {
-               res = IOTCON_RESPONSE_RESULT_OK;
-               options = _ocprocess_parse_header_options(resp->rcvdVendorSpecificHeaderOptions,
-                               resp->numRcvdVendorSpecificHeaderOptions);
-       } else {
-               WARN("resp error(%d)", result);
-               res = IOTCON_RESPONSE_RESULT_ERROR;
-               options = NULL;
-       }
+       res = _ocprocess_parse_oic_result(resp->result);
+
+       options = _ocprocess_parse_header_options(resp->rcvdVendorSpecificHeaderOptions,
+                       resp->numRcvdVendorSpecificHeaderOptions);
 
        ret = _ocprocess_worker(_worker_crud_cb, ICD_CRUD_GET, resp->payload, res,
                        options, ctx);
@@ -569,7 +589,6 @@ OCStackApplicationResult icd_ioty_ocprocess_put_cb(void *ctx, OCDoHandle handle,
 {
        FN_CALL;
        int ret, res;
-       OCStackResult result;
        GVariantBuilder *options;
 
        RETV_IF(NULL == ctx, OC_STACK_DELETE_TRANSACTION);
@@ -580,24 +599,10 @@ OCStackApplicationResult icd_ioty_ocprocess_put_cb(void *ctx, OCDoHandle handle,
                return OC_STACK_DELETE_TRANSACTION;
        }
 
-       result = resp->result;
-       switch (result) {
-       case OC_STACK_OK:
-               res = IOTCON_RESPONSE_RESULT_OK;
-               break;
-       case OC_STACK_RESOURCE_CREATED:
-               res = IOTCON_RESPONSE_RESULT_RESOURCE_CREATED;
-               break;
-       default:
-               WARN("resp error(%d)", result);
-               res = IOTCON_RESPONSE_RESULT_ERROR;
-               options = NULL;
-       }
+       res = _ocprocess_parse_oic_result(resp->result);
 
-       if (IOTCON_RESPONSE_RESULT_ERROR != res) {
-               options = _ocprocess_parse_header_options(resp->rcvdVendorSpecificHeaderOptions,
-                               resp->numRcvdVendorSpecificHeaderOptions);
-       }
+       options = _ocprocess_parse_header_options(resp->rcvdVendorSpecificHeaderOptions,
+                       resp->numRcvdVendorSpecificHeaderOptions);
 
        ret = _ocprocess_worker(_worker_crud_cb, ICD_CRUD_PUT, resp->payload, res,
                        options, ctx);
@@ -616,7 +621,6 @@ OCStackApplicationResult icd_ioty_ocprocess_post_cb(void *ctx, OCDoHandle handle
 {
        FN_CALL;
        int ret, res;
-       OCStackResult result;
        GVariantBuilder *options;
 
        RETV_IF(NULL == ctx, OC_STACK_DELETE_TRANSACTION);
@@ -627,24 +631,10 @@ OCStackApplicationResult icd_ioty_ocprocess_post_cb(void *ctx, OCDoHandle handle
                return OC_STACK_DELETE_TRANSACTION;
        }
 
-       result = resp->result;
-       switch (result) {
-       case OC_STACK_OK:
-               res = IOTCON_RESPONSE_RESULT_OK;
-               break;
-       case OC_STACK_RESOURCE_CREATED:
-               res = IOTCON_RESPONSE_RESULT_RESOURCE_CREATED;
-               break;
-       default:
-               WARN("resp error(%d)", result);
-               res = IOTCON_RESPONSE_RESULT_ERROR;
-               options = NULL;
-       }
+       res = _ocprocess_parse_oic_result(resp->result);
 
-       if (IOTCON_RESPONSE_RESULT_ERROR != res) {
-               options = _ocprocess_parse_header_options(resp->rcvdVendorSpecificHeaderOptions,
-                               resp->numRcvdVendorSpecificHeaderOptions);
-       }
+       options = _ocprocess_parse_header_options(resp->rcvdVendorSpecificHeaderOptions,
+                       resp->numRcvdVendorSpecificHeaderOptions);
 
        ret = _ocprocess_worker(_worker_crud_cb, ICD_CRUD_POST, resp->payload, res,
                        options, ctx);
@@ -663,7 +653,6 @@ OCStackApplicationResult icd_ioty_ocprocess_delete_cb(void *ctx, OCDoHandle hand
 {
        FN_CALL;
        int ret, res;
-       OCStackResult result;
        GVariantBuilder *options;
 
        RETV_IF(NULL == ctx, OC_STACK_DELETE_TRANSACTION);
@@ -674,24 +663,10 @@ OCStackApplicationResult icd_ioty_ocprocess_delete_cb(void *ctx, OCDoHandle hand
                return OC_STACK_DELETE_TRANSACTION;
        }
 
-       result = resp->result;
-       switch (result) {
-       case OC_STACK_OK:
-               res = IOTCON_RESPONSE_RESULT_OK;
-               break;
-       case OC_STACK_RESOURCE_DELETED:
-               res = IOTCON_RESPONSE_RESULT_RESOURCE_DELETED;
-               break;
-       default:
-               WARN("resp error(%d)", result);
-               res = IOTCON_RESPONSE_RESULT_ERROR;
-               options = NULL;
-       }
+       res = _ocprocess_parse_oic_result(resp->result);
 
-       if (IOTCON_RESPONSE_RESULT_ERROR != res) {
-               options = _ocprocess_parse_header_options(resp->rcvdVendorSpecificHeaderOptions,
-                               resp->numRcvdVendorSpecificHeaderOptions);
-       }
+       options = _ocprocess_parse_header_options(resp->rcvdVendorSpecificHeaderOptions,
+                       resp->numRcvdVendorSpecificHeaderOptions);
 
        ret = _ocprocess_worker(_worker_crud_cb, ICD_CRUD_DELETE, NULL, res, options, ctx);
        if (IOTCON_ERROR_NONE != ret) {
@@ -733,8 +708,13 @@ static void _observe_cb_response_error(const char *dest, unsigned int signum, in
 {
        int ret;
        GVariant *value;
+       GVariant *payload;
+       GVariantBuilder options;
 
-       value = g_variant_new("(a(qs)vii)", NULL, NULL, ret_val, 0);
+       g_variant_builder_init(&options, G_VARIANT_TYPE("a(qs)"));
+       payload = icd_payload_representation_empty_gvariant();
+
+       value = g_variant_new("(a(qs)vii)", &options, payload, ret_val, 0);
 
        ret = _ocprocess_response_signal(dest, IC_DBUS_SIGNAL_OBSERVE, signum, value);
        if (IOTCON_ERROR_NONE != ret)
@@ -746,7 +726,6 @@ OCStackApplicationResult icd_ioty_ocprocess_observe_cb(void *ctx, OCDoHandle han
                OCClientResponse *resp)
 {
        int ret, res;
-       OCStackResult result;
        GVariantBuilder *options;
        struct icd_observe_context *observe_ctx;
        icd_sig_ctx_s *sig_context = ctx;
@@ -768,16 +747,10 @@ OCStackApplicationResult icd_ioty_ocprocess_observe_cb(void *ctx, OCDoHandle han
                return OC_STACK_KEEP_TRANSACTION;
        }
 
-       result = resp->result;
-       if (OC_STACK_OK == result) {
-               res = IOTCON_RESPONSE_RESULT_OK;
-               options = _ocprocess_parse_header_options(resp->rcvdVendorSpecificHeaderOptions,
-                               resp->numRcvdVendorSpecificHeaderOptions);
-       } else {
-               WARN("resp error(%d)", result);
-               res = IOTCON_RESPONSE_RESULT_ERROR;
-               options = NULL;
-       }
+       res = _ocprocess_parse_oic_result(resp->result);
+
+       options = _ocprocess_parse_header_options(resp->rcvdVendorSpecificHeaderOptions,
+                       resp->numRcvdVendorSpecificHeaderOptions);
 
        observe_ctx->payload = icd_payload_to_gvariant(resp->payload);
        observe_ctx->signum = sig_context->signum;
index 61c1802fc9982057ca7286efdd39b0f697145497..d7c64819f610b026d2b6b8e6795448a86e9d4cf8 100644 (file)
@@ -507,22 +507,31 @@ void icd_ioty_complete(int type, GDBusMethodInvocation *invocation, GVariant *va
 void icd_ioty_complete_error(int type, GDBusMethodInvocation *invocation, int ret_val)
 {
        GVariant *value;
+       GVariant *payload;
+       GVariantBuilder options;
 
        switch (type) {
        case ICD_CRUD_GET:
-               value = g_variant_new("(a(qs)vi)", NULL, NULL, ret_val);
+               g_variant_builder_init(&options, G_VARIANT_TYPE("a(qs)"));
+               payload = icd_payload_representation_empty_gvariant();
+               value = g_variant_new("(a(qs)vi)", &options, payload, ret_val);
                ic_dbus_complete_get(icd_dbus_get_object(), invocation, value);
                break;
        case ICD_CRUD_PUT:
-               value = g_variant_new("(a(qs)vi)", NULL, NULL, ret_val);
+               g_variant_builder_init(&options, G_VARIANT_TYPE("a(qs)"));
+               payload = icd_payload_representation_empty_gvariant();
+               value = g_variant_new("(a(qs)vi)", &options, payload, ret_val);
                ic_dbus_complete_put(icd_dbus_get_object(), invocation, value);
                break;
        case ICD_CRUD_POST:
-               value = g_variant_new("(a(qs)vi)", NULL, NULL, ret_val);
+               g_variant_builder_init(&options, G_VARIANT_TYPE("a(qs)"));
+               payload = icd_payload_representation_empty_gvariant();
+               value = g_variant_new("(a(qs)vi)", &options, payload, ret_val);
                ic_dbus_complete_post(icd_dbus_get_object(), invocation, value);
                break;
        case ICD_CRUD_DELETE:
-               value = g_variant_new("(a(qs)i)", NULL, ret_val);
+               g_variant_builder_init(&options, G_VARIANT_TYPE("a(qs)"));
+               value = g_variant_new("(a(qs)i)", &options, ret_val);
                ic_dbus_complete_delete(icd_dbus_get_object(), invocation, value);
                break;
        case ICD_TIZEN_INFO:
@@ -530,6 +539,7 @@ void icd_ioty_complete_error(int type, GDBusMethodInvocation *invocation, int re
                ic_dbus_complete_get_tizen_info(icd_dbus_get_object(), invocation, value);
                break;
        }
+
 }
 
 
index 2315a9a9f1a9bcb9a63d08ec8e6796221d94595f..74327db0b5011945c6522ca7986cfb4a40a301f9 100644 (file)
@@ -245,7 +245,8 @@ static GVariantBuilder* _icd_state_value_to_gvariant(OCRepPayload *repr)
 }
 
 
-static GVariant* _icd_payload_representation_to_gvariant(OCRepPayload *repr, gboolean is_parent)
+static GVariant* _icd_payload_representation_to_gvariant(OCRepPayload *repr,
+               gboolean is_parent)
 {
        OCStringLL *node;
        int ret, ifaces = 0;
@@ -300,6 +301,21 @@ static GVariant* _icd_payload_representation_to_gvariant(OCRepPayload *repr, gbo
 }
 
 
+GVariant* icd_payload_representation_empty_gvariant(void)
+{
+       GVariant *value;
+       GVariantBuilder types, repr, children;
+
+       g_variant_builder_init(&types, G_VARIANT_TYPE("as"));
+       g_variant_builder_init(&repr, G_VARIANT_TYPE("a{sv}"));
+       g_variant_builder_init(&children, G_VARIANT_TYPE("av"));
+
+       value = g_variant_new("(siasa{sv}av)", IC_STR_NULL, 0, &types, &repr, &children);
+
+       return value;
+}
+
+
 static GVariant* _icd_payload_platform_to_gvariant(OCPlatformPayload *repr)
 {
        GVariant *value;
index 5528f32f085c99e49cf9cea3262198d4483f1d99..432cdca8191cb46bcde0bd362dd2057e931b5435 100644 (file)
@@ -20,6 +20,7 @@
 
 #include <ocpayload.h>
 
+GVariant* icd_payload_representation_empty_gvariant(void);
 GVariant* icd_payload_to_gvariant(OCPayload *payload);
 GVariant** icd_payload_res_to_gvariant(OCPayload *payload, OCDevAddr *dev_addr);
 OCRepPayload* icd_payload_representation_from_gvariant(GVariant *var);
index 0b771b4df28d88df1b240b0947f2136c6fff342c..db75d2c77ba0ad4658a1ff1eb2561c581778b593 100644 (file)
@@ -213,8 +213,10 @@ GVariant* icl_dbus_observers_to_gvariant(iotcon_observers_h observers)
        GVariantBuilder builder;
 
        g_variant_builder_init(&builder, G_VARIANT_TYPE("ai"));
-       for (node = observers->observers_list; node; node = node->next)
-               g_variant_builder_add(&builder, "i", GPOINTER_TO_INT(node->data));
+       if (observers) {
+               for (node = observers->observers_list; node; node = node->next)
+                       g_variant_builder_add(&builder, "i", GPOINTER_TO_INT(node->data));
+       }
 
        return g_variant_builder_end(&builder);
 }
index d0cd5f897cbc4aa787885eea47ee86d71defc8fc..4bd2929c3a0e5288ce56f78acccae8d8e0f05dba 100644 (file)
@@ -122,10 +122,10 @@ static icl_cb_container_s* _dbus_find_conn_changed_cb(iotcon_connection_changed_
 API int iotcon_add_connection_changed_cb(iotcon_connection_changed_cb cb, void *user_data)
 {
        unsigned int id;
+       icl_cb_container_s *cb_container;
 
        RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
 
-       icl_cb_container_s *cb_container;
 
        if (_dbus_find_conn_changed_cb(cb, user_data)) {
                ERR("This callback is already registered.");
diff --git a/lib/icl-lite-resource.c b/lib/icl-lite-resource.c
new file mode 100644 (file)
index 0000000..66bb75c
--- /dev/null
@@ -0,0 +1,427 @@
+/*
+ * 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 <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <glib.h>
+
+#include "iotcon.h"
+#include "ic-utils.h"
+#include "icl.h"
+#include "icl-dbus.h"
+#include "icl-repr.h"
+#include "icl-repr-state.h"
+#include "icl-repr-value.h"
+#include "icl-repr-list.h"
+#include "icl-dbus-type.h"
+#include "icl-payload.h"
+#include "icl-resource.h"
+#include "icl-response.h"
+
+struct icl_lite_resource {
+       char *uri_path;
+       iotcon_state_h state;
+       int64_t handle;
+       unsigned int sub_id;
+       bool is_observable;
+};
+
+
+static inline int _icl_lite_resource_set_state(iotcon_state_h state,
+               iotcon_state_h res_state)
+{
+       GHashTableIter iter;
+       gpointer key, value;
+       iotcon_value_h res_value, src_value;
+
+       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);
+               if (NULL == res_value) {
+                       WARN("Invalid Key(%s)", key);
+                       continue;
+               }
+
+               src_value = icl_value_clone(value);
+               if (NULL == src_value) {
+                       ERR("icl_value_clone() Fail");
+                       return IOTCON_ERROR_REPRESENTATION;
+               }
+
+               if (src_value->type != res_value->type) {
+                       WARN("Different Type(%d)", (src_value->type));
+                       icl_value_destroy(src_value);
+                       continue;
+               }
+
+               g_hash_table_replace(res_state->hash_table, ic_utils_strdup(key), src_value);
+       }
+
+       return IOTCON_ERROR_NONE;
+}
+
+
+static int _icl_lite_resource_response_send(iotcon_representation_h repr,
+               int64_t oic_request_h, int64_t oic_resource_h, int response_result)
+{
+       int ret;
+       iotcon_response_h response;
+
+       response = calloc(1, sizeof(struct icl_resource_response));
+       if (NULL == response) {
+               ERR("calloc() Fail(%d)", errno);
+               return IOTCON_ERROR_OUT_OF_MEMORY;
+       }
+       response->iface = IOTCON_INTERFACE_DEFAULT;
+       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);
+
+       ret = iotcon_response_send(response);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_response_send() Fail(%d)", ret);
+               iotcon_response_destroy(response);
+               return ret;
+       }
+
+       iotcon_response_destroy(response);
+
+       return IOTCON_ERROR_NONE;
+}
+
+
+static int _icl_lite_resource_notify(iotcon_lite_resource_h lite_resource)
+{
+       int ret;
+       struct icl_resource resource = {0};
+
+       RETV_IF(NULL == lite_resource, IOTCON_ERROR_INVALID_PARAMETER);
+       if (false == lite_resource->is_observable)
+               return IOTCON_ERROR_NONE;
+
+       resource.handle = lite_resource->handle;
+       resource.sub_id = lite_resource->sub_id;
+
+       ret = iotcon_resource_notify(&resource, NULL, NULL);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_resource_notify() Fail(%d)", ret);
+               return ret;
+       }
+
+       return IOTCON_ERROR_NONE;
+}
+
+
+static void _icl_lite_resource_request_handler(GDBusConnection *connection,
+               const gchar *sender_name,
+               const gchar *object_path,
+               const gchar *interface_name,
+               const gchar *signal_name,
+               GVariant *parameters,
+               gpointer user_data)
+{
+       GVariant *repr_gvar;
+       int ret, request_types;
+       iotcon_representation_h repr;
+       iotcon_state_h recv_state = NULL;
+       GVariantIter *repr_iter, *state_iter;
+       int64_t oic_request_h, oic_resource_h;
+       iotcon_lite_resource_h resource = user_data;
+
+       ret = iotcon_representation_create(&repr);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_representation_create() Fail(%d)", ret);
+               _icl_lite_resource_response_send(NULL, oic_request_h, oic_resource_h,
+                               IOTCON_RESPONSE_RESULT_ERROR);
+               return;
+       }
+
+       ret = iotcon_representation_set_uri_path(repr, resource->uri_path);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_representation_set_uri_path() Fail(%d)", ret);
+               _icl_lite_resource_response_send(repr, oic_request_h, oic_resource_h,
+                               IOTCON_RESPONSE_RESULT_ERROR);
+               iotcon_representation_destroy(repr);
+               return;
+       }
+
+       g_variant_get(parameters, "(ia(qs)a(ss)iiavxx)",
+                       &request_types,
+                       NULL,   // &options,
+                       NULL,   // &query,
+                       NULL,   // &action,
+                       NULL,   // &observer_id,
+                       &repr_iter,
+                       &oic_request_h,
+                       &oic_resource_h);
+
+       switch (request_types) {
+       case IOTCON_REQUEST_GET:
+       case (IOTCON_REQUEST_GET | IOTCON_REQUEST_OBSERVE):
+               break;
+       case IOTCON_REQUEST_PUT:
+               if (FALSE == g_variant_iter_loop(repr_iter, "v", &repr_gvar)) {
+                       ERR("Received representation is empty.");
+                       _icl_lite_resource_response_send(repr, oic_request_h, oic_resource_h,
+                                       IOTCON_RESPONSE_RESULT_ERROR);
+                       iotcon_representation_destroy(repr);
+                       return;
+               }
+
+               g_variant_get(repr_gvar, "(siasa{sv}av)", NULL, NULL, NULL, &state_iter,
+                               NULL);
+
+               ret = iotcon_state_create(&recv_state);
+               if (IOTCON_ERROR_NONE != ret) {
+                       ERR("iotcon_state_create() Fail(%d)", ret);
+                       _icl_lite_resource_response_send(repr, oic_request_h, oic_resource_h,
+                                       IOTCON_RESPONSE_RESULT_ERROR);
+                       iotcon_representation_destroy(repr);
+                       return;
+               }
+
+               icl_state_from_gvariant(recv_state, state_iter);
+
+               ret = _icl_lite_resource_set_state(recv_state, resource->state);
+               if (IOTCON_ERROR_NONE != ret) {
+                       ERR("_icl_lite_resource_set_state() Fail(%d)", ret);
+                       _icl_lite_resource_response_send(repr, oic_request_h, oic_resource_h,
+                                       IOTCON_RESPONSE_RESULT_ERROR);
+                       iotcon_state_destroy(recv_state);
+                       iotcon_representation_destroy(repr);
+                       return;
+               }
+
+               iotcon_state_destroy(recv_state);
+               break;
+       case IOTCON_REQUEST_POST:
+       case IOTCON_REQUEST_DELETE:
+       default:
+               WARN("Not supported request (only GET / PUT / OBSERVE)");
+               ret = _icl_lite_resource_response_send(repr, oic_request_h, oic_resource_h,
+                               IOTCON_RESPONSE_RESULT_FORBIDDEN);
+               if (IOTCON_ERROR_NONE != ret)
+                       ERR("_icl_lite_resource_response_send() Fail(%d)", ret);
+               iotcon_representation_destroy(repr);
+               return;
+       }
+
+       ret = iotcon_representation_set_state(repr, resource->state);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_representation_set_state() Fail(%d)", ret);
+               _icl_lite_resource_response_send(repr, oic_request_h, oic_resource_h,
+                               IOTCON_RESPONSE_RESULT_ERROR);
+               iotcon_representation_destroy(repr);
+               return;
+       }
+
+       ret = _icl_lite_resource_response_send(repr, oic_request_h, oic_resource_h,
+                       IOTCON_RESPONSE_RESULT_OK);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("_icl_lite_resource_response_send() Fail(%d)", ret);
+               iotcon_representation_destroy(repr);
+               return;
+       }
+
+       if (IOTCON_REQUEST_PUT == request_types) {
+               ret = _icl_lite_resource_notify(resource);
+               if (IOTCON_ERROR_NONE != ret)
+                       WARN("_icl_lite_resource_notify() Fail(%d)", ret);
+       }
+
+       iotcon_representation_destroy(repr);
+}
+
+
+static void _icl_lite_resource_conn_cleanup(iotcon_lite_resource_h resource)
+{
+       resource->sub_id = 0;
+
+       if (resource->handle) {
+               resource->handle = 0;
+               return;
+       }
+
+       iotcon_state_destroy(resource->state);
+       free(resource->uri_path);
+       free(resource);
+}
+
+
+/* The length of uri_path should be less than or equal to 36. */
+API int iotcon_lite_resource_create(const char *uri_path,
+               iotcon_resource_types_h res_types,
+               int properties,
+               iotcon_state_h state,
+               iotcon_lite_resource_h *resource_handle)
+{
+       unsigned int sub_id;
+       const gchar **types;
+       GError *error = NULL;
+       int ret, iface, signal_number;
+       iotcon_lite_resource_h resource;
+       char sig_name[IC_DBUS_SIGNAL_LENGTH];
+
+       RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS);
+       RETV_IF(NULL == uri_path, IOTCON_ERROR_INVALID_PARAMETER);
+       RETVM_IF(ICL_URI_PATH_LENGTH_MAX < strlen(uri_path),
+                       IOTCON_ERROR_INVALID_PARAMETER, "Invalid uri_path(%s)", uri_path);
+       RETV_IF(NULL == res_types, IOTCON_ERROR_INVALID_PARAMETER);
+
+       resource = calloc(1, sizeof(struct icl_lite_resource));
+       if (NULL == resource) {
+               ERR("calloc() Fail(%d)", errno);
+               return IOTCON_ERROR_OUT_OF_MEMORY;
+       }
+
+       iface = IOTCON_INTERFACE_DEFAULT;
+
+       types = icl_dbus_resource_types_to_array(res_types);
+       if (NULL == types) {
+               ERR("icl_dbus_resource_types_to_array() Fail");
+               free(resource);
+               return IOTCON_ERROR_INVALID_PARAMETER;
+       }
+
+       signal_number = icl_dbus_generate_signal_number();
+
+       ic_dbus_call_register_resource_sync(icl_dbus_get_object(), uri_path, types, iface,
+                       properties, signal_number, &(resource->handle), NULL, &error);
+       if (error) {
+               ERR("ic_dbus_call_register_resource_sync() Fail(%s)", error->message);
+               ret = icl_dbus_convert_dbus_error(error->code);
+               g_error_free(error);
+               free(types);
+               free(resource);
+               return ret;
+       }
+       free(types);
+
+       if (0 == resource->handle) {
+               ERR("iotcon-daemon Fail");
+               free(resource);
+               return IOTCON_ERROR_IOTIVITY;
+       }
+
+       resource->is_observable = properties & IOTCON_OBSERVABLE;
+       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;
+       }
+
+       snprintf(sig_name, sizeof(sig_name), "%s_%u", IC_DBUS_SIGNAL_REQUEST_HANDLER,
+                       signal_number);
+
+       sub_id = icl_dbus_subscribe_signal(sig_name, resource,
+                       _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);
+               free(resource->uri_path);
+               free(resource);
+               return IOTCON_ERROR_DBUS;
+       }
+
+       resource->sub_id = sub_id;
+
+       *resource_handle = resource;
+
+       return IOTCON_ERROR_NONE;
+}
+
+
+API int iotcon_lite_resource_destroy(iotcon_lite_resource_h resource)
+{
+       int ret;
+       GError *error = NULL;
+
+       RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
+
+       if (0 == resource->handle) { /* disconnected iotcon dbus */
+               WARN("Invalid Resource handle");
+               iotcon_state_destroy(resource->state);
+               free(resource->uri_path);
+               free(resource);
+               return IOTCON_ERROR_NONE;
+       }
+
+       if (NULL == icl_dbus_get_object()) {
+               ERR("icl_dbus_get_object() return NULL");
+               return IOTCON_ERROR_DBUS;
+       }
+
+       ic_dbus_call_unregister_resource_sync(icl_dbus_get_object(), resource->handle, NULL,
+                       &error);
+       if (error) {
+               ERR("ic_dbus_call_unregister_resource_sync() Fail(%s)", error->message);
+               ret = icl_dbus_convert_dbus_error(error->code);
+               g_error_free(error);
+               return ret;
+       }
+
+       resource->handle = 0;
+
+       icl_dbus_unsubscribe_signal(resource->sub_id);
+
+       return IOTCON_ERROR_NONE;
+}
+
+
+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;
+       }
+
+       iotcon_state_destroy(resource->state);
+
+       resource->state = cloned_state;
+
+       ret = _icl_lite_resource_notify(resource);
+       if (IOTCON_ERROR_NONE != ret)
+               WARN("_icl_lite_resource_notify() Fail");
+
+       return IOTCON_ERROR_NONE;
+}
+
+
+API int iotcon_lite_resource_get_state(iotcon_lite_resource_h resource,
+               iotcon_state_h *state)
+{
+       RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
+       RETV_IF(NULL == state, IOTCON_ERROR_INVALID_PARAMETER);
+
+       *state = resource->state;
+
+       return IOTCON_ERROR_NONE;
+}
index 15b12ffdcc3eff7166ab9712731021c2dbc76515..f27626ecbc86a0cad1a70ab44f9f73c22cfb3749 100644 (file)
@@ -135,7 +135,7 @@ static GVariantBuilder* _icl_state_value_to_gvariant_builder(GHashTable *hash)
                        break;
                case IOTCON_TYPE_NONE:
                default:
-                       ERR("Invalid Type");
+                       ERR("Invalid Type(%d)", state_value->type);
                        g_variant_builder_unref(builder);
                        return NULL;
                }
@@ -206,7 +206,7 @@ GVariant* icl_representation_to_gvariant(iotcon_representation_h repr)
 }
 
 
-static void _icl_state_from_gvariant(iotcon_state_h state, GVariantIter *iter)
+void icl_state_from_gvariant(iotcon_state_h state, GVariantIter *iter)
 {
        char *key;
        GVariant *var;
@@ -239,7 +239,7 @@ static void _icl_state_from_gvariant(iotcon_state_h state, GVariantIter *iter)
                } else if (g_variant_is_of_type(var, G_VARIANT_TYPE("a{sv}"))) {
                        GVariantIter *state_iter;
                        g_variant_get(var, "(&a{sv})", &state_iter);
-                       _icl_state_from_gvariant(state_value, state_iter);
+                       icl_state_from_gvariant(state_value, state_iter);
                        value = icl_value_create_state(state_value);
                }
 
@@ -313,7 +313,7 @@ static iotcon_list_h _icl_state_list_from_gvariant(GVariant *var)
                        } else if (g_variant_is_of_type(value, G_VARIANT_TYPE("a{sv}"))) {
                                GVariantIter *state_iter;
                                g_variant_get(value, "(&a{sv})", &state_iter);
-                               _icl_state_from_gvariant(state_value, state_iter);
+                               icl_state_from_gvariant(state_value, state_iter);
                                iotcon_list_add_state(list, state_value, -1);
                        }
                }
@@ -372,7 +372,7 @@ iotcon_representation_h icl_representation_from_gvariant(GVariant *var)
        g_variant_iter_free(resource_types);
 
        /* attribute */
-       _icl_state_from_gvariant(state, repr_gvar);
+       icl_state_from_gvariant(state, repr_gvar);
 
        ret = iotcon_representation_set_state(repr, state);
        if (IOTCON_ERROR_NONE != ret) {
index 584f44f0c2e41245005d4859d1d3d3a2673e7346..1f3eb239b337ef43e1cb608dddac6072cee3b0f3 100644 (file)
@@ -18,6 +18,7 @@
 
 #include <glib.h>
 
+void icl_state_from_gvariant(iotcon_state_h state, GVariantIter *iter);
 GVariant* icl_representation_to_gvariant(iotcon_representation_h repr);
 iotcon_representation_h icl_representation_from_gvariant(GVariant *var);
 
index d1f48f0b43df9a8477d9c730ccea3915a8a4d8a1..ea7935c8ed4309a25d22df47946b0f286c63196e 100644 (file)
@@ -38,6 +38,7 @@ typedef struct icl_presence {
 
 API int iotcon_start_presence(unsigned int time_to_live)
 {
+       FN_CALL;
        int ret;
        GError *error = NULL;
 
diff --git a/lib/icl-remote-resource-caching.c b/lib/icl-remote-resource-caching.c
new file mode 100644 (file)
index 0000000..e6365f2
--- /dev/null
@@ -0,0 +1,251 @@
+/*
+ * 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 <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <glib.h>
+
+#include "iotcon.h"
+#include "ic-dbus.h"
+#include "icl.h"
+#include "icl-dbus.h"
+#include "icl-dbus-type.h"
+#include "icl-remote-resource.h"
+
+static void _caching_get_cb(iotcon_remote_resource_h resource,
+               iotcon_representation_h repr,
+               iotcon_options_h options,
+               int response_result,
+               void *user_data)
+{
+       int ret;
+       iotcon_representation_h cloned_repr;
+
+       RET_IF(NULL == resource);
+       RET_IF(NULL == resource->caching_handle);
+       RET_IF(IOTCON_RESPONSE_RESULT_OK != response_result);
+
+       ret = iotcon_representation_clone(repr, &cloned_repr);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_representation_clone() Fail(%d)", ret);
+               return;
+       }
+
+       if (resource->caching_handle->repr)
+               iotcon_representation_destroy(resource->caching_handle->repr);
+
+       resource->caching_handle->repr = cloned_repr;
+
+       if (resource->caching_handle->cb)
+               resource->caching_handle->cb(resource, repr, resource->caching_handle->user_data);
+}
+
+
+static void _caching_observe_cb(GDBusConnection *connection,
+               const gchar *sender_name,
+               const gchar *object_path,
+               const gchar *interface_name,
+               const gchar *signal_name,
+               GVariant *parameters,
+               gpointer user_data)
+{
+       int ret;
+       iotcon_remote_resource_h resource = user_data;
+
+       RET_IF(NULL == resource);
+       RET_IF(NULL == resource->caching_handle);
+
+       ret = iotcon_remote_resource_get(resource, NULL, _caching_get_cb, NULL);
+       if (IOTCON_ERROR_NONE != ret)
+               ERR("iotcon_remote_resource_get() for caching Fail(%d)", ret);
+}
+
+
+static void _caching_observe_cleanup(iotcon_remote_resource_h resource)
+{
+       resource->caching_handle->observe_handle = 0;
+       resource->caching_handle->observe_sub_id = 0;
+}
+
+
+static int _caching_observer_start(iotcon_remote_resource_h resource)
+{
+       int ret;
+       int64_t handle = 0;
+       unsigned int sub_id = 0;
+
+       RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
+
+       ret = icl_remote_resource_observer_start(resource, IOTCON_OBSERVE, NULL,
+                       _caching_observe_cb, resource, _caching_observe_cleanup, &sub_id, &handle);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("icl_remote_resource_observer_start() Fail(%d)", ret);
+               return ret;
+       }
+
+       resource->caching_handle->observe_sub_id = sub_id;
+       resource->caching_handle->observe_handle = handle;
+
+       return IOTCON_ERROR_NONE;
+}
+
+
+static int _caching_observer_stop(iotcon_remote_resource_h resource)
+{
+       int ret;
+
+       RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
+       if (0 == resource->caching_handle->observe_handle) {
+               ERR("It doesn't have a caching observe handle");
+               return IOTCON_ERROR_INVALID_PARAMETER;
+       }
+
+       ret = icl_remote_resource_observer_stop(resource,
+                       NULL,
+                       resource->caching_handle->observe_handle,
+                       resource->caching_handle->observe_sub_id);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("icl_remote_resource_observer_stop() Fail(%d)", ret);
+               return ret;
+       }
+
+       return IOTCON_ERROR_NONE;
+}
+
+
+static gboolean _caching_get_timer(gpointer user_data)
+{
+       int ret;
+       iotcon_remote_resource_h resource = user_data;
+
+       RETV_IF(NULL == resource, G_SOURCE_REMOVE);
+
+       ret = iotcon_remote_resource_get(resource, NULL, _caching_get_cb, NULL);
+       if (IOTCON_ERROR_NONE != ret)
+               ERR("iotcon_remote_resource_get() for caching Fail(%d)", ret);
+
+       return G_SOURCE_CONTINUE;
+}
+
+
+API int iotcon_remote_resource_start_caching(iotcon_remote_resource_h resource,
+               int caching_interval,
+               iotcon_remote_resource_cached_representation_changed_cb cb,
+               void *user_data)
+{
+       int ret;
+       unsigned int get_timer_id;
+
+       RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
+       RETV_IF(ICL_REMOTE_RESOURCE_MAX_TIME_INTERVAL < caching_interval,
+                       IOTCON_ERROR_INVALID_PARAMETER);
+
+       if (resource->caching_handle) {
+               ERR("Already Start Caching");
+               return IOTCON_ERROR_ALREADY;
+       }
+
+       INFO("Start Caching");
+
+       resource->caching_handle = calloc(1, sizeof(struct icl_remote_resource_caching));
+       if (NULL == resource->caching_handle) {
+               ERR("calloc() Fail(%d)", errno);
+               return IOTCON_ERROR_OUT_OF_MEMORY;
+       }
+
+       if (caching_interval <= 0) {
+               WARN("Because time interval is negative, it sets default time interval.(10 sec)");
+               resource->caching_handle->get_timer_interval
+                       = ICL_REMOTE_RESOURCE_DEFAULT_TIME_INTERVAL;
+       } else {
+               resource->caching_handle->get_timer_interval = caching_interval;
+       }
+
+       _caching_get_timer(resource);
+
+       /* OBSERVE METHOD */
+       ret = _caching_observer_start(resource);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("_caching_observer_start() Fail(%d)", ret);
+               free(resource->caching_handle);
+               resource->caching_handle = NULL;
+               return ret;
+       }
+
+       /* GET METHOD */
+       resource->caching_handle->cb = cb;
+       resource->caching_handle->user_data = user_data;
+
+       get_timer_id = g_timeout_add_seconds(resource->caching_handle->get_timer_interval,
+                       _caching_get_timer, resource);
+       resource->caching_handle->get_timer_id = get_timer_id;
+
+       return IOTCON_ERROR_NONE;
+}
+
+
+API int iotcon_remote_resource_stop_caching(iotcon_remote_resource_h resource)
+{
+       int ret;
+
+       RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
+
+       if (NULL == resource->caching_handle) {
+               ERR("Not Cached");
+               return IOTCON_ERROR_INVALID_PARAMETER;
+       }
+
+       INFO("Stop Caching");
+
+       /* GET METHOD */
+       g_source_remove(resource->caching_handle->get_timer_id);
+
+       if (resource->caching_handle->repr) {
+               iotcon_representation_destroy(resource->caching_handle->repr);
+               resource->caching_handle->repr = NULL;
+       }
+
+       /* OBSERVE METHOD */
+       ret = _caching_observer_stop(resource);
+       if (IOTCON_ERROR_NONE == ret) {
+               ERR("_caching_observer_stop() Fail(%d)", ret);
+               return ret;
+       }
+
+       free(resource->caching_handle);
+       resource->caching_handle = NULL;
+
+       return IOTCON_ERROR_NONE;
+}
+
+
+API int iotcon_remote_resource_get_cached_representation(
+               iotcon_remote_resource_h resource,
+               iotcon_representation_h *representation)
+{
+       RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
+       RETV_IF(NULL == representation, IOTCON_ERROR_INVALID_PARAMETER);
+
+       if (NULL == resource->caching_handle->repr) {
+               ERR("No Caching Representation");
+               return IOTCON_ERROR_NO_DATA;
+       }
+
+       *representation = resource->caching_handle->repr;
+
+       return IOTCON_ERROR_NONE;
+}
+
index 975c24d0debedd5912196884f7bd91e837369754..7398203aed57a323101dfbe7807eb541920308d6 100644 (file)
@@ -18,6 +18,7 @@
 #include <stdlib.h>
 #include <errno.h>
 #include <glib.h>
+#include <gio/gio.h>
 
 #include "iotcon.h"
 #include "ic-utils.h"
@@ -477,25 +478,21 @@ static void _icl_observe_conn_cleanup(icl_on_observe_s *cb_container)
 }
 
 
-API int iotcon_remote_resource_observer_start(iotcon_remote_resource_h resource,
+int icl_remote_resource_observer_start(iotcon_remote_resource_h resource,
                iotcon_observe_type_e observe_type,
                iotcon_query_h query,
-               iotcon_remote_resource_observe_cb cb,
-               void *user_data)
+               GDBusSignalCallback sig_handler,
+               void *cb_container,
+               void *cb_free,
+               unsigned int *sub_id,
+               int64_t *observe_handle)
 {
-       GVariant *arg_query;
-       unsigned int sub_id;
-       GVariant *arg_remote_resource;
        GError *error = NULL;
-       int64_t observe_handle;
-       icl_on_observe_s *cb_container;
        int ret, signal_number;
+       GVariant *arg_query, *arg_remote_resource;
        char signal_name[IC_DBUS_SIGNAL_LENGTH] = {0};
 
        RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS);
-       RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
-       RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
-       RETV_IF(resource->observe_handle || resource->observe_sub_id, IOTCON_ERROR_ALREADY);
 
        signal_number = icl_dbus_generate_signal_number();
 
@@ -503,7 +500,7 @@ API int iotcon_remote_resource_observer_start(iotcon_remote_resource_h resource,
        arg_query = icl_dbus_query_to_gvariant(query);
 
        ic_dbus_call_observer_start_sync(icl_dbus_get_object(), arg_remote_resource,
-                       observe_type, arg_query, signal_number, &observe_handle, NULL, &error);
+                       observe_type, arg_query, signal_number, observe_handle, NULL, &error);
        if (error) {
                ERR("ic_dbus_call_observer_start_sync() Fail(%s)", error->message);
                ret = icl_dbus_convert_dbus_error(error->code);
@@ -513,7 +510,7 @@ API int iotcon_remote_resource_observer_start(iotcon_remote_resource_h resource,
                return ret;
        }
 
-       if (0 == observe_handle) {
+       if (0 == *observe_handle) {
                ERR("iotcon-daemon Fail");
                return IOTCON_ERROR_IOTIVITY;
        }
@@ -521,6 +518,31 @@ API int iotcon_remote_resource_observer_start(iotcon_remote_resource_h resource,
        snprintf(signal_name, sizeof(signal_name), "%s_%u", IC_DBUS_SIGNAL_OBSERVE,
                        signal_number);
 
+       *sub_id = icl_dbus_subscribe_signal(signal_name, cb_container, cb_free, sig_handler);
+       if (0 == *sub_id) {
+               ERR("icl_dbus_subscribe_signal() Fail");
+               return IOTCON_ERROR_DBUS;
+       }
+
+       return IOTCON_ERROR_NONE;
+}
+
+
+API int iotcon_remote_resource_observer_start(iotcon_remote_resource_h resource,
+               iotcon_observe_type_e observe_type,
+               iotcon_query_h query,
+               iotcon_remote_resource_observe_cb cb,
+               void *user_data)
+{
+       int ret;
+       unsigned int sub_id;
+       int64_t observe_handle;
+       icl_on_observe_s *cb_container;
+
+       RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
+       RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
+       RETV_IF(resource->observe_handle || resource->observe_sub_id, IOTCON_ERROR_ALREADY);
+
        cb_container = calloc(1, sizeof(icl_on_observe_s));
        if (NULL == cb_container) {
                ERR("calloc() Fail(%d)", errno);
@@ -531,13 +553,20 @@ API int iotcon_remote_resource_observer_start(iotcon_remote_resource_h resource,
        cb_container->cb = cb;
        cb_container->user_data = user_data;
 
-       sub_id = icl_dbus_subscribe_signal(signal_name, cb_container,
-                       _icl_observe_conn_cleanup, _icl_on_observe_cb);
-       if (0 == sub_id) {
-               ERR("icl_dbus_subscribe_signal() Fail");
+       ret = icl_remote_resource_observer_start(resource,
+                       observe_type,
+                       query,
+                       _icl_on_observe_cb,
+                       cb_container,
+                       _icl_observe_conn_cleanup,
+                       &sub_id,
+                       &observe_handle);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("icl_remote_resource_observer_start() Fail(%d)", ret);
                free(cb_container);
-               return IOTCON_ERROR_DBUS;
+               return ret;
        }
+
        resource->observe_sub_id = sub_id;
        resource->observe_handle = observe_handle;
 
@@ -545,23 +574,19 @@ API int iotcon_remote_resource_observer_start(iotcon_remote_resource_h resource,
 }
 
 
-API int iotcon_remote_resource_observer_stop(iotcon_remote_resource_h resource)
+int icl_remote_resource_observer_stop(iotcon_remote_resource_h resource,
+               iotcon_options_h options, int64_t handle, unsigned int sub_id)
 {
        int ret;
        GError *error = NULL;
        GVariant *arg_options;
 
        RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS);
-       RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
-       if (0 == resource->observe_handle) {
-               ERR("It doesn't have a observe_handle");
-               return IOTCON_ERROR_INVALID_PARAMETER;
-       }
 
-       arg_options = icl_dbus_options_to_gvariant(resource->header_options);
+       arg_options = icl_dbus_options_to_gvariant(options);
 
-       ic_dbus_call_observer_stop_sync(icl_dbus_get_object(), resource->observe_handle,
-                       arg_options, &ret, NULL, &error);
+       ic_dbus_call_observer_stop_sync(icl_dbus_get_object(), handle, arg_options,
+                       &ret, NULL, &error);
        if (error) {
                ERR("ic_dbus_call_observer_stop_sync() Fail(%s)", error->message);
                ret = icl_dbus_convert_dbus_error(error->code);
@@ -575,6 +600,27 @@ API int iotcon_remote_resource_observer_stop(iotcon_remote_resource_h resource)
 
        icl_dbus_unsubscribe_signal(resource->observe_sub_id);
 
-       return ret;
+       return IOTCON_ERROR_NONE;
+}
+
+
+API int iotcon_remote_resource_observer_stop(iotcon_remote_resource_h resource)
+{
+       int ret;
+
+       RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
+       if (0 == resource->observe_handle) {
+               ERR("It doesn't have a observe_handle");
+               return IOTCON_ERROR_INVALID_PARAMETER;
+       }
+
+       ret = icl_remote_resource_observer_stop(resource, resource->header_options,
+                       resource->observe_handle, resource->observe_sub_id);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("icl_remote_resource_observer_stop() Fail(%d)", ret);
+               return ret;
+       }
+
+       return IOTCON_ERROR_NONE;
 }
 
diff --git a/lib/icl-remote-resource-monitoring.c b/lib/icl-remote-resource-monitoring.c
new file mode 100644 (file)
index 0000000..b9f3dc0
--- /dev/null
@@ -0,0 +1,180 @@
+/*
+ * 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 <errno.h>
+#include <glib.h>
+
+#include "iotcon.h"
+#include "icl.h"
+#include "icl-remote-resource.h"
+
+static void _monitoring_get_cb(iotcon_remote_resource_h resource,
+               iotcon_representation_h repr,
+               iotcon_options_h options,
+               int response_result,
+               void *user_data)
+{
+       iotcon_remote_resource_state_e resource_state;
+
+       RET_IF(NULL == resource);
+       RET_IF(NULL == resource->monitoring_handle);
+
+       if (IOTCON_RESPONSE_RESULT_OK <= response_result)
+               resource_state = IOTCON_REMOTE_RESOURCE_STATE_ALIVE;
+       else
+               resource_state = IOTCON_REMOTE_RESOURCE_STATE_LOST_SIGNAL;
+
+       if (resource_state == resource->monitoring_handle->resource_state)
+               return;
+
+       resource->monitoring_handle->resource_state = resource_state;
+
+       if (resource->monitoring_handle->cb)
+               resource->monitoring_handle->cb(resource, resource_state,
+                               resource->monitoring_handle->user_data);
+}
+
+
+static gboolean _monitoring_get_timer(gpointer user_data)
+{
+       int ret;
+       iotcon_remote_resource_h resource = user_data;
+
+       RETV_IF(NULL == resource, G_SOURCE_REMOVE);
+
+       ret = iotcon_remote_resource_get(resource, NULL, _monitoring_get_cb, NULL);
+       if (IOTCON_ERROR_NONE != ret)
+               ERR("iotcon_remote_resource_get() for caching Fail(%d)", ret);
+
+       return G_SOURCE_CONTINUE;
+}
+
+
+static void _monitoring_presence_cb(int result, unsigned int nonce,
+               const char *host_address, void *user_data)
+{
+       unsigned int get_timer_id;
+       iotcon_remote_resource_h resource = user_data;
+
+       RET_IF(NULL == resource);
+       RET_IF(NULL == resource->monitoring_handle);
+
+       g_source_remove(resource->monitoring_handle->get_timer_id);
+
+       _monitoring_get_timer(resource);
+       get_timer_id = g_timeout_add_seconds(resource->caching_handle->get_timer_interval,
+                       _monitoring_get_timer, resource);
+       resource->monitoring_handle->get_timer_id = get_timer_id;
+}
+
+
+API int iotcon_remote_resource_start_monitoring(iotcon_remote_resource_h resource,
+               int monitoring_interval,
+               iotcon_remote_resource_state_changed_cb cb,
+               void *user_data)
+{
+       int ret;
+       char *host_address;
+       unsigned int get_timer_id;
+
+       RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
+       RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
+       RETV_IF(ICL_REMOTE_RESOURCE_MAX_TIME_INTERVAL < monitoring_interval,
+                       IOTCON_ERROR_INVALID_PARAMETER);
+
+       if (resource->monitoring_handle) {
+               ERR("Already Start Monitoring");
+               return IOTCON_ERROR_ALREADY;
+       }
+
+       INFO("Start Monitoring");
+
+       resource->monitoring_handle = calloc(1, sizeof(struct icl_remote_resource_monitoring));
+       if (NULL == resource->monitoring_handle) {
+               ERR("calloc() Fail(%d)", errno);
+               return IOTCON_ERROR_OUT_OF_MEMORY;
+       }
+
+       if (monitoring_interval <= 0) {
+               WARN("Because time interval is negative, it sets default time interval.(10 sec)");
+               resource->monitoring_handle->get_timer_interval
+                       = ICL_REMOTE_RESOURCE_DEFAULT_TIME_INTERVAL;
+       } else {
+               resource->monitoring_handle->get_timer_interval = monitoring_interval;
+       }
+
+       _monitoring_get_timer(resource);
+
+       /* GET METHOD (Resource Presence) */
+       resource->monitoring_handle->cb = cb;
+       resource->monitoring_handle->user_data = user_data;
+
+       get_timer_id = g_timeout_add_seconds(resource->monitoring_handle->get_timer_interval,
+                       _monitoring_get_timer, resource);
+       resource->monitoring_handle->get_timer_id = get_timer_id;
+
+       /* Device Presence */
+       ret = iotcon_remote_resource_get_host(resource, &host_address);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_remote_resource_get_host() Fail(%d)", ret);
+               g_source_remove(resource->monitoring_handle->get_timer_id);
+               free(resource->monitoring_handle);
+               resource->monitoring_handle = NULL;
+               return ret;
+       }
+
+       ret = iotcon_subscribe_presence(host_address, NULL, _monitoring_presence_cb, resource,
+                       &resource->monitoring_handle->presence);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_subscribe_presence() Fail(%d)", ret);
+               g_source_remove(resource->monitoring_handle->get_timer_id);
+               free(resource->monitoring_handle);
+               resource->monitoring_handle = NULL;
+               return ret;
+       }
+
+       return IOTCON_ERROR_NONE;
+}
+
+
+API int iotcon_remote_resource_stop_monitoring(iotcon_remote_resource_h resource)
+{
+       int ret;
+
+       RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
+
+       if (NULL == resource->monitoring_handle) {
+               ERR("Not Monitoring");
+               return IOTCON_ERROR_INVALID_PARAMETER;
+       }
+
+       INFO("Stop Monitoring");
+
+       /* Device Presence */
+       ret = iotcon_unsubscribe_presence(resource->monitoring_handle->presence);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_unsubscribe_presence() Fail(%d)", ret);
+               return ret;
+       }
+
+       /* GET METHOD */
+       g_source_remove(resource->monitoring_handle->get_timer_id);
+
+       free(resource->monitoring_handle);
+       resource->monitoring_handle = NULL;
+
+       return IOTCON_ERROR_NONE;
+}
index d17cdc03f8da934da955bb2e71b5b7858a585c28..29750912da4c25b58fd0e3537b903223f3f731e9 100644 (file)
@@ -203,6 +203,11 @@ API void iotcon_remote_resource_destroy(iotcon_remote_resource_h resource)
 
        icl_remote_resource_crud_stop(resource);
 
+       if (resource->caching_handle)
+               iotcon_remote_resource_stop_caching(resource);
+       if (resource->monitoring_handle)
+               iotcon_remote_resource_stop_monitoring(resource);
+
        free(resource->uri_path);
        free(resource->host);
        free(resource->device_id);
index f14c36307acaab427f180d49fa22e752d3318952..e6a6860e42239b9b5b5d8490b22c58f5afec3157 100644 (file)
 #define __IOT_CONNECTIVITY_MANAGER_LIBRARY_CLIENT_H__
 
 #include <stdint.h>
-#include "iotcon-struct.h"
+#include <glib.h>
+#include <gio/gio.h>
+#include "iotcon.h"
 #include "icl-options.h"
 
+#define ICL_REMOTE_RESOURCE_DEFAULT_TIME_INTERVAL 10 /* 10 sec */
+#define ICL_REMOTE_RESOURCE_MAX_TIME_INTERVAL 3600 /* 60 min */
+
+typedef enum {
+       ICL_DEVICE_STATE_ALIVE,
+       ICL_DEVICE_STATE_LOST_SIGNAL,
+} icl_remote_resource_device_state_e;
+
+struct icl_remote_resource_caching {
+       unsigned int get_timer_id;
+       int get_timer_interval;
+       iotcon_representation_h repr;
+       iotcon_remote_resource_cached_representation_changed_cb cb;
+       void *user_data;
+       unsigned int observe_sub_id;
+       int64_t observe_handle;
+};
+
+struct icl_remote_resource_monitoring {
+       unsigned int get_timer_id;
+       int get_timer_interval;
+       iotcon_remote_resource_state_e resource_state;
+       iotcon_remote_resource_state_changed_cb cb;
+       void *user_data;
+       iotcon_presence_h presence;
+       icl_remote_resource_device_state_e device_state;
+};
+
+typedef struct icl_remote_resource_caching* icl_remote_resource_caching_h;
+typedef struct icl_remote_resource_monitoring* icl_remote_resource_monitoring_h;
+
 struct icl_remote_resource {
        char *uri_path;
        char *host;
@@ -32,9 +65,22 @@ struct icl_remote_resource {
        iotcon_connectivity_type_e conn_type;
        int64_t observe_handle;
        unsigned int observe_sub_id;
+       icl_remote_resource_caching_h caching_handle;
+       icl_remote_resource_monitoring_h monitoring_handle;
 };
 
 void icl_remote_resource_crud_stop(iotcon_remote_resource_h resource);
 
+int icl_remote_resource_observer_start(iotcon_remote_resource_h resource,
+               iotcon_observe_type_e observe_type,
+               iotcon_query_h query,
+               GDBusSignalCallback sig_handler,
+               void *cb_container,
+               void *cb_free,
+               unsigned int *sub_id,
+               int64_t *observe_handle);
+
+int icl_remote_resource_observer_stop(iotcon_remote_resource_h resource,
+               iotcon_options_h options, int64_t handle, unsigned int sub_id);
 
 #endif /* __IOT_CONNECTIVITY_MANAGER_LIBRARY_CLIENT_H__ */
index ab539574aead6c42d1fcc4d443236c54bd570f7e..072b21d47a2fd6c32f4f9d789e83a2431931bf83 100644 (file)
@@ -723,7 +723,7 @@ static int _icl_list_clone_state(iotcon_list_h list, iotcon_list_h ret_list)
 
 
                if (state_val->hash_table) {
-                       g_hash_table_foreach(state_val->hash_table, (GHFunc)icl_state_clone,
+                       g_hash_table_foreach(state_val->hash_table, (GHFunc)icl_state_clone_foreach,
                                        copied_state);
                }
 
index f315396cfd5c8b2cc949797d88f262add97350a5..9c92e3205d18ad1b80f6df9c75bc23ca33440ca1 100644 (file)
@@ -451,3 +451,29 @@ int icl_state_set_value(iotcon_state_h state, const char *key, iotcon_value_h va
 
        return IOTCON_ERROR_NONE;
 }
+
+
+int icl_state_clone(iotcon_state_h src, iotcon_state_h *dest)
+{
+       int ret;
+
+       iotcon_state_h cloned_state = NULL;
+
+       RETV_IF(NULL == src, IOTCON_ERROR_INVALID_PARAMETER);
+       RETV_IF(NULL == dest, IOTCON_ERROR_INVALID_PARAMETER);
+
+       if (src->hash_table) {
+               ret = iotcon_state_create(&cloned_state);
+               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);
+       }
+
+       *dest = cloned_state;
+
+       return IOTCON_ERROR_NONE;
+}
index 4b99808de51e1872e45d7f8f50ca224657a34186..30302cd78035e0a43e89c53f7171b4e358d1f035 100644 (file)
@@ -27,4 +27,6 @@ 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);
+
 #endif /* __IOT_CONNECTIVITY_MANAGER_LIBRARY_REPRESENTATION_STATE_H__ */
index f70e23fe2e6bea9ca1bcd13e53b258b12a36e23b..8f80ae7eb8e2886036deacc720dbab15841b46e5 100644 (file)
@@ -326,7 +326,7 @@ API int iotcon_state_get_keys_count(iotcon_state_h state, unsigned int *count)
 }
 
 
-void icl_state_clone(char *key, iotcon_value_h src_val, iotcon_state_h dest_state)
+void icl_state_clone_foreach(char *key, iotcon_value_h src_val, iotcon_state_h dest_state)
 {
        FN_CALL;
        int type, ret;
@@ -379,7 +379,7 @@ void icl_state_clone(char *key, iotcon_value_h src_val, iotcon_state_h dest_stat
                        return;
                }
 
-               g_hash_table_foreach(child_state->hash_table, (GHFunc)icl_state_clone,
+               g_hash_table_foreach(child_state->hash_table, (GHFunc)icl_state_clone_foreach,
                                copied_state);
 
                value = icl_value_create_state(copied_state);
@@ -450,14 +450,22 @@ API int iotcon_representation_clone(const iotcon_representation_h src,
 
        ori_state = src->state;
        if (ori_state->hash_table) {
-               g_hash_table_foreach(ori_state->hash_table, (GHFunc)icl_state_clone,
+               ret = iotcon_state_create(&cloned_state);
+               if (IOTCON_ERROR_NONE != ret) {
+                       ERR("iotcon_state_create() Fail");
+                       iotcon_representation_destroy(cloned_repr);
+                       return ret;
+               }
+               g_hash_table_foreach(ori_state->hash_table, (GHFunc)icl_state_clone_foreach,
                                cloned_state);
                ret = iotcon_representation_set_state(cloned_repr, cloned_state);
                if (IOTCON_ERROR_NONE != ret) {
                        ERR("iotcon_representation_set_state() Fail");
+                       iotcon_state_destroy(cloned_state);
                        iotcon_representation_destroy(cloned_repr);
                        return ret;
                }
+               iotcon_state_destroy(cloned_state);
        }
 
        *dest = cloned_repr;
index cbc8285f547f0b01887ef2099dc5c5150dbde035..728ce190910a131a1c27651597cdf6cf385f8bd6 100644 (file)
@@ -39,6 +39,6 @@ struct icl_state_s {
 
 void icl_representation_inc_ref_count(iotcon_representation_h val);
 
-void icl_state_clone(char *key, iotcon_value_h src_val, iotcon_state_h dest_state);
+void icl_state_clone_foreach(char *key, iotcon_value_h src_val, iotcon_state_h dest_state);
 
 #endif /* __IOT_CONNECTIVITY_MANAGER_LIBRARY_REPRESENTATION_H__ */
index 2fdfb8aebc5cbcf49a24cbe38031183b2c1ac576..aae9c7d74a7596e91359237bdfdfca18942c7c26 100644 (file)
 #include "icl-resource.h"
 #include "icl-payload.h"
 
-/**
- * @brief The maximum length of uri_path path which can be held in a resource.
- *
- * @since_tizen 3.0
- */
-#define ICL_URI_PATH_LENGTH_MAX 36
-
-
 static void _icl_request_handler(GDBusConnection *connection,
                const gchar *sender_name,
                const gchar *object_path,
@@ -191,7 +183,7 @@ API int iotcon_resource_create(const char *uri_path,
        if (NULL == types) {
                ERR("icl_dbus_resource_types_to_array() Fail");
                free(resource);
-               return IOTCON_ERROR_INVALID_PARAMETER;
+               return IOTCON_ERROR_OUT_OF_MEMORY;
        }
 
        signal_number = icl_dbus_generate_signal_number();
index 18526224c66da8e14ce76f2bdb24a6d8ae67f314..dd8738fdabdb32f78127d9709e0c5568302ee191 100644 (file)
  */
 #define ICL_CONTAINED_RESOURCES_MAX 5
 
+/**
+ * @brief The maximum length of uri_path which can be held in a resource.
+ *
+ * @since_tizen 3.0
+ */
+#define ICL_URI_PATH_LENGTH_MAX 36
 
 struct icl_notify_msg {
        int error_code;
index 12662c26d397d419743733386225913610d0e24b..3bcf0e32fb02f18651c6612997f1984ec3958761 100644 (file)
@@ -213,6 +213,16 @@ typedef enum {
        IOTCON_TIZEN_INFO_TIZEN_DEVICE_ID, /**< Indicates unique identifier for tizen device */
 } iotcon_tizen_info_e;
 
+/**
+ * @brief Enumeration for states of remote resource.
+ *
+ * @since_tizen 3.0
+ */
+typedef enum {
+       IOTCON_REMOTE_RESOURCE_STATE_ALIVE, /**< Indicates remote resource is alive */
+       IOTCON_REMOTE_RESOURCE_STATE_LOST_SIGNAL, /**< Indicates remote resource is lost */
+} iotcon_remote_resource_state_e;
+
 /**
  * @}
  */
index a4a16dbc59f465f29d27da1014760de68b53d0b0..899e306793927fddbc3d2f8c1f34e1c270aecee4 100644 (file)
  * @{
  */
 
+/**
+ * @brief The handle of lite resource.
+ * @details iotcon_lite_resource_h is an opaque data structure to represent registered\n
+ * resource by server. A resource has host, uri_path, resource types, and internal\n
+ * handle. If observable attribute of resource is true, client can observe\n
+ * this resource. When client request by GET / PUT / OBSERVE functions, handler will be\n
+ * invoked, internally. Then, it sends the response to client, automatically.
+ *
+ * @since_tizen 3.0
+ */
+typedef struct icl_lite_resource* iotcon_lite_resource_h;
+
 /**
  * @ingroup CAPI_IOT_CONNECTIVITY_REPRESENTATION_MODULE
  * @brief The handle of list which is consist of iotcon_value_h type values.
@@ -1356,6 +1368,57 @@ typedef struct icl_tizen_info* iotcon_tizen_info_h;
 int iotcon_tizen_info_get_property(iotcon_tizen_info_h tizen_info,
                                iotcon_tizen_info_e property, char **value);
 
+/**
+ * @brief Update state into the lite resource handle
+ *
+ * @since_tizen 3.0
+ *
+ * @param[in] resource The handle of the lite resource
+ * @param[in] state The state handle to update
+ *
+ * @return 0 on success, otherwise a negative error value.
+ * @retval #IOTCON_ERROR_NONE  Successful
+ * @retval #IOTCON_ERROR_INVALID_PARAMETER  Invalid parameter
+ * @retval #IOTCON_ERROR_OUT_OF_MEMORY  Out of memory
+ *
+ * @see iotcon_lite_resource_get_state()
+ */
+int iotcon_lite_resource_update_state(iotcon_lite_resource_h resource,
+               iotcon_state_h state);
+
+/**
+ * @brief Get state from the lite resource handle
+ *
+ * @since_tizen 3.0
+ *
+ * @param[in] resource The handle of the lite resource
+ * @param[out] state The state handle of the lite resource
+ *
+ * @return 0 on success, otherwise a negative error value.
+ * @retval #IOTCON_ERROR_NONE  Successful
+ * @retval #IOTCON_ERROR_INVALID_PARAMETER  Invalid parameter
+ *
+ * @see iotcon_lite_resource_update_state()
+ */
+int iotcon_lite_resource_get_state(iotcon_lite_resource_h resource,
+               iotcon_state_h *state);
+
+/**
+ * @brief Get cached representation from the remote resource
+ *
+ * @since_tizen 3.0
+ *
+ * @param[in] resource The handle of the remote resource
+ * @param[out] representation The handle of the representation
+ *
+ * @return 0 on success, otherwise a negative error value.
+ * @retval #IOTCON_ERROR_NONE  Successful
+ * @retval #IOTCON_ERROR_INVALID_PARAMETER  Invalid parameter
+ * @retval #IOTCON_ERROR_NO_DATA  No data
+ */
+int iotcon_remote_resource_get_cached_representation(
+               iotcon_remote_resource_h resource,
+               iotcon_representation_h *representation);
 /**
  * @}
  */
index ed180c7e12c7eb64d3599e57349586b52c9424eb..fc3b7ee465253120b8246348838853433601e4e1 100644 (file)
@@ -629,7 +629,8 @@ int iotcon_unsubscribe_presence(iotcon_presence_h presence_handle);
  *
  * @see iotcon_find_resource()
  */
-typedef void (*iotcon_found_resource_cb)(iotcon_remote_resource_h resource, int result, void *user_data);
+typedef void (*iotcon_found_resource_cb)(iotcon_remote_resource_h resource, int result,
+               void *user_data);
 
 /**
  * @brief Finds resources.
@@ -1062,6 +1063,210 @@ typedef void (*iotcon_remote_resource_delete_cb)(iotcon_remote_resource_h resour
 int iotcon_remote_resource_delete(iotcon_remote_resource_h resource,
                iotcon_remote_resource_delete_cb cb, void *user_data);
 
+/**
+ * @brief Create a lite resource handle and registers the resource in server
+ * @details Registers a resource specified by @a uri_path, @a res_types, @a state which have
+ * @a properties in Iotcon server.\n
+ * When client requests some operations, it send a response to client, automatically.\n
+ *
+ * @since_tizen 3.0
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/internet
+ *
+ * @remarks @a uri_path length must be less than or equal 36.\n
+ * You must unregister resource by calling iotcon_resource_destroy()
+ * if resource is no longer needed.
+ *
+ * @param[in] uri_path The URI path of the resource.
+ * @param[in] res_types The list of type of the resource.
+ * @param[in] properties The property of the resource.
+ * @param[in] state The state handle to set.
+ * @param[out] resource_handle The handle of the resource
+ *
+ * @return 0 on success, otherwise a negative error value.
+ * @retval #IOTCON_ERROR_NONE  Successful
+ * @retval #IOTCON_ERROR_INVALID_PARAMETER  Invalid parameter
+ * @retval #IOTCON_ERROR_IOTIVITY  Iotivity errors
+ * @retval #IOTCON_ERROR_DBUS  Dbus errors
+ * @retval #IOTCON_ERROR_OUT_OF_MEMORY  Out of memory
+ * @retval #IOTCON_ERROR_PERMISSION_DENIED Permission denied
+ *
+ * @post When the resource receive CRUD request, iotcon_request_handler_cb() will be called.
+ *
+ * @see iotcon_lite_resource_destroy()
+ */
+int iotcon_lite_resource_create(const char *uri_path,
+               iotcon_resource_types_h res_types,
+               int properties,
+               iotcon_state_h state,
+               iotcon_lite_resource_h *resource_handle);
+
+/**
+ * @brief Destroy the resource and releases its data.
+ *
+ * @since_tizen 3.0
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/internet
+ *
+ * @remarks When a normal variable is used, there are only dbus error and permission\n
+ * denied error. If the errors of this API are not handled, then you must check\n
+ * whether dbus is running and an application have the privileges for the API.
+ *
+ * @param[in] resource The handle of the lite resource to be unregistered
+ *
+ * @return 0 on success, otherwise a negative error value.
+ * @retval #IOTCON_ERROR_NONE  Successful
+ * @retval #IOTCON_ERROR_INVALID_PARAMETER  Invalid parameter
+ * @retval #IOTCON_ERROR_DBUS  Dbus error
+ * @retval #IOTCON_ERROR_PERMISSION_DENIED Permission denied
+ *
+ * @see iotcon_lite_resource_create()
+ */
+int iotcon_lite_resource_destroy(iotcon_lite_resource_h resource);
+
+/**
+ * @brief Specifies the type of function passed to iotcon_remote_resource_start_caching().
+ *
+ * @since_tizen 3.0
+ *
+ * @param[in] resource The handle of the remote resource
+ * @param[in] representation The handle of the representation
+ * @param[in] user_data The user data to pass to the function
+ *
+ * @pre The callback must be registered using iotcon_remote_resource_start_caching()\n
+ *
+ * @see iotcon_remote_resource_start_caching()
+ * @see iotcon_remote_resource_stop_caching()
+ */
+typedef void (*iotcon_remote_resource_cached_representation_changed_cb)(
+               iotcon_remote_resource_h resource,
+               iotcon_representation_h representation,
+               void *user_data);
+
+/**
+ * @brief Start caching of a remote resource.
+ * @details Use this function to start caching the resource's attribute data (state).\n
+ * Default caching time interval is 10 seconds.
+ * Internally, it operates GET method, periodically, and it observes the remote resource.
+ *
+ * @since_tizen 3.0
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/internet
+ *
+ * @param[in] resource The handle of the remote resource to be cached
+ * @param[in] caching_interval Seconds for caching time interval.\n
+ * If value is 0, then it sets 10 seconds(default caching time).
+ * @param[in] cb The callback function to add into callback list
+ * @param[in] user_data The user data to pass to the callback function
+ *
+ * @return 0 on success, otherwise a negative error value.
+ * @retval #IOTCON_ERROR_NONE  Successful
+ * @retval #IOTCON_ERROR_DBUS  Dbus error
+ * @retval #IOTCON_ERROR_SYSTEM System error
+ * @retval #IOTCON_ERROR_PERMISSION_DENIED Permission denied
+ * @retval #IOTCON_ERROR_INVALID_PARAMETER  Invalid parameter
+ * @retval #IOTCON_ERROR_ALREADY Already done
+ * @retval #IOTCON_ERROR_OUT_OF_MEMORY  Out of memory
+ *
+ * @see iotcon_remote_resource_stop_caching()
+ * @see iotcon_remote_resource_cached_representation_changed_cb()
+ */
+int iotcon_remote_resource_start_caching(iotcon_remote_resource_h resource,
+               int caching_interval,
+               iotcon_remote_resource_cached_representation_changed_cb cb,
+               void *user_data);
+
+/**
+ * @brief Stop caching of a remote resource.
+ * @details Use this function to stop caching the resource's attribute data (state).\n
+ *
+ * @since_tizen 3.0
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/internet
+ *
+ * @param[in] resource The handle of the remote resource
+ *
+ * @return 0 on success, otherwise a negative error value.
+ * @retval #IOTCON_ERROR_NONE  Successful
+ * @retval #IOTCON_ERROR_INVALID_PARAMETER  Invalid parameter
+ * @retval #IOTCON_ERROR_DBUS  Dbus error
+ * @retval #IOTCON_ERROR_SYSTEM System error
+ * @retval #IOTCON_ERROR_PERMISSION_DENIED Permission denied
+ *
+ * @see iotcon_remote_resource_start_caching()
+ * @see iotcon_remote_resource_cached_representation_changed_cb()
+ */
+int iotcon_remote_resource_stop_caching(iotcon_remote_resource_h resource);
+
+/**
+ * @brief Specifies the type of function passed to iotcon_remote_resource_start_monitoring().
+ *
+ * @since_tizen 3.0
+ *
+ * @param[in] resource The handle of the remote resource
+ * @param[in] state The state of the remote resource
+ * @param[in] user_data The user data to pass to the function
+ *
+ * @pre The callback must be registered using iotcon_remote_resource_start_monitoring()\n
+ *
+ * @see iotcon_remote_resource_start_monitoring()
+ * @see iotcon_remote_resource_stop_monitoring()
+ */
+typedef void (*iotcon_remote_resource_state_changed_cb)(iotcon_remote_resource_h resource,
+               iotcon_remote_resource_state_e state, void *user_data);
+
+/**
+ * @brief Start monitoring of a remote resource.
+ * @details When remote resource's state are changed, registered callbacks will be called\n
+ * in turn. Default monitoring time interval is 10 seconds.
+ * Internally, it operates GET method, periodically, and it subscribes the devices's presence.
+ *
+ * @since_tizen 3.0
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/internet
+ *
+ * @param[in] resource The handle of the remote resource
+ * @param[in] monitoring_interval Seconds for monitoring time interval.\n
+ * If value is 0, then it sets 10 seconds(default monitoring time).
+ * @param[in] cb The callback function to add into callback list
+ * @param[in] user_data The user data to pass to the callback function
+ *
+ * @return 0 on success, otherwise a negative error value.
+ * @retval #IOTCON_ERROR_NONE  Successful
+ * @retval #IOTCON_ERROR_INVALID_PARAMETER  Invalid parameter
+ * @retval #IOTCON_ERROR_ALREADY  Already done
+ * @retval #IOTCON_ERROR_OUT_OF_MEMORY  Out of memory
+ *
+ * @see iotcon_remote_resource_stop_monitoring()
+ * @see iotcon_remote_resource_state_changed_cb()
+ */
+int iotcon_remote_resource_start_monitoring(iotcon_remote_resource_h resource,
+               int monitoring_interval,
+               iotcon_remote_resource_state_changed_cb cb,
+               void *user_data);
+
+/**
+ * @brief Stop monitoring.
+ * @details Use this function to stop monitoring the remote resource.
+ *
+ * @since_tizen 3.0
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/internet
+ *
+ * @param[in] resource The handle of the remote resource
+ *
+ * @return 0 on success, otherwise a negative error value.
+ * @retval #IOTCON_ERROR_NONE  Successful
+ * @retval #IOTCON_ERROR_INVALID_PARAMETER  Invalid parameter
+ * @retval #IOTCON_ERROR_DBUS  Dbus error
+ * @retval #IOTCON_ERROR_SYSTEM System error
+ * @retval #IOTCON_ERROR_PERMISSION_DENIED Permission denied
+ *
+ * @see iotcon_remote_resource_start_monitoring()
+ * @see iotcon_remote_resource_state_changed_cb()
+ */
+int iotcon_remote_resource_stop_monitoring(iotcon_remote_resource_h resource);
+
 /**
  * @}
  */
index fb816b34dfceb7d92bdf17560c873909f814cce2..91d52773b59aa9a715b429ad00bbd9ad7d4df877 100644 (file)
@@ -147,8 +147,10 @@ systemctl daemon-reload
 %files test
 %manifest %{name}-test.manifest
 %defattr(-,root,root,-)
-%{_bindir}/crud-test-client
-%{_bindir}/crud-test-server
-%{_bindir}/device-test-client
-%{_bindir}/repr-test-client
-%{_bindir}/repr-test-server
+%{_bindir}/iotcon-test-basic-client
+%{_bindir}/iotcon-test-basic-server
+%{_bindir}/iotcon-test-device-client
+%{_bindir}/iotcon-test-repr-client
+%{_bindir}/iotcon-test-repr-server
+%{_bindir}/iotcon-test-encap-client
+%{_bindir}/iotcon-test-encap-server
index f3ed3ccaf847d4cb476f5c7c8bc283d454805237..12d5b92f73c42aac7f472e59d3d6be95ffc066bc 100644 (file)
@@ -1,39 +1,53 @@
 LINK_DIRECTORIES(${CMAKE_BINARY_DIR})
 INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/lib/include)
 
-SET(CRUD_TEST_CLIENT "crud-test-client")
-SET(CRUD_TEST_SERVER "crud-test-server")
-SET(CRUD_TEST_CLIENT_SRCS "crud-test-client.c")
-SET(CRUD_TEST_SERVER_SRCS "crud-test-server.c")
+SET(IOTCON_TEST_BASIC_CLIENT "iotcon-test-basic-client")
+SET(IOTCON_TEST_BASIC_SERVER "iotcon-test-basic-server")
+SET(IOTCON_TEST_BASIC_CLIENT_SRCS "iotcon-test-basic-client.c")
+SET(IOTCON_TEST_BASIC_SERVER_SRCS "iotcon-test-basic-server.c")
 
-SET(DEVICE_TEST_CLIENT "device-test-client")
-SET(DEVICE_TEST_CLIENT_SRCS "device-test-client.c")
+SET(IOTCON_TEST_DEVICE_CLIENT "iotcon-test-device-client")
+SET(IOTCON_TEST_DEVICE_CLIENT_SRCS "iotcon-test-device-client.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")
+SET(IOTCON_TEST_REPR_CLIENT "iotcon-test-repr-client")
+SET(IOTCON_TEST_REPR_SERVER "iotcon-test-repr-server")
+SET(IOTCON_TEST_REPR_CLIENT_SRCS "iotcon-test-repr-client.c")
+SET(IOTCON_TEST_REPR_SERVER_SRCS "iotcon-test-repr-server.c")
+
+SET(IOTCON_TEST_ENCAP_CLIENT "iotcon-test-encap-client")
+SET(IOTCON_TEST_ENCAP_SERVER "iotcon-test-encap-server")
+SET(IOTCON_TEST_ENCAP_CLIENT_SRCS "iotcon-test-encap-client.c")
+SET(IOTCON_TEST_ENCAP_SERVER_SRCS "iotcon-test-encap-server.c")
 
 pkg_check_modules(test_pkgs REQUIRED dlog glib-2.0)
 INCLUDE_DIRECTORIES(${test_pkgs_INCLUDE_DIRS})
 LINK_DIRECTORIES(${test_pkgs_LIBRARY_DIRS})
 
-ADD_EXECUTABLE(${CRUD_TEST_CLIENT} ${CRUD_TEST_CLIENT_SRCS})
-TARGET_LINK_LIBRARIES(${CRUD_TEST_CLIENT} ${test_pkgs_LIBRARIES} ${CLIENT})
-INSTALL(TARGETS ${CRUD_TEST_CLIENT} DESTINATION ${BIN_INSTALL_DIR})
+ADD_EXECUTABLE(${IOTCON_TEST_BASIC_CLIENT} ${IOTCON_TEST_BASIC_CLIENT_SRCS})
+TARGET_LINK_LIBRARIES(${IOTCON_TEST_BASIC_CLIENT} ${test_pkgs_LIBRARIES} ${CLIENT})
+INSTALL(TARGETS ${IOTCON_TEST_BASIC_CLIENT} DESTINATION ${BIN_INSTALL_DIR})
+
+ADD_EXECUTABLE(${IOTCON_TEST_BASIC_SERVER} ${IOTCON_TEST_BASIC_SERVER_SRCS})
+TARGET_LINK_LIBRARIES(${IOTCON_TEST_BASIC_SERVER} ${test_pkgs_LIBRARIES} ${CLIENT})
+INSTALL(TARGETS ${IOTCON_TEST_BASIC_SERVER} DESTINATION ${BIN_INSTALL_DIR})
+
+ADD_EXECUTABLE(${IOTCON_TEST_DEVICE_CLIENT} ${IOTCON_TEST_DEVICE_CLIENT_SRCS})
+TARGET_LINK_LIBRARIES(${IOTCON_TEST_DEVICE_CLIENT} ${test_pkgs_LIBRARIES} ${CLIENT})
+INSTALL(TARGETS ${IOTCON_TEST_DEVICE_CLIENT} DESTINATION ${BIN_INSTALL_DIR})
+
+ADD_EXECUTABLE(${IOTCON_TEST_REPR_CLIENT} ${IOTCON_TEST_REPR_CLIENT_SRCS})
+TARGET_LINK_LIBRARIES(${IOTCON_TEST_REPR_CLIENT} ${test_pkgs_LIBRARIES} ${CLIENT})
+INSTALL(TARGETS ${IOTCON_TEST_REPR_CLIENT} DESTINATION ${BIN_INSTALL_DIR})
 
-ADD_EXECUTABLE(${CRUD_TEST_SERVER} ${CRUD_TEST_SERVER_SRCS})
-TARGET_LINK_LIBRARIES(${CRUD_TEST_SERVER} ${test_pkgs_LIBRARIES} ${CLIENT})
-INSTALL(TARGETS ${CRUD_TEST_SERVER} DESTINATION ${BIN_INSTALL_DIR})
+ADD_EXECUTABLE(${IOTCON_TEST_REPR_SERVER} ${IOTCON_TEST_REPR_SERVER_SRCS})
+TARGET_LINK_LIBRARIES(${IOTCON_TEST_REPR_SERVER} ${test_pkgs_LIBRARIES} ${CLIENT})
+INSTALL(TARGETS ${IOTCON_TEST_REPR_SERVER} DESTINATION ${BIN_INSTALL_DIR})
 
-ADD_EXECUTABLE(${DEVICE_TEST_CLIENT} ${DEVICE_TEST_CLIENT_SRCS})
-TARGET_LINK_LIBRARIES(${DEVICE_TEST_CLIENT} ${test_pkgs_LIBRARIES} ${CLIENT})
-INSTALL(TARGETS ${DEVICE_TEST_CLIENT} DESTINATION ${BIN_INSTALL_DIR})
+ADD_EXECUTABLE(${IOTCON_TEST_ENCAP_CLIENT} ${IOTCON_TEST_ENCAP_CLIENT_SRCS})
+TARGET_LINK_LIBRARIES(${IOTCON_TEST_ENCAP_CLIENT} ${test_pkgs_LIBRARIES} ${CLIENT})
+INSTALL(TARGETS ${IOTCON_TEST_ENCAP_CLIENT} 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(${IOTCON_TEST_ENCAP_SERVER} ${IOTCON_TEST_ENCAP_SERVER_SRCS})
+TARGET_LINK_LIBRARIES(${IOTCON_TEST_ENCAP_SERVER} ${test_pkgs_LIBRARIES} ${CLIENT})
+INSTALL(TARGETS ${IOTCON_TEST_ENCAP_SERVER} 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})
diff --git a/test/crud-test-client.c b/test/crud-test-client.c
deleted file mode 100644 (file)
index 7498997..0000000
+++ /dev/null
@@ -1,463 +0,0 @@
-/*
- * 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 <stdbool.h>
-#include <stdlib.h>
-#include <glib.h>
-
-#include <iotcon.h>
-#include "test.h"
-
-static char *door_resource_device_id;
-static GList *device_id_list;
-
-static const char* const door_uri_path = "/a/door";
-
-static void _on_observe(iotcon_remote_resource_h resource,
-               iotcon_representation_h recv_repr,
-               iotcon_options_h header_options,
-               int response_result,
-               int sequence_number,
-               void *user_data)
-{
-       INFO("_on_observe");
-
-       static int i = 0;
-       i++;
-
-       if (2 == i) {
-               iotcon_remote_resource_observer_stop(resource);
-               iotcon_remote_resource_destroy(resource);
-       }
-}
-
-static void _on_delete(iotcon_remote_resource_h resource, iotcon_options_h header_options,
-               int response_result, void *user_data)
-{
-       int ret;
-       iotcon_remote_resource_h door_resource = user_data;
-
-       RETM_IF(IOTCON_RESPONSE_RESULT_OK != response_result
-                       && IOTCON_RESPONSE_RESULT_RESOURCE_DELETED != response_result,
-                       "_on_delete Response error(%d)", response_result);
-       INFO("DELETE request was successful");
-
-       /* delete callback operations */
-
-       ret = iotcon_remote_resource_observer_start(door_resource, IOTCON_OBSERVE_ALL, NULL,
-                       _on_observe, NULL);
-       if (IOTCON_ERROR_NONE != ret)
-               ERR("iotcon_remote_resource_observer_start() Fail(%d)", ret);
-
-       iotcon_remote_resource_destroy(resource);
-}
-
-static void _on_post(iotcon_remote_resource_h resource, iotcon_representation_h recv_repr,
-               iotcon_options_h header_options, int response_result, void *user_data)
-{
-       int ret, ifaces = 0;
-       iotcon_state_h recv_state;
-       char *host, *created_uri_path;
-       iotcon_resource_types_h types = NULL;
-       iotcon_remote_resource_h new_door_resource, door_resource;
-
-       RETM_IF(IOTCON_RESPONSE_RESULT_OK != response_result
-                       && IOTCON_RESPONSE_RESULT_RESOURCE_CREATED != response_result,
-                       "_on_post Response error(%d)", response_result);
-       INFO("POST request was successful");
-
-       ret = iotcon_representation_get_state(recv_repr, &recv_state);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_representation_get_state() Fail(%d)", ret);
-               return;
-       }
-
-       ret = iotcon_state_get_str(recv_state, "createduripath", &created_uri_path);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_state_get_str() Fail(%d)", ret);
-               return;
-       }
-       DBG("New resource created : %s", created_uri_path);
-
-       ret = iotcon_remote_resource_get_host(resource, &host);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_remote_resource_get_host() Fail(%d)", ret);
-               return;
-       }
-
-       ret = iotcon_remote_resource_get_types(resource, &types);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_remote_resource_get_types() Fail(%d)", ret);
-               return;
-       }
-
-       ret = iotcon_remote_resource_get_interfaces(resource, &ifaces);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_remote_resource_get_interfaces() Fail(%d)", ret);
-               return;
-       }
-
-       ret = iotcon_remote_resource_create(host, created_uri_path, true, types, ifaces,
-                       &new_door_resource);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_remote_resource_create() Fail(%d)", ret);
-               return;
-       }
-
-       ret = iotcon_remote_resource_clone(resource, &door_resource);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_remote_resource_clone() Fail(%d)", ret);
-               iotcon_remote_resource_destroy(new_door_resource);
-               return;
-       }
-
-       ret = iotcon_remote_resource_delete(new_door_resource, _on_delete, door_resource);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_remote_resource_delete() Fail(%d)", ret);
-               iotcon_remote_resource_destroy(door_resource);
-               iotcon_remote_resource_destroy(new_door_resource);
-               return;
-       }
-
-       iotcon_remote_resource_destroy(resource);
-}
-
-static void _on_put(iotcon_remote_resource_h resource, iotcon_representation_h recv_repr,
-               iotcon_options_h header_options, int response_result, void *user_data)
-{
-       int ret;
-       iotcon_representation_h send_repr;
-
-       RETM_IF(IOTCON_RESPONSE_RESULT_OK != response_result, "_on_put Response error(%d)",
-                       response_result);
-       INFO("PUT request was successful");
-
-       ret = iotcon_representation_create(&send_repr);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_representation_create() Fail(%d)", ret);
-               return;
-       }
-
-       /* send POST request */
-       ret = iotcon_remote_resource_post(resource, send_repr, NULL, _on_post, NULL);
-       if (IOTCON_ERROR_NONE != ret)
-               ERR("iotcon_remote_resource_post() Fail(%d)", ret);
-
-       iotcon_representation_destroy(send_repr);
-}
-
-static void _on_get(iotcon_remote_resource_h resource, iotcon_representation_h recv_repr,
-               iotcon_options_h header_options, int response_result, void *user_data)
-{
-       int ret;
-       bool opened = true;
-       iotcon_representation_h send_repr;
-       iotcon_state_h send_state;
-       iotcon_state_h recv_state = NULL;
-
-       RETM_IF(IOTCON_RESPONSE_RESULT_OK != response_result, "_on_get Response error(%d)",
-                       response_result);
-       INFO("GET request was successful");
-
-       ret = iotcon_representation_get_state(recv_repr, &recv_state);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_representation_get_state() Fail(%d)", ret);
-               return;
-       }
-
-       ret = iotcon_state_get_bool(recv_state, "opened", &opened);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_state_get_bool() Fail(%d)", ret);
-               return;
-       }
-
-       ret = iotcon_representation_create(&send_repr);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_representation_create() Fail(%d)", ret);
-               return;
-       }
-
-       ret = iotcon_state_create(&send_state);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_state_create() Fail(%d)", ret);
-               iotcon_representation_destroy(send_repr);
-               return;
-       }
-
-       ret = iotcon_state_set_bool(send_state, "opened", true);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_state_set_bool() Fail(%d)", ret);
-               iotcon_state_destroy(send_state);
-               iotcon_representation_destroy(send_repr);
-               return;
-       }
-
-       ret = iotcon_representation_set_state(send_repr, send_state);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_representation_set_state() Fail(%d)", ret);
-               iotcon_state_destroy(send_state);
-               iotcon_representation_destroy(send_repr);
-               return;
-       }
-
-       iotcon_state_destroy(send_state);
-
-       /* send PUT request */
-       ret = iotcon_remote_resource_put(resource, send_repr, NULL, _on_put, NULL);
-       if (IOTCON_ERROR_NONE != ret)
-               ERR("iotcon_remote_resource_put() Fail(%d)", ret);
-
-       iotcon_representation_destroy(send_repr);
-}
-
-static int _get_res_type_cb(const char *string, void *user_data)
-{
-       char *resource_uri_path = user_data;
-
-       DBG("[%s] resource type : %s", resource_uri_path, string);
-
-       return IOTCON_FUNC_CONTINUE;
-}
-
-static void _presence_handler(int result, unsigned int nonce,
-               const char *host_address, void *user_data)
-{
-       INFO("_presence_handler");
-       INFO("result : %d", result);
-       INFO("nonce : %d", nonce);
-       INFO("host_address : %s", host_address);
-}
-
-static int _device_id_compare(const void *a, const void *b)
-{
-       return strcmp(a, b);
-}
-
-static void _get_tizen_info(iotcon_tizen_info_h info, int response_result,
-               void *user_data)
-{
-       int ret;
-       char *device_name = NULL;
-       char *tizen_device_id = NULL;
-
-       RETM_IF(IOTCON_RESPONSE_RESULT_OK != response_result,
-                       "_get_tizen_info Response error(%d)", response_result);
-
-       ret = iotcon_tizen_info_get_property(info, IOTCON_TIZEN_INFO_DEVICE_NAME,
-                       &device_name);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_tizen_info_get_property() Fail(%d)", ret);
-               return;
-       }
-
-       ret = iotcon_tizen_info_get_property(info, IOTCON_TIZEN_INFO_TIZEN_DEVICE_ID,
-                       &tizen_device_id);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_tizen_info_get_property() Fail(%d)", ret);
-               return;
-       }
-
-       INFO("This is Tizen Device.");
-       INFO("- Device name : %s", device_name);
-       INFO("- Tizen Device ID : %s", tizen_device_id);
-}
-
-static void _found_resource(iotcon_remote_resource_h resource, int result, void *user_data)
-{
-       GList *node;
-       char *resource_host;
-       char *resource_uri_path;
-       char *resource_device_id;
-       int ret, resource_interfaces;
-       iotcon_presence_h presence_handle;
-       iotcon_resource_types_h resource_types;
-       iotcon_remote_resource_h resource_clone = NULL;
-
-       RETM_IF(IOTCON_ERROR_NONE != result, "Invalid result(%d)", result);
-
-       if (NULL == resource)
-               return;
-
-       INFO("===== resource found =====");
-
-       /* get the resource URI */
-       ret = iotcon_remote_resource_get_uri_path(resource, &resource_uri_path);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_remote_resource_get_uri_path() Fail(%d)", ret);
-               return;
-       }
-
-       /* get the device unique id.
-        * this is unique per-server independent on how it was discovered. */
-       ret = iotcon_remote_resource_get_device_id(resource, &resource_device_id);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_remote_resource_get_device_id() Fail(%d)", ret);
-               return;
-       }
-       DBG("[%s] resource device id : %s", resource_uri_path, resource_device_id);
-
-       node = g_list_find_custom(device_id_list, resource_device_id, _device_id_compare);
-
-       if (node && TEST_STR_EQUAL == strcmp(door_uri_path, resource_uri_path)) {
-               DBG("uri_path \"%s\" already found. skip !", resource_uri_path);
-               return;
-       }
-
-       door_resource_device_id = strdup(resource_device_id);
-       if (NULL == door_resource_device_id) {
-               ERR("strdup(door_resource_device_id) Fail");
-               return;
-       }
-
-       device_id_list = g_list_append(device_id_list, door_resource_device_id);
-
-       /* get the resource host address */
-       ret = iotcon_remote_resource_get_host(resource, &resource_host);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_remote_resource_get_host() Fail(%d)", ret);
-               device_id_list = g_list_remove(device_id_list, door_resource_device_id);
-               free(door_resource_device_id);
-               return;
-       }
-       DBG("[%s] resource host : %s", resource_uri_path, resource_host);
-
-       /* get the resource interfaces */
-       ret = iotcon_remote_resource_get_interfaces(resource, &resource_interfaces);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_remote_resource_get_interfaces() Fail(%d)", ret);
-               device_id_list = g_list_remove(device_id_list, door_resource_device_id);
-               free(door_resource_device_id);
-               return;
-       }
-       if (IOTCON_INTERFACE_DEFAULT & resource_interfaces)
-               DBG("[%s] resource interface : DEFAULT_INTERFACE", resource_uri_path);
-       if (IOTCON_INTERFACE_LINK & resource_interfaces)
-               DBG("[%s] resource interface : LINK_INTERFACE", resource_uri_path);
-       if (IOTCON_INTERFACE_BATCH & resource_interfaces)
-               DBG("[%s] resource interface : BATCH_INTERFACE", resource_uri_path);
-       if (IOTCON_INTERFACE_GROUP & resource_interfaces)
-               DBG("[%s] resource interface : GROUP_INTERFACE", resource_uri_path);
-
-       /* get the resource types */
-       ret = iotcon_remote_resource_get_types(resource, &resource_types);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_remote_resource_get_types() Fail(%d)", ret);
-               device_id_list = g_list_remove(device_id_list, door_resource_device_id);
-               free(door_resource_device_id);
-               return;
-       }
-
-       ret = iotcon_resource_types_foreach(resource_types, _get_res_type_cb,
-                       resource_uri_path);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_resource_types_foreach() Fail(%d)", ret);
-               device_id_list = g_list_remove(device_id_list, door_resource_device_id);
-               free(door_resource_device_id);
-               return;
-       }
-
-       /* get tizen info */
-       ret = iotcon_get_tizen_info(resource_host, _get_tizen_info, NULL);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_get_tizen_info() Fail(%d)", ret);
-               device_id_list = g_list_remove(device_id_list, door_resource_device_id);
-               free(door_resource_device_id);
-               return;
-       }
-
-       ret = iotcon_subscribe_presence(resource_host, "core.door", _presence_handler, NULL,
-                       &presence_handle);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_subscribe_presence() Fail(%d)", ret);
-               device_id_list = g_list_remove(device_id_list, door_resource_device_id);
-               free(door_resource_device_id);
-               return;
-       }
-
-       if (TEST_STR_EQUAL == strcmp(door_uri_path, resource_uri_path)) {
-               iotcon_query_h query;
-
-               ret = iotcon_query_create(&query);
-               if (IOTCON_ERROR_NONE != ret) {
-                       ERR("iotcon_query_create() Fail(%d)", ret);
-                       device_id_list = g_list_remove(device_id_list, door_resource_device_id);
-                       free(door_resource_device_id);
-                       return;
-               }
-
-               ret = iotcon_query_add(query, "query_key", "query_value");
-               if (IOTCON_ERROR_NONE != ret) {
-                       ERR("iotcon_query_add() Fail(%d)", ret);
-                       iotcon_query_destroy(query);
-                       device_id_list = g_list_remove(device_id_list, door_resource_device_id);
-                       free(door_resource_device_id);
-                       return;
-               }
-
-               ret = iotcon_remote_resource_clone(resource, &resource_clone);
-               if (IOTCON_ERROR_NONE != ret) {
-                       ERR("iotcon_remote_resource_clone() Fail(%d)", ret);
-                       iotcon_query_destroy(query);
-                       device_id_list = g_list_remove(device_id_list, door_resource_device_id);
-                       free(door_resource_device_id);
-                       return;
-               }
-
-               /* send GET Request */
-               ret = iotcon_remote_resource_get(resource_clone, query, _on_get, NULL);
-               if (IOTCON_ERROR_NONE != ret)
-                       ERR("iotcon_remote_resource_get() Fail(%d)", ret);
-
-               iotcon_query_destroy(query);
-       }
-
-       device_id_list = g_list_remove(device_id_list, door_resource_device_id);
-       free(door_resource_device_id);
-}
-
-int main(int argc, char **argv)
-{
-       FN_CALL;
-       int ret;
-       GMainLoop *loop;
-
-       loop = g_main_loop_new(NULL, FALSE);
-
-       /* iotcon open */
-       ret = iotcon_open();
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_open() Fail(%d)", ret);
-               return -1;
-       }
-
-       /* find door typed resources */
-       ret = iotcon_find_resource(IOTCON_MULTICAST_ADDRESS, "core.door", &_found_resource,
-                       NULL);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_find_resource() Fail(%d)", ret);
-               iotcon_close();
-               return -1;
-       }
-
-       g_main_loop_run(loop);
-       g_main_loop_unref(loop);
-
-       g_list_free_full(device_id_list, free);
-
-       /* iotcon close */
-       iotcon_close();
-
-       return 0;
-}
diff --git a/test/crud-test-server.c b/test/crud-test-server.c
deleted file mode 100644 (file)
index ef586e9..0000000
+++ /dev/null
@@ -1,569 +0,0 @@
-/*
- * 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 <iotcon-internal.h>
-#include "test.h"
-
-/* Door Resource */
-typedef struct _door_resource_s {
-       bool state;
-       char *uri_path;
-       char *type;
-       int ifaces;
-       int properties;
-       iotcon_resource_h handle;
-       iotcon_observers_h observers;
-       iotcon_representation_h repr;
-} door_resource_s;
-
-static bool resource_created;
-
-static void _request_handler(iotcon_resource_h resource, iotcon_request_h request,
-               void *user_data);
-
-static int _set_door_resource(door_resource_s *door)
-{
-       int ret;
-
-       door->state = false;
-
-       door->uri_path = strdup("/a/door");
-       if (NULL == door->uri_path) {
-               ERR("strdup(/a/door) Fail");
-               return -1;
-       }
-
-       door->type = strdup("core.door");
-       if (NULL == door->type) {
-               ERR("strdup(core.door) Fail");
-               free(door->uri_path);
-               return -1;
-       }
-
-       door->ifaces = IOTCON_INTERFACE_DEFAULT;
-       door->properties = IOTCON_DISCOVERABLE;
-
-       ret = iotcon_observers_create(&door->observers);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_observers_create() Fail");
-               free(door->type);
-               free(door->uri_path);
-               return -1;
-       }
-
-       return 0;
-}
-
-static void _free_door_resource(door_resource_s *door)
-{
-       iotcon_observers_destroy(door->observers);
-       free(door->type);
-       free(door->uri_path);
-}
-
-static void _check_door_state(door_resource_s door)
-{
-       if (false == door.state)
-               INFO("[Door] closed.");
-       else
-               INFO("[Door] opened.");
-}
-
-static iotcon_resource_h _create_door_resource(char *uri_path, char *type, int ifaces,
-               int properties, void *user_data)
-{
-       int ret;
-       iotcon_resource_h handle;
-       iotcon_resource_types_h resource_types;
-
-       ret = iotcon_resource_types_create(&resource_types);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_resource_types_create() Fail(%d)", ret);
-               return NULL;
-       }
-
-       ret = iotcon_resource_types_add(resource_types, type);
-       if (IOTCON_ERROR_NONE != ret) {
-               iotcon_resource_types_destroy(resource_types);
-               ERR("iotcon_resource_types_add() Fail(%d)", ret);
-               return NULL;
-       }
-
-       /* register door resource */
-       ret = iotcon_resource_create(uri_path, resource_types, ifaces, properties,
-                       _request_handler, user_data, &handle);
-       if (IOTCON_ERROR_NONE != ret) {
-               iotcon_resource_types_destroy(resource_types);
-               ERR("iotcon_resource_create() Fail");
-               return NULL;
-       }
-
-       iotcon_resource_types_destroy(resource_types);
-
-       return handle;
-}
-
-static int _send_response(iotcon_response_h response, iotcon_representation_h repr,
-               iotcon_response_result_e result)
-{
-       int ret;
-
-       ret = iotcon_response_set_result(response, result);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_response_set_result() Fail(%d)", ret);
-               return -1;
-       }
-
-       ret = iotcon_response_set_representation(response, repr);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_response_set_representation() Fail(%d)", ret);
-               return -1;
-       }
-
-       /* send Representation to the client */
-       ret = iotcon_response_send(response);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_response_send() Fail(%d)", ret);
-               return -1;
-       }
-
-       return 0;
-}
-
-static void _request_handler_get(door_resource_s *door, iotcon_response_h response)
-{
-       int ret;
-       iotcon_representation_h resp_repr;
-       iotcon_state_h resp_state;
-       INFO("GET request");
-
-       /* create a door Representation */
-       ret = iotcon_representation_create(&resp_repr);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_representation_create() Fail(%d)", ret);
-               return;
-       }
-
-       /* create a door state */
-       ret = iotcon_state_create(&resp_state);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_state_create() Fail(%d)", ret);
-               iotcon_representation_destroy(resp_repr);
-               return;
-       }
-
-       ret = iotcon_representation_set_uri_path(resp_repr, door->uri_path);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_representation_set_uri_path() Fail(%d)", ret);
-               iotcon_state_destroy(resp_state);
-               iotcon_representation_destroy(resp_repr);
-               return;
-       }
-
-       ret = iotcon_state_set_bool(resp_state, "opened", door->state);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_state_set_bool() Fail(%d)", ret);
-               iotcon_state_destroy(resp_state);
-               iotcon_representation_destroy(resp_repr);
-               return;
-       }
-
-       ret = iotcon_representation_set_state(resp_repr, resp_state);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_representation_set_state() Fail(%d)", ret);
-               iotcon_state_destroy(resp_state);
-               iotcon_representation_destroy(resp_repr);
-               return;
-       }
-
-       iotcon_state_destroy(resp_state);
-
-       ret = _send_response(response, resp_repr, IOTCON_RESPONSE_RESULT_OK);
-       if (0 != ret)
-               ERR("_send_response() Fail(%d)", ret);
-
-       iotcon_representation_destroy(resp_repr);
-}
-
-static void _request_handler_put(door_resource_s *door, iotcon_request_h request,
-               iotcon_response_h response)
-{
-       int ret;
-       bool bval;
-       iotcon_representation_h req_repr, resp_repr;
-       iotcon_state_h req_state, resp_state;;
-       INFO("PUT request");
-
-       ret = iotcon_request_get_representation(request, &req_repr);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_request_get_representation() Fail(%d)", ret);
-               return;
-       }
-
-       ret = iotcon_representation_get_state(req_repr, &req_state);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_representation_get_state() Fail(%d)", ret);
-               return;
-       }
-
-       ret = iotcon_state_get_bool(req_state, "opened", &bval);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_state_get_bool() Fail(%d)", ret);
-               return;
-       }
-
-       door->state = bval;
-
-       _check_door_state(*door);
-
-       ret = iotcon_representation_create(&resp_repr);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_representation_create() Fail(%d)", ret);
-               return;
-       }
-
-       ret = iotcon_representation_set_uri_path(resp_repr, door->uri_path);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_representation_set_uri_path() Fail(%d)", ret);
-               iotcon_representation_destroy(resp_repr);
-               return;
-       }
-
-       ret = iotcon_state_create(&resp_state);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_state_create() Fail(%d)", ret);
-               iotcon_representation_destroy(resp_repr);
-               return;
-       }
-
-       ret = iotcon_state_set_bool(resp_state, "opened", door->state);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_state_set_bool() Fail(%d)", ret);
-               iotcon_state_destroy(resp_state);
-               iotcon_representation_destroy(resp_repr);
-               return;
-       }
-
-       ret = iotcon_representation_set_state(resp_repr, resp_state);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_representation_set_state() Fail(%d)", ret);
-               iotcon_state_destroy(resp_state);
-               iotcon_representation_destroy(resp_repr);
-               return;
-       }
-
-       iotcon_state_destroy(resp_state);
-
-       ret = _send_response(response, resp_repr, IOTCON_RESPONSE_RESULT_OK);
-       if (0 != ret)
-               ERR("_send_response() Fail(%d)", ret);
-
-       iotcon_representation_destroy(resp_repr);
-}
-
-static gboolean _notifier(gpointer user_data)
-{
-       int ret;
-       static int i = 0;
-       iotcon_notimsg_h msg;
-       door_resource_s *door;
-       iotcon_representation_h repr;
-
-       door = user_data;
-
-       if ((5 == i++) || !(door->observers))
-               return FALSE;
-
-       INFO("NOTIFY!");
-       ret = iotcon_representation_create(&repr);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_representation_create() Fail(%d)", ret);
-               return FALSE;
-       }
-
-       ret = iotcon_notimsg_create(repr, IOTCON_INTERFACE_DEFAULT, &msg);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_notimsg_create() Fail(%d)", ret);
-               iotcon_representation_destroy(repr);
-               return FALSE;
-       }
-
-       ret = iotcon_resource_notify(door->handle, msg, door->observers);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_resource_notify() Fail(%d)", ret);
-               iotcon_notimsg_destroy(msg);
-               iotcon_representation_destroy(repr);
-               return FALSE;
-       }
-
-       iotcon_notimsg_destroy(msg);
-       iotcon_representation_destroy(repr);
-
-       return TRUE;
-}
-
-static void _request_handler_post(door_resource_s *door, iotcon_response_h response)
-{
-       int ret;
-       iotcon_state_h resp_state;
-       iotcon_representation_h resp_repr = NULL;
-       iotcon_resource_h new_door_handle;
-       INFO("POST request");
-
-       if (resource_created) {
-               INFO("Resource is already created");
-               return;
-       }
-
-       new_door_handle = _create_door_resource("/a/door1", door->type,
-                       IOTCON_INTERFACE_DEFAULT, (IOTCON_DISCOVERABLE | IOTCON_OBSERVABLE), NULL);
-       if (NULL == new_door_handle) {
-               ERR("_create_door_resource() Fail");
-               return;
-       }
-       resource_created = true;
-
-       /* send information that new resource was created */
-       ret = iotcon_representation_create(&resp_repr);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_representation_create() Fail(%d)", ret);
-               return;
-       }
-
-       ret = iotcon_state_create(&resp_state);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_state_create() Fail(%d)", ret);
-               iotcon_representation_destroy(resp_repr);
-               return;
-       }
-
-       ret = iotcon_state_set_str(resp_state, "createduripath", "/a/door1");
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_state_set_str() Fail(%d)", ret);
-               iotcon_state_destroy(resp_state);
-               iotcon_representation_destroy(resp_repr);
-               return;
-       }
-
-       ret = iotcon_representation_set_state(resp_repr, resp_state);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_representation_set_state() Fail(%d)", ret);
-               iotcon_state_destroy(resp_state);
-               iotcon_representation_destroy(resp_repr);
-               return;
-       }
-
-       iotcon_state_destroy(resp_state);
-
-       ret = _send_response(response, resp_repr, IOTCON_RESPONSE_RESULT_RESOURCE_CREATED);
-       if (0 != ret) {
-               ERR("_send_response() Fail(%d)", ret);
-               iotcon_representation_destroy(resp_repr);
-               return;
-       }
-
-       iotcon_representation_destroy(resp_repr);
-
-       /* add observe */
-       g_timeout_add_seconds(5, _notifier, door);
-}
-
-static void _request_handler_delete(iotcon_resource_h resource,
-               iotcon_response_h response)
-{
-       int ret;
-       iotcon_representation_h resp_repr = NULL;
-       iotcon_response_result_e result = IOTCON_RESPONSE_RESULT_OK;
-       INFO("DELETE request");
-
-       ret = iotcon_resource_destroy(resource);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_unregiser_resource() Fail(%d)", ret);
-               return;
-       }
-
-       result = IOTCON_RESPONSE_RESULT_RESOURCE_DELETED;
-
-       ret = iotcon_representation_create(&resp_repr);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_representation_create() Fail(%d)", ret);
-               return;
-       }
-
-       ret = _send_response(response, resp_repr, result);
-       if (0 != ret)
-               ERR("_send_response() Fail(%d)", ret);
-
-       iotcon_representation_destroy(resp_repr);
-}
-
-static int _query_cb(const char *key, const char *value, void *user_data)
-{
-       INFO("key : %s, value : %s", key, value);
-
-       return IOTCON_FUNC_CONTINUE;
-}
-
-static void _request_handler(iotcon_resource_h resource, iotcon_request_h request,
-               void *user_data)
-{
-       door_resource_s *door;
-       iotcon_query_h query;
-       iotcon_response_h response = NULL;
-       int ret, types, observer_id, observer_action;
-
-       RET_IF(NULL == request);
-
-       door = user_data;
-
-       ret = iotcon_request_get_query(request, &query);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_request_get_query() Fail(%d)", ret);
-               return;
-       }
-       if (query)
-               iotcon_query_foreach(query, _query_cb, NULL);
-
-       ret = iotcon_request_get_types(request, &types);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_request_get_types() Fail(%d)", ret);
-               return;
-       }
-
-       ret = iotcon_response_create(request, &response);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_response_create() Fail(%d)", ret);
-               return;
-       }
-
-       if (IOTCON_REQUEST_GET & types)
-               _request_handler_get(door, response);
-
-       else if (IOTCON_REQUEST_PUT & types)
-               _request_handler_put(door, request, response);
-
-       else if (IOTCON_REQUEST_POST & types)
-               _request_handler_post(door, response);
-
-       else if (IOTCON_REQUEST_DELETE & types)
-               _request_handler_delete(resource, response);
-
-       iotcon_response_destroy(response);
-
-       if (IOTCON_REQUEST_OBSERVE & types) {
-               ret = iotcon_request_get_observer_action(request, &observer_action);
-               if (IOTCON_ERROR_NONE != ret) {
-                       ERR("iotcon_request_get_observer_action() Fail(%d)", ret);
-                       return;
-               }
-
-               if (IOTCON_OBSERVE_REGISTER == observer_action) {
-                       ret = iotcon_request_get_observer_id(request, &observer_id);
-                       if (IOTCON_ERROR_NONE != ret) {
-                               ERR("iotcon_request_get_observer_id() Fail(%d)", ret);
-                               return;
-                       }
-                       ret = iotcon_observers_add(door->observers, observer_id);
-               } else if (IOTCON_OBSERVE_DEREGISTER == observer_action) {
-                       ret = iotcon_request_get_observer_id(request, &observer_id);
-                       if (IOTCON_ERROR_NONE != ret) {
-                               ERR("iotcon_request_get_observer_id() Fail(%d)", ret);
-                               return;
-                       }
-                       ret = iotcon_observers_remove(door->observers, observer_id);
-                       if (IOTCON_ERROR_NONE != ret) {
-                               ERR("iotcon_observers_remove() Fail(%d)", ret);
-                               return;
-                       }
-               }
-       }
-}
-
-static gboolean _presence_timer(gpointer user_data)
-{
-       static int i = 0;
-       i++;
-       if (i % 2)
-               iotcon_start_presence(10);
-       else
-               iotcon_stop_presence();
-
-       if (4 == i)
-               return FALSE;
-
-       return TRUE;
-}
-
-int main(int argc, char **argv)
-{
-       FN_CALL;
-       int ret;
-       GMainLoop *loop;
-       door_resource_s my_door = {0};
-
-       loop = g_main_loop_new(NULL, FALSE);
-
-       /* iotcon open */
-       ret = iotcon_open();
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_open() Fail(%d)", ret);
-               return -1;
-       }
-
-       /* set local door resource */
-       ret = _set_door_resource(&my_door);
-       if (0 != ret) {
-               ERR("_set_door_resource() Fail");
-               iotcon_close();
-               return -1;
-       }
-
-       /* add resource options */
-       my_door.ifaces |= IOTCON_INTERFACE_BATCH;
-       my_door.properties |= IOTCON_OBSERVABLE;
-
-       /* add presence */
-       g_timeout_add_seconds(10, _presence_timer, NULL);
-
-       /* create new door resource */
-       my_door.handle = _create_door_resource(my_door.uri_path, my_door.type, my_door.ifaces,
-                       my_door.properties, &my_door);
-       if (NULL == my_door.handle) {
-               ERR("_create_door_resource() Fail");
-               _free_door_resource(&my_door);
-               iotcon_close();
-               return -1;
-       }
-
-       _check_door_state(my_door);
-
-       g_main_loop_run(loop);
-       g_main_loop_unref(loop);
-
-       iotcon_resource_destroy(my_door.handle);
-
-       _free_door_resource(&my_door);
-
-       /* iotcon close */
-       iotcon_close();
-
-       return 0;
-}
diff --git a/test/device-test-client.c b/test/device-test-client.c
deleted file mode 100644 (file)
index 88d3fc8..0000000
+++ /dev/null
@@ -1,205 +0,0 @@
-/*
- * 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 <glib.h>
-#include <iotcon.h>
-#include "test.h"
-
-static void _get_device_info(iotcon_device_info_h info, int result, void *user_data)
-{
-       int ret;
-       char *device_name = NULL;
-       char *spec_ver = NULL;
-       char *device_id = NULL;
-       char *data_model_ver = NULL;
-
-       RETM_IF(IOTCON_ERROR_NONE != result, "Invalid result (%d)", result);
-
-       ret = iotcon_device_info_get_property(info, IOTCON_DEVICE_INFO_NAME, &device_name);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_device_info_get_property() Fail(%d)", ret);
-               return;
-       }
-
-       ret = iotcon_device_info_get_property(info, IOTCON_DEVICE_INFO_SPEC_VER, &spec_ver);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_device_info_get_property() Fail(%d)", ret);
-               return;
-       }
-
-       ret = iotcon_device_info_get_property(info, IOTCON_DEVICE_INFO_ID, &device_id);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_device_info_get_property() Fail(%d)", ret);
-               return;
-       }
-
-       ret = iotcon_device_info_get_property(info, IOTCON_DEVICE_INFO_DATA_MODEL_VER,
-                       &data_model_ver);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_device_info_get_property() Fail(%d)", ret);
-               return;
-       }
-
-       INFO("Get Device Info");
-       INFO("device_name : %s", device_name);
-       INFO("spec_version : %s", spec_ver);
-       INFO("device_id : %s", device_id);
-       INFO("data_model_version : %s", data_model_ver);
-}
-
-static void _get_platform_info(iotcon_platform_info_h info, int result, void *user_data)
-{
-       int ret;
-       char *platform_id = NULL;
-       char *manuf_name = NULL;
-       char *manuf_url = NULL;
-       char *model_number = NULL;
-       char *date_of_manuf = NULL;
-       char *platform_ver = NULL;
-       char *os_ver = NULL;
-       char *hardware_ver = NULL;
-       char *firmware_ver = NULL;
-       char *support_url = NULL;
-       char *system_time = NULL;
-
-       RETM_IF(IOTCON_ERROR_NONE != result, "Invalid result (%d)", result);
-
-       ret = iotcon_platform_info_get_property(info, IOTCON_PLATFORM_INFO_ID, &platform_id);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_platform_info_get_property() Fail(%d)", ret);
-               return;
-       }
-
-       ret = iotcon_platform_info_get_property(info, IOTCON_PLATFORM_INFO_MANUF_NAME,
-                       &manuf_name);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_platform_info_get_property() Fail(%d)", ret);
-               return;
-       }
-
-       ret = iotcon_platform_info_get_property(info, IOTCON_PLATFORM_INFO_MANUF_URL,
-                       &manuf_url);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_platform_info_get_property() Fail(%d)", ret);
-               return;
-       }
-
-       ret = iotcon_platform_info_get_property(info, IOTCON_PLATFORM_INFO_MODEL_NUMBER,
-                       &model_number);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_platform_info_get_property() Fail(%d)", ret);
-               return;
-       }
-
-       ret = iotcon_platform_info_get_property(info, IOTCON_PLATFORM_INFO_DATE_OF_MANUF,
-                       &date_of_manuf);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_platform_info_get_property() Fail(%d)", ret);
-               return;
-       }
-
-       ret = iotcon_platform_info_get_property(info, IOTCON_PLATFORM_INFO_PLATFORM_VER,
-                       &platform_ver);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_platform_info_get_property() Fail(%d)", ret);
-               return;
-       }
-
-       ret = iotcon_platform_info_get_property(info, IOTCON_PLATFORM_INFO_OS_VER, &os_ver);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_platform_info_get_property() Fail(%d)", ret);
-               return;
-       }
-
-       ret = iotcon_platform_info_get_property(info, IOTCON_PLATFORM_INFO_HARDWARE_VER,
-                       &hardware_ver);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_platform_info_get_property() Fail(%d)", ret);
-               return;
-       }
-
-       ret = iotcon_platform_info_get_property(info, IOTCON_PLATFORM_INFO_FIRMWARE_VER,
-                       &firmware_ver);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_platform_info_get_property() Fail(%d)", ret);
-               return;
-       }
-
-       ret = iotcon_platform_info_get_property(info, IOTCON_PLATFORM_INFO_SUPPORT_URL,
-                       &support_url);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_platform_info_get_property() Fail(%d)", ret);
-               return;
-       }
-
-       ret = iotcon_platform_info_get_property(info, IOTCON_PLATFORM_INFO_SYSTEM_TIME,
-                       &system_time);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_platform_info_get_property() Fail(%d)", ret);
-               return;
-       }
-
-       INFO("Get Platform Info");
-       INFO("platform_id : %s", platform_id);
-       INFO("manuf_name : %s", manuf_name);
-       INFO("manuf_url : %s", manuf_url);
-       INFO("model_number : %s", model_number);
-       INFO("date_of_manufacture : %s", date_of_manuf);
-       INFO("platform_ver : %s", platform_ver);
-       INFO("os_ver : %s", os_ver);
-       INFO("hardware_ver : %s", hardware_ver);
-       INFO("firmware_ver : %s", firmware_ver);
-       INFO("support_url : %s", support_url);
-       INFO("system_time : %s", system_time);
-}
-
-int main()
-{
-       int ret;
-       GMainLoop *loop;
-
-       loop = g_main_loop_new(NULL, FALSE);
-
-       /* iotcon open */
-       ret = iotcon_open();
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_open() Fail(%d)", ret);
-               return -1;
-       }
-
-       ret = iotcon_get_device_info(IOTCON_MULTICAST_ADDRESS, _get_device_info, NULL);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_get_device_info() Fail(%d)", ret);
-               iotcon_close();
-               return -1;
-       }
-
-       ret = iotcon_get_platform_info(IOTCON_MULTICAST_ADDRESS, _get_platform_info, NULL);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_get_platform_info() Fail(%d)", ret);
-               iotcon_close();
-               return -1;
-       }
-
-       g_main_loop_run(loop);
-
-       g_main_loop_unref(loop);
-
-       /* iotcon close */
-       iotcon_close();
-
-       return 0;
-}
diff --git a/test/iotcon-test-basic-client.c b/test/iotcon-test-basic-client.c
new file mode 100644 (file)
index 0000000..7498997
--- /dev/null
@@ -0,0 +1,463 @@
+/*
+ * 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 <stdbool.h>
+#include <stdlib.h>
+#include <glib.h>
+
+#include <iotcon.h>
+#include "test.h"
+
+static char *door_resource_device_id;
+static GList *device_id_list;
+
+static const char* const door_uri_path = "/a/door";
+
+static void _on_observe(iotcon_remote_resource_h resource,
+               iotcon_representation_h recv_repr,
+               iotcon_options_h header_options,
+               int response_result,
+               int sequence_number,
+               void *user_data)
+{
+       INFO("_on_observe");
+
+       static int i = 0;
+       i++;
+
+       if (2 == i) {
+               iotcon_remote_resource_observer_stop(resource);
+               iotcon_remote_resource_destroy(resource);
+       }
+}
+
+static void _on_delete(iotcon_remote_resource_h resource, iotcon_options_h header_options,
+               int response_result, void *user_data)
+{
+       int ret;
+       iotcon_remote_resource_h door_resource = user_data;
+
+       RETM_IF(IOTCON_RESPONSE_RESULT_OK != response_result
+                       && IOTCON_RESPONSE_RESULT_RESOURCE_DELETED != response_result,
+                       "_on_delete Response error(%d)", response_result);
+       INFO("DELETE request was successful");
+
+       /* delete callback operations */
+
+       ret = iotcon_remote_resource_observer_start(door_resource, IOTCON_OBSERVE_ALL, NULL,
+                       _on_observe, NULL);
+       if (IOTCON_ERROR_NONE != ret)
+               ERR("iotcon_remote_resource_observer_start() Fail(%d)", ret);
+
+       iotcon_remote_resource_destroy(resource);
+}
+
+static void _on_post(iotcon_remote_resource_h resource, iotcon_representation_h recv_repr,
+               iotcon_options_h header_options, int response_result, void *user_data)
+{
+       int ret, ifaces = 0;
+       iotcon_state_h recv_state;
+       char *host, *created_uri_path;
+       iotcon_resource_types_h types = NULL;
+       iotcon_remote_resource_h new_door_resource, door_resource;
+
+       RETM_IF(IOTCON_RESPONSE_RESULT_OK != response_result
+                       && IOTCON_RESPONSE_RESULT_RESOURCE_CREATED != response_result,
+                       "_on_post Response error(%d)", response_result);
+       INFO("POST request was successful");
+
+       ret = iotcon_representation_get_state(recv_repr, &recv_state);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_representation_get_state() Fail(%d)", ret);
+               return;
+       }
+
+       ret = iotcon_state_get_str(recv_state, "createduripath", &created_uri_path);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_state_get_str() Fail(%d)", ret);
+               return;
+       }
+       DBG("New resource created : %s", created_uri_path);
+
+       ret = iotcon_remote_resource_get_host(resource, &host);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_remote_resource_get_host() Fail(%d)", ret);
+               return;
+       }
+
+       ret = iotcon_remote_resource_get_types(resource, &types);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_remote_resource_get_types() Fail(%d)", ret);
+               return;
+       }
+
+       ret = iotcon_remote_resource_get_interfaces(resource, &ifaces);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_remote_resource_get_interfaces() Fail(%d)", ret);
+               return;
+       }
+
+       ret = iotcon_remote_resource_create(host, created_uri_path, true, types, ifaces,
+                       &new_door_resource);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_remote_resource_create() Fail(%d)", ret);
+               return;
+       }
+
+       ret = iotcon_remote_resource_clone(resource, &door_resource);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_remote_resource_clone() Fail(%d)", ret);
+               iotcon_remote_resource_destroy(new_door_resource);
+               return;
+       }
+
+       ret = iotcon_remote_resource_delete(new_door_resource, _on_delete, door_resource);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_remote_resource_delete() Fail(%d)", ret);
+               iotcon_remote_resource_destroy(door_resource);
+               iotcon_remote_resource_destroy(new_door_resource);
+               return;
+       }
+
+       iotcon_remote_resource_destroy(resource);
+}
+
+static void _on_put(iotcon_remote_resource_h resource, iotcon_representation_h recv_repr,
+               iotcon_options_h header_options, int response_result, void *user_data)
+{
+       int ret;
+       iotcon_representation_h send_repr;
+
+       RETM_IF(IOTCON_RESPONSE_RESULT_OK != response_result, "_on_put Response error(%d)",
+                       response_result);
+       INFO("PUT request was successful");
+
+       ret = iotcon_representation_create(&send_repr);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_representation_create() Fail(%d)", ret);
+               return;
+       }
+
+       /* send POST request */
+       ret = iotcon_remote_resource_post(resource, send_repr, NULL, _on_post, NULL);
+       if (IOTCON_ERROR_NONE != ret)
+               ERR("iotcon_remote_resource_post() Fail(%d)", ret);
+
+       iotcon_representation_destroy(send_repr);
+}
+
+static void _on_get(iotcon_remote_resource_h resource, iotcon_representation_h recv_repr,
+               iotcon_options_h header_options, int response_result, void *user_data)
+{
+       int ret;
+       bool opened = true;
+       iotcon_representation_h send_repr;
+       iotcon_state_h send_state;
+       iotcon_state_h recv_state = NULL;
+
+       RETM_IF(IOTCON_RESPONSE_RESULT_OK != response_result, "_on_get Response error(%d)",
+                       response_result);
+       INFO("GET request was successful");
+
+       ret = iotcon_representation_get_state(recv_repr, &recv_state);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_representation_get_state() Fail(%d)", ret);
+               return;
+       }
+
+       ret = iotcon_state_get_bool(recv_state, "opened", &opened);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_state_get_bool() Fail(%d)", ret);
+               return;
+       }
+
+       ret = iotcon_representation_create(&send_repr);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_representation_create() Fail(%d)", ret);
+               return;
+       }
+
+       ret = iotcon_state_create(&send_state);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_state_create() Fail(%d)", ret);
+               iotcon_representation_destroy(send_repr);
+               return;
+       }
+
+       ret = iotcon_state_set_bool(send_state, "opened", true);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_state_set_bool() Fail(%d)", ret);
+               iotcon_state_destroy(send_state);
+               iotcon_representation_destroy(send_repr);
+               return;
+       }
+
+       ret = iotcon_representation_set_state(send_repr, send_state);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_representation_set_state() Fail(%d)", ret);
+               iotcon_state_destroy(send_state);
+               iotcon_representation_destroy(send_repr);
+               return;
+       }
+
+       iotcon_state_destroy(send_state);
+
+       /* send PUT request */
+       ret = iotcon_remote_resource_put(resource, send_repr, NULL, _on_put, NULL);
+       if (IOTCON_ERROR_NONE != ret)
+               ERR("iotcon_remote_resource_put() Fail(%d)", ret);
+
+       iotcon_representation_destroy(send_repr);
+}
+
+static int _get_res_type_cb(const char *string, void *user_data)
+{
+       char *resource_uri_path = user_data;
+
+       DBG("[%s] resource type : %s", resource_uri_path, string);
+
+       return IOTCON_FUNC_CONTINUE;
+}
+
+static void _presence_handler(int result, unsigned int nonce,
+               const char *host_address, void *user_data)
+{
+       INFO("_presence_handler");
+       INFO("result : %d", result);
+       INFO("nonce : %d", nonce);
+       INFO("host_address : %s", host_address);
+}
+
+static int _device_id_compare(const void *a, const void *b)
+{
+       return strcmp(a, b);
+}
+
+static void _get_tizen_info(iotcon_tizen_info_h info, int response_result,
+               void *user_data)
+{
+       int ret;
+       char *device_name = NULL;
+       char *tizen_device_id = NULL;
+
+       RETM_IF(IOTCON_RESPONSE_RESULT_OK != response_result,
+                       "_get_tizen_info Response error(%d)", response_result);
+
+       ret = iotcon_tizen_info_get_property(info, IOTCON_TIZEN_INFO_DEVICE_NAME,
+                       &device_name);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_tizen_info_get_property() Fail(%d)", ret);
+               return;
+       }
+
+       ret = iotcon_tizen_info_get_property(info, IOTCON_TIZEN_INFO_TIZEN_DEVICE_ID,
+                       &tizen_device_id);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_tizen_info_get_property() Fail(%d)", ret);
+               return;
+       }
+
+       INFO("This is Tizen Device.");
+       INFO("- Device name : %s", device_name);
+       INFO("- Tizen Device ID : %s", tizen_device_id);
+}
+
+static void _found_resource(iotcon_remote_resource_h resource, int result, void *user_data)
+{
+       GList *node;
+       char *resource_host;
+       char *resource_uri_path;
+       char *resource_device_id;
+       int ret, resource_interfaces;
+       iotcon_presence_h presence_handle;
+       iotcon_resource_types_h resource_types;
+       iotcon_remote_resource_h resource_clone = NULL;
+
+       RETM_IF(IOTCON_ERROR_NONE != result, "Invalid result(%d)", result);
+
+       if (NULL == resource)
+               return;
+
+       INFO("===== resource found =====");
+
+       /* get the resource URI */
+       ret = iotcon_remote_resource_get_uri_path(resource, &resource_uri_path);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_remote_resource_get_uri_path() Fail(%d)", ret);
+               return;
+       }
+
+       /* get the device unique id.
+        * this is unique per-server independent on how it was discovered. */
+       ret = iotcon_remote_resource_get_device_id(resource, &resource_device_id);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_remote_resource_get_device_id() Fail(%d)", ret);
+               return;
+       }
+       DBG("[%s] resource device id : %s", resource_uri_path, resource_device_id);
+
+       node = g_list_find_custom(device_id_list, resource_device_id, _device_id_compare);
+
+       if (node && TEST_STR_EQUAL == strcmp(door_uri_path, resource_uri_path)) {
+               DBG("uri_path \"%s\" already found. skip !", resource_uri_path);
+               return;
+       }
+
+       door_resource_device_id = strdup(resource_device_id);
+       if (NULL == door_resource_device_id) {
+               ERR("strdup(door_resource_device_id) Fail");
+               return;
+       }
+
+       device_id_list = g_list_append(device_id_list, door_resource_device_id);
+
+       /* get the resource host address */
+       ret = iotcon_remote_resource_get_host(resource, &resource_host);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_remote_resource_get_host() Fail(%d)", ret);
+               device_id_list = g_list_remove(device_id_list, door_resource_device_id);
+               free(door_resource_device_id);
+               return;
+       }
+       DBG("[%s] resource host : %s", resource_uri_path, resource_host);
+
+       /* get the resource interfaces */
+       ret = iotcon_remote_resource_get_interfaces(resource, &resource_interfaces);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_remote_resource_get_interfaces() Fail(%d)", ret);
+               device_id_list = g_list_remove(device_id_list, door_resource_device_id);
+               free(door_resource_device_id);
+               return;
+       }
+       if (IOTCON_INTERFACE_DEFAULT & resource_interfaces)
+               DBG("[%s] resource interface : DEFAULT_INTERFACE", resource_uri_path);
+       if (IOTCON_INTERFACE_LINK & resource_interfaces)
+               DBG("[%s] resource interface : LINK_INTERFACE", resource_uri_path);
+       if (IOTCON_INTERFACE_BATCH & resource_interfaces)
+               DBG("[%s] resource interface : BATCH_INTERFACE", resource_uri_path);
+       if (IOTCON_INTERFACE_GROUP & resource_interfaces)
+               DBG("[%s] resource interface : GROUP_INTERFACE", resource_uri_path);
+
+       /* get the resource types */
+       ret = iotcon_remote_resource_get_types(resource, &resource_types);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_remote_resource_get_types() Fail(%d)", ret);
+               device_id_list = g_list_remove(device_id_list, door_resource_device_id);
+               free(door_resource_device_id);
+               return;
+       }
+
+       ret = iotcon_resource_types_foreach(resource_types, _get_res_type_cb,
+                       resource_uri_path);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_resource_types_foreach() Fail(%d)", ret);
+               device_id_list = g_list_remove(device_id_list, door_resource_device_id);
+               free(door_resource_device_id);
+               return;
+       }
+
+       /* get tizen info */
+       ret = iotcon_get_tizen_info(resource_host, _get_tizen_info, NULL);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_get_tizen_info() Fail(%d)", ret);
+               device_id_list = g_list_remove(device_id_list, door_resource_device_id);
+               free(door_resource_device_id);
+               return;
+       }
+
+       ret = iotcon_subscribe_presence(resource_host, "core.door", _presence_handler, NULL,
+                       &presence_handle);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_subscribe_presence() Fail(%d)", ret);
+               device_id_list = g_list_remove(device_id_list, door_resource_device_id);
+               free(door_resource_device_id);
+               return;
+       }
+
+       if (TEST_STR_EQUAL == strcmp(door_uri_path, resource_uri_path)) {
+               iotcon_query_h query;
+
+               ret = iotcon_query_create(&query);
+               if (IOTCON_ERROR_NONE != ret) {
+                       ERR("iotcon_query_create() Fail(%d)", ret);
+                       device_id_list = g_list_remove(device_id_list, door_resource_device_id);
+                       free(door_resource_device_id);
+                       return;
+               }
+
+               ret = iotcon_query_add(query, "query_key", "query_value");
+               if (IOTCON_ERROR_NONE != ret) {
+                       ERR("iotcon_query_add() Fail(%d)", ret);
+                       iotcon_query_destroy(query);
+                       device_id_list = g_list_remove(device_id_list, door_resource_device_id);
+                       free(door_resource_device_id);
+                       return;
+               }
+
+               ret = iotcon_remote_resource_clone(resource, &resource_clone);
+               if (IOTCON_ERROR_NONE != ret) {
+                       ERR("iotcon_remote_resource_clone() Fail(%d)", ret);
+                       iotcon_query_destroy(query);
+                       device_id_list = g_list_remove(device_id_list, door_resource_device_id);
+                       free(door_resource_device_id);
+                       return;
+               }
+
+               /* send GET Request */
+               ret = iotcon_remote_resource_get(resource_clone, query, _on_get, NULL);
+               if (IOTCON_ERROR_NONE != ret)
+                       ERR("iotcon_remote_resource_get() Fail(%d)", ret);
+
+               iotcon_query_destroy(query);
+       }
+
+       device_id_list = g_list_remove(device_id_list, door_resource_device_id);
+       free(door_resource_device_id);
+}
+
+int main(int argc, char **argv)
+{
+       FN_CALL;
+       int ret;
+       GMainLoop *loop;
+
+       loop = g_main_loop_new(NULL, FALSE);
+
+       /* iotcon open */
+       ret = iotcon_open();
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_open() Fail(%d)", ret);
+               return -1;
+       }
+
+       /* find door typed resources */
+       ret = iotcon_find_resource(IOTCON_MULTICAST_ADDRESS, "core.door", &_found_resource,
+                       NULL);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_find_resource() Fail(%d)", ret);
+               iotcon_close();
+               return -1;
+       }
+
+       g_main_loop_run(loop);
+       g_main_loop_unref(loop);
+
+       g_list_free_full(device_id_list, free);
+
+       /* iotcon close */
+       iotcon_close();
+
+       return 0;
+}
diff --git a/test/iotcon-test-basic-server.c b/test/iotcon-test-basic-server.c
new file mode 100644 (file)
index 0000000..f8c5051
--- /dev/null
@@ -0,0 +1,569 @@
+/*
+ * 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 <iotcon-internal.h>
+#include "test.h"
+
+/* Door Resource */
+typedef struct _door_resource_s {
+       bool state;
+       char *uri_path;
+       char *type;
+       int ifaces;
+       int properties;
+       iotcon_resource_h handle;
+       iotcon_observers_h observers;
+       iotcon_representation_h repr;
+} door_resource_s;
+
+static bool resource_created;
+
+static void _request_handler(iotcon_resource_h resource, iotcon_request_h request,
+               void *user_data);
+
+static int _set_door_resource(door_resource_s *door)
+{
+       int ret;
+
+       door->state = false;
+
+       door->uri_path = strdup("/a/door");
+       if (NULL == door->uri_path) {
+               ERR("strdup(/a/door) Fail");
+               return -1;
+       }
+
+       door->type = strdup("core.door");
+       if (NULL == door->type) {
+               ERR("strdup(core.door) Fail");
+               free(door->uri_path);
+               return -1;
+       }
+
+       door->ifaces = IOTCON_INTERFACE_DEFAULT;
+       door->properties = IOTCON_DISCOVERABLE;
+
+       ret = iotcon_observers_create(&door->observers);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_observers_create() Fail");
+               free(door->type);
+               free(door->uri_path);
+               return -1;
+       }
+
+       return 0;
+}
+
+static void _free_door_resource(door_resource_s *door)
+{
+       iotcon_observers_destroy(door->observers);
+       free(door->type);
+       free(door->uri_path);
+}
+
+static void _check_door_state(door_resource_s door)
+{
+       if (false == door.state)
+               INFO("[Door] closed.");
+       else
+               INFO("[Door] opened.");
+}
+
+static iotcon_resource_h _create_door_resource(char *uri_path, char *type, int ifaces,
+               int properties, void *user_data)
+{
+       int ret;
+       iotcon_resource_h handle;
+       iotcon_resource_types_h resource_types;
+
+       ret = iotcon_resource_types_create(&resource_types);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_resource_types_create() Fail(%d)", ret);
+               return NULL;
+       }
+
+       ret = iotcon_resource_types_add(resource_types, type);
+       if (IOTCON_ERROR_NONE != ret) {
+               iotcon_resource_types_destroy(resource_types);
+               ERR("iotcon_resource_types_add() Fail(%d)", ret);
+               return NULL;
+       }
+
+       /* register door resource */
+       ret = iotcon_resource_create(uri_path, resource_types, ifaces, properties,
+                       _request_handler, user_data, &handle);
+       if (IOTCON_ERROR_NONE != ret) {
+               iotcon_resource_types_destroy(resource_types);
+               ERR("iotcon_resource_create() Fail");
+               return NULL;
+       }
+
+       iotcon_resource_types_destroy(resource_types);
+
+       return handle;
+}
+
+static int _send_response(iotcon_response_h response, iotcon_representation_h repr,
+               iotcon_response_result_e result)
+{
+       int ret;
+
+       ret = iotcon_response_set_result(response, result);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_response_set_result() Fail(%d)", ret);
+               return -1;
+       }
+
+       ret = iotcon_response_set_representation(response, repr);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_response_set_representation() Fail(%d)", ret);
+               return -1;
+       }
+
+       /* send Representation to the client */
+       ret = iotcon_response_send(response);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_response_send() Fail(%d)", ret);
+               return -1;
+       }
+
+       return 0;
+}
+
+static void _request_handler_get(door_resource_s *door, iotcon_response_h response)
+{
+       int ret;
+       iotcon_representation_h resp_repr;
+       iotcon_state_h resp_state;
+       INFO("GET request");
+
+       /* create a door Representation */
+       ret = iotcon_representation_create(&resp_repr);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_representation_create() Fail(%d)", ret);
+               return;
+       }
+
+       /* create a door state */
+       ret = iotcon_state_create(&resp_state);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_state_create() Fail(%d)", ret);
+               iotcon_representation_destroy(resp_repr);
+               return;
+       }
+
+       ret = iotcon_representation_set_uri_path(resp_repr, door->uri_path);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_representation_set_uri_path() Fail(%d)", ret);
+               iotcon_state_destroy(resp_state);
+               iotcon_representation_destroy(resp_repr);
+               return;
+       }
+
+       ret = iotcon_state_set_bool(resp_state, "opened", door->state);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_state_set_bool() Fail(%d)", ret);
+               iotcon_state_destroy(resp_state);
+               iotcon_representation_destroy(resp_repr);
+               return;
+       }
+
+       ret = iotcon_representation_set_state(resp_repr, resp_state);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_representation_set_state() Fail(%d)", ret);
+               iotcon_state_destroy(resp_state);
+               iotcon_representation_destroy(resp_repr);
+               return;
+       }
+
+       iotcon_state_destroy(resp_state);
+
+       ret = _send_response(response, resp_repr, IOTCON_RESPONSE_RESULT_OK);
+       if (0 != ret)
+               ERR("_send_response() Fail(%d)", ret);
+
+       iotcon_representation_destroy(resp_repr);
+}
+
+static void _request_handler_put(door_resource_s *door, iotcon_request_h request,
+               iotcon_response_h response)
+{
+       int ret;
+       bool bval;
+       iotcon_representation_h req_repr, resp_repr;
+       iotcon_state_h req_state, resp_state;;
+       INFO("PUT request");
+
+       ret = iotcon_request_get_representation(request, &req_repr);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_request_get_representation() Fail(%d)", ret);
+               return;
+       }
+
+       ret = iotcon_representation_get_state(req_repr, &req_state);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_representation_get_state() Fail(%d)", ret);
+               return;
+       }
+
+       ret = iotcon_state_get_bool(req_state, "opened", &bval);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_state_get_bool() Fail(%d)", ret);
+               return;
+       }
+
+       door->state = bval;
+
+       _check_door_state(*door);
+
+       ret = iotcon_representation_create(&resp_repr);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_representation_create() Fail(%d)", ret);
+               return;
+       }
+
+       ret = iotcon_representation_set_uri_path(resp_repr, door->uri_path);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_representation_set_uri_path() Fail(%d)", ret);
+               iotcon_representation_destroy(resp_repr);
+               return;
+       }
+
+       ret = iotcon_state_create(&resp_state);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_state_create() Fail(%d)", ret);
+               iotcon_representation_destroy(resp_repr);
+               return;
+       }
+
+       ret = iotcon_state_set_bool(resp_state, "opened", door->state);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_state_set_bool() Fail(%d)", ret);
+               iotcon_state_destroy(resp_state);
+               iotcon_representation_destroy(resp_repr);
+               return;
+       }
+
+       ret = iotcon_representation_set_state(resp_repr, resp_state);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_representation_set_state() Fail(%d)", ret);
+               iotcon_state_destroy(resp_state);
+               iotcon_representation_destroy(resp_repr);
+               return;
+       }
+
+       iotcon_state_destroy(resp_state);
+
+       ret = _send_response(response, resp_repr, IOTCON_RESPONSE_RESULT_OK);
+       if (0 != ret)
+               ERR("_send_response() Fail(%d)", ret);
+
+       iotcon_representation_destroy(resp_repr);
+}
+
+static gboolean _notifier(gpointer user_data)
+{
+       int ret;
+       static int i = 0;
+       iotcon_notimsg_h msg;
+       door_resource_s *door;
+       iotcon_representation_h repr;
+
+       door = user_data;
+
+       if ((5 == i++) || !(door->observers))
+               return G_SOURCE_REMOVE;
+
+       INFO("NOTIFY!");
+       ret = iotcon_representation_create(&repr);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_representation_create() Fail(%d)", ret);
+               return G_SOURCE_REMOVE;
+       }
+
+       ret = iotcon_notimsg_create(repr, IOTCON_INTERFACE_DEFAULT, &msg);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_notimsg_create() Fail(%d)", ret);
+               iotcon_representation_destroy(repr);
+               return G_SOURCE_REMOVE;
+       }
+
+       ret = iotcon_resource_notify(door->handle, msg, door->observers);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_resource_notify() Fail(%d)", ret);
+               iotcon_notimsg_destroy(msg);
+               iotcon_representation_destroy(repr);
+               return G_SOURCE_REMOVE;
+       }
+
+       iotcon_notimsg_destroy(msg);
+       iotcon_representation_destroy(repr);
+
+       return G_SOURCE_CONTINUE;
+}
+
+static void _request_handler_post(door_resource_s *door, iotcon_response_h response)
+{
+       int ret;
+       iotcon_state_h resp_state;
+       iotcon_representation_h resp_repr = NULL;
+       iotcon_resource_h new_door_handle;
+       INFO("POST request");
+
+       if (resource_created) {
+               INFO("Resource is already created");
+               return;
+       }
+
+       new_door_handle = _create_door_resource("/a/door1", door->type,
+                       IOTCON_INTERFACE_DEFAULT, (IOTCON_DISCOVERABLE | IOTCON_OBSERVABLE), NULL);
+       if (NULL == new_door_handle) {
+               ERR("_create_door_resource() Fail");
+               return;
+       }
+       resource_created = true;
+
+       /* send information that new resource was created */
+       ret = iotcon_representation_create(&resp_repr);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_representation_create() Fail(%d)", ret);
+               return;
+       }
+
+       ret = iotcon_state_create(&resp_state);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_state_create() Fail(%d)", ret);
+               iotcon_representation_destroy(resp_repr);
+               return;
+       }
+
+       ret = iotcon_state_set_str(resp_state, "createduripath", "/a/door1");
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_state_set_str() Fail(%d)", ret);
+               iotcon_state_destroy(resp_state);
+               iotcon_representation_destroy(resp_repr);
+               return;
+       }
+
+       ret = iotcon_representation_set_state(resp_repr, resp_state);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_representation_set_state() Fail(%d)", ret);
+               iotcon_state_destroy(resp_state);
+               iotcon_representation_destroy(resp_repr);
+               return;
+       }
+
+       iotcon_state_destroy(resp_state);
+
+       ret = _send_response(response, resp_repr, IOTCON_RESPONSE_RESULT_RESOURCE_CREATED);
+       if (0 != ret) {
+               ERR("_send_response() Fail(%d)", ret);
+               iotcon_representation_destroy(resp_repr);
+               return;
+       }
+
+       iotcon_representation_destroy(resp_repr);
+
+       /* add observe */
+       g_timeout_add_seconds(5, _notifier, door);
+}
+
+static void _request_handler_delete(iotcon_resource_h resource,
+               iotcon_response_h response)
+{
+       int ret;
+       iotcon_representation_h resp_repr = NULL;
+       iotcon_response_result_e result = IOTCON_RESPONSE_RESULT_OK;
+       INFO("DELETE request");
+
+       ret = iotcon_resource_destroy(resource);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_unregiser_resource() Fail(%d)", ret);
+               return;
+       }
+
+       result = IOTCON_RESPONSE_RESULT_RESOURCE_DELETED;
+
+       ret = iotcon_representation_create(&resp_repr);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_representation_create() Fail(%d)", ret);
+               return;
+       }
+
+       ret = _send_response(response, resp_repr, result);
+       if (0 != ret)
+               ERR("_send_response() Fail(%d)", ret);
+
+       iotcon_representation_destroy(resp_repr);
+}
+
+static int _query_cb(const char *key, const char *value, void *user_data)
+{
+       INFO("key : %s, value : %s", key, value);
+
+       return IOTCON_FUNC_CONTINUE;
+}
+
+static void _request_handler(iotcon_resource_h resource, iotcon_request_h request,
+               void *user_data)
+{
+       door_resource_s *door;
+       iotcon_query_h query;
+       iotcon_response_h response = NULL;
+       int ret, types, observer_id, observer_action;
+
+       RET_IF(NULL == request);
+
+       door = user_data;
+
+       ret = iotcon_request_get_query(request, &query);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_request_get_query() Fail(%d)", ret);
+               return;
+       }
+       if (query)
+               iotcon_query_foreach(query, _query_cb, NULL);
+
+       ret = iotcon_request_get_types(request, &types);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_request_get_types() Fail(%d)", ret);
+               return;
+       }
+
+       ret = iotcon_response_create(request, &response);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_response_create() Fail(%d)", ret);
+               return;
+       }
+
+       if (IOTCON_REQUEST_GET & types)
+               _request_handler_get(door, response);
+
+       else if (IOTCON_REQUEST_PUT & types)
+               _request_handler_put(door, request, response);
+
+       else if (IOTCON_REQUEST_POST & types)
+               _request_handler_post(door, response);
+
+       else if (IOTCON_REQUEST_DELETE & types)
+               _request_handler_delete(resource, response);
+
+       iotcon_response_destroy(response);
+
+       if (IOTCON_REQUEST_OBSERVE & types) {
+               ret = iotcon_request_get_observer_action(request, &observer_action);
+               if (IOTCON_ERROR_NONE != ret) {
+                       ERR("iotcon_request_get_observer_action() Fail(%d)", ret);
+                       return;
+               }
+
+               if (IOTCON_OBSERVE_REGISTER == observer_action) {
+                       ret = iotcon_request_get_observer_id(request, &observer_id);
+                       if (IOTCON_ERROR_NONE != ret) {
+                               ERR("iotcon_request_get_observer_id() Fail(%d)", ret);
+                               return;
+                       }
+                       ret = iotcon_observers_add(door->observers, observer_id);
+               } else if (IOTCON_OBSERVE_DEREGISTER == observer_action) {
+                       ret = iotcon_request_get_observer_id(request, &observer_id);
+                       if (IOTCON_ERROR_NONE != ret) {
+                               ERR("iotcon_request_get_observer_id() Fail(%d)", ret);
+                               return;
+                       }
+                       ret = iotcon_observers_remove(door->observers, observer_id);
+                       if (IOTCON_ERROR_NONE != ret) {
+                               ERR("iotcon_observers_remove() Fail(%d)", ret);
+                               return;
+                       }
+               }
+       }
+}
+
+static gboolean _presence_timer(gpointer user_data)
+{
+       static int i = 0;
+       i++;
+       if (i % 2)
+               iotcon_start_presence(10);
+       else
+               iotcon_stop_presence();
+
+       if (4 == i)
+               return G_SOURCE_REMOVE;
+
+       return G_SOURCE_CONTINUE;
+}
+
+int main(int argc, char **argv)
+{
+       FN_CALL;
+       int ret;
+       GMainLoop *loop;
+       door_resource_s my_door = {0};
+
+       loop = g_main_loop_new(NULL, FALSE);
+
+       /* iotcon open */
+       ret = iotcon_open();
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_open() Fail(%d)", ret);
+               return -1;
+       }
+
+       /* set local door resource */
+       ret = _set_door_resource(&my_door);
+       if (0 != ret) {
+               ERR("_set_door_resource() Fail");
+               iotcon_close();
+               return -1;
+       }
+
+       /* add resource options */
+       my_door.ifaces |= IOTCON_INTERFACE_BATCH;
+       my_door.properties |= IOTCON_OBSERVABLE;
+
+       /* add presence */
+       g_timeout_add_seconds(10, _presence_timer, NULL);
+
+       /* create new door resource */
+       my_door.handle = _create_door_resource(my_door.uri_path, my_door.type, my_door.ifaces,
+                       my_door.properties, &my_door);
+       if (NULL == my_door.handle) {
+               ERR("_create_door_resource() Fail");
+               _free_door_resource(&my_door);
+               iotcon_close();
+               return -1;
+       }
+
+       _check_door_state(my_door);
+
+       g_main_loop_run(loop);
+       g_main_loop_unref(loop);
+
+       iotcon_resource_destroy(my_door.handle);
+
+       _free_door_resource(&my_door);
+
+       /* iotcon close */
+       iotcon_close();
+
+       return 0;
+}
diff --git a/test/iotcon-test-device-client.c b/test/iotcon-test-device-client.c
new file mode 100644 (file)
index 0000000..88d3fc8
--- /dev/null
@@ -0,0 +1,205 @@
+/*
+ * 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 <glib.h>
+#include <iotcon.h>
+#include "test.h"
+
+static void _get_device_info(iotcon_device_info_h info, int result, void *user_data)
+{
+       int ret;
+       char *device_name = NULL;
+       char *spec_ver = NULL;
+       char *device_id = NULL;
+       char *data_model_ver = NULL;
+
+       RETM_IF(IOTCON_ERROR_NONE != result, "Invalid result (%d)", result);
+
+       ret = iotcon_device_info_get_property(info, IOTCON_DEVICE_INFO_NAME, &device_name);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_device_info_get_property() Fail(%d)", ret);
+               return;
+       }
+
+       ret = iotcon_device_info_get_property(info, IOTCON_DEVICE_INFO_SPEC_VER, &spec_ver);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_device_info_get_property() Fail(%d)", ret);
+               return;
+       }
+
+       ret = iotcon_device_info_get_property(info, IOTCON_DEVICE_INFO_ID, &device_id);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_device_info_get_property() Fail(%d)", ret);
+               return;
+       }
+
+       ret = iotcon_device_info_get_property(info, IOTCON_DEVICE_INFO_DATA_MODEL_VER,
+                       &data_model_ver);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_device_info_get_property() Fail(%d)", ret);
+               return;
+       }
+
+       INFO("Get Device Info");
+       INFO("device_name : %s", device_name);
+       INFO("spec_version : %s", spec_ver);
+       INFO("device_id : %s", device_id);
+       INFO("data_model_version : %s", data_model_ver);
+}
+
+static void _get_platform_info(iotcon_platform_info_h info, int result, void *user_data)
+{
+       int ret;
+       char *platform_id = NULL;
+       char *manuf_name = NULL;
+       char *manuf_url = NULL;
+       char *model_number = NULL;
+       char *date_of_manuf = NULL;
+       char *platform_ver = NULL;
+       char *os_ver = NULL;
+       char *hardware_ver = NULL;
+       char *firmware_ver = NULL;
+       char *support_url = NULL;
+       char *system_time = NULL;
+
+       RETM_IF(IOTCON_ERROR_NONE != result, "Invalid result (%d)", result);
+
+       ret = iotcon_platform_info_get_property(info, IOTCON_PLATFORM_INFO_ID, &platform_id);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_platform_info_get_property() Fail(%d)", ret);
+               return;
+       }
+
+       ret = iotcon_platform_info_get_property(info, IOTCON_PLATFORM_INFO_MANUF_NAME,
+                       &manuf_name);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_platform_info_get_property() Fail(%d)", ret);
+               return;
+       }
+
+       ret = iotcon_platform_info_get_property(info, IOTCON_PLATFORM_INFO_MANUF_URL,
+                       &manuf_url);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_platform_info_get_property() Fail(%d)", ret);
+               return;
+       }
+
+       ret = iotcon_platform_info_get_property(info, IOTCON_PLATFORM_INFO_MODEL_NUMBER,
+                       &model_number);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_platform_info_get_property() Fail(%d)", ret);
+               return;
+       }
+
+       ret = iotcon_platform_info_get_property(info, IOTCON_PLATFORM_INFO_DATE_OF_MANUF,
+                       &date_of_manuf);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_platform_info_get_property() Fail(%d)", ret);
+               return;
+       }
+
+       ret = iotcon_platform_info_get_property(info, IOTCON_PLATFORM_INFO_PLATFORM_VER,
+                       &platform_ver);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_platform_info_get_property() Fail(%d)", ret);
+               return;
+       }
+
+       ret = iotcon_platform_info_get_property(info, IOTCON_PLATFORM_INFO_OS_VER, &os_ver);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_platform_info_get_property() Fail(%d)", ret);
+               return;
+       }
+
+       ret = iotcon_platform_info_get_property(info, IOTCON_PLATFORM_INFO_HARDWARE_VER,
+                       &hardware_ver);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_platform_info_get_property() Fail(%d)", ret);
+               return;
+       }
+
+       ret = iotcon_platform_info_get_property(info, IOTCON_PLATFORM_INFO_FIRMWARE_VER,
+                       &firmware_ver);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_platform_info_get_property() Fail(%d)", ret);
+               return;
+       }
+
+       ret = iotcon_platform_info_get_property(info, IOTCON_PLATFORM_INFO_SUPPORT_URL,
+                       &support_url);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_platform_info_get_property() Fail(%d)", ret);
+               return;
+       }
+
+       ret = iotcon_platform_info_get_property(info, IOTCON_PLATFORM_INFO_SYSTEM_TIME,
+                       &system_time);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_platform_info_get_property() Fail(%d)", ret);
+               return;
+       }
+
+       INFO("Get Platform Info");
+       INFO("platform_id : %s", platform_id);
+       INFO("manuf_name : %s", manuf_name);
+       INFO("manuf_url : %s", manuf_url);
+       INFO("model_number : %s", model_number);
+       INFO("date_of_manufacture : %s", date_of_manuf);
+       INFO("platform_ver : %s", platform_ver);
+       INFO("os_ver : %s", os_ver);
+       INFO("hardware_ver : %s", hardware_ver);
+       INFO("firmware_ver : %s", firmware_ver);
+       INFO("support_url : %s", support_url);
+       INFO("system_time : %s", system_time);
+}
+
+int main()
+{
+       int ret;
+       GMainLoop *loop;
+
+       loop = g_main_loop_new(NULL, FALSE);
+
+       /* iotcon open */
+       ret = iotcon_open();
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_open() Fail(%d)", ret);
+               return -1;
+       }
+
+       ret = iotcon_get_device_info(IOTCON_MULTICAST_ADDRESS, _get_device_info, NULL);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_get_device_info() Fail(%d)", ret);
+               iotcon_close();
+               return -1;
+       }
+
+       ret = iotcon_get_platform_info(IOTCON_MULTICAST_ADDRESS, _get_platform_info, NULL);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_get_platform_info() Fail(%d)", ret);
+               iotcon_close();
+               return -1;
+       }
+
+       g_main_loop_run(loop);
+
+       g_main_loop_unref(loop);
+
+       /* iotcon close */
+       iotcon_close();
+
+       return 0;
+}
diff --git a/test/iotcon-test-encap-client.c b/test/iotcon-test-encap-client.c
new file mode 100644 (file)
index 0000000..1e48d94
--- /dev/null
@@ -0,0 +1,251 @@
+/*
+ * 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 <stdbool.h>
+#include <stdlib.h>
+#include <glib.h>
+
+#include <iotcon.h>
+#include "test.h"
+
+static char *door_resource_device_id;
+static GList *device_id_list;
+
+static int _get_res_type_cb(const char *string, void *user_data)
+{
+       char *resource_uri_path = user_data;
+
+       DBG("[%s] resource type : %s", resource_uri_path, string);
+
+       return IOTCON_FUNC_CONTINUE;
+}
+
+static int _device_id_compare(const void *a, const void *b)
+{
+       return strcmp(a, b);
+}
+
+static void _state_changed_cb(iotcon_remote_resource_h resource,
+               iotcon_remote_resource_state_e state, void *user_data)
+{
+       INFO("Resource State is Changed");
+
+       switch (state) {
+       case IOTCON_REMOTE_RESOURCE_STATE_ALIVE:
+               INFO(" --- ALIVE");
+               break;
+       case IOTCON_REMOTE_RESOURCE_STATE_LOST_SIGNAL:
+               INFO(" --- LOST_SIGNAL");
+               break;
+       default:
+               break;
+       }
+}
+
+static void _representation_changed_cb(iotcon_remote_resource_h resource,
+               iotcon_representation_h representation, void *user_data)
+{
+       int ret;
+       bool opened;
+       iotcon_state_h state;
+
+       INFO("Resource is cached");
+
+       ret = iotcon_representation_get_state(representation, &state);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_representation_get_state() Fail(%d)", ret);
+               return;
+       }
+
+       ret = iotcon_state_get_bool(state, "opened", &opened);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_state_get_bool() Fail(%d)", ret);
+               return;
+       }
+
+       switch (opened) {
+       case true:
+               INFO("door is opened");
+               break;
+       case false:
+               INFO("door is closed");
+               break;
+       default:
+               break;
+       }
+}
+
+static void _found_resource(iotcon_remote_resource_h resource, int result,
+               void *user_data)
+{
+       GList *node;
+       char *resource_host;
+       char *resource_uri_path;
+       char *resource_device_id;
+       int ret, resource_interfaces;
+       iotcon_resource_types_h resource_types;
+       iotcon_remote_resource_h cloned_resource;
+
+       RETM_IF(IOTCON_ERROR_NONE != result, "Invalid result(%d)", result);
+
+       if (NULL == resource)
+               return;
+
+       INFO("===== resource found =====");
+
+       /* get the resource URI */
+       ret = iotcon_remote_resource_get_uri_path(resource, &resource_uri_path);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_remote_resource_get_uri_path() Fail(%d)", ret);
+               return;
+       }
+
+       /* get the device unique id.
+        * this is unique per-server independent on how it was discovered. */
+       ret = iotcon_remote_resource_get_device_id(resource, &resource_device_id);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_remote_resource_get_device_id() Fail(%d)", ret);
+               return;
+       }
+       DBG("[%s] resource device id : %s", resource_uri_path, resource_device_id);
+
+       node = g_list_find_custom(device_id_list, resource_device_id, _device_id_compare);
+
+       door_resource_device_id = strdup(resource_device_id);
+       if (NULL == door_resource_device_id) {
+               ERR("strdup(door_resource_device_id) Fail");
+               return;
+       }
+
+       device_id_list = g_list_append(device_id_list, door_resource_device_id);
+
+       if (node) {
+               DBG("This device(%s) is already found. skip !", door_resource_device_id);
+               return;
+       }
+
+       /* get the resource host address */
+       ret = iotcon_remote_resource_get_host(resource, &resource_host);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_remote_resource_get_host() Fail(%d)", ret);
+               device_id_list = g_list_remove(device_id_list, door_resource_device_id);
+               free(door_resource_device_id);
+               return;
+       }
+       DBG("[%s] resource host : %s", resource_uri_path, resource_host);
+
+       /* get the resource interfaces */
+       ret = iotcon_remote_resource_get_interfaces(resource, &resource_interfaces);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_remote_resource_get_interfaces() Fail(%d)", ret);
+               device_id_list = g_list_remove(device_id_list, door_resource_device_id);
+               free(door_resource_device_id);
+               return;
+       }
+       if (IOTCON_INTERFACE_DEFAULT & resource_interfaces)
+               DBG("[%s] resource interface : DEFAULT_INTERFACE", resource_uri_path);
+       if (IOTCON_INTERFACE_LINK & resource_interfaces)
+               DBG("[%s] resource interface : LINK_INTERFACE", resource_uri_path);
+       if (IOTCON_INTERFACE_BATCH & resource_interfaces)
+               DBG("[%s] resource interface : BATCH_INTERFACE", resource_uri_path);
+       if (IOTCON_INTERFACE_GROUP & resource_interfaces)
+               DBG("[%s] resource interface : GROUP_INTERFACE", resource_uri_path);
+
+       /* get the resource types */
+       ret = iotcon_remote_resource_get_types(resource, &resource_types);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_remote_resource_get_types() Fail(%d)", ret);
+               device_id_list = g_list_remove(device_id_list, door_resource_device_id);
+               free(door_resource_device_id);
+               return;
+       }
+
+       ret = iotcon_resource_types_foreach(resource_types, _get_res_type_cb,
+                       resource_uri_path);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_resource_types_foreach() Fail(%d)", ret);
+               device_id_list = g_list_remove(device_id_list, door_resource_device_id);
+               free(door_resource_device_id);
+               return;
+       }
+
+       ret = iotcon_remote_resource_clone(resource, &cloned_resource);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_remote_resource_clone() Fail(%d)", ret);
+               device_id_list = g_list_remove(device_id_list, door_resource_device_id);
+               free(door_resource_device_id);
+               return;
+       }
+
+       /* Start Monitoring */
+       ret = iotcon_remote_resource_start_monitoring(cloned_resource, 10, _state_changed_cb,
+                       NULL);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_remote_resource_start_monitoring() Fail(%d)", ret);
+               device_id_list = g_list_remove(device_id_list, door_resource_device_id);
+               free(door_resource_device_id);
+               return;
+       }
+
+       /* Start Caching */
+       ret = iotcon_remote_resource_start_caching(cloned_resource, 10,
+                       _representation_changed_cb, NULL);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_remote_resource_start_caching() Fail(%d)", ret);
+               iotcon_remote_resource_stop_monitoring(resource);
+               device_id_list = g_list_remove(device_id_list, door_resource_device_id);
+               free(door_resource_device_id);
+               return;
+       }
+
+       device_id_list = g_list_remove(device_id_list, door_resource_device_id);
+       free(door_resource_device_id);
+}
+
+int main(int argc, char **argv)
+{
+       FN_CALL;
+       int ret;
+       GMainLoop *loop;
+       iotcon_remote_resource_h resource;
+
+       loop = g_main_loop_new(NULL, FALSE);
+
+       /* iotcon open */
+       ret = iotcon_open();
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_open() Fail(%d)", ret);
+               return -1;
+       }
+
+       /* find door typed resources */
+       ret = iotcon_find_resource(IOTCON_MULTICAST_ADDRESS, "core.door", &_found_resource,
+                       &resource);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_find_resource() Fail(%d)", ret);
+               iotcon_close();
+               return -1;
+       }
+
+       g_main_loop_run(loop);
+       g_main_loop_unref(loop);
+
+       g_list_free_full(device_id_list, free);
+
+       /* iotcon close */
+       iotcon_close();
+
+       return 0;
+}
diff --git a/test/iotcon-test-encap-server.c b/test/iotcon-test-encap-server.c
new file mode 100644 (file)
index 0000000..e4c0f26
--- /dev/null
@@ -0,0 +1,234 @@
+/*
+ * 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 <iotcon-internal.h>
+#include "test.h"
+
+/* Door Resource */
+typedef struct _door_resource_s {
+       bool state;
+       char *uri_path;
+       char *type;
+       int properties;
+       iotcon_lite_resource_h handle;
+} door_resource_s;
+
+static int _set_door_resource(door_resource_s *door)
+{
+       door->state = false;
+
+       door->uri_path = strdup("/a/door");
+       if (NULL == door->uri_path) {
+               ERR("strdup(/a/door) Fail");
+               return -1;
+       }
+
+       door->type = strdup("core.door");
+       if (NULL == door->type) {
+               ERR("strdup(core.door) Fail");
+               free(door->uri_path);
+               return -1;
+       }
+
+       door->properties = IOTCON_DISCOVERABLE;
+
+       return 0;
+}
+
+static void _free_door_resource(door_resource_s *door)
+{
+       free(door->type);
+       free(door->uri_path);
+}
+
+static void _check_door_state(door_resource_s door)
+{
+       if (false == door.state)
+               INFO("[Door] closed.");
+       else
+               INFO("[Door] opened.");
+}
+
+static gboolean _door_state_changer(gpointer user_data)
+{
+       int ret;
+       door_resource_s *door = user_data;
+       iotcon_state_h recv_state, send_state;
+
+       ret = iotcon_lite_resource_get_state(door->handle, &recv_state);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_lite_resource_get_state() Fail(%d)", ret);
+               return G_SOURCE_CONTINUE;
+       }
+
+       ret = iotcon_state_get_bool(recv_state, "opened", &(door->state));
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_state_get_bool() Fail(%d)", ret);
+               return G_SOURCE_CONTINUE;
+       }
+
+       if (true == door->state)
+               door->state = false;
+       else
+               door->state = true;
+
+       ret = iotcon_state_create(&send_state);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_state_create() Fail(%d)", ret);
+               return G_SOURCE_CONTINUE;
+       }
+
+       ret = iotcon_state_set_bool(send_state, "opened", door->state);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_state_set_bool() Fail(%d)", ret);
+               iotcon_state_destroy(send_state);
+               return G_SOURCE_CONTINUE;
+       }
+
+       ret = iotcon_lite_resource_update_state(door->handle, send_state);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_lite_resource_update_state() Fail(%d)", ret);
+               iotcon_state_destroy(send_state);
+               return G_SOURCE_CONTINUE;
+       }
+
+       iotcon_state_destroy(send_state);
+
+       _check_door_state(*door);
+
+       return G_SOURCE_CONTINUE;
+}
+
+static iotcon_lite_resource_h _create_door_resource(char *uri_path, char *type,
+               int properties, void *user_data)
+{
+       int ret;
+       iotcon_state_h state;
+       iotcon_lite_resource_h handle;
+       iotcon_resource_types_h resource_types;
+
+       ret = iotcon_resource_types_create(&resource_types);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_resource_types_create() Fail(%d)", ret);
+               return NULL;
+       }
+
+       ret = iotcon_resource_types_add(resource_types, type);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_resource_types_add() Fail(%d)", ret);
+               iotcon_resource_types_destroy(resource_types);
+               return NULL;
+       }
+
+       ret = iotcon_state_create(&state);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_state_create() Fail(%d)", ret);
+               iotcon_resource_types_destroy(resource_types);
+               return NULL;
+       }
+
+       ret = iotcon_state_set_bool(state, "opened", false);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_state_set_bool() Fail(%d)", ret);
+               iotcon_state_destroy(state);
+               iotcon_resource_types_destroy(resource_types);
+               return NULL;
+       }
+
+       /* register door resource */
+       ret = iotcon_lite_resource_create(uri_path, resource_types, properties, state,
+                       &handle);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_resource_create() Fail");
+               iotcon_state_destroy(state);
+               iotcon_resource_types_destroy(resource_types);
+               return NULL;
+       }
+
+       iotcon_state_destroy(state);
+       iotcon_resource_types_destroy(resource_types);
+
+       return handle;
+}
+
+int main(int argc, char **argv)
+{
+       FN_CALL;
+       int ret;
+       GMainLoop *loop;
+       door_resource_s my_door = {0};
+
+       loop = g_main_loop_new(NULL, FALSE);
+
+       /* iotcon open */
+       ret = iotcon_open();
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_open() Fail(%d)", ret);
+               return -1;
+       }
+
+       /* start presence */
+       ret = iotcon_start_presence(10);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_start_presence() Fail(%d)", ret);
+               return -1;
+       }
+
+       /* set local door resource */
+       ret = _set_door_resource(&my_door);
+       if (0 != ret) {
+               ERR("_set_door_resource() Fail");
+               iotcon_close();
+               return -1;
+       }
+
+       /* add resource options */
+       my_door.properties |= IOTCON_OBSERVABLE;
+
+       /* create new door resource */
+       my_door.handle = _create_door_resource(my_door.uri_path, my_door.type,
+                       my_door.properties, &my_door);
+       if (NULL == my_door.handle) {
+               ERR("_create_door_resource() Fail");
+               _free_door_resource(&my_door);
+               iotcon_stop_presence();
+               iotcon_close();
+               return -1;
+       }
+
+       _check_door_state(my_door);
+
+       g_timeout_add_seconds(7, _door_state_changer, &my_door);
+
+       g_main_loop_run(loop);
+       g_main_loop_unref(loop);
+
+       iotcon_lite_resource_destroy(my_door.handle);
+
+       _free_door_resource(&my_door);
+
+       iotcon_stop_presence();
+
+       /* iotcon close */
+       iotcon_close();
+
+       return 0;
+}
diff --git a/test/iotcon-test-repr-client.c b/test/iotcon-test-repr-client.c
new file mode 100644 (file)
index 0000000..aa5117c
--- /dev/null
@@ -0,0 +1,352 @@
+/*
+ * 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.h"
+
+static char *room_resource_device_id;
+static GList *device_id_list;
+
+static const char* const room_uri_path = "/a/room";
+
+static int _get_int_list_cb(int pos, const int value, void *user_data)
+{
+       DBG("%d°C", value);
+
+       return IOTCON_FUNC_CONTINUE;
+}
+
+static void _on_get(iotcon_representation_h recv_repr, int response_result)
+{
+       int i, ret, int_val;
+       bool is_null, bool_val;
+       char *uri_path, *str_val;
+       iotcon_list_h list_val;
+       iotcon_representation_h child_repr;
+       iotcon_state_h recv_state, child_state;
+       unsigned int key_count, children_count;
+
+       RETM_IF(IOTCON_RESPONSE_RESULT_OK != response_result, "_on_get Response error(%d)",
+                       response_result);
+       INFO("GET request was successful");
+
+       DBG("[ parent representation ]");
+       ret = iotcon_representation_get_uri_path(recv_repr, &uri_path);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_representation_get_uri_path() Fail(%d)", ret);
+               return;
+       }
+       DBG("uri_path : %s", uri_path);
+
+       ret = iotcon_representation_get_state(recv_repr, &recv_state);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_representation_get_state() Fail(%d)", ret);
+               return;
+       }
+
+       ret = iotcon_state_get_keys_count(recv_state, &key_count);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_state_get_keys_count() Fail(%d)", ret);
+               return;
+       }
+
+       if (key_count) {
+               ret = iotcon_state_get_str(recv_state, "name", &str_val);
+               if (IOTCON_ERROR_NONE != ret) {
+                       ERR("iotcon_state_get_str() Fail(%d)", ret);
+                       return;
+               }
+               DBG("name : %s", str_val);
+
+               ret = iotcon_state_get_list(recv_state, "today_temp", &list_val);
+               if (IOTCON_ERROR_NONE != ret) {
+                       ERR("iotcon_state_get_list() Fail(%d)", ret);
+                       return;
+               }
+
+               DBG("today's temperature :");
+               ret = iotcon_list_foreach_int(list_val, _get_int_list_cb, NULL);
+               if (IOTCON_ERROR_NONE != ret) {
+                       ERR("iotcon_list_foreach_int() Fail(%d)", ret);
+                       return;
+               }
+
+               ret = iotcon_state_is_null(recv_state, "null value", &is_null);
+               if (IOTCON_ERROR_NONE != ret) {
+                       ERR("iotcon_state_is_null() Fail(%d)", ret);
+                       return;
+               }
+
+               if (is_null)
+                       DBG("null value is null");
+       }
+
+       ret = iotcon_representation_get_children_count(recv_repr, &children_count);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_representation_get_children_count() Fail(%d)", ret);
+               return;
+       }
+
+       for (i = 0; i < children_count; i++) {
+               DBG("[ child representation ]");
+
+               ret = iotcon_representation_get_nth_child(recv_repr, i, &child_repr);
+               if (IOTCON_ERROR_NONE != ret) {
+                       ERR("iotcon_representation_get_nth_child(%d) Fail(%d)", i, ret);
+                       continue;
+               }
+
+               ret = iotcon_representation_get_uri_path(child_repr, &uri_path);
+               if (IOTCON_ERROR_NONE != ret) {
+                       ERR("iotcon_representation_get_uri_path() Fail(%d)", ret);
+                       continue;
+               }
+               DBG("uri_path : %s", uri_path);
+
+               ret = iotcon_representation_get_state(child_repr, &child_state);
+               if (IOTCON_ERROR_NONE != ret) {
+                       ERR("iotcon_representation_get_state() Fail(%d)", ret);
+                       continue;
+               }
+
+               if (TEST_STR_EQUAL == strcmp("/a/light", uri_path)) {
+                       ret = iotcon_state_get_keys_count(child_state, &key_count);
+                       if (IOTCON_ERROR_NONE != ret) {
+                               ERR("iotcon_state_get_keys_count() Fail(%d)", ret);
+                               continue;
+                       }
+
+                       if (key_count) {
+                               ret = iotcon_state_get_int(child_state, "brightness", &int_val);
+                               if (IOTCON_ERROR_NONE != ret) {
+                                       ERR("iotcon_state_get_int() Fail(%d)", ret);
+                                       continue;
+                               }
+                               DBG("brightness : %d", int_val);
+                       }
+               } else if (TEST_STR_EQUAL == strcmp("/a/switch", uri_path)) {
+                       ret = iotcon_state_get_keys_count(child_state, &key_count);
+                       if (IOTCON_ERROR_NONE != ret) {
+                               ERR("iotcon_state_get_keys_count() Fail(%d)", ret);
+                               continue;
+                       }
+                       if (key_count) {
+                               ret = iotcon_state_get_bool(child_state, "switch", &bool_val);
+                               if (IOTCON_ERROR_NONE != ret) {
+                                       ERR("iotcon_state_get_bool() Fail(%d)", ret);
+                                       continue;
+                               }
+                               DBG("switch : %d", bool_val);
+                       }
+               }
+       }
+}
+
+static void _on_get_2nd(iotcon_remote_resource_h resource,
+               iotcon_representation_h recv_repr,
+               iotcon_options_h header_options,
+               int response_result,
+               void *user_data)
+{
+       _on_get(recv_repr, response_result);
+}
+
+static void _on_get_1st(iotcon_remote_resource_h resource,
+               iotcon_representation_h recv_repr,
+               iotcon_options_h header_options,
+               int response_result,
+               void *user_data)
+{
+       int ret;
+       iotcon_query_h query_params;
+
+       _on_get(recv_repr, response_result);
+
+       ret = iotcon_query_create(&query_params);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_query_create() Fail(%d)", ret);
+               return;
+       }
+
+       ret = iotcon_query_add(query_params, "if", "oic.if.b");
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_query_add() Fail(%d)", ret);
+               iotcon_query_destroy(query_params);
+               return;
+       }
+
+       /* send GET request again with BATCH interface */
+       ret = iotcon_remote_resource_get(resource, query_params, _on_get_2nd, NULL);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_remote_resource_get() Fail(%d)", ret);
+               iotcon_query_destroy(query_params);
+               return;
+       }
+
+       iotcon_query_destroy(query_params);
+}
+
+static int _get_res_type_fn(const char *string, void *user_data)
+{
+       char *resource_uri_path = user_data;
+
+       DBG("[%s] resource type : %s", resource_uri_path, string);
+
+       return IOTCON_FUNC_CONTINUE;
+}
+
+static int _device_id_compare(const void *a, const void *b)
+{
+       return strcmp(a, b);
+}
+
+static void _found_resource(iotcon_remote_resource_h resource, int result, void *user_data)
+{
+       GList *node;
+       char *resource_host;
+       char *resource_uri_path;
+       char *resource_device_id;
+       int ret, resource_interfaces;
+       iotcon_resource_types_h resource_types;
+
+       RETM_IF(IOTCON_ERROR_NONE != result, "Invalid result(%d)", result);
+
+       if (NULL == resource)
+               return;
+
+       INFO("===== resource found =====");
+
+       /* get the resource URI */
+       ret = iotcon_remote_resource_get_uri_path(resource, &resource_uri_path);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_remote_resource_get_uri_path() Fail(%d)", ret);
+               return;
+       }
+
+       /* get the resource device id */
+       ret = iotcon_remote_resource_get_device_id(resource, &resource_device_id);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_remote_resource_get_device_id() Fail(%d)", ret);
+               return;
+       }
+       DBG("[%s] resource device id : %s", resource_uri_path, resource_device_id);
+
+       node = g_list_find_custom(device_id_list, resource_device_id, _device_id_compare);
+
+       if (node && TEST_STR_EQUAL == strcmp(room_uri_path, resource_uri_path)) {
+               DBG("uri_path \"%s\" already found. skip !", resource_uri_path);
+               return;
+       }
+
+       room_resource_device_id = strdup(resource_device_id);
+       if (NULL == room_resource_device_id) {
+               ERR("strdup(room_resource_device_id) Fail");
+               return;
+       }
+
+       device_id_list = g_list_append(device_id_list, room_resource_device_id);
+
+       /* get the resource host address */
+       ret = iotcon_remote_resource_get_host(resource, &resource_host);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_remote_resource_get_host() Fail(%d)", ret);
+               device_id_list = g_list_remove(device_id_list, room_resource_device_id);
+               free(room_resource_device_id);
+               return;
+       }
+       DBG("[%s] resource host : %s", resource_uri_path, resource_host);
+
+       /* get the resource interfaces */
+       ret = iotcon_remote_resource_get_interfaces(resource, &resource_interfaces);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_remote_resource_get_interfaces() Fail(%d)", ret);
+               device_id_list = g_list_remove(device_id_list, room_resource_device_id);
+               free(room_resource_device_id);
+               return;
+       }
+       if (IOTCON_INTERFACE_DEFAULT & resource_interfaces)
+               DBG("[%s] resource interface : DEFAULT_INTERFACE", resource_uri_path);
+       if (IOTCON_INTERFACE_LINK & resource_interfaces)
+               DBG("[%s] resource interface : LINK_INTERFACE", resource_uri_path);
+       if (IOTCON_INTERFACE_BATCH & resource_interfaces)
+               DBG("[%s] resource interface : BATCH_INTERFACE", resource_uri_path);
+       if (IOTCON_INTERFACE_GROUP & resource_interfaces)
+               DBG("[%s] resource interface : GROUP_INTERFACE", resource_uri_path);
+
+       /* get the resource types */
+       ret = iotcon_remote_resource_get_types(resource, &resource_types);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_remote_resource_get_types() Fail(%d)", ret);
+               device_id_list = g_list_remove(device_id_list, room_resource_device_id);
+               free(room_resource_device_id);
+               return;
+       }
+       ret = iotcon_resource_types_foreach(resource_types, _get_res_type_fn,
+                       resource_uri_path);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_resource_types_foreach() Fail(%d)", ret);
+               device_id_list = g_list_remove(device_id_list, room_resource_device_id);
+               free(room_resource_device_id);
+               return;
+       }
+
+       if (TEST_STR_EQUAL == strcmp(room_uri_path, resource_uri_path)) {
+               /* send GET request */
+               ret = iotcon_remote_resource_get(resource, NULL, _on_get_1st, NULL);
+               if (IOTCON_ERROR_NONE != ret)
+                       ERR("iotcon_remote_resource_get() Fail(%d)", ret);
+       }
+
+       device_id_list = g_list_remove(device_id_list, room_resource_device_id);
+       free(room_resource_device_id);
+}
+
+int main(int argc, char **argv)
+{
+       FN_CALL;
+       int ret;
+       GMainLoop *loop;
+
+       loop = g_main_loop_new(NULL, FALSE);
+
+       /* iotcon open */
+       ret = iotcon_open();
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_open() Fail(%d)", ret);
+               return -1;
+       }
+
+       /* find room typed resources */
+       ret = iotcon_find_resource(IOTCON_MULTICAST_ADDRESS, "core.room", &_found_resource,
+                       NULL);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_find_resource() Fail(%d)", ret);
+               iotcon_close();
+               return -1;
+       }
+
+       g_main_loop_run(loop);
+       g_main_loop_unref(loop);
+
+       g_list_free_full(device_id_list, free);
+
+       /* iotcon close */
+       iotcon_close();
+
+       return 0;
+}
diff --git a/test/iotcon-test-repr-server.c b/test/iotcon-test-repr-server.c
new file mode 100644 (file)
index 0000000..42d3935
--- /dev/null
@@ -0,0 +1,576 @@
+/*
+ * 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.h"
+
+static int _send_response(iotcon_response_h response, iotcon_representation_h repr,
+               iotcon_interface_e iface)
+{
+       int ret;
+
+       ret = iotcon_response_set_representation(response, repr);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_response_set_representation() Fail(%d)", ret);
+               return -1;
+       }
+
+       ret = iotcon_response_set_interface(response, iface);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_response_set_interface() Fail(%d)", ret);
+               return -1;
+       }
+
+       ret = iotcon_response_set_result(response, IOTCON_RESPONSE_RESULT_OK);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_response_set_result() Fail(%d)", ret);
+               return -1;
+       }
+
+       /* send Representation to the client */
+       ret = iotcon_response_send(response);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_response_send() Fail(%d)", ret);
+               return -1;
+       }
+
+       return 0;
+}
+
+static void _light_request_handler_get(iotcon_response_h response)
+{
+       int ret;
+       iotcon_representation_h resp_repr;
+
+       INFO("GET request - Light");
+
+       /* create a light Representation */
+       ret = iotcon_representation_create(&resp_repr);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_representation_create() Fail(%d)", ret);
+               return;
+       }
+
+       ret = _send_response(response, resp_repr, IOTCON_INTERFACE_DEFAULT);
+       if (0 != ret)
+               ERR("_send_response() Fail(%d)", ret);
+
+       iotcon_representation_destroy(resp_repr);
+}
+
+static void _room_request_handler_get(iotcon_request_h request,
+               iotcon_response_h response)
+{
+       int ret;
+       iotcon_representation_h room_repr, light_repr, switch_repr;
+       iotcon_state_h room_state, light_state, switch_state;
+       iotcon_list_h temperature_list;
+
+       iotcon_query_h query;
+       char *query_str = NULL;
+
+       iotcon_interface_e iface;
+
+       INFO("GET request - Room");
+
+       /* create a room Representation */
+       ret = iotcon_representation_create(&room_repr);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_representation_create() Fail(%d)", ret);
+               return;
+       }
+
+       /* create a room state */
+       ret = iotcon_state_create(&room_state);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_state_create() Fail(%d)", ret);
+               iotcon_representation_destroy(room_repr);
+               return;
+       }
+
+       ret = iotcon_representation_set_uri_path(room_repr, "/a/room");
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_representation_set_uri_path() Fail(%d)", ret);
+               iotcon_state_destroy(room_state);
+               iotcon_representation_destroy(room_repr);
+               return;
+       }
+
+       ret = iotcon_state_set_str(room_state, "name", "Michael's Room");
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_state_set_str() Fail(%d)", ret);
+               iotcon_state_destroy(room_state);
+               iotcon_representation_destroy(room_repr);
+               return;
+       }
+
+       /* set null */
+       ret = iotcon_state_set_null(room_state, "null value");
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_state_set_null() Fail(%d)", ret);
+               iotcon_state_destroy(room_state);
+               iotcon_representation_destroy(room_repr);
+               return;
+       }
+
+       ret = iotcon_list_create(IOTCON_TYPE_INT, &temperature_list);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_list_create() Fail(%d)", ret);
+               iotcon_state_destroy(room_state);
+               iotcon_representation_destroy(room_repr);
+               return;
+       }
+
+       ret = iotcon_list_add_int(temperature_list, 22, -1);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_list_add_int() Fail(%d)", ret);
+               iotcon_state_destroy(room_state);
+               iotcon_representation_destroy(room_repr);
+               return;
+       }
+
+       ret = iotcon_list_add_int(temperature_list, 23, -1);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_list_add_int() Fail(%d)", ret);
+               iotcon_state_destroy(room_state);
+               iotcon_representation_destroy(room_repr);
+               return;
+       }
+
+       ret = iotcon_list_add_int(temperature_list, 24, -1);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_list_add_int() Fail(%d)", ret);
+               iotcon_state_destroy(room_state);
+               iotcon_representation_destroy(room_repr);
+               return;
+       }
+
+       ret = iotcon_list_add_int(temperature_list, 25, -1);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_list_add_int() Fail(%d)", ret);
+               iotcon_state_destroy(room_state);
+               iotcon_representation_destroy(room_repr);
+               return;
+       }
+
+       ret = iotcon_list_add_int(temperature_list, 26, -1);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_list_add_int() Fail(%d)", ret);
+               iotcon_state_destroy(room_state);
+               iotcon_representation_destroy(room_repr);
+               return;
+       }
+
+       ret = iotcon_state_set_list(room_state, "today_temp", temperature_list);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_state_set_list() Fail(%d)", ret);
+               iotcon_state_destroy(room_state);
+               iotcon_representation_destroy(room_repr);
+               return;
+       }
+
+       iotcon_list_destroy(temperature_list);
+
+       /* Set a room state into room Representation */
+       ret = iotcon_representation_set_state(room_repr, room_state);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_representation_set_state() Fail(%d)", ret);
+               iotcon_state_destroy(room_state);
+               iotcon_representation_destroy(room_repr);
+               return;
+       }
+
+       iotcon_state_destroy(room_state);
+
+       /* create a light Representation */
+       ret = iotcon_representation_create(&light_repr);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_representation_create() Fail(%d)", ret);
+               iotcon_representation_destroy(room_repr);
+               return;
+       }
+
+       ret = iotcon_representation_set_uri_path(light_repr, "/a/light");
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_representation_set_uri_path() Fail(%d)", ret);
+               iotcon_representation_destroy(light_repr);
+               iotcon_representation_destroy(room_repr);
+               return;
+       }
+
+       /* create a light state */
+       ret = iotcon_state_create(&light_state);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_state_create() Fail(%d)", ret);
+               iotcon_state_destroy(light_state);
+               iotcon_representation_destroy(light_repr);
+               iotcon_representation_destroy(room_repr);
+               return;
+       }
+
+       ret = iotcon_state_set_int(light_state, "brightness", 50);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_state_set_int() Fail(%d)", ret);
+               iotcon_state_destroy(light_state);
+               iotcon_representation_destroy(light_repr);
+               iotcon_representation_destroy(room_repr);
+               return;
+       }
+
+       /* Set a light state into light Representation */
+       ret = iotcon_representation_set_state(light_repr, light_state);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_representation_set_state() Fail(%d)", ret);
+               iotcon_state_destroy(light_state);
+               iotcon_representation_destroy(light_repr);
+               iotcon_representation_destroy(room_repr);
+               return;
+       }
+
+       iotcon_state_destroy(light_state);
+
+       ret = iotcon_representation_append_child(room_repr, light_repr);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_representation_append_child() Fail(%d)", ret);
+               iotcon_representation_destroy(light_repr);
+               iotcon_representation_destroy(room_repr);
+               return;
+       }
+
+       iotcon_representation_destroy(light_repr);
+
+       /* create a switch Representation */
+       ret = iotcon_representation_create(&switch_repr);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_representation_create() Fail(%d)", ret);
+               iotcon_representation_destroy(room_repr);
+               return;
+       }
+
+       ret = iotcon_representation_set_uri_path(switch_repr, "/a/switch");
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_representation_set_uri_path() Fail(%d)", ret);
+               iotcon_representation_destroy(switch_repr);
+               iotcon_representation_destroy(room_repr);
+               return;
+       }
+
+       /* create a switch state */
+       ret = iotcon_state_create(&switch_state);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_state_create() Fail(%d)", ret);
+               iotcon_representation_destroy(switch_repr);
+               iotcon_representation_destroy(room_repr);
+               return;
+       }
+
+       ret = iotcon_state_set_bool(switch_state, "switch", false);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_state_set_bool() Fail(%d)", ret);
+               iotcon_state_destroy(switch_state);
+               iotcon_representation_destroy(switch_repr);
+               iotcon_representation_destroy(room_repr);
+               return;
+       }
+
+       /* Set a light state into light Representation */
+       ret = iotcon_representation_set_state(switch_repr, switch_state);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_representation_set_state() Fail(%d)", ret);
+               iotcon_state_destroy(switch_state);
+               iotcon_representation_destroy(switch_repr);
+               iotcon_representation_destroy(room_repr);
+               return;
+       }
+
+       iotcon_state_destroy(switch_state);
+
+       ret = iotcon_representation_append_child(room_repr, switch_repr);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_representation_append_child() Fail(%d)", ret);
+               iotcon_representation_destroy(switch_repr);
+               iotcon_representation_destroy(room_repr);
+               return;
+       }
+
+       iotcon_representation_destroy(switch_repr);
+
+       ret = iotcon_request_get_query(request, &query);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_request_get_query() Fail(%d)", ret);
+               iotcon_representation_destroy(room_repr);
+               return;
+       }
+
+       if (query) {
+               ret = iotcon_query_lookup(query, "if", &query_str);
+               if (IOTCON_ERROR_NONE != ret) {
+                       ERR("iotcon_query_lookup() Fail(%d)", ret);
+                       iotcon_representation_destroy(room_repr);
+                       return;
+               }
+       }
+
+       if (query_str && (TEST_STR_EQUAL == strcmp("oic.if.b", query_str))) {
+               DBG("operation for BATCH interface");
+               iface = IOTCON_INTERFACE_BATCH;
+       } else {
+               DBG("operation for DEFAULT interface");
+               iface = IOTCON_INTERFACE_DEFAULT;
+       }
+
+       ret = _send_response(response, room_repr, iface);
+       if (0 != ret)
+               ERR("_send_response() Fail(%d)", ret);
+
+       iotcon_representation_destroy(room_repr);
+}
+
+static void _request_handler_put(iotcon_request_h request, iotcon_response_h response)
+{
+       int ret;
+       iotcon_representation_h resp_repr;
+
+       ret = iotcon_representation_create(&resp_repr);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_representation_create() Fail(%d)", ret);
+               return;
+       }
+
+       INFO("PUT request");
+
+       /* do PUT operation */
+
+       ret = _send_response(response, resp_repr, IOTCON_INTERFACE_DEFAULT);
+       if (0 != ret)
+               ERR("_send_response() Fail(%d)", ret);
+
+       iotcon_representation_destroy(resp_repr);
+}
+
+static void _request_handler_post(iotcon_response_h response)
+{
+       int ret;
+       iotcon_representation_h resp_repr;
+
+       ret = iotcon_representation_create(&resp_repr);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_representation_create() Fail(%d)", ret);
+               return;
+       }
+
+       INFO("POST request");
+
+       /* do POST operation */
+
+       ret = _send_response(response, resp_repr, IOTCON_INTERFACE_DEFAULT);
+       if (0 != ret)
+               ERR("_send_response() Fail(%d)", ret);
+
+       iotcon_representation_destroy(resp_repr);
+}
+
+static void _request_handler_delete(iotcon_response_h response)
+{
+       int ret;
+       iotcon_representation_h resp_repr;
+
+       ret = iotcon_representation_create(&resp_repr);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_representation_create() Fail(%d)", ret);
+               return;
+       }
+
+       INFO("DELETE request");
+
+       /* do DELETE operation */
+
+       ret = _send_response(response, resp_repr, IOTCON_INTERFACE_DEFAULT);
+       if (0 != ret)
+               ERR("_send_response() Fail(%d)", ret);
+
+       iotcon_representation_destroy(resp_repr);
+}
+
+static void _light_request_handler(iotcon_resource_h resource, iotcon_request_h request,
+               void *user_data)
+{
+       int ret;
+       int types;
+       iotcon_response_h response;
+
+       RET_IF(NULL == request);
+
+       ret = iotcon_request_get_types(request, &types);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_request_get_types() Fail(%d)", ret);
+               return;
+       }
+
+       ret = iotcon_response_create(request, &response);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_response_create() Fail");
+               return;
+       }
+
+       if (IOTCON_REQUEST_GET & types)
+               _light_request_handler_get(response);
+
+       else if (IOTCON_REQUEST_PUT & types)
+               _request_handler_put(request, response);
+
+       else if (IOTCON_REQUEST_POST & types)
+               _request_handler_post(response);
+
+       else if (IOTCON_REQUEST_DELETE & types)
+               _request_handler_delete(response);
+
+       iotcon_response_destroy(response);
+}
+
+static void _room_request_handler(iotcon_resource_h resource, iotcon_request_h request,
+               void *user_data)
+{
+       FN_CALL;
+       int ret, types;
+       iotcon_response_h response;
+
+       RET_IF(NULL == request);
+
+       ret = iotcon_request_get_types(request, &types);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_request_get_types() Fail(%d)", ret);
+               return;
+       }
+
+       ret = iotcon_response_create(request, &response);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_response_create() Fail(%d)", ret);
+               return;
+       }
+
+       if (IOTCON_REQUEST_GET & types)
+               _room_request_handler_get(request, response);
+
+       else if (IOTCON_REQUEST_PUT & types)
+               _request_handler_put(request, response);
+
+       else if (IOTCON_REQUEST_POST & types)
+               _request_handler_post(response);
+
+       else if (IOTCON_REQUEST_DELETE & types)
+               _request_handler_delete(response);
+
+       iotcon_response_destroy(response);
+}
+
+int main(int argc, char **argv)
+{
+       FN_CALL;
+       int ret;
+       GMainLoop *loop;
+       iotcon_resource_h room_handle, light_handle;
+       iotcon_resource_types_h room_rtypes, light_rtypes;
+
+       loop = g_main_loop_new(NULL, FALSE);
+
+       /* iotcon open */
+       ret = iotcon_open();
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_open() Fail(%d)", ret);
+               return -1;
+       }
+
+       /* register room resource */
+       ret = iotcon_resource_types_create(&room_rtypes);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_resource_types_create() Fail(%d)", ret);
+               iotcon_close();
+               return -1;
+       }
+       ret = iotcon_resource_types_add(room_rtypes, "core.room");
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_resource_types_add(%s) Fail(%d)", "core.room", ret);
+               iotcon_resource_types_destroy(room_rtypes);
+               iotcon_close();
+               return -1;
+       }
+
+       ret = iotcon_resource_create("/a/room", room_rtypes,
+                       (IOTCON_INTERFACE_DEFAULT | IOTCON_INTERFACE_BATCH),
+                       (IOTCON_DISCOVERABLE | IOTCON_OBSERVABLE), _room_request_handler,
+                       NULL, &room_handle);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_resource_create() Fail(%d)", ret);
+               iotcon_resource_types_destroy(room_rtypes);
+               iotcon_close();
+               return -1;
+       }
+       iotcon_resource_types_destroy(room_rtypes);
+
+       /* register light resource */
+       ret = iotcon_resource_types_create(&light_rtypes);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_resource_types_create() Fail(%d)", ret);
+               iotcon_resource_destroy(room_handle);
+               iotcon_close();
+               return -1;
+       }
+       ret = iotcon_resource_types_add(light_rtypes, "core.light");
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_resource_types_add(%s) Fail(%d)", "core.light", ret);
+               iotcon_resource_types_destroy(light_rtypes);
+               iotcon_resource_destroy(room_handle);
+               iotcon_close();
+               return -1;
+       }
+
+       ret = iotcon_resource_create("/a/light", light_rtypes,
+                       (IOTCON_INTERFACE_DEFAULT | IOTCON_INTERFACE_BATCH),
+                       (IOTCON_DISCOVERABLE | IOTCON_OBSERVABLE), _light_request_handler,
+                       NULL, &light_handle);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_resource_create() Fail");
+               iotcon_resource_types_destroy(light_rtypes);
+               iotcon_resource_destroy(room_handle);
+               iotcon_close();
+               return -1;
+       }
+       iotcon_resource_types_destroy(light_rtypes);
+
+       ret = iotcon_resource_bind_child_resource(room_handle, light_handle);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_resource_bind_child_resource() Fail");
+               iotcon_resource_destroy(light_handle);
+               iotcon_resource_destroy(room_handle);
+               iotcon_close();
+               return -1;
+       }
+
+       g_main_loop_run(loop);
+       g_main_loop_unref(loop);
+
+       iotcon_resource_destroy(light_handle);
+       iotcon_resource_destroy(room_handle);
+
+       /* iotcon close */
+       iotcon_close();
+
+       return 0;
+}
diff --git a/test/repr-test-client.c b/test/repr-test-client.c
deleted file mode 100644 (file)
index aa5117c..0000000
+++ /dev/null
@@ -1,352 +0,0 @@
-/*
- * 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.h"
-
-static char *room_resource_device_id;
-static GList *device_id_list;
-
-static const char* const room_uri_path = "/a/room";
-
-static int _get_int_list_cb(int pos, const int value, void *user_data)
-{
-       DBG("%d°C", value);
-
-       return IOTCON_FUNC_CONTINUE;
-}
-
-static void _on_get(iotcon_representation_h recv_repr, int response_result)
-{
-       int i, ret, int_val;
-       bool is_null, bool_val;
-       char *uri_path, *str_val;
-       iotcon_list_h list_val;
-       iotcon_representation_h child_repr;
-       iotcon_state_h recv_state, child_state;
-       unsigned int key_count, children_count;
-
-       RETM_IF(IOTCON_RESPONSE_RESULT_OK != response_result, "_on_get Response error(%d)",
-                       response_result);
-       INFO("GET request was successful");
-
-       DBG("[ parent representation ]");
-       ret = iotcon_representation_get_uri_path(recv_repr, &uri_path);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_representation_get_uri_path() Fail(%d)", ret);
-               return;
-       }
-       DBG("uri_path : %s", uri_path);
-
-       ret = iotcon_representation_get_state(recv_repr, &recv_state);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_representation_get_state() Fail(%d)", ret);
-               return;
-       }
-
-       ret = iotcon_state_get_keys_count(recv_state, &key_count);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_state_get_keys_count() Fail(%d)", ret);
-               return;
-       }
-
-       if (key_count) {
-               ret = iotcon_state_get_str(recv_state, "name", &str_val);
-               if (IOTCON_ERROR_NONE != ret) {
-                       ERR("iotcon_state_get_str() Fail(%d)", ret);
-                       return;
-               }
-               DBG("name : %s", str_val);
-
-               ret = iotcon_state_get_list(recv_state, "today_temp", &list_val);
-               if (IOTCON_ERROR_NONE != ret) {
-                       ERR("iotcon_state_get_list() Fail(%d)", ret);
-                       return;
-               }
-
-               DBG("today's temperature :");
-               ret = iotcon_list_foreach_int(list_val, _get_int_list_cb, NULL);
-               if (IOTCON_ERROR_NONE != ret) {
-                       ERR("iotcon_list_foreach_int() Fail(%d)", ret);
-                       return;
-               }
-
-               ret = iotcon_state_is_null(recv_state, "null value", &is_null);
-               if (IOTCON_ERROR_NONE != ret) {
-                       ERR("iotcon_state_is_null() Fail(%d)", ret);
-                       return;
-               }
-
-               if (is_null)
-                       DBG("null value is null");
-       }
-
-       ret = iotcon_representation_get_children_count(recv_repr, &children_count);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_representation_get_children_count() Fail(%d)", ret);
-               return;
-       }
-
-       for (i = 0; i < children_count; i++) {
-               DBG("[ child representation ]");
-
-               ret = iotcon_representation_get_nth_child(recv_repr, i, &child_repr);
-               if (IOTCON_ERROR_NONE != ret) {
-                       ERR("iotcon_representation_get_nth_child(%d) Fail(%d)", i, ret);
-                       continue;
-               }
-
-               ret = iotcon_representation_get_uri_path(child_repr, &uri_path);
-               if (IOTCON_ERROR_NONE != ret) {
-                       ERR("iotcon_representation_get_uri_path() Fail(%d)", ret);
-                       continue;
-               }
-               DBG("uri_path : %s", uri_path);
-
-               ret = iotcon_representation_get_state(child_repr, &child_state);
-               if (IOTCON_ERROR_NONE != ret) {
-                       ERR("iotcon_representation_get_state() Fail(%d)", ret);
-                       continue;
-               }
-
-               if (TEST_STR_EQUAL == strcmp("/a/light", uri_path)) {
-                       ret = iotcon_state_get_keys_count(child_state, &key_count);
-                       if (IOTCON_ERROR_NONE != ret) {
-                               ERR("iotcon_state_get_keys_count() Fail(%d)", ret);
-                               continue;
-                       }
-
-                       if (key_count) {
-                               ret = iotcon_state_get_int(child_state, "brightness", &int_val);
-                               if (IOTCON_ERROR_NONE != ret) {
-                                       ERR("iotcon_state_get_int() Fail(%d)", ret);
-                                       continue;
-                               }
-                               DBG("brightness : %d", int_val);
-                       }
-               } else if (TEST_STR_EQUAL == strcmp("/a/switch", uri_path)) {
-                       ret = iotcon_state_get_keys_count(child_state, &key_count);
-                       if (IOTCON_ERROR_NONE != ret) {
-                               ERR("iotcon_state_get_keys_count() Fail(%d)", ret);
-                               continue;
-                       }
-                       if (key_count) {
-                               ret = iotcon_state_get_bool(child_state, "switch", &bool_val);
-                               if (IOTCON_ERROR_NONE != ret) {
-                                       ERR("iotcon_state_get_bool() Fail(%d)", ret);
-                                       continue;
-                               }
-                               DBG("switch : %d", bool_val);
-                       }
-               }
-       }
-}
-
-static void _on_get_2nd(iotcon_remote_resource_h resource,
-               iotcon_representation_h recv_repr,
-               iotcon_options_h header_options,
-               int response_result,
-               void *user_data)
-{
-       _on_get(recv_repr, response_result);
-}
-
-static void _on_get_1st(iotcon_remote_resource_h resource,
-               iotcon_representation_h recv_repr,
-               iotcon_options_h header_options,
-               int response_result,
-               void *user_data)
-{
-       int ret;
-       iotcon_query_h query_params;
-
-       _on_get(recv_repr, response_result);
-
-       ret = iotcon_query_create(&query_params);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_query_create() Fail(%d)", ret);
-               return;
-       }
-
-       ret = iotcon_query_add(query_params, "if", "oic.if.b");
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_query_add() Fail(%d)", ret);
-               iotcon_query_destroy(query_params);
-               return;
-       }
-
-       /* send GET request again with BATCH interface */
-       ret = iotcon_remote_resource_get(resource, query_params, _on_get_2nd, NULL);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_remote_resource_get() Fail(%d)", ret);
-               iotcon_query_destroy(query_params);
-               return;
-       }
-
-       iotcon_query_destroy(query_params);
-}
-
-static int _get_res_type_fn(const char *string, void *user_data)
-{
-       char *resource_uri_path = user_data;
-
-       DBG("[%s] resource type : %s", resource_uri_path, string);
-
-       return IOTCON_FUNC_CONTINUE;
-}
-
-static int _device_id_compare(const void *a, const void *b)
-{
-       return strcmp(a, b);
-}
-
-static void _found_resource(iotcon_remote_resource_h resource, int result, void *user_data)
-{
-       GList *node;
-       char *resource_host;
-       char *resource_uri_path;
-       char *resource_device_id;
-       int ret, resource_interfaces;
-       iotcon_resource_types_h resource_types;
-
-       RETM_IF(IOTCON_ERROR_NONE != result, "Invalid result(%d)", result);
-
-       if (NULL == resource)
-               return;
-
-       INFO("===== resource found =====");
-
-       /* get the resource URI */
-       ret = iotcon_remote_resource_get_uri_path(resource, &resource_uri_path);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_remote_resource_get_uri_path() Fail(%d)", ret);
-               return;
-       }
-
-       /* get the resource device id */
-       ret = iotcon_remote_resource_get_device_id(resource, &resource_device_id);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_remote_resource_get_device_id() Fail(%d)", ret);
-               return;
-       }
-       DBG("[%s] resource device id : %s", resource_uri_path, resource_device_id);
-
-       node = g_list_find_custom(device_id_list, resource_device_id, _device_id_compare);
-
-       if (node && TEST_STR_EQUAL == strcmp(room_uri_path, resource_uri_path)) {
-               DBG("uri_path \"%s\" already found. skip !", resource_uri_path);
-               return;
-       }
-
-       room_resource_device_id = strdup(resource_device_id);
-       if (NULL == room_resource_device_id) {
-               ERR("strdup(room_resource_device_id) Fail");
-               return;
-       }
-
-       device_id_list = g_list_append(device_id_list, room_resource_device_id);
-
-       /* get the resource host address */
-       ret = iotcon_remote_resource_get_host(resource, &resource_host);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_remote_resource_get_host() Fail(%d)", ret);
-               device_id_list = g_list_remove(device_id_list, room_resource_device_id);
-               free(room_resource_device_id);
-               return;
-       }
-       DBG("[%s] resource host : %s", resource_uri_path, resource_host);
-
-       /* get the resource interfaces */
-       ret = iotcon_remote_resource_get_interfaces(resource, &resource_interfaces);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_remote_resource_get_interfaces() Fail(%d)", ret);
-               device_id_list = g_list_remove(device_id_list, room_resource_device_id);
-               free(room_resource_device_id);
-               return;
-       }
-       if (IOTCON_INTERFACE_DEFAULT & resource_interfaces)
-               DBG("[%s] resource interface : DEFAULT_INTERFACE", resource_uri_path);
-       if (IOTCON_INTERFACE_LINK & resource_interfaces)
-               DBG("[%s] resource interface : LINK_INTERFACE", resource_uri_path);
-       if (IOTCON_INTERFACE_BATCH & resource_interfaces)
-               DBG("[%s] resource interface : BATCH_INTERFACE", resource_uri_path);
-       if (IOTCON_INTERFACE_GROUP & resource_interfaces)
-               DBG("[%s] resource interface : GROUP_INTERFACE", resource_uri_path);
-
-       /* get the resource types */
-       ret = iotcon_remote_resource_get_types(resource, &resource_types);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_remote_resource_get_types() Fail(%d)", ret);
-               device_id_list = g_list_remove(device_id_list, room_resource_device_id);
-               free(room_resource_device_id);
-               return;
-       }
-       ret = iotcon_resource_types_foreach(resource_types, _get_res_type_fn,
-                       resource_uri_path);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_resource_types_foreach() Fail(%d)", ret);
-               device_id_list = g_list_remove(device_id_list, room_resource_device_id);
-               free(room_resource_device_id);
-               return;
-       }
-
-       if (TEST_STR_EQUAL == strcmp(room_uri_path, resource_uri_path)) {
-               /* send GET request */
-               ret = iotcon_remote_resource_get(resource, NULL, _on_get_1st, NULL);
-               if (IOTCON_ERROR_NONE != ret)
-                       ERR("iotcon_remote_resource_get() Fail(%d)", ret);
-       }
-
-       device_id_list = g_list_remove(device_id_list, room_resource_device_id);
-       free(room_resource_device_id);
-}
-
-int main(int argc, char **argv)
-{
-       FN_CALL;
-       int ret;
-       GMainLoop *loop;
-
-       loop = g_main_loop_new(NULL, FALSE);
-
-       /* iotcon open */
-       ret = iotcon_open();
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_open() Fail(%d)", ret);
-               return -1;
-       }
-
-       /* find room typed resources */
-       ret = iotcon_find_resource(IOTCON_MULTICAST_ADDRESS, "core.room", &_found_resource,
-                       NULL);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_find_resource() Fail(%d)", ret);
-               iotcon_close();
-               return -1;
-       }
-
-       g_main_loop_run(loop);
-       g_main_loop_unref(loop);
-
-       g_list_free_full(device_id_list, free);
-
-       /* iotcon close */
-       iotcon_close();
-
-       return 0;
-}
diff --git a/test/repr-test-server.c b/test/repr-test-server.c
deleted file mode 100644 (file)
index 42d3935..0000000
+++ /dev/null
@@ -1,576 +0,0 @@
-/*
- * 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.h"
-
-static int _send_response(iotcon_response_h response, iotcon_representation_h repr,
-               iotcon_interface_e iface)
-{
-       int ret;
-
-       ret = iotcon_response_set_representation(response, repr);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_response_set_representation() Fail(%d)", ret);
-               return -1;
-       }
-
-       ret = iotcon_response_set_interface(response, iface);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_response_set_interface() Fail(%d)", ret);
-               return -1;
-       }
-
-       ret = iotcon_response_set_result(response, IOTCON_RESPONSE_RESULT_OK);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_response_set_result() Fail(%d)", ret);
-               return -1;
-       }
-
-       /* send Representation to the client */
-       ret = iotcon_response_send(response);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_response_send() Fail(%d)", ret);
-               return -1;
-       }
-
-       return 0;
-}
-
-static void _light_request_handler_get(iotcon_response_h response)
-{
-       int ret;
-       iotcon_representation_h resp_repr;
-
-       INFO("GET request - Light");
-
-       /* create a light Representation */
-       ret = iotcon_representation_create(&resp_repr);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_representation_create() Fail(%d)", ret);
-               return;
-       }
-
-       ret = _send_response(response, resp_repr, IOTCON_INTERFACE_DEFAULT);
-       if (0 != ret)
-               ERR("_send_response() Fail(%d)", ret);
-
-       iotcon_representation_destroy(resp_repr);
-}
-
-static void _room_request_handler_get(iotcon_request_h request,
-               iotcon_response_h response)
-{
-       int ret;
-       iotcon_representation_h room_repr, light_repr, switch_repr;
-       iotcon_state_h room_state, light_state, switch_state;
-       iotcon_list_h temperature_list;
-
-       iotcon_query_h query;
-       char *query_str = NULL;
-
-       iotcon_interface_e iface;
-
-       INFO("GET request - Room");
-
-       /* create a room Representation */
-       ret = iotcon_representation_create(&room_repr);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_representation_create() Fail(%d)", ret);
-               return;
-       }
-
-       /* create a room state */
-       ret = iotcon_state_create(&room_state);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_state_create() Fail(%d)", ret);
-               iotcon_representation_destroy(room_repr);
-               return;
-       }
-
-       ret = iotcon_representation_set_uri_path(room_repr, "/a/room");
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_representation_set_uri_path() Fail(%d)", ret);
-               iotcon_state_destroy(room_state);
-               iotcon_representation_destroy(room_repr);
-               return;
-       }
-
-       ret = iotcon_state_set_str(room_state, "name", "Michael's Room");
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_state_set_str() Fail(%d)", ret);
-               iotcon_state_destroy(room_state);
-               iotcon_representation_destroy(room_repr);
-               return;
-       }
-
-       /* set null */
-       ret = iotcon_state_set_null(room_state, "null value");
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_state_set_null() Fail(%d)", ret);
-               iotcon_state_destroy(room_state);
-               iotcon_representation_destroy(room_repr);
-               return;
-       }
-
-       ret = iotcon_list_create(IOTCON_TYPE_INT, &temperature_list);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_list_create() Fail(%d)", ret);
-               iotcon_state_destroy(room_state);
-               iotcon_representation_destroy(room_repr);
-               return;
-       }
-
-       ret = iotcon_list_add_int(temperature_list, 22, -1);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_list_add_int() Fail(%d)", ret);
-               iotcon_state_destroy(room_state);
-               iotcon_representation_destroy(room_repr);
-               return;
-       }
-
-       ret = iotcon_list_add_int(temperature_list, 23, -1);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_list_add_int() Fail(%d)", ret);
-               iotcon_state_destroy(room_state);
-               iotcon_representation_destroy(room_repr);
-               return;
-       }
-
-       ret = iotcon_list_add_int(temperature_list, 24, -1);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_list_add_int() Fail(%d)", ret);
-               iotcon_state_destroy(room_state);
-               iotcon_representation_destroy(room_repr);
-               return;
-       }
-
-       ret = iotcon_list_add_int(temperature_list, 25, -1);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_list_add_int() Fail(%d)", ret);
-               iotcon_state_destroy(room_state);
-               iotcon_representation_destroy(room_repr);
-               return;
-       }
-
-       ret = iotcon_list_add_int(temperature_list, 26, -1);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_list_add_int() Fail(%d)", ret);
-               iotcon_state_destroy(room_state);
-               iotcon_representation_destroy(room_repr);
-               return;
-       }
-
-       ret = iotcon_state_set_list(room_state, "today_temp", temperature_list);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_state_set_list() Fail(%d)", ret);
-               iotcon_state_destroy(room_state);
-               iotcon_representation_destroy(room_repr);
-               return;
-       }
-
-       iotcon_list_destroy(temperature_list);
-
-       /* Set a room state into room Representation */
-       ret = iotcon_representation_set_state(room_repr, room_state);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_representation_set_state() Fail(%d)", ret);
-               iotcon_state_destroy(room_state);
-               iotcon_representation_destroy(room_repr);
-               return;
-       }
-
-       iotcon_state_destroy(room_state);
-
-       /* create a light Representation */
-       ret = iotcon_representation_create(&light_repr);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_representation_create() Fail(%d)", ret);
-               iotcon_representation_destroy(room_repr);
-               return;
-       }
-
-       ret = iotcon_representation_set_uri_path(light_repr, "/a/light");
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_representation_set_uri_path() Fail(%d)", ret);
-               iotcon_representation_destroy(light_repr);
-               iotcon_representation_destroy(room_repr);
-               return;
-       }
-
-       /* create a light state */
-       ret = iotcon_state_create(&light_state);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_state_create() Fail(%d)", ret);
-               iotcon_state_destroy(light_state);
-               iotcon_representation_destroy(light_repr);
-               iotcon_representation_destroy(room_repr);
-               return;
-       }
-
-       ret = iotcon_state_set_int(light_state, "brightness", 50);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_state_set_int() Fail(%d)", ret);
-               iotcon_state_destroy(light_state);
-               iotcon_representation_destroy(light_repr);
-               iotcon_representation_destroy(room_repr);
-               return;
-       }
-
-       /* Set a light state into light Representation */
-       ret = iotcon_representation_set_state(light_repr, light_state);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_representation_set_state() Fail(%d)", ret);
-               iotcon_state_destroy(light_state);
-               iotcon_representation_destroy(light_repr);
-               iotcon_representation_destroy(room_repr);
-               return;
-       }
-
-       iotcon_state_destroy(light_state);
-
-       ret = iotcon_representation_append_child(room_repr, light_repr);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_representation_append_child() Fail(%d)", ret);
-               iotcon_representation_destroy(light_repr);
-               iotcon_representation_destroy(room_repr);
-               return;
-       }
-
-       iotcon_representation_destroy(light_repr);
-
-       /* create a switch Representation */
-       ret = iotcon_representation_create(&switch_repr);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_representation_create() Fail(%d)", ret);
-               iotcon_representation_destroy(room_repr);
-               return;
-       }
-
-       ret = iotcon_representation_set_uri_path(switch_repr, "/a/switch");
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_representation_set_uri_path() Fail(%d)", ret);
-               iotcon_representation_destroy(switch_repr);
-               iotcon_representation_destroy(room_repr);
-               return;
-       }
-
-       /* create a switch state */
-       ret = iotcon_state_create(&switch_state);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_state_create() Fail(%d)", ret);
-               iotcon_representation_destroy(switch_repr);
-               iotcon_representation_destroy(room_repr);
-               return;
-       }
-
-       ret = iotcon_state_set_bool(switch_state, "switch", false);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_state_set_bool() Fail(%d)", ret);
-               iotcon_state_destroy(switch_state);
-               iotcon_representation_destroy(switch_repr);
-               iotcon_representation_destroy(room_repr);
-               return;
-       }
-
-       /* Set a light state into light Representation */
-       ret = iotcon_representation_set_state(switch_repr, switch_state);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_representation_set_state() Fail(%d)", ret);
-               iotcon_state_destroy(switch_state);
-               iotcon_representation_destroy(switch_repr);
-               iotcon_representation_destroy(room_repr);
-               return;
-       }
-
-       iotcon_state_destroy(switch_state);
-
-       ret = iotcon_representation_append_child(room_repr, switch_repr);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_representation_append_child() Fail(%d)", ret);
-               iotcon_representation_destroy(switch_repr);
-               iotcon_representation_destroy(room_repr);
-               return;
-       }
-
-       iotcon_representation_destroy(switch_repr);
-
-       ret = iotcon_request_get_query(request, &query);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_request_get_query() Fail(%d)", ret);
-               iotcon_representation_destroy(room_repr);
-               return;
-       }
-
-       if (query) {
-               ret = iotcon_query_lookup(query, "if", &query_str);
-               if (IOTCON_ERROR_NONE != ret) {
-                       ERR("iotcon_query_lookup() Fail(%d)", ret);
-                       iotcon_representation_destroy(room_repr);
-                       return;
-               }
-       }
-
-       if (query_str && (TEST_STR_EQUAL == strcmp("oic.if.b", query_str))) {
-               DBG("operation for BATCH interface");
-               iface = IOTCON_INTERFACE_BATCH;
-       } else {
-               DBG("operation for DEFAULT interface");
-               iface = IOTCON_INTERFACE_DEFAULT;
-       }
-
-       ret = _send_response(response, room_repr, iface);
-       if (0 != ret)
-               ERR("_send_response() Fail(%d)", ret);
-
-       iotcon_representation_destroy(room_repr);
-}
-
-static void _request_handler_put(iotcon_request_h request, iotcon_response_h response)
-{
-       int ret;
-       iotcon_representation_h resp_repr;
-
-       ret = iotcon_representation_create(&resp_repr);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_representation_create() Fail(%d)", ret);
-               return;
-       }
-
-       INFO("PUT request");
-
-       /* do PUT operation */
-
-       ret = _send_response(response, resp_repr, IOTCON_INTERFACE_DEFAULT);
-       if (0 != ret)
-               ERR("_send_response() Fail(%d)", ret);
-
-       iotcon_representation_destroy(resp_repr);
-}
-
-static void _request_handler_post(iotcon_response_h response)
-{
-       int ret;
-       iotcon_representation_h resp_repr;
-
-       ret = iotcon_representation_create(&resp_repr);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_representation_create() Fail(%d)", ret);
-               return;
-       }
-
-       INFO("POST request");
-
-       /* do POST operation */
-
-       ret = _send_response(response, resp_repr, IOTCON_INTERFACE_DEFAULT);
-       if (0 != ret)
-               ERR("_send_response() Fail(%d)", ret);
-
-       iotcon_representation_destroy(resp_repr);
-}
-
-static void _request_handler_delete(iotcon_response_h response)
-{
-       int ret;
-       iotcon_representation_h resp_repr;
-
-       ret = iotcon_representation_create(&resp_repr);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_representation_create() Fail(%d)", ret);
-               return;
-       }
-
-       INFO("DELETE request");
-
-       /* do DELETE operation */
-
-       ret = _send_response(response, resp_repr, IOTCON_INTERFACE_DEFAULT);
-       if (0 != ret)
-               ERR("_send_response() Fail(%d)", ret);
-
-       iotcon_representation_destroy(resp_repr);
-}
-
-static void _light_request_handler(iotcon_resource_h resource, iotcon_request_h request,
-               void *user_data)
-{
-       int ret;
-       int types;
-       iotcon_response_h response;
-
-       RET_IF(NULL == request);
-
-       ret = iotcon_request_get_types(request, &types);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_request_get_types() Fail(%d)", ret);
-               return;
-       }
-
-       ret = iotcon_response_create(request, &response);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_response_create() Fail");
-               return;
-       }
-
-       if (IOTCON_REQUEST_GET & types)
-               _light_request_handler_get(response);
-
-       else if (IOTCON_REQUEST_PUT & types)
-               _request_handler_put(request, response);
-
-       else if (IOTCON_REQUEST_POST & types)
-               _request_handler_post(response);
-
-       else if (IOTCON_REQUEST_DELETE & types)
-               _request_handler_delete(response);
-
-       iotcon_response_destroy(response);
-}
-
-static void _room_request_handler(iotcon_resource_h resource, iotcon_request_h request,
-               void *user_data)
-{
-       FN_CALL;
-       int ret, types;
-       iotcon_response_h response;
-
-       RET_IF(NULL == request);
-
-       ret = iotcon_request_get_types(request, &types);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_request_get_types() Fail(%d)", ret);
-               return;
-       }
-
-       ret = iotcon_response_create(request, &response);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_response_create() Fail(%d)", ret);
-               return;
-       }
-
-       if (IOTCON_REQUEST_GET & types)
-               _room_request_handler_get(request, response);
-
-       else if (IOTCON_REQUEST_PUT & types)
-               _request_handler_put(request, response);
-
-       else if (IOTCON_REQUEST_POST & types)
-               _request_handler_post(response);
-
-       else if (IOTCON_REQUEST_DELETE & types)
-               _request_handler_delete(response);
-
-       iotcon_response_destroy(response);
-}
-
-int main(int argc, char **argv)
-{
-       FN_CALL;
-       int ret;
-       GMainLoop *loop;
-       iotcon_resource_h room_handle, light_handle;
-       iotcon_resource_types_h room_rtypes, light_rtypes;
-
-       loop = g_main_loop_new(NULL, FALSE);
-
-       /* iotcon open */
-       ret = iotcon_open();
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_open() Fail(%d)", ret);
-               return -1;
-       }
-
-       /* register room resource */
-       ret = iotcon_resource_types_create(&room_rtypes);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_resource_types_create() Fail(%d)", ret);
-               iotcon_close();
-               return -1;
-       }
-       ret = iotcon_resource_types_add(room_rtypes, "core.room");
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_resource_types_add(%s) Fail(%d)", "core.room", ret);
-               iotcon_resource_types_destroy(room_rtypes);
-               iotcon_close();
-               return -1;
-       }
-
-       ret = iotcon_resource_create("/a/room", room_rtypes,
-                       (IOTCON_INTERFACE_DEFAULT | IOTCON_INTERFACE_BATCH),
-                       (IOTCON_DISCOVERABLE | IOTCON_OBSERVABLE), _room_request_handler,
-                       NULL, &room_handle);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_resource_create() Fail(%d)", ret);
-               iotcon_resource_types_destroy(room_rtypes);
-               iotcon_close();
-               return -1;
-       }
-       iotcon_resource_types_destroy(room_rtypes);
-
-       /* register light resource */
-       ret = iotcon_resource_types_create(&light_rtypes);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_resource_types_create() Fail(%d)", ret);
-               iotcon_resource_destroy(room_handle);
-               iotcon_close();
-               return -1;
-       }
-       ret = iotcon_resource_types_add(light_rtypes, "core.light");
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_resource_types_add(%s) Fail(%d)", "core.light", ret);
-               iotcon_resource_types_destroy(light_rtypes);
-               iotcon_resource_destroy(room_handle);
-               iotcon_close();
-               return -1;
-       }
-
-       ret = iotcon_resource_create("/a/light", light_rtypes,
-                       (IOTCON_INTERFACE_DEFAULT | IOTCON_INTERFACE_BATCH),
-                       (IOTCON_DISCOVERABLE | IOTCON_OBSERVABLE), _light_request_handler,
-                       NULL, &light_handle);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_resource_create() Fail");
-               iotcon_resource_types_destroy(light_rtypes);
-               iotcon_resource_destroy(room_handle);
-               iotcon_close();
-               return -1;
-       }
-       iotcon_resource_types_destroy(light_rtypes);
-
-       ret = iotcon_resource_bind_child_resource(room_handle, light_handle);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("iotcon_resource_bind_child_resource() Fail");
-               iotcon_resource_destroy(light_handle);
-               iotcon_resource_destroy(room_handle);
-               iotcon_close();
-               return -1;
-       }
-
-       g_main_loop_run(loop);
-       g_main_loop_unref(loop);
-
-       iotcon_resource_destroy(light_handle);
-       iotcon_resource_destroy(room_handle);
-
-       /* iotcon close */
-       iotcon_close();
-
-       return 0;
-}