tools: lshal: Add missing flag to print hal backend information
[platform/hal/api/common.git] / tools / lshal / lshal.c
1 /*
2  * lshal Tool
3  *
4  * Copyright (c) 2023 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 <glib.h>
20
21 #include <sys/types.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <signal.h>
26 #include <unistd.h>
27 #include <inttypes.h>
28
29 #include "hal-common.h"
30 #include "hal-common-interface.h"
31
32 #include "hal-api-conf.h"
33
34 #define BIT(x)                                  (1ULL << x)
35 #define BUFF_MAX                                256
36
37 #define LSHAL_FLAG_BACKEND_VERIFICATION         BIT(0)
38 #define LSHAL_FLAG_BACKEND_MODULE_NAME          BIT(1)
39 #define LSHAL_FLAG_BACKEND_LIB_NAME             BIT(2)
40 #define LSHAL_FLAG_BACKEND_SYMBOL_NAME          BIT(3)
41 #define LSHAL_FLAG_BACKEND_NAME                 BIT(4)
42 #define LSHAL_FLAG_BACKEND_VENDOR_NAME          BIT(5)
43 #define LSHAL_FLAG_BACKEND_USAGE_COUNT          BIT(6)
44 #define LSHAL_FLAG_BACKEND_ABI_VERSION          BIT(7)
45
46 struct _hal_backend_module_data {
47         /**
48          * Each HAL module defines the different backend_module_data.
49          * In order to test the all HAL backends, just use the large buffer.
50          */
51         void *unused_buffer[10000];
52 } *backend_module_data;
53
54 static int lshal_verify_hal_backend(enum hal_module module)
55 {
56         int ret;
57
58         ret = hal_common_get_backend(module, (void **)&backend_module_data);
59         if (ret < 0)
60                 return ret;
61
62         ret = hal_common_put_backend(module, backend_module_data);
63         if (ret < 0)
64                 return ret;
65
66         return 0;
67 }
68
69 static void lshal_print_border(void) {
70         for (int i = 0; i < 267; i++)
71                 printf("-");
72         printf("\n");
73 }
74
75 static void lshal_print_hal_backend_info(u_int32_t flags) {
76         int ret;
77         enum hal_module module;
78         char str[BUFF_MAX];
79         int backend_verification_count = 0;
80
81         lshal_print_border();
82         printf(" %-38s |    | %-55s | %-10s | %-15s | %-45s | %-25s | %-25s | %-25s |\n",
83                         "",
84                         "",
85                         "Backend",
86                         "Backend",
87                         "Backend",
88                         "Backend ABI Version",
89                         "Backend Name",
90                         "Vendor Name");
91
92         printf(" %-38s | ID | %-55s | %-10s | %-15s | %-45s | %-25s | %-25s | %-25s |\n",
93                         "HAL Module Name",
94                         "Backend Library Name",
95                         "Open Count",
96                         "Verification",
97                         "Symbol Name",
98                         "(Written by Developer)",
99                         "(Written by Developer)",
100                         "(Written by Developer)");
101         lshal_print_border();
102
103         for (module = HAL_MODULE_UNKNOWN + 1; module < HAL_MODULE_END; module++) {
104                 if (LSHAL_FLAG_BACKEND_MODULE_NAME & flags) {
105                         strncpy(str, "", BUFF_MAX - 1);
106
107                         ret = hal_common_get_backend_module_name(module, str, BUFF_MAX - 1);
108                         if (ret < 0)
109                                 printf(" %-38s | %-2d |", "", module);
110                         else
111                                 printf(" %-38s | %-2d |", str, module);
112                 }
113
114                 if (LSHAL_FLAG_BACKEND_LIB_NAME & flags) {
115                         strncpy(str, "", BUFF_MAX - 1);
116
117                         ret = hal_common_get_backend_library_name(module, str, BUFF_MAX - 1);
118                         if (ret < 0)
119                                 printf(" %-55s |", "");
120                         else
121                                 printf(" %-55s |", str);
122                 }
123
124                 if (LSHAL_FLAG_BACKEND_USAGE_COUNT & flags) {
125                         int count = hal_common_get_backend_count(module);
126                         if (count < 0)
127                                 printf(" %-10s |", "");
128                         else
129                                 printf(" %-10d |", count);
130                 }
131
132                 if (LSHAL_FLAG_BACKEND_VERIFICATION & flags) {
133                         ret = lshal_verify_hal_backend(module);
134
135                         if (ret == -ENOENT) {
136                                 printf(" %-15s |", "NO  |");
137                         } else if (ret < 0) {
138                                 printf(" %-15s |", "    |");
139                         } else {
140                                 printf(" %-15s |", "YES | VERIFIED");
141                                 backend_verification_count++;
142                         }
143                 }
144
145                 if (LSHAL_FLAG_BACKEND_SYMBOL_NAME & flags) {
146                         strncpy(str, "", BUFF_MAX - 1);
147
148                         ret = hal_common_get_backend_symbol_name(module, str, BUFF_MAX - 1);
149                         if (ret < 0)
150                                 printf(" %-45s |", "");
151                         else
152                                 printf(" %-45s |", str);
153                 }
154
155                 if (LSHAL_FLAG_BACKEND_ABI_VERSION & flags) {
156                         strncpy(str, "", BUFF_MAX - 1);
157
158                         ret = hal_common_get_backend_abi_version(module);
159                         if (ret == HAL_ABI_VERSION_UNKNOWN)
160                                 printf(" %-25s |", "");
161                         else
162                                 printf(" %-25s |", hal_abi_version_str[ret]);
163                 }
164
165                 if (LSHAL_FLAG_BACKEND_NAME & flags) {
166                         strncpy(str, "", BUFF_MAX - 1);
167
168                         ret = hal_common_get_backend_name(module, str, BUFF_MAX - 1);
169                         if (ret < 0)
170                                 printf(" %-25s |", "");
171                         else
172                                 printf(" %-25s |", str);
173                 }
174
175                 if (LSHAL_FLAG_BACKEND_VENDOR_NAME & flags) {
176                         strncpy(str, "", BUFF_MAX - 1);
177
178                         ret = hal_common_get_backend_vendor(module, str, BUFF_MAX - 1);
179                         if (ret < 0)
180                                 printf(" %-25s |", "");
181                         else
182                                 printf(" %-25s |", str);
183                 }
184
185                 printf("\n");
186         }
187
188         lshal_print_border();
189         printf(" %-38s | %-2d | %-55s | %-10s | %15d | %-45s | %-25s | %-25s | %-25s |\n",
190                         "TOTAL",
191                         HAL_MODULE_END - 1,
192                         "",
193                         "",
194                         backend_verification_count,
195                         "",
196                         "",
197                         "",
198                         "");
199         lshal_print_border();
200 }
201
202 static void lshal_handle_signal(int signal) {}
203
204 static void lshal(void)
205 {
206         u_int32_t flags = (LSHAL_FLAG_BACKEND_VERIFICATION
207                         | LSHAL_FLAG_BACKEND_LIB_NAME
208                         | LSHAL_FLAG_BACKEND_SYMBOL_NAME
209                         | LSHAL_FLAG_BACKEND_NAME
210                         | LSHAL_FLAG_BACKEND_VENDOR_NAME
211                         | LSHAL_FLAG_BACKEND_USAGE_COUNT
212                         | LSHAL_FLAG_BACKEND_MODULE_NAME
213                         | LSHAL_FLAG_BACKEND_ABI_VERSION);
214
215         lshal_print_hal_backend_info(flags);
216 }
217
218 int main(int argc, char *argv[])
219 {
220         /* Register signal handler for freeing resources */
221         signal(SIGINT, lshal_handle_signal);
222
223         lshal();
224
225         return 0;
226 }