apply Serverside functions related to resource
authoryoungman <yman.jung@samsung.com>
Mon, 20 Jul 2015 08:34:45 +0000 (17:34 +0900)
committeryoungman <yman.jung@samsung.com>
Tue, 3 Nov 2015 11:08:19 +0000 (20:08 +0900)
Change-Id: I73a04df860ae12edd549c18b19b5d0fa3f22f303
Signed-off-by: youngman <yman.jung@samsung.com>
13 files changed:
common/ic-dbus.xml
daemon/icd-dbus.c
daemon/icd-dbus.h
daemon/icd-ioty-ocprocess.c
daemon/icd-ioty-ocprocess.h
daemon/icd-ioty.c
daemon/icd-ioty.h
lib/icl-dbus-type.c
lib/icl-dbus.c
lib/icl-request.c
lib/icl.c
lib/include/iotcon-struct.h
lib/include/iotcon.h

index 7c7ca3c..b9a1ee0 100644 (file)
@@ -34,7 +34,7 @@
                </method>
                <method name="notifyListOfObservers">
                        <arg type="i" name="resource" direction="in"/>
-                       <arg type="a(iis)" name="notify_msg" direction="in"/>
+                       <arg type="a(is)" name="notify_msg" direction="in"/>
                        <arg type="ai" name="observers" direction="in"/>
                        <arg type="i" name="ret" direction="out"/>
                </method>
@@ -43,7 +43,7 @@
                        <arg type="i" name="ret" direction="out"/>
                </method>
                <method name="sendResponse">
-                       <arg type="(sia(qs)iisii)" name="response" direction="in"/>
+                       <arg type="(sia(qs)isii)" name="response" direction="in"/>
                        <arg type="i" name="ret" direction="out"/>
                </method>
                <method name="findResource">
index d7e22d2..a57cf61 100644 (file)
 #include "icd-dbus.h"
 
 static GDBusConnection *icd_dbus_conn;
-static GList *icd_bus_list; /* global list to care resource handle for each sender bus */
 
-typedef struct _icd_bus_s {
+static GList *icd_dbus_client_list; /* global list to care resource handle for each sender bus */
+static GMutex icd_dbus_client_list_mutex;
+
+typedef struct _icd_dbus_client_s {
        gchar *sender;
        GList *hdlist;
-} icd_bus_s;
+} icd_dbus_client_s;
 
