#include <gio/gio.h>
#include <glib-object.h>
-#include <json-glib/json-glib.h>
#include "hal-common.h"
#include "hal-common-interface.h"
-#include "hal-api-conf.h"
-
#include "common.h"
-
-static enum hal_abi_version _platform_abi_version = HAL_ABI_VERSION_END;
-
-static JsonParser *_parser = NULL;
-static JsonObject *_root_object = NULL;
+#include "hal-api-conf.h"
+#include "hal-api-list.h"
static GHashTable *_module_hash = NULL;
-static bool _initialized = false;
-
-void __attribute__ ((destructor)) _destroy_configuration(void);
-
-void _destroy_configuration(void)
-{
- if (_parser)
- g_object_unref(_parser);
-
- if (_module_hash) {
- g_hash_table_remove_all(_module_hash);
- g_hash_table_unref(_module_hash);
- }
-
- _initialized = false;
-}
-
-static enum hal_abi_version __convert_abi_version_str_to_enum(const char *abi_version) {
- int version;
- for (version = HAL_ABI_VERSION_UNKNOWN + 1; version < HAL_ABI_VERSION_END; version++){
- if (g_strcmp0(abi_version, hal_abi_version_str[version]) == 0)
- return (enum hal_abi_version)version;
- }
-
- return HAL_ABI_VERSION_UNKNOWN;
-}
-
-static const char *__convert_module_to_string(enum hal_module module)
-{
- return hal_module_string[module];
-}
-
-static enum hal_group __convert_group_str_to_enum(const char * group)
-{
- enum hal_group group_idx;
-
- for (group_idx = HAL_GROUP_UNKNOWN + 1; group_idx < HAL_GROUP_END; group_idx++) {
- if (g_strcmp0(group, hal_group_string[group_idx]) == 0)
- return group_idx;
- }
-
- return HAL_GROUP_UNKNOWN;
-}
-
-static enum hal_license __convert_license_str_to_enum(const char *license)
-{
- if (g_strcmp0(license, "APACHE_2_0") == 0)
- return HAL_LICENSE_APACHE_2_0;
-
- if (g_strcmp0(license, "FLORA") == 0)
- return HAL_LICENSE_FLORA;
-
- if (g_strcmp0(license, "MIT") == 0)
- return HAL_LICENSE_MIT;
-
- return HAL_LICENSE_UNKNOWN;
-}
+static int _usage_count = 0;
__attribute__ ((visibility("default")))
void _destroy_module_info(gpointer data)
} while (0);
struct __hal_module_info *info = (struct __hal_module_info *)data;
- gboolean is_contained = false;
if (info) {
- is_contained = g_hash_table_contains(_module_hash, GINT_TO_POINTER(info->module));
- if (is_contained)
- g_hash_table_steal(_module_hash, GINT_TO_POINTER(info->module));
-
SAFE_FREE_AND_NULL(info->module_name);
+ SAFE_FREE_AND_NULL(info->backend_module_name);
SAFE_FREE_AND_NULL(info->library_name);
SAFE_FREE_AND_NULL(info->library_name_64bit);
SAFE_FREE_AND_NULL(info->symbol_name);
- SAFE_FREE_AND_NULL(info->abi_versions);
SAFE_FREE_AND_NULL(info);
}
}
-static bool __init_configuration(void)
+static struct __hal_module_info* _get_module_info(enum hal_module module)
{
- JsonNode *root_node = NULL;
- GError *error = NULL;
- gboolean result;
-
- if (_initialized)
- return _initialized;
-
- _parser = json_parser_new();
- result = json_parser_load_from_file(_parser, HAL_CONFIGURATION_PATH, &error);
- if (result == FALSE) {
- _E("Failed to parsing json configuration file : %s\n", error->message);
- goto err;
- }
-
- root_node = json_parser_get_root(_parser);
- if (JSON_NODE_HOLDS_OBJECT(root_node) == FALSE) {
- _E("There is no object in root node\n");
- goto err;
- }
-
- _root_object = json_node_get_object(root_node);
- _module_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, _destroy_module_info);
-
- _initialized = true;
-
- return _initialized;
-
-err:
- if (error)
- g_error_free(error);
- _destroy_configuration();
-
- return _initialized;
+ return &g_hal_module_info[module];
}
-static struct __hal_module_info *__create_hal_module_info(enum hal_module module, JsonObject *object)
+static struct __hal_module_info* _get_module_info_with_library_name(enum hal_module module,
+ const char *library_name)
{
- struct __hal_module_info *info;
- JsonArray *abi_versions_array = NULL;
- GList *abi_list = NULL;
- GList *iter_list = NULL;
- int list_index = 0;
- const char *tmp;
+ struct __hal_module_info *info = NULL, *new_info = NULL, *tmp_info = NULL;
+ char *library_name_prefix = NULL;
- info = (struct __hal_module_info *)calloc(1, sizeof(struct __hal_module_info));
- if (info == NULL) {
- _E("Out of Memory\n");
+ if (!_module_hash || !library_name)
return NULL;
- }
-
- info->module = module;
- info->module_name = g_strdup(__convert_module_to_string(module));
- tmp = json_object_get_string_member(object, "group");
- info->group = __convert_group_str_to_enum(tmp);
-
- tmp = json_object_get_string_member(object, "license");
- info->license = __convert_license_str_to_enum(tmp);
+ tmp_info = _get_module_info(module);
+ if (tmp_info == NULL) {
+ _E("Failed to get HAL module(%d) information\n", module);
+ return NULL;
+ }
- info->library_name = g_strdup(json_object_get_string_member(object, "library_name"));
- info->library_name_64bit = g_strdup(json_object_get_string_member(object, "library_name_64bit"));
- info->symbol_name = g_strdup(json_object_get_string_member(object, "symbol_name"));
+ if (tmp_info->backend_module_name == NULL) {
+ _E("Don't support HAL backend of HAL module(%s)\n",
+ tmp_info->module_name);
+ return NULL;
+ }
+ library_name_prefix = g_strdup_printf("libhal-backend-%s",
+ tmp_info->backend_module_name);
+ if (!library_name_prefix) {
+ _E("Failed to allocate library_name_prefix of HAL module(%s)\n",
+ tmp_info->module_name);
+ return NULL;
+ }
- if (info->library_name && info->library_name_64bit && info->symbol_name)
- info->hal_api = true;
+ if (!g_str_has_prefix(library_name, library_name_prefix)) {
+ _E("Invalid library name(%s) of HAL module(%s)\n",
+ library_name, tmp_info->module_name);
+ goto out;
+ }
- abi_versions_array = json_object_get_array_member(object, "abi_versions");
- if (abi_versions_array == NULL)
+ /* Find module info with the passed library name */
+ info = (struct __hal_module_info*)g_hash_table_lookup(_module_hash,
+ (gpointer)library_name);
+ if (info) {
+ g_free(library_name_prefix);
return info;
+ }
- abi_list = json_array_get_elements(abi_versions_array);
- info->num_abi_versions = g_list_length(abi_list);
- if (info->num_abi_versions > 0) {
- info->abi_versions = (struct hal_abi_version_match*)calloc(info->num_abi_versions,
- sizeof(struct hal_abi_version_match));
- if (info->abi_versions == NULL) {
- _E("Out of Memory\n");
- _destroy_module_info(info);
- g_list_free(abi_list);
- return NULL;
- }
-
- for (iter_list = abi_list; iter_list != NULL; iter_list = iter_list->next) {
- JsonObject *tmp_object = json_node_get_object(iter_list->data);
-
- tmp = json_object_get_string_member(tmp_object, "platform_abi_version");
- info->abi_versions[list_index].platform_abi_version =
- __convert_abi_version_str_to_enum(tmp);
+ /* Create new module info with the passed library name */
+ info = tmp_info;
- tmp = json_object_get_string_member(tmp_object, "backend_min_abi_version");
- info->abi_versions[list_index].backend_min_abi_version =
- __convert_abi_version_str_to_enum(tmp);
- list_index++;
- }
+ new_info = (struct __hal_module_info *)calloc(1,
+ sizeof(struct __hal_module_info));
+ if (new_info == NULL) {
+ _E("Failed to allocate the memory\n");
+ goto out;
}
- if (abi_list)
- g_list_free(abi_list);
-
- return info;
+ new_info->usage_count = 0;
+ new_info->group = info->group;
+ new_info->module = info->module;
+ new_info->license = info->license;
+ new_info->module_name = g_strdup(info->module_name);
+#if defined(__aarch64__) || defined(__x86_64__) || defined(__riscv)
+ new_info->library_name_64bit = g_strdup_printf("/hal/lib64/%s", library_name);
+#else
+ new_info->library_name = g_strdup_printf("/hal/lib/%s", library_name);
+#endif
+ new_info->symbol_name = g_strdup(info->symbol_name);
+ new_info->num_abi_versions = info->num_abi_versions;
+ new_info->abi_versions = info->abi_versions;
+ new_info->hal_api = info->hal_api;
+ new_info->hal_backend_extension = true;
+
+ g_hash_table_insert(_module_hash, (gpointer)library_name, new_info);
+
+out:
+ g_free(library_name_prefix);
+
+ return new_info;
}
__attribute__ ((visibility("default")))
-struct __hal_module_info* _hal_api_conf_get_module_info(enum hal_module module)
+struct __hal_module_info* _hal_api_conf_get_module_info(enum hal_module module,
+ const char *library_name)
{
- struct __hal_module_info *info = NULL;
- JsonArray *module_array = NULL;
- GList *module_list = NULL;
- GList *iter_list;
- const char *group_name = NULL;
- const char *module_name = NULL;
- bool ret_initialized;
+ if (!_module_hash)
+ return NULL;
- ret_initialized = __init_configuration();
- if (ret_initialized == false) {
- _E("Failed to parse json information\n");
- goto ret;
- }
+ if (!library_name)
+ return _get_module_info(module);
+ else
+ return _get_module_info_with_library_name(module, library_name);
+}
- info = (struct __hal_module_info*)g_hash_table_lookup(_module_hash, GINT_TO_POINTER(module));
- if (info != NULL)
- return info;
- module_array = json_object_get_array_member(_root_object, "MODULE_INFO");
- module_list = json_array_get_elements(module_array);
- module_name = __convert_module_to_string(module);
+enum hal_abi_version _hal_api_conf_get_platform_abi_version(void)
+{
+ return g_platform_curr_abi_version;
+}
- for (iter_list = module_list; iter_list != NULL; iter_list = iter_list->next) {
- JsonObject *tmp_object = json_node_get_object(iter_list->data);
- const char *value = json_object_get_string_member(tmp_object, "module");
- if (g_strcmp0(value, module_name) == 0) {
- info = __create_hal_module_info(module, tmp_object);
- if (info == NULL)
- _E("Failed to create hal module info\n");
- else
- g_hash_table_insert(_module_hash, GINT_TO_POINTER(module), info);
- break;
- }
- }
+__attribute__ ((visibility("default")))
+int _hal_api_conf_init(void)
+{
+ if (_usage_count++ > 0)
+ return 0;
-ret:
- if (module_list)
- g_list_free(module_list);
+ _module_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, _destroy_module_info);
- return info;
+ return 0;
}
-enum hal_abi_version _hal_api_conf_get_platform_abi_version(void)
+__attribute__ ((visibility("default")))
+void _hal_api_conf_exit(void)
{
- const char *abi_version = NULL;
- bool ret_initialized;
-
- if (_platform_abi_version != HAL_ABI_VERSION_END)
- return _platform_abi_version;
+ _usage_count--;
+ if (_usage_count != 0)
+ return;
- ret_initialized = __init_configuration();
- if (ret_initialized == false) {
- _E("Failed to parser json file\n");
- return HAL_ABI_VERSION_UNKNOWN;
+ if (_module_hash) {
+ g_hash_table_remove_all(_module_hash);
+ g_hash_table_unref(_module_hash);
}
-
- abi_version = json_object_get_string_member(_root_object, "PLATFORM_ABI_VERSION");
- _platform_abi_version = __convert_abi_version_str_to_enum(abi_version);
-
- return _platform_abi_version;
}