modify "is_observable->properties", & remove tizen_info
[platform/core/iot/iotcon.git] / daemon / icd-ioty-ocprocess.c
index 85254e7..6cc3787 100644 (file)
@@ -16,7 +16,7 @@
 
 #include <stdint.h>
 #include <stdlib.h>
-#include <unistd.h> /* for usleep() */
+#include <unistd.h>
 #include <glib.h>
 
 #include <ocstack.h>
@@ -83,13 +83,6 @@ struct icd_info_context {
 };
 
 
-struct icd_tizen_info_context {
-       OCRequestHandle request_h;
-       OCResourceHandle resource_h;
-       GDBusMethodInvocation *invocation;
-};
-
-
 struct icd_observe_context {
        int64_t signal_number;
        int res;
@@ -110,6 +103,14 @@ struct icd_presence_context {
 };
 
 
+struct icd_encap_context {
+       int64_t signal_number;
+       OCRepPayload *oic_payload;
+       OCStackResult ret;
+       icd_encap_info_s *encap_info;
+};
+
+
 void icd_ioty_ocprocess_stop()
 {
        icd_ioty_alive = 0;
@@ -224,7 +225,7 @@ static int _ioty_oic_action_to_ioty_action(int oic_action)
        case OC_OBSERVE_NO_OPTION:
        default:
                ERR("Invalid action (%d)", oic_action);
-               action = IOTCON_OBSERVE_TYPE_NONE;
+               action = IOTCON_OBSERVE_NO_TYPE;
        }
        return action;
 }
@@ -329,6 +330,7 @@ OCEntityHandlerResult icd_ioty_ocprocess_req_handler(OCEntityHandlerFlag flag,
        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;
        }
@@ -355,6 +357,7 @@ OCEntityHandlerResult icd_ioty_ocprocess_req_handler(OCEntityHandlerFlag flag,
                        req_ctx->payload = NULL;
                        break;
                default:
+                       free(req_ctx->dev_addr);
                        free(req_ctx->bus_name);
                        free(req_ctx);
                        return OC_EH_ERROR;
@@ -366,7 +369,7 @@ OCEntityHandlerResult icd_ioty_ocprocess_req_handler(OCEntityHandlerFlag flag,
                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_TYPE_NONE;
+               req_ctx->observe_type = IOTCON_OBSERVE_NO_TYPE;
        }
 
        /* header options */
@@ -406,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) {
@@ -417,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;
@@ -583,20 +588,20 @@ static int _ocprocess_parse_oic_result(OCStackResult result)
 
        switch (result) {
        case OC_STACK_OK:
-               res = IOTCON_RESPONSE_RESULT_OK;
+               res = IOTCON_RESPONSE_OK;
                break;
        case OC_STACK_RESOURCE_CREATED:
-               res = IOTCON_RESPONSE_RESULT_RESOURCE_CREATED;
+               res = IOTCON_RESPONSE_RESOURCE_CREATED;
                break;
        case OC_STACK_RESOURCE_DELETED:
-               res = IOTCON_RESPONSE_RESULT_RESOURCE_DELETED;
+               res = IOTCON_RESPONSE_RESULT_DELETED;
                break;
        case OC_STACK_UNAUTHORIZED_REQ:
-               res = IOTCON_RESPONSE_RESULT_FORBIDDEN;
+               res = IOTCON_RESPONSE_FORBIDDEN;
                break;
        default:
                WARN("response error(%d)", result);
-               res = IOTCON_RESPONSE_RESULT_ERROR;
+               res = IOTCON_RESPONSE_ERROR;
                break;
        }
 
@@ -888,7 +893,7 @@ static void _presence_cb_response_error(OCDoHandle handle, int ret_val)
        GVariant *value, *value2;
 
        value = g_variant_new("(iusiis)", ret_val, 0, IC_STR_NULL, IOTCON_CONNECTIVITY_ALL,
-                       IOTCON_PRESENCE_TRIGGER_RESOURCE_CREATED, IC_STR_NULL);
+                       IOTCON_PRESENCE_RESOURCE_CREATED, IC_STR_NULL);
        value2 = g_variant_ref(value);
 
        ret = _ocprocess_response_signal(NULL, IC_DBUS_SIGNAL_PRESENCE,
@@ -915,13 +920,13 @@ static int _presence_trigger_to_ioty_trigger(OCPresenceTrigger src,
 
        switch (src) {
        case OC_PRESENCE_TRIGGER_CREATE:
-               *dest = IOTCON_PRESENCE_TRIGGER_RESOURCE_CREATED;
+               *dest = IOTCON_PRESENCE_RESOURCE_CREATED;
                break;
        case OC_PRESENCE_TRIGGER_CHANGE:
-               *dest = IOTCON_PRESENCE_TRIGGER_RESOURCE_UPDATED;
+               *dest = IOTCON_PRESENCE_RESOURCE_UPDATED;
                break;
        case OC_PRESENCE_TRIGGER_DELETE:
-               *dest = IOTCON_PRESENCE_TRIGGER_RESOURCE_DESTROYED;
+               *dest = IOTCON_PRESENCE_RESOURCE_DESTROYED;
                break;
        default:
                ERR("Invalid trigger(%d)", src);
@@ -1054,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;
+
+       RETV_IF(NULL == resp, OC_STACK_KEEP_TRANSACTION);
 
-       icd_ioty_complete(ICD_TIZEN_INFO, ctx, tizen_info);
+       if ((OC_STACK_OK == resp->result) && (OC_PRESENCE_TRIGGER_DELETE != payload->trigger))
+               return OC_STACK_KEEP_TRANSACTION;
 
-       return OC_STACK_DELETE_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;
 }