Add new API to check whether metered or non metered
[platform/core/api/connection.git] / test / connection_test.c
1 /*
2  * Copyright (c) 2011-2013 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdio.h>
18 #include <errno.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22 #include <glib.h>
23
24 #include "net_connection.h"
25
26 #include <tizen_error.h>
27
28 #define LOG_RED "\033[0;31m"
29 #define LOG_GREEN "\033[0;32m"
30 #define LOG_BROWN "\033[0;33m"
31 #define LOG_BLUE "\033[0;34m"
32 #define LOG_END "\033[0;m"
33
34 #define RETURN_FAIL_DESTROY(x) {connection_profile_destroy(x); return -1; }
35
36 gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data);
37
38 connection_h connection = NULL;
39 static GSList *state_cb_list = NULL;
40
41
42 static bool test_get_user_string(const char *msg, char *buf, int buf_size)
43 {
44         if (msg == NULL || buf == NULL || buf_size < 2)
45                 return false;
46
47         int rv;
48         printf("%s\n", msg);
49         memset(buf, 0, buf_size);
50         rv = read(0, buf, buf_size - 1);
51
52         if (rv < 0 || buf[0] == '\0' || buf[0] == '\n' || buf[0] == '\r') {
53                 buf[0] = '\0';
54                 return false;
55         }
56
57         buf[rv-1] = '\0';
58         return true;
59 }
60
61 static bool test_get_user_int(const char *msg, int *num)
62 {
63         if (msg == NULL || num == NULL)
64                 return false;
65
66         int rv;
67         char buf[32] = {0,};
68         printf("%s\n", msg);
69         rv = read(0, buf, 32);
70
71         if (rv < 0 || *buf == 0 || *buf == '\n' || *buf == '\r')
72                 return false;
73
74         *num = atoi(buf);
75         return true;
76 }
77
78 static const char *test_print_state(connection_profile_state_e state)
79 {
80         switch (state) {
81         case CONNECTION_PROFILE_STATE_DISCONNECTED:
82                 return "Disconnected";
83         case CONNECTION_PROFILE_STATE_ASSOCIATION:
84                 return "Association";
85         case CONNECTION_PROFILE_STATE_CONFIGURATION:
86                 return "Configuration";
87         case CONNECTION_PROFILE_STATE_CONNECTED:
88                 return "Connected";
89         default:
90                 return "Unknown";
91         }
92 }
93
94 static const char *test_print_connection_type(connection_type_e type)
95 {
96         switch (type) {
97         case CONNECTION_TYPE_DISCONNECTED:
98                 return "Disconnected";
99         case CONNECTION_TYPE_WIFI:
100                 return "Wifi";
101         case CONNECTION_TYPE_CELLULAR:
102                 return "Cellular";
103         case CONNECTION_TYPE_ETHERNET:
104                 return "Ethernet";
105         case CONNECTION_TYPE_BT:
106                 return "BT";
107         case CONNECTION_TYPE_NET_PROXY:
108                 return "Net_Proxy";
109         default:
110                 return "Unknown";
111         }
112 }
113
114 static const char *test_print_cellular_state(connection_cellular_state_e state)
115 {
116         switch (state) {
117         case CONNECTION_CELLULAR_STATE_OUT_OF_SERVICE:
118                 return "Out of service";
119         case CONNECTION_CELLULAR_STATE_FLIGHT_MODE:
120                 return "Flight mode";
121         case CONNECTION_CELLULAR_STATE_ROAMING_OFF:
122                 return "Roaming off";
123         case CONNECTION_CELLULAR_STATE_CALL_ONLY_AVAILABLE:
124                 return "Call only available";
125         case CONNECTION_CELLULAR_STATE_AVAILABLE:
126                 return "Available";
127         case CONNECTION_CELLULAR_STATE_CONNECTED:
128                 return "Connected";
129         default:
130                 return "Unknown";
131         }
132 }
133
134 static const char *test_print_wifi_state(connection_wifi_state_e state)
135 {
136         switch (state) {
137         case CONNECTION_WIFI_STATE_DEACTIVATED:
138                 return "Deactivated";
139         case CONNECTION_WIFI_STATE_DISCONNECTED:
140                 return "Disconnected";
141         case CONNECTION_WIFI_STATE_CONNECTED:
142                 return "Connected";
143         default:
144                 return "Unknown";
145         }
146 }
147
148 static const char *test_print_cellular_service_type(connection_cellular_service_type_e type)
149 {
150         switch (type) {
151         case CONNECTION_CELLULAR_SERVICE_TYPE_UNKNOWN:
152                 return "Unknown";
153         case CONNECTION_CELLULAR_SERVICE_TYPE_INTERNET:
154                 return "Internet";
155         case CONNECTION_CELLULAR_SERVICE_TYPE_MMS:
156                 return "MMS";
157         case CONNECTION_CELLULAR_SERVICE_TYPE_PREPAID_INTERNET:
158                 return "Prepaid internet";
159         case CONNECTION_CELLULAR_SERVICE_TYPE_PREPAID_MMS:
160                 return "Prepaid MMS";
161         case CONNECTION_CELLULAR_SERVICE_TYPE_TETHERING:
162                 return "Tethering";
163         case CONNECTION_CELLULAR_SERVICE_TYPE_APPLICATION:
164                 return "Application";
165         default:
166                 return "Unknown";
167         }
168 }
169
170 static const char* test_print_cellular_auth_type(connection_cellular_auth_type_e type)
171 {
172         switch (type) {
173         case CONNECTION_CELLULAR_AUTH_TYPE_PAP:
174                 return "PAP";
175         case CONNECTION_CELLULAR_AUTH_TYPE_CHAP:
176                 return "CHAP";
177         case CONNECTION_CELLULAR_AUTH_TYPE_NONE:
178         default:
179                 return "None";
180         }
181 }
182
183 static const char* test_print_cellular_pdn_type(connection_cellular_pdn_type_e type)
184 {
185         switch (type) {
186         case CONNECTION_CELLULAR_PDN_TYPE_IPV4:
187                 return "IPv4";
188         case CONNECTION_CELLULAR_PDN_TYPE_IPV6:
189                 return "IPv6";
190         case CONNECTION_CELLULAR_PDN_TYPE_IPV4_IPv6:
191                 return "Dual";
192         case CONNECTION_CELLULAR_PDN_TYPE_UNKNOWN:
193         default:
194                 return "Unknown";
195         }
196 }
197
198 static const char *test_print_error(connection_error_e error)
199 {
200         switch (error) {
201         case CONNECTION_ERROR_NONE:
202                 return "CONNECTION_ERROR_NONE";
203         case CONNECTION_ERROR_INVALID_PARAMETER:
204                 return "CONNECTION_ERROR_INVALID_PARAMETER";
205         case CONNECTION_ERROR_OUT_OF_MEMORY:
206                 return "CONNECTION_ERROR_OUT_OF_MEMORY";
207         case CONNECTION_ERROR_INVALID_OPERATION:
208                 return "CONNECTION_ERROR_INVALID_OPERATION";
209         case CONNECTION_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED:
210                 return "CONNECTION_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED";
211         case CONNECTION_ERROR_OPERATION_FAILED:
212                 return "CONNECTION_ERROR_OPERATION_FAILED";
213         case CONNECTION_ERROR_ITERATOR_END:
214                 return "CONNECTION_ERROR_ITERATOR_END";
215         case CONNECTION_ERROR_NO_CONNECTION:
216                 return "CONNECTION_ERROR_NO_CONNECTION";
217         case CONNECTION_ERROR_NOW_IN_PROGRESS:
218                 return "CONNECTION_ERROR_NOW_IN_PROGRESS";
219         case CONNECTION_ERROR_ALREADY_EXISTS:
220                 return "CONNECTION_ERROR_ALREADY_EXISTS";
221         case CONNECTION_ERROR_OPERATION_ABORTED:
222                 return "CONNECTION_ERROR_OPERATION_ABORTED";
223         case CONNECTION_ERROR_DHCP_FAILED:
224                 return "CONNECTION_ERROR_DHCP_FAILED";
225         case CONNECTION_ERROR_INVALID_KEY:
226                 return "CONNECTION_ERROR_INVALID_KEY";
227         case CONNECTION_ERROR_NO_REPLY:
228                 return "CONNECTION_ERROR_NO_REPLY";
229         case CONNECTION_ERROR_PERMISSION_DENIED:
230                 return "CONNECTION_ERROR_PERMISSION_DENIED";
231         case CONNECTION_ERROR_NOT_SUPPORTED:
232                 return "CONNECTION_ERROR_NOT_SUPPORTED";
233         default:
234                 return "CONNECTION_ERROR_UNKNOWN";
235         }
236 }
237
238 static void test_type_changed_callback(connection_type_e type, void* user_data)
239 {
240         printf("Type changed callback, connection type : %d\n", type);
241 }
242
243 static void test_ip_changed_callback(const char* ipv4_address, const char* ipv6_address, void* user_data)
244 {
245         printf("IP changed callback, IPv4 address : %s, IPv6 address : %s\n",
246                         ipv4_address, (ipv6_address ? ipv6_address : "NULL"));
247 }
248
249 static void test_proxy_changed_callback(const char* ipv4_address, const char* ipv6_address, void* user_data)
250 {
251         printf("Proxy changed callback, IPv4 address : %s, IPv6 address : %s\n",
252                         ipv4_address, (ipv6_address ? ipv6_address : "NULL"));
253 }
254
255 static void test_profile_state_callback(connection_profile_state_e state, void* user_data)
256 {
257         char *profile_name;
258         connection_profile_h profile = user_data;
259
260         if (profile == NULL)
261                 return;
262
263         if (connection_profile_get_name(profile, &profile_name) != CONNECTION_ERROR_NONE)
264                 return;
265
266         printf("[%s] : %s\n", test_print_state(state), profile_name);
267         g_free(profile_name);
268 }
269
270 static void test_connection_opened_callback(connection_error_e result, void* user_data)
271 {
272         if (result ==  CONNECTION_ERROR_NONE)
273                 printf("Connection open Succeeded\n");
274         else
275                 printf("Connection open Failed, err : [%s]\n", test_print_error(result));
276 }
277
278 static void test_connection_closed_callback(connection_error_e result, void* user_data)
279 {
280         if (result ==  CONNECTION_ERROR_NONE)
281                 printf("Connection close Succeeded\n");
282         else
283                 printf("Connection close Failed, err : [%s]\n", test_print_error(result));
284 }
285
286 static void test_connection_reset_profile_callback(connection_error_e result, void* user_data)
287 {
288         if (result ==  CONNECTION_ERROR_NONE)
289                 printf("Reset profile Succeeded\n");
290         else
291                 printf("Reset profile Failed, err : [%s]\n", test_print_error(result));
292 }
293
294 static void test_connection_set_default_callback(connection_error_e result, void* user_data)
295 {
296         if (result ==  CONNECTION_ERROR_NONE)
297                 printf("Default profile setting Succeeded\n");
298         else
299                 printf("Default profile setting Failed, err : [%s]\n", test_print_error(result));
300 }
301
302 void test_get_ethernet_cable_state_callback(connection_ethernet_cable_state_e state,
303                                                                 void* user_data)
304 {
305         if (state == CONNECTION_ETHERNET_CABLE_ATTACHED)
306                 printf("Ethernet Cable Connected\n");
307         else if (state == CONNECTION_ETHERNET_CABLE_DETACHED)
308                 printf("Ethernet Cable Disconnected\n");
309 }
310
311 static bool test_get_user_selected_profile(connection_profile_h *profile, bool select)
312 {
313         int rv = 0;
314         int input = 0;
315         char *profile_name;
316         connection_profile_type_e profile_type;
317         connection_profile_state_e profile_state;
318         connection_profile_iterator_h profile_iter;
319         connection_profile_h profile_h;
320
321         connection_profile_h profile_list[100] = {0,};
322         int profile_count = 0;
323
324         rv = connection_get_profile_iterator(connection, CONNECTION_ITERATOR_TYPE_REGISTERED, &profile_iter);
325         if (rv != CONNECTION_ERROR_NONE) {
326                 printf("Fail to get profile iterator [%s]\n", test_print_error(rv));
327                 return false;
328         }
329
330         while (connection_profile_iterator_has_next(profile_iter)) {
331                 if (connection_profile_iterator_next(profile_iter, &profile_h) != CONNECTION_ERROR_NONE) {
332                         printf("Fail to get profile handle\n");
333                         return false;
334                 }
335
336                 if (connection_profile_get_name(profile_h, &profile_name) != CONNECTION_ERROR_NONE) {
337                         printf("Fail to get profile name\n");
338                         return false;
339                 }
340
341                 if (connection_profile_get_type(profile_h, &profile_type) != CONNECTION_ERROR_NONE) {
342                         printf("Fail to get profile type\n");
343                         g_free(profile_name);
344                         return false;
345                 }
346
347                 if (connection_profile_get_state(profile_h, &profile_state) != CONNECTION_ERROR_NONE) {
348                         printf("Fail to get profile state\n");
349                         g_free(profile_name);
350                         return false;
351                 }
352
353                 printf("%d. state:[%s], profile name:%s", profile_count,
354                                 test_print_state(profile_state), profile_name);
355                 if (profile_type == CONNECTION_PROFILE_TYPE_CELLULAR) {
356                         connection_cellular_service_type_e service_type;
357                         if (connection_profile_get_cellular_service_type(
358                                 profile_h, &service_type) !=
359                                 CONNECTION_ERROR_NONE)
360                                 printf("Fail to get cellular service type!\n");
361
362                         printf("[%s]",
363                                 test_print_cellular_service_type(service_type));
364                 }
365                 printf("\n");
366
367                 profile_list[profile_count] = profile_h;
368                 profile_count++;
369
370                 g_free(profile_name);
371                 if (profile_count >= 100)
372                         break;
373         }
374
375         if (select == false)
376                 return true;
377
378         if (test_get_user_int("Input profile number(Enter for cancel) :", &input) == false ||
379             input >= profile_count ||
380             input < 0) {
381                 printf("Wrong number!!\n");
382                 return false;
383         }
384
385         if (profile)
386                 *profile = profile_list[input];
387
388         return true;
389 }
390
391 static int test_update_cellular_info(connection_profile_h profile)
392 {
393         int rv = 0;
394         char input_str1[100] = {0,};
395         char input_str2[100] = {0,};
396         int input_int = 0;
397         int type_val = 0;
398
399         if (test_get_user_int("Input Network Type (internet:1, MMS:2, Prepaid internet:3, "
400                         "Prepaid MMS:4, Tethering:5, Application:6)"
401                         " - (Enter for skip) :", &input_int)) {
402                 switch (input_int) {
403                 case 1:
404                         rv = connection_profile_set_cellular_service_type(profile,
405                                         CONNECTION_CELLULAR_SERVICE_TYPE_INTERNET);
406                         break;
407                 case 2:
408                         rv = connection_profile_set_cellular_service_type(profile,
409                                         CONNECTION_CELLULAR_SERVICE_TYPE_MMS);
410                         break;
411                 case 3:
412                         rv = connection_profile_set_cellular_service_type(profile,
413                                         CONNECTION_CELLULAR_SERVICE_TYPE_PREPAID_INTERNET);
414                         break;
415                 case 4:
416                         rv = connection_profile_set_cellular_service_type(profile,
417                                         CONNECTION_CELLULAR_SERVICE_TYPE_PREPAID_MMS);
418                         break;
419                 case 5:
420                         rv = connection_profile_set_cellular_service_type(profile,
421                                         CONNECTION_CELLULAR_SERVICE_TYPE_TETHERING);
422                         break;
423                 case 6:
424                         rv = connection_profile_set_cellular_service_type(profile,
425                                         CONNECTION_CELLULAR_SERVICE_TYPE_APPLICATION);
426                         break;
427                 default:
428                         return -1;
429                 }
430
431                 if (rv != CONNECTION_ERROR_NONE)
432                         return -1;
433         } else
434                 return -1;
435
436         if (test_get_user_string("Input Apn - (Enter for skip) :", input_str1, 100)) {
437                 g_strstrip(input_str1);
438                 rv = connection_profile_set_cellular_apn(profile, input_str1);
439                 if (rv != CONNECTION_ERROR_NONE)
440                         return -1;
441         }
442
443         if (test_get_user_string("Input Proxy - (Enter for skip) :", input_str1, 100)) {
444                 g_strstrip(input_str1);
445                 rv = connection_profile_set_proxy_address(profile, CONNECTION_ADDRESS_FAMILY_IPV4, input_str1);
446                 if (rv != CONNECTION_ERROR_NONE)
447                         return -1;
448         }
449
450         if (test_get_user_string("Input HomeURL - (Enter for skip) :", input_str1, 100)) {
451                 g_strstrip(input_str1);
452                 rv = connection_profile_set_cellular_home_url(profile, input_str1);
453                 if (rv != CONNECTION_ERROR_NONE)
454                         return -1;
455         }
456
457         if (test_get_user_int("Input AuthType(0:NONE 1:PAP 2:CHAP) - (Enter for skip) :", &input_int)) {
458                 switch (input_int) {
459                 case 0:
460                         rv = connection_profile_set_cellular_auth_info(profile,
461                                         CONNECTION_CELLULAR_AUTH_TYPE_NONE, "", "");
462                         if (rv != CONNECTION_ERROR_NONE)
463                                 return -1;
464
465                         break;
466                 case 1:
467                         type_val = CONNECTION_CELLULAR_AUTH_TYPE_PAP;
468                         /* fall through */
469                 case 2:
470                         if (input_int == 2) type_val = CONNECTION_CELLULAR_AUTH_TYPE_CHAP;
471
472                         if (test_get_user_string("Input AuthId(Enter for skip) :", input_str1, 100) == false)
473                                 input_str1[0] = 0;
474                         if (test_get_user_string("Input AuthPwd(Enter for skip) :", input_str2, 100) == false)
475                                 input_str2[0] = 0;
476
477                         g_strstrip(input_str1);
478                         g_strstrip(input_str2);
479                         rv = connection_profile_set_cellular_auth_info(profile, type_val, input_str1, input_str2);
480                         if (rv != CONNECTION_ERROR_NONE)
481                                 return -1;
482                 }
483         }
484
485         if (test_get_user_int("Input PdnType(1:IPv4 2:IPv6 3:IPv4v6) - (Enter for skip) :", &input_int)) {
486                 switch (input_int) {
487                 case 1:
488                         rv = connection_profile_set_cellular_pdn_type(profile, CONNECTION_CELLULAR_PDN_TYPE_IPV4);
489                         break;
490                 case 2:
491                         rv = connection_profile_set_cellular_pdn_type(profile, CONNECTION_CELLULAR_PDN_TYPE_IPV6);
492                         break;
493                 case 3:
494                         rv = connection_profile_set_cellular_pdn_type(profile, CONNECTION_CELLULAR_PDN_TYPE_IPV4_IPv6);
495                         break;
496                 }
497
498                 if (rv != CONNECTION_ERROR_NONE)
499                         return -1;
500         }
501
502         if (test_get_user_int("Input RoamPdnType(1:IPv4 2:IPv6 3:IPv4v6) - (Enter for skip) :", &input_int)) {
503                 switch (input_int) {
504                 case 1:
505                         rv = connection_profile_set_cellular_roam_pdn_type(profile, CONNECTION_CELLULAR_PDN_TYPE_IPV4);
506                         break;
507                 case 2:
508                         rv = connection_profile_set_cellular_roam_pdn_type(profile, CONNECTION_CELLULAR_PDN_TYPE_IPV6);
509                         break;
510                 case 3:
511                         rv = connection_profile_set_cellular_roam_pdn_type(profile, CONNECTION_CELLULAR_PDN_TYPE_IPV4_IPv6);
512                         break;
513                 }
514
515                 if (rv != CONNECTION_ERROR_NONE)
516                         return -1;
517         }
518
519         return 1;
520 }
521
522 static int test_update_wifi_info(connection_profile_h profile)
523 {
524         int rv = 0;
525         char input_str[100] = {0,};
526
527         if (test_get_user_string("Input Passphrase - (Enter for skip) :", input_str, 100)) {
528                 rv = connection_profile_set_wifi_passphrase(profile, input_str);
529                 if (rv != CONNECTION_ERROR_NONE)
530                         return -1;
531         }
532
533         return 1;
534 }
535
536 static int test_update_dns_info(connection_profile_h profile,
537                 connection_address_family_e address_family)
538 {
539         int rv = 0;
540         char input_str[100] = {0,};
541         if (test_get_user_string("Input DNS 1 Address - (Enter for skip) :", input_str, 100)) {
542                 rv = connection_profile_set_dns_address(profile,
543                                 1,
544                                 address_family,
545                                 input_str);
546                 if (rv != CONNECTION_ERROR_NONE)
547                         return -1;
548
549                 if (test_get_user_string("Input DNS 2 Address - (Enter for skip) :", input_str, 100)) {
550                         rv = connection_profile_set_dns_address(profile,
551                                         2,
552                                         address_family,
553                                         input_str);
554                         if (rv != CONNECTION_ERROR_NONE)
555                                 return -1;
556                 }
557         }
558         return 1;
559 }
560
561 static int test_update_ip_info(connection_profile_h profile, connection_address_family_e address_family)
562 {
563         int rv = 0;
564         int input_int = 0;
565         char input_str[100] = {0,};
566
567         if (test_get_user_string("Input IP Address - (Enter for skip) :", input_str, 100)) {
568                 rv = connection_profile_set_ip_address(profile,
569                                                         address_family,
570                                                         input_str);
571                 if (rv != CONNECTION_ERROR_NONE)
572                         return -1;
573         }
574
575         if (test_get_user_string("Input Netmask - (Enter for skip) :", input_str, 100)) {
576                 rv = connection_profile_set_subnet_mask(profile,
577                                                         address_family,
578                                                         input_str);
579                 if (rv != CONNECTION_ERROR_NONE)
580                         return -1;
581         }
582
583         if (test_get_user_int("Input Prefix Length - (Enter for skip) :", &input_int)) {
584                 rv = connection_profile_set_prefix_length(profile,
585                                                         address_family,
586                                                         input_int);
587                 if (rv != CONNECTION_ERROR_NONE)
588                         return -1;
589         }
590
591         if (test_get_user_string("Input Gateway - (Enter for skip) :", input_str, 100)) {
592                 rv = connection_profile_set_gateway_address(profile,
593                                                         address_family,
594                                                         input_str);
595                 if (rv != CONNECTION_ERROR_NONE)
596                         return -1;
597         }
598
599         if (test_update_dns_info(profile, address_family) < 0)
600                 return -1;
601
602         return 1;
603 }
604
605 static int test_update_proxy_info(connection_profile_h profile, connection_address_family_e address_family)
606 {
607         int rv = 0;
608         int input_int = 0;
609         char input_str[100] = {0,};
610
611         if (test_get_user_int("Input Proxy Type (1:direct, 2:auto, 3:manual)"
612                                         " - (Enter for skip) :", &input_int)) {
613                 switch (input_int) {
614                 case 1:
615                         rv = connection_profile_set_proxy_type(profile,
616                                         CONNECTION_PROXY_TYPE_DIRECT);
617
618                         if (rv != CONNECTION_ERROR_NONE)
619                                 return -1;
620                         else
621                                 return 1;
622                 case 2:
623                         rv = connection_profile_set_proxy_type(profile,
624                                         CONNECTION_PROXY_TYPE_AUTO);
625                         break;
626                 case 3:
627                         rv = connection_profile_set_proxy_type(profile,
628                                         CONNECTION_PROXY_TYPE_MANUAL);
629                         break;
630                 default:
631                         return -1;
632                 }
633
634                 if (rv != CONNECTION_ERROR_NONE)
635                         return -1;
636
637                 if (test_get_user_string("Input auto Proxy URL or Proxy address"
638                                         " - (Enter for skip) :", input_str, 100)) {
639                         rv = connection_profile_set_proxy_address(profile,
640                                                                 address_family,
641                                                                 input_str);
642                         if (rv != CONNECTION_ERROR_NONE)
643                                 return -1;
644                 }
645
646         } else
647                 return -1;
648
649         return 1;
650 }
651
652
653
654 static int test_update_network_info(connection_profile_h profile)
655 {
656         int rv = 0;
657         int input_int = 0;
658         int dns_input = 0;
659         int address_family = 0;
660
661         test_get_user_int("Input Address Family (0:IPv4 1:IPv6) :", &address_family);
662
663         if (test_get_user_int("Input IPv4/IPv6 Address Type (DHCP:1, Static:2, Auto:3)"
664                                 " - (Enter for skip) :", &input_int)) {
665                 switch (input_int) {
666                 case 1:
667                         rv = connection_profile_set_ip_config_type(profile,
668                                         address_family,
669                                         CONNECTION_IP_CONFIG_TYPE_DYNAMIC);
670                         if (test_get_user_int("Input DNS Address Type (Static:1, DHCP:2)"
671                                                 " - (Enter for skip) :", &dns_input)) {
672                                 switch (dns_input) {
673                                 case CONNECTION_DNS_CONFIG_TYPE_STATIC:
674                                         rv = connection_profile_set_dns_config_type(
675                                                         profile,
676                                                         address_family,
677                                                         CONNECTION_DNS_CONFIG_TYPE_STATIC);
678                                         if (rv != CONNECTION_ERROR_NONE)
679                                                 return -1;
680                                         if (test_update_dns_info(profile,
681                                                                 address_family) == -1)
682                                                 return -1;
683                                         break;
684                                 case CONNECTION_DNS_CONFIG_TYPE_DYNAMIC:
685                                         rv = connection_profile_set_dns_config_type(
686                                                         profile,
687                                                         address_family,
688                                                         CONNECTION_DNS_CONFIG_TYPE_DYNAMIC);
689                                         if (rv != CONNECTION_ERROR_NONE)
690                                                 return -1;
691                                         break;
692                                 }
693                         }
694                         break;
695                 case 2:
696                         rv = connection_profile_set_ip_config_type(profile,
697                                                                    address_family,
698                                                                    CONNECTION_IP_CONFIG_TYPE_STATIC);
699                         if (rv != CONNECTION_ERROR_NONE)
700                                 return -1;
701
702                         if (test_update_ip_info(profile, address_family) == -1)
703                                 return -1;
704
705                         if (test_update_proxy_info(profile, address_family) == -1)
706                                 return -1;
707                         break;
708                 case 3:
709                         rv = connection_profile_set_ip_config_type(profile,
710                                                                 address_family,
711                                                                 CONNECTION_IP_CONFIG_TYPE_AUTO);
712                         break;
713                 default:
714                         return -1;
715                 }
716
717                 if (rv != CONNECTION_ERROR_NONE)
718                         return -1;
719         } else
720                 return -1;
721
722         return 1;
723 }
724
725 static void test_print_cellular_info(connection_profile_h profile)
726 {
727         connection_cellular_service_type_e service_type;
728         connection_cellular_pdn_type_e pdn_type;
729         connection_cellular_pdn_type_e roam_pdn_type;
730         char *apn = NULL;
731         connection_cellular_auth_type_e auth_type;
732         char *user_name = NULL;
733         char *password = NULL;
734         char *home_url = NULL;
735         bool roaming = false;
736         bool hidden = false;
737         bool editable = false;
738
739         if (connection_profile_get_cellular_service_type(profile, &service_type) != CONNECTION_ERROR_NONE)
740                 printf("Fail to get cellular service type!\n");
741         else
742                 printf("Cellular service type : %s\n", test_print_cellular_service_type(service_type));
743
744         if (connection_profile_get_cellular_pdn_type(profile, &pdn_type) != CONNECTION_ERROR_NONE)
745                 printf("Fail to get cellular pdn type!\n");
746         else
747                 printf("Cellular pdn type : %s\n", test_print_cellular_pdn_type(pdn_type));
748
749         if (connection_profile_get_cellular_roam_pdn_type(profile, &roam_pdn_type) != CONNECTION_ERROR_NONE)
750                 printf("Fail to get cellular roam pdn type!\n");
751         else
752                 printf("Cellular roam pdn type : %s\n", test_print_cellular_pdn_type(roam_pdn_type));
753
754         if (connection_profile_get_cellular_apn(profile, &apn) != CONNECTION_ERROR_NONE)
755                 printf("Fail to get cellular APN!\n");
756         else {
757                 printf("Cellular APN : %s\n", apn);
758                 g_free(apn);
759         }
760
761         if (connection_profile_get_cellular_auth_info(profile, &auth_type, &user_name, &password) != CONNECTION_ERROR_NONE)
762                 printf("Fail to get auth info!\n");
763         else {
764                 printf("Cellular auth type : %s\n", test_print_cellular_auth_type(auth_type));
765                 printf("Cellular user_name : %s\n", user_name);
766                 printf("Cellular password : %s\n", password);
767                 g_free(user_name);
768                 g_free(password);
769         }
770
771         if (connection_profile_get_cellular_home_url(profile, &home_url) != CONNECTION_ERROR_NONE)
772                 printf("Fail to get cellular home url!\n");
773         else {
774                 printf("Cellular home url : %s\n", home_url);
775                 g_free(home_url);
776         }
777
778         if (connection_profile_is_cellular_roaming(profile, &roaming) != CONNECTION_ERROR_NONE)
779                 printf("Fail to get cellular roaming state!\n");
780         else
781                 printf("Cellular roaming : %s\n", roaming ? "true" : "false");
782
783         if (connection_profile_is_cellular_hidden(profile, &hidden) != CONNECTION_ERROR_NONE)
784                 printf("Fail to get cellular hidden state!\n");
785         else
786                 printf("Cellular hidden : %s\n", hidden ? "true" : "false");
787
788         if (connection_profile_is_cellular_editable(profile, &editable) != CONNECTION_ERROR_NONE)
789                 printf("Fail to get cellular editing state!\n");
790         else
791                 printf("Cellular editable : %s\n", editable ? "true" : "false");
792 }
793
794 static void test_print_wifi_info(connection_profile_h profile)
795 {
796         char *essid = NULL;
797         char *bssid = NULL;
798         int rssi = 0;
799         int frequency = 0;
800         int max_speed = 0;
801         connection_wifi_security_type_e security_type;
802         connection_wifi_encryption_type_e encryption_type;
803         bool pass_required = false;
804         bool wps_supported = false;
805
806         if (connection_profile_get_wifi_essid(profile, &essid) != CONNECTION_ERROR_NONE)
807                 printf("Fail to get Wi-Fi essid!\n");
808         else {
809                 printf("Wi-Fi essid : %s\n", essid);
810                 g_free(essid);
811         }
812
813         if (connection_profile_get_wifi_bssid(profile, &bssid) != CONNECTION_ERROR_NONE)
814                 printf("Fail to get Wi-Fi bssid!\n");
815         else {
816                 printf("Wi-Fi bssid : %s\n", bssid);
817                 g_free(bssid);
818         }
819
820         if (connection_profile_get_wifi_rssi(profile, &rssi) != CONNECTION_ERROR_NONE)
821                 printf("Fail to get Wi-Fi rssi!\n");
822         else
823                 printf("Wi-Fi rssi : %d\n", rssi);
824
825         if (connection_profile_get_wifi_frequency(profile, &frequency) != CONNECTION_ERROR_NONE)
826                 printf("Fail to get Wi-Fi frequency!\n");
827         else
828                 printf("Wi-Fi frequency : %d\n", frequency);
829
830         if (connection_profile_get_wifi_max_speed(profile, &max_speed) != CONNECTION_ERROR_NONE)
831                 printf("Fail to get Wi-Fi max speed!\n");
832         else
833                 printf("Wi-Fi max speed : %d\n", max_speed);
834
835         if (connection_profile_get_wifi_security_type(profile, &security_type) != CONNECTION_ERROR_NONE)
836                 printf("Fail to get Wi-Fi security type!\n");
837         else
838                 printf("Wi-Fi security type : %d\n", security_type);
839
840         if (connection_profile_get_wifi_encryption_type(profile, &encryption_type) != CONNECTION_ERROR_NONE)
841                 printf("Fail to get Wi-Fi encryption type!\n");
842         else
843                 printf("Wi-Fi encryption type : %d\n", encryption_type);
844
845         if (connection_profile_is_wifi_passphrase_required(profile, &pass_required) != CONNECTION_ERROR_NONE)
846                 printf("Fail to get Wi-Fi passphrase required!\n");
847         else
848                 printf("Wi-Fi passphrase required : %s\n", pass_required ? "true" : "false");
849
850         if (connection_profile_is_wifi_wps_supported(profile, &wps_supported) != CONNECTION_ERROR_NONE)
851                 printf("Fail to get Wi-Fi wps info\n");
852         else
853                 printf("Wi-Fi wps supported : %s\n", wps_supported ? "true" : "false");
854 }
855
856 static void test_print_network_info(connection_profile_h profile, connection_address_family_e address_family)
857 {
858         char *interface_name = NULL;
859         char *ip = NULL;
860         char *subnet = NULL;
861         char *gateway = NULL;
862         char *dhcp_server = NULL;
863         int dhcp_lease_duration = 0;
864         char *dns1 = NULL;
865         char *dns2 = NULL;
866         char *proxy = NULL;
867         int prefix_len;
868         connection_ip_config_type_e ip_type;
869         connection_proxy_type_e proxy_type;
870         connection_dns_config_type_e dns_type;
871
872         if (connection_profile_get_network_interface_name(profile, &interface_name) != CONNECTION_ERROR_NONE)
873                 printf("Fail to get interface name!\n");
874         else {
875                 printf("Interface name : %s\n", interface_name);
876                 g_free(interface_name);
877         }
878
879         if (connection_profile_get_ip_config_type(profile, address_family, &ip_type) != CONNECTION_ERROR_NONE)
880                 printf("Fail to get ipconfig type!\n");
881         else
882                 printf("Ipconfig type : %d\n", ip_type);
883
884         if (connection_profile_get_ip_address(profile, address_family, &ip) != CONNECTION_ERROR_NONE)
885                 printf("Fail to get IP address!\n");
886         else {
887                 printf("IP address : %s\n", ip);
888                 g_free(ip);
889         }
890
891         if (connection_profile_get_gateway_address(profile, address_family, &gateway) != CONNECTION_ERROR_NONE)
892                 printf("Fail to get gateway!\n");
893         else {
894                 printf("Gateway : %s\n", gateway);
895                 g_free(gateway);
896         }
897
898         if (connection_profile_get_dhcp_server_address(profile, address_family, &dhcp_server) != CONNECTION_ERROR_NONE)
899                 printf("Fail to get DHCP Server address!\n");
900         else {
901                 printf("DHCP Server : %s\n", dhcp_server);
902                 g_free(dhcp_server);
903         }
904
905         if (connection_profile_get_dhcp_lease_duration(profile, address_family, &dhcp_lease_duration) != CONNECTION_ERROR_NONE)
906                 printf("Fail to get DHCP lease duration!\n");
907         else {
908                 printf("DHCP lease duration : %d\n", dhcp_lease_duration);
909         }
910
911         if (connection_profile_get_subnet_mask(profile, address_family, &subnet) != CONNECTION_ERROR_NONE)
912                 printf("Fail to get subnet mask!\n");
913         else {
914                 printf("Subnet mask : %s\n", subnet);
915                 g_free(subnet);
916         }
917
918         if (connection_profile_get_prefix_length(profile, address_family, &prefix_len) != CONNECTION_ERROR_NONE)
919                 printf("Fail to get prefix length!\n");
920         else
921                 printf("Prefix length : %d\n", prefix_len);
922
923         if (connection_profile_get_dns_config_type(profile, address_family, &dns_type) != CONNECTION_ERROR_NONE)
924                 printf("Fail to get DNS configuration type!\n");
925         else
926                 printf("DNS configuration type : %d\n", dns_type);
927
928         if (connection_profile_get_dns_address(profile, 1, address_family, &dns1) != CONNECTION_ERROR_NONE)
929                 printf("Fail to get DNS1!\n");
930         else {
931                 printf("DNS1 : %s\n", dns1);
932                 g_free(dns1);
933         }
934
935         if (connection_profile_get_dns_address(profile, 2, address_family, &dns2) != CONNECTION_ERROR_NONE)
936                 printf("Fail to get DNS2!\n");
937         else {
938                 printf("DNS2 : %s\n", dns2);
939                 g_free(dns2);
940         }
941
942         if (connection_profile_get_proxy_type(profile, &proxy_type) != CONNECTION_ERROR_NONE)
943                 printf("Fail to get proxy type!\n");
944         else
945                 printf("Proxy type : %d\n", proxy_type);
946
947         if (connection_profile_get_proxy_address(profile, address_family, &proxy) != CONNECTION_ERROR_NONE)
948                 printf("Fail to get proxy!\n");
949         else {
950                 printf("Proxy : %s\n", proxy);
951                 g_free(proxy);
952         }
953 }
954
955 int test_register_client(void)
956 {
957
958         int err = connection_create(&connection);
959
960         if (CONNECTION_ERROR_NONE == err) {
961                 connection_set_type_changed_cb(connection, test_type_changed_callback, NULL);
962                 connection_set_ip_address_changed_cb(connection, test_ip_changed_callback, NULL);
963                 connection_set_proxy_address_changed_cb(connection, test_proxy_changed_callback, NULL);
964                 connection_set_ethernet_cable_state_chaged_cb(connection,
965                                         test_get_ethernet_cable_state_callback, NULL);
966         } else {
967                 printf("Client registration failed [%s]\n", test_print_error(err));
968                 return -1;
969         }
970
971         printf("Client registration success\n");
972         return 1;
973 }
974
975 int  test_deregister_client(void)
976 {
977         int rv = 0;
978         GSList *list;
979         connection_profile_h profile;
980
981         if (connection != NULL)
982                 rv = connection_destroy(connection);
983         else {
984                 printf("Cannot deregister : Handle is NULL\n");
985                 rv = CONNECTION_ERROR_INVALID_OPERATION;
986         }
987
988         if (rv != CONNECTION_ERROR_NONE) {
989                 printf("Client deregistration fail [%s]\n", test_print_error(rv));
990                 return -1;
991         }
992
993         if (state_cb_list) {
994                 for (list = state_cb_list; list; list = list->next) {
995                         profile = list->data;
996                         connection_profile_destroy(profile);
997                 }
998
999                 g_slist_free(state_cb_list);
1000                 state_cb_list = NULL;
1001         }
1002
1003         connection = NULL;
1004         printf("Client deregistration success\n");
1005
1006         return 1;
1007 }
1008
1009 int test_get_network_state(void)
1010 {
1011         int rv = 0;
1012         connection_type_e net_state;
1013
1014         rv = connection_get_type(connection, &net_state);
1015
1016         if (rv != CONNECTION_ERROR_NONE) {
1017                 printf("Fail to get network state [%s]\n", test_print_error(rv));
1018                 return -1;
1019         }
1020
1021         printf("Retval = [%s] network connection state [%s]\n",
1022                 test_print_error(rv), test_print_connection_type(net_state));
1023
1024         return 1;
1025 }
1026
1027 int test_get_cellular_state(void)
1028 {
1029         int rv = 0;
1030         connection_cellular_state_e cellular_state;
1031
1032         rv = connection_get_cellular_state(connection, &cellular_state);
1033
1034         if (rv != CONNECTION_ERROR_NONE) {
1035                 printf("Fail to get Cellular state [%s]\n", test_print_error(rv));
1036                 return -1;
1037         }
1038
1039         printf("Retval = [%s] Cellular state [%s]\n",
1040                 test_print_error(rv), test_print_cellular_state(cellular_state));
1041
1042         return 1;
1043 }
1044
1045 int test_get_wifi_state(void)
1046 {
1047         int rv = 0;
1048         connection_wifi_state_e wifi_state;
1049
1050         rv = connection_get_wifi_state(connection, &wifi_state);
1051
1052         if (rv != CONNECTION_ERROR_NONE) {
1053                 printf("Fail to get WiFi state [%s]\n", test_print_error(rv));
1054                 return -1;
1055         }
1056
1057         printf("Retval = [%s] WiFi state [%s]\n",
1058                 test_print_error(rv), test_print_wifi_state(wifi_state));
1059
1060         return 1;
1061 }
1062
1063 int test_get_current_proxy(void)
1064 {
1065         char *proxy_addr = NULL;
1066
1067         connection_get_proxy(connection, CONNECTION_ADDRESS_FAMILY_IPV4, &proxy_addr);
1068
1069         if (proxy_addr == NULL) {
1070                 printf("Proxy address does not exist\n");
1071                 return -1;
1072         }
1073
1074         printf("Current Proxy [%s]\n", proxy_addr);
1075         g_free(proxy_addr);
1076
1077         return 1;
1078 }
1079
1080 int test_get_current_ip(void)
1081 {
1082         char *ip_addr = NULL;
1083         int input;
1084         bool rv;
1085
1086         rv = test_get_user_int("Input Address type to get"
1087                 "(1:IPV4, 2:IPV6):", &input);
1088
1089         if (rv == false) {
1090                 printf("Invalid input!!\n");
1091                 return -1;
1092         }
1093
1094         switch (input) {
1095         case 1:
1096                 connection_get_ip_address(connection, CONNECTION_ADDRESS_FAMILY_IPV4, &ip_addr);
1097                 if (ip_addr == NULL) {
1098                         printf("IPv4 address does not exist\n");
1099                         return -1;
1100                 }
1101                 printf("IPv4 address : %s\n", ip_addr);
1102                 break;
1103
1104         case 2:
1105                 connection_get_ip_address(connection, CONNECTION_ADDRESS_FAMILY_IPV6, &ip_addr);
1106                 if (ip_addr == NULL) {
1107                         printf("IPv6 address does not exist\n");
1108                         return -1;
1109                 }
1110                 printf("IPv6 address : %s\n", ip_addr);
1111                 break;
1112         default:
1113                 printf("Wrong IP address family!!\n");
1114                 return -1;
1115         }
1116
1117         g_free(ip_addr);
1118         return 1;
1119 }
1120
1121 int test_get_call_statistics_info(void)
1122 {
1123         long long rv = 0;
1124
1125         connection_get_statistics(connection, CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_LAST_RECEIVED_DATA, &rv);
1126         printf("last recv data size [%lld]\n", rv);
1127         connection_get_statistics(connection, CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_LAST_SENT_DATA, &rv);
1128         printf("last sent data size [%lld]\n", rv);
1129         connection_get_statistics(connection, CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_TOTAL_RECEIVED_DATA, &rv);
1130         printf("total received data size [%lld]\n", rv);
1131         connection_get_statistics(connection, CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_TOTAL_SENT_DATA, &rv);
1132         printf("total sent data size [%lld]\n", rv);
1133
1134         return 1;
1135 }
1136
1137 int test_get_wifi_call_statistics_info(void)
1138 {
1139         long long rv = 0;
1140
1141         connection_get_statistics(connection, CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_LAST_RECEIVED_DATA, &rv);
1142         printf("WiFi last recv data size [%lld]\n", rv);
1143         connection_get_statistics(connection, CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_LAST_SENT_DATA, &rv);
1144         printf("WiFi last sent data size [%lld]\n", rv);
1145         connection_get_statistics(connection, CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_TOTAL_RECEIVED_DATA, &rv);
1146         printf("WiFi total received data size [%lld]\n", rv);
1147         connection_get_statistics(connection, CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_TOTAL_SENT_DATA, &rv);
1148         printf("WiFi total sent data size [%lld]\n", rv);
1149
1150         return 1;
1151 }
1152
1153 int test_get_profile_list(void)
1154 {
1155         if (test_get_user_selected_profile(NULL, false) == false)
1156                 return -1;
1157
1158         return 1;
1159 }
1160
1161 int test_get_default_profile_list(void)
1162 {
1163         int rv = 0;
1164         char *profile_name = NULL;
1165         connection_profile_iterator_h profile_iter;
1166         connection_profile_h profile_h;
1167         connection_cellular_service_type_e service_type;
1168         bool is_default = false;
1169
1170         rv = connection_get_profile_iterator(connection, CONNECTION_ITERATOR_TYPE_DEFAULT, &profile_iter);
1171         if (rv != CONNECTION_ERROR_NONE) {
1172                 printf("Fail to get profile iterator [%s]\n", test_print_error(rv));
1173                 return -1;
1174         }
1175
1176         while (connection_profile_iterator_has_next(profile_iter)) {
1177                 if (connection_profile_iterator_next(profile_iter, &profile_h) != CONNECTION_ERROR_NONE) {
1178                         printf("Fail to get profile handle\n");
1179                         return -1;
1180                 }
1181
1182                 if (connection_profile_get_name(profile_h, &profile_name) != CONNECTION_ERROR_NONE) {
1183                         printf("Fail to get profile name\n");
1184                         return -1;
1185                 }
1186                 printf("profile name : %s\n", profile_name);
1187                 g_free(profile_name);
1188
1189                 if (connection_profile_get_cellular_service_type(profile_h, &service_type) != CONNECTION_ERROR_NONE) {
1190                         printf("Fail to get profile service type\n");
1191                         return -1;
1192                 }
1193                 printf("service type : %d\n", service_type);
1194
1195                 if (connection_profile_is_cellular_default(profile_h, &is_default) != CONNECTION_ERROR_NONE) {
1196                         printf("Fail to get profile subscriber id\n");
1197                         return -1;
1198                 }
1199                 printf("Default : %d\n", is_default);
1200         }
1201
1202         return 1;
1203 }
1204
1205 int test_get_connected_profile_list(void)
1206 {
1207         int rv = 0;
1208         char *profile_name = NULL;
1209         connection_profile_iterator_h profile_iter;
1210         connection_profile_h profile_h;
1211         bool is_default = false;
1212         connection_profile_type_e type;
1213
1214         rv = connection_get_profile_iterator(connection, CONNECTION_ITERATOR_TYPE_CONNECTED, &profile_iter);
1215         if (rv != CONNECTION_ERROR_NONE) {
1216                 printf("Fail to get profile iterator [%s]\n", test_print_error(rv));
1217                 return -1;
1218         }
1219
1220         while (connection_profile_iterator_has_next(profile_iter)) {
1221                 if (connection_profile_iterator_next(profile_iter, &profile_h) != CONNECTION_ERROR_NONE) {
1222                         printf("Fail to get profile handle\n");
1223                         return -1;
1224                 }
1225
1226                 if (connection_profile_get_name(profile_h, &profile_name) != CONNECTION_ERROR_NONE) {
1227                         printf("Fail to get profile name\n");
1228                         return -1;
1229                 }
1230                 printf("profile name is %s\n", profile_name);
1231                 g_free(profile_name);
1232
1233                 if (connection_profile_get_type(profile_h, &type) != CONNECTION_ERROR_NONE) {
1234                         printf("Fail to get profile type\n");
1235                         return -1;
1236                 }
1237                 printf("profile type is %d\n", type);
1238
1239                 if (type == CONNECTION_PROFILE_TYPE_CELLULAR) {
1240                 if (connection_profile_is_cellular_default(profile_h, &is_default) != CONNECTION_ERROR_NONE) {
1241                         printf("Fail to get profile is default\n");
1242                         return -1;
1243                 }
1244                         printf("[%s]\n", is_default ? "default" : "not default");
1245                 }
1246         }
1247
1248         return 1;
1249 }
1250
1251 int test_get_current_profile(void)
1252 {
1253         int rv = 0;
1254         char *profile_name = NULL;
1255         connection_profile_h profile_h;
1256
1257         rv = connection_get_current_profile(connection, &profile_h);
1258         if (rv != CONNECTION_ERROR_NONE) {
1259                 printf("Fail to get profile iterator [%s]\n", test_print_error(rv));
1260                 return -1;
1261         }
1262
1263         if (connection_profile_get_name(profile_h, &profile_name) != CONNECTION_ERROR_NONE) {
1264                 printf("Fail to get profile name\n");
1265                 return -1;
1266         }
1267         printf("profile name : %s\n", profile_name);
1268         g_free(profile_name);
1269
1270         connection_profile_destroy(profile_h);
1271
1272         return 1;
1273 }
1274
1275 int test_open_profile(void)
1276 {
1277         connection_profile_h profile;
1278
1279         printf("\n** Choose a profile to open. **\n");
1280
1281         if (test_get_user_selected_profile(&profile, true) == false)
1282                 return -1;
1283
1284         if (connection_open_profile(connection, profile, test_connection_opened_callback, NULL) != CONNECTION_ERROR_NONE) {
1285                 printf("Connection open Failed!!\n");
1286                 return -1;
1287         }
1288
1289         return 1;
1290 }
1291
1292 int test_get_default_cellular_service_type(void)
1293 {
1294         int input;
1295         int rv;
1296         int service_type;
1297         connection_profile_h profile;
1298         char *profile_name = NULL;
1299
1300         rv = test_get_user_int("Input profile type to get"
1301                         "(1:Internet, 2:MMS, 3:Prepaid internet, 4:Prepaid MMS, 5:Tethering, 6:Application):", &input);
1302
1303         if (rv == false) {
1304                 printf("Invalid input!!\n");
1305                 return -1;
1306         }
1307
1308         switch (input) {
1309         case 1:
1310                 service_type = CONNECTION_CELLULAR_SERVICE_TYPE_INTERNET;
1311                 break;
1312         case 2:
1313                 service_type = CONNECTION_CELLULAR_SERVICE_TYPE_MMS;
1314                 break;
1315         case 3:
1316                 service_type = CONNECTION_CELLULAR_SERVICE_TYPE_PREPAID_INTERNET;
1317                 break;
1318         case 4:
1319                 service_type = CONNECTION_CELLULAR_SERVICE_TYPE_PREPAID_MMS;
1320                 break;
1321         case 5:
1322                 service_type = CONNECTION_CELLULAR_SERVICE_TYPE_TETHERING;
1323                 break;
1324         case 6:
1325                 service_type =  CONNECTION_CELLULAR_SERVICE_TYPE_APPLICATION;
1326                 break;
1327         default:
1328                 printf("Wrong number!!\n");
1329                 return -1;
1330         }
1331
1332         if (connection_get_default_cellular_service_profile(connection, service_type, &profile) != CONNECTION_ERROR_NONE)
1333                 return -1;
1334
1335         if (connection_profile_get_name(profile, &profile_name) != CONNECTION_ERROR_NONE) {
1336                 printf("Fail to get profile name\n");
1337                 connection_profile_destroy(profile);
1338                 return -1;
1339         }
1340         printf("Default profile name : %s\n", profile_name);
1341         g_free(profile_name);
1342
1343         connection_profile_destroy(profile);
1344
1345         return 1;
1346 }
1347
1348 int test_set_default_cellular_service_type(void)
1349 {
1350         connection_profile_h profile;
1351         connection_cellular_service_type_e type;
1352         int input, rv;
1353
1354         rv = test_get_user_int("Input API type (1:sync, 2:async)", &input);
1355
1356         if (rv == false || (input != 1 && input != 2)) {
1357                 printf("Invalid input!!\n");
1358                 return -1;
1359         }
1360
1361         printf("\n** Choose a profile to set default service(internet or prepaid internet type only). **\n");
1362
1363         if (test_get_user_selected_profile(&profile, true) == false)
1364                 return -1;
1365
1366         if (connection_profile_get_cellular_service_type(profile, &type) != CONNECTION_ERROR_NONE) {
1367                 printf("Fail to get cellular service type\n");
1368                 return -1;
1369         }
1370
1371         if (input == 1) {
1372                 if (connection_set_default_cellular_service_profile(connection, type, profile) != CONNECTION_ERROR_NONE)
1373                         return -1;
1374         } else {
1375                 if (connection_set_default_cellular_service_profile_async(connection,
1376                                 type, profile, test_connection_set_default_callback, NULL) != CONNECTION_ERROR_NONE)
1377                         return -1;
1378         }
1379
1380         return 1;
1381 }
1382
1383 int test_close_profile(void)
1384 {
1385         connection_profile_h profile;
1386
1387         printf("\n** Choose a profile to close. **\n");
1388
1389         if (test_get_user_selected_profile(&profile, true) == false)
1390                 return -1;
1391
1392         if (connection_close_profile(connection, profile, test_connection_closed_callback, NULL) != CONNECTION_ERROR_NONE) {
1393                 printf("Connection close Failed!!\n");
1394                 return -1;
1395         }
1396
1397         return 1;
1398 }
1399
1400 int test_add_profile(void)
1401 {
1402         int rv = 0;
1403         connection_profile_h profile;
1404         char input_str[100] = {0,};
1405
1406         if (test_get_user_string("Input Keyword - (Enter for skip) :", input_str, 100) == false)
1407                 return -1;
1408
1409         g_strstrip(input_str);
1410         rv = connection_profile_create(CONNECTION_PROFILE_TYPE_CELLULAR, input_str, &profile);
1411         if (rv != CONNECTION_ERROR_NONE)
1412                 RETURN_FAIL_DESTROY(profile);
1413
1414         if (test_update_cellular_info(profile) == -1)
1415                 RETURN_FAIL_DESTROY(profile);
1416
1417         rv = connection_add_profile(connection, profile);
1418         if (rv != CONNECTION_ERROR_NONE)
1419                 RETURN_FAIL_DESTROY(profile);
1420
1421         connection_profile_destroy(profile);
1422         return 1;
1423 }
1424
1425 int test_remove_profile(void)
1426 {
1427         connection_profile_h profile;
1428
1429         printf("\n** Choose a profile to remove. **\n");
1430         if (test_get_user_selected_profile(&profile, true) == false)
1431                 return -1;
1432
1433         if (connection_remove_profile(connection, profile) != CONNECTION_ERROR_NONE) {
1434                 printf("Remove profile Failed!!\n");
1435                 return -1;
1436         }
1437
1438         return 1;
1439 }
1440
1441 int test_update_profile(void)
1442 {
1443         int rv = 0;
1444
1445         connection_profile_type_e prof_type;
1446         connection_profile_h profile;
1447
1448         printf("\n** Choose a profile to update. **\n");
1449         if (test_get_user_selected_profile(&profile, true) == false)
1450                 return -1;
1451
1452         if (connection_profile_get_type(profile, &prof_type) != CONNECTION_ERROR_NONE)
1453                 return -1;
1454
1455         switch (prof_type) {
1456         case CONNECTION_PROFILE_TYPE_CELLULAR:
1457                 if (test_update_cellular_info(profile) == -1)
1458                         return -1;
1459
1460                 break;
1461         case CONNECTION_PROFILE_TYPE_WIFI:
1462                 if (test_update_wifi_info(profile) == -1)
1463                         return -1;
1464
1465                 if (test_update_network_info(profile) == -1)
1466                         return -1;
1467
1468                 break;
1469         case CONNECTION_PROFILE_TYPE_ETHERNET:
1470                 if (test_update_network_info(profile) == -1)
1471                         return -1;
1472                 break;
1473
1474         case CONNECTION_PROFILE_TYPE_BT:
1475                 printf("Not supported!\n");
1476                 /* fall through */
1477         default:
1478                 return -1;
1479         }
1480
1481         rv = connection_update_profile(connection, profile);
1482         if (rv != CONNECTION_ERROR_NONE)
1483                 return -1;
1484
1485         return 1;
1486 }
1487
1488 int test_get_profile_info(void)
1489 {
1490         connection_profile_type_e prof_type;
1491         connection_profile_state_e profile_state;
1492         connection_profile_state_e profile_ipv6_state;
1493         connection_profile_h profile;
1494         char *profile_name = NULL;
1495         int address_family = 0;
1496
1497         printf("\n** Choose a profile to print. **\n");
1498         if (test_get_user_selected_profile(&profile, true) == false)
1499                 return -1;
1500
1501         if (connection_profile_get_name(profile, &profile_name) != CONNECTION_ERROR_NONE) {
1502                 printf("Fail to get profile name\n");
1503                 return -1;
1504         } else {
1505                 printf("Profile Name : %s\n", profile_name);
1506                 g_free(profile_name);
1507         }
1508
1509         if (connection_profile_get_state(profile, &profile_state) != CONNECTION_ERROR_NONE) {
1510                 printf("Fail to get profile IPv4 state\n");
1511                 return -1;
1512         } else
1513                 printf("Profile State : %s\n", test_print_state(profile_state));
1514
1515         if (connection_profile_get_ipv6_state(profile, &profile_ipv6_state) != CONNECTION_ERROR_NONE) {
1516                 printf("Fail to get profile IPv6 state\n");
1517                 return -1;
1518         } else
1519                 printf("Profile IPv6 State : %s\n", test_print_state(profile_ipv6_state));
1520
1521
1522         if (connection_profile_get_type(profile, &prof_type) != CONNECTION_ERROR_NONE)
1523                 return -1;
1524
1525         test_get_user_int("Input Address Family (0:IPv4 1:IPv6) :", &address_family);
1526
1527         switch (prof_type) {
1528         case CONNECTION_PROFILE_TYPE_CELLULAR:
1529                 printf("Profile Type : Cellular\n");
1530                 test_print_cellular_info(profile);
1531                 break;
1532         case CONNECTION_PROFILE_TYPE_WIFI:
1533                 printf("Profile Type : Wi-Fi\n");
1534                 test_print_wifi_info(profile);
1535                 break;
1536         case CONNECTION_PROFILE_TYPE_ETHERNET:
1537                 printf("Profile Type : Ethernet\n");
1538                 break;
1539         case CONNECTION_PROFILE_TYPE_BT:
1540                 printf("Profile Type : Bluetooth\n");
1541                 break;
1542         default:
1543                 return -1;
1544         }
1545
1546         test_print_network_info(profile, address_family);
1547
1548         return 1;
1549 }
1550
1551 int test_refresh_profile_info(void)
1552 {
1553         connection_profile_type_e prof_type;
1554         connection_profile_state_e profile_state;
1555         connection_profile_h profile;
1556         char *profile_name = NULL;
1557         int address_family = 0;
1558
1559         printf("\n** Choose a profile to refresh. **\n");
1560         if (test_get_user_selected_profile(&profile, true) == false)
1561                 return -1;
1562
1563         if (connection_profile_refresh(profile) != CONNECTION_ERROR_NONE)
1564                 return -1;
1565
1566         if (connection_profile_get_name(profile, &profile_name) != CONNECTION_ERROR_NONE) {
1567                 printf("Fail to get profile name\n");
1568                 return -1;
1569         } else {
1570                 printf("Profile Name : %s\n", profile_name);
1571                 g_free(profile_name);
1572         }
1573
1574         if (connection_profile_get_state(profile, &profile_state) != CONNECTION_ERROR_NONE) {
1575                 printf("Fail to get profile state\n");
1576                 return -1;
1577         } else
1578                 printf("Profile State : %s\n", test_print_state(profile_state));
1579
1580
1581         if (connection_profile_get_type(profile, &prof_type) != CONNECTION_ERROR_NONE)
1582                 return -1;
1583
1584         test_get_user_int("Input Address Family (0:IPv4 1:IPv6) :", &address_family);
1585
1586         switch (prof_type) {
1587         case CONNECTION_PROFILE_TYPE_CELLULAR:
1588                 printf("Profile Type : Cellular\n");
1589                 test_print_cellular_info(profile);
1590                 break;
1591         case CONNECTION_PROFILE_TYPE_WIFI:
1592                 printf("Profile Type : Wi-Fi\n");
1593                 test_print_wifi_info(profile);
1594                 break;
1595         case CONNECTION_PROFILE_TYPE_ETHERNET:
1596                 printf("Profile Type : Ethernet\n");
1597                 break;
1598         case CONNECTION_PROFILE_TYPE_BT:
1599                 printf("Profile Type : Bluetooth\n");
1600                 break;
1601         default:
1602                 return -1;
1603         }
1604
1605         test_print_network_info(profile, address_family);
1606
1607         return 1;
1608 }
1609
1610 int test_set_state_changed_callback()
1611 {
1612         connection_profile_h profile;
1613         connection_profile_h profile_clone;
1614
1615         printf("\n** Choose a profile to set callback. **\n");
1616         if (test_get_user_selected_profile(&profile, true) == false)
1617                 return -1;
1618
1619         if (connection_profile_clone(&profile_clone, profile) != CONNECTION_ERROR_NONE)
1620                 return -1;
1621
1622         if (connection_profile_set_state_changed_cb(profile,
1623                         test_profile_state_callback, profile_clone) != CONNECTION_ERROR_NONE) {
1624                 connection_profile_destroy(profile_clone);
1625                 return -1;
1626         }
1627
1628         state_cb_list = g_slist_append(state_cb_list, profile_clone);
1629
1630         return 1;
1631 }
1632
1633 int test_unset_state_changed_callback()
1634 {
1635         connection_profile_h profile;
1636         GSList *list;
1637         char *profile_name = NULL;
1638         int count = 0;
1639         int input = 0;
1640
1641         printf("\n** Choose a profile to unset callback. **\n");
1642         for (list = state_cb_list; list; list = list->next) {
1643                 profile = list->data;
1644                 if (connection_profile_get_name(profile, &profile_name) != CONNECTION_ERROR_NONE) {
1645                         printf("Fail to get profile name!\n");
1646                         return -1;
1647                 } else {
1648                         printf("%d. %s\n", count, profile_name);
1649                         g_free(profile_name);
1650                 }
1651
1652                 count++;
1653         }
1654
1655         if (test_get_user_int("Input profile number(Enter for cancel) :", &input) == false ||
1656             input >= count ||
1657             input < 0) {
1658                 printf("Wrong number!!\n");
1659                 return -1;
1660         }
1661
1662         count = 0;
1663         for (list = state_cb_list; list; list = list->next) {
1664                 if (count == input) {
1665                         profile = list->data;
1666                         goto unset;
1667                 }
1668
1669                 count++;
1670         }
1671
1672         return -1;
1673
1674 unset:
1675         if (connection_profile_unset_state_changed_cb(profile) != CONNECTION_ERROR_NONE)
1676                 return -1;
1677
1678         state_cb_list = g_slist_remove(state_cb_list, profile);
1679         connection_profile_destroy(profile);
1680
1681         return 1;
1682 }
1683
1684 int test_reset_call_statistics_info(void)
1685 {
1686         int ret = CONNECTION_ERROR_NONE;
1687
1688         ret = connection_reset_statistics(connection, CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_LAST_RECEIVED_DATA);
1689         printf("reset last recv data size [%d]\n", ret);
1690         ret = connection_reset_statistics(connection, CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_LAST_SENT_DATA);
1691         printf("last sent data size [%d]\n", ret);
1692         ret = connection_reset_statistics(connection, CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_TOTAL_RECEIVED_DATA);
1693         printf("total received data size [%d]\n", ret);
1694         ret = connection_reset_statistics(connection, CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_TOTAL_SENT_DATA);
1695         printf("total sent data size [%d]\n", ret);
1696
1697         return 1;
1698 }
1699
1700 int test_reset_wifi_call_statistics_info(void)
1701 {
1702         int ret = CONNECTION_ERROR_NONE;
1703
1704         ret = connection_reset_statistics(connection, CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_LAST_SENT_DATA);
1705         printf("WiFi last sent data size [%d]\n", ret);
1706         ret = connection_reset_statistics(connection, CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_LAST_RECEIVED_DATA);
1707         printf("WiFi last recv data size [%d]\n", ret);
1708         ret = connection_reset_statistics(connection, CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_TOTAL_SENT_DATA);
1709         printf("WiFi total sent data size [%d]\n", ret);
1710         ret = connection_reset_statistics(connection, CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_TOTAL_RECEIVED_DATA);
1711         printf("WiFi total received data size [%d]\n", ret);
1712
1713         return 1;
1714 }
1715
1716 int test_add_route(void)
1717 {
1718         int rv = 0;
1719         char ip_addr[100] = {0};
1720         char if_name[40] = {0};
1721
1722         if (test_get_user_string("Input IP - (Enter for skip) :", ip_addr, 100) == false)
1723                 return -1;
1724
1725         if (test_get_user_string("Input Interface name - (Enter for skip) :", if_name, 40) == false)
1726                 return -1;
1727
1728         g_strstrip(ip_addr);
1729         g_strstrip(if_name);
1730         rv = connection_add_route(connection, if_name, ip_addr);
1731         if (rv != CONNECTION_ERROR_NONE) {
1732                 printf("Fail to get add new route [%d]\n", rv);
1733                 return -1;
1734         }
1735         printf("Add Route successfully\n");
1736
1737         return 1;
1738 }
1739
1740 int test_remove_route(void)
1741 {
1742         int rv = 0;
1743         char ip_addr[100] = {0};
1744         char if_name[40] = {0};
1745
1746         if (test_get_user_string("Input IP - (Enter for skip) :", ip_addr, 100) == false)
1747                 return -1;
1748
1749         if (test_get_user_string("Input Interface name - (Enter for skip) :", if_name, 40) == false)
1750                 return -1;
1751
1752         g_strstrip(ip_addr);
1753         g_strstrip(if_name);
1754         rv = connection_remove_route(connection, if_name, ip_addr);
1755         if (rv != CONNECTION_ERROR_NONE) {
1756                 printf("Fail to remove the route [%s]\n", test_print_error(rv));
1757                 return -1;
1758         }
1759         printf("Remove Route successfully\n");
1760
1761         return 1;
1762 }
1763
1764 int test_add_route_ipv6(void)
1765 {
1766         int rv = 0;
1767         char ip_addr[100] = {0};
1768         char gateway[100] = {0};
1769         char if_name[40] = {0};
1770
1771         if (test_get_user_string("Input IPv6 - (Enter for skip) :", ip_addr, 100) == false)
1772                 return -1;
1773
1774         if (test_get_user_string("Input Gateway - (Enter for skip) :", gateway, 100) == false)
1775                 return -1;
1776
1777         if (test_get_user_string("Input Interface name - (Enter for skip) :", if_name, 40) == false)
1778                 return -1;
1779
1780         g_strstrip(ip_addr);
1781         g_strstrip(gateway);
1782         g_strstrip(if_name);
1783         rv = connection_add_route_ipv6(connection, if_name, ip_addr, gateway);
1784         if (rv != CONNECTION_ERROR_NONE) {
1785                 printf("Fail to get add new route [%d]\n", rv);
1786                 return -1;
1787         }
1788         printf("Add Route successfully\n");
1789
1790         return 1;
1791 }
1792
1793 int test_remove_route_ipv6(void)
1794 {
1795         int rv = 0;
1796         char ip_addr[100] = {0};
1797         char gateway[100] = {0};
1798         char if_name[40] = {0};
1799
1800         if (test_get_user_string("Input IPv6 - (Enter for skip) :", ip_addr, 100) == false)
1801                 return -1;
1802
1803         if (test_get_user_string("Input Gateway - (Enter for skip) :", gateway, 100) == false)
1804                 return -1;
1805
1806         if (test_get_user_string("Input Interface name - (Enter for skip) :", if_name, 40) == false)
1807                 return -1;
1808
1809         g_strstrip(ip_addr);
1810         g_strstrip(gateway);
1811         g_strstrip(if_name);
1812         rv = connection_remove_route_ipv6(connection, if_name, ip_addr, gateway);
1813         if (rv != CONNECTION_ERROR_NONE) {
1814                 printf("Fail to remove the route [%d]\n", rv);
1815                 return -1;
1816         }
1817         printf("Remove Route successfully\n");
1818
1819         return 1;
1820 }
1821
1822 int test_add_route_entry(void)
1823 {
1824         char ip_addr[100] = {0};
1825         char gateway[100] = {0};
1826         char if_name[40] = {0};
1827         int input;
1828         bool input_rv;
1829         int rv = 0;
1830
1831         input_rv = test_get_user_int("Input Address type to get"
1832                 "(1:IPV4, 2:IPV6):", &input);
1833
1834         if (input_rv == false) {
1835                 printf("Invalid input!!\n");
1836                 return -1;
1837         }
1838
1839         switch (input) {
1840         case 1:
1841                 if (test_get_user_string("Input Interface name - (Enter for skip) :", if_name, 40) == false)
1842                         return -1;
1843
1844                 if (test_get_user_string("Input IP - (Enter for skip) :", ip_addr, 100) == false)
1845                         return -1;
1846
1847                 if (test_get_user_string("Input Gateway - (Enter for skip) :", gateway, 100) == false)
1848                         return -1;
1849
1850                 g_strstrip(ip_addr);
1851                 g_strstrip(gateway);
1852                 g_strstrip(if_name);
1853                 rv = connection_add_route_entry(connection, CONNECTION_ADDRESS_FAMILY_IPV4, if_name, ip_addr, gateway);
1854                 if (rv != CONNECTION_ERROR_NONE) {
1855                         printf("Fail to get add new route [%d]\n", rv);
1856                         return -1;
1857                 }
1858                 printf("Add Route successfully\n");
1859                 break;
1860
1861         case 2:
1862                 if (test_get_user_string("Input Interface name - (Enter for skip) :", if_name, 40) == false)
1863                         return -1;
1864
1865                 if (test_get_user_string("Input IP - (Enter for skip) :", ip_addr, 100) == false)
1866                         return -1;
1867
1868                 if (test_get_user_string("Input Gateway - (Enter for skip) :", gateway, 100) == false)
1869                         return -1;
1870
1871                 g_strstrip(ip_addr);
1872                 g_strstrip(gateway);
1873                 g_strstrip(if_name);
1874                 rv = connection_add_route_entry(connection, CONNECTION_ADDRESS_FAMILY_IPV6, if_name, ip_addr, gateway);
1875                 if (rv != CONNECTION_ERROR_NONE) {
1876                         printf("Fail to get add new route [%d]\n", rv);
1877                         return -1;
1878                 }
1879                 printf("Add Route successfully\n");
1880                 break;
1881
1882         default:
1883                 printf("Wrong IP address family!!\n");
1884                 return -1;
1885
1886         }
1887
1888         return 1;
1889
1890 }
1891
1892 int test_remove_route_entry(void)
1893 {
1894         char ip_addr[100] = {0};
1895         char gateway[100] = {0};
1896         char if_name[40] = {0};
1897         int input;
1898         bool input_rv;
1899         int rv = 0;
1900
1901         input_rv = test_get_user_int("Input Address type to get"
1902                 "(1:IPV4, 2:IPV6):", &input);
1903
1904         if (input_rv == false) {
1905                 printf("Invalid input!!\n");
1906                 return -1;
1907         }
1908
1909         switch (input) {
1910         case 1:
1911                 if (test_get_user_string("Input Interface name - (Enter for skip) :", if_name, 40) == false)
1912                         return -1;
1913
1914                 if (test_get_user_string("Input IP - (Enter for skip) :", ip_addr, 100) == false)
1915                         return -1;
1916
1917                 if (test_get_user_string("Input Gateway - (Enter for skip) :", gateway, 100) == false)
1918                         return -1;
1919
1920                 g_strstrip(ip_addr);
1921                 g_strstrip(gateway);
1922                 g_strstrip(if_name);
1923                 rv = connection_remove_route_entry(connection, CONNECTION_ADDRESS_FAMILY_IPV4, if_name, ip_addr, gateway);
1924                 if (rv != CONNECTION_ERROR_NONE) {
1925                         printf("Fail to remove the route [%s]\n", test_print_error(rv));
1926                         return -1;
1927                 }
1928                 printf("Remove Route successfully\n");
1929
1930                 break;
1931
1932         case 2:
1933                 if (test_get_user_string("Input Interface name - (Enter for skip) :", if_name, 40) == false)
1934                         return -1;
1935
1936                 if (test_get_user_string("Input IP - (Enter for skip) :", ip_addr, 100) == false)
1937                         return -1;
1938
1939                 if (test_get_user_string("Input Gateway - (Enter for skip) :", gateway, 100) == false)
1940                         return -1;
1941
1942                 g_strstrip(ip_addr);
1943                 g_strstrip(gateway);
1944                 g_strstrip(if_name);
1945                 rv = connection_remove_route_entry(connection, CONNECTION_ADDRESS_FAMILY_IPV6, if_name, ip_addr, gateway);
1946                 if (rv != CONNECTION_ERROR_NONE) {
1947                         printf("Fail to remove the route [%d]\n", rv);
1948                         return -1;
1949                 }
1950                 printf("Remove Route successfully\n");
1951                 break;
1952
1953         default:
1954                 printf("Wrong IP address family!!\n");
1955                 return -1;
1956
1957         }
1958
1959         return 1;
1960
1961 }
1962
1963 int test_get_bt_state(void)
1964 {
1965         int rv = 0;
1966         connection_bt_state_e bt_state;
1967
1968         rv = connection_get_bt_state(connection, &bt_state);
1969
1970         if (rv != CONNECTION_ERROR_NONE) {
1971                 printf("Fail to get Bluetooth state [%s]\n", test_print_error(rv));
1972                 return -1;
1973         }
1974
1975         printf("Retval = [%s], Bluetooth state [%d]\n", test_print_error(rv), bt_state);
1976
1977         return 1;
1978 }
1979
1980 int test_get_profile_id(void)
1981 {
1982         connection_profile_h profile;
1983         char *profile_id;
1984
1985         printf("\n** Choose a profile to see profile id. **\n");
1986         if (test_get_user_selected_profile(&profile, true) == false)
1987                 return -1;
1988
1989         if (connection_profile_get_id(profile, &profile_id) != CONNECTION_ERROR_NONE) {
1990                 printf("Fail to get profile name\n");
1991                 return -1;
1992         } else {
1993                 printf("Profile id : %s\n", profile_id);
1994                 g_free(profile_id);
1995         }
1996
1997         return 1;
1998 }
1999
2000 int test_get_mac_address(void)
2001 {
2002         int rv = 0, type = 0;
2003         connection_type_e conn_type;
2004         char *mac_addr = NULL;
2005
2006         test_get_user_int("Input connection type (1:wifi, 2:ethernet)", &type);
2007
2008         switch (type) {
2009         case 1:
2010                 conn_type = CONNECTION_TYPE_WIFI;
2011                 break;
2012         case 2:
2013                 conn_type = CONNECTION_TYPE_ETHERNET;
2014                 break;
2015         default:
2016                 printf("Wrong number!!\n");
2017                 return -1;
2018         }
2019
2020         rv = connection_get_mac_address(connection, conn_type, &mac_addr);
2021
2022         if (rv != CONNECTION_ERROR_NONE) {
2023                 printf("Fail to get MAC address [%s]\n", test_print_error(rv));
2024                 return -1;
2025         }
2026
2027         printf("mac address is %s\n", mac_addr);
2028
2029         g_free(mac_addr);
2030
2031         return 1;
2032 }
2033
2034 int test_get_ethernet_cable_state(void)
2035 {
2036         int rv = 0;
2037         connection_ethernet_cable_state_e cable_state;
2038
2039         rv = connection_get_ethernet_cable_state(connection, &cable_state);
2040
2041         if (rv != CONNECTION_ERROR_NONE) {
2042                 printf("Fail to get ethernet cable state [%s]\n", test_print_error(rv));
2043                 return -1;
2044         }
2045
2046         printf("Retval = [%s], Ethernet cable state [%d]\n", test_print_error(rv), cable_state);
2047
2048         return 1;
2049 }
2050
2051 int test_reset_profile(void)
2052 {
2053         int type, sim_id, rv;
2054
2055         rv = test_get_user_int("Input reset type (0:default profile reset, 1:delete profile reset)", &type);
2056
2057         if (rv == false || (type != 0 && type != 1)) {
2058                 printf("Invalid input!!\n");
2059                 return -1;
2060         }
2061
2062         rv = test_get_user_int("Input SIM id to reset (0:SIM1, 1:SIM2)", &sim_id);
2063
2064         if (rv == false || (sim_id != 0 && sim_id != 1)) {
2065                 printf("Invalid input!!\n");
2066                 return -1;
2067         }
2068
2069         if (connection_reset_profile(connection, type, sim_id, test_connection_reset_profile_callback, NULL) != CONNECTION_ERROR_NONE)
2070                 return -1;
2071
2072         return 1;
2073 }
2074
2075 static bool test_get_ipv6_address_callback(char *ipv6_address, void* user_data)
2076 {
2077         printf("IPv6 Address : %s\n", ipv6_address);
2078         return true;
2079 }
2080
2081 int test_foreach_ipv6_address(void)
2082 {
2083         int rv = 0;
2084         int type;
2085         connection_type_e conn_type;
2086
2087         test_get_user_int("Input Connection Type(1: WiFi 2: Ethernet) :", &type);
2088
2089         switch (type) {
2090         case 1:
2091                 conn_type = CONNECTION_TYPE_WIFI;
2092                 break;
2093         case 2:
2094                 conn_type = CONNECTION_TYPE_ETHERNET;
2095                 break;
2096         default:
2097                 printf("Wrong number!!\n");
2098                 return -1;
2099         }
2100
2101         rv = connection_foreach_ipv6_address(connection, conn_type, test_get_ipv6_address_callback, NULL);
2102         if (rv != CONNECTION_ERROR_NONE) {
2103                 printf("Fail to get IPv6 address\n");
2104                 return -1;
2105         }
2106
2107         return 1;
2108 }
2109
2110 int test_is_metered_network(void)
2111 {
2112         int rv = 0;
2113         bool metered_state;
2114
2115         rv = connection_is_metered_network(connection, &metered_state);
2116
2117         if (rv != CONNECTION_ERROR_NONE) {
2118                 printf("Fail to get metered state [%s]\n", test_print_error(rv));
2119                 return -1;
2120         }
2121
2122         printf("Retval = [%s] metered state [%s]\n",
2123                 test_print_error(rv), metered_state ? "TRUE" : "FALSE");
2124
2125         return 1;
2126 }
2127
2128 int main(int argc, char **argv)
2129 {
2130         GMainLoop *mainloop;
2131         mainloop = g_main_loop_new(NULL, FALSE);
2132
2133         GIOChannel *channel = g_io_channel_unix_new(0);
2134         g_io_add_watch(channel, (G_IO_IN|G_IO_ERR|G_IO_HUP|G_IO_NVAL), test_thread, NULL);
2135
2136         printf("Test Thread created...\n");
2137
2138         g_main_loop_run(mainloop);
2139
2140         return 0;
2141 }
2142
2143 gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data)
2144 {
2145         int rv = 0;
2146         char a[100];
2147
2148         memset(a, '\0', 100);
2149         printf("Event received from stdin\n");
2150
2151         rv = read(0, a, 100);
2152
2153         if (rv < 0 || a[0] == '0') {
2154                 if (connection != NULL)
2155                         test_deregister_client();
2156
2157                 exit(1);
2158         }
2159
2160         if (*a == '\n' || *a == '\r') {
2161                 printf("\n\n Network Connection API Test App\n\n");
2162                 printf("Options..\n");
2163                 printf(LOG_GREEN "1   - Create Handle and set callbacks\n" LOG_END);
2164                 printf("2   - Destroy Handle(unset callbacks automatically)\n");
2165                 printf(LOG_GREEN "3   - Get network state\n" LOG_END);
2166                 printf(LOG_GREEN "4   - Get cellular state (please insert SIM Card)\n" LOG_END);
2167                 printf(LOG_GREEN "5   - Get wifi state (please turn on WiFi)\n" LOG_END);
2168                 printf("6   - Get current proxy address \n");
2169                 printf("7   - Get current Ip address\n");
2170                 printf("8   - Get cellular data call statistics\n");
2171                 printf("9   - Get WiFi data call statistics\n");
2172                 printf(LOG_GREEN "a   - Get Profile list\n" LOG_END);
2173                 printf(LOG_GREEN "b   - Get Connected Profile list\n" LOG_END);
2174                 printf(LOG_GREEN "c   - Get Current profile\n" LOG_END);
2175                 printf("d   - Open connection with profile\n");
2176                 printf("e   - Get default cellular service by type\n");
2177                 printf("f   - Set default cellular service by type\n");
2178                 printf("g   - Close connection with profile\n");
2179                 printf("h   - Add profile(Cellular and Wifi only)\n");
2180                 printf("i   - Remove profile(Cellular:delete, WiFi:forgot)\n");
2181                 printf("j   - Update profile\n");
2182                 printf("k   - Get profile info\n");
2183                 printf("l   - Refresh profile info\n");
2184                 printf("m   - Set state changed callback\n");
2185                 printf("n   - Unset state changed callback\n");
2186                 printf("o   - Reset cellular data call statistics\n");
2187                 printf("p   - Reset WiFi data call statistics\n");
2188                 printf("q   - Add new route\n");
2189                 printf("r   - Remove a route\n");
2190                 printf("s   - Get Bluetooth state\n");
2191                 printf("t   - Get profile id\n");
2192                 printf("u   - Reset profile\n");
2193                 printf("v   - Get all cellular default profiles\n");
2194                 printf("w   - Get mac address\n");
2195                 printf("x   - Get ethernet cable state\n");
2196                 printf("B   - Add IPv6 new route\n");
2197                 printf("C   - Remove IPv6 route\n");
2198                 printf("D   - Add new route entry\n");
2199                 printf("E   - Remove route entry\n");
2200                 printf("F   - Get all IPv6 address\n");
2201                 printf("G   - Get metered state\n");
2202                 printf(LOG_RED "0   - Exit \n" LOG_END);
2203                 printf("ENTER   - Show options menu.......\n");
2204         }
2205
2206         switch (a[0]) {
2207         case '1':
2208                 rv = test_register_client();
2209                 break;
2210         case '2':
2211                 rv = test_deregister_client();
2212                 break;
2213         case '3':
2214                 rv = test_get_network_state();
2215                 break;
2216         case '4':
2217                 rv = test_get_cellular_state();
2218                 break;
2219         case '5':
2220                 rv = test_get_wifi_state();
2221                 break;
2222         case '6':
2223                 rv = test_get_current_proxy();
2224                 break;
2225         case '7':
2226                 rv = test_get_current_ip();
2227                 break;
2228         case '8':
2229                 rv = test_get_call_statistics_info();
2230                 break;
2231         case '9':
2232                 rv = test_get_wifi_call_statistics_info();
2233                 break;
2234         case 'a':
2235                 rv = test_get_profile_list();
2236                 break;
2237         case 'b':
2238                 rv = test_get_connected_profile_list();
2239                 break;
2240         case 'c':
2241                 rv = test_get_current_profile();
2242                 break;
2243         case 'd':
2244                 rv = test_open_profile();
2245                 break;
2246         case 'e':
2247                 rv = test_get_default_cellular_service_type();
2248                 break;
2249         case 'f':
2250                 rv = test_set_default_cellular_service_type();
2251                 break;
2252         case 'g':
2253                 rv = test_close_profile();
2254                 break;
2255         case 'h':
2256                 rv = test_add_profile();
2257                 break;
2258         case 'i':
2259                 rv = test_remove_profile();
2260                 break;
2261         case 'j':
2262                 rv = test_update_profile();
2263                 break;
2264         case 'k':
2265                 rv = test_get_profile_info();
2266                 break;
2267         case 'l':
2268                 rv = test_refresh_profile_info();
2269                 break;
2270         case 'm':
2271                 rv = test_set_state_changed_callback();
2272                 break;
2273         case 'n':
2274                 rv = test_unset_state_changed_callback();
2275                 break;
2276         case 'o':
2277                 rv = test_reset_call_statistics_info();
2278                 break;
2279         case 'p':
2280                 rv = test_reset_wifi_call_statistics_info();
2281                 break;
2282         case 'q':
2283                 rv = test_add_route();
2284                 break;
2285         case 'r':
2286                 rv = test_remove_route();
2287                 break;
2288         case 's':
2289                 rv = test_get_bt_state();
2290                 break;
2291         case 't':
2292                 rv = test_get_profile_id();
2293                 break;
2294         case 'u':
2295                 rv = test_reset_profile();
2296                 break;
2297         case 'v':
2298                 rv = test_get_default_profile_list();
2299                 break;
2300         case 'w':
2301                 rv = test_get_mac_address();
2302                 break;
2303         case 'x':
2304                 rv = test_get_ethernet_cable_state();
2305                 break;
2306         case 'B':
2307                 rv = test_add_route_ipv6();
2308                 break;
2309         case 'C':
2310                 rv = test_remove_route_ipv6();
2311                 break;
2312         case 'D':
2313                 rv = test_add_route_entry();
2314                 break;
2315         case 'E':
2316                 rv = test_remove_route_entry();
2317                 break;
2318         case 'F':
2319                 rv = test_foreach_ipv6_address();
2320                 break;
2321         case 'G':
2322                 rv = test_is_metered_network();
2323                 break;
2324         }
2325
2326         if (rv == 1)
2327                 printf("Operation succeeded!\n");
2328         else
2329                 printf("Operation failed!\n");
2330
2331         return TRUE;
2332 }