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