#include <gio/gio.h>
#include <glib-object.h>
-#include <json-c/json.h>
-#include <json-c/arraylist.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 json_object *_json_file_object = NULL;
+#include "hal-api-conf.h"
+#include "hal-api-list.h"
static GHashTable *_module_hash = NULL;
static int _usage_count = 0;
-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;
-}
-
__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 const char * __get_json_object_string(json_object *object, const char *key)
+static struct __hal_module_info* _get_module_info(enum hal_module module)
{
- json_object *temp_object = NULL;
-
- json_object_object_get_ex(object, key, &temp_object);
- return json_object_get_string(temp_object);
+ return &g_hal_module_info[module];
}
-static struct __hal_module_info *__create_hal_module_info(enum hal_module module, json_object *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;
- GList *abi_list = NULL;
- GList *iter_list = NULL;
- int list_index = 0;
- json_object *abi_versions_array;
- json_object *tmp_object;
- 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 = __get_json_object_string(object, "group");
- info->group = __convert_group_str_to_enum(tmp);
-
- tmp = __get_json_object_string(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(__get_json_object_string(object, "library_name"));
- info->library_name_64bit = g_strdup(__get_json_object_string(object, "library_name_64bit"));
- info->symbol_name = g_strdup(__get_json_object_string(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;
+ }
- json_object_object_get_ex(object, "abi_versions", &abi_versions_array);
- if (json_object_get_type(abi_versions_array) != json_type_array)
+ /* 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;
+ }
- info->num_abi_versions = json_object_array_length(abi_versions_array);
- 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);
- return NULL;
- }
-
- for (int i = 0; i < info->num_abi_versions; i++) {
- json_object *abi_object = json_object_array_get_idx(abi_versions_array, i);
-
- tmp = __get_json_object_string(abi_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 = __get_json_object_string(abi_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;
}
- 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;
- json_object *module_array_object = NULL;
- const char *group_name = NULL;
- const char *module_name = NULL;
- int i;
-
- if (!_json_file_object || !_module_hash)
+ if (!_module_hash)
return NULL;
- info = (struct __hal_module_info*)g_hash_table_lookup(_module_hash, GINT_TO_POINTER(module));
- if (info != NULL)
- return info;
-
- json_object_object_get_ex(_json_file_object, "MODULE_INFO", &module_array_object);
- module_name = __convert_module_to_string(module);
-
- for (i = 0; i < json_object_array_length(module_array_object); i++) {
- json_object *temp_object = json_object_array_get_idx(module_array_object, i);
- const char *value = __get_json_object_string(temp_object, "module");
- if (g_strcmp0(value, module_name) == 0) {
- info = __create_hal_module_info(module, temp_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;
- }
- }
-
- return info;
+ if (!library_name)
+ return _get_module_info(module);
+ else
+ return _get_module_info_with_library_name(module, library_name);
}
+
enum hal_abi_version _hal_api_conf_get_platform_abi_version(void)
{
- const char *abi_version = NULL;
- bool ret_initialized;
- json_object *platform_obj = NULL;
-
- if (_platform_abi_version != HAL_ABI_VERSION_END)
- return _platform_abi_version;
-
- if (!_json_file_object || !_module_hash)
- return HAL_ABI_VERSION_UNKNOWN;
-
- abi_version = __get_json_object_string(_json_file_object, "PLATFORM_ABI_VERSION");
- _platform_abi_version = __convert_abi_version_str_to_enum(abi_version);
-
- return _platform_abi_version;
+ return g_platform_curr_abi_version;
}
__attribute__ ((visibility("default")))
if (_usage_count++ > 0)
return 0;
- _json_file_object = json_object_from_file(HAL_CONFIGURATION_PATH);
- if (_json_file_object == NULL) {
- _E("Failed to parsing json configuration file : %s\n", json_util_get_last_err());
- goto err;
- }
-
_module_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, _destroy_module_info);
return 0;
-
-err:
- _usage_count--;
-
- if (_module_hash) {
- g_hash_table_remove_all(_module_hash);
- g_hash_table_unref(_module_hash);
- }
-
- return -EINVAL;
}
__attribute__ ((visibility("default")))
if (_usage_count != 0)
return;
- if (_json_file_object) {
- json_object_put(_json_file_object);
- _json_file_object = NULL;
- }
-
if (_module_hash) {
g_hash_table_remove_all(_module_hash);
g_hash_table_unref(_module_hash);