2 * Copyright (c) 2011-2013 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.
24 #include "net_connection.h"
25 #include <tizen_error.h>
27 #define RETURN_FAIL_DESTROY(x) {connection_profile_destroy(x); return -1;}
29 gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data);
31 connection_h connection = NULL;
32 static GSList *state_cb_list = NULL;
35 static bool test_get_user_string(const char *msg, char *buf, int buf_size)
37 if (msg == NULL || buf == NULL || buf_size < 2)
42 memset(buf, 0, buf_size);
43 rv = read(0, buf, buf_size - 1);
45 if (rv < 0 || buf[0] == '\0' || buf[0] == '\n' || buf[0] == '\r') {
57 static bool test_get_user_int(const char *msg, int *num)
59 if (msg == NULL || num == NULL)
65 rv = read(0, buf, 32);
67 if (rv < 0 || *buf == 0 || *buf == '\n' || *buf == '\r')
74 static const char *test_print_state(connection_profile_state_e state)
77 case CONNECTION_PROFILE_STATE_DISCONNECTED:
78 return "Disconnected";
79 case CONNECTION_PROFILE_STATE_ASSOCIATION:
81 case CONNECTION_PROFILE_STATE_CONFIGURATION:
82 return "Configuration";
83 case CONNECTION_PROFILE_STATE_CONNECTED:
90 static const char *test_print_error(connection_error_e error)
93 case CONNECTION_ERROR_NONE:
94 return "CONNECTION_ERROR_NONE";
95 case CONNECTION_ERROR_INVALID_PARAMETER:
96 return "CONNECTION_ERROR_INVALID_PARAMETER";
97 case CONNECTION_ERROR_OUT_OF_MEMORY:
98 return "CONNECTION_ERROR_OUT_OF_MEMORY";
99 case CONNECTION_ERROR_INVALID_OPERATION:
100 return "CONNECTION_ERROR_INVALID_OPERATION";
101 case CONNECTION_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED:
102 return "CONNECTION_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED";
103 case CONNECTION_ERROR_OPERATION_FAILED:
104 return "CONNECTION_ERROR_OPERATION_FAILED";
105 case CONNECTION_ERROR_ITERATOR_END:
106 return "CONNECTION_ERROR_ITERATOR_END";
107 case CONNECTION_ERROR_NO_CONNECTION:
108 return "CONNECTION_ERROR_NO_CONNECTION";
109 case CONNECTION_ERROR_NOW_IN_PROGRESS:
110 return "CONNECTION_ERROR_NOW_IN_PROGRESS";
111 case CONNECTION_ERROR_ALREADY_EXISTS:
112 return "CONNECTION_ERROR_ALREADY_EXISTS";
113 case CONNECTION_ERROR_OPERATION_ABORTED:
114 return "CONNECTION_ERROR_OPERATION_ABORTED";
115 case CONNECTION_ERROR_DHCP_FAILED:
116 return "CONNECTION_ERROR_DHCP_FAILED";
117 case CONNECTION_ERROR_INVALID_KEY:
118 return "CONNECTION_ERROR_INVALID_KEY";
119 case CONNECTION_ERROR_NO_REPLY:
120 return "CONNECTION_ERROR_NO_REPLY";
121 case CONNECTION_ERROR_PERMISSION_DENIED:
122 return "CONNECTION_ERROR_PERMISSION_DENIED";
123 case CONNECTION_ERROR_NOT_SUPPORTED:
124 return "CONNECTION_ERROR_NOT_SUPPORTED";
126 return "CONNECTION_ERROR_UNKNOWN";
130 static void test_type_changed_callback(connection_type_e type, void* user_data)
132 printf("Type changed callback, connection type : %d\n", type);
135 static void test_ip_changed_callback(const char* ipv4_address, const char* ipv6_address, void* user_data)
137 printf("IP changed callback, IPv4 address : %s, IPv6 address : %s\n",
138 ipv4_address, (ipv6_address ? ipv6_address : "NULL"));
141 static void test_proxy_changed_callback(const char* ipv4_address, const char* ipv6_address, void* user_data)
143 printf("Proxy changed callback, IPv4 address : %s, IPv6 address : %s\n",
144 ipv4_address, (ipv6_address ? ipv6_address : "NULL"));
147 static void test_profile_state_callback(connection_profile_state_e state, void* user_data)
150 connection_profile_h profile = user_data;
155 if (connection_profile_get_name(profile, &profile_name) != CONNECTION_ERROR_NONE)
158 printf("[%s] : %s\n", test_print_state(state), profile_name);
159 g_free(profile_name);
162 static void test_connection_opened_callback(connection_error_e result, void* user_data)
164 if (result == CONNECTION_ERROR_NONE)
165 printf("Connection open Succeeded\n");
167 printf("Connection open Failed, err : %d\n", result);
170 static void test_connection_closed_callback(connection_error_e result, void* user_data)
172 if (result == CONNECTION_ERROR_NONE)
173 printf("Connection close Succeeded\n");
175 printf("Connection close Failed, err : %d\n", result);
178 static void test_connection_reset_profile_callback(connection_error_e result, void* user_data)
180 if (result == CONNECTION_ERROR_NONE)
181 printf("Reset profile Succeeded\n");
183 printf("Reset profile Failed, err : [%s]\n", test_print_error(result));
186 static void test_connection_set_default_callback(connection_error_e result, void* user_data)
188 if (result == CONNECTION_ERROR_NONE)
189 printf("Default profile setting Succeeded\n");
191 printf("Default profile setting Failed, err : %d\n", result);
194 static bool test_get_user_selected_profile(connection_profile_h *profile, bool select)
199 connection_profile_type_e profile_type;
200 connection_profile_state_e profile_state;
201 connection_profile_iterator_h profile_iter;
202 connection_profile_h profile_h;
204 connection_profile_h profile_list[100] = {0,};
205 int profile_count = 0;
207 rv = connection_get_profile_iterator(connection, CONNECTION_ITERATOR_TYPE_REGISTERED, &profile_iter);
208 if (rv != CONNECTION_ERROR_NONE) {
209 printf("Fail to get profile iterator [%d]\n", rv);
213 while (connection_profile_iterator_has_next(profile_iter)) {
214 if (connection_profile_iterator_next(profile_iter, &profile_h) != CONNECTION_ERROR_NONE) {
215 printf("Fail to get profile handle\n");
219 if (connection_profile_get_name(profile_h, &profile_name) != CONNECTION_ERROR_NONE) {
220 printf("Fail to get profile name\n");
224 if (connection_profile_get_type(profile_h, &profile_type) != CONNECTION_ERROR_NONE) {
225 printf("Fail to get profile type\n");
226 g_free(profile_name);
230 if (connection_profile_get_state(profile_h, &profile_state) != CONNECTION_ERROR_NONE) {
231 printf("Fail to get profile state\n");
232 g_free(profile_name);
236 if (profile_type == CONNECTION_PROFILE_TYPE_WIFI) {
238 connection_profile_get_wifi_essid(profile_h, &essid);
239 printf("%d. state:[%s], profile name:%s, essid:%s\n",
240 profile_count, test_print_state(profile_state),
241 profile_name, (essid)? essid : "");
244 profile_list[profile_count] = profile_h;
247 printf("%d. state:[%s], profile name : %s\n",
248 profile_count, test_print_state(profile_state), profile_name);
250 profile_list[profile_count] = profile_h;
254 g_free(profile_name);
255 if (profile_count >= 100)
262 if (test_get_user_int("Input profile number(Enter for cancel) :", &input) == false ||
263 input >= profile_count ||
265 printf("Wrong number!!\n");
270 *profile = profile_list[input];
275 static int test_update_cellular_info(connection_profile_h profile)
278 char input_str1[100] = {0,};
279 char input_str2[100] = {0,};
283 if (test_get_user_int("Input Network Type (internet:1, MMS:2, Prepaid internet:3, "
284 "Prepaid MMS:4, Tethering:5, Application:6)"
285 " - (Enter for skip) :", &input_int)) {
288 rv = connection_profile_set_cellular_service_type(profile,
289 CONNECTION_CELLULAR_SERVICE_TYPE_INTERNET);
292 rv = connection_profile_set_cellular_service_type(profile,
293 CONNECTION_CELLULAR_SERVICE_TYPE_MMS);
296 rv = connection_profile_set_cellular_service_type(profile,
297 CONNECTION_CELLULAR_SERVICE_TYPE_PREPAID_INTERNET);
300 rv = connection_profile_set_cellular_service_type(profile,
301 CONNECTION_CELLULAR_SERVICE_TYPE_PREPAID_MMS);
304 rv = connection_profile_set_cellular_service_type(profile,
305 CONNECTION_CELLULAR_SERVICE_TYPE_TETHERING);
308 rv = connection_profile_set_cellular_service_type(profile,
309 CONNECTION_CELLULAR_SERVICE_TYPE_APPLICATION);
315 if (rv != CONNECTION_ERROR_NONE)
320 if (test_get_user_string("Input Apn - (Enter for skip) :", input_str1, 100)) {
321 rv = connection_profile_set_cellular_apn(profile, input_str1);
322 if (rv != CONNECTION_ERROR_NONE)
326 if (test_get_user_string("Input Proxy - (Enter for skip) :", input_str1, 100)) {
327 rv = connection_profile_set_proxy_address(profile, CONNECTION_ADDRESS_FAMILY_IPV4, input_str1);
328 if (rv != CONNECTION_ERROR_NONE)
332 if (test_get_user_string("Input HomeURL - (Enter for skip) :", input_str1, 100)) {
333 rv = connection_profile_set_cellular_home_url(profile, input_str1);
334 if (rv != CONNECTION_ERROR_NONE)
338 if (test_get_user_int("Input AuthType(0:NONE 1:PAP 2:CHAP) - (Enter for skip) :", &input_int)) {
341 rv = connection_profile_set_cellular_auth_info(profile,
342 CONNECTION_CELLULAR_AUTH_TYPE_NONE, "", "");
343 if (rv != CONNECTION_ERROR_NONE)
348 type_val = CONNECTION_CELLULAR_AUTH_TYPE_PAP;
351 if (input_int == 2) type_val = CONNECTION_CELLULAR_AUTH_TYPE_CHAP;
353 if (test_get_user_string("Input AuthId(Enter for skip) :", input_str1, 100) == false)
355 if (test_get_user_string("Input AuthPwd(Enter for skip) :", input_str2, 100) == false)
358 rv = connection_profile_set_cellular_auth_info(profile, type_val, input_str1, input_str2);
359 if (rv != CONNECTION_ERROR_NONE)
367 static int test_update_wifi_info(connection_profile_h profile)
370 char input_str[100] = {0,};
372 if (test_get_user_string("Input Passphrase - (Enter for skip) :", input_str, 100)) {
373 rv = connection_profile_set_wifi_passphrase(profile, input_str);
374 if (rv != CONNECTION_ERROR_NONE)
381 static int test_update_ip_info(connection_profile_h profile)
384 char input_str[100] = {0,};
386 if (test_get_user_string("Input IP Address - (Enter for skip) :", input_str, 100)) {
387 rv = connection_profile_set_ip_address(profile,
388 CONNECTION_ADDRESS_FAMILY_IPV4,
390 if (rv != CONNECTION_ERROR_NONE)
394 if (test_get_user_string("Input Netmask - (Enter for skip) :", input_str, 100)) {
395 rv = connection_profile_set_subnet_mask(profile,
396 CONNECTION_ADDRESS_FAMILY_IPV4,
398 if (rv != CONNECTION_ERROR_NONE)
402 if (test_get_user_string("Input Gateway - (Enter for skip) :", input_str, 100)) {
403 rv = connection_profile_set_gateway_address(profile,
404 CONNECTION_ADDRESS_FAMILY_IPV4,
406 if (rv != CONNECTION_ERROR_NONE)
410 if (test_get_user_string("Input DNS 1 Address - (Enter for skip) :", input_str, 100)) {
411 rv = connection_profile_set_dns_address(profile,
413 CONNECTION_ADDRESS_FAMILY_IPV4,
415 if (rv != CONNECTION_ERROR_NONE)
418 if (test_get_user_string("Input DNS 2 Address - (Enter for skip) :", input_str, 100)) {
419 rv = connection_profile_set_dns_address(profile,
421 CONNECTION_ADDRESS_FAMILY_IPV4,
423 if (rv != CONNECTION_ERROR_NONE)
431 static int test_update_proxy_info(connection_profile_h profile)
435 char input_str[100] = {0,};
437 if (test_get_user_int("Input Proxy Type (1:direct, 2:auto, 3:manual)"
438 " - (Enter for skip) :", &input_int)) {
441 rv = connection_profile_set_proxy_type(profile,
442 CONNECTION_PROXY_TYPE_DIRECT);
444 if (rv != CONNECTION_ERROR_NONE)
449 rv = connection_profile_set_proxy_type(profile,
450 CONNECTION_PROXY_TYPE_AUTO);
453 rv = connection_profile_set_proxy_type(profile,
454 CONNECTION_PROXY_TYPE_MANUAL);
460 if (rv != CONNECTION_ERROR_NONE)
463 if (test_get_user_string("Input auto Proxy URL or Proxy address"
464 " - (Enter for skip) :", input_str, 100)) {
465 rv = connection_profile_set_proxy_address(profile,
466 CONNECTION_ADDRESS_FAMILY_IPV4,
468 if (rv != CONNECTION_ERROR_NONE)
478 static int test_update_network_info(connection_profile_h profile)
483 if (test_get_user_int("Input IPv4 Address Type (DHCP:1, Static:2)"
484 " - (Enter for skip) :", &input_int)) {
487 rv = connection_profile_set_ip_config_type(profile,
488 CONNECTION_ADDRESS_FAMILY_IPV4,
489 CONNECTION_IP_CONFIG_TYPE_DYNAMIC);
492 rv = connection_profile_set_ip_config_type(profile,
493 CONNECTION_ADDRESS_FAMILY_IPV4,
494 CONNECTION_IP_CONFIG_TYPE_STATIC);
495 if (rv != CONNECTION_ERROR_NONE)
498 if (test_update_ip_info(profile) == -1)
501 if (test_update_proxy_info(profile) == -1)
508 if (rv != CONNECTION_ERROR_NONE)
516 static void test_print_cellular_info(connection_profile_h profile)
518 connection_cellular_network_type_e network_type;
519 connection_cellular_service_type_e service_type;
521 connection_cellular_auth_type_e auth_type;
522 char *user_name = NULL;
523 char *password = NULL;
524 char *home_url = NULL;
525 bool roaming = false;
527 bool editable = false;
529 if (connection_profile_get_cellular_network_type(profile, &network_type) != CONNECTION_ERROR_NONE)
530 printf("Fail to get cellular network type!\n");
532 printf("Cellular network type : %d\n", network_type);
534 if (connection_profile_get_cellular_service_type(profile, &service_type) != CONNECTION_ERROR_NONE)
535 printf("Fail to get cellular service type!\n");
537 printf("Cellular service type : %d\n", service_type);
539 if (connection_profile_get_cellular_apn(profile, &apn) != CONNECTION_ERROR_NONE)
540 printf("Fail to get cellular APN!\n");
542 printf("Cellular APN : %s\n", apn);
546 if (connection_profile_get_cellular_auth_info(profile, &auth_type, &user_name, &password) != CONNECTION_ERROR_NONE)
547 printf("Fail to get auth info!\n");
549 printf("Cellular auth type : %d\n", auth_type);
550 printf("Cellular user_name : %s\n", user_name);
551 printf("Cellular password : %s\n", password);
556 if (connection_profile_get_cellular_home_url(profile, &home_url) != CONNECTION_ERROR_NONE)
557 printf("Fail to get cellular home url!\n");
559 printf("Cellular home url : %s\n", home_url);
563 if (connection_profile_is_cellular_roaming(profile, &roaming) != CONNECTION_ERROR_NONE)
564 printf("Fail to get cellular is roaming!\n");
566 printf("Cellular roaming : %s\n", roaming ? "true" : "false");
568 if (connection_profile_is_cellular_hidden(profile, &hidden) != CONNECTION_ERROR_NONE)
569 printf("Fail to get cellular hidden state!\n");
571 printf("Cellular hidden : %s\n", hidden ? "true" : "false");
573 if (connection_profile_is_cellular_editable(profile, &editable) != CONNECTION_ERROR_NONE)
574 printf("Fail to get cellular editing state!\n");
576 printf("Cellular editable : %s\n", editable ? "true" : "false");
579 static void test_print_wifi_info(connection_profile_h profile)
586 connection_wifi_security_type_e security_type;
587 connection_wifi_encryption_type_e encryption_type;
588 bool pass_required = false;
589 bool wps_supported = false;
591 if (connection_profile_get_wifi_essid(profile, &essid) != CONNECTION_ERROR_NONE)
592 printf("Fail to get Wi-Fi essid!\n");
594 printf("Wi-Fi essid : %s\n", essid);
598 if (connection_profile_get_wifi_bssid(profile, &bssid) != CONNECTION_ERROR_NONE)
599 printf("Fail to get Wi-Fi bssid!\n");
601 printf("Wi-Fi bssid : %s\n", bssid);
605 if (connection_profile_get_wifi_rssi(profile, &rssi) != CONNECTION_ERROR_NONE)
606 printf("Fail to get Wi-Fi rssi!\n");
608 printf("Wi-Fi rssi : %d\n", rssi);
610 if (connection_profile_get_wifi_frequency(profile, &frequency) != CONNECTION_ERROR_NONE)
611 printf("Fail to get Wi-Fi frequency!\n");
613 printf("Wi-Fi frequency : %d\n", frequency);
615 if (connection_profile_get_wifi_max_speed(profile, &max_speed) != CONNECTION_ERROR_NONE)
616 printf("Fail to get Wi-Fi max speed!\n");
618 printf("Wi-Fi max speed : %d\n", max_speed);
620 if (connection_profile_get_wifi_security_type(profile, &security_type) != CONNECTION_ERROR_NONE)
621 printf("Fail to get Wi-Fi security type!\n");
623 printf("Wi-Fi security type : %d\n", security_type);
625 if (connection_profile_get_wifi_encryption_type(profile, &encryption_type) != CONNECTION_ERROR_NONE)
626 printf("Fail to get Wi-Fi encryption type!\n");
628 printf("Wi-Fi encryption type : %d\n", encryption_type);
630 if (connection_profile_is_wifi_passphrase_required(profile, &pass_required) != CONNECTION_ERROR_NONE)
631 printf("Fail to get Wi-Fi passphrase required!\n");
633 printf("Wi-Fi passphrase required : %s\n", pass_required ? "true" : "false");
635 if (connection_profile_is_wifi_wps_supported(profile, &wps_supported) != CONNECTION_ERROR_NONE)
636 printf("Fail to get Wi-Fi wps info\n");
638 printf("Wi-Fi wps supported : %s\n", wps_supported ? "true" : "false");
641 static void test_print_network_info(connection_profile_h profile)
643 char *interface_name = NULL;
644 connection_ip_config_type_e ip_type;
647 char *gateway = NULL;
650 connection_proxy_type_e proxy_type;
653 if (connection_profile_get_network_interface_name(profile, &interface_name) != CONNECTION_ERROR_NONE)
654 printf("Fail to get interface name!\n");
656 printf("Interface name : %s\n", interface_name);
657 g_free(interface_name);
660 if (connection_profile_get_ip_config_type(profile, CONNECTION_ADDRESS_FAMILY_IPV4, &ip_type) != CONNECTION_ERROR_NONE)
661 printf("Fail to get ipconfig type!\n");
663 printf("Ipconfig type : %d\n", ip_type);
665 if (connection_profile_get_ip_address(profile, CONNECTION_ADDRESS_FAMILY_IPV4, &ip) != CONNECTION_ERROR_NONE)
666 printf("Fail to get IP address!\n");
668 printf("IP address : %s\n", ip);
672 if (connection_profile_get_subnet_mask(profile, CONNECTION_ADDRESS_FAMILY_IPV4, &subnet) != CONNECTION_ERROR_NONE)
673 printf("Fail to get subnet mask!\n");
675 printf("Subnet mask : %s\n", subnet);
679 if (connection_profile_get_gateway_address(profile, CONNECTION_ADDRESS_FAMILY_IPV4, &gateway) != CONNECTION_ERROR_NONE)
680 printf("Fail to get gateway!\n");
682 printf("Gateway : %s\n", gateway);
686 if (connection_profile_get_dns_address(profile, 1, CONNECTION_ADDRESS_FAMILY_IPV4, &dns1) != CONNECTION_ERROR_NONE)
687 printf("Fail to get DNS1!\n");
689 printf("DNS1 : %s\n", dns1);
693 if (connection_profile_get_dns_address(profile, 2, CONNECTION_ADDRESS_FAMILY_IPV4, &dns2) != CONNECTION_ERROR_NONE)
694 printf("Fail to get DNS2!\n");
696 printf("DNS2 : %s\n", dns2);
700 if (connection_profile_get_proxy_type(profile, &proxy_type) != CONNECTION_ERROR_NONE)
701 printf("Fail to get proxy type!\n");
703 printf("Proxy type : %d\n", proxy_type);
705 if (connection_profile_get_proxy_address(profile, CONNECTION_ADDRESS_FAMILY_IPV4, &proxy) != CONNECTION_ERROR_NONE)
706 printf("Fail to get proxy!\n");
708 printf("Proxy : %s\n", proxy);
713 int test_register_client(void)
716 int err = connection_create(&connection);
718 if (CONNECTION_ERROR_NONE == err) {
719 connection_set_type_changed_cb(connection, test_type_changed_callback, NULL);
720 connection_set_ip_address_changed_cb(connection, test_ip_changed_callback, NULL);
721 connection_set_proxy_address_changed_cb(connection, test_proxy_changed_callback, NULL);
723 printf("Client registration failed %d\n", err);
727 printf("Client registration success\n");
731 int test_deregister_client(void)
735 connection_profile_h profile;
737 if (connection != NULL)
738 rv = connection_destroy(connection);
740 printf("Cannot deregister : Handle is NULL\n");
741 rv = CONNECTION_ERROR_INVALID_OPERATION;
744 if (rv != CONNECTION_ERROR_NONE){
745 printf("Client deregistration fail [%d]\n", rv);
750 for (list = state_cb_list; list; list = list->next) {
751 profile = list->data;
752 connection_profile_destroy(profile);
755 g_slist_free(state_cb_list);
756 state_cb_list = NULL;
760 printf("Client deregistration success\n");
765 int test_get_network_state(void)
768 connection_type_e net_state;
770 rv = connection_get_type(connection, &net_state);
772 if (rv != CONNECTION_ERROR_NONE) {
773 printf("Fail to get network state [%d]\n", rv);
777 printf("Retval = %d network connection state [%d]\n", rv, net_state);
782 int test_get_cellular_state(void)
785 connection_cellular_state_e cellular_state;
787 rv = connection_get_cellular_state(connection, &cellular_state);
789 if (rv != CONNECTION_ERROR_NONE) {
790 printf("Fail to get Cellular state [%d]\n", rv);
794 printf("Retval = %d Cellular state [%d]\n", rv, cellular_state);
799 int test_get_wifi_state(void)
802 connection_wifi_state_e wifi_state;
804 rv = connection_get_wifi_state(connection, &wifi_state);
806 if (rv != CONNECTION_ERROR_NONE) {
807 printf("Fail to get WiFi state [%d]\n", rv);
811 printf("Retval = %d WiFi state [%d]\n", rv, wifi_state);
816 int test_get_current_proxy(void)
818 char *proxy_addr = NULL;
820 connection_get_proxy(connection, CONNECTION_ADDRESS_FAMILY_IPV4, &proxy_addr);
822 if (proxy_addr == NULL) {
823 printf("Proxy address does not exist\n");
827 printf("Current Proxy [%s]\n", proxy_addr);
833 int test_get_current_ip(void)
835 char *ip_addr = NULL;
837 connection_get_ip_address(connection, CONNECTION_ADDRESS_FAMILY_IPV4, &ip_addr);
839 if (ip_addr == NULL) {
840 printf("IP address does not exist\n");
844 printf("IPv4 address : %s\n", ip_addr);
850 int test_get_call_statistics_info(void)
854 connection_get_statistics(connection, CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_LAST_RECEIVED_DATA, &rv);
855 printf("last recv data size [%lld]\n", rv);
856 connection_get_statistics(connection, CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_LAST_SENT_DATA, &rv);
857 printf("last sent data size [%lld]\n",rv );
858 connection_get_statistics(connection, CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_TOTAL_RECEIVED_DATA, &rv);
859 printf("total received data size [%lld]\n",rv );
860 connection_get_statistics(connection, CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_TOTAL_SENT_DATA, &rv);
861 printf("total sent data size [%lld]\n", rv);
866 int test_get_wifi_call_statistics_info(void)
870 connection_get_statistics(connection, CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_LAST_RECEIVED_DATA, &rv);
871 printf("WiFi last recv data size [%lld]\n", rv);
872 connection_get_statistics(connection, CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_LAST_SENT_DATA, &rv);
873 printf("WiFi last sent data size [%lld]\n",rv );
874 connection_get_statistics(connection, CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_TOTAL_RECEIVED_DATA, &rv);
875 printf("WiFi total received data size [%lld]\n",rv );
876 connection_get_statistics(connection, CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_TOTAL_SENT_DATA, &rv);
877 printf("WiFi total sent data size [%lld]\n", rv);
882 int test_get_profile_list(void)
884 if (test_get_user_selected_profile(NULL, false) == false)
890 int test_get_default_profile_list(void)
893 char *profile_name = NULL;
894 connection_profile_iterator_h profile_iter;
895 connection_profile_h profile_h;
896 connection_cellular_service_type_e service_type;
897 bool is_default = false;
899 rv = connection_get_profile_iterator(connection, CONNECTION_ITERATOR_TYPE_DEFAULT, &profile_iter);
900 if (rv != CONNECTION_ERROR_NONE) {
901 printf("Fail to get profile iterator [%s]\n", test_print_error(rv));
905 while (connection_profile_iterator_has_next(profile_iter)) {
906 if (connection_profile_iterator_next(profile_iter, &profile_h) != CONNECTION_ERROR_NONE) {
907 printf("Fail to get profile handle\n");
911 if (connection_profile_get_name(profile_h, &profile_name) != CONNECTION_ERROR_NONE) {
912 printf("Fail to get profile name\n");
915 printf("profile name : %s\n", profile_name);
916 g_free(profile_name);
918 if (connection_profile_get_cellular_service_type(profile_h, &service_type) != CONNECTION_ERROR_NONE) {
919 printf("Fail to get profile service type\n");
922 printf("service type : %d\n", service_type);
924 if (connection_profile_is_cellular_default(profile_h, &is_default) != CONNECTION_ERROR_NONE) {
925 printf("Fail to get profile subscriber id\n");
928 printf("Default : %d\n", is_default);
934 int test_get_connected_profile_list(void)
937 char *profile_name = NULL;
938 connection_profile_iterator_h profile_iter;
939 connection_profile_h profile_h;
940 bool is_default = false;
941 connection_profile_type_e type;
943 rv = connection_get_profile_iterator(connection, CONNECTION_ITERATOR_TYPE_CONNECTED, &profile_iter);
944 if (rv != CONNECTION_ERROR_NONE) {
945 printf("Fail to get profile iterator [%d]\n", rv);
949 while (connection_profile_iterator_has_next(profile_iter)) {
950 if (connection_profile_iterator_next(profile_iter, &profile_h) != CONNECTION_ERROR_NONE) {
951 printf("Fail to get profile handle\n");
955 if (connection_profile_get_name(profile_h, &profile_name) != CONNECTION_ERROR_NONE) {
956 printf("Fail to get profile name\n");
959 printf("profile name : %s\n", profile_name);
960 g_free(profile_name);
962 if (connection_profile_get_type(profile_h, &type) != CONNECTION_ERROR_NONE) {
963 printf("Fail to get profile type\n");
966 printf("profile type is %d\n", type);
968 if (type == CONNECTION_PROFILE_TYPE_CELLULAR) {
969 if (connection_profile_is_cellular_default(profile_h, &is_default) != CONNECTION_ERROR_NONE) {
970 printf("Fail to get profile is default\n");
973 printf("[%s]\n", is_default ? "default" : "not default");
980 int test_get_current_profile(void)
983 char *profile_name = NULL;
984 connection_profile_h profile_h;
986 rv = connection_get_current_profile(connection, &profile_h);
987 if (rv != CONNECTION_ERROR_NONE) {
988 printf("Fail to get profile iterator [%d]\n", rv);
992 if (connection_profile_get_name(profile_h, &profile_name) != CONNECTION_ERROR_NONE) {
993 printf("Fail to get profile name\n");
996 printf("profile name : %s\n", profile_name);
997 g_free(profile_name);
999 connection_profile_destroy(profile_h);
1004 int test_open_profile(void)
1006 connection_profile_h profile;
1008 printf("\n** Choose a profile to open. **\n");
1010 if (test_get_user_selected_profile(&profile, true) == false)
1013 if (connection_open_profile(connection, profile, test_connection_opened_callback, NULL) != CONNECTION_ERROR_NONE) {
1014 printf("Connection open Failed!!\n");
1021 int test_get_default_cellular_service_type(void)
1026 connection_profile_h profile;
1027 char *profile_name = NULL;
1029 rv = test_get_user_int("Input profile type to get"
1030 "(1:Internet, 2:MMS, 3:Prepaid internet, 4:Prepaid MMS, 5:Tethering):", &input);
1033 printf("Invalid input!!\n");
1039 service_type = CONNECTION_CELLULAR_SERVICE_TYPE_INTERNET;
1042 service_type = CONNECTION_CELLULAR_SERVICE_TYPE_MMS;
1045 service_type = CONNECTION_CELLULAR_SERVICE_TYPE_PREPAID_INTERNET;
1048 service_type = CONNECTION_CELLULAR_SERVICE_TYPE_PREPAID_MMS;
1051 service_type = CONNECTION_CELLULAR_SERVICE_TYPE_TETHERING;
1054 printf("Wrong number!!\n");
1058 if (connection_get_default_cellular_service_profile(connection, service_type, &profile) != CONNECTION_ERROR_NONE)
1061 if (connection_profile_get_name(profile, &profile_name) != CONNECTION_ERROR_NONE) {
1062 printf("Fail to get profile name\n");
1063 connection_profile_destroy(profile);
1066 printf("Default profile name : %s\n", profile_name);
1067 g_free(profile_name);
1069 connection_profile_destroy(profile);
1074 int test_set_default_cellular_service_type(void)
1076 connection_profile_h profile;
1077 connection_cellular_service_type_e type;
1080 rv = test_get_user_int("Input API type (1:sync, 2:async)", &input);
1082 if (rv == false || (input != 1 && input != 2)) {
1083 printf("Invalid input!!\n");
1087 printf("\n** Choose a profile to set default service(internet or prepaid internet type only). **\n");
1089 if (test_get_user_selected_profile(&profile, true) == false)
1092 if (connection_profile_get_cellular_service_type(profile, &type) != CONNECTION_ERROR_NONE) {
1093 printf("Fail to get cellular service type\n");
1098 if (connection_set_default_cellular_service_profile(connection, type, profile) != CONNECTION_ERROR_NONE)
1101 if (connection_set_default_cellular_service_profile_async(connection,
1102 type, profile, test_connection_set_default_callback, NULL) != CONNECTION_ERROR_NONE)
1109 int test_close_profile(void)
1111 connection_profile_h profile;
1113 printf("\n** Choose a profile to close. **\n");
1115 if (test_get_user_selected_profile(&profile, true) == false)
1118 if (connection_close_profile(connection, profile, test_connection_closed_callback, NULL) != CONNECTION_ERROR_NONE) {
1119 printf("Connection close Failed!!\n");
1126 int test_add_profile(void)
1129 connection_profile_h profile;
1130 char input_str[100] = {0,};
1132 if (test_get_user_string("Input Keyword - (Enter for skip) :", input_str, 100) == false)
1135 rv = connection_profile_create(CONNECTION_PROFILE_TYPE_CELLULAR, input_str, &profile);
1136 if (rv != CONNECTION_ERROR_NONE)
1137 RETURN_FAIL_DESTROY(profile);
1139 if (test_update_cellular_info(profile) == -1)
1140 RETURN_FAIL_DESTROY(profile);
1142 rv = connection_add_profile(connection, profile);
1143 if (rv != CONNECTION_ERROR_NONE)
1144 RETURN_FAIL_DESTROY(profile);
1146 connection_profile_destroy(profile);
1150 int test_remove_profile(void)
1152 connection_profile_h profile;
1154 printf("\n** Choose a profile to remove. **\n");
1155 if (test_get_user_selected_profile(&profile, true) == false)
1158 if (connection_remove_profile(connection, profile) != CONNECTION_ERROR_NONE) {
1159 printf("Remove profile Failed!!\n");
1166 int test_update_profile(void)
1170 connection_profile_type_e prof_type;
1171 connection_profile_h profile;
1173 printf("\n** Choose a profile to update. **\n");
1174 if (test_get_user_selected_profile(&profile, true) == false)
1177 if (connection_profile_get_type(profile, &prof_type) != CONNECTION_ERROR_NONE)
1180 switch (prof_type) {
1181 case CONNECTION_PROFILE_TYPE_CELLULAR:
1182 if (test_update_cellular_info(profile) == -1)
1186 case CONNECTION_PROFILE_TYPE_WIFI:
1187 if (test_update_wifi_info(profile) == -1)
1190 if (test_update_network_info(profile) == -1)
1194 case CONNECTION_PROFILE_TYPE_ETHERNET:
1195 case CONNECTION_PROFILE_TYPE_BT:
1196 printf("Not supported!\n");
1202 rv = connection_update_profile(connection, profile);
1203 if (rv != CONNECTION_ERROR_NONE)
1209 int test_get_profile_info(void)
1211 connection_profile_type_e prof_type;
1212 connection_profile_state_e profile_state;
1213 connection_profile_h profile;
1214 char *profile_name = NULL;
1216 printf("\n** Choose a profile to print. **\n");
1217 if (test_get_user_selected_profile(&profile, true) == false)
1220 if (connection_profile_get_name(profile, &profile_name) != CONNECTION_ERROR_NONE) {
1221 printf("Fail to get profile name\n");
1224 printf("Profile Name : %s\n", profile_name);
1225 g_free(profile_name);
1228 if (connection_profile_get_state(profile, &profile_state) != CONNECTION_ERROR_NONE) {
1229 printf("Fail to get profile state\n");
1232 printf("Profile State : %s\n", test_print_state(profile_state));
1235 if (connection_profile_get_type(profile, &prof_type) != CONNECTION_ERROR_NONE)
1238 switch (prof_type) {
1239 case CONNECTION_PROFILE_TYPE_CELLULAR:
1240 printf("Profile Type : Cellular\n");
1241 test_print_cellular_info(profile);
1243 case CONNECTION_PROFILE_TYPE_WIFI:
1244 printf("Profile Type : Wi-Fi\n");
1245 test_print_wifi_info(profile);
1247 case CONNECTION_PROFILE_TYPE_ETHERNET:
1248 printf("Profile Type : Ethernet\n");
1250 case CONNECTION_PROFILE_TYPE_BT:
1251 printf("Profile Type : Bluetooth\n");
1257 test_print_network_info(profile);
1262 int test_refresh_profile_info(void)
1264 connection_profile_type_e prof_type;
1265 connection_profile_state_e profile_state;
1266 connection_profile_h profile;
1267 char *profile_name = NULL;
1269 printf("\n** Choose a profile to refresh. **\n");
1270 if (test_get_user_selected_profile(&profile, true) == false)
1273 if (connection_profile_refresh(profile) != CONNECTION_ERROR_NONE)
1276 if (connection_profile_get_name(profile, &profile_name) != CONNECTION_ERROR_NONE) {
1277 printf("Fail to get profile name\n");
1280 printf("Profile Name : %s\n", profile_name);
1281 g_free(profile_name);
1284 if (connection_profile_get_state(profile, &profile_state) != CONNECTION_ERROR_NONE) {
1285 printf("Fail to get profile state\n");
1288 printf("Profile State : %s\n", test_print_state(profile_state));
1291 if (connection_profile_get_type(profile, &prof_type) != CONNECTION_ERROR_NONE)
1294 switch (prof_type) {
1295 case CONNECTION_PROFILE_TYPE_CELLULAR:
1296 printf("Profile Type : Cellular\n");
1297 test_print_cellular_info(profile);
1299 case CONNECTION_PROFILE_TYPE_WIFI:
1300 printf("Profile Type : Wi-Fi\n");
1301 test_print_wifi_info(profile);
1303 case CONNECTION_PROFILE_TYPE_ETHERNET:
1304 printf("Profile Type : Ethernet\n");
1306 case CONNECTION_PROFILE_TYPE_BT:
1307 printf("Profile Type : Bluetooth\n");
1313 test_print_network_info(profile);
1318 int test_set_state_changed_callback()
1320 connection_profile_h profile;
1321 connection_profile_h profile_clone;
1323 printf("\n** Choose a profile to set callback. **\n");
1324 if (test_get_user_selected_profile(&profile, true) == false)
1327 if (connection_profile_clone(&profile_clone, profile) != CONNECTION_ERROR_NONE)
1330 if (connection_profile_set_state_changed_cb(profile,
1331 test_profile_state_callback, profile_clone) != CONNECTION_ERROR_NONE) {
1332 connection_profile_destroy(profile_clone);
1336 state_cb_list = g_slist_append(state_cb_list, profile_clone);
1341 int test_unset_state_changed_callback()
1343 connection_profile_h profile;
1345 char *profile_name = NULL;
1349 printf("\n** Choose a profile to unset callback. **\n");
1350 for (list = state_cb_list; list; list = list->next) {
1351 profile = list->data;
1352 if (connection_profile_get_name(profile, &profile_name) != CONNECTION_ERROR_NONE) {
1353 printf("Fail to get profile name!\n");
1356 printf("%d. %s\n", count, profile_name);
1357 g_free(profile_name);
1363 if (test_get_user_int("Input profile number(Enter for cancel) :", &input) == false ||
1366 printf("Wrong number!!\n");
1371 for (list = state_cb_list; list; list = list->next) {
1372 if (count == input) {
1373 profile = list->data;
1383 if (connection_profile_unset_state_changed_cb(profile) != CONNECTION_ERROR_NONE)
1386 state_cb_list = g_slist_remove(state_cb_list, profile);
1387 connection_profile_destroy(profile);
1392 int test_reset_call_statistics_info(void)
1394 int ret = CONNECTION_ERROR_NONE;
1396 ret = connection_reset_statistics(connection, CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_LAST_RECEIVED_DATA);
1397 printf("reset last recv data size [%d]\n", ret);
1398 ret = connection_reset_statistics(connection, CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_LAST_SENT_DATA);
1399 printf("last sent data size [%d]\n", ret);
1400 ret = connection_reset_statistics(connection, CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_TOTAL_RECEIVED_DATA);
1401 printf("total received data size [%d]\n", ret);
1402 ret = connection_reset_statistics(connection, CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_TOTAL_SENT_DATA);
1403 printf("total sent data size [%d]\n", ret);
1408 int test_reset_wifi_call_statistics_info(void)
1410 int ret = CONNECTION_ERROR_NONE;
1412 ret = connection_reset_statistics(connection, CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_LAST_SENT_DATA);
1413 printf("WiFi last sent data size [%d]\n", ret);
1414 ret = connection_reset_statistics(connection, CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_LAST_RECEIVED_DATA);
1415 printf("WiFi last recv data size [%d]\n", ret);
1416 ret = connection_reset_statistics(connection, CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_TOTAL_SENT_DATA);
1417 printf("WiFi total sent data size [%d]\n", ret);
1418 ret = connection_reset_statistics(connection, CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_TOTAL_RECEIVED_DATA);
1419 printf("WiFi total received data size [%d]\n", ret);
1424 int test_add_route(void)
1430 if (test_get_user_string("Input IP - (Enter for skip) :", ip_addr, 30) == false)
1433 if (test_get_user_string("Input Interface name - (Enter for skip) :", if_name, 40) == false)
1436 rv = connection_add_route(connection, if_name, ip_addr);
1437 if (rv != CONNECTION_ERROR_NONE) {
1438 printf("Fail to get add new route [%d]\n", rv);
1445 int test_remove_route(void)
1451 if (test_get_user_string("Input IP - (Enter for skip) :", ip_addr, 30) == false)
1454 if (test_get_user_string("Input Interface name - (Enter for skip) :", if_name, 40) == false)
1457 rv = connection_remove_route(connection, if_name, ip_addr);
1458 if (rv != CONNECTION_ERROR_NONE) {
1459 printf("Fail to remove the route [%s]\n", test_print_error(rv));
1466 int test_get_bt_state(void)
1469 connection_bt_state_e bt_state;
1471 rv = connection_get_bt_state(connection, &bt_state);
1473 if (rv != CONNECTION_ERROR_NONE) {
1474 printf("Fail to get Bluetooth state [%d]\n", rv);
1478 printf("Retval = %d, Bluetooth state [%d]\n", rv, bt_state);
1483 int test_get_profile_id(void)
1485 connection_profile_h profile;
1488 printf("\n** Choose a profile to see profile id. **\n");
1489 if (test_get_user_selected_profile(&profile, true) == false)
1492 if (connection_profile_get_id(profile, &profile_id) != CONNECTION_ERROR_NONE) {
1493 printf("Fail to get profile name\n");
1496 printf("Profile id : %s\n", profile_id);
1503 int test_reset_profile(void)
1505 int type, sim_id, rv;
1507 rv = test_get_user_int("Input reset type (0:default profile reset, 1:delete profile reset)", &type);
1509 if (rv == false || (type != 0 && type != 1)) {
1510 printf("Invalid input!!\n");
1514 rv = test_get_user_int("Input SIM id to reset (0:SIM1, 1:SIM2)", &sim_id);
1516 if (rv == false || (sim_id != 0 && sim_id != 1)) {
1517 printf("Invalid input!!\n");
1521 if (connection_reset_profile(connection, type, sim_id, test_connection_reset_profile_callback, NULL) != CONNECTION_ERROR_NONE) {
1528 int main(int argc, char **argv)
1530 GMainLoop *mainloop;
1531 mainloop = g_main_loop_new (NULL, FALSE);
1533 GIOChannel *channel = g_io_channel_unix_new(0);
1534 g_io_add_watch(channel, (G_IO_IN|G_IO_ERR|G_IO_HUP|G_IO_NVAL), test_thread,NULL );
1536 printf("Test Thread created...\n");
1538 g_main_loop_run (mainloop);
1543 gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data)
1548 memset(a, '\0', 100);
1549 printf("Event received from stdin\n");
1551 rv = read(0, a, 100);
1553 if (rv < 0 || a[0] == '0') {
1554 if (connection != NULL)
1555 test_deregister_client();
1560 if (*a == '\n' || *a == '\r'){
1561 printf("\n\n Network Connection API Test App\n\n");
1562 printf("Options..\n");
1563 printf("1 - Create Handle and set callbacks\n");
1564 printf("2 - Destroy Handle(unset callbacks automatically)\n");
1565 printf("3 - Get network state\n");
1566 printf("4 - Get cellular state (please insert SIM Card)\n");
1567 printf("5 - Get wifi state (please turn on WiFi)\n");
1568 printf("6 - Get current proxy address \n");
1569 printf("7 - Get current Ip address\n");
1570 printf("8 - Get cellular data call statistics\n");
1571 printf("9 - Get WiFi data call statistics\n");
1572 printf("a - Get Profile list\n");
1573 printf("b - Get Connected Profile list\n");
1574 printf("c - Get Current profile\n");
1575 printf("d - Open connection with profile\n");
1576 printf("e - Get default cellular service by type\n");
1577 printf("f - Set default cellular service by type\n");
1578 printf("g - Close connection with profile\n");
1579 printf("h - Add profile(Cellular only)\n");
1580 printf("i - Remove profile(Cellular:delete, WiFi:forgot)\n");
1581 printf("j - Update profile\n");
1582 printf("k - Get profile info\n");
1583 printf("l - Refresh profile info\n");
1584 printf("m - Set state changed callback\n");
1585 printf("n - Unset state changed callback\n");
1586 printf("o - Reset cellular data call statistics\n");
1587 printf("p - Reset WiFi data call statistics\n");
1588 printf("q - Add new route\n");
1589 printf("r - Get Bluetooth state\n");
1590 printf("s - Get profile id\n");
1591 printf("0 - Exit \n");
1592 printf("ENTER - Show options menu.......\n");
1597 rv = test_register_client();
1600 rv = test_deregister_client();
1603 rv = test_get_network_state();
1606 rv = test_get_cellular_state();
1609 rv = test_get_wifi_state();
1612 rv = test_get_current_proxy();
1615 rv = test_get_current_ip();
1618 rv = test_get_call_statistics_info();
1621 rv = test_get_wifi_call_statistics_info();
1624 rv = test_get_profile_list();
1627 rv = test_get_connected_profile_list();
1630 rv = test_get_current_profile();
1633 rv = test_open_profile();
1636 rv = test_get_default_cellular_service_type();
1639 rv = test_set_default_cellular_service_type();
1642 rv = test_close_profile();
1645 rv = test_add_profile();
1648 rv = test_remove_profile();
1651 rv = test_update_profile();
1654 rv = test_get_profile_info();
1657 rv = test_refresh_profile_info();
1660 rv = test_set_state_changed_callback();
1663 rv = test_unset_state_changed_callback();
1666 rv = test_reset_call_statistics_info();
1669 rv = test_reset_wifi_call_statistics_info();
1672 rv = test_add_route();
1675 rv = test_remove_route();
1678 rv = test_get_bt_state();
1681 rv = test_get_profile_id();
1684 rv = test_reset_profile();
1687 rv = test_get_default_profile_list();
1692 printf("Operation succeeded!\n");
1694 printf("Operation failed!\n");