dbe83c905751576fca1998fb22121fbbe7ddc50e
[platform/core/api/connection.git] / test / connection_test.c
1 /*
2  * Copyright (c) 2011-2013 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <stdio.h>
18 #include <errno.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22 #include <glib.h>
23
24 #include "net_connection.h"
25 #include <tizen_error.h>
26
27 #define RETURN_FAIL_DESTROY(x) {connection_profile_destroy(x); return -1;}
28
29 gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data);
30
31 connection_h connection = NULL;
32 static GSList *state_cb_list = NULL;
33
34
35 static bool test_get_user_string(const char *msg, char *buf, int buf_size)
36 {
37         if (msg == NULL || buf == NULL || buf_size < 2)
38                 return false;
39
40         int rv;
41         printf("%s\n", msg);
42         memset(buf, 0, buf_size);
43         rv = read(0, buf, buf_size - 1);
44
45         if (rv < 0 || buf[0] == '\0' || buf[0] == '\n' || buf[0] == '\r') {
46                 buf[0] = '\0';
47                 return false;
48         }
49
50         buf[rv-1]='\0';
51
52         return true;
53 }
54
55 static bool test_get_user_int(const char *msg, int *num)
56 {
57         if (msg == NULL || num == NULL)
58                 return false;
59
60         int rv;
61         char buf[32] = {0,};
62         printf("%s\n", msg);
63         rv = read(0, buf, 32);
64
65         if (rv < 0 || *buf == 0 || *buf == '\n' || *buf == '\r')
66                 return false;
67
68         *num = atoi(buf);
69         return true;
70 }
71
72 static const char *test_print_state(connection_profile_state_e state)
73 {
74         switch (state) {
75         case CONNECTION_PROFILE_STATE_DISCONNECTED:
76                 return "Disconnected";
77         case CONNECTION_PROFILE_STATE_ASSOCIATION:
78                 return "Association";
79         case CONNECTION_PROFILE_STATE_CONFIGURATION:
80                 return "Configuration";
81         case CONNECTION_PROFILE_STATE_CONNECTED:
82                 return "Connected";
83         default:
84                 return "Unknown";
85         }
86 }
87
88 static const char *test_print_error(connection_error_e error)
89 {
90         switch (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";
123         default:
124                 return "CONNECTION_ERROR_UNKNOWN";
125         }
126 }
127
128 static void test_type_changed_callback(connection_type_e type, void* user_data)
129 {
130         printf("Type changed callback, connection type : %d\n", type);
131 }
132
133 static void test_ip_changed_callback(const char* ipv4_address, const char* ipv6_address, void* user_data)
134 {
135         printf("IP changed callback, IPv4 address : %s, IPv6 address : %s\n",
136                         ipv4_address, (ipv6_address ? ipv6_address : "NULL"));
137 }
138
139 static void test_proxy_changed_callback(const char* ipv4_address, const char* ipv6_address, void* user_data)
140 {
141         printf("Proxy changed callback, IPv4 address : %s, IPv6 address : %s\n",
142                         ipv4_address, (ipv6_address ? ipv6_address : "NULL"));
143 }
144
145 static void test_profile_state_callback(connection_profile_state_e state, void* user_data)
146 {
147         char *profile_name;
148         connection_profile_h profile = user_data;
149
150         if (profile == NULL)
151                 return;
152
153         if (connection_profile_get_name(profile, &profile_name) != CONNECTION_ERROR_NONE)
154                 return;
155
156         printf("[%s] : %s\n", test_print_state(state), profile_name);
157         g_free(profile_name);
158 }
159
160 static void test_connection_opened_callback(connection_error_e result, void* user_data)
161 {
162         if (result ==  CONNECTION_ERROR_NONE)
163                 printf("Connection open Succeeded\n");
164         else
165                 printf("Connection open Failed, err : %d\n", result);
166 }
167
168 static void test_connection_closed_callback(connection_error_e result, void* user_data)
169 {
170         if (result ==  CONNECTION_ERROR_NONE)
171                 printf("Connection close Succeeded\n");
172         else
173                 printf("Connection close Failed, err : %d\n", result);
174 }
175
176 static void test_connection_reset_profile_callback(connection_error_e result, void* user_data)
177 {
178         if (result ==  CONNECTION_ERROR_NONE)
179                 printf("Reset profile Succeeded\n");
180         else
181                 printf("Reset profile Failed, err : [%s]\n", test_print_error(result));
182 }
183
184 static void test_connection_set_default_callback(connection_error_e result, void* user_data)
185 {
186         if (result ==  CONNECTION_ERROR_NONE)
187                 printf("Default profile setting Succeeded\n");
188         else
189                 printf("Default profile setting Failed, err : %d\n", result);
190 }
191
192 void test_get_ethernet_cable_state_callback(connection_ethernet_cable_state_e state,
193                                                                 void* user_data)
194 {
195         if(state == CONNECTION_ETHERNET_CABLE_ATTACHED)
196                 printf("Ethernet Cable Connected\n");
197         else if(state == CONNECTION_ETHERNET_CABLE_DETACHED)
198                 printf("Ethernet Cable Disconnected\n");
199 }
200
201 static bool test_get_user_selected_profile(connection_profile_h *profile, bool select)
202 {
203         int rv = 0;
204         int input = 0;
205         char *profile_name;
206         connection_profile_type_e profile_type;
207         connection_profile_state_e profile_state;
208         connection_profile_iterator_h profile_iter;
209         connection_profile_h profile_h;
210
211         connection_profile_h profile_list[100] = {0,};
212         int profile_count = 0;
213
214         rv = connection_get_profile_iterator(connection, CONNECTION_ITERATOR_TYPE_REGISTERED, &profile_iter);
215         if (rv != CONNECTION_ERROR_NONE) {
216                 printf("Fail to get profile iterator [%d]\n", rv);
217                 return false;
218         }
219
220         while (connection_profile_iterator_has_next(profile_iter)) {
221                 if (connection_profile_iterator_next(profile_iter, &profile_h) != CONNECTION_ERROR_NONE) {
222                         printf("Fail to get profile handle\n");
223                         return false;
224                 }
225
226                 if (connection_profile_get_name(profile_h, &profile_name) != CONNECTION_ERROR_NONE) {
227                         printf("Fail to get profile name\n");
228                         return false;
229                 }
230
231                 if (connection_profile_get_type(profile_h, &profile_type) != CONNECTION_ERROR_NONE) {
232                         printf("Fail to get profile type\n");
233                         g_free(profile_name);
234                         return false;
235                 }
236
237                 if (connection_profile_get_state(profile_h, &profile_state) != CONNECTION_ERROR_NONE) {
238                         printf("Fail to get profile state\n");
239                         g_free(profile_name);
240                         return false;
241                 }
242
243                 if (profile_type == CONNECTION_PROFILE_TYPE_WIFI) {
244                         char *essid;
245                         connection_profile_get_wifi_essid(profile_h, &essid);
246                         printf("%d. state:[%s], profile name:%s, essid:%s\n",
247                                 profile_count, test_print_state(profile_state),
248                                 profile_name, (essid)? essid : "");
249                         g_free(essid);
250
251                         profile_list[profile_count] = profile_h;
252                         profile_count++;
253                 } else {
254                         printf("%d. state:[%s], profile name : %s\n",
255                                 profile_count, test_print_state(profile_state), profile_name);
256
257                         profile_list[profile_count] = profile_h;
258                         profile_count++;
259                 }
260
261                 g_free(profile_name);
262                 if (profile_count >= 100)
263                         break;
264         }
265
266         if (select == false)
267                 return true;
268
269         if (test_get_user_int("Input profile number(Enter for cancel) :", &input) == false ||
270             input >= profile_count ||
271             input < 0) {
272                 printf("Wrong number!!\n");
273                 return false;
274         }
275
276         if (profile)
277                 *profile = profile_list[input];
278
279         return true;
280 }
281
282 static int test_update_cellular_info(connection_profile_h profile)
283 {
284         int rv = 0;
285         char input_str1[100] = {0,};
286         char input_str2[100] = {0,};
287         int input_int = 0;
288         int type_val = 0;
289
290         if (test_get_user_int("Input Network Type (internet:1, MMS:2, Prepaid internet:3, "
291                         "Prepaid MMS:4, Tethering:5, Application:6)"
292                         " - (Enter for skip) :", &input_int)) {
293                 switch (input_int) {
294                 case 1:
295                         rv = connection_profile_set_cellular_service_type(profile,
296                                         CONNECTION_CELLULAR_SERVICE_TYPE_INTERNET);
297                         break;
298                 case 2:
299                         rv = connection_profile_set_cellular_service_type(profile,
300                                         CONNECTION_CELLULAR_SERVICE_TYPE_MMS);
301                         break;
302                 case 3:
303                         rv = connection_profile_set_cellular_service_type(profile,
304                                         CONNECTION_CELLULAR_SERVICE_TYPE_PREPAID_INTERNET);
305                         break;
306                 case 4:
307                         rv = connection_profile_set_cellular_service_type(profile,
308                                         CONNECTION_CELLULAR_SERVICE_TYPE_PREPAID_MMS);
309                         break;
310                 case 5:
311                         rv = connection_profile_set_cellular_service_type(profile,
312                                         CONNECTION_CELLULAR_SERVICE_TYPE_TETHERING);
313                         break;
314                 case 6:
315                         rv = connection_profile_set_cellular_service_type(profile,
316                                         CONNECTION_CELLULAR_SERVICE_TYPE_APPLICATION);
317                         break;
318                 default:
319                         return -1;
320                 }
321
322                 if (rv != CONNECTION_ERROR_NONE)
323                         return -1;
324         } else
325                 return -1;
326
327         if (test_get_user_string("Input Apn - (Enter for skip) :", input_str1, 100)) {
328                 rv = connection_profile_set_cellular_apn(profile, input_str1);
329                 if (rv != CONNECTION_ERROR_NONE)
330                         return -1;
331         }
332
333         if (test_get_user_string("Input Proxy - (Enter for skip) :", input_str1, 100)) {
334                 rv = connection_profile_set_proxy_address(profile, CONNECTION_ADDRESS_FAMILY_IPV4, input_str1);
335                 if (rv != CONNECTION_ERROR_NONE)
336                         return -1;
337         }
338
339         if (test_get_user_string("Input HomeURL - (Enter for skip) :", input_str1, 100)) {
340                 rv = connection_profile_set_cellular_home_url(profile, input_str1);
341                 if (rv != CONNECTION_ERROR_NONE)
342                         return -1;
343         }
344
345         if (test_get_user_int("Input AuthType(0:NONE 1:PAP 2:CHAP) - (Enter for skip) :", &input_int)) {
346                 switch (input_int) {
347                 case 0:
348                         rv = connection_profile_set_cellular_auth_info(profile,
349                                         CONNECTION_CELLULAR_AUTH_TYPE_NONE, "", "");
350                         if (rv != CONNECTION_ERROR_NONE)
351                                 return -1;
352
353                         break;
354                 case 1:
355                         type_val = CONNECTION_CELLULAR_AUTH_TYPE_PAP;
356                         /* fall through */
357                 case 2:
358                         if (input_int == 2) type_val = CONNECTION_CELLULAR_AUTH_TYPE_CHAP;
359
360                         if (test_get_user_string("Input AuthId(Enter for skip) :", input_str1, 100) == false)
361                                 input_str1[0] = 0;
362                         if (test_get_user_string("Input AuthPwd(Enter for skip) :", input_str2, 100) == false)
363                                 input_str2[0] = 0;
364
365                         rv = connection_profile_set_cellular_auth_info(profile, type_val, input_str1, input_str2);
366                         if (rv != CONNECTION_ERROR_NONE)
367                                 return -1;
368                 }
369         }
370
371         return 1;
372 }
373
374 static int test_update_wifi_info(connection_profile_h profile)
375 {
376         int rv = 0;
377         char input_str[100] = {0,};
378
379         if (test_get_user_string("Input Passphrase - (Enter for skip) :", input_str, 100)) {
380                 rv = connection_profile_set_wifi_passphrase(profile, input_str);
381                 if (rv != CONNECTION_ERROR_NONE)
382                         return -1;
383         }
384
385         return 1;
386 }
387
388 static int test_update_ip_info(connection_profile_h profile, connection_address_family_e address_family)
389 {
390         int rv = 0;
391         char input_str[100] = {0,};
392
393         if (test_get_user_string("Input IP Address - (Enter for skip) :", input_str, 100)) {
394                 rv = connection_profile_set_ip_address(profile,
395                                                         address_family,
396                                                         input_str);
397                 if (rv != CONNECTION_ERROR_NONE)
398                         return -1;
399         }
400
401         if (test_get_user_string("Input Netmask - (Enter for skip) :", input_str, 100)) {
402                 rv = connection_profile_set_subnet_mask(profile,
403                                                         address_family,
404                                                         input_str);
405                 if (rv != CONNECTION_ERROR_NONE)
406                         return -1;
407         }
408
409         if (test_get_user_string("Input Gateway - (Enter for skip) :", input_str, 100)) {
410                 rv = connection_profile_set_gateway_address(profile,
411                                                         address_family,
412                                                         input_str);
413                 if (rv != CONNECTION_ERROR_NONE)
414                         return -1;
415         }
416
417         if (test_get_user_string("Input DNS 1 Address - (Enter for skip) :", input_str, 100)) {
418                 rv = connection_profile_set_dns_address(profile,
419                                                         1,
420                                                         address_family,
421                                                         input_str);
422                 if (rv != CONNECTION_ERROR_NONE)
423                         return -1;
424
425                 if (test_get_user_string("Input DNS 2 Address - (Enter for skip) :", input_str, 100)) {
426                         rv = connection_profile_set_dns_address(profile,
427                                                                 2,
428                                                                 address_family,
429                                                                 input_str);
430                         if (rv != CONNECTION_ERROR_NONE)
431                                 return -1;
432                 }
433         }
434
435         return 1;
436 }
437
438 static int test_update_proxy_info(connection_profile_h profile, connection_address_family_e address_family)
439 {
440         int rv = 0;
441         int input_int = 0;
442         char input_str[100] = {0,};
443
444         if (test_get_user_int("Input Proxy Type (1:direct, 2:auto, 3:manual)"
445                                         " - (Enter for skip) :", &input_int)) {
446                 switch (input_int) {
447                 case 1:
448                         rv = connection_profile_set_proxy_type(profile,
449                                         CONNECTION_PROXY_TYPE_DIRECT);
450
451                         if (rv != CONNECTION_ERROR_NONE)
452                                 return -1;
453                         else
454                                 return 1;
455                 case 2:
456                         rv = connection_profile_set_proxy_type(profile,
457                                         CONNECTION_PROXY_TYPE_AUTO);
458                         break;
459                 case 3:
460                         rv = connection_profile_set_proxy_type(profile,
461                                         CONNECTION_PROXY_TYPE_MANUAL);
462                         break;
463                 default:
464                         return -1;
465                 }
466
467                 if (rv != CONNECTION_ERROR_NONE)
468                         return -1;
469
470                 if (test_get_user_string("Input auto Proxy URL or Proxy address"
471                                         " - (Enter for skip) :", input_str, 100)) {
472                         rv = connection_profile_set_proxy_address(profile,
473                                                                 address_family,
474                                                                 input_str);
475                         if (rv != CONNECTION_ERROR_NONE)
476                                 return -1;
477                 }
478
479         } else
480                 return -1;
481
482         return 1;
483 }
484
485 static int test_update_network_info(connection_profile_h profile)
486 {
487         int rv = 0;
488         int input_int = 0;
489         int address_family = 0;
490
491         test_get_user_int("Input Address Family (0:IPv4 1:IPv6) :", &address_family);
492
493         if (test_get_user_int("Input IPv4 Address Type (DHCP:1, Static:2)"
494                                 " - (Enter for skip) :", &input_int)) {
495                 switch (input_int) {
496                 case 1:
497                         rv = connection_profile_set_ip_config_type(profile,
498                                                                    address_family,
499                                                                    CONNECTION_IP_CONFIG_TYPE_DYNAMIC);
500                         break;
501                 case 2:
502                         rv = connection_profile_set_ip_config_type(profile,
503                                                                    address_family,
504                                                                    CONNECTION_IP_CONFIG_TYPE_STATIC);
505                         if (rv != CONNECTION_ERROR_NONE)
506                                 return -1;
507
508                         if (test_update_ip_info(profile, address_family) == -1)
509                                 return -1;
510
511                         if (test_update_proxy_info(profile, address_family) == -1)
512                                 return -1;
513                         break;
514                 default:
515                         return -1;
516                 }
517
518                 if (rv != CONNECTION_ERROR_NONE)
519                         return -1;
520         } else
521                 return -1;
522
523         return 1;
524 }
525
526 static void test_print_cellular_info(connection_profile_h profile)
527 {
528         connection_cellular_network_type_e network_type;
529         connection_cellular_service_type_e service_type;
530         char *apn = NULL;
531         connection_cellular_auth_type_e auth_type;
532         char *user_name = NULL;
533         char *password = NULL;
534         char *home_url = NULL;
535         bool roaming = false;
536         bool hidden = false;
537         bool editable = false;
538
539         if (connection_profile_get_cellular_network_type(profile, &network_type) != CONNECTION_ERROR_NONE)
540                 printf("Fail to get cellular network type!\n");
541         else
542                 printf("Cellular network type : %d\n", network_type);
543
544         if (connection_profile_get_cellular_service_type(profile, &service_type) != CONNECTION_ERROR_NONE)
545                 printf("Fail to get cellular service type!\n");
546         else
547                 printf("Cellular service type : %d\n", service_type);
548
549         if (connection_profile_get_cellular_apn(profile, &apn) != CONNECTION_ERROR_NONE)
550                 printf("Fail to get cellular APN!\n");
551         else {
552                 printf("Cellular APN : %s\n", apn);
553                 g_free(apn);
554         }
555
556         if (connection_profile_get_cellular_auth_info(profile, &auth_type, &user_name, &password) != CONNECTION_ERROR_NONE)
557                 printf("Fail to get auth info!\n");
558         else {
559                 printf("Cellular auth type : %d\n", auth_type);
560                 printf("Cellular user_name : %s\n", user_name);
561                 printf("Cellular password : %s\n", password);
562                 g_free(user_name);
563                 g_free(password);
564         }
565
566         if (connection_profile_get_cellular_home_url(profile, &home_url) != CONNECTION_ERROR_NONE)
567                 printf("Fail to get cellular home url!\n");
568         else {
569                 printf("Cellular home url : %s\n", home_url);
570                 g_free(home_url);
571         }
572
573         if (connection_profile_is_cellular_roaming(profile, &roaming) != CONNECTION_ERROR_NONE)
574                 printf("Fail to get cellular is roaming!\n");
575         else
576                 printf("Cellular roaming : %s\n", roaming ? "true" : "false");
577
578         if (connection_profile_is_cellular_hidden(profile, &hidden) != CONNECTION_ERROR_NONE)
579                 printf("Fail to get cellular hidden state!\n");
580         else
581                 printf("Cellular hidden : %s\n", hidden ? "true" : "false");
582
583         if (connection_profile_is_cellular_editable(profile, &editable) != CONNECTION_ERROR_NONE)
584                 printf("Fail to get cellular editing state!\n");
585         else
586                 printf("Cellular editable : %s\n", editable ? "true" : "false");
587 }
588
589 static void test_print_wifi_info(connection_profile_h profile)
590 {
591         char *essid = NULL;
592         char *bssid = NULL;
593         int rssi = 0;
594         int frequency = 0;
595         int max_speed = 0;
596         connection_wifi_security_type_e security_type;
597         connection_wifi_encryption_type_e encryption_type;
598         bool pass_required = false;
599         bool wps_supported = false;
600
601         if (connection_profile_get_wifi_essid(profile, &essid) != CONNECTION_ERROR_NONE)
602                 printf("Fail to get Wi-Fi essid!\n");
603         else {
604                 printf("Wi-Fi essid : %s\n", essid);
605                 g_free(essid);
606         }
607
608         if (connection_profile_get_wifi_bssid(profile, &bssid) != CONNECTION_ERROR_NONE)
609                 printf("Fail to get Wi-Fi bssid!\n");
610         else {
611                 printf("Wi-Fi bssid : %s\n", bssid);
612                 g_free(bssid);
613         }
614
615         if (connection_profile_get_wifi_rssi(profile, &rssi) != CONNECTION_ERROR_NONE)
616                 printf("Fail to get Wi-Fi rssi!\n");
617         else
618                 printf("Wi-Fi rssi : %d\n", rssi);
619
620         if (connection_profile_get_wifi_frequency(profile, &frequency) != CONNECTION_ERROR_NONE)
621                 printf("Fail to get Wi-Fi frequency!\n");
622         else
623                 printf("Wi-Fi frequency : %d\n", frequency);
624
625         if (connection_profile_get_wifi_max_speed(profile, &max_speed) != CONNECTION_ERROR_NONE)
626                 printf("Fail to get Wi-Fi max speed!\n");
627         else
628                 printf("Wi-Fi max speed : %d\n", max_speed);
629
630         if (connection_profile_get_wifi_security_type(profile, &security_type) != CONNECTION_ERROR_NONE)
631                 printf("Fail to get Wi-Fi security type!\n");
632         else
633                 printf("Wi-Fi security type : %d\n", security_type);
634
635         if (connection_profile_get_wifi_encryption_type(profile, &encryption_type) != CONNECTION_ERROR_NONE)
636                 printf("Fail to get Wi-Fi encryption type!\n");
637         else
638                 printf("Wi-Fi encryption type : %d\n", encryption_type);
639
640         if (connection_profile_is_wifi_passphrase_required(profile, &pass_required) != CONNECTION_ERROR_NONE)
641                 printf("Fail to get Wi-Fi passphrase required!\n");
642         else
643                 printf("Wi-Fi passphrase required : %s\n", pass_required ? "true" : "false");
644
645         if (connection_profile_is_wifi_wps_supported(profile, &wps_supported) != CONNECTION_ERROR_NONE)
646                 printf("Fail to get Wi-Fi wps info\n");
647         else
648                 printf("Wi-Fi wps supported : %s\n", wps_supported ? "true" : "false");
649 }
650
651 static void test_print_network_info(connection_profile_h profile, connection_address_family_e address_family)
652 {
653         char *interface_name = NULL;
654         connection_ip_config_type_e ip_type;
655         char *ip = NULL;
656         char *subnet = NULL;
657         char *gateway = NULL;
658         char *dns1 = NULL;
659         char *dns2 = NULL;
660         connection_proxy_type_e proxy_type;
661         char *proxy = NULL;
662
663         if (connection_profile_get_network_interface_name(profile, &interface_name) != CONNECTION_ERROR_NONE)
664                 printf("Fail to get interface name!\n");
665         else {
666                 printf("Interface name : %s\n", interface_name);
667                 g_free(interface_name);
668         }
669
670         if (connection_profile_get_ip_config_type(profile, address_family, &ip_type) != CONNECTION_ERROR_NONE)
671                 printf("Fail to get ipconfig type!\n");
672         else
673                 printf("Ipconfig type : %d\n", ip_type);
674
675         if (connection_profile_get_ip_address(profile, address_family, &ip) != CONNECTION_ERROR_NONE)
676                 printf("Fail to get IP address!\n");
677         else {
678                 printf("IP address : %s\n", ip);
679                 g_free(ip);
680         }
681
682         if (connection_profile_get_subnet_mask(profile, address_family, &subnet) != CONNECTION_ERROR_NONE)
683                 printf("Fail to get subnet mask!\n");
684         else {
685                 printf("Subnet mask : %s\n", subnet);
686                 g_free(subnet);
687         }
688
689         if (connection_profile_get_gateway_address(profile, address_family, &gateway) != CONNECTION_ERROR_NONE)
690                 printf("Fail to get gateway!\n");
691         else {
692                 printf("Gateway : %s\n", gateway);
693                 g_free(gateway);
694         }
695
696         if (connection_profile_get_dns_address(profile, 1, address_family, &dns1) != CONNECTION_ERROR_NONE)
697                 printf("Fail to get DNS1!\n");
698         else {
699                 printf("DNS1 : %s\n", dns1);
700                 g_free(dns1);
701         }
702
703         if (connection_profile_get_dns_address(profile, 2, address_family, &dns2) != CONNECTION_ERROR_NONE)
704                 printf("Fail to get DNS2!\n");
705         else {
706                 printf("DNS2 : %s\n", dns2);
707                 g_free(dns2);
708         }
709
710         if (connection_profile_get_proxy_type(profile, &proxy_type) != CONNECTION_ERROR_NONE)
711                 printf("Fail to get proxy type!\n");
712         else
713                 printf("Proxy type : %d\n", proxy_type);
714
715         if (connection_profile_get_proxy_address(profile, address_family, &proxy) != CONNECTION_ERROR_NONE)
716                 printf("Fail to get proxy!\n");
717         else {
718                 printf("Proxy : %s\n", proxy);
719                 g_free(proxy);
720         }
721 }
722
723 int test_register_client(void)
724 {
725
726         int err = connection_create(&connection);
727
728         if (CONNECTION_ERROR_NONE == err) {
729                 connection_set_type_changed_cb(connection, test_type_changed_callback, NULL);
730                 connection_set_ip_address_changed_cb(connection, test_ip_changed_callback, NULL);
731                 connection_set_proxy_address_changed_cb(connection, test_proxy_changed_callback, NULL);
732                 connection_set_ethernet_cable_state_chaged_cb(connection,
733                                         test_get_ethernet_cable_state_callback, NULL);
734         } else {
735                 printf("Client registration failed %d\n", err);
736                 return -1;
737         }
738
739         printf("Client registration success\n");
740         return 1;
741 }
742
743 int  test_deregister_client(void)
744 {
745         int rv = 0;
746         GSList *list;
747         connection_profile_h profile;
748
749         if (connection != NULL)
750                 rv = connection_destroy(connection);
751         else {
752                 printf("Cannot deregister : Handle is NULL\n");
753                 rv = CONNECTION_ERROR_INVALID_OPERATION;
754         }
755
756         if (rv != CONNECTION_ERROR_NONE){
757                 printf("Client deregistration fail [%d]\n", rv);
758                 return -1;
759         }
760
761         if (state_cb_list) {
762                 for (list = state_cb_list; list; list = list->next) {
763                         profile = list->data;
764                         connection_profile_destroy(profile);
765                 }
766
767                 g_slist_free(state_cb_list);
768                 state_cb_list = NULL;
769         }
770
771         connection = NULL;
772         printf("Client deregistration success\n");
773
774         return 1;
775 }
776
777 int test_get_network_state(void)
778 {
779         int rv = 0;
780         connection_type_e net_state;
781
782         rv = connection_get_type(connection, &net_state);
783
784         if (rv != CONNECTION_ERROR_NONE) {
785                 printf("Fail to get network state [%d]\n", rv);
786                 return -1;
787         }
788
789         printf("Retval = %d network connection state [%d]\n", rv, net_state);
790
791         return 1;
792 }
793
794 int test_get_cellular_state(void)
795 {
796         int rv = 0;
797         connection_cellular_state_e cellular_state;
798
799         rv = connection_get_cellular_state(connection, &cellular_state);
800
801         if (rv != CONNECTION_ERROR_NONE) {
802                 printf("Fail to get Cellular state [%d]\n", rv);
803                 return -1;
804         }
805
806         printf("Retval = %d Cellular state [%d]\n", rv, cellular_state);
807
808         return 1;
809 }
810
811 int test_get_wifi_state(void)
812 {
813         int rv = 0;
814         connection_wifi_state_e wifi_state;
815
816         rv = connection_get_wifi_state(connection, &wifi_state);
817
818         if (rv != CONNECTION_ERROR_NONE) {
819                 printf("Fail to get WiFi state [%d]\n", rv);
820                 return -1;
821         }
822
823         printf("Retval = %d WiFi state [%d]\n", rv, wifi_state);
824
825         return 1;
826 }
827
828 int test_get_current_proxy(void)
829 {
830         char *proxy_addr = NULL;
831
832         connection_get_proxy(connection, CONNECTION_ADDRESS_FAMILY_IPV4, &proxy_addr);
833
834         if (proxy_addr == NULL) {
835                 printf("Proxy address does not exist\n");
836                 return -1;
837         }
838
839         printf("Current Proxy [%s]\n", proxy_addr);
840         g_free(proxy_addr);
841
842         return 1;
843 }
844
845 int test_get_current_ip(void)
846 {
847         char *ip_addr = NULL;
848        int input;
849        bool rv;
850
851        rv = test_get_user_int("Input Address type to get"
852                        "(1:IPV4, 2:IPV6):", &input);
853
854        if (rv == false) {
855                printf("Invalid input!!\n");
856                return -1;
857        }
858
859         switch (input) {
860         case 1:
861                 connection_get_ip_address(connection, CONNECTION_ADDRESS_FAMILY_IPV4, &ip_addr);
862                 if (ip_addr == NULL) {
863                         printf("IPv4 address does not exist\n");
864                         return -1;
865                 }
866                 printf("IPv4 address : %s\n", ip_addr);
867                 break;
868
869         case 2:
870                 connection_get_ip_address(connection, CONNECTION_ADDRESS_FAMILY_IPV6, &ip_addr);
871                 if (ip_addr == NULL) {
872                         printf("IPv6 address does not exist\n");
873                         return -1;
874                 }
875                 printf("IPv6 address : %s\n", ip_addr);
876                 break;
877         default:
878                 printf("Wrong IP address family!!\n");
879                 return -1;
880         }
881
882         g_free(ip_addr);
883         return 1;
884 }
885
886 int test_get_call_statistics_info(void)
887 {
888         long long rv = 0;
889
890         connection_get_statistics(connection, CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_LAST_RECEIVED_DATA, &rv);
891         printf("last recv data size [%lld]\n", rv);
892         connection_get_statistics(connection, CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_LAST_SENT_DATA, &rv);
893         printf("last sent data size [%lld]\n",rv );
894         connection_get_statistics(connection, CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_TOTAL_RECEIVED_DATA, &rv);
895         printf("total received data size [%lld]\n",rv );
896         connection_get_statistics(connection, CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_TOTAL_SENT_DATA, &rv);
897         printf("total sent data size [%lld]\n", rv);
898
899         return 1;
900 }
901
902 int test_get_wifi_call_statistics_info(void)
903 {
904         long long rv = 0;
905
906         connection_get_statistics(connection, CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_LAST_RECEIVED_DATA, &rv);
907         printf("WiFi last recv data size [%lld]\n", rv);
908         connection_get_statistics(connection, CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_LAST_SENT_DATA, &rv);
909         printf("WiFi last sent data size [%lld]\n",rv );
910         connection_get_statistics(connection, CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_TOTAL_RECEIVED_DATA, &rv);
911         printf("WiFi total received data size [%lld]\n",rv );
912         connection_get_statistics(connection, CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_TOTAL_SENT_DATA, &rv);
913         printf("WiFi total sent data size [%lld]\n", rv);
914
915         return 1;
916 }
917
918 int test_get_profile_list(void)
919 {
920         if (test_get_user_selected_profile(NULL, false) == false)
921                 return -1;
922
923         return 1;
924 }
925
926 int test_get_default_profile_list(void)
927 {
928         int rv = 0;
929         char *profile_name = NULL;
930         connection_profile_iterator_h profile_iter;
931         connection_profile_h profile_h;
932         connection_cellular_service_type_e service_type;
933         bool is_default = false;
934
935         rv = connection_get_profile_iterator(connection, CONNECTION_ITERATOR_TYPE_DEFAULT, &profile_iter);
936         if (rv != CONNECTION_ERROR_NONE) {
937                 printf("Fail to get profile iterator [%s]\n", test_print_error(rv));
938                 return -1;
939         }
940
941         while (connection_profile_iterator_has_next(profile_iter)) {
942                 if (connection_profile_iterator_next(profile_iter, &profile_h) != CONNECTION_ERROR_NONE) {
943                         printf("Fail to get profile handle\n");
944                         return -1;
945                 }
946
947                 if (connection_profile_get_name(profile_h, &profile_name) != CONNECTION_ERROR_NONE) {
948                         printf("Fail to get profile name\n");
949                         return -1;
950                 }
951                 printf("profile name : %s\n", profile_name);
952                 g_free(profile_name);
953
954                 if (connection_profile_get_cellular_service_type(profile_h, &service_type) != CONNECTION_ERROR_NONE) {
955                         printf("Fail to get profile service type\n");
956                         return -1;
957                 }
958                 printf("service type : %d\n", service_type);
959
960                 if (connection_profile_is_cellular_default(profile_h, &is_default) != CONNECTION_ERROR_NONE) {
961                         printf("Fail to get profile subscriber id\n");
962                         return -1;
963                 }
964                 printf("Default : %d\n", is_default);
965         }
966
967         return 1;
968 }
969
970 int test_get_connected_profile_list(void)
971 {
972         int rv = 0;
973         char *profile_name = NULL;
974         connection_profile_iterator_h profile_iter;
975         connection_profile_h profile_h;
976         bool is_default = false;
977         connection_profile_type_e type;
978
979         rv = connection_get_profile_iterator(connection, CONNECTION_ITERATOR_TYPE_CONNECTED, &profile_iter);
980         if (rv != CONNECTION_ERROR_NONE) {
981                 printf("Fail to get profile iterator [%d]\n", rv);
982                 return -1;
983         }
984
985         while (connection_profile_iterator_has_next(profile_iter)) {
986                 if (connection_profile_iterator_next(profile_iter, &profile_h) != CONNECTION_ERROR_NONE) {
987                         printf("Fail to get profile handle\n");
988                         return -1;
989                 }
990
991                 if (connection_profile_get_name(profile_h, &profile_name) != CONNECTION_ERROR_NONE) {
992                         printf("Fail to get profile name\n");
993                         return -1;
994                 }
995                 printf("profile name : %s\n", profile_name);
996                 g_free(profile_name);
997
998                 if (connection_profile_get_type(profile_h, &type) != CONNECTION_ERROR_NONE) {
999                         printf("Fail to get profile type\n");
1000                         return -1;
1001                 }
1002                 printf("profile type is %d\n", type);
1003
1004                 if (type == CONNECTION_PROFILE_TYPE_CELLULAR) {
1005                 if (connection_profile_is_cellular_default(profile_h, &is_default) != CONNECTION_ERROR_NONE) {
1006                         printf("Fail to get profile is default\n");
1007                         return -1;
1008                 }
1009                         printf("[%s]\n", is_default ? "default" : "not default");
1010                 }
1011         }
1012
1013         return 1;
1014 }
1015
1016 int test_get_current_profile(void)
1017 {
1018         int rv = 0;
1019         char *profile_name = NULL;
1020         connection_profile_h profile_h;
1021
1022         rv = connection_get_current_profile(connection, &profile_h);
1023         if (rv != CONNECTION_ERROR_NONE) {
1024                 printf("Fail to get profile iterator [%d]\n", rv);
1025                 return -1;
1026         }
1027
1028         if (connection_profile_get_name(profile_h, &profile_name) != CONNECTION_ERROR_NONE) {
1029                 printf("Fail to get profile name\n");
1030                 return -1;
1031         }
1032         printf("profile name : %s\n", profile_name);
1033         g_free(profile_name);
1034
1035         connection_profile_destroy(profile_h);
1036
1037         return 1;
1038 }
1039
1040 int test_open_profile(void)
1041 {
1042         connection_profile_h profile;
1043
1044         printf("\n** Choose a profile to open. **\n");
1045
1046         if (test_get_user_selected_profile(&profile, true) == false)
1047                 return -1;
1048
1049         if (connection_open_profile(connection, profile, test_connection_opened_callback, NULL) != CONNECTION_ERROR_NONE) {
1050                 printf("Connection open Failed!!\n");
1051                 return -1;
1052         }
1053
1054         return 1;
1055 }
1056
1057 int test_get_default_cellular_service_type(void)
1058 {
1059         int input;
1060         int rv;
1061         int service_type;
1062         connection_profile_h profile;
1063         char *profile_name = NULL;
1064
1065         rv = test_get_user_int("Input profile type to get"
1066                         "(1:Internet, 2:MMS, 3:Prepaid internet, 4:Prepaid MMS, 5:Tethering):", &input);
1067
1068         if (rv == false) {
1069                 printf("Invalid input!!\n");
1070                 return -1;
1071         }
1072
1073         switch (input) {
1074         case 1:
1075                 service_type = CONNECTION_CELLULAR_SERVICE_TYPE_INTERNET;
1076                 break;
1077         case 2:
1078                 service_type = CONNECTION_CELLULAR_SERVICE_TYPE_MMS;
1079                 break;
1080         case 3:
1081                 service_type = CONNECTION_CELLULAR_SERVICE_TYPE_PREPAID_INTERNET;
1082                 break;
1083         case 4:
1084                 service_type = CONNECTION_CELLULAR_SERVICE_TYPE_PREPAID_MMS;
1085                 break;
1086         case 5:
1087                 service_type = CONNECTION_CELLULAR_SERVICE_TYPE_TETHERING;
1088                 break;
1089         default:
1090                 printf("Wrong number!!\n");
1091                 return -1;
1092         }
1093
1094         if (connection_get_default_cellular_service_profile(connection, service_type, &profile) != CONNECTION_ERROR_NONE)
1095                 return -1;
1096
1097         if (connection_profile_get_name(profile, &profile_name) != CONNECTION_ERROR_NONE) {
1098                 printf("Fail to get profile name\n");
1099                 connection_profile_destroy(profile);
1100                 return -1;
1101         }
1102         printf("Default profile name : %s\n", profile_name);
1103         g_free(profile_name);
1104
1105         connection_profile_destroy(profile);
1106
1107         return 1;
1108 }
1109
1110 int test_set_default_cellular_service_type(void)
1111 {
1112         connection_profile_h profile;
1113         connection_cellular_service_type_e type;
1114         int input, rv;
1115
1116         rv = test_get_user_int("Input API type (1:sync, 2:async)", &input);
1117
1118         if (rv == false || (input != 1 && input != 2)) {
1119                 printf("Invalid input!!\n");
1120                 return -1;
1121         }
1122
1123         printf("\n** Choose a profile to set default service(internet or prepaid internet type only). **\n");
1124
1125         if (test_get_user_selected_profile(&profile, true) == false)
1126                 return -1;
1127
1128         if (connection_profile_get_cellular_service_type(profile, &type) != CONNECTION_ERROR_NONE) {
1129                 printf("Fail to get cellular service type\n");
1130                 return -1;
1131         }
1132
1133         if (input == 1) {
1134                 if (connection_set_default_cellular_service_profile(connection, type, profile) != CONNECTION_ERROR_NONE)
1135                         return -1;
1136         } else {
1137                 if (connection_set_default_cellular_service_profile_async(connection,
1138                                 type, profile, test_connection_set_default_callback, NULL) != CONNECTION_ERROR_NONE)
1139                         return -1;
1140         }
1141
1142         return 1;
1143 }
1144
1145 int test_close_profile(void)
1146 {
1147         connection_profile_h profile;
1148
1149         printf("\n** Choose a profile to close. **\n");
1150
1151         if (test_get_user_selected_profile(&profile, true) == false)
1152                 return -1;
1153
1154         if (connection_close_profile(connection, profile, test_connection_closed_callback, NULL) != CONNECTION_ERROR_NONE) {
1155                 printf("Connection close Failed!!\n");
1156                 return -1;
1157         }
1158
1159         return 1;
1160 }
1161
1162 int test_add_profile(void)
1163 {
1164         int rv = 0;
1165         connection_profile_h profile;
1166         char input_str[100] = {0,};
1167
1168         if (test_get_user_string("Input Keyword - (Enter for skip) :", input_str, 100) == false)
1169                 return -1;
1170
1171         rv = connection_profile_create(CONNECTION_PROFILE_TYPE_CELLULAR, input_str, &profile);
1172         if (rv != CONNECTION_ERROR_NONE)
1173                 RETURN_FAIL_DESTROY(profile);
1174
1175         if (test_update_cellular_info(profile) == -1)
1176                 RETURN_FAIL_DESTROY(profile);
1177
1178         rv = connection_add_profile(connection, profile);
1179         if (rv != CONNECTION_ERROR_NONE)
1180                 RETURN_FAIL_DESTROY(profile);
1181
1182         connection_profile_destroy(profile);
1183         return 1;
1184 }
1185
1186 int test_remove_profile(void)
1187 {
1188         connection_profile_h profile;
1189
1190         printf("\n** Choose a profile to remove. **\n");
1191         if (test_get_user_selected_profile(&profile, true) == false)
1192                 return -1;
1193
1194         if (connection_remove_profile(connection, profile) != CONNECTION_ERROR_NONE) {
1195                 printf("Remove profile Failed!!\n");
1196                 return -1;
1197         }
1198
1199         return 1;
1200 }
1201
1202 int test_update_profile(void)
1203 {
1204         int rv = 0;
1205
1206         connection_profile_type_e prof_type;
1207         connection_profile_h profile;
1208
1209         printf("\n** Choose a profile to update. **\n");
1210         if (test_get_user_selected_profile(&profile, true) == false)
1211                 return -1;
1212
1213         if (connection_profile_get_type(profile, &prof_type) != CONNECTION_ERROR_NONE)
1214                 return -1;
1215
1216         switch (prof_type) {
1217         case CONNECTION_PROFILE_TYPE_CELLULAR:
1218                 if (test_update_cellular_info(profile) == -1)
1219                         return -1;
1220
1221                 break;
1222         case CONNECTION_PROFILE_TYPE_WIFI:
1223                 if (test_update_wifi_info(profile) == -1)
1224                         return -1;
1225
1226                 if (test_update_network_info(profile) == -1)
1227                         return -1;
1228
1229                 break;
1230         case CONNECTION_PROFILE_TYPE_ETHERNET:
1231                 if (test_update_network_info(profile) == -1)
1232                         return -1;
1233
1234                 break;
1235         case CONNECTION_PROFILE_TYPE_BT:
1236                 printf("Not supported!\n");
1237                 /* fall through */
1238         default:
1239                 return -1;
1240         }
1241
1242         rv = connection_update_profile(connection, profile);
1243         if (rv != CONNECTION_ERROR_NONE)
1244                 return -1;
1245
1246         return 1;
1247 }
1248
1249 int test_get_profile_info(void)
1250 {
1251         connection_profile_type_e prof_type;
1252         connection_profile_state_e profile_state;
1253         connection_profile_h profile;
1254         char *profile_name = NULL;
1255         int address_family = 0;
1256
1257         printf("\n** Choose a profile to print. **\n");
1258         if (test_get_user_selected_profile(&profile, true) == false)
1259                 return -1;
1260
1261         if (connection_profile_get_name(profile, &profile_name) != CONNECTION_ERROR_NONE) {
1262                 printf("Fail to get profile name\n");
1263                 return -1;
1264         } else {
1265                 printf("Profile Name : %s\n", profile_name);
1266                 g_free(profile_name);
1267         }
1268
1269         if (connection_profile_get_state(profile, &profile_state) != CONNECTION_ERROR_NONE) {
1270                 printf("Fail to get profile state\n");
1271                 return -1;
1272         } else
1273                 printf("Profile State : %s\n", test_print_state(profile_state));
1274
1275         if (connection_profile_get_type(profile, &prof_type) != CONNECTION_ERROR_NONE)
1276                 return -1;
1277
1278         test_get_user_int("Input Address Family (0:IPv4 1:IPv6) :", &address_family);
1279
1280         switch (prof_type) {
1281         case CONNECTION_PROFILE_TYPE_CELLULAR:
1282                 printf("Profile Type : Cellular\n");
1283                 test_print_cellular_info(profile);
1284                 break;
1285         case CONNECTION_PROFILE_TYPE_WIFI:
1286                 printf("Profile Type : Wi-Fi\n");
1287                 test_print_wifi_info(profile);
1288                 break;
1289         case CONNECTION_PROFILE_TYPE_ETHERNET:
1290                 printf("Profile Type : Ethernet\n");
1291                 break;
1292         case CONNECTION_PROFILE_TYPE_BT:
1293                 printf("Profile Type : Bluetooth\n");
1294                 break;
1295         default:
1296                 return -1;
1297         }
1298
1299         test_print_network_info(profile, address_family);
1300
1301         return 1;
1302 }
1303
1304 int test_refresh_profile_info(void)
1305 {
1306         connection_profile_type_e prof_type;
1307         connection_profile_state_e profile_state;
1308         connection_profile_h profile;
1309         char *profile_name = NULL;
1310         int address_family = 0;
1311
1312         printf("\n** Choose a profile to refresh. **\n");
1313         if (test_get_user_selected_profile(&profile, true) == false)
1314                 return -1;
1315
1316         if (connection_profile_refresh(profile) != CONNECTION_ERROR_NONE)
1317                 return -1;
1318
1319         if (connection_profile_get_name(profile, &profile_name) != CONNECTION_ERROR_NONE) {
1320                 printf("Fail to get profile name\n");
1321                 return -1;
1322         } else {
1323                 printf("Profile Name : %s\n", profile_name);
1324                 g_free(profile_name);
1325         }
1326
1327         if (connection_profile_get_state(profile, &profile_state) != CONNECTION_ERROR_NONE) {
1328                 printf("Fail to get profile state\n");
1329                 return -1;
1330         } else
1331                 printf("Profile State : %s\n", test_print_state(profile_state));
1332
1333
1334         if (connection_profile_get_type(profile, &prof_type) != CONNECTION_ERROR_NONE)
1335                 return -1;
1336
1337         test_get_user_int("Input Address Family (0:IPv4 1:IPv6) :", &address_family);
1338
1339         switch (prof_type) {
1340         case CONNECTION_PROFILE_TYPE_CELLULAR:
1341                 printf("Profile Type : Cellular\n");
1342                 test_print_cellular_info(profile);
1343                 break;
1344         case CONNECTION_PROFILE_TYPE_WIFI:
1345                 printf("Profile Type : Wi-Fi\n");
1346                 test_print_wifi_info(profile);
1347                 break;
1348         case CONNECTION_PROFILE_TYPE_ETHERNET:
1349                 printf("Profile Type : Ethernet\n");
1350                 break;
1351         case CONNECTION_PROFILE_TYPE_BT:
1352                 printf("Profile Type : Bluetooth\n");
1353                 break;
1354         default:
1355                 return -1;
1356         }
1357
1358         test_print_network_info(profile, address_family);
1359
1360         return 1;
1361 }
1362
1363 int test_set_state_changed_callback()
1364 {
1365         connection_profile_h profile;
1366         connection_profile_h profile_clone;
1367
1368         printf("\n** Choose a profile to set callback. **\n");
1369         if (test_get_user_selected_profile(&profile, true) == false)
1370                 return -1;
1371
1372         if (connection_profile_clone(&profile_clone, profile) != CONNECTION_ERROR_NONE)
1373                 return -1;
1374
1375         if (connection_profile_set_state_changed_cb(profile,
1376                         test_profile_state_callback, profile_clone) != CONNECTION_ERROR_NONE) {
1377                 connection_profile_destroy(profile_clone);
1378                 return -1;
1379         }
1380
1381         state_cb_list = g_slist_append(state_cb_list, profile_clone);
1382
1383         return 1;
1384 }
1385
1386 int test_unset_state_changed_callback()
1387 {
1388         connection_profile_h profile;
1389         GSList *list;
1390         char *profile_name = NULL;
1391         int count = 0;
1392         int input = 0;
1393
1394         printf("\n** Choose a profile to unset callback. **\n");
1395         for (list = state_cb_list; list; list = list->next) {
1396                 profile = list->data;
1397                 if (connection_profile_get_name(profile, &profile_name) != CONNECTION_ERROR_NONE) {
1398                         printf("Fail to get profile name!\n");
1399                         return -1;
1400                 } else {
1401                         printf("%d. %s\n", count, profile_name);
1402                         g_free(profile_name);
1403                 }
1404
1405                 count++;
1406         }
1407
1408         if (test_get_user_int("Input profile number(Enter for cancel) :", &input) == false ||
1409             input >= count ||
1410             input < 0) {
1411                 printf("Wrong number!!\n");
1412                 return -1;
1413         }
1414
1415         count = 0;
1416         for (list = state_cb_list; list; list = list->next) {
1417                 if (count == input) {
1418                         profile = list->data;
1419                         goto unset;
1420                 }
1421
1422                 count++;
1423         }
1424
1425         return -1;
1426
1427 unset:
1428         if (connection_profile_unset_state_changed_cb(profile) != CONNECTION_ERROR_NONE)
1429                 return -1;
1430
1431         state_cb_list = g_slist_remove(state_cb_list, profile);
1432         connection_profile_destroy(profile);
1433
1434         return 1;
1435 }
1436
1437 int test_reset_call_statistics_info(void)
1438 {
1439         int ret = CONNECTION_ERROR_NONE;
1440
1441         ret = connection_reset_statistics(connection, CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_LAST_RECEIVED_DATA);
1442         printf("reset last recv data size [%d]\n", ret);
1443         ret = connection_reset_statistics(connection, CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_LAST_SENT_DATA);
1444         printf("last sent data size [%d]\n", ret);
1445         ret = connection_reset_statistics(connection, CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_TOTAL_RECEIVED_DATA);
1446         printf("total received data size [%d]\n", ret);
1447         ret = connection_reset_statistics(connection, CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_TOTAL_SENT_DATA);
1448         printf("total sent data size [%d]\n", ret);
1449
1450         return 1;
1451 }
1452
1453 int test_reset_wifi_call_statistics_info(void)
1454 {
1455         int ret = CONNECTION_ERROR_NONE;
1456
1457         ret = connection_reset_statistics(connection, CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_LAST_SENT_DATA);
1458         printf("WiFi last sent data size [%d]\n", ret);
1459         ret = connection_reset_statistics(connection, CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_LAST_RECEIVED_DATA);
1460         printf("WiFi last recv data size [%d]\n", ret);
1461         ret = connection_reset_statistics(connection, CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_TOTAL_SENT_DATA);
1462         printf("WiFi total sent data size [%d]\n", ret);
1463         ret = connection_reset_statistics(connection, CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_TOTAL_RECEIVED_DATA);
1464         printf("WiFi total received data size [%d]\n", ret);
1465
1466         return 1;
1467 }
1468
1469 int test_add_route(void)
1470 {
1471         int rv = 0;
1472         char ip_addr[100] = {0};
1473         char if_name[40] = {0};
1474
1475         if (test_get_user_string("Input IP - (Enter for skip) :", ip_addr, 100) == false)
1476                 return -1;
1477
1478         if (test_get_user_string("Input Interface name - (Enter for skip) :", if_name, 40) == false)
1479                 return -1;
1480
1481         g_strstrip(ip_addr);
1482         g_strstrip(if_name);
1483         rv = connection_add_route(connection, if_name, ip_addr);
1484         if (rv != CONNECTION_ERROR_NONE) {
1485                 printf("Fail to get add new route [%d]\n", rv);
1486                 return -1;
1487         }
1488         printf("Add Route successfully\n");
1489
1490         return 1;
1491 }
1492
1493 int test_remove_route(void)
1494 {
1495         int rv = 0;
1496         char ip_addr[100] = {0};
1497         char if_name[40] = {0};
1498
1499         if (test_get_user_string("Input IP - (Enter for skip) :", ip_addr, 100) == false)
1500                 return -1;
1501
1502         if (test_get_user_string("Input Interface name - (Enter for skip) :", if_name, 40) == false)
1503                 return -1;
1504
1505         g_strstrip(ip_addr);
1506         g_strstrip(if_name);
1507         rv = connection_remove_route(connection, if_name, ip_addr);
1508         if (rv != CONNECTION_ERROR_NONE) {
1509                 printf("Fail to remove the route [%s]\n", test_print_error(rv));
1510                 return -1;
1511         }
1512         printf("Remove Route successfully\n");
1513
1514         return 1;
1515 }
1516
1517 int test_add_route_ipv6(void)
1518 {
1519         int rv = 0;
1520         char ip_addr[100] = {0};
1521         char gateway[100] = {0};
1522         char if_name[40] = {0};
1523
1524         if (test_get_user_string("Input IPv6 - (Enter for skip) :", ip_addr, 100) == false)
1525                 return -1;
1526
1527         if (test_get_user_string("Input Gateway - (Enter for skip) :", gateway, 100) == false)
1528                 return -1;
1529
1530         if (test_get_user_string("Input Interface name - (Enter for skip) :", if_name, 40) == false)
1531                 return -1;
1532
1533         g_strstrip(ip_addr);
1534         g_strstrip(gateway);
1535         g_strstrip(if_name);
1536         rv = connection_add_route_ipv6(connection, if_name, ip_addr, gateway);
1537         if (rv != CONNECTION_ERROR_NONE) {
1538                 printf("Fail to get add new route [%d]\n", rv);
1539                 return -1;
1540         }
1541         printf("Add Route successfully\n");
1542
1543         return 1;
1544 }
1545
1546 int test_remove_route_ipv6(void)
1547 {
1548         int rv = 0;
1549         char ip_addr[100] = {0};
1550         char gateway[100] = {0};
1551         char if_name[40] = {0};
1552
1553         if (test_get_user_string("Input IPv6 - (Enter for skip) :", ip_addr, 100) == false)
1554                 return -1;
1555
1556         if (test_get_user_string("Input Gateway - (Enter for skip) :", gateway, 100) == false)
1557                 return -1;
1558
1559         if (test_get_user_string("Input Interface name - (Enter for skip) :", if_name, 40) == false)
1560                 return -1;
1561
1562         g_strstrip(ip_addr);
1563         g_strstrip(gateway);
1564         g_strstrip(if_name);
1565         rv = connection_remove_route_ipv6(connection, if_name, ip_addr, gateway);
1566         if (rv != CONNECTION_ERROR_NONE) {
1567                 printf("Fail to remove the route [%d]\n", rv);
1568                 return -1;
1569         }
1570         printf("Remove Route successfully\n");
1571
1572         return 1;
1573 }
1574
1575 int test_get_bt_state(void)
1576 {
1577         int rv = 0;
1578         connection_bt_state_e bt_state;
1579
1580         rv = connection_get_bt_state(connection, &bt_state);
1581
1582         if (rv != CONNECTION_ERROR_NONE) {
1583                 printf("Fail to get Bluetooth state [%d]\n", rv);
1584                 return -1;
1585         }
1586
1587         printf("Retval = %d, Bluetooth state [%d]\n", rv, bt_state);
1588
1589         return 1;
1590 }
1591
1592 int test_get_profile_id(void)
1593 {
1594         connection_profile_h profile;
1595         char *profile_id;
1596
1597         printf("\n** Choose a profile to see profile id. **\n");
1598         if (test_get_user_selected_profile(&profile, true) == false)
1599                 return -1;
1600
1601         if (connection_profile_get_id(profile, &profile_id) != CONNECTION_ERROR_NONE) {
1602                 printf("Fail to get profile name\n");
1603                 return -1;
1604         } else {
1605                 printf("Profile id : %s\n", profile_id);
1606                 g_free(profile_id);
1607         }
1608
1609         return 1;
1610 }
1611
1612 int test_get_mac_address(void)
1613 {
1614         int rv = 0, type = 0;
1615         connection_type_e conn_type;
1616         char *mac_addr = NULL;
1617
1618         test_get_user_int("Input connection type (1:wifi, 2:ethernet)", &type);
1619
1620         switch (type) {
1621         case 1:
1622                 conn_type = CONNECTION_TYPE_WIFI;
1623                 break;
1624         case 2:
1625                 conn_type = CONNECTION_TYPE_ETHERNET;
1626                 break;
1627         default:
1628                 printf("Wrong number!!\n");
1629                 return -1;
1630         }
1631
1632         rv = connection_get_mac_address(connection, conn_type, &mac_addr);
1633
1634         if (rv != CONNECTION_ERROR_NONE) {
1635                 printf("Fail to get MAC address [%s]\n", test_print_error(rv));
1636                 return -1;
1637         }
1638
1639         printf("mac address is %s\n", mac_addr);
1640
1641         g_free(mac_addr);
1642
1643         return 1;
1644 }
1645
1646 int test_get_ethernet_cable_state(void)
1647 {
1648         int rv = 0;
1649         connection_ethernet_cable_state_e cable_state;
1650
1651         rv = connection_get_ethernet_cable_state(connection, &cable_state);
1652
1653         if (rv != CONNECTION_ERROR_NONE) {
1654                 printf("Fail to get ethernet cable state [%s]\n", test_print_error(rv));
1655                 return -1;
1656         }
1657
1658         printf("Retval = [%s], Ethernet cable state [%d]\n", test_print_error(rv), cable_state);
1659
1660         return 1;
1661 }
1662
1663 int test_reset_profile(void)
1664 {
1665         int type, sim_id, rv;
1666
1667         rv = test_get_user_int("Input reset type (0:default profile reset, 1:delete profile reset)", &type);
1668
1669         if (rv == false || (type != 0 && type != 1)) {
1670                 printf("Invalid input!!\n");
1671                 return -1;
1672         }
1673
1674         rv = test_get_user_int("Input SIM id to reset (0:SIM1, 1:SIM2)", &sim_id);
1675
1676         if (rv == false || (sim_id != 0 && sim_id != 1)) {
1677                 printf("Invalid input!!\n");
1678                 return -1;
1679         }
1680
1681         if (connection_reset_profile(connection, type, sim_id, test_connection_reset_profile_callback, NULL) != CONNECTION_ERROR_NONE) {
1682                 return -1;
1683         }
1684
1685         return 1;
1686 }
1687
1688 int main(int argc, char **argv)
1689 {
1690         GMainLoop *mainloop;
1691         mainloop = g_main_loop_new (NULL, FALSE);
1692
1693         GIOChannel *channel = g_io_channel_unix_new(0);
1694         g_io_add_watch(channel, (G_IO_IN|G_IO_ERR|G_IO_HUP|G_IO_NVAL), test_thread,NULL );
1695
1696         printf("Test Thread created...\n");
1697
1698         g_main_loop_run (mainloop);
1699
1700         return 0;
1701 }
1702
1703 gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data)
1704 {
1705         int rv = 0;
1706         char a[100];
1707
1708         memset(a, '\0', 100);
1709         printf("Event received from stdin\n");
1710
1711         rv = read(0, a, 100);
1712
1713         if (rv < 0 || a[0] == '0') {
1714                 if (connection != NULL)
1715                         test_deregister_client();
1716
1717                 exit(1);
1718         }
1719
1720         if (*a == '\n' || *a == '\r'){
1721                 printf("\n\n Network Connection API Test App\n\n");
1722                 printf("Options..\n");
1723                 printf("1       - Create Handle and set callbacks\n");
1724                 printf("2       - Destroy Handle(unset callbacks automatically)\n");
1725                 printf("3       - Get network state\n");
1726                 printf("4       - Get cellular state (please insert SIM Card)\n");
1727                 printf("5       - Get wifi state (please turn on WiFi)\n");
1728                 printf("6       - Get current proxy address \n");
1729                 printf("7       - Get current Ip address\n");
1730                 printf("8       - Get cellular data call statistics\n");
1731                 printf("9       - Get WiFi data call statistics\n");
1732                 printf("a       - Get Profile list\n");
1733                 printf("b       - Get Connected Profile list\n");
1734                 printf("c       - Get Current profile\n");
1735                 printf("d       - Open connection with profile\n");
1736                 printf("e       - Get default cellular service by type\n");
1737                 printf("f       - Set default cellular service by type\n");
1738                 printf("g       - Close connection with profile\n");
1739                 printf("h       - Add profile(Cellular only)\n");
1740                 printf("i       - Remove profile(Cellular:delete, WiFi:forgot)\n");
1741                 printf("j       - Update profile\n");
1742                 printf("k       - Get profile info\n");
1743                 printf("l       - Refresh profile info\n");
1744                 printf("m       - Set state changed callback\n");
1745                 printf("n       - Unset state changed callback\n");
1746                 printf("o       - Reset cellular data call statistics\n");
1747                 printf("p       - Reset WiFi data call statistics\n");
1748                 printf("q       - Add new route\n");
1749                 printf("r       - Remove a route\n");
1750                 printf("s       - Get Bluetooth state\n");
1751                 printf("t       - Get profile id\n");
1752                 printf("u       - Reset profile\n");
1753                 printf("v       - Get all cellular default profiles\n");
1754                 printf("w       - Get mac address\n");
1755                 printf("x       - Get ethernet cable state\n");
1756                 printf("B       - Add IPv6 new route\n");
1757                 printf("C       - Remove IPv6 route\n");
1758                 printf("0       - Exit \n");
1759                 printf("ENTER  - Show options menu.......\n");
1760         }
1761
1762         switch (a[0]) {
1763         case '1':
1764                 rv = test_register_client();
1765                 break;
1766         case '2':
1767                 rv = test_deregister_client();
1768                 break;
1769         case '3':
1770                 rv = test_get_network_state();
1771                 break;
1772         case '4':
1773                 rv = test_get_cellular_state();
1774                 break;
1775         case '5':
1776                 rv = test_get_wifi_state();
1777                 break;
1778         case '6':
1779                 rv = test_get_current_proxy();
1780                 break;
1781         case '7':
1782                 rv = test_get_current_ip();
1783                 break;
1784         case '8':
1785                 rv = test_get_call_statistics_info();
1786                 break;
1787         case '9':
1788                 rv = test_get_wifi_call_statistics_info();
1789                 break;
1790         case 'a':
1791                 rv = test_get_profile_list();
1792                 break;
1793         case 'b':
1794                 rv = test_get_connected_profile_list();
1795                 break;
1796         case 'c':
1797                 rv = test_get_current_profile();
1798                 break;
1799         case 'd':
1800                 rv = test_open_profile();
1801                 break;
1802         case 'e':
1803                 rv = test_get_default_cellular_service_type();
1804                 break;
1805         case 'f':
1806                 rv = test_set_default_cellular_service_type();
1807                 break;
1808         case 'g':
1809                 rv = test_close_profile();
1810                 break;
1811         case 'h':
1812                 rv = test_add_profile();
1813                 break;
1814         case 'i':
1815                 rv = test_remove_profile();
1816                 break;
1817         case 'j':
1818                 rv = test_update_profile();
1819                 break;
1820         case 'k':
1821                 rv = test_get_profile_info();
1822                 break;
1823         case 'l':
1824                 rv = test_refresh_profile_info();
1825                 break;
1826         case 'm':
1827                 rv = test_set_state_changed_callback();
1828                 break;
1829         case 'n':
1830                 rv = test_unset_state_changed_callback();
1831                 break;
1832         case 'o':
1833                 rv = test_reset_call_statistics_info();
1834                 break;
1835         case 'p':
1836                 rv = test_reset_wifi_call_statistics_info();
1837                 break;
1838         case 'q':
1839                 rv = test_add_route();
1840                 break;
1841         case 'r':
1842                 rv = test_remove_route();
1843                 break;
1844         case 's':
1845                 rv = test_get_bt_state();
1846                 break;
1847         case 't':
1848                 rv = test_get_profile_id();
1849                 break;
1850         case 'u':
1851                 rv = test_reset_profile();
1852                 break;
1853         case 'v':
1854                 rv = test_get_default_profile_list();
1855                 break;
1856         case 'w':
1857                 rv = test_get_mac_address();
1858                 break;
1859         case 'x':
1860                 rv = test_get_ethernet_cable_state();
1861                 break;
1862         case 'B':
1863                 rv = test_add_route_ipv6();
1864                 break;
1865         case 'C':
1866                 rv = test_remove_route_ipv6();
1867                 break;
1868         }
1869
1870         if (rv == 1)
1871                 printf("Operation succeeded!\n");
1872         else
1873                 printf("Operation failed!\n");
1874
1875         return TRUE;
1876 }