Add new rpm package for test application
[platform/core/api/wifi.git] / test / wifi_test.c
1 /*
2  * Copyright (c) 2012-2013 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
17 #include <glib.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <netdb.h>
21 #include <sys/socket.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <sys/un.h>
25 #include <fcntl.h>
26 #include <errno.h>
27 #include <sys/ioctl.h>
28 #include <signal.h>
29 #include <assert.h>
30 #include <wifi.h>
31 #include <tizen_error.h>
32
33 #define LOG_RED "\033[0;31m"
34 #define LOG_GREEN "\033[0;32m"
35 #define LOG_BROWN "\033[0;33m"
36 #define LOG_BLUE "\033[0;34m"
37 #define LOG_END "\033[0;m"
38
39 gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data);
40
41 static const char *__test_convert_error_to_string(wifi_error_e err_type)
42 {
43         switch (err_type) {
44         case WIFI_ERROR_NONE:
45                 return "NONE";
46         case WIFI_ERROR_INVALID_PARAMETER:
47                 return "INVALID_PARAMETER";
48         case WIFI_ERROR_OUT_OF_MEMORY:
49                 return "OUT_OF_MEMORY";
50         case WIFI_ERROR_INVALID_OPERATION:
51                 return "INVALID_OPERATION";
52         case WIFI_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED:
53                 return "ADDRESS_FAMILY_NOT_SUPPORTED";
54         case WIFI_ERROR_OPERATION_FAILED:
55                 return "OPERATION_FAILED";
56         case WIFI_ERROR_NO_CONNECTION:
57                 return "NO_CONNECTION";
58         case WIFI_ERROR_NOW_IN_PROGRESS:
59                 return "NOW_IN_PROGRESS";
60         case WIFI_ERROR_ALREADY_EXISTS:
61                 return "ALREADY_EXISTS";
62         case WIFI_ERROR_OPERATION_ABORTED:
63                 return "OPERATION_ABORTED";
64         case WIFI_ERROR_DHCP_FAILED:
65                 return "DHCP_FAILED";
66         case WIFI_ERROR_INVALID_KEY:
67                 return "INVALID_KEY";
68         case WIFI_ERROR_NO_REPLY:
69                 return "NO_REPLY";
70         case WIFI_ERROR_SECURITY_RESTRICTED:
71                 return "SECURITY_RESTRICTED";
72         case WIFI_ERROR_PERMISSION_DENIED:
73                 return "PERMISSION_DENIED";
74         case WIFI_ERROR_NOT_SUPPORTED:
75                 return "NOT_SUPPORTED";
76         }
77
78         return "UNKNOWN";
79 }
80
81 static void __test_device_state_callback(wifi_device_state_e state, void* user_data)
82 {
83         printf("Device state changed callback");
84
85         if (state == WIFI_DEVICE_STATE_ACTIVATED)
86                 printf(", state : Activated\n");
87         else
88                 printf(", state : Deactivated\n");
89 }
90
91 static void __test_bg_scan_completed_callback(wifi_error_e error_code, void* user_data)
92 {
93         printf("Background Scan Completed, error code : %s\n",
94                         __test_convert_error_to_string(error_code));
95 }
96
97 static void __test_scan_request_callback(wifi_error_e error_code, void* user_data)
98 {
99         if (user_data != NULL)
100                 printf("user_data : %s\n", (char *)user_data);
101
102         printf("Scan Completed from scan request, error code : %s\n",
103                         __test_convert_error_to_string(error_code));
104 }
105
106 static void __test_connection_state_callback(wifi_connection_state_e state, wifi_ap_h ap, void* user_data)
107 {
108         int rv = 0;
109         char *ap_name = NULL;
110
111         printf("Connection state changed callback");
112
113         switch (state) {
114         case WIFI_CONNECTION_STATE_CONNECTED:
115                 printf(", state : Connected");
116                 break;
117         case WIFI_CONNECTION_STATE_ASSOCIATION:
118                 printf(", state : Association");
119                 break;
120         case WIFI_CONNECTION_STATE_CONFIGURATION:
121                 printf(", state : Configuration");
122                 break;
123         case WIFI_CONNECTION_STATE_DISCONNECTED:
124                 printf(", state : Disconnected");
125                 break;
126         case WIFI_CONNECTION_STATE_FAILURE:
127                 printf(", state : Failure");
128                 break;
129         default:
130                 printf(", state : Unknown");
131         }
132
133         rv = wifi_ap_get_essid(ap, &ap_name);
134         if (rv != WIFI_ERROR_NONE)
135                 printf(", Fail to get AP name [%s]\n", __test_convert_error_to_string(rv));
136         else {
137                 printf(", AP name : %s\n", ap_name);
138                 g_free(ap_name);
139         }
140 }
141
142 static void __test_activated_callback(wifi_error_e result, void* user_data)
143 {
144         if (result == WIFI_ERROR_NONE)
145                 printf("Wi-Fi Activation Succeeded\n");
146         else
147                 printf("Wi-Fi Activation Failed! error : %s\n", __test_convert_error_to_string(result));
148 }
149
150 static void __test_deactivated_callback(wifi_error_e result, void* user_data)
151 {
152         if (result == WIFI_ERROR_NONE)
153                 printf("Wi-Fi Deactivation Succeeded\n");
154         else
155                 printf("Wi-Fi Deactivation Failed! error : %s\n", __test_convert_error_to_string(result));
156 }
157
158 static void __test_connected_callback(wifi_error_e result, void* user_data)
159 {
160         if (result == WIFI_ERROR_NONE)
161                 printf("Wi-Fi Connection Succeeded\n");
162         else
163                 printf("Wi-Fi Connection Failed! error : %s\n", __test_convert_error_to_string(result));
164 }
165
166 static void __test_disconnected_callback(wifi_error_e result, void* user_data)
167 {
168         if (result == WIFI_ERROR_NONE)
169                 printf("Wi-Fi Disconnection Succeeded\n");
170         else
171                 printf("Wi-Fi Disconnection Failed! error : %s\n", __test_convert_error_to_string(result));
172 }
173
174 static void __test_rssi_level_callback(wifi_rssi_level_e rssi_level, void* user_data)
175 {
176         printf("RSSI level changed callback, level = %d\n", rssi_level);
177 }
178
179 static const char* __test_print_state(wifi_connection_state_e state)
180 {
181         switch (state) {
182         case WIFI_CONNECTION_STATE_FAILURE:
183                 return "Failure";
184         case WIFI_CONNECTION_STATE_DISCONNECTED:
185                 return "Disconnected";
186         case WIFI_CONNECTION_STATE_ASSOCIATION:
187                 return "Association";
188         case WIFI_CONNECTION_STATE_CONNECTED:
189                 return "Connected";
190         case WIFI_CONNECTION_STATE_CONFIGURATION:
191                 return "Configuration";
192         }
193
194         return "Unknown";
195 }
196
197 static bool __test_compare_ap_name(const char *ap_name, const char *ap_name_part)
198 {
199         int ap_name_len = strlen(ap_name);
200         int ap_name_part_len = strlen(ap_name_part);
201
202         if (strncmp(ap_name, ap_name_part,
203                                 ap_name_len > ap_name_part_len ? ap_name_len : ap_name_part_len) == 0)
204                 return true;
205         else
206                 return false;
207 }
208
209 static bool __test_found_ap_callback(wifi_ap_h ap, void *user_data)
210 {
211         int rv = 0;
212         char *ap_name = NULL;
213         wifi_connection_state_e state;
214
215         rv = wifi_ap_get_essid(ap, &ap_name);
216         if (rv != WIFI_ERROR_NONE) {
217                 printf("Fail to get AP name [%s]\n", __test_convert_error_to_string(rv));
218                 return false;
219         }
220
221         rv = wifi_ap_get_connection_state(ap, &state);
222         if (rv != WIFI_ERROR_NONE) {
223                 printf("Fail to get State [%s]\n", __test_convert_error_to_string(rv));
224                 g_free(ap_name);
225                 return false;
226         }
227
228         printf("AP name : %s, state : %s\n", ap_name, __test_print_state(state));
229         g_free(ap_name);
230
231         return true;
232 }
233
234 static bool __test_found_connect_ap_callback(wifi_ap_h ap, void *user_data)
235 {
236         int rv = 0;
237         char *ap_name = NULL;
238         char *ap_name_part = (char*)user_data;
239
240         rv = wifi_ap_get_essid(ap, &ap_name);
241         if (rv != WIFI_ERROR_NONE) {
242                 printf("Fail to get AP name [%s]\n", __test_convert_error_to_string(rv));
243                 return false;
244         }
245
246         if (__test_compare_ap_name(ap_name, ap_name_part)) {
247                 bool required = false;
248
249                 if (wifi_ap_is_passphrase_required(ap, &required) == WIFI_ERROR_NONE)
250                         printf("Passphrase required : %s\n", required ? "TRUE" : "FALSE");
251                 else
252                         printf("Fail to get Passphrase required\n");
253
254                 if (required) {
255                         char passphrase[100];
256                         printf("Input passphrase for %s : ", ap_name);
257                         rv = scanf("%99s", passphrase);
258
259                         rv = wifi_ap_set_passphrase(ap, passphrase);
260                         if (rv != WIFI_ERROR_NONE) {
261                                 printf("Fail to set passphrase : %s\n", __test_convert_error_to_string(rv));
262                                 g_free(ap_name);
263                                 return false;
264                         }
265                 }
266
267                 rv = wifi_connect(ap, __test_connected_callback, NULL);
268                 if (rv != WIFI_ERROR_NONE)
269                         printf("Fail to connection request [%s] : %s\n", ap_name, __test_convert_error_to_string(rv));
270                 else
271                         printf("Success to connection request [%s]\n", ap_name);
272
273                 g_free(ap_name);
274                 return false;
275         }
276
277         g_free(ap_name);
278         return true;
279 }
280
281 static bool __test_found_connect_wps_callback(wifi_ap_h ap, void *user_data)
282 {
283         int rv = 0;
284         char *ap_name = NULL;
285         char *ap_name_part = (char*)user_data;
286
287         rv = wifi_ap_get_essid(ap, &ap_name);
288         if (rv != WIFI_ERROR_NONE) {
289                 printf("Fail to get AP name [%s]\n", __test_convert_error_to_string(rv));
290                 return false;
291         }
292
293         if (__test_compare_ap_name(ap_name, ap_name_part)) {
294                 int user_sel;
295                 char pin[32] = {0,};
296
297                 printf("%s - Input WPS method (1:PBC, 2:PIN) :\n", ap_name);
298                 rv = scanf("%9d", &user_sel);
299
300                 switch (user_sel) {
301                 case 1:
302                         rv = wifi_connect_by_wps_pbc(ap, __test_connected_callback, NULL);
303                         break;
304                 case 2:
305                         printf("Input PIN code :\n");
306                         rv = scanf("%31s", pin);
307                         rv = wifi_connect_by_wps_pin(ap, pin, __test_connected_callback, NULL);
308                         break;
309                 default:
310                         printf("Invalid input!\n");
311                         g_free(ap_name);
312                         return false;
313                 }
314
315                 if (rv != WIFI_ERROR_NONE)
316                         printf("Fail to connection request [%s] : %s\n", ap_name, __test_convert_error_to_string(rv));
317                 else
318                         printf("Success to connection request [%s]\n", ap_name);
319
320                 g_free(ap_name);
321                 return false;
322         }
323
324         g_free(ap_name);
325         return true;
326 }
327
328 static bool __test_found_disconnect_ap_callback(wifi_ap_h ap, void *user_data)
329 {
330         int rv = 0;
331         char *ap_name = NULL;
332         char *ap_name_part = (char*)user_data;
333
334         rv = wifi_ap_get_essid(ap, &ap_name);
335         if (rv != WIFI_ERROR_NONE) {
336                 printf("Fail to get AP name [%s]\n", __test_convert_error_to_string(rv));
337                 return false;
338         }
339
340         if (__test_compare_ap_name(ap_name, ap_name_part)) {
341                 rv = wifi_disconnect(ap, __test_disconnected_callback, NULL);
342                 if (rv != WIFI_ERROR_NONE)
343                         printf("Fail to disconnection reqeust %s : [%s]\n", ap_name, __test_convert_error_to_string(rv));
344                 else
345                         printf("Success to disconnection request %s\n", ap_name);
346
347                 g_free(ap_name);
348                 return false;
349         }
350
351         g_free(ap_name);
352         return true;
353 }
354
355 static bool __test_found_forget_ap_callback(wifi_ap_h ap, void *user_data)
356 {
357         int rv = 0;
358         char *ap_name = NULL;
359         char *ap_name_part = (char*)user_data;
360
361         rv = wifi_ap_get_essid(ap, &ap_name);
362         if (rv != WIFI_ERROR_NONE) {
363                 printf("Fail to get AP name [%s]\n", __test_convert_error_to_string(rv));
364                 return false;
365         }
366
367         if (__test_compare_ap_name(ap_name, ap_name_part)) {
368                 rv = wifi_forget_ap(ap);
369                 if (rv != WIFI_ERROR_NONE)
370                         printf("Fail to forget [%s] : %s\n", ap_name, __test_convert_error_to_string(rv));
371                 else
372                         printf("Success to forget [%s]\n", ap_name);
373
374                 g_free(ap_name);
375                 return false;
376         }
377
378         g_free(ap_name);
379         return true;
380 }
381
382 static bool __test_found_eap_ap_callback(wifi_ap_h ap, void *user_data)
383 {
384         int rv = 0;
385         char *ap_name = NULL;
386         char *ap_name_part = (char*)user_data;
387
388         rv = wifi_ap_get_essid(ap, &ap_name);
389         if (rv != WIFI_ERROR_NONE) {
390                 printf("Fail to get AP name [%s]\n", __test_convert_error_to_string(rv));
391                 return false;
392         }
393
394         if (__test_compare_ap_name(ap_name, ap_name_part)) {
395                 wifi_security_type_e type;
396
397                 if (wifi_ap_get_security_type(ap, &type) == WIFI_ERROR_NONE)
398                         printf("Security type : %d\n", type);
399                 else
400                         printf("Fail to get Security type\n");
401
402                 if (type != WIFI_SECURITY_TYPE_EAP) {
403                         g_free(ap_name);
404                         return false;
405                 }
406
407                 char input_str1[100];
408                 printf("Input user name for %s : ", ap_name);
409                 rv = scanf("%99s", input_str1);
410
411                 char input_str2[100];
412                 printf("Input password for %s : ", ap_name);
413                 rv = scanf("%99s", input_str2);
414
415                 rv = wifi_ap_set_eap_passphrase(ap, input_str1, input_str2);
416                 if (rv != WIFI_ERROR_NONE) {
417                         printf("Fail to set eap passphrase : %s\n", __test_convert_error_to_string(rv));
418                         g_free(ap_name);
419                         return false;
420                 }
421
422                 char *inputed_name = NULL;
423                 bool is_pass_set;
424                 rv = wifi_ap_get_eap_passphrase(ap, &inputed_name, &is_pass_set);
425                 if (rv != WIFI_ERROR_NONE) {
426                         printf("Fail to get eap passphrase : %s\n", __test_convert_error_to_string(rv));
427                         g_free(ap_name);
428                         return false;
429                 }
430
431                 printf("name : %s, is password set : %s\n", inputed_name, is_pass_set ? "TRUE" : "FALSE");
432
433                 printf("Input certificate file:\n");
434                 rv = scanf("%99s", input_str1);
435
436                 rv = wifi_ap_set_eap_ca_cert_file(ap, input_str1);
437                 if (rv != WIFI_ERROR_NONE) {
438                         printf("Fail to set eap certificatefile : %s\n", __test_convert_error_to_string(rv));
439                         g_free(ap_name);
440                         return false;
441                 }
442
443                 int input_int;
444                 printf("Input EAP type:\n");
445                 printf("0 -> WIFI_EAP_TYPE_PEAP\n");
446                 printf("1 -> WIFI_EAP_TYPE_TLS\n");
447                 printf("2 -> WIFI_EAP_TYPE_TTLS\n");
448                 printf("3 -> WIFI_EAP_TYPE_SIM\n");
449                 printf("4 -> WIFI_EAP_TYPE_AKA\n");
450                 rv = scanf("%d", &input_int);
451
452                 rv = wifi_ap_set_eap_type(ap, input_int);
453                 if (rv != WIFI_ERROR_NONE) {
454                         printf("Fail to set eap type : %s\n", __test_convert_error_to_string(rv));
455                         g_free(ap_name);
456                         return false;
457                 }
458
459                 printf("Input EAP authentication type:\n");
460                 printf("0 -> WIFI_EAP_AUTH_TYPE_NONE\n");
461                 printf("1 -> WIFI_EAP_AUTH_TYPE_PAP\n");
462                 printf("2 -> WIFI_EAP_AUTH_TYPE_MSCHAP\n");
463                 printf("3 -> WIFI_EAP_AUTH_TYPE_MSCHAPV2\n");
464                 printf("4 -> WIFI_EAP_AUTH_TYPE_GTC\n");
465                 printf("5 -> WIFI_EAP_AUTH_TYPE_MD5\n");
466                 rv = scanf("%d", &input_int);
467
468                 rv = wifi_ap_set_eap_auth_type(ap, input_int);
469                 if (rv != WIFI_ERROR_NONE) {
470                         printf("Fail to set eap auth type : %s\n", __test_convert_error_to_string(rv));
471                         g_free(ap_name);
472                         return false;
473                 }
474
475                 rv = wifi_connect(ap, __test_connected_callback, NULL);
476                 if (rv != WIFI_ERROR_NONE)
477                         printf("Fail to connection request [%s] : %s\n", ap_name, __test_convert_error_to_string(rv));
478                 else
479                         printf("Success to connection request [%s]\n", ap_name);
480
481                 g_free(ap_name);
482                 g_free(inputed_name);
483                 return false;
484         }
485
486         g_free(ap_name);
487         return true;
488 }
489
490 static bool test_get_user_int(const char *msg, int *num)
491 {
492         if (msg == NULL || num == NULL)
493                 return false;
494
495         int rv;
496         char buf[32] = {0,};
497         printf("%s\n", msg);
498         rv = read(0, buf, 32);
499
500         if (rv < 0 || *buf == 0 || *buf == '\n' || *buf == '\r')
501                 return false;
502
503         *num = atoi(buf);
504         return true;
505 }
506
507 static bool __test_found_change_ip_method_callback(wifi_ap_h ap, void *user_data)
508 {
509         int rv;
510         char *ap_name;
511         char *ap_name_part = (char*)user_data;
512
513         rv = wifi_ap_get_essid(ap, &ap_name);
514         if (rv != WIFI_ERROR_NONE) {
515                 printf("Fail to get AP name [%s]\n", __test_convert_error_to_string(rv));
516                 return false;
517         }
518
519         if (__test_compare_ap_name(ap_name, ap_name_part)) {
520                 wifi_ip_config_type_e type;
521                 int method;
522                 int address_type;
523
524                 printf("Input new method type (1:dhcp, 2:manual, 3:auto) :\n");
525                 rv = scanf("%9d", &method);
526                 if (rv <= 0) {
527                         g_free(ap_name);
528                         return false;
529                 }
530
531                 rv = test_get_user_int("Input Address type to get"
532                                                         "(0:IPV4, 1:IPV6):", &address_type);
533
534                 if (rv == false || (address_type != 0 && address_type != 1)) {
535                         printf("Invalid input!!\n");
536                         return false;
537                 }
538
539                 switch (method) {
540                 case 1:
541                         type = WIFI_IP_CONFIG_TYPE_DYNAMIC;
542                         break;
543                 case 2:
544                         type = WIFI_IP_CONFIG_TYPE_STATIC;
545                         break;
546                 case 3:
547                         type = WIFI_IP_CONFIG_TYPE_AUTO;
548                         break;
549                 default:
550                         printf("Invalid input!\n");
551                         g_free(ap_name);
552                         return false;
553                 }
554
555                 rv = wifi_ap_set_ip_config_type(ap, address_type, type);
556                 if (rv != WIFI_ERROR_NONE)
557                         printf("Fail to set ip method type[%s]\n", __test_convert_error_to_string(rv));
558
559                 if (type == WIFI_IP_CONFIG_TYPE_STATIC) {
560                         char ip_addr[16];
561
562                         printf("Input new ip address (x:skip, 0:clear) :\n");
563                         rv = scanf("%15s", ip_addr);
564                         if (rv > 0) {
565                                 switch (ip_addr[0]) {
566                                 case 'x':
567                                         rv = WIFI_ERROR_NONE;
568                                         break;
569                                 case '0':
570                                         rv = wifi_ap_set_ip_address(ap, address_type, NULL);
571                                         break;
572                                 default:
573                                         rv = wifi_ap_set_ip_address(ap, address_type, ip_addr);
574                                 }
575
576                                 if (rv != WIFI_ERROR_NONE)
577                                         printf("Fail to set ip address[%s]\n",
578                                                         __test_convert_error_to_string(rv));
579                         }
580
581                         printf("Input new subnet mask (x:skip, 0:clear) :\n");
582                         rv = scanf("%15s", ip_addr);
583                         if (rv > 0) {
584                                 switch (ip_addr[0]) {
585                                 case 'x':
586                                         rv = WIFI_ERROR_NONE;
587                                         break;
588                                 case '0':
589                                         rv = wifi_ap_set_subnet_mask(ap, address_type, NULL);
590                                         break;
591                                 default:
592                                         rv = wifi_ap_set_subnet_mask(ap, address_type, ip_addr);
593                                 }
594
595                                 if (rv != WIFI_ERROR_NONE)
596                                         printf("Fail to set subnet mask[%s]\n",
597                                                         __test_convert_error_to_string(rv));
598                         }
599
600                         printf("Input new gateway address (x:skip, 0:clear) :\n");
601                         rv = scanf("%15s", ip_addr);
602                         if (rv > 0) {
603                                 switch (ip_addr[0]) {
604                                 case 'x':
605                                         rv = WIFI_ERROR_NONE;
606                                         break;
607                                 case '0':
608                                         rv = wifi_ap_set_gateway_address(ap, address_type, NULL);
609                                         break;
610                                 default:
611                                         rv = wifi_ap_set_gateway_address(ap, address_type, ip_addr);
612                                 }
613
614                                 if (rv != WIFI_ERROR_NONE)
615                                         printf("Fail to set gateway address[%s]\n",
616                                                         __test_convert_error_to_string(rv));
617                         }
618                 }
619
620                 g_free(ap_name);
621                 return false;
622         }
623
624         g_free(ap_name);
625         return true;
626 }
627
628 static bool __test_found_change_proxy_method_callback(wifi_ap_h ap, void *user_data)
629 {
630         int rv, address_type;
631         char *ap_name;
632         char *ap_name_part = (char*)user_data;
633
634         rv = wifi_ap_get_essid(ap, &ap_name);
635         if (rv != WIFI_ERROR_NONE) {
636                 printf("Fail to get AP name [%s]\n", __test_convert_error_to_string(rv));
637                 return false;
638         }
639
640         printf("ap_name %s, user input name %s\n", ap_name, ap_name_part);
641         if (__test_compare_ap_name(ap_name, ap_name_part)) {
642                 wifi_proxy_type_e type;
643                 char proxy_addr[65];
644                 int method;
645
646                 printf("Input new method type (1:direct, 2:manual, 3:auto) :\n");
647                 rv = scanf("%9d", &method);
648                 if (rv <= 0) {
649                         g_free(ap_name);
650                         return false;
651                 }
652
653                 rv = test_get_user_int("Input Address type to get"
654                                                         "(0:IPV4, 1:IPV6):", &address_type);
655
656                 if (rv == false || (address_type != 0 && address_type != 1)) {
657                         printf("Invalid input!!\n");
658                         return false;
659                 }
660
661                 switch (method) {
662                 case 1:
663                         type = WIFI_PROXY_TYPE_DIRECT;
664                         break;
665                 case 2:
666                         type = WIFI_PROXY_TYPE_MANUAL;
667                         break;
668                 case 3:
669                         type = WIFI_PROXY_TYPE_AUTO;
670                         break;
671                 default:
672                         printf("Invalid input!\n");
673                         g_free(ap_name);
674                         return false;
675                 }
676
677                 rv = wifi_ap_set_proxy_type(ap, type);
678                 if (rv != WIFI_ERROR_NONE)
679                         printf("Fail to set proxy method type[%s]\n", __test_convert_error_to_string(rv));
680
681                 printf("Input new proxy address (x:skip, 0:clear) :\n");
682                 rv = scanf("%64s", proxy_addr);
683
684                 if (rv > 0) {
685                         switch (proxy_addr[0]) {
686                         case 'x':
687                                 rv = WIFI_ERROR_NONE;
688                                 break;
689                         case '0':
690                                 rv = wifi_ap_set_proxy_address(ap, address_type, NULL);
691                                 break;
692                         default:
693                                 rv = wifi_ap_set_proxy_address(ap, address_type, proxy_addr);
694                         }
695
696                         if (rv != WIFI_ERROR_NONE)
697                                 printf("Fail to set proxy address[%s]\n", __test_convert_error_to_string(rv));
698                 }
699
700                 g_free(ap_name);
701                 return false;
702         }
703
704         g_free(ap_name);
705         return true;
706 }
707
708 static bool __test_found_print_ap_info_callback(wifi_ap_h ap, void *user_data)
709 {
710         int rv, address_type = 0;
711         char *ap_name;
712         char *str_value;
713         int int_value;
714         wifi_connection_state_e conn_state;
715         wifi_ip_config_type_e ip_type;
716         wifi_proxy_type_e proxy_type;
717         wifi_security_type_e sec_type;
718         wifi_encryption_type_e enc_type;
719         wifi_eap_type_e eap_type;
720         wifi_eap_auth_type_e eap_auth_type;
721         bool bool_value;
722         char *ap_name_part = (char*)user_data;
723
724         rv = wifi_ap_get_essid(ap, &ap_name);
725         if (rv != WIFI_ERROR_NONE) {
726                 printf("Fail to get AP name [%s]\n", __test_convert_error_to_string(rv));
727                 return false;
728         }
729
730         printf("ap_name %s, user input name %s\n", ap_name, ap_name_part);
731         if (__test_compare_ap_name(ap_name, ap_name_part)) {
732                 /* Basic info */
733                 printf("ESSID : %s\n", ap_name);
734
735                 if (wifi_ap_get_bssid(ap, &str_value) == WIFI_ERROR_NONE) {
736                         printf("BSSID : %s\n", str_value);
737                         g_free(str_value);
738                 } else
739                         printf("Fail to get BSSID\n");
740
741                 if (wifi_ap_get_rssi(ap, &int_value) == WIFI_ERROR_NONE)
742                         printf("RSSI : %d\n", int_value);
743                 else
744                         printf("Fail to get RSSI\n");
745
746                 if (wifi_ap_get_frequency(ap, &int_value) == WIFI_ERROR_NONE)
747                         printf("Frequency : %d\n", int_value);
748                 else
749                         printf("Fail to get Frequency\n");
750
751                 if (wifi_ap_get_max_speed(ap, &int_value) == WIFI_ERROR_NONE)
752                         printf("Max speed : %d\n", int_value);
753                 else
754                         printf("Fail to get Max speed\n");
755
756                 if (wifi_ap_is_favorite(ap, &bool_value) == WIFI_ERROR_NONE)
757                         printf("Favorite : %s\n", bool_value ? "TRUE" : "FALSE");
758                 else
759                         printf("Fail to get Favorite\n");
760
761                 /* Network info */
762                 if (wifi_ap_get_connection_state(ap, &conn_state) == WIFI_ERROR_NONE)
763                         printf("Connection State : %d\n", conn_state);
764                 else
765                         printf("Fail to get Connection State\n");
766
767                 rv = test_get_user_int("Input Address type to get"
768                                                         "(0:IPV4, 1:IPV6):", &address_type);
769
770                 if (rv == false || (address_type != 0 && address_type != 1)) {
771                         printf("Invalid input!!\n");
772                         return false;
773                 }
774
775                 if (wifi_ap_get_ip_config_type(ap, address_type, &ip_type) == WIFI_ERROR_NONE)
776                         printf("IP config type : %d\n", ip_type);
777                 else
778                         printf("Fail to get IP config type\n");
779
780                 if (wifi_ap_get_ip_address(ap, address_type, &str_value) == WIFI_ERROR_NONE) {
781                         printf("IP : %s\n", str_value);
782                         g_free(str_value);
783                 } else
784                         printf("Fail to get IP\n");
785
786                 if (wifi_ap_get_subnet_mask(ap, address_type, &str_value) == WIFI_ERROR_NONE) {
787                         printf("Subnet mask : %s\n", str_value);
788                         g_free(str_value);
789                 } else
790                         printf("Fail to get Subnet mask\n");
791
792                 if (wifi_ap_get_gateway_address(ap, address_type, &str_value) == WIFI_ERROR_NONE) {
793                         printf("Gateway : %s\n", str_value);
794                         g_free(str_value);
795                 } else
796                         printf("Fail to get Gateway\n");
797
798                 if (wifi_ap_get_proxy_type(ap, &proxy_type) == WIFI_ERROR_NONE)
799                         printf("Proxy type : %d\n", proxy_type);
800                 else
801                         printf("Fail to get Proxy type\n");
802
803                 if (wifi_ap_get_proxy_address(ap, address_type, &str_value) == WIFI_ERROR_NONE) {
804                         printf("Proxy : %s\n", str_value);
805                         g_free(str_value);
806                 } else
807                         printf("Fail to get Proxy\n");
808
809                 if (wifi_ap_get_dns_address(ap, 1, address_type, &str_value) == WIFI_ERROR_NONE) {
810                         printf("DNS1 : %s\n", str_value);
811                         g_free(str_value);
812                 } else
813                         printf("Fail to get DNS1\n");
814
815                 if (wifi_ap_get_dns_address(ap, 2, address_type, &str_value) == WIFI_ERROR_NONE) {
816                         printf("DNS2 : %s\n", str_value);
817                         g_free(str_value);
818                 } else
819                         printf("Fail to get DNS2\n");
820
821                 /* Security info */
822                 if (wifi_ap_get_security_type(ap, &sec_type) == WIFI_ERROR_NONE)
823                         printf("Security type : %d\n", sec_type);
824                 else
825                         printf("Fail to get Security type\n");
826
827                 if (wifi_ap_get_encryption_type(ap, &enc_type) == WIFI_ERROR_NONE)
828                         printf("Encryption type : %d\n", enc_type);
829                 else
830                         printf("Fail to get Encryption type\n");
831
832                 if (wifi_ap_is_passphrase_required(ap, &bool_value) == WIFI_ERROR_NONE)
833                         printf("Passphrase required : %s\n", bool_value ? "TRUE" : "FALSE");
834                 else
835                         printf("Fail to get Passphrase required\n");
836
837                 if (wifi_ap_is_wps_supported(ap, &bool_value) == WIFI_ERROR_NONE)
838                         printf("WPS supported : %s\n", bool_value ? "TRUE" : "FALSE");
839                 else
840                         printf("Fail to get WPS supported\n");
841
842                 if (sec_type != WIFI_SECURITY_TYPE_EAP) {
843                         g_free(ap_name);
844                         return false;
845                 }
846
847                 /* EAP info */
848                 if (wifi_ap_get_eap_type(ap, &eap_type) == WIFI_ERROR_NONE)
849                         printf("EAP type : %d\n", eap_type);
850                 else
851                         printf("Fail to get EAP type\n");
852
853                 if (wifi_ap_get_eap_auth_type(ap, &eap_auth_type) == WIFI_ERROR_NONE)
854                         printf("EAP auth type : %d\n", eap_auth_type);
855                 else
856                         printf("Fail to get EAP auth type\n");
857
858                 if (wifi_ap_get_eap_passphrase(ap, &str_value, &bool_value) == WIFI_ERROR_NONE) {
859                         printf("EAP user name : %s\n", str_value);
860                         printf("EAP is password setted : %s\n", bool_value ? "TRUE" : "FALSE");
861                         g_free(str_value);
862                 } else
863                         printf("Fail to get EAP passphrase(user name/password)\n");
864
865                 if (wifi_ap_get_eap_ca_cert_file(ap, &str_value) == WIFI_ERROR_NONE) {
866                         printf("EAP ca cert file : %s\n", str_value);
867                         g_free(str_value);
868                 } else
869                         printf("Fail to get EAP ca cert file\n");
870
871                 if (wifi_ap_get_eap_client_cert_file(ap, &str_value) == WIFI_ERROR_NONE) {
872                         printf("EAP client cert file : %s\n", str_value);
873                         g_free(str_value);
874                 } else
875                         printf("Fail to get EAP client cert file\n");
876
877                 if (wifi_ap_get_eap_private_key_file(ap, &str_value) == WIFI_ERROR_NONE) {
878                         printf("EAP private key file : %s\n", str_value);
879                         g_free(str_value);
880                 } else
881                         printf("Fail to get EAP private key file\n");
882
883                 g_free(ap_name);
884                 return false;
885         }
886
887         g_free(ap_name);
888         return true;
889 }
890
891 static bool _test_config_list_cb(const wifi_config_h config, void *user_data)
892 {
893         gchar *name = NULL;
894         wifi_security_type_e security_type;
895
896         wifi_config_get_name(config, &name);
897         wifi_config_get_security_type(config, &security_type);
898
899         printf("Name[%s] ", name);
900         printf("Security type[%d] ", security_type);
901         if (security_type == WIFI_SECURITY_TYPE_EAP) {
902                 wifi_eap_type_e eap_type;
903                 wifi_eap_auth_type_e eap_auth_type;
904                 wifi_config_get_eap_type(config, &eap_type);
905                 printf("Eap type[%d] ", eap_type);
906                 wifi_config_get_eap_auth_type(config, &eap_auth_type);
907                 printf("Eap auth type[%d]", eap_auth_type);
908         }
909         printf("\n");
910
911         g_free(name);
912
913         return true;
914 }
915
916 struct _wifi_conf {
917         char name[33];
918         int type;
919 };
920 static bool _test_config_list_cb_for_remove(const wifi_config_h config, void *user_data)
921 {
922
923         struct _wifi_conf *c = (struct _wifi_conf *)user_data;
924         gchar *name = NULL;
925         wifi_security_type_e security_type;
926
927         wifi_config_get_name(config, &name);
928         wifi_config_get_security_type(config, &security_type);
929
930         if (strncmp(name, c->name, sizeof(c->name)) == 0 && security_type == c->type) {
931                 int rv = wifi_config_remove(config);
932                 if (rv != WIFI_ERROR_NONE)
933                         printf("Fail to remove configurations [%s]\n", __test_convert_error_to_string(rv));
934                 else
935                         printf("Success to remove configuration [%s]\n", name);
936                 g_free(name);
937                 return false;
938         }
939
940         g_free(name);
941         return true;
942 }
943
944 static bool __test_found_specific_aps_callback(wifi_ap_h ap, void *user_data)
945 {
946         printf("Found specific ap Completed\n");
947
948         int rv;
949         char *ap_name = NULL;
950         wifi_security_type_e security_type = WIFI_SECURITY_TYPE_NONE;
951
952         rv = wifi_ap_get_essid(ap, &ap_name);
953         if (rv != WIFI_ERROR_NONE) {
954                 printf("Fail to get AP name [%s]\n", __test_convert_error_to_string(rv));
955                 return -1;
956         }
957         printf("[AP name] : %s\n", ap_name);
958
959         rv = wifi_ap_get_security_type(ap, &security_type);
960         if (rv == WIFI_ERROR_NONE)
961                 printf("[Security type] : %d\n", security_type);
962         else {
963                 printf("Fail to get Security type\n");
964                 g_free(ap_name);
965                 return false;
966         }
967
968         switch (security_type) {
969         case WIFI_SECURITY_TYPE_WEP:
970         case WIFI_SECURITY_TYPE_WPA_PSK:
971         case WIFI_SECURITY_TYPE_WPA2_PSK:
972                 {
973                         char passphrase[100];
974                         printf("Input passphrase for %s : ", ap_name);
975                         rv = scanf("%99s", passphrase);
976
977                         rv = wifi_ap_set_passphrase(ap, passphrase);
978                         if (rv != WIFI_ERROR_NONE) {
979                                 printf("Fail to set passphrase : %s\n", __test_convert_error_to_string(rv));
980                                 g_free(ap_name);
981                                 return false;
982                         }
983                 }
984                 break;
985         case WIFI_SECURITY_TYPE_EAP:
986                 {
987                         char input_str1[100];
988                         printf("Input user name for %s : ", ap_name);
989                         rv = scanf("%99s", input_str1);
990
991                         char input_str2[100];
992                         printf("Input password for %s : ", ap_name);
993                         rv = scanf("%99s", input_str2);
994
995                         rv = wifi_ap_set_eap_passphrase(ap, input_str1, input_str2);
996                         if (rv != WIFI_ERROR_NONE) {
997                                 printf("Fail to set eap passphrase : %s\n", __test_convert_error_to_string(rv));
998                                 g_free(ap_name);
999                                 return false;
1000                         }
1001
1002                         char *inputed_name = NULL;
1003                         bool is_pass_set;
1004                         rv = wifi_ap_get_eap_passphrase(ap, &inputed_name, &is_pass_set);
1005                         if (rv != WIFI_ERROR_NONE) {
1006                                 printf("Fail to get eap passphrase : %s\n", __test_convert_error_to_string(rv));
1007                                 g_free(ap_name);
1008                                 return false;
1009                         }
1010
1011                         printf("name : %s, is password set : %s\n", inputed_name, is_pass_set ? "TRUE" : "FALSE");
1012                         g_free(inputed_name);
1013                 }
1014                 break;
1015         case WIFI_SECURITY_TYPE_NONE:
1016         default:
1017                 break;
1018         }
1019
1020         rv = wifi_connect(ap, __test_connected_callback, NULL);
1021         if (rv != WIFI_ERROR_NONE)
1022                 printf("Fail to connection request [%s] : %s\n", ap_name, __test_convert_error_to_string(rv));
1023         else
1024                 printf("Success to connection request [%s]\n", ap_name);
1025
1026         g_free(ap_name);
1027         return true;
1028 }
1029
1030 static void __test_scan_specific_ap_callback(wifi_error_e error_code, void* user_data)
1031 {
1032         int rv;
1033
1034         printf("Specific scan Completed from scan request, error code : %s\n",
1035                         __test_convert_error_to_string(error_code));
1036
1037         if (error_code != WIFI_ERROR_NONE)
1038                 return;
1039
1040         rv = wifi_foreach_found_specific_aps(__test_found_specific_aps_callback, user_data);
1041         if (rv != WIFI_ERROR_NONE) {
1042                 printf("Fail to get specific AP(can't get AP list) [%s]\n", __test_convert_error_to_string(rv));
1043                 return;
1044         }
1045 }
1046
1047 int test_wifi_init(void)
1048 {
1049         int rv = wifi_initialize();
1050
1051         if (rv == WIFI_ERROR_NONE) {
1052                 wifi_set_device_state_changed_cb(__test_device_state_callback, NULL);
1053                 wifi_set_background_scan_cb(__test_bg_scan_completed_callback, NULL);
1054                 wifi_set_connection_state_changed_cb(__test_connection_state_callback, NULL);
1055                 wifi_set_rssi_level_changed_cb(__test_rssi_level_callback, NULL);
1056         } else {
1057                 printf("Wifi init failed [%s]\n", __test_convert_error_to_string(rv));
1058                 return -1;
1059         }
1060
1061         printf("Wifi init succeeded\n");
1062         return 1;
1063 }
1064
1065 int  test_wifi_deinit(void)
1066 {
1067         int rv = 0;
1068
1069         rv = wifi_deinitialize();
1070
1071         if (rv != WIFI_ERROR_NONE) {
1072                 printf("Wifi deinit failed [%s]\n", __test_convert_error_to_string(rv));
1073                 return -1;
1074         }
1075
1076         printf("Wifi deinit succeeded\n");
1077         return 1;
1078 }
1079
1080 int test_wifi_activate(void)
1081 {
1082         int rv = 0;
1083
1084         bool state = false;
1085
1086         rv = wifi_is_activated(&state);
1087
1088         if (state != true) {
1089                 rv = wifi_activate(__test_activated_callback, NULL);
1090
1091                 if (rv != WIFI_ERROR_NONE) {
1092                         printf("Fail to activate Wi-Fi device [%s]", __test_convert_error_to_string(rv));
1093                         return -1;
1094                 }
1095         } else {
1096                 __test_activated_callback(WIFI_ERROR_NONE, NULL);
1097         }
1098
1099         return 1;
1100 }
1101
1102 int test_wifi_deactivate(void)
1103 {
1104         int rv = 0;
1105         bool state = false;
1106
1107         rv = wifi_is_activated(&state);
1108
1109         if (state == true) {
1110                 rv = wifi_deactivate(__test_deactivated_callback, NULL);
1111
1112                 if (rv != WIFI_ERROR_NONE) {
1113                         printf("Fail to deactivate Wi-Fi device [%s]\n", __test_convert_error_to_string(rv));
1114                         return -1;
1115                 }
1116         } else {
1117                 __test_deactivated_callback(WIFI_ERROR_NONE, NULL);
1118         }
1119
1120         return 1;
1121 }
1122
1123 int test_is_activated(void)
1124 {
1125         int rv = 0;
1126         bool state = false;
1127
1128         rv = wifi_is_activated(&state);
1129
1130         if (rv != WIFI_ERROR_NONE) {
1131                 printf("Fail to get Wi-Fi device state [%s]\n", __test_convert_error_to_string(rv));
1132                 return -1;
1133         }
1134
1135         printf("Success to get Wi-Fi device state : %s\n", (state) ? "TRUE" : "FALSE");
1136
1137         return 1;
1138 }
1139
1140 int test_get_connection_state(void)
1141 {
1142         int rv = 0;
1143         wifi_connection_state_e connection_state;
1144
1145         rv = wifi_get_connection_state(&connection_state);
1146
1147         if (rv != WIFI_ERROR_NONE) {
1148                 printf("Fail to get connection state [%s]\n", __test_convert_error_to_string(rv));
1149                 return -1;
1150         }
1151
1152         printf("Success to get connection state : ");
1153         switch (connection_state) {
1154         case WIFI_CONNECTION_STATE_ASSOCIATION:
1155                 printf("Association\n");
1156                 break;
1157         case WIFI_CONNECTION_STATE_CONNECTED:
1158                 printf("Connected\n");
1159                 break;
1160         case WIFI_CONNECTION_STATE_CONFIGURATION:
1161                 printf("Configuration\n");
1162                 break;
1163         case WIFI_CONNECTION_STATE_DISCONNECTED:
1164                 printf("Disconnected\n");
1165                 break;
1166         default:
1167                 printf("Unknown\n");
1168         }
1169
1170         return 1;
1171 }
1172
1173 int test_get_mac_address(void)
1174 {
1175         int rv = 0;
1176         char *mac_addr = NULL;
1177
1178         rv = wifi_get_mac_address(&mac_addr);
1179
1180         if (rv != WIFI_ERROR_NONE) {
1181                 printf("Fail to get MAC address [%s]\n", __test_convert_error_to_string(rv));
1182                 return -1;
1183         }
1184
1185         printf("MAC address : %s\n", mac_addr);
1186         g_free(mac_addr);
1187
1188         return 1;
1189 }
1190
1191 int test_get_interface_name(void)
1192 {
1193         int rv = 0;
1194         char *if_name = NULL;
1195
1196         rv = wifi_get_network_interface_name(&if_name);
1197
1198         if (rv != WIFI_ERROR_NONE) {
1199                 printf("Fail to get Interface name [%s]\n", __test_convert_error_to_string(rv));
1200                 return -1;
1201         }
1202
1203         printf("Interface name : %s\n", if_name);
1204         g_free(if_name);
1205
1206         return 1;
1207 }
1208
1209 int test_scan_request(void)
1210 {
1211         int rv = 0;
1212
1213         rv = wifi_scan(__test_scan_request_callback, NULL);
1214
1215         if (rv != WIFI_ERROR_NONE) {
1216                 printf("Scan request failed [%s]\n", __test_convert_error_to_string(rv));
1217                 return -1;
1218         }
1219
1220         printf("Scan request succeeded\n");
1221
1222         return 1;
1223 }
1224
1225 int test_get_connected_ap(void)
1226 {
1227         int rv = 0;
1228         char *ap_name = NULL;
1229         wifi_ap_h ap_h;
1230
1231         rv = wifi_get_connected_ap(&ap_h);
1232         if (rv != WIFI_ERROR_NONE) {
1233                 printf("Fail to get connected AP [%s]\n", __test_convert_error_to_string(rv));
1234                 return -1;
1235         }
1236
1237         rv = wifi_ap_get_essid(ap_h, &ap_name);
1238         if (rv != WIFI_ERROR_NONE) {
1239                 printf("Fail to get essid [%s]\n", __test_convert_error_to_string(rv));
1240                 wifi_ap_destroy(ap_h);
1241                 return -1;
1242         }
1243
1244         printf("Connected AP : %s\n", ap_name);
1245         g_free(ap_name);
1246         wifi_ap_destroy(ap_h);
1247
1248         return 1;
1249 }
1250
1251 int test_foreach_found_aps(void)
1252 {
1253         int rv = 0;
1254
1255         rv = wifi_foreach_found_aps(__test_found_ap_callback, NULL);
1256         if (rv != WIFI_ERROR_NONE) {
1257                 printf("Fail to get AP list [%s]\n", __test_convert_error_to_string(rv));
1258                 return -1;
1259         }
1260
1261         printf("Get AP list finished\n");
1262
1263         return 1;
1264 }
1265
1266 int test_connect_ap(void)
1267 {
1268         int rv = 0;
1269         char ap_name[33];
1270         bool state = false;
1271
1272         wifi_is_activated(&state);
1273         if (state == false)
1274                 return -1;
1275
1276         printf("Input a part of AP name to connect : ");
1277         rv = scanf("%32s", ap_name);
1278
1279         rv = wifi_foreach_found_aps(__test_found_connect_ap_callback, ap_name);
1280         if (rv != WIFI_ERROR_NONE) {
1281                 printf("Fail to connect (can't get AP list) [%s]\n", __test_convert_error_to_string(rv));
1282                 return -1;
1283         }
1284
1285         printf("Connection step finished\n");
1286         return 1;
1287 }
1288
1289 int test_connect_specific_ap(void)
1290 {
1291         int rv;
1292         char ap_name[33];
1293
1294         printf("Input a part of specific AP name to connect : ");
1295         rv = scanf("%32s", ap_name);
1296         if (rv <= 0)
1297                 return -1;
1298
1299         rv = wifi_scan_specific_ap(ap_name, __test_scan_specific_ap_callback, NULL);
1300
1301         if (rv != WIFI_ERROR_NONE) {
1302                 printf("Scan request failed [%s]\n", __test_convert_error_to_string(rv));
1303                 return -1;
1304         }
1305
1306         printf("Scan specific AP request succeeded\n");
1307         return 1;
1308 }
1309
1310 int test_disconnect_ap(void)
1311 {
1312         int rv = 0;
1313         char ap_name[33];
1314         bool state = false;
1315
1316         wifi_is_activated(&state);
1317         if (state == false)
1318                 return -1;
1319
1320         printf("Input a part of AP name to disconnect : ");
1321         rv = scanf("%32s", ap_name);
1322
1323         rv = wifi_foreach_found_aps(__test_found_disconnect_ap_callback, ap_name);
1324         if (rv != WIFI_ERROR_NONE) {
1325                 printf("Fail to disconnect (can't get AP list) [%s]\n", __test_convert_error_to_string(rv));
1326                 return -1;
1327         }
1328
1329         printf("Disconnection step finished\n");
1330         return 1;
1331 }
1332
1333 int test_connect_wps(void)
1334 {
1335         int rv = 0;
1336         char ap_name[33];
1337         bool state = false;
1338
1339         wifi_is_activated(&state);
1340         if (state == false)
1341                 return -1;
1342
1343         printf("Input a part of AP name to connect by wps : ");
1344         rv = scanf("%32s", ap_name);
1345
1346         rv = wifi_foreach_found_aps(__test_found_connect_wps_callback, ap_name);
1347         if (rv != WIFI_ERROR_NONE) {
1348                 printf("Fail to connect (can't get AP list) [%s]\n", __test_convert_error_to_string(rv));
1349                 return -1;
1350         }
1351
1352         printf("Connection step finished\n");
1353         return 1;
1354 }
1355
1356 int test_forget_ap(void)
1357 {
1358         int rv = 0;
1359         char ap_name[33];
1360         bool state = false;
1361
1362         wifi_is_activated(&state);
1363         if (state == false)
1364                 return -1;
1365
1366         printf("Input a part of AP name to forget : ");
1367         rv = scanf("%32s", ap_name);
1368
1369         rv = wifi_foreach_found_aps(__test_found_forget_ap_callback, ap_name);
1370         if (rv != WIFI_ERROR_NONE) {
1371                 printf("Fail to forget (can't get AP list) [%s]\n", __test_convert_error_to_string(rv));
1372                 return -1;
1373         }
1374
1375         printf("Forget AP finished\n");
1376         return 1;
1377 }
1378
1379 int test_connect_eap_ap(void)
1380 {
1381         int rv = 0;
1382         char ap_name[33];
1383         bool state = false;
1384
1385         wifi_is_activated(&state);
1386         if (state == false)
1387                 return -1;
1388
1389         printf("Input a part of AP name to connect : ");
1390         rv = scanf("%32s", ap_name);
1391
1392         rv = wifi_foreach_found_aps(__test_found_eap_ap_callback, ap_name);
1393         if (rv != WIFI_ERROR_NONE) {
1394                 printf("Fail to connect (can't get AP list) [%s]\n", __test_convert_error_to_string(rv));
1395                 return -1;
1396         }
1397
1398         printf("Connection step finished\n");
1399         return 1;
1400 }
1401
1402 int test_set_ip_method(void)
1403 {
1404         int rv;
1405         char ap_name[33];
1406         bool state;
1407
1408         rv = wifi_is_activated(&state);
1409         if (rv != WIFI_ERROR_NONE || state == false)
1410                 return -1;
1411
1412         printf("Input a part of AP name to change IP method : ");
1413         rv = scanf("%32s", ap_name);
1414         if (rv <= 0)
1415                 return -1;
1416
1417         rv = wifi_foreach_found_aps(__test_found_change_ip_method_callback, ap_name);
1418         if (rv != WIFI_ERROR_NONE) {
1419                 printf("Fail to change (can't get AP list) [%s]\n", __test_convert_error_to_string(rv));
1420                 return -1;
1421         }
1422
1423         printf("IP method changing finished\n");
1424         return 1;
1425 }
1426
1427 int test_set_proxy_method(void)
1428 {
1429         int rv;
1430         char ap_name[33];
1431         bool state;
1432
1433         rv = wifi_is_activated(&state);
1434         if (rv != WIFI_ERROR_NONE || state == false)
1435                 return -1;
1436
1437         printf("Input a part of AP name to change Proxy method : ");
1438         rv = scanf("%32s", ap_name);
1439         if (rv <= 0)
1440                 return -1;
1441
1442         rv = wifi_foreach_found_aps(__test_found_change_proxy_method_callback, ap_name);
1443         if (rv != WIFI_ERROR_NONE) {
1444                 printf("Fail to change (can't get AP list) [%s]\n", __test_convert_error_to_string(rv));
1445                 return -1;
1446         }
1447
1448         printf("Proxy method changing finished\n");
1449         return 1;
1450 }
1451
1452 int test_get_ap_info(void)
1453 {
1454         int rv;
1455         char ap_name[33];
1456         bool state;
1457
1458         rv = wifi_is_activated(&state);
1459         if (rv != WIFI_ERROR_NONE || state == false)
1460                 return -1;
1461
1462         printf("Input a part of AP name to get detailed info : ");
1463         rv = scanf("%32s", ap_name);
1464         if (rv <= 0)
1465                 return -1;
1466
1467         rv = wifi_foreach_found_aps(__test_found_print_ap_info_callback, ap_name);
1468         if (rv != WIFI_ERROR_NONE) {
1469                 printf("Fail to get AP info (can't get AP list) [%s]\n", __test_convert_error_to_string(rv));
1470                 return -1;
1471         }
1472
1473         printf("AP info printing finished\n");
1474         return 1;
1475 }
1476
1477 int test_load_configuration(void)
1478 {
1479         int rv;
1480
1481         rv = wifi_config_foreach_configuration(_test_config_list_cb, NULL);
1482         if (rv != WIFI_ERROR_NONE) {
1483                 printf("Fail to get configurations [%s]\n", __test_convert_error_to_string(rv));
1484                 return -1;
1485         }
1486
1487         return 1;
1488 }
1489
1490 int test_save_configuration(void)
1491 {
1492         int rv;
1493         char name[33] = { 0, };
1494         char passphrase[100] = { 0, };
1495         int type = 0;
1496         wifi_config_h config;
1497
1498         printf("Input AP configuration\n");
1499         printf("Name : ");
1500         rv = scanf("%32s", name);
1501         if (rv <= 0)
1502                 return -1;
1503
1504         printf("Passphrase : ");
1505         rv = scanf("%99s", passphrase);
1506         if (rv <= 0)
1507                 return -1;
1508
1509         printf("Security type(None(0), WEP(1), WPA-PSK(2), EAP(4) : ");
1510         rv = scanf("%d", &type);
1511         if (rv <= 0)
1512                 return -1;
1513
1514         rv = wifi_config_create(name, passphrase, type, &config);
1515         if (rv != WIFI_ERROR_NONE)
1516                 return -1;
1517
1518         rv = wifi_config_save_configuration(config);
1519         if (rv != WIFI_ERROR_NONE)
1520                 return -1;
1521
1522         rv = wifi_config_destroy(config);
1523         if (rv != WIFI_ERROR_NONE)
1524                 return -1;
1525
1526         return 1;
1527 }
1528
1529 int test_remove_configuration(void)
1530 {
1531         int rv;
1532         struct _wifi_conf c;
1533
1534         printf("Input AP configuration\n");
1535         printf("Name : ");
1536         rv = scanf("%32s", c.name);
1537         if (rv <= 0)
1538                 return -1;
1539
1540         printf("Security type(None(0), WEP(1), WPA-PSK(2), EAP(4) : ");
1541         rv = scanf("%d", &c.type);
1542         if (rv <= 0)
1543                 return -1;
1544
1545         rv = wifi_config_foreach_configuration(_test_config_list_cb_for_remove, &c);
1546         if (rv != WIFI_ERROR_NONE) {
1547                 printf("Fail to get configurations [%s]\n", __test_convert_error_to_string(rv));
1548                 return -1;
1549         }
1550
1551         return 1;
1552 }
1553
1554 int test_set_configuration_proxy_and_hidden(void)
1555 {
1556         int rv;
1557         char name[33] = { 0, };
1558         char passphrase[100] = { 0, };
1559         int type = 0;
1560         char proxy[100] = { 0, };
1561         int hidden = 0;
1562         wifi_config_h config;
1563
1564         printf("Input AP configuration\n");
1565         printf("Name : ");
1566         rv = scanf("%32s", name);
1567         if (rv <= 0)
1568                 return -1;
1569
1570         printf("Passphrase : ");
1571         rv = scanf("%99s", passphrase);
1572         if (rv <= 0)
1573                 return -1;
1574
1575         printf("Security type(None(0), WEP(1), WPA-PSK(2), EAP(4) : ");
1576         rv = scanf("%d", &type);
1577         if (rv <= 0)
1578                 return -1;
1579
1580         printf("Proxy(server:port) : ");
1581         rv = scanf("%99s", proxy);
1582         if (rv <= 0)
1583                 return -1;
1584
1585         printf("Hidden(1:Hidden) : ");
1586         rv = scanf("%d", &hidden);
1587         if (rv <= 0)
1588                 return -1;
1589
1590         rv = wifi_config_create(name, passphrase, type, &config);
1591         if (rv != WIFI_ERROR_NONE)
1592                 return -1;
1593
1594         rv = wifi_config_save_configuration(config);
1595         if (rv != WIFI_ERROR_NONE) {
1596                 wifi_config_destroy(config);
1597                 return -1;
1598         }
1599
1600         rv = wifi_config_set_proxy_address(config, WIFI_ADDRESS_FAMILY_IPV4, proxy);
1601         if (rv != WIFI_ERROR_NONE) {
1602                 wifi_config_destroy(config);
1603                 return -1;
1604         }
1605
1606         if (hidden == 1)
1607                 rv = wifi_config_set_hidden_ap_property(config, TRUE);
1608         else
1609                 rv = wifi_config_set_hidden_ap_property(config, FALSE);
1610         if (rv != WIFI_ERROR_NONE) {
1611                 wifi_config_destroy(config);
1612                 return -1;
1613         }
1614
1615         rv = wifi_config_destroy(config);
1616         if (rv != WIFI_ERROR_NONE)
1617                 return -1;
1618
1619         return 1;
1620 }
1621
1622 int test_set_eap_configuration(void)
1623 {
1624         int rv;
1625         char name[33] = { 0, };
1626         char passphrase[100] = { 0, };
1627         int type = WIFI_SECURITY_TYPE_EAP;
1628         wifi_config_h config;
1629
1630         printf("Input EAP configuration\n");
1631         printf("Name : ");
1632         rv = scanf("%32s", name);
1633         if (rv <= 0)
1634                 return -1;
1635
1636         printf("Passphrase : ");
1637         rv = scanf("%99s", passphrase);
1638         if (rv <= 0)
1639                 return -1;
1640
1641         rv = wifi_config_create(name, passphrase, type, &config);
1642         if (rv != WIFI_ERROR_NONE)
1643                 return -1;
1644
1645         rv = wifi_config_save_configuration(config);
1646         if (rv != WIFI_ERROR_NONE)
1647                 return -1;
1648
1649         rv = wifi_config_set_eap_type(config, WIFI_EAP_TYPE_TLS);
1650         if (rv != WIFI_ERROR_NONE)
1651                 return -1;
1652
1653         rv = wifi_config_set_eap_auth_type(config, WIFI_EAP_AUTH_TYPE_MD5);
1654         if (rv != WIFI_ERROR_NONE)
1655                 return -1;
1656
1657         rv = wifi_config_destroy(config);
1658         if (rv != WIFI_ERROR_NONE)
1659                 return -1;
1660
1661         return 1;
1662 }
1663
1664 int test_wifi_tdls_disconnect(void)
1665 {
1666         int rv = 0;
1667
1668         char peer_mac[18];
1669         printf("Enter Mac_address: ");
1670         if (scanf(" %17s", peer_mac) < 1)
1671                 return -1;
1672
1673         if (strlen(peer_mac) > 17) {
1674                 printf("Wrong Mac_address\n");
1675                 return -1;
1676         }
1677
1678         rv = wifi_tdls_disconnect(peer_mac);
1679         if (rv != WIFI_ERROR_NONE) {
1680                 printf("test_wifi_tdls_disconnect() is failed [%s]\n", __test_convert_error_to_string(rv));
1681                 return -1;
1682         }
1683         return 1;
1684 }
1685
1686 int test_wifi_tdls_get_connected_peer(void)
1687 {
1688         int rv = 0;
1689         char *mac_addr = NULL;
1690
1691         rv = wifi_tdls_get_connected_peer(&mac_addr);
1692         if (rv != WIFI_ERROR_NONE) {
1693                 printf("wifi_tdls_get_connected_peer() is failed [%s]\n", __test_convert_error_to_string(rv));
1694                 return -1;
1695         }
1696         printf("Peer Mac address is [%s]\n", mac_addr);
1697         g_free(mac_addr);
1698         return 1;
1699 }
1700
1701 int test_wifi_connect_specific_ap(void)
1702 {
1703         int rv = 0;
1704         char ap_name[33];
1705         char passphrase[65];
1706         bool state = false;
1707         int sec_type;
1708
1709         wifi_is_activated(&state);
1710         if (state == false)
1711                 return -1;
1712
1713         printf("Input hidden AP name to connect : ");
1714         rv = scanf("%32s", ap_name);
1715         printf("Input Security Type :\n");
1716         printf("0 -> WIFI_SECURITY_TYPE_NONE\n");
1717         printf("1 -> WIFI_SECURITY_TYPE_WEP\n");
1718         printf("2 -> WIFI_SECURITY_TYPE_WPA_PSK/WIFI_SECURITY_TYPE_WPA2_PSK\n");
1719         rv = scanf("%d", &sec_type);
1720         if (sec_type == 1 || sec_type == 2) {
1721                 printf("Input hidden AP passphrase : ");
1722                 rv = scanf("%64s", passphrase);
1723         }
1724
1725         rv = wifi_connect_specific_ap(ap_name, sec_type, passphrase,
1726                         __test_connected_callback, NULL);
1727         if (rv != WIFI_ERROR_NONE) {
1728                 printf("Fail to connect to hidden AP [%s]\n", __test_convert_error_to_string(rv));
1729                 return -1;
1730         }
1731
1732         printf("Hidden Connection step finished\n");
1733         return 1;
1734 }
1735
1736 int main(int argc, char **argv)
1737 {
1738         GMainLoop *mainloop;
1739 #if !GLIB_CHECK_VERSION(2, 36, 0)
1740         g_type_init();
1741 #endif
1742         mainloop = g_main_loop_new(NULL, FALSE);
1743
1744         GIOChannel *channel = g_io_channel_unix_new(0);
1745         g_io_add_watch(channel, (G_IO_IN|G_IO_ERR|G_IO_HUP|G_IO_NVAL), test_thread, NULL);
1746
1747         printf("Test Thread created...\n");
1748
1749         g_main_loop_run(mainloop);
1750
1751         return 0;
1752 }
1753
1754 gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data)
1755 {
1756         int rv;
1757         char a[10];
1758
1759         printf("Event received from stdin\n");
1760
1761         rv = read(0, a, 10);
1762
1763         if (rv <= 0 || a[0] == '0') {
1764                 rv = wifi_deinitialize();
1765
1766                 if (rv != WIFI_ERROR_NONE)
1767                         printf("Fail to deinitialize.\n");
1768
1769                 exit(1);
1770         }
1771
1772         if (a[0] == '\n' || a[0] == '\r') {
1773                 printf("\n\n Network Connection API Test App\n\n");
1774                 printf("Options..\n");
1775                 printf(LOG_GREEN "1   - Wi-Fi init and set callbacks\n" LOG_END);
1776                 printf("2   - Wi-Fi deinit(unset callbacks automatically)\n");
1777                 printf(LOG_GREEN "3   - Activate Wi-Fi device\n" LOG_END);
1778                 printf("4   - Deactivate Wi-Fi device\n");
1779                 printf("5   - Is Wi-Fi activated?\n");
1780                 printf("6   - Get connection state\n");
1781                 printf("7   - Get MAC address\n");
1782                 printf("8   - Get Wi-Fi interface name\n");
1783                 printf(LOG_GREEN "9   - Scan request\n" LOG_END);
1784                 printf("a   - Get Connected AP\n");
1785                 printf("b   - Get AP list\n");
1786                 printf(LOG_GREEN "c   - Connect\n" LOG_END);
1787                 printf("d   - Disconnect\n");
1788                 printf("e   - Connect by wps pbc\n");
1789                 printf("f   - Forget an AP\n");
1790                 printf("g   - Set & connect EAP\n");
1791                 printf("h   - Set IP method type\n");
1792                 printf("i   - Set Proxy method type\n");
1793                 printf("j   - Get Ap info\n");
1794                 printf("k   - Connect Specific AP\n");
1795                 printf("l   - Load configuration\n");
1796                 printf("m   - Save configuration\n");
1797                 printf("n   - Remove configuration\n");
1798                 printf("o   - Set configuration proxy and hidden\n");
1799                 printf("p   - Set EAP configuration\n");
1800                 printf("q   - TDLS TearDown\n");
1801                 printf("r   - TDLS Get Connected Peer\n");
1802                 printf("s   - Connect to Hidden AP\n");
1803                 printf(LOG_RED "0   - Exit \n" LOG_END);
1804
1805                 printf("ENTER  - Show options menu.......\n");
1806         }
1807
1808         switch (a[0]) {
1809         case '1':
1810                 rv = test_wifi_init();
1811                 break;
1812         case '2':
1813                 rv = test_wifi_deinit();
1814                 break;
1815         case '3':
1816                 rv = test_wifi_activate();
1817                 break;
1818         case '4':
1819                 rv = test_wifi_deactivate();
1820                 break;
1821         case '5':
1822                 rv = test_is_activated();
1823                 break;
1824         case '6':
1825                 rv = test_get_connection_state();
1826                 break;
1827         case '7':
1828                 rv = test_get_mac_address();
1829                 break;
1830         case '8':
1831                 rv = test_get_interface_name();
1832                 break;
1833         case '9':
1834                 rv = test_scan_request();
1835                 break;
1836         case 'a':
1837                 rv = test_get_connected_ap();
1838                 break;
1839         case 'b':
1840                 rv = test_foreach_found_aps();
1841                 break;
1842         case 'c':
1843                 rv = test_connect_ap();
1844                 break;
1845         case 'd':
1846                 rv = test_disconnect_ap();
1847                 break;
1848         case 'e':
1849                 rv = test_connect_wps();
1850                 break;
1851         case 'f':
1852                 rv = test_forget_ap();
1853                 break;
1854         case 'g':
1855                 rv = test_connect_eap_ap();
1856                 break;
1857         case 'h':
1858                 rv = test_set_ip_method();
1859                 break;
1860         case 'i':
1861                 rv = test_set_proxy_method();
1862                 break;
1863         case 'j':
1864                 rv = test_get_ap_info();
1865                 break;
1866         case 'k':
1867                 rv = test_connect_specific_ap();
1868                 break;
1869         case 'l':
1870                 rv = test_load_configuration();
1871                 break;
1872         case 'm':
1873                 rv = test_save_configuration();
1874                 break;
1875         case 'n':
1876                 rv = test_remove_configuration();
1877                 break;
1878         case 'o':
1879                 rv = test_set_configuration_proxy_and_hidden();
1880                 break;
1881         case 'p':
1882                 rv = test_set_eap_configuration();
1883                 break;
1884         case 'q':
1885                 rv = test_wifi_tdls_disconnect();
1886                 break;
1887         case 'r':
1888                 rv = test_wifi_tdls_get_connected_peer();
1889                 break;
1890         case 's':
1891                 rv = test_wifi_connect_specific_ap();
1892                 break;
1893         default:
1894                 break;
1895         }
1896
1897         if (rv == 1)
1898                 printf("Operation succeeded!\n");
1899         else
1900                 printf("Operation failed!\n");
1901
1902         return TRUE;
1903 }
1904