2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <glib-object.h>
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
29 #include "tethering.h"
31 #define INPUT_BUF_LEN 128
32 #define DISABLE_REASON_TEXT_LEN 64
33 #define COMMON_STR_BUF_LEN 32
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;
44 static GMainLoop *mainloop = NULL;
46 static bool __is_err(tethering_error_e ret)
51 case TETHERING_ERROR_INVALID_PARAMETER:
52 err_msg = "Wrong parameter is used";
55 case TETHERING_ERROR_OUT_OF_MEMORY:
56 err_msg = "Memory is not enough";
59 case TETHERING_ERROR_NONE:
62 case TETHERING_ERROR_NOT_ENABLED:
63 err_msg = "Tethering is not enabled";
66 case TETHERING_ERROR_OPERATION_FAILED:
67 err_msg = "Operation is failed";
70 case TETHERING_ERROR_RESOURCE_BUSY:
71 err_msg = "Resource is busy";
75 err_msg = "This should not be happened";
79 g_print("%s\n", err_msg);
84 static const char *__convert_tethering_type_to_str(const tethering_type_e type)
86 static char str_buf[COMMON_STR_BUF_LEN] = {0, };
89 case TETHERING_TYPE_USB:
90 g_strlcpy(str_buf, "USB Tethering", sizeof(str_buf));
93 case TETHERING_TYPE_WIFI:
94 g_strlcpy(str_buf, "Wi-Fi Tethering", sizeof(str_buf));
97 case TETHERING_TYPE_BT:
98 g_strlcpy(str_buf, "Bluetooth Tethering", sizeof(str_buf));
101 case TETHERING_TYPE_RESERVED:
102 g_strlcpy(str_buf, "Wi-Fi AP", sizeof(str_buf));
106 g_strlcpy(str_buf, "Unknown", sizeof(str_buf));
113 static const char *__convert_disabled_code_to_str(const tethering_disabled_cause_e code)
115 static char str_buf[DISABLE_REASON_TEXT_LEN] = {0, };
118 case TETHERING_DISABLED_BY_USB_DISCONNECTION:
119 strncpy(str_buf, "disabled due to usb disconnection", sizeof(str_buf));
122 case TETHERING_DISABLED_BY_FLIGHT_MODE:
123 strncpy(str_buf, "disabled due to flight mode on", sizeof(str_buf));
126 case TETHERING_DISABLED_BY_LOW_BATTERY:
127 strncpy(str_buf, "disabled due to low battery", sizeof(str_buf));
130 case TETHERING_DISABLED_BY_NETWORK_CLOSE:
131 strncpy(str_buf, "disabled due to pdp network close", sizeof(str_buf));
134 case TETHERING_DISABLED_BY_TIMEOUT:
135 strncpy(str_buf, "disabled due to timeout", sizeof(str_buf));
138 case TETHERING_DISABLED_BY_OTHERS:
139 strncpy(str_buf, "disabled by other apps", sizeof(str_buf));
142 case TETHERING_DISABLED_BY_REQUEST:
143 strncpy(str_buf, "disabled by my request", sizeof(str_buf));
146 case TETHERING_DISABLED_BY_WIFI_ON:
147 strncpy(str_buf, "disabled by Wi-Fi station on", sizeof(str_buf));
150 case TETHERING_DISABLED_BY_BT_OFF:
151 strncpy(str_buf, "disabled by bluetooth off", sizeof(str_buf));
155 strncpy(str_buf, "disabled by unknown reason", sizeof(str_buf));
162 static void __register_cbs(tethering_h th, __tethering_cbs *cbs, void *user_data)
164 tethering_error_e ret = TETHERING_ERROR_NONE;
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");
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");
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");
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");
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");
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");
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");
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");
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");
214 static void __deregister_cbs(tethering_h th)
216 tethering_error_e ret = TETHERING_ERROR_NONE;
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");
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");
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");
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");
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");
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");
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");
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");
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");
257 /* Tethering callbacks */
258 static void __enabled_cb(tethering_error_e error, tethering_type_e type, bool is_requested, void *data)
260 if (error != TETHERING_ERROR_NONE) {
264 g_print("## %s is not enabled. error code[0x%X]\n",
265 __convert_tethering_type_to_str(type),
271 g_print("## %s is enabled successfully\n",
272 __convert_tethering_type_to_str(type));
274 g_print("## %s is enabled by other app\n",
275 __convert_tethering_type_to_str(type));
280 static void __disabled_cb(tethering_error_e error, tethering_type_e type, tethering_disabled_cause_e code, void *data)
282 if (error != TETHERING_ERROR_NONE) {
283 if (code != TETHERING_DISABLED_BY_REQUEST)
286 g_print("## %s is not disabled. error code[0x%X]\n",
287 __convert_tethering_type_to_str(type), error);
291 g_print("## %s is %s\n",
292 __convert_tethering_type_to_str(type),
293 __convert_disabled_code_to_str(code));
298 static void __connection_state_changed_cb(tethering_client_h client, bool open, void *data)
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;
306 tethering_client_clone(&clone, client);
308 g_print("tetheirng_client_clone is failed\n");
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);
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);
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);
335 tethering_client_destroy(clone);
340 static void __data_usage_cb(tethering_error_e result, unsigned long long received_data,
341 unsigned long long sent_data, void *user_data)
343 g_print("__data_usage_cb\n");
345 if (result != TETHERING_ERROR_NONE) {
346 g_print("tethering_get_data_usage is failed. error[0x%X]\n", result);
350 g_print("## Received data : %llu bytes\n", received_data);
351 g_print("## Sent data : %llu bytes\n", sent_data);
356 static void __settings_reloaded_cb(tethering_error_e result, void *user_data)
358 g_print("__settings_reloaded_cb\n");
360 if (result != TETHERING_ERROR_NONE) {
361 g_print("tethering_wifi_reload_settings is failed. error[0x%X]\n", result);
365 g_print("## Wi-Fi tethering setting is reloaded\n");
370 static bool __clients_foreach_cb(tethering_client_h client, void *data)
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;
378 /* Clone internal information */
379 if (tethering_client_clone(&clone, client) != TETHERING_ERROR_NONE) {
380 g_print("tethering_client_clone is failed\n");
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");
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");
391 if (tethering_client_get_mac_address(clone, &mac_address) != TETHERING_ERROR_NONE)
392 g_print("tethering_client_get_mac_address is failed\n");
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 */
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);
404 /* Destroy cloned objects */
412 tethering_client_destroy(clone);
414 /* Continue iteration */
418 static void __security_type_changed_cb(tethering_wifi_security_type_e changed_type, void *user_data)
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");
426 static void __ssid_visibility_changed_cb(bool changed_visible, void *user_data)
428 g_print("SSID visibility for Wi-Fi tethering changed to [%s]\n",
429 changed_visible ? "visible" : "invisible");
433 static void __passphrase_changed_cb(void *user_data)
435 g_print("Wi-Fi Tethering passphrase is changed\n");
438 /* End of tethering callbacks */
440 static void __enable_tethering(tethering_h th, tethering_type_e type)
445 tethering_error_e error = TETHERING_ERROR_NONE;
447 error = tethering_enable(th, type);
453 static void __disable_tethering(tethering_h th, tethering_type_e type)
458 tethering_error_e error = TETHERING_ERROR_NONE;
460 error = tethering_disable(th, type);
466 static void __print_interface_info(tethering_h th, tethering_type_e type)
468 char *interface = NULL;
469 char *mac_address = NULL;
470 char *ip_address = NULL;
471 char *gateway_address = NULL;
472 char *subnet_mask = NULL;
474 if (tethering_is_enabled(th, type) == FALSE) {
475 g_print("%s is not enabled\n",
476 __convert_tethering_type_to_str(type));
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,
484 tethering_get_gateway_address(th, type, TETHERING_ADDRESS_FAMILY_IPV4,
486 tethering_get_subnet_mask(th, type, TETHERING_ADDRESS_FAMILY_IPV4,
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);
502 free(gateway_address);
509 static void __print_wifi_tethering_setting(tethering_h th)
512 char *passphrase = NULL;
513 bool visibility = false;
514 tethering_wifi_security_type_e security_type = TETHERING_WIFI_SECURITY_TYPE_NONE;
516 int error = TETHERING_ERROR_NONE;
518 error = tethering_wifi_get_ssid(th, &ssid);
519 if (error != TETHERING_ERROR_NONE)
522 g_print("\n\t** WiFi tethering SSID : %s\n", ssid);
524 error = tethering_wifi_get_passphrase(th, &passphrase);
525 if (error != TETHERING_ERROR_NONE)
528 g_print("\t** WiFi tethering passphrase : %s\n", passphrase);
530 error = tethering_wifi_get_ssid_visibility(th, &visibility);
531 if (error != TETHERING_ERROR_NONE)
534 g_print("\t** WiFi tethering ssid visibility : %s\n",
535 visibility ? "visible" : "invisible");
537 error = tethering_wifi_get_security_type(th, &security_type);
538 if (error != TETHERING_ERROR_NONE)
541 g_print("\t** WiFi tethering security_type : %s\n",
543 TETHERING_WIFI_SECURITY_TYPE_NONE ?
544 "open" : "wpa2-psk");
554 static void __print_wifi_ap_setting(tethering_h th)
557 char *passphrase = NULL;
558 bool visibility = false;
559 tethering_wifi_security_type_e security_type = TETHERING_WIFI_SECURITY_TYPE_NONE;
561 int error = TETHERING_ERROR_NONE;
563 error = tethering_wifi_ap_get_ssid(th, &ssid);
564 if (error != TETHERING_ERROR_NONE)
567 g_print("\n\t** WiFi AP SSID : %s\n", ssid);
569 error = tethering_wifi_ap_get_passphrase(th, &passphrase);
570 if (error != TETHERING_ERROR_NONE)
573 g_print("\t** WiFi AP passphrase : %s\n", passphrase);
575 error = tethering_wifi_ap_get_ssid_visibility(th, &visibility);
576 if (error != TETHERING_ERROR_NONE)
579 g_print("\t** WiFi AP ssid visibility : %s\n",
580 visibility ? "visible" : "invisible");
582 error = tethering_wifi_ap_get_security_type(th, &security_type);
583 if (error != TETHERING_ERROR_NONE)
586 g_print("\t** WiFi AP security_type : %s\n",
588 TETHERING_WIFI_SECURITY_TYPE_NONE ?
589 "open" : "wpa2-psk");
599 void print_menu(void)
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> ");
622 gboolean input(GIOChannel *channel, GIOCondition condition, gpointer data)
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, };
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};
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");
645 ios = g_io_channel_read_chars(channel, buf, INPUT_BUF_LEN, &read, &err);
647 g_print("g_io_channel_read_chars is failed : %s\n",
651 } else if (ios != G_IO_STATUS_NORMAL) {
652 g_print("g_io_channel_read_chars is failed : %d\n", ios);
661 param = strrchr(buf, ' ');
664 if (!strcmp(cmd, "quit")) {
665 g_main_loop_quit(mainloop);
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",
685 if (!strcmp(cmd, "get") && !strcmp(param, "wifi_setting")) {
686 __print_wifi_tethering_setting(th);
690 if (!strcmp(cmd, "get") && !strcmp(param, "wifi_ap_setting")) {
691 __print_wifi_ap_setting(th);
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",
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",
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",
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",
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",
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",
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",
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",
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",
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",
776 if (!strcmp(cmd, "do handle_creation_test")) {
780 g_print("testing %d times....\n", count);
783 g_print("Destroying tethering %dth time\n", i);
785 __deregister_cbs(th);
787 error = tethering_destroy(th);
789 if (__is_err(error) == true)
793 g_print("Creating tethering %dth time\n", i);
794 error = tethering_create(&th);
795 if (__is_err(error) == true)
797 __register_cbs(th, &cbs, NULL);
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;
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",
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);
839 int main(int argc, char *argv[])
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};
849 #if !GLIB_CHECK_VERSION(2, 36, 0)
853 /* Create tethering handle */
854 ret = tethering_create(&th);
855 if (__is_err(ret) == true)
859 __register_cbs(th, &cbs, NULL);
861 stdin_channel = g_io_channel_unix_new(0);
862 if (stdin_channel == NULL)
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);
869 g_io_add_watch(stdin_channel, G_IO_IN, input, (gpointer)th);
873 mainloop = g_main_loop_new(NULL, 0);
875 g_main_loop_run(mainloop);
876 g_main_loop_unref(mainloop);
879 __deregister_cbs(th);
881 /* Destroy tethering handle */
882 ret = tethering_destroy(th);