halapi: common: Remove json unused code
[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 "common.h"
29 #include "hal-api-conf.h"
30 #include "hal-api-list.h"
31
32 static GHashTable *_module_hash = NULL;
33
34 static int _usage_count = 0;
35
36 __attribute__ ((visibility("default")))
37 void _destroy_module_info(gpointer data)
38 {
39 #define SAFE_FREE_AND_NULL(x) \
40 do { \
41         if (x) { \
42                 free(x); \
43                 x = NULL; \
44         } \
45 } while (0);
46
47         struct __hal_module_info *info = (struct __hal_module_info *)data;
48
49         if (info) {
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->abi_versions);
56                 SAFE_FREE_AND_NULL(info);
57         }
58 }
59
60 static struct __hal_module_info* _get_module_info(enum hal_module module)
61 {
62         return &g_hal_module_info[module];
63 }
64
65 static struct __hal_module_info* _get_module_info_with_library_name(enum hal_module module,
66                                                                 const char *library_name)
67 {
68         struct __hal_module_info *info = NULL, *new_info = NULL, *tmp_info = NULL;
69         json_object *module_array_object = NULL;
70         const char *group_name = NULL;
71         const char *module_name = NULL;
72         char *library_name_prefix = NULL;
73         int ret;
74
75         if (!_module_hash | !library_name)
76                 return NULL;
77
78         tmp_info = _get_module_info(module);
79         if (tmp_info == NULL) {
80                 _E("Failed to get HAL module(%d) information\n", module);
81                 return NULL;
82         }
83
84         if (tmp_info->backend_module_name == NULL) {
85                 _E("Don't support HAL backend of HAL module(%s)\n",
86                                 tmp_info->module_name);
87                 return NULL;
88         }
89         library_name_prefix = g_strdup_printf("libhal-backend-%s",
90                                 tmp_info->backend_module_name);
91         if (!library_name_prefix) {
92                 _E("Failed to allocate library_name_prefix of HAL module(%s)\n",
93                                 tmp_info->module_name);
94                 return NULL;
95         }
96
97         if (!g_str_has_prefix(library_name, library_name_prefix)) {
98                 _E("Invalid library name(%s) of HAL module(%s)\n",
99                                 library_name, tmp_info->module_name);
100                 goto out;
101         }
102
103         /* Find module info with the passed library name */
104         info = (struct __hal_module_info*)g_hash_table_lookup(_module_hash,
105                                                 (gpointer)library_name);
106         if (info) {
107                 g_free(library_name_prefix);
108                 return info;
109         }
110
111         /* Create new module info with the passed library name */
112         info = tmp_info;
113
114         new_info = (struct __hal_module_info *)calloc(1,
115                                         sizeof(struct __hal_module_info));
116         if (new_info == NULL) {
117                 _E("Failed to allocate the memory\n");
118                 goto out;
119         }
120
121         new_info->usage_count = 0;
122         new_info->group = info->group;
123         new_info->module = info->module;
124         new_info->license = info->license;
125         new_info->module_name = g_strdup(info->module_name);
126 #if defined(__aarch64__)
127         new_info->library_name_64bit = g_strdup_printf("/hal/lib64/%s", library_name);
128 #else
129         new_info->library_name = g_strdup_printf("/hal/lib/%s", library_name);
130 #endif
131         new_info->symbol_name = g_strdup(info->symbol_name);
132         new_info->num_abi_versions = info->num_abi_versions;
133         new_info->abi_versions = info->abi_versions;
134         new_info->hal_api = info->hal_api;
135         new_info->hal_backend_extension = true;
136
137         g_hash_table_insert(_module_hash, (gpointer)library_name, new_info);
138
139 out:
140         g_free(library_name_prefix);
141
142         return new_info;
143 }
144
145 __attribute__ ((visibility("default")))
146 struct __hal_module_info* _hal_api_conf_get_module_info(enum hal_module module,
147                                                         const char *library_name)
148 {
149         if (!_module_hash)
150                 return NULL;
151
152         if (!library_name)
153                 return _get_module_info(module);
154         else
155                 return _get_module_info_with_library_name(module, library_name);
156 }
157
158
159 enum hal_abi_version _hal_api_conf_get_platform_abi_version(void)
160 {
161         return g_platform_curr_abi_version;
162 }
163
164 __attribute__ ((visibility("default")))
165 int _hal_api_conf_init(void)
166 {
167         if (_usage_count++ > 0)
168                 return 0;
169
170         _module_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, _destroy_module_info);
171
172         return 0;
173 }
174
175 __attribute__ ((visibility("default")))
176 void _hal_api_conf_exit(void)
177 {
178         _usage_count--;
179         if (_usage_count != 0)
180                 return;
181
182         if (_module_hash) {
183                 g_hash_table_remove_all(_module_hash);
184                 g_hash_table_unref(_module_hash);
185         }
186 }