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";
76 err_msg = "This should not be happened";
80 g_print("##ERR: %s\n", err_msg);
85 static const char *__convert_tethering_type_to_str(const tethering_type_e type)
87 static char str_buf[COMMON_STR_BUF_LEN] = {0, };
90 case TETHERING_TYPE_USB:
91 g_strlcpy(str_buf, "USB Tethering", sizeof(str_buf));
94 case TETHERING_TYPE_WIFI:
95 g_strlcpy(str_buf, "Wi-Fi Tethering", sizeof(str_buf));
98 case TETHERING_TYPE_BT:
99 g_strlcpy(str_buf, "Bluetooth Tethering", sizeof(str_buf));
103 g_strlcpy(str_buf, "Unknown", sizeof(str_buf));
110 static const char *__convert_disabled_code_to_str(const tethering_disabled_cause_e code)
112 static char str_buf[DISABLE_REASON_TEXT_LEN] = {0, };
115 case TETHERING_DISABLED_BY_USB_DISCONNECTION:
116 strncpy(str_buf, "disabled due to usb disconnection", sizeof(str_buf));
119 case TETHERING_DISABLED_BY_FLIGHT_MODE:
120 strncpy(str_buf, "disabled due to flight mode on", sizeof(str_buf));
123 case TETHERING_DISABLED_BY_LOW_BATTERY:
124 strncpy(str_buf, "disabled due to low battery", sizeof(str_buf));
127 case TETHERING_DISABLED_BY_NETWORK_CLOSE:
128 strncpy(str_buf, "disabled due to pdp network close", sizeof(str_buf));
131 case TETHERING_DISABLED_BY_TIMEOUT:
132 strncpy(str_buf, "disabled due to timeout", sizeof(str_buf));
135 case TETHERING_DISABLED_BY_OTHERS:
136 strncpy(str_buf, "disabled by other apps", sizeof(str_buf));
139 case TETHERING_DISABLED_BY_REQUEST:
140 strncpy(str_buf, "disabled by my request", sizeof(str_buf));
143 case TETHERING_DISABLED_BY_WIFI_ON:
144 strncpy(str_buf, "disabled by Wi-Fi station on", sizeof(str_buf));
147 case TETHERING_DISABLED_BY_BT_OFF:
148 strncpy(str_buf, "disabled by bluetooth off", sizeof(str_buf));
152 strncpy(str_buf, "disabled by unknown reason", sizeof(str_buf));
159 static void __register_cbs(tethering_h th, __tethering_cbs *cbs, void *user_data)
161 tethering_error_e ret = TETHERING_ERROR_NONE;
163 ret = tethering_set_enabled_cb(th, TETHERING_TYPE_ALL,
164 cbs->enabled_cb, user_data);
165 if (__is_err(ret) == true)
166 g_print("tethering_set_enabled_cb is failed\n");
168 ret = tethering_set_disabled_cb(th, TETHERING_TYPE_ALL,
169 cbs->disabled_cb, user_data);
170 if (__is_err(ret) == true)
171 g_print("tethering_set_disabled_cb is failed\n");
173 ret = tethering_set_connection_state_changed_cb(th, TETHERING_TYPE_ALL,
174 cbs->changed_cb, user_data);
175 if (__is_err(ret) == true)
176 g_print("tethering_set_connection_state_changed_cb is failed\n");
178 ret = tethering_wifi_set_security_type_changed_cb(th,
179 cbs->security_type_changed_cb, user_data);
180 if (__is_err(ret) == true)
181 g_print("tethering_wifi_set_security_type_changed_cb is failed\n");
183 ret = tethering_wifi_set_ssid_visibility_changed_cb(th,
184 cbs->ssid_visibility_changed_cb, user_data);
185 if (__is_err(ret) == true)
186 g_print("tethering_wifi_set_ssid_visibility_changed_cb is failed\n");
188 ret = tethering_wifi_set_passphrase_changed_cb(th,
189 cbs->passphrase_changed_cb, user_data);
190 if (__is_err(ret) == true)
191 g_print("tethering_wifi_set_passphrase_changed_cb is failed\n");
196 static void __deregister_cbs(tethering_h th)
198 tethering_error_e ret = TETHERING_ERROR_NONE;
200 ret = tethering_unset_enabled_cb(th, TETHERING_TYPE_ALL);
201 if (__is_err(ret) == true)
202 g_print("tethering_unset_enabled_cb is failed\n");
204 ret = tethering_unset_disabled_cb(th, TETHERING_TYPE_ALL);
205 if (__is_err(ret) == true)
206 g_print("tethering_unset_disabled_cb is failed\n");
208 ret = tethering_unset_connection_state_changed_cb(th, TETHERING_TYPE_ALL);
209 if (__is_err(ret) == true)
210 g_print("tethering_unset_connection_state_changed_cb is failed\n");
212 ret = tethering_wifi_unset_security_type_changed_cb(th);
213 if (__is_err(ret) == true)
214 g_print("tethering_wifi_unset_security_type_changed_cb is failed\n");
216 ret = tethering_wifi_unset_ssid_visibility_changed_cb(th);
217 if (__is_err(ret) == true)
218 g_print("tethering_wifi_unset_ssid_visibility_changed_cb is failed\n");
220 ret = tethering_wifi_unset_passphrase_changed_cb(th);
221 if (__is_err(ret) == true)
222 g_print("tethering_wifi_unset_passphrase_changed_cb is failed\n");
227 /* Tethering callbacks */
228 static void __enabled_cb(tethering_error_e error, tethering_type_e type, bool is_requested, void *data)
230 if (__is_err(error)) {
234 g_print("## %s is not enabled. error code[0x%X]\n",
235 __convert_tethering_type_to_str(type),
241 g_print("## %s is enabled successfully\n",
242 __convert_tethering_type_to_str(type));
244 g_print("## %s is enabled by other app\n",
245 __convert_tethering_type_to_str(type));
250 static void __disabled_cb(tethering_error_e error, tethering_type_e type, tethering_disabled_cause_e code, void *data)
252 if (__is_err(error)) {
253 if (code != TETHERING_DISABLED_BY_REQUEST)
256 g_print("## %s is not disabled. error code[0x%X]\n",
257 __convert_tethering_type_to_str(type), error);
261 g_print("## %s is %s\n",
262 __convert_tethering_type_to_str(type),
263 __convert_disabled_code_to_str(code));
268 static void __connection_state_changed_cb(tethering_client_h client, bool open, void *data)
270 tethering_client_h clone = NULL;
271 tethering_type_e type;
272 char *ip_address = NULL;
273 char *mac_address = NULL;
274 char *hostname = NULL;
276 tethering_client_clone(&clone, client);
278 g_print("tetheirng_client_clone is failed\n");
282 tethering_client_get_tethering_type(clone, &type);
283 tethering_client_get_ip_address(clone,
284 TETHERING_ADDRESS_FAMILY_IPV4, &ip_address);
285 tethering_client_get_mac_address(clone, &mac_address);
286 tethering_client_get_name(clone, &hostname);
289 g_print("## New station Type [%s], IP [%s], MAC [%s], hostname [%s]\n",
290 __convert_tethering_type_to_str(type),
291 ip_address, mac_address, hostname);
293 g_print("## Disconnected station Type [%s], IP [%s], MAC [%s], hostname [%s]\n",
294 __convert_tethering_type_to_str(type),
295 ip_address, mac_address, hostname);
305 tethering_client_destroy(clone);
310 static void __data_usage_cb(tethering_error_e result, unsigned long long received_data,
311 unsigned long long sent_data, void *user_data)
313 g_print("__data_usage_cb\n");
315 if (result != TETHERING_ERROR_NONE) {
316 g_print("tethering_get_data_usage is failed. error[0x%X]\n", result);
320 g_print("## Received data : %llu bytes\n", received_data);
321 g_print("## Sent data : %llu bytes\n", sent_data);
326 static void __settings_reloaded_cb(tethering_error_e result, void *user_data)
328 g_print("__settings_reloaded_cb\n");
330 if (result != TETHERING_ERROR_NONE) {
331 g_print("tethering_wifi_reload_settings is failed. error[0x%X]\n", result);
335 g_print("## Wi-Fi tethering setting is reloaded\n");
340 static bool __clients_foreach_cb(tethering_client_h client, void *data)
342 tethering_client_h clone = NULL;
343 tethering_type_e type;
344 char *ip_address = NULL;
345 char *mac_address = NULL;
346 char *hostname = NULL;
348 /* Clone internal information */
349 if (tethering_client_clone(&clone, client) != TETHERING_ERROR_NONE) {
350 g_print("tethering_client_clone is failed\n");
354 /* Get information */
355 if (tethering_client_get_tethering_type(clone, &type) != TETHERING_ERROR_NONE)
356 g_print("tethering_client_get_type is failed\n");
358 if (tethering_client_get_ip_address(clone, TETHERING_ADDRESS_FAMILY_IPV4, &ip_address) != TETHERING_ERROR_NONE)
359 g_print("tethering_client_get_ip_address is failed\n");
361 if (tethering_client_get_mac_address(clone, &mac_address) != TETHERING_ERROR_NONE)
362 g_print("tethering_client_get_mac_address is failed\n");
364 if (tethering_client_get_name(clone, &hostname) != TETHERING_ERROR_NONE)
365 g_print("tethering_client_get_hostname is failed\n");
367 /* End of getting information */
369 g_print("\n< Client Info. >\n");
370 g_print("\tType %s\n", __convert_tethering_type_to_str(type));
371 g_print("\tIP Address %s\n", ip_address);
372 g_print("\tMAC Address : %s\n", mac_address);
373 g_print("\tHostname : %s\n", hostname);
375 /* Destroy cloned objects */
383 tethering_client_destroy(clone);
385 /* Continue iteration */
389 static void __security_type_changed_cb(tethering_wifi_security_type_e changed_type, void *user_data)
392 char *sec_str = NULL;
394 switch (changed_type) {
395 case TETHERING_WIFI_SECURITY_TYPE_NONE:
398 case TETHERING_WIFI_SECURITY_TYPE_WPA2_PSK:
399 sec_str = "wpa2-psk";
401 case TETHERING_WIFI_SECURITY_TYPE_WPS:
408 g_print("Wi-Fi Tethering Security type is changed to [%s]\n", sec_str);
413 static void __ssid_visibility_changed_cb(bool changed_visible, void *user_data)
415 g_print("SSID visibility for Wi-Fi tethering changed to [%s]\n",
416 changed_visible ? "visible" : "invisible");
420 static void __passphrase_changed_cb(void *user_data)
422 g_print("Wi-Fi Tethering passphrase is changed\n");
425 /* End of tethering callbacks */
427 static void __print_interface_info(tethering_h th, tethering_type_e type)
429 char *interface = NULL;
430 char *mac_address = NULL;
431 char *ip_address = NULL;
432 char *gateway_address = NULL;
433 char *subnet_mask = NULL;
435 if (tethering_is_enabled(th, type) == FALSE) {
436 g_print("%s is not enabled\n",
437 __convert_tethering_type_to_str(type));
441 tethering_get_network_interface_name(th, type, &interface);
442 tethering_get_mac_address(th, type, &mac_address);
443 tethering_get_ip_address(th, type, TETHERING_ADDRESS_FAMILY_IPV4,
445 tethering_get_gateway_address(th, type, TETHERING_ADDRESS_FAMILY_IPV4,
447 tethering_get_subnet_mask(th, type, TETHERING_ADDRESS_FAMILY_IPV4,
450 g_print("interface name : %s\n", interface);
451 g_print("mac address : %s\n", mac_address);
452 g_print("ip address : %s\n", ip_address);
453 g_print("gateway address: %s\n", gateway_address);
454 g_print("subnet mask : %s\n", subnet_mask);
463 free(gateway_address);
470 static void __print_wifi_tethering_setting(tethering_h th)
473 char *passphrase = NULL;
474 char *sec_str = NULL;
475 bool visibility = false;
477 bool forwarding_enabled = false;
478 bool filtering_enabled = false;
480 int max_connected = 0;
481 tethering_wifi_security_type_e security_type = TETHERING_WIFI_SECURITY_TYPE_NONE;
482 tethering_wifi_mode_type_e hw_mode = TETHERING_WIFI_MODE_TYPE_G;
484 int error = TETHERING_ERROR_NONE;
486 error = tethering_wifi_get_ssid(th, &ssid);
487 if (error != TETHERING_ERROR_NONE)
490 g_print("\n\t** WiFi tethering SSID : %s\n", ssid);
492 error = tethering_wifi_get_passphrase(th, &passphrase);
493 if (error != TETHERING_ERROR_NONE)
496 g_print("\t** WiFi tethering passphrase : %s\n", passphrase);
498 error = tethering_wifi_get_ssid_visibility(th, &visibility);
499 if (error != TETHERING_ERROR_NONE)
502 g_print("\t** WiFi tethering ssid visibility : %s\n",
503 visibility ? "visible" : "invisible");
505 error = tethering_wifi_get_security_type(th, &security_type);
506 if (error != TETHERING_ERROR_NONE)
509 switch (security_type) {
510 case TETHERING_WIFI_SECURITY_TYPE_NONE:
513 case TETHERING_WIFI_SECURITY_TYPE_WPA2_PSK:
514 sec_str = "wpa2-psk";
516 case TETHERING_WIFI_SECURITY_TYPE_WPS:
523 g_print("\t** WiFi tethering security_type : %s\n", sec_str);
526 error = tethering_wifi_get_mode(th, &hw_mode);
527 if (error != TETHERING_ERROR_NONE)
530 g_print("\t** WiFi tethering mode : %d\n", hw_mode);
532 error = tethering_wifi_get_channel(th, &channel);
533 if (error != TETHERING_ERROR_NONE)
536 g_print("\t** WiFi tethering channel : %d\n", channel);
538 error = tethering_wifi_get_max_connected_device(th, &max_connected);
539 if (error != TETHERING_ERROR_NONE)
542 g_print("\t** WiFi tethering max connected device : %d\n", max_connected);
544 error = tethering_wifi_get_mac_filter(th, &mac_filter);
545 if (error != TETHERING_ERROR_NONE)
548 g_print("\t** WiFi tethering mac filter : %s\n",
549 mac_filter ? "enable" : "disable");
551 error = tethering_wifi_is_port_filtering_enabled(th, &filtering_enabled);
552 if (error != TETHERING_ERROR_NONE)
555 g_print("\t** WiFi tethering port filtering : %s\n",
556 filtering_enabled ? "enable" : "disable");
558 error = tethering_wifi_is_port_forwarding_enabled(th, &forwarding_enabled);
559 if (error != TETHERING_ERROR_NONE)
562 g_print("\t** WiFi tethering port forwarding : %s\n",
563 forwarding_enabled ? "enable" : "disable");
573 void __display_list(GSList *list)
575 GSList *iterator = NULL;
577 for (iterator = list; iterator; iterator = iterator->next)
578 printf("%s\n", (char*)iterator->data);
581 bool __get_tethering_type(tethering_type_e *type)
586 printf("Select tethering type (1:Wi-Fi, 2:BT, 3:USB 4:ALL)\n");
587 ret = scanf("%9d", &sel);
589 printf("scanf is failed!!\n");
595 *type = TETHERING_TYPE_WIFI;
598 *type = TETHERING_TYPE_BT;
601 *type = TETHERING_TYPE_USB;
604 *type = TETHERING_TYPE_ALL;
607 printf("Invalid input!!\n");
614 static int test_tethering_create(void)
616 int ret = tethering_create(&th);
617 __tethering_cbs cbs = {
618 __enabled_cb, __disabled_cb,
619 __connection_state_changed_cb, __security_type_changed_cb,
620 __ssid_visibility_changed_cb, __passphrase_changed_cb};
622 if (__is_err(ret) == false) __register_cbs(th, &cbs, NULL);
624 printf("Tethering create is failed\n");
627 printf("Tethering create and register callback success\n");
632 static int test_tethering_destroy(void)
634 int ret = TETHERING_ERROR_NONE;
636 __deregister_cbs(th);
638 ret = tethering_destroy(th);
639 if (__is_err(ret) == true) {
640 printf("Tethering destroy is failed\n");
647 static int test_tethering_enable(void)
649 int ret = TETHERING_ERROR_NONE;
650 tethering_type_e type;
652 if (!__get_tethering_type(&type))
655 ret = tethering_enable(th, type);
656 if (__is_err(ret) == true) {
657 printf("Fail to enable tethering\n");
663 static int test_tethering_disable(void)
665 int ret = TETHERING_ERROR_NONE;
666 tethering_type_e type;
668 if (!__get_tethering_type(&type))
671 ret = tethering_disable(th, type);
672 if (__is_err(ret) == true) {
673 printf("Fail to disable tethering\n");
679 static int test_tethering_get_client_info(void)
682 tethering_type_e type;
684 if (!__get_tethering_type(&type))
687 ret = tethering_foreach_connected_clients(th, type,
688 __clients_foreach_cb, NULL);
689 if (__is_err(ret) == true) {
690 printf("Fail to disable tethering\n");
697 static int test_tethering_get_interface_info(void)
699 tethering_type_e type;
701 if (!__get_tethering_type(&type))
704 __print_interface_info(th, type);
709 static int test_tethering_get_data_usage(void)
711 int ret = tethering_get_data_usage(th, __data_usage_cb, NULL);
713 if (__is_err(ret) == true) {
714 printf("Fail to get data usage!!\n");
721 static int test_tethering_wifi_get_setting(void)
723 __print_wifi_tethering_setting(th);
727 static int test_tethering_wifi_set_ssid(void)
732 printf("Input SSID for Wi-Fi tethering: ");
733 ret = scanf("%99s", ssid);
735 printf("scanf is failed!!\n");
739 ret = tethering_wifi_set_ssid(th, ssid);
740 if (__is_err(ret) == true) {
741 printf("Fail to set wifi ssid!!\n");
748 static int test_tethering_wifi_set_security_type(void)
753 printf("Input security type for Wi-Fi tethering (0:NONE, 1:WPA2_PSK, 2:WPS)");
754 ret = scanf("%9d", &security_type);
756 printf("scanf is failed!!\n");
760 ret = tethering_wifi_set_security_type(th, security_type);
761 if (__is_err(ret) == true) {
762 printf("Fail to set security type!!\n");
769 int test_tethering_wifi_set_visibility(void)
774 printf("Input security type for Wi-Fi tethering (0:invisible, 1:visible)");
775 ret = scanf("%9d", &visibility);
777 printf("scanf is failed!!\n");
781 ret = tethering_wifi_set_ssid_visibility(th, visibility);
782 if (__is_err(ret) == true) {
783 printf("Fail to set visibility!!\n");
790 static int test_tethering_wifi_set_passphrase(void)
793 char passphrase[100];
795 printf("Input passphrase for Wi-Fi tethering: ");
796 ret = scanf("%99s", passphrase);
798 printf("scanf is failed!!\n");
802 ret = tethering_wifi_set_passphrase(th, passphrase);
803 if (__is_err(ret) == true) {
804 printf("Fail to set passphrase!!\n");
811 static int test_tethering_wifi_set_channel(void)
816 printf("Input channel for Wi-Fi tethering: ");
817 ret = scanf("%d", &channel);
819 ret = tethering_wifi_set_channel(th, channel);
820 if (__is_err(ret) == true) {
821 printf("Fail to set channel!!\n");
828 static int test_tethering_wifi_set_mode(void)
833 printf("Input hw_mode for Wi-Fi tethering(0-b, 1-g, 2-a, 3-ad): ");
834 ret = scanf("%d", &type);
836 ret = tethering_wifi_set_mode(th, type);
837 if (__is_err(ret) == true) {
838 printf("Fail to set mode!!\n");
845 static int test_tethering_wifi_enable_dhcp(void)
850 printf("Input (0-Disable, 1-Enable): ");
851 ret = scanf("%d", &enable);
853 ret = tethering_wifi_enable_dhcp(th, enable);
854 if (__is_err(ret) == true) {
855 printf("Fail to enable dhcp server!!\n");
862 static int test_tethering_wifi_set_dhcp_range(void)
865 char rangestart[16], rangestop[16];
867 printf("Input range (ex: 192.168.0.50 192.168.0.150): ");
869 ret = scanf("%15s %15s", rangestart, rangestop);
871 ret = tethering_wifi_set_dhcp_range(th, rangestart, rangestop);
872 if (__is_err(ret) == true) {
873 printf("Fail to set dhcp range and enable dhcp server!!\n");
880 static int test_tethering_wifi_is_dhcp_enabled(void)
885 ret = tethering_wifi_is_dhcp_enabled(th, &enabled);
887 if (__is_err(ret) == true) {
888 printf("Fail to get dhcp server status!!\n");
891 printf("DHCP server is %s\n", enabled ? "enabled" : "disabled");
897 static int test_tethering_wifi_set_mac_filtering(void)
902 printf("Input mac filtering option (0: disable, 1: enable): ");
903 ret = scanf("%d", &enable);
905 ret = tethering_wifi_set_mac_filter(th, enable);
906 if (__is_err(ret) == true) {
907 printf("Fail to set mac filtering!!\n");
914 static int test_tethering_manage_mac_list(void)
920 printf("Select MAC list to modify (0: allowed mac list, 1: blocked mac list): ");
921 ret = scanf("%d", &list);
923 printf("Select option (0: Add, 1: Remove): ");
924 ret = scanf("%d", &option);
926 printf("Input MAC Address to add/remove allowed/blocked mac list: ");
927 ret = scanf("%99s", mac);
929 printf("scanf is failed!!\n");
933 if (!list && !option) {
934 /* Add to allowed mac list*/
935 ret = tethering_wifi_add_allowed_mac_list(th, mac);
936 } else if (!list && option) {
937 /* Remove from allowed mac list */
938 ret = tethering_wifi_remove_allowed_mac_list(th, mac);
939 } else if (list && !option) {
940 /* Add to blocked mac list */
941 ret = tethering_wifi_add_blocked_mac_list(th, mac);
942 } else if (list && option) {
943 /* Remove from blocked mac list */
944 ret = tethering_wifi_remove_blocked_mac_list(th, mac);
946 printf("Input failed!!\n");
956 static int test_tethering_get_mac_list(void)
960 void *mac_list = NULL;
962 printf("Select MAC list to get (0: allowed mac list, 1: blocked mac list): ");
963 ret = scanf("%d", &list);
967 ret = tethering_wifi_get_allowed_mac_list(th, &mac_list);
970 ret = tethering_wifi_get_blocked_mac_list(th, &mac_list);
973 printf("Input failed!!\n");
980 __display_list(mac_list);
985 static int test_tethering_wifi_reload_settings(void)
987 int ret = tethering_wifi_reload_settings(th, __settings_reloaded_cb, NULL);
989 if (__is_err(ret) == true) {
990 printf("Fail to reload wifi tethering!!\n");
996 static int test_tethering_wifi_get_txpower(void)
998 int ret = TETHERING_ERROR_NONE;
1000 unsigned int txpower = 0;
1001 ret = tethering_wifi_get_txpower(th, &txpower);
1002 if (__is_err(ret) == true) {
1003 printf("Fail to get txpower!!\n");
1006 g_print("tethering_hostapd_get_txpower received [%d]\n", txpower);
1010 static int test_tethering_wifi_set_txpower(void)
1013 unsigned int txpower = 0;
1015 printf("Input tx power for Wi-Fi tethering: ");
1016 ret = scanf("%d", &txpower);
1018 ret = tethering_wifi_set_txpower(th, txpower);
1019 if (__is_err(ret) == true) {
1020 printf("Fail to set txpower!!\n");
1027 static int test_tethering_wifi_set_mtu(void)
1030 unsigned int mtu = 0;
1032 printf("Input mtu for Wi-Fi tethering: ");
1033 ret = scanf("%d", &mtu);
1035 ret = tethering_wifi_set_mtu(th, mtu);
1036 if (__is_err(ret) == true) {
1037 printf("Fail to set mtu!!\n");
1044 static int test_tethering_wifi_change_mac(void)
1049 printf("Input mac address: ");
1050 ret = scanf("%17s", mac);
1052 ret = tethering_wifi_change_mac(th, mac);
1053 if (__is_err(ret) == true) {
1054 printf("Fail to change mac!\n");
1061 static int test_tethering_wifi_set_max_connected_device(void)
1066 printf("Input max connected device: ");
1067 ret = scanf("%d", &max_connected);
1069 ret = tethering_wifi_set_max_connected_device(th, max_connected);
1070 if (__is_err(ret) == true) {
1071 printf("Fail to set max connected device!\n");
1079 static int test_tethering_wifi_enable_port_forwarding(void)
1084 printf("Wi-Fi tethring port forwarding(0:disable 1:enable): ");
1085 ret = scanf("%d", &enable);
1087 ret = tethering_wifi_enable_port_forwarding(th, enable);
1088 if (__is_err(ret) == true) {
1089 printf("Fail to enable port forwarding!\n");
1096 static int test_tethering_wifi_add_port_forwarding_rule(void)
1103 int org_port, final_port;
1105 printf("Input ifname, protocol, original ip/port, final ip/port: ");
1106 ret = scanf("%19s", ifname);
1107 ret = scanf("%19s", proto);
1108 ret = scanf("%15s", org_ip);
1109 ret = scanf("%d", &org_port);
1110 ret = scanf("%15s", final_ip);
1111 ret = scanf("%d", &final_port);
1113 ret = tethering_wifi_add_port_forwarding_rule(th, ifname, proto, org_ip, org_port, final_ip, final_port);
1114 if (__is_err(ret) == true) {
1115 printf("Fail to add port forwarding rule!\n");
1122 static int test_tethering_wifi_reset_port_forwarding_rule(void)
1126 ret = tethering_wifi_reset_port_forwarding_rule(th);
1127 if (__is_err(ret) == true) {
1128 printf("Fail to reset port forwarding rule!\n");
1135 static int test_tethering_wifi_get_port_forwarding_rule(void)
1138 void *pf_list = NULL;
1140 ret = tethering_wifi_get_port_forwarding_rule(th, &pf_list);
1141 if (__is_err(ret) == true) {
1142 printf("Fail to get port forwarding rule!\n");
1146 __display_list(pf_list);
1151 static int test_tethering_wifi_enable_port_filtering(void)
1156 printf("Wi-Fi tethring port filtering(0:disable 1:enable): ");
1157 ret = scanf("%d", &enable);
1159 ret = tethering_wifi_enable_port_filtering(th, enable);
1160 if (__is_err(ret) == true) {
1161 printf("Fail to enable port filtering!\n");
1168 static int test_tethering_wifi_add_port_filtering_rule(void)
1175 printf("Input protocol, port, allow: ");
1176 ret = scanf("%19s", proto);
1177 ret = scanf("%d", &port);
1178 ret = scanf("%d", &allow);
1180 ret = tethering_wifi_add_port_filtering_rule(th, port, proto, allow);
1181 if (__is_err(ret) == true) {
1182 printf("Fail to add port forwarding rule!\n");
1189 static int test_tethering_wifi_add_custom_port_filtering_rule(void)
1196 printf("Input protocol, port1, port2, allow: ");
1197 ret = scanf("%19s", proto);
1198 ret = scanf("%d", &port1);
1199 ret = scanf("%d", &port2);
1200 ret = scanf("%d", &allow);
1202 ret = tethering_wifi_add_custom_port_filtering_rule(th, port1, port2, proto, allow);
1203 if (__is_err(ret) == true) {
1204 printf("Fail to add custom port forwarding rule!\n");
1211 static int test_tethering_wifi_get_port_filtering_rule(void)
1214 void *pf_list = NULL;
1216 ret = tethering_wifi_get_port_filtering_rule(th, &pf_list);
1217 if (__is_err(ret) == true) {
1218 printf("Fail to get port filtering rule!\n");
1222 __display_list(pf_list);
1227 static int test_tethering_wifi_get_custom_port_filtering_rule(void)
1230 void *pf_list = NULL;
1232 ret = tethering_wifi_get_custom_port_filtering_rule(th, &pf_list);
1233 if (__is_err(ret) == true) {
1234 printf("Fail to get port filtering rule!\n");
1238 __display_list(pf_list);
1243 static int test_tethering_wifi_set_vpn_passthrough_rule(void)
1248 printf("Select vpn passthrough type (0:IPSEC 1:PPTP 2:L2TP): ");
1249 ret = scanf("%d", &type);
1251 ret = tethering_wifi_set_vpn_passthrough_rule(th, (tethering_vpn_passthrough_type_e)type, true);
1252 if (__is_err(ret) == true) {
1253 printf("Fail to get port filtering rule!\n");
1260 static int test_tethering_wifi_push_wps_button(void)
1264 ret = tethering_wifi_push_wps_button(th);
1265 if (__is_err(ret) == true) {
1266 printf("Fail to get port filtering rule!\n");
1273 static int test_tethering_wifi_set_wps_pin(void)
1278 printf("Input WPS PIN: ");
1279 ret = scanf("%127s", wps_pin);
1281 ret = tethering_wifi_set_wps_pin(th, wps_pin);
1282 if (__is_err(ret) == true) {
1283 printf("Fail to get port filtering rule!\n");
1290 int main(int argc, char **argv)
1292 GMainLoop *mainloop;
1294 #if !GLIB_CHECK_VERSION(2, 36, 0)
1297 mainloop = g_main_loop_new(NULL, false);
1299 GIOChannel *channel = g_io_channel_unix_new(0);
1300 g_io_add_watch(channel, (G_IO_IN|G_IO_ERR|G_IO_HUP|G_IO_NVAL), test_thread, NULL);
1301 printf("Test Thread created...\n");
1302 g_main_loop_run(mainloop);
1307 gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data)
1312 printf("Event received from stdin\n");
1314 rv = read(0, a, 10);
1316 if (rv <= 0 || a[0] == '0')
1319 if (a[0] == '\n' || a[0] == '\r') {
1320 printf("\n\n Network Connection API Test App\n\n");
1321 printf("Options..\n");
1322 printf("1 - Tethering create and set callbacks\n");
1323 printf("2 - Tethering destroy\n");
1324 printf("3 - Enable Tethering\n");
1325 printf("4 - Disable Tethering\n");
1326 printf("5 - Get client information\n");
1327 printf("6 - Get interface information\n");
1328 printf("7 - Get data usage\n");
1329 printf("8 - Get Wi-Fi tethering setting\n");
1330 printf("a - Set Wi-Fi tethering SSID\n");
1331 printf("b - Set Wi-Fi tethering security type\n");
1332 printf("c - Set Wi-Fi tethering visibility\n");
1333 printf("d - Set Wi-Fi tethering passphrase\n");
1334 printf("e - Set Wi-Fi tethering mac filtering\n");
1335 printf("f - Add/Remove MAC adress to/from allowed/blocked list\n");
1336 printf("g - Get allowed/blocked list\n");
1337 printf("k - Reload Wi-Fi tethering\n");
1338 printf("m - Set Wi-Fi channel\n");
1339 printf("n - Set Wi-Fi hw_mode\n");
1340 printf("o - Enable dhcp server\n");
1341 printf("p - Enable dhcp server with range\n");
1342 printf("q - Is dhcp server enabled?\n");
1343 printf("r - Get Wi-Fi txpower\n");
1344 printf("s - Set Wi-Fi txpower\n");
1345 printf("t - Set Wi-Fi mtu\n");
1346 printf("u - Change mac address\n");
1347 printf("v - Set max connected device(Wi-Fi tethering)\n");
1348 printf("w - Enable port forwarding\n");
1349 printf("x - Add port forwarding rule\n");
1350 printf("y - Reset port forwarding rule\n");
1351 printf("z - Get port forwarding rule\n");
1352 printf("A - Enable port filtering\n");
1353 printf("B - Add port filtering rule\n");
1354 printf("C - Add custom port filtering rule\n");
1355 printf("D - Get port filtering rule\n");
1356 printf("E - Get custom port filtering rule\n");
1357 printf("F - Set vpn passthrough rule\n");
1358 printf("G - Push WPS button\n");
1359 printf("H - Set WPS PIN\n");
1361 printf("ENTER - Show options menu.......\n");
1366 rv = test_tethering_create();
1369 rv = test_tethering_destroy();
1372 rv = test_tethering_enable();
1375 rv = test_tethering_disable();
1378 rv = test_tethering_get_client_info();
1381 rv = test_tethering_get_interface_info();
1384 rv = test_tethering_get_data_usage();
1387 rv = test_tethering_wifi_get_setting();
1390 rv = test_tethering_wifi_set_ssid();
1393 rv = test_tethering_wifi_set_security_type();
1396 rv = test_tethering_wifi_set_visibility();
1399 rv = test_tethering_wifi_set_passphrase();
1402 rv = test_tethering_wifi_set_mac_filtering();
1405 rv = test_tethering_manage_mac_list();
1408 rv = test_tethering_get_mac_list();
1411 rv = test_tethering_wifi_reload_settings();
1414 rv = test_tethering_wifi_set_channel();
1417 rv = test_tethering_wifi_set_mode();
1420 rv = test_tethering_wifi_enable_dhcp();
1423 rv = test_tethering_wifi_set_dhcp_range();
1426 rv = test_tethering_wifi_is_dhcp_enabled();
1429 rv = test_tethering_wifi_get_txpower();
1432 rv = test_tethering_wifi_set_txpower();
1435 rv = test_tethering_wifi_set_mtu();
1438 rv = test_tethering_wifi_change_mac();
1441 rv = test_tethering_wifi_set_max_connected_device();
1444 rv = test_tethering_wifi_enable_port_forwarding();
1447 rv = test_tethering_wifi_add_port_forwarding_rule();
1450 rv = test_tethering_wifi_reset_port_forwarding_rule();
1453 rv = test_tethering_wifi_get_port_forwarding_rule();
1456 rv = test_tethering_wifi_enable_port_filtering();
1459 rv = test_tethering_wifi_add_port_filtering_rule();
1462 rv = test_tethering_wifi_add_custom_port_filtering_rule();
1465 rv = test_tethering_wifi_get_port_filtering_rule();
1468 rv = test_tethering_wifi_get_custom_port_filtering_rule();
1471 rv = test_tethering_wifi_set_vpn_passthrough_rule();
1474 rv = test_tethering_wifi_push_wps_button();
1477 rv = test_tethering_wifi_set_wps_pin();
1482 printf("Operation succeeded!\n");
1484 printf("Operation failed!\n");