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";
79 case TETHERING_ERROR_NOT_SUPPORT_API:
80 err_msg = "Not supported";
84 err_msg = "This should not be happened";
88 g_print("##ERR: %s\n", err_msg);
93 static bool test_get_user_string(const char *msg, char *buf, int buf_size)
95 if (msg == NULL || buf == NULL || buf_size < 2)
100 memset(buf, 0, buf_size);
101 rv = read(0, buf, buf_size - 1);
103 if (rv < 0 || buf[0] == '\0' || buf[0] == '\n' || buf[0] == '\r') {
112 static const char *__convert_tethering_type_to_str(const tethering_type_e type)
114 static char str_buf[COMMON_STR_BUF_LEN] = {0, };
117 case TETHERING_TYPE_USB:
118 g_strlcpy(str_buf, "USB Tethering", sizeof(str_buf));
121 case TETHERING_TYPE_WIFI:
122 g_strlcpy(str_buf, "Wi-Fi Tethering", sizeof(str_buf));
125 case TETHERING_TYPE_BT:
126 g_strlcpy(str_buf, "Bluetooth Tethering", sizeof(str_buf));
129 case TETHERING_TYPE_P2P:
130 g_strlcpy(str_buf, "P2P Tethering", sizeof(str_buf));
134 g_strlcpy(str_buf, "Unknown", sizeof(str_buf));
141 static const char *__convert_disabled_code_to_str(const tethering_disabled_cause_e code)
143 static char str_buf[DISABLE_REASON_TEXT_LEN] = {0, };
146 case TETHERING_DISABLED_BY_USB_DISCONNECTION:
147 strncpy(str_buf, "disabled due to usb disconnection", sizeof(str_buf));
150 case TETHERING_DISABLED_BY_FLIGHT_MODE:
151 strncpy(str_buf, "disabled due to flight mode on", sizeof(str_buf));
154 case TETHERING_DISABLED_BY_LOW_BATTERY:
155 strncpy(str_buf, "disabled due to low battery", sizeof(str_buf));
158 case TETHERING_DISABLED_BY_NETWORK_CLOSE:
159 strncpy(str_buf, "disabled due to pdp network close", sizeof(str_buf));
162 case TETHERING_DISABLED_BY_TIMEOUT:
163 strncpy(str_buf, "disabled due to timeout", sizeof(str_buf));
166 case TETHERING_DISABLED_BY_OTHERS:
167 strncpy(str_buf, "disabled by other apps", sizeof(str_buf));
170 case TETHERING_DISABLED_BY_REQUEST:
171 strncpy(str_buf, "disabled by my request", sizeof(str_buf));
174 case TETHERING_DISABLED_BY_WIFI_ON:
175 strncpy(str_buf, "disabled by Wi-Fi station on", sizeof(str_buf));
178 case TETHERING_DISABLED_BY_BT_OFF:
179 strncpy(str_buf, "disabled by bluetooth off", sizeof(str_buf));
183 strncpy(str_buf, "disabled by unknown reason", sizeof(str_buf));
190 static void __register_cbs(tethering_h th, __tethering_cbs *cbs, void *user_data)
192 tethering_error_e ret = TETHERING_ERROR_NONE;
194 ret = tethering_set_enabled_cb(th, TETHERING_TYPE_ALL,
195 cbs->enabled_cb, user_data);
196 if (__is_err(ret) == true)
197 g_print("tethering_set_enabled_cb is failed\n");
199 ret = tethering_set_disabled_cb(th, TETHERING_TYPE_ALL,
200 cbs->disabled_cb, user_data);
201 if (__is_err(ret) == true)
202 g_print("tethering_set_disabled_cb is failed\n");
204 ret = tethering_set_connection_state_changed_cb(th, TETHERING_TYPE_ALL,
205 cbs->changed_cb, user_data);
206 if (__is_err(ret) == true)
207 g_print("tethering_set_connection_state_changed_cb is failed\n");
209 ret = tethering_wifi_set_security_type_changed_cb(th,
210 cbs->security_type_changed_cb, user_data);
211 if (__is_err(ret) == true)
212 g_print("tethering_wifi_set_security_type_changed_cb is failed\n");
214 ret = tethering_wifi_set_ssid_visibility_changed_cb(th,
215 cbs->ssid_visibility_changed_cb, user_data);
216 if (__is_err(ret) == true)
217 g_print("tethering_wifi_set_ssid_visibility_changed_cb is failed\n");
219 ret = tethering_wifi_set_passphrase_changed_cb(th,
220 cbs->passphrase_changed_cb, user_data);
221 if (__is_err(ret) == true)
222 g_print("tethering_wifi_set_passphrase_changed_cb is failed\n");
227 static void __deregister_cbs(tethering_h th)
229 tethering_error_e ret = TETHERING_ERROR_NONE;
231 ret = tethering_unset_enabled_cb(th, TETHERING_TYPE_ALL);
232 if (__is_err(ret) == true)
233 g_print("tethering_unset_enabled_cb is failed\n");
235 ret = tethering_unset_disabled_cb(th, TETHERING_TYPE_ALL);
236 if (__is_err(ret) == true)
237 g_print("tethering_unset_disabled_cb is failed\n");
239 ret = tethering_unset_connection_state_changed_cb(th, TETHERING_TYPE_ALL);
240 if (__is_err(ret) == true)
241 g_print("tethering_unset_connection_state_changed_cb is failed\n");
243 ret = tethering_wifi_unset_security_type_changed_cb(th);
244 if (__is_err(ret) == true)
245 g_print("tethering_wifi_unset_security_type_changed_cb is failed\n");
247 ret = tethering_wifi_unset_ssid_visibility_changed_cb(th);
248 if (__is_err(ret) == true)
249 g_print("tethering_wifi_unset_ssid_visibility_changed_cb is failed\n");
251 ret = tethering_wifi_unset_passphrase_changed_cb(th);
252 if (__is_err(ret) == true)
253 g_print("tethering_wifi_unset_passphrase_changed_cb is failed\n");
258 /* Tethering callbacks */
259 static void __enabled_cb(tethering_error_e error, tethering_type_e type, bool is_requested, void *data)
261 if (__is_err(error)) {
265 g_print("## %s is not enabled. error code[0x%X]\n",
266 __convert_tethering_type_to_str(type),
272 g_print("## %s is enabled successfully\n",
273 __convert_tethering_type_to_str(type));
275 g_print("## %s is enabled by other app\n",
276 __convert_tethering_type_to_str(type));
281 static void __disabled_cb(tethering_error_e error, tethering_type_e type, tethering_disabled_cause_e code, void *data)
283 if (__is_err(error)) {
284 if (code != TETHERING_DISABLED_BY_REQUEST)
287 g_print("## %s is not disabled. error code[0x%X]\n",
288 __convert_tethering_type_to_str(type), error);
292 g_print("## %s is %s\n",
293 __convert_tethering_type_to_str(type),
294 __convert_disabled_code_to_str(code));
299 static void __connection_state_changed_cb(tethering_client_h client, bool open, void *data)
301 tethering_client_h clone = NULL;
302 tethering_type_e type;
303 char *ip_address = NULL;
304 char *mac_address = NULL;
305 char *hostname = NULL;
307 tethering_client_clone(&clone, client);
309 g_print("tetheirng_client_clone is failed\n");
313 tethering_client_get_tethering_type(clone, &type);
314 tethering_client_get_ip_address(clone,
315 TETHERING_ADDRESS_FAMILY_IPV4, &ip_address);
316 tethering_client_get_mac_address(clone, &mac_address);
317 tethering_client_get_name(clone, &hostname);
320 g_print("## New station Type [%s], IP [%s], MAC [%s], hostname [%s]\n",
321 __convert_tethering_type_to_str(type),
322 ip_address, mac_address, hostname);
324 g_print("## Disconnected station Type [%s], IP [%s], MAC [%s], hostname [%s]\n",
325 __convert_tethering_type_to_str(type),
326 ip_address, mac_address, hostname);
336 tethering_client_destroy(clone);
341 static void __data_usage_cb(tethering_error_e result, unsigned long long received_data,
342 unsigned long long sent_data, void *user_data)
344 g_print("__data_usage_cb\n");
346 if (result != TETHERING_ERROR_NONE) {
347 g_print("tethering_get_data_usage is failed. error[0x%X]\n", result);
351 g_print("## Received data : %llu bytes\n", received_data);
352 g_print("## Sent data : %llu bytes\n", sent_data);
357 static void __settings_reloaded_cb(tethering_error_e result, void *user_data)
359 g_print("__settings_reloaded_cb\n");
361 if (result != TETHERING_ERROR_NONE) {
362 g_print("tethering_wifi_reload_settings is failed. error[0x%X]\n", result);
366 g_print("## Wi-Fi tethering setting is reloaded\n");
371 static bool __clients_foreach_cb(tethering_client_h client, void *data)
373 tethering_client_h clone = NULL;
374 tethering_type_e type;
375 char *ip_address = NULL;
376 char *mac_address = NULL;
377 char *hostname = NULL;
379 /* Clone internal information */
380 if (tethering_client_clone(&clone, client) != TETHERING_ERROR_NONE) {
381 g_print("tethering_client_clone is failed\n");
385 /* Get information */
386 if (tethering_client_get_tethering_type(clone, &type) != TETHERING_ERROR_NONE)
387 g_print("tethering_client_get_type is failed\n");
389 if (tethering_client_get_ip_address(clone, TETHERING_ADDRESS_FAMILY_IPV4, &ip_address) != TETHERING_ERROR_NONE)
390 g_print("tethering_client_get_ip_address is failed\n");
392 if (tethering_client_get_mac_address(clone, &mac_address) != TETHERING_ERROR_NONE)
393 g_print("tethering_client_get_mac_address is failed\n");
395 if (tethering_client_get_name(clone, &hostname) != TETHERING_ERROR_NONE)
396 g_print("tethering_client_get_hostname is failed\n");
398 /* End of getting information */
400 g_print("\n< Client Info. >\n");
401 g_print("\tType %s\n", __convert_tethering_type_to_str(type));
402 g_print("\tIP Address %s\n", ip_address);
403 g_print("\tMAC Address : %s\n", mac_address);
404 g_print("\tHostname : %s\n", hostname);
406 /* Destroy cloned objects */
414 tethering_client_destroy(clone);
416 /* Continue iteration */
420 static void __security_type_changed_cb(tethering_wifi_security_type_e changed_type, void *user_data)
423 char *sec_str = NULL;
425 switch (changed_type) {
426 case TETHERING_WIFI_SECURITY_TYPE_NONE:
429 case TETHERING_WIFI_SECURITY_TYPE_WPA2_PSK:
430 sec_str = "wpa2-psk";
432 case TETHERING_WIFI_SECURITY_TYPE_WPS:
435 case TETHERING_WIFI_SECURITY_TYPE_SAE:
442 g_print("Wi-Fi Tethering Security type is changed to [%s]\n", sec_str);
447 static void __ssid_visibility_changed_cb(bool changed_visible, void *user_data)
449 g_print("SSID visibility for Wi-Fi tethering changed to [%s]\n",
450 changed_visible ? "visible" : "invisible");
454 static void __passphrase_changed_cb(void *user_data)
456 g_print("Wi-Fi Tethering passphrase is changed\n");
459 /* End of tethering callbacks */
461 static void __print_interface_info(tethering_h th, tethering_type_e type)
463 char *interface = NULL;
464 char *mac_address = NULL;
465 char *ip_address = NULL;
466 char *ip6_address = NULL;
467 char *gateway_address = NULL;
468 char *gateway6_address = NULL;
469 char *subnet_mask = NULL;
472 if (tethering_is_enabled(th, type) == FALSE) {
473 g_print("%s is not enabled\n",
474 __convert_tethering_type_to_str(type));
478 tethering_get_network_interface_name(th, type, &interface);
479 tethering_get_mac_address(th, type, &mac_address);
480 tethering_get_ip_address(th, type, TETHERING_ADDRESS_FAMILY_IPV4,
482 tethering_get_ip_address(th, type, TETHERING_ADDRESS_FAMILY_IPV6,
484 tethering_get_gateway_address(th, type, TETHERING_ADDRESS_FAMILY_IPV4,
486 tethering_get_gateway_address(th, type, TETHERING_ADDRESS_FAMILY_IPV6,
488 tethering_get_subnet_mask(th, type, TETHERING_ADDRESS_FAMILY_IPV4,
490 tethering_get_subnet_mask(th, type, TETHERING_ADDRESS_FAMILY_IPV6,
493 g_print("interface name : %s\n", interface);
494 g_print("mac address : %s\n", mac_address);
495 g_print("IPv4 address : %s\n", ip_address);
496 g_print("IPv6 address : %s\n", ip6_address);
497 g_print("gateway address: %s\n", gateway_address);
498 g_print("IPv6 gateway address: %s\n", gateway6_address);
499 g_print("subnet mask : %s\n", subnet_mask);
500 g_print("IPv6 prefix : %s\n", prefix);
511 free(gateway_address);
514 if (gateway6_address)
515 free(gateway6_address);
522 static void __print_wifi_tethering_setting(tethering_h th)
525 char *passphrase = NULL;
526 char *sec_str = NULL;
527 bool visibility = false;
529 bool forwarding_enabled = false;
530 bool filtering_enabled = false;
532 int max_connected = 0;
533 tethering_wifi_security_type_e security_type = TETHERING_WIFI_SECURITY_TYPE_NONE;
534 tethering_wifi_mode_type_e hw_mode = TETHERING_WIFI_MODE_TYPE_G;
536 int error = TETHERING_ERROR_NONE;
538 error = tethering_wifi_get_ssid(th, &ssid);
539 if (error == TETHERING_ERROR_NONE)
540 g_print("\n\t** WiFi tethering SSID : %s\n", ssid);
542 error = tethering_wifi_get_passphrase(th, &passphrase);
543 if (error == TETHERING_ERROR_NONE)
544 g_print("\t** WiFi tethering passphrase : %s\n", passphrase);
546 error = tethering_wifi_get_ssid_visibility(th, &visibility);
547 if (error == TETHERING_ERROR_NONE)
548 g_print("\t** WiFi tethering ssid visibility : %s\n",
549 visibility ? "visible" : "invisible");
551 error = tethering_wifi_get_security_type(th, &security_type);
552 if (error == TETHERING_ERROR_NONE) {
553 switch (security_type) {
554 case TETHERING_WIFI_SECURITY_TYPE_NONE:
557 case TETHERING_WIFI_SECURITY_TYPE_WPA2_PSK:
558 sec_str = "wpa2-psk";
560 case TETHERING_WIFI_SECURITY_TYPE_WPS:
563 case TETHERING_WIFI_SECURITY_TYPE_SAE:
570 g_print("\t** WiFi tethering security_type : %s\n", sec_str);
573 error = tethering_wifi_get_mode(th, &hw_mode);
574 if (error == TETHERING_ERROR_NONE)
575 g_print("\t** WiFi tethering mode : %d\n", hw_mode);
577 error = tethering_wifi_get_channel(th, &channel);
578 if (error == TETHERING_ERROR_NONE)
579 g_print("\t** WiFi tethering channel : %d\n", channel);
581 error = tethering_wifi_get_max_connected_device(th, &max_connected);
582 if (error == TETHERING_ERROR_NONE)
583 g_print("\t** WiFi tethering max connected device : %d\n", max_connected);
585 error = tethering_wifi_get_mac_filter(th, &mac_filter);
586 if (error == TETHERING_ERROR_NONE)
587 g_print("\t** WiFi tethering mac filter : %s\n",
588 mac_filter ? "enable" : "disable");
590 error = tethering_wifi_is_port_filtering_enabled(th, &filtering_enabled);
591 if (error == TETHERING_ERROR_NONE)
592 g_print("\t** WiFi tethering port filtering : %s\n",
593 filtering_enabled ? "enable" : "disable");
595 error = tethering_wifi_is_port_forwarding_enabled(th, &forwarding_enabled);
596 if (error == TETHERING_ERROR_NONE)
597 g_print("\t** WiFi tethering port forwarding : %s\n",
598 forwarding_enabled ? "enable" : "disable");
608 void __display_list(GSList *list)
610 GSList *iterator = NULL;
612 for (iterator = list; iterator; iterator = iterator->next)
613 printf("%s\n", (char*)iterator->data);
616 bool __get_tethering_type(tethering_type_e *type)
621 printf("Select tethering type (1:Wi-Fi, 2:BT, 3:USB 4:P2P 5:ALL)\n");
622 ret = scanf("%9d", &sel);
624 printf("scanf is failed!!\n");
630 *type = TETHERING_TYPE_WIFI;
633 *type = TETHERING_TYPE_BT;
636 *type = TETHERING_TYPE_USB;
639 *type = TETHERING_TYPE_P2P;
642 *type = TETHERING_TYPE_ALL;
645 printf("Invalid input!!\n");
652 static int test_tethering_create(void)
654 int ret = tethering_create(&th);
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};
660 if (__is_err(ret) == false) __register_cbs(th, &cbs, NULL);
662 printf("Tethering create is failed\n");
665 printf("Tethering create and register callback success\n");
670 static int test_tethering_destroy(void)
672 int ret = TETHERING_ERROR_NONE;
674 __deregister_cbs(th);
676 ret = tethering_destroy(th);
677 if (__is_err(ret) == true) {
678 printf("Tethering destroy is failed\n");
685 static int test_tethering_enable(void)
687 int ret = TETHERING_ERROR_NONE;
688 tethering_type_e type;
689 int address_type = 0;
691 printf("IPv4: 0, IPv6: 1\n");
692 ret = scanf("%d", &address_type);
694 if (!__get_tethering_type(&type))
698 ret = tethering_ipv6_enable(th, type);
700 ret = tethering_enable(th, type);
702 if (__is_err(ret) == true) {
703 printf("Fail to enable tethering\n");
709 static int test_tethering_disable(void)
711 int ret = TETHERING_ERROR_NONE;
712 tethering_type_e type;
713 int address_type = 0;
715 printf("IPv4: 0, IPv6: 1\n");
716 ret = scanf("%d", &address_type);
718 if (!__get_tethering_type(&type))
722 ret = tethering_ipv6_disable(th, type);
724 ret = tethering_disable(th, type);
726 if (__is_err(ret) == true) {
727 printf("Fail to disable tethering\n");
733 static int test_tethering_get_client_info(void)
736 tethering_type_e type;
738 if (!__get_tethering_type(&type))
741 ret = tethering_foreach_connected_clients(th, type,
742 __clients_foreach_cb, NULL);
743 if (__is_err(ret) == true) {
744 printf("Fail to disable tethering\n");
751 static int test_tethering_get_interface_info(void)
753 tethering_type_e type;
755 if (!__get_tethering_type(&type))
758 __print_interface_info(th, type);
763 static int test_tethering_get_data_usage(void)
765 int ret = tethering_get_data_usage(th, __data_usage_cb, NULL);
767 if (__is_err(ret) == true) {
768 printf("Fail to get data usage!!\n");
775 static int test_tethering_wifi_get_setting(void)
777 __print_wifi_tethering_setting(th);
781 static int test_tethering_wifi_set_ssid(void)
784 char ssid[100] = {0, };
786 if (test_get_user_string("Input SSID for Wi-Fi tethering:",
787 ssid, 100) == false) {
788 printf("Failed to read user input!!\n");
792 ret = tethering_wifi_set_ssid(th, ssid);
793 if (__is_err(ret) == true) {
794 printf("Fail to set wifi ssid!!\n");
801 static int test_tethering_wifi_set_security_type(void)
806 printf("Input security type for Wi-Fi tethering (0:NONE, 1:WPA2_PSK, 2:WPS, 3:SAE)");
807 ret = scanf("%9d", &security_type);
809 printf("scanf is failed!!\n");
813 ret = tethering_wifi_set_security_type(th, security_type);
814 if (__is_err(ret) == true) {
815 printf("Fail to set security type!!\n");
822 int test_tethering_wifi_set_visibility(void)
827 printf("Input security type for Wi-Fi tethering (0:invisible, 1:visible)");
828 ret = scanf("%9d", &visibility);
830 printf("scanf is failed!!\n");
834 ret = tethering_wifi_set_ssid_visibility(th, visibility);
835 if (__is_err(ret) == true) {
836 printf("Fail to set visibility!!\n");
843 static int test_tethering_wifi_set_passphrase(void)
846 char passphrase[100] = {0, };
848 if (test_get_user_string("Input passphrase for Wi-Fi tethering:",
849 passphrase, 100) == false) {
850 printf("Failed to read user input!!\n");
854 ret = tethering_wifi_set_passphrase(th, passphrase);
855 if (__is_err(ret) == true) {
856 printf("Fail to set passphrase!!\n");
863 static int test_tethering_wifi_set_channel(void)
868 printf("Input channel for Wi-Fi tethering: ");
869 ret = scanf("%d", &channel);
871 ret = tethering_wifi_set_channel(th, channel);
872 if (__is_err(ret) == true) {
873 printf("Fail to set channel!!\n");
880 static int test_tethering_wifi_set_mode(void)
885 printf("Input hw_mode for Wi-Fi tethering(0-b, 1-g, 2-a, 3-ad): ");
886 ret = scanf("%d", &type);
888 ret = tethering_wifi_set_mode(th, type);
889 if (__is_err(ret) == true) {
890 printf("Fail to set mode!!\n");
897 static int test_tethering_wifi_enable_dhcp(void)
902 printf("Input (0-Disable, 1-Enable): ");
903 ret = scanf("%d", &enable);
905 ret = tethering_wifi_enable_dhcp(th, enable);
906 if (__is_err(ret) == true) {
907 printf("Fail to enable dhcp server!!\n");
914 static int test_tethering_wifi_set_dhcp_range(void)
917 char rangestart[16], rangestop[16];
919 printf("Input range (ex: 192.168.0.50 192.168.0.150): ");
921 ret = scanf("%15s %15s", rangestart, rangestop);
923 ret = tethering_wifi_set_dhcp_range(th, rangestart, rangestop);
924 if (__is_err(ret) == true) {
925 printf("Fail to set dhcp range and enable dhcp server!!\n");
932 static int test_tethering_wifi_is_dhcp_enabled(void)
937 ret = tethering_wifi_is_dhcp_enabled(th, &enabled);
939 if (__is_err(ret) == true) {
940 printf("Fail to get dhcp server status!!\n");
943 printf("DHCP server is %s\n", enabled ? "enabled" : "disabled");
949 static int test_tethering_wifi_set_mac_filtering(void)
954 printf("Input mac filtering option (0: disable, 1: enable): ");
955 ret = scanf("%d", &enable);
957 ret = tethering_wifi_set_mac_filter(th, enable);
958 if (__is_err(ret) == true) {
959 printf("Fail to set mac filtering!!\n");
966 static int test_tethering_manage_mac_list(void)
972 printf("Select MAC list to modify (0: allowed mac list, 1: blocked mac list): ");
973 ret = scanf("%d", &list);
975 printf("Select option (0: Add, 1: Remove): ");
976 ret = scanf("%d", &option);
978 printf("Input MAC Address to add/remove allowed/blocked mac list: ");
979 ret = scanf("%99s", mac);
981 printf("scanf is failed!!\n");
985 if (!list && !option) {
986 /* Add to allowed mac list*/
987 ret = tethering_wifi_add_allowed_mac_list(th, mac);
988 } else if (!list && option) {
989 /* Remove from allowed mac list */
990 ret = tethering_wifi_remove_allowed_mac_list(th, mac);
991 } else if (list && !option) {
992 /* Add to blocked mac list */
993 ret = tethering_wifi_add_blocked_mac_list(th, mac);
995 /* Remove from blocked mac list */
996 ret = tethering_wifi_remove_blocked_mac_list(th, mac);
1005 static int test_tethering_get_mac_list(void)
1009 void *mac_list = NULL;
1011 printf("Select MAC list to get (0: allowed mac list, 1: blocked mac list): ");
1012 ret = scanf("%d", &list);
1016 ret = tethering_wifi_get_allowed_mac_list(th, &mac_list);
1019 ret = tethering_wifi_get_blocked_mac_list(th, &mac_list);
1022 printf("Input failed!!\n");
1029 __display_list(mac_list);
1034 static int test_tethering_wifi_reload_settings(void)
1036 int ret = tethering_wifi_reload_settings(th, __settings_reloaded_cb, NULL);
1038 if (__is_err(ret) == true) {
1039 printf("Fail to reload wifi tethering!!\n");
1045 static int test_tethering_wifi_get_txpower(void)
1047 int ret = TETHERING_ERROR_NONE;
1049 unsigned int txpower = 0;
1050 ret = tethering_wifi_get_txpower(th, &txpower);
1051 if (__is_err(ret) == true) {
1052 printf("Fail to get txpower!!\n");
1055 g_print("tethering_hostapd_get_txpower received [%d]\n", txpower);
1059 static int test_tethering_wifi_set_txpower(void)
1062 unsigned int txpower = 0;
1064 printf("Input tx power for Wi-Fi tethering: ");
1065 ret = scanf("%d", &txpower);
1067 ret = tethering_wifi_set_txpower(th, txpower);
1068 if (__is_err(ret) == true) {
1069 printf("Fail to set txpower!!\n");
1076 static int test_tethering_wifi_set_mtu(void)
1079 unsigned int mtu = 0;
1081 printf("Input mtu for Wi-Fi tethering: ");
1082 ret = scanf("%d", &mtu);
1084 ret = tethering_wifi_set_mtu(th, mtu);
1085 if (__is_err(ret) == true) {
1086 printf("Fail to set mtu!!\n");
1093 static int test_tethering_wifi_change_mac(void)
1098 printf("Input mac address: ");
1099 ret = scanf("%17s", mac);
1101 ret = tethering_wifi_change_mac(th, mac);
1102 if (__is_err(ret) == true) {
1103 printf("Fail to change mac!\n");
1110 static int test_tethering_wifi_set_max_connected_device(void)
1115 printf("Input max connected device: ");
1116 ret = scanf("%d", &max_connected);
1118 ret = tethering_wifi_set_max_connected_device(th, max_connected);
1119 if (__is_err(ret) == true) {
1120 printf("Fail to set max connected device!\n");
1128 static int test_tethering_wifi_enable_port_forwarding(void)
1133 printf("Wi-Fi tethring port forwarding(0:disable 1:enable): ");
1134 ret = scanf("%d", &enable);
1136 ret = tethering_wifi_enable_port_forwarding(th, enable);
1137 if (__is_err(ret) == true) {
1138 printf("Fail to enable port forwarding!\n");
1145 static int test_tethering_wifi_add_port_forwarding_rule(void)
1152 int org_port, final_port;
1154 printf("Input ifname, protocol, original ip/port, final ip/port: ");
1155 ret = scanf("%19s", ifname);
1156 ret = scanf("%19s", proto);
1157 ret = scanf("%15s", org_ip);
1158 ret = scanf("%d", &org_port);
1159 ret = scanf("%15s", final_ip);
1160 ret = scanf("%d", &final_port);
1162 ret = tethering_wifi_add_port_forwarding_rule(th, ifname, proto, org_ip, org_port, final_ip, final_port);
1163 if (__is_err(ret) == true) {
1164 printf("Fail to add port forwarding rule!\n");
1171 static int test_tethering_wifi_reset_port_forwarding_rule(void)
1175 ret = tethering_wifi_reset_port_forwarding_rule(th);
1176 if (__is_err(ret) == true) {
1177 printf("Fail to reset port forwarding rule!\n");
1184 static int test_tethering_wifi_get_port_forwarding_rule(void)
1187 void *pf_list = NULL;
1189 ret = tethering_wifi_get_port_forwarding_rule(th, &pf_list);
1190 if (__is_err(ret) == true) {
1191 printf("Fail to get port forwarding rule!\n");
1195 __display_list(pf_list);
1200 static int test_tethering_wifi_enable_port_filtering(void)
1205 printf("Wi-Fi tethring port filtering(0:disable 1:enable): ");
1206 ret = scanf("%d", &enable);
1208 ret = tethering_wifi_enable_port_filtering(th, enable);
1209 if (__is_err(ret) == true) {
1210 printf("Fail to enable port filtering!\n");
1217 static int test_tethering_wifi_add_port_filtering_rule(void)
1224 printf("Input protocol, port, allow: ");
1225 ret = scanf("%19s", proto);
1226 ret = scanf("%d", &port);
1227 ret = scanf("%d", &allow);
1229 ret = tethering_wifi_add_port_filtering_rule(th, port, proto, allow);
1230 if (__is_err(ret) == true) {
1231 printf("Fail to add port forwarding rule!\n");
1238 static int test_tethering_wifi_add_custom_port_filtering_rule(void)
1245 printf("Input protocol, port1, port2, allow: ");
1246 ret = scanf("%19s", proto);
1247 ret = scanf("%d", &port1);
1248 ret = scanf("%d", &port2);
1249 ret = scanf("%d", &allow);
1251 ret = tethering_wifi_add_custom_port_filtering_rule(th, port1, port2, proto, allow);
1252 if (__is_err(ret) == true) {
1253 printf("Fail to add custom port forwarding rule!\n");
1260 static int test_tethering_wifi_get_port_filtering_rule(void)
1263 void *pf_list = NULL;
1265 ret = tethering_wifi_get_port_filtering_rule(th, &pf_list);
1266 if (__is_err(ret) == true) {
1267 printf("Fail to get port filtering rule!\n");
1271 __display_list(pf_list);
1276 static int test_tethering_wifi_get_custom_port_filtering_rule(void)
1279 void *pf_list = NULL;
1281 ret = tethering_wifi_get_custom_port_filtering_rule(th, &pf_list);
1282 if (__is_err(ret) == true) {
1283 printf("Fail to get port filtering rule!\n");
1287 __display_list(pf_list);
1292 static int test_tethering_wifi_set_vpn_passthrough_rule(void)
1297 printf("Select vpn passthrough type (0:IPSEC 1:PPTP 2:L2TP): ");
1298 ret = scanf("%d", &type);
1300 ret = tethering_wifi_set_vpn_passthrough_rule(th, (tethering_vpn_passthrough_type_e)type, true);
1301 if (__is_err(ret) == true) {
1302 printf("Fail to get port filtering rule!\n");
1309 static int test_tethering_wifi_push_wps_button(void)
1313 ret = tethering_wifi_push_wps_button(th);
1314 if (__is_err(ret) == true) {
1315 printf("Fail to get port filtering rule!\n");
1322 static int test_tethering_wifi_set_wps_pin(void)
1325 char wps_pin[128] = {0, };
1327 if (test_get_user_string("Input WPS PIN: ",
1328 wps_pin, 128) == false) {
1329 printf("Failed to read user input!!\n");
1333 ret = tethering_wifi_set_wps_pin(th, wps_pin);
1334 if (__is_err(ret) == true) {
1335 printf("Fail to get port filtering rule!\n");
1342 int main(int argc, char **argv)
1344 GMainLoop *mainloop;
1346 #if !GLIB_CHECK_VERSION(2, 36, 0)
1349 mainloop = g_main_loop_new(NULL, false);
1351 GIOChannel *channel = g_io_channel_unix_new(0);
1352 g_io_add_watch(channel, (G_IO_IN|G_IO_ERR|G_IO_HUP|G_IO_NVAL), test_thread, NULL);
1353 printf("Test Thread created...\n");
1354 g_main_loop_run(mainloop);
1359 gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data)
1364 printf("Event received from stdin\n");
1366 rv = read(0, a, 10);
1368 if (rv <= 0 || a[0] == '0')
1371 if (a[0] == '\n' || a[0] == '\r') {
1372 printf("\n\n Network Connection API Test App\n\n");
1373 printf("Options..\n");
1374 printf("1 - Tethering create and set callbacks\n");
1375 printf("2 - Tethering destroy\n");
1376 printf("3 - Enable Tethering\n");
1377 printf("4 - Disable Tethering\n");
1378 printf("5 - Get client information\n");
1379 printf("6 - Get interface information\n");
1380 printf("7 - Get data usage\n");
1381 printf("8 - Get Wi-Fi tethering setting\n");
1382 printf("a - Set Wi-Fi tethering SSID\n");
1383 printf("b - Set Wi-Fi tethering security type\n");
1384 printf("c - Set Wi-Fi tethering visibility\n");
1385 printf("d - Set Wi-Fi tethering passphrase\n");
1386 printf("e - Set Wi-Fi tethering mac filtering\n");
1387 printf("f - Add/Remove MAC adress to/from allowed/blocked list\n");
1388 printf("g - Get allowed/blocked list\n");
1389 printf("k - Reload Wi-Fi tethering\n");
1390 printf("m - Set Wi-Fi channel\n");
1391 printf("n - Set Wi-Fi hw_mode\n");
1392 printf("o - Enable dhcp server\n");
1393 printf("p - Enable dhcp server with range\n");
1394 printf("q - Is dhcp server enabled?\n");
1395 printf("r - Get Wi-Fi txpower\n");
1396 printf("s - Set Wi-Fi txpower\n");
1397 printf("t - Set Wi-Fi mtu\n");
1398 printf("u - Change mac address\n");
1399 printf("v - Set max connected device(Wi-Fi tethering)\n");
1400 printf("w - Enable port forwarding\n");
1401 printf("x - Add port forwarding rule\n");
1402 printf("y - Reset port forwarding rule\n");
1403 printf("z - Get port forwarding rule\n");
1404 printf("A - Enable port filtering\n");
1405 printf("B - Add port filtering rule\n");
1406 printf("C - Add custom port filtering rule\n");
1407 printf("D - Get port filtering rule\n");
1408 printf("E - Get custom port filtering rule\n");
1409 printf("F - Set vpn passthrough rule\n");
1410 printf("G - Push WPS button\n");
1411 printf("H - Set WPS PIN\n");
1413 printf("ENTER - Show options menu.......\n");
1418 rv = test_tethering_create();
1421 rv = test_tethering_destroy();
1424 rv = test_tethering_enable();
1427 rv = test_tethering_disable();
1430 rv = test_tethering_get_client_info();
1433 rv = test_tethering_get_interface_info();
1436 rv = test_tethering_get_data_usage();
1439 rv = test_tethering_wifi_get_setting();
1442 rv = test_tethering_wifi_set_ssid();
1445 rv = test_tethering_wifi_set_security_type();
1448 rv = test_tethering_wifi_set_visibility();
1451 rv = test_tethering_wifi_set_passphrase();
1454 rv = test_tethering_wifi_set_mac_filtering();
1457 rv = test_tethering_manage_mac_list();
1460 rv = test_tethering_get_mac_list();
1463 rv = test_tethering_wifi_reload_settings();
1466 rv = test_tethering_wifi_set_channel();
1469 rv = test_tethering_wifi_set_mode();
1472 rv = test_tethering_wifi_enable_dhcp();
1475 rv = test_tethering_wifi_set_dhcp_range();
1478 rv = test_tethering_wifi_is_dhcp_enabled();
1481 rv = test_tethering_wifi_get_txpower();
1484 rv = test_tethering_wifi_set_txpower();
1487 rv = test_tethering_wifi_set_mtu();
1490 rv = test_tethering_wifi_change_mac();
1493 rv = test_tethering_wifi_set_max_connected_device();
1496 rv = test_tethering_wifi_enable_port_forwarding();
1499 rv = test_tethering_wifi_add_port_forwarding_rule();
1502 rv = test_tethering_wifi_reset_port_forwarding_rule();
1505 rv = test_tethering_wifi_get_port_forwarding_rule();
1508 rv = test_tethering_wifi_enable_port_filtering();
1511 rv = test_tethering_wifi_add_port_filtering_rule();
1514 rv = test_tethering_wifi_add_custom_port_filtering_rule();
1517 rv = test_tethering_wifi_get_port_filtering_rule();
1520 rv = test_tethering_wifi_get_custom_port_filtering_rule();
1523 rv = test_tethering_wifi_set_vpn_passthrough_rule();
1526 rv = test_tethering_wifi_push_wps_button();
1529 rv = test_tethering_wifi_set_wps_pin();
1534 printf("Operation succeeded!\n");
1536 printf("Operation failed!\n");