Add a new wifi error type for association failure
[platform/core/api/wifi-manager.git] / tools / manager-tool / wifi_mgr_tool.c
1 /*
2  * Copyright (c) 2016 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 <stdio.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <string.h>
21 #include <errno.h>
22
23 #include <glib.h>
24 #include <glib-object.h>
25
26 #include "wifi_mgr_tool.h"
27 #include "wifi_mgr_menu.h"
28 #include "wifi_mgr_public.h"
29 #include "wifi_mgr_extension.h"
30
31 wifi_manager_h g_wifi_h = NULL;
32 wifi_manager_device_state_e g_device_state = WIFI_MANAGER_DEVICE_STATE_DEACTIVATED;
33 static wifi_manager_h g_wifi2_h = NULL;
34 static int cs_tid = 0;
35
36 static char g_ifname[MENU_DATA_SIZE] = "wlan1";
37
38 extern struct menu_data menu_public[];
39 extern struct menu_data menu_extension[];
40
41 static struct menu_data menu_main[] = {
42         { "1", LOG_LIGHTBLUE "[Public]" LOG_END, menu_public, NULL, NULL},
43         { "2", LOG_LIGHTMAGENTA "[Extension]" LOG_END, menu_extension, NULL, NULL},
44         { NULL, NULL, NULL, NULL, NULL },
45 };
46
47 static void __test_device_state_cb(wifi_manager_device_state_e state, void *user_data)
48 {
49         wifi_manager_h wifi_h = NULL;
50         char *if_name = NULL;
51
52         g_device_state = state;
53
54         wifi_h = GPOINTER_TO_INT(user_data) == 1 ? g_wifi_h : g_wifi2_h;
55         wifi_manager_get_network_interface_name(wifi_h, &if_name);
56
57         msg("[%d:%s] Device state changed callback [%s]",
58                 GPOINTER_TO_INT(user_data), if_name,
59                 test_wifi_mgr_device_state_to_string(g_device_state));
60
61         FREE(if_name);
62 }
63
64 static void __test_scan_changed_cb(wifi_manager_scan_state_e state, void* user_data)
65 {
66         wifi_manager_h wifi_h = NULL;
67         char *if_name = NULL;
68
69         wifi_h = GPOINTER_TO_INT(user_data) == 1 ? g_wifi_h : g_wifi2_h;
70         wifi_manager_get_network_interface_name(wifi_h, &if_name);
71
72         msg("[%d:%s] Scan changed callback [%s]",
73                 GPOINTER_TO_INT(user_data), if_name,
74                 test_wifi_mgr_scan_state_to_string(state));
75
76         FREE(if_name);
77 }
78
79 static void __test_connection_state_cb(wifi_manager_connection_state_e state,
80                         wifi_manager_ap_h ap, void* user_data)
81 {
82         int ret = WIFI_MANAGER_ERROR_NONE;
83         wifi_manager_h wifi_h = NULL;
84         char *if_name = NULL;
85         char *ap_name = NULL;
86
87         wifi_h = GPOINTER_TO_INT(user_data) == 1 ? g_wifi_h : g_wifi2_h;
88         wifi_manager_get_network_interface_name(wifi_h, &if_name);
89
90         ret = wifi_manager_ap_get_essid(ap, &ap_name);
91         if (ret != WIFI_MANAGER_ERROR_NONE) {
92                 msg(LOG_RED "Failed to get AP name [%s]" LOG_END,
93                         test_wifi_mgr_convert_error_to_string(ret));
94                 FREE(if_name);
95                 return;
96         }
97
98         msg("[%d:%s] Connection state changed callback " LOG_CYAN "[%s]" LOG_END "[%s]",
99                 GPOINTER_TO_INT(user_data), if_name, ap_name,
100                 test_wifi_mgr_conn_state_to_string(state));
101
102         FREE(ap_name);
103         FREE(if_name);
104 }
105
106 static void __test_rssi_level_cb(wifi_manager_rssi_level_e rssi_level, void* user_data)
107 {
108         wifi_manager_h wifi_h = NULL;
109         char *if_name = NULL;
110
111         wifi_h = GPOINTER_TO_INT(user_data) == 1 ? g_wifi_h : g_wifi2_h;
112         wifi_manager_get_network_interface_name(wifi_h, &if_name);
113
114         msg("[%d:%s] RSSI level changed callback [%d]",
115                 GPOINTER_TO_INT(user_data), if_name, rssi_level);
116
117         FREE(if_name);
118 }
119
120 static void __test_bg_scan_completed_cb(wifi_manager_error_e error_code, void* user_data)
121 {
122         wifi_manager_h wifi_h = NULL;
123         char *if_name = NULL;
124
125         wifi_h = GPOINTER_TO_INT(user_data) == 1 ? g_wifi_h : g_wifi2_h;
126         wifi_manager_get_network_interface_name(wifi_h, &if_name);
127
128         msg("[%d:%s] Background scan completed [%s]",
129                 GPOINTER_TO_INT(user_data), if_name,
130                 test_wifi_mgr_convert_error_to_string(error_code));
131
132         FREE(if_name);
133 }
134
135 static void __test_ip_conflict_cb(char *mac, wifi_manager_ip_conflict_state_e state, void *user_data)
136 {
137         wifi_manager_h wifi_h = NULL;
138         char *if_name = NULL;
139
140         wifi_h = GPOINTER_TO_INT(user_data) == 1 ? g_wifi_h : g_wifi2_h;
141         wifi_manager_get_network_interface_name(wifi_h, &if_name);
142
143         msgn("[%d:%s] Ip conflict", GPOINTER_TO_INT(user_data), if_name);
144
145         if (state == WIFI_MANAGER_IP_CONFLICT_STATE_CONFLICT_DETECTED)
146                 msg(" detected [%s]", mac);
147         else if (state == WIFI_MANAGER_IP_CONFLICT_STATE_CONFLICT_NOT_DETECTED)
148                 msg(" removed");
149         else
150                 msg(" state unknown");
151
152         FREE(if_name);
153 }
154
155 static void __test_get_wifi_module_state_cb(wifi_manager_module_state_e state, void *user_data)
156 {
157         wifi_manager_h wifi_h = NULL;
158         char *if_name = NULL;
159
160         wifi_h = GPOINTER_TO_INT(user_data) == 1 ? g_wifi_h : g_wifi2_h;
161         wifi_manager_get_network_interface_name(wifi_h, &if_name);
162
163         msgn("[%d%s] Wi-Fi module state changed callback [%s]",
164                 GPOINTER_TO_INT(user_data), if_name,
165                 test_wifi_mgr_module_state_to_string(state));
166
167         FREE(if_name);
168 }
169
170 static void __test_tdls_state_cb(wifi_manager_tdls_state_e state, char *peer_mac_add, void *user_data)
171 {
172         wifi_manager_h wifi_h = NULL;
173         char *if_name = NULL;
174
175         wifi_h = GPOINTER_TO_INT(user_data) == 1 ? g_wifi_h : g_wifi2_h;
176         wifi_manager_get_network_interface_name(wifi_h, &if_name);
177
178         msgn("[%d:%s] TDLS state changed callback", GPOINTER_TO_INT(user_data), if_name);
179
180         if (state == WIFI_MANAGER_TDLS_STATE_CONNECTED)
181                 msg("TDLS [Connected] Peer MAC address [%s]", peer_mac_add);
182         else
183                 msg("TDLS [Disconnected] Peer MAC address [%s]", peer_mac_add);
184
185         FREE(if_name);
186 }
187
188 static void __test_tdls_discover_cb(wifi_manager_tdls_discovery_state_e state,
189                         char *peer_mac_add, void *user_data)
190 {
191         wifi_manager_h wifi_h = NULL;
192         char *if_name = NULL;
193
194         wifi_h = GPOINTER_TO_INT(user_data) == 1 ? g_wifi_h : g_wifi2_h;
195         wifi_manager_get_network_interface_name(wifi_h, &if_name);
196
197         msgn("[%d:%s] TDLS discover callback ", GPOINTER_TO_INT(user_data), if_name);
198
199         msgn("Peer MAC address [%s] state [%d] ", peer_mac_add, state);
200         if (state == WIFI_MANAGER_TDLS_DISCOVERY_STATE_ONGOING)
201                 msg("Discovery is ongoing");
202         else
203                 msg("Discovery is finished");
204
205         FREE(if_name);
206 }
207
208 static void __test_dpp_event_cb(wifi_manager_dpp_event_e event,
209                 wifi_manager_dpp_state_e state, wifi_manager_dpp_h handle, void *user_data)
210 {
211         wifi_manager_h wifi_h = NULL;
212         char *if_name = NULL;
213
214         wifi_h = GPOINTER_TO_INT(user_data) == 1 ? g_wifi_h : g_wifi2_h;
215         wifi_manager_get_network_interface_name(wifi_h, &if_name);
216
217         msgn("[%d:%s] DPP event callback ", GPOINTER_TO_INT(user_data), if_name);
218
219         msg("event [%s] ", test_wifi_mgr_dpp_event_to_string(event));
220         if (event == WIFI_MANAGER_DPP_EVENT_STATE_CHANGED)
221                 msg("state [%s]", test_wifi_mgr_dpp_state_to_string(state));
222
223         FREE(if_name);
224 }
225
226 static int __test_init_cbs(wifi_manager_h g_wifi_h, int ret, int num)
227 {
228         char *if_name = NULL;
229
230         if (ret == WIFI_MANAGER_ERROR_NONE) {
231                 wifi_manager_set_device_state_changed_cb(g_wifi_h, __test_device_state_cb, GINT_TO_POINTER(num));
232                 wifi_manager_set_scan_state_changed_cb(g_wifi_h, __test_scan_changed_cb, GINT_TO_POINTER(num));
233                 wifi_manager_set_connection_state_changed_cb(g_wifi_h, __test_connection_state_cb, GINT_TO_POINTER(num));
234                 wifi_manager_set_rssi_level_changed_cb(g_wifi_h, __test_rssi_level_cb, GINT_TO_POINTER(num));
235                 wifi_manager_set_background_scan_cb(g_wifi_h, __test_bg_scan_completed_cb, GINT_TO_POINTER(num));
236                 wifi_manager_set_ip_conflict_cb(g_wifi_h, __test_ip_conflict_cb, GINT_TO_POINTER(num));
237                 wifi_manager_set_module_state_changed_cb(g_wifi_h, __test_get_wifi_module_state_cb, GINT_TO_POINTER(num));
238
239                 wifi_manager_tdls_set_state_changed_cb(g_wifi_h, __test_tdls_state_cb, GINT_TO_POINTER(num));
240                 wifi_manager_tdls_set_discovered_cb(g_wifi_h, __test_tdls_discover_cb, GINT_TO_POINTER(num));
241
242                 wifi_manager_dpp_set_event_cb(g_wifi_h, __test_dpp_event_cb, GINT_TO_POINTER(num));
243
244                 wifi_manager_get_network_interface_name(g_wifi_h, &if_name);
245                 msg(LOG_GREEN "[%d:%s] Success to initialize wifi handle [%p]" LOG_END, num, if_name, g_wifi_h);
246                 FREE(if_name);
247
248                 return 1;
249         } else {
250                 msg("[%d] Failed to initialize wifi handle " LOG_RED "[%s]" LOG_END,
251                         num, test_wifi_mgr_convert_error_to_string(ret));
252
253                 return 0;
254         }
255 }
256
257 static int __test_init_dev_state_out(void)
258 {
259         char *if_name = NULL;
260         int ret = WIFI_MANAGER_ERROR_NONE;
261         bool state = FALSE;
262
263         ret = wifi_manager_is_activated(g_wifi_h, &state);
264         wifi_manager_get_network_interface_name(g_wifi_h, &if_name);
265         if (ret == WIFI_MANAGER_ERROR_NONE) {
266                 g_device_state = state;
267                 msg("[1:%s] Wi-Fi device state " LOG_CYAN "[%s]" LOG_END, if_name,
268                                 test_wifi_mgr_device_state_to_string(g_device_state));
269         } else {
270                 msg("[1] Failed to get Wi-Fi device state " LOG_RED "[%s]" LOG_END,
271                                 test_wifi_mgr_convert_error_to_string(ret));
272         }
273
274         FREE(if_name);
275
276         return ret;
277 }
278
279 static void __test_deinit_all(void)
280 {
281         if (g_wifi_h) {
282                 wifi_manager_deinitialize(g_wifi_h);
283                 g_wifi_h = NULL;
284         }
285
286         if (g_wifi2_h) {
287                 wifi_manager_deinitialize(g_wifi2_h);
288                 g_wifi2_h = NULL;
289         }
290 }
291
292 static int _test_wifi_mgr_initialize(MManager *mm, struct menu_data *menu)
293 {
294         int ret = WIFI_MANAGER_ERROR_NONE;
295
296         __test_deinit_all();
297
298         ret = wifi_manager_initialize(&g_wifi_h);
299         if (!__test_init_cbs(g_wifi_h, ret, 1))
300                 return ret;
301
302         ret = wifi_manager_initialize(&g_wifi2_h);
303         if (!__test_init_cbs(g_wifi2_h, ret, 2))
304                 return ret;
305
306         ret = __test_init_dev_state_out();
307
308         return ret;
309 }
310
311 static int _test_wifi_mgr_initialize_cs(MManager *mm, struct menu_data *menu)
312 {
313         int ret = WIFI_MANAGER_ERROR_NONE;
314
315         cs_tid = get_tid();
316
317         __test_deinit_all();
318
319         ret = wifi_manager_initialize_cs(cs_tid, &g_wifi_h);
320         __test_init_cbs(g_wifi_h, ret, 1);
321
322         ret = wifi_manager_initialize_cs(cs_tid, &g_wifi2_h);
323         __test_init_cbs(g_wifi2_h, ret, 2);
324
325         if (ret == WIFI_MANAGER_ERROR_NONE)
326                 ret = __test_init_dev_state_out();
327
328         return ret;
329 }
330
331 static int _test_wifi_mgr_initialize_with_ifname(MManager *mm, struct menu_data *menu)
332 {
333         int ret = WIFI_MANAGER_ERROR_NONE;
334
335         __test_deinit_all();
336
337         ret = wifi_manager_initialize_with_interface_name(&g_wifi_h, g_ifname);
338         if (!__test_init_cbs(g_wifi_h, ret, 1))
339                 return ret;
340
341         ret = wifi_manager_initialize_with_interface_name(&g_wifi2_h, g_ifname);
342         if (!__test_init_cbs(g_wifi2_h, ret, 2))
343                 return ret;
344
345         ret = __test_init_dev_state_out();
346
347         return ret;
348 }
349
350 static int _test_wifi_mgr_deinitialize(void)
351 {
352         int ret = WIFI_MANAGER_ERROR_NONE;
353         char *if_name = NULL;
354
355         if (g_wifi_h) {
356                 if (cs_tid)
357                         ret = wifi_manager_deinitialize_cs(cs_tid, g_wifi_h);
358                 else
359                         ret = wifi_manager_deinitialize(g_wifi_h);
360
361                 if (ret == WIFI_MANAGER_ERROR_NONE) {
362                         wifi_manager_get_network_interface_name(g_wifi_h, &if_name);
363                         msg(LOG_GREEN "[1:%s] Succeeded to deinitialize wifi handle" LOG_END, if_name);
364                         FREE(if_name);
365                 } else
366                         msg("[1] Failed to deinitialize wifi handle " LOG_RED "[%s]" LOG_END,
367                                 test_wifi_mgr_convert_error_to_string(ret));
368         }
369
370         if (g_wifi2_h) {
371                 if (cs_tid)
372                         ret = wifi_manager_deinitialize_cs(cs_tid, g_wifi2_h);
373                 else
374                         ret = wifi_manager_deinitialize(g_wifi2_h);
375
376                 if (ret == WIFI_MANAGER_ERROR_NONE) {
377                         wifi_manager_get_network_interface_name(g_wifi2_h, &if_name);
378                         msg(LOG_GREEN "[2:%s] Succeeded to deinitialize wifi handle" LOG_END, if_name);
379                         FREE(if_name);
380                 } else
381                         msg("[2] Failed to deinitialize wifi handle " LOG_RED "[%s]" LOG_END,
382                                 test_wifi_mgr_convert_error_to_string(ret));
383         }
384
385         cs_tid = 0;
386
387         return ret;
388 }
389
390 static gboolean _test_wifi_mgr_create_init_menu(struct menu_data init_menu[5])
391 {
392         init_menu[0].key = "0";
393         init_menu[0].title = LOG_LIGHTGREEN "[Interface Name]" LOG_END;
394         init_menu[0].sub_menu = NULL;
395         init_menu[0].callback = NULL;
396         init_menu[0].data = g_ifname;
397
398         init_menu[1].key = "1";
399         init_menu[1].title = LOG_YELLOW "Wifi Mgr Init" LOG_END;
400         init_menu[1].sub_menu = menu_main;
401         init_menu[1].callback = _test_wifi_mgr_initialize;
402         init_menu[1].data = NULL;
403
404         init_menu[2].key = "2";
405         init_menu[2].title = LOG_YELLOW "Wifi Mgr Init for C#" LOG_END;
406         init_menu[2].sub_menu = menu_main;
407         init_menu[2].callback = _test_wifi_mgr_initialize_cs;
408         init_menu[2].data = NULL;
409
410         init_menu[3].key = "3";
411         init_menu[3].title = LOG_YELLOW "Wifi Mgr Init with ifname" LOG_END;
412         init_menu[3].sub_menu = menu_main;
413         init_menu[3].callback = _test_wifi_mgr_initialize_with_ifname;
414         init_menu[3].data = NULL;
415
416         return TRUE;
417 }
418
419 bool test_wifi_mgr_compare_string(const char *str, const char *part)
420 {
421         int str_len = strlen(str);
422         int part_len = strlen(part);
423
424         if (strncmp(str, part, MAX(str_len, part_len)) == 0)
425                 return TRUE;
426         else
427                 return FALSE;
428 }
429
430 const char *test_wifi_mgr_autoscan_mode_to_string(wifi_manager_autoscan_mode_e state)
431 {
432         switch (state) {
433         case WIFI_MANAGER_AUTOSCAN_MODE_EXPONENTIAL:
434                 return "Exponential";
435         case WIFI_MANAGER_AUTOSCAN_MODE_PERIODIC:
436                 return "Periodic";
437         }
438
439         return "Unknown";
440 }
441
442 const char *test_wifi_mgr_ipconflict_state_to_string(wifi_manager_ip_conflict_state_e state)
443 {
444         switch (state) {
445         case WIFI_MANAGER_IP_CONFLICT_STATE_CONFLICT_NOT_DETECTED:
446                 return "Absent";
447         case WIFI_MANAGER_IP_CONFLICT_STATE_CONFLICT_DETECTED:
448                 return "Present";
449         default:
450                 break;
451         }
452
453         return "Unknown";
454 }
455
456 const char *test_wifi_mgr_device_state_to_string(wifi_manager_device_state_e state)
457 {
458         switch (state) {
459         case WIFI_MANAGER_DEVICE_STATE_DEACTIVATED:
460                 return "Deactivated";
461         case WIFI_MANAGER_DEVICE_STATE_ACTIVATED:
462                 return "Activated";
463         }
464
465         return "Unknown";
466 }
467
468 const char *test_wifi_mgr_module_state_to_string(wifi_manager_module_state_e state)
469 {
470         switch (state) {
471         case WIFI_MANAGER_MODULE_STATE_DETACHED:
472                 return "Detached";
473         case WIFI_MANAGER_MODULE_STATE_ATTACHED:
474                 return "Attached";
475         }
476
477         return "Unknown";
478 }
479
480 const char *test_wifi_mgr_scan_state_to_string(wifi_manager_scan_state_e state)
481 {
482         switch (state) {
483         case WIFI_MANAGER_SCAN_STATE_NOT_SCANNING:
484                 return "Not scanning";
485         case WIFI_MANAGER_SCAN_STATE_SCANNING:
486                 return "Scanning";
487         }
488
489         return "Unknown";
490 }
491
492 const char *test_wifi_mgr_eap_auth_type_to_string(wifi_manager_eap_auth_type_e type)
493 {
494         switch (type) {
495         case WIFI_MANAGER_EAP_AUTH_TYPE_NONE:
496                 return "None";
497         case WIFI_MANAGER_EAP_AUTH_TYPE_PAP:
498                 return "PAP";
499         case WIFI_MANAGER_EAP_AUTH_TYPE_MSCHAP:
500                 return "MSCHAP";
501         case WIFI_MANAGER_EAP_AUTH_TYPE_MSCHAPV2:
502                 return "MSCHAPv2";
503         case WIFI_MANAGER_EAP_AUTH_TYPE_GTC:
504                 return "GTC";
505         case WIFI_MANAGER_EAP_AUTH_TYPE_MD5:
506                 return "MD5";
507         }
508
509         return "Unknown";
510 }
511
512 const char *test_wifi_mgr_eap_type_to_string(wifi_manager_eap_type_e type)
513 {
514         switch (type) {
515         case WIFI_MANAGER_EAP_TYPE_PEAP:
516                 return "PEAP";
517         case WIFI_MANAGER_EAP_TYPE_TLS:
518                 return "TLS";
519         case WIFI_MANAGER_EAP_TYPE_TTLS:
520                 return "TTLS";
521         case WIFI_MANAGER_EAP_TYPE_SIM:
522                 return "SIM";
523         case WIFI_MANAGER_EAP_TYPE_AKA:
524                 return "AKA";
525         case WIFI_MANAGER_EAP_TYPE_AKA_PRIME:
526                 return "AKA'";
527         case WIFI_MANAGER_EAP_TYPE_FAST:
528                 return "FAST";
529         case WIFI_MANAGER_EAP_TYPE_PWD:
530                 return "PWD";
531         }
532
533         return "Unknown";
534 }
535
536 const char *test_wifi_mgr_encryption_type_to_string(wifi_manager_encryption_type_e type)
537 {
538         switch (type) {
539         case WIFI_MANAGER_ENCRYPTION_TYPE_NONE:
540                 return "None";
541         case WIFI_MANAGER_ENCRYPTION_TYPE_WEP:
542                 return "WEP";
543         case WIFI_MANAGER_ENCRYPTION_TYPE_TKIP:
544                 return "TKIP";
545         case WIFI_MANAGER_ENCRYPTION_TYPE_AES:
546                 return "AES";
547         case WIFI_MANAGER_ENCRYPTION_TYPE_TKIP_AES_MIXED:
548                 return "TKIP AES MIXED";
549         }
550
551         return "Unknown";
552 }
553
554 const char *test_wifi_mgr_dns_config_type_to_string(wifi_manager_dns_config_type_e type)
555 {
556         switch (type) {
557         case WIFI_MANAGER_DNS_CONFIG_TYPE_NONE:
558                 return "None";
559         case WIFI_MANAGER_DNS_CONFIG_TYPE_STATIC:
560                 return "Static";
561         case WIFI_MANAGER_DNS_CONFIG_TYPE_DYNAMIC:
562                 return "Dynamic";
563         }
564
565         return "Unknown";
566 }
567
568 const char *test_wifi_mgr_proxy_type_to_string(wifi_manager_proxy_type_e type)
569 {
570         switch (type) {
571         case WIFI_MANAGER_PROXY_TYPE_DIRECT:
572                 return "Direct";
573         case WIFI_MANAGER_PROXY_TYPE_AUTO:
574                 return "Auto";
575         case WIFI_MANAGER_PROXY_TYPE_MANUAL:
576                 return "Manual";
577         }
578
579         return "Unknown";
580 }
581
582 const char *test_wifi_mgr_ip_config_type_to_string(wifi_manager_ip_config_type_e type)
583 {
584         switch (type) {
585         case WIFI_MANAGER_IP_CONFIG_TYPE_NONE:
586                 return "None";
587         case WIFI_MANAGER_IP_CONFIG_TYPE_STATIC:
588                 return "Static";
589         case WIFI_MANAGER_IP_CONFIG_TYPE_DYNAMIC:
590                 return "Dynamic";
591         case WIFI_MANAGER_IP_CONFIG_TYPE_AUTO:
592                 return "Auto";
593         case WIFI_MANAGER_IP_CONFIG_TYPE_FIXED:
594                 return "Fixed";
595         }
596
597         return "Unknown";
598 }
599
600 const char *test_wifi_mgr_sec_type_to_string(wifi_manager_security_type_e type)
601 {
602         switch (type) {
603         case WIFI_MANAGER_SECURITY_TYPE_NONE:
604                 return "None";
605         case WIFI_MANAGER_SECURITY_TYPE_WEP:
606                 return "WEP";
607         case WIFI_MANAGER_SECURITY_TYPE_WPA_PSK:
608                 return "WPA";
609         case WIFI_MANAGER_SECURITY_TYPE_WPA2_PSK:
610                 return "WPA2";
611         case WIFI_MANAGER_SECURITY_TYPE_EAP:
612                 return "EAP";
613         case WIFI_MANAGER_SECURITY_TYPE_WPA_FT_PSK:
614                 return "FT_PSK";
615         case WIFI_MANAGER_SECURITY_TYPE_SAE:
616                 return "WPA3";
617         case WIFI_MANAGER_SECURITY_TYPE_OWE:
618                 return "OWE";
619         case WIFI_MANAGER_SECURITY_TYPE_DPP:
620                 return "DPP";
621         }
622
623         return "Unknown";
624 }
625
626 const char *test_wifi_mgr_conn_state_to_string(wifi_manager_connection_state_e state)
627 {
628         switch (state) {
629         case WIFI_MANAGER_CONNECTION_STATE_FAILURE:
630                 return "Failure";
631         case WIFI_MANAGER_CONNECTION_STATE_DISCONNECTED:
632                 return "Disconnected";
633         case WIFI_MANAGER_CONNECTION_STATE_ASSOCIATION:
634                 return "Association";
635         case WIFI_MANAGER_CONNECTION_STATE_CONNECTED:
636                 return "Connected";
637         case WIFI_MANAGER_CONNECTION_STATE_CONFIGURATION:
638                 return "Configuration";
639         }
640
641         return "Unknown";
642 }
643
644 const char *test_wifi_mgr_convert_error_to_string(wifi_manager_error_e err_type)
645 {
646         switch (err_type) {
647         case WIFI_MANAGER_ERROR_NONE:
648                 return "NONE";
649         case WIFI_MANAGER_ERROR_INVALID_PARAMETER:
650                 return "INVALID_PARAMETER";
651         case WIFI_MANAGER_ERROR_OUT_OF_MEMORY:
652                 return "OUT_OF_MEMORY";
653         case WIFI_MANAGER_ERROR_INVALID_OPERATION:
654                 return "INVALID_OPERATION";
655         case WIFI_MANAGER_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED:
656                 return "ADDRESS_FAMILY_NOT_SUPPORTED";
657         case WIFI_MANAGER_ERROR_OPERATION_FAILED:
658                 return "OPERATION_FAILED";
659         case WIFI_MANAGER_ERROR_NO_CONNECTION:
660                 return "NO_CONNECTION";
661         case WIFI_MANAGER_ERROR_NOW_IN_PROGRESS:
662                 return "NOW_IN_PROGRESS";
663         case WIFI_MANAGER_ERROR_ALREADY_EXISTS:
664                 return "ALREADY_EXISTS";
665         case WIFI_MANAGER_ERROR_OPERATION_ABORTED:
666                 return "OPERATION_ABORTED";
667         case WIFI_MANAGER_ERROR_DHCP_FAILED:
668                 return "DHCP_FAILED";
669         case WIFI_MANAGER_ERROR_INVALID_KEY:
670                 return "INVALID_KEY";
671         case WIFI_MANAGER_ERROR_NO_REPLY:
672                 return "NO_REPLY";
673         case WIFI_MANAGER_ERROR_SECURITY_RESTRICTED:
674                 return "SECURITY_RESTRICTED";
675         case WIFI_MANAGER_ERROR_ALREADY_INITIALIZED:
676                 return "ALREADY_INITIALIZED";
677         case WIFI_MANAGER_ERROR_OUT_OF_RANGE:
678                 return "OUT_OF_RANGE";
679         case WIFI_MANAGER_ERROR_CONNECT_FAILED:
680                 return "CONNECT_FAILED";
681         case WIFI_MANAGER_ERROR_LOGIN_FAILED:
682                 return "LOGIN_FAILED";
683         case WIFI_MANAGER_ERROR_AUTHENTICATION_FAILED:
684                 return "AUTH_FAILED";
685         case WIFI_MANAGER_ERROR_ASSOCIATION_FAILED:
686                 return "ASSOC_FAILED";
687         case WIFI_MANAGER_ERROR_PIN_MISSING:
688                 return "PIN_MISSING";
689         case WIFI_MANAGER_ERROR_WPS_OVERLAP:
690                 return "WPS_OVERLAP";
691         case WIFI_MANAGER_ERROR_WPS_TIMEOUT:
692                 return "WPS_TIMEOUT";
693         case WIFI_MANAGER_ERROR_WPS_WEP_PROHIBITED:
694                 return "WPS_WEP_PROHIBITED";
695         case WIFI_MANAGER_ERROR_PERMISSION_DENIED:
696                 return "PERMISSION_DENIED";
697         case WIFI_MANAGER_ERROR_OFFLINE:
698                 return "OFFLINE";
699         case WIFI_MANAGER_ERROR_INVALID_GATEWAY:
700                 return "INVALID_GATEWAY";
701         case WIFI_MANAGER_ERROR_NOT_SUPPORTED:
702                 return "NOT_SUPPORTED";
703         case WIFI_MANAGER_ERROR_NOT_INITIALIZED:
704                 return "NOT_INITIALIZED";
705         default:
706                 return "UNKNOWN";
707         }
708 }
709
710 const char *test_wifi_mgr_dpp_state_to_string(wifi_manager_dpp_state_e state)
711 {
712         switch (state) {
713         case WIFI_MANAGER_DPP_STATE_NONE:
714                 return "NONE";
715         case WIFI_MANAGER_DPP_STATE_URI_REQUESTED:
716                 return "URI_REQUESTED";
717         case WIFI_MANAGER_DPP_STATE_AWAITING:
718                 return "AWAITING";
719         case WIFI_MANAGER_DPP_STATE_BOOTSTRAPPED:
720                 return "BOOTSTRAPPED";
721         case WIFI_MANAGER_DPP_STATE_AUTHENTICATING:
722                 return "AUTHENTICATING";
723         case WIFI_MANAGER_DPP_STATE_AUTHENTICATED:
724                 return "AUTHENTICATED";
725         case WIFI_MANAGER_DPP_STATE_CONFIGURING:
726                 return "CONFIGURING";
727         case WIFI_MANAGER_DPP_STATE_CONFIGURED:
728                 return "CONFIGURED";
729         default:
730                 return "UNKNOWN";
731         }
732 }
733
734 const char *test_wifi_mgr_dpp_event_to_string(wifi_manager_dpp_event_e event)
735 {
736         switch (event) {
737         case WIFI_MANAGER_DPP_EVENT_REQUEST_FAILED:
738                 return "REQUEST_FAILED";
739         case WIFI_MANAGER_DPP_EVENT_URI_GENERATED:
740                 return "URI_GENERATED";
741         case WIFI_MANAGER_DPP_EVENT_SCAN_PEER_QR:
742                 return "SCAN_PEER_QR";
743         case WIFI_MANAGER_DPP_EVENT_BUSY:
744                 return "BUSY";
745         case WIFI_MANAGER_DPP_EVENT_AUTH_FAILED:
746                 return "AUTH_FAILED";
747         case WIFI_MANAGER_DPP_EVENT_NOT_COMPATIBLE:
748                 return "NOT_COMPATIBLE";
749         case WIFI_MANAGER_DPP_EVENT_CONF_FAILED:
750                 return "CONF FAILED";
751         case WIFI_MANAGER_DPP_EVENT_TIMEOUT:
752                 return "TIMEOUT";
753         case WIFI_MANAGER_DPP_EVENT_STATE_CHANGED:
754                 return "STATE_CHANGED";
755         default:
756                 return "UNKNOWN";
757         }
758 }
759
760 const char *test_wifi_mgr_dpp_device_role_to_string(wifi_manager_dpp_role_e role)
761 {
762         switch (role) {
763         case WIFI_MANAGER_DPP_ROLE_CONFIGURATOR:
764                 return "CONFIGURATOR";
765         case WIFI_MANAGER_DPP_ROLE_ENROLLEE:
766                 return "ENROLLEE";
767         default:
768                 return "UNKNOWN";
769         }
770 }
771
772 const char *test_wifi_mgr_dpp_network_role_to_string(wifi_manager_dpp_network_role_e role)
773 {
774         switch (role) {
775         case WIFI_MANAGER_DPP_NETWORK_ROLE_AP:
776                 return "AP";
777         case WIFI_MANAGER_DPP_NETWORK_ROLE_STA:
778                 return "STA";
779         default:
780                 return "UNKNOWN";
781         }
782 }
783
784 const char *test_wifi_mgr_dpp_akm_to_string(wifi_manager_dpp_akm_e akm)
785 {
786         switch (akm) {
787         case WIFI_MANAGER_DPP_AKM_PSK:
788                 return "PSK";
789         case WIFI_MANAGER_DPP_AKM_SAE:
790                 return "SAE";
791         case WIFI_MANAGER_DPP_AKM_DPP:
792                 return "DPP";
793         default:
794                 return "UNKNOWN";
795         }
796 }
797
798 int main(int arg, char **argv)
799 {
800         GMainLoop *mainloop = NULL;
801         GIOChannel *channel = g_io_channel_unix_new(STDIN_FILENO);
802         MManager *manager;
803         struct menu_data init_menu[5] = { {NULL, NULL, NULL, NULL, NULL} };
804
805 #if !GLIB_CHECK_VERSION(2, 35, 0)
806         g_type_init();
807 #endif
808         mainloop = g_main_loop_new(NULL, FALSE);
809
810         msg("");
811         msg(LOG_GREEN "* Wifi Manager Tool " LOG_END);
812         msg("* Build On: %s  %s", __DATE__, __TIME__);
813
814         if (_test_wifi_mgr_create_init_menu(init_menu) == FALSE)
815                 goto OUT;
816
817         manager = menu_manager_new(init_menu, mainloop);
818         if (!manager)
819                 goto OUT;
820
821         menu_manager_run(manager);
822
823         g_io_add_watch(channel, (G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL),
824                         on_menu_manager_keyboard, manager);
825         g_main_loop_run(mainloop);
826
827 OUT:
828         _test_wifi_mgr_deinitialize();
829         g_main_loop_unref(mainloop);
830         msg("******* Bye bye *******");
831
832         return 0;
833 }