-typedef struct _ic_resource_handle {
+typedef struct _icd_resource_handle {
        void *handle;
        unsigned int number;
 } icd_resource_handle_s;
 
-static void _icd_dbus_resource_handle_free(int handle)
+
+static void _icd_dbus_client_list_lock()
+{
+       g_mutex_lock(&icd_dbus_client_list_mutex);
+}
+
+
+static void _icd_dbus_client_list_unlock()
 {
-       icd_bus_s *bus;
+       g_mutex_unlock(&icd_dbus_client_list_mutex);
+}
+
+
+static void _icd_dbus_resource_handle_free(void *handle)
+{
+       icd_dbus_client_s *bus;
        GList *cur_bus, *cur_hd;
        icd_resource_handle_s *rsrc_handle;
 
-       cur_bus = icd_bus_list;
+       _icd_dbus_client_list_lock();
+       cur_bus = icd_dbus_client_list;
        while (cur_bus) {
                bus = cur_bus->data;
                if (NULL == bus) {
                        ERR("bus is NULL");
+                       _icd_dbus_client_list_unlock();
                        return;
                }
 
@@ -55,10 +72,12 @@ static void _icd_dbus_resource_handle_free(int handle)
                while (cur_hd) {
                        rsrc_handle = cur_hd->data;
 
-                       if (rsrc_handle->handle == GINT_TO_POINTER(handle)) {
-                               DBG("resource handle(%u, %u) removed from handle list", handle, rsrc_handle->number);
+                       if (rsrc_handle->handle == handle) {
+                               DBG("resource handle(%u, %u) removed from handle list", handle,
+                                               rsrc_handle->number);
                                bus->hdlist = g_list_delete_link(bus->hdlist, cur_hd);
                                free(rsrc_handle);
+                               _icd_dbus_client_list_unlock();
                                return;
                        }
                        cur_hd = cur_hd->next;
@@ -67,24 +86,27 @@ static void _icd_dbus_resource_handle_free(int handle)
                cur_bus = cur_bus->next;
        }
 
+       _icd_dbus_client_list_unlock();
        return;
 }
 
-int icd_dbus_bus_list_get_info(int handle, unsigned int *sig_num, const gchar **sender)
+int icd_dbus_bus_list_get_info(void *handle, unsigned int *sig_num, gchar **sender)
 {
        FN_CALL;
-       icd_bus_s *bus;
+       icd_dbus_client_s *bus;
        GList *cur_bus, *cur_hd;
        icd_resource_handle_s *rsrc_handle;
 
        RETV_IF(NULL == sig_num, IOTCON_ERROR_INVALID_PARAMETER);
        RETV_IF(NULL == sender, IOTCON_ERROR_INVALID_PARAMETER);
 
-       cur_bus = icd_bus_list;
+       _icd_dbus_client_list_lock();
+       cur_bus = icd_dbus_client_list;
        while (cur_bus) {
                bus = cur_bus->data;
                if (NULL == bus) {
                        ERR("bus is NULL");
+                       _icd_dbus_client_list_unlock();
                        return IOTCON_ERROR_NO_DATA;
                }
 
@@ -92,10 +114,12 @@ int icd_dbus_bus_list_get_info(int handle, unsigned int *sig_num, const gchar **
                while (cur_hd) {
                        rsrc_handle = cur_hd->data;
 
-                       if (rsrc_handle->handle == GINT_TO_POINTER(handle)) {
-                               DBG("signal_number(%u) for resource handle(%u) found", rsrc_handle->number, handle);
+                       if (rsrc_handle->handle == handle) {
+                               DBG("signal_number(%u) for resource handle(%u) found",
+                                               rsrc_handle->number, handle);
                                *sig_num = rsrc_handle->number;
-                               *sender = bus->sender;
+                               *sender = ic_utils_strdup(bus->sender);
+                               _icd_dbus_client_list_unlock();
                                return IOTCON_ERROR_NONE;
                        }
                        cur_hd = cur_hd->next;
@@ -104,6 +128,7 @@ int icd_dbus_bus_list_get_info(int handle, unsigned int *sig_num, const gchar **
                cur_bus = cur_bus->next;
        }
 
+       _icd_dbus_client_list_unlock();
        return IOTCON_ERROR_NO_DATA;
 }
 
@@ -136,9 +161,10 @@ int icd_dbus_emit_signal(const char *dest, const char *signal_name, GVariant *va
        return IOTCON_ERROR_NONE;
 }
 
-void _ic_dbus_cleanup_handle(icd_resource_handle_s *rsrc_handle)
+static void _icd_dbus_cleanup_handle(void *data)
 {
        int ret;
+       icd_resource_handle_s *rsrc_handle = data;
 
        RET_IF(NULL == rsrc_handle);
        RET_IF(NULL == rsrc_handle->handle);
@@ -154,12 +180,12 @@ void _ic_dbus_cleanup_handle(icd_resource_handle_s *rsrc_handle)
 
 static int _icd_dbus_bus_list_cleanup_handle_list(GList *list)
 {
-       icd_bus_s *bus;
+       icd_dbus_client_s *bus;
 
        RETV_IF(NULL == list, IOTCON_ERROR_INVALID_PARAMETER);
 
        bus = list->data;
-       g_list_free_full(bus->hdlist, (GDestroyNotify)_ic_dbus_cleanup_handle);
+       g_list_free_full(bus->hdlist, _icd_dbus_cleanup_handle);
        free(bus->sender);
        bus->sender = NULL;
        free(bus);
@@ -170,12 +196,12 @@ static int _icd_dbus_bus_list_cleanup_handle_list(GList *list)
 static int _icd_dbus_bus_list_find_sender(const gchar *owner, GList **ret_list)
 {
        GList *cur;
-       icd_bus_s *bus;
+       icd_dbus_client_s *bus;
 
        RETV_IF(NULL == owner, IOTCON_ERROR_INVALID_PARAMETER);
        RETV_IF(NULL == ret_list, IOTCON_ERROR_INVALID_PARAMETER);
 
-       cur = icd_bus_list;
+       cur = icd_dbus_client_list;
        while (cur) {
                bus = cur->data;
                if (NULL == bus) {
@@ -209,9 +235,11 @@ static void _icd_dbus_name_owner_changed_cb(GDBusConnection *connection,
        g_variant_get(parameters, "(&s&s&s)", &name, &old_owner, &new_owner);
 
        if (0 == strlen(new_owner)) {
+               _icd_dbus_client_list_lock();
                ret = _icd_dbus_bus_list_find_sender(old_owner, &sender);
                if (IOTCON_ERROR_NONE != ret) {
                        ERR("_icd_dbus_bus_list_find_sender() Fail(%d)", ret);
+                       _icd_dbus_client_list_unlock();
                        return;
                }
 
@@ -221,10 +249,12 @@ static void _icd_dbus_name_owner_changed_cb(GDBusConnection *connection,
                        ret = _icd_dbus_bus_list_cleanup_handle_list(sender);
                        if (IOTCON_ERROR_NONE != ret) {
                                ERR("_icd_dbus_bus_list_cleanup_handle_list() Fail(%d)", ret);
+                               _icd_dbus_client_list_unlock();
                                return;
                        }
-                       icd_bus_list = g_list_delete_link(icd_bus_list, sender);
+                       icd_dbus_client_list = g_list_delete_link(icd_dbus_client_list, sender);
                }
+               _icd_dbus_client_list_unlock();
        }
 }
 
@@ -259,18 +289,20 @@ static int _icd_dbus_resource_list_append_handle(const gchar *sender, void *hand
        GList *cur_bus, *cur_hd;
        bool sender_exist = false;
        icd_resource_handle_s *rsrc_handle;
-       icd_bus_s *new_bus = NULL;
-       icd_bus_s *bus;
+       icd_dbus_client_s *new_bus = NULL;
+       icd_dbus_client_s *bus;
 
        RETV_IF(NULL == sender, IOTCON_ERROR_INVALID_PARAMETER);
        RETV_IF(NULL == handle, IOTCON_ERROR_INVALID_PARAMETER);
 
-       cur_bus = icd_bus_list;
+       _icd_dbus_client_list_lock();
+       cur_bus = icd_dbus_client_list;
 
        while (cur_bus) {
                bus = cur_bus->data;
                if (NULL == bus) {
                        ERR("bus is NULL");
+                       _icd_dbus_client_list_unlock();
                        return IOTCON_ERROR_NO_DATA;
                }
 
@@ -286,9 +318,10 @@ static int _icd_dbus_resource_list_append_handle(const gchar *sender, void *hand
        if (false == sender_exist) {
                DBG("sender(%s) not exist. make new one.", sender);
 
-               new_bus = calloc(1, sizeof(icd_bus_s));
+               new_bus = calloc(1, sizeof(icd_dbus_client_s));
                if (NULL == new_bus) {
                        ERR("calloc(bus) Fail(%d)", errno);
+                       _icd_dbus_client_list_unlock();
                        return IOTCON_ERROR_OUT_OF_MEMORY;
                }
 
@@ -296,6 +329,7 @@ static int _icd_dbus_resource_list_append_handle(const gchar *sender, void *hand
                if (NULL == sender_dup) {
                        ERR("ic_utils_strdup() Fail");
                        free(new_bus);
+                       _icd_dbus_client_list_unlock();
                        return IOTCON_ERROR_OUT_OF_MEMORY;
                }
 
@@ -308,8 +342,10 @@ static int _icd_dbus_resource_list_append_handle(const gchar *sender, void *hand
        while (cur_hd) {
                rsrc_handle = cur_hd->data;
 
-               if (rsrc_handle->handle == GINT_TO_POINTER(handle)) {
-                       ERR("resource handle(%u, %u) already exist", rsrc_handle->handle, rsrc_handle->number);
+               if (rsrc_handle->handle == handle) {
+                       ERR("resource handle(%u, %u) already exist", rsrc_handle->handle,
+                                       rsrc_handle->number);
+                       _icd_dbus_client_list_unlock();
                        return IOTCON_ERROR_ALREADY;
                }
 
@@ -323,6 +359,7 @@ static int _icd_dbus_resource_list_append_handle(const gchar *sender, void *hand
                        free(new_bus->sender);
                        free(new_bus);
                }
+               _icd_dbus_client_list_unlock();
                return IOTCON_ERROR_OUT_OF_MEMORY;
        }
 
@@ -334,8 +371,9 @@ static int _icd_dbus_resource_list_append_handle(const gchar *sender, void *hand
        bus->hdlist = g_list_append(bus->hdlist, rsrc_handle);
 
        if (false == sender_exist)
-               icd_bus_list = g_list_append(icd_bus_list, bus);
+               icd_dbus_client_list = g_list_append(icd_dbus_client_list, bus);
 
+       _icd_dbus_client_list_unlock();
        return IOTCON_ERROR_NONE;
 }
 
@@ -355,6 +393,7 @@ static gboolean _dbus_handle_register_resource(icDbus *object,
        handle = icd_ioty_register_resource(uri_path, resource_types, ifaces, properties);
        if (handle) {
                sender = g_dbus_method_invocation_get_sender(invocation);
+
                ret = _icd_dbus_resource_list_append_handle(sender, handle, signal_number);
                if (IOTCON_ERROR_NONE != ret) {
                        ERR("_icd_dbus_resource_list_append_handle() Fail(%d)", ret);
@@ -382,7 +421,7 @@ static gboolean _dbus_handle_unregister_resource(icDbus *object,
        else
                DBG("handle(%u) deregistered", resource);
 
-       _icd_dbus_resource_handle_free(resource);
+       _icd_dbus_resource_handle_free(GINT_TO_POINTER(resource));
 
        ic_dbus_complete_unregister_resource(object, invocation, ret);
 
@@ -595,7 +634,8 @@ static gboolean _dbus_handle_notify_list_of_observers(icDbus *object,
 {
        int ret;
 
-       ret = icd_ioty_notify_list_of_observers(resource, notify_msg, observers);
+       ret = icd_ioty_notify_list_of_observers(GINT_TO_POINTER(resource), notify_msg,
+                       observers);
        if (IOTCON_ERROR_NONE != ret)
                ERR("icd_ioty_notify_list_of_observers() Fail(%d)", ret);
 
@@ -610,7 +650,7 @@ static gboolean _dbus_handle_notify_all(icDbus *object, GDBusMethodInvocation *i
 {
        int ret;
 
-       ret = icd_ioty_notify_all(resource);
+       ret = icd_ioty_notify_all(GINT_TO_POINTER(resource));
        if (IOTCON_ERROR_NONE != ret)
                ERR("icd_ioty_notify_all() Fail(%d)", ret);
 
index a7386f8..15adac1 100644 (file)
 #ifndef __IOT_CONNECTIVITY_MANAGER_DAEMON_DBUS_H__
 #define __IOT_CONNECTIVITY_MANAGER_DAEMON_DBUS_H__
 
-int icd_dbus_bus_list_get_info(int handle, unsigned int *sig_num, const gchar **sender);
+#include <glib.h>
+
+int icd_dbus_bus_list_get_info(void *handle, unsigned int *sig_num, gchar **sender);
 int icd_dbus_emit_signal(const char *dest, const char *sig_name, GVariant *value);
 unsigned int icd_dbus_init();
 void icd_dbus_deinit(unsigned int id);
 
-
 #endif /*__IOT_CONNECTIVITY_MANAGER_DAEMON_DBUS_H__*/
index acbc3d5..3412e88 100644 (file)
@@ -46,6 +46,20 @@ enum _icd_secure_type
 };
 
 
+struct icd_req_context {
+       unsigned int signum;
+       char *sender;
+       char *payload;
+       int types;
+       int observer_id;
+       int observe_action;
+       OCRequestHandle request_h;
+       OCResourceHandle resource_h;
+       GVariantBuilder *options;
+       GVariantBuilder *query;
+};
+
+
 struct icd_find_context {
        unsigned int signum;
        char *sender;
@@ -63,6 +77,7 @@ struct icd_get_context {
        GVariantBuilder *options;
 };
 
+
 void icd_ioty_ocprocess_stop()
 {
        icd_ioty_alive = 0;
@@ -125,12 +140,175 @@ static int _ocprocess_worker_start(_ocprocess_fn fn, void *ctx)
 }
 
 
-OCEntityHandlerResult EntityHandlerWrapper(OCEntityHandlerFlag flag,
-               OCEntityHandlerRequest *entityHandlerRequest)
+static int _ocprocess_response_signal(const char *dest, const char *signal,
+               unsigned int signum, GVariant *value)
 {
-       FN_CALL;
+       int ret;
+       char sig_name[IC_DBUS_SIGNAL_LENGTH] = {0};
+
+       ret = snprintf(sig_name, sizeof(sig_name), "%s_%u", signal, signum);
+       if (ret <= 0 || sizeof(sig_name) <= ret) {
+               ERR("snprintf() Fail(%d)", ret);
+               return IOTCON_ERROR_UNKNOWN;
+       }
+
+       ret = icd_dbus_emit_signal(dest, sig_name, value);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("icd_dbus_emit_signal() Fail(%d)", ret);
+               return ret;
+       }
+
+       return IOTCON_ERROR_NONE;
+}
+
+
+static inline GVariantBuilder* _ocprocess_parse_header_options(
+               OCHeaderOption *oic_option, int option_size)
+{
+       int i;
+       GVariantBuilder *options;
+
+       options = g_variant_builder_new(G_VARIANT_TYPE("a(qs)"));
+       for (i = 0; i < option_size; i++) {
+               g_variant_builder_add(options, "(qs)", oic_option[i].optionID,
+                               oic_option[i].optionData);
+       }
+
+       return options;
+}
+
+
+static int _worker_req_handler(void *context)
+{
+       int ret;
+       GVariant *value;
+       struct icd_req_context *ctx = context;
+
+       RETV_IF(NULL == ctx, IOTCON_ERROR_INVALID_PARAMETER);
+
+       value = g_variant_new("(ia(qs)a(ss)iisii)",
+                       ctx->types,
+                       ctx->options,
+                       ctx->query,
+                       ctx->observe_action,
+                       ctx->observer_id,
+                       ctx->payload,
+                       GPOINTER_TO_INT(ctx->request_h),
+                       GPOINTER_TO_INT(ctx->resource_h));
+
+       ret = _ocprocess_response_signal(ctx->sender, IC_DBUS_SIGNAL_REQUEST_HANDLER,
+                       ctx->signum, value);
+       if (IOTCON_ERROR_NONE != ret)
+               ERR("_ocprocess_response_signal() Fail(%d)", ret);
+
+       free(ctx->sender);
+       free(ctx->payload);
+       g_variant_builder_unref(ctx->options);
+       g_variant_builder_unref(ctx->query);
+       free(ctx);
+
+       return ret;
+}
+
+
+OCEntityHandlerResult icd_ioty_ocprocess_req_handler(OCEntityHandlerFlag flag,
+               OCEntityHandlerRequest *request)
+{
+       int ret;
+       unsigned int signal_number;
+       char *query_str, *query_key, *query_value;
+       char *token, *save_ptr1, *save_ptr2;
+       char *sender = NULL;
+       struct icd_req_context *req_ctx;
 
-       DBG("reqJSONPayload : %s", entityHandlerRequest->reqJSONPayload);
+       RETV_IF(NULL == request, OC_EH_ERROR);
+
+       req_ctx = calloc(1, sizeof(struct icd_req_context));
+       if (NULL == req_ctx) {
+               ERR("calloc() Fail(%d)", errno);
+               return OC_EH_ERROR;
+       }
+
+       /* handle */
+       req_ctx->request_h = request->requestHandle;
+       req_ctx->resource_h = request->resource;
+
+       ret = icd_dbus_bus_list_get_info(req_ctx->resource_h, &signal_number, &sender);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("icd_dbus_bus_list_get_info() Fail(%d)", ret);
+               free(req_ctx);
+               return OC_EH_ERROR;
+       }
+
+       /* signal number & sender */
+       req_ctx->signum = signal_number;
+       req_ctx->sender = sender;
+
+       /* request type */
+       if (OC_REQUEST_FLAG & flag) {
+               switch (request->method) {
+               case OC_REST_GET:
+                       req_ctx->types = IOTCON_REQUEST_GET;
+                       req_ctx->payload = strdup(IC_STR_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->payload = ic_utils_strdup(request->reqJSONPayload);
+                       break;
+               case OC_REST_POST:
+                       req_ctx->types = IOTCON_REQUEST_POST;
+                       req_ctx->payload = ic_utils_strdup(request->reqJSONPayload);
+                       break;
+               case OC_REST_DELETE:
+                       req_ctx->types = IOTCON_REQUEST_DELETE;
+                       req_ctx->payload = strdup(IC_STR_NULL);
+                       break;
+               default:
+                       free(req_ctx->sender);
+                       free(req_ctx);
+                       return OC_EH_ERROR;
+               }
+       }
+
+       /* header options */
+       req_ctx->options = _ocprocess_parse_header_options(
+                       request->rcvdVendorSpecificHeaderOptions,
+                       request->numRcvdVendorSpecificHeaderOptions);
+
+       /* query */
+       req_ctx->query = g_variant_builder_new(G_VARIANT_TYPE("a(ss)"));
+       query_str = request->query;
+       while ((token = strtok_r(query_str, "&", &save_ptr1))) {
+               while ((query_key = strtok_r(token, "=", &save_ptr2))) {
+                       token = NULL;
+                       query_value = strtok_r(token, "=", &save_ptr2);
+                       if (NULL == query_value)
+                               break;
+
+                       g_variant_builder_add(req_ctx->query, "(ss)", query_key, query_value);
+               }
+               query_str = NULL;
+       }
+
+       ret = _ocprocess_worker_start(_worker_req_handler, req_ctx);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("_ocprocess_worker_start() Fail(%d)", ret);
+               free(req_ctx->sender);
+               free(req_ctx->payload);
+               g_variant_builder_unref(req_ctx->options);
+               g_variant_builder_unref(req_ctx->query);
+               free(req_ctx);
+               return OC_EH_ERROR;
+       }
+
+       /* DO NOT FREE req_ctx. It MUST be freed in the _worker_req_handler func */
 
        return OC_EH_OK;
 }
@@ -159,28 +337,6 @@ gpointer icd_ioty_ocprocess_thread(gpointer data)
 }
 
 
-static int _ocprocess_response_signal(const char *dest, const char *signal,
-               unsigned int signum, GVariant *value)
-{
-       int ret;
-       char sig_name[IC_DBUS_SIGNAL_LENGTH] = {0};
-
-       ret = snprintf(sig_name, sizeof(sig_name), "%s_%u", signal, signum);
-       if (ret <= 0 || sizeof(sig_name) <= ret) {
-               ERR("snprintf() Fail(%d)", ret);
-               return IOTCON_ERROR_UNKNOWN;
-       }
-
-       ret = icd_dbus_emit_signal(dest, sig_name, value);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("icd_dbus_emit_signal() Fail(%d)", ret);
-               return ret;
-       }
-
-       return IOTCON_ERROR_NONE;
-}
-
-
 /*
  * returned string SHOULD be released by you
  */
@@ -280,7 +436,7 @@ static inline int _find_cb_response(JsonObject *rsrc_obj,
        ret = _ocprocess_response_signal(ctx->sender, IC_DBUS_SIGNAL_FOUND_RESOURCE,
                        ctx->signum, value);
        if (IOTCON_ERROR_NONE != ret) {
-               ERR("icd_dbus_emit_signal() Fail(%d)", ret);
+               ERR("_ocprocess_response_signal() Fail(%d)", ret);
                return ret;
        }
 
@@ -363,7 +519,7 @@ static int _worker_find_cb(void *context)
 
 
 OCStackApplicationResult icd_ioty_ocprocess_find_cb(void *ctx, OCDoHandle handle,
-               OCClientResponse *clientResponse)
+               OCClientResponse *resp)
 {
        int ret;
        OCDevAddr *dev_addr;
@@ -371,10 +527,8 @@ OCStackApplicationResult icd_ioty_ocprocess_find_cb(void *ctx, OCDoHandle handle
        icd_sig_ctx_s *sig_context = ctx;
 
        RETV_IF(NULL == ctx, OC_STACK_KEEP_TRANSACTION);
-       RETV_IF(NULL == clientResponse, OC_STACK_KEEP_TRANSACTION);
-       RETV_IF(NULL == clientResponse->resJSONPayload, OC_STACK_KEEP_TRANSACTION);
-
-       DBG("JSON Payload : %s", clientResponse->resJSONPayload);
+       RETV_IF(NULL == resp, OC_STACK_KEEP_TRANSACTION);
+       RETV_IF(NULL == resp->resJSONPayload, OC_STACK_KEEP_TRANSACTION);
 
        find_ctx = calloc(1, sizeof(struct icd_find_context));
        if (NULL == find_ctx) {
@@ -388,13 +542,13 @@ OCStackApplicationResult icd_ioty_ocprocess_find_cb(void *ctx, OCDoHandle handle
                free(find_ctx);
                return OC_STACK_KEEP_TRANSACTION;
        }
-       memcpy(dev_addr, clientResponse->addr, sizeof(OCDevAddr));
+       memcpy(dev_addr, resp->addr, sizeof(OCDevAddr));
 
        find_ctx->signum = sig_context->signum;
        find_ctx->sender = ic_utils_strdup(sig_context->sender);
-       find_ctx->payload = ic_utils_strdup(clientResponse->resJSONPayload);
+       find_ctx->payload = ic_utils_strdup(resp->resJSONPayload);
        find_ctx->dev_addr = dev_addr;
-       find_ctx->conn_type = clientResponse->connType;
+       find_ctx->conn_type = resp->connType;
 
        ret = _ocprocess_worker_start(_worker_find_cb, find_ctx);
        if (IOTCON_ERROR_NONE != ret) {
@@ -450,27 +604,6 @@ static void _get_cb_response_error(const char *dest, unsigned int signum, int re
 }
 
 
-static inline GVariantBuilder* _ocprocess_parse_header_options(OCClientResponse *resp)
-{
-       int i;
-       GVariantBuilder *options;
-
-       RETV_IF(NULL == resp, NULL);
-
-       if (0 == resp->numRcvdVendorSpecificHeaderOptions)
-               return NULL;
-
-       options = g_variant_builder_new(G_VARIANT_TYPE("a(qs)"));
-       for (i = 0; i < resp->numRcvdVendorSpecificHeaderOptions; i++) {
-               g_variant_builder_add(options, "(qs)",
-                               resp->rcvdVendorSpecificHeaderOptions[i].optionID,
-                               resp->rcvdVendorSpecificHeaderOptions[i].optionData);
-       }
-
-       return options;
-}
-
-
 OCStackApplicationResult icd_ioty_ocprocess_get_cb(void *ctx, OCDoHandle handle,
                OCClientResponse *resp)
 {
@@ -501,7 +634,8 @@ OCStackApplicationResult icd_ioty_ocprocess_get_cb(void *ctx, OCDoHandle handle,
        result = resp->result;
        if (result == OC_STACK_OK) {
                res = IOTCON_RESPONSE_RESULT_OK;
-               options = _ocprocess_parse_header_options(resp);
+               options = _ocprocess_parse_header_options(resp->rcvdVendorSpecificHeaderOptions,
+                               resp->numRcvdVendorSpecificHeaderOptions);
        } else {
                WARN("resp error(%d)", result);
                res = IOTCON_RESPONSE_RESULT_ERROR;
index 5dcb830..42756b5 100644 (file)
 
 void icd_ioty_ocprocess_stop();
 
-OCEntityHandlerResult EntityHandlerWrapper(OCEntityHandlerFlag flag,
-               OCEntityHandlerRequest *entityHandlerRequest);
-
 gpointer icd_ioty_ocprocess_thread(gpointer data);
 
+OCEntityHandlerResult icd_ioty_ocprocess_req_handler(OCEntityHandlerFlag flag,
+               OCEntityHandlerRequest *request);
+
 OCStackApplicationResult icd_ioty_ocprocess_find_cb(void* ctx, OCDoHandle handle,
-               OCClientResponse* clientResponse);
+               OCClientResponse* resp);
 
 OCStackApplicationResult icd_ioty_ocprocess_get_cb(void* ctx, OCDoHandle handle,
-               OCClientResponse* clientResponse);
+               OCClientResponse* resp);
 
 #endif /*__IOT_CONNECTIVITY_MANAGER_DAEMON_IOTIVITY_THREAD_H__*/
index f014e36..c9b7cbc 100644 (file)
@@ -32,9 +32,7 @@
 #include "icd-ioty.h"
 #include "icd-ioty-ocprocess.h"
 
-GHashTable *icd_worker_table;
-
-GMutex icd_csdk_mutex;
+static GMutex icd_csdk_mutex;
 
 void icd_ioty_csdk_lock()
 {
@@ -73,6 +71,7 @@ GThread* icd_ioty_init(const char *addr, unsigned short port)
        return thread;
 }
 
+
 void icd_ioty_deinit(GThread *thread)
 {
        OCStackResult result;
@@ -113,8 +112,8 @@ OCResourceHandle icd_ioty_register_resource(const char *uri_path,
        }
 
        icd_ioty_csdk_lock();
-       ret = OCCreateResource(&handle, res_types[0], resInterface, uri_path, EntityHandlerWrapper,
-                       properties);
+       ret = OCCreateResource(&handle, res_types[0], resInterface, uri_path,
+                       icd_ioty_ocprocess_req_handler, properties);
        icd_ioty_csdk_unlock();
        if (OC_STACK_OK != ret) {
                ERR("OCCreateResource() Fail(%d)", ret);
@@ -139,7 +138,16 @@ OCResourceHandle icd_ioty_register_resource(const char *uri_path,
 
 int icd_ioty_unregister_resource(OCResourceHandle resource_handle)
 {
-       // TODO : To be implemented
+       OCStackResult ret;
+
+       icd_ioty_csdk_lock();
+       ret = OCDeleteResource(resource_handle);
+       icd_ioty_csdk_unlock();
+       if (OC_STACK_OK != ret) {
+               ERR("OCDeleteResource() Fail(%d)", ret);
+               return IOTCON_ERROR_IOTIVITY;
+       }
+
        return IOTCON_ERROR_NONE;
 }
 
@@ -180,7 +188,9 @@ int icd_ioty_bind_interface(OCResourceHandle resourceHandle, iotcon_interface_e
                return ret;
        }
 
+       icd_ioty_csdk_lock();
        result = OCBindResourceInterfaceToResource(resourceHandle, resource_interface);
+       icd_ioty_csdk_unlock();
        if (OC_STACK_OK != result) {
                ERR("OCBindResourceInterfaceToResource() Fail(%d)", result);
                return IOTCON_ERROR_IOTIVITY;
@@ -193,7 +203,9 @@ int icd_ioty_bind_type(OCResourceHandle resource_handle, const char *resource_ty
 {
        OCStackResult ret;
 
+       icd_ioty_csdk_lock();
        ret = OCBindResourceTypeToResource(resource_handle, resource_type);
+       icd_ioty_csdk_unlock();
        if (OC_STACK_OK != ret) {
                ERR("OCBindResourceTypeToResource() Fail(%d)", ret);
                return IOTCON_ERROR_IOTIVITY;
@@ -205,40 +217,180 @@ int icd_ioty_bind_type(OCResourceHandle resource_handle, const char *resource_ty
 
 int icd_ioty_bind_resource(OCResourceHandle parent, OCResourceHandle child)
 {
-       // TODO : To be implemented
+       OCStackResult ret;
+
+       icd_ioty_csdk_lock();
+       ret = OCBindResource(parent, child);
+       icd_ioty_csdk_unlock();
+       if (OC_STACK_OK != ret) {
+               ERR("OCBindResource() Fail(%d)", ret);
+               return IOTCON_ERROR_IOTIVITY;
+       }
+
        return IOTCON_ERROR_NONE;
 }
 
 
 int icd_ioty_unbind_resource(OCResourceHandle parent, OCResourceHandle child)
 {
-       // TODO : To be implemented
+       OCStackResult ret;
+
+       icd_ioty_csdk_lock();
+       ret = OCUnBindResource(parent, child);
+       icd_ioty_csdk_unlock();
+       if (OC_STACK_OK != ret) {
+               ERR("OCUnBindResource() Fail(%d)", ret);
+               return IOTCON_ERROR_IOTIVITY;
+       }
+
        return IOTCON_ERROR_NONE;
 }
 
 
-int icd_ioty_notify_list_of_observers(int resHandle, GVariant *msg, GVariant *observers)
+int icd_ioty_notify_list_of_observers(void *handle, GVariant *msg, GVariant *observers)
 {
-       // TODO : To be implemented
+       int i, error_code, obs_length;
+       char *repr_json = NULL;
+       GVariantIter obs_iter, msg_iter;
+       OCStackResult ret;
+
+       g_variant_iter_init(&obs_iter, observers);
+       obs_length = g_variant_iter_n_children(&obs_iter);
+
+       /* Variable-length Array */
+       OCObservationId obs_ids[obs_length];
+
+       for (i = 0; i < obs_length; i++)
+               g_variant_iter_loop(&obs_iter, "i", &obs_ids[i]);
+
+       g_variant_iter_init(&msg_iter, msg);
+       g_variant_iter_loop(&msg_iter, "(i&s)", &error_code, &repr_json);
+       /* TODO : How to use error_code. */
+
+       icd_ioty_csdk_lock();
+       /* TODO : QoS is come from lib. And user can set QoS to client structure.  */
+       ret = OCNotifyListOfObservers(handle, obs_ids, obs_length, repr_json, OC_HIGH_QOS);
+       icd_ioty_csdk_unlock();
+
+       if (OC_STACK_NO_OBSERVERS == ret) {
+               WARN("No Observers. Stop Notifying");
+               return IOTCON_ERROR_NONE;
+       } else if (OC_STACK_OK != ret) {
+               ERR("OCNotifyListOfObservers() Fail(%d)", ret);
+               return IOTCON_ERROR_IOTIVITY;
+       }
+
        return IOTCON_ERROR_NONE;
 }
 
 
-int icd_ioty_notify_all(int resHandle)
+int icd_ioty_notify_all(void *handle)
 {
-       // TODO : To be implemented
+       OCStackResult ret;
+
+       icd_ioty_csdk_lock();
+       /* TODO : QoS is come from lib. And user can set QoS to client structure.  */
+       ret = OCNotifyAllObservers(handle, OC_HIGH_QOS);
+       icd_ioty_csdk_unlock();
+
+       if (OC_STACK_NO_OBSERVERS == ret) {
+               WARN("No Observers. Stop Notifying");
+               return IOTCON_ERROR_NONE;
+       } else if (OC_STACK_OK != ret) {
+               ERR("OCNotifyAllObservers() Fail(%d)", ret);
+               return IOTCON_ERROR_IOTIVITY;
+       }
+
+       return IOTCON_ERROR_NONE;
+}
+
+
+static int _ioty_get_header_options(GVariantIter *src, int src_size,
+               OCHeaderOption dest[], int dest_size)
+{
+       int i = 0;
+       char *option_data;
+       unsigned short option_id;
+
+       RETV_IF(NULL == dest, IOTCON_ERROR_INVALID_PARAMETER);
+
+       if (dest_size < src_size) {
+               ERR("Exceed Size(%d)", src_size);
+               return IOTCON_ERROR_INVALID_PARAMETER;
+       }
+
+       while (g_variant_iter_loop(src, "(q&s)", &option_id, &option_data)) {
+               dest[i].protocolID = OC_COAP_ID;
+               dest[i].optionID = option_id;
+               dest[i].optionLength = strlen(option_data) + 1;
+               memcpy(dest[i].optionData, option_data, dest[i].optionLength);
+               i++;
+       }
+
        return IOTCON_ERROR_NONE;
 }
 
 
 int icd_ioty_send_response(GVariant *resp)
 {
-       // TODO : To be implemented
+       int result, error_code, options_size;
+       int request_handle, resource_handle;
+       char *new_uri_path, *repr_json;
+       GVariantIter *options;
+       OCStackResult ret;
+       OCEntityHandlerResponse response = {0};
+
+       g_variant_get(resp, "(&sia(qs)i&sii)",
+                       &new_uri_path,
+                       &error_code,
+                       &options,
+                       &result,
+                       &repr_json,
+                       &request_handle,
+                       &resource_handle);
+
+       response.requestHandle = GINT_TO_POINTER(request_handle);
+       response.resourceHandle = GINT_TO_POINTER(resource_handle);
+       response.ehResult = (OCEntityHandlerResult)result;
+
+       if (OC_EH_RESOURCE_CREATED == response.ehResult)
+               snprintf(response.resourceUri, sizeof(response.resourceUri), "%s", new_uri_path);
+
+       options_size = g_variant_iter_n_children(options);
+       response.numSendVendorSpecificHeaderOptions = options_size;
+
+       if (0 != options_size) {
+               int ret= _ioty_get_header_options(options,
+                               response.numSendVendorSpecificHeaderOptions,
+                               response.sendVendorSpecificHeaderOptions,
+                               sizeof(response.sendVendorSpecificHeaderOptions)
+                               / sizeof(response.sendVendorSpecificHeaderOptions[0]));
+
+               if (IOTCON_ERROR_NONE != ret)
+                       ERR("_ioty_get_header_options() Fail(%d)", ret);
+       }
+       g_variant_iter_free(options);
+
+       response.payload = repr_json;
+       response.payloadSize = strlen(response.payload) + 1;
+
+       /* related to block transfer */
+       response.persistentBufferFlag = 0;
+
+       icd_ioty_csdk_lock();
+       ret = OCDoResponse(&response);
+       icd_ioty_csdk_unlock();
+
+       if (OC_STACK_OK != ret) {
+               ERR("OCDoResponse() Fail(%d)", ret);
+               return IOTCON_ERROR_IOTIVITY;
+       }
+
        return IOTCON_ERROR_NONE;
 }
 
 
-void _ioty_free_signal_context(void *data)
+static void _ioty_free_signal_context(void *data)
 {
        icd_sig_ctx_s *context = data;
        free(context->sender);
@@ -299,30 +451,6 @@ int icd_ioty_find_resource(const char *host_address, const char *resource_type,
 }
 
 
-static int _ioty_get_header_options(GVariantIter *src, int src_size,
-               OCHeaderOption dest[], int dest_size)
-{
-       gsize len;
-       char *option_data;
-       unsigned short option_id;
-
-       RETV_IF(NULL == dest, IOTCON_ERROR_INVALID_PARAMETER);
-
-       if (dest_size < src_size) {
-               ERR("Exceed Size(%d)", src_size);
-               return IOTCON_ERROR_INVALID_PARAMETER;
-       }
-
-       while (g_variant_iter_loop(src, "(q&s)", &option_id, &option_data)) {
-               dest[i].protocolID = OC_COAP_ID;
-               dest[i].optionID = option_id;
-               dest[i].optionLength = strlen(option_data)+1;
-               memcpy(dest[i].optionData, option_data, dest[i].optionLength);
-       }
-
-       return IOTCON_ERROR_NONE;
-}
-
 /*
  * returned string SHOULD be released by you
  */
@@ -335,10 +463,6 @@ static char* _icd_ioty_resource_generate_uri(char *host, char *uri_path, GVarian
        char uri_buf[PATH_MAX] = {0};
 
        len = snprintf(uri_buf, sizeof(uri_buf), "%s%s", host, uri_path);
-       if (len < 0) {
-               ERR("snprintf() Fail");
-               return NULL;
-       }
 
        /* remove suffix '/' */
        if ('/' == uri_buf[strlen(uri_buf) - 1]) {
@@ -360,12 +484,6 @@ static char* _icd_ioty_resource_generate_uri(char *host, char *uri_path, GVarian
                        query_len = snprintf(uri_buf + len, sizeof(uri_buf), "&%s=%s", key, value);
                }
 
-               if (0 > query_len) {
-                       ERR("snprintf() Fail");
-                       g_variant_iter_free(queryIter);
-                       return NULL;
-               }
-
                len += query_len;
        }
        g_variant_iter_free(queryIter);
index cb89fdf..19e0270 100644 (file)
@@ -52,9 +52,9 @@ int icd_ioty_bind_resource(void *parent, void *child);
 
 int icd_ioty_unbind_resource(void *parent, void *child);
 
-int icd_ioty_notify_list_of_observers(int resHandle, GVariant *msg, GVariant *observers);
+int icd_ioty_notify_list_of_observers(void *handle, GVariant *msg, GVariant *observers);
 
-int icd_ioty_notify_all(int resHandle);
+int icd_ioty_notify_all(void *handle);
 
 int icd_ioty_send_response(GVariant *resp);
 
index 788de63..d8578eb 100644 (file)
@@ -57,18 +57,19 @@ GVariant* icl_dbus_notimsg_to_gvariant(struct icl_notify_msg *msg)
        GVariant *value;
        GVariantBuilder *builder;
 
-       builder = g_variant_builder_new(G_VARIANT_TYPE("a(iis)"));
+       builder = g_variant_builder_new(G_VARIANT_TYPE("a(is)"));
 
        if (msg) {
+               /* TODO Make repr_json using interface */
                repr_json = icl_repr_generate_json(msg->repr, false);
                if (NULL == repr_json) {
                        ERR("icl_repr_generate_json() Fail");
                        g_variant_builder_unref(builder);
                        return NULL;
                }
-               g_variant_builder_add(builder, "(iis)", msg->error_code, msg->iface, repr_json);
+               g_variant_builder_add(builder, "(is)", msg->error_code, repr_json);
        }
-       value = g_variant_new("a(iis)", builder);
+       value = g_variant_new("a(is)", builder);
 
        free(repr_json);
        g_variant_builder_unref(builder);
@@ -79,6 +80,7 @@ GVariant* icl_dbus_notimsg_to_gvariant(struct icl_notify_msg *msg)
 
 GVariant* icl_dbus_response_to_gvariant(struct icl_resource_response *response)
 {
+       char *repr_json;
        GHashTableIter iter;
        GVariantBuilder *options;
        gpointer option_id, option_data;
@@ -92,13 +94,20 @@ GVariant* icl_dbus_response_to_gvariant(struct icl_resource_response *response)
                        g_variant_builder_add(options, "(qs)", GPOINTER_TO_INT(option_id), option_data);
        }
 
-       value = g_variant_new("(sia(qs)iisii)",
+       /* TODO Make repr_json using interface */
+       repr_json = icl_repr_generate_json(response->repr, false);
+       if (NULL == repr_json) {
+               ERR("icl_repr_generate_json() Fail");
+               g_variant_builder_unref(options);
+               return NULL;
+       }
+
+       value = g_variant_new("(sia(qs)isii)",
                        ic_utils_dbus_encode_str(response->new_uri_path),
                        response->error_code,
                        options,
-                       response->iface,
                        response->result,
-                       icl_repr_generate_json(response->repr, false),
+                       repr_json,
                        GPOINTER_TO_INT(response->request_handle),
                        GPOINTER_TO_INT(response->resource_handle));
 
index 61df554..d0ea28b 100644 (file)
@@ -125,30 +125,26 @@ static void _icl_dbus_request_handler(GDBusConnection *connection,
 {
        FN_CALL;
 
-       int index = 0;
        GVariantIter *options;
        unsigned short option_id;
        char *option_data;
        GVariantIter *query;
        char *key = NULL;
        char *value = NULL;
-       GVariantIter *repr;
        char *repr_json;
-       char *repr_uri_path;
        int request_handle;
        int resource_handle;
        struct icl_resource_request request = {0};
        icl_cb_container_s *cb_container = user_data;
        iotcon_request_handler_cb cb = cb_container->cb;
 
-       g_variant_get(parameters, "(i&sa(qs)a(ss)iiasii)",
+       g_variant_get(parameters, "(ia(qs)a(ss)ii&sii)",
                        &request.types,
-                       &request.uri_path,
                        &options,
                        &query,
                        &request.observation_info.action,
                        &request.observation_info.observer_id,
-                       &repr,
+                       &repr_json,
                        &request_handle,
                        &resource_handle);
 
@@ -169,27 +165,16 @@ static void _icl_dbus_request_handler(GDBusConnection *connection,
        request.request_handle = GINT_TO_POINTER(request_handle);
        request.resource_handle = GINT_TO_POINTER(resource_handle);
 
-       for (index = 0; g_variant_iter_loop(repr, "&s", &repr_json); index++) {
-               iotcon_repr_h cur_repr = icl_repr_parse_json(repr_json);
-               if (NULL == cur_repr) {
-                       ERR("icl_repr_parse_json() Fail");
-                       iotcon_options_free(request.header_options);
-                       iotcon_query_free(request.query);
-                       if (request.repr)
-                               iotcon_repr_free(request.repr);
-                       g_variant_iter_free(repr);
+       if (ic_utils_dbus_decode_str(repr_json)) {
+               request.repr = icl_repr_create_repr(repr_json);
+               if (NULL == request.repr) {
+                       ERR("icl_repr_create_repr() Fail");
                        return;
                }
-               repr_uri_path = icl_repr_json_get_uri_path(repr_json);
-               iotcon_repr_set_uri_path(cur_repr, repr_uri_path);
-               free(repr_uri_path);
-
-               if (0 == index)
-                       request.repr = cur_repr;
-               else
-                       request.repr->children = g_list_append(request.repr->children, cur_repr);
        }
-       g_variant_iter_free(repr);
+
+       /* TODO remove request.uri */
+       request.uri_path = "temp_uri_path";
 
        if (cb)
                cb(&request, cb_container->user_data);
index f0916e8..14a26d6 100644 (file)
@@ -19,7 +19,7 @@
 #include "icl-request.h"
 
 /* The content of the request should not be freed by user. */
-API int iotcon_request_get_uri(iotcon_request_h request, char **uri_path)
+API int iotcon_request_get_uri_path(iotcon_request_h request, char **uri_path)
 {
        RETV_IF(NULL == request, IOTCON_ERROR_INVALID_PARAMETER);
        RETV_IF(NULL == uri_path, IOTCON_ERROR_INVALID_PARAMETER);
index 641dc6f..130c140 100644 (file)
--- a/lib/icl.c
+++ b/lib/icl.c
@@ -307,7 +307,7 @@ API int iotcon_resource_get_nth_child(iotcon_resource_h parent, int index,
 
 
 /* The content of the resource should not be freed by user. */
-API int iotcon_resource_get_uri(iotcon_resource_h resource, char **uri_path)
+API int iotcon_resource_get_uri_path(iotcon_resource_h resource, char **uri_path)
 {
        RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
        RETV_IF(NULL == uri_path, IOTCON_ERROR_INVALID_PARAMETER);
index 39d3738..c28acf5 100644 (file)
@@ -116,7 +116,7 @@ typedef struct icl_resource* iotcon_resource_h;
 int iotcon_resource_get_number_of_children(iotcon_resource_h resource, int *number);
 int iotcon_resource_get_nth_child(iotcon_resource_h parent, int index,
                iotcon_resource_h *child);
-int iotcon_resource_get_uri(iotcon_resource_h resource, char **uri_path);
+int iotcon_resource_get_uri_path(iotcon_resource_h resource, char **uri_path);
 int iotcon_resource_get_types(iotcon_resource_h resource, iotcon_resource_types_h *types);
 int iotcon_resource_get_interfaces(iotcon_resource_h resource, int *ifaces);
 int iotcon_resource_is_observable(iotcon_resource_h resource, bool *observable);
@@ -131,7 +131,7 @@ int iotcon_client_is_observable(iotcon_client_h resource, bool *observable);
 int iotcon_client_set_options(iotcon_client_h resource, iotcon_options_h header_options);
 
 typedef struct icl_resource_request* iotcon_request_h;
-int iotcon_request_get_uri(iotcon_request_h request, char **uri_path);
+int iotcon_request_get_uri_path(iotcon_request_h request, char **uri_path);
 int iotcon_request_get_representation(iotcon_request_h request, iotcon_repr_h *repr);
 int iotcon_request_get_types(iotcon_request_h request, int *types);
 int iotcon_request_get_options(iotcon_request_h request, iotcon_options_h *options);
index b58f4da..5ccad1e 100644 (file)
@@ -38,7 +38,7 @@ void iotcon_deinitialize();
  * Even if iotcon is disconnected, it is allowed to derive elements from handles.
  * On the other hand, using the functions related to connection are not allowed.
  * For example, when iotcon is disconnected, it is possible to use
- * "iotcon_resource_get_uri()", but it is impossible to use "iotcon_notify_all()".
+ * "iotcon_resource_get_uri_path()", but it is impossible to use "iotcon_notify_all()".
  * Thus, if you want to use functions related to connection, above all, you should free
  * handles, and you should make new handles.
  * In case of "iotcon_client_h", if the information of the resource owner are unchanged