comp-manager: Replace iotcon_query with iotcon_representation
authorSaurav Babu <saurav.babu@samsung.com>
Mon, 26 Feb 2018 14:00:55 +0000 (19:30 +0530)
committersaerome.kim <saerome.kim@samsung.com>
Mon, 2 Jul 2018 10:38:48 +0000 (19:38 +0900)
When data(data=xx=abcd) is send using iotcon_query then only xx is
received at receiver. So this patch uses POST method in
iotcon_representation to send data. Using this we can receive correct
data(xx=abcd) at receiver.

Signed-off-by: Saurav Babu <saurav.babu@samsung.com>
src/companion-manager/src/comp_iot.c

index 99982c0..f2717ca 100755 (executable)
@@ -123,112 +123,126 @@ static void _request_handler(iotcon_resource_h resource, iotcon_request_h reques
        if (IOTCON_ERROR_NONE != ret)
                return;
 
-       if (IOTCON_REQUEST_GET == type) {
+       LOG_DEBUG("iotcon request type %d", type);
+       if (IOTCON_REQUEST_POST == type) {
                iotcon_response_h response = NULL;
-               iotcon_query_h query = NULL;
+               iotcon_representation_h req_repr;
+               iotcon_attributes_h attributes;
+               char *cmd;
+               char *arg = NULL;
+               int result;
+               comp_request_type_e command;
+
+               ret = iotcon_request_get_representation(request, &req_repr);
+               if (IOTCON_ERROR_NONE != ret) {
+                       LOG_ERR("iotcon_request_get_representation() Fail(%d)", ret);
+                       return;
+               }
 
-               ret = iotcon_request_get_query(request, &query);
-               if (IOTCON_ERROR_NONE != ret)
-                        return;
+               ret = iotcon_representation_get_attributes(req_repr, &attributes);
+               if (IOTCON_ERROR_NONE != ret) {
+                       LOG_ERR("iotcon_representation_get_attributes() Fail(%d)", ret);
+                       return;
+               }
 
-               if (IOTCON_ERROR_NONE == ret && query) {
-                       char *cmd;
-                       char *arg = NULL;
-                       int result;
-                       comp_request_type_e command;
+               ret = iotcon_attributes_get_str(attributes, "CMD", &cmd);
+               if (IOTCON_ERROR_NONE != ret) {
+                       LOG_ERR("iotcon_attributes_get_bool() Fail(%d)", ret);
+                       return;
+               }
 
-                       iotcon_query_lookup(query, "CMD", &cmd);
+               LOG_DEBUG("cmd %s", cmd);
 
-                       command = string2command(cmd);
+               command = string2command(cmd);
 
-                       if (command == COMP_REQ_CREATE_GROUP) {
-                               LOG_DEBUG("Request create group");
-                               char *group_name;
-                               iotcon_query_lookup(query, "name", &group_name);
+               if (command == COMP_REQ_CREATE_GROUP) {
+                       LOG_DEBUG("Request create group");
+                       char *group_name;
+                       iotcon_attributes_get_str(attributes, "name", &group_name);
 
-                               result = comp_group_create(group_name);
-                               arg = g_strdup(group_name);
+                       LOG_DEBUG("group_name : %s", group_name);
+                       result = comp_group_create(group_name);
+                       arg = g_strdup(group_name);
 
-                       } else if (command == COMP_REQ_INVITE_DEVICE) {
-                               LOG_DEBUG("Request invite");
-                               char *group_name;
-                               char *uuid;
+               } else if (command == COMP_REQ_INVITE_DEVICE) {
+                       LOG_DEBUG("Request invite");
+                       char *group_name;
+                       char *uuid;
 
-                               iotcon_query_lookup(query, "name", &group_name);
-                               iotcon_query_lookup(query, "id", &uuid);
+                       iotcon_attributes_get_str(attributes, "name", &group_name);
+                       iotcon_attributes_get_str(attributes, "id", &uuid);
 
-                               LOG_DEBUG("group_name : %s, UUID : %s", group_name, uuid);
+                       LOG_DEBUG("group_name : %s, UUID : %s", group_name, uuid);
 
-                               result = comp_group_invite(group_name, uuid, "12341234");
-                               arg = g_strdup(uuid);
+                       result = comp_group_invite(group_name, uuid, "12341234");
+                       arg = g_strdup(uuid);
 
-                       } else if (command == COMP_REQ_EJECT_DEVICE) {
-                               LOG_DEBUG("Request eject");
-                               char *group_name;
-                               char *uuid;
-                               comp_context_t *comp_ctx = comp_context_get_context();
+               } else if (command == COMP_REQ_EJECT_DEVICE) {
+                       LOG_DEBUG("Request eject");
+                       char *group_name;
+                       char *uuid;
+                       comp_context_t *comp_ctx = comp_context_get_context();
 
-                               iotcon_query_lookup(query, "name", &group_name);
-                               iotcon_query_lookup(query, "id", &uuid);
+                       iotcon_attributes_get_str(attributes, "name", &group_name);
+                       iotcon_attributes_get_str(attributes, "id", &uuid);
 
-                               LOG_DEBUG("group_name : %s, Self UUID : %s Target UUID : %s",
-                                                 group_name, comp_ctx->device_uuid, uuid);
+                       LOG_DEBUG("group_name : %s, Self UUID : %s Target UUID : %s",
+                                         group_name, comp_ctx->device_uuid, uuid);
 
-                               result = comp_group_dismiss(comp_ctx->device_uuid, uuid);
-                               arg = g_strdup(uuid);
+                       result = comp_group_dismiss(comp_ctx->device_uuid, uuid);
+                       arg = g_strdup(uuid);
 
-                       } else if (command == COMP_REQ_DELETE_GROUP) {
-                               LOG_DEBUG("Request delete group");
-                               arg = g_strdup("DELETED");
-                       } else if (command == COMP_REQ_SEND_DATA) {
+               } else if (command == COMP_REQ_DELETE_GROUP) {
+                       LOG_DEBUG("Request delete group");
+                       arg = g_strdup("DELETED");
+               } else if (command == COMP_REQ_SEND_DATA) {
 #ifdef SUPPORT_BASE64_ENCODING
-                               int payload_len;
-                               char *data = NULL;
-
-                               LOG_DEBUG("Receive Data");
-
-                               iotcon_query_lookup(query, "data", &data);
-                               /*
-                                 * BASE64 encoding/decoding system use '=' as padding byte
-                                 * But, query parameter does not allow use '=' character.Basically,
-                                 * So, in order to use BASE64 as query parameters, we need additioinal length param
-                                 * such as 'len=xx'
-                                 */
-                               payload_len = strlen(data); /* This data may be cliped the last padding 1~2 bytes ('='/'==') */
-
-                               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;
+                       int payload_len;
+                       char *data = NULL;
+
+                       LOG_DEBUG("Receive Data");
+
+                       iotcon_attributes_get_str(attributes, "data", &data);
+                       /*
+                        * BASE64 encoding/decoding system use '=' as padding byte
+                        * But, query parameter does not allow use '=' character.Basically,
+                        * So, in order to use BASE64 as query parameters, we need additioinal length param
+                        * such as 'len=xx'
+                        */
+                       payload_len = strlen(data); /* This data may be cliped the last padding 1~2 bytes ('='/'==') */
+
+                       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);
+                       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);
+                       LOG_DEBUG("successfully decoded to base64. %s", arg);
 
-                               iotcon_query_remove(query, "data");
+                       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);
+                       char *data = NULL;
+                       iotcon_attributes_get_str(attributes, "data", &data);
+                       LOG_DEBUG("Receive Data = %s", data);
+                       arg = g_strdup(data);
 #endif
-                       } else {
-                               LOG_ERR("Unknown request command");
-                       }
+               } else {
+                       LOG_ERR("Unknown request command");
+               }
 
-                       notify_request_result(cmd, arg, result);
+               notify_request_result(cmd, arg, result);
 
-                       free(arg);
-               }
+               free(arg);
 
                ret = iotcon_response_create(request, &response);
                if (IOTCON_ERROR_NONE != ret)
