Change dbus service name for mobileap-agent and add test case for getting channel...
[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         int channel = 0;
492         tethering_wifi_security_type_e security_type = TETHERING_WIFI_SECURITY_TYPE_NONE;
493         tethering_wifi_mode_type_e hw_mode = TETHERING_WIFI_MODE_TYPE_G;
494
495         int error = TETHERING_ERROR_NONE;
496
497         error = tethering_wifi_get_ssid(th, &ssid);
498         if (error != TETHERING_ERROR_NONE)
499                 __is_err(error);
500         else
501                 g_print("\n\t** WiFi tethering SSID : %s\n", ssid);
502
503         error = tethering_wifi_get_passphrase(th, &passphrase);
504         if (error != TETHERING_ERROR_NONE)
505                 __is_err(error);
506         else
507                 g_print("\t** WiFi tethering passphrase : %s\n", passphrase);
508
509         error = tethering_wifi_get_ssid_visibility(th, &visibility);
510         if (error != TETHERING_ERROR_NONE)
511                 __is_err(error);
512         else
513                 g_print("\t** WiFi tethering ssid visibility : %s\n",
514                                 visibility ? "visible" : "invisible");
515
516         error = tethering_wifi_get_security_type(th, &security_type);
517         if (error != TETHERING_ERROR_NONE)
518                 __is_err(error);
519         else
520                 g_print("\t** WiFi tethering security_type : %s\n",
521                                 security_type ==
522                                 TETHERING_WIFI_SECURITY_TYPE_NONE ?
523                                 "open" : "wpa2-psk");
524
525         error = tethering_wifi_get_mac_filter(th, &mac_filter);
526         if (error != TETHERING_ERROR_NONE)
527                 __is_err(error);
528         else
529                 g_print("\t** WiFi tethering mac filter : %s\n",
530                                 mac_filter ? "enable" : "disable");
531
532         error = tethering_wifi_get_mode(th, &hw_mode);
533         if (error != TETHERING_ERROR_NONE)
534                 __is_err(error);
535         else
536                  g_print("\t** WiFi tethering mode : %d\n", hw_mode);
537
538         error = tethering_wifi_get_channel(th, &channel);
539         if (error != TETHERING_ERROR_NONE)
540                 __is_err(error);
541         else
542                  g_print("\t** WiFi tethering channel : %d\n", channel);
543
544         if (ssid)
545                 free(ssid);
546         if (passphrase)
547                 free(passphrase);
548
549         return;
550 }
551
552 static void __print_wifi_ap_setting(tethering_h th)
553 {
554         char *ssid = NULL;
555         char *passphrase = NULL;
556         bool visibility = false;
557         tethering_wifi_security_type_e security_type = TETHERING_WIFI_SECURITY_TYPE_NONE;
558
559         int error = TETHERING_ERROR_NONE;
560
561         error = tethering_wifi_ap_get_ssid(th, &ssid);
562         if (error != TETHERING_ERROR_NONE)
563                 __is_err(error);
564         else
565                 g_print("\n\t** WiFi AP SSID : %s\n", ssid);
566
567         error = tethering_wifi_ap_get_passphrase(th, &passphrase);
568         if (error != TETHERING_ERROR_NONE)
569                 __is_err(error);
570         else
571                 g_print("\t** WiFi AP passphrase : %s\n", passphrase);
572
573         error = tethering_wifi_ap_get_ssid_visibility(th, &visibility);
574         if (error != TETHERING_ERROR_NONE)
575                 __is_err(error);
576         else
577                 g_print("\t** WiFi AP ssid visibility : %s\n",
578                                 visibility ? "visible" : "invisible");
579
580         error = tethering_wifi_ap_get_security_type(th, &security_type);
581         if (error != TETHERING_ERROR_NONE)
582                 __is_err(error);
583         else
584                 g_print("\t** WiFi AP security_type : %s\n",
585                                 security_type ==
586                                 TETHERING_WIFI_SECURITY_TYPE_NONE ?
587                                 "open" : "wpa2-psk");
588
589         if (ssid)
590                 free(ssid);
591         if (passphrase)
592                 free(passphrase);
593
594         return;
595 }
596
597 bool __get_tethering_type(tethering_type_e *type)
598 {
599         int sel;
600         int ret;
601
602         printf("Select tethering type (1:Wi-Fi, 2:BT, 3:USB 4:WiFi AP, 5:ALL)\n");
603         ret = scanf("%9d", &sel);
604         if (ret < 0) {
605                 printf("scanf is failed!!\n");
606                 return false;
607         }
608
609         switch (sel) {
610         case 1:
611                 *type = TETHERING_TYPE_WIFI;
612                 break;
613         case 2:
614                 *type = TETHERING_TYPE_BT;
615                 break;
616         case 3:
617                 *type = TETHERING_TYPE_USB;
618                 break;
619         case 4:
620                 *type = TETHERING_TYPE_RESERVED;
621                 break;
622         case 5:
623                 *type = TETHERING_TYPE_ALL;
624                 break;
625         default:
626                 printf("Invalid input!!\n");
627                 return false;
628         }
629
630         return true;
631 }
632
633 static int test_tethering_create(void)
634 {
635         int ret = tethering_create(&th);
636         __tethering_cbs cbs = {
637                 __enabled_cb, __disabled_cb,
638                 __connection_state_changed_cb, __security_type_changed_cb,
639                 __ssid_visibility_changed_cb, __passphrase_changed_cb};
640
641         if (__is_err(ret) == false) __register_cbs(th, &cbs, NULL);
642         else {
643                 printf("Tethering create is failed\n");
644                 return -1;
645         }
646         printf("Tethering create and register callback success\n");
647
648         return 1;
649 }
650
651 static int test_tethering_destroy(void)
652 {
653         int ret = TETHERING_ERROR_NONE;
654
655         __deregister_cbs(th);
656
657         ret = tethering_destroy(th);
658         if (__is_err(ret) == true) {
659                 printf("Tethering destroy is failed\n");
660                 return -1;
661         }
662
663         return 1;
664 }
665
666 static int test_tethering_enable(void)
667 {
668         int ret = TETHERING_ERROR_NONE;
669         tethering_type_e type;
670
671         if (!__get_tethering_type(&type))
672                 return -1;
673
674         ret = tethering_enable(th, type);
675         if (__is_err(ret) == true) {
676                 printf("Fail to enable tethering\n");
677                 return -1;
678         }
679         return 1;
680 }
681
682 static int test_tethering_disable(void)
683 {
684         int ret = TETHERING_ERROR_NONE;
685         tethering_type_e type;
686
687         if (!__get_tethering_type(&type))
688                 return -1;
689
690         ret = tethering_disable(th, type);
691         if (__is_err(ret) == true) {
692                 printf("Fail to disable tethering\n");
693                 return -1;
694         }
695         return 1;
696 }
697
698 static int test_tethering_get_client_info(void)
699 {
700         int ret;
701         tethering_type_e type;
702
703         if (!__get_tethering_type(&type))
704                 return -1;
705
706         ret = tethering_foreach_connected_clients(th, type,
707                                         __clients_foreach_cb, NULL);
708         if (__is_err(ret) == true) {
709                 printf("Fail to disable tethering\n");
710                 return -1;
711         }
712
713         return 1;
714 }
715
716 static int test_tethering_get_interface_info(void)
717 {
718         tethering_type_e type;
719
720         if (!__get_tethering_type(&type))
721                 return -1;
722
723         __print_interface_info(th, type);
724
725         return 1;
726 }
727
728 static int test_tethering_get_data_usage(void)
729 {
730         int ret = tethering_get_data_usage(th, __data_usage_cb, NULL);
731
732         if (__is_err(ret) == true) {
733                 printf("Fail to get data usage!!\n");
734                 return -1;
735         }
736
737         return 1;
738 }
739
740 static int test_tethering_wifi_get_setting(void)
741 {
742         __print_wifi_tethering_setting(th);
743         return 1;
744 }
745
746 static int test_tethering_wifi_ap_get_setting(void)
747 {
748         __print_wifi_ap_setting(th);
749         return 1;
750 }
751
752 static int test_tethering_wifi_set_ssid(void)
753 {
754         int ret;
755         char ssid[100];
756
757         printf("Input SSID for Wi-Fi tethering: ");
758         ret = scanf("%99s", ssid);
759         if (ret < 0) {
760                 printf("scanf is failed!!\n");
761                 return -1;
762         }
763
764         ret = tethering_wifi_set_ssid(th, ssid);
765         if (__is_err(ret) == true) {
766                 printf("Fail to set wifi ssid!!\n");
767                 return -1;
768         }
769
770         return 1;
771 }
772
773 static int test_tethering_wifi_set_security_type(void)
774 {
775         int ret;
776         int security_type;
777
778         printf("Input security type for Wi-Fi tethering (0:NONE, 1:WPA2_PSK)");
779         ret = scanf("%9d", &security_type);
780         if (ret < 0) {
781                 printf("scanf is failed!!\n");
782                 return -1;
783         }
784
785         ret = tethering_wifi_set_security_type(th, security_type);
786         if (__is_err(ret) == true) {
787                 printf("Fail to set security type!!\n");
788                 return -1;
789         }
790
791         return 1;
792 }
793
794 int test_tethering_wifi_set_visibility(void)
795 {
796         int ret;
797         int visibility;
798
799         printf("Input security type for Wi-Fi tethering (0:invisible, 1:visible)");
800         ret = scanf("%9d", &visibility);
801         if (ret < 0) {
802                 printf("scanf is failed!!\n");
803                 return -1;
804         }
805
806         ret = tethering_wifi_set_ssid_visibility(th, visibility);
807         if (__is_err(ret) == true) {
808                 printf("Fail to set visibility!!\n");
809                 return -1;
810         }
811
812         return 1;
813 }
814
815 static int test_tethering_wifi_set_passphrase(void)
816 {
817         int ret;
818         char passphrase[100];
819
820         printf("Input passphrase for Wi-Fi tethering: ");
821         ret = scanf("%99s", passphrase);
822         if (ret < 0) {
823                 printf("scanf is failed!!\n");
824                 return -1;
825         }
826
827         ret = tethering_wifi_set_passphrase(th, passphrase);
828         if (__is_err(ret) == true) {
829                 printf("Fail to set passphrase!!\n");
830                 return -1;
831         }
832
833         return 1;
834 }
835
836 static int test_tethering_wifi_set_channel(void)
837 {
838         int ret;
839         int channel;
840
841         printf("Input channel for Wi-Fi tethering: ");
842         ret = scanf("%d", &channel);
843
844         ret = tethering_wifi_set_channel(th, channel);
845         if (__is_err(ret) == true) {
846                 printf("Fail to set channel!!\n");
847                 return -1;
848         }
849
850         return 1;
851 }
852
853 static int test_tethering_wifi_set_mode(void)
854 {
855         int ret;
856         int type;
857
858         printf("Input hw_mode for Wi-Fi tethering(0-b, 1-g, 2-a, 3-ad): ");
859         ret = scanf("%d", &type);
860
861         ret = tethering_wifi_set_mode(th, type);
862         if (__is_err(ret) == true) {
863                 printf("Fail to set mode!!\n");
864                 return -1;
865         }
866
867         return 1;
868 }
869
870 static int test_tethering_wifi_enable_dhcp(void)
871 {
872         int ret;
873         int enable;
874
875         printf("Input (0-Disable, 1-Enable): ");
876         ret = scanf("%d", &enable);
877
878         ret = tethering_wifi_enable_dhcp(th, enable);
879         if (__is_err(ret) == true) {
880                 printf("Fail to enable dhcp server!!\n");
881                 return -1;
882         }
883
884         return 1;
885 }
886
887 static int test_tethering_wifi_set_dhcp_range(void)
888 {
889         int ret;
890         char rangestart[16], rangestop[16];
891
892         printf("Input range (ex: 192.168.0.50 192.168.0.150): ");
893
894         ret = scanf("%s %s", rangestart, rangestop);
895
896         ret = tethering_wifi_set_dhcp_range(th, rangestart, rangestop);
897         if (__is_err(ret) == true) {
898                 printf("Fail to set dhcp range and enable dhcp server!!\n");
899                 return -1;
900         }
901
902         return 1;
903 }
904
905 static int test_tethering_wifi_is_dhcp_enabled(void)
906 {
907         int ret;
908         bool enabled;
909
910         ret = tethering_wifi_is_dhcp_enabled(th, &enabled);
911
912         if (__is_err(ret) == true) {
913                 printf("Fail to get dhcp server status!!\n");
914                 return -1;
915         }
916         else {
917                 printf("DHCP server is %s\n", enabled? "enabled": "disabled");
918         }
919
920         return 1;
921 }
922
923 static int test_tethering_wifi_set_mac_filtering(void)
924 {
925         int ret;
926         int enable;
927
928         printf("Input mac filtering option (0: disable, 1: enable): ");
929         ret = scanf("%d", &enable);
930
931         ret = tethering_wifi_set_mac_filter(th, enable);
932         if (__is_err(ret) == true) {
933                 printf("Fail to set mac filtering!!\n");
934                 return -1;
935         }
936
937         return 1;
938 }
939
940 static int test_tethering_manage_mac_list(void)
941 {
942         int ret = 0;
943         int list, option;
944         char mac[100];
945
946         printf("Select MAC list to modify (0: allowed mac list, 1: blocked mac list): ");
947         ret = scanf("%d", &list);
948
949         printf("Select option (0: Add, 1: Remove): ");
950         ret = scanf("%d", &option);
951
952         printf("Input MAC Address to add/remove allowed/blocked mac list: ");
953         ret = scanf("%99s", mac);
954         if (ret < 0) {
955                 printf("scanf is failed!!\n");
956                 return -1;
957         }
958
959         if (!list && !option) {
960                 /* Add to allowed mac list*/
961                 ret = tethering_wifi_add_allowed_mac_list(th, mac);
962         } else if (!list && option) {
963                 /* Remove from allowed mac list */
964                 ret = tethering_wifi_remove_allowed_mac_list(th, mac);
965         } else if (list && !option) {
966                 /* Add to blocked mac list */
967                 ret = tethering_wifi_add_blocked_mac_list(th, mac);
968         } else if (list && option) {
969                 /* Remove from blocked mac list */
970                 ret = tethering_wifi_remove_blocked_mac_list(th, mac);
971         } else {
972                 printf("Input Failed!!\n");
973                 return -1;
974         }
975
976         if (ret < 0)
977                 return -1;
978
979         return 1;
980 }
981
982 static int test_tethering_wifi_ap_set_ssid(void)
983 {
984         int ret;
985         char ssid[100];
986
987         printf("Input SSID for Wi-Fi AP tethering: ");
988         ret = scanf("%99s", ssid);
989         if (ret < 0) {
990                 printf("scanf is failed!!\n");
991                 return -1;
992         }
993
994         ret = tethering_wifi_ap_set_ssid(th, ssid);
995         if (__is_err(ret) == true) {
996                 printf("Fail to set wifi ap ssid!!\n");
997                 return -1;
998         }
999         return 1;
1000 }
1001
1002 static int test_tethering_wifi_ap_set_security_type(void)
1003 {
1004         int ret;
1005         int security_type;
1006
1007         printf("Input security type for Wi-Fi AP tethering (0:NONE, 1:WPA2_PSK)");
1008         ret = scanf("%9d", &security_type);
1009         if (ret < 0) {
1010                 printf("scanf is failed!!\n");
1011                 return -1;
1012         }
1013
1014         ret = tethering_wifi_ap_set_security_type(th, security_type);
1015         if (__is_err(ret) == true) {
1016                 printf("Fail to set security type!!\n");
1017                 return -1;
1018         }
1019         return 1;
1020 }
1021
1022 static int test_tethering_wifi_ap_set_visibility(void)
1023 {
1024         int ret;
1025         int visibility;
1026
1027         printf("Input security type for Wi-Fi tethering (0:invisible, 1:visible)");
1028         ret = scanf("%9d", &visibility);
1029         if (ret < 0) {
1030                 printf("scanf is failed!!\n");
1031                 return -1;
1032         }
1033
1034         ret = tethering_wifi_ap_set_ssid_visibility(th, visibility);
1035         if (__is_err(ret) == true) {
1036                 printf("Fail to set visibility!!\n");
1037                 return -1;
1038         }
1039         return 1;
1040 }
1041
1042 static int test_tethering_wifi_ap_set_passphrase(void)
1043 {
1044         int ret;
1045         char passphrase[100];
1046
1047         printf("Input passphrase for Wi-Fi tethering: ");
1048         ret = scanf("%99s", passphrase);
1049         if (ret < 0) {
1050                 printf("scanf is failed!!\n");
1051                 return -1;
1052         }
1053
1054         ret = tethering_wifi_ap_set_passphrase(th, passphrase);
1055         if (__is_err(ret) == true) {
1056                 printf("Fail to set passphrase!!\n");
1057                 return -1;
1058         }
1059         return 1;
1060 }
1061
1062 static int test_tethering_wifi_reload_settings(void)
1063 {
1064         int ret = tethering_wifi_reload_settings(th, __settings_reloaded_cb, NULL);
1065
1066         if (__is_err(ret) == true) {
1067                 printf("Fail to reload wifi tethering!!\n");
1068                 return -1;
1069         }
1070         return 1;
1071 }
1072
1073 static int test_tethering_wifi_ap_reload_settings(void)
1074 {
1075         int ret = tethering_wifi_ap_reload_settings(th, __settings_reloaded_cb, NULL);
1076
1077         if (__is_err(ret) == true) {
1078                 printf("Fail to reload wifi tethering!!\n");
1079                 return -1;
1080         }
1081         return 1;
1082 }
1083
1084 int main(int argc, char **argv)
1085 {
1086         GMainLoop *mainloop;
1087
1088 #if !GLIB_CHECK_VERSION(2, 36, 0)
1089         g_type_init();
1090 #endif
1091         mainloop = g_main_loop_new(NULL, false);
1092
1093         GIOChannel *channel = g_io_channel_unix_new(0);
1094         g_io_add_watch(channel, (G_IO_IN|G_IO_ERR|G_IO_HUP|G_IO_NVAL), test_thread, NULL);
1095         printf("Test Thread created...\n");
1096         g_main_loop_run(mainloop);
1097
1098         return 0;
1099 }
1100
1101 gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data)
1102 {
1103         int rv;
1104         char a[10];
1105
1106         printf("Event received from stdin\n");
1107
1108         rv = read(0, a, 10);
1109
1110         if (rv <= 0 || a[0] == '0')
1111                 exit(1);
1112
1113         if (a[0] == '\n' || a[0] == '\r') {
1114                 printf("\n\n Network Connection API Test App\n\n");
1115                 printf("Options..\n");
1116                 printf("1       - Tethering create and set callbacks\n");
1117                 printf("2       - Tethering destroy\n");
1118                 printf("3       - Enable Tethering\n");
1119                 printf("4       - Disable Tethering\n");
1120                 printf("5       - Get client information\n");
1121                 printf("6       - Get interface information\n");
1122                 printf("7       - Get data usage\n");
1123                 printf("8       - Get Wi-Fi tethering setting\n");
1124                 printf("9       - Get Wi-Fi AP setting\n");
1125                 printf("a       - Set Wi-Fi tethering SSID\n");
1126                 printf("b       - Set Wi-Fi tethering security type\n");
1127                 printf("c       - Set Wi-Fi tethering visibility\n");
1128                 printf("d       - Set Wi-Fi tethering passphrase\n");
1129                 printf("e       - Set Wi-Fi tethering mac filtering\n");
1130                 printf("f       - Add/Remove MAC adress to/from allowed/blocked list\n");
1131                 printf("g       - Set Wi-Fi AP SSID\n");
1132                 printf("h       - Set Wi-Fi AP security type\n");
1133                 printf("i       - Set Wi-Fi AP visibility\n");
1134                 printf("j       - Set Wi-Fi AP passphrase\n");
1135                 printf("k       - Reload Wi-Fi tethering\n");
1136                 printf("l       - Reload Wi-Fi AP\n");
1137                 printf("m       - Set Wi-Fi channel\n");
1138                 printf("n       - Set Wi-Fi hw_mode\n");
1139                 printf("o       - Enable dhcp server\n");
1140                 printf("p       - Enable dhcp server with range\n");
1141                 printf("q       - Is dhcp server enabled?\n");
1142                 printf("0       - Exit \n");
1143                 printf("ENTER  - Show options menu.......\n");
1144         }
1145
1146         switch (a[0]) {
1147         case '1':
1148                 rv = test_tethering_create();
1149                 break;
1150         case '2':
1151                 rv = test_tethering_destroy();
1152                 break;
1153         case '3':
1154                 rv = test_tethering_enable();
1155                 break;
1156         case '4':
1157                 rv = test_tethering_disable();
1158                 break;
1159         case '5':
1160                 rv = test_tethering_get_client_info();
1161                 break;
1162         case '6':
1163                 rv = test_tethering_get_interface_info();
1164                 break;
1165         case '7':
1166                 rv = test_tethering_get_data_usage();
1167                 break;
1168         case '8':
1169                 rv = test_tethering_wifi_get_setting();
1170                 break;
1171         case '9':
1172                 rv = test_tethering_wifi_ap_get_setting();
1173                 break;
1174         case 'a':
1175                 rv = test_tethering_wifi_set_ssid();
1176                 break;
1177         case 'b':
1178                 rv = test_tethering_wifi_set_security_type();
1179                 break;
1180         case 'c':
1181                 rv = test_tethering_wifi_set_visibility();
1182                 break;
1183         case 'd':
1184                 rv = test_tethering_wifi_set_passphrase();
1185                 break;
1186         case 'e':
1187                 rv = test_tethering_wifi_set_mac_filtering();
1188                 break;
1189         case 'f':
1190                 rv = test_tethering_manage_mac_list();
1191                 break;
1192         case 'g':
1193                 rv = test_tethering_wifi_ap_set_ssid();
1194                 break;
1195         case 'h':
1196                 rv = test_tethering_wifi_ap_set_security_type();
1197                 break;
1198         case 'i':
1199                 rv = test_tethering_wifi_ap_set_visibility();
1200                 break;
1201         case 'j':
1202                 rv = test_tethering_wifi_ap_set_passphrase();
1203                 break;
1204         case 'k':
1205                 rv = test_tethering_wifi_reload_settings();
1206                 break;
1207         case 'l':
1208                 rv = test_tethering_wifi_ap_reload_settings();
1209                 break;
1210         case 'm':
1211                 rv = test_tethering_wifi_set_channel();
1212                 break;
1213         case 'n':
1214                 rv = test_tethering_wifi_set_mode();
1215                 break;
1216         case 'o':
1217                 rv = test_tethering_wifi_enable_dhcp();
1218                 break;
1219         case 'p':
1220                 rv = test_tethering_wifi_set_dhcp_range();
1221                 break;
1222         case 'q':
1223                 rv = test_tethering_wifi_is_dhcp_enabled();
1224                 break;
1225         }
1226
1227         if (rv == 1)
1228                 printf("Operation succeeded!\n");
1229         else
1230                 printf("Operation failed!\n");
1231
1232         return true;
1233 }