comp-manager: Added Send Data with Post method using IoTivity C++ stack
authorSaurav Babu <saurav.babu@samsung.com>
Thu, 22 Mar 2018 10:12:33 +0000 (15:42 +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/src/comp_iot.cpp

index c91376b..05940a6 100644 (file)
@@ -534,6 +534,7 @@ static void _clear_user_data(void *user_data)
        }
 }
 
+#if 0
 static void _on_get(iotcon_remote_resource_h resource, iotcon_error_e err,
         iotcon_request_type_e request_type, iotcon_response_h response, void *user_data)
 {
@@ -562,8 +563,28 @@ static void _on_get(iotcon_remote_resource_h resource, iotcon_error_e err,
 
        _clear_user_data(user_data);
 }
+#endif
+
+static void _on_post(const HeaderOptions& /*headerOptions*/,
+                                        const OCRepresentation& rep, const int eCode)
+{
+       last_get_result = eCode;
+       int ret;
+       try {
+               if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED ||
+                       eCode == OC_STACK_RESOURCE_CHANGED) {
+                       LOG_DEBUG("Post request is successful");
+               } else {
+                       LOG_ERR("_on_post Response error %d", eCode);
+               }
+       } catch(std::exception& e) {
+               LOG_ERR("Exception %s in on post", e.what());
+       }
+
+}
 
-static bool _found_resource(std::shared_ptr<OCResource> resource)
+static bool _found_resource(std::shared_ptr<OCResource> resource,
+                                                       void *user_data)
 {
        int ret;
        char *resource_uri_path;
@@ -573,17 +594,24 @@ static bool _found_resource(std::shared_ptr<OCResource> resource)
        comp_group_type_e group_type;
        std::string resourceURI;
        std::string hostAddress;
-       static int resourceCount = 0;
 
        LOG_DEBUG("Found Resource");
        try {
                if(resource) {
-                       resource_uri_path = g_strdup(resource->uri().c_str());
                        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());
 
                        LOG_DEBUG("Resource URI : %s", resource_uri_path);
                        LOG_DEBUG("Host Address : %s", resource_host);
+
                        LOG_DEBUG("Resource Device ID : %s", resource_device_id);
 
                        for(auto &resourceTypes : resource->getResourceTypes()) {
@@ -609,17 +637,68 @@ static bool _found_resource(std::shared_ptr<OCResource> resource)
                                found_group_count++;
 
                                g_free(temp);
-                               g_free(resource_uri_path);
-                               g_free(resource_host);
-                               g_free(resource_type);
-                               g_free(resource_device_id);
 
-                               return IOTCON_FUNC_CONTINUE;
                        } else if (g_strcmp0(resource_type, "core.comp.data") == 0) {
+                               comp_command_t *cmd = (comp_command_t *)user_data;
+
+                               LOG_DEBUG("Resource Type %s", resource_type);
+                               LOG_DEBUG("Command Device ID: %s", cmd ? cmd->uuid : "NULL");
+
+                               if (cmd != NULL && strcmp(cmd->uuid, resource_device_id) == 0) {
+                                       OCRepresentation rep;
+
+                                       LOG_DEBUG("Request to Remote Device");
+
+                                       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);
+                                       } 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);
+                                       } else if (cmd->command == COMP_REQ_EJECT_DEVICE) { //request eject
+                                               rep.setValue("CMD", "3");
+                                               rep.setValue("name", cmd->arg1);
+                                               rep.setValue("id", cmd->arg2);
+                                       } else if (cmd->command == COMP_REQ_DELETE_GROUP) { //request delete group
+                                               rep.setValue("CMD", "4");
+                                               rep.setValue("name", cmd->arg1);
+                                       } else { /* Send Data */
+                                               rep.setValue("CMD", "5");
+#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);
+                                               LOG_DEBUG("b64BufSize =%d outSize = %d b64Buf = %s",
+                                                                 b64BufSize, outSize, b64Buf);
+                                               g_free(b64Buf);
+#else
+                                               rep.setValue("data", cmd->arg1);
+#endif
+                                       }
+                                       resource->post(rep, QueryParamsMap(), _on_post);
+                               }
                        }
+
+                       g_free(resource_uri_path);
+                       g_free(resource_host);
+                       g_free(resource_type);
+                       g_free(resource_device_id);
                }
-               else
-               {
+               else {
                        LOG_ERR("Resource is invalid");
                }
        }
@@ -843,10 +922,20 @@ int comp_iot_discovery_resource(comp_resource_type_e resource_type, int timeout,
                requestURI << OC_RSRVD_WELL_KNOWN_URI << "?rt=" <<
                        comp_resource_get_type(resource_type);
 
+               auto found_cb = std::bind (&_found_resource, std::placeholders::_1,
+                                                                  cmd);
                LOG_DEBUG("Querying for Resource");
-               ret = OCPlatform::findResource("", requestURI.str(),
-                                                                          CT_ADAPTER_IP,
-                                                                          &_found_resource);
+               LOG_DEBUG("[Saurav] host address  %s", cmd->host ? cmd->host : "NULL");
+               if (cmd) {
+                       if (cmd->host)
+                               ret = OCPlatform::findResource(cmd->host, requestURI.str(),
+                                                                                          CT_ADAPTER_IP,
+                                                                                          found_cb);
+                       else
+                               ret = OCPlatform::findResource("", requestURI.str(),
+                                                                                          CT_ADAPTER_IP,
+                                                                                          found_cb);
+               }
        } catch (OCException &e) {
                LOG_ERR("Failed to find resource %s", e.reason());
                ret = e.code();