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