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