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') {
50 buf[buf_size - 1] = '\0';
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 void test_type_changed_callback(connection_type_e type, void* user_data)
90 printf("Type changed callback, connection type : %d\n", type);
93 static void test_ip_changed_callback(const char* ipv4_address, const char* ipv6_address, void* user_data)
95 printf("IP changed callback, IPv4 address : %s, IPv6 address : %s\n",
96 ipv4_address, (ipv6_address ? ipv6_address : "NULL"));
99 static void test_proxy_changed_callback(const char* ipv4_address, const char* ipv6_address, void* user_data)
101 printf("Proxy changed callback, IPv4 address : %s, IPv6 address : %s\n",
102 ipv4_address, (ipv6_address ? ipv6_address : "NULL"));
105 static void test_profile_state_callback(connection_profile_state_e state, void* user_data)
108 connection_profile_h profile = user_data;
113 if (connection_profile_get_name(profile, &profile_name) != CONNECTION_ERROR_NONE)
116 printf("[%s] : %s\n", test_print_state(state), profile_name);
117 g_free(profile_name);
120 static void test_connection_opened_callback(connection_error_e result, void* user_data)
122 if (result == CONNECTION_ERROR_NONE)
123 printf("Connection open Succeeded\n");
125 printf("Connection open Failed, err : %d\n", result);
128 static void test_connection_closed_callback(connection_error_e result, void* user_data)
130 if (result == CONNECTION_ERROR_NONE)
131 printf("Connection close Succeeded\n");
133 printf("Connection close Failed, err : %d\n", result);
136 static bool test_get_user_selected_profile(connection_profile_h *profile, bool select)
141 connection_profile_type_e profile_type;
142 connection_profile_state_e profile_state;
143 connection_profile_iterator_h profile_iter;
144 connection_profile_h profile_h;
146 connection_profile_h profile_list[100] = {0,};
147 int profile_count = 0;
149 rv = connection_get_profile_iterator(connection, CONNECTION_ITERATOR_TYPE_REGISTERED, &profile_iter);
150 if (rv != CONNECTION_ERROR_NONE) {
151 printf("Fail to get profile iterator [%d]\n", rv);
155 while (connection_profile_iterator_has_next(profile_iter)) {
156 if (connection_profile_iterator_next(profile_iter, &profile_h) != CONNECTION_ERROR_NONE) {
157 printf("Fail to get profile handle\n");
161 if (connection_profile_get_name(profile_h, &profile_name) != CONNECTION_ERROR_NONE) {
162 printf("Fail to get profile name\n");
166 if (connection_profile_get_type(profile_h, &profile_type) != CONNECTION_ERROR_NONE) {
167 printf("Fail to get profile type\n");
168 g_free(profile_name);
172 if (connection_profile_get_state(profile_h, &profile_state) != CONNECTION_ERROR_NONE) {
173 printf("Fail to get profile state\n");
174 g_free(profile_name);
178 if (profile_type == CONNECTION_PROFILE_TYPE_WIFI) {
180 connection_profile_get_wifi_essid(profile_h, &essid);
181 printf("%d. state:[%s], profile name:%s, essid:%s\n",
182 profile_count, test_print_state(profile_state),
183 profile_name, (essid)? essid : "");
186 profile_list[profile_count] = profile_h;
189 printf("%d. state:[%s], profile name : %s\n",
190 profile_count, test_print_state(profile_state), profile_name);
192 profile_list[profile_count] = profile_h;
196 g_free(profile_name);
197 if (profile_count >= 100)
204 if (test_get_user_int("Input profile number(Enter for cancel) :", &input) == false ||
205 input >= profile_count ||
207 printf("Wrong number!!\n");
212 *profile = profile_list[input];
217 static int test_update_cellular_info(connection_profile_h profile)
220 char input_str1[100] = {0,};
221 char input_str2[100] = {0,};
225 if (test_get_user_int("Input Network Type (internet:1, MMS:2, Prepaid internet:3, "
226 "Prepaid MMS:4, Tethering:5, Application:6)"
227 " - (Enter for skip) :", &input_int)) {
230 rv = connection_profile_set_cellular_service_type(profile,
231 CONNECTION_CELLULAR_SERVICE_TYPE_INTERNET);
234 rv = connection_profile_set_cellular_service_type(profile,
235 CONNECTION_CELLULAR_SERVICE_TYPE_MMS);
238 rv = connection_profile_set_cellular_service_type(profile,
239 CONNECTION_CELLULAR_SERVICE_TYPE_PREPAID_INTERNET);
242 rv = connection_profile_set_cellular_service_type(profile,
243 CONNECTION_CELLULAR_SERVICE_TYPE_PREPAID_MMS);
246 rv = connection_profile_set_cellular_service_type(profile,
247 CONNECTION_CELLULAR_SERVICE_TYPE_TETHERING);
250 rv = connection_profile_set_cellular_service_type(profile,
251 CONNECTION_CELLULAR_SERVICE_TYPE_APPLICATION);
257 if (rv != CONNECTION_ERROR_NONE)
262 if (test_get_user_string("Input Apn - (Enter for skip) :", input_str1, 100)) {
263 rv = connection_profile_set_cellular_apn(profile, input_str1);
264 if (rv != CONNECTION_ERROR_NONE)
268 if (test_get_user_string("Input Proxy - (Enter for skip) :", input_str1, 100)) {
269 rv = connection_profile_set_proxy_address(profile, CONNECTION_ADDRESS_FAMILY_IPV4, input_str1);
270 if (rv != CONNECTION_ERROR_NONE)
274 if (test_get_user_string("Input HomeURL - (Enter for skip) :", input_str1, 100)) {
275 rv = connection_profile_set_cellular_home_url(profile, input_str1);
276 if (rv != CONNECTION_ERROR_NONE)
280 if (test_get_user_int("Input AuthType(0:NONE 1:PAP 2:CHAP) - (Enter for skip) :", &input_int)) {
283 rv = connection_profile_set_cellular_auth_info(profile,
284 CONNECTION_CELLULAR_AUTH_TYPE_NONE, "", "");
285 if (rv != CONNECTION_ERROR_NONE)
290 type_val = CONNECTION_CELLULAR_AUTH_TYPE_PAP;
293 if (input_int == 2) type_val = CONNECTION_CELLULAR_AUTH_TYPE_CHAP;
295 if (test_get_user_string("Input AuthId(Enter for skip) :", input_str1, 100) == false)
297 if (test_get_user_string("Input AuthPwd(Enter for skip) :", input_str2, 100) == false)
300 rv = connection_profile_set_cellular_auth_info(profile, type_val, input_str1, input_str2);
301 if (rv != CONNECTION_ERROR_NONE)
309 static int test_update_wifi_info(connection_profile_h profile)
312 char input_str[100] = {0,};
314 if (test_get_user_string("Input Passphrase - (Enter for skip) :", input_str, 100)) {
315 rv = connection_profile_set_wifi_passphrase(profile, input_str);
316 if (rv != CONNECTION_ERROR_NONE)
323 static int test_update_ip_info(connection_profile_h profile)
326 char input_str[100] = {0,};
328 if (test_get_user_string("Input IP Address - (Enter for skip) :", input_str, 100)) {
329 rv = connection_profile_set_ip_address(profile,
330 CONNECTION_ADDRESS_FAMILY_IPV4,
332 if (rv != CONNECTION_ERROR_NONE)
336 if (test_get_user_string("Input Netmask - (Enter for skip) :", input_str, 100)) {
337 rv = connection_profile_set_subnet_mask(profile,
338 CONNECTION_ADDRESS_FAMILY_IPV4,
340 if (rv != CONNECTION_ERROR_NONE)
344 if (test_get_user_string("Input Gateway - (Enter for skip) :", input_str, 100)) {
345 rv = connection_profile_set_gateway_address(profile,
346 CONNECTION_ADDRESS_FAMILY_IPV4,
348 if (rv != CONNECTION_ERROR_NONE)
352 if (test_get_user_string("Input DNS 1 Address - (Enter for skip) :", input_str, 100)) {
353 rv = connection_profile_set_dns_address(profile,
355 CONNECTION_ADDRESS_FAMILY_IPV4,
357 if (rv != CONNECTION_ERROR_NONE)
360 if (test_get_user_string("Input DNS 2 Address - (Enter for skip) :", input_str, 100)) {
361 rv = connection_profile_set_dns_address(profile,
363 CONNECTION_ADDRESS_FAMILY_IPV4,
365 if (rv != CONNECTION_ERROR_NONE)
373 static int test_update_proxy_info(connection_profile_h profile)
377 char input_str[100] = {0,};
379 if (test_get_user_int("Input Proxy Type (1:direct, 2:auto, 3:manual)"
380 " - (Enter for skip) :", &input_int)) {
383 rv = connection_profile_set_proxy_type(profile,
384 CONNECTION_PROXY_TYPE_DIRECT);
386 if (rv != CONNECTION_ERROR_NONE)
391 rv = connection_profile_set_proxy_type(profile,
392 CONNECTION_PROXY_TYPE_AUTO);
395 rv = connection_profile_set_proxy_type(profile,
396 CONNECTION_PROXY_TYPE_MANUAL);
402 if (rv != CONNECTION_ERROR_NONE)
405 if (test_get_user_string("Input auto Proxy URL or Proxy address"
406 " - (Enter for skip) :", input_str, 100)) {
407 rv = connection_profile_set_proxy_address(profile,
408 CONNECTION_ADDRESS_FAMILY_IPV4,
410 if (rv != CONNECTION_ERROR_NONE)
420 static int test_update_network_info(connection_profile_h profile)
425 if (test_get_user_int("Input IPv4 Address Type (DHCP:1, Static:2)"
426 " - (Enter for skip) :", &input_int)) {
429 rv = connection_profile_set_ip_config_type(profile,
430 CONNECTION_ADDRESS_FAMILY_IPV4,
431 CONNECTION_IP_CONFIG_TYPE_DYNAMIC);
434 rv = connection_profile_set_ip_config_type(profile,
435 CONNECTION_ADDRESS_FAMILY_IPV4,
436 CONNECTION_IP_CONFIG_TYPE_STATIC);
437 if (rv != CONNECTION_ERROR_NONE)
440 if (test_update_ip_info(profile) == -1)
443 if (test_update_proxy_info(profile) == -1)
450 if (rv != CONNECTION_ERROR_NONE)
458 static void test_print_cellular_info(connection_profile_h profile)
460 connection_cellular_network_type_e network_type;
461 connection_cellular_service_type_e service_type;
463 connection_cellular_auth_type_e auth_type;
464 char *user_name = NULL;
465 char *password = NULL;
466 char *home_url = NULL;
467 bool roaming = false;
469 if (connection_profile_get_cellular_network_type(profile, &network_type) != CONNECTION_ERROR_NONE)
470 printf("Fail to get cellular network type!\n");
472 printf("Cellular network type : %d\n", network_type);
474 if (connection_profile_get_cellular_service_type(profile, &service_type) != CONNECTION_ERROR_NONE)
475 printf("Fail to get cellular service type!\n");
477 printf("Cellular service type : %d\n", service_type);
479 if (connection_profile_get_cellular_apn(profile, &apn) != CONNECTION_ERROR_NONE)
480 printf("Fail to get cellular APN!\n");
482 printf("Cellular APN : %s\n", apn);
486 if (connection_profile_get_cellular_auth_info(profile, &auth_type, &user_name, &password) != CONNECTION_ERROR_NONE)
487 printf("Fail to get auth info!\n");
489 printf("Cellular auth type : %d\n", auth_type);
490 printf("Cellular user_name : %s\n", user_name);
491 printf("Cellular password : %s\n", password);
496 if (connection_profile_get_cellular_home_url(profile, &home_url) != CONNECTION_ERROR_NONE)
497 printf("Fail to get cellular home url!\n");
499 printf("Cellular home url : %s\n", home_url);
503 if (connection_profile_is_cellular_roaming(profile, &roaming) != CONNECTION_ERROR_NONE)
504 printf("Fail to get cellular is roaming!\n");
506 printf("Cellular roaming : %s\n", roaming ? "true" : "false");
509 static void test_print_wifi_info(connection_profile_h profile)
516 connection_wifi_security_type_e security_type;
517 connection_wifi_encryption_type_e encryption_type;
518 bool pass_required = false;
519 bool wps_supported = false;
521 if (connection_profile_get_wifi_essid(profile, &essid) != CONNECTION_ERROR_NONE)
522 printf("Fail to get Wi-Fi essid!\n");
524 printf("Wi-Fi essid : %s\n", essid);
528 if (connection_profile_get_wifi_bssid(profile, &bssid) != CONNECTION_ERROR_NONE)
529 printf("Fail to get Wi-Fi bssid!\n");
531 printf("Wi-Fi bssid : %s\n", bssid);
535 if (connection_profile_get_wifi_rssi(profile, &rssi) != CONNECTION_ERROR_NONE)
536 printf("Fail to get Wi-Fi rssi!\n");
538 printf("Wi-Fi rssi : %d\n", rssi);
540 if (connection_profile_get_wifi_frequency(profile, &frequency) != CONNECTION_ERROR_NONE)
541 printf("Fail to get Wi-Fi frequency!\n");
543 printf("Wi-Fi frequency : %d\n", frequency);
545 if (connection_profile_get_wifi_max_speed(profile, &max_speed) != CONNECTION_ERROR_NONE)
546 printf("Fail to get Wi-Fi max speed!\n");
548 printf("Wi-Fi max speed : %d\n", max_speed);
550 if (connection_profile_get_wifi_security_type(profile, &security_type) != CONNECTION_ERROR_NONE)
551 printf("Fail to get Wi-Fi security type!\n");
553 printf("Wi-Fi security type : %d\n", security_type);
555 if (connection_profile_get_wifi_encryption_type(profile, &encryption_type) != CONNECTION_ERROR_NONE)
556 printf("Fail to get Wi-Fi encryption type!\n");
558 printf("Wi-Fi encryption type : %d\n", encryption_type);
560 if (connection_profile_is_wifi_passphrase_required(profile, &pass_required) != CONNECTION_ERROR_NONE)
561 printf("Fail to get Wi-Fi passphrase required!\n");
563 printf("Wi-Fi passphrase required : %s\n", pass_required ? "true" : "false");
565 if (connection_profile_is_wifi_wps_supported(profile, &wps_supported) != CONNECTION_ERROR_NONE)
566 printf("Fail to get Wi-Fi wps info\n");
568 printf("Wi-Fi wps supported : %s\n", wps_supported ? "true" : "false");
571 static void test_print_network_info(connection_profile_h profile)
573 char *interface_name = NULL;
574 connection_ip_config_type_e ip_type;
577 char *gateway = NULL;
580 connection_proxy_type_e proxy_type;
583 if (connection_profile_get_network_interface_name(profile, &interface_name) != CONNECTION_ERROR_NONE)
584 printf("Fail to get interface name!\n");
586 printf("Interface name : %s\n", interface_name);
587 g_free(interface_name);
590 if (connection_profile_get_ip_config_type(profile, CONNECTION_ADDRESS_FAMILY_IPV4, &ip_type) != CONNECTION_ERROR_NONE)
591 printf("Fail to get ipconfig type!\n");
593 printf("Ipconfig type : %d\n", ip_type);
595 if (connection_profile_get_ip_address(profile, CONNECTION_ADDRESS_FAMILY_IPV4, &ip) != CONNECTION_ERROR_NONE)
596 printf("Fail to get IP address!\n");
598 printf("IP address : %s\n", ip);
602 if (connection_profile_get_subnet_mask(profile, CONNECTION_ADDRESS_FAMILY_IPV4, &subnet) != CONNECTION_ERROR_NONE)
603 printf("Fail to get subnet mask!\n");
605 printf("Subnet mask : %s\n", subnet);
609 if (connection_profile_get_gateway_address(profile, CONNECTION_ADDRESS_FAMILY_IPV4, &gateway) != CONNECTION_ERROR_NONE)
610 printf("Fail to get gateway!\n");
612 printf("Gateway : %s\n", gateway);
616 if (connection_profile_get_dns_address(profile, 1, CONNECTION_ADDRESS_FAMILY_IPV4, &dns1) != CONNECTION_ERROR_NONE)
617 printf("Fail to get DNS1!\n");
619 printf("DNS1 : %s\n", dns1);
623 if (connection_profile_get_dns_address(profile, 2, CONNECTION_ADDRESS_FAMILY_IPV4, &dns2) != CONNECTION_ERROR_NONE)
624 printf("Fail to get DNS2!\n");
626 printf("DNS2 : %s\n", dns2);
630 if (connection_profile_get_proxy_type(profile, &proxy_type) != CONNECTION_ERROR_NONE)
631 printf("Fail to get proxy type!\n");
633 printf("Proxy type : %d\n", proxy_type);
635 if (connection_profile_get_proxy_address(profile, CONNECTION_ADDRESS_FAMILY_IPV4, &proxy) != CONNECTION_ERROR_NONE)
636 printf("Fail to get proxy!\n");
638 printf("Proxy : %s\n", proxy);
643 int test_register_client(void)
646 int err = connection_create(&connection);
648 if (CONNECTION_ERROR_NONE == err) {
649 connection_set_type_changed_cb(connection, test_type_changed_callback, NULL);
650 connection_set_ip_address_changed_cb(connection, test_ip_changed_callback, NULL);
651 connection_set_proxy_address_changed_cb(connection, test_proxy_changed_callback, NULL);
653 printf("Client registration failed %d\n", err);
657 printf("Client registration success\n");
661 int test_deregister_client(void)
665 connection_profile_h profile;
667 if (connection != NULL)
668 rv = connection_destroy(connection);
670 printf("Cannot deregister : Handle is NULL\n");
671 rv = CONNECTION_ERROR_INVALID_OPERATION;
674 if (rv != CONNECTION_ERROR_NONE){
675 printf("Client deregistration fail [%d]\n", rv);
680 for (list = state_cb_list; list; list = list->next) {
681 profile = list->data;
682 connection_profile_destroy(profile);
685 g_slist_free(state_cb_list);
686 state_cb_list = NULL;
690 printf("Client deregistration success\n");
695 int test_get_network_state(void)
698 connection_type_e net_state;
700 rv = connection_get_type(connection, &net_state);
702 if (rv != CONNECTION_ERROR_NONE) {
703 printf("Fail to get network state [%d]\n", rv);
707 printf("Retval = %d network connection state [%d]\n", rv, net_state);
712 int test_get_cellular_state(void)
715 connection_cellular_state_e cellular_state;
717 rv = connection_get_cellular_state(connection, &cellular_state);
719 if (rv != CONNECTION_ERROR_NONE) {
720 printf("Fail to get Cellular state [%d]\n", rv);
724 printf("Retval = %d Cellular state [%d]\n", rv, cellular_state);
729 int test_get_wifi_state(void)
732 connection_wifi_state_e wifi_state;
734 rv = connection_get_wifi_state(connection, &wifi_state);
736 if (rv != CONNECTION_ERROR_NONE) {
737 printf("Fail to get WiFi state [%d]\n", rv);
741 printf("Retval = %d WiFi state [%d]\n", rv, wifi_state);
746 int test_get_current_proxy(void)
748 char *proxy_addr = NULL;
750 connection_get_proxy(connection, CONNECTION_ADDRESS_FAMILY_IPV4, &proxy_addr);
752 if (proxy_addr == NULL) {
753 printf("Proxy address does not exist\n");
757 printf("Current Proxy [%s]\n", proxy_addr);
763 int test_get_current_ip(void)
765 char *ip_addr = NULL;
767 connection_get_ip_address(connection, CONNECTION_ADDRESS_FAMILY_IPV4, &ip_addr);
769 if (ip_addr == NULL) {
770 printf("IP address does not exist\n");
774 printf("IPv4 address : %s\n", ip_addr);
780 int test_get_call_statistics_info(void)
784 connection_get_statistics(CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_LAST_RECEIVED_DATA, &rv);
785 printf("last recv data size [%lld]\n", rv);
786 connection_get_statistics(CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_LAST_SENT_DATA, &rv);
787 printf("last sent data size [%lld]\n",rv );
788 connection_get_statistics(CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_TOTAL_RECEIVED_DATA, &rv);
789 printf("total received data size [%lld]\n",rv );
790 connection_get_statistics(CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_TOTAL_SENT_DATA, &rv);
791 printf("total sent data size [%lld]\n", rv);
796 int test_get_wifi_call_statistics_info(void)
800 connection_get_statistics(CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_LAST_RECEIVED_DATA, &rv);
801 printf("WiFi last recv data size [%lld]\n", rv);
802 connection_get_statistics(CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_LAST_SENT_DATA, &rv);
803 printf("WiFi last sent data size [%lld]\n",rv );
804 connection_get_statistics(CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_TOTAL_RECEIVED_DATA, &rv);
805 printf("WiFi total received data size [%lld]\n",rv );
806 connection_get_statistics(CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_TOTAL_SENT_DATA, &rv);
807 printf("WiFi total sent data size [%lld]\n", rv);
812 int test_get_profile_list(void)
814 if (test_get_user_selected_profile(NULL, false) == false)
820 int test_get_connected_profile_list(void)
823 char *profile_name = NULL;
824 connection_profile_iterator_h profile_iter;
825 connection_profile_h profile_h;
827 rv = connection_get_profile_iterator(connection, CONNECTION_ITERATOR_TYPE_CONNECTED, &profile_iter);
828 if (rv != CONNECTION_ERROR_NONE) {
829 printf("Fail to get profile iterator [%d]\n", rv);
833 while (connection_profile_iterator_has_next(profile_iter)) {
834 if (connection_profile_iterator_next(profile_iter, &profile_h) != CONNECTION_ERROR_NONE) {
835 printf("Fail to get profile handle\n");
839 if (connection_profile_get_name(profile_h, &profile_name) != CONNECTION_ERROR_NONE) {
840 printf("Fail to get profile name\n");
843 printf("profile name : %s\n", profile_name);
844 g_free(profile_name);
850 int test_get_current_profile(void)
853 char *profile_name = NULL;
854 connection_profile_h profile_h;
856 rv = connection_get_current_profile(connection, &profile_h);
857 if (rv != CONNECTION_ERROR_NONE) {
858 printf("Fail to get profile iterator [%d]\n", rv);
862 if (connection_profile_get_name(profile_h, &profile_name) != CONNECTION_ERROR_NONE) {
863 printf("Fail to get profile name\n");
866 printf("profile name : %s\n", profile_name);
867 g_free(profile_name);
869 connection_profile_destroy(profile_h);
874 int test_open_profile(void)
876 connection_profile_h profile;
878 printf("\n** Choose a profile to open. **\n");
880 if (test_get_user_selected_profile(&profile, true) == false)
883 if (connection_open_profile(connection, profile, test_connection_opened_callback, NULL) != CONNECTION_ERROR_NONE) {
884 printf("Connection open Failed!!\n");
891 int test_get_default_cellular_service_type(void)
896 connection_profile_h profile;
897 char *profile_name = NULL;
899 rv = test_get_user_int("Input profile type to get"
900 "(1:Internet, 2:MMS, 3:Prepaid internet, 4:Prepaid MMS, 5:Tethering):", &input);
903 printf("Invalid input!!\n");
909 service_type = CONNECTION_CELLULAR_SERVICE_TYPE_INTERNET;
912 service_type = CONNECTION_CELLULAR_SERVICE_TYPE_MMS;
915 service_type = CONNECTION_CELLULAR_SERVICE_TYPE_PREPAID_INTERNET;
918 service_type = CONNECTION_CELLULAR_SERVICE_TYPE_PREPAID_MMS;
921 service_type = CONNECTION_CELLULAR_SERVICE_TYPE_TETHERING;
924 printf("Wrong number!!\n");
928 if (connection_get_default_cellular_service_profile(connection, service_type, &profile) != CONNECTION_ERROR_NONE)
931 if (connection_profile_get_name(profile, &profile_name) != CONNECTION_ERROR_NONE) {
932 printf("Fail to get profile name\n");
933 connection_profile_destroy(profile);
936 printf("Default profile name : %s\n", profile_name);
937 g_free(profile_name);
939 connection_profile_destroy(profile);
944 int test_set_default_cellular_service_type(void)
946 connection_profile_h profile;
947 connection_cellular_service_type_e type;
949 printf("\n** Choose a profile to set default service(internet or prepaid internet type only). **\n");
950 if (test_get_user_selected_profile(&profile, true) == false)
953 if (connection_profile_get_cellular_service_type(profile, &type) != CONNECTION_ERROR_NONE) {
954 printf("Fail to get cellular service type\n");
958 if (connection_set_default_cellular_service_profile(connection, type, profile) != CONNECTION_ERROR_NONE)
964 int test_close_profile(void)
966 connection_profile_h profile;
968 printf("\n** Choose a profile to close. **\n");
970 if (test_get_user_selected_profile(&profile, true) == false)
973 if (connection_close_profile(connection, profile, test_connection_closed_callback, NULL) != CONNECTION_ERROR_NONE) {
974 printf("Connection close Failed!!\n");
981 int test_add_profile(void)
984 connection_profile_h profile;
985 char input_str[100] = {0,};
987 if (test_get_user_string("Input Keyword - (Enter for skip) :", input_str, 100) == false)
990 rv = connection_profile_create(CONNECTION_PROFILE_TYPE_CELLULAR, input_str, &profile);
991 if (rv != CONNECTION_ERROR_NONE)
992 RETURN_FAIL_DESTROY(profile);
994 if (test_update_cellular_info(profile) == -1)
995 RETURN_FAIL_DESTROY(profile);
997 rv = connection_add_profile(connection, profile);
998 if (rv != CONNECTION_ERROR_NONE)
999 RETURN_FAIL_DESTROY(profile);
1001 connection_profile_destroy(profile);
1005 int test_remove_profile(void)
1007 connection_profile_h profile;
1009 printf("\n** Choose a profile to remove. **\n");
1010 if (test_get_user_selected_profile(&profile, true) == false)
1013 if (connection_remove_profile(connection, profile) != CONNECTION_ERROR_NONE) {
1014 printf("Remove profile Failed!!\n");
1021 int test_update_profile(void)
1025 connection_profile_type_e prof_type;
1026 connection_profile_h profile;
1028 printf("\n** Choose a profile to update. **\n");
1029 if (test_get_user_selected_profile(&profile, true) == false)
1032 if (connection_profile_get_type(profile, &prof_type) != CONNECTION_ERROR_NONE)
1035 switch (prof_type) {
1036 case CONNECTION_PROFILE_TYPE_CELLULAR:
1037 if (test_update_cellular_info(profile) == -1)
1041 case CONNECTION_PROFILE_TYPE_WIFI:
1042 if (test_update_wifi_info(profile) == -1)
1045 if (test_update_network_info(profile) == -1)
1049 case CONNECTION_PROFILE_TYPE_ETHERNET:
1050 case CONNECTION_PROFILE_TYPE_BT:
1051 printf("Not supported!\n");
1057 rv = connection_update_profile(connection, profile);
1058 if (rv != CONNECTION_ERROR_NONE)
1064 int test_get_profile_info(void)
1066 connection_profile_type_e prof_type;
1067 connection_profile_state_e profile_state;
1068 connection_profile_h profile;
1069 char *profile_name = NULL;
1071 printf("\n** Choose a profile to print. **\n");
1072 if (test_get_user_selected_profile(&profile, true) == false)
1075 if (connection_profile_get_name(profile, &profile_name) != CONNECTION_ERROR_NONE) {
1076 printf("Fail to get profile name\n");
1079 printf("Profile Name : %s\n", profile_name);
1080 g_free(profile_name);
1083 if (connection_profile_get_state(profile, &profile_state) != CONNECTION_ERROR_NONE) {
1084 printf("Fail to get profile state\n");
1087 printf("Profile State : %s\n", test_print_state(profile_state));
1090 if (connection_profile_get_type(profile, &prof_type) != CONNECTION_ERROR_NONE)
1093 switch (prof_type) {
1094 case CONNECTION_PROFILE_TYPE_CELLULAR:
1095 printf("Profile Type : Cellular\n");
1096 test_print_cellular_info(profile);
1098 case CONNECTION_PROFILE_TYPE_WIFI:
1099 printf("Profile Type : Wi-Fi\n");
1100 test_print_wifi_info(profile);
1102 case CONNECTION_PROFILE_TYPE_ETHERNET:
1103 printf("Profile Type : Ethernet\n");
1105 case CONNECTION_PROFILE_TYPE_BT:
1106 printf("Profile Type : Bluetooth\n");
1112 test_print_network_info(profile);
1117 int test_refresh_profile_info(void)
1119 connection_profile_type_e prof_type;
1120 connection_profile_state_e profile_state;
1121 connection_profile_h profile;
1122 char *profile_name = NULL;
1124 printf("\n** Choose a profile to refresh. **\n");
1125 if (test_get_user_selected_profile(&profile, true) == false)
1128 if (connection_profile_refresh(profile) != CONNECTION_ERROR_NONE)
1131 if (connection_profile_get_name(profile, &profile_name) != CONNECTION_ERROR_NONE) {
1132 printf("Fail to get profile name\n");
1135 printf("Profile Name : %s\n", profile_name);
1136 g_free(profile_name);
1139 if (connection_profile_get_state(profile, &profile_state) != CONNECTION_ERROR_NONE) {
1140 printf("Fail to get profile state\n");
1143 printf("Profile State : %s\n", test_print_state(profile_state));
1146 if (connection_profile_get_type(profile, &prof_type) != CONNECTION_ERROR_NONE)
1149 switch (prof_type) {
1150 case CONNECTION_PROFILE_TYPE_CELLULAR:
1151 printf("Profile Type : Cellular\n");
1152 test_print_cellular_info(profile);
1154 case CONNECTION_PROFILE_TYPE_WIFI:
1155 printf("Profile Type : Wi-Fi\n");
1156 test_print_wifi_info(profile);
1158 case CONNECTION_PROFILE_TYPE_ETHERNET:
1159 printf("Profile Type : Ethernet\n");
1161 case CONNECTION_PROFILE_TYPE_BT:
1162 printf("Profile Type : Bluetooth\n");
1168 test_print_network_info(profile);
1173 int test_set_state_changed_callback()
1175 connection_profile_h profile;
1176 connection_profile_h profile_clone;
1178 printf("\n** Choose a profile to set callback. **\n");
1179 if (test_get_user_selected_profile(&profile, true) == false)
1182 if (connection_profile_clone(&profile_clone, profile) != CONNECTION_ERROR_NONE)
1185 if (connection_profile_set_state_changed_cb(profile,
1186 test_profile_state_callback, profile_clone) != CONNECTION_ERROR_NONE) {
1187 connection_profile_destroy(profile_clone);
1191 state_cb_list = g_slist_append(state_cb_list, profile_clone);
1196 int test_unset_state_changed_callback()
1198 connection_profile_h profile;
1200 char *profile_name = NULL;
1204 printf("\n** Choose a profile to unset callback. **\n");
1205 for (list = state_cb_list; list; list = list->next) {
1206 profile = list->data;
1207 if (connection_profile_get_name(profile, &profile_name) != CONNECTION_ERROR_NONE) {
1208 printf("Fail to get profile name!\n");
1211 printf("%d. %s\n", count, profile_name);
1212 g_free(profile_name);
1218 if (test_get_user_int("Input profile number(Enter for cancel) :", &input) == false ||
1221 printf("Wrong number!!\n");
1226 for (list = state_cb_list; list; list = list->next) {
1227 if (count == input) {
1228 profile = list->data;
1238 if (connection_profile_unset_state_changed_cb(profile) != CONNECTION_ERROR_NONE)
1241 state_cb_list = g_slist_remove(state_cb_list, profile);
1242 connection_profile_destroy(profile);
1247 int test_reset_call_statistics_info(void)
1249 int ret = CONNECTION_ERROR_NONE;
1251 ret = connection_reset_statistics(CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_LAST_RECEIVED_DATA);
1252 printf("reset last recv data size [%d]\n", ret);
1253 ret = connection_reset_statistics(CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_LAST_SENT_DATA);
1254 printf("last sent data size [%d]\n", ret);
1255 ret = connection_reset_statistics(CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_TOTAL_RECEIVED_DATA);
1256 printf("total received data size [%d]\n", ret);
1257 ret = connection_reset_statistics(CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_TOTAL_SENT_DATA);
1258 printf("total sent data size [%d]\n", ret);
1263 int test_reset_wifi_call_statistics_info(void)
1265 int ret = CONNECTION_ERROR_NONE;
1267 ret = connection_reset_statistics(CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_LAST_SENT_DATA);
1268 printf("WiFi last sent data size [%d]\n", ret);
1269 ret = connection_reset_statistics(CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_LAST_RECEIVED_DATA);
1270 printf("WiFi last recv data size [%d]\n", ret);
1271 ret = connection_reset_statistics(CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_TOTAL_SENT_DATA);
1272 printf("WiFi total sent data size [%d]\n", ret);
1273 ret = connection_reset_statistics(CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_TOTAL_RECEIVED_DATA);
1274 printf("WiFi total received data size [%d]\n", ret);
1279 int test_add_route(void)
1285 if (test_get_user_string("Input IP - (Enter for skip) :", ip_addr, 30) == false)
1288 if (test_get_user_string("Input Interface name - (Enter for skip) :", if_name, 40) == false)
1291 rv = connection_add_route(connection, if_name, ip_addr);
1292 if (rv != CONNECTION_ERROR_NONE) {
1293 printf("Fail to get add new route [%d]\n", rv);
1300 int test_get_bt_state(void)
1303 connection_bt_state_e bt_state;
1305 rv = connection_get_bt_state(connection, &bt_state);
1307 if (rv != CONNECTION_ERROR_NONE) {
1308 printf("Fail to get Bluetooth state [%d]\n", rv);
1312 printf("Retval = %d, Bluetooth state [%d]\n", rv, bt_state);
1317 int test_get_profile_id(void)
1319 connection_profile_h profile;
1322 printf("\n** Choose a profile to see profile id. **\n");
1323 if (test_get_user_selected_profile(&profile, true) == false)
1326 if (connection_profile_get_id(profile, &profile_id) != CONNECTION_ERROR_NONE) {
1327 printf("Fail to get profile name\n");
1330 printf("Profile id : %s\n", profile_id);
1337 int main(int argc, char **argv)
1340 GMainLoop *mainloop;
1341 mainloop = g_main_loop_new (NULL, FALSE);
1343 GIOChannel *channel = g_io_channel_unix_new(0);
1344 g_io_add_watch(channel, (G_IO_IN|G_IO_ERR|G_IO_HUP|G_IO_NVAL), test_thread,NULL );
1346 printf("Test Thread created...\n");
1348 g_main_loop_run (mainloop);
1353 gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data)
1358 memset(a, '\0', 100);
1359 printf("Event received from stdin\n");
1361 rv = read(0, a, 100);
1363 if (rv < 0 || a[0] == '0') {
1364 if (connection != NULL)
1365 test_deregister_client();
1370 if (*a == '\n' || *a == '\r'){
1371 printf("\n\n Network Connection API Test App\n\n");
1372 printf("Options..\n");
1373 printf("1 - Create Handle and set callbacks\n");
1374 printf("2 - Destroy Handle(unset callbacks automatically)\n");
1375 printf("3 - Get network state\n");
1376 printf("4 - Get cellular state (please insert SIM Card)\n");
1377 printf("5 - Get wifi state (please turn on WiFi)\n");
1378 printf("6 - Get current proxy address \n");
1379 printf("7 - Get current Ip address\n");
1380 printf("8 - Get cellular data call statistics\n");
1381 printf("9 - Get WiFi data call statistics\n");
1382 printf("a - Get Profile list\n");
1383 printf("b - Get Connected Profile list\n");
1384 printf("c - Get Current profile\n");
1385 printf("d - Open connection with profile\n");
1386 printf("e - Get default cellular service by type\n");
1387 printf("f - Set default cellular service by type\n");
1388 printf("g - Close connection with profile\n");
1389 printf("h - Add profile(Cellular only)\n");
1390 printf("i - Remove profile(Cellular:delete, WiFi:forgot)\n");
1391 printf("j - Update profile\n");
1392 printf("k - Get profile info\n");
1393 printf("l - Refresh profile info\n");
1394 printf("m - Set state changed callback\n");
1395 printf("n - Unset state changed callback\n");
1396 printf("o - Reset cellular data call statistics\n");
1397 printf("p - Reset WiFi data call statistics\n");
1398 printf("q - Add new route\n");
1399 printf("r - Get Bluetooth state\n");
1400 printf("s - Get profile id\n");
1401 printf("0 - Exit \n");
1402 printf("ENTER - Show options menu.......\n");
1407 rv = test_register_client();
1410 rv = test_deregister_client();
1413 rv = test_get_network_state();
1416 rv = test_get_cellular_state();
1419 rv = test_get_wifi_state();
1422 rv = test_get_current_proxy();
1425 rv = test_get_current_ip();
1428 rv = test_get_call_statistics_info();
1431 rv = test_get_wifi_call_statistics_info();
1434 rv = test_get_profile_list();
1437 rv = test_get_connected_profile_list();
1440 rv = test_get_current_profile();
1443 rv = test_open_profile();
1446 rv = test_get_default_cellular_service_type();
1449 rv = test_set_default_cellular_service_type();
1452 rv = test_close_profile();
1455 rv = test_add_profile();
1458 rv = test_remove_profile();
1461 rv = test_update_profile();
1464 rv = test_get_profile_info();
1467 rv = test_refresh_profile_info();
1470 rv = test_set_state_changed_callback();
1473 rv = test_unset_state_changed_callback();
1476 rv = test_reset_call_statistics_info();
1479 rv = test_reset_wifi_call_statistics_info();
1482 rv = test_add_route();
1485 rv = test_get_bt_state();
1488 rv = test_get_profile_id();
1493 printf("Operation succeeded!\n");
1495 printf("Operation failed!\n");