comp-manager: Handle Post request from client using IoTivity C++ Stack
authorSaurav Babu <saurav.babu@samsung.com>
Fri, 23 Mar 2018 03:35:11 +0000 (09:05 +0530)
committersaerome.kim <saerome.kim@samsung.com>
Mon, 2 Jul 2018 10:38:49 +0000 (19:38 +0900)
Signed-off-by: Saurav Babu <saurav.babu@samsung.com>
src/companion-manager/include/comp_gdbus_group.h
src/companion-manager/include/comp_group.h
src/companion-manager/src/comp_gdbus_group.c
src/companion-manager/src/comp_group.c
src/companion-manager/src/comp_iot.cpp

index 0228db3..cdb8f8e 100644 (file)
@@ -111,7 +111,7 @@ void notify_mowned_device_found(int device_count, GVariant *device_data);
 void notify_mowned_device_find_finish(int ret);
 void notify_group_join(int result);
 void notify_send_data_finish(const char *resp_data, int ret);
-void notify_request_result(char *cmd, char *arg, int ret);
+void notify_request_result(const char *cmd, char *arg, int ret);
 
 #ifdef __cplusplus
 }
index 06a7b24..3e2c6ed 100644 (file)
@@ -68,7 +68,7 @@ typedef struct {
 int comp_group_initialize();
 
 /* create group and destroy */
-int comp_group_create(char* name);
+int comp_group_create(const char* name);
 int comp_group_destroy(comp_group_t *handle);
 
 /* Find Remote groups */
@@ -88,13 +88,14 @@ int comp_group_find_mot_enabled_devices(int timeout);
 char *comp_group_invite_get_uuid();
 char *comp_group_invite_get_pin();
 //Join to device in group (async)
-int comp_group_invite(char *group_name, char *uuid, char *pin);
+int comp_group_invite(const char *group_name, const char *uuid,
+                                         const char *pin);
 comp_group_invite_info_t *comp_group_get_invite_info();
 void comp_group_free_invite_info();
 void comp_group_notify_group_invite(int result);
 
 //dismiss from group (async)
-int comp_group_dismiss(gchar *uuid_dev1, gchar *uuid_dev2);
+int comp_group_dismiss(gchar *uuid_dev1, const char *uuid_dev2);
 void comp_group_notify_group_dismiss(int result);
 
 int comp_group_pair_resource(char* target1, char *subject1, char *uri1,
index 9dcf55d..30da137 100644 (file)
@@ -401,7 +401,7 @@ void notify_send_data_finish(const char *resp_data, int ret)
        group_emit_send_data_finish(group_dbus_get_object(), resp_data, ret);
 }
 
-void notify_request_result(char *cmd, char *arg, int ret)
+void notify_request_result(const char *cmd, char *arg, int ret)
 {
        group_emit_request_result(group_dbus_get_object(), cmd, arg, ret);
 }
index 18dc9f8..f84f358 100644 (file)
@@ -64,7 +64,7 @@ void __print_groups_information()
 }
 
 /* create group and destroy */
-int comp_group_create(char* name)
+int comp_group_create(const char* name)
 {
        int ret;
 
@@ -492,7 +492,7 @@ int comp_group_add_device_in_group(char *group_name, char *uuid)
 }
 
 //Join to device in group (async)
-int comp_group_invite(char *group_name, char *uuid, char *pin)
+int comp_group_invite(const char *group_name, const char *uuid, const char *pin)
 {
        int ret = COMP_ERROR_NONE;
 
@@ -583,7 +583,7 @@ int comp_group_remove_device_in_group(char *uuid)
 }
 
 //dismiss from group (async)
-int comp_group_dismiss(gchar *uuid_dev1, gchar *uuid_dev2)
+int comp_group_dismiss(gchar *uuid_dev1, const char *uuid_dev2)
 {
        int ret;
 
index 05940a6..9273247 100644 (file)
@@ -192,7 +192,7 @@ int comp_iot_initialize()
        PlatformConfig cfg {
                OC::ServiceType::InProc,
                        OC::ModeType::Both,
-                       (OCTransportAdapter)(OCTransportAdapter::OC_ADAPTER_IP|OCTransportAdapter::OC_ADAPTER_TCP),
+                       (OCTransportAdapter)(OCTransportAdapter::OC_ADAPTER_IP),
                        OC::QualityOfService::LowQos,
                        &ps
        };
@@ -284,6 +284,17 @@ int comp_iot_initialize()
        return COMP_ERROR_NONE;
 }
 
