2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include <glib-object.h>
22 #include <json-c/json.h>
23 #include <json-c/arraylist.h>
25 #include "hal-common.h"
26 #include "hal-common-interface.h"
29 #include "hal-api-conf.h"
30 #include "hal-api-list.h"
32 static GHashTable *_module_hash = NULL;
34 static int _usage_count = 0;
36 __attribute__ ((visibility("default")))
37 void _destroy_module_info(gpointer data)
39 #define SAFE_FREE_AND_NULL(x) \
47 struct __hal_module_info *info = (struct __hal_module_info *)data;
50 SAFE_FREE_AND_NULL(info->module_name);
51 SAFE_FREE_AND_NULL(info->backend_module_name);
52 SAFE_FREE_AND_NULL(info->library_name);
53 SAFE_FREE_AND_NULL(info->library_name_64bit);
54 SAFE_FREE_AND_NULL(info->symbol_name);
55 SAFE_FREE_AND_NULL(info);
59 static struct __hal_module_info* _get_module_info(enum hal_module module)
61 return &g_hal_module_info[module];
64 static struct __hal_module_info* _get_module_info_with_library_name(enum hal_module module,
65 const char *library_name)
67 struct __hal_module_info *info = NULL, *new_info = NULL, *tmp_info = NULL;
68 json_object *module_array_object = NULL;
69 const char *group_name = NULL;
70 const char *module_name = NULL;
71 char *library_name_prefix = NULL;
74 if (!_module_hash || !library_name)
77 tmp_info = _get_module_info(module);
78 if (tmp_info == NULL) {
79 _E("Failed to get HAL module(%d) information\n", module);
83 if (tmp_info->backend_module_name == NULL) {
84 _E("Don't support HAL backend of HAL module(%s)\n",
85 tmp_info->module_name);
88 library_name_prefix = g_strdup_printf("libhal-backend-%s",
89 tmp_info->backend_module_name);
90 if (!library_name_prefix) {
91 _E("Failed to allocate library_name_prefix of HAL module(%s)\n",
92 tmp_info->module_name);
96 if (!g_str_has_prefix(library_name, library_name_prefix)) {
97 _E("Invalid library name(%s) of HAL module(%s)\n",
98 library_name, tmp_info->module_name);
102 /* Find module info with the passed library name */
103 info = (struct __hal_module_info*)g_hash_table_lookup(_module_hash,
104 (gpointer)library_name);
106 g_free(library_name_prefix);
110 /* Create new module info with the passed library name */
113 new_info = (struct __hal_module_info *)calloc(1,
114 sizeof(struct __hal_module_info));
115 if (new_info == NULL) {
116 _E("Failed to allocate the memory\n");
120 new_info->usage_count = 0;
121 new_info->group = info->group;
122 new_info->module = info->module;
123 new_info->license = info->license;
124 new_info->module_name = g_strdup(info->module_name);
125 #if defined(__aarch64__)
126 new_info->library_name_64bit = g_strdup_printf("/hal/lib64/%s", library_name);
128 new_info->library_name = g_strdup_printf("/hal/lib/%s", library_name);
130 new_info->symbol_name = g_strdup(info->symbol_name);
131 new_info->num_abi_versions = info->num_abi_versions;
132 new_info->abi_versions = info->abi_versions;
133 new_info->hal_api = info->hal_api;
134 new_info->hal_backend_extension = true;
136 g_hash_table_insert(_module_hash, (gpointer)library_name, new_info);
139 g_free(library_name_prefix);
144 __attribute__ ((visibility("default")))
145 struct __hal_module_info* _hal_api_conf_get_module_info(enum hal_module module,
146 const char *library_name)
152 return _get_module_info(module);
154 return _get_module_info_with_library_name(module, library_name);
158 enum hal_abi_version _hal_api_conf_get_platform_abi_version(void)
160 return g_platform_curr_abi_version;
163 __attribute__ ((visibility("default")))
164 int _hal_api_conf_init(void)
166 if (_usage_count++ > 0)
169 _module_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, _destroy_module_info);
174 __attribute__ ((visibility("default")))
175 void _hal_api_conf_exit(void)
178 if (_usage_count != 0)
182 g_hash_table_remove_all(_module_hash);
183 g_hash_table_unref(_module_hash);