Implemented WPS feature
[platform/core/api/tethering.git] / test / tethering_test.c
1 /*
2 * Copyright (c) 2011 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 <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <unistd.h>
21 #include <glib.h>
22 #include <glib-object.h>
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
26
27 #include <vconf.h>
28
29 #include "tethering.h"
30
31 #define DISABLE_REASON_TEXT_LEN 64
32 #define COMMON_STR_BUF_LEN      32
33
34 gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data);
35
36 typedef struct {
37         tethering_enabled_cb enabled_cb;
38         tethering_disabled_cb disabled_cb;
39         tethering_connection_state_changed_cb changed_cb;
40         tethering_wifi_security_type_changed_cb security_type_changed_cb;
41         tethering_wifi_ssid_visibility_changed_cb ssid_visibility_changed_cb;
42         tethering_wifi_passphrase_changed_cb passphrase_changed_cb;
43 } __tethering_cbs;
44
45 tethering_h th = NULL;
46
47 static bool __is_err(tethering_error_e ret)
48 {
49         char *err_msg = NULL;
50
51         switch (ret) {
52         case TETHERING_ERROR_INVALID_PARAMETER:
53                 err_msg = "Wrong parameter is used";
54                 break;
55
56         case TETHERING_ERROR_OUT_OF_MEMORY:
57                 err_msg = "Memory is not enough";
58                 break;
59
60         case TETHERING_ERROR_NONE:
61                 return false;
62
63         case TETHERING_ERROR_NOT_ENABLED:
64                 err_msg = "Tethering is not enabled";
65                 break;
66
67         case TETHERING_ERROR_OPERATION_FAILED:
68                 err_msg = "Operation is failed";
69                 break;
70
71         case TETHERING_ERROR_RESOURCE_BUSY:
72                 err_msg = "Resource is busy";
73                 break;
74
75         default:
76                 err_msg = "This should not be happened";
77                 break;
78         }
79
80         g_print("##ERR: %s\n", err_msg);
81
82         return true;
83 }
84
85 static const char *__convert_tethering_type_to_str(const tethering_type_e type)
86 {
87         static char str_buf[COMMON_STR_BUF_LEN] = {0, };
88
89         switch (type) {
90         case TETHERING_TYPE_USB:
91                 g_strlcpy(str_buf, "USB Tethering", sizeof(str_buf));
92                 break;
93
94         case TETHERING_TYPE_WIFI:
95                 g_strlcpy(str_buf, "Wi-Fi Tethering", sizeof(str_buf));
96                 break;
97
98         case TETHERING_TYPE_BT:
99                 g_strlcpy(str_buf, "Bluetooth Tethering", sizeof(str_buf));
100                 break;
101
102         default:
103                 g_strlcpy(str_buf, "Unknown", sizeof(str_buf));
104                 break;
105         }
106
107         return str_buf;
108 }
109
110 static const char *__convert_disabled_code_to_str(const tethering_disabled_cause_e code)
111 {
112         static char str_buf[DISABLE_REASON_TEXT_LEN] = {0, };
113
114         switch (code) {
115         case TETHERING_DISABLED_BY_USB_DISCONNECTION:
116                 strncpy(str_buf, "disabled due to usb disconnection", sizeof(str_buf));
117                 break;
118
119         case TETHERING_DISABLED_BY_FLIGHT_MODE:
120                 strncpy(str_buf, "disabled due to flight mode on", sizeof(str_buf));
121                 break;
122
123         case TETHERING_DISABLED_BY_LOW_BATTERY:
124                 strncpy(str_buf, "disabled due to low battery", sizeof(str_buf));
125                 break;
126
127         case TETHERING_DISABLED_BY_NETWORK_CLOSE:
128                 strncpy(str_buf, "disabled due to pdp network close", sizeof(str_buf));
129                 break;
130
131         case TETHERING_DISABLED_BY_TIMEOUT:
132                 strncpy(str_buf, "disabled due to timeout", sizeof(str_buf));
133                 break;
134
135         case TETHERING_DISABLED_BY_OTHERS:
136                 strncpy(str_buf, "disabled by other apps", sizeof(str_buf));
137                 break;
138
139         case TETHERING_DISABLED_BY_REQUEST:
140                 strncpy(str_buf, "disabled by my request", sizeof(str_buf));
141                 break;
142
143         case TETHERING_DISABLED_BY_WIFI_ON:
144                 strncpy(str_buf, "disabled by Wi-Fi station on", sizeof(str_buf));
145                 break;
146
147         case TETHERING_DISABLED_BY_BT_OFF:
148                 strncpy(str_buf, "disabled by bluetooth off", sizeof(str_buf));
149                 break;
150
151         default:
152                 strncpy(str_buf, "disabled by unknown reason", sizeof(str_buf));
153                 break;
154         }
155
156         return str_buf;
157 }
158
159 static void __register_cbs(tethering_h th, __tethering_cbs *cbs, void *user_data)
160 {
161         tethering_error_e ret = TETHERING_ERROR_NONE;
162
163         ret = tethering_set_enabled_cb(th, TETHERING_TYPE_ALL,
164                         cbs->enabled_cb, user_data);
165         if (__is_err(ret) == true)
166                 g_print("tethering_set_enabled_cb is failed\n");
167
168         ret = tethering_set_disabled_cb(th, TETHERING_TYPE_ALL,
169                         cbs->disabled_cb, user_data);
170         if (__is_err(ret) == true)
171                 g_print("tethering_set_disabled_cb is failed\n");
172
173         ret = tethering_set_connection_state_changed_cb(th, TETHERING_TYPE_ALL,
174                         cbs->changed_cb, user_data);
175         if (__is_err(ret) == true)
176                 g_print("tethering_set_connection_state_changed_cb is failed\n");
177
178         ret = tethering_wifi_set_security_type_changed_cb(th,
179                         cbs->security_type_changed_cb, user_data);
180         if (__is_err(ret) == true)
181                 g_print("tethering_wifi_set_security_type_changed_cb is failed\n");
182
183         ret = tethering_wifi_set_ssid_visibility_changed_cb(th,
184                         cbs->ssid_visibility_changed_cb, user_data);
185         if (__is_err(ret) == true)
186                 g_print("tethering_wifi_set_ssid_visibility_changed_cb is failed\n");
187
188         ret = tethering_wifi_set_passphrase_changed_cb(th,
189                         cbs->passphrase_changed_cb, user_data);
190         if (__is_err(ret) == true)
191                 g_print("tethering_wifi_set_passphrase_changed_cb is failed\n");
192
193         return;
194 }
195
196 static void __deregister_cbs(tethering_h th)
197 {
198         tethering_error_e ret = TETHERING_ERROR_NONE;
199
200         ret = tethering_unset_enabled_cb(th, TETHERING_TYPE_ALL);
201         if (__is_err(ret) == true)
202                 g_print("tethering_unset_enabled_cb is failed\n");
203
204         ret = tethering_unset_disabled_cb(th, TETHERING_TYPE_ALL);
205         if (__is_err(ret) == true)
206                 g_print("tethering_unset_disabled_cb is failed\n");
207
208         ret = tethering_unset_connection_state_changed_cb(th, TETHERING_TYPE_ALL);
209         if (__is_err(ret) == true)
210                 g_print("tethering_unset_connection_state_changed_cb is failed\n");
211
212         ret = tethering_wifi_unset_security_type_changed_cb(th);
213         if (__is_err(ret) == true)
214                 g_print("tethering_wifi_unset_security_type_changed_cb is failed\n");
215
216         ret = tethering_wifi_unset_ssid_visibility_changed_cb(th);
217         if (__is_err(ret) == true)
218                 g_print("tethering_wifi_unset_ssid_visibility_changed_cb is failed\n");
219
220         ret = tethering_wifi_unset_passphrase_changed_cb(th);
221         if (__is_err(ret) == true)
222                 g_print("tethering_wifi_unset_passphrase_changed_cb is failed\n");
223
224         return;
225 }
226
227 /* Tethering callbacks */
228 static void __enabled_cb(tethering_error_e error, tethering_type_e type, bool is_requested, void *data)
229 {
230         if (__is_err(error)) {
231                 if (!is_requested)
232                         return;
233
234                 g_print("## %s is not enabled. error code[0x%X]\n",
235                                 __convert_tethering_type_to_str(type),
236                                 error);
237                 return;
238         }
239
240         if (is_requested)
241                 g_print("## %s is enabled successfully\n",
242                                 __convert_tethering_type_to_str(type));
243         else
244                 g_print("## %s is enabled by other app\n",
245                                 __convert_tethering_type_to_str(type));
246
247         return;
248 }
249
250 static void __disabled_cb(tethering_error_e error, tethering_type_e type, tethering_disabled_cause_e code, void *data)
251 {
252         if (__is_err(error)) {
253                 if (code != TETHERING_DISABLED_BY_REQUEST)
254                         return;
255
256                 g_print("## %s is not disabled. error code[0x%X]\n",
257                                 __convert_tethering_type_to_str(type), error);
258                 return;
259         }
260
261         g_print("## %s is %s\n",
262                         __convert_tethering_type_to_str(type),
263                         __convert_disabled_code_to_str(code));
264
265         return;
266 }
267
268 static void __connection_state_changed_cb(tethering_client_h client, bool open, void *data)
269 {
270         tethering_client_h clone = NULL;
271         tethering_type_e type;
272         char *ip_address = NULL;
273         char *mac_address = NULL;
274         char *hostname = NULL;
275
276         tethering_client_clone(&clone, client);
277         if (clone == NULL) {
278                 g_print("tetheirng_client_clone is failed\n");
279                 return;
280         }
281
282         tethering_client_get_tethering_type(clone, &type);
283         tethering_client_get_ip_address(clone,
284                         TETHERING_ADDRESS_FAMILY_IPV4, &ip_address);
285         tethering_client_get_mac_address(clone, &mac_address);
286         tethering_client_get_name(clone, &hostname);
287
288         if (open) {
289                 g_print("## New station Type [%s], IP [%s], MAC [%s], hostname [%s]\n",
290                                 __convert_tethering_type_to_str(type),
291                                 ip_address, mac_address, hostname);
292         } else {
293                 g_print("## Disconnected station Type [%s], IP [%s], MAC [%s], hostname [%s]\n",
294                                 __convert_tethering_type_to_str(type),
295                                 ip_address, mac_address, hostname);
296         }
297
298         if (ip_address)
299                 free(ip_address);
300         if (mac_address)
301                 free(mac_address);
302         if (hostname)
303                 free(hostname);
304
305         tethering_client_destroy(clone);
306
307         return;
308 }
309
310 static void __data_usage_cb(tethering_error_e result, unsigned long long received_data,
311                 unsigned long long sent_data, void *user_data)
312 {
313         g_print("__data_usage_cb\n");
314
315         if (result != TETHERING_ERROR_NONE) {
316                 g_print("tethering_get_data_usage is failed. error[0x%X]\n", result);
317                 return;
318         }
319
320         g_print("## Received data : %llu bytes\n", received_data);
321         g_print("## Sent data : %llu bytes\n", sent_data);
322
323         return;
324 }
325
326 static void __settings_reloaded_cb(tethering_error_e result, void *user_data)
327 {
328         g_print("__settings_reloaded_cb\n");
329
330         if (result != TETHERING_ERROR_NONE) {
331                 g_print("tethering_wifi_reload_settings is failed. error[0x%X]\n", result);
332                 return;
333         }
334
335         g_print("## Wi-Fi tethering setting is reloaded\n");
336
337         return;
338 }
339
340 static bool __clients_foreach_cb(tethering_client_h client, void *data)
341 {
342         tethering_client_h clone = NULL;
343         tethering_type_e type;
344         char *ip_address = NULL;
345         char *mac_address = NULL;
346         char *hostname = NULL;
347
348         /* Clone internal information */
349         if (tethering_client_clone(&clone, client) != TETHERING_ERROR_NONE) {
350                 g_print("tethering_client_clone is failed\n");
351                 return false;
352         }
353
354         /* Get information */
355         if (tethering_client_get_tethering_type(clone, &type) != TETHERING_ERROR_NONE)
356                 g_print("tethering_client_get_type is failed\n");
357
358         if (tethering_client_get_ip_address(clone, TETHERING_ADDRESS_FAMILY_IPV4, &ip_address) != TETHERING_ERROR_NONE)
359                 g_print("tethering_client_get_ip_address is failed\n");
360
361         if (tethering_client_get_mac_address(clone, &mac_address) != TETHERING_ERROR_NONE)
362                 g_print("tethering_client_get_mac_address is failed\n");
363
364         if (tethering_client_get_name(clone, &hostname) != TETHERING_ERROR_NONE)
365                 g_print("tethering_client_get_hostname is failed\n");
366
367         /* End of getting information */
368
369         g_print("\n< Client Info. >\n");
370         g_print("\tType %s\n", __convert_tethering_type_to_str(type));
371         g_print("\tIP Address %s\n", ip_address);
372         g_print("\tMAC Address : %s\n", mac_address);
373         g_print("\tHostname : %s\n", hostname);
374
375         /* Destroy cloned objects */
376         if (ip_address)
377                 free(ip_address);
378         if (mac_address)
379                 free(mac_address);
380         if (hostname)
381                 free(hostname);
382
383         tethering_client_destroy(clone);
384
385         /* Continue iteration */
386         return true;
387 }
388
389 static void __security_type_changed_cb(tethering_wifi_security_type_e changed_type, void *user_data)
390 {
391
392         char *sec_str = NULL;
393
394         switch (changed_type) {
395         case TETHERING_WIFI_SECURITY_TYPE_NONE:
396                 sec_str = "open";
397                 break;
398         case TETHERING_WIFI_SECURITY_TYPE_WPA2_PSK:
399                 sec_str = "wpa2-psk";
400                 break;
401         case TETHERING_WIFI_SECURITY_TYPE_WPS:
402                 sec_str = "wps";
403                 break;
404         default:
405                 sec_str = "unknown";
406                 break;
407         }
408         g_print("Wi-Fi Tethering Security type is changed to [%s]\n", sec_str);
409
410         return;
411 }
412
413 static void __ssid_visibility_changed_cb(bool changed_visible, void *user_data)
414 {
415         g_print("SSID visibility for Wi-Fi tethering changed to [%s]\n",
416                         changed_visible ? "visible" : "invisible");
417         return;
418 }
419
420 static void __passphrase_changed_cb(void *user_data)
421 {
422         g_print("Wi-Fi Tethering passphrase is changed\n");
423         return;
424 }
425 /* End of tethering callbacks */
426
427 static void __print_interface_info(tethering_h th, tethering_type_e type)
428 {
429         char *interface = NULL;
430         char *mac_address = NULL;
431         char *ip_address = NULL;
432         char *gateway_address = NULL;
433         char *subnet_mask = NULL;
434
435         if (tethering_is_enabled(th, type) == FALSE) {
436                 g_print("%s is not enabled\n",
437                                 __convert_tethering_type_to_str(type));
438                 return;
439         }
440
441         tethering_get_network_interface_name(th, type, &interface);
442         tethering_get_mac_address(th, type, &mac_address);
443         tethering_get_ip_address(th, type, TETHERING_ADDRESS_FAMILY_IPV4,
444                         &ip_address);
445         tethering_get_gateway_address(th, type, TETHERING_ADDRESS_FAMILY_IPV4,
446                         &gateway_address);
447         tethering_get_subnet_mask(th, type, TETHERING_ADDRESS_FAMILY_IPV4,
448                         &subnet_mask);
449
450         g_print("interface name : %s\n", interface);
451         g_print("mac address : %s\n", mac_address);
452         g_print("ip address : %s\n", ip_address);
453         g_print("gateway address: %s\n", gateway_address);
454         g_print("subnet mask : %s\n", subnet_mask);
455
456         if (interface)
457                 free(interface);
458         if (mac_address)
459                 free(mac_address);
460         if (ip_address)
461                 free(ip_address);
462         if (gateway_address)
463                 free(gateway_address);
464         if (subnet_mask)
465                 free(subnet_mask);
466
467         return;
468 }
469
470 static void __print_wifi_tethering_setting(tethering_h th)
471 {
472         char *ssid = NULL;
473         char *passphrase = NULL;
474         char *sec_str = NULL;
475         bool visibility = false;
476         bool mac_filter = 0;
477         bool forwarding_enabled = false;
478         bool filtering_enabled = false;
479         int channel = 0;
480         int max_connected = 0;
481         tethering_wifi_security_type_e security_type = TETHERING_WIFI_SECURITY_TYPE_NONE;
482         tethering_wifi_mode_type_e hw_mode = TETHERING_WIFI_MODE_TYPE_G;
483
484         int error = TETHERING_ERROR_NONE;
485
486         error = tethering_wifi_get_ssid(th, &ssid);
487         if (error != TETHERING_ERROR_NONE)
488                 __is_err(error);
489         else
490                 g_print("\n\t** WiFi tethering SSID : %s\n", ssid);
491
492         error = tethering_wifi_get_passphrase(th, &passphrase);
493         if (error != TETHERING_ERROR_NONE)
494                 __is_err(error);
495         else
496                 g_print("\t** WiFi tethering passphrase : %s\n", passphrase);
497
498         error = tethering_wifi_get_ssid_visibility(th, &visibility);
499         if (error != TETHERING_ERROR_NONE)
500                 __is_err(error);
501         else
502                 g_print("\t** WiFi tethering ssid visibility : %s\n",
503                                 visibility ? "visible" : "invisible");
504
505         error = tethering_wifi_get_security_type(th, &security_type);
506         if (error != TETHERING_ERROR_NONE)
507                 __is_err(error);
508         else {
509                 switch (security_type) {
510                 case TETHERING_WIFI_SECURITY_TYPE_NONE:
511                         sec_str = "open";
512                         break;
513                 case TETHERING_WIFI_SECURITY_TYPE_WPA2_PSK:
514                         sec_str = "wpa2-psk";
515                         break;
516                 case TETHERING_WIFI_SECURITY_TYPE_WPS:
517                         sec_str = "wps";
518                         break;
519                 default:
520                         sec_str = "unknown";
521                         break;
522                 }
523                 g_print("\t** WiFi tethering security_type : %s\n", sec_str);
524         }
525
526         error = tethering_wifi_get_mode(th, &hw_mode);
527         if (error != TETHERING_ERROR_NONE)
528                 __is_err(error);
529         else
530                  g_print("\t** WiFi tethering mode : %d\n", hw_mode);
531
532         error = tethering_wifi_get_channel(th, &channel);
533         if (error != TETHERING_ERROR_NONE)
534                 __is_err(error);
535         else
536                  g_print("\t** WiFi tethering channel : %d\n", channel);
537
538         error = tethering_wifi_get_max_connected_device(th, &max_connected);
539         if (error != TETHERING_ERROR_NONE)
540                 __is_err(error);
541         else
542                  g_print("\t** WiFi tethering max connected device : %d\n", max_connected);
543
544         error = tethering_wifi_get_mac_filter(th, &mac_filter);
545         if (error != TETHERING_ERROR_NONE)
546                 __is_err(error);
547         else
548                 g_print("\t** WiFi tethering mac filter : %s\n",
549                                 mac_filter ? "enable" : "disable");
550
551         error = tethering_wifi_is_port_filtering_enabled(th, &filtering_enabled);
552         if (error != TETHERING_ERROR_NONE)
553                 __is_err(error);
554         else
555                 g_print("\t** WiFi tethering port filtering : %s\n",
556                                 filtering_enabled ? "enable" : "disable");
557
558         error = tethering_wifi_is_port_forwarding_enabled(th, &forwarding_enabled);
559         if (error != TETHERING_ERROR_NONE)
560                 __is_err(error);
561         else
562                 g_print("\t** WiFi tethering port forwarding : %s\n",
563                                 forwarding_enabled ? "enable" : "disable");
564
565         if (ssid)
566                 free(ssid);
567         if (passphrase)
568                 free(passphrase);
569
570         return;
571 }
572
573 void __display_list(GSList *list)
574 {
575         GSList *iterator = NULL;
576
577         for (iterator = list; iterator; iterator = iterator->next)
578                 printf("%s\n", (char*)iterator->data);
579 }
580
581 bool __get_tethering_type(tethering_type_e *type)
582 {
583         int sel;
584         int ret;
585
586         printf("Select tethering type (1:Wi-Fi, 2:BT, 3:USB 4:ALL)\n");
587         ret = scanf("%9d", &sel);
588         if (ret < 0) {
589                 printf("scanf is failed!!\n");
590                 return false;
591         }
592
593         switch (sel) {
594         case 1:
595                 *type = TETHERING_TYPE_WIFI;
596                 break;
597         case 2:
598                 *type = TETHERING_TYPE_BT;
599                 break;
600         case 3:
601                 *type = TETHERING_TYPE_USB;
602                 break;
603         case 4:
604                 *type = TETHERING_TYPE_ALL;
605                 break;
606         default:
607                 printf("Invalid input!!\n");
608                 return false;
609         }
610
611         return true;
612 }
613
614 static int test_tethering_create(void)
615 {
616         int ret = tethering_create(&th);
617         __tethering_cbs cbs = {
618                 __enabled_cb, __disabled_cb,
619                 __connection_state_changed_cb, __security_type_changed_cb,
620                 __ssid_visibility_changed_cb, __passphrase_changed_cb};
621
622         if (__is_err(ret) == false) __register_cbs(th, &cbs, NULL);
623         else {
624                 printf("Tethering create is failed\n");
625                 return -1;
626         }
627         printf("Tethering create and register callback success\n");
628
629         return 1;
630 }
631
632 static int test_tethering_destroy(void)
633 {
634         int ret = TETHERING_ERROR_NONE;
635
636         __deregister_cbs(th);
637
638         ret = tethering_destroy(th);
639         if (__is_err(ret) == true) {
640                 printf("Tethering destroy is failed\n");
641                 return -1;
642         }
643
644         return 1;
645 }
646
647 static int test_tethering_enable(void)
648 {
649         int ret = TETHERING_ERROR_NONE;
650         tethering_type_e type;
651
652         if (!__get_tethering_type(&type))
653                 return -1;
654
655         ret = tethering_enable(th, type);
656         if (__is_err(ret) == true) {
657                 printf("Fail to enable tethering\n");
658                 return -1;
659         }
660         return 1;
661 }
662
663 static int test_tethering_disable(void)
664 {
665         int ret = TETHERING_ERROR_NONE;
666         tethering_type_e type;
667
668         if (!__get_tethering_type(&type))
669                 return -1;
670
671         ret = tethering_disable(th, type);
672         if (__is_err(ret) == true) {
673                 printf("Fail to disable tethering\n");
674                 return -1;
675         }
676         return 1;
677 }
678
679 static int test_tethering_get_client_info(void)
680 {
681         int ret;
682         tethering_type_e type;
683
684         if (!__get_tethering_type(&type))
685                 return -1;
686
687         ret = tethering_foreach_connected_clients(th, type,
688                                         __clients_foreach_cb, NULL);
689         if (__is_err(ret) == true) {
690                 printf("Fail to disable tethering\n");
691                 return -1;
692         }
693
694         return 1;
695 }
696
697 static int test_tethering_get_interface_info(void)
698 {
699         tethering_type_e type;
700
701         if (!__get_tethering_type(&type))
702                 return -1;
703
704         __print_interface_info(th, type);
705
706         return 1;
707 }
708
709 static int test_tethering_get_data_usage(void)
710 {
711         int ret = tethering_get_data_usage(th, __data_usage_cb, NULL);
712
713         if (__is_err(ret) == true) {
714                 printf("Fail to get data usage!!\n");
715                 return -1;
716         }
717
718         return 1;
719 }
720
721 static int test_tethering_wifi_get_setting(void)
722 {
723         __print_wifi_tethering_setting(th);
724         return 1;
725 }
726
727 static int test_tethering_wifi_set_ssid(void)
728 {
729         int ret;
730         char ssid[100];
731
732         printf("Input SSID for Wi-Fi tethering: ");
733         ret = scanf("%99s", ssid);
734         if (ret < 0) {
735                 printf("scanf is failed!!\n");
736                 return -1;
737         }
738
739         ret = tethering_wifi_set_ssid(th, ssid);
740         if (__is_err(ret) == true) {
741                 printf("Fail to set wifi ssid!!\n");
742                 return -1;
743         }
744
745         return 1;
746 }
747
748 static int test_tethering_wifi_set_security_type(void)
749 {
750         int ret;
751         int security_type;
752
753         printf("Input security type for Wi-Fi tethering (0:NONE, 1:WPA2_PSK, 2:WPS)");
754         ret = scanf("%9d", &security_type);
755         if (ret < 0) {
756                 printf("scanf is failed!!\n");
757                 return -1;
758         }
759
760         ret = tethering_wifi_set_security_type(th, security_type);
761         if (__is_err(ret) == true) {
762                 printf("Fail to set security type!!\n");
763                 return -1;
764         }
765
766         return 1;
767 }
768
769 int test_tethering_wifi_set_visibility(void)
770 {
771         int ret;
772         int visibility;
773
774         printf("Input security type for Wi-Fi tethering (0:invisible, 1:visible)");
775         ret = scanf("%9d", &visibility);
776         if (ret < 0) {
777                 printf("scanf is failed!!\n");
778                 return -1;
779         }
780
781         ret = tethering_wifi_set_ssid_visibility(th, visibility);
782         if (__is_err(ret) == true) {
783                 printf("Fail to set visibility!!\n");
784                 return -1;
785         }
786
787         return 1;
788 }
789
790 static int test_tethering_wifi_set_passphrase(void)
791 {
792         int ret;
793         char passphrase[100];
794
795         printf("Input passphrase for Wi-Fi tethering: ");
796         ret = scanf("%99s", passphrase);
797         if (ret < 0) {
798                 printf("scanf is failed!!\n");
799                 return -1;
800         }
801
802         ret = tethering_wifi_set_passphrase(th, passphrase);
803         if (__is_err(ret) == true) {
804                 printf("Fail to set passphrase!!\n");
805                 return -1;
806         }
807
808         return 1;
809 }
810
811 static int test_tethering_wifi_set_channel(void)
812 {
813         int ret;
814         int channel;
815
816         printf("Input channel for Wi-Fi tethering: ");
817         ret = scanf("%d", &channel);
818
819         ret = tethering_wifi_set_channel(th, channel);
820         if (__is_err(ret) == true) {
821                 printf("Fail to set channel!!\n");
822                 return -1;
823         }
824
825         return 1;
826 }
827
828 static int test_tethering_wifi_set_mode(void)
829 {
830         int ret;
831         int type;
832
833         printf("Input hw_mode for Wi-Fi tethering(0-b, 1-g, 2-a, 3-ad): ");
834         ret = scanf("%d", &type);
835
836         ret = tethering_wifi_set_mode(th, type);
837         if (__is_err(ret) == true) {
838                 printf("Fail to set mode!!\n");
839                 return -1;
840         }
841
842         return 1;
843 }
844
845 static int test_tethering_wifi_enable_dhcp(void)
846 {
847         int ret;
848         int enable;
849
850         printf("Input (0-Disable, 1-Enable): ");
851         ret = scanf("%d", &enable);
852
853         ret = tethering_wifi_enable_dhcp(th, enable);
854         if (__is_err(ret) == true) {
855                 printf("Fail to enable dhcp server!!\n");
856                 return -1;
857         }
858
859         return 1;
860 }
861
862 static int test_tethering_wifi_set_dhcp_range(void)
863 {
864         int ret;
865         char rangestart[16], rangestop[16];
866
867         printf("Input range (ex: 192.168.0.50 192.168.0.150): ");
868
869         ret = scanf("%15s %15s", rangestart, rangestop);
870
871         ret = tethering_wifi_set_dhcp_range(th, rangestart, rangestop);
872         if (__is_err(ret) == true) {
873                 printf("Fail to set dhcp range and enable dhcp server!!\n");
874                 return -1;
875         }
876
877         return 1;
878 }
879
880 static int test_tethering_wifi_is_dhcp_enabled(void)
881 {
882         int ret;
883         bool enabled;
884
885         ret = tethering_wifi_is_dhcp_enabled(th, &enabled);
886
887         if (__is_err(ret) == true) {
888                 printf("Fail to get dhcp server status!!\n");
889                 return -1;
890         } else {
891                 printf("DHCP server is %s\n", enabled ? "enabled" : "disabled");
892         }
893
894         return 1;
895 }
896
897 static int test_tethering_wifi_set_mac_filtering(void)
898 {
899         int ret;
900         int enable;
901
902         printf("Input mac filtering option (0: disable, 1: enable): ");
903         ret = scanf("%d", &enable);
904
905         ret = tethering_wifi_set_mac_filter(th, enable);
906         if (__is_err(ret) == true) {
907                 printf("Fail to set mac filtering!!\n");
908                 return -1;
909         }
910
911         return 1;
912 }
913
914 static int test_tethering_manage_mac_list(void)
915 {
916         int ret = 0;
917         int list, option;
918         char mac[100];
919
920         printf("Select MAC list to modify (0: allowed mac list, 1: blocked mac list): ");
921         ret = scanf("%d", &list);
922
923         printf("Select option (0: Add, 1: Remove): ");
924         ret = scanf("%d", &option);
925
926         printf("Input MAC Address to add/remove allowed/blocked mac list: ");
927         ret = scanf("%99s", mac);
928         if (ret < 0) {
929                 printf("scanf is failed!!\n");
930                 return -1;
931         }
932
933         if (!list && !option) {
934                 /* Add to allowed mac list*/
935                 ret = tethering_wifi_add_allowed_mac_list(th, mac);
936         } else if (!list && option) {
937                 /* Remove from allowed mac list */
938                 ret = tethering_wifi_remove_allowed_mac_list(th, mac);
939         } else if (list && !option) {
940                 /* Add to blocked mac list */
941                 ret = tethering_wifi_add_blocked_mac_list(th, mac);
942         } else if (list && option) {
943                 /* Remove from blocked mac list */
944                 ret = tethering_wifi_remove_blocked_mac_list(th, mac);
945         } else {
946                 printf("Input failed!!\n");
947                 return -1;
948         }
949
950         if (ret < 0)
951                 return -1;
952
953         return 1;
954 }
955
956 static int test_tethering_get_mac_list(void)
957 {
958         int ret = 0;
959         int list = 0;
960         void *mac_list = NULL;
961
962         printf("Select MAC list to get (0: allowed mac list, 1: blocked mac list): ");
963         ret = scanf("%d", &list);
964
965         switch (list) {
966         case 0:
967                 ret = tethering_wifi_get_allowed_mac_list(th, &mac_list);
968                 break;
969         case 1:
970                 ret = tethering_wifi_get_blocked_mac_list(th, &mac_list);
971                 break;
972         default:
973                 printf("Input failed!!\n");
974                 break;
975         }
976
977         if (ret < 0)
978                 return -1;
979
980         __display_list(mac_list);
981
982         return 1;
983 }
984
985 static int test_tethering_wifi_reload_settings(void)
986 {
987         int ret = tethering_wifi_reload_settings(th, __settings_reloaded_cb, NULL);
988
989         if (__is_err(ret) == true) {
990                 printf("Fail to reload wifi tethering!!\n");
991                 return -1;
992         }
993         return 1;
994 }
995
996 static int test_tethering_wifi_get_txpower(void)
997 {
998         int ret = TETHERING_ERROR_NONE;
999
1000         unsigned int txpower = 0;
1001         ret = tethering_wifi_get_txpower(th, &txpower);
1002         if (__is_err(ret) == true) {
1003                 printf("Fail to get txpower!!\n");
1004                 return -1;
1005         }
1006         g_print("tethering_hostapd_get_txpower received [%d]\n", txpower);
1007         return 1;
1008 }
1009
1010 static int test_tethering_wifi_set_txpower(void)
1011 {
1012         int ret;
1013         unsigned int txpower = 0;
1014
1015         printf("Input tx power for Wi-Fi tethering: ");
1016         ret = scanf("%d", &txpower);
1017
1018         ret = tethering_wifi_set_txpower(th, txpower);
1019         if (__is_err(ret) == true) {
1020                 printf("Fail to set txpower!!\n");
1021                 return -1;
1022         }
1023
1024         return 1;
1025 }
1026
1027 static int test_tethering_wifi_set_mtu(void)
1028 {
1029         int ret;
1030         unsigned int mtu = 0;
1031
1032         printf("Input mtu for Wi-Fi tethering: ");
1033         ret = scanf("%d", &mtu);
1034
1035         ret = tethering_wifi_set_mtu(th, mtu);
1036         if (__is_err(ret) == true) {
1037                 printf("Fail to set mtu!!\n");
1038                 return -1;
1039         }
1040
1041         return 1;
1042 }
1043
1044 static int test_tethering_wifi_change_mac(void)
1045 {
1046         int ret;
1047         char mac[18];
1048
1049         printf("Input mac address: ");
1050         ret = scanf("%17s", mac);
1051
1052         ret = tethering_wifi_change_mac(th, mac);
1053         if (__is_err(ret) == true) {
1054                 printf("Fail to change mac!\n");
1055                 return -1;
1056         }
1057
1058         return 1;
1059 }
1060
1061 static int test_tethering_wifi_set_max_connected_device(void)
1062 {
1063         int ret;
1064         int max_connected;
1065
1066         printf("Input max connected device: ");
1067         ret = scanf("%d", &max_connected);
1068
1069         ret = tethering_wifi_set_max_connected_device(th, max_connected);
1070         if (__is_err(ret) == true) {
1071                 printf("Fail to set max connected device!\n");
1072                 return -1;
1073         }
1074
1075         return 1;
1076
1077 }
1078
1079 static int test_tethering_wifi_enable_port_forwarding(void)
1080 {
1081         int ret;
1082         int enable = false;
1083
1084         printf("Wi-Fi tethring port forwarding(0:disable 1:enable): ");
1085         ret = scanf("%d", &enable);
1086
1087         ret = tethering_wifi_enable_port_forwarding(th, enable);
1088         if (__is_err(ret) == true) {
1089                 printf("Fail to enable port forwarding!\n");
1090                 return -1;
1091         }
1092
1093         return 1;
1094 }
1095
1096 static int test_tethering_wifi_add_port_forwarding_rule(void)
1097 {
1098         int ret;
1099         char ifname[20];
1100         char proto[20];
1101         char org_ip[16];
1102         char final_ip[16];
1103         int org_port, final_port;
1104
1105         printf("Input ifname, protocol, original ip/port, final ip/port: ");
1106         ret = scanf("%19s", ifname);
1107         ret = scanf("%19s", proto);
1108         ret = scanf("%15s", org_ip);
1109         ret = scanf("%d", &org_port);
1110         ret = scanf("%15s", final_ip);
1111         ret = scanf("%d", &final_port);
1112
1113         ret = tethering_wifi_add_port_forwarding_rule(th, ifname, proto, org_ip, org_port, final_ip, final_port);
1114         if (__is_err(ret) == true) {
1115                 printf("Fail to add port forwarding rule!\n");
1116                 return -1;
1117         }
1118
1119         return 1;
1120 }
1121
1122 static int test_tethering_wifi_reset_port_forwarding_rule(void)
1123 {
1124         int ret;
1125
1126         ret = tethering_wifi_reset_port_forwarding_rule(th);
1127         if (__is_err(ret) == true) {
1128                 printf("Fail to reset port forwarding rule!\n");
1129                 return -1;
1130         }
1131
1132         return 1;
1133 }
1134
1135 static int test_tethering_wifi_get_port_forwarding_rule(void)
1136 {
1137         int ret = 0;
1138         void *pf_list = NULL;
1139
1140         ret = tethering_wifi_get_port_forwarding_rule(th, &pf_list);
1141         if (__is_err(ret) == true) {
1142                 printf("Fail to get port forwarding rule!\n");
1143                 return -1;
1144         }
1145
1146         __display_list(pf_list);
1147
1148         return 1;
1149 }
1150
1151 static int test_tethering_wifi_enable_port_filtering(void)
1152 {
1153         int ret;
1154         int enable = false;
1155
1156         printf("Wi-Fi tethring port filtering(0:disable 1:enable): ");
1157         ret = scanf("%d", &enable);
1158
1159         ret = tethering_wifi_enable_port_filtering(th, enable);
1160         if (__is_err(ret) == true) {
1161                 printf("Fail to enable port filtering!\n");
1162                 return -1;
1163         }
1164
1165         return 1;
1166 }
1167
1168 static int test_tethering_wifi_add_port_filtering_rule(void)
1169 {
1170         int ret;
1171         char proto[20];
1172         int port;
1173         int allow;
1174
1175         printf("Input protocol, port, allow: ");
1176         ret = scanf("%19s", proto);
1177         ret = scanf("%d", &port);
1178         ret = scanf("%d", &allow);
1179
1180         ret = tethering_wifi_add_port_filtering_rule(th, port, proto, allow);
1181         if (__is_err(ret) == true) {
1182                 printf("Fail to add port forwarding rule!\n");
1183                 return -1;
1184         }
1185
1186         return 1;
1187 }
1188
1189 static int test_tethering_wifi_add_custom_port_filtering_rule(void)
1190 {
1191         int ret;
1192         char proto[20];
1193         int port1, port2;
1194         int allow;
1195
1196         printf("Input protocol, port1, port2, allow: ");
1197         ret = scanf("%19s", proto);
1198         ret = scanf("%d", &port1);
1199         ret = scanf("%d", &port2);
1200         ret = scanf("%d", &allow);
1201
1202         ret = tethering_wifi_add_custom_port_filtering_rule(th, port1, port2, proto, allow);
1203         if (__is_err(ret) == true) {
1204                 printf("Fail to add custom port forwarding rule!\n");
1205                 return -1;
1206         }
1207
1208         return 1;
1209 }
1210
1211 static int test_tethering_wifi_get_port_filtering_rule(void)
1212 {
1213         int ret = 0;
1214         void *pf_list = NULL;
1215
1216         ret = tethering_wifi_get_port_filtering_rule(th, &pf_list);
1217         if (__is_err(ret) == true) {
1218                 printf("Fail to get port filtering rule!\n");
1219                 return -1;
1220         }
1221
1222         __display_list(pf_list);
1223
1224         return 1;
1225 }
1226
1227 static int test_tethering_wifi_get_custom_port_filtering_rule(void)
1228 {
1229         int ret = 0;
1230         void *pf_list = NULL;
1231
1232         ret = tethering_wifi_get_custom_port_filtering_rule(th, &pf_list);
1233         if (__is_err(ret) == true) {
1234                 printf("Fail to get port filtering rule!\n");
1235                 return -1;
1236         }
1237
1238         __display_list(pf_list);
1239
1240         return 1;
1241 }
1242
1243 static int test_tethering_wifi_set_vpn_passthrough_rule(void)
1244 {
1245         int ret = 0;
1246         int type;
1247
1248         printf("Select vpn passthrough type (0:IPSEC 1:PPTP 2:L2TP): ");
1249         ret = scanf("%d", &type);
1250
1251         ret = tethering_wifi_set_vpn_passthrough_rule(th, (tethering_vpn_passthrough_type_e)type, true);
1252         if (__is_err(ret) == true) {
1253                 printf("Fail to get port filtering rule!\n");
1254                 return -1;
1255         }
1256
1257         return 1;
1258 }
1259
1260 static int test_tethering_wifi_push_wps_button(void)
1261 {
1262         int ret = 0;
1263
1264         ret = tethering_wifi_push_wps_button(th);
1265         if (__is_err(ret) == true) {
1266                 printf("Fail to get port filtering rule!\n");
1267                 return -1;
1268         }
1269
1270         return 1;
1271 }
1272
1273 static int test_tethering_wifi_set_wps_pin(void)
1274 {
1275         int ret = 0;
1276         char wps_pin[128];
1277
1278         printf("Input WPS PIN: ");
1279         ret = scanf("%127s", wps_pin);
1280
1281         ret = tethering_wifi_set_wps_pin(th, wps_pin);
1282         if (__is_err(ret) == true) {
1283                 printf("Fail to get port filtering rule!\n");
1284                 return -1;
1285         }
1286
1287         return 1;
1288 }
1289
1290 int main(int argc, char **argv)
1291 {
1292         GMainLoop *mainloop;
1293
1294 #if !GLIB_CHECK_VERSION(2, 36, 0)
1295         g_type_init();
1296 #endif
1297         mainloop = g_main_loop_new(NULL, false);
1298
1299         GIOChannel *channel = g_io_channel_unix_new(0);
1300         g_io_add_watch(channel, (G_IO_IN|G_IO_ERR|G_IO_HUP|G_IO_NVAL), test_thread, NULL);
1301         printf("Test Thread created...\n");
1302         g_main_loop_run(mainloop);
1303
1304         return 0;
1305 }
1306
1307 gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data)
1308 {
1309         int rv;
1310         char a[10];
1311
1312         printf("Event received from stdin\n");
1313
1314         rv = read(0, a, 10);
1315
1316         if (rv <= 0 || a[0] == '0')
1317                 exit(1);
1318
1319         if (a[0] == '\n' || a[0] == '\r') {
1320                 printf("\n\n Network Connection API Test App\n\n");
1321                 printf("Options..\n");
1322                 printf("1       - Tethering create and set callbacks\n");
1323                 printf("2       - Tethering destroy\n");
1324                 printf("3       - Enable Tethering\n");
1325                 printf("4       - Disable Tethering\n");
1326                 printf("5       - Get client information\n");
1327                 printf("6       - Get interface information\n");
1328                 printf("7       - Get data usage\n");
1329                 printf("8       - Get Wi-Fi tethering setting\n");
1330                 printf("a       - Set Wi-Fi tethering SSID\n");
1331                 printf("b       - Set Wi-Fi tethering security type\n");
1332                 printf("c       - Set Wi-Fi tethering visibility\n");
1333                 printf("d       - Set Wi-Fi tethering passphrase\n");
1334                 printf("e       - Set Wi-Fi tethering mac filtering\n");
1335                 printf("f       - Add/Remove MAC adress to/from allowed/blocked list\n");
1336                 printf("g       - Get allowed/blocked list\n");
1337                 printf("k       - Reload Wi-Fi tethering\n");
1338                 printf("m       - Set Wi-Fi channel\n");
1339                 printf("n       - Set Wi-Fi hw_mode\n");
1340                 printf("o       - Enable dhcp server\n");
1341                 printf("p       - Enable dhcp server with range\n");
1342                 printf("q       - Is dhcp server enabled?\n");
1343                 printf("r       - Get Wi-Fi txpower\n");
1344                 printf("s       - Set Wi-Fi txpower\n");
1345                 printf("t       - Set Wi-Fi mtu\n");
1346                 printf("u       - Change mac address\n");
1347                 printf("v       - Set max connected device(Wi-Fi tethering)\n");
1348                 printf("w       - Enable port forwarding\n");
1349                 printf("x       - Add port forwarding rule\n");
1350                 printf("y       - Reset port forwarding rule\n");
1351                 printf("z       - Get port forwarding rule\n");
1352                 printf("A       - Enable port filtering\n");
1353                 printf("B       - Add port filtering rule\n");
1354                 printf("C       - Add custom port filtering rule\n");
1355                 printf("D       - Get port filtering rule\n");
1356                 printf("E       - Get custom port filtering rule\n");
1357                 printf("F       - Set vpn passthrough rule\n");
1358                 printf("G       - Push WPS button\n");
1359                 printf("H       - Set WPS PIN\n");
1360                 printf("0       - \n");
1361                 printf("ENTER  - Show options menu.......\n");
1362         }
1363
1364         switch (a[0]) {
1365         case '1':
1366                 rv = test_tethering_create();
1367                 break;
1368         case '2':
1369                 rv = test_tethering_destroy();
1370                 break;
1371         case '3':
1372                 rv = test_tethering_enable();
1373                 break;
1374         case '4':
1375                 rv = test_tethering_disable();
1376                 break;
1377         case '5':
1378                 rv = test_tethering_get_client_info();
1379                 break;
1380         case '6':
1381                 rv = test_tethering_get_interface_info();
1382                 break;
1383         case '7':
1384                 rv = test_tethering_get_data_usage();
1385                 break;
1386         case '8':
1387                 rv = test_tethering_wifi_get_setting();
1388                 break;
1389         case 'a':
1390                 rv = test_tethering_wifi_set_ssid();
1391                 break;
1392         case 'b':
1393                 rv = test_tethering_wifi_set_security_type();
1394                 break;
1395         case 'c':
1396                 rv = test_tethering_wifi_set_visibility();
1397                 break;
1398         case 'd':
1399                 rv = test_tethering_wifi_set_passphrase();
1400                 break;
1401         case 'e':
1402                 rv = test_tethering_wifi_set_mac_filtering();
1403                 break;
1404         case 'f':
1405                 rv = test_tethering_manage_mac_list();
1406                 break;
1407         case 'g':
1408                 rv = test_tethering_get_mac_list();
1409                 break;
1410         case 'k':
1411                 rv = test_tethering_wifi_reload_settings();
1412                 break;
1413         case 'm':
1414                 rv = test_tethering_wifi_set_channel();
1415                 break;
1416         case 'n':
1417                 rv = test_tethering_wifi_set_mode();
1418                 break;
1419         case 'o':
1420                 rv = test_tethering_wifi_enable_dhcp();
1421                 break;
1422         case 'p':
1423                 rv = test_tethering_wifi_set_dhcp_range();
1424                 break;
1425         case 'q':
1426                 rv = test_tethering_wifi_is_dhcp_enabled();
1427                 break;
1428         case 'r':
1429                 rv = test_tethering_wifi_get_txpower();
1430                 break;
1431         case 's':
1432                 rv = test_tethering_wifi_set_txpower();
1433                 break;
1434         case 't':
1435                 rv = test_tethering_wifi_set_mtu();
1436                 break;
1437         case 'u':
1438                 rv = test_tethering_wifi_change_mac();
1439                 break;
1440         case 'v':
1441                 rv = test_tethering_wifi_set_max_connected_device();
1442                 break;
1443         case 'w':
1444                 rv = test_tethering_wifi_enable_port_forwarding();
1445                 break;
1446         case 'x':
1447                 rv = test_tethering_wifi_add_port_forwarding_rule();
1448                 break;
1449         case 'y':
1450                 rv = test_tethering_wifi_reset_port_forwarding_rule();
1451                 break;
1452         case 'z':
1453                 rv = test_tethering_wifi_get_port_forwarding_rule();
1454                 break;
1455         case 'A':
1456                 rv = test_tethering_wifi_enable_port_filtering();
1457                 break;
1458         case 'B':
1459                 rv = test_tethering_wifi_add_port_filtering_rule();
1460                 break;
1461         case 'C':
1462                 rv = test_tethering_wifi_add_custom_port_filtering_rule();
1463                 break;
1464         case 'D':
1465                 rv = test_tethering_wifi_get_port_filtering_rule();
1466                 break;
1467         case 'E':
1468                 rv = test_tethering_wifi_get_custom_port_filtering_rule();
1469                 break;
1470         case 'F':
1471                 rv = test_tethering_wifi_set_vpn_passthrough_rule();
1472                 break;
1473         case 'G':
1474                 rv = test_tethering_wifi_push_wps_button();
1475                 break;
1476         case 'H':
1477                 rv = test_tethering_wifi_set_wps_pin();
1478                 break;
1479         }
1480
1481         if (rv == 1)
1482                 printf("Operation succeeded!\n");
1483         else
1484                 printf("Operation failed!\n");
1485
1486         return true;
1487 }