Add new Wi-Fi tethering APIs
[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         g_print("Wi-Fi Tethering Security type is changed to [%s]\n",
392                         changed_type == TETHERING_WIFI_SECURITY_TYPE_NONE ?
393                         "open" : "wpa2-psk");
394         return;
395 }
396
397 static void __ssid_visibility_changed_cb(bool changed_visible, void *user_data)
398 {
399         g_print("SSID visibility for Wi-Fi tethering changed to [%s]\n",
400                         changed_visible ? "visible" : "invisible");
401         return;
402 }
403
404 static void __passphrase_changed_cb(void *user_data)
405 {
406         g_print("Wi-Fi Tethering passphrase is changed\n");
407         return;
408 }
409 /* End of tethering callbacks */
410
411 static void __print_interface_info(tethering_h th, tethering_type_e type)
412 {
413         char *interface = NULL;
414         char *mac_address = NULL;
415         char *ip_address = NULL;
416         char *gateway_address = NULL;
417         char *subnet_mask = NULL;
418
419         if (tethering_is_enabled(th, type) == FALSE) {
420                 g_print("%s is not enabled\n",
421                                 __convert_tethering_type_to_str(type));
422                 return;
423         }
424
425         tethering_get_network_interface_name(th, type, &interface);
426         tethering_get_mac_address(th, type, &mac_address);
427         tethering_get_ip_address(th, type, TETHERING_ADDRESS_FAMILY_IPV4,
428                         &ip_address);
429         tethering_get_gateway_address(th, type, TETHERING_ADDRESS_FAMILY_IPV4,
430                         &gateway_address);
431         tethering_get_subnet_mask(th, type, TETHERING_ADDRESS_FAMILY_IPV4,
432                         &subnet_mask);
433
434         g_print("interface name : %s\n", interface);
435         g_print("mac address : %s\n", mac_address);
436         g_print("ip address : %s\n", ip_address);
437         g_print("gateway address: %s\n", gateway_address);
438         g_print("subnet mask : %s\n", subnet_mask);
439
440         if (interface)
441                 free(interface);
442         if (mac_address)
443                 free(mac_address);
444         if (ip_address)
445                 free(ip_address);
446         if (gateway_address)
447                 free(gateway_address);
448         if (subnet_mask)
449                 free(subnet_mask);
450
451         return;
452 }
453
454 static void __print_wifi_tethering_setting(tethering_h th)
455 {
456         char *ssid = NULL;
457         char *passphrase = NULL;
458         bool visibility = false;
459         bool mac_filter = 0;
460         bool forwarding_enabled = false;
461         bool filtering_enabled = false;
462         int channel = 0;
463         int max_connected = 0;
464         tethering_wifi_security_type_e security_type = TETHERING_WIFI_SECURITY_TYPE_NONE;
465         tethering_wifi_mode_type_e hw_mode = TETHERING_WIFI_MODE_TYPE_G;
466
467         int error = TETHERING_ERROR_NONE;
468
469         error = tethering_wifi_get_ssid(th, &ssid);
470         if (error != TETHERING_ERROR_NONE)
471                 __is_err(error);
472         else
473                 g_print("\n\t** WiFi tethering SSID : %s\n", ssid);
474
475         error = tethering_wifi_get_passphrase(th, &passphrase);
476         if (error != TETHERING_ERROR_NONE)
477                 __is_err(error);
478         else
479                 g_print("\t** WiFi tethering passphrase : %s\n", passphrase);
480
481         error = tethering_wifi_get_ssid_visibility(th, &visibility);
482         if (error != TETHERING_ERROR_NONE)
483                 __is_err(error);
484         else
485                 g_print("\t** WiFi tethering ssid visibility : %s\n",
486                                 visibility ? "visible" : "invisible");
487
488         error = tethering_wifi_get_security_type(th, &security_type);
489         if (error != TETHERING_ERROR_NONE)
490                 __is_err(error);
491         else
492                 g_print("\t** WiFi tethering security_type : %s\n",
493                                 security_type ==
494                                 TETHERING_WIFI_SECURITY_TYPE_NONE ?
495                                 "open" : "wpa2-psk");
496
497         error = tethering_wifi_get_mode(th, &hw_mode);
498         if (error != TETHERING_ERROR_NONE)
499                 __is_err(error);
500         else
501                  g_print("\t** WiFi tethering mode : %d\n", hw_mode);
502
503         error = tethering_wifi_get_channel(th, &channel);
504         if (error != TETHERING_ERROR_NONE)
505                 __is_err(error);
506         else
507                  g_print("\t** WiFi tethering channel : %d\n", channel);
508
509         error = tethering_wifi_get_max_connected_device(th, &max_connected);
510         if (error != TETHERING_ERROR_NONE)
511                 __is_err(error);
512         else
513                  g_print("\t** WiFi tethering max connected device : %d\n", max_connected);
514
515         error = tethering_wifi_get_mac_filter(th, &mac_filter);
516         if (error != TETHERING_ERROR_NONE)
517                 __is_err(error);
518         else
519                 g_print("\t** WiFi tethering mac filter : %s\n",
520                                 mac_filter ? "enable" : "disable");
521
522         error = tethering_wifi_is_port_filtering_enabled(th, &filtering_enabled);
523         if (error != TETHERING_ERROR_NONE)
524                 __is_err(error);
525         else
526                 g_print("\t** WiFi tethering port filtering : %s\n",
527                                 filtering_enabled ? "enable" : "disable");
528
529         error = tethering_wifi_is_port_forwarding_enabled(th, &forwarding_enabled);
530         if (error != TETHERING_ERROR_NONE)
531                 __is_err(error);
532         else
533                 g_print("\t** WiFi tethering port forwarding : %s\n",
534                                 forwarding_enabled ? "enable" : "disable");
535
536         if (ssid)
537                 free(ssid);
538         if (passphrase)
539                 free(passphrase);
540
541         return;
542 }
543
544 void __display_list(GSList *list)
545 {
546         GSList *iterator = NULL;
547
548         for (iterator = list; iterator; iterator = iterator->next)
549                 printf("%s\n", (char*)iterator->data);
550 }
551
552 bool __get_tethering_type(tethering_type_e *type)
553 {
554         int sel;
555         int ret;
556
557         printf("Select tethering type (1:Wi-Fi, 2:BT, 3:USB 4:ALL)\n");
558         ret = scanf("%9d", &sel);
559         if (ret < 0) {
560                 printf("scanf is failed!!\n");
561                 return false;
562         }
563
564         switch (sel) {
565         case 1:
566                 *type = TETHERING_TYPE_WIFI;
567                 break;
568         case 2:
569                 *type = TETHERING_TYPE_BT;
570                 break;
571         case 3:
572                 *type = TETHERING_TYPE_USB;
573                 break;
574         case 4:
575                 *type = TETHERING_TYPE_ALL;
576                 break;
577         default:
578                 printf("Invalid input!!\n");
579                 return false;
580         }
581
582         return true;
583 }
584
585 static int test_tethering_create(void)
586 {
587         int ret = tethering_create(&th);
588         __tethering_cbs cbs = {
589                 __enabled_cb, __disabled_cb,
590                 __connection_state_changed_cb, __security_type_changed_cb,
591                 __ssid_visibility_changed_cb, __passphrase_changed_cb};
592
593         if (__is_err(ret) == false) __register_cbs(th, &cbs, NULL);
594         else {
595                 printf("Tethering create is failed\n");
596                 return -1;
597         }
598         printf("Tethering create and register callback success\n");
599
600         return 1;
601 }
602
603 static int test_tethering_destroy(void)
604 {
605         int ret = TETHERING_ERROR_NONE;
606
607         __deregister_cbs(th);
608
609         ret = tethering_destroy(th);
610         if (__is_err(ret) == true) {
611                 printf("Tethering destroy is failed\n");
612                 return -1;
613         }
614
615         return 1;
616 }
617
618 static int test_tethering_enable(void)
619 {
620         int ret = TETHERING_ERROR_NONE;
621         tethering_type_e type;
622
623         if (!__get_tethering_type(&type))
624                 return -1;
625
626         ret = tethering_enable(th, type);
627         if (__is_err(ret) == true) {
628                 printf("Fail to enable tethering\n");
629                 return -1;
630         }
631         return 1;
632 }
633
634 static int test_tethering_disable(void)
635 {
636         int ret = TETHERING_ERROR_NONE;
637         tethering_type_e type;
638
639         if (!__get_tethering_type(&type))
640                 return -1;
641
642         ret = tethering_disable(th, type);
643         if (__is_err(ret) == true) {
644                 printf("Fail to disable tethering\n");
645                 return -1;
646         }
647         return 1;
648 }
649
650 static int test_tethering_get_client_info(void)
651 {
652         int ret;
653         tethering_type_e type;
654
655         if (!__get_tethering_type(&type))
656                 return -1;
657
658         ret = tethering_foreach_connected_clients(th, type,
659                                         __clients_foreach_cb, NULL);
660         if (__is_err(ret) == true) {
661                 printf("Fail to disable tethering\n");
662                 return -1;
663         }
664
665         return 1;
666 }
667
668 static int test_tethering_get_interface_info(void)
669 {
670         tethering_type_e type;
671
672         if (!__get_tethering_type(&type))
673                 return -1;
674
675         __print_interface_info(th, type);
676
677         return 1;
678 }
679
680 static int test_tethering_get_data_usage(void)
681 {
682         int ret = tethering_get_data_usage(th, __data_usage_cb, NULL);
683
684         if (__is_err(ret) == true) {
685                 printf("Fail to get data usage!!\n");
686                 return -1;
687         }
688
689         return 1;
690 }
691
692 static int test_tethering_wifi_get_setting(void)
693 {
694         __print_wifi_tethering_setting(th);
695         return 1;
696 }
697
698 static int test_tethering_wifi_set_ssid(void)
699 {
700         int ret;
701         char ssid[100];
702
703         printf("Input SSID for Wi-Fi tethering: ");
704         ret = scanf("%99s", ssid);
705         if (ret < 0) {
706                 printf("scanf is failed!!\n");
707                 return -1;
708         }
709
710         ret = tethering_wifi_set_ssid(th, ssid);
711         if (__is_err(ret) == true) {
712                 printf("Fail to set wifi ssid!!\n");
713                 return -1;
714         }
715
716         return 1;
717 }
718
719 static int test_tethering_wifi_set_security_type(void)
720 {
721         int ret;
722         int security_type;
723
724         printf("Input security type for Wi-Fi tethering (0:NONE, 1:WPA2_PSK)");
725         ret = scanf("%9d", &security_type);
726         if (ret < 0) {
727                 printf("scanf is failed!!\n");
728                 return -1;
729         }
730
731         ret = tethering_wifi_set_security_type(th, security_type);
732         if (__is_err(ret) == true) {
733                 printf("Fail to set security type!!\n");
734                 return -1;
735         }
736
737         return 1;
738 }
739
740 int test_tethering_wifi_set_visibility(void)
741 {
742         int ret;
743         int visibility;
744
745         printf("Input security type for Wi-Fi tethering (0:invisible, 1:visible)");
746         ret = scanf("%9d", &visibility);
747         if (ret < 0) {
748                 printf("scanf is failed!!\n");
749                 return -1;
750         }
751
752         ret = tethering_wifi_set_ssid_visibility(th, visibility);
753         if (__is_err(ret) == true) {
754                 printf("Fail to set visibility!!\n");
755                 return -1;
756         }
757
758         return 1;
759 }
760
761 static int test_tethering_wifi_set_passphrase(void)
762 {
763         int ret;
764         char passphrase[100];
765
766         printf("Input passphrase for Wi-Fi tethering: ");
767         ret = scanf("%99s", passphrase);
768         if (ret < 0) {
769                 printf("scanf is failed!!\n");
770                 return -1;
771         }
772
773         ret = tethering_wifi_set_passphrase(th, passphrase);
774         if (__is_err(ret) == true) {
775                 printf("Fail to set passphrase!!\n");
776                 return -1;
777         }
778
779         return 1;
780 }
781
782 static int test_tethering_wifi_set_channel(void)
783 {
784         int ret;
785         int channel;
786
787         printf("Input channel for Wi-Fi tethering: ");
788         ret = scanf("%d", &channel);
789
790         ret = tethering_wifi_set_channel(th, channel);
791         if (__is_err(ret) == true) {
792                 printf("Fail to set channel!!\n");
793                 return -1;
794         }
795
796         return 1;
797 }
798
799 static int test_tethering_wifi_set_mode(void)
800 {
801         int ret;
802         int type;
803
804         printf("Input hw_mode for Wi-Fi tethering(0-b, 1-g, 2-a, 3-ad): ");
805         ret = scanf("%d", &type);
806
807         ret = tethering_wifi_set_mode(th, type);
808         if (__is_err(ret) == true) {
809                 printf("Fail to set mode!!\n");
810                 return -1;
811         }
812
813         return 1;
814 }
815
816 static int test_tethering_wifi_enable_dhcp(void)
817 {
818         int ret;
819         int enable;
820
821         printf("Input (0-Disable, 1-Enable): ");
822         ret = scanf("%d", &enable);
823
824         ret = tethering_wifi_enable_dhcp(th, enable);
825         if (__is_err(ret) == true) {
826                 printf("Fail to enable dhcp server!!\n");
827                 return -1;
828         }
829
830         return 1;
831 }
832
833 static int test_tethering_wifi_set_dhcp_range(void)
834 {
835         int ret;
836         char rangestart[16], rangestop[16];
837
838         printf("Input range (ex: 192.168.0.50 192.168.0.150): ");
839
840         ret = scanf("%15s %15s", rangestart, rangestop);
841
842         ret = tethering_wifi_set_dhcp_range(th, rangestart, rangestop);
843         if (__is_err(ret) == true) {
844                 printf("Fail to set dhcp range and enable dhcp server!!\n");
845                 return -1;
846         }
847
848         return 1;
849 }
850
851 static int test_tethering_wifi_is_dhcp_enabled(void)
852 {
853         int ret;
854         bool enabled;
855
856         ret = tethering_wifi_is_dhcp_enabled(th, &enabled);
857
858         if (__is_err(ret) == true) {
859                 printf("Fail to get dhcp server status!!\n");
860                 return -1;
861         } else {
862                 printf("DHCP server is %s\n", enabled ? "enabled" : "disabled");
863         }
864
865         return 1;
866 }
867
868 static int test_tethering_wifi_set_mac_filtering(void)
869 {
870         int ret;
871         int enable;
872
873         printf("Input mac filtering option (0: disable, 1: enable): ");
874         ret = scanf("%d", &enable);
875
876         ret = tethering_wifi_set_mac_filter(th, enable);
877         if (__is_err(ret) == true) {
878                 printf("Fail to set mac filtering!!\n");
879                 return -1;
880         }
881
882         return 1;
883 }
884
885 static int test_tethering_manage_mac_list(void)
886 {
887         int ret = 0;
888         int list, option;
889         char mac[100];
890
891         printf("Select MAC list to modify (0: allowed mac list, 1: blocked mac list): ");
892         ret = scanf("%d", &list);
893
894         printf("Select option (0: Add, 1: Remove): ");
895         ret = scanf("%d", &option);
896
897         printf("Input MAC Address to add/remove allowed/blocked mac list: ");
898         ret = scanf("%99s", mac);
899         if (ret < 0) {
900                 printf("scanf is failed!!\n");
901                 return -1;
902         }
903
904         if (!list && !option) {
905                 /* Add to allowed mac list*/
906                 ret = tethering_wifi_add_allowed_mac_list(th, mac);
907         } else if (!list && option) {
908                 /* Remove from allowed mac list */
909                 ret = tethering_wifi_remove_allowed_mac_list(th, mac);
910         } else if (list && !option) {
911                 /* Add to blocked mac list */
912                 ret = tethering_wifi_add_blocked_mac_list(th, mac);
913         } else if (list && option) {
914                 /* Remove from blocked mac list */
915                 ret = tethering_wifi_remove_blocked_mac_list(th, mac);
916         } else {
917                 printf("Input failed!!\n");
918                 return -1;
919         }
920
921         if (ret < 0)
922                 return -1;
923
924         return 1;
925 }
926
927 static int test_tethering_get_mac_list(void)
928 {
929         int ret = 0;
930         int list = 0;
931         void *mac_list = NULL;
932
933         printf("Select MAC list to get (0: allowed mac list, 1: blocked mac list): ");
934         ret = scanf("%d", &list);
935
936         switch (list) {
937         case 0:
938                 ret = tethering_wifi_get_allowed_mac_list(th, &mac_list);
939                 break;
940         case 1:
941                 ret = tethering_wifi_get_blocked_mac_list(th, &mac_list);
942                 break;
943         default:
944                 printf("Input failed!!\n");
945                 break;
946         }
947
948         if (ret < 0)
949                 return -1;
950
951         __display_list(mac_list);
952
953         return 1;
954 }
955
956 static int test_tethering_wifi_reload_settings(void)
957 {
958         int ret = tethering_wifi_reload_settings(th, __settings_reloaded_cb, NULL);
959
960         if (__is_err(ret) == true) {
961                 printf("Fail to reload wifi tethering!!\n");
962                 return -1;
963         }
964         return 1;
965 }
966
967 static int test_tethering_wifi_get_txpower(void)
968 {
969         int ret = TETHERING_ERROR_NONE;
970
971         unsigned int txpower = 0;
972         ret = tethering_wifi_get_txpower(th, &txpower);
973         if (__is_err(ret) == true) {
974                 printf("Fail to get txpower!!\n");
975                 return -1;
976         }
977         g_print("tethering_hostapd_get_txpower received [%d]\n",txpower);
978         return 1;
979 }
980
981 static int test_tethering_wifi_set_txpower(void)
982 {
983         int ret;
984         unsigned int txpower = 0;
985
986         printf("Input tx power for Wi-Fi tethering: ");
987         ret = scanf("%d", &txpower);
988
989         ret = tethering_wifi_set_txpower(th, txpower);
990         if (__is_err(ret) == true) {
991                 printf("Fail to set txpower!!\n");
992                 return -1;
993         }
994
995         return 1;
996 }
997
998 static int test_tethering_wifi_set_mtu(void)
999 {
1000         int ret;
1001         unsigned int mtu = 0;
1002
1003         printf("Input mtu for Wi-Fi tethering: ");
1004         ret = scanf("%d", &mtu);
1005
1006         ret = tethering_wifi_set_mtu(th, mtu);
1007         if (__is_err(ret) == true) {
1008                 printf("Fail to set mtu!!\n");
1009                 return -1;
1010         }
1011
1012         return 1;
1013 }
1014
1015 static int test_tethering_wifi_change_mac(void)
1016 {
1017         int ret;
1018         char mac[18];
1019
1020         printf("Input mac address: ");
1021         ret = scanf("%17s", mac);
1022
1023         ret = tethering_wifi_change_mac(th, mac);
1024         if (__is_err(ret) == true) {
1025                 printf("Fail to change mac!\n");
1026                 return -1;
1027         }
1028
1029         return 1;
1030 }
1031
1032 static int test_tethering_wifi_set_max_connected_device(void)
1033 {
1034         int ret;
1035         int max_connected;
1036
1037         printf("Input max connected device: ");
1038         ret = scanf("%d", &max_connected);
1039
1040         ret = tethering_wifi_set_max_connected_device(th, max_connected);
1041         if (__is_err(ret) == true) {
1042                 printf("Fail to set max connected device!\n");
1043                 return -1;
1044         }
1045
1046         return 1;
1047
1048 }
1049
1050 static int test_tethering_wifi_enable_port_forwarding(void)
1051 {
1052         int ret;
1053         int enable = false;
1054
1055         printf("Wi-Fi tethring port forwarding(0:disable 1:enable): ");
1056         ret = scanf("%d", &enable);
1057
1058         ret =tethering_wifi_enable_port_forwarding(th, enable);
1059         if (__is_err(ret) == true) {
1060                 printf("Fail to enable port forwarding!\n");
1061                 return -1;
1062         }
1063
1064         return 1;
1065 }
1066
1067 static int test_tethering_wifi_add_port_forwarding_rule(void)
1068 {
1069         int ret;
1070         char ifname[20];
1071         char proto[20];
1072         char org_ip[16];
1073         char final_ip[16];
1074         int org_port, final_port;
1075
1076         printf("Input ifname, protocol, original ip/port, final ip/port: ");
1077         ret = scanf("%19s", ifname);
1078         ret = scanf("%19s", proto);
1079         ret = scanf("%15s", org_ip);
1080         ret = scanf("%d", &org_port);
1081         ret = scanf("%15s", final_ip);
1082         ret = scanf("%d", &final_port);
1083
1084         ret = tethering_wifi_add_port_forwarding_rule(th, ifname, proto, org_ip, org_port, final_ip, final_port);
1085         if (__is_err(ret) == true) {
1086                 printf("Fail to add port forwarding rule!\n");
1087                 return -1;
1088         }
1089
1090         return 1;
1091 }
1092
1093 static int test_tethering_wifi_reset_port_forwarding_rule(void)
1094 {
1095         int ret;
1096
1097         ret = tethering_wifi_reset_port_forwarding_rule(th);
1098         if (__is_err(ret) == true) {
1099                 printf("Fail to reset port forwarding rule!\n");
1100                 return -1;
1101         }
1102
1103         return 1;
1104 }
1105
1106 static int test_tethering_wifi_get_port_forwarding_rule(void)
1107 {
1108         int ret = 0;
1109         void *pf_list = NULL;
1110
1111         ret = tethering_wifi_get_port_forwarding_rule(th, &pf_list);
1112         if (__is_err(ret) == true) {
1113                 printf("Fail to get port forwarding rule!\n");
1114                 return -1;
1115         }
1116
1117         __display_list(pf_list);
1118
1119         return 1;
1120 }
1121
1122 static int test_tethering_wifi_enable_port_filtering(void)
1123 {
1124         int ret;
1125         int enable = false;
1126
1127         printf("Wi-Fi tethring port filtering(0:disable 1:enable): ");
1128         ret = scanf("%d", &enable);
1129
1130         ret =tethering_wifi_enable_port_filtering(th, enable);
1131         if (__is_err(ret) == true) {
1132                 printf("Fail to enable port filtering!\n");
1133                 return -1;
1134         }
1135
1136         return 1;
1137 }
1138
1139 static int test_tethering_wifi_add_port_filtering_rule(void)
1140 {
1141         int ret;
1142         char proto[20];
1143         int port;
1144         int allow;
1145
1146         printf("Input protocol, port, allow: ");
1147         ret = scanf("%19s", proto);
1148         ret = scanf("%d", &port);
1149         ret = scanf("%d", &allow);
1150
1151         ret = tethering_wifi_add_port_filtering_rule(th, port, proto, allow);
1152         if (__is_err(ret) == true) {
1153                 printf("Fail to add port forwarding rule!\n");
1154                 return -1;
1155         }
1156
1157         return 1;
1158 }
1159
1160 static int test_tethering_wifi_add_custom_port_filtering_rule(void)
1161 {
1162         int ret;
1163         char proto[20];
1164         int port1, port2;
1165         int allow;
1166
1167         printf("Input protocol, port1, port2, allow: ");
1168         ret = scanf("%19s", proto);
1169         ret = scanf("%d", &port1);
1170         ret = scanf("%d", &port2);
1171         ret = scanf("%d", &allow);
1172
1173         ret = tethering_wifi_add_custom_port_filtering_rule(th, port1, port2, proto, allow);
1174         if (__is_err(ret) == true) {
1175                 printf("Fail to add custom port forwarding rule!\n");
1176                 return -1;
1177         }
1178
1179         return 1;
1180 }
1181
1182 static int test_tethering_wifi_get_port_filtering_rule(void)
1183 {
1184         int ret = 0;
1185         void *pf_list = NULL;
1186
1187         ret = tethering_wifi_get_port_filtering_rule(th, &pf_list);
1188         if (__is_err(ret) == true) {
1189                 printf("Fail to get port filtering rule!\n");
1190                 return -1;
1191         }
1192
1193         __display_list(pf_list);
1194
1195         return 1;
1196 }
1197
1198 static int test_tethering_wifi_get_custom_port_filtering_rule(void)
1199 {
1200         int ret = 0;
1201         void *pf_list = NULL;
1202
1203         ret = tethering_wifi_get_custom_port_filtering_rule(th, &pf_list);
1204         if (__is_err(ret) == true) {
1205                 printf("Fail to get port filtering rule!\n");
1206                 return -1;
1207         }
1208
1209         __display_list(pf_list);
1210
1211         return 1;
1212 }
1213
1214 static int test_tethering_wifi_set_vpn_passthrough_rule(void)
1215 {
1216         int ret = 0;
1217         int type;
1218
1219         printf("Select vpn passthrough type (0:IPSEC 1:PPTP 2:L2TP): ");
1220         ret = scanf("%d", &type);
1221
1222         ret = tethering_wifi_set_vpn_passthrough_rule(th, (tethering_vpn_passthrough_type_e)type, true);
1223         if (__is_err(ret) == true) {
1224                 printf("Fail to get port filtering rule!\n");
1225                 return -1;
1226         }
1227
1228         return 1;
1229 }
1230
1231 int main(int argc, char **argv)
1232 {
1233         GMainLoop *mainloop;
1234
1235 #if !GLIB_CHECK_VERSION(2, 36, 0)
1236         g_type_init();
1237 #endif
1238         mainloop = g_main_loop_new(NULL, false);
1239
1240         GIOChannel *channel = g_io_channel_unix_new(0);
1241         g_io_add_watch(channel, (G_IO_IN|G_IO_ERR|G_IO_HUP|G_IO_NVAL), test_thread, NULL);
1242         printf("Test Thread created...\n");
1243         g_main_loop_run(mainloop);
1244
1245         return 0;
1246 }
1247
1248 gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data)
1249 {
1250         int rv;
1251         char a[10];
1252
1253         printf("Event received from stdin\n");
1254
1255         rv = read(0, a, 10);
1256
1257         if (rv <= 0 || a[0] == '0')
1258                 exit(1);
1259
1260         if (a[0] == '\n' || a[0] == '\r') {
1261                 printf("\n\n Network Connection API Test App\n\n");
1262                 printf("Options..\n");
1263                 printf("1       - Tethering create and set callbacks\n");
1264                 printf("2       - Tethering destroy\n");
1265                 printf("3       - Enable Tethering\n");
1266                 printf("4       - Disable Tethering\n");
1267                 printf("5       - Get client information\n");
1268                 printf("6       - Get interface information\n");
1269                 printf("7       - Get data usage\n");
1270                 printf("8       - Get Wi-Fi tethering setting\n");
1271                 printf("a       - Set Wi-Fi tethering SSID\n");
1272                 printf("b       - Set Wi-Fi tethering security type\n");
1273                 printf("c       - Set Wi-Fi tethering visibility\n");
1274                 printf("d       - Set Wi-Fi tethering passphrase\n");
1275                 printf("e       - Set Wi-Fi tethering mac filtering\n");
1276                 printf("f       - Add/Remove MAC adress to/from allowed/blocked list\n");
1277                 printf("g       - Get allowed/blocked list\n");
1278                 printf("k       - Reload Wi-Fi tethering\n");
1279                 printf("m       - Set Wi-Fi channel\n");
1280                 printf("n       - Set Wi-Fi hw_mode\n");
1281                 printf("o       - Enable dhcp server\n");
1282                 printf("p       - Enable dhcp server with range\n");
1283                 printf("q       - Is dhcp server enabled?\n");
1284                 printf("r       - Get Wi-Fi txpower\n");
1285                 printf("s       - Set Wi-Fi txpower\n");
1286                 printf("t       - Set Wi-Fi mtu\n");
1287                 printf("u       - Change mac address\n");
1288                 printf("v       - Set max connected device(Wi-Fi tethering)\n");
1289                 printf("w       - Enable port forwarding\n");
1290                 printf("x       - Add port forwarding rule\n");
1291                 printf("y       - Reset port forwarding rule\n");
1292                 printf("z       - Get port forwarding rule\n");
1293                 printf("A       - Enable port filtering\n");
1294                 printf("B       - Add port filtering rule\n");
1295                 printf("C       - Add custom port filtering rule\n");
1296                 printf("D       - Get port filtering rule\n");
1297                 printf("E       - Get custom port filtering rule\n");
1298                 printf("F       - Set vpn passthrough rule\n");
1299                 printf("0       - \n");
1300                 printf("ENTER  - Show options menu.......\n");
1301         }
1302
1303         switch (a[0]) {
1304         case '1':
1305                 rv = test_tethering_create();
1306                 break;
1307         case '2':
1308                 rv = test_tethering_destroy();
1309                 break;
1310         case '3':
1311                 rv = test_tethering_enable();
1312                 break;
1313         case '4':
1314                 rv = test_tethering_disable();
1315                 break;
1316         case '5':
1317                 rv = test_tethering_get_client_info();
1318                 break;
1319         case '6':
1320                 rv = test_tethering_get_interface_info();
1321                 break;
1322         case '7':
1323                 rv = test_tethering_get_data_usage();
1324                 break;
1325         case '8':
1326                 rv = test_tethering_wifi_get_setting();
1327                 break;
1328         case 'a':
1329                 rv = test_tethering_wifi_set_ssid();
1330                 break;
1331         case 'b':
1332                 rv = test_tethering_wifi_set_security_type();
1333                 break;
1334         case 'c':
1335                 rv = test_tethering_wifi_set_visibility();
1336                 break;
1337         case 'd':
1338                 rv = test_tethering_wifi_set_passphrase();
1339                 break;
1340         case 'e':
1341                 rv = test_tethering_wifi_set_mac_filtering();
1342                 break;
1343         case 'f':
1344                 rv = test_tethering_manage_mac_list();
1345                 break;
1346         case 'g':
1347                 rv = test_tethering_get_mac_list();
1348                 break;
1349         case 'k':
1350                 rv = test_tethering_wifi_reload_settings();
1351                 break;
1352         case 'm':
1353                 rv = test_tethering_wifi_set_channel();
1354                 break;
1355         case 'n':
1356                 rv = test_tethering_wifi_set_mode();
1357                 break;
1358         case 'o':
1359                 rv = test_tethering_wifi_enable_dhcp();
1360                 break;
1361         case 'p':
1362                 rv = test_tethering_wifi_set_dhcp_range();
1363                 break;
1364         case 'q':
1365                 rv = test_tethering_wifi_is_dhcp_enabled();
1366                 break;
1367         case 'r':
1368                 rv = test_tethering_wifi_get_txpower();
1369                 break;
1370         case 's':
1371                 rv = test_tethering_wifi_set_txpower();
1372                 break;
1373         case 't':
1374                 rv = test_tethering_wifi_set_mtu();
1375                 break;
1376         case 'u':
1377                 rv = test_tethering_wifi_change_mac();
1378                 break;
1379         case 'v':
1380                 rv = test_tethering_wifi_set_max_connected_device();
1381                 break;
1382         case 'w':
1383                 rv = test_tethering_wifi_enable_port_forwarding();
1384                 break;
1385         case 'x':
1386                 rv = test_tethering_wifi_add_port_forwarding_rule();
1387                 break;
1388         case 'y':
1389                 rv = test_tethering_wifi_reset_port_forwarding_rule();
1390                 break;
1391         case 'z':
1392                 rv = test_tethering_wifi_get_port_forwarding_rule();
1393                 break;
1394         case 'A':
1395                 rv = test_tethering_wifi_enable_port_filtering();
1396                 break;
1397         case 'B':
1398                 rv = test_tethering_wifi_add_port_filtering_rule();
1399                 break;
1400         case 'C':
1401                 rv = test_tethering_wifi_add_custom_port_filtering_rule();
1402                 break;
1403         case 'D':
1404                 rv = test_tethering_wifi_get_port_filtering_rule();
1405                 break;
1406         case 'E':
1407                 rv = test_tethering_wifi_get_custom_port_filtering_rule();
1408                 break;
1409         case 'F':
1410                 rv = test_tethering_wifi_set_vpn_passthrough_rule();
1411                 break;
1412         }
1413
1414         if (rv == 1)
1415                 printf("Operation succeeded!\n");
1416         else
1417                 printf("Operation failed!\n");
1418
1419         return true;
1420 }