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