halapi: Remove circular dependency between packages
[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
22 #include <json-c/json.h>
23 #include <json-c/arraylist.h>
24
25 #include "hal-common.h"
26 #include "hal-common-interface.h"
27
28 #include "hal-api-conf.h"
29
30 #include "common.h"
31
32 static enum hal_abi_version _platform_abi_version = HAL_ABI_VERSION_END;
33
34 static json_object *_json_file_object = NULL;
35
36 static GHashTable *_module_hash = NULL;
37
38 static int _usage_count = 0;
39
40 static enum hal_abi_version __convert_abi_version_str_to_enum(const char *abi_version) {
41         int version;
42         for (version = HAL_ABI_VERSION_UNKNOWN + 1; version < HAL_ABI_VERSION_END; version++){
43                 if (g_strcmp0(abi_version, hal_abi_version_str[version]) == 0)
44                         return (enum hal_abi_version)version;
45         }
46
47         return HAL_ABI_VERSION_UNKNOWN;
48 }
49
50 static const char *__convert_module_to_string(enum hal_module module)
51 {
52         return hal_module_string[module];
53 }
54
55 static enum hal_group __convert_group_str_to_enum(const char * group)
56 {
57         enum hal_group group_idx;
58
59         for (group_idx = HAL_GROUP_UNKNOWN + 1; group_idx < HAL_GROUP_END; group_idx++) {
60                 if (g_strcmp0(group, hal_group_string[group_idx]) == 0)
61                         return group_idx;
62         }
63
64         return HAL_GROUP_UNKNOWN;
65 }
66
67 static enum hal_license __convert_license_str_to_enum(const char *license)
68 {
69         if (g_strcmp0(license, "APACHE_2_0") == 0)
70                 return HAL_LICENSE_APACHE_2_0;
71
72         if (g_strcmp0(license, "FLORA") == 0)
73                 return HAL_LICENSE_FLORA;
74
75         if (g_strcmp0(license, "MIT") == 0)
76                 return HAL_LICENSE_MIT;
77
78         return HAL_LICENSE_UNKNOWN;
79 }
80
81 __attribute__ ((visibility("default")))
82 void _destroy_module_info(gpointer data)
83 {
84 #define SAFE_FREE_AND_NULL(x) \
85 do { \
86         if (x) { \
87                 free(x); \
88                 x = NULL; \
89         } \
90 } while (0);
91
92         struct __hal_module_info *info = (struct __hal_module_info *)data;
93         gboolean is_contained = false;
94
95         if (info) {
96                 is_contained = g_hash_table_contains(_module_hash, GINT_TO_POINTER(info->module));
97                 if (is_contained)
98                         g_hash_table_steal(_module_hash, GINT_TO_POINTER(info->module));
99
100                 SAFE_FREE_AND_NULL(info->module_name);
101                 SAFE_FREE_AND_NULL(info->library_name);
102                 SAFE_FREE_AND_NULL(info->library_name_64bit);
103                 SAFE_FREE_AND_NULL(info->symbol_name);
104                 SAFE_FREE_AND_NULL(info->abi_versions);
105                 SAFE_FREE_AND_NULL(info);
106         }
107 }
108
109 static const char * __get_json_object_string(json_object *object, const char *key)
110 {
111         json_object *temp_object = NULL;
112
113         json_object_object_get_ex(object, key, &temp_object);
114         return json_object_get_string(temp_object);
115 }
116
117 static struct __hal_module_info *__create_hal_module_info(enum hal_module module, json_object *object)
118 {
119         struct __hal_module_info *info;
120         GList *abi_list = NULL;
121         GList *iter_list = NULL;
122         int list_index = 0;
123         json_object *abi_versions_array;
124         json_object *tmp_object;
125         const char *tmp;
126
127         info = (struct __hal_module_info *)calloc(1, sizeof(struct __hal_module_info));
128         if (info == NULL) {
129                 _E("Out of Memory\n");
130                 return NULL;
131         }
132
133         info->module = module;
134         info->module_name = g_strdup(__convert_module_to_string(module));
135
136         tmp = __get_json_object_string(object, "group");
137         info->group = __convert_group_str_to_enum(tmp);
138
139         tmp = __get_json_object_string(object, "license");
140         info->license = __convert_license_str_to_enum(tmp);
141
142         info->library_name = g_strdup(__get_json_object_string(object, "library_name"));
143         info->library_name_64bit = g_strdup(__get_json_object_string(object, "library_name_64bit"));
144         info->symbol_name = g_strdup(__get_json_object_string(object, "symbol_name"));
145
146         if (info->library_name && info->library_name_64bit && info->symbol_name)
147                 info->hal_api = true;
148
149         json_object_object_get_ex(object, "abi_versions", &abi_versions_array);
150         if (json_object_get_type(abi_versions_array) != json_type_array)
151                 return info;
152
153         info->num_abi_versions = json_object_array_length(abi_versions_array);
154         if (info->num_abi_versions > 0) {
155                 info->abi_versions = (struct hal_abi_version_match*)calloc(info->num_abi_versions,
156                                 sizeof(struct hal_abi_version_match));
157                 if (info->abi_versions == NULL) {
158                         _E("Out of Memory\n");
159                         _destroy_module_info(info);
160                         return NULL;
161                 }
162
163                 for (int i = 0; i < info->num_abi_versions; i++) {
164                         json_object *abi_object = json_object_array_get_idx(abi_versions_array, i);
165
166                         tmp = __get_json_object_string(abi_object, "platform_abi_version");
167                         info->abi_versions[list_index].platform_abi_version =
168                                 __convert_abi_version_str_to_enum(tmp);
169
170                         tmp = __get_json_object_string(abi_object, "backend_min_abi_version");
171                         info->abi_versions[list_index].backend_min_abi_version =
172                                 __convert_abi_version_str_to_enum(tmp);
173                         list_index++;
174                 }
175         }
176
177         return info;
178 }
179
180 __attribute__ ((visibility("default")))
181 struct __hal_module_info* _hal_api_conf_get_module_info(enum hal_module module)
182 {
183         struct __hal_module_info *info = NULL;
184         json_object *module_array_object = NULL;
185         const char *group_name = NULL;
186         const char *module_name = NULL;
187         int i;
188
189         if (!_json_file_object || !_module_hash)
190                 return NULL;
191
192         info = (struct __hal_module_info*)g_hash_table_lookup(_module_hash, GINT_TO_POINTER(module));
193         if (info != NULL)
194                 return info;
195
196         json_object_object_get_ex(_json_file_object, "MODULE_INFO", &module_array_object);
197         module_name = __convert_module_to_string(module);
198
199         for (i = 0; i < json_object_array_length(module_array_object); i++) {
200                 json_object *temp_object = json_object_array_get_idx(module_array_object, i);
201                 const char *value = __get_json_object_string(temp_object, "module");
202                 if (g_strcmp0(value, module_name) == 0) {
203                         info = __create_hal_module_info(module, temp_object);
204                         if (info == NULL)
205                                 _E("Failed to create hal module info\n");
206                         else
207                                 g_hash_table_insert(_module_hash, GINT_TO_POINTER(module), info);
208                         break;
209                 }
210         }
211
212         return info;
213 }
214
215 enum hal_abi_version _hal_api_conf_get_platform_abi_version(void)
216 {
217         const char *abi_version = NULL;
218         bool ret_initialized;
219         json_object *platform_obj = NULL;
220
221         if (_platform_abi_version != HAL_ABI_VERSION_END)
222                 return _platform_abi_version;
223
224         if (!_json_file_object || !_module_hash)
225                 return HAL_ABI_VERSION_UNKNOWN;
226
227         abi_version = __get_json_object_string(_json_file_object, "PLATFORM_ABI_VERSION");
228         _platform_abi_version = __convert_abi_version_str_to_enum(abi_version);
229
230         return _platform_abi_version;
231 }
232
233 __attribute__ ((visibility("default")))
234 int _hal_api_conf_init(void)
235 {
236         if (_usage_count++ > 0)
237                 return 0;
238
239         _json_file_object = json_object_from_file(HAL_CONFIGURATION_PATH);
240         if (_json_file_object == NULL) {
241                 _E("Failed to parsing json configuration file : %s\n", json_util_get_last_err());
242                 goto err;
243         }
244
245         _module_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, _destroy_module_info);
246
247         return 0;
248
249 err:
250         _usage_count--;
251
252         if (_module_hash) {
253                 g_hash_table_remove_all(_module_hash);
254                 g_hash_table_unref(_module_hash);
255         }
256
257         return -EINVAL;
258 }
259
260 __attribute__ ((visibility("default")))
261 void _hal_api_conf_exit(void)
262 {
263         _usage_count--;
264         if (_usage_count != 0)
265                 return;
266
267         if (_json_file_object) {
268                 json_object_put(_json_file_object);
269                 _json_file_object = NULL;
270         }
271
272         if (_module_hash) {
273                 g_hash_table_remove_all(_module_hash);
274                 g_hash_table_unref(_module_hash);
275         }
276 }