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