Modified tethering test file
[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
173         ret = tethering_set_enabled_cb(th, TETHERING_TYPE_RESERVED,
174                         cbs->enabled_cb, user_data);
175         if (__is_err(ret) == true) {
176                 g_print("tethering_set_enabled_cb is failed\n");
177         }
178
179         ret = tethering_set_disabled_cb(th, TETHERING_TYPE_ALL,
180                         cbs->disabled_cb, user_data);
181         if (__is_err(ret) == true) {
182                 g_print("tethering_set_disabled_cb is failed\n");
183         }
184
185         ret = tethering_set_disabled_cb(th, TETHERING_TYPE_RESERVED,
186                         cbs->disabled_cb, user_data);
187         if (__is_err(ret) == true) {
188                 g_print("tethering_set_disabled_cb is failed\n");
189         }
190
191         ret = tethering_set_connection_state_changed_cb(th, TETHERING_TYPE_ALL,
192                         cbs->changed_cb, user_data);
193         if (__is_err(ret) == true) {
194                 g_print("tethering_set_connection_state_changed_cb is failed\n");
195         }
196
197         ret = tethering_set_connection_state_changed_cb(th, TETHERING_TYPE_RESERVED,
198                         cbs->changed_cb, user_data);
199         if (__is_err(ret) == true) {
200                 g_print("tethering_set_connection_state_changed_cb is failed\n");
201         }
202
203         ret = tethering_wifi_set_security_type_changed_cb(th,
204                         cbs->security_type_changed_cb, user_data);
205         if (__is_err(ret) == true) {
206                 g_print("tethering_wifi_set_security_type_changed_cb is failed\n");
207         }
208
209         ret = tethering_wifi_set_ssid_visibility_changed_cb(th,
210                         cbs->ssid_visibility_changed_cb, user_data);
211         if (__is_err(ret) == true) {
212                 g_print("tethering_wifi_set_ssid_visibility_changed_cb is failed\n");
213         }
214
215         ret = tethering_wifi_set_passphrase_changed_cb(th,
216                         cbs->passphrase_changed_cb, user_data);
217         if (__is_err(ret) == true) {
218                 g_print("tethering_wifi_set_passphrase_changed_cb is failed\n");
219         }
220
221         return;
222 }
223
224 static void __deregister_cbs(tethering_h th)
225 {
226         tethering_error_e ret = TETHERING_ERROR_NONE;
227
228         ret = tethering_unset_enabled_cb(th, TETHERING_TYPE_ALL);
229         if (__is_err(ret) == true) {
230                 g_print("tethering_unset_enabled_cb is failed\n");
231         }
232
233         ret = tethering_unset_enabled_cb(th, TETHERING_TYPE_RESERVED);
234         if (__is_err(ret) == true) {
235                 g_print("tethering_unset_enabled_cb is failed\n");
236         }
237
238         ret = tethering_unset_disabled_cb(th, TETHERING_TYPE_ALL);
239         if (__is_err(ret) == true) {
240                 g_print("tethering_unset_disabled_cb is failed\n");
241         }
242
243         ret = tethering_unset_disabled_cb(th, TETHERING_TYPE_RESERVED);
244         if (__is_err(ret) == true) {
245                 g_print("tethering_unset_disabled_cb is failed\n");
246         }
247
248         ret = tethering_unset_connection_state_changed_cb(th, TETHERING_TYPE_ALL);
249         if (__is_err(ret) == true) {
250                 g_print("tethering_unset_connection_state_changed_cb is failed\n");
251         }
252
253         ret = tethering_unset_connection_state_changed_cb(th, TETHERING_TYPE_RESERVED);
254         if (__is_err(ret) == true) {
255                 g_print("tethering_unset_connection_state_changed_cb is failed\n");
256         }
257
258         ret = tethering_wifi_unset_security_type_changed_cb(th);
259         if (__is_err(ret) == true) {
260                 g_print("tethering_wifi_unset_security_type_changed_cb is failed\n");
261         }
262
263         ret = tethering_wifi_unset_ssid_visibility_changed_cb(th);
264         if (__is_err(ret) == true) {
265                 g_print("tethering_wifi_unset_ssid_visibility_changed_cb is failed\n");
266         }
267
268         ret = tethering_wifi_unset_passphrase_changed_cb(th);
269         if (__is_err(ret) == true) {
270                 g_print("tethering_wifi_unset_passphrase_changed_cb is failed\n");
271         }
272
273         return;
274 }
275
276 /* Tethering callbacks */
277 static void __enabled_cb(tethering_error_e error, tethering_type_e type, bool is_requested, void *data)
278 {
279         if (__is_err(error)) {
280                 if (!is_requested) {
281                         return;
282                 }
283
284                 g_print("## %s is not enabled. error code[0x%X]\n",
285                                 __convert_tethering_type_to_str(type),
286                                 error);
287                 return;
288         }
289
290         if (is_requested)
291                 g_print("## %s is enabled successfully\n",
292                                 __convert_tethering_type_to_str(type));
293         else
294                 g_print("## %s is enabled by other app\n",
295                                 __convert_tethering_type_to_str(type));
296
297         return;
298 }
299
300 static void __disabled_cb(tethering_error_e error, tethering_type_e type, tethering_disabled_cause_e code, void *data)
301 {
302         if (__is_err(error)) {
303                 if (code != TETHERING_DISABLED_BY_REQUEST) {
304                         return;
305                 }
306
307                 g_print("## %s is not disabled. error code[0x%X]\n",
308                                 __convert_tethering_type_to_str(type), error);
309                 return;
310         }
311
312         g_print("## %s is %s\n",
313                         __convert_tethering_type_to_str(type),
314                         __convert_disabled_code_to_str(code));
315
316         return;
317 }
318
319 static void __connection_state_changed_cb(tethering_client_h client, bool open, void *data)
320 {
321         tethering_client_h clone = NULL;
322         tethering_type_e type;
323         char *ip_address = NULL;
324         char *mac_address = NULL;
325         char *hostname = NULL;
326
327         tethering_client_clone(&clone, client);
328         if (clone == NULL) {
329                 g_print("tetheirng_client_clone is failed\n");
330                 return;
331         }
332
333         tethering_client_get_tethering_type(clone, &type);
334         tethering_client_get_ip_address(clone,
335                         TETHERING_ADDRESS_FAMILY_IPV4, &ip_address);
336         tethering_client_get_mac_address(clone, &mac_address);
337         tethering_client_get_name(clone, &hostname);
338
339         if (open) {
340                 g_print("## New station Type [%s], IP [%s], MAC [%s], hostname [%s]\n",
341                                 __convert_tethering_type_to_str(type),
342                                 ip_address, mac_address, hostname);
343         } else {
344                 g_print("## Disconnected station Type [%s], IP [%s], MAC [%s], hostname [%s]\n",
345                                 __convert_tethering_type_to_str(type),
346                                 ip_address, mac_address, hostname);
347         }
348
349         if (ip_address)
350                 free(ip_address);
351         if (mac_address)
352                 free(mac_address);
353         if (hostname)
354                 free(hostname);
355
356         tethering_client_destroy(clone);
357
358         return;
359 }
360
361 static void __data_usage_cb(tethering_error_e result, unsigned long long received_data,
362                 unsigned long long sent_data, void *user_data)
363 {
364         g_print("__data_usage_cb\n");
365
366         if (result != TETHERING_ERROR_NONE) {
367                 g_print("tethering_get_data_usage is failed. error[0x%X]\n", result);
368                 return;
369         }
370
371         g_print("## Received data : %llu bytes\n", received_data);
372         g_print("## Sent data : %llu bytes\n", sent_data);
373
374         return;
375 }
376
377 static void __settings_reloaded_cb(tethering_error_e result, void *user_data)
378 {
379         g_print("__settings_reloaded_cb\n");
380
381         if (result != TETHERING_ERROR_NONE) {
382                 g_print("tethering_wifi_reload_settings is failed. error[0x%X]\n", result);
383                 return;
384         }
385
386         g_print("## Wi-Fi tethering setting is reloaded\n");
387
388         return;
389 }
390
391 static bool __clients_foreach_cb(tethering_client_h client, void *data)
392 {
393         tethering_client_h clone = NULL;
394         tethering_type_e type;
395         char *ip_address = NULL;
396         char *mac_address = NULL;
397         char *hostname = NULL;
398
399         /* Clone internal information */
400         if (tethering_client_clone(&clone, client) != TETHERING_ERROR_NONE) {
401                 g_print("tethering_client_clone is failed\n");
402                 return false;
403         }
404
405         /* Get information */
406         if (tethering_client_get_tethering_type(clone, &type) != TETHERING_ERROR_NONE) {
407                 g_print("tethering_client_get_type is failed\n");
408         }
409
410         if (tethering_client_get_ip_address(clone, TETHERING_ADDRESS_FAMILY_IPV4, &ip_address) != TETHERING_ERROR_NONE) {
411                 g_print("tethering_client_get_ip_address is failed\n");
412         }
413
414         if (tethering_client_get_mac_address(clone, &mac_address) != TETHERING_ERROR_NONE) {
415                 g_print("tethering_client_get_mac_address is failed\n");
416         }
417
418         if (tethering_client_get_name(clone, &hostname) != TETHERING_ERROR_NONE) {
419                 g_print("tethering_client_get_hostname is failed\n");
420         }
421         /* End of getting information */
422
423         g_print("\n< Client Info. >\n");
424         g_print("\tType %s\n", __convert_tethering_type_to_str(type));
425         g_print("\tIP Address %s\n", ip_address);
426         g_print("\tMAC Address : %s\n", mac_address);
427         g_print("\tHostname : %s\n", hostname);
428
429         /* Destroy cloned objects */
430         if (ip_address)
431                 free(ip_address);
432         if (mac_address)
433                 free(mac_address);
434         if (hostname)
435                 free(hostname);
436
437         tethering_client_destroy(clone);
438
439         /* Continue iteration */
440         return true;
441 }
442
443 static void __security_type_changed_cb(tethering_wifi_security_type_e changed_type, void *user_data)
444 {
445         g_print("Wi-Fi Tethering Security type is changed to [%s]\n",
446                         changed_type == TETHERING_WIFI_SECURITY_TYPE_NONE ?
447                         "open" : "wpa2-psk");
448         return;
449 }
450
451 static void __ssid_visibility_changed_cb(bool changed_visible, void *user_data)
452 {
453         g_print("SSID visibility for Wi-Fi tethering changed to [%s]\n",
454                         changed_visible ? "visible" : "invisible");
455         return;
456 }
457
458 static void __passphrase_changed_cb(void *user_data)
459 {
460         g_print("Wi-Fi Tethering passphrase is changed\n");
461         return;
462 }
463 /* End of tethering callbacks */
464
465 static void __print_interface_info(tethering_h th, tethering_type_e type)
466 {
467         char *interface = NULL;
468         char *mac_address = NULL;
469         char *ip_address = NULL;
470         char *gateway_address = NULL;
471         char *subnet_mask = NULL;
472
473         if (tethering_is_enabled(th, type) == FALSE) {
474                 g_print("%s is not enabled\n",
475                                 __convert_tethering_type_to_str(type));
476                 return;
477         }
478
479         tethering_get_network_interface_name(th, type, &interface);
480         tethering_get_mac_address(th, type, &mac_address);
481         tethering_get_ip_address(th, type, TETHERING_ADDRESS_FAMILY_IPV4,
482                         &ip_address);
483         tethering_get_gateway_address(th, type, TETHERING_ADDRESS_FAMILY_IPV4,
484                         &gateway_address);
485         tethering_get_subnet_mask(th, type, TETHERING_ADDRESS_FAMILY_IPV4,
486                         &subnet_mask);
487
488         g_print("interface name : %s\n", interface);
489         g_print("mac address : %s\n", mac_address);
490         g_print("ip address : %s\n", ip_address);
491         g_print("gateway address: %s\n", gateway_address);
492         g_print("subnet mask : %s\n", subnet_mask);
493
494         if (interface)
495                 free(interface);
496         if (mac_address)
497                 free(mac_address);
498         if (ip_address)
499                 free(ip_address);
500         if (gateway_address)
501                 free(gateway_address);
502         if (subnet_mask)
503                 free(subnet_mask);
504
505         return;
506 }
507
508 static void __print_wifi_tethering_setting(tethering_h th)
509 {
510         char *ssid = NULL;
511         char *passphrase = NULL;
512         bool visibility = false;
513         tethering_wifi_security_type_e security_type = TETHERING_WIFI_SECURITY_TYPE_NONE;
514
515         int error = TETHERING_ERROR_NONE;
516
517         error = tethering_wifi_get_ssid(th, &ssid);
518         if (error != TETHERING_ERROR_NONE)
519                 __is_err(error);
520         else
521                 g_print("\n\t** WiFi tethering SSID : %s\n", ssid);
522
523         error = tethering_wifi_get_passphrase(th, &passphrase);
524         if (error != TETHERING_ERROR_NONE)
525                 __is_err(error);
526         else
527                 g_print("\t** WiFi tethering passphrase : %s\n", passphrase);
528
529         error = tethering_wifi_get_ssid_visibility(th, &visibility);
530         if (error != TETHERING_ERROR_NONE)
531                 __is_err(error);
532         else
533                 g_print("\t** WiFi tethering ssid visibility : %s\n",
534                                 visibility ? "visible" : "invisible");
535
536         error = tethering_wifi_get_security_type(th, &security_type);
537         if (error != TETHERING_ERROR_NONE)
538                 __is_err(error);
539         else
540                 g_print("\t** WiFi tethering security_type : %s\n",
541                                 security_type ==
542                                 TETHERING_WIFI_SECURITY_TYPE_NONE ?
543                                 "open" : "wpa2-psk");
544
545         if (ssid)
546                 free(ssid);
547         if (passphrase)
548                 free(passphrase);
549
550         return;
551 }
552
553 static void __print_wifi_ap_setting(tethering_h th)
554 {
555         char *ssid = NULL;
556         char *passphrase = NULL;
557         bool visibility = false;
558         tethering_wifi_security_type_e security_type = TETHERING_WIFI_SECURITY_TYPE_NONE;
559
560         int error = TETHERING_ERROR_NONE;
561
562         error = tethering_wifi_ap_get_ssid(th, &ssid);
563         if (error != TETHERING_ERROR_NONE)
564                 __is_err(error);
565         else
566                 g_print("\n\t** WiFi AP SSID : %s\n", ssid);
567
568         error = tethering_wifi_ap_get_passphrase(th, &passphrase);
569         if (error != TETHERING_ERROR_NONE)
570                 __is_err(error);
571         else
572                 g_print("\t** WiFi AP passphrase : %s\n", passphrase);
573
574         error = tethering_wifi_ap_get_ssid_visibility(th, &visibility);
575         if (error != TETHERING_ERROR_NONE)
576                 __is_err(error);
577         else
578                 g_print("\t** WiFi AP ssid visibility : %s\n",
579                                 visibility ? "visible" : "invisible");
580
581         error = tethering_wifi_ap_get_security_type(th, &security_type);
582         if (error != TETHERING_ERROR_NONE)
583                 __is_err(error);
584         else
585                 g_print("\t** WiFi AP security_type : %s\n",
586                                 security_type ==
587                                 TETHERING_WIFI_SECURITY_TYPE_NONE ?
588                                 "open" : "wpa2-psk");
589
590         if (ssid)
591                 free(ssid);
592         if (passphrase)
593                 free(passphrase);
594
595         return;
596 }
597
598 bool __get_tethering_type(tethering_type_e *type)
599 {
600         int sel;
601         int ret;
602
603         printf("Select tethering type (1:Wi-Fi, 2:BT, 3:USB 4:WiFi AP, 5:ALL)\n");
604         ret = scanf("%9d", &sel);
605         if (ret < 0) {
606                 printf("scanf is failed!!\n");
607                 return false;
608         }
609
610         switch (sel) {
611                 case 1:
612                         *type = TETHERING_TYPE_WIFI;
613                         break;
614                 case 2:
615                         *type = TETHERING_TYPE_BT;
616                         break;
617                 case 3:
618                         *type = TETHERING_TYPE_USB;
619                         break;
620                 case 4:
621                         *type = TETHERING_TYPE_RESERVED;
622                         break;
623                 case 5:
624                         *type = TETHERING_TYPE_ALL;
625                         break;
626                default:
627                         printf("Invalid input!!\n");
628                         return false;
629         }
630
631         return true;
632 }
633
634 static int test_tethering_create(void)
635 {
636         int ret = tethering_create(&th);
637         __tethering_cbs cbs = {
638                 __enabled_cb, __disabled_cb,
639                 __connection_state_changed_cb, __security_type_changed_cb,
640                 __ssid_visibility_changed_cb, __passphrase_changed_cb};
641
642         if (__is_err(ret) == false) __register_cbs(th, &cbs, NULL);
643         else {
644                 printf("Tethering create is failed\n");
645                 return -1;
646         }
647         printf("Tethering create and register callback success\n");
648
649         return 1;
650 }
651
652 static int test_tethering_destroy(void)
653 {
654         int ret = TETHERING_ERROR_NONE;
655
656         __deregister_cbs(th);
657
658         ret = tethering_destroy(th);
659         if (__is_err(ret) == true)
660         {
661                 printf("Tethering destroy is failed\n");
662                 return -1;
663         }
664
665         return 1;
666 }
667
668 static int test_tethering_enable(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_enable(th, type);
677         if (__is_err(ret) == true) {
678                 printf("Fail to enable tethering\n");
679                 return -1;
680         }
681         return 1;
682 }
683
684 static int test_tethering_disable(void)
685 {
686         int ret = TETHERING_ERROR_NONE;
687         tethering_type_e type;
688
689         if (!__get_tethering_type(&type))
690                 return -1;
691
692         ret = tethering_disable(th, type);
693         if (__is_err(ret) == true) {
694                 printf("Fail to disable tethering\n");
695                 return -1;
696         }
697         return 1;
698 }
699
700 static int test_tethering_get_client_info(void)
701 {
702         int ret;
703         tethering_type_e type;
704
705         if (!__get_tethering_type(&type))
706                 return -1;
707
708         ret = tethering_foreach_connected_clients(th, type,
709                                         __clients_foreach_cb, NULL);
710         if (__is_err(ret) == true) {
711                 printf("Fail to disable tethering\n");
712                 return -1;
713         }
714
715         return 1;
716 }
717
718 static int test_tethering_get_interface_info(void)
719 {
720         tethering_type_e type;
721
722         if (!__get_tethering_type(&type))
723                 return -1;
724
725         __print_interface_info(th, type);
726
727         return 1;
728 }
729
730 static int test_tethering_get_data_usage(void)
731 {
732         int ret = tethering_get_data_usage(th, __data_usage_cb, NULL);
733
734         if (__is_err(ret) == true) {
735                 printf("Fail to get data usage!!\n");
736                 return -1;
737         }
738
739         return 1;
740 }
741
742 static int test_tethering_wifi_get_setting(void)
743 {
744         __print_wifi_tethering_setting(th);
745         return 1;
746 }
747
748 static int test_tethering_wifi_ap_get_setting(void)
749 {
750         __print_wifi_ap_setting(th);
751         return 1;
752 }
753
754 static int test_tethering_wifi_set_ssid(void)
755 {
756         int ret;
757         char ssid[100];
758
759         printf("Input SSID for Wi-Fi tethering: ");
760         ret = scanf("%99s", ssid);
761         if (ret < 0) {
762                 printf("scanf is failed!!\n");
763                 return -1;
764         }
765
766         ret = tethering_wifi_set_ssid(th, ssid);
767         if (__is_err(ret) == true) {
768                 printf("Fail to set wifi ssid!!\n");
769                 return -1;
770         }
771
772         return 1;
773 }
774
775 static int test_tethering_wifi_set_security_type(void)
776 {
777         int ret;
778         int security_type;
779
780         printf("Input security type for Wi-Fi tethering (0:NONE, 1:WPA2_PSK)");
781         ret = scanf("%9d", &security_type);
782         if (ret < 0) {
783                 printf("scanf is failed!!\n");
784                 return -1;
785         }
786
787         ret = tethering_wifi_set_security_type(th, security_type);
788         if (__is_err(ret) == true) {
789                 printf("Fail to set security type!!\n");
790                 return -1;
791         }
792
793         return 1;
794 }
795
796 int test_tethering_wifi_set_visibility(void)
797 {
798         int ret;
799         int visibility;
800
801         printf("Input security type for Wi-Fi tethering (0:invisible, 1:visible)");
802         ret = scanf("%9d", &visibility);
803         if (ret < 0) {
804                 printf("scanf is failed!!\n");
805                 return -1;
806         }
807
808         ret = tethering_wifi_set_ssid_visibility(th, visibility);
809         if (__is_err(ret) == true) {
810                 printf("Fail to set visibility!!\n");
811                 return -1;
812         }
813
814         return 1;
815 }
816
817 static int test_tethering_wifi_set_passphrase(void)
818 {
819         int ret;
820         char passphrase[100];
821
822         printf("Input passphrase for Wi-Fi tethering: ");
823         ret = scanf("%99s", passphrase);
824         if (ret < 0) {
825                 printf("scanf is failed!!\n");
826                 return -1;
827         }
828
829         ret = tethering_wifi_set_passphrase(th, passphrase);
830         if (__is_err(ret) == true) {
831                 printf("Fail to set passphrase!!\n");
832                 return -1;
833         }
834
835         return 1;
836 }
837
838 static int test_tethering_wifi_ap_set_ssid(void)
839 {
840         int ret;
841         char ssid[100];
842
843         printf("Input SSID for Wi-Fi AP tethering: ");
844         ret = scanf("%99s", ssid);
845         if (ret < 0) {
846                 printf("scanf is failed!!\n");
847                 return -1;
848         }
849
850         ret = tethering_wifi_ap_set_ssid(th, ssid);
851         if (__is_err(ret) == true) {
852                 printf("Fail to set wifi ap ssid!!\n");
853                 return -1;
854         }
855         return 1;
856 }
857
858 static int test_tethering_wifi_ap_set_security_type(void)
859 {
860         int ret;
861         int security_type;
862
863         printf("Input security type for Wi-Fi AP tethering (0:NONE, 1:WPA2_PSK)");
864         ret = scanf("%9d", &security_type);
865         if (ret < 0) {
866                 printf("scanf is failed!!\n");
867                 return -1;
868         }
869
870         ret = tethering_wifi_ap_set_security_type(th, security_type);
871         if (__is_err(ret) == true) {
872                 printf("Fail to set security type!!\n");
873                 return -1;
874         }
875         return 1;
876 }
877
878 static int test_tethering_wifi_ap_set_visibility(void)
879 {
880         int ret;
881         int visibility;
882
883         printf("Input security type for Wi-Fi tethering (0:invisible, 1:visible)");
884         ret = scanf("%9d", &visibility);
885         if (ret < 0) {
886                 printf("scanf is failed!!\n");
887                 return -1;
888         }
889
890         ret = tethering_wifi_ap_set_ssid_visibility(th, visibility);
891         if (__is_err(ret) == true) {
892                 printf("Fail to set visibility!!\n");
893                 return -1;
894         }
895         return 1;
896 }
897
898 static int test_tethering_wifi_ap_set_passphrase(void)
899 {
900         int ret;
901         char passphrase[100];
902
903         printf("Input passphrase for Wi-Fi tethering: ");
904         ret = scanf("%99s", passphrase);
905         if (ret < 0) {
906                 printf("scanf is failed!!\n");
907                 return -1;
908         }
909
910         ret = tethering_wifi_ap_set_passphrase(th, passphrase);
911         if (__is_err(ret) == true) {
912                  printf("Fail to set passphrase!!\n");
913                  return -1;
914         }
915         return 1;
916 }
917
918 static int test_tethering_wifi_reload_settings(void)
919 {
920         int ret = tethering_wifi_reload_settings(th, __settings_reloaded_cb, NULL);
921
922         if (__is_err(ret) == true) {
923                 printf("Fail to reload wifi tethering!!\n");
924                 return -1;
925         }
926         return 1;
927 }
928
929 static int test_tethering_wifi_ap_reload_settings(void)
930 {
931         int ret = tethering_wifi_ap_reload_settings(th, __settings_reloaded_cb, NULL);
932
933         if (__is_err(ret) == true) {
934                 printf("Fail to reload wifi tethering!!\n");
935                 return -1;
936         }
937         return 1;
938 }
939
940 int main(int argc, char **argv)
941 {
942         GMainLoop *mainloop;
943
944 #if !GLIB_CHECK_VERSION(2,36,0)
945         g_type_init();
946 #endif
947         mainloop = g_main_loop_new (NULL, false);
948
949         GIOChannel *channel = g_io_channel_unix_new(0);
950         g_io_add_watch(channel, (G_IO_IN|G_IO_ERR|G_IO_HUP|G_IO_NVAL), test_thread, NULL);
951         printf("Test Thread created...\n");
952         g_main_loop_run (mainloop);
953
954         return 0;
955 }
956
957 gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data)
958 {
959         int rv;
960         char a[10];
961
962         printf("Event received from stdin\n");
963
964         rv = read(0, a, 10);
965
966         if (rv <= 0 || a[0] == '0') {
967                 // Finish
968                 exit(1);
969         }
970
971         if (a[0] == '\n' || a[0] == '\r') {
972                 printf("\n\n Network Connection API Test App\n\n");
973                 printf("Options..\n");
974                 printf("1       - Tethering create and set callbacks\n");
975                 printf("2       - Tethering destroy\n");
976                 printf("3       - Enable Tethering\n");
977                 printf("4       - Disable Tethering\n");
978                 printf("5       - Get client information\n");
979                 printf("6       - Get interface information\n");
980                 printf("7       - Get data usage\n");
981                 printf("8       - Get Wi-Fi tethering setting\n");
982                 printf("9       - Get Wi-Fi AP setting\n");
983                 printf("a       - Set Wi-Fi tethering SSID\n");
984                 printf("b       - Set Wi-Fi tethering security type\n");
985                 printf("c       - Set Wi-Fi tethering visibility\n");
986                 printf("d       - Set Wi-Fi tethering passphrase\n");
987                 printf("e       - Set Wi-Fi AP SSID\n");
988                 printf("f       - Set Wi-Fi AP security type\n");
989                 printf("g       - Set Wi-Fi AP visibility\n");
990                 printf("h       - Set Wi-Fi AP passphrase\n");
991                 printf("i       - Reload Wi-Fi tethering\n");
992                 printf("j       - Reload Wi-Fi AP\n");
993                 printf("0       - Exit \n");
994                 printf("ENTER  - Show options menu.......\n");
995         }
996
997         switch (a[0]) {
998                 case '1':
999                         rv = test_tethering_create();
1000                         break;
1001                 case '2':
1002                         rv = test_tethering_destroy();
1003                         break;
1004                 case '3':
1005                         rv = test_tethering_enable();
1006                         break;
1007                 case '4':
1008                         rv = test_tethering_disable();
1009                         break;
1010                 case '5':
1011                         rv = test_tethering_get_client_info();
1012                         break;
1013                 case '6':
1014                         rv = test_tethering_get_interface_info();
1015                         break;
1016                 case '7':
1017                         rv = test_tethering_get_data_usage();
1018                         break;
1019                 case '8':
1020                         rv = test_tethering_wifi_get_setting();
1021                         break;
1022                 case '9':
1023                         rv = test_tethering_wifi_ap_get_setting();
1024                         break;
1025                 case 'a':
1026                         rv = test_tethering_wifi_set_ssid();
1027                         break;
1028                 case 'b':
1029                         rv = test_tethering_wifi_set_security_type();
1030                         break;
1031                 case 'c':
1032                         rv = test_tethering_wifi_set_visibility();
1033                         break;
1034                 case 'd':
1035                         rv = test_tethering_wifi_set_passphrase();
1036                         break;
1037                 case 'e':
1038                         rv = test_tethering_wifi_ap_set_ssid();
1039                         break;
1040                 case 'f':
1041                         rv = test_tethering_wifi_ap_set_security_type();
1042                         break;
1043                 case 'g':
1044                         rv = test_tethering_wifi_ap_set_visibility();
1045                         break;
1046                 case 'h':
1047                         rv = test_tethering_wifi_ap_set_passphrase();
1048                         break;
1049                 case 'i':
1050                         rv = test_tethering_wifi_reload_settings();
1051                         break;
1052                 case 'j':
1053                         rv = test_tethering_wifi_ap_reload_settings();
1054                         break;
1055
1056         }
1057
1058         if (rv == 1)
1059                 printf("Operation succeeded!\n");
1060         else
1061                 printf("Operation failed!\n");
1062
1063         return true;
1064 }