a89dece73491d2954a953ae4bfc45a9e01183efb
[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 INPUT_BUF_LEN           128
32 #define DISABLE_REASON_TEXT_LEN 64
33 #define COMMON_STR_BUF_LEN      32
34
35 typedef struct {
36         tethering_enabled_cb enabled_cb;
37         tethering_disabled_cb disabled_cb;
38         tethering_connection_state_changed_cb changed_cb;
39         tethering_wifi_security_type_changed_cb security_type_changed_cb;
40         tethering_wifi_ssid_visibility_changed_cb ssid_visibility_changed_cb;
41         tethering_wifi_passphrase_changed_cb passphrase_changed_cb;
42 } __tethering_cbs;
43
44 static GMainLoop *mainloop = NULL;
45
46 static bool __is_err(tethering_error_e ret)
47 {
48         char *err_msg = NULL;
49
50         switch (ret) {
51         case TETHERING_ERROR_INVALID_PARAMETER:
52                 err_msg = "Wrong parameter is used";
53                 break;
54
55         case TETHERING_ERROR_OUT_OF_MEMORY:
56                 err_msg = "Memory is not enough";
57                 break;
58
59         case TETHERING_ERROR_NONE:
60                 return false;
61
62         case TETHERING_ERROR_NOT_ENABLED:
63                 err_msg = "Tethering is not enabled";
64                 break;
65
66         case TETHERING_ERROR_OPERATION_FAILED:
67                 err_msg = "Operation is failed";
68                 break;
69
70         case TETHERING_ERROR_RESOURCE_BUSY:
71                 err_msg = "Resource is busy";
72                 break;
73
74         default:
75                 err_msg = "This should not be happened";
76                 break;
77         }
78
79         g_print("%s\n", err_msg);
80
81         return true;
82 }
83
84 static const char *__convert_tethering_type_to_str(const tethering_type_e type)
85 {
86         static char str_buf[COMMON_STR_BUF_LEN] = {0, };
87
88         switch (type) {
89         case TETHERING_TYPE_USB:
90                 g_strlcpy(str_buf, "USB Tethering", sizeof(str_buf));
91                 break;
92
93         case TETHERING_TYPE_WIFI:
94                 g_strlcpy(str_buf, "Wi-Fi Tethering", sizeof(str_buf));
95                 break;
96
97         case TETHERING_TYPE_BT:
98                 g_strlcpy(str_buf, "Bluetooth Tethering", sizeof(str_buf));
99                 break;
100
101         case TETHERING_TYPE_RESERVED:
102                 g_strlcpy(str_buf, "Wi-Fi AP", sizeof(str_buf));
103                 break;
104
105         default:
106                 g_strlcpy(str_buf, "Unknown", sizeof(str_buf));
107                 break;
108         }
109
110         return str_buf;
111 }
112
113 static const char *__convert_disabled_code_to_str(const tethering_disabled_cause_e code)
114 {
115         static char str_buf[DISABLE_REASON_TEXT_LEN] = {0, };
116
117         switch (code) {
118         case TETHERING_DISABLED_BY_USB_DISCONNECTION:
119                 strncpy(str_buf, "disabled due to usb disconnection", sizeof(str_buf));
120                 break;
121
122         case TETHERING_DISABLED_BY_FLIGHT_MODE:
123                 strncpy(str_buf, "disabled due to flight mode on", sizeof(str_buf));
124                 break;
125
126         case TETHERING_DISABLED_BY_LOW_BATTERY:
127                 strncpy(str_buf, "disabled due to low battery", sizeof(str_buf));
128                 break;
129
130         case TETHERING_DISABLED_BY_NETWORK_CLOSE:
131                 strncpy(str_buf, "disabled due to pdp network close", sizeof(str_buf));
132                 break;
133
134         case TETHERING_DISABLED_BY_TIMEOUT:
135                 strncpy(str_buf, "disabled due to timeout", sizeof(str_buf));
136                 break;
137
138         case TETHERING_DISABLED_BY_OTHERS:
139                 strncpy(str_buf, "disabled by other apps", sizeof(str_buf));
140                 break;
141
142         case TETHERING_DISABLED_BY_REQUEST:
143                 strncpy(str_buf, "disabled by my request", sizeof(str_buf));
144                 break;
145
146         case TETHERING_DISABLED_BY_WIFI_ON:
147                 strncpy(str_buf, "disabled by Wi-Fi station on", sizeof(str_buf));
148                 break;
149
150         case TETHERING_DISABLED_BY_BT_OFF:
151                 strncpy(str_buf, "disabled by bluetooth off", sizeof(str_buf));
152                 break;
153
154         default:
155                 strncpy(str_buf, "disabled by unknown reason", sizeof(str_buf));
156                 break;
157         }
158
159         return str_buf;
160 }
161
162 static void __register_cbs(tethering_h th, __tethering_cbs *cbs, void *user_data)
163 {
164         tethering_error_e ret = TETHERING_ERROR_NONE;
165
166         ret = tethering_set_enabled_cb(th, TETHERING_TYPE_ALL,
167                         cbs->enabled_cb, user_data);
168         if (__is_err(ret) == true)
169                 g_print("tethering_set_enabled_cb is failed\n");
170
171         ret = tethering_set_enabled_cb(th, TETHERING_TYPE_RESERVED,
172                         cbs->enabled_cb, user_data);
173         if (__is_err(ret) == true)
174                 g_print("tethering_set_enabled_cb is failed\n");
175
176         ret = tethering_set_disabled_cb(th, TETHERING_TYPE_ALL,
177                         cbs->disabled_cb, user_data);
178         if (__is_err(ret) == true)
179                 g_print("tethering_set_disabled_cb is failed\n");
180
181         ret = tethering_set_disabled_cb(th, TETHERING_TYPE_RESERVED,
182                         cbs->disabled_cb, user_data);
183         if (__is_err(ret) == true)
184                 g_print("tethering_set_disabled_cb is failed\n");
185
186         ret = tethering_set_connection_state_changed_cb(th, TETHERING_TYPE_ALL,
187                         cbs->changed_cb, user_data);
188         if (__is_err(ret) == true)
189                 g_print("tethering_set_connection_state_changed_cb is failed\n");
190
191         ret = tethering_set_connection_state_changed_cb(th, TETHERING_TYPE_RESERVED,
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         ret = tethering_wifi_set_security_type_changed_cb(th,
197                         cbs->security_type_changed_cb, user_data);
198         if (__is_err(ret) == true)
199                 g_print("tethering_wifi_set_security_type_changed_cb is failed\n");
200
201         ret = tethering_wifi_set_ssid_visibility_changed_cb(th,
202                         cbs->ssid_visibility_changed_cb, user_data);
203         if (__is_err(ret) == true)
204                 g_print("tethering_wifi_set_ssid_visibility_changed_cb is failed\n");
205
206         ret = tethering_wifi_set_passphrase_changed_cb(th,
207                         cbs->passphrase_changed_cb, user_data);
208         if (__is_err(ret) == true)
209                 g_print("tethering_wifi_set_passphrase_changed_cb is failed\n");
210
211         return;
212 }
213
214 static void __deregister_cbs(tethering_h th)
215 {
216         tethering_error_e ret = TETHERING_ERROR_NONE;
217
218         ret = tethering_unset_enabled_cb(th, TETHERING_TYPE_ALL);
219         if (__is_err(ret) == true)
220                 g_print("tethering_unset_enabled_cb is failed\n");
221
222         ret = tethering_unset_enabled_cb(th, TETHERING_TYPE_RESERVED);
223         if (__is_err(ret) == true)
224                 g_print("tethering_unset_enabled_cb is failed\n");
225
226         ret = tethering_unset_disabled_cb(th, TETHERING_TYPE_ALL);
227         if (__is_err(ret) == true)
228                 g_print("tethering_unset_disabled_cb is failed\n");
229
230         ret = tethering_unset_disabled_cb(th, TETHERING_TYPE_RESERVED);
231         if (__is_err(ret) == true)
232                 g_print("tethering_unset_disabled_cb is failed\n");
233
234         ret = tethering_unset_connection_state_changed_cb(th, TETHERING_TYPE_ALL);
235         if (__is_err(ret) == true)
236                 g_print("tethering_unset_connection_state_changed_cb is failed\n");
237
238         ret = tethering_unset_connection_state_changed_cb(th, TETHERING_TYPE_RESERVED);
239         if (__is_err(ret) == true)
240                 g_print("tethering_unset_connection_state_changed_cb is failed\n");
241
242         ret = tethering_wifi_unset_security_type_changed_cb(th);
243         if (__is_err(ret) == true)
244                 g_print("tethering_wifi_unset_security_type_changed_cb is failed\n");
245
246         ret = tethering_wifi_unset_ssid_visibility_changed_cb(th);
247         if (__is_err(ret) == true)
248                 g_print("tethering_wifi_unset_ssid_visibility_changed_cb is failed\n");
249
250         ret = tethering_wifi_unset_passphrase_changed_cb(th);
251         if (__is_err(ret) == true)
252                 g_print("tethering_wifi_unset_passphrase_changed_cb is failed\n");
253
254         return;
255 }
256
257 /* Tethering callbacks */
258 static void __enabled_cb(tethering_error_e error, tethering_type_e type, bool is_requested, void *data)
259 {
260         if (error != TETHERING_ERROR_NONE) {
261                 if (!is_requested)
262                         return;
263
264                 g_print("## %s is not enabled. error code[0x%X]\n",
265                                 __convert_tethering_type_to_str(type),
266                                 error);
267                 return;
268         }
269
270         if (is_requested)
271                 g_print("## %s is enabled successfully\n",
272                                 __convert_tethering_type_to_str(type));
273         else
274                 g_print("## %s is enabled by other app\n",
275                                 __convert_tethering_type_to_str(type));
276
277         return;
278 }
279
280 static void __disabled_cb(tethering_error_e error, tethering_type_e type, tethering_disabled_cause_e code, void *data)
281 {
282         if (error != TETHERING_ERROR_NONE) {
283                 if (code != TETHERING_DISABLED_BY_REQUEST)
284                         return;
285
286                 g_print("## %s is not disabled. error code[0x%X]\n",
287                                 __convert_tethering_type_to_str(type), error);
288                 return;
289         }
290
291         g_print("## %s is %s\n",
292                         __convert_tethering_type_to_str(type),
293                         __convert_disabled_code_to_str(code));
294
295         return;
296 }
297
298 static void __connection_state_changed_cb(tethering_client_h client, bool open, void *data)
299 {
300         tethering_client_h clone = NULL;
301         tethering_type_e type;
302         char *ip_address = NULL;
303         char *mac_address = NULL;
304         char *hostname = NULL;
305
306         tethering_client_clone(&clone, client);
307         if (clone == NULL) {
308                 g_print("tetheirng_client_clone is failed\n");
309                 return;
310         }
311
312         tethering_client_get_tethering_type(clone, &type);
313         tethering_client_get_ip_address(clone,
314                         TETHERING_ADDRESS_FAMILY_IPV4, &ip_address);
315         tethering_client_get_mac_address(clone, &mac_address);
316         tethering_client_get_name(clone, &hostname);
317
318         if (open) {
319                 g_print("## New station Type [%s], IP [%s], MAC [%s], hostname [%s]\n",
320                                 __convert_tethering_type_to_str(type),
321                                 ip_address, mac_address, hostname);
322         } else {
323                 g_print("## Disconnected station Type [%s], IP [%s], MAC [%s], hostname [%s]\n",
324                                 __convert_tethering_type_to_str(type),
325                                 ip_address, mac_address, hostname);
326         }
327
328         if (ip_address)
329                 free(ip_address);
330         if (mac_address)
331                 free(mac_address);
332         if (hostname)
333                 free(hostname);
334
335         tethering_client_destroy(clone);
336
337         return;
338 }
339
340 static void __data_usage_cb(tethering_error_e result, unsigned long long received_data,
341                 unsigned long long sent_data, void *user_data)
342 {
343         g_print("__data_usage_cb\n");
344
345         if (result != TETHERING_ERROR_NONE) {
346                 g_print("tethering_get_data_usage is failed. error[0x%X]\n", result);
347                 return;
348         }
349
350         g_print("## Received data : %llu bytes\n", received_data);
351         g_print("## Sent data : %llu bytes\n", sent_data);
352
353         return;
354 }
355
356 static void __settings_reloaded_cb(tethering_error_e result, void *user_data)
357 {
358         g_print("__settings_reloaded_cb\n");
359
360         if (result != TETHERING_ERROR_NONE) {
361                 g_print("tethering_wifi_reload_settings is failed. error[0x%X]\n", result);
362                 return;
363         }
364
365         g_print("## Wi-Fi tethering setting is reloaded\n");
366
367         return;
368 }
369
370 static bool __clients_foreach_cb(tethering_client_h client, void *data)
371 {
372         tethering_client_h clone = NULL;
373         tethering_type_e type;
374         char *ip_address = NULL;
375         char *mac_address = NULL;
376         char *hostname = NULL;
377
378         /* Clone internal information */
379         if (tethering_client_clone(&clone, client) != TETHERING_ERROR_NONE) {
380                 g_print("tethering_client_clone is failed\n");
381                 return false;
382         }
383
384         /* Get information */
385         if (tethering_client_get_tethering_type(clone, &type) != TETHERING_ERROR_NONE)
386                 g_print("tethering_client_get_type is failed\n");
387
388         if (tethering_client_get_ip_address(clone, TETHERING_ADDRESS_FAMILY_IPV4, &ip_address) != TETHERING_ERROR_NONE)
389                 g_print("tethering_client_get_ip_address is failed\n");
390
391         if (tethering_client_get_mac_address(clone, &mac_address) != TETHERING_ERROR_NONE)
392                 g_print("tethering_client_get_mac_address is failed\n");
393
394         if (tethering_client_get_name(clone, &hostname) != TETHERING_ERROR_NONE)
395                 g_print("tethering_client_get_hostname is failed\n");
396         /* End of getting information */
397
398         g_print("\n< Client Info. >\n");
399         g_print("\tType %s\n", __convert_tethering_type_to_str(type));
400         g_print("\tIP Address %s\n", ip_address);
401         g_print("\tMAC Address : %s\n", mac_address);
402         g_print("\tHostname : %s\n", hostname);
403
404         /* Destroy cloned objects */
405         if (ip_address)
406                 free(ip_address);
407         if (mac_address)
408                 free(mac_address);
409         if (hostname)
410                 free(hostname);
411
412         tethering_client_destroy(clone);
413
414         /* Continue iteration */
415         return true;
416 }
417
418 static void __security_type_changed_cb(tethering_wifi_security_type_e changed_type, void *user_data)
419 {
420         g_print("Wi-Fi Tethering Security type is changed to [%s]\n",
421                         changed_type == TETHERING_WIFI_SECURITY_TYPE_NONE ?
422                         "open" : "wpa2-psk");
423         return;
424 }
425
426 static void __ssid_visibility_changed_cb(bool changed_visible, void *user_data)
427 {
428         g_print("SSID visibility for Wi-Fi tethering changed to [%s]\n",
429                         changed_visible ? "visible" : "invisible");
430         return;
431 }
432
433 static void __passphrase_changed_cb(void *user_data)
434 {
435         g_print("Wi-Fi Tethering passphrase is changed\n");
436         return;
437 }
438 /* End of tethering callbacks */
439
440 static void __enable_tethering(tethering_h th, tethering_type_e type)
441 {
442         if (th == NULL)
443                 return;
444
445         tethering_error_e error = TETHERING_ERROR_NONE;
446
447         error = tethering_enable(th, type);
448         __is_err(error);
449
450         return;
451 }
452
453 static void __disable_tethering(tethering_h th, tethering_type_e type)
454 {
455         if (th == NULL)
456                 return;
457
458         tethering_error_e error = TETHERING_ERROR_NONE;
459
460         error = tethering_disable(th, type);
461         __is_err(error);
462
463         return;
464 }
465
466 static void __print_interface_info(tethering_h th, tethering_type_e type)
467 {
468         char *interface = NULL;
469         char *mac_address = NULL;
470         char *ip_address = NULL;
471         char *gateway_address = NULL;
472         char *subnet_mask = NULL;
473
474         if (tethering_is_enabled(th, type) == FALSE) {
475                 g_print("%s is not enabled\n",
476                                 __convert_tethering_type_to_str(type));
477                 return;
478         }
479
480         tethering_get_network_interface_name(th, type, &interface);
481         tethering_get_mac_address(th, type, &mac_address);
482         tethering_get_ip_address(th, type, TETHERING_ADDRESS_FAMILY_IPV4,
483                         &ip_address);
484         tethering_get_gateway_address(th, type, TETHERING_ADDRESS_FAMILY_IPV4,
485                         &gateway_address);
486         tethering_get_subnet_mask(th, type, TETHERING_ADDRESS_FAMILY_IPV4,
487                         &subnet_mask);
488
489         g_print("interface name : %s\n", interface);
490         g_print("mac address : %s\n", mac_address);
491         g_print("ip address : %s\n", ip_address);
492         g_print("gateway address: %s\n", gateway_address);
493         g_print("subnet mask : %s\n", subnet_mask);
494
495         if (interface)
496                 free(interface);
497         if (mac_address)
498                 free(mac_address);
499         if (ip_address)
500                 free(ip_address);
501         if (gateway_address)
502                 free(gateway_address);
503         if (subnet_mask)
504                 free(subnet_mask);
505
506         return;
507 }
508
509 static void __print_wifi_tethering_setting(tethering_h th)
510 {
511         char *ssid = NULL;
512         char *passphrase = NULL;
513         bool visibility = false;
514         tethering_wifi_security_type_e security_type = TETHERING_WIFI_SECURITY_TYPE_NONE;
515
516         int error = TETHERING_ERROR_NONE;
517
518         error = tethering_wifi_get_ssid(th, &ssid);
519         if (error != TETHERING_ERROR_NONE)
520                 __is_err(error);
521         else
522                 g_print("\n\t** WiFi tethering SSID : %s\n", ssid);
523
524         error = tethering_wifi_get_passphrase(th, &passphrase);
525         if (error != TETHERING_ERROR_NONE)
526                 __is_err(error);
527         else
528                 g_print("\t** WiFi tethering passphrase : %s\n", passphrase);
529
530         error = tethering_wifi_get_ssid_visibility(th, &visibility);
531         if (error != TETHERING_ERROR_NONE)
532                 __is_err(error);
533         else
534                 g_print("\t** WiFi tethering ssid visibility : %s\n",
535                                 visibility ? "visible" : "invisible");
536
537         error = tethering_wifi_get_security_type(th, &security_type);
538         if (error != TETHERING_ERROR_NONE)
539                 __is_err(error);
540         else
541                 g_print("\t** WiFi tethering security_type : %s\n",
542                                 security_type ==
543                                 TETHERING_WIFI_SECURITY_TYPE_NONE ?
544                                 "open" : "wpa2-psk");
545
546         if (ssid)
547                 free(ssid);
548         if (passphrase)
549                 free(passphrase);
550
551         return;
552 }
553
554 static void __print_wifi_ap_setting(tethering_h th)
555 {
556         char *ssid = NULL;
557         char *passphrase = NULL;
558         bool visibility = false;
559         tethering_wifi_security_type_e security_type = TETHERING_WIFI_SECURITY_TYPE_NONE;
560
561         int error = TETHERING_ERROR_NONE;
562
563         error = tethering_wifi_ap_get_ssid(th, &ssid);
564         if (error != TETHERING_ERROR_NONE)
565                 __is_err(error);
566         else
567                 g_print("\n\t** WiFi AP SSID : %s\n", ssid);
568
569         error = tethering_wifi_ap_get_passphrase(th, &passphrase);
570         if (error != TETHERING_ERROR_NONE)
571                 __is_err(error);
572         else
573                 g_print("\t** WiFi AP passphrase : %s\n", passphrase);
574
575         error = tethering_wifi_ap_get_ssid_visibility(th, &visibility);
576         if (error != TETHERING_ERROR_NONE)
577                 __is_err(error);
578         else
579                 g_print("\t** WiFi AP ssid visibility : %s\n",
580                                 visibility ? "visible" : "invisible");
581
582         error = tethering_wifi_ap_get_security_type(th, &security_type);
583         if (error != TETHERING_ERROR_NONE)
584                 __is_err(error);
585         else
586                 g_print("\t** WiFi AP security_type : %s\n",
587                                 security_type ==
588                                 TETHERING_WIFI_SECURITY_TYPE_NONE ?
589                                 "open" : "wpa2-psk");
590
591         if (ssid)
592                 free(ssid);
593         if (passphrase)
594                 free(passphrase);
595
596         return;
597 }
598
599 void print_menu(void)
600 {
601         g_print("\nTo get client information, enter 'clients [USB | WIFI | BT | AP | ALL]'");
602         g_print("\nTo get interface information, enter 'info [USB | WIFI | BT | AP]'");
603         g_print("\nTo get data usage, enter 'get data_usage'");
604         g_print("\nTo enable tethering, enter 'enable [USB | WIFI | BT | AP | ALL]'");
605         g_print("\nTo disable tethering, enter 'disable [USB | WIFI | BT | AP | ALL]'");
606         g_print("\nTo get Wi-Fi tethering setting, enter 'get wifi_setting'");
607         g_print("\nTo get Wi-Fi AP setting, enter 'get wifi_ap_setting'");
608         g_print("\nTo reload Wi-Fi tethering setting, enter 'reload wifi_setting'");
609         g_print("\nTo reload Wi-Fi AP setting, enter 'reload wifi_ap_setting'");
610         g_print("\nTo set Wi-Fi tethering setting, enter '[set_security_type | set_visibility] [0 | 1]'");
611         g_print("\nTo set Wi-Fi AP setting, enter '[set_ap_security_type | set_ap_visibility] [0 | 1]'");
612         g_print("\nTo set Wi-Fi tethering passphrase, enter 'set_passphrase [passphrase]'");
613         g_print("\nTo set Wi-Fi AP passphrase, enter 'set_ap_passphrase [passphrase]'");
614         g_print("\nTo set Wi-Fi tethering SSID, enter 'set_ssid [SSID]'");
615         g_print("\nTo set Wi-Fi AP SSID, enter 'set_ap_ssid [SSID]'");
616         g_print("\nTo do testing multiple time to create and destroy tethering enter 'do handle_creation_test [number_of_times]'");
617         g_print("\nTo quit, enter 'quit'\n> ");
618
619         return;
620 }
621
622 gboolean input(GIOChannel *channel, GIOCondition condition, gpointer data)
623 {
624         tethering_h th = (tethering_h)data;
625         tethering_type_e type = 0;
626         tethering_error_e error = 0;
627         gchar buf[INPUT_BUF_LEN] = {0, };
628         gchar *cmd = NULL;
629         gchar *param = NULL;
630         gsize read = 0;
631         __tethering_cbs cbs = {
632                 __enabled_cb, __disabled_cb,
633                 __connection_state_changed_cb, __security_type_changed_cb,
634                 __ssid_visibility_changed_cb, __passphrase_changed_cb};
635
636 #if !GLIB_CHECK_VERSION(2, 31, 0)
637         if (g_io_channel_read(channel, buf, INPUT_BUF_LEN, &read) != G_IO_ERROR_NONE) {
638                 g_print("g_io_channel_read is failed\n");
639                 return FALSE;
640         }
641 #else
642         GError *err = NULL;
643         GIOStatus ios;
644
645         ios = g_io_channel_read_chars(channel, buf, INPUT_BUF_LEN, &read, &err);
646         if (err != NULL) {
647                 g_print("g_io_channel_read_chars is failed : %s\n",
648                                 err->message);
649                 g_error_free(err);
650                 return FALSE;
651         } else if (ios != G_IO_STATUS_NORMAL) {
652                 g_print("g_io_channel_read_chars is failed : %d\n", ios);
653                 return FALSE;
654         }
655 #endif
656
657         buf[read] = '\0';
658         g_strstrip(buf);
659
660         cmd = buf;
661         param = strrchr(buf, ' ');
662
663         /* No parameter */
664         if (!strcmp(cmd, "quit")) {
665                 g_main_loop_quit(mainloop);
666                 return TRUE;
667         }
668
669         if (param == NULL) {
670                 print_menu();
671                 return TRUE;
672         }
673         *param = '\0';
674         param++;
675
676         /* One parameter except type */
677         if (!strcmp(cmd, "get") && !strcmp(param, "data_usage")) {
678                 error = tethering_get_data_usage(th, __data_usage_cb, NULL);
679                 if (error != TETHERING_ERROR_NONE)
680                         g_print("tethering_get_data_usage is failed [0x%X]\n",
681                                         error);
682                 goto DONE;
683         }
684
685         if (!strcmp(cmd, "get") && !strcmp(param, "wifi_setting")) {
686                 __print_wifi_tethering_setting(th);
687                 goto DONE;
688         }
689
690         if (!strcmp(cmd, "get") && !strcmp(param, "wifi_ap_setting")) {
691                 __print_wifi_ap_setting(th);
692                 goto DONE;
693         }
694
695         if (!strcmp(cmd, "reload") && !strcmp(param, "wifi_setting")) {
696                 error = tethering_wifi_reload_settings(th, __settings_reloaded_cb, NULL);
697                 if (error != TETHERING_ERROR_NONE)
698                         g_print("tethering_wifi_reload_settings is failed [0x%X]\n",
699                                         error);
700                 goto DONE;
701         }
702
703         if (!strcmp(cmd, "reload") && !strcmp(param, "wifi_ap_setting")) {
704                 error = tethering_wifi_ap_reload_settings(th, __settings_reloaded_cb, NULL);
705                 if (error != TETHERING_ERROR_NONE)
706                         g_print("tethering_wifi_ap_reload_settings is failed [0x%X]\n",
707                                         error);
708                 goto DONE;
709         }
710
711         if (!strcmp(cmd, "set_visibility")) {
712                 error = tethering_wifi_set_ssid_visibility(th, atoi(param));
713                 if (error != TETHERING_ERROR_NONE)
714                         g_print("tethering_wifi_set_ssid_visibility is failed [0x%X]\n",
715                                         error);
716                 goto DONE;
717         }
718
719         if (!strcmp(cmd, "set_ap_visibility")) {
720                 error = tethering_wifi_ap_set_ssid_visibility(th, atoi(param));
721                 if (error != TETHERING_ERROR_NONE)
722                         g_print("tethering_wifi_ap_set_ssid_visibility is failed [0x%X]\n",
723                                         error);
724                 goto DONE;
725         }
726
727         if (!strcmp(cmd, "set_security_type")) {
728                 error = tethering_wifi_set_security_type(th, atoi(param));
729                 if (error != TETHERING_ERROR_NONE)
730                         g_print("tethering_wifi_set_security_type is failed [0x%X]\n",
731                                         error);
732                 goto DONE;
733         }
734
735         if (!strcmp(cmd, "set_ap_security_type")) {
736                 error = tethering_wifi_ap_set_security_type(th, atoi(param));
737                 if (error != TETHERING_ERROR_NONE)
738                         g_print("tethering_wifi_ap_set_security_type is failed [0x%X]\n",
739                                         error);
740                 goto DONE;
741         }
742
743         /* This should be removed */
744         if (!strcmp(cmd, "set_passphrase")) {
745                 error = tethering_wifi_set_passphrase(th, param);
746                 if (error != TETHERING_ERROR_NONE)
747                         g_print("tethering_wifi_set_passphrase is failed [0x%X]\n",
748                                         error);
749                 goto DONE;
750         }
751
752         if (!strcmp(cmd, "set_ap_passphrase")) {
753                 error = tethering_wifi_ap_set_passphrase(th, param);
754                 if (error != TETHERING_ERROR_NONE)
755                         g_print("tethering_wifi_ap_set_passphrase is failed [0x%X]\n",
756                                         error);
757                 goto DONE;
758         }
759
760         if (!strcmp(cmd, "set_ssid")) {
761                 error = tethering_wifi_set_ssid(th, param);
762                 if (error != TETHERING_ERROR_NONE)
763                         g_print("tethering_wifi_set_ssid is failed [0x%X]\n",
764                                         error);
765                 goto DONE;
766         }
767
768         if (!strcmp(cmd, "set_ap_ssid")) {
769                 error = tethering_wifi_ap_set_ssid(th, param);
770                 if (error != TETHERING_ERROR_NONE)
771                         g_print("tethering_wifi_ap_set_ssid is failed [0x%X]\n",
772                                 error);
773                 goto DONE;
774         }
775
776         if (!strcmp(cmd, "do handle_creation_test")) {
777                 int count = 0;
778                 int i = 0;
779                 count = atoi(param);
780                 g_print("testing %d times....\n", count);
781                 while (count > 0) {
782                         sleep(3);
783                         g_print("Destroying tethering %dth time\n", i);
784                         if (NULL != th) {
785                                 __deregister_cbs(th);
786
787                                 error = tethering_destroy(th);
788
789                                 if (__is_err(error) == true)
790                                         return 0;
791                         }
792                         sleep(3);
793                         g_print("Creating tethering %dth time\n", i);
794                         error = tethering_create(&th);
795                         if (__is_err(error) == true)
796                                 return 0;
797                         __register_cbs(th, &cbs, NULL);
798                         i++;
799                         count--;
800                 }
801                 goto DONE;
802         }
803
804         /* One parameter(type) */
805         if (!strcmp(param, "USB"))
806                 type = TETHERING_TYPE_USB;
807         else if (!strcmp(param, "WIFI"))
808                 type = TETHERING_TYPE_WIFI;
809         else if (!strcmp(param, "BT"))
810                 type = TETHERING_TYPE_BT;
811         else if (!strcmp(param, "AP"))
812                 type = TETHERING_TYPE_RESERVED;
813         else if (!strcmp(param, "ALL"))
814                 type = TETHERING_TYPE_ALL;
815         else
816                 goto DONE;
817
818         if (!strcmp(cmd, "clients")) {
819                 error = tethering_foreach_connected_clients(th, type,
820                                 __clients_foreach_cb, NULL);
821                 if (error != TETHERING_ERROR_NONE)
822                         g_print("tethering_get_data_usage is failed [0x%X]\n",
823                                         error);
824         } else if (!strcmp(cmd, "info")) {
825                 __print_interface_info(th, type);
826         } else if (!strcmp(cmd, "enable")) {
827                 __enable_tethering(th, type);
828         } else if (!strcmp(cmd, "disable")) {
829                 __disable_tethering(th, type);
830         } else {
831                 goto DONE;
832         }
833
834 DONE:
835         print_menu();
836         return TRUE;
837 }
838
839 int main(int argc, char *argv[])
840 {
841         tethering_h th = NULL;
842         GIOChannel *stdin_channel = NULL;
843         tethering_error_e ret = TETHERING_ERROR_NONE;
844         __tethering_cbs cbs = {
845                 __enabled_cb, __disabled_cb,
846                 __connection_state_changed_cb, __security_type_changed_cb,
847                 __ssid_visibility_changed_cb, __passphrase_changed_cb};
848
849 #if !GLIB_CHECK_VERSION(2, 36, 0)
850         g_type_init();
851 #endif
852
853         /* Create tethering handle */
854         ret = tethering_create(&th);
855         if (__is_err(ret) == true)
856                 return 0;
857
858         /* Register cbs */
859         __register_cbs(th, &cbs, NULL);
860
861         stdin_channel = g_io_channel_unix_new(0);
862         if (stdin_channel == NULL)
863                 return 0;
864
865         g_io_channel_set_encoding(stdin_channel, NULL, NULL);
866         g_io_channel_set_flags(stdin_channel,
867                         G_IO_FLAG_APPEND | G_IO_FLAG_NONBLOCK, NULL);
868
869         g_io_add_watch(stdin_channel, G_IO_IN, input, (gpointer)th);
870
871         print_menu();
872
873         mainloop = g_main_loop_new(NULL, 0);
874
875         g_main_loop_run(mainloop);
876         g_main_loop_unref(mainloop);
877
878         /* Deregister cbs */
879         __deregister_cbs(th);
880
881         /* Destroy tethering handle */
882         ret = tethering_destroy(th);
883         __is_err(ret);
884
885         return 0;
886 }