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 DISABLE_REASON_TEXT_LEN 64
32 #define COMMON_STR_BUF_LEN 32
34 gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data);
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;
45 tethering_h th = NULL;
47 static bool __is_err(tethering_error_e ret)
52 case TETHERING_ERROR_INVALID_PARAMETER:
53 err_msg = "Wrong parameter is used";
56 case TETHERING_ERROR_OUT_OF_MEMORY:
57 err_msg = "Memory is not enough";
60 case TETHERING_ERROR_NONE:
63 case TETHERING_ERROR_NOT_ENABLED:
64 err_msg = "Tethering is not enabled";
67 case TETHERING_ERROR_OPERATION_FAILED:
68 err_msg = "Operation is failed";
71 case TETHERING_ERROR_RESOURCE_BUSY:
72 err_msg = "Resource is busy";
75 case TETHERING_ERROR_NOT_PERMITTED:
76 err_msg = "Operation is not permitted";
80 err_msg = "This should not be happened";
84 g_print("##ERR: %s\n", err_msg);
89 static const char *__convert_tethering_type_to_str(const tethering_type_e type)
91 static char str_buf[COMMON_STR_BUF_LEN] = {0, };
94 case TETHERING_TYPE_USB:
95 g_strlcpy(str_buf, "USB Tethering", sizeof(str_buf));
98 case TETHERING_TYPE_WIFI:
99 g_strlcpy(str_buf, "Wi-Fi Tethering", sizeof(str_buf));
102 case TETHERING_TYPE_BT:
103 g_strlcpy(str_buf, "Bluetooth Tethering", sizeof(str_buf));
107 g_strlcpy(str_buf, "Unknown", sizeof(str_buf));
114 static const char *__convert_disabled_code_to_str(const tethering_disabled_cause_e code)
116 static char str_buf[DISABLE_REASON_TEXT_LEN] = {0, };
119 case TETHERING_DISABLED_BY_USB_DISCONNECTION:
120 strncpy(str_buf, "disabled due to usb disconnection", sizeof(str_buf));
123 case TETHERING_DISABLED_BY_FLIGHT_MODE:
124 strncpy(str_buf, "disabled due to flight mode on", sizeof(str_buf));
127 case TETHERING_DISABLED_BY_LOW_BATTERY:
128 strncpy(str_buf, "disabled due to low battery", sizeof(str_buf));
131 case TETHERING_DISABLED_BY_NETWORK_CLOSE:
132 strncpy(str_buf, "disabled due to pdp network close", sizeof(str_buf));
135 case TETHERING_DISABLED_BY_TIMEOUT:
136 strncpy(str_buf, "disabled due to timeout", sizeof(str_buf));
139 case TETHERING_DISABLED_BY_OTHERS:
140 strncpy(str_buf, "disabled by other apps", sizeof(str_buf));
143 case TETHERING_DISABLED_BY_REQUEST:
144 strncpy(str_buf, "disabled by my request", sizeof(str_buf));
147 case TETHERING_DISABLED_BY_WIFI_ON:
148 strncpy(str_buf, "disabled by Wi-Fi station on", sizeof(str_buf));
151 case TETHERING_DISABLED_BY_BT_OFF:
152 strncpy(str_buf, "disabled by bluetooth off", sizeof(str_buf));
156 strncpy(str_buf, "disabled by unknown reason", sizeof(str_buf));
163 static void __register_cbs(tethering_h th, __tethering_cbs *cbs, void *user_data)
165 tethering_error_e ret = TETHERING_ERROR_NONE;
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");
172 ret = tethering_set_disabled_cb(th, TETHERING_TYPE_ALL,
173 cbs->disabled_cb, user_data);
174 if (__is_err(ret) == true)
175 g_print("tethering_set_disabled_cb is failed\n");
177 ret = tethering_set_connection_state_changed_cb(th, TETHERING_TYPE_ALL,
178 cbs->changed_cb, user_data);
179 if (__is_err(ret) == true)
180 g_print("tethering_set_connection_state_changed_cb is failed\n");
182 ret = tethering_wifi_set_security_type_changed_cb(th,
183 cbs->security_type_changed_cb, user_data);
184 if (__is_err(ret) == true)
185 g_print("tethering_wifi_set_security_type_changed_cb is failed\n");
187 ret = tethering_wifi_set_ssid_visibility_changed_cb(th,
188 cbs->ssid_visibility_changed_cb, user_data);
189 if (__is_err(ret) == true)
190 g_print("tethering_wifi_set_ssid_visibility_changed_cb is failed\n");
192 ret = tethering_wifi_set_passphrase_changed_cb(th,
193 cbs->passphrase_changed_cb, user_data);
194 if (__is_err(ret) == true)
195 g_print("tethering_wifi_set_passphrase_changed_cb is failed\n");
200 static void __deregister_cbs(tethering_h th)
202 tethering_error_e ret = TETHERING_ERROR_NONE;
204 ret = tethering_unset_enabled_cb(th, TETHERING_TYPE_ALL);
205 if (__is_err(ret) == true)
206 g_print("tethering_unset_enabled_cb is failed\n");
208 ret = tethering_unset_disabled_cb(th, TETHERING_TYPE_ALL);
209 if (__is_err(ret) == true)
210 g_print("tethering_unset_disabled_cb is failed\n");
212 ret = tethering_unset_connection_state_changed_cb(th, TETHERING_TYPE_ALL);
213 if (__is_err(ret) == true)
214 g_print("tethering_unset_connection_state_changed_cb is failed\n");
216 ret = tethering_wifi_unset_security_type_changed_cb(th);
217 if (__is_err(ret) == true)
218 g_print("tethering_wifi_unset_security_type_changed_cb is failed\n");
220 ret = tethering_wifi_unset_ssid_visibility_changed_cb(th);
221 if (__is_err(ret) == true)
222 g_print("tethering_wifi_unset_ssid_visibility_changed_cb is failed\n");
224 ret = tethering_wifi_unset_passphrase_changed_cb(th);
225 if (__is_err(ret) == true)
226 g_print("tethering_wifi_unset_passphrase_changed_cb is failed\n");
231 /* Tethering callbacks */
232 static void __enabled_cb(tethering_error_e error, tethering_type_e type, bool is_requested, void *data)
234 if (__is_err(error)) {
238 g_print("## %s is not enabled. error code[0x%X]\n",
239 __convert_tethering_type_to_str(type),
245 g_print("## %s is enabled successfully\n",
246 __convert_tethering_type_to_str(type));
248 g_print("## %s is enabled by other app\n",
249 __convert_tethering_type_to_str(type));
254 static void __disabled_cb(tethering_error_e error, tethering_type_e type, tethering_disabled_cause_e code, void *data)
256 if (__is_err(error)) {
257 if (code != TETHERING_DISABLED_BY_REQUEST)
260 g_print("## %s is not disabled. error code[0x%X]\n",
261 __convert_tethering_type_to_str(type), error);
265 g_print("## %s is %s\n",
266 __convert_tethering_type_to_str(type),
267 __convert_disabled_code_to_str(code));
272 static void __connection_state_changed_cb(tethering_client_h client, bool open, void *data)
274 tethering_client_h clone = NULL;
275 tethering_type_e type;
276 char *ip_address = NULL;
277 char *mac_address = NULL;
278 char *hostname = NULL;
280 tethering_client_clone(&clone, client);
282 g_print("tetheirng_client_clone is failed\n");
286 tethering_client_get_tethering_type(clone, &type);
287 tethering_client_get_ip_address(clone,
288 TETHERING_ADDRESS_FAMILY_IPV4, &ip_address);
289 tethering_client_get_mac_address(clone, &mac_address);
290 tethering_client_get_name(clone, &hostname);
293 g_print("## New station Type [%s], IP [%s], MAC [%s], hostname [%s]\n",
294 __convert_tethering_type_to_str(type),
295 ip_address, mac_address, hostname);
297 g_print("## Disconnected station Type [%s], IP [%s], MAC [%s], hostname [%s]\n",
298 __convert_tethering_type_to_str(type),
299 ip_address, mac_address, hostname);
309 tethering_client_destroy(clone);
314 static void __data_usage_cb(tethering_error_e result, unsigned long long received_data,
315 unsigned long long sent_data, void *user_data)
317 g_print("__data_usage_cb\n");
319 if (result != TETHERING_ERROR_NONE) {
320 g_print("tethering_get_data_usage is failed. error[0x%X]\n", result);
324 g_print("## Received data : %llu bytes\n", received_data);
325 g_print("## Sent data : %llu bytes\n", sent_data);
330 static void __settings_reloaded_cb(tethering_error_e result, void *user_data)
332 g_print("__settings_reloaded_cb\n");
334 if (result != TETHERING_ERROR_NONE) {
335 g_print("tethering_wifi_reload_settings is failed. error[0x%X]\n", result);
339 g_print("## Wi-Fi tethering setting is reloaded\n");
344 static bool __clients_foreach_cb(tethering_client_h client, void *data)
346 tethering_client_h clone = NULL;
347 tethering_type_e type;
348 char *ip_address = NULL;
349 char *mac_address = NULL;
350 char *hostname = NULL;
352 /* Clone internal information */
353 if (tethering_client_clone(&clone, client) != TETHERING_ERROR_NONE) {
354 g_print("tethering_client_clone is failed\n");
358 /* Get information */
359 if (tethering_client_get_tethering_type(clone, &type) != TETHERING_ERROR_NONE)
360 g_print("tethering_client_get_type is failed\n");
362 if (tethering_client_get_ip_address(clone, TETHERING_ADDRESS_FAMILY_IPV4, &ip_address) != TETHERING_ERROR_NONE)
363 g_print("tethering_client_get_ip_address is failed\n");
365 if (tethering_client_get_mac_address(clone, &mac_address) != TETHERING_ERROR_NONE)
366 g_print("tethering_client_get_mac_address is failed\n");
368 if (tethering_client_get_name(clone, &hostname) != TETHERING_ERROR_NONE)
369 g_print("tethering_client_get_hostname is failed\n");
371 /* End of getting information */
373 g_print("\n< Client Info. >\n");
374 g_print("\tType %s\n", __convert_tethering_type_to_str(type));
375 g_print("\tIP Address %s\n", ip_address);
376 g_print("\tMAC Address : %s\n", mac_address);
377 g_print("\tHostname : %s\n", hostname);
379 /* Destroy cloned objects */
387 tethering_client_destroy(clone);
389 /* Continue iteration */
393 static void __security_type_changed_cb(tethering_wifi_security_type_e changed_type, void *user_data)
396 char *sec_str = NULL;
398 switch (changed_type) {
399 case TETHERING_WIFI_SECURITY_TYPE_NONE:
402 case TETHERING_WIFI_SECURITY_TYPE_WPA2_PSK:
403 sec_str = "wpa2-psk";
405 case TETHERING_WIFI_SECURITY_TYPE_WPS:
412 g_print("Wi-Fi Tethering Security type is changed to [%s]\n", sec_str);
417 static void __ssid_visibility_changed_cb(bool changed_visible, void *user_data)
419 g_print("SSID visibility for Wi-Fi tethering changed to [%s]\n",
420 changed_visible ? "visible" : "invisible");
424 static void __passphrase_changed_cb(void *user_data)
426 g_print("Wi-Fi Tethering passphrase is changed\n");
429 /* End of tethering callbacks */
431 static void __print_interface_info(tethering_h th, tethering_type_e type)
433 char *interface = NULL;
434 char *mac_address = NULL;
435 char *ip_address = NULL;
436 char *ip6_address = NULL;
437 char *gateway_address = NULL;
438 char *gateway6_address = NULL;
439 char *subnet_mask = NULL;
442 if (tethering_is_enabled(th, type) == FALSE) {
443 g_print("%s is not enabled\n",
444 __convert_tethering_type_to_str(type));
448 tethering_get_network_interface_name(th, type, &interface);
449 tethering_get_mac_address(th, type, &mac_address);
450 tethering_get_ip_address(th, type, TETHERING_ADDRESS_FAMILY_IPV4,
452 tethering_get_ip_address(th, type, TETHERING_ADDRESS_FAMILY_IPV6,
454 tethering_get_gateway_address(th, type, TETHERING_ADDRESS_FAMILY_IPV4,
456 tethering_get_gateway_address(th, type, TETHERING_ADDRESS_FAMILY_IPV6,
458 tethering_get_subnet_mask(th, type, TETHERING_ADDRESS_FAMILY_IPV4,
460 tethering_get_subnet_mask(th, type, TETHERING_ADDRESS_FAMILY_IPV6,
463 g_print("interface name : %s\n", interface);
464 g_print("mac address : %s\n", mac_address);
465 g_print("IPv4 address : %s\n", ip_address);
466 g_print("IPv6 address : %s\n", ip6_address);
467 g_print("gateway address: %s\n", gateway_address);
468 g_print("IPv6 gateway address: %s\n", gateway6_address);
469 g_print("subnet mask : %s\n", subnet_mask);
470 g_print("IPv6 prefix : %s\n", prefix);
481 free(gateway_address);
484 if (gateway6_address)
485 free(gateway6_address);
492 static void __print_wifi_tethering_setting(tethering_h th)
495 char *passphrase = NULL;
496 char *sec_str = NULL;
497 bool visibility = false;
499 bool forwarding_enabled = false;
500 bool filtering_enabled = false;
502 int max_connected = 0;
503 tethering_wifi_security_type_e security_type = TETHERING_WIFI_SECURITY_TYPE_NONE;
504 tethering_wifi_mode_type_e hw_mode = TETHERING_WIFI_MODE_TYPE_G;
506 int error = TETHERING_ERROR_NONE;
508 error = tethering_wifi_get_ssid(th, &ssid);
509 if (error != TETHERING_ERROR_NONE)
512 g_print("\n\t** WiFi tethering SSID : %s\n", ssid);
514 error = tethering_wifi_get_passphrase(th, &passphrase);
515 if (error != TETHERING_ERROR_NONE)
518 g_print("\t** WiFi tethering passphrase : %s\n", passphrase);
520 error = tethering_wifi_get_ssid_visibility(th, &visibility);
521 if (error != TETHERING_ERROR_NONE)
524 g_print("\t** WiFi tethering ssid visibility : %s\n",
525 visibility ? "visible" : "invisible");
527 error = tethering_wifi_get_security_type(th, &security_type);
528 if (error != TETHERING_ERROR_NONE)
531 switch (security_type) {
532 case TETHERING_WIFI_SECURITY_TYPE_NONE:
535 case TETHERING_WIFI_SECURITY_TYPE_WPA2_PSK:
536 sec_str = "wpa2-psk";
538 case TETHERING_WIFI_SECURITY_TYPE_WPS:
545 g_print("\t** WiFi tethering security_type : %s\n", sec_str);
548 error = tethering_wifi_get_mode(th, &hw_mode);
549 if (error != TETHERING_ERROR_NONE)
552 g_print("\t** WiFi tethering mode : %d\n", hw_mode);
554 error = tethering_wifi_get_channel(th, &channel);
555 if (error != TETHERING_ERROR_NONE)
558 g_print("\t** WiFi tethering channel : %d\n", channel);
560 error = tethering_wifi_get_max_connected_device(th, &max_connected);
561 if (error != TETHERING_ERROR_NONE)
564 g_print("\t** WiFi tethering max connected device : %d\n", max_connected);
566 error = tethering_wifi_get_mac_filter(th, &mac_filter);
567 if (error != TETHERING_ERROR_NONE)
570 g_print("\t** WiFi tethering mac filter : %s\n",
571 mac_filter ? "enable" : "disable");
573 error = tethering_wifi_is_port_filtering_enabled(th, &filtering_enabled);
574 if (error != TETHERING_ERROR_NONE)
577 g_print("\t** WiFi tethering port filtering : %s\n",
578 filtering_enabled ? "enable" : "disable");
580 error = tethering_wifi_is_port_forwarding_enabled(th, &forwarding_enabled);
581 if (error != TETHERING_ERROR_NONE)
584 g_print("\t** WiFi tethering port forwarding : %s\n",
585 forwarding_enabled ? "enable" : "disable");
595 void __display_list(GSList *list)
597 GSList *iterator = NULL;
599 for (iterator = list; iterator; iterator = iterator->next)
600 printf("%s\n", (char*)iterator->data);
603 bool __get_tethering_type(tethering_type_e *type)
608 printf("Select tethering type (1:Wi-Fi, 2:BT, 3:USB 4:ALL)\n");
609 ret = scanf("%9d", &sel);
611 printf("scanf is failed!!\n");
617 *type = TETHERING_TYPE_WIFI;
620 *type = TETHERING_TYPE_BT;
623 *type = TETHERING_TYPE_USB;
626 *type = TETHERING_TYPE_ALL;
629 printf("Invalid input!!\n");
636 static int test_tethering_create(void)
638 int ret = tethering_create(&th);
639 __tethering_cbs cbs = {
640 __enabled_cb, __disabled_cb,
641 __connection_state_changed_cb, __security_type_changed_cb,
642 __ssid_visibility_changed_cb, __passphrase_changed_cb};
644 if (__is_err(ret) == false) __register_cbs(th, &cbs, NULL);
646 printf("Tethering create is failed\n");
649 printf("Tethering create and register callback success\n");
654 static int test_tethering_destroy(void)
656 int ret = TETHERING_ERROR_NONE;
658 __deregister_cbs(th);
660 ret = tethering_destroy(th);
661 if (__is_err(ret) == true) {
662 printf("Tethering destroy is failed\n");
669 static int test_tethering_enable(void)
671 int ret = TETHERING_ERROR_NONE;
672 tethering_type_e type;
673 int address_type = 0;
675 printf("IPv4: 0, IPv6: 1\n");
676 ret = scanf("%d", &address_type);
678 if (!__get_tethering_type(&type))
682 ret = tethering_ipv6_enable(th, type);
684 ret = tethering_enable(th, type);
686 if (__is_err(ret) == true) {
687 printf("Fail to enable tethering\n");
693 static int test_tethering_disable(void)
695 int ret = TETHERING_ERROR_NONE;
696 tethering_type_e type;
697 int address_type = 0;
699 printf("IPv4: 0, IPv6: 1\n");
700 ret = scanf("%d", &address_type);
702 if (!__get_tethering_type(&type))
706 ret = tethering_ipv6_disable(th, type);
708 ret = tethering_disable(th, type);
710 if (__is_err(ret) == true) {
711 printf("Fail to disable tethering\n");
717 static int test_tethering_get_client_info(void)
720 tethering_type_e type;
722 if (!__get_tethering_type(&type))
725 ret = tethering_foreach_connected_clients(th, type,
726 __clients_foreach_cb, NULL);
727 if (__is_err(ret) == true) {
728 printf("Fail to disable tethering\n");
735 static int test_tethering_get_interface_info(void)
737 tethering_type_e type;
739 if (!__get_tethering_type(&type))
742 __print_interface_info(th, type);
747 static int test_tethering_get_data_usage(void)
749 int ret = tethering_get_data_usage(th, __data_usage_cb, NULL);
751 if (__is_err(ret) == true) {
752 printf("Fail to get data usage!!\n");
759 static int test_tethering_wifi_get_setting(void)
761 __print_wifi_tethering_setting(th);
765 static int test_tethering_wifi_set_ssid(void)
770 printf("Input SSID for Wi-Fi tethering: ");
771 ret = scanf("%99s", ssid);
773 printf("scanf is failed!!\n");
777 ret = tethering_wifi_set_ssid(th, ssid);
778 if (__is_err(ret) == true) {
779 printf("Fail to set wifi ssid!!\n");
786 static int test_tethering_wifi_set_security_type(void)
791 printf("Input security type for Wi-Fi tethering (0:NONE, 1:WPA2_PSK, 2:WPS)");
792 ret = scanf("%9d", &security_type);
794 printf("scanf is failed!!\n");
798 ret = tethering_wifi_set_security_type(th, security_type);
799 if (__is_err(ret) == true) {
800 printf("Fail to set security type!!\n");
807 int test_tethering_wifi_set_visibility(void)
812 printf("Input security type for Wi-Fi tethering (0:invisible, 1:visible)");
813 ret = scanf("%9d", &visibility);
815 printf("scanf is failed!!\n");
819 ret = tethering_wifi_set_ssid_visibility(th, visibility);
820 if (__is_err(ret) == true) {
821 printf("Fail to set visibility!!\n");
828 static int test_tethering_wifi_set_passphrase(void)
831 char passphrase[100];
833 printf("Input passphrase for Wi-Fi tethering: ");
834 ret = scanf("%99s", passphrase);
836 printf("scanf is failed!!\n");
840 ret = tethering_wifi_set_passphrase(th, passphrase);
841 if (__is_err(ret) == true) {
842 printf("Fail to set passphrase!!\n");
849 static int test_tethering_wifi_set_channel(void)
854 printf("Input channel for Wi-Fi tethering: ");
855 ret = scanf("%d", &channel);
857 ret = tethering_wifi_set_channel(th, channel);
858 if (__is_err(ret) == true) {
859 printf("Fail to set channel!!\n");
866 static int test_tethering_wifi_set_mode(void)
871 printf("Input hw_mode for Wi-Fi tethering(0-b, 1-g, 2-a, 3-ad): ");
872 ret = scanf("%d", &type);
874 ret = tethering_wifi_set_mode(th, type);
875 if (__is_err(ret) == true) {
876 printf("Fail to set mode!!\n");
883 static int test_tethering_wifi_enable_dhcp(void)
888 printf("Input (0-Disable, 1-Enable): ");
889 ret = scanf("%d", &enable);
891 ret = tethering_wifi_enable_dhcp(th, enable);
892 if (__is_err(ret) == true) {
893 printf("Fail to enable dhcp server!!\n");
900 static int test_tethering_wifi_set_dhcp_range(void)
903 char rangestart[16], rangestop[16];
905 printf("Input range (ex: 192.168.0.50 192.168.0.150): ");
907 ret = scanf("%15s %15s", rangestart, rangestop);
909 ret = tethering_wifi_set_dhcp_range(th, rangestart, rangestop);
910 if (__is_err(ret) == true) {
911 printf("Fail to set dhcp range and enable dhcp server!!\n");
918 static int test_tethering_wifi_is_dhcp_enabled(void)
923 ret = tethering_wifi_is_dhcp_enabled(th, &enabled);
925 if (__is_err(ret) == true) {
926 printf("Fail to get dhcp server status!!\n");
929 printf("DHCP server is %s\n", enabled ? "enabled" : "disabled");
935 static int test_tethering_wifi_set_mac_filtering(void)
940 printf("Input mac filtering option (0: disable, 1: enable): ");
941 ret = scanf("%d", &enable);
943 ret = tethering_wifi_set_mac_filter(th, enable);
944 if (__is_err(ret) == true) {
945 printf("Fail to set mac filtering!!\n");
952 static int test_tethering_manage_mac_list(void)
958 printf("Select MAC list to modify (0: allowed mac list, 1: blocked mac list): ");
959 ret = scanf("%d", &list);
961 printf("Select option (0: Add, 1: Remove): ");
962 ret = scanf("%d", &option);
964 printf("Input MAC Address to add/remove allowed/blocked mac list: ");
965 ret = scanf("%99s", mac);
967 printf("scanf is failed!!\n");
971 if (!list && !option) {
972 /* Add to allowed mac list*/
973 ret = tethering_wifi_add_allowed_mac_list(th, mac);
974 } else if (!list && option) {
975 /* Remove from allowed mac list */
976 ret = tethering_wifi_remove_allowed_mac_list(th, mac);
977 } else if (list && !option) {
978 /* Add to blocked mac list */
979 ret = tethering_wifi_add_blocked_mac_list(th, mac);
980 } else if (list && option) {
981 /* Remove from blocked mac list */
982 ret = tethering_wifi_remove_blocked_mac_list(th, mac);
984 printf("Input failed!!\n");
994 static int test_tethering_get_mac_list(void)
998 void *mac_list = NULL;
1000 printf("Select MAC list to get (0: allowed mac list, 1: blocked mac list): ");
1001 ret = scanf("%d", &list);
1005 ret = tethering_wifi_get_allowed_mac_list(th, &mac_list);
1008 ret = tethering_wifi_get_blocked_mac_list(th, &mac_list);
1011 printf("Input failed!!\n");
1018 __display_list(mac_list);
1023 static int test_tethering_wifi_reload_settings(void)
1025 int ret = tethering_wifi_reload_settings(th, __settings_reloaded_cb, NULL);
1027 if (__is_err(ret) == true) {
1028 printf("Fail to reload wifi tethering!!\n");
1034 static int test_tethering_wifi_get_txpower(void)
1036 int ret = TETHERING_ERROR_NONE;
1038 unsigned int txpower = 0;
1039 ret = tethering_wifi_get_txpower(th, &txpower);
1040 if (__is_err(ret) == true) {
1041 printf("Fail to get txpower!!\n");
1044 g_print("tethering_hostapd_get_txpower received [%d]\n", txpower);
1048 static int test_tethering_wifi_set_txpower(void)
1051 unsigned int txpower = 0;
1053 printf("Input tx power for Wi-Fi tethering: ");
1054 ret = scanf("%d", &txpower);
1056 ret = tethering_wifi_set_txpower(th, txpower);
1057 if (__is_err(ret) == true) {
1058 printf("Fail to set txpower!!\n");
1065 static int test_tethering_wifi_set_mtu(void)
1068 unsigned int mtu = 0;
1070 printf("Input mtu for Wi-Fi tethering: ");
1071 ret = scanf("%d", &mtu);
1073 ret = tethering_wifi_set_mtu(th, mtu);
1074 if (__is_err(ret) == true) {
1075 printf("Fail to set mtu!!\n");
1082 static int test_tethering_wifi_change_mac(void)
1087 printf("Input mac address: ");
1088 ret = scanf("%17s", mac);
1090 ret = tethering_wifi_change_mac(th, mac);
1091 if (__is_err(ret) == true) {
1092 printf("Fail to change mac!\n");
1099 static int test_tethering_wifi_set_max_connected_device(void)
1104 printf("Input max connected device: ");
1105 ret = scanf("%d", &max_connected);
1107 ret = tethering_wifi_set_max_connected_device(th, max_connected);
1108 if (__is_err(ret) == true) {
1109 printf("Fail to set max connected device!\n");
1117 static int test_tethering_wifi_enable_port_forwarding(void)
1122 printf("Wi-Fi tethring port forwarding(0:disable 1:enable): ");
1123 ret = scanf("%d", &enable);
1125 ret = tethering_wifi_enable_port_forwarding(th, enable);
1126 if (__is_err(ret) == true) {
1127 printf("Fail to enable port forwarding!\n");
1134 static int test_tethering_wifi_add_port_forwarding_rule(void)
1141 int org_port, final_port;
1143 printf("Input ifname, protocol, original ip/port, final ip/port: ");
1144 ret = scanf("%19s", ifname);
1145 ret = scanf("%19s", proto);
1146 ret = scanf("%15s", org_ip);
1147 ret = scanf("%d", &org_port);
1148 ret = scanf("%15s", final_ip);
1149 ret = scanf("%d", &final_port);
1151 ret = tethering_wifi_add_port_forwarding_rule(th, ifname, proto, org_ip, org_port, final_ip, final_port);
1152 if (__is_err(ret) == true) {
1153 printf("Fail to add port forwarding rule!\n");
1160 static int test_tethering_wifi_reset_port_forwarding_rule(void)
1164 ret = tethering_wifi_reset_port_forwarding_rule(th);
1165 if (__is_err(ret) == true) {
1166 printf("Fail to reset port forwarding rule!\n");
1173 static int test_tethering_wifi_get_port_forwarding_rule(void)
1176 void *pf_list = NULL;
1178 ret = tethering_wifi_get_port_forwarding_rule(th, &pf_list);
1179 if (__is_err(ret) == true) {
1180 printf("Fail to get port forwarding rule!\n");
1184 __display_list(pf_list);
1189 static int test_tethering_wifi_enable_port_filtering(void)
1194 printf("Wi-Fi tethring port filtering(0:disable 1:enable): ");
1195 ret = scanf("%d", &enable);
1197 ret = tethering_wifi_enable_port_filtering(th, enable);
1198 if (__is_err(ret) == true) {
1199 printf("Fail to enable port filtering!\n");
1206 static int test_tethering_wifi_add_port_filtering_rule(void)
1213 printf("Input protocol, port, allow: ");
1214 ret = scanf("%19s", proto);
1215 ret = scanf("%d", &port);
1216 ret = scanf("%d", &allow);
1218 ret = tethering_wifi_add_port_filtering_rule(th, port, proto, allow);
1219 if (__is_err(ret) == true) {
1220 printf("Fail to add port forwarding rule!\n");
1227 static int test_tethering_wifi_add_custom_port_filtering_rule(void)
1234 printf("Input protocol, port1, port2, allow: ");
1235 ret = scanf("%19s", proto);
1236 ret = scanf("%d", &port1);
1237 ret = scanf("%d", &port2);
1238 ret = scanf("%d", &allow);
1240 ret = tethering_wifi_add_custom_port_filtering_rule(th, port1, port2, proto, allow);
1241 if (__is_err(ret) == true) {
1242 printf("Fail to add custom port forwarding rule!\n");
1249 static int test_tethering_wifi_get_port_filtering_rule(void)
1252 void *pf_list = NULL;
1254 ret = tethering_wifi_get_port_filtering_rule(th, &pf_list);
1255 if (__is_err(ret) == true) {
1256 printf("Fail to get port filtering rule!\n");
1260 __display_list(pf_list);
1265 static int test_tethering_wifi_get_custom_port_filtering_rule(void)
1268 void *pf_list = NULL;
1270 ret = tethering_wifi_get_custom_port_filtering_rule(th, &pf_list);
1271 if (__is_err(ret) == true) {
1272 printf("Fail to get port filtering rule!\n");
1276 __display_list(pf_list);
1281 static int test_tethering_wifi_set_vpn_passthrough_rule(void)
1286 printf("Select vpn passthrough type (0:IPSEC 1:PPTP 2:L2TP): ");
1287 ret = scanf("%d", &type);
1289 ret = tethering_wifi_set_vpn_passthrough_rule(th, (tethering_vpn_passthrough_type_e)type, true);
1290 if (__is_err(ret) == true) {
1291 printf("Fail to get port filtering rule!\n");
1298 static int test_tethering_wifi_push_wps_button(void)
1302 ret = tethering_wifi_push_wps_button(th);
1303 if (__is_err(ret) == true) {
1304 printf("Fail to get port filtering rule!\n");
1311 static int test_tethering_wifi_set_wps_pin(void)
1316 printf("Input WPS PIN: ");
1317 ret = scanf("%127s", wps_pin);
1319 ret = tethering_wifi_set_wps_pin(th, wps_pin);
1320 if (__is_err(ret) == true) {
1321 printf("Fail to get port filtering rule!\n");
1328 int main(int argc, char **argv)
1330 GMainLoop *mainloop;
1332 #if !GLIB_CHECK_VERSION(2, 36, 0)
1335 mainloop = g_main_loop_new(NULL, false);
1337 GIOChannel *channel = g_io_channel_unix_new(0);
1338 g_io_add_watch(channel, (G_IO_IN|G_IO_ERR|G_IO_HUP|G_IO_NVAL), test_thread, NULL);
1339 printf("Test Thread created...\n");
1340 g_main_loop_run(mainloop);
1345 gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data)
1350 printf("Event received from stdin\n");
1352 rv = read(0, a, 10);
1354 if (rv <= 0 || a[0] == '0')
1357 if (a[0] == '\n' || a[0] == '\r') {
1358 printf("\n\n Network Connection API Test App\n\n");
1359 printf("Options..\n");
1360 printf("1 - Tethering create and set callbacks\n");
1361 printf("2 - Tethering destroy\n");
1362 printf("3 - Enable Tethering\n");
1363 printf("4 - Disable Tethering\n");
1364 printf("5 - Get client information\n");
1365 printf("6 - Get interface information\n");
1366 printf("7 - Get data usage\n");
1367 printf("8 - Get Wi-Fi tethering setting\n");
1368 printf("a - Set Wi-Fi tethering SSID\n");
1369 printf("b - Set Wi-Fi tethering security type\n");
1370 printf("c - Set Wi-Fi tethering visibility\n");
1371 printf("d - Set Wi-Fi tethering passphrase\n");
1372 printf("e - Set Wi-Fi tethering mac filtering\n");
1373 printf("f - Add/Remove MAC adress to/from allowed/blocked list\n");
1374 printf("g - Get allowed/blocked list\n");
1375 printf("k - Reload Wi-Fi tethering\n");
1376 printf("m - Set Wi-Fi channel\n");
1377 printf("n - Set Wi-Fi hw_mode\n");
1378 printf("o - Enable dhcp server\n");
1379 printf("p - Enable dhcp server with range\n");
1380 printf("q - Is dhcp server enabled?\n");
1381 printf("r - Get Wi-Fi txpower\n");
1382 printf("s - Set Wi-Fi txpower\n");
1383 printf("t - Set Wi-Fi mtu\n");
1384 printf("u - Change mac address\n");
1385 printf("v - Set max connected device(Wi-Fi tethering)\n");
1386 printf("w - Enable port forwarding\n");
1387 printf("x - Add port forwarding rule\n");
1388 printf("y - Reset port forwarding rule\n");
1389 printf("z - Get port forwarding rule\n");
1390 printf("A - Enable port filtering\n");
1391 printf("B - Add port filtering rule\n");
1392 printf("C - Add custom port filtering rule\n");
1393 printf("D - Get port filtering rule\n");
1394 printf("E - Get custom port filtering rule\n");
1395 printf("F - Set vpn passthrough rule\n");
1396 printf("G - Push WPS button\n");
1397 printf("H - Set WPS PIN\n");
1399 printf("ENTER - Show options menu.......\n");
1404 rv = test_tethering_create();
1407 rv = test_tethering_destroy();
1410 rv = test_tethering_enable();
1413 rv = test_tethering_disable();
1416 rv = test_tethering_get_client_info();
1419 rv = test_tethering_get_interface_info();
1422 rv = test_tethering_get_data_usage();
1425 rv = test_tethering_wifi_get_setting();
1428 rv = test_tethering_wifi_set_ssid();
1431 rv = test_tethering_wifi_set_security_type();
1434 rv = test_tethering_wifi_set_visibility();
1437 rv = test_tethering_wifi_set_passphrase();
1440 rv = test_tethering_wifi_set_mac_filtering();
1443 rv = test_tethering_manage_mac_list();
1446 rv = test_tethering_get_mac_list();
1449 rv = test_tethering_wifi_reload_settings();
1452 rv = test_tethering_wifi_set_channel();
1455 rv = test_tethering_wifi_set_mode();
1458 rv = test_tethering_wifi_enable_dhcp();
1461 rv = test_tethering_wifi_set_dhcp_range();
1464 rv = test_tethering_wifi_is_dhcp_enabled();
1467 rv = test_tethering_wifi_get_txpower();
1470 rv = test_tethering_wifi_set_txpower();
1473 rv = test_tethering_wifi_set_mtu();
1476 rv = test_tethering_wifi_change_mac();
1479 rv = test_tethering_wifi_set_max_connected_device();
1482 rv = test_tethering_wifi_enable_port_forwarding();
1485 rv = test_tethering_wifi_add_port_forwarding_rule();
1488 rv = test_tethering_wifi_reset_port_forwarding_rule();
1491 rv = test_tethering_wifi_get_port_forwarding_rule();
1494 rv = test_tethering_wifi_enable_port_filtering();
1497 rv = test_tethering_wifi_add_port_filtering_rule();
1500 rv = test_tethering_wifi_add_custom_port_filtering_rule();
1503 rv = test_tethering_wifi_get_port_filtering_rule();
1506 rv = test_tethering_wifi_get_custom_port_filtering_rule();
1509 rv = test_tethering_wifi_set_vpn_passthrough_rule();
1512 rv = test_tethering_wifi_push_wps_button();
1515 rv = test_tethering_wifi_set_wps_pin();
1520 printf("Operation succeeded!\n");
1522 printf("Operation failed!\n");