modify "is_observable->properties", & remove tizen_info
[platform/core/iot/iotcon.git] / daemon / icd-ioty-ocprocess.c
index 4e21f5e..6cc3787 100644 (file)
@@ -14,8 +14,9 @@
  * limitations under the License.
  */
 
+#include <stdint.h>
 #include <stdlib.h>
-#include <unistd.h> /* for usleep() */
+#include <unistd.h>
 #include <glib.h>
 
 #include <ocstack.h>
@@ -43,21 +44,22 @@ struct icd_ioty_worker
 
 
 struct icd_req_context {
-       unsigned int signum;
+       int64_t signal_number;
        char *bus_name;
-       int types;
-       int observer_id;
-       int observe_action;
+       int request_type;
+       int observe_id;
+       int observe_type;
        OCRequestHandle request_h;
        OCResourceHandle resource_h;
        GVariant *payload;
        GVariantBuilder *options;
        GVariantBuilder *query;
+       OCDevAddr *dev_addr;
 };
 
 
 struct icd_find_context {
-       unsigned int signum;
+       int64_t signal_number;
        char *bus_name;
        int conn_type;
        GVariant **payload;
@@ -74,22 +76,15 @@ struct icd_crud_context {
 
 
 struct icd_info_context {
-       unsigned int signum;
+       int64_t signal_number;
        int info_type;
        char *bus_name;
        GVariant *payload;
 };
 
 
-struct icd_tizen_info_context {
-       OCRequestHandle request_h;
-       OCResourceHandle resource_h;
-       GDBusMethodInvocation *invocation;
-};
-
-
 struct icd_observe_context {
-       unsigned int signum;
+       int64_t signal_number;
        int res;
        int seqnum;
        char *bus_name;
@@ -99,11 +94,20 @@ struct icd_observe_context {
 
 
 struct icd_presence_context {
-       unsigned int signum;
-       char *bus_name;
+       OCDoHandle handle;
        int result;
        unsigned int nonce;
        OCDevAddr *dev_addr;
+       iotcon_presence_trigger_e trigger;
+       char *resource_type;
+};
+
+
+struct icd_encap_context {
+       int64_t signal_number;
+       OCRepPayload *oic_payload;
+       OCStackResult ret;
+       icd_encap_info_s *encap_info;
 };
 
 
@@ -170,19 +174,19 @@ static int _ocprocess_worker_start(_ocprocess_cb cb, void *ctx)
 }
 
 
-static int _ocprocess_response_signal(const char *dest, const char *signal,
-               unsigned int signum, GVariant *value)
+static int _ocprocess_response_signal(const char *dest, const char *signal_prefix,
+               int64_t signal_number, GVariant *value)
 {
        int ret;
-       char sig_name[IC_DBUS_SIGNAL_LENGTH] = {0};
+       char signal_name[IC_DBUS_SIGNAL_LENGTH] = {0};
 
-       ret = snprintf(sig_name, sizeof(sig_name), "%s_%u", signal, signum);
-       if (ret <= 0 || sizeof(sig_name) <= ret) {
+       ret = snprintf(signal_name, sizeof(signal_name), "%s_%llx", signal_prefix, signal_number);
+       if (ret <= 0 || sizeof(signal_name) <= ret) {
                ERR("snprintf() Fail(%d)", ret);
                return IOTCON_ERROR_IO_ERROR;
        }
 
-       ret = icd_dbus_emit_signal(dest, sig_name, value);
+       ret = icd_dbus_emit_signal(dest, signal_name, value);
        if (IOTCON_ERROR_NONE != ret) {
                ERR("icd_dbus_emit_signal() Fail(%d)", ret);
                return ret;
@@ -207,39 +211,82 @@ static inline GVariantBuilder* _ocprocess_parse_header_options(
        return options;
 }
 
+static int _ioty_oic_action_to_ioty_action(int oic_action)
+{
+       int action;
+
+       switch (oic_action) {
+       case OC_OBSERVE_REGISTER:
+               action = IOTCON_OBSERVE_REGISTER;
+               break;
+       case OC_OBSERVE_DEREGISTER:
+               action = IOTCON_OBSERVE_DEREGISTER;
+               break;
+       case OC_OBSERVE_NO_OPTION:
+       default:
+               ERR("Invalid action (%d)", oic_action);
+               action = IOTCON_OBSERVE_NO_TYPE;
+       }
+       return action;
+}
+
+
+static void _icd_req_context_free(struct icd_req_context *ctx)
+{
+       free(ctx->bus_name);
+       if (ctx->payload)
+               g_variant_unref(ctx->payload);
+       g_variant_builder_unref(ctx->options);
+       g_variant_builder_unref(ctx->query);
+       free(ctx->dev_addr);
+       free(ctx);
+}
+
 
 static int _worker_req_handler(void *context)
 {
-       int ret;
        GVariant *value;
-       struct icd_req_context *ctx = context;
+       char *host_address;
+       int ret, conn_type;
        GVariantBuilder payload_builder;
+       struct icd_req_context *ctx = context;
 
        RETV_IF(NULL == ctx, IOTCON_ERROR_INVALID_PARAMETER);
 
        g_variant_builder_init(&payload_builder, G_VARIANT_TYPE("av"));
-       if (ctx->payload)
+       if (ctx->payload) {
                g_variant_builder_add(&payload_builder, "v", ctx->payload);
+               ctx->payload = NULL;
+       }
+
+       ret = icd_ioty_get_host_address(ctx->dev_addr, &host_address, &conn_type);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("icd_ioty_get_host_address() Fail(%d)", ret);
+               g_variant_builder_clear(&payload_builder);
+               _icd_req_context_free(ctx);
+               return ret;
+       }
 
-       value = g_variant_new("(ia(qs)a(ss)iiavxx)",
-                       ctx->types,
+       value = g_variant_new("(siia(qs)a(ss)iiavxx)",
+                       host_address,
+                       conn_type,
+                       ctx->request_type,
                        ctx->options,
                        ctx->query,
-                       ctx->observe_action,
-                       ctx->observer_id,
+                       ctx->observe_type,
+                       ctx->observe_id,
                        &payload_builder,
                        ICD_POINTER_TO_INT64(ctx->request_h),
                        ICD_POINTER_TO_INT64(ctx->resource_h));
 
+       free(host_address);
+
        ret = _ocprocess_response_signal(ctx->bus_name, IC_DBUS_SIGNAL_REQUEST_HANDLER,
-                       ctx->signum, value);
+                       ctx->signal_number, value);
        if (IOTCON_ERROR_NONE != ret)
                ERR("_ocprocess_response_signal() Fail(%d)", ret);
 
-       free(ctx->bus_name);
-       g_variant_builder_unref(ctx->options);
-       g_variant_builder_unref(ctx->query);
-       free(ctx);
+       _icd_req_context_free(ctx);
 
        return ret;
 }
@@ -250,11 +297,12 @@ OCEntityHandlerResult icd_ioty_ocprocess_req_handler(OCEntityHandlerFlag flag,
 {
        FN_CALL;
        int ret;
-       unsigned int signal_number;
+       int64_t signal_number;
        char *query_str, *query_key, *query_value;
        char *token, *save_ptr1, *save_ptr2;
        char *bus_name = NULL;
        struct icd_req_context *req_ctx;
+       OCDevAddr *dev_addr;
 
        RETV_IF(NULL == request, OC_EH_ERROR);
 
@@ -276,42 +324,54 @@ OCEntityHandlerResult icd_ioty_ocprocess_req_handler(OCEntityHandlerFlag flag,
        }
 
        /* signal number & bus_name */
-       req_ctx->signum = signal_number;
+       req_ctx->signal_number = signal_number;
        req_ctx->bus_name = bus_name;
 
+       dev_addr = calloc(1, sizeof(OCDevAddr));
+       if (NULL == dev_addr) {
+               ERR("calloc() Fail(%d)", errno);
+               free(req_ctx->bus_name);
+               free(req_ctx);
+               return OC_EH_ERROR;
+       }
+       memcpy(dev_addr, &request->devAddr, sizeof(OCDevAddr));
+       req_ctx->dev_addr = dev_addr;
+
        /* request type */
        if (OC_REQUEST_FLAG & flag) {
                switch (request->method) {
                case OC_REST_GET:
-                       req_ctx->types = IOTCON_REQUEST_GET;
+                       req_ctx->request_type = IOTCON_REQUEST_GET;
                        req_ctx->payload = NULL;
-
-                       if (OC_OBSERVE_FLAG & flag) {
-                               req_ctx->types |= IOTCON_REQUEST_OBSERVE;
-                               /* observation info*/
-                               req_ctx->observer_id = request->obsInfo.obsId;
-                               req_ctx->observe_action = request->obsInfo.action;
-                       }
                        break;
                case OC_REST_PUT:
-                       req_ctx->types = IOTCON_REQUEST_PUT;
+                       req_ctx->request_type = IOTCON_REQUEST_PUT;
                        req_ctx->payload = icd_payload_to_gvariant(request->payload);
                        break;
                case OC_REST_POST:
-                       req_ctx->types = IOTCON_REQUEST_POST;
+                       req_ctx->request_type = IOTCON_REQUEST_POST;
                        req_ctx->payload = icd_payload_to_gvariant(request->payload);
                        break;
                case OC_REST_DELETE:
-                       req_ctx->types = IOTCON_REQUEST_DELETE;
+                       req_ctx->request_type = IOTCON_REQUEST_DELETE;
                        req_ctx->payload = NULL;
                        break;
                default:
+                       free(req_ctx->dev_addr);
                        free(req_ctx->bus_name);
                        free(req_ctx);
                        return OC_EH_ERROR;
                }
        }
 
+       if (OC_OBSERVE_FLAG & flag) {
+               /* observation info*/
+               req_ctx->observe_id = request->obsInfo.obsId;
+               req_ctx->observe_type = _ioty_oic_action_to_ioty_action(request->obsInfo.action);
+       } else {
+               req_ctx->observe_type = IOTCON_OBSERVE_NO_TYPE;
+       }
+
        /* header options */
        req_ctx->options = _ocprocess_parse_header_options(
                        request->rcvdVendorSpecificHeaderOptions,
@@ -335,12 +395,7 @@ OCEntityHandlerResult icd_ioty_ocprocess_req_handler(OCEntityHandlerFlag flag,
        ret = _ocprocess_worker_start(_worker_req_handler, req_ctx);
        if (IOTCON_ERROR_NONE != ret) {
                ERR("_ocprocess_worker_start() Fail(%d)", ret);
-               free(req_ctx->bus_name);
-               if (req_ctx->payload)
-                       g_variant_unref(req_ctx->payload);
-               g_variant_builder_unref(req_ctx->options);
-               g_variant_builder_unref(req_ctx->query);
-               free(req_ctx);
+               _icd_req_context_free(req_ctx);
                return OC_EH_ERROR;
        }
 
@@ -354,6 +409,7 @@ gpointer icd_ioty_ocprocess_thread(gpointer data)
 {
        FN_CALL;
        OCStackResult result;
+       const struct timespec delay = {0, 10 * 1000 * 1000}; /* 10 ms */
 
        icd_ioty_alive = 1;
        while (icd_ioty_alive) {
@@ -365,8 +421,9 @@ gpointer icd_ioty_ocprocess_thread(gpointer data)
                        break;
                }
 
-               /* TODO : SHOULD revise time or usleep */
-               usleep(10);
+               /* TODO : Current '10ms' is not proven sleep time. Revise the time after test.
+                * Or recommend changes to event driven architecture */
+               nanosleep(&delay, NULL);
        }
 
        return NULL;
@@ -387,7 +444,7 @@ static int _worker_find_cb(void *context)
                 * To reduce the number of emit_signal, let's send signal only one time for one device.
                 * for ex, client list. */
                ret = _ocprocess_response_signal(ctx->bus_name, IC_DBUS_SIGNAL_FOUND_RESOURCE,
-                               ctx->signum, value);
+                               ctx->signal_number, value);
                if (IOTCON_ERROR_NONE != ret) {
                        ERR("_ocprocess_response_signal() Fail(%d)", ret);
                        g_variant_unref(value);
@@ -424,7 +481,7 @@ OCStackApplicationResult icd_ioty_ocprocess_find_cb(void *ctx, OCDoHandle handle
                return OC_STACK_KEEP_TRANSACTION;
        }
 
-       find_ctx->signum = sig_context->signum;
+       find_ctx->signal_number = sig_context->signal_number;
        find_ctx->bus_name = ic_utils_strdup(sig_context->bus_name);
        find_ctx->payload = icd_payload_res_to_gvariant(resp->payload, &resp->devAddr);
        find_ctx->conn_type = icd_ioty_transport_flag_to_conn_type(resp->devAddr.adapter,
@@ -470,17 +527,18 @@ static int _worker_crud_cb(void *context)
 static int _worker_info_cb(void *context)
 {
        int ret;
-       const char *sig_name = NULL;
+       const char *signal_prefix = NULL;
        struct icd_info_context *ctx = context;
 
        RETV_IF(NULL == ctx, IOTCON_ERROR_INVALID_PARAMETER);
 
        if (ICD_DEVICE_INFO == ctx->info_type)
-               sig_name = IC_DBUS_SIGNAL_DEVICE;
+               signal_prefix = IC_DBUS_SIGNAL_DEVICE;
        else if (ICD_PLATFORM_INFO == ctx->info_type)
-               sig_name = IC_DBUS_SIGNAL_PLATFORM;
+               signal_prefix = IC_DBUS_SIGNAL_PLATFORM;
 
-       ret = _ocprocess_response_signal(ctx->bus_name, sig_name, ctx->signum, ctx->payload);
+       ret = _ocprocess_response_signal(ctx->bus_name, signal_prefix, ctx->signal_number,
+                       ctx->payload);
        if (IOTCON_ERROR_NONE != ret)
                ERR("_ocprocess_response_signal() Fail(%d)", ret);
 
@@ -524,13 +582,38 @@ static int _ocprocess_worker(_ocprocess_cb cb, int type, OCPayload *payload, int
        return ret;
 }
 
+static int _ocprocess_parse_oic_result(OCStackResult result)
+{
+       int res;
+
+       switch (result) {
+       case OC_STACK_OK:
+               res = IOTCON_RESPONSE_OK;
+               break;
+       case OC_STACK_RESOURCE_CREATED:
+               res = IOTCON_RESPONSE_RESOURCE_CREATED;
+               break;
+       case OC_STACK_RESOURCE_DELETED:
+               res = IOTCON_RESPONSE_RESULT_DELETED;
+               break;
+       case OC_STACK_UNAUTHORIZED_REQ:
+               res = IOTCON_RESPONSE_FORBIDDEN;
+               break;
+       default:
+               WARN("response error(%d)", result);
+               res = IOTCON_RESPONSE_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 +624,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 +646,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 +656,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 +678,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 +688,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 +710,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 +720,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) {
@@ -715,8 +747,8 @@ static int _worker_observe_cb(void *context)
        value = g_variant_new("(a(qs)vii)", ctx->options, ctx->payload, ctx->res,
                        ctx->seqnum);
 
-       ret = _ocprocess_response_signal(ctx->bus_name, IC_DBUS_SIGNAL_OBSERVE, ctx->signum,
-                       value);
+       ret = _ocprocess_response_signal(ctx->bus_name, IC_DBUS_SIGNAL_OBSERVE,
+                       ctx->signal_number, value);
        if (IOTCON_ERROR_NONE != ret)
                ERR("_ocprocess_response_signal() Fail(%d)", ret);
 
@@ -729,24 +761,29 @@ static int _worker_observe_cb(void *context)
 }
 
 
-static void _observe_cb_response_error(const char *dest, unsigned int signum, int ret_val)
+static void _observe_cb_response_error(const char *dest,
+               int64_t signal_number, int ret_val)
 {
        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();
 
-       ret = _ocprocess_response_signal(dest, IC_DBUS_SIGNAL_OBSERVE, signum, value);
+       value = g_variant_new("(a(qs)vii)", &options, payload, ret_val, 0);
+
+       ret = _ocprocess_response_signal(dest, IC_DBUS_SIGNAL_OBSERVE, signal_number, value);
        if (IOTCON_ERROR_NONE != ret)
                ERR("_ocprocess_response_signal() Fail(%d)", ret);
 }
 
 
-OCStackApplicationResult icd_ioty_ocprocess_observe_cb(void *ctx, OCDoHandle handle,
-               OCClientResponse *resp)
+OCStackApplicationResult icd_ioty_ocprocess_observe_cb(void *ctx,
+               OCDoHandle handle, OCClientResponse *resp)
 {
        int ret, res;
-       OCStackResult result;
        GVariantBuilder *options;
        struct icd_observe_context *observe_ctx;
        icd_sig_ctx_s *sig_context = ctx;
@@ -755,7 +792,7 @@ OCStackApplicationResult icd_ioty_ocprocess_observe_cb(void *ctx, OCDoHandle han
 
        if (NULL == resp->payload) {
                ERR("payload is empty");
-               _observe_cb_response_error(sig_context->bus_name, sig_context->signum,
+               _observe_cb_response_error(sig_context->bus_name, sig_context->signal_number,
                                IOTCON_ERROR_IOTIVITY);
                return OC_STACK_KEEP_TRANSACTION;
        }
@@ -763,24 +800,18 @@ OCStackApplicationResult icd_ioty_ocprocess_observe_cb(void *ctx, OCDoHandle han
        observe_ctx = calloc(1, sizeof(struct icd_observe_context));
        if (NULL == observe_ctx) {
                ERR("calloc() Fail(%d)", errno);
-               _observe_cb_response_error(sig_context->bus_name, sig_context->signum,
+               _observe_cb_response_error(sig_context->bus_name, sig_context->signal_number,
                                IOTCON_ERROR_OUT_OF_MEMORY);
                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;
+       observe_ctx->signal_number = sig_context->signal_number;
        observe_ctx->res = res;
        observe_ctx->bus_name = ic_utils_strdup(sig_context->bus_name);
        observe_ctx->options = options;
@@ -788,7 +819,7 @@ OCStackApplicationResult icd_ioty_ocprocess_observe_cb(void *ctx, OCDoHandle han
        ret = _ocprocess_worker_start(_worker_observe_cb, observe_ctx);
        if (IOTCON_ERROR_NONE != ret) {
                ERR("_ocprocess_worker_start() Fail(%d)", ret);
-               _observe_cb_response_error(sig_context->bus_name, sig_context->signum, ret);
+               _observe_cb_response_error(sig_context->bus_name, sig_context->signal_number, ret);
                free(observe_ctx->bus_name);
                if (observe_ctx->payload)
                        g_variant_unref(observe_ctx->payload);
@@ -806,25 +837,47 @@ OCStackApplicationResult icd_ioty_ocprocess_observe_cb(void *ctx, OCDoHandle han
 
 static int _worker_presence_cb(void *context)
 {
-       FN_CALL;
-       int ret;
-       GVariant *value;
-       char addr[PATH_MAX] = {0};
+       int conn_type;
+       OCDoHandle handle;
+       char *host_address;
+       GVariant *value, *value2;
+       int ret = IOTCON_ERROR_NONE;
        struct icd_presence_context *ctx = context;
 
        RETV_IF(NULL == ctx, IOTCON_ERROR_INVALID_PARAMETER);
 
-       snprintf(addr, sizeof(addr), "%s:%d", ctx->dev_addr->addr, ctx->dev_addr->port);
+       ret = icd_ioty_get_host_address(ctx->dev_addr, &host_address, &conn_type);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("icd_ioty_get_host_address() Fail(%d)", ret);
+               free(ctx->resource_type);
+               free(ctx->dev_addr);
+               free(ctx);
+               return ret;
+       }
+
+       value = g_variant_new("(iusiis)", ctx->result, ctx->nonce, host_address, conn_type,
+                       ctx->trigger, ic_utils_dbus_encode_str(ctx->resource_type));
+       value2 = g_variant_ref(value);
 
-       value = g_variant_new("(ius)", ctx->result, ctx->nonce, addr);
+       free(host_address);
 
-       ret = _ocprocess_response_signal(ctx->bus_name, IC_DBUS_SIGNAL_PRESENCE, ctx->signum,
-                       value);
+       ret = _ocprocess_response_signal(NULL, IC_DBUS_SIGNAL_PRESENCE,
+                       ICD_POINTER_TO_INT64(ctx->handle), value);
        if (IOTCON_ERROR_NONE != ret)
                ERR("_ocprocess_response_signal() Fail(%d)", ret);
 
+       handle = icd_ioty_presence_table_get_handle(ICD_MULTICAST_ADDRESS);
+       if (handle && (handle != ctx->handle)) {
+               ret = _ocprocess_response_signal(NULL, IC_DBUS_SIGNAL_PRESENCE,
+                               ICD_POINTER_TO_INT64(handle), value2);
+               if (IOTCON_ERROR_NONE != ret)
+                       ERR("_ocprocess_response_signal() Fail(%d)", ret);
+       } else {
+               g_variant_unref(value2);
+       }
+
        /* ctx was allocated from icd_ioty_ocprocess_presence_cb() */
-       free(ctx->bus_name);
+       free(ctx->resource_type);
        free(ctx->dev_addr);
        free(ctx);
 
@@ -832,18 +885,55 @@ static int _worker_presence_cb(void *context)
 }
 
 
-static void _presence_cb_response_error(const char *dest, unsigned int signum,
-               int ret_val)
+static void _presence_cb_response_error(OCDoHandle handle, int ret_val)
 {
        FN_CALL;
        int ret;
-       GVariant *value;
+       OCDoHandle handle2;
+       GVariant *value, *value2;
 
-       value = g_variant_new("(ius)", ret_val, 0, IC_STR_NULL);
+       value = g_variant_new("(iusiis)", ret_val, 0, IC_STR_NULL, IOTCON_CONNECTIVITY_ALL,
+                       IOTCON_PRESENCE_RESOURCE_CREATED, IC_STR_NULL);
+       value2 = g_variant_ref(value);
 
-       ret = _ocprocess_response_signal(dest, IC_DBUS_SIGNAL_PRESENCE, signum, value);
+       ret = _ocprocess_response_signal(NULL, IC_DBUS_SIGNAL_PRESENCE,
+                       ICD_POINTER_TO_INT64(handle), value);
        if (IOTCON_ERROR_NONE != ret)
                ERR("_ocprocess_response_signal() Fail(%d)", ret);
+
+       handle2 = icd_ioty_presence_table_get_handle(ICD_MULTICAST_ADDRESS);
+       if (handle2 && (handle2 != handle)) {
+               ret = _ocprocess_response_signal(NULL, IC_DBUS_SIGNAL_PRESENCE,
+                               ICD_POINTER_TO_INT64(handle), value2);
+               if (IOTCON_ERROR_NONE != ret)
+                       ERR("_ocprocess_response_signal() Fail(%d)", ret);
+       } else {
+               g_variant_unref(value2);
+       }
+}
+
+
+static int _presence_trigger_to_ioty_trigger(OCPresenceTrigger src,
+               iotcon_presence_trigger_e *dest)
+{
+       RETV_IF(NULL == dest, IOTCON_ERROR_INVALID_PARAMETER);
+
+       switch (src) {
+       case OC_PRESENCE_TRIGGER_CREATE:
+               *dest = IOTCON_PRESENCE_RESOURCE_CREATED;
+               break;
+       case OC_PRESENCE_TRIGGER_CHANGE:
+               *dest = IOTCON_PRESENCE_RESOURCE_UPDATED;
+               break;
+       case OC_PRESENCE_TRIGGER_DELETE:
+               *dest = IOTCON_PRESENCE_RESOURCE_DESTROYED;
+               break;
+       default:
+               ERR("Invalid trigger(%d)", src);
+               return IOTCON_ERROR_INVALID_PARAMETER;
+       }
+
+       return IOTCON_ERROR_NONE;
 }
 
 
@@ -853,33 +943,39 @@ OCStackApplicationResult icd_ioty_ocprocess_presence_cb(void *ctx, OCDoHandle ha
        FN_CALL;
        int ret;
        OCDevAddr *dev_addr;
-       icd_sig_ctx_s *sig_context = ctx;
+       OCPresencePayload *payload;
        struct icd_presence_context *presence_ctx;
 
-       RETV_IF(NULL == ctx, OC_STACK_KEEP_TRANSACTION);
        RETV_IF(NULL == resp, OC_STACK_KEEP_TRANSACTION);
 
        presence_ctx = calloc(1, sizeof(struct icd_presence_context));
        if (NULL == presence_ctx) {
                ERR("calloc() Fail(%d)", errno);
-               _presence_cb_response_error(sig_context->bus_name, sig_context->signum,
-                               IOTCON_ERROR_OUT_OF_MEMORY);
+               _presence_cb_response_error(handle, IOTCON_ERROR_OUT_OF_MEMORY);
                return OC_STACK_KEEP_TRANSACTION;
        }
 
        dev_addr = calloc(1, sizeof(OCDevAddr));
        if (NULL == dev_addr) {
                ERR("calloc() Fail(%d)", errno);
-               _presence_cb_response_error(sig_context->bus_name, sig_context->signum,
-                               IOTCON_ERROR_OUT_OF_MEMORY);
+               _presence_cb_response_error(handle, IOTCON_ERROR_OUT_OF_MEMORY);
                free(presence_ctx);
                return OC_STACK_KEEP_TRANSACTION;
        }
        memcpy(dev_addr, &resp->devAddr, sizeof(OCDevAddr));
 
+       payload = (OCPresencePayload*)resp->payload;
+
        switch (resp->result) {
        case OC_STACK_OK:
                presence_ctx->result = IOTCON_PRESENCE_OK;
+               ret = _presence_trigger_to_ioty_trigger(payload->trigger, &presence_ctx->trigger);
+               if (IOTCON_ERROR_NONE != ret) {
+                       ERR("_presence_trigger_to_ioty_trigger() Fail(%d)", ret);
+                       _presence_cb_response_error(handle, ret);
+                       free(presence_ctx);
+                       return OC_STACK_KEEP_TRANSACTION;
+               }
                break;
        case OC_STACK_PRESENCE_STOPPED:
                presence_ctx->result = IOTCON_PRESENCE_STOPPED;
@@ -893,22 +989,23 @@ OCStackApplicationResult icd_ioty_ocprocess_presence_cb(void *ctx, OCDoHandle ha
                presence_ctx->result = IOTCON_ERROR_IOTIVITY;
        }
 
-       presence_ctx->signum = sig_context->signum;
-       presence_ctx->bus_name = ic_utils_strdup(sig_context->bus_name);
+       presence_ctx->handle = handle;
        presence_ctx->nonce = resp->sequenceNumber;
        presence_ctx->dev_addr = dev_addr;
 
+       if (payload->resourceType)
+               presence_ctx->resource_type = strdup(payload->resourceType);
+
        ret = _ocprocess_worker_start(_worker_presence_cb, presence_ctx);
        if (IOTCON_ERROR_NONE != ret) {
                ERR("_ocprocess_worker_start() Fail(%d)", ret);
-               _presence_cb_response_error(sig_context->bus_name, sig_context->signum, ret);
-               free(presence_ctx->bus_name);
+               _presence_cb_response_error(handle, ret);
+               free(presence_ctx->resource_type);
                free(presence_ctx->dev_addr);
                free(presence_ctx);
                return OC_STACK_KEEP_TRANSACTION;
        }
 
-       /* DO NOT FREE sig_context. It MUST be freed in the ocstack */
        /* DO NOT FREE presence_ctx. It MUST be freed in the _worker_presence_cb func */
 
        return OC_STACK_KEEP_TRANSACTION;
@@ -942,7 +1039,7 @@ OCStackApplicationResult icd_ioty_ocprocess_info_cb(void *ctx, OCDoHandle handle
 
        info_ctx->info_type = info_type;
        info_ctx->payload = icd_payload_to_gvariant(resp->payload);
-       info_ctx->signum = sig_context->signum;
+       info_ctx->signal_number = sig_context->signal_number;
        info_ctx->bus_name = ic_utils_strdup(sig_context->bus_name);
 
        ret = _ocprocess_worker_start(_worker_info_cb, info_ctx);
@@ -962,129 +1059,155 @@ OCStackApplicationResult icd_ioty_ocprocess_info_cb(void *ctx, OCDoHandle handle
 }
 
 
-static int _worker_tizen_info_handler(void *context)
+static int _worker_encap_get_cb(void *context)
 {
        int ret;
-       char *device_name;
-       char *tizen_device_id;
-       OCStackResult result;
-       OCRepPayload *payload;
-       OCEntityHandlerResponse response = {0};
-       struct icd_tizen_info_context *ctx = context;
+       struct icd_encap_context *ctx = context;
+       iotcon_remote_resource_state_e resource_state;
+       GVariant *monitoring_value, *caching_value;
 
-       response.requestHandle = ctx->request_h;
-       response.resourceHandle = ctx->resource_h;
-       response.ehResult = OC_EH_OK;
+       RETV_IF(NULL == ctx, IOTCON_ERROR_INVALID_PARAMETER);
 
-       /* Get Tizen Info */
-       ret = icd_ioty_tizen_info_get_property(&device_name, &tizen_device_id);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("icd_ioty_tizen_info_get_property() Fail(%d)", ret);
-               response.ehResult = OC_EH_ERROR;
+       /* MONITORING */
+       if (0 < ctx->encap_info->monitoring_count) {
+               switch (ctx->ret) {
+               case OC_STACK_OK:
+                       resource_state = IOTCON_REMOTE_RESOURCE_ALIVE;
+                       break;
+               case OC_STACK_ERROR:
+               default:
+                       resource_state = IOTCON_REMOTE_RESOURCE_LOST_SIGNAL;
+               }
+               if (resource_state != ctx->encap_info->resource_state) {
+                       ctx->encap_info->resource_state = resource_state;
+                       monitoring_value = g_variant_new("(i)", resource_state);
+                       ret = _ocprocess_response_signal(NULL, IC_DBUS_SIGNAL_MONITORING,
+                                       ctx->encap_info->signal_number, monitoring_value);
+                       if (IOTCON_ERROR_NONE != ret) {
+                               ERR("_ocprocess_response_signal() Fail(%d)", ret);
+                               OCRepPayloadDestroy(ctx->oic_payload);
+                               free(ctx);
+                               return ret;
+                       }
+               }
        }
 
-       /* payload */
-       payload = OCRepPayloadCreate();
-       OCRepPayloadSetUri(payload, ICD_IOTY_TIZEN_INFO_URI);
-       OCRepPayloadAddResourceType(payload, ICD_IOTY_TIZEN_INFO_TYPE);
-       OCRepPayloadAddInterface(payload, IC_INTERFACE_DEFAULT);
+       /* CACHING */
+       if (0 < ctx->encap_info->caching_count) {
+               if (OC_STACK_OK != ctx->ret) {
+                       OCRepPayloadDestroy(ctx->oic_payload);
+                       free(ctx);
+                       return IOTCON_ERROR_NONE;
+               }
 
-       OCRepPayloadSetPropString(payload, ICD_IOTY_TIZEN_INFO_DEVICE_NAME,
-                       ic_utils_dbus_encode_str(device_name));
-       OCRepPayloadSetPropString(payload, ICD_IOTY_TIZEN_INFO_TIZEN_DEVICE_ID,
-                       ic_utils_dbus_encode_str(tizen_device_id));
-       response.payload = (OCPayload*)payload;
+               ret = icd_payload_representation_compare(ctx->encap_info->oic_payload,
+                               ctx->oic_payload);
+               if (IC_EQUAL == ret) {
+                       OCRepPayloadDestroy(ctx->oic_payload);
+                       free(ctx);
+                       return IOTCON_ERROR_NONE;
+               }
 
-       icd_ioty_csdk_lock();
-       result = OCDoResponse(&response);
-       icd_ioty_csdk_unlock();
+               ctx->encap_info->oic_payload = ctx->oic_payload;
+               caching_value = icd_payload_to_gvariant((OCPayload*)ctx->oic_payload);
 
-       if (OC_STACK_OK != result) {
-               ERR("OCDoResponse() Fail(%d)", result);
-               free(ctx);
-               return IOTCON_ERROR_IOTIVITY;
+               ret = _ocprocess_response_signal(NULL, IC_DBUS_SIGNAL_CACHING,
+                               ctx->encap_info->signal_number, caching_value);
+               if (IOTCON_ERROR_NONE != ret) {
+                       ERR("_ocprocess_response_signal() Fail(%d)", ret);
+                       OCRepPayloadDestroy(ctx->oic_payload);
+                       free(ctx);
+                       return ret;
+               }
        }
 
+       OCRepPayloadDestroy(ctx->oic_payload);
        free(ctx);
+
        return IOTCON_ERROR_NONE;
 }
 
 
-OCEntityHandlerResult icd_ioty_ocprocess_tizen_info_handler(OCEntityHandlerFlag flag,
-               OCEntityHandlerRequest *request, void *user_data)
+OCStackApplicationResult icd_ioty_ocprocess_encap_get_cb(void *ctx, OCDoHandle handle,
+               OCClientResponse *resp)
 {
+       FN_CALL;
        int ret;
-       struct icd_tizen_info_context *tizen_info_ctx;
+       icd_encap_info_s *encap_info = ctx;
+       struct icd_encap_context *encap_ctx;
 
-       if ((0 == (OC_REQUEST_FLAG & flag)) || (OC_REST_GET != request->method)) {
-               ERR("Prohibited Action");
-               return OC_EH_FORBIDDEN;
-       }
+       RETV_IF(NULL == ctx, OC_STACK_DELETE_TRANSACTION);
 
-       tizen_info_ctx = calloc(1, sizeof(struct icd_tizen_info_context));
-       if (NULL == tizen_info_ctx) {
+       encap_ctx = calloc(1, sizeof(struct icd_encap_context));
+       if (NULL == encap_ctx) {
                ERR("calloc() Fail(%d)", errno);
-               return OC_EH_ERROR;
+               return OC_STACK_DELETE_TRANSACTION;
        }
 
-       tizen_info_ctx->request_h = request->requestHandle;
-       tizen_info_ctx->resource_h = request->resource;
+       encap_ctx->ret = resp->result;
+       encap_ctx->oic_payload = OCRepPayloadClone((OCRepPayload*)resp->payload);
+       encap_ctx->encap_info = encap_info;
 
-       ret = _ocprocess_worker_start(_worker_tizen_info_handler, tizen_info_ctx);
+       ret = _ocprocess_worker_start(_worker_encap_get_cb, encap_ctx);
        if (IOTCON_ERROR_NONE != ret) {
                ERR("_ocprocess_worker_start() Fail(%d)", ret);
-               free(tizen_info_ctx);
-               return OC_EH_ERROR;
+               OCRepPayloadDestroy((OCRepPayload*)encap_ctx->oic_payload);
+               free(encap_ctx);
+               return OC_STACK_DELETE_TRANSACTION;
        }
 
-       return OC_EH_OK;
+       return OC_STACK_DELETE_TRANSACTION;
 }
 
 
-OCStackApplicationResult icd_ioty_ocprocess_get_tizen_info_cb(void *ctx,
-               OCDoHandle handle, OCClientResponse *resp)
+static int _worker_encap_get(void *context)
 {
-       int res;
-       char *device_name;
-       char *tizen_device_id;
-       GVariant *tizen_info;
-       OCRepPayload *payload;
-       OCRepPayloadValue *val;
+       icd_encap_info_s *encap_info = context;
 
-       RETV_IF(NULL == ctx, OC_STACK_DELETE_TRANSACTION);
+       g_source_remove(encap_info->get_timer_id);
+       icd_ioty_encap_get(encap_info);
+       encap_info->get_timer_id = g_timeout_add_seconds(icd_ioty_encap_get_time_interval(),
+                       icd_ioty_encap_get, encap_info);
 
-       if (NULL == resp->payload) {
-               ERR("payload is empty");
-               icd_ioty_complete_error(ICD_TIZEN_INFO, ctx, IOTCON_ERROR_IOTIVITY);
-               return OC_STACK_DELETE_TRANSACTION;
-       }
+       return IOTCON_ERROR_NONE;
+}
 
-       payload = (OCRepPayload*)resp->payload;
-       val = payload->values;
-       if (NULL == val) {
-               ERR("Invalid payload");
-               icd_ioty_complete_error(ICD_TIZEN_INFO, ctx, IOTCON_ERROR_IOTIVITY);
-               return OC_STACK_DELETE_TRANSACTION;
-       }
-       device_name = val->str;
 
-       val = val->next;
-       if (NULL == val) {
-               ERR("Invalid Payload");
-               icd_ioty_complete_error(ICD_TIZEN_INFO, ctx, IOTCON_ERROR_IOTIVITY);
-               return OC_STACK_DELETE_TRANSACTION;
+OCStackApplicationResult icd_ioty_ocprocess_encap_observe_cb(void *ctx, OCDoHandle handle,
+               OCClientResponse *resp)
+{
+       FN_CALL;
+       int ret;
+
+       RETV_IF(NULL == resp, OC_STACK_KEEP_TRANSACTION);
+
+       ret = _ocprocess_worker_start(_worker_encap_get, ctx);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("_ocprocess_worker_start() Fail(%d)", ret);
+               return OC_STACK_KEEP_TRANSACTION;
        }
 
-       tizen_device_id = val->str;
+       return OC_STACK_KEEP_TRANSACTION;
+}
 
-       if (OC_STACK_OK == resp->result)
-               res = IOTCON_RESPONSE_RESULT_OK;
-       else
-               res = IOTCON_RESPONSE_RESULT_ERROR;
 
-       tizen_info = g_variant_new("(ssi)", device_name, tizen_device_id, res);
+OCStackApplicationResult icd_ioty_ocprocess_encap_presence_cb(void *ctx,
+               OCDoHandle handle, OCClientResponse *resp)
+{
+       FN_CALL;
+       int ret;
+       OCPresencePayload *payload = (OCPresencePayload*)resp->payload;
 
-       icd_ioty_complete(ICD_TIZEN_INFO, ctx, tizen_info);
+       RETV_IF(NULL == resp, OC_STACK_KEEP_TRANSACTION);
 
-       return OC_STACK_DELETE_TRANSACTION;
+       if ((OC_STACK_OK == resp->result) && (OC_PRESENCE_TRIGGER_DELETE != payload->trigger))
+               return OC_STACK_KEEP_TRANSACTION;
+
+       ret = _ocprocess_worker_start(_worker_encap_get, ctx);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("_ocprocess_worker_start() Fail(%d)", ret);
+               return OC_STACK_KEEP_TRANSACTION;
+       }
+
+       return OC_STACK_KEEP_TRANSACTION;
 }