comp-manager: Send data directly to host without discovery
authorSaurav Babu <saurav.babu@samsung.com>
Thu, 29 Mar 2018 10:47:04 +0000 (16:17 +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_iot.h
src/companion-manager/src/comp_group.c
src/companion-manager/src/comp_iot.cpp

index baa7a47..957d96f 100644 (file)
@@ -52,6 +52,8 @@ int comp_iot_initialize();
 int comp_iot_add_resource(comp_resource_type_e resource_type, const char *uri);
 int comp_iot_delete_resource(comp_resource_type_e resource_type);
 int comp_iot_discovery_resource(comp_resource_type_e resource_type, int timeout, void *user_data);
+int comp_iot_send_data(comp_resource_type_e resource_type, int timeout,
+                                          comp_command_t *cmd);
 int comp_iot_deinitialize();
 
 #ifdef __cplusplus
index 001829a..533dce9 100644 (file)
@@ -649,10 +649,12 @@ int comp_group_send_data(gchar *uuid_dev, gchar *addr, int port, gchar *data,
        cmd->command = COMP_REQ_SEND_DATA;
        cmd->uuid = g_strdup(uuid_dev);
 
+       /* IPv6 Address should be encoded for RFC6874  */
        if (strchr(ip, ':')) /* IPv6 Adress */
-               cmd->host = g_strdup_printf("coap://[%s]:%d", ip, port);
+               cmd->host = g_strdup_printf("coaps://[%s%s%s]:%d", ip, "%25",
+                                                                       percent + 1, port);
        else /* IPv4 Address */
-               cmd->host = g_strdup_printf("coap://%s:%d", ip, port);
+               cmd->host = g_strdup_printf("coaps://%s:%d", ip, port);
 
        cmd->arg1 = g_malloc0(len + 1);
        if (NULL == cmd->arg1) {
@@ -670,9 +672,9 @@ int comp_group_send_data(gchar *uuid_dev, gchar *addr, int port, gchar *data,
 
        LOG_DEBUG("UUID %s host %s", cmd->uuid, cmd->host);
 
-       ret = comp_iot_discovery_resource(COMP_RESOURCE_TYPE_DATA, 5, cmd);
+       ret = comp_iot_send_data(COMP_RESOURCE_TYPE_DATA, 3, cmd);
        if (ret != COMP_ERROR_NONE) {
-               LOG_ERR("Failed to discover resource : %s",
+               LOG_ERR("Failed to send data : %s",
                                comp_log_get_error_string(ret));
        }
 
index 9acbd68..71d617f 100644 (file)
@@ -744,6 +744,51 @@ int comp_iot_discovery_resource(comp_resource_type_e resource_type, int timeout,
        return ret;
 }
 
+int comp_iot_send_data(comp_resource_type_e resource_type, int timeout,
+                                          comp_command_t *cmd)
+{
+       int ret;
+       OCResource::Ptr resource;
+       std::string requestURI;
+       std::vector<std::string> ifaces = {DEFAULT_INTERFACE};
+       std::vector<std::string> resource_types;
+       resource_types = {comp_resource_get_type(resource_type)};
+
+       try {
+               requestURI = std::string(comp_resource_get_uri_prefix(resource_type)) +
+                                                       "1";
+
+               LOG_DEBUG("Request URI %s", requestURI.c_str());
+
+               resource = OCPlatform::constructResourceObject(std::string(cmd->host),
+                                                          requestURI, CT_ADAPTER_IP, false,
+                                                          resource_types, ifaces);
+               if (!resource) {
+                       LOG_ERR("Failed to construct resource");
+                       return -1;
+               }
+
+               OCRepresentation rep;
+               rep.setValue("CMD", COMP_REQ_SEND_DATA);
+               rep.setValue("data", std::string(cmd->arg1));
+
+               ret = resource->post(rep, QueryParamsMap(),
+                                                  std::bind (&_on_post, std::placeholders::_1,
+                                                  std::placeholders::_2, std::placeholders::_3, cmd));
+
+       } catch (OCException &e) {
+               LOG_ERR("Failed to send data %s", e.what());
+               ret = e.code();
+       }
+
+       cmd->resource_type = resource_type;
+       cmd->tid = g_timeout_add_seconds(timeout + 1, _timeout_cb, cmd);
+
+       LOG_DEBUG("Send Data : %s", get_error_message(ret));
+
+       return ret;
+}
+
 int comp_iot_delete_resource(comp_resource_type_e resource_type)
 {
        //delete resource from resource list using resource_type and index