Merge "Add test case for ethernet profile" into tizen
[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)
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                                                         CONNECTION_ADDRESS_FAMILY_IPV4,
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                                                         CONNECTION_ADDRESS_FAMILY_IPV4,
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                                                         CONNECTION_ADDRESS_FAMILY_IPV4,
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                                                         CONNECTION_ADDRESS_FAMILY_IPV4,
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                                                                 CONNECTION_ADDRESS_FAMILY_IPV4,
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)
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                                                                 CONNECTION_ADDRESS_FAMILY_IPV4,
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
490         if (test_get_user_int("Input IPv4 Address Type (DHCP:1, Static:2)"
491                                 " - (Enter for skip) :", &input_int)) {
492                 switch (input_int) {
493                 case 1:
494                         rv = connection_profile_set_ip_config_type(profile,
495                                                                 CONNECTION_ADDRESS_FAMILY_IPV4,
496                                                                 CONNECTION_IP_CONFIG_TYPE_DYNAMIC);
497                         break;
498                 case 2:
499                         rv = connection_profile_set_ip_config_type(profile,
500                                                                 CONNECTION_ADDRESS_FAMILY_IPV4,
501                                                                 CONNECTION_IP_CONFIG_TYPE_STATIC);
502                         if (rv != CONNECTION_ERROR_NONE)
503                                 return -1;
504
505                         if (test_update_ip_info(profile) == -1)
506                                 return -1;
507
508                         if (test_update_proxy_info(profile) == -1)
509                                 return -1;
510                         break;
511                 default:
512                         return -1;
513                 }
514
515                 if (rv != CONNECTION_ERROR_NONE)
516                         return -1;
517         } else
518                 return -1;
519
520         return 1;
521 }
522
523 static void test_print_cellular_info(connection_profile_h profile)
524 {
525         connection_cellular_network_type_e network_type;
526         connection_cellular_service_type_e service_type;
527         char *apn = NULL;
528         connection_cellular_auth_type_e auth_type;
529         char *user_name = NULL;
530         char *password = NULL;
531         char *home_url = NULL;
532         bool roaming = false;
533         bool hidden = false;
534         bool editable = false;
535
536         if (connection_profile_get_cellular_network_type(profile, &network_type) != CONNECTION_ERROR_NONE)
537                 printf("Fail to get cellular network type!\n");
538         else
539                 printf("Cellular network type : %d\n", network_type);
540
541         if (connection_profile_get_cellular_service_type(profile, &service_type) != CONNECTION_ERROR_NONE)
542                 printf("Fail to get cellular service type!\n");
543         else
544                 printf("Cellular service type : %d\n", service_type);
545
546         if (connection_profile_get_cellular_apn(profile, &apn) != CONNECTION_ERROR_NONE)
547                 printf("Fail to get cellular APN!\n");
548         else {
549                 printf("Cellular APN : %s\n", apn);
550                 g_free(apn);
551         }
552
553         if (connection_profile_get_cellular_auth_info(profile, &auth_type, &user_name, &password) != CONNECTION_ERROR_NONE)
554                 printf("Fail to get auth info!\n");
555         else {
556                 printf("Cellular auth type : %d\n", auth_type);
557                 printf("Cellular user_name : %s\n", user_name);
558                 printf("Cellular password : %s\n", password);
559                 g_free(user_name);
560                 g_free(password);
561         }
562
563         if (connection_profile_get_cellular_home_url(profile, &home_url) != CONNECTION_ERROR_NONE)
564                 printf("Fail to get cellular home url!\n");
565         else {
566                 printf("Cellular home url : %s\n", home_url);
567                 g_free(home_url);
568         }
569
570         if (connection_profile_is_cellular_roaming(profile, &roaming) != CONNECTION_ERROR_NONE)
571                 printf("Fail to get cellular is roaming!\n");
572         else
573                 printf("Cellular roaming : %s\n", roaming ? "true" : "false");
574
575         if (connection_profile_is_cellular_hidden(profile, &hidden) != CONNECTION_ERROR_NONE)
576                 printf("Fail to get cellular hidden state!\n");
577         else
578                 printf("Cellular hidden : %s\n", hidden ? "true" : "false");
579
580         if (connection_profile_is_cellular_editable(profile, &editable) != CONNECTION_ERROR_NONE)
581                 printf("Fail to get cellular editing state!\n");
582         else
583                 printf("Cellular editable : %s\n", editable ? "true" : "false");
584 }
585
586 static void test_print_wifi_info(connection_profile_h profile)
587 {
588         char *essid = NULL;
589         char *bssid = NULL;
590         int rssi = 0;
591         int frequency = 0;
592         int max_speed = 0;
593         connection_wifi_security_type_e security_type;
594         connection_wifi_encryption_type_e encryption_type;
595         bool pass_required = false;
596         bool wps_supported = false;
597
598         if (connection_profile_get_wifi_essid(profile, &essid) != CONNECTION_ERROR_NONE)
599                 printf("Fail to get Wi-Fi essid!\n");
600         else {
601                 printf("Wi-Fi essid : %s\n", essid);
602                 g_free(essid);
603         }
604
605         if (connection_profile_get_wifi_bssid(profile, &bssid) != CONNECTION_ERROR_NONE)
606                 printf("Fail to get Wi-Fi bssid!\n");
607         else {
608                 printf("Wi-Fi bssid : %s\n", bssid);
609                 g_free(bssid);
610         }
611
612         if (connection_profile_get_wifi_rssi(profile, &rssi) != CONNECTION_ERROR_NONE)
613                 printf("Fail to get Wi-Fi rssi!\n");
614         else
615                 printf("Wi-Fi rssi : %d\n", rssi);
616
617         if (connection_profile_get_wifi_frequency(profile, &frequency) != CONNECTION_ERROR_NONE)
618                 printf("Fail to get Wi-Fi frequency!\n");
619         else
620                 printf("Wi-Fi frequency : %d\n", frequency);
621
622         if (connection_profile_get_wifi_max_speed(profile, &max_speed) != CONNECTION_ERROR_NONE)
623                 printf("Fail to get Wi-Fi max speed!\n");
624         else
625                 printf("Wi-Fi max speed : %d\n", max_speed);
626
627         if (connection_profile_get_wifi_security_type(profile, &security_type) != CONNECTION_ERROR_NONE)
628                 printf("Fail to get Wi-Fi security type!\n");
629         else
630                 printf("Wi-Fi security type : %d\n", security_type);
631
632         if (connection_profile_get_wifi_encryption_type(profile, &encryption_type) != CONNECTION_ERROR_NONE)
633                 printf("Fail to get Wi-Fi encryption type!\n");
634         else
635                 printf("Wi-Fi encryption type : %d\n", encryption_type);
636
637         if (connection_profile_is_wifi_passphrase_required(profile, &pass_required) != CONNECTION_ERROR_NONE)
638                 printf("Fail to get Wi-Fi passphrase required!\n");
639         else
640                 printf("Wi-Fi passphrase required : %s\n", pass_required ? "true" : "false");
641
642         if (connection_profile_is_wifi_wps_supported(profile, &wps_supported) != CONNECTION_ERROR_NONE)
643                 printf("Fail to get Wi-Fi wps info\n");
644         else
645                 printf("Wi-Fi wps supported : %s\n", wps_supported ? "true" : "false");
646 }
647
648 static void test_print_network_info(connection_profile_h profile)
649 {
650         char *interface_name = NULL;
651         connection_ip_config_type_e ip_type;
652         char *ip = NULL;
653         char *subnet = NULL;
654         char *gateway = NULL;
655         char *dns1 = NULL;
656         char *dns2 = NULL;
657         connection_proxy_type_e proxy_type;
658         char *proxy = NULL;
659
660         if (connection_profile_get_network_interface_name(profile, &interface_name) != CONNECTION_ERROR_NONE)
661                 printf("Fail to get interface name!\n");
662         else {
663                 printf("Interface name : %s\n", interface_name);
664                 g_free(interface_name);
665         }
666
667         if (connection_profile_get_ip_config_type(profile, CONNECTION_ADDRESS_FAMILY_IPV4, &ip_type) != CONNECTION_ERROR_NONE)
668                 printf("Fail to get ipconfig type!\n");
669         else
670                 printf("Ipconfig type : %d\n", ip_type);
671
672         if (connection_profile_get_ip_address(profile, CONNECTION_ADDRESS_FAMILY_IPV4, &ip) != CONNECTION_ERROR_NONE)
673                 printf("Fail to get IP address!\n");
674         else {
675                 printf("IP address : %s\n", ip);
676                 g_free(ip);
677         }
678
679         if (connection_profile_get_subnet_mask(profile, CONNECTION_ADDRESS_FAMILY_IPV4, &subnet) != CONNECTION_ERROR_NONE)
680                 printf("Fail to get subnet mask!\n");
681         else {
682                 printf("Subnet mask : %s\n", subnet);
683                 g_free(subnet);
684         }
685
686         if (connection_profile_get_gateway_address(profile, CONNECTION_ADDRESS_FAMILY_IPV4, &gateway) != CONNECTION_ERROR_NONE)
687                 printf("Fail to get gateway!\n");
688         else {
689                 printf("Gateway : %s\n", gateway);
690                 g_free(gateway);
691         }
692
693         if (connection_profile_get_dns_address(profile, 1, CONNECTION_ADDRESS_FAMILY_IPV4, &dns1) != CONNECTION_ERROR_NONE)
694                 printf("Fail to get DNS1!\n");
695         else {
696                 printf("DNS1 : %s\n", dns1);
697                 g_free(dns1);
698         }
699
700         if (connection_profile_get_dns_address(profile, 2, CONNECTION_ADDRESS_FAMILY_IPV4, &dns2) != CONNECTION_ERROR_NONE)
701                 printf("Fail to get DNS2!\n");
702         else {
703                 printf("DNS2 : %s\n", dns2);
704                 g_free(dns2);
705         }
706
707         if (connection_profile_get_proxy_type(profile, &proxy_type) != CONNECTION_ERROR_NONE)
708                 printf("Fail to get proxy type!\n");
709         else
710                 printf("Proxy type : %d\n", proxy_type);
711
712         if (connection_profile_get_proxy_address(profile, CONNECTION_ADDRESS_FAMILY_IPV4, &proxy) != CONNECTION_ERROR_NONE)
713                 printf("Fail to get proxy!\n");
714         else {
715                 printf("Proxy : %s\n", proxy);
716                 g_free(proxy);
717         }
718 }
719
720 int test_register_client(void)
721 {
722
723         int err = connection_create(&connection);
724
725         if (CONNECTION_ERROR_NONE == err) {
726                 connection_set_type_changed_cb(connection, test_type_changed_callback, NULL);
727                 connection_set_ip_address_changed_cb(connection, test_ip_changed_callback, NULL);
728                 connection_set_proxy_address_changed_cb(connection, test_proxy_changed_callback, NULL);
729                 connection_set_ethernet_cable_state_chaged_cb(connection,
730                                         test_get_ethernet_cable_state_callback, NULL);
731         } else {
732                 printf("Client registration failed %d\n", err);
733                 return -1;
734         }
735
736         printf("Client registration success\n");
737         return 1;
738 }
739
740 int  test_deregister_client(void)
741 {
742         int rv = 0;
743         GSList *list;
744         connection_profile_h profile;
745
746         if (connection != NULL)
747                 rv = connection_destroy(connection);
748         else {
749                 printf("Cannot deregister : Handle is NULL\n");
750                 rv = CONNECTION_ERROR_INVALID_OPERATION;
751         }
752
753         if (rv != CONNECTION_ERROR_NONE){
754                 printf("Client deregistration fail [%d]\n", rv);
755                 return -1;
756         }
757
758         if (state_cb_list) {
759                 for (list = state_cb_list; list; list = list->next) {
760                         profile = list->data;
761                         connection_profile_destroy(profile);
762                 }
763
764                 g_slist_free(state_cb_list);
765                 state_cb_list = NULL;
766         }
767
768         connection = NULL;
769         printf("Client deregistration success\n");
770
771         return 1;
772 }
773
774 int test_get_network_state(void)
775 {
776         int rv = 0;
777         connection_type_e net_state;
778
779         rv = connection_get_type(connection, &net_state);
780
781         if (rv != CONNECTION_ERROR_NONE) {
782                 printf("Fail to get network state [%d]\n", rv);
783                 return -1;
784         }
785
786         printf("Retval = %d network connection state [%d]\n", rv, net_state);
787
788         return 1;
789 }
790
791 int test_get_cellular_state(void)
792 {
793         int rv = 0;
794         connection_cellular_state_e cellular_state;
795
796         rv = connection_get_cellular_state(connection, &cellular_state);
797
798         if (rv != CONNECTION_ERROR_NONE) {
799                 printf("Fail to get Cellular state [%d]\n", rv);
800                 return -1;
801         }
802
803         printf("Retval = %d Cellular state [%d]\n", rv, cellular_state);
804
805         return 1;
806 }
807
808 int test_get_wifi_state(void)
809 {
810         int rv = 0;
811         connection_wifi_state_e wifi_state;
812
813         rv = connection_get_wifi_state(connection, &wifi_state);
814
815         if (rv != CONNECTION_ERROR_NONE) {
816                 printf("Fail to get WiFi state [%d]\n", rv);
817                 return -1;
818         }
819
820         printf("Retval = %d WiFi state [%d]\n", rv, wifi_state);
821
822         return 1;
823 }
824
825 int test_get_current_proxy(void)
826 {
827         char *proxy_addr = NULL;
828
829         connection_get_proxy(connection, CONNECTION_ADDRESS_FAMILY_IPV4, &proxy_addr);
830
831         if (proxy_addr == NULL) {
832                 printf("Proxy address does not exist\n");
833                 return -1;
834         }
835
836         printf("Current Proxy [%s]\n", proxy_addr);
837         g_free(proxy_addr);
838
839         return 1;
840 }
841
842 int test_get_current_ip(void)
843 {
844         char *ip_addr = NULL;
845
846         connection_get_ip_address(connection, CONNECTION_ADDRESS_FAMILY_IPV4, &ip_addr);
847
848         if (ip_addr == NULL) {
849                 printf("IP address does not exist\n");
850                 return -1;
851         }
852
853         printf("IPv4 address : %s\n", ip_addr);
854         g_free(ip_addr);
855
856         return 1;
857 }
858
859 int test_get_call_statistics_info(void)
860 {
861         long long rv = 0;
862
863         connection_get_statistics(connection, CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_LAST_RECEIVED_DATA, &rv);
864         printf("last recv data size [%lld]\n", rv);
865         connection_get_statistics(connection, CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_LAST_SENT_DATA, &rv);
866         printf("last sent data size [%lld]\n",rv );
867         connection_get_statistics(connection, CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_TOTAL_RECEIVED_DATA, &rv);
868         printf("total received data size [%lld]\n",rv );
869         connection_get_statistics(connection, CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_TOTAL_SENT_DATA, &rv);
870         printf("total sent data size [%lld]\n", rv);
871
872         return 1;
873 }
874
875 int test_get_wifi_call_statistics_info(void)
876 {
877         long long rv = 0;
878
879         connection_get_statistics(connection, CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_LAST_RECEIVED_DATA, &rv);
880         printf("WiFi last recv data size [%lld]\n", rv);
881         connection_get_statistics(connection, CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_LAST_SENT_DATA, &rv);
882         printf("WiFi last sent data size [%lld]\n",rv );
883         connection_get_statistics(connection, CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_TOTAL_RECEIVED_DATA, &rv);
884         printf("WiFi total received data size [%lld]\n",rv );
885         connection_get_statistics(connection, CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_TOTAL_SENT_DATA, &rv);
886         printf("WiFi total sent data size [%lld]\n", rv);
887
888         return 1;
889 }
890
891 int test_get_profile_list(void)
892 {
893         if (test_get_user_selected_profile(NULL, false) == false)
894                 return -1;
895
896         return 1;
897 }
898
899 int test_get_default_profile_list(void)
900 {
901         int rv = 0;
902         char *profile_name = NULL;
903         connection_profile_iterator_h profile_iter;
904         connection_profile_h profile_h;
905         connection_cellular_service_type_e service_type;
906         bool is_default = false;
907
908         rv = connection_get_profile_iterator(connection, CONNECTION_ITERATOR_TYPE_DEFAULT, &profile_iter);
909         if (rv != CONNECTION_ERROR_NONE) {
910                 printf("Fail to get profile iterator [%s]\n", test_print_error(rv));
911                 return -1;
912         }
913
914         while (connection_profile_iterator_has_next(profile_iter)) {
915                 if (connection_profile_iterator_next(profile_iter, &profile_h) != CONNECTION_ERROR_NONE) {
916                         printf("Fail to get profile handle\n");
917                         return -1;
918                 }
919
920                 if (connection_profile_get_name(profile_h, &profile_name) != CONNECTION_ERROR_NONE) {
921                         printf("Fail to get profile name\n");
922                         return -1;
923                 }
924                 printf("profile name : %s\n", profile_name);
925                 g_free(profile_name);
926
927                 if (connection_profile_get_cellular_service_type(profile_h, &service_type) != CONNECTION_ERROR_NONE) {
928                         printf("Fail to get profile service type\n");
929                         return -1;
930                 }
931                 printf("service type : %d\n", service_type);
932
933                 if (connection_profile_is_cellular_default(profile_h, &is_default) != CONNECTION_ERROR_NONE) {
934                         printf("Fail to get profile subscriber id\n");
935                         return -1;
936                 }
937                 printf("Default : %d\n", is_default);
938         }
939
940         return 1;
941 }
942
943 int test_get_connected_profile_list(void)
944 {
945         int rv = 0;
946         char *profile_name = NULL;
947         connection_profile_iterator_h profile_iter;
948         connection_profile_h profile_h;
949         bool is_default = false;
950         connection_profile_type_e type;
951
952         rv = connection_get_profile_iterator(connection, CONNECTION_ITERATOR_TYPE_CONNECTED, &profile_iter);
953         if (rv != CONNECTION_ERROR_NONE) {
954                 printf("Fail to get profile iterator [%d]\n", rv);
955                 return -1;
956         }
957
958         while (connection_profile_iterator_has_next(profile_iter)) {
959                 if (connection_profile_iterator_next(profile_iter, &profile_h) != CONNECTION_ERROR_NONE) {
960                         printf("Fail to get profile handle\n");
961                         return -1;
962                 }
963
964                 if (connection_profile_get_name(profile_h, &profile_name) != CONNECTION_ERROR_NONE) {
965                         printf("Fail to get profile name\n");
966                         return -1;
967                 }
968                 printf("profile name : %s\n", profile_name);
969                 g_free(profile_name);
970
971                 if (connection_profile_get_type(profile_h, &type) != CONNECTION_ERROR_NONE) {
972                         printf("Fail to get profile type\n");
973                         return -1;
974                 }
975                 printf("profile type is %d\n", type);
976
977                 if (type == CONNECTION_PROFILE_TYPE_CELLULAR) {
978                 if (connection_profile_is_cellular_default(profile_h, &is_default) != CONNECTION_ERROR_NONE) {
979                         printf("Fail to get profile is default\n");
980                         return -1;
981                 }
982                         printf("[%s]\n", is_default ? "default" : "not default");
983                 }
984         }
985
986         return 1;
987 }
988
989 int test_get_current_profile(void)
990 {
991         int rv = 0;
992         char *profile_name = NULL;
993         connection_profile_h profile_h;
994
995         rv = connection_get_current_profile(connection, &profile_h);
996         if (rv != CONNECTION_ERROR_NONE) {
997                 printf("Fail to get profile iterator [%d]\n", rv);
998                 return -1;
999         }
1000
1001         if (connection_profile_get_name(profile_h, &profile_name) != CONNECTION_ERROR_NONE) {
1002                 printf("Fail to get profile name\n");
1003                 return -1;
1004         }
1005         printf("profile name : %s\n", profile_name);
1006         g_free(profile_name);
1007
1008         connection_profile_destroy(profile_h);
1009
1010         return 1;
1011 }
1012
1013 int test_open_profile(void)
1014 {
1015         connection_profile_h profile;
1016
1017         printf("\n** Choose a profile to open. **\n");
1018
1019         if (test_get_user_selected_profile(&profile, true) == false)
1020                 return -1;
1021
1022         if (connection_open_profile(connection, profile, test_connection_opened_callback, NULL) != CONNECTION_ERROR_NONE) {
1023                 printf("Connection open Failed!!\n");
1024                 return -1;
1025         }
1026
1027         return 1;
1028 }
1029
1030 int test_get_default_cellular_service_type(void)
1031 {
1032         int input;
1033         int rv;
1034         int service_type;
1035         connection_profile_h profile;
1036         char *profile_name = NULL;
1037
1038         rv = test_get_user_int("Input profile type to get"
1039                         "(1:Internet, 2:MMS, 3:Prepaid internet, 4:Prepaid MMS, 5:Tethering):", &input);
1040
1041         if (rv == false) {
1042                 printf("Invalid input!!\n");
1043                 return -1;
1044         }
1045
1046         switch (input) {
1047         case 1:
1048                 service_type = CONNECTION_CELLULAR_SERVICE_TYPE_INTERNET;
1049                 break;
1050         case 2:
1051                 service_type = CONNECTION_CELLULAR_SERVICE_TYPE_MMS;
1052                 break;
1053         case 3:
1054                 service_type = CONNECTION_CELLULAR_SERVICE_TYPE_PREPAID_INTERNET;
1055                 break;
1056         case 4:
1057                 service_type = CONNECTION_CELLULAR_SERVICE_TYPE_PREPAID_MMS;
1058                 break;
1059         case 5:
1060                 service_type = CONNECTION_CELLULAR_SERVICE_TYPE_TETHERING;
1061                 break;
1062         default:
1063                 printf("Wrong number!!\n");
1064                 return -1;
1065         }
1066
1067         if (connection_get_default_cellular_service_profile(connection, service_type, &profile) != CONNECTION_ERROR_NONE)
1068                 return -1;
1069
1070         if (connection_profile_get_name(profile, &profile_name) != CONNECTION_ERROR_NONE) {
1071                 printf("Fail to get profile name\n");
1072                 connection_profile_destroy(profile);
1073                 return -1;
1074         }
1075         printf("Default profile name : %s\n", profile_name);
1076         g_free(profile_name);
1077
1078         connection_profile_destroy(profile);
1079
1080         return 1;
1081 }
1082
1083 int test_set_default_cellular_service_type(void)
1084 {
1085         connection_profile_h profile;
1086         connection_cellular_service_type_e type;
1087         int input, rv;
1088
1089         rv = test_get_user_int("Input API type (1:sync, 2:async)", &input);
1090
1091         if (rv == false || (input != 1 && input != 2)) {
1092                 printf("Invalid input!!\n");
1093                 return -1;
1094         }
1095
1096         printf("\n** Choose a profile to set default service(internet or prepaid internet type only). **\n");
1097
1098         if (test_get_user_selected_profile(&profile, true) == false)
1099                 return -1;
1100
1101         if (connection_profile_get_cellular_service_type(profile, &type) != CONNECTION_ERROR_NONE) {
1102                 printf("Fail to get cellular service type\n");
1103                 return -1;
1104         }
1105
1106         if (input == 1) {
1107                 if (connection_set_default_cellular_service_profile(connection, type, profile) != CONNECTION_ERROR_NONE)
1108                         return -1;
1109         } else {
1110                 if (connection_set_default_cellular_service_profile_async(connection,
1111                                 type, profile, test_connection_set_default_callback, NULL) != CONNECTION_ERROR_NONE)
1112                         return -1;
1113         }
1114
1115         return 1;
1116 }
1117
1118 int test_close_profile(void)
1119 {
1120         connection_profile_h profile;
1121
1122         printf("\n** Choose a profile to close. **\n");
1123
1124         if (test_get_user_selected_profile(&profile, true) == false)
1125                 return -1;
1126
1127         if (connection_close_profile(connection, profile, test_connection_closed_callback, NULL) != CONNECTION_ERROR_NONE) {
1128                 printf("Connection close Failed!!\n");
1129                 return -1;
1130         }
1131
1132         return 1;
1133 }
1134
1135 int test_add_profile(void)
1136 {
1137         int rv = 0;
1138         connection_profile_h profile;
1139         char input_str[100] = {0,};
1140
1141         if (test_get_user_string("Input Keyword - (Enter for skip) :", input_str, 100) == false)
1142                 return -1;
1143
1144         rv = connection_profile_create(CONNECTION_PROFILE_TYPE_CELLULAR, input_str, &profile);
1145         if (rv != CONNECTION_ERROR_NONE)
1146                 RETURN_FAIL_DESTROY(profile);
1147
1148         if (test_update_cellular_info(profile) == -1)
1149                 RETURN_FAIL_DESTROY(profile);
1150
1151         rv = connection_add_profile(connection, profile);
1152         if (rv != CONNECTION_ERROR_NONE)
1153                 RETURN_FAIL_DESTROY(profile);
1154
1155         connection_profile_destroy(profile);
1156         return 1;
1157 }
1158
1159 int test_remove_profile(void)
1160 {
1161         connection_profile_h profile;
1162
1163         printf("\n** Choose a profile to remove. **\n");
1164         if (test_get_user_selected_profile(&profile, true) == false)
1165                 return -1;
1166
1167         if (connection_remove_profile(connection, profile) != CONNECTION_ERROR_NONE) {
1168                 printf("Remove profile Failed!!\n");
1169                 return -1;
1170         }
1171
1172         return 1;
1173 }
1174
1175 int test_update_profile(void)
1176 {
1177         int rv = 0;
1178
1179         connection_profile_type_e prof_type;
1180         connection_profile_h profile;
1181
1182         printf("\n** Choose a profile to update. **\n");
1183         if (test_get_user_selected_profile(&profile, true) == false)
1184                 return -1;
1185
1186         if (connection_profile_get_type(profile, &prof_type) != CONNECTION_ERROR_NONE)
1187                 return -1;
1188
1189         switch (prof_type) {
1190         case CONNECTION_PROFILE_TYPE_CELLULAR:
1191                 if (test_update_cellular_info(profile) == -1)
1192                         return -1;
1193
1194                 break;
1195         case CONNECTION_PROFILE_TYPE_WIFI:
1196                 if (test_update_wifi_info(profile) == -1)
1197                         return -1;
1198
1199                 if (test_update_network_info(profile) == -1)
1200                         return -1;
1201
1202                 break;
1203         case CONNECTION_PROFILE_TYPE_ETHERNET:
1204                 if (test_update_network_info(profile) == -1)
1205                         return -1;
1206
1207                 break;
1208         case CONNECTION_PROFILE_TYPE_BT:
1209                 printf("Not supported!\n");
1210                 /* fall through */
1211         default:
1212                 return -1;
1213         }
1214
1215         rv = connection_update_profile(connection, profile);
1216         if (rv != CONNECTION_ERROR_NONE)
1217                 return -1;
1218
1219         return 1;
1220 }
1221
1222 int test_get_profile_info(void)
1223 {
1224         connection_profile_type_e prof_type;
1225         connection_profile_state_e profile_state;
1226         connection_profile_h profile;
1227         char *profile_name = NULL;
1228
1229         printf("\n** Choose a profile to print. **\n");
1230         if (test_get_user_selected_profile(&profile, true) == false)
1231                 return -1;
1232
1233         if (connection_profile_get_name(profile, &profile_name) != CONNECTION_ERROR_NONE) {
1234                 printf("Fail to get profile name\n");
1235                 return -1;
1236         } else {
1237                 printf("Profile Name : %s\n", profile_name);
1238                 g_free(profile_name);
1239         }
1240
1241         if (connection_profile_get_state(profile, &profile_state) != CONNECTION_ERROR_NONE) {
1242                 printf("Fail to get profile state\n");
1243                 return -1;
1244         } else
1245                 printf("Profile State : %s\n", test_print_state(profile_state));
1246
1247
1248         if (connection_profile_get_type(profile, &prof_type) != CONNECTION_ERROR_NONE)
1249                 return -1;
1250
1251         switch (prof_type) {
1252         case CONNECTION_PROFILE_TYPE_CELLULAR:
1253                 printf("Profile Type : Cellular\n");
1254                 test_print_cellular_info(profile);
1255                 break;
1256         case CONNECTION_PROFILE_TYPE_WIFI:
1257                 printf("Profile Type : Wi-Fi\n");
1258                 test_print_wifi_info(profile);
1259                 break;
1260         case CONNECTION_PROFILE_TYPE_ETHERNET:
1261                 printf("Profile Type : Ethernet\n");
1262                 break;
1263         case CONNECTION_PROFILE_TYPE_BT:
1264                 printf("Profile Type : Bluetooth\n");
1265                 break;
1266         default:
1267                 return -1;
1268         }
1269
1270         test_print_network_info(profile);
1271
1272         return 1;
1273 }
1274
1275 int test_refresh_profile_info(void)
1276 {
1277         connection_profile_type_e prof_type;
1278         connection_profile_state_e profile_state;
1279         connection_profile_h profile;
1280         char *profile_name = NULL;
1281
1282         printf("\n** Choose a profile to refresh. **\n");
1283         if (test_get_user_selected_profile(&profile, true) == false)
1284                 return -1;
1285
1286         if (connection_profile_refresh(profile) != CONNECTION_ERROR_NONE)
1287                 return -1;
1288
1289         if (connection_profile_get_name(profile, &profile_name) != CONNECTION_ERROR_NONE) {
1290                 printf("Fail to get profile name\n");
1291                 return -1;
1292         } else {
1293                 printf("Profile Name : %s\n", profile_name);
1294                 g_free(profile_name);
1295         }
1296
1297         if (connection_profile_get_state(profile, &profile_state) != CONNECTION_ERROR_NONE) {
1298                 printf("Fail to get profile state\n");
1299                 return -1;
1300         } else
1301                 printf("Profile State : %s\n", test_print_state(profile_state));
1302
1303
1304         if (connection_profile_get_type(profile, &prof_type) != CONNECTION_ERROR_NONE)
1305                 return -1;
1306
1307         switch (prof_type) {
1308         case CONNECTION_PROFILE_TYPE_CELLULAR:
1309                 printf("Profile Type : Cellular\n");
1310                 test_print_cellular_info(profile);
1311                 break;
1312         case CONNECTION_PROFILE_TYPE_WIFI:
1313                 printf("Profile Type : Wi-Fi\n");
1314                 test_print_wifi_info(profile);
1315                 break;
1316         case CONNECTION_PROFILE_TYPE_ETHERNET:
1317                 printf("Profile Type : Ethernet\n");
1318                 break;
1319         case CONNECTION_PROFILE_TYPE_BT:
1320                 printf("Profile Type : Bluetooth\n");
1321                 break;
1322         default:
1323                 return -1;
1324         }
1325
1326         test_print_network_info(profile);
1327
1328         return 1;
1329 }
1330
1331 int test_set_state_changed_callback()
1332 {
1333         connection_profile_h profile;
1334         connection_profile_h profile_clone;
1335
1336         printf("\n** Choose a profile to set callback. **\n");
1337         if (test_get_user_selected_profile(&profile, true) == false)
1338                 return -1;
1339
1340         if (connection_profile_clone(&profile_clone, profile) != CONNECTION_ERROR_NONE)
1341                 return -1;
1342
1343         if (connection_profile_set_state_changed_cb(profile,
1344                         test_profile_state_callback, profile_clone) != CONNECTION_ERROR_NONE) {
1345                 connection_profile_destroy(profile_clone);
1346                 return -1;
1347         }
1348
1349         state_cb_list = g_slist_append(state_cb_list, profile_clone);
1350
1351         return 1;
1352 }
1353
1354 int test_unset_state_changed_callback()
1355 {
1356         connection_profile_h profile;
1357         GSList *list;
1358         char *profile_name = NULL;
1359         int count = 0;
1360         int input = 0;
1361
1362         printf("\n** Choose a profile to unset callback. **\n");
1363         for (list = state_cb_list; list; list = list->next) {
1364                 profile = list->data;
1365                 if (connection_profile_get_name(profile, &profile_name) != CONNECTION_ERROR_NONE) {
1366                         printf("Fail to get profile name!\n");
1367                         return -1;
1368                 } else {
1369                         printf("%d. %s\n", count, profile_name);
1370                         g_free(profile_name);
1371                 }
1372
1373                 count++;
1374         }
1375
1376         if (test_get_user_int("Input profile number(Enter for cancel) :", &input) == false ||
1377             input >= count ||
1378             input < 0) {
1379                 printf("Wrong number!!\n");
1380                 return -1;
1381         }
1382
1383         count = 0;
1384         for (list = state_cb_list; list; list = list->next) {
1385                 if (count == input) {
1386                         profile = list->data;
1387                         goto unset;
1388                 }
1389
1390                 count++;
1391         }
1392
1393         return -1;
1394
1395 unset:
1396         if (connection_profile_unset_state_changed_cb(profile) != CONNECTION_ERROR_NONE)
1397                 return -1;
1398
1399         state_cb_list = g_slist_remove(state_cb_list, profile);
1400         connection_profile_destroy(profile);
1401
1402         return 1;
1403 }
1404
1405 int test_reset_call_statistics_info(void)
1406 {
1407         int ret = CONNECTION_ERROR_NONE;
1408
1409         ret = connection_reset_statistics(connection, CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_LAST_RECEIVED_DATA);
1410         printf("reset last recv data size [%d]\n", ret);
1411         ret = connection_reset_statistics(connection, CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_LAST_SENT_DATA);
1412         printf("last sent data size [%d]\n", ret);
1413         ret = connection_reset_statistics(connection, CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_TOTAL_RECEIVED_DATA);
1414         printf("total received data size [%d]\n", ret);
1415         ret = connection_reset_statistics(connection, CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_TOTAL_SENT_DATA);
1416         printf("total sent data size [%d]\n", ret);
1417
1418         return 1;
1419 }
1420
1421 int test_reset_wifi_call_statistics_info(void)
1422 {
1423         int ret = CONNECTION_ERROR_NONE;
1424
1425         ret = connection_reset_statistics(connection, CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_LAST_SENT_DATA);
1426         printf("WiFi last sent data size [%d]\n", ret);
1427         ret = connection_reset_statistics(connection, CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_LAST_RECEIVED_DATA);
1428         printf("WiFi last recv data size [%d]\n", ret);
1429         ret = connection_reset_statistics(connection, CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_TOTAL_SENT_DATA);
1430         printf("WiFi total sent data size [%d]\n", ret);
1431         ret = connection_reset_statistics(connection, CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_TOTAL_RECEIVED_DATA);
1432         printf("WiFi total received data size [%d]\n", ret);
1433
1434         return 1;
1435 }
1436
1437 int test_add_route(void)
1438 {
1439         int rv = 0;
1440         char ip_addr[30];
1441         char if_name[40];
1442
1443         if (test_get_user_string("Input IP - (Enter for skip) :", ip_addr, 30) == false)
1444                 return -1;
1445
1446         if (test_get_user_string("Input Interface name - (Enter for skip) :", if_name, 40) == false)
1447                 return -1;
1448
1449         rv = connection_add_route(connection, if_name, ip_addr);
1450         if (rv != CONNECTION_ERROR_NONE) {
1451                 printf("Fail to get add new route [%d]\n", rv);
1452                 return -1;
1453         }
1454
1455         return 1;
1456 }
1457
1458 int test_remove_route(void)
1459 {
1460         int rv = 0;
1461         char ip_addr[30];
1462         char if_name[40];
1463
1464         if (test_get_user_string("Input IP - (Enter for skip) :", ip_addr, 30) == false)
1465                 return -1;
1466
1467         if (test_get_user_string("Input Interface name - (Enter for skip) :", if_name, 40) == false)
1468                 return -1;
1469
1470         rv = connection_remove_route(connection, if_name, ip_addr);
1471         if (rv != CONNECTION_ERROR_NONE) {
1472                 printf("Fail to remove the route [%s]\n", test_print_error(rv));
1473                 return -1;
1474         }
1475
1476         return 1;
1477 }
1478
1479 int test_get_bt_state(void)
1480 {
1481         int rv = 0;
1482         connection_bt_state_e bt_state;
1483
1484         rv = connection_get_bt_state(connection, &bt_state);
1485
1486         if (rv != CONNECTION_ERROR_NONE) {
1487                 printf("Fail to get Bluetooth state [%d]\n", rv);
1488                 return -1;
1489         }
1490
1491         printf("Retval = %d, Bluetooth state [%d]\n", rv, bt_state);
1492
1493         return 1;
1494 }
1495
1496 int test_get_profile_id(void)
1497 {
1498         connection_profile_h profile;
1499         char *profile_id;
1500
1501         printf("\n** Choose a profile to see profile id. **\n");
1502         if (test_get_user_selected_profile(&profile, true) == false)
1503                 return -1;
1504
1505         if (connection_profile_get_id(profile, &profile_id) != CONNECTION_ERROR_NONE) {
1506                 printf("Fail to get profile name\n");
1507                 return -1;
1508         } else {
1509                 printf("Profile id : %s\n", profile_id);
1510                 g_free(profile_id);
1511         }
1512
1513         return 1;
1514 }
1515
1516 int test_get_mac_address(void)
1517 {
1518         int rv = 0, type = 0;
1519         connection_type_e conn_type;
1520         char *mac_addr = NULL;
1521
1522         test_get_user_int("Input connection type (1:wifi, 2:ethernet)", &type);
1523
1524         switch (type) {
1525         case 1:
1526                 conn_type = CONNECTION_TYPE_WIFI;
1527                 break;
1528         case 2:
1529                 conn_type = CONNECTION_TYPE_ETHERNET;
1530                 break;
1531         default:
1532                 printf("Wrong number!!\n");
1533                 return -1;
1534         }
1535
1536         rv = connection_get_mac_address(connection, conn_type, &mac_addr);
1537
1538         if (rv != CONNECTION_ERROR_NONE) {
1539                 printf("Fail to get MAC address [%s]\n", test_print_error(rv));
1540                 return -1;
1541         }
1542
1543         printf("mac address is %s\n", mac_addr);
1544
1545         g_free(mac_addr);
1546
1547         return 1;
1548 }
1549
1550 int test_get_ethernet_cable_state(void)
1551 {
1552         int rv = 0;
1553         connection_ethernet_cable_state_e cable_state;
1554
1555         rv = connection_get_ethernet_cable_state(connection, &cable_state);
1556
1557         if (rv != CONNECTION_ERROR_NONE) {
1558                 printf("Fail to get ethernet cable state [%s]\n", test_print_error(rv));
1559                 return -1;
1560         }
1561
1562         printf("Retval = [%s], Ethernet cable state [%d]\n", test_print_error(rv), cable_state);
1563
1564         return 1;
1565 }
1566
1567 int test_reset_profile(void)
1568 {
1569         int type, sim_id, rv;
1570
1571         rv = test_get_user_int("Input reset type (0:default profile reset, 1:delete profile reset)", &type);
1572
1573         if (rv == false || (type != 0 && type != 1)) {
1574                 printf("Invalid input!!\n");
1575                 return -1;
1576         }
1577
1578         rv = test_get_user_int("Input SIM id to reset (0:SIM1, 1:SIM2)", &sim_id);
1579
1580         if (rv == false || (sim_id != 0 && sim_id != 1)) {
1581                 printf("Invalid input!!\n");
1582                 return -1;
1583         }
1584
1585         if (connection_reset_profile(connection, type, sim_id, test_connection_reset_profile_callback, NULL) != CONNECTION_ERROR_NONE) {
1586                 return -1;
1587         }
1588
1589         return 1;
1590 }
1591
1592 int main(int argc, char **argv)
1593 {
1594         GMainLoop *mainloop;
1595         mainloop = g_main_loop_new (NULL, FALSE);
1596
1597         GIOChannel *channel = g_io_channel_unix_new(0);
1598         g_io_add_watch(channel, (G_IO_IN|G_IO_ERR|G_IO_HUP|G_IO_NVAL), test_thread,NULL );
1599
1600         printf("Test Thread created...\n");
1601
1602         g_main_loop_run (mainloop);
1603
1604         return 0;
1605 }
1606
1607 gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data)
1608 {
1609         int rv = 0;
1610         char a[100];
1611         
1612         memset(a, '\0', 100);
1613         printf("Event received from stdin\n");
1614         
1615         rv = read(0, a, 100);
1616         
1617         if (rv < 0 || a[0] == '0') {
1618                 if (connection != NULL)
1619                         test_deregister_client();
1620
1621                 exit(1);
1622         }
1623
1624         if (*a == '\n' || *a == '\r'){
1625                 printf("\n\n Network Connection API Test App\n\n");
1626                 printf("Options..\n");
1627                 printf("1       - Create Handle and set callbacks\n");
1628                 printf("2       - Destroy Handle(unset callbacks automatically)\n");
1629                 printf("3       - Get network state\n");
1630                 printf("4       - Get cellular state (please insert SIM Card)\n");
1631                 printf("5       - Get wifi state (please turn on WiFi)\n");
1632                 printf("6       - Get current proxy address \n");
1633                 printf("7       - Get current Ip address\n");
1634                 printf("8       - Get cellular data call statistics\n");
1635                 printf("9       - Get WiFi data call statistics\n");
1636                 printf("a       - Get Profile list\n");
1637                 printf("b       - Get Connected Profile list\n");
1638                 printf("c       - Get Current profile\n");
1639                 printf("d       - Open connection with profile\n");
1640                 printf("e       - Get default cellular service by type\n");
1641                 printf("f       - Set default cellular service by type\n");
1642                 printf("g       - Close connection with profile\n");
1643                 printf("h       - Add profile(Cellular only)\n");
1644                 printf("i       - Remove profile(Cellular:delete, WiFi:forgot)\n");
1645                 printf("j       - Update profile\n");
1646                 printf("k       - Get profile info\n");
1647                 printf("l       - Refresh profile info\n");
1648                 printf("m       - Set state changed callback\n");
1649                 printf("n       - Unset state changed callback\n");
1650                 printf("o       - Reset cellular data call statistics\n");
1651                 printf("p       - Reset WiFi data call statistics\n");
1652                 printf("q       - Add new route\n");
1653                 printf("r       - Remove a route\n");
1654                 printf("s       - Get Bluetooth state\n");
1655                 printf("t       - Get profile id\n");
1656                 printf("u       - Reset profile\n");
1657                 printf("v       - Get all cellular default profiles\n");
1658                 printf("w       - Get mac address\n");
1659                 printf("x       - Get ethernet cable state\n");
1660                 printf("0       - Exit \n");
1661                 printf("ENTER  - Show options menu.......\n");
1662         }
1663
1664         switch (a[0]) {
1665         case '1':
1666                 rv = test_register_client();
1667                 break;
1668         case '2':
1669                 rv = test_deregister_client();
1670                 break;
1671         case '3':
1672                 rv = test_get_network_state();
1673                 break;
1674         case '4':
1675                 rv = test_get_cellular_state();
1676                 break;
1677         case '5':
1678                 rv = test_get_wifi_state();
1679                 break;
1680         case '6':
1681                 rv = test_get_current_proxy();
1682                 break;
1683         case '7':
1684                 rv = test_get_current_ip();
1685                 break;
1686         case '8':
1687                 rv = test_get_call_statistics_info();
1688                 break;
1689         case '9':
1690                 rv = test_get_wifi_call_statistics_info();
1691                 break;
1692         case 'a':
1693                 rv = test_get_profile_list();
1694                 break;
1695         case 'b':
1696                 rv = test_get_connected_profile_list();
1697                 break;
1698         case 'c':
1699                 rv = test_get_current_profile();
1700                 break;
1701         case 'd':
1702                 rv = test_open_profile();
1703                 break;
1704         case 'e':
1705                 rv = test_get_default_cellular_service_type();
1706                 break;
1707         case 'f':
1708                 rv = test_set_default_cellular_service_type();
1709                 break;
1710         case 'g':
1711                 rv = test_close_profile();
1712                 break;
1713         case 'h':
1714                 rv = test_add_profile();
1715                 break;
1716         case 'i':
1717                 rv = test_remove_profile();
1718                 break;
1719         case 'j':
1720                 rv = test_update_profile();
1721                 break;
1722         case 'k':
1723                 rv = test_get_profile_info();
1724                 break;
1725         case 'l':
1726                 rv = test_refresh_profile_info();
1727                 break;
1728         case 'm':
1729                 rv = test_set_state_changed_callback();
1730                 break;
1731         case 'n':
1732                 rv = test_unset_state_changed_callback();
1733                 break;
1734         case 'o':
1735                 rv = test_reset_call_statistics_info();
1736                 break;
1737         case 'p':
1738                 rv = test_reset_wifi_call_statistics_info();
1739                 break;
1740         case 'q':
1741                 rv = test_add_route();
1742                 break;
1743         case 'r':
1744                 rv = test_remove_route();
1745                 break;
1746         case 's':
1747                 rv = test_get_bt_state();
1748                 break;
1749         case 't':
1750                 rv = test_get_profile_id();
1751                 break;
1752         case 'u':
1753                 rv = test_reset_profile();
1754                 break;
1755         case 'v':
1756                 rv = test_get_default_profile_list();
1757                 break;
1758         case 'w':
1759                 rv = test_get_mac_address();
1760                 break;
1761         case 'x':
1762                 rv = test_get_ethernet_cable_state();
1763                 break;
1764         }
1765
1766         if (rv == 1)
1767                 printf("Operation succeeded!\n");
1768         else
1769                 printf("Operation failed!\n");
1770
1771         return TRUE;
1772 }