0b4f5d894c7b9bbaa371180840f64234fdda8433
[platform/hal/api/common.git] / src / hal-api-conf.c
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <stdlib.h>
18
19 #include <gio/gio.h>
20 #include <glib-object.h>
21 #include <json-glib/json-glib.h>
22
23 #include "hal-common.h"
24 #include "hal-common-interface.h"
25
26 #include "hal-api-conf.h"
27
28 #include "common.h"
29
30 static enum hal_abi_version _platform_abi_version = HAL_ABI_VERSION_END;
31
32 static JsonParser *_parser = NULL;
33 static JsonObject *_root_object = NULL;
34
35 static GHashTable *_module_hash = NULL;
36
37 static bool _initialized = false;
38
39 void __attribute__ ((destructor)) _destroy_configuration(void);
40
41 void _destroy_configuration(void)
42 {
43         if (_parser)
44                 g_object_unref(_parser);
45
46         if (_module_hash) {
47                 g_hash_table_remove_all(_module_hash);
48                 g_hash_table_unref(_module_hash);
49         }
50
51         _initialized = false;
52 }
53
54 static enum hal_abi_version __convert_abi_version_str_to_enum(const char *abi_version) {
55         int 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;
59         }
60
61         return HAL_ABI_VERSION_UNKNOWN;
62 }
63
64 static const char *__convert_module_to_string(enum hal_module module)
65 {
66         return hal_module_string[module];
67 }
68
69 static enum hal_group __convert_group_str_to_enum(const char * group)
70 {
71         enum hal_group group_idx;
72
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)
75                         return group_idx;
76         }
77
78         return HAL_GROUP_UNKNOWN;
79 }
80
81 static enum hal_license __convert_license_str_to_enum(const char *license)
82 {
83         if (g_strcmp0(license, "APACHE_2_0") == 0)
84                 return HAL_LICENSE_APACHE_2_0;
85
86         if (g_strcmp0(license, "FLORA") == 0)
87                 return HAL_LICENSE_FLORA;
88
89         if (g_strcmp0(license, "MIT") == 0)
90                 return HAL_LICENSE_MIT;
91
92         return HAL_LICENSE_UNKNOWN;
93 }
94
95 __attribute__ ((visibility("default")))
96 void _destroy_module_info(gpointer data)
97 {
98 #define SAFE_FREE_AND_NULL(x) \
99 do { \
100         if (x) { \
101                 free(x); \
102                 x = NULL; \
103         } \
104 } while (0);
105
106         struct __hal_module_info *info = (struct __hal_module_info *)data;
107         gboolean is_contained = false;
108
109         if (info) {
110                 is_contained = g_hash_table_contains(_module_hash, GINT_TO_POINTER(info->module));
111                 if (is_contained)
112                         g_hash_table_steal(_module_hash, GINT_TO_POINTER(info->module));
113
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);
120         }
121 }
122
123 static bool __init_configuration(void)
124 {
125         JsonNode *root_node = NULL;
126         GError *error = NULL;
127         gboolean result;
128
129         if (_initialized)
130                 return _initialized;
131
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);
136                 goto err;
137         }
138
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");
142                 goto err;
143         }
144
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);
147
148         _initialized = true;
149
150         return _initialized;
151
152 err:
153         if (error)
154                 g_error_free(error);
155         _destroy_configuration();
156
157         return _initialized;
158 }
159
160 static struct __hal_module_info *__create_hal_module_info(enum hal_module module, JsonObject *object)
161 {
162         struct __hal_module_info *info;
163         JsonArray *abi_versions_array = NULL;
164         GList *abi_list = NULL;
165         GList *iter_list = NULL;
166         int list_index = 0;
167         const char *tmp;
168
169         info = (struct __hal_module_info *)calloc(1, sizeof(struct __hal_module_info));
170         if (info == NULL) {
171                 _E("Out of Memory\n");
172                 return NULL;
173         }
174
175         info->module = module;
176         info->module_name = g_strdup(__convert_module_to_string(module));
177
178         tmp = json_object_get_string_member(object, "group");
179         info->group = __convert_group_str_to_enum(tmp);
180
181         tmp = json_object_get_string_member(object, "license");
182         info->license = __convert_license_str_to_enum(tmp);
183
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"));
187
188         if (info->library_name && info->library_name_64bit && info->symbol_name)
189                 info->hal_api = true;
190
191         abi_versions_array = json_object_get_array_member(object, "abi_versions");
192         if (abi_versions_array == NULL)
193                 return info;
194
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);
204                         return NULL;
205                 }
206
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);
209
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);
213
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);
217                         list_index++;
218                 }
219         }
220
221         if (abi_list)
222                 g_list_free(abi_list);
223
224         return info;
225 }
226
227 __attribute__ ((visibility("default")))
228 struct __hal_module_info* _hal_api_conf_get_module_info(enum hal_module module)
229 {
230         struct __hal_module_info *info = NULL;
231         JsonArray *module_array = NULL;
232         GList *module_list = NULL;
233         GList *iter_list;
234         const char *group_name = NULL;
235         const char *module_name = NULL;
236         bool ret_initialized;
237
238         ret_initialized = __init_configuration();
239         if (ret_initialized == false) {
240                 _E("Failed to parse json information\n");
241                 goto ret;
242         }
243
244         info = (struct __hal_module_info*)g_hash_table_lookup(_module_hash, GINT_TO_POINTER(module));
245         if (info != NULL)
246                 return info;
247
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);
251
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);
257                         if (info == NULL)
258                                 _E("Failed to create hal module info\n");
259                         else
260                                 g_hash_table_insert(_module_hash, GINT_TO_POINTER(module), info);
261                         break;
262                 }
263         }
264
265 ret:
266         if (module_list)
267                 g_list_free(module_list);
268
269         return info;
270 }
271
272 enum hal_abi_version _hal_api_conf_get_platform_abi_version(void)
273 {
274         const char *abi_version = NULL;
275         bool ret_initialized;
276
277         if (_platform_abi_version != HAL_ABI_VERSION_END)
278                 return _platform_abi_version;
279
280         ret_initialized = __init_configuration();
281         if (ret_initialized == false) {
282                 _E("Failed to parser json file\n");
283                 return HAL_ABI_VERSION_UNKNOWN;
284         }
285
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);
288
289         return _platform_abi_version;
290 }