@@ -476,7 +490,8 @@ static bool _found_resource(iotcon_remote_resource_h resource,
                        LOG_DEBUG("Request to Remote Device");
 
                        iotcon_remote_resource_h resource_clone = NULL;
-                       iotcon_query_h query = NULL;
+                       iotcon_representation_h repr;
+                       iotcon_attributes_h attributes;
 
                        ret = iotcon_remote_resource_clone(resource, &resource_clone);
                        if (IOTCON_ERROR_NONE != ret) {
@@ -484,31 +499,38 @@ static bool _found_resource(iotcon_remote_resource_h resource,
                                return IOTCON_FUNC_CONTINUE;
                        }
 
-                       ret = iotcon_query_create(&query);
+                       ret = iotcon_representation_create(&repr);
                        if (IOTCON_ERROR_NONE != ret) {
-                               LOG_ERR("iotcon_query_create failed = %d", ret);
+                               LOG_ERR("iotcon_representation_create() Fail(%d)", ret);
                                return IOTCON_FUNC_CONTINUE;
                        }
 
+                       ret = iotcon_attributes_create(&attributes);
+                       if (IOTCON_ERROR_NONE != ret) {
+                               LOG_ERR("iotcon_attributes_create() Fail(%d)", ret);
+                               iotcon_representation_destroy(repr);
+                               return;
+                       }
+
                        LOG_DEBUG("CMD = %s", command2string(cmd->command));
 
                        if (cmd->command == COMP_REQ_CREATE_GROUP) { //request create group
-                               iotcon_query_add(query, "CMD", "1");
-                               iotcon_query_add(query, "name", cmd->arg1);
+                               iotcon_attributes_add_str(attributes, "CMD", "1");
+                               iotcon_attributes_add_str(attributes, "name", cmd->arg1);
                        } else if (cmd->command == COMP_REQ_INVITE_DEVICE) { //request invite
-                               iotcon_query_add(query, "CMD", "2");
-                               iotcon_query_add(query, "name", cmd->arg1);
-                               iotcon_query_add(query, "id", cmd->arg2);
-                               iotcon_query_add(query, "PIN", cmd->arg3);
+                               iotcon_attributes_add_str(attributes, "CMD", "2");
+                               iotcon_attributes_add_str(attributes, "name", cmd->arg1);
+                               iotcon_attributes_add_str(attributes, "id", cmd->arg2);
+                               iotcon_attributes_add_str(attributes, "PIN", cmd->arg3);
                        } else if (cmd->command == COMP_REQ_EJECT_DEVICE) { //request eject
-                               iotcon_query_add(query, "CMD", "3");
-                               iotcon_query_add(query, "name", cmd->arg1);
-                               iotcon_query_add(query, "id", cmd->arg2);
+                               iotcon_attributes_add_str(attributes, "CMD", "3");
+                               iotcon_attributes_add_str(attributes, "name", cmd->arg1);
+                               iotcon_attributes_add_str(attributes, "id", cmd->arg2);
                        } else if (cmd->command == COMP_REQ_DELETE_GROUP) { //request delete group
-                               iotcon_query_add(query, "CMD", "4");
-                               iotcon_query_add(query, "name", cmd->arg1);
+                               iotcon_attributes_add_str(attributes, "CMD", "4");
+                               iotcon_attributes_add_str(attributes, "name", cmd->arg1);
                        } else { /* Send Data */
-                               iotcon_query_add(query, "CMD", "5");
+                               iotcon_attributes_add_str(attributes, "CMD", "5");
 #ifdef SUPPORT_BASE64_ENCODING
                                uint32_t outSize = 0;
                                size_t b64BufSize = B64ENCODE_OUT_SAFESIZE((cmd->arg1_len + 1));
@@ -518,24 +540,38 @@ static bool _found_resource(iotcon_remote_resource_h resource,
                                        _clear_user_data(cmd);
                                        return IOTCON_FUNC_CONTINUE;
                                }
-                               b64Encode((const char *)cmd->arg1, cmd->arg1_len, b64Buf, b64BufSize, &outSize);
+                               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);
+                               iotcon_attributes_add_str(attributes, "data", b64Buf);
+                               LOG_DEBUG("b64BufSize =%d outSize = %d b64Buf = %s", b64BufSize,
+                                                 outSize, b64Buf);
                                g_free(b64Buf);
 #else
-                               iotcon_query_add(query, "data", cmd->arg1);
+                               iotcon_attributes_add_str(attributes, "data", cmd->arg1);
 #endif
                        }
 
-                       ret = iotcon_remote_resource_get(resource_clone, query, _on_get, cmd);
+                       ret = iotcon_representation_set_attributes(repr, attributes);
+                       if (IOTCON_ERROR_NONE != ret) {
+                               LOG_ERR("iotcon_representation_set_attributes() Fail(%d)", ret);
+                               iotcon_attributes_destroy(attributes);
+                               iotcon_representation_destroy(repr);
+                               return IOTCON_FUNC_CONTINUE;
+                       }
+
+                       iotcon_attributes_destroy(attributes);
+
+                       ret = iotcon_remote_resource_post(resource_clone, repr, NULL,
+                                                                                        _on_get, NULL);
                        if (IOTCON_ERROR_NONE != ret) {
+                               LOG_ERR("iotcon_remote_resource_put() Fail(%d)", ret);
                                 iotcon_remote_resource_destroy(resource_clone);
                                 _clear_user_data(cmd);
                                 return IOTCON_FUNC_CONTINUE;
                        }
 
-                       iotcon_query_destroy(query);
+                       iotcon_representation_destroy(repr);
                }
 
        }