From 1e05ee5d75ced286e797fcbe7e315e8d10303304 Mon Sep 17 00:00:00 2001 From: Saurav Babu Date: Wed, 21 Mar 2018 20:44:57 +0530 Subject: [PATCH] comp-manager: Code cleanup when adding new resource Signed-off-by: Saurav Babu --- src/companion-manager/include/comp_resource.h | 3 +- src/companion-manager/src/comp_iot.cpp | 70 ++------------------------- src/companion-manager/src/comp_resource.c | 31 +++--------- 3 files changed, 12 insertions(+), 92 deletions(-) diff --git a/src/companion-manager/include/comp_resource.h b/src/companion-manager/include/comp_resource.h index 84f0ee3..27de9cb 100644 --- a/src/companion-manager/include/comp_resource.h +++ b/src/companion-manager/include/comp_resource.h @@ -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); diff --git a/src/companion-manager/src/comp_iot.cpp b/src/companion-manager/src/comp_iot.cpp index a1feb23..7ccc4e2 100644 --- a/src/companion-manager/src/comp_iot.cpp +++ b/src/companion-manager/src/comp_iot.cpp @@ -434,22 +434,18 @@ OCEntityHandlerResult _request_handler(std::shared_ptr 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) diff --git a/src/companion-manager/src/comp_resource.c b/src/companion-manager/src/comp_resource.c index 1d55294..171a2f7 100644 --- a/src/companion-manager/src/comp_resource.c +++ b/src/companion-manager/src/comp_resource.c @@ -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-------------------"); -- 2.7.4