+static void _print_oc_representation(OCRepresentation rep)
+{
+       /* attribute.type() is an enum class AttributeType defined in
+        * AttributeValue.h header file of IoTivity */
+       for (auto& attribute : rep) {
+               LOG_DEBUG("%s of type %d with value %s", attribute.attrname().c_str(),
+                                 attribute.type(),
+                                 attribute.getValueToString().c_str());
+       }
+}
+
 OCEntityHandlerResult _request_handler(std::shared_ptr<OCResourceRequest> request)
 {
        LOG_DEBUG("_request_handler is called");
@@ -293,6 +304,141 @@ OCEntityHandlerResult _request_handler(std::shared_ptr<OCResourceRequest> reques
                int requestFlag = request->getRequestHandlerFlag();
 
                LOG_DEBUG("request type %s flag %x", requestType.c_str(), requestFlag);
+
+               if (requestType != "POST") {
+                       LOG_ERR("Invalid Request Type");
+                       return OC_EH_OK;
+               }
+
+               if(requestFlag & RequestHandlerFlag::RequestFlag) {
+                       LOG_DEBUG("requestFlag : Request");
+                       auto pResponse = std::make_shared<OC::OCResourceResponse>();
+                       char *arg = NULL;
+                       int result;
+                       int cmd;
+                       std::string group_name, uuid, data;
+                       comp_context_t *comp_ctx;
+
+                       try {
+                               OCRepresentation rep = request->getResourceRepresentation();
+
+                               _print_oc_representation(rep);
+
+                               if (!rep.getValue("CMD", cmd)) {
+                                       LOG_ERR("CMD not found in representation");
+                               } else {
+                                       LOG_DEBUG("Command received %d", cmd);
+                               }
+                               switch(cmd) {
+                               case COMP_REQ_SEND_DATA:
+#ifdef SUPPORT_BASE64_ENCODING
+                                       int payload_len;
+
+                                       LOG_DEBUG("Receive Data");
+
+                                       if (!rep.getValue("data", data))
+                                               LOG_ERR("data not found");
+
+                                       /*
+                                        * 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.c_str()); /* This data may be cliped the last padding 1~2 bytes ('='/'==') */
+
+                                       LOG_DEBUG("data = %s payload_len = %d", data.c_str(),
+                                                         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.c_str(), 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
+                                       if (!rep.getValue("data", data))
+                                               LOG_ERR("data not found");
+
+                                       LOG_DEBUG("Receive Data = %s", data.c_str());
+                                       arg = g_strdup(data.c_str());
+#endif
+                                       break;
+                               case COMP_REQ_CREATE_GROUP:
+                                       LOG_DEBUG("Request create group");
+
+                                       if (rep.getValue("name", group_name)) {
+                                               LOG_DEBUG("group_name : %s", group_name.c_str());
+                                               result = comp_group_create(group_name.c_str());
+                                               arg = g_strdup(group_name.c_str());
+                                       }
+                                       break;
+                               case COMP_REQ_INVITE_DEVICE:
+                                       LOG_DEBUG("Request invite device");
+
+                                       if (!rep.getValue("name", group_name))
+                                               LOG_ERR("Group name not found");
+                                       if (!rep.getValue("id", uuid))
+                                               LOG_ERR("UUID not found");
+
+                                       LOG_DEBUG("group name : %s, UUID : %s", group_name.c_str(),
+                                                         uuid.c_str());
+
+                                       result = comp_group_invite(group_name.c_str(),
+                                                                  uuid.c_str(), "12341234");
+                                       arg = g_strdup(uuid.c_str());
+                                       break;
+                               case COMP_REQ_EJECT_DEVICE:
+                                       LOG_DEBUG("Request eject device");
+
+                                       comp_ctx = comp_context_get_context();
+
+                                       if (!rep.getValue("name", group_name))
+                                               LOG_ERR("Group name not found");
+                                       if (!rep.getValue("id", uuid))
+                                               LOG_ERR("UUID not found");
+
+                                       LOG_DEBUG("group name : %s Self UUID : %s Target UUID : %s",
+                                                         group_name.c_str(),comp_ctx->device_uuid,
+                                                         uuid.c_str());
+
+                                       result = comp_group_dismiss(comp_ctx->device_uuid,
+                                                                  uuid.c_str());
+                                       arg = g_strdup(uuid.c_str());
+                                       break;
+                               case COMP_REQ_DELETE_GROUP:
+                                       LOG_DEBUG("Request delete group");
+                                       arg = g_strdup("DELETED");
+                                       break;
+                               default:
+                                       LOG_ERR("Unknown request command %d", cmd);
+                                       break;
+                               }
+
+                               notify_request_result(command2string((comp_request_type_e) cmd),
+                                                                         arg, result);
+
+                               g_free(arg);
+
+                               pResponse->setErrorCode(200);
+                               pResponse->setResponseResult(OC_EH_OK);
+                               OCPlatform::sendResponse(pResponse);
+                       } catch (std::exception& e) {
+                               LOG_ERR("Exceptioin occured %s", e.what());
+                       }
+               }
        }
 
        return OC_EH_OK;
@@ -600,12 +746,6 @@ static bool _found_resource(std::shared_ptr<OCResource> resource,
                if(resource) {
                        resource_host = g_strdup(resource->host().c_str());
 
-                       if (g_strstr_len(resource_host, strlen(resource_host), "tcp")) {
-                               LOG_DEBUG("Ignore Resource found for TCP host");
-                               g_free(resource_host);
-                               return IOTCON_FUNC_CONTINUE;
-                       }
-
                        resource_uri_path = g_strdup(resource->uri().c_str());
                        resource_device_id = g_strdup(resource->sid().c_str());
 
@@ -652,44 +792,43 @@ static bool _found_resource(std::shared_ptr<OCResource> resource,
                                        LOG_DEBUG("CMD = %s", command2string(cmd->command));
 
                                        if (cmd->command == COMP_REQ_CREATE_GROUP) { //request create group
-                                               rep.setValue("CMD", "1");
-                                               rep.setValue("name", cmd->arg1);
+                                               rep.setValue("CMD", COMP_REQ_CREATE_GROUP);
+                                               rep.setValue("name", std::string(cmd->arg1));
                                        } else if (cmd->command == COMP_REQ_INVITE_DEVICE) { //request invite
-                                               rep.setValue("CMD", "2");
-                                               rep.setValue("name", cmd->arg1);
-                                               rep.setValue("id", cmd->arg2);
-                                               rep.setValue("PIN", cmd->arg3);
+                                               rep.setValue("CMD", COMP_REQ_INVITE_DEVICE);
+                                               rep.setValue("name", std::string(cmd->arg1));
+                                               rep.setValue("id", std::string(cmd->arg2));
+                                               rep.setValue("PIN", std::string(cmd->arg3));
                                        } else if (cmd->command == COMP_REQ_EJECT_DEVICE) { //request eject
-                                               rep.setValue("CMD", "3");
-                                               rep.setValue("name", cmd->arg1);
-                                               rep.setValue("id", cmd->arg2);
+                                               rep.setValue("CMD", COMP_REQ_EJECT_DEVICE);
+                                               rep.setValue("name", std::string(cmd->arg1));
+                                               rep.setValue("id", std::string(cmd->arg2));
                                        } else if (cmd->command == COMP_REQ_DELETE_GROUP) { //request delete group
-                                               rep.setValue("CMD", "4");
-                                               rep.setValue("name", cmd->arg1);
+                                               rep.setValue("CMD", COMP_REQ_DELETE_GROUP);
+                                               rep.setValue("name", std::string(cmd->arg1));
                                        } else { /* Send Data */
-                                               rep.setValue("CMD", "5");
+                                               rep.setValue("CMD", COMP_REQ_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';
-                                               rep.setValue("data", b64Buf);
+                                               rep.setValue("data", std::string(b64Buf));
                                                LOG_DEBUG("b64BufSize =%d outSize = %d b64Buf = %s",
                                                                  b64BufSize, outSize, b64Buf);
                                                g_free(b64Buf);
 #else
-                                               rep.setValue("data", cmd->arg1);
+                                               rep.setValue("data", std::string(cmd->arg1));
 #endif
                                        }
-                                       resource->post(rep, QueryParamsMap(), _on_post);
+                                       resource->post(rep, QueryParamsMap(), &_on_post);
                                }
                        }
 
@@ -911,7 +1050,6 @@ int comp_iot_discovery_resource(comp_resource_type_e resource_type, int timeout,
        void *user_data)
 {
        int ret;
-       iotcon_query_h query;
        std::ostringstream requestURI;
        comp_command_t *cmd = (comp_command_t *) user_data;
        comp_check_null_ret_error("cmd", cmd, COMP_ERROR_INVALID_PARAMETER);
@@ -924,9 +1062,10 @@ int comp_iot_discovery_resource(comp_resource_type_e resource_type, int timeout,
 
                auto found_cb = std::bind (&_found_resource, std::placeholders::_1,
                                                                   cmd);
-               LOG_DEBUG("Querying for Resource");
-               LOG_DEBUG("[Saurav] host address  %s", cmd->host ? cmd->host : "NULL");
                if (cmd) {
+                       LOG_DEBUG("Querying for Resource host address  %s",
+                                         cmd->host ? cmd->host : "NULL");
+
                        if (cmd->host)
                                ret = OCPlatform::findResource(cmd->host, requestURI.str(),
                                                                                           CT_ADAPTER_IP,