comp-manager: change send_data method as unicast like 'find group'
authorsaerome.kim <saerome.kim@samsung.com>
Mon, 29 Jan 2018 09:10:20 +0000 (18:10 +0900)
committersaerome.kim <saerome.kim@samsung.com>
Mon, 2 Jul 2018 10:38:47 +0000 (19:38 +0900)
Signed-off-by: saerome.kim <saerome.kim@samsung.com>
capi/src/companion.c
src/companion-manager/include/comp_group.h
src/companion-manager/include/comp_iot.h
src/companion-manager/src/comp_group.c
src/companion-manager/src/comp_iot.c

index a2fe63f2d5aeb65a5e49c64cf4e4d5e33aa46e58..3acba29fde77043b7d28dd14b34c2711ac43f773 100644 (file)
@@ -508,8 +508,10 @@ int companion_send_data(companion_device_h device, char *data, int len,
        companion_device_t *dev = (companion_device_t *)device;
 
        buf = g_malloc0(len + 1);
-       if (NULL == buf)
+       if (NULL == buf) {
+               _ERR("g_malloc0 is failed");
                return COMP_ERROR_OUT_OF_MEMORY;
+       }
        memcpy(buf, data, len);
 
        send_data_finish_cb.finish_cb = finish_cb;
index 4b0b15e1e26c8813cd64f306c8d4b580c9ac38a8..f4023a1ff8aebbac15c2bc20a263c623dfc897c0 100644 (file)
@@ -42,6 +42,17 @@ typedef struct {
        int permission_2;
 } comp_group_invite_info_t;
 
+/**
+ * @brief The command type for request.
+ *
+ * @since_tizen 5.0
+ */
+#define COMP_REQ_SEND_DATA "0" /**< Send data */
+#define COMP_REQ_CREATE_GROUP "1" /**< Create Group */
+#define COMP_REQ_INVITE_DEVICE "2" /**< Invite Device */
+#define COMP_REQ_EJECT_DEVICE "3" /**< Exile Device */
+#define COMP_REQ_DELETE_GROUP "4" /**< Delete Group */
+
 /* Called when daemon is start. */
 int comp_group_initialize();
 
index b1b30aa9d192db75ec1385b1dc5b47a62c938d09..e3238d683f6637381c7372bd440d07f5be1450a5 100644 (file)
@@ -18,6 +18,9 @@ typedef struct _comp_command_t {
        gchar *uuid;
        gchar *host;
        gchar *arg1;
+#ifdef SUPPORT_BASE64_ENCODING
+       int arg1_len;
+#endif
        gchar *arg2;
        gchar *arg3;
        gchar *arg4;
index 465d86d2f7d793c2c95cd83eafbb3e4889093184..bca713d20585b64335997574bd34975448bd7e1f 100644 (file)
@@ -547,7 +547,7 @@ int comp_group_send_data(gchar *uuid_dev, gchar *addr, int port, gchar *data,
                percent[0] = '\0';
 
        comp_command_t *cmd = g_new0(comp_command_t, 1);
-       cmd->command = g_strdup("5");
+       cmd->command = g_strdup(COMP_REQ_SEND_DATA);
        cmd->uuid = g_strdup(uuid_dev);
 
        if (strchr(ip, ':')) /* IPv6 Adress */
@@ -563,6 +563,9 @@ int comp_group_send_data(gchar *uuid_dev, gchar *addr, int port, gchar *data,
                return ret;
        }
        memcpy(cmd->arg1, data, len);
+#ifdef SUPPORT_BASE64_ENCODING
+       cmd->arg1_len = len;
+#endif
 
        LOG_DEBUG("UUID %s host %s", cmd->uuid, cmd->host);
 
@@ -788,7 +791,7 @@ int comp_group_request_create_group(char *uuid, char *group_name)
        LOG_ERR("[Request Create Group] to %s", uuid);
 
        comp_command_t *cmd = g_new0(comp_command_t, 1);
-       cmd->command = g_strdup("1");
+       cmd->command = g_strdup(COMP_REQ_CREATE_GROUP);
        cmd->uuid = g_strdup(uuid);
        cmd->arg1 = g_strdup(group_name);
 
@@ -809,7 +812,7 @@ int comp_group_request_invite(char *uuid, char *group_name, char *target_uuid, c
        LOG_ERR("[Request Invite] to %s", uuid);
 
        comp_command_t *cmd = g_new0(comp_command_t, 1);
-       cmd->command = g_strdup("2");
+       cmd->command = g_strdup(COMP_REQ_INVITE_DEVICE);
        cmd->uuid = g_strdup(uuid);
        cmd->arg1 = g_strdup(group_name);
        cmd->arg2 = g_strdup(target_uuid);
@@ -833,7 +836,7 @@ int comp_group_request_eject(char *uuid, char *group_name, char *target_uuid)
        LOG_ERR("[Request Eject] to %s", uuid);
 
        comp_command_t *cmd = g_new0(comp_command_t, 1);
-       cmd->command = g_strdup("3");
+       cmd->command = g_strdup(COMP_REQ_EJECT_DEVICE);
        cmd->uuid = g_strdup(uuid);
        cmd->arg1 = g_strdup(group_name);
        cmd->arg2 = g_strdup(target_uuid);
@@ -857,7 +860,7 @@ int comp_group_request_delete_group(char *uuid, char *group_name)
        LOG_ERR("[Request Delete Group] to %s", uuid);
 
        comp_command_t *cmd = g_new0(comp_command_t, 1);
-       cmd->command = g_strdup("4");
+       cmd->command = g_strdup(COMP_REQ_DELETE_GROUP);
        cmd->uuid = g_strdup(uuid);
        cmd->arg1 = g_strdup(group_name);
 
index c0f7a2f9fb1d1f828b241b53ec29413eaf5d128d..c64daff0766d74d75b7fe8d42bae8da301588a00 100644 (file)
@@ -1,8 +1,10 @@
 #include <tzplatform_config.h>
-#include <iotivity_config.h>
-#include <platform_features.h>
+
 #include <utlist.h>
+#include <base64.h>
 #include <srmutility.h>
+#include <iotivity_config.h>
+#include <platform_features.h>
 #include <ocprovisioningmanager.h>
 
 #include <comp_iot.h>
@@ -104,7 +106,7 @@ static void _request_handler(iotcon_resource_h resource, iotcon_request_h reques
 
                        iotcon_query_lookup(query, "CMD", &command);
 
-                       if (strcmp(command, "1") == 0) {
+                       if (strcmp(command, COMP_REQ_CREATE_GROUP) == 0) {
                                LOG_DEBUG("Request create group");
                                char *group_name;
                                iotcon_query_lookup(query, "name", &group_name);
@@ -113,7 +115,7 @@ static void _request_handler(iotcon_resource_h resource, iotcon_request_h reques
                                arg = g_strdup(group_name);
 
                                free(group_name);
-                       } else if (strcmp(command, "2") == 0) {
+                       } else if (strcmp(command, COMP_REQ_INVITE_DEVICE) == 0) {
                                LOG_DEBUG("Request invite");
                                char *group_name;
                                char *uuid;
@@ -128,7 +130,7 @@ static void _request_handler(iotcon_resource_h resource, iotcon_request_h reques
 
                                free(group_name);
                                free(uuid);
-                       } else if (strcmp(command, "3") == 0) {
+                       } else if (strcmp(command, COMP_REQ_EJECT_DEVICE) == 0) {
                                LOG_DEBUG("Request eject");
                                char *group_name;
                                char *uuid;
@@ -143,15 +145,45 @@ static void _request_handler(iotcon_resource_h resource, iotcon_request_h reques
 
                                free(group_name);
                                free(uuid);
-                       } else if (strcmp(command, "4") == 0) {
+                       } else if (strcmp(command, COMP_REQ_DELETE_GROUP) == 0) {
                                LOG_DEBUG("Request delete group");
                                arg = g_strdup("DELETED");
                        } else {
+#ifdef SUPPORT_BASE64_ENCODING
+                               int payload_len;
                                char *data = NULL;
+
                                LOG_DEBUG("Receive Data");
+
+                               iotcon_query_lookup(query, "data", &data);
+                               payload_len = strlen(data);
+
+                               LOG_DEBUG("data = %s payload_len = %d", data, payload_len);
+
+                               size_t outSize = B64DECODE_OUT_SAFESIZE(payload_len + 1);
+                               uint8_t* out = g_malloc0(outSize);
+                               if (NULL == out) {
+                                       LOG_ERR("Can't allocate memory for base64 str");
+                                       return;
+                               }
+                               uint32_t len = 0;
+
+                               if(B64_OK == b64Decode(data, payload_len, out, outSize, &len)) {
+                                       LOG_ERR("Base64 decoding failed.");
+                                       return;
+                               }
+                               memcpy(arg, out, len);
+
+                               LOG_DEBUG("successfully decoded to base64. %s", arg);
+
+                               iotcon_query_remove(query, "data");
+#else
+                               char *data = NULL;
                                iotcon_query_lookup(query, "data", &data);
+                               LOG_DEBUG("Receive Data = %s", data);
                                arg = g_strdup(data);
                                free(data);
+#endif
                        }
 
                        notify_request_result(command, arg, result);
@@ -165,12 +197,14 @@ static void _request_handler(iotcon_resource_h resource, iotcon_request_h reques
 
                ret = iotcon_response_set_result(response, IOTCON_RESPONSE_OK);
                if (IOTCON_ERROR_NONE != ret) {
+                       LOG_ERR("iotcon_response_set_result Faild = %d", ret);
                        iotcon_response_destroy(response);
                        return;
                }
 
                ret = iotcon_response_send(response);
                if (IOTCON_ERROR_NONE != ret) {
+                       LOG_ERR("iotcon_response_send Faild = %d", ret);
                        iotcon_response_destroy(response);
                        return;
                }
@@ -394,28 +428,49 @@ static bool _found_resource(iotcon_remote_resource_h resource,
                        iotcon_query_h query = NULL;
 
                        ret = iotcon_remote_resource_clone(resource, &resource_clone);
-                       if (IOTCON_ERROR_NONE != ret)
+                       if (IOTCON_ERROR_NONE != ret) {
+                               LOG_ERR("iotcon_remote_resource_clone failed = %d", ret);
                                return IOTCON_FUNC_CONTINUE;
+                       }
 
                        ret = iotcon_query_create(&query);
-                       if (IOTCON_ERROR_NONE != ret)
+                       if (IOTCON_ERROR_NONE != ret) {
+                               LOG_ERR("iotcon_query_create failed = %d", ret);
                                return IOTCON_FUNC_CONTINUE;
+                       }
 
                        iotcon_query_add(query, "CMD", cmd->command);
+                       LOG_DEBUG("CMD = %s", cmd->command);
 
-                       if (strcmp(cmd->command, "1") == 0) { //request create group
+                       if (strcmp(cmd->command, COMP_REQ_CREATE_GROUP) == 0) { //request create group
                                iotcon_query_add(query, "name", cmd->arg1);
-                       } else if (strcmp(cmd->command, "2") == 0) { //request invite
+                       } else if (strcmp(cmd->command, COMP_REQ_INVITE_DEVICE) == 0) { //request invite
                                iotcon_query_add(query, "name", cmd->arg1);
                                iotcon_query_add(query, "id", cmd->arg2);
                                iotcon_query_add(query, "PIN", cmd->arg3);
-                       } else if (strcmp(cmd->command, "3") == 0) { //request eject
+                       } else if (strcmp(cmd->command, COMP_REQ_EJECT_DEVICE) == 0) { //request eject
                                iotcon_query_add(query, "name", cmd->arg1);
                                iotcon_query_add(query, "id", cmd->arg2);
-                       } else if (strcmp(cmd->command, "4") == 0) { //request delete group
+                       } else if (strcmp(cmd->command, COMP_REQ_DELETE_GROUP) == 0) { //request delete group
                                iotcon_query_add(query, "name", cmd->arg1);
-                       } else if (strcmp(cmd->command, "5") == 0) { /* Send Data */
+                       } else { /* Send Data */
+#ifdef SUPPORT_BASE64_ENCODING
+                               uint32_t outSize = 0;
+                               size_t b64BufSize = B64ENCODE_OUT_SAFESIZE((cmd->arg1_len + 1));
+                               char* b64Buf = g_malloc0(b64BufSize);
+                               if (NULL == b64Buf) {
+                                       iotcon_remote_resource_destroy(resource_clone);
+                                       _clear_user_data(cmd);
+                                       return IOTCON_FUNC_CONTINUE;
+                               }
+                               b64Encode((const char *)cmd->arg1, cmd->arg1_len, b64Buf, b64BufSize, &outSize);
+                               b64Buf[b64BufSize] = '\0';
+                               iotcon_query_add(query, "data", b64Buf);
+                               LOG_DEBUG("b64BufSize =%d outSize = %d b64Buf = %s", b64BufSize, outSize, b64Buf);
+                               g_free(b64Buf);
+#else
                                iotcon_query_add(query, "data", cmd->arg1);
+#endif
                        }
 
                        ret = iotcon_remote_resource_get(resource_clone, query, _on_get, cmd);
@@ -483,7 +538,8 @@ int comp_iot_discovery_resource(comp_resource_type_e resource_type, int timeout,
 
        LOG_DEBUG("Set Resource Type : %s", get_error_message(ret));
 
-       if (cmd && (g_strcmp0(cmd->command, "5") == 0))
+       if (cmd && (g_strcmp0(cmd->command, COMP_REQ_CREATE_GROUP) == 0 ||
+                g_strcmp0(cmd->command, COMP_REQ_SEND_DATA) == 0))
                ret = iotcon_find_resource(cmd->host,
                        IOTCON_CONNECTIVITY_IP | IOTCON_CONNECTIVITY_PREFER_UDP,
                        query, _found_resource, user_data);