Remove 'reserved (wifi ap)' type
[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         int channel = 0;
461         tethering_wifi_security_type_e security_type = TETHERING_WIFI_SECURITY_TYPE_NONE;
462         tethering_wifi_mode_type_e hw_mode = TETHERING_WIFI_MODE_TYPE_G;
463
464         int error = TETHERING_ERROR_NONE;
465
466         error = tethering_wifi_get_ssid(th, &ssid);
467         if (error != TETHERING_ERROR_NONE)
468                 __is_err(error);
469         else
470                 g_print("\n\t** WiFi tethering SSID : %s\n", ssid);
471
472         error = tethering_wifi_get_passphrase(th, &passphrase);
473         if (error != TETHERING_ERROR_NONE)
474                 __is_err(error);
475         else
476                 g_print("\t** WiFi tethering passphrase : %s\n", passphrase);
477
478         error = tethering_wifi_get_ssid_visibility(th, &visibility);
479         if (error != TETHERING_ERROR_NONE)
480                 __is_err(error);
481         else
482                 g_print("\t** WiFi tethering ssid visibility : %s\n",
483                                 visibility ? "visible" : "invisible");
484
485         error = tethering_wifi_get_security_type(th, &security_type);
486         if (error != TETHERING_ERROR_NONE)
487                 __is_err(error);
488         else
489                 g_print("\t** WiFi tethering security_type : %s\n",
490                                 security_type ==
491                                 TETHERING_WIFI_SECURITY_TYPE_NONE ?
492                                 "open" : "wpa2-psk");
493
494         error = tethering_wifi_get_mac_filter(th, &mac_filter);
495         if (error != TETHERING_ERROR_NONE)
496                 __is_err(error);
497         else
498                 g_print("\t** WiFi tethering mac filter : %s\n",
499                                 mac_filter ? "enable" : "disable");
500
501         error = tethering_wifi_get_mode(th, &hw_mode);
502         if (error != TETHERING_ERROR_NONE)
503                 __is_err(error);
504         else
505                  g_print("\t** WiFi tethering mode : %d\n", hw_mode);
506
507         error = tethering_wifi_get_channel(th, &channel);
508         if (error != TETHERING_ERROR_NONE)
509                 __is_err(error);
510         else
511                  g_print("\t** WiFi tethering channel : %d\n", channel);
512
513         if (ssid)
514                 free(ssid);
515         if (passphrase)
516                 free(passphrase);
517
518         return;
519 }
520
521 bool __get_tethering_type(tethering_type_e *type)
522 {
523         int sel;
524         int ret;
525
526         printf("Select tethering type (1:Wi-Fi, 2:BT, 3:USB 4:ALL)\n");
527         ret = scanf("%9d", &sel);
528         if (ret < 0) {
529                 printf("scanf is failed!!\n");
530                 return false;
531         }
532
533         switch (sel) {
534         case 1:
535                 *type = TETHERING_TYPE_WIFI;
536                 break;
537         case 2:
538                 *type = TETHERING_TYPE_BT;
539                 break;
540         case 3:
541                 *type = TETHERING_TYPE_USB;
542                 break;
543         case 4:
544                 *type = TETHERING_TYPE_ALL;
545                 break;
546         default:
547                 printf("Invalid input!!\n");
548                 return false;
549         }
550
551         return true;
552 }
553
554 static int test_tethering_create(void)
555 {
556         int ret = tethering_create(&th);
557         __tethering_cbs cbs = {
558                 __enabled_cb, __disabled_cb,
559                 __connection_state_changed_cb, __security_type_changed_cb,
560                 __ssid_visibility_changed_cb, __passphrase_changed_cb};
561
562         if (__is_err(ret) == false) __register_cbs(th, &cbs, NULL);
563         else {
564                 printf("Tethering create is failed\n");
565                 return -1;
566         }
567         printf("Tethering create and register callback success\n");
568
569         return 1;
570 }
571
572 static int test_tethering_destroy(void)
573 {
574         int ret = TETHERING_ERROR_NONE;
575
576         __deregister_cbs(th);
577
578         ret = tethering_destroy(th);
579         if (__is_err(ret) == true) {
580                 printf("Tethering destroy is failed\n");
581                 return -1;
582         }
583
584         return 1;
585 }
586
587 static int test_tethering_enable(void)
588 {
589         int ret = TETHERING_ERROR_NONE;
590         tethering_type_e type;
591
592         if (!__get_tethering_type(&type))
593                 return -1;
594
595         ret = tethering_enable(th, type);
596         if (__is_err(ret) == true) {
597                 printf("Fail to enable tethering\n");
598                 return -1;
599         }
600         return 1;
601 }
602
603 static int test_tethering_disable(void)
604 {
605         int ret = TETHERING_ERROR_NONE;
606         tethering_type_e type;
607
608         if (!__get_tethering_type(&type))
609                 return -1;
610
611         ret = tethering_disable(th, type);
612         if (__is_err(ret) == true) {
613                 printf("Fail to disable tethering\n");
614                 return -1;
615         }
616         return 1;
617 }
618
619 static int test_tethering_get_client_info(void)
620 {
621         int ret;
622         tethering_type_e type;
623
624         if (!__get_tethering_type(&type))
625                 return -1;
626
627         ret = tethering_foreach_connected_clients(th, type,
628                                         __clients_foreach_cb, NULL);
629         if (__is_err(ret) == true) {
630                 printf("Fail to disable tethering\n");
631                 return -1;
632         }
633
634         return 1;
635 }
636
637 static int test_tethering_get_interface_info(void)
638 {
639         tethering_type_e type;
640
641         if (!__get_tethering_type(&type))
642                 return -1;
643
644         __print_interface_info(th, type);
645
646         return 1;
647 }
648
649 static int test_tethering_get_data_usage(void)
650 {
651         int ret = tethering_get_data_usage(th, __data_usage_cb, NULL);
652
653         if (__is_err(ret) == true) {
654                 printf("Fail to get data usage!!\n");
655                 return -1;
656         }
657
658         return 1;
659 }
660
661 static int test_tethering_wifi_get_setting(void)
662 {
663         __print_wifi_tethering_setting(th);
664         return 1;
665 }
666
667 static int test_tethering_wifi_set_ssid(void)
668 {
669         int ret;
670         char ssid[100];
671
672         printf("Input SSID for Wi-Fi tethering: ");
673         ret = scanf("%99s", ssid);
674         if (ret < 0) {
675                 printf("scanf is failed!!\n");
676                 return -1;
677         }
678
679         ret = tethering_wifi_set_ssid(th, ssid);
680         if (__is_err(ret) == true) {
681                 printf("Fail to set wifi ssid!!\n");
682                 return -1;
683         }
684
685         return 1;
686 }
687
688 static int test_tethering_wifi_set_security_type(void)
689 {
690         int ret;
691         int security_type;
692
693         printf("Input security type for Wi-Fi tethering (0:NONE, 1:WPA2_PSK)");
694         ret = scanf("%9d", &security_type);
695         if (ret < 0) {
696                 printf("scanf is failed!!\n");
697                 return -1;
698         }
699
700         ret = tethering_wifi_set_security_type(th, security_type);
701         if (__is_err(ret) == true) {
702                 printf("Fail to set security type!!\n");
703                 return -1;
704         }
705
706         return 1;
707 }
708
709 int test_tethering_wifi_set_visibility(void)
710 {
711         int ret;
712         int visibility;
713
714         printf("Input security type for Wi-Fi tethering (0:invisible, 1:visible)");
715         ret = scanf("%9d", &visibility);
716         if (ret < 0) {
717                 printf("scanf is failed!!\n");
718                 return -1;
719         }
720
721         ret = tethering_wifi_set_ssid_visibility(th, visibility);
722         if (__is_err(ret) == true) {
723                 printf("Fail to set visibility!!\n");
724                 return -1;
725         }
726
727         return 1;
728 }
729
730 static int test_tethering_wifi_set_passphrase(void)
731 {
732         int ret;
733         char passphrase[100];
734
735         printf("Input passphrase for Wi-Fi tethering: ");
736         ret = scanf("%99s", passphrase);
737         if (ret < 0) {
738                 printf("scanf is failed!!\n");
739                 return -1;
740         }
741
742         ret = tethering_wifi_set_passphrase(th, passphrase);
743         if (__is_err(ret) == true) {
744                 printf("Fail to set passphrase!!\n");
745                 return -1;
746         }
747
748         return 1;
749 }
750
751 static int test_tethering_wifi_set_channel(void)
752 {
753         int ret;
754         int channel;
755
756         printf("Input channel for Wi-Fi tethering: ");
757         ret = scanf("%d", &channel);
758
759         ret = tethering_wifi_set_channel(th, channel);
760         if (__is_err(ret) == true) {
761                 printf("Fail to set channel!!\n");
762                 return -1;
763         }
764
765         return 1;
766 }
767
768 static int test_tethering_wifi_set_mode(void)
769 {
770         int ret;
771         int type;
772
773         printf("Input hw_mode for Wi-Fi tethering(0-b, 1-g, 2-a, 3-ad): ");
774         ret = scanf("%d", &type);
775
776         ret = tethering_wifi_set_mode(th, type);
777         if (__is_err(ret) == true) {
778                 printf("Fail to set mode!!\n");
779                 return -1;
780         }
781
782         return 1;
783 }
784
785 static int test_tethering_wifi_enable_dhcp(void)
786 {
787         int ret;
788         int enable;
789
790         printf("Input (0-Disable, 1-Enable): ");
791         ret = scanf("%d", &enable);
792
793         ret = tethering_wifi_enable_dhcp(th, enable);
794         if (__is_err(ret) == true) {
795                 printf("Fail to enable dhcp server!!\n");
796                 return -1;
797         }
798
799         return 1;
800 }
801
802 static int test_tethering_wifi_set_dhcp_range(void)
803 {
804         int ret;
805         char rangestart[16], rangestop[16];
806
807         printf("Input range (ex: 192.168.0.50 192.168.0.150): ");
808
809         ret = scanf("%15s %15s", rangestart, rangestop);
810
811         ret = tethering_wifi_set_dhcp_range(th, rangestart, rangestop);
812         if (__is_err(ret) == true) {
813                 printf("Fail to set dhcp range and enable dhcp server!!\n");
814                 return -1;
815         }
816
817         return 1;
818 }
819
820 static int test_tethering_wifi_is_dhcp_enabled(void)
821 {
822         int ret;
823         bool enabled;
824
825         ret = tethering_wifi_is_dhcp_enabled(th, &enabled);
826
827         if (__is_err(ret) == true) {
828                 printf("Fail to get dhcp server status!!\n");
829                 return -1;
830         } else {
831                 printf("DHCP server is %s\n", enabled ? "enabled" : "disabled");
832         }
833
834         return 1;
835 }
836
837 static int test_tethering_wifi_set_mac_filtering(void)
838 {
839         int ret;
840         int enable;
841
842         printf("Input mac filtering option (0: disable, 1: enable): ");
843         ret = scanf("%d", &enable);
844
845         ret = tethering_wifi_set_mac_filter(th, enable);
846         if (__is_err(ret) == true) {
847                 printf("Fail to set mac filtering!!\n");
848                 return -1;
849         }
850
851         return 1;
852 }
853
854 static int test_tethering_manage_mac_list(void)
855 {
856         int ret = 0;
857         int list, option;
858         char mac[100];
859
860         printf("Select MAC list to modify (0: allowed mac list, 1: blocked mac list): ");
861         ret = scanf("%d", &list);
862
863         printf("Select option (0: Add, 1: Remove): ");
864         ret = scanf("%d", &option);
865
866         printf("Input MAC Address to add/remove allowed/blocked mac list: ");
867         ret = scanf("%99s", mac);
868         if (ret < 0) {
869                 printf("scanf is failed!!\n");
870                 return -1;
871         }
872
873         if (!list && !option) {
874                 /* Add to allowed mac list*/
875                 ret = tethering_wifi_add_allowed_mac_list(th, mac);
876         } else if (!list && option) {
877                 /* Remove from allowed mac list */
878                 ret = tethering_wifi_remove_allowed_mac_list(th, mac);
879         } else if (list && !option) {
880                 /* Add to blocked mac list */
881                 ret = tethering_wifi_add_blocked_mac_list(th, mac);
882         } else if (list && option) {
883                 /* Remove from blocked mac list */
884                 ret = tethering_wifi_remove_blocked_mac_list(th, mac);
885         } else {
886                 printf("Input Failed!!\n");
887                 return -1;
888         }
889
890         if (ret < 0)
891                 return -1;
892
893         return 1;
894 }
895
896 static int test_tethering_wifi_reload_settings(void)
897 {
898         int ret = tethering_wifi_reload_settings(th, __settings_reloaded_cb, NULL);
899
900         if (__is_err(ret) == true) {
901                 printf("Fail to reload wifi tethering!!\n");
902                 return -1;
903         }
904         return 1;
905 }
906
907 int main(int argc, char **argv)
908 {
909         GMainLoop *mainloop;
910
911 #if !GLIB_CHECK_VERSION(2, 36, 0)
912         g_type_init();
913 #endif
914         mainloop = g_main_loop_new(NULL, false);
915
916         GIOChannel *channel = g_io_channel_unix_new(0);
917         g_io_add_watch(channel, (G_IO_IN|G_IO_ERR|G_IO_HUP|G_IO_NVAL), test_thread, NULL);
918         printf("Test Thread created...\n");
919         g_main_loop_run(mainloop);
920
921         return 0;
922 }
923
924 gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data)
925 {
926         int rv;
927         char a[10];
928
929         printf("Event received from stdin\n");
930
931         rv = read(0, a, 10);
932
933         if (rv <= 0 || a[0] == '0')
934                 exit(1);
935
936         if (a[0] == '\n' || a[0] == '\r') {
937                 printf("\n\n Network Connection API Test App\n\n");
938                 printf("Options..\n");
939                 printf("1       - Tethering create and set callbacks\n");
940                 printf("2       - Tethering destroy\n");
941                 printf("3       - Enable Tethering\n");
942                 printf("4       - Disable Tethering\n");
943                 printf("5       - Get client information\n");
944                 printf("6       - Get interface information\n");
945                 printf("7       - Get data usage\n");
946                 printf("8       - Get Wi-Fi tethering setting\n");
947                 printf("a       - Set Wi-Fi tethering SSID\n");
948                 printf("b       - Set Wi-Fi tethering security type\n");
949                 printf("c       - Set Wi-Fi tethering visibility\n");
950                 printf("d       - Set Wi-Fi tethering passphrase\n");
951                 printf("e       - Set Wi-Fi tethering mac filtering\n");
952                 printf("f       - Add/Remove MAC adress to/from allowed/blocked list\n");
953                 printf("k       - Reload Wi-Fi tethering\n");
954                 printf("m       - Set Wi-Fi channel\n");
955                 printf("n       - Set Wi-Fi hw_mode\n");
956                 printf("o       - Enable dhcp server\n");
957                 printf("p       - Enable dhcp server with range\n");
958                 printf("q       - Is dhcp server enabled?\n");
959                 printf("0       - Exit \n");
960                 printf("ENTER  - Show options menu.......\n");
961         }
962
963         switch (a[0]) {
964         case '1':
965                 rv = test_tethering_create();
966                 break;
967         case '2':
968                 rv = test_tethering_destroy();
969                 break;
970         case '3':
971                 rv = test_tethering_enable();
972                 break;
973         case '4':
974                 rv = test_tethering_disable();
975                 break;
976         case '5':
977                 rv = test_tethering_get_client_info();
978                 break;
979         case '6':
980                 rv = test_tethering_get_interface_info();
981                 break;
982         case '7':
983                 rv = test_tethering_get_data_usage();
984                 break;
985         case '8':
986                 rv = test_tethering_wifi_get_setting();
987                 break;
988         case 'a':
989                 rv = test_tethering_wifi_set_ssid();
990                 break;
991         case 'b':
992                 rv = test_tethering_wifi_set_security_type();
993                 break;
994         case 'c':
995                 rv = test_tethering_wifi_set_visibility();
996                 break;
997         case 'd':
998                 rv = test_tethering_wifi_set_passphrase();
999                 break;
1000         case 'e':
1001                 rv = test_tethering_wifi_set_mac_filtering();
1002                 break;
1003         case 'f':
1004                 rv = test_tethering_manage_mac_list();
1005                 break;
1006         case 'k':
1007                 rv = test_tethering_wifi_reload_settings();
1008                 break;
1009         case 'm':
1010                 rv = test_tethering_wifi_set_channel();
1011                 break;
1012         case 'n':
1013                 rv = test_tethering_wifi_set_mode();
1014                 break;
1015         case 'o':
1016                 rv = test_tethering_wifi_enable_dhcp();
1017                 break;
1018         case 'p':
1019                 rv = test_tethering_wifi_set_dhcp_range();
1020                 break;
1021         case 'q':
1022                 rv = test_tethering_wifi_is_dhcp_enabled();
1023                 break;
1024         }
1025
1026         if (rv == 1)
1027                 printf("Operation succeeded!\n");
1028         else
1029                 printf("Operation failed!\n");
1030
1031         return true;
1032 }