347057b197b2287be25906894b932330adf1d765
[platform/core/api/wifi-manager.git] / tools / manager-test / wman_test_main.c
1 /*
2  * Copyright (c) 2012-2020 Samsung Electronics Co., Ltd All Rights Reserved
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 #include <glib.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <wifi-manager.h>
21 #include <wifi-manager-extension.h>
22
23 #include "wman_test_common.h"
24 #include "wman_test_ap.h"
25 #include "wman_test_misc.h"
26 #include "wman_test_config.h"
27 #include "wman_test_extension.h"
28
29 #define LOG_RED "\033[0;31m"
30 #define LOG_GREEN "\033[0;32m"
31 #define LOG_BROWN "\033[0;33m"
32 #define LOG_BLUE "\033[0;34m"
33 #define LOG_END "\033[0;m"
34
35 static wifi_manager_h wifi = NULL;
36 static wifi_manager_h wifi2 = NULL;
37
38 static void __test_device_state_callback(wifi_manager_device_state_e state, void* user_data)
39 {
40         printf("[%s] Device state changed callback", (char *)user_data);
41
42         if (state == WIFI_MANAGER_DEVICE_STATE_ACTIVATED)
43                 printf(", state : Activated\n");
44         else
45                 printf(", state : Deactivated\n");
46 }
47
48 static void __test_scan_changed_callback(wifi_manager_scan_state_e state, void* user_data)
49 {
50         printf("Scan changed, scan state : %d\n", state);
51 }
52
53 static void __test_bg_scan_completed_callback(wifi_manager_error_e error_code, void* user_data)
54 {
55         printf("[%s] Background Scan Completed, error code : %s\n",
56                 (char *)user_data, wman_test_strerror(error_code));
57 }
58
59 static void __test_ip_conflict_callback(char *mac, wifi_manager_ip_conflict_state_e state, void *user_data)
60 {
61         if (state == WIFI_MANAGER_IP_CONFLICT_STATE_CONFLICT_DETECTED)
62                 printf("[%s] Ip conflict detected : %s\n", (char *)user_data, mac);
63         else if (state == WIFI_MANAGER_IP_CONFLICT_STATE_CONFLICT_NOT_DETECTED)
64                 printf("Ip conflict removed\n");
65         else
66                 printf("Ip conflict state unknown\n");
67 }
68
69 static void __test_connection_state_callback(wifi_manager_connection_state_e state, wifi_manager_ap_h ap, void* user_data)
70 {
71         int rv = 0;
72         char *ap_name = NULL;
73
74         printf("[%s] Connection state changed callback", (char *)user_data);
75
76         switch (state) {
77         case WIFI_MANAGER_CONNECTION_STATE_CONNECTED:
78                 printf(", state : Connected");
79                 break;
80         case WIFI_MANAGER_CONNECTION_STATE_ASSOCIATION:
81                 printf(", state : Association");
82                 break;
83         case WIFI_MANAGER_CONNECTION_STATE_CONFIGURATION:
84                 printf(", state : Configuration");
85                 break;
86         case WIFI_MANAGER_CONNECTION_STATE_DISCONNECTED:
87                 printf(", state : Disconnected");
88                 break;
89         default:
90                 printf(", state : Unknown");
91         }
92
93         rv = wifi_manager_ap_get_essid(ap, &ap_name);
94         if (rv != WIFI_MANAGER_ERROR_NONE)
95                 printf(", Fail to get AP name [%s]\n", wman_test_strerror(rv));
96         else {
97                 printf(", AP name : %s\n", ap_name);
98                 free(ap_name);
99         }
100 }
101
102 static void __test_rssi_level_callback(wifi_manager_rssi_level_e rssi_level, void* user_data)
103 {
104         printf("[%s] RSSI level changed callback, level = %d\n", (char *)user_data, rssi_level);
105 }
106
107 static void __test_tdls_discover_callback(wifi_manager_tdls_discovery_state_e state, char *peer_mac_add, void *user_data)
108 {
109         printf("[%s] TDLS discover callback", (char *)user_data);
110
111         printf(", Peer Mac Address [%s], state :%d\n", peer_mac_add, state);
112         if (state == WIFI_MANAGER_TDLS_DISCOVERY_STATE_ONGOING)
113                 printf(", Discovery is ongoing");
114         else
115                 printf(", Discovery is finished");
116 }
117
118 static void __test_get_wifi_module_state_callback(wifi_manager_module_state_e state, void *user_data)
119 {
120         printf("Wi-Fi Module State Changed callback : %d", state);
121
122         if (state == WIFI_MANAGER_MODULE_STATE_ATTACHED)
123                 printf(", Wi-Fi Module Attached\n");
124         else
125                 printf(", Wi-Fi Module Detached\n");
126 }
127
128 static void __test_tdls_state_callback(wifi_manager_tdls_state_e state, char *peer_mac_add, void *user_data)
129 {
130         printf("[%s] TDLS state changed callback", (char *)user_data);
131
132         if (state == WIFI_MANAGER_TDLS_STATE_CONNECTED)
133                 printf(", state : TDLS Connected, Peer Mac Address [%s]\n", peer_mac_add);
134         else
135                 printf(", state : TDLS Disconnected, Peer Mac Address [%s]\n", peer_mac_add);
136 }
137
138 static void __test_roaming_state_changed_callback(wifi_manager_roam_e state,
139                                                         char *cur_bssid, char *dst_bssid, void *user_data)
140 {
141         printf("[%s] Wi-Fi Roaming state changed callback", (char *)user_data);
142
143         switch (state) {
144         case WIFI_MANAGER_ROAM_SCAN_REQUIRED:
145                 printf(", state : Scan Required");
146                 break;
147         case WIFI_MANAGER_ROAM_STARTED:
148                 printf(", state : Roaming Started");
149                 break;
150         case WIFI_MANAGER_ROAM_FAILURE:
151                 printf(", state : Romaing Failure");
152                 break;
153         case WIFI_MANAGER_ROAM_SUCCESS:
154                 printf(", state : Romaing Success");
155                 break;
156         case WIFI_MANAGER_ROAM_UNKNOWN:
157         /* fall through */
158         default:
159                 printf(", state : Unknown");
160         }
161
162         printf(", cur_bssid: %s, dst_bssid: %s\n", cur_bssid, dst_bssid);
163 }
164
165 static void __test_dhcp_changed_callback(wifi_manager_dhcp_state_e state, wifi_manager_error_e error_code, void *user_data)
166 {
167                 printf("[%s] DHCP state changed callback, state : %d, error : %s\n",
168                                 (char *)user_data, state, wman_test_strerror(error_code));
169 }
170
171 int wman_test_init(const char *ifname)
172 {
173         int rv;
174
175         if (wifi || wifi2) {
176                 printf("Already initialized\n");
177                 return -1;
178         }
179
180         if (!ifname)
181                 rv = wifi_manager_initialize(&wifi);
182         else
183                 rv = wifi_manager_initialize_with_interface_name(&wifi, ifname);
184
185         if (rv == WIFI_MANAGER_ERROR_NONE) {
186                 wifi_manager_set_device_state_changed_cb(wifi, __test_device_state_callback, "1");
187                 wifi_manager_set_scan_state_changed_cb(wifi, __test_scan_changed_callback, "1");
188                 wifi_manager_set_background_scan_cb(wifi, __test_bg_scan_completed_callback, "1");
189                 wifi_manager_set_ip_conflict_cb(wifi, __test_ip_conflict_callback, "1");
190                 wifi_manager_set_connection_state_changed_cb(wifi, __test_connection_state_callback, "1");
191                 wifi_manager_set_rssi_level_changed_cb(wifi, __test_rssi_level_callback, "1");
192                 wifi_manager_tdls_set_state_changed_cb(wifi, __test_tdls_state_callback, "1");
193                 wifi_manager_tdls_set_discovered_cb(wifi, __test_tdls_discover_callback, "1");
194                 wifi_manager_set_module_state_changed_cb(wifi, __test_get_wifi_module_state_callback, "1");
195                 wifi_manager_set_roaming_cb(wifi, __test_roaming_state_changed_callback, "1");
196                 wifi_manager_set_dhcp_state_changed_cb(wifi, __test_dhcp_changed_callback, "1");
197
198         } else {
199                 printf("[1] Wifi init failed [%s]\n", wman_test_strerror(rv));
200                 return -1;
201         }
202
203         if (!ifname)
204                 rv = wifi_manager_initialize(&wifi2);
205         else
206                 rv = wifi_manager_initialize_with_interface_name(&wifi2, ifname);
207
208         if (rv == WIFI_MANAGER_ERROR_NONE) {
209                 wifi_manager_set_device_state_changed_cb(wifi2, __test_device_state_callback, "2");
210                 wifi_manager_set_background_scan_cb(wifi2, __test_bg_scan_completed_callback, "2");
211                 wifi_manager_set_connection_state_changed_cb(wifi2, __test_connection_state_callback, "2");
212                 wifi_manager_set_rssi_level_changed_cb(wifi2, __test_rssi_level_callback, "2");
213                 wifi_manager_tdls_set_state_changed_cb(wifi2, __test_tdls_state_callback, "2");
214                 wifi_manager_tdls_set_discovered_cb(wifi, __test_tdls_discover_callback, "2");
215                 wifi_manager_set_module_state_changed_cb(wifi, __test_get_wifi_module_state_callback, "2");
216                 wifi_manager_set_dhcp_state_changed_cb(wifi2, __test_dhcp_changed_callback, "2");
217         } else {
218                 printf("[2] Wifi init failed [%s]\n", wman_test_strerror(rv));
219                 wifi_manager_deinitialize(wifi);
220                 wifi = NULL;
221                 return -1;
222         }
223
224         printf("Wifi init succeeded\n");
225         return 1;
226 }
227
228 int  wman_test_deinit(void)
229 {
230         int rv = wifi_manager_deinitialize(wifi);
231
232         if (rv != WIFI_MANAGER_ERROR_NONE) {
233                 printf("[1] Wifi deinit failed [%s]\n", wman_test_strerror(rv));
234                 return -1;
235         }
236
237         wifi = NULL;
238
239         rv = wifi_manager_deinitialize(wifi2);
240
241         if (rv != WIFI_MANAGER_ERROR_NONE) {
242                 printf("[2] Wifi deinit failed [%s]\n", wman_test_strerror(rv));
243                 return -1;
244         }
245
246         wifi2 = NULL;
247
248         printf("Wifi deinit succeeded\n");
249         return 1;
250 }
251
252 int wman_test_init_with_ifname(void)
253 {
254         int rv;
255         char ifname[33] = { 0, };
256
257         printf("Input interface name: ");
258         rv = scanf(" %32[^\n]s", ifname);
259         if (rv < 1)
260                 return -1;
261
262         rv = wman_test_init(ifname);
263
264         return rv;
265 }
266
267 gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data)
268 {
269         int rv;
270         char a[10];
271
272         printf("Event received from stdin\n");
273
274         rv = read(0, a, 10);
275
276         if (rv <= 0 || a[0] == '0') {
277                 rv = wifi_manager_deinitialize(wifi);
278
279                 if (rv != WIFI_MANAGER_ERROR_NONE)
280                         printf("Fail to deinitialize.\n");
281
282                 exit(1);
283         }
284
285         if (a[0] == '\n' || a[0] == '\r') {
286 /* Public API */
287                 printf("\n\n Network Connection API Test App\n\n");
288                 printf("Options..\n");
289                 printf(LOG_BLUE "[Public APIs]\n" LOG_END);
290                 printf(LOG_GREEN "1   - Wi-Fi init and set callbacks\n" LOG_END);
291                 printf(":   - Wi-Fi init and set callbacks with interface name\n");
292                 printf("2   - Wi-Fi deinit(unset callbacks automatically)\n");
293                 printf(LOG_GREEN "3   - Activate Wi-Fi device\n" LOG_END);
294                 printf("4   - Deactivate Wi-Fi device\n");
295                 printf("5   - Is Wi-Fi activated?\n");
296                 printf("6   - Get connection state\n");
297                 printf("7   - Get MAC address\n");
298                 printf("8   - Get Wi-Fi interface name\n");
299                 printf(LOG_GREEN "9   - Scan request\n" LOG_END);
300                 printf("a   - Get Connected AP\n");
301                 printf("b   - Get AP list\n");
302                 printf(LOG_GREEN "c   - Connect\n" LOG_END);
303                 printf("d   - Disconnect\n");
304                 printf("e   - Connect by wps pbc\n");
305                 printf("f   - Forget an AP\n");
306                 printf("g   - Set & connect EAP\n");
307                 printf("h   - Set IP method type\n");
308                 printf("i   - Set Proxy method type\n");
309                 printf("j   - Get Ap info\n");
310                 printf("k   - Connect Specific AP\n");
311                 printf("l   - Load configuration\n");
312                 printf("m   - Save configuration\n");
313                 printf("n   - Remove configuration\n");
314                 printf("o   - TDLS Discover\n");
315                 printf("p   - TDLS Connect\n");
316                 printf("q   - TDLS Connected peer\n");
317                 printf("r   - TDLS Disconnect\n");
318                 printf("s   - Connect Hidden AP\n");
319                 printf("t   - Connect WPS PBC without SSID\n");
320                 printf("u   - Connect WPS PIN without SSID\n");
321                 printf("v   - Cancel WPS Request\n");
322                 printf("w   - Get wifi scanning state\n");
323                 printf("x   - Enable TDLS Channel Switch Request\n");
324                 printf("y   - Disable TDLS Channel Switch Request\n");
325                 printf("z   - Get Wi-Fi Module State\n");
326                 printf("A   - BSSID Scan (Wi-Fi Position System Scan)\n");
327                 printf("B   - Multi Scan (Multi Frequency & Multi SSID)\n");
328                 printf("C   - Add VSIE\n");
329                 printf("D   - Get VSIE\n");
330                 printf("E   - Remove VSIE\n");
331                 printf("F   - Set IP conflict detection mode\n");
332                 printf("G   - Get IP conflict detection mode\n");
333                 printf("H   - Get IP conflict state\n");
334                 printf("I   - Get generated PIN number\n");
335                 printf("J   - Forget an AP(async)\n");
336 /* Extension APIs */
337                 printf(LOG_BLUE "[Extension API]\n" LOG_END);
338                 printf("K   - Set Enable or Disable Auto Scan\n");
339                 printf("L   - Set Mode of Auto Scan\n");
340                 printf("M   - Get Enable or Disable Auto Scan\n");
341                 printf("N   - Get Mode of Auto Scan\n");
342                 printf("O   - Flush BSS\n");
343                 printf("P   - Set enable or disable auto connect\n");
344                 printf("Q   - Get enable or disable auto connect\n");
345                 printf("R   - Set enable or disable of Wi-Fi profile auto-connect\n");
346                 printf("S   - Get enable or disable of Wi-Fi profile auto-connect\n");
347                 printf("T   - Set the IP conflict detection period\n");
348                 printf("U   - Get the IP conflict detection period\n");
349                 printf("V   - Netlink Scan Normal/Specific-AP\n");
350                 printf("W   - Get passpoint state\n");
351                 printf("X   - Set passpoint on/off\n");
352                 printf("Y   - Get Access Point Hardware Mode\n");
353                 printf("Z   - Get 5Ghz supported\n");
354                 printf("!   - Set mac policy\n");
355                 printf("@   - Set preassoc mac policy\n");
356                 printf("#   - Set random mac lifetime\n");
357                 printf("$   - Get mac policies\n");
358                 printf("^   - Set country code\n");
359                 printf("&   - Get country code\n");
360                 printf("*   - Check available security type\n");
361                 printf("(   - Get power-save state\n");
362                 printf(")   - Set power-save state\n");
363                 printf("-   - Get power-save mode\n");
364                 printf("=   - Set power-save mode\n");
365                 printf("[   - Get Interval of Auto Scan\n");
366                 printf("]   - Set Interval of Auto Scan\n");
367                 printf(";   - Remove All Wi-Fi configurations\n");
368                 printf(LOG_RED "0   - Exit \n" LOG_END);
369
370                 printf("ENTER  - Show options menu.......\n");
371         }
372
373         switch (a[0]) {
374 /* Public API */
375         case '1':
376                 rv = wman_test_init(NULL);
377                 break;
378         case ':':
379                 rv = wman_test_init_with_ifname();
380                 break;
381         case '2':
382                 rv = wman_test_deinit();
383                 break;
384         case '3':
385                 rv = wman_test_activate(wifi);
386                 break;
387         case '4':
388                 rv = wman_test_deactivate(wifi);
389                 break;
390         case '5':
391                 rv = wman_test_is_activated(wifi);
392                 break;
393         case '6':
394                 rv = wman_test_get_connection_state(wifi);
395                 break;
396         case '7':
397                 rv = wman_test_get_mac_address(wifi);
398                 break;
399         case '8':
400                 rv = wman_test_get_interface_name(wifi);
401                 break;
402         case '9':
403                 rv = wman_test_scan(wifi);
404                 break;
405         case 'a':
406                 rv = wman_test_get_connected_ap(wifi);
407                 break;
408         case 'b':
409                 rv = wman_test_foreach_found_ap(wifi);
410                 break;
411         case 'c':
412                 rv = wman_test_connect(wifi);
413                 break;
414         case 'd':
415                 rv = wman_test_disconnect(wifi);
416                 break;
417         case 'e':
418                 rv = wman_test_connect_wps(wifi);
419                 break;
420         case 'f':
421                 rv = wman_test_forget_ap(wifi);
422                 break;
423         case 'g':
424                 rv = wman_test_connect_eap_ap(wifi);
425                 break;
426         case 'h':
427                 rv = wman_test_set_ip_method(wifi);
428                 break;
429         case 'i':
430                 rv = wman_test_set_proxy_method(wifi);
431                 break;
432         case 'j':
433                 rv = wman_test_get_ap_info(wifi);
434                 break;
435         case 'k':
436                 rv = wman_test_connect_specific_ap(wifi);
437                 break;
438         case 'l':
439                 rv = wman_test_config_load_configuration(wifi);
440                 break;
441         case 'm':
442                 rv = wman_test_config_save(wifi);
443                 break;
444         case 'n':
445                 rv = wman_test_config_remove(wifi);
446                 break;
447         case 'o':
448                 rv = wman_test_tdls_discover(wifi);
449                 break;
450         case 'p':
451                 rv = wman_test_tdls_connect(wifi);
452                 break;
453         case 'q':
454                 rv = wman_test_tdls_get_connected_peer(wifi);
455                 break;
456         case 'r':
457                 rv = wman_test_tdls_disconnect(wifi);
458                 break;
459         case 's':
460                 rv = wman_test_connect_hidden_ap(wifi);
461                 break;
462         case 't':
463                 rv = wman_test_connect_wps_pbc_without_ssid(wifi);
464                 break;
465         case 'u':
466                 rv = wman_test_connect_wps_pin_without_ssid(wifi);
467                 break;
468         case 'v':
469                 rv = wman_test_cancel_wps(wifi);
470                 break;
471         case 'w':
472                 rv = wman_test_get_scan_state(wifi);
473                 break;
474         case 'x':
475                 rv = wman_test_tdls_enable_channel_switch(wifi);
476                 break;
477         case 'y':
478                 rv = wman_test_tdls_disable_channel_switch(wifi);
479                 break;
480         case 'z':
481                 rv = wman_test_get_module_state(wifi);
482                 break;
483         case 'A':
484                 rv = wman_test_bssid_scan(wifi);
485                 break;
486         case 'B':
487                 rv = wman_test_specific_ap_start_multi_scan(wifi);
488                 break;
489         case 'C':
490                 rv = wman_test_add_vsie(wifi);
491                 break;
492         case 'D':
493                 rv = wman_test_get_vsie(wifi);
494                 break;
495         case 'E':
496                 rv = wman_test_remove_vsie(wifi);
497                 break;
498         case 'F':
499                 rv = wman_test_set_ip_conflict_detect_enable(wifi);
500                 break;
501         case 'G':
502                 rv = wman_test_ip_conflict_detect_is_enabled(wifi);
503                 break;
504         case 'H':
505                 rv = wman_test_get_ip_conflict_state(wifi);
506                 break;
507         case 'I':
508                 rv = wman_test_get_wps_generated_pin(wifi);
509                 break;
510         case 'J':
511                 rv = wman_test_forget_ap_async(wifi);
512                 break;
513
514 /* Extension APIs */
515         case 'K':
516                 rv = wman_test_set_autoscan_state(wifi);
517                 break;
518         case 'L':
519                 rv = wman_test_set_autoscan_mode(wifi);
520                 break;
521         case 'M':
522                 rv = wman_test_get_autoscan_state(wifi);
523                 break;
524         case 'N':
525                 rv = wman_test_get_autoscan_mode(wifi);
526                 break;
527         case 'O':
528                 rv = wman_test_flush_bss(wifi);
529                 break;
530         case 'P':
531                 rv = wman_test_set_auto_connect(wifi);
532                 break;
533         case 'Q':
534                 rv = wman_test_get_auto_connect(wifi);
535                 break;
536         case 'R':
537                 rv = wman_test_set_ap_auto_connect(wifi);
538                 break;
539         case 'S':
540                 rv = wman_test_get_ap_auto_connect(wifi);
541                 break;
542         case 'T':
543                 rv = wman_test_set_ip_conflict_period(wifi);
544                 break;
545         case 'U':
546                 rv = wman_test_get_ip_conflict_period(wifi);
547                 break;
548         case 'V':
549                 rv = wman_test_netlink_scan(wifi);
550                 break;
551         case 'W':
552                 rv = wman_test_get_passpoint_state(wifi);
553                 break;
554         case 'X':
555                 rv = wman_test_set_passpoint_enable(wifi);
556                 break;
557         case 'Y':
558                 rv = wman_test_get_connection_mode(wifi);
559                 break;
560         case 'Z':
561                 rv = wman_test_get_5ghz_band_supported(wifi);
562                 break;
563         case '!':
564                 rv = wman_test_set_mac_policy(wifi);
565                 break;
566         case '@':
567                 rv = wman_test_set_preassoc_mac_policy(wifi);
568                 break;
569         case '#':
570                 rv = wman_test_set_random_mac_lifetime(wifi);
571                 break;
572         case '$':
573                 rv = wman_test_get_mac_policies(wifi);
574                 break;
575         case '^':
576                 rv = wman_test_set_country_code(wifi);
577                 break;
578         case '&':
579                 rv = wman_test_get_country_code(wifi);
580                 break;
581         case '*':
582                 rv = wman_test_check_supported_security_type(wifi);
583                 break;
584         case '(':
585                 rv = wman_test_get_power_save_state(wifi);
586                 break;
587         case ')':
588                 rv = wman_test_set_power_save_state(wifi);
589                 break;
590         case '-':
591                 rv = wman_test_get_power_save_mode(wifi);
592                 break;
593         case '=':
594                 rv = wman_test_set_power_save_mode(wifi);
595                 break;
596         case '[':
597                 rv = wman_test_get_autoscan_interval(wifi);
598                 break;
599         case ']':
600                 rv = wman_test_set_autoscan_interval(wifi);
601                 break;
602         case ';':
603                 rv = wman_test_reset_wifi_configurations(wifi);
604                 break;
605         default:
606                 break;
607         }
608
609         if (rv == 1)
610                 printf("Operation succeeded!\n");
611         else
612                 printf("Operation failed!\n");
613
614         return TRUE;
615 }
616
617 int main(int argc, char **argv)
618 {
619         GMainLoop *mainloop;
620 #if !GLIB_CHECK_VERSION(2, 36, 0)
621         g_type_init();
622 #endif
623         mainloop = g_main_loop_new(NULL, FALSE);
624
625         GIOChannel *channel = g_io_channel_unix_new(0);
626         g_io_add_watch(channel, (G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL), test_thread, NULL);
627
628         printf("Test Thread created...\n");
629
630         g_main_loop_run(mainloop);
631
632         return 0;
633 }