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