comp-manager: Find Resources using IoTivity C++ Stack
authorSaurav Babu <saurav.babu@samsung.com>
Wed, 21 Mar 2018 14:43:02 +0000 (20:13 +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/src/comp_gdbus_group.c
src/companion-manager/src/comp_iot.cpp

index 5431397..0228db3 100644 (file)
@@ -110,7 +110,7 @@ void notify_group_device_eject_result(int ret);
 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(char *resp_data, int ret);
+void notify_send_data_finish(const char *resp_data, int ret);
 void notify_request_result(char *cmd, char *arg, int ret);
 
 #ifdef __cplusplus
index c9fb5cf..9dcf55d 100644 (file)
@@ -396,7 +396,7 @@ void notify_group_join(int result)
        group_emit_join_result(group_dbus_get_object(), result);
 }
 
-void notify_send_data_finish(char *resp_data, int ret)
+void notify_send_data_finish(const char *resp_data, int ret)
 {
        group_emit_send_data_finish(group_dbus_get_object(), resp_data, ret);
 }
index a37142b..a1feb23 100644 (file)
@@ -165,7 +165,7 @@ int comp_iot_initialize()
 
        PlatformConfig cfg {
                OC::ServiceType::InProc,
-                       OC::ModeType::Server,
+                       OC::ModeType::Both,
                        (OCTransportAdapter)(OCTransportAdapter::OC_ADAPTER_IP|OCTransportAdapter::OC_ADAPTER_TCP),
                        OC::QualityOfService::LowQos,
                        &ps
@@ -599,10 +599,74 @@ static void _on_get(iotcon_remote_resource_h resource, iotcon_error_e err,
        _clear_user_data(user_data);
 }
 
-static bool _found_resource(iotcon_remote_resource_h resource,
-                                               iotcon_error_e result, void *user_data)
+static bool _found_resource(std::shared_ptr<OCResource> resource)
 {
        int ret;
+       char *resource_uri_path;
+       char *resource_device_id = NULL;
+       char *resource_host;
+       char *resource_type = NULL;
+       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());
+                       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()) {
+                               resource_type = g_strdup(resourceTypes.c_str());
+                               LOG_DEBUG("Resource Type : %s", resource_type);
+                       }
+
+                       if (g_strcmp0(resource_type, "core.comp.group") == 0) {
+                               comp_context_t *comp_ctx = comp_context_get_context();
+                               if (g_strcmp0(resource_device_id, comp_ctx->device_uuid) == 0)
+                                       group_type = COMP_GROUP_DEVICE;
+                               else
+                                       group_type = COMP_GROUP_REMOTE_DEVICE;
+
+                               char *temp = g_strdup(resource_uri_path);
+                               char *ptr = strtok(temp, "/");
+                               ptr = strtok(NULL, "/");
+                               ptr = strtok(NULL, "/");
+
+                               LOG_DEBUG("group name is %s", ptr);
+                               comp_group_add_new(resource_uri_path, resource_device_id, ptr,
+                                                                  resource_host, resource_type, group_type);
+                               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) {
+                       }
+               }
+               else
+               {
+                       LOG_ERR("Resource is invalid");
+               }
+       }
+       catch(std::exception& e)
+       {
+               LOG_ERR("Exception in foundResource: %s", e.what());
+       }
+
+       return IOTCON_FUNC_CONTINUE;
+#if 0
+       int ret;
        char *resource_uri_path = NULL;
        iotcon_resource_types_h resource_types;
        char *resource_device_id;
@@ -770,6 +834,7 @@ static bool _found_resource(iotcon_remote_resource_h resource,
        }
 
        return IOTCON_FUNC_CONTINUE;
+#endif
 }
 
 static gboolean _timeout_cb(gpointer data)
@@ -792,14 +857,30 @@ static gboolean _timeout_cb(gpointer data)
        return false;
 }
 
-int comp_iot_discovery_resource(comp_resource_type_e resource_type, int timeout, void *user_data)
+int comp_iot_discovery_resource(comp_resource_type_e resource_type, int timeout,
+                                                               void *user_data)
 {
        int ret;
        iotcon_query_h query;
        comp_command_t *cmd = (comp_command_t *) user_data;
+       std::ostringstream requestURI;
 
        found_group_count = 0;
 
+       try {
+               requestURI << OC_RSRVD_WELL_KNOWN_URI << "?rt=" <<
+                       comp_resource_get_type(resource_type);
+
+               LOG_DEBUG("Querying for Resource");
+               ret = OCPlatform::findResource("", requestURI.str(),
+                                                                          CT_ADAPTER_IP,
+                                                                          &_found_resource);
+       } catch (OCException &e) {
+               LOG_ERR("Failed to find resource %s", e.reason());
+               ret = e.code();
+       }
+
+#if 0
        ret = iotcon_set_timeout(timeout);
        if (IOTCON_ERROR_NONE != ret) {
                LOG_ERR("Failed to set timeout value");
@@ -837,6 +918,7 @@ int comp_iot_discovery_resource(comp_resource_type_e resource_type, int timeout,
                ret = iotcon_find_resource(cmd->host,
                        IOTCON_CONNECTIVITY_IP | IOTCON_CONNECTIVITY_PREFER_UDP,
                        query, _found_resource, user_data);
+#endif
 
        cmd->resource_type = resource_type;
        cmd->tid = g_timeout_add_seconds(timeout + 1, _timeout_cb, cmd);