Add support hal-api-common library to check HAL ABI and support of haltest
[platform/hal/api/common.git] / src / hal-api-common.c
1 /*
2  * HAL Common API
3  *
4  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the License);
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #include <stdio.h>
20 #include <stdint.h>
21 #include <stdbool.h>
22 #include <dlog.h>
23
24 #include "common.h"
25
26 #ifndef EXPORT
27 #define EXPORT __attribute__ ((visibility("default")))
28 #endif
29
30 EXPORT
31 const char *hal_common_get_backend_library_name(enum hal_module module)
32 {
33         char *library_name;
34
35         /* Check parameter whether is valid or not */
36         if (module <= HAL_MODULE_UNKNOWN || module >= HAL_MODULE_END) {
37                 _E("Invalid parameter of HAL module (%d)\n", module);
38                 return NULL;
39         }
40
41 #if defined(__aarch64__)
42         library_name = hal_module_info[module].library_name_64bit;
43 #else
44         library_name = hal_module_info[module].library_name;
45 #endif
46
47         if (!library_name) {
48                 _E("%s backend library name is NULL\n",
49                                 hal_module_info[module].module_name);
50                 return NULL;
51         }
52         return library_name;
53 }
54
55 EXPORT
56 const char *hal_common_get_backend_symbol_name(enum hal_module module)
57 {
58         /* Check parameter whether is valid or not */
59         if (module <= HAL_MODULE_UNKNOWN || module >= HAL_MODULE_END) {
60                 _E("Invalid paramer of HAL module (%d)\n", module);
61                 return NULL;
62         }
63
64         if (!hal_module_info[module].symbol_name) {
65                 _E("%s backend library name is NULL\n",
66                                 hal_module_info[module].module_name);
67                 return NULL;
68         }
69
70         return hal_module_info[module].symbol_name;
71 }
72
73 EXPORT
74 int hal_common_check_backend_abi_version(enum hal_module module,
75                                 enum hal_abi_version abi_version)
76 {
77         int i;
78
79         /* Check parameter whether is valid or not */
80         if (module <= HAL_MODULE_UNKNOWN || module >= HAL_MODULE_END) {
81                 _E("Invalid paramer of HAL module (%d)\n", module);
82                 return TIZEN_ERROR_INVALID_PARAMETER;
83         }
84
85         if (abi_version <= HAL_ABI_VERSION_UNKNOWN
86                         || abi_version >= HAL_ABI_VERSION_END) {
87                 _E("Invalid paramer of HAL ABI version (%d)\n", abi_version);
88                 return TIZEN_ERROR_INVALID_PARAMETER;
89         }
90
91         /* Check abi_version whether is supported or not */
92         if (!hal_module_info[module].hal_api) {
93                 _E("HAL module(%d) doesn't support HAL API\n", module);
94                 return TIZEN_ERROR_INVALID_PARAMETER;
95         }
96
97         if (!hal_module_info[module].num_abi_versions
98                         || !hal_module_info[module].abi_versions) {
99                 _E("HAL module(%d) doesn't have the ABI version information\n",
100                                                 module);
101                 return TIZEN_ERROR_INVALID_PARAMETER;
102         }
103
104         for (i = 0; i < hal_module_info[module].num_abi_versions; i++) {
105                 struct hal_abi_version_match *data
106                                 = &hal_module_info[module].abi_versions[i];
107
108                 if (g_curr_hal_abi_version != data->curr_version)
109                         continue;
110
111                 if (abi_version <= data->curr_version
112                                 && abi_version >= data->compat_version)
113                         return TIZEN_ERROR_NONE;
114         }
115
116         return TIZEN_ERROR_INVALID_PARAMETER;
117 }