comp-manager: Code cleanup when adding new resource
authorSaurav Babu <saurav.babu@samsung.com>
Wed, 21 Mar 2018 15:14:57 +0000 (20:44 +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_resource.h
src/companion-manager/src/comp_iot.cpp
src/companion-manager/src/comp_resource.c

index 84f0ee3..27de9cb 100644 (file)
@@ -34,11 +34,10 @@ typedef struct {
 } comp_rd_t;
 
 typedef struct {
-       iotcon_resource_h resource;
        char *uri;
 } comp_re_t;
 
-int comp_resource_append(comp_resource_type_e resource_type, char *uri, iotcon_resource_h resource);
+int comp_resource_append(comp_resource_type_e resource_type, const char *uri);
 char *comp_resource_get_type(comp_resource_type_e resource_type);
 char *comp_resource_get_uri_prefix(comp_resource_type_e resource_type);
 uint8_t comp_resource_get_policies(comp_resource_type_e resource_type);
index a1feb23..7ccc4e2 100644 (file)
@@ -434,22 +434,18 @@ OCEntityHandlerResult _request_handler(std::shared_ptr<OCResourceRequest> reques
 
 int comp_iot_add_resource(comp_resource_type_e resource_type, const char *uri)
 {
-       //char uri_path[PATH_MAX] = {0,};
        OCResourceHandle resourceHandle;
        std::string uri_path;
 
-       uri_path =  std::string(comp_resource_get_uri_prefix(resource_type)) + std::string(uri);
-       //strncpy(uri_path, comp_resource_get_uri_prefix(resource_type), PATH_MAX);
-       //strncat(uri_path, uri, PATH_MAX);
+       uri_path =  std::string(comp_resource_get_uri_prefix(resource_type)) +
+                               std::string(uri);
 
        LOG_DEBUG("[ADD] resource uri is %s", uri_path.c_str());
 
-       EntityHandler cb = std::bind(&_request_handler, std::placeholders::_1);
-
        OCStackResult result = OCPlatform::registerResource(resourceHandle,
                                                        uri_path,
                                                        std::string(comp_resource_get_type(resource_type)),
-                                                       std::string(DEFAULT_INTERFACE), cb,
+                                                       std::string(DEFAULT_INTERFACE), &_request_handler,
                                                        comp_resource_get_policies(resource_type));
        if (result != OC_STACK_OK) {
                LOG_ERR("Failed to create resource");
@@ -457,67 +453,11 @@ int comp_iot_add_resource(comp_resource_type_e resource_type, const char *uri)
        }
 
        LOG_DEBUG("Successfully created resource");
-       return COMP_ERROR_NONE;
 
-#if 0
-       iotcon_resource_h resource = NULL;
-       iotcon_resource_types_h resource_types = NULL;
-       iotcon_resource_interfaces_h resource_ifaces = NULL;
-       char uri_path[PATH_MAX] = {0,};
-
-       int ret = iotcon_resource_types_create(&resource_types);
-       if (IOTCON_ERROR_NONE != ret) {
-               LOG_ERR("iotcon_resource_types_create failed! : %s", get_error_message(ret));
-               goto EXIT;
-       }
-
-       ret = iotcon_resource_types_add(resource_types, comp_resource_get_type(resource_type));
-       if (IOTCON_ERROR_NONE != ret) {
-               LOG_ERR("iotcon_resource_types_add failed! : %s", get_error_message(ret));
-               goto EXIT;
-       }
-
-       ret = iotcon_resource_interfaces_create(&resource_ifaces);
-       if (IOTCON_ERROR_NONE != ret) {
-               LOG_ERR("iotcon_resource_interfaces_create failed! : %s", get_error_message(ret));
-               goto EXIT;
-       }
-
-       ret = iotcon_resource_interfaces_add(resource_ifaces, IOTCON_INTERFACE_DEFAULT);
-       if (IOTCON_ERROR_NONE != ret) {
-               LOG_ERR("iotcon_resource_interfaces_add failed! : %s", get_error_message(ret));
-               goto EXIT;
-       }
-
-       strncpy(uri_path, comp_resource_get_uri_prefix(resource_type), PATH_MAX);
-       strncat(uri_path, uri, PATH_MAX);
-
-       LOG_DEBUG("[ADD] resource uri is %s", uri_path);
-       //name duplication check
-
-       ret = iotcon_resource_create(uri_path, resource_types, resource_ifaces,
-                                       comp_resource_get_policies(resource_type), _request_handler, NULL, &resource);
-       if (IOTCON_ERROR_NONE != ret) {
-               LOG_ERR("iotcon_resource_create failed! : %s", get_error_message(ret));
-               goto EXIT;
-       }
-
-       //get resource list of resource type
-       ret = comp_resource_append(resource_type, uri, resource);
-       if (COMP_ERROR_NONE != ret) {
-               LOG_ERR("comp_resource_append failed! : %s", comp_log_get_error_string(ret));
-               goto EXIT;
-       }
-
-EXIT:
-       if (resource_types != NULL)
-               iotcon_resource_types_destroy(resource_types);
-
-       if (resource_ifaces != NULL)
-               iotcon_resource_interfaces_destroy(resource_ifaces);
+       //Append to resource list
+       comp_resource_append(resource_type, uri_path.c_str());
 
        return COMP_ERROR_NONE;
-#endif
 }
 
 static bool _get_res_type_cb(const char *string, void *user_data)
index 1d55294..171a2f7 100644 (file)
@@ -29,14 +29,13 @@ static comp_rd_t comp_rds[] = {
                IOTCON_RESOURCE_DISCOVERABLE | IOTCON_RESOURCE_OBSERVABLE | IOTCON_RESOURCE_SECURE},
 };
 
-int comp_resource_append(comp_resource_type_e resource_type, char *uri, iotcon_resource_h resource)
+int comp_resource_append(comp_resource_type_e resource_type, const char *uri)
 {
        comp_context_t *comp_ctx = comp_context_get_context();
        GList *list = comp_ctx->resource_list[resource_type];
 
        //append resource handle / name to list
        comp_re_t *element = g_new0(comp_re_t, 1);
-       element->resource = resource;
        element->uri = g_strdup(uri);
        list = g_list_append(list, element);
 
@@ -45,26 +44,6 @@ int comp_resource_append(comp_resource_type_e resource_type, char *uri, iotcon_r
        return 0;
 }
 
-int comp_resource_get_resource_from_uri(comp_resource_type_e resource_type,
-       char *uri, iotcon_resource_h *parent_resource)
-{
-       comp_context_t *comp_ctx = comp_context_get_context();
-       GList *list = comp_ctx->resource_list[resource_type];
-
-       GList *l;
-       for (l = list; l != NULL; l = l->next) {
-               comp_re_t *re = (comp_re_t *)l->data;
-
-               if (strcmp(re->uri, uri) == 0) {
-                       LOG_DEBUG("Resource found - type : %d uri : %s", resource_type, uri);
-                       *parent_resource = re->resource;
-                       return 0;
-               }
-       }
-
-       return -1;
-}
-
 char *comp_resource_get_type(comp_resource_type_e resource_type)
 {
        return comp_rds[resource_type].type;
@@ -83,7 +62,7 @@ uint8_t comp_resource_get_policies(comp_resource_type_e resource_type)
 void __print_foreach_resource(gpointer data, gpointer user_data)
 {
        comp_re_t *re = (comp_re_t *)data;
-       LOG_DEBUG("    ** resource : %x, uri : %s", re->resource, re->uri);
+       LOG_DEBUG("    ** uri : %s", re->uri);
 }
 
 void comp_resource_print_list()
@@ -93,8 +72,10 @@ void comp_resource_print_list()
        LOG_DEBUG("-------------------Print resource list-----------------------");
 
        for (int i = 0; i < COMP_RESOURCE_TYPE_MAX; i++) {
-               LOG_DEBUG("Resource type %d list length : %d", i, g_list_length(comp_ctx->resource_list[i]));
-               g_list_foreach(comp_ctx->resource_list[i], __print_foreach_resource, NULL);
+               LOG_DEBUG("Resource type %d list length : %d", i,
+                                 g_list_length(comp_ctx->resource_list[i]));
+               g_list_foreach(comp_ctx->resource_list[i], __print_foreach_resource,
+                                          NULL);
        }
 
        LOG_DEBUG("-------------------Print resource list end-------------------");