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>
21 #include <json-glib/json-glib.h>
23 #include "hal-common.h"
24 #include "hal-common-interface.h"
26 #include "hal-api-conf.h"
30 static enum hal_abi_version _platform_abi_version = HAL_ABI_VERSION_END;
32 static JsonParser *_parser = NULL;
33 static JsonObject *_root_object = NULL;
35 static GHashTable *_module_hash = NULL;
37 static bool _initialized = false;
39 void __attribute__ ((destructor)) _destroy_configuration(void);
41 void _destroy_configuration(void)
44 g_object_unref(_parser);
47 g_hash_table_remove_all(_module_hash);
48 g_hash_table_unref(_module_hash);
54 static enum hal_abi_version __convert_abi_version_str_to_enum(const char *abi_version) {
56 for (version = HAL_ABI_VERSION_UNKNOWN + 1; version < HAL_ABI_VERSION_END; version++){
57 if (g_strcmp0(abi_version, hal_abi_version_str[version]) == 0)
58 return (enum hal_abi_version)version;
61 return HAL_ABI_VERSION_UNKNOWN;
64 static const char *__convert_module_to_string(enum hal_module module)
66 return hal_module_string[module];
69 static enum hal_group __convert_group_str_to_enum(const char * group)
71 enum hal_group group_idx;
73 for (group_idx = HAL_GROUP_UNKNOWN + 1; group_idx < HAL_GROUP_END; group_idx++) {
74 if (g_strcmp0(group, hal_group_string[group_idx]) == 0)
78 return HAL_GROUP_UNKNOWN;
81 static enum hal_license __convert_license_str_to_enum(const char *license)
83 if (g_strcmp0(license, "APACHE_2_0") == 0)
84 return HAL_LICENSE_APACHE_2_0;
86 if (g_strcmp0(license, "FLORA") == 0)
87 return HAL_LICENSE_FLORA;
89 if (g_strcmp0(license, "MIT") == 0)
90 return HAL_LICENSE_MIT;
92 return HAL_LICENSE_UNKNOWN;
95 __attribute__ ((visibility("default")))
96 void _destroy_module_info(gpointer data)
98 #define SAFE_FREE_AND_NULL(x) \
106 struct __hal_module_info *info = (struct __hal_module_info *)data;
107 gboolean is_contained = false;
110 is_contained = g_hash_table_contains(_module_hash, GINT_TO_POINTER(info->module));
112 g_hash_table_steal(_module_hash, GINT_TO_POINTER(info->module));
114 SAFE_FREE_AND_NULL(info->module_name);
115 SAFE_FREE_AND_NULL(info->library_name);
116 SAFE_FREE_AND_NULL(info->library_name_64bit);
117 SAFE_FREE_AND_NULL(info->symbol_name);
118 SAFE_FREE_AND_NULL(info->abi_versions);
119 SAFE_FREE_AND_NULL(info);
123 static bool __init_configuration(void)
125 JsonNode *root_node = NULL;
126 GError *error = NULL;
132 _parser = json_parser_new();
133 result = json_parser_load_from_file(_parser, HAL_CONFIGURATION_PATH, &error);
134 if (result == FALSE) {
135 _E("Failed to parsing json configuration file : %s\n", error->message);
139 root_node = json_parser_get_root(_parser);
140 if (JSON_NODE_HOLDS_OBJECT(root_node) == FALSE) {
141 _E("There is no object in root node\n");
145 _root_object = json_node_get_object(root_node);
146 _module_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, _destroy_module_info);
155 _destroy_configuration();
160 static struct __hal_module_info *__create_hal_module_info(enum hal_module module, JsonObject *object)
162 struct __hal_module_info *info;
163 JsonArray *abi_versions_array = NULL;
164 GList *abi_list = NULL;
165 GList *iter_list = NULL;
169 info = (struct __hal_module_info *)calloc(1, sizeof(struct __hal_module_info));
171 _E("Out of Memory\n");
175 info->module = module;
176 info->module_name = g_strdup(__convert_module_to_string(module));
178 tmp = json_object_get_string_member(object, "group");
179 info->group = __convert_group_str_to_enum(tmp);
181 tmp = json_object_get_string_member(object, "license");
182 info->license = __convert_license_str_to_enum(tmp);
184 info->library_name = g_strdup(json_object_get_string_member(object, "library_name"));
185 info->library_name_64bit = g_strdup(json_object_get_string_member(object, "library_name_64bit"));
186 info->symbol_name = g_strdup(json_object_get_string_member(object, "symbol_name"));
188 if (info->library_name && info->library_name_64bit && info->symbol_name)
189 info->hal_api = true;
191 abi_versions_array = json_object_get_array_member(object, "abi_versions");
192 if (abi_versions_array == NULL)
195 abi_list = json_array_get_elements(abi_versions_array);
196 info->num_abi_versions = g_list_length(abi_list);
197 if (info->num_abi_versions > 0) {
198 info->abi_versions = (struct hal_abi_version_match*)calloc(info->num_abi_versions,
199 sizeof(struct hal_abi_version_match));
200 if (info->abi_versions == NULL) {
201 _E("Out of Memory\n");
202 _destroy_module_info(info);
203 g_list_free(abi_list);
207 for (iter_list = abi_list; iter_list != NULL; iter_list = iter_list->next) {
208 JsonObject *tmp_object = json_node_get_object(iter_list->data);
210 tmp = json_object_get_string_member(tmp_object, "platform_abi_version");
211 info->abi_versions[list_index].platform_abi_version =
212 __convert_abi_version_str_to_enum(tmp);
214 tmp = json_object_get_string_member(tmp_object, "backend_min_abi_version");
215 info->abi_versions[list_index].backend_min_abi_version =
216 __convert_abi_version_str_to_enum(tmp);
222 g_list_free(abi_list);
227 __attribute__ ((visibility("default")))
228 struct __hal_module_info* _hal_api_conf_get_module_info(enum hal_module module)
230 struct __hal_module_info *info = NULL;
231 JsonArray *module_array = NULL;
232 GList *module_list = NULL;
234 const char *group_name = NULL;
235 const char *module_name = NULL;
236 bool ret_initialized;
238 ret_initialized = __init_configuration();
239 if (ret_initialized == false) {
240 _E("Failed to parse json information\n");
244 info = (struct __hal_module_info*)g_hash_table_lookup(_module_hash, GINT_TO_POINTER(module));
248 module_array = json_object_get_array_member(_root_object, "MODULE_INFO");
249 module_list = json_array_get_elements(module_array);
250 module_name = __convert_module_to_string(module);
252 for (iter_list = module_list; iter_list != NULL; iter_list = iter_list->next) {
253 JsonObject *tmp_object = json_node_get_object(iter_list->data);
254 const char *value = json_object_get_string_member(tmp_object, "module");
255 if (g_strcmp0(value, module_name) == 0) {
256 info = __create_hal_module_info(module, tmp_object);
258 _E("Failed to create hal module info\n");
260 g_hash_table_insert(_module_hash, GINT_TO_POINTER(module), info);
267 g_list_free(module_list);
272 enum hal_abi_version _hal_api_conf_get_platform_abi_version(void)
274 const char *abi_version = NULL;
275 bool ret_initialized;
277 if (_platform_abi_version != HAL_ABI_VERSION_END)
278 return _platform_abi_version;
280 ret_initialized = __init_configuration();
281 if (ret_initialized == false) {
282 _E("Failed to parser json file\n");
283 return HAL_ABI_VERSION_UNKNOWN;
286 abi_version = json_object_get_string_member(_root_object, "PLATFORM_ABI_VERSION");
287 _platform_abi_version = __convert_abi_version_str_to_enum(abi_version);
289 return _platform_abi_version;