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