[capi-tethering] Enabling/disabling the mac-filtering feature
[platform/core/api/tethering.git] / test / tethering_test.c
1 /*
2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <unistd.h>
21 #include <glib.h>
22 #include <glib-object.h>
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
26
27 #include <vconf.h>
28
29 #include "tethering.h"
30
31 #define DISABLE_REASON_TEXT_LEN 64
32 #define COMMON_STR_BUF_LEN      32
33
34 gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data);
35
36 typedef struct {
37         tethering_enabled_cb enabled_cb;
38         tethering_disabled_cb disabled_cb;
39         tethering_connection_state_changed_cb changed_cb;
40         tethering_wifi_security_type_changed_cb security_type_changed_cb;
41         tethering_wifi_ssid_visibility_changed_cb ssid_visibility_changed_cb;
42         tethering_wifi_passphrase_changed_cb passphrase_changed_cb;
43 } __tethering_cbs;
44
45 tethering_h th = NULL;
46
47 static bool __is_err(tethering_error_e ret)
48 {
49         char *err_msg = NULL;
50
51         switch (ret) {
52         case TETHERING_ERROR_INVALID_PARAMETER:
53                 err_msg = "Wrong parameter is used";
54                 break;
55
56         case TETHERING_ERROR_OUT_OF_MEMORY:
57                 err_msg = "Memory is not enough";
58                 break;
59
60         case TETHERING_ERROR_NONE:
61                 return false;
62
63         case TETHERING_ERROR_NOT_ENABLED:
64                 err_msg = "Tethering is not enabled";
65                 break;
66
67         case TETHERING_ERROR_OPERATION_FAILED:
68                 err_msg = "Operation is failed";
69                 break;
70
71         case TETHERING_ERROR_RESOURCE_BUSY:
72                 err_msg = "Resource is busy";
73                 break;
74
75         default:
76                 err_msg = "This should not be happened";
77                 break;
78         }
79
80         g_print("##ERR: %s\n", err_msg);
81
82         return true;
83 }
84
85 static const char *__convert_tethering_type_to_str(const tethering_type_e type)
86 {
87         static char str_buf[COMMON_STR_BUF_LEN] = {0, };
88
89         switch (type) {
90         case TETHERING_TYPE_USB:
91                 g_strlcpy(str_buf, "USB Tethering", sizeof(str_buf));
92                 break;
93
94         case TETHERING_TYPE_WIFI:
95                 g_strlcpy(str_buf, "Wi-Fi Tethering", sizeof(str_buf));
96                 break;
97
98         case TETHERING_TYPE_BT:
99                 g_strlcpy(str_buf, "Bluetooth Tethering", sizeof(str_buf));
100                 break;
101
102         case TETHERING_TYPE_RESERVED:
103                 g_strlcpy(str_buf, "Wi-Fi AP", sizeof(str_buf));
104                 break;
105
106         default:
107                 g_strlcpy(str_buf, "Unknown", sizeof(str_buf));
108                 break;
109         }
110
111         return str_buf;
112 }
113
114 static const char *__convert_disabled_code_to_str(const tethering_disabled_cause_e code)
115 {
116         static char str_buf[DISABLE_REASON_TEXT_LEN] = {0, };
117
118         switch (code) {
119         case TETHERING_DISABLED_BY_USB_DISCONNECTION:
120                 strncpy(str_buf, "disabled due to usb disconnection", sizeof(str_buf));
121                 break;
122
123         case TETHERING_DISABLED_BY_FLIGHT_MODE:
124                 strncpy(str_buf, "disabled due to flight mode on", sizeof(str_buf));
125                 break;
126
127         case TETHERING_DISABLED_BY_LOW_BATTERY:
128                 strncpy(str_buf, "disabled due to low battery", sizeof(str_buf));
129                 break;
130
131         case TETHERING_DISABLED_BY_NETWORK_CLOSE:
132                 strncpy(str_buf, "disabled due to pdp network close", sizeof(str_buf));
133                 break;
134
135         case TETHERING_DISABLED_BY_TIMEOUT:
136                 strncpy(str_buf, "disabled due to timeout", sizeof(str_buf));
137                 break;
138
139         case TETHERING_DISABLED_BY_OTHERS:
140                 strncpy(str_buf, "disabled by other apps", sizeof(str_buf));
141                 break;
142
143         case TETHERING_DISABLED_BY_REQUEST:
144                 strncpy(str_buf, "disabled by my request", sizeof(str_buf));
145                 break;
146
147         case TETHERING_DISABLED_BY_WIFI_ON:
148                 strncpy(str_buf, "disabled by Wi-Fi station on", sizeof(str_buf));
149                 break;
150
151         case TETHERING_DISABLED_BY_BT_OFF:
152                 strncpy(str_buf, "disabled by bluetooth off", sizeof(str_buf));
153                 break;
154
155         default:
156                 strncpy(str_buf, "disabled by unknown reason", sizeof(str_buf));
157                 break;
158         }
159
160         return str_buf;
161 }
162
163 static void __register_cbs(tethering_h th, __tethering_cbs *cbs, void *user_data)
164 {
165         tethering_error_e ret = TETHERING_ERROR_NONE;
166
167         ret = tethering_set_enabled_cb(th, TETHERING_TYPE_ALL,
168                         cbs->enabled_cb, user_data);
169         if (__is_err(ret) == true)
170                 g_print("tethering_set_enabled_cb is failed\n");
171
172         ret = tethering_set_enabled_cb(th, TETHERING_TYPE_RESERVED,
173                         cbs->enabled_cb, user_data);
174         if (__is_err(ret) == true)
175                 g_print("tethering_set_enabled_cb is failed\n");
176
177         ret = tethering_set_disabled_cb(th, TETHERING_TYPE_ALL,
178                         cbs->disabled_cb, user_data);
179         if (__is_err(ret) == true)
180                 g_print("tethering_set_disabled_cb is failed\n");
181
182         ret = tethering_set_disabled_cb(th, TETHERING_TYPE_RESERVED,
183                         cbs->disabled_cb, user_data);
184         if (__is_err(ret) == true)
185                 g_print("tethering_set_disabled_cb is failed\n");
186
187         ret = tethering_set_connection_state_changed_cb(th, TETHERING_TYPE_ALL,
188                         cbs->changed_cb, user_data);
189         if (__is_err(ret) == true)
190                 g_print("tethering_set_connection_state_changed_cb is failed\n");
191
192         ret = tethering_set_connection_state_changed_cb(th, TETHERING_TYPE_RESERVED,
193                         cbs->changed_cb, user_data);
194         if (__is_err(ret) == true)
195                 g_print("tethering_set_connection_state_changed_cb is failed\n");
196
197         ret = tethering_wifi_set_security_type_changed_cb(th,
198                         cbs->security_type_changed_cb, user_data);
199         if (__is_err(ret) == true)
200                 g_print("tethering_wifi_set_security_type_changed_cb is failed\n");
201
202         ret = tethering_wifi_set_ssid_visibility_changed_cb(th,
203                         cbs->ssid_visibility_changed_cb, user_data);
204         if (__is_err(ret) == true)
205                 g_print("tethering_wifi_set_ssid_visibility_changed_cb is failed\n");
206
207         ret = tethering_wifi_set_passphrase_changed_cb(th,
208                         cbs->passphrase_changed_cb, user_data);
209         if (__is_err(ret) == true)
210                 g_print("tethering_wifi_set_passphrase_changed_cb is failed\n");
211
212         return;
213 }
214
215 static void __deregister_cbs(tethering_h th)
216 {
217         tethering_error_e ret = TETHERING_ERROR_NONE;
218
219         ret = tethering_unset_enabled_cb(th, TETHERING_TYPE_ALL);
220         if (__is_err(ret) == true)
221                 g_print("tethering_unset_enabled_cb is failed\n");
222
223         ret = tethering_unset_enabled_cb(th, TETHERING_TYPE_RESERVED);
224         if (__is_err(ret) == true)
225                 g_print("tethering_unset_enabled_cb is failed\n");
226
227         ret = tethering_unset_disabled_cb(th, TETHERING_TYPE_ALL);
228         if (__is_err(ret) == true)
229                 g_print("tethering_unset_disabled_cb is failed\n");
230
231         ret = tethering_unset_disabled_cb(th, TETHERING_TYPE_RESERVED);
232         if (__is_err(ret) == true)
233                 g_print("tethering_unset_disabled_cb is failed\n");
234
235         ret = tethering_unset_connection_state_changed_cb(th, TETHERING_TYPE_ALL);
236         if (__is_err(ret) == true)
237                 g_print("tethering_unset_connection_state_changed_cb is failed\n");
238
239         ret = tethering_unset_connection_state_changed_cb(th, TETHERING_TYPE_RESERVED);
240         if (__is_err(ret) == true)
241                 g_print("tethering_unset_connection_state_changed_cb is failed\n");
242
243         ret = tethering_wifi_unset_security_type_changed_cb(th);
244         if (__is_err(ret) == true)
245                 g_print("tethering_wifi_unset_security_type_changed_cb is failed\n");
246
247         ret = tethering_wifi_unset_ssid_visibility_changed_cb(th);
248         if (__is_err(ret) == true)
249                 g_print("tethering_wifi_unset_ssid_visibility_changed_cb is failed\n");
250
251         ret = tethering_wifi_unset_passphrase_changed_cb(th);
252         if (__is_err(ret) == true)
253                 g_print("tethering_wifi_unset_passphrase_changed_cb is failed\n");
254
255         return;
256 }
257
258 /* Tethering callbacks */
259 static void __enabled_cb(tethering_error_e error, tethering_type_e type, bool is_requested, void *data)
260 {
261         if (__is_err(error)) {
262                 if (!is_requested)
263                         return;
264
265                 g_print("## %s is not enabled. error code[0x%X]\n",
266                                 __convert_tethering_type_to_str(type),
267                                 error);
268                 return;
269         }
270
271         if (is_requested)
272                 g_print("## %s is enabled successfully\n",
273                                 __convert_tethering_type_to_str(type));
274         else
275                 g_print("## %s is enabled by other app\n",
276                                 __convert_tethering_type_to_str(type));
277
278         return;
279 }
280
281 static void __disabled_cb(tethering_error_e error, tethering_type_e type, tethering_disabled_cause_e code, void *data)
282 {
283         if (__is_err(error)) {
284                 if (code != TETHERING_DISABLED_BY_REQUEST)
285                         return;
286
287                 g_print("## %s is not disabled. error code[0x%X]\n",
288                                 __convert_tethering_type_to_str(type), error);
289                 return;
290         }
291
292         g_print("## %s is %s\n",
293                         __convert_tethering_type_to_str(type),
294                         __convert_disabled_code_to_str(code));
295
296         return;
297 }
298
299 static void __connection_state_changed_cb(tethering_client_h client, bool open, void *data)
300 {
301         tethering_client_h clone = NULL;
302         tethering_type_e type;
303         char *ip_address = NULL;
304         char *mac_address = NULL;
305         char *hostname = NULL;
306
307         tethering_client_clone(&clone, client);
308         if (clone == NULL) {
309                 g_print("tetheirng_client_clone is failed\n");
310                 return;
311         }
312
313         tethering_client_get_tethering_type(clone, &type);
314         tethering_client_get_ip_address(clone,
315                         TETHERING_ADDRESS_FAMILY_IPV4, &ip_address);
316         tethering_client_get_mac_address(clone, &mac_address);
317         tethering_client_get_name(clone, &hostname);
318
319         if (open) {
320                 g_print("## New station Type [%s], IP [%s], MAC [%s], hostname [%s]\n",
321                                 __convert_tethering_type_to_str(type),
322                                 ip_address, mac_address, hostname);
323         } else {
324                 g_print("## Disconnected station Type [%s], IP [%s], MAC [%s], hostname [%s]\n",
325                                 __convert_tethering_type_to_str(type),
326                                 ip_address, mac_address, hostname);
327         }
328
329         if (ip_address)
330                 free(ip_address);
331         if (mac_address)
332                 free(mac_address);
333         if (hostname)
334                 free(hostname);
335
336         tethering_client_destroy(clone);
337
338         return;
339 }
340
341 static void __data_usage_cb(tethering_error_e result, unsigned long long received_data,
342                 unsigned long long sent_data, void *user_data)
343 {
344         g_print("__data_usage_cb\n");
345
346         if (result != TETHERING_ERROR_NONE) {
347                 g_print("tethering_get_data_usage is failed. error[0x%X]\n", result);
348                 return;
349         }
350
351         g_print("## Received data : %llu bytes\n", received_data);
352         g_print("## Sent data : %llu bytes\n", sent_data);
353
354         return;
355 }
356
357 static void __settings_reloaded_cb(tethering_error_e result, void *user_data)
358 {
359         g_print("__settings_reloaded_cb\n");
360
361         if (result != TETHERING_ERROR_NONE) {
362                 g_print("tethering_wifi_reload_settings is failed. error[0x%X]\n", result);
363                 return;
364         }
365
366         g_print("## Wi-Fi tethering setting is reloaded\n");
367
368         return;
369 }
370
371 static bool __clients_foreach_cb(tethering_client_h client, void *data)
372 {
373         tethering_client_h clone = NULL;
374         tethering_type_e type;
375         char *ip_address = NULL;
376         char *mac_address = NULL;
377         char *hostname = NULL;
378
379         /* Clone internal information */
380         if (tethering_client_clone(&clone, client) != TETHERING_ERROR_NONE) {
381                 g_print("tethering_client_clone is failed\n");
382                 return false;
383         }
384
385         /* Get information */
386         if (tethering_client_get_tethering_type(clone, &type) != TETHERING_ERROR_NONE)
387                 g_print("tethering_client_get_type is failed\n");
388
389         if (tethering_client_get_ip_address(clone, TETHERING_ADDRESS_FAMILY_IPV4, &ip_address) != TETHERING_ERROR_NONE)
390                 g_print("tethering_client_get_ip_address is failed\n");
391
392         if (tethering_client_get_mac_address(clone, &mac_address) != TETHERING_ERROR_NONE)
393                 g_print("tethering_client_get_mac_address is failed\n");
394
395         if (tethering_client_get_name(clone, &hostname) != TETHERING_ERROR_NONE)
396                 g_print("tethering_client_get_hostname is failed\n");
397
398         /* End of getting information */
399
400         g_print("\n< Client Info. >\n");
401         g_print("\tType %s\n", __convert_tethering_type_to_str(type));
402         g_print("\tIP Address %s\n", ip_address);
403         g_print("\tMAC Address : %s\n", mac_address);
404         g_print("\tHostname : %s\n", hostname);
405
406         /* Destroy cloned objects */
407         if (ip_address)
408                 free(ip_address);
409         if (mac_address)
410                 free(mac_address);
411         if (hostname)
412                 free(hostname);
413
414         tethering_client_destroy(clone);
415
416         /* Continue iteration */
417         return true;
418 }
419
420 static void __security_type_changed_cb(tethering_wifi_security_type_e changed_type, void *user_data)
421 {
422         g_print("Wi-Fi Tethering Security type is changed to [%s]\n",
423                         changed_type == TETHERING_WIFI_SECURITY_TYPE_NONE ?
424                         "open" : "wpa2-psk");
425         return;
426 }
427
428 static void __ssid_visibility_changed_cb(bool changed_visible, void *user_data)
429 {
430         g_print("SSID visibility for Wi-Fi tethering changed to [%s]\n",
431                         changed_visible ? "visible" : "invisible");
432         return;
433 }
434
435 static void __passphrase_changed_cb(void *user_data)
436 {
437         g_print("Wi-Fi Tethering passphrase is changed\n");
438         return;
439 }
440 /* End of tethering callbacks */
441
442 static void __print_interface_info(tethering_h th, tethering_type_e type)
443 {
444         char *interface = NULL;
445         char *mac_address = NULL;
446         char *ip_address = NULL;
447         char *gateway_address = NULL;
448         char *subnet_mask = NULL;
449
450         if (tethering_is_enabled(th, type) == FALSE) {
451                 g_print("%s is not enabled\n",
452                                 __convert_tethering_type_to_str(type));
453                 return;
454         }
455
456         tethering_get_network_interface_name(th, type, &interface);
457         tethering_get_mac_address(th, type, &mac_address);
458         tethering_get_ip_address(th, type, TETHERING_ADDRESS_FAMILY_IPV4,
459                         &ip_address);
460         tethering_get_gateway_address(th, type, TETHERING_ADDRESS_FAMILY_IPV4,
461                         &gateway_address);
462         tethering_get_subnet_mask(th, type, TETHERING_ADDRESS_FAMILY_IPV4,
463                         &subnet_mask);
464
465         g_print("interface name : %s\n", interface);
466         g_print("mac address : %s\n", mac_address);
467         g_print("ip address : %s\n", ip_address);
468         g_print("gateway address: %s\n", gateway_address);
469         g_print("subnet mask : %s\n", subnet_mask);
470
471         if (interface)
472                 free(interface);
473         if (mac_address)
474                 free(mac_address);
475         if (ip_address)
476                 free(ip_address);
477         if (gateway_address)
478                 free(gateway_address);
479         if (subnet_mask)
480                 free(subnet_mask);
481
482         return;
483 }
484
485 static void __print_wifi_tethering_setting(tethering_h th)
486 {
487         char *ssid = NULL;
488         char *passphrase = NULL;
489         bool visibility = false;
490         bool mac_filter = 0;
491         tethering_wifi_security_type_e security_type = TETHERING_WIFI_SECURITY_TYPE_NONE;
492
493         int error = TETHERING_ERROR_NONE;
494
495         error = tethering_wifi_get_ssid(th, &ssid);
496         if (error != TETHERING_ERROR_NONE)
497                 __is_err(error);
498         else
499                 g_print("\n\t** WiFi tethering SSID : %s\n", ssid);
500
501         error = tethering_wifi_get_passphrase(th, &passphrase);
502         if (error != TETHERING_ERROR_NONE)
503                 __is_err(error);
504         else
505                 g_print("\t** WiFi tethering passphrase : %s\n", passphrase);
506
507         error = tethering_wifi_get_ssid_visibility(th, &visibility);
508         if (error != TETHERING_ERROR_NONE)
509                 __is_err(error);
510         else
511                 g_print("\t** WiFi tethering ssid visibility : %s\n",
512                                 visibility ? "visible" : "invisible");
513
514         error = tethering_wifi_get_security_type(th, &security_type);
515         if (error != TETHERING_ERROR_NONE)
516                 __is_err(error);
517         else
518                 g_print("\t** WiFi tethering security_type : %s\n",
519                                 security_type ==
520                                 TETHERING_WIFI_SECURITY_TYPE_NONE ?
521                                 "open" : "wpa2-psk");
522
523         error = tethering_wifi_get_mac_filter(th, &mac_filter);
524         if (error != TETHERING_ERROR_NONE)
525                 __is_err(error);
526         else
527                 g_print("\t** WiFi tethering mac filter : %s\n",
528                                 mac_filter ? "enable" : "disable");
529
530         if (ssid)
531                 free(ssid);
532         if (passphrase)
533                 free(passphrase);
534
535         return;
536 }
537
538 static void __print_wifi_ap_setting(tethering_h th)
539 {
540         char *ssid = NULL;
541         char *passphrase = NULL;
542         bool visibility = false;
543         tethering_wifi_security_type_e security_type = TETHERING_WIFI_SECURITY_TYPE_NONE;
544
545         int error = TETHERING_ERROR_NONE;
546
547         error = tethering_wifi_ap_get_ssid(th, &ssid);
548         if (error != TETHERING_ERROR_NONE)
549                 __is_err(error);
550         else
551                 g_print("\n\t** WiFi AP SSID : %s\n", ssid);
552
553         error = tethering_wifi_ap_get_passphrase(th, &passphrase);
554         if (error != TETHERING_ERROR_NONE)
555                 __is_err(error);
556         else
557                 g_print("\t** WiFi AP passphrase : %s\n", passphrase);
558
559         error = tethering_wifi_ap_get_ssid_visibility(th, &visibility);
560         if (error != TETHERING_ERROR_NONE)
561                 __is_err(error);
562         else
563                 g_print("\t** WiFi AP ssid visibility : %s\n",
564                                 visibility ? "visible" : "invisible");
565
566         error = tethering_wifi_ap_get_security_type(th, &security_type);
567         if (error != TETHERING_ERROR_NONE)
568                 __is_err(error);
569         else
570                 g_print("\t** WiFi AP security_type : %s\n",
571                                 security_type ==
572                                 TETHERING_WIFI_SECURITY_TYPE_NONE ?
573                                 "open" : "wpa2-psk");
574
575         if (ssid)
576                 free(ssid);
577         if (passphrase)
578                 free(passphrase);
579
580         return;
581 }
582
583 bool __get_tethering_type(tethering_type_e *type)
584 {
585         int sel;
586         int ret;
587
588         printf("Select tethering type (1:Wi-Fi, 2:BT, 3:USB 4:WiFi AP, 5:ALL)\n");
589         ret = scanf("%9d", &sel);
590         if (ret < 0) {
591                 printf("scanf is failed!!\n");
592                 return false;
593         }
594
595         switch (sel) {
596         case 1:
597                 *type = TETHERING_TYPE_WIFI;
598                 break;
599         case 2:
600                 *type = TETHERING_TYPE_BT;
601                 break;
602         case 3:
603                 *type = TETHERING_TYPE_USB;
604                 break;
605         case 4:
606                 *type = TETHERING_TYPE_RESERVED;
607                 break;
608         case 5:
609                 *type = TETHERING_TYPE_ALL;
610                 break;
611         default:
612                 printf("Invalid input!!\n");
613                 return false;
614         }
615
616         return true;
617 }
618
619 static int test_tethering_create(void)
620 {
621         int ret = tethering_create(&th);
622         __tethering_cbs cbs = {
623                 __enabled_cb, __disabled_cb,
624                 __connection_state_changed_cb, __security_type_changed_cb,
625                 __ssid_visibility_changed_cb, __passphrase_changed_cb};
626
627         if (__is_err(ret) == false) __register_cbs(th, &cbs, NULL);
628         else {
629                 printf("Tethering create is failed\n");
630                 return -1;
631         }
632         printf("Tethering create and register callback success\n");
633
634         return 1;
635 }
636
637 static int test_tethering_destroy(void)
638 {
639         int ret = TETHERING_ERROR_NONE;
640
641         __deregister_cbs(th);
642
643         ret = tethering_destroy(th);
644         if (__is_err(ret) == true) {
645                 printf("Tethering destroy is failed\n");
646                 return -1;
647         }
648
649         return 1;
650 }
651
652 static int test_tethering_enable(void)
653 {
654         int ret = TETHERING_ERROR_NONE;
655         tethering_type_e type;
656
657         if (!__get_tethering_type(&type))
658                 return -1;
659
660         ret = tethering_enable(th, type);
661         if (__is_err(ret) == true) {
662                 printf("Fail to enable tethering\n");
663                 return -1;
664         }
665         return 1;
666 }
667
668 static int test_tethering_disable(void)
669 {
670         int ret = TETHERING_ERROR_NONE;
671         tethering_type_e type;
672
673         if (!__get_tethering_type(&type))
674                 return -1;
675
676         ret = tethering_disable(th, type);
677         if (__is_err(ret) == true) {
678                 printf("Fail to disable tethering\n");
679                 return -1;
680         }
681         return 1;
682 }
683
684 static int test_tethering_get_client_info(void)
685 {
686         int ret;
687         tethering_type_e type;
688
689         if (!__get_tethering_type(&type))
690                 return -1;
691
692         ret = tethering_foreach_connected_clients(th, type,
693                                         __clients_foreach_cb, NULL);
694         if (__is_err(ret) == true) {
695                 printf("Fail to disable tethering\n");
696                 return -1;
697         }
698
699         return 1;
700 }
701
702 static int test_tethering_get_interface_info(void)
703 {
704         tethering_type_e type;
705
706         if (!__get_tethering_type(&type))
707                 return -1;
708
709         __print_interface_info(th, type);
710
711         return 1;
712 }
713
714 static int test_tethering_get_data_usage(void)
715 {
716         int ret = tethering_get_data_usage(th, __data_usage_cb, NULL);
717
718         if (__is_err(ret) == true) {
719                 printf("Fail to get data usage!!\n");
720                 return -1;
721         }
722
723         return 1;
724 }
725
726 static int test_tethering_wifi_get_setting(void)
727 {
728         __print_wifi_tethering_setting(th);
729         return 1;
730 }
731
732 static int test_tethering_wifi_ap_get_setting(void)
733 {
734         __print_wifi_ap_setting(th);
735         return 1;
736 }
737
738 static int test_tethering_wifi_set_ssid(void)
739 {
740         int ret;
741         char ssid[100];
742
743         printf("Input SSID for Wi-Fi tethering: ");
744         ret = scanf("%99s", ssid);
745         if (ret < 0) {
746                 printf("scanf is failed!!\n");
747                 return -1;
748         }
749
750         ret = tethering_wifi_set_ssid(th, ssid);
751         if (__is_err(ret) == true) {
752                 printf("Fail to set wifi ssid!!\n");
753                 return -1;
754         }
755
756         return 1;
757 }
758
759 static int test_tethering_wifi_set_security_type(void)
760 {
761         int ret;
762         int security_type;
763
764         printf("Input security type for Wi-Fi tethering (0:NONE, 1:WPA2_PSK)");
765         ret = scanf("%9d", &security_type);
766         if (ret < 0) {
767                 printf("scanf is failed!!\n");
768                 return -1;
769         }
770
771         ret = tethering_wifi_set_security_type(th, security_type);
772         if (__is_err(ret) == true) {
773                 printf("Fail to set security type!!\n");
774                 return -1;
775         }
776
777         return 1;
778 }
779
780 int test_tethering_wifi_set_visibility(void)
781 {
782         int ret;
783         int visibility;
784
785         printf("Input security type for Wi-Fi tethering (0:invisible, 1:visible)");
786         ret = scanf("%9d", &visibility);
787         if (ret < 0) {
788                 printf("scanf is failed!!\n");
789                 return -1;
790         }
791
792         ret = tethering_wifi_set_ssid_visibility(th, visibility);
793         if (__is_err(ret) == true) {
794                 printf("Fail to set visibility!!\n");
795                 return -1;
796         }
797
798         return 1;
799 }
800
801 static int test_tethering_wifi_set_passphrase(void)
802 {
803         int ret;
804         char passphrase[100];
805
806         printf("Input passphrase for Wi-Fi tethering: ");
807         ret = scanf("%99s", passphrase);
808         if (ret < 0) {
809                 printf("scanf is failed!!\n");
810                 return -1;
811         }
812
813         ret = tethering_wifi_set_passphrase(th, passphrase);
814         if (__is_err(ret) == true) {
815                 printf("Fail to set passphrase!!\n");
816                 return -1;
817         }
818
819         return 1;
820 }
821
822 static int test_tethering_wifi_set_channel(void)
823 {
824         int ret;
825         int channel;
826
827         printf("Input channel for Wi-Fi tethering: ");
828         ret = scanf("%d", &channel);
829
830         ret = tethering_wifi_set_channel(th, channel);
831         if (__is_err(ret) == true) {
832                 printf("Fail to set channel!!\n");
833                 return -1;
834         }
835
836         return 1;
837 }
838
839 static int test_tethering_wifi_set_mode(void)
840 {
841         int ret;
842         int type;
843
844         printf("Input hw_mode for Wi-Fi tethering(0-b, 1-g, 2-a, 3-ad): ");
845         ret = scanf("%d", &type);
846
847         ret = tethering_wifi_set_mode(th, type);
848         if (__is_err(ret) == true) {
849                 printf("Fail to set mode!!\n");
850                 return -1;
851         }
852
853         return 1;
854 }
855
856 static int test_tethering_wifi_set_mac_filtering(void)
857 {
858         int ret;
859         int enable;
860
861         printf("Input mac filtering option (0: disable, 1: enable): ");
862         ret = scanf("%d", &enable);
863
864         ret = tethering_wifi_set_mac_filter(th, enable);
865         if (__is_err(ret) == true) {
866                 printf("Fail to set mac filtering!!\n");
867                 return -1;
868         }
869
870         return 1;
871 }
872
873 static int test_tethering_manage_mac_list(void)
874 {
875         int ret = 0;
876         int list, option;
877         char mac[100];
878
879         printf("Select MAC list to modify (0: allowed mac list, 1: blocked mac list): ");
880         ret = scanf("%d", &list);
881
882         printf("Select option (0: Add, 1: Remove): ");
883         ret = scanf("%d", &option);
884
885         printf("Input MAC Address to add/remove allowed/blocked mac list: ");
886         ret = scanf("%99s", mac);
887         if (ret < 0) {
888                 printf("scanf is failed!!\n");
889                 return -1;
890         }
891
892         if (!list && !option) {
893                 /* Add to allowed mac list*/
894                 ret = tethering_wifi_add_allowed_mac_list(th, mac);
895         } else if (!list && option) {
896                 /* Remove from allowed mac list */
897                 ret = tethering_wifi_remove_allowed_mac_list(th, mac);
898         } else if (list && !option) {
899                 /* Add to blocked mac list */
900                 ret = tethering_wifi_add_blocked_mac_list(th, mac);
901         } else if (list && option) {
902                 /* Remove from blocked mac list */
903                 ret = tethering_wifi_remove_blocked_mac_list(th, mac);
904         } else {
905                 printf("Input Failed!!\n");
906                 return -1;
907         }
908
909         if (ret < 0)
910                 return -1;
911
912         return 1;
913 }
914
915 static int test_tethering_wifi_ap_set_ssid(void)
916 {
917         int ret;
918         char ssid[100];
919
920         printf("Input SSID for Wi-Fi AP tethering: ");
921         ret = scanf("%99s", ssid);
922         if (ret < 0) {
923                 printf("scanf is failed!!\n");
924                 return -1;
925         }
926
927         ret = tethering_wifi_ap_set_ssid(th, ssid);
928         if (__is_err(ret) == true) {
929                 printf("Fail to set wifi ap ssid!!\n");
930                 return -1;
931         }
932         return 1;
933 }
934
935 static int test_tethering_wifi_ap_set_security_type(void)
936 {
937         int ret;
938         int security_type;
939
940         printf("Input security type for Wi-Fi AP tethering (0:NONE, 1:WPA2_PSK)");
941         ret = scanf("%9d", &security_type);
942         if (ret < 0) {
943                 printf("scanf is failed!!\n");
944                 return -1;
945         }
946
947         ret = tethering_wifi_ap_set_security_type(th, security_type);
948         if (__is_err(ret) == true) {
949                 printf("Fail to set security type!!\n");
950                 return -1;
951         }
952         return 1;
953 }
954
955 static int test_tethering_wifi_ap_set_visibility(void)
956 {
957         int ret;
958         int visibility;
959
960         printf("Input security type for Wi-Fi tethering (0:invisible, 1:visible)");
961         ret = scanf("%9d", &visibility);
962         if (ret < 0) {
963                 printf("scanf is failed!!\n");
964                 return -1;
965         }
966
967         ret = tethering_wifi_ap_set_ssid_visibility(th, visibility);
968         if (__is_err(ret) == true) {
969                 printf("Fail to set visibility!!\n");
970                 return -1;
971         }
972         return 1;
973 }
974
975 static int test_tethering_wifi_ap_set_passphrase(void)
976 {
977         int ret;
978         char passphrase[100];
979
980         printf("Input passphrase for Wi-Fi tethering: ");
981         ret = scanf("%99s", passphrase);
982         if (ret < 0) {
983                 printf("scanf is failed!!\n");
984                 return -1;
985         }
986
987         ret = tethering_wifi_ap_set_passphrase(th, passphrase);
988         if (__is_err(ret) == true) {
989                 printf("Fail to set passphrase!!\n");
990                 return -1;
991         }
992         return 1;
993 }
994
995 static int test_tethering_wifi_reload_settings(void)
996 {
997         int ret = tethering_wifi_reload_settings(th, __settings_reloaded_cb, NULL);
998
999         if (__is_err(ret) == true) {
1000                 printf("Fail to reload wifi tethering!!\n");
1001                 return -1;
1002         }
1003         return 1;
1004 }
1005
1006 static int test_tethering_wifi_ap_reload_settings(void)
1007 {
1008         int ret = tethering_wifi_ap_reload_settings(th, __settings_reloaded_cb, NULL);
1009
1010         if (__is_err(ret) == true) {
1011                 printf("Fail to reload wifi tethering!!\n");
1012                 return -1;
1013         }
1014         return 1;
1015 }
1016
1017 int main(int argc, char **argv)
1018 {
1019         GMainLoop *mainloop;
1020
1021 #if !GLIB_CHECK_VERSION(2, 36, 0)
1022         g_type_init();
1023 #endif
1024         mainloop = g_main_loop_new(NULL, false);
1025
1026         GIOChannel *channel = g_io_channel_unix_new(0);
1027         g_io_add_watch(channel, (G_IO_IN|G_IO_ERR|G_IO_HUP|G_IO_NVAL), test_thread, NULL);
1028         printf("Test Thread created...\n");
1029         g_main_loop_run(mainloop);
1030
1031         return 0;
1032 }
1033
1034 gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data)
1035 {
1036         int rv;
1037         char a[10];
1038
1039         printf("Event received from stdin\n");
1040
1041         rv = read(0, a, 10);
1042
1043         if (rv <= 0 || a[0] == '0')
1044                 exit(1);
1045
1046         if (a[0] == '\n' || a[0] == '\r') {
1047                 printf("\n\n Network Connection API Test App\n\n");
1048                 printf("Options..\n");
1049                 printf("1       - Tethering create and set callbacks\n");
1050                 printf("2       - Tethering destroy\n");
1051                 printf("3       - Enable Tethering\n");
1052                 printf("4       - Disable Tethering\n");
1053                 printf("5       - Get client information\n");
1054                 printf("6       - Get interface information\n");
1055                 printf("7       - Get data usage\n");
1056                 printf("8       - Get Wi-Fi tethering setting\n");
1057                 printf("9       - Get Wi-Fi AP setting\n");
1058                 printf("a       - Set Wi-Fi tethering SSID\n");
1059                 printf("b       - Set Wi-Fi tethering security type\n");
1060                 printf("c       - Set Wi-Fi tethering visibility\n");
1061                 printf("d       - Set Wi-Fi tethering passphrase\n");
1062                 printf("e       - Set Wi-Fi tethering mac filtering\n");
1063                 printf("f       - Add/Remove MAC adress to/from allowed/blocked list\n");
1064                 printf("g       - Set Wi-Fi AP SSID\n");
1065                 printf("h       - Set Wi-Fi AP security type\n");
1066                 printf("i       - Set Wi-Fi AP visibility\n");
1067                 printf("j       - Set Wi-Fi AP passphrase\n");
1068                 printf("k       - Reload Wi-Fi tethering\n");
1069                 printf("l       - Reload Wi-Fi AP\n");
1070                 printf("m       - Set Wi-Fi channel\n");
1071                 printf("n       - Set Wi-Fi hw_mode\n");
1072                 printf("0       - Exit \n");
1073                 printf("ENTER  - Show options menu.......\n");
1074         }
1075
1076         switch (a[0]) {
1077         case '1':
1078                 rv = test_tethering_create();
1079                 break;
1080         case '2':
1081                 rv = test_tethering_destroy();
1082                 break;
1083         case '3':
1084                 rv = test_tethering_enable();
1085                 break;
1086         case '4':
1087                 rv = test_tethering_disable();
1088                 break;
1089         case '5':
1090                 rv = test_tethering_get_client_info();
1091                 break;
1092         case '6':
1093                 rv = test_tethering_get_interface_info();
1094                 break;
1095         case '7':
1096                 rv = test_tethering_get_data_usage();
1097                 break;
1098         case '8':
1099                 rv = test_tethering_wifi_get_setting();
1100                 break;
1101         case '9':
1102                 rv = test_tethering_wifi_ap_get_setting();
1103                 break;
1104         case 'a':
1105                 rv = test_tethering_wifi_set_ssid();
1106                 break;
1107         case 'b':
1108                 rv = test_tethering_wifi_set_security_type();
1109                 break;
1110         case 'c':
1111                 rv = test_tethering_wifi_set_visibility();
1112                 break;
1113         case 'd':
1114                 rv = test_tethering_wifi_set_passphrase();
1115                 break;
1116         case 'e':
1117                 rv = test_tethering_wifi_set_mac_filtering();
1118                 break;
1119         case 'f':
1120                 rv = test_tethering_manage_mac_list();
1121                 break;
1122         case 'g':
1123                 rv = test_tethering_wifi_ap_set_ssid();
1124                 break;
1125         case 'h':
1126                 rv = test_tethering_wifi_ap_set_security_type();
1127                 break;
1128         case 'i':
1129                 rv = test_tethering_wifi_ap_set_visibility();
1130                 break;
1131         case 'j':
1132                 rv = test_tethering_wifi_ap_set_passphrase();
1133                 break;
1134         case 'k':
1135                 rv = test_tethering_wifi_reload_settings();
1136                 break;
1137         case 'l':
1138                 rv = test_tethering_wifi_ap_reload_settings();
1139                 break;
1140         case 'm':
1141                 rv = test_tethering_wifi_set_channel();
1142                 break;
1143         case 'n':
1144                 rv = test_tethering_wifi_set_mode();
1145                 break;
1146         }
1147
1148         if (rv == 1)
1149                 printf("Operation succeeded!\n");
1150         else
1151                 printf("Operation failed!\n");
1152
1153         return true;
1154 }