From: Chanwoo Choi Date: Tue, 6 Jul 2021 09:21:49 +0000 (+0900) Subject: halapi: Add new helper functions to get multiple library names X-Git-Tag: submit/tizen/20210713.080137^0 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=6f8de361cb40d57e8d3076177ba928f92f041981;p=platform%2Fhal%2Fapi%2Fcommon.git halapi: Add new helper functions to get multiple library names In order to get the multiple library namse on device, add new helper function as following. Firstly, get the number of HAL backend libraries hal_common_get_backend_count() and the get the library names by hal_common_get_backend_library_names(). - Get the number of the backend libraries according to the type of HAL module int hal_common_get_backend_count(enum hal_module module); - Get the backend library names according to the type of HAL module int hal_common_get_backend_library_names(enum hal_module module, char **library_names, int library_count, int library_name_size); Change-Id: If6ecc2f550693768e6e63a572dd99c791984e596 Signed-off-by: Chanwoo Choi --- diff --git a/include/hal-common.h b/include/hal-common.h index 7ec6c1f..1b0e328 100644 --- a/include/hal-common.h +++ b/include/hal-common.h @@ -228,6 +228,26 @@ int hal_common_get_backend_name(enum hal_module module, char *name, int size); */ int hal_common_get_backend_vendor(enum hal_module module, char *vendor, int size); +/** + * @brief Get the number of the backend libraries according to the type of HAL module + * @param[in] HAL module id among enum hal_moudle + * @return @c 0 on success, otherwise a negative error value + */ +int hal_common_get_backend_count(enum hal_module module); + +/** + * @brief Get the backend library names according to the type of HAL module + * @param[in] HAL module id among enum hal_moudle + * @param[out] Data pointer should be filled by backend library names + * @param[in] Number of backend library of specific HAL module + * @param[in] Maximum length of the library name + * @return @c 0 on success, otherwise a negative error value + */ +int hal_common_get_backend_library_names(enum hal_module module, + char **library_names, + int library_count, + int library_name_size); + #ifdef __cplusplus } #endif diff --git a/src/hal-api-common.c b/src/hal-api-common.c index 8f17557..5dbdf58 100644 --- a/src/hal-api-common.c +++ b/src/hal-api-common.c @@ -21,6 +21,7 @@ #include #include #include +#include #define _GNU_SOURCE #include @@ -598,3 +599,117 @@ int hal_common_get_backend_vendor(enum hal_module module, char *vendor, int size { return __get_backend_data(module, NULL, NULL, 0, vendor, size); } + + +static int __get_backend_library_data(enum hal_module module, + char **lib_names, + int lib_count, + int lib_name_size) +{ + struct __hal_module_info *info = NULL; + struct dirent *de; + DIR *dir; + const char *backend_module_name = NULL; + int count, i, ret, len; +#if defined(__aarch64__) + const char hal_backend_path[] = "/hal/lib64"; +#else + const char hal_backend_path[] = "/hal/lib"; +#endif + + /* Check parameter whether is valid or not */ + if (module <= HAL_MODULE_UNKNOWN || module >= HAL_MODULE_END) { + _E("Invalid parameter of HAL module (%d)\n", module); + return TIZEN_ERROR_INVALID_PARAMETER; + } + + if (_hal_api_conf_init()) + return TIZEN_ERROR_UNKNOWN; + + info = _hal_api_conf_get_module_info(module, NULL); + if (info == NULL) { + _E("Failed to get HAL module(%d) information\n", module); + ret = TIZEN_ERROR_INVALID_PARAMETER; + goto err; + } + + if (info->backend_module_name == NULL) { + _E("Don't support HAL backend of HAL module(%s)\n", + info->module_name); + ret = TIZEN_ERROR_INVALID_PARAMETER; + goto err; + } + backend_module_name = g_strdup_printf("libhal-backend-%s", + info->backend_module_name); + if (!backend_module_name) { + _E("Failed to allocate the backend_module_name of HAL module(%s)\n", + info->module_name); + ret = TIZEN_ERROR_UNKNOWN; + goto err; + } + + /* Find HAL backend libraries */ + dir = opendir(hal_backend_path); + if (!dir) { + _E("Failed to find HAL backend path(%s) for HAL module(%s)\n", + hal_backend_path, info->module_name); + ret = TIZEN_ERROR_UNKNOWN; + goto err_free_backend_module_name; + } + + count = 0; + while ((de = readdir(dir)) != NULL) { + if (!g_str_has_prefix(de->d_name, backend_module_name)) + continue; + + if (lib_count == 0) + count++; + else if (lib_count > 0) { + len = strlen(de->d_name) + 1; + + if (len > lib_name_size) + len = lib_name_size; + + strncpy(lib_names[count++], de->d_name, len); + } + } + + if (lib_count > 0 && count != lib_count) { + ret = TIZEN_ERROR_INVALID_PARAMETER; + goto err_mismatch_count; + } + + closedir(dir); + _hal_api_conf_exit(); + g_free(backend_module_name); + + return count; + +err_mismatch_count: + for (i = count - 1; i >= 0; i--) + memset(lib_names[i], 0, strlen(lib_names[i])); + + closedir(dir); +err_free_backend_module_name: + g_free(backend_module_name); +err: + _hal_api_conf_exit(); + return ret; +} + +EXPORT +int hal_common_get_backend_count(enum hal_module module) +{ + return __get_backend_library_data(module, NULL, 0, 0); +} + +EXPORT +int hal_common_get_backend_library_names(enum hal_module module, + char **library_names, + int library_count, + int library_name_size) +{ + int ret = __get_backend_library_data(module, library_names, + library_count, library_name_size); + return (ret < 0) ? ret : 0; +}