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