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') {
55 static bool test_get_user_int(const char *msg, int *num)
57 if (msg == NULL || num == NULL)
63 rv = read(0, buf, 32);
65 if (rv < 0 || *buf == 0 || *buf == '\n' || *buf == '\r')
72 static const char *test_print_state(connection_profile_state_e state)
75 case CONNECTION_PROFILE_STATE_DISCONNECTED:
76 return "Disconnected";
77 case CONNECTION_PROFILE_STATE_ASSOCIATION:
79 case CONNECTION_PROFILE_STATE_CONFIGURATION:
80 return "Configuration";
81 case CONNECTION_PROFILE_STATE_CONNECTED:
88 static const char *test_print_error(connection_error_e error)
91 case CONNECTION_ERROR_NONE:
92 return "CONNECTION_ERROR_NONE";
93 case CONNECTION_ERROR_INVALID_PARAMETER:
94 return "CONNECTION_ERROR_INVALID_PARAMETER";
95 case CONNECTION_ERROR_OUT_OF_MEMORY:
96 return "CONNECTION_ERROR_OUT_OF_MEMORY";
97 case CONNECTION_ERROR_INVALID_OPERATION:
98 return "CONNECTION_ERROR_INVALID_OPERATION";
99 case CONNECTION_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED:
100 return "CONNECTION_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED";
101 case CONNECTION_ERROR_OPERATION_FAILED:
102 return "CONNECTION_ERROR_OPERATION_FAILED";
103 case CONNECTION_ERROR_ITERATOR_END:
104 return "CONNECTION_ERROR_ITERATOR_END";
105 case CONNECTION_ERROR_NO_CONNECTION:
106 return "CONNECTION_ERROR_NO_CONNECTION";
107 case CONNECTION_ERROR_NOW_IN_PROGRESS:
108 return "CONNECTION_ERROR_NOW_IN_PROGRESS";
109 case CONNECTION_ERROR_ALREADY_EXISTS:
110 return "CONNECTION_ERROR_ALREADY_EXISTS";
111 case CONNECTION_ERROR_OPERATION_ABORTED:
112 return "CONNECTION_ERROR_OPERATION_ABORTED";
113 case CONNECTION_ERROR_DHCP_FAILED:
114 return "CONNECTION_ERROR_DHCP_FAILED";
115 case CONNECTION_ERROR_INVALID_KEY:
116 return "CONNECTION_ERROR_INVALID_KEY";
117 case CONNECTION_ERROR_NO_REPLY:
118 return "CONNECTION_ERROR_NO_REPLY";
119 case CONNECTION_ERROR_PERMISSION_DENIED:
120 return "CONNECTION_ERROR_PERMISSION_DENIED";
121 case CONNECTION_ERROR_NOT_SUPPORTED:
122 return "CONNECTION_ERROR_NOT_SUPPORTED";
124 return "CONNECTION_ERROR_UNKNOWN";
128 static void test_type_changed_callback(connection_type_e type, void* user_data)
130 printf("Type changed callback, connection type : %d\n", type);
133 static void test_ip_changed_callback(const char* ipv4_address, const char* ipv6_address, void* user_data)
135 printf("IP changed callback, IPv4 address : %s, IPv6 address : %s\n",
136 ipv4_address, (ipv6_address ? ipv6_address : "NULL"));
139 static void test_proxy_changed_callback(const char* ipv4_address, const char* ipv6_address, void* user_data)
141 printf("Proxy changed callback, IPv4 address : %s, IPv6 address : %s\n",
142 ipv4_address, (ipv6_address ? ipv6_address : "NULL"));
145 static void test_profile_state_callback(connection_profile_state_e state, void* user_data)
148 connection_profile_h profile = user_data;
153 if (connection_profile_get_name(profile, &profile_name) != CONNECTION_ERROR_NONE)
156 printf("[%s] : %s\n", test_print_state(state), profile_name);
157 g_free(profile_name);
160 static void test_connection_opened_callback(connection_error_e result, void* user_data)
162 if (result == CONNECTION_ERROR_NONE)
163 printf("Connection open Succeeded\n");
165 printf("Connection open Failed, err : %d\n", result);
168 static void test_connection_closed_callback(connection_error_e result, void* user_data)
170 if (result == CONNECTION_ERROR_NONE)
171 printf("Connection close Succeeded\n");
173 printf("Connection close Failed, err : %d\n", result);
176 static void test_connection_reset_profile_callback(connection_error_e result, void* user_data)
178 if (result == CONNECTION_ERROR_NONE)
179 printf("Reset profile Succeeded\n");
181 printf("Reset profile Failed, err : [%s]\n", test_print_error(result));
184 static void test_connection_set_default_callback(connection_error_e result, void* user_data)
186 if (result == CONNECTION_ERROR_NONE)
187 printf("Default profile setting Succeeded\n");
189 printf("Default profile setting Failed, err : %d\n", result);
192 static bool test_get_user_selected_profile(connection_profile_h *profile, bool select)
197 connection_profile_type_e profile_type;
198 connection_profile_state_e profile_state;
199 connection_profile_iterator_h profile_iter;
200 connection_profile_h profile_h;
202 connection_profile_h profile_list[100] = {0,};
203 int profile_count = 0;
205 rv = connection_get_profile_iterator(connection, CONNECTION_ITERATOR_TYPE_REGISTERED, &profile_iter);
206 if (rv != CONNECTION_ERROR_NONE) {
207 printf("Fail to get profile iterator [%d]\n", rv);
211 while (connection_profile_iterator_has_next(profile_iter)) {
212 if (connection_profile_iterator_next(profile_iter, &profile_h) != CONNECTION_ERROR_NONE) {
213 printf("Fail to get profile handle\n");
217 if (connection_profile_get_name(profile_h, &profile_name) != CONNECTION_ERROR_NONE) {
218 printf("Fail to get profile name\n");
222 if (connection_profile_get_type(profile_h, &profile_type) != CONNECTION_ERROR_NONE) {
223 printf("Fail to get profile type\n");
224 g_free(profile_name);
228 if (connection_profile_get_state(profile_h, &profile_state) != CONNECTION_ERROR_NONE) {
229 printf("Fail to get profile state\n");
230 g_free(profile_name);
234 if (profile_type == CONNECTION_PROFILE_TYPE_WIFI) {
236 connection_profile_get_wifi_essid(profile_h, &essid);
237 printf("%d. state:[%s], profile name:%s, essid:%s\n",
238 profile_count, test_print_state(profile_state),
239 profile_name, (essid)? essid : "");
242 profile_list[profile_count] = profile_h;
245 printf("%d. state:[%s], profile name : %s\n",
246 profile_count, test_print_state(profile_state), profile_name);
248 profile_list[profile_count] = profile_h;
252 g_free(profile_name);
253 if (profile_count >= 100)
260 if (test_get_user_int("Input profile number(Enter for cancel) :", &input) == false ||
261 input >= profile_count ||
263 printf("Wrong number!!\n");
268 *profile = profile_list[input];
273 static int test_update_cellular_info(connection_profile_h profile)
276 char input_str1[100] = {0,};
277 char input_str2[100] = {0,};
281 if (test_get_user_int("Input Network Type (internet:1, MMS:2, Prepaid internet:3, "
282 "Prepaid MMS:4, Tethering:5, Application:6)"
283 " - (Enter for skip) :", &input_int)) {
286 rv = connection_profile_set_cellular_service_type(profile,
287 CONNECTION_CELLULAR_SERVICE_TYPE_INTERNET);
290 rv = connection_profile_set_cellular_service_type(profile,
291 CONNECTION_CELLULAR_SERVICE_TYPE_MMS);
294 rv = connection_profile_set_cellular_service_type(profile,
295 CONNECTION_CELLULAR_SERVICE_TYPE_PREPAID_INTERNET);
298 rv = connection_profile_set_cellular_service_type(profile,
299 CONNECTION_CELLULAR_SERVICE_TYPE_PREPAID_MMS);
302 rv = connection_profile_set_cellular_service_type(profile,
303 CONNECTION_CELLULAR_SERVICE_TYPE_TETHERING);
306 rv = connection_profile_set_cellular_service_type(profile,
307 CONNECTION_CELLULAR_SERVICE_TYPE_APPLICATION);
313 if (rv != CONNECTION_ERROR_NONE)
318 if (test_get_user_string("Input Apn - (Enter for skip) :", input_str1, 100)) {
319 rv = connection_profile_set_cellular_apn(profile, input_str1);
320 if (rv != CONNECTION_ERROR_NONE)
324 if (test_get_user_string("Input Proxy - (Enter for skip) :", input_str1, 100)) {
325 rv = connection_profile_set_proxy_address(profile, CONNECTION_ADDRESS_FAMILY_IPV4, input_str1);
326 if (rv != CONNECTION_ERROR_NONE)
330 if (test_get_user_string("Input HomeURL - (Enter for skip) :", input_str1, 100)) {
331 rv = connection_profile_set_cellular_home_url(profile, input_str1);
332 if (rv != CONNECTION_ERROR_NONE)
336 if (test_get_user_int("Input AuthType(0:NONE 1:PAP 2:CHAP) - (Enter for skip) :", &input_int)) {
339 rv = connection_profile_set_cellular_auth_info(profile,
340 CONNECTION_CELLULAR_AUTH_TYPE_NONE, "", "");
341 if (rv != CONNECTION_ERROR_NONE)
346 type_val = CONNECTION_CELLULAR_AUTH_TYPE_PAP;
349 if (input_int == 2) type_val = CONNECTION_CELLULAR_AUTH_TYPE_CHAP;
351 if (test_get_user_string("Input AuthId(Enter for skip) :", input_str1, 100) == false)
353 if (test_get_user_string("Input AuthPwd(Enter for skip) :", input_str2, 100) == false)
356 rv = connection_profile_set_cellular_auth_info(profile, type_val, input_str1, input_str2);
357 if (rv != CONNECTION_ERROR_NONE)
365 static int test_update_wifi_info(connection_profile_h profile)
368 char input_str[100] = {0,};
370 if (test_get_user_string("Input Passphrase - (Enter for skip) :", input_str, 100)) {
371 rv = connection_profile_set_wifi_passphrase(profile, input_str);
372 if (rv != CONNECTION_ERROR_NONE)
379 static int test_update_ip_info(connection_profile_h profile)
382 char input_str[100] = {0,};
384 if (test_get_user_string("Input IP Address - (Enter for skip) :", input_str, 100)) {
385 rv = connection_profile_set_ip_address(profile,
386 CONNECTION_ADDRESS_FAMILY_IPV4,
388 if (rv != CONNECTION_ERROR_NONE)
392 if (test_get_user_string("Input Netmask - (Enter for skip) :", input_str, 100)) {
393 rv = connection_profile_set_subnet_mask(profile,
394 CONNECTION_ADDRESS_FAMILY_IPV4,
396 if (rv != CONNECTION_ERROR_NONE)
400 if (test_get_user_string("Input Gateway - (Enter for skip) :", input_str, 100)) {
401 rv = connection_profile_set_gateway_address(profile,
402 CONNECTION_ADDRESS_FAMILY_IPV4,
404 if (rv != CONNECTION_ERROR_NONE)
408 if (test_get_user_string("Input DNS 1 Address - (Enter for skip) :", input_str, 100)) {
409 rv = connection_profile_set_dns_address(profile,
411 CONNECTION_ADDRESS_FAMILY_IPV4,
413 if (rv != CONNECTION_ERROR_NONE)
416 if (test_get_user_string("Input DNS 2 Address - (Enter for skip) :", input_str, 100)) {
417 rv = connection_profile_set_dns_address(profile,
419 CONNECTION_ADDRESS_FAMILY_IPV4,
421 if (rv != CONNECTION_ERROR_NONE)
429 static int test_update_proxy_info(connection_profile_h profile)
433 char input_str[100] = {0,};
435 if (test_get_user_int("Input Proxy Type (1:direct, 2:auto, 3:manual)"
436 " - (Enter for skip) :", &input_int)) {
439 rv = connection_profile_set_proxy_type(profile,
440 CONNECTION_PROXY_TYPE_DIRECT);
442 if (rv != CONNECTION_ERROR_NONE)
447 rv = connection_profile_set_proxy_type(profile,
448 CONNECTION_PROXY_TYPE_AUTO);
451 rv = connection_profile_set_proxy_type(profile,
452 CONNECTION_PROXY_TYPE_MANUAL);
458 if (rv != CONNECTION_ERROR_NONE)
461 if (test_get_user_string("Input auto Proxy URL or Proxy address"
462 " - (Enter for skip) :", input_str, 100)) {
463 rv = connection_profile_set_proxy_address(profile,
464 CONNECTION_ADDRESS_FAMILY_IPV4,
466 if (rv != CONNECTION_ERROR_NONE)
476 static int test_update_network_info(connection_profile_h profile)
481 if (test_get_user_int("Input IPv4 Address Type (DHCP:1, Static:2)"
482 " - (Enter for skip) :", &input_int)) {
485 rv = connection_profile_set_ip_config_type(profile,
486 CONNECTION_ADDRESS_FAMILY_IPV4,
487 CONNECTION_IP_CONFIG_TYPE_DYNAMIC);
490 rv = connection_profile_set_ip_config_type(profile,
491 CONNECTION_ADDRESS_FAMILY_IPV4,
492 CONNECTION_IP_CONFIG_TYPE_STATIC);
493 if (rv != CONNECTION_ERROR_NONE)
496 if (test_update_ip_info(profile) == -1)
499 if (test_update_proxy_info(profile) == -1)
506 if (rv != CONNECTION_ERROR_NONE)
514 static void test_print_cellular_info(connection_profile_h profile)
516 connection_cellular_network_type_e network_type;
517 connection_cellular_service_type_e service_type;
519 connection_cellular_auth_type_e auth_type;
520 char *user_name = NULL;
521 char *password = NULL;
522 char *home_url = NULL;
523 bool roaming = false;
525 bool editable = false;
527 if (connection_profile_get_cellular_network_type(profile, &network_type) != CONNECTION_ERROR_NONE)
528 printf("Fail to get cellular network type!\n");
530 printf("Cellular network type : %d\n", network_type);
532 if (connection_profile_get_cellular_service_type(profile, &service_type) != CONNECTION_ERROR_NONE)
533 printf("Fail to get cellular service type!\n");
535 printf("Cellular service type : %d\n", service_type);
537 if (connection_profile_get_cellular_apn(profile, &apn) != CONNECTION_ERROR_NONE)
538 printf("Fail to get cellular APN!\n");
540 printf("Cellular APN : %s\n", apn);
544 if (connection_profile_get_cellular_auth_info(profile, &auth_type, &user_name, &password) != CONNECTION_ERROR_NONE)
545 printf("Fail to get auth info!\n");
547 printf("Cellular auth type : %d\n", auth_type);
548 printf("Cellular user_name : %s\n", user_name);
549 printf("Cellular password : %s\n", password);
554 if (connection_profile_get_cellular_home_url(profile, &home_url) != CONNECTION_ERROR_NONE)
555 printf("Fail to get cellular home url!\n");
557 printf("Cellular home url : %s\n", home_url);
561 if (connection_profile_is_cellular_roaming(profile, &roaming) != CONNECTION_ERROR_NONE)
562 printf("Fail to get cellular is roaming!\n");
564 printf("Cellular roaming : %s\n", roaming ? "true" : "false");
566 if (connection_profile_is_cellular_hidden(profile, &hidden) != CONNECTION_ERROR_NONE)
567 printf("Fail to get cellular hidden state!\n");
569 printf("Cellular hidden : %s\n", hidden ? "true" : "false");
571 if (connection_profile_is_cellular_editable(profile, &editable) != CONNECTION_ERROR_NONE)
572 printf("Fail to get cellular editing state!\n");
574 printf("Cellular editable : %s\n", editable ? "true" : "false");
577 static void test_print_wifi_info(connection_profile_h profile)
584 connection_wifi_security_type_e security_type;
585 connection_wifi_encryption_type_e encryption_type;
586 bool pass_required = false;
587 bool wps_supported = false;
589 if (connection_profile_get_wifi_essid(profile, &essid) != CONNECTION_ERROR_NONE)
590 printf("Fail to get Wi-Fi essid!\n");
592 printf("Wi-Fi essid : %s\n", essid);
596 if (connection_profile_get_wifi_bssid(profile, &bssid) != CONNECTION_ERROR_NONE)
597 printf("Fail to get Wi-Fi bssid!\n");
599 printf("Wi-Fi bssid : %s\n", bssid);
603 if (connection_profile_get_wifi_rssi(profile, &rssi) != CONNECTION_ERROR_NONE)
604 printf("Fail to get Wi-Fi rssi!\n");
606 printf("Wi-Fi rssi : %d\n", rssi);
608 if (connection_profile_get_wifi_frequency(profile, &frequency) != CONNECTION_ERROR_NONE)
609 printf("Fail to get Wi-Fi frequency!\n");
611 printf("Wi-Fi frequency : %d\n", frequency);
613 if (connection_profile_get_wifi_max_speed(profile, &max_speed) != CONNECTION_ERROR_NONE)
614 printf("Fail to get Wi-Fi max speed!\n");
616 printf("Wi-Fi max speed : %d\n", max_speed);
618 if (connection_profile_get_wifi_security_type(profile, &security_type) != CONNECTION_ERROR_NONE)
619 printf("Fail to get Wi-Fi security type!\n");
621 printf("Wi-Fi security type : %d\n", security_type);
623 if (connection_profile_get_wifi_encryption_type(profile, &encryption_type) != CONNECTION_ERROR_NONE)
624 printf("Fail to get Wi-Fi encryption type!\n");
626 printf("Wi-Fi encryption type : %d\n", encryption_type);
628 if (connection_profile_is_wifi_passphrase_required(profile, &pass_required) != CONNECTION_ERROR_NONE)
629 printf("Fail to get Wi-Fi passphrase required!\n");
631 printf("Wi-Fi passphrase required : %s\n", pass_required ? "true" : "false");
633 if (connection_profile_is_wifi_wps_supported(profile, &wps_supported) != CONNECTION_ERROR_NONE)
634 printf("Fail to get Wi-Fi wps info\n");
636 printf("Wi-Fi wps supported : %s\n", wps_supported ? "true" : "false");
639 static void test_print_network_info(connection_profile_h profile)
641 char *interface_name = NULL;
642 connection_ip_config_type_e ip_type;
645 char *gateway = NULL;
648 connection_proxy_type_e proxy_type;
651 if (connection_profile_get_network_interface_name(profile, &interface_name) != CONNECTION_ERROR_NONE)
652 printf("Fail to get interface name!\n");
654 printf("Interface name : %s\n", interface_name);
655 g_free(interface_name);
658 if (connection_profile_get_ip_config_type(profile, CONNECTION_ADDRESS_FAMILY_IPV4, &ip_type) != CONNECTION_ERROR_NONE)
659 printf("Fail to get ipconfig type!\n");
661 printf("Ipconfig type : %d\n", ip_type);
663 if (connection_profile_get_ip_address(profile, CONNECTION_ADDRESS_FAMILY_IPV4, &ip) != CONNECTION_ERROR_NONE)
664 printf("Fail to get IP address!\n");
666 printf("IP address : %s\n", ip);
670 if (connection_profile_get_subnet_mask(profile, CONNECTION_ADDRESS_FAMILY_IPV4, &subnet) != CONNECTION_ERROR_NONE)
671 printf("Fail to get subnet mask!\n");
673 printf("Subnet mask : %s\n", subnet);
677 if (connection_profile_get_gateway_address(profile, CONNECTION_ADDRESS_FAMILY_IPV4, &gateway) != CONNECTION_ERROR_NONE)
678 printf("Fail to get gateway!\n");
680 printf("Gateway : %s\n", gateway);
684 if (connection_profile_get_dns_address(profile, 1, CONNECTION_ADDRESS_FAMILY_IPV4, &dns1) != CONNECTION_ERROR_NONE)
685 printf("Fail to get DNS1!\n");
687 printf("DNS1 : %s\n", dns1);
691 if (connection_profile_get_dns_address(profile, 2, CONNECTION_ADDRESS_FAMILY_IPV4, &dns2) != CONNECTION_ERROR_NONE)
692 printf("Fail to get DNS2!\n");
694 printf("DNS2 : %s\n", dns2);
698 if (connection_profile_get_proxy_type(profile, &proxy_type) != CONNECTION_ERROR_NONE)
699 printf("Fail to get proxy type!\n");
701 printf("Proxy type : %d\n", proxy_type);
703 if (connection_profile_get_proxy_address(profile, CONNECTION_ADDRESS_FAMILY_IPV4, &proxy) != CONNECTION_ERROR_NONE)
704 printf("Fail to get proxy!\n");
706 printf("Proxy : %s\n", proxy);
711 int test_register_client(void)
714 int err = connection_create(&connection);
716 if (CONNECTION_ERROR_NONE == err) {
717 connection_set_type_changed_cb(connection, test_type_changed_callback, NULL);
718 connection_set_ip_address_changed_cb(connection, test_ip_changed_callback, NULL);
719 connection_set_proxy_address_changed_cb(connection, test_proxy_changed_callback, NULL);
721 printf("Client registration failed %d\n", err);
725 printf("Client registration success\n");
729 int test_deregister_client(void)
733 connection_profile_h profile;
735 if (connection != NULL)
736 rv = connection_destroy(connection);
738 printf("Cannot deregister : Handle is NULL\n");
739 rv = CONNECTION_ERROR_INVALID_OPERATION;
742 if (rv != CONNECTION_ERROR_NONE){
743 printf("Client deregistration fail [%d]\n", rv);
748 for (list = state_cb_list; list; list = list->next) {
749 profile = list->data;
750 connection_profile_destroy(profile);
753 g_slist_free(state_cb_list);
754 state_cb_list = NULL;
758 printf("Client deregistration success\n");
763 int test_get_network_state(void)
766 connection_type_e net_state;
768 rv = connection_get_type(connection, &net_state);
770 if (rv != CONNECTION_ERROR_NONE) {
771 printf("Fail to get network state [%d]\n", rv);
775 printf("Retval = %d network connection state [%d]\n", rv, net_state);
780 int test_get_cellular_state(void)
783 connection_cellular_state_e cellular_state;
785 rv = connection_get_cellular_state(connection, &cellular_state);
787 if (rv != CONNECTION_ERROR_NONE) {
788 printf("Fail to get Cellular state [%d]\n", rv);
792 printf("Retval = %d Cellular state [%d]\n", rv, cellular_state);
797 int test_get_wifi_state(void)
800 connection_wifi_state_e wifi_state;
802 rv = connection_get_wifi_state(connection, &wifi_state);
804 if (rv != CONNECTION_ERROR_NONE) {
805 printf("Fail to get WiFi state [%d]\n", rv);
809 printf("Retval = %d WiFi state [%d]\n", rv, wifi_state);
814 int test_get_current_proxy(void)
816 char *proxy_addr = NULL;
818 connection_get_proxy(connection, CONNECTION_ADDRESS_FAMILY_IPV4, &proxy_addr);
820 if (proxy_addr == NULL) {
821 printf("Proxy address does not exist\n");
825 printf("Current Proxy [%s]\n", proxy_addr);
831 int test_get_current_ip(void)
833 char *ip_addr = NULL;
835 connection_get_ip_address(connection, CONNECTION_ADDRESS_FAMILY_IPV4, &ip_addr);
837 if (ip_addr == NULL) {
838 printf("IP address does not exist\n");
842 printf("IPv4 address : %s\n", ip_addr);
848 int test_get_call_statistics_info(void)
852 connection_get_statistics(connection, CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_LAST_RECEIVED_DATA, &rv);
853 printf("last recv data size [%lld]\n", rv);
854 connection_get_statistics(connection, CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_LAST_SENT_DATA, &rv);
855 printf("last sent data size [%lld]\n",rv );
856 connection_get_statistics(connection, CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_TOTAL_RECEIVED_DATA, &rv);
857 printf("total received data size [%lld]\n",rv );
858 connection_get_statistics(connection, CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_TOTAL_SENT_DATA, &rv);
859 printf("total sent data size [%lld]\n", rv);
864 int test_get_wifi_call_statistics_info(void)
868 connection_get_statistics(connection, CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_LAST_RECEIVED_DATA, &rv);
869 printf("WiFi last recv data size [%lld]\n", rv);
870 connection_get_statistics(connection, CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_LAST_SENT_DATA, &rv);
871 printf("WiFi last sent data size [%lld]\n",rv );
872 connection_get_statistics(connection, CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_TOTAL_RECEIVED_DATA, &rv);
873 printf("WiFi total received data size [%lld]\n",rv );
874 connection_get_statistics(connection, CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_TOTAL_SENT_DATA, &rv);
875 printf("WiFi total sent data size [%lld]\n", rv);
880 int test_get_profile_list(void)
882 if (test_get_user_selected_profile(NULL, false) == false)
888 int test_get_default_profile_list(void)
891 char *profile_name = NULL;
892 connection_profile_iterator_h profile_iter;
893 connection_profile_h profile_h;
894 connection_cellular_service_type_e service_type;
895 bool is_default = false;
897 rv = connection_get_profile_iterator(connection, CONNECTION_ITERATOR_TYPE_DEFAULT, &profile_iter);
898 if (rv != CONNECTION_ERROR_NONE) {
899 printf("Fail to get profile iterator [%s]\n", test_print_error(rv));
903 while (connection_profile_iterator_has_next(profile_iter)) {
904 if (connection_profile_iterator_next(profile_iter, &profile_h) != CONNECTION_ERROR_NONE) {
905 printf("Fail to get profile handle\n");
909 if (connection_profile_get_name(profile_h, &profile_name) != CONNECTION_ERROR_NONE) {
910 printf("Fail to get profile name\n");
913 printf("profile name : %s\n", profile_name);
914 g_free(profile_name);
916 if (connection_profile_get_cellular_service_type(profile_h, &service_type) != CONNECTION_ERROR_NONE) {
917 printf("Fail to get profile service type\n");
920 printf("service type : %d\n", service_type);
922 if (connection_profile_is_cellular_default(profile_h, &is_default) != CONNECTION_ERROR_NONE) {
923 printf("Fail to get profile subscriber id\n");
926 printf("Default : %d\n", is_default);
932 int test_get_connected_profile_list(void)
935 char *profile_name = NULL;
936 connection_profile_iterator_h profile_iter;
937 connection_profile_h profile_h;
938 bool is_default = false;
939 connection_profile_type_e type;
941 rv = connection_get_profile_iterator(connection, CONNECTION_ITERATOR_TYPE_CONNECTED, &profile_iter);
942 if (rv != CONNECTION_ERROR_NONE) {
943 printf("Fail to get profile iterator [%d]\n", rv);
947 while (connection_profile_iterator_has_next(profile_iter)) {
948 if (connection_profile_iterator_next(profile_iter, &profile_h) != CONNECTION_ERROR_NONE) {
949 printf("Fail to get profile handle\n");
953 if (connection_profile_get_name(profile_h, &profile_name) != CONNECTION_ERROR_NONE) {
954 printf("Fail to get profile name\n");
957 printf("profile name : %s\n", profile_name);
958 g_free(profile_name);
960 if (connection_profile_get_type(profile_h, &type) != CONNECTION_ERROR_NONE) {
961 printf("Fail to get profile type\n");
964 printf("profile type is %d\n", type);
966 if (type == CONNECTION_PROFILE_TYPE_CELLULAR) {
967 if (connection_profile_is_cellular_default(profile_h, &is_default) != CONNECTION_ERROR_NONE) {
968 printf("Fail to get profile is default\n");
971 printf("[%s]\n", is_default ? "default" : "not default");
978 int test_get_current_profile(void)
981 char *profile_name = NULL;
982 connection_profile_h profile_h;
984 rv = connection_get_current_profile(connection, &profile_h);
985 if (rv != CONNECTION_ERROR_NONE) {
986 printf("Fail to get profile iterator [%d]\n", rv);
990 if (connection_profile_get_name(profile_h, &profile_name) != CONNECTION_ERROR_NONE) {
991 printf("Fail to get profile name\n");
994 printf("profile name : %s\n", profile_name);
995 g_free(profile_name);
997 connection_profile_destroy(profile_h);
1002 int test_open_profile(void)
1004 connection_profile_h profile;
1006 printf("\n** Choose a profile to open. **\n");
1008 if (test_get_user_selected_profile(&profile, true) == false)
1011 if (connection_open_profile(connection, profile, test_connection_opened_callback, NULL) != CONNECTION_ERROR_NONE) {
1012 printf("Connection open Failed!!\n");
1019 int test_get_default_cellular_service_type(void)
1024 connection_profile_h profile;
1025 char *profile_name = NULL;
1027 rv = test_get_user_int("Input profile type to get"
1028 "(1:Internet, 2:MMS, 3:Prepaid internet, 4:Prepaid MMS, 5:Tethering):", &input);
1031 printf("Invalid input!!\n");
1037 service_type = CONNECTION_CELLULAR_SERVICE_TYPE_INTERNET;
1040 service_type = CONNECTION_CELLULAR_SERVICE_TYPE_MMS;
1043 service_type = CONNECTION_CELLULAR_SERVICE_TYPE_PREPAID_INTERNET;
1046 service_type = CONNECTION_CELLULAR_SERVICE_TYPE_PREPAID_MMS;
1049 service_type = CONNECTION_CELLULAR_SERVICE_TYPE_TETHERING;
1052 printf("Wrong number!!\n");
1056 if (connection_get_default_cellular_service_profile(connection, service_type, &profile) != CONNECTION_ERROR_NONE)
1059 if (connection_profile_get_name(profile, &profile_name) != CONNECTION_ERROR_NONE) {
1060 printf("Fail to get profile name\n");
1061 connection_profile_destroy(profile);
1064 printf("Default profile name : %s\n", profile_name);
1065 g_free(profile_name);
1067 connection_profile_destroy(profile);
1072 int test_set_default_cellular_service_type(void)
1074 connection_profile_h profile;
1075 connection_cellular_service_type_e type;
1078 rv = test_get_user_int("Input API type (1:sync, 2:async)", &input);
1080 if (rv == false || (input != 1 && input != 2)) {
1081 printf("Invalid input!!\n");
1085 printf("\n** Choose a profile to set default service(internet or prepaid internet type only). **\n");
1087 if (test_get_user_selected_profile(&profile, true) == false)
1090 if (connection_profile_get_cellular_service_type(profile, &type) != CONNECTION_ERROR_NONE) {
1091 printf("Fail to get cellular service type\n");
1096 if (connection_set_default_cellular_service_profile(connection, type, profile) != CONNECTION_ERROR_NONE)
1099 if (connection_set_default_cellular_service_profile_async(connection,
1100 type, profile, test_connection_set_default_callback, NULL) != CONNECTION_ERROR_NONE)
1107 int test_close_profile(void)
1109 connection_profile_h profile;
1111 printf("\n** Choose a profile to close. **\n");
1113 if (test_get_user_selected_profile(&profile, true) == false)
1116 if (connection_close_profile(connection, profile, test_connection_closed_callback, NULL) != CONNECTION_ERROR_NONE) {
1117 printf("Connection close Failed!!\n");
1124 int test_add_profile(void)
1127 connection_profile_h profile;
1128 char input_str[100] = {0,};
1130 if (test_get_user_string("Input Keyword - (Enter for skip) :", input_str, 100) == false)
1133 rv = connection_profile_create(CONNECTION_PROFILE_TYPE_CELLULAR, input_str, &profile);
1134 if (rv != CONNECTION_ERROR_NONE)
1135 RETURN_FAIL_DESTROY(profile);
1137 if (test_update_cellular_info(profile) == -1)
1138 RETURN_FAIL_DESTROY(profile);
1140 rv = connection_add_profile(connection, profile);
1141 if (rv != CONNECTION_ERROR_NONE)
1142 RETURN_FAIL_DESTROY(profile);
1144 connection_profile_destroy(profile);
1148 int test_remove_profile(void)
1150 connection_profile_h profile;
1152 printf("\n** Choose a profile to remove. **\n");
1153 if (test_get_user_selected_profile(&profile, true) == false)
1156 if (connection_remove_profile(connection, profile) != CONNECTION_ERROR_NONE) {
1157 printf("Remove profile Failed!!\n");
1164 int test_update_profile(void)
1168 connection_profile_type_e prof_type;
1169 connection_profile_h profile;
1171 printf("\n** Choose a profile to update. **\n");
1172 if (test_get_user_selected_profile(&profile, true) == false)
1175 if (connection_profile_get_type(profile, &prof_type) != CONNECTION_ERROR_NONE)
1178 switch (prof_type) {
1179 case CONNECTION_PROFILE_TYPE_CELLULAR:
1180 if (test_update_cellular_info(profile) == -1)
1184 case CONNECTION_PROFILE_TYPE_WIFI:
1185 if (test_update_wifi_info(profile) == -1)
1188 if (test_update_network_info(profile) == -1)
1192 case CONNECTION_PROFILE_TYPE_ETHERNET:
1193 if (test_update_network_info(profile) == -1)
1197 case CONNECTION_PROFILE_TYPE_BT:
1198 printf("Not supported!\n");
1204 rv = connection_update_profile(connection, profile);
1205 if (rv != CONNECTION_ERROR_NONE)
1211 int test_get_profile_info(void)
1213 connection_profile_type_e prof_type;
1214 connection_profile_state_e profile_state;
1215 connection_profile_h profile;
1216 char *profile_name = NULL;
1218 printf("\n** Choose a profile to print. **\n");
1219 if (test_get_user_selected_profile(&profile, true) == false)
1222 if (connection_profile_get_name(profile, &profile_name) != CONNECTION_ERROR_NONE) {
1223 printf("Fail to get profile name\n");
1226 printf("Profile Name : %s\n", profile_name);
1227 g_free(profile_name);
1230 if (connection_profile_get_state(profile, &profile_state) != CONNECTION_ERROR_NONE) {
1231 printf("Fail to get profile state\n");
1234 printf("Profile State : %s\n", test_print_state(profile_state));
1237 if (connection_profile_get_type(profile, &prof_type) != CONNECTION_ERROR_NONE)
1240 switch (prof_type) {
1241 case CONNECTION_PROFILE_TYPE_CELLULAR:
1242 printf("Profile Type : Cellular\n");
1243 test_print_cellular_info(profile);
1245 case CONNECTION_PROFILE_TYPE_WIFI:
1246 printf("Profile Type : Wi-Fi\n");
1247 test_print_wifi_info(profile);
1249 case CONNECTION_PROFILE_TYPE_ETHERNET:
1250 printf("Profile Type : Ethernet\n");
1252 case CONNECTION_PROFILE_TYPE_BT:
1253 printf("Profile Type : Bluetooth\n");
1259 test_print_network_info(profile);
1264 int test_refresh_profile_info(void)
1266 connection_profile_type_e prof_type;
1267 connection_profile_state_e profile_state;
1268 connection_profile_h profile;
1269 char *profile_name = NULL;
1271 printf("\n** Choose a profile to refresh. **\n");
1272 if (test_get_user_selected_profile(&profile, true) == false)
1275 if (connection_profile_refresh(profile) != CONNECTION_ERROR_NONE)
1278 if (connection_profile_get_name(profile, &profile_name) != CONNECTION_ERROR_NONE) {
1279 printf("Fail to get profile name\n");
1282 printf("Profile Name : %s\n", profile_name);
1283 g_free(profile_name);
1286 if (connection_profile_get_state(profile, &profile_state) != CONNECTION_ERROR_NONE) {
1287 printf("Fail to get profile state\n");
1290 printf("Profile State : %s\n", test_print_state(profile_state));
1293 if (connection_profile_get_type(profile, &prof_type) != CONNECTION_ERROR_NONE)
1296 switch (prof_type) {
1297 case CONNECTION_PROFILE_TYPE_CELLULAR:
1298 printf("Profile Type : Cellular\n");
1299 test_print_cellular_info(profile);
1301 case CONNECTION_PROFILE_TYPE_WIFI:
1302 printf("Profile Type : Wi-Fi\n");
1303 test_print_wifi_info(profile);
1305 case CONNECTION_PROFILE_TYPE_ETHERNET:
1306 printf("Profile Type : Ethernet\n");
1308 case CONNECTION_PROFILE_TYPE_BT:
1309 printf("Profile Type : Bluetooth\n");
1315 test_print_network_info(profile);
1320 int test_set_state_changed_callback()
1322 connection_profile_h profile;
1323 connection_profile_h profile_clone;
1325 printf("\n** Choose a profile to set callback. **\n");
1326 if (test_get_user_selected_profile(&profile, true) == false)
1329 if (connection_profile_clone(&profile_clone, profile) != CONNECTION_ERROR_NONE)
1332 if (connection_profile_set_state_changed_cb(profile,
1333 test_profile_state_callback, profile_clone) != CONNECTION_ERROR_NONE) {
1334 connection_profile_destroy(profile_clone);
1338 state_cb_list = g_slist_append(state_cb_list, profile_clone);
1343 int test_unset_state_changed_callback()
1345 connection_profile_h profile;
1347 char *profile_name = NULL;
1351 printf("\n** Choose a profile to unset callback. **\n");
1352 for (list = state_cb_list; list; list = list->next) {
1353 profile = list->data;
1354 if (connection_profile_get_name(profile, &profile_name) != CONNECTION_ERROR_NONE) {
1355 printf("Fail to get profile name!\n");
1358 printf("%d. %s\n", count, profile_name);
1359 g_free(profile_name);
1365 if (test_get_user_int("Input profile number(Enter for cancel) :", &input) == false ||
1368 printf("Wrong number!!\n");
1373 for (list = state_cb_list; list; list = list->next) {
1374 if (count == input) {
1375 profile = list->data;
1385 if (connection_profile_unset_state_changed_cb(profile) != CONNECTION_ERROR_NONE)
1388 state_cb_list = g_slist_remove(state_cb_list, profile);
1389 connection_profile_destroy(profile);
1394 int test_reset_call_statistics_info(void)
1396 int ret = CONNECTION_ERROR_NONE;
1398 ret = connection_reset_statistics(connection, CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_LAST_RECEIVED_DATA);
1399 printf("reset last recv data size [%d]\n", ret);
1400 ret = connection_reset_statistics(connection, CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_LAST_SENT_DATA);
1401 printf("last sent data size [%d]\n", ret);
1402 ret = connection_reset_statistics(connection, CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_TOTAL_RECEIVED_DATA);
1403 printf("total received data size [%d]\n", ret);
1404 ret = connection_reset_statistics(connection, CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_TOTAL_SENT_DATA);
1405 printf("total sent data size [%d]\n", ret);
1410 int test_reset_wifi_call_statistics_info(void)
1412 int ret = CONNECTION_ERROR_NONE;
1414 ret = connection_reset_statistics(connection, CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_LAST_SENT_DATA);
1415 printf("WiFi last sent data size [%d]\n", ret);
1416 ret = connection_reset_statistics(connection, CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_LAST_RECEIVED_DATA);
1417 printf("WiFi last recv data size [%d]\n", ret);
1418 ret = connection_reset_statistics(connection, CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_TOTAL_SENT_DATA);
1419 printf("WiFi total sent data size [%d]\n", ret);
1420 ret = connection_reset_statistics(connection, CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_TOTAL_RECEIVED_DATA);
1421 printf("WiFi total received data size [%d]\n", ret);
1426 int test_add_route(void)
1432 if (test_get_user_string("Input IP - (Enter for skip) :", ip_addr, 30) == false)
1435 if (test_get_user_string("Input Interface name - (Enter for skip) :", if_name, 40) == false)
1438 rv = connection_add_route(connection, if_name, ip_addr);
1439 if (rv != CONNECTION_ERROR_NONE) {
1440 printf("Fail to get add new route [%d]\n", rv);
1447 int test_remove_route(void)
1453 if (test_get_user_string("Input IP - (Enter for skip) :", ip_addr, 30) == false)
1456 if (test_get_user_string("Input Interface name - (Enter for skip) :", if_name, 40) == false)
1459 rv = connection_remove_route(connection, if_name, ip_addr);
1460 if (rv != CONNECTION_ERROR_NONE) {
1461 printf("Fail to remove the route [%s]\n", test_print_error(rv));
1468 int test_get_bt_state(void)
1471 connection_bt_state_e bt_state;
1473 rv = connection_get_bt_state(connection, &bt_state);
1475 if (rv != CONNECTION_ERROR_NONE) {
1476 printf("Fail to get Bluetooth state [%d]\n", rv);
1480 printf("Retval = %d, Bluetooth state [%d]\n", rv, bt_state);
1485 int test_get_profile_id(void)
1487 connection_profile_h profile;
1490 printf("\n** Choose a profile to see profile id. **\n");
1491 if (test_get_user_selected_profile(&profile, true) == false)
1494 if (connection_profile_get_id(profile, &profile_id) != CONNECTION_ERROR_NONE) {
1495 printf("Fail to get profile name\n");
1498 printf("Profile id : %s\n", profile_id);
1505 int test_reset_profile(void)
1507 int type, sim_id, rv;
1509 rv = test_get_user_int("Input reset type (0:default profile reset, 1:delete profile reset)", &type);
1511 if (rv == false || (type != 0 && type != 1)) {
1512 printf("Invalid input!!\n");
1516 rv = test_get_user_int("Input SIM id to reset (0:SIM1, 1:SIM2)", &sim_id);
1518 if (rv == false || (sim_id != 0 && sim_id != 1)) {
1519 printf("Invalid input!!\n");
1523 if (connection_reset_profile(connection, type, sim_id, test_connection_reset_profile_callback, NULL) != CONNECTION_ERROR_NONE) {
1530 int main(int argc, char **argv)
1532 GMainLoop *mainloop;
1533 mainloop = g_main_loop_new (NULL, FALSE);
1535 GIOChannel *channel = g_io_channel_unix_new(0);
1536 g_io_add_watch(channel, (G_IO_IN|G_IO_ERR|G_IO_HUP|G_IO_NVAL), test_thread,NULL );
1538 printf("Test Thread created...\n");
1540 g_main_loop_run (mainloop);
1545 gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data)
1550 memset(a, '\0', 100);
1551 printf("Event received from stdin\n");
1553 rv = read(0, a, 100);
1555 if (rv < 0 || a[0] == '0') {
1556 if (connection != NULL)
1557 test_deregister_client();
1562 if (*a == '\n' || *a == '\r'){
1563 printf("\n\n Network Connection API Test App\n\n");
1564 printf("Options..\n");
1565 printf("1 - Create Handle and set callbacks\n");
1566 printf("2 - Destroy Handle(unset callbacks automatically)\n");
1567 printf("3 - Get network state\n");
1568 printf("4 - Get cellular state (please insert SIM Card)\n");
1569 printf("5 - Get wifi state (please turn on WiFi)\n");
1570 printf("6 - Get current proxy address \n");
1571 printf("7 - Get current Ip address\n");
1572 printf("8 - Get cellular data call statistics\n");
1573 printf("9 - Get WiFi data call statistics\n");
1574 printf("a - Get Profile list\n");
1575 printf("b - Get Connected Profile list\n");
1576 printf("c - Get Current profile\n");
1577 printf("d - Open connection with profile\n");
1578 printf("e - Get default cellular service by type\n");
1579 printf("f - Set default cellular service by type\n");
1580 printf("g - Close connection with profile\n");
1581 printf("h - Add profile(Cellular only)\n");
1582 printf("i - Remove profile(Cellular:delete, WiFi:forgot)\n");
1583 printf("j - Update profile\n");
1584 printf("k - Get profile info\n");
1585 printf("l - Refresh profile info\n");
1586 printf("m - Set state changed callback\n");
1587 printf("n - Unset state changed callback\n");
1588 printf("o - Reset cellular data call statistics\n");
1589 printf("p - Reset WiFi data call statistics\n");
1590 printf("q - Add new route\n");
1591 printf("r - Get Bluetooth state\n");
1592 printf("s - Get profile id\n");
1593 printf("0 - Exit \n");
1594 printf("ENTER - Show options menu.......\n");
1599 rv = test_register_client();
1602 rv = test_deregister_client();
1605 rv = test_get_network_state();
1608 rv = test_get_cellular_state();
1611 rv = test_get_wifi_state();
1614 rv = test_get_current_proxy();
1617 rv = test_get_current_ip();
1620 rv = test_get_call_statistics_info();
1623 rv = test_get_wifi_call_statistics_info();
1626 rv = test_get_profile_list();
1629 rv = test_get_connected_profile_list();
1632 rv = test_get_current_profile();
1635 rv = test_open_profile();
1638 rv = test_get_default_cellular_service_type();
1641 rv = test_set_default_cellular_service_type();
1644 rv = test_close_profile();
1647 rv = test_add_profile();
1650 rv = test_remove_profile();
1653 rv = test_update_profile();
1656 rv = test_get_profile_info();
1659 rv = test_refresh_profile_info();
1662 rv = test_set_state_changed_callback();
1665 rv = test_unset_state_changed_callback();
1668 rv = test_reset_call_statistics_info();
1671 rv = test_reset_wifi_call_statistics_info();
1674 rv = test_add_route();
1677 rv = test_remove_route();
1680 rv = test_get_bt_state();
1683 rv = test_get_profile_id();
1686 rv = test_reset_profile();
1689 rv = test_get_default_profile_list();
1694 printf("Operation succeeded!\n");
1696 printf("Operation failed!\n");