c50f098ce41b7f970bc1f874d189d7953f2e2f80
[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         case CONNECTION_PROFILE_TYPE_BT:
1205                 printf("Not supported!\n");
1206                 /* fall through */
1207         default:
1208                 return -1;
1209         }
1210
1211         rv = connection_update_profile(connection, profile);
1212         if (rv != CONNECTION_ERROR_NONE)
1213                 return -1;
1214
1215         return 1;
1216 }
1217
1218 int test_get_profile_info(void)
1219 {
1220         connection_profile_type_e prof_type;
1221         connection_profile_state_e profile_state;
1222         connection_profile_h profile;
1223         char *profile_name = NULL;
1224
1225         printf("\n** Choose a profile to print. **\n");
1226         if (test_get_user_selected_profile(&profile, true) == false)
1227                 return -1;
1228
1229         if (connection_profile_get_name(profile, &profile_name) != CONNECTION_ERROR_NONE) {
1230                 printf("Fail to get profile name\n");
1231                 return -1;
1232         } else {
1233                 printf("Profile Name : %s\n", profile_name);
1234                 g_free(profile_name);
1235         }
1236
1237         if (connection_profile_get_state(profile, &profile_state) != CONNECTION_ERROR_NONE) {
1238                 printf("Fail to get profile state\n");
1239                 return -1;
1240         } else
1241                 printf("Profile State : %s\n", test_print_state(profile_state));
1242
1243
1244         if (connection_profile_get_type(profile, &prof_type) != CONNECTION_ERROR_NONE)
1245                 return -1;
1246
1247         switch (prof_type) {
1248         case CONNECTION_PROFILE_TYPE_CELLULAR:
1249                 printf("Profile Type : Cellular\n");
1250                 test_print_cellular_info(profile);
1251                 break;
1252         case CONNECTION_PROFILE_TYPE_WIFI:
1253                 printf("Profile Type : Wi-Fi\n");
1254                 test_print_wifi_info(profile);
1255                 break;
1256         case CONNECTION_PROFILE_TYPE_ETHERNET:
1257                 printf("Profile Type : Ethernet\n");
1258                 break;
1259         case CONNECTION_PROFILE_TYPE_BT:
1260                 printf("Profile Type : Bluetooth\n");
1261                 break;
1262         default:
1263                 return -1;
1264         }
1265
1266         test_print_network_info(profile);
1267
1268         return 1;
1269 }
1270
1271 int test_refresh_profile_info(void)
1272 {
1273         connection_profile_type_e prof_type;
1274         connection_profile_state_e profile_state;
1275         connection_profile_h profile;
1276         char *profile_name = NULL;
1277
1278         printf("\n** Choose a profile to refresh. **\n");
1279         if (test_get_user_selected_profile(&profile, true) == false)
1280                 return -1;
1281
1282         if (connection_profile_refresh(profile) != CONNECTION_ERROR_NONE)
1283                 return -1;
1284
1285         if (connection_profile_get_name(profile, &profile_name) != CONNECTION_ERROR_NONE) {
1286                 printf("Fail to get profile name\n");
1287                 return -1;
1288         } else {
1289                 printf("Profile Name : %s\n", profile_name);
1290                 g_free(profile_name);
1291         }
1292
1293         if (connection_profile_get_state(profile, &profile_state) != CONNECTION_ERROR_NONE) {
1294                 printf("Fail to get profile state\n");
1295                 return -1;
1296         } else
1297                 printf("Profile State : %s\n", test_print_state(profile_state));
1298
1299
1300         if (connection_profile_get_type(profile, &prof_type) != CONNECTION_ERROR_NONE)
1301                 return -1;
1302
1303         switch (prof_type) {
1304         case CONNECTION_PROFILE_TYPE_CELLULAR:
1305                 printf("Profile Type : Cellular\n");
1306                 test_print_cellular_info(profile);
1307                 break;
1308         case CONNECTION_PROFILE_TYPE_WIFI:
1309                 printf("Profile Type : Wi-Fi\n");
1310                 test_print_wifi_info(profile);
1311                 break;
1312         case CONNECTION_PROFILE_TYPE_ETHERNET:
1313                 printf("Profile Type : Ethernet\n");
1314                 break;
1315         case CONNECTION_PROFILE_TYPE_BT:
1316                 printf("Profile Type : Bluetooth\n");
1317                 break;
1318         default:
1319                 return -1;
1320         }
1321
1322         test_print_network_info(profile);
1323
1324         return 1;
1325 }
1326
1327 int test_set_state_changed_callback()
1328 {
1329         connection_profile_h profile;
1330         connection_profile_h profile_clone;
1331
1332         printf("\n** Choose a profile to set callback. **\n");
1333         if (test_get_user_selected_profile(&profile, true) == false)
1334                 return -1;
1335
1336         if (connection_profile_clone(&profile_clone, profile) != CONNECTION_ERROR_NONE)
1337                 return -1;
1338
1339         if (connection_profile_set_state_changed_cb(profile,
1340                         test_profile_state_callback, profile_clone) != CONNECTION_ERROR_NONE) {
1341                 connection_profile_destroy(profile_clone);
1342                 return -1;
1343         }
1344
1345         state_cb_list = g_slist_append(state_cb_list, profile_clone);
1346
1347         return 1;
1348 }
1349
1350 int test_unset_state_changed_callback()
1351 {
1352         connection_profile_h profile;
1353         GSList *list;
1354         char *profile_name = NULL;
1355         int count = 0;
1356         int input = 0;
1357
1358         printf("\n** Choose a profile to unset callback. **\n");
1359         for (list = state_cb_list; list; list = list->next) {
1360                 profile = list->data;
1361                 if (connection_profile_get_name(profile, &profile_name) != CONNECTION_ERROR_NONE) {
1362                         printf("Fail to get profile name!\n");
1363                         return -1;
1364                 } else {
1365                         printf("%d. %s\n", count, profile_name);
1366                         g_free(profile_name);
1367                 }
1368
1369                 count++;
1370         }
1371
1372         if (test_get_user_int("Input profile number(Enter for cancel) :", &input) == false ||
1373             input >= count ||
1374             input < 0) {
1375                 printf("Wrong number!!\n");
1376                 return -1;
1377         }
1378
1379         count = 0;
1380         for (list = state_cb_list; list; list = list->next) {
1381                 if (count == input) {
1382                         profile = list->data;
1383                         goto unset;
1384                 }
1385
1386                 count++;
1387         }
1388
1389         return -1;
1390
1391 unset:
1392         if (connection_profile_unset_state_changed_cb(profile) != CONNECTION_ERROR_NONE)
1393                 return -1;
1394
1395         state_cb_list = g_slist_remove(state_cb_list, profile);
1396         connection_profile_destroy(profile);
1397
1398         return 1;
1399 }
1400
1401 int test_reset_call_statistics_info(void)
1402 {
1403         int ret = CONNECTION_ERROR_NONE;
1404
1405         ret = connection_reset_statistics(connection, CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_LAST_RECEIVED_DATA);
1406         printf("reset last recv data size [%d]\n", ret);
1407         ret = connection_reset_statistics(connection, CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_LAST_SENT_DATA);
1408         printf("last sent data size [%d]\n", ret);
1409         ret = connection_reset_statistics(connection, CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_TOTAL_RECEIVED_DATA);
1410         printf("total received data size [%d]\n", ret);
1411         ret = connection_reset_statistics(connection, CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_TOTAL_SENT_DATA);
1412         printf("total sent data size [%d]\n", ret);
1413
1414         return 1;
1415 }
1416
1417 int test_reset_wifi_call_statistics_info(void)
1418 {
1419         int ret = CONNECTION_ERROR_NONE;
1420
1421         ret = connection_reset_statistics(connection, CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_LAST_SENT_DATA);
1422         printf("WiFi last sent data size [%d]\n", ret);
1423         ret = connection_reset_statistics(connection, CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_LAST_RECEIVED_DATA);
1424         printf("WiFi last recv data size [%d]\n", ret);
1425         ret = connection_reset_statistics(connection, CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_TOTAL_SENT_DATA);
1426         printf("WiFi total sent data size [%d]\n", ret);
1427         ret = connection_reset_statistics(connection, CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_TOTAL_RECEIVED_DATA);
1428         printf("WiFi total received data size [%d]\n", ret);
1429
1430         return 1;
1431 }
1432
1433 int test_add_route(void)
1434 {
1435         int rv = 0;
1436         char ip_addr[30];
1437         char if_name[40];
1438
1439         if (test_get_user_string("Input IP - (Enter for skip) :", ip_addr, 30) == false)
1440                 return -1;
1441
1442         if (test_get_user_string("Input Interface name - (Enter for skip) :", if_name, 40) == false)
1443                 return -1;
1444
1445         rv = connection_add_route(connection, if_name, ip_addr);
1446         if (rv != CONNECTION_ERROR_NONE) {
1447                 printf("Fail to get add new route [%d]\n", rv);
1448                 return -1;
1449         }
1450
1451         return 1;
1452 }
1453
1454 int test_remove_route(void)
1455 {
1456         int rv = 0;
1457         char ip_addr[30];
1458         char if_name[40];
1459
1460         if (test_get_user_string("Input IP - (Enter for skip) :", ip_addr, 30) == false)
1461                 return -1;
1462
1463         if (test_get_user_string("Input Interface name - (Enter for skip) :", if_name, 40) == false)
1464                 return -1;
1465
1466         rv = connection_remove_route(connection, if_name, ip_addr);
1467         if (rv != CONNECTION_ERROR_NONE) {
1468                 printf("Fail to remove the route [%s]\n", test_print_error(rv));
1469                 return -1;
1470         }
1471
1472         return 1;
1473 }
1474
1475 int test_get_bt_state(void)
1476 {
1477         int rv = 0;
1478         connection_bt_state_e bt_state;
1479
1480         rv = connection_get_bt_state(connection, &bt_state);
1481
1482         if (rv != CONNECTION_ERROR_NONE) {
1483                 printf("Fail to get Bluetooth state [%d]\n", rv);
1484                 return -1;
1485         }
1486
1487         printf("Retval = %d, Bluetooth state [%d]\n", rv, bt_state);
1488
1489         return 1;
1490 }
1491
1492 int test_get_profile_id(void)
1493 {
1494         connection_profile_h profile;
1495         char *profile_id;
1496
1497         printf("\n** Choose a profile to see profile id. **\n");
1498         if (test_get_user_selected_profile(&profile, true) == false)
1499                 return -1;
1500
1501         if (connection_profile_get_id(profile, &profile_id) != CONNECTION_ERROR_NONE) {
1502                 printf("Fail to get profile name\n");
1503                 return -1;
1504         } else {
1505                 printf("Profile id : %s\n", profile_id);
1506                 g_free(profile_id);
1507         }
1508
1509         return 1;
1510 }
1511
1512 int test_get_mac_address(void)
1513 {
1514         int rv = 0, type = 0;
1515         connection_type_e conn_type;
1516         char *mac_addr = NULL;
1517
1518         test_get_user_int("Input connection type (1:wifi, 2:ethernet)", &type);
1519
1520         switch (type) {
1521         case 1:
1522                 conn_type = CONNECTION_TYPE_WIFI;
1523                 break;
1524         case 2:
1525                 conn_type = CONNECTION_TYPE_ETHERNET;
1526                 break;
1527         default:
1528                 printf("Wrong number!!\n");
1529                 return -1;
1530         }
1531
1532         rv = connection_get_mac_address(connection, conn_type, &mac_addr);
1533
1534         if (rv != CONNECTION_ERROR_NONE) {
1535                 printf("Fail to get MAC address [%s]\n", test_print_error(rv));
1536                 return -1;
1537         }
1538
1539         printf("mac address is %s\n", mac_addr);
1540
1541         g_free(mac_addr);
1542
1543         return 1;
1544 }
1545
1546 int test_get_ethernet_cable_state(void)
1547 {
1548         int rv = 0;
1549         connection_ethernet_cable_state_e cable_state;
1550
1551         rv = connection_get_ethernet_cable_state(connection, &cable_state);
1552
1553         if (rv != CONNECTION_ERROR_NONE) {
1554                 printf("Fail to get ethernet cable state [%s]\n", test_print_error(rv));
1555                 return -1;
1556         }
1557
1558         printf("Retval = [%s], Ethernet cable state [%d]\n", test_print_error(rv), cable_state);
1559
1560         return 1;
1561 }
1562
1563 int test_reset_profile(void)
1564 {
1565         int type, sim_id, rv;
1566
1567         rv = test_get_user_int("Input reset type (0:default profile reset, 1:delete profile reset)", &type);
1568
1569         if (rv == false || (type != 0 && type != 1)) {
1570                 printf("Invalid input!!\n");
1571                 return -1;
1572         }
1573
1574         rv = test_get_user_int("Input SIM id to reset (0:SIM1, 1:SIM2)", &sim_id);
1575
1576         if (rv == false || (sim_id != 0 && sim_id != 1)) {
1577                 printf("Invalid input!!\n");
1578                 return -1;
1579         }
1580
1581         if (connection_reset_profile(connection, type, sim_id, test_connection_reset_profile_callback, NULL) != CONNECTION_ERROR_NONE) {
1582                 return -1;
1583         }
1584
1585         return 1;
1586 }
1587
1588 int main(int argc, char **argv)
1589 {
1590         GMainLoop *mainloop;
1591         mainloop = g_main_loop_new (NULL, FALSE);
1592
1593         GIOChannel *channel = g_io_channel_unix_new(0);
1594         g_io_add_watch(channel, (G_IO_IN|G_IO_ERR|G_IO_HUP|G_IO_NVAL), test_thread,NULL );
1595
1596         printf("Test Thread created...\n");
1597
1598         g_main_loop_run (mainloop);
1599
1600         return 0;
1601 }
1602
1603 gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data)
1604 {
1605         int rv = 0;
1606         char a[100];
1607         
1608         memset(a, '\0', 100);
1609         printf("Event received from stdin\n");
1610         
1611         rv = read(0, a, 100);
1612         
1613         if (rv < 0 || a[0] == '0') {
1614                 if (connection != NULL)
1615                         test_deregister_client();
1616
1617                 exit(1);
1618         }
1619
1620         if (*a == '\n' || *a == '\r'){
1621                 printf("\n\n Network Connection API Test App\n\n");
1622                 printf("Options..\n");
1623                 printf("1       - Create Handle and set callbacks\n");
1624                 printf("2       - Destroy Handle(unset callbacks automatically)\n");
1625                 printf("3       - Get network state\n");
1626                 printf("4       - Get cellular state (please insert SIM Card)\n");
1627                 printf("5       - Get wifi state (please turn on WiFi)\n");
1628                 printf("6       - Get current proxy address \n");
1629                 printf("7       - Get current Ip address\n");
1630                 printf("8       - Get cellular data call statistics\n");
1631                 printf("9       - Get WiFi data call statistics\n");
1632                 printf("a       - Get Profile list\n");
1633                 printf("b       - Get Connected Profile list\n");
1634                 printf("c       - Get Current profile\n");
1635                 printf("d       - Open connection with profile\n");
1636                 printf("e       - Get default cellular service by type\n");
1637                 printf("f       - Set default cellular service by type\n");
1638                 printf("g       - Close connection with profile\n");
1639                 printf("h       - Add profile(Cellular only)\n");
1640                 printf("i       - Remove profile(Cellular:delete, WiFi:forgot)\n");
1641                 printf("j       - Update profile\n");
1642                 printf("k       - Get profile info\n");
1643                 printf("l       - Refresh profile info\n");
1644                 printf("m       - Set state changed callback\n");
1645                 printf("n       - Unset state changed callback\n");
1646                 printf("o       - Reset cellular data call statistics\n");
1647                 printf("p       - Reset WiFi data call statistics\n");
1648                 printf("q       - Add new route\n");
1649                 printf("r       - Remove a route\n");
1650                 printf("s       - Get Bluetooth state\n");
1651                 printf("t       - Get profile id\n");
1652                 printf("u       - Reset profile\n");
1653                 printf("v       - Get all cellular default profiles\n");
1654                 printf("w       - Get mac address\n");
1655                 printf("x       - Get ethernet cable state\n");
1656                 printf("0       - Exit \n");
1657                 printf("ENTER  - Show options menu.......\n");
1658         }
1659
1660         switch (a[0]) {
1661         case '1':
1662                 rv = test_register_client();
1663                 break;
1664         case '2':
1665                 rv = test_deregister_client();
1666                 break;
1667         case '3':
1668                 rv = test_get_network_state();
1669                 break;
1670         case '4':
1671                 rv = test_get_cellular_state();
1672                 break;
1673         case '5':
1674                 rv = test_get_wifi_state();
1675                 break;
1676         case '6':
1677                 rv = test_get_current_proxy();
1678                 break;
1679         case '7':
1680                 rv = test_get_current_ip();
1681                 break;
1682         case '8':
1683                 rv = test_get_call_statistics_info();
1684                 break;
1685         case '9':
1686                 rv = test_get_wifi_call_statistics_info();
1687                 break;
1688         case 'a':
1689                 rv = test_get_profile_list();
1690                 break;
1691         case 'b':
1692                 rv = test_get_connected_profile_list();
1693                 break;
1694         case 'c':
1695                 rv = test_get_current_profile();
1696                 break;
1697         case 'd':
1698                 rv = test_open_profile();
1699                 break;
1700         case 'e':
1701                 rv = test_get_default_cellular_service_type();
1702                 break;
1703         case 'f':
1704                 rv = test_set_default_cellular_service_type();
1705                 break;
1706         case 'g':
1707                 rv = test_close_profile();
1708                 break;
1709         case 'h':
1710                 rv = test_add_profile();
1711                 break;
1712         case 'i':
1713                 rv = test_remove_profile();
1714                 break;
1715         case 'j':
1716                 rv = test_update_profile();
1717                 break;
1718         case 'k':
1719                 rv = test_get_profile_info();
1720                 break;
1721         case 'l':
1722                 rv = test_refresh_profile_info();
1723                 break;
1724         case 'm':
1725                 rv = test_set_state_changed_callback();
1726                 break;
1727         case 'n':
1728                 rv = test_unset_state_changed_callback();
1729                 break;
1730         case 'o':
1731                 rv = test_reset_call_statistics_info();
1732                 break;
1733         case 'p':
1734                 rv = test_reset_wifi_call_statistics_info();
1735                 break;
1736         case 'q':
1737                 rv = test_add_route();
1738                 break;
1739         case 'r':
1740                 rv = test_remove_route();
1741                 break;
1742         case 's':
1743                 rv = test_get_bt_state();
1744                 break;
1745         case 't':
1746                 rv = test_get_profile_id();
1747                 break;
1748         case 'u':
1749                 rv = test_reset_profile();
1750                 break;
1751         case 'v':
1752                 rv = test_get_default_profile_list();
1753                 break;
1754         case 'w':
1755                 rv = test_get_mac_address();
1756                 break;
1757         case 'x':
1758                 rv = test_get_ethernet_cable_state();
1759                 break;
1760         }
1761
1762         if (rv == 1)
1763                 printf("Operation succeeded!\n");
1764         else
1765                 printf("Operation failed!\n");
1766
1767         return TRUE;
1768 }