Merge 2.3 code for sync
[platform/core/api/connection.git] / src / connection.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 <string.h>
19 #include <glib.h>
20 #include <vconf/vconf.h>
21 #include "net_connection_private.h"
22
23 static GSList *conn_handle_list = NULL;
24
25 static void __connection_cb_state_change_cb(keynode_t *node, void *user_data);
26 static void __connection_cb_ip_change_cb(keynode_t *node, void *user_data);
27 static void __connection_cb_proxy_change_cb(keynode_t *node, void *user_data);
28
29 static int __connection_convert_net_state(int status)
30 {
31         switch (status) {
32         case VCONFKEY_NETWORK_CELLULAR:
33                 return CONNECTION_TYPE_CELLULAR;
34         case VCONFKEY_NETWORK_WIFI:
35                 return CONNECTION_TYPE_WIFI;
36         case VCONFKEY_NETWORK_ETHERNET:
37                 return CONNECTION_TYPE_ETHERNET;
38         case VCONFKEY_NETWORK_BLUETOOTH:
39                 return CONNECTION_TYPE_BT;
40         default:
41                 return CONNECTION_TYPE_DISCONNECTED;
42         }
43 }
44
45 static int __connection_convert_cellular_state(int status)
46 {
47         switch (status) {
48         case VCONFKEY_NETWORK_CELLULAR_ON:
49                 return CONNECTION_CELLULAR_STATE_AVAILABLE;
50         case VCONFKEY_NETWORK_CELLULAR_3G_OPTION_OFF:
51                 return CONNECTION_CELLULAR_STATE_CALL_ONLY_AVAILABLE;
52         case VCONFKEY_NETWORK_CELLULAR_ROAMING_OFF:
53                 return CONNECTION_CELLULAR_STATE_ROAMING_OFF;
54         case VCONFKEY_NETWORK_CELLULAR_FLIGHT_MODE:
55                 return CONNECTION_CELLULAR_STATE_FLIGHT_MODE;
56         default:
57                 return CONNECTION_CELLULAR_STATE_OUT_OF_SERVICE;
58         }
59 }
60
61 static int __connection_get_type_changed_callback_count(void)
62 {
63         GSList *list;
64         int count = 0;
65
66         for (list = conn_handle_list; list; list = list->next) {
67                 connection_handle_s *local_handle = (connection_handle_s *)list->data;
68                 if (local_handle->type_changed_callback) count++;
69         }
70
71         return count;
72 }
73
74 static int __connection_get_ip_changed_callback_count(void)
75 {
76         GSList *list;
77         int count = 0;
78
79         for (list = conn_handle_list; list; list = list->next) {
80                 connection_handle_s *local_handle = (connection_handle_s *)list->data;
81                 if (local_handle->ip_changed_callback) count++;
82         }
83
84         return count;
85 }
86
87 static int __connection_get_proxy_changed_callback_count(void)
88 {
89         GSList *list;
90         int count = 0;
91
92         for (list = conn_handle_list; list; list = list->next) {
93                 connection_handle_s *local_handle = (connection_handle_s *)list->data;
94                 if (local_handle->proxy_changed_callback) count++;
95         }
96
97         return count;
98 }
99
100 static int __connection_set_type_changed_callback(connection_h connection,
101                                                         void *callback, void *user_data)
102 {
103         connection_handle_s *local_handle = (connection_handle_s *)connection;
104
105         if (callback) {
106                 if (__connection_get_type_changed_callback_count() == 0)
107                         if (vconf_notify_key_changed(VCONFKEY_NETWORK_STATUS ,
108                                         __connection_cb_state_change_cb, NULL))
109                                 return CONNECTION_ERROR_OPERATION_FAILED;
110
111                 local_handle->state_changed_user_data = user_data;
112         } else {
113                 if (local_handle->type_changed_callback &&
114                     __connection_get_type_changed_callback_count() == 1)
115                         if (vconf_ignore_key_changed(VCONFKEY_NETWORK_STATUS,
116                                         __connection_cb_state_change_cb))
117                                 return CONNECTION_ERROR_OPERATION_FAILED;
118         }
119
120         local_handle->type_changed_callback = callback;
121         return CONNECTION_ERROR_NONE;
122 }
123
124 static int __connection_set_ip_changed_callback(connection_h connection,
125                                                         void *callback, void *user_data)
126 {
127         connection_handle_s *local_handle = (connection_handle_s *)connection;
128
129         if (callback) {
130                 if (__connection_get_ip_changed_callback_count() == 0)
131                         if (vconf_notify_key_changed(VCONFKEY_NETWORK_IP,
132                                         __connection_cb_ip_change_cb, NULL))
133                                 return CONNECTION_ERROR_OPERATION_FAILED;
134
135                 local_handle->ip_changed_user_data = user_data;
136         } else {
137                 if (local_handle->ip_changed_callback &&
138                     __connection_get_ip_changed_callback_count() == 1)
139                         if (vconf_ignore_key_changed(VCONFKEY_NETWORK_IP,
140                                         __connection_cb_ip_change_cb))
141                                 return CONNECTION_ERROR_OPERATION_FAILED;
142         }
143
144         local_handle->ip_changed_callback = callback;
145         return CONNECTION_ERROR_NONE;
146 }
147
148 static int __connection_set_proxy_changed_callback(connection_h connection,
149                                                         void *callback, void *user_data)
150 {
151         connection_handle_s *local_handle = (connection_handle_s *)connection;
152
153         if (callback) {
154                 if (__connection_get_proxy_changed_callback_count() == 0)
155                         if (vconf_notify_key_changed(VCONFKEY_NETWORK_PROXY,
156                                         __connection_cb_proxy_change_cb, NULL))
157                                 return CONNECTION_ERROR_OPERATION_FAILED;
158
159                 local_handle->proxy_changed_user_data = user_data;
160         } else {
161                 if (local_handle->proxy_changed_callback &&
162                     __connection_get_proxy_changed_callback_count() == 1)
163                         if (vconf_ignore_key_changed(VCONFKEY_NETWORK_PROXY,
164                                         __connection_cb_proxy_change_cb))
165                                 return CONNECTION_ERROR_OPERATION_FAILED;
166         }
167
168         local_handle->proxy_changed_callback = callback;
169         return CONNECTION_ERROR_NONE;
170 }
171
172 static void __connection_cb_state_change_cb(keynode_t *node, void *user_data)
173 {
174         CONNECTION_LOG(CONNECTION_INFO, "Net Status Changed Indication\n");
175
176         GSList *list;
177         int state = vconf_keynode_get_int(node);
178
179         for (list = conn_handle_list; list; list = list->next) {
180                 connection_handle_s *local_handle = (connection_handle_s *)list->data;
181                 if (local_handle->type_changed_callback)
182                         local_handle->type_changed_callback(
183                                         __connection_convert_net_state(state),
184                                         local_handle->state_changed_user_data);
185         }
186 }
187
188 static void __connection_cb_ip_change_cb(keynode_t *node, void *user_data)
189 {
190         CONNECTION_LOG(CONNECTION_INFO, "Net IP Changed Indication\n");
191
192         GSList *list;
193         char *ip_addr = vconf_keynode_get_str(node);
194
195         for (list = conn_handle_list; list; list = list->next) {
196                 connection_handle_s *local_handle = (connection_handle_s *)list->data;
197                 if (local_handle->ip_changed_callback)
198                         local_handle->ip_changed_callback(
199                                         ip_addr, NULL,
200                                         local_handle->ip_changed_user_data);
201         }
202 }
203
204 static void __connection_cb_proxy_change_cb(keynode_t *node, void *user_data)
205 {
206         CONNECTION_LOG(CONNECTION_INFO, "Net IP Changed Indication\n");
207
208         GSList *list;
209         char *proxy = vconf_keynode_get_str(node);
210
211         for (list = conn_handle_list; list; list = list->next) {
212                 connection_handle_s *local_handle = (connection_handle_s *)list->data;
213                 if (local_handle->proxy_changed_callback)
214                         local_handle->proxy_changed_callback(
215                                         proxy, NULL,
216                                         local_handle->proxy_changed_user_data);
217         }
218 }
219
220 static bool __connection_check_handle_validity(connection_h connection)
221 {
222         GSList *list;
223
224         for (list = conn_handle_list; list; list = list->next)
225                 if (connection == list->data) return true;
226
227         return false;
228 }
229
230 static int __connection_get_handle_count(void)
231 {
232         GSList *list;
233         int count = 0;
234
235         if (!conn_handle_list)
236                 return count;
237
238         for (list = conn_handle_list; list; list = list->next) count++;
239
240         return count;
241 }
242
243 /* Connection Manager ********************************************************/
244 EXPORT_API int connection_create(connection_h* connection)
245 {
246         CONNECTION_MUTEX_LOCK;
247
248         if (connection == NULL || __connection_check_handle_validity(*connection)) {
249                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
250                 CONNECTION_MUTEX_UNLOCK;
251                 return CONNECTION_ERROR_INVALID_PARAMETER;
252         }
253
254         if (_connection_libnet_init() == false) {
255                 CONNECTION_LOG(CONNECTION_ERROR, "Creation failed!\n");
256                 CONNECTION_MUTEX_UNLOCK;
257                 return CONNECTION_ERROR_OPERATION_FAILED;
258         }
259
260         CONNECTION_LOG(CONNECTION_ERROR, "Connection successfully created!\n");
261
262         *connection = g_try_malloc0(sizeof(connection_handle_s));
263         if (*connection != NULL) {
264                 CONNECTION_LOG(CONNECTION_INFO, "New Handle Created %p\n", *connection);
265         } else {
266                 CONNECTION_MUTEX_UNLOCK;
267                 return CONNECTION_ERROR_OUT_OF_MEMORY;
268         }
269
270         conn_handle_list = g_slist_append(conn_handle_list, *connection);
271
272         CONNECTION_MUTEX_UNLOCK;
273         return CONNECTION_ERROR_NONE;
274 }
275
276 EXPORT_API int connection_destroy(connection_h connection)
277 {
278         CONNECTION_MUTEX_LOCK;
279
280         if (connection == NULL || !(__connection_check_handle_validity(connection))) {
281                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
282                 CONNECTION_MUTEX_UNLOCK;
283                 return CONNECTION_ERROR_INVALID_PARAMETER;
284         }
285
286         CONNECTION_LOG(CONNECTION_INFO, "Destroy Handle : %p\n", connection);
287
288         __connection_set_type_changed_callback(connection, NULL, NULL);
289         __connection_set_ip_changed_callback(connection, NULL, NULL);
290         __connection_set_proxy_changed_callback(connection, NULL, NULL);
291
292         conn_handle_list = g_slist_remove(conn_handle_list, connection);
293
294         g_free(connection);
295
296         if (__connection_get_handle_count() == 0)
297                 _connection_libnet_deinit();
298
299         CONNECTION_MUTEX_UNLOCK;
300         return CONNECTION_ERROR_NONE;
301 }
302
303 EXPORT_API int connection_get_type(connection_h connection, connection_type_e* type)
304 {
305         int status = 0;
306
307         if (type == NULL || !(__connection_check_handle_validity(connection))) {
308                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
309                 return CONNECTION_ERROR_INVALID_PARAMETER;
310         }
311
312         if (vconf_get_int(VCONFKEY_NETWORK_STATUS, &status)) {
313                 CONNECTION_LOG(CONNECTION_ERROR, "vconf_get_int Failed = %d\n", status);
314                 return CONNECTION_ERROR_OPERATION_FAILED;
315         }
316
317         CONNECTION_LOG(CONNECTION_INFO, "Connected Network = %d\n", status);
318
319         *type = __connection_convert_net_state(status);
320
321         return CONNECTION_ERROR_NONE;
322 }
323
324 EXPORT_API int connection_get_ip_address(connection_h connection,
325                                 connection_address_family_e address_family, char** ip_address)
326 {
327         if (ip_address == NULL || !(__connection_check_handle_validity(connection))) {
328                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
329                 return CONNECTION_ERROR_INVALID_PARAMETER;
330         }
331
332         switch (address_family) {
333         case CONNECTION_ADDRESS_FAMILY_IPV4:
334                 *ip_address = vconf_get_str(VCONFKEY_NETWORK_IP);
335                 break;
336         case CONNECTION_ADDRESS_FAMILY_IPV6:
337                 CONNECTION_LOG(CONNECTION_ERROR, "Not supported yet\n");
338                 return CONNECTION_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED;
339                 break;
340         default:
341                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
342                 return CONNECTION_ERROR_INVALID_PARAMETER;
343         }
344
345         if (*ip_address == NULL) {
346                 CONNECTION_LOG(CONNECTION_ERROR, "vconf_get_str Failed\n");
347                 return CONNECTION_ERROR_OPERATION_FAILED;
348         }
349
350         return CONNECTION_ERROR_NONE;
351 }
352
353 EXPORT_API int connection_get_proxy(connection_h connection,
354                                 connection_address_family_e address_family, char** proxy)
355 {
356         if (proxy == NULL || !(__connection_check_handle_validity(connection))) {
357                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
358                 return CONNECTION_ERROR_INVALID_PARAMETER;
359         }
360
361         switch (address_family) {
362         case CONNECTION_ADDRESS_FAMILY_IPV4:
363                 *proxy = vconf_get_str(VCONFKEY_NETWORK_PROXY);
364                 break;
365         case CONNECTION_ADDRESS_FAMILY_IPV6:
366                 CONNECTION_LOG(CONNECTION_ERROR, "Not supported yet\n");
367                 return CONNECTION_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED;
368                 break;
369         default:
370                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
371                 return CONNECTION_ERROR_INVALID_PARAMETER;
372         }
373
374         if (*proxy == NULL) {
375                 CONNECTION_LOG(CONNECTION_ERROR, "vconf_get_str Failed\n");
376                 return CONNECTION_ERROR_OPERATION_FAILED;
377         }
378
379         return CONNECTION_ERROR_NONE;
380 }
381
382 EXPORT_API int connection_get_cellular_state(connection_h connection, connection_cellular_state_e* state)
383 {
384         int status = 0;
385         int cellular_state = 0;
386
387         if (state == NULL || !(__connection_check_handle_validity(connection))) {
388                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
389                 return CONNECTION_ERROR_INVALID_PARAMETER;
390         }
391
392         if (!vconf_get_int(VCONFKEY_NETWORK_CELLULAR_STATE, &status)) {
393                 CONNECTION_LOG(CONNECTION_INFO, "Cellular = %d\n", status);
394                 *state = __connection_convert_cellular_state(status);
395
396                 if (*state == CONNECTION_CELLULAR_STATE_AVAILABLE) {
397                         if (vconf_get_int(VCONFKEY_DNET_STATE, &cellular_state)) {
398                                 CONNECTION_LOG(CONNECTION_ERROR,
399                                                 "vconf_get_int Failed = %d\n", cellular_state);
400                                 return CONNECTION_ERROR_OPERATION_FAILED;
401                         }
402                 }
403
404                 CONNECTION_LOG(CONNECTION_INFO, "Connection state = %d\n", cellular_state);
405
406                 if (cellular_state == VCONFKEY_DNET_NORMAL_CONNECTED ||
407                     cellular_state == VCONFKEY_DNET_SECURE_CONNECTED ||
408                     cellular_state == VCONFKEY_DNET_TRANSFER)
409                         *state = CONNECTION_CELLULAR_STATE_CONNECTED;
410
411                 return CONNECTION_ERROR_NONE;
412         } else {
413                 CONNECTION_LOG(CONNECTION_ERROR, "vconf_get_int Failed = %d\n", status);
414                 return CONNECTION_ERROR_OPERATION_FAILED;
415         }
416 }
417
418 EXPORT_API int connection_get_wifi_state(connection_h connection, connection_wifi_state_e* state)
419 {
420         int rv;
421
422         if (state == NULL || !(__connection_check_handle_validity(connection))) {
423                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
424                 return CONNECTION_ERROR_INVALID_PARAMETER;
425         }
426
427         rv = _connection_libnet_get_wifi_state(state);
428         if (rv != CONNECTION_ERROR_NONE) {
429                 CONNECTION_LOG(CONNECTION_ERROR, "Fail to get Wi-Fi state[%d]", rv);
430                 return rv;
431         }
432
433         CONNECTION_LOG(CONNECTION_INFO, "WiFi state = %d\n", *state);
434
435         return CONNECTION_ERROR_NONE;
436 }
437
438 EXPORT_API int connection_get_ethernet_state(connection_h connection, connection_ethernet_state_e* state)
439 {
440         if (state == NULL || !(__connection_check_handle_validity(connection))) {
441                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
442                 return CONNECTION_ERROR_INVALID_PARAMETER;
443         }
444
445         return _connection_libnet_get_ethernet_state(state);
446 }
447
448 EXPORT_API int connection_get_bt_state(connection_h connection, connection_bt_state_e* state)
449 {
450         if (state == NULL || !(__connection_check_handle_validity(connection))) {
451                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
452                 return CONNECTION_ERROR_INVALID_PARAMETER;
453         }
454
455         return _connection_libnet_get_bluetooth_state(state);
456
457 }
458
459 EXPORT_API int connection_set_type_changed_cb(connection_h connection,
460                                         connection_type_changed_cb callback, void* user_data)
461 {
462         if (callback == NULL || !(__connection_check_handle_validity(connection))) {
463                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
464                 return CONNECTION_ERROR_INVALID_PARAMETER;
465         }
466
467         return __connection_set_type_changed_callback(connection, callback, user_data);
468 }
469
470 EXPORT_API int connection_unset_type_changed_cb(connection_h connection)
471 {
472         if (!(__connection_check_handle_validity(connection))) {
473                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
474                 return CONNECTION_ERROR_INVALID_PARAMETER;
475         }
476
477         return __connection_set_type_changed_callback(connection, NULL, NULL);
478 }
479
480 EXPORT_API int connection_set_ip_address_changed_cb(connection_h connection,
481                                 connection_address_changed_cb callback, void* user_data)
482 {
483         if (callback == NULL || !(__connection_check_handle_validity(connection))) {
484                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
485                 return CONNECTION_ERROR_INVALID_PARAMETER;
486         }
487
488         return __connection_set_ip_changed_callback(connection, callback, user_data);
489 }
490
491 EXPORT_API int connection_unset_ip_address_changed_cb(connection_h connection)
492 {
493         if (!(__connection_check_handle_validity(connection))) {
494                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
495                 return CONNECTION_ERROR_INVALID_PARAMETER;
496         }
497
498         return __connection_set_ip_changed_callback(connection, NULL, NULL);
499 }
500
501 EXPORT_API int connection_set_proxy_address_changed_cb(connection_h connection,
502                                 connection_address_changed_cb callback, void* user_data)
503 {
504         if (callback == NULL || !(__connection_check_handle_validity(connection))) {
505                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
506                 return CONNECTION_ERROR_INVALID_PARAMETER;
507         }
508
509         return __connection_set_proxy_changed_callback(connection, callback, user_data);
510 }
511
512 EXPORT_API int connection_unset_proxy_address_changed_cb(connection_h connection)
513 {
514         if (!(__connection_check_handle_validity(connection))) {
515                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
516                 return CONNECTION_ERROR_INVALID_PARAMETER;
517         }
518
519         return __connection_set_proxy_changed_callback(connection, NULL, NULL);
520 }
521
522 EXPORT_API int connection_add_profile(connection_h connection, connection_profile_h profile)
523 {
524         if (!(__connection_check_handle_validity(connection)) ||
525             !(_connection_libnet_check_profile_validity(profile))) {
526                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
527                 return CONNECTION_ERROR_INVALID_PARAMETER;
528         }
529
530         int rv = 0;
531
532         net_profile_info_t *profile_info = profile;
533
534         if (profile_info->profile_type != NET_DEVICE_CELLULAR) {
535                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
536                 return CONNECTION_ERROR_INVALID_PARAMETER;
537         }
538
539         rv = net_add_profile(profile_info->ProfileInfo.Pdp.ServiceType, (net_profile_info_t*)profile);
540         if (rv != NET_ERR_NONE) {
541                 CONNECTION_LOG(CONNECTION_ERROR, "net_add_profile Failed = %d\n", rv);
542                 return CONNECTION_ERROR_OPERATION_FAILED;
543         }
544
545         return CONNECTION_ERROR_NONE;
546 }
547
548 EXPORT_API int connection_remove_profile(connection_h connection, connection_profile_h profile)
549 {
550         if (!(__connection_check_handle_validity(connection)) ||
551             !(_connection_libnet_check_profile_validity(profile))) {
552                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
553                 return CONNECTION_ERROR_INVALID_PARAMETER;
554         }
555
556         int rv = 0;
557         net_profile_info_t *profile_info = profile;
558
559         if (profile_info->profile_type != NET_DEVICE_CELLULAR &&
560             profile_info->profile_type != NET_DEVICE_WIFI) {
561                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
562                 return CONNECTION_ERROR_INVALID_PARAMETER;
563         }
564
565         rv = net_delete_profile(profile_info->ProfileName);
566         if (rv != NET_ERR_NONE) {
567                 CONNECTION_LOG(CONNECTION_ERROR, "net_delete_profile Failed = %d\n", rv);
568                 return CONNECTION_ERROR_OPERATION_FAILED;
569         }
570
571         return CONNECTION_ERROR_NONE;
572 }
573
574 EXPORT_API int connection_update_profile(connection_h connection, connection_profile_h profile)
575 {
576         if (!(__connection_check_handle_validity(connection)) ||
577             !(_connection_libnet_check_profile_validity(profile))) {
578                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
579                 return CONNECTION_ERROR_INVALID_PARAMETER;
580         }
581
582         int rv = 0;
583         net_profile_info_t *profile_info = profile;
584
585         rv = net_modify_profile(profile_info->ProfileName, (net_profile_info_t*)profile);
586         if (rv != NET_ERR_NONE) {
587                 CONNECTION_LOG(CONNECTION_ERROR, "net_modify_profile Failed = %d\n", rv);
588                 return CONNECTION_ERROR_OPERATION_FAILED;
589         }
590
591         return CONNECTION_ERROR_NONE;
592 }
593
594 EXPORT_API int connection_get_profile_iterator(connection_h connection,
595                 connection_iterator_type_e type, connection_profile_iterator_h* profile_iterator)
596 {
597         if (!(__connection_check_handle_validity(connection)) ||
598             (type != CONNECTION_ITERATOR_TYPE_REGISTERED &&
599              type != CONNECTION_ITERATOR_TYPE_CONNECTED)) {
600                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
601                 return CONNECTION_ERROR_INVALID_PARAMETER;
602         }
603
604         return _connection_libnet_get_profile_iterator(type, profile_iterator);
605 }
606
607 EXPORT_API int connection_profile_iterator_next(connection_profile_iterator_h profile_iterator,
608                                                         connection_profile_h* profile)
609 {
610         return _connection_libnet_get_iterator_next(profile_iterator, profile);
611 }
612
613 EXPORT_API bool connection_profile_iterator_has_next(connection_profile_iterator_h profile_iterator)
614 {
615         return _connection_libnet_iterator_has_next(profile_iterator);
616 }
617
618 EXPORT_API int connection_destroy_profile_iterator(connection_profile_iterator_h profile_iterator)
619 {
620         return _connection_libnet_destroy_iterator(profile_iterator);
621 }
622
623 EXPORT_API int connection_get_current_profile(connection_h connection, connection_profile_h* profile)
624 {
625         if (!(__connection_check_handle_validity(connection)) || profile == NULL) {
626                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
627                 return CONNECTION_ERROR_INVALID_PARAMETER;
628         }
629
630         return _connection_libnet_get_current_profile(profile);
631 }
632
633 EXPORT_API int connection_get_default_cellular_service_profile(connection_h connection,
634                 connection_cellular_service_type_e type, connection_profile_h* profile)
635 {
636         if (!(__connection_check_handle_validity(connection)) || profile == NULL) {
637                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
638                 return CONNECTION_ERROR_INVALID_PARAMETER;
639         }
640
641         return _connection_libnet_get_cellular_service_profile(type, profile);
642 }
643
644 EXPORT_API int connection_set_default_cellular_service_profile(connection_h connection,
645                 connection_cellular_service_type_e type, connection_profile_h profile)
646 {
647         if (!(__connection_check_handle_validity(connection)) || profile == NULL) {
648                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
649                 return CONNECTION_ERROR_INVALID_PARAMETER;
650         }
651
652         return _connection_libnet_set_cellular_service_profile_sync(type, profile);
653 }
654
655 EXPORT_API int connection_set_default_cellular_service_profile_async(connection_h connection,
656                 connection_cellular_service_type_e type, connection_profile_h profile,
657                 connection_set_default_cb callback, void* user_data)
658 {
659         if (!(__connection_check_handle_validity(connection)) ||
660             profile == NULL || callback == NULL) {
661                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
662                 return CONNECTION_ERROR_INVALID_PARAMETER;
663         }
664
665         return _connection_libnet_set_cellular_service_profile_async(type, profile, callback, user_data);
666 }
667
668 EXPORT_API int connection_open_profile(connection_h connection, connection_profile_h profile,
669                                         connection_opened_cb callback, void* user_data)
670 {
671         if (!(__connection_check_handle_validity(connection)) ||
672             profile == NULL || callback == NULL) {
673                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
674                 return CONNECTION_ERROR_INVALID_PARAMETER;
675         }
676
677         return _connection_libnet_open_profile(profile, callback, user_data);
678 }
679
680 EXPORT_API int connection_close_profile(connection_h connection, connection_profile_h profile,
681                                         connection_closed_cb callback, void* user_data)
682 {
683         if (!(__connection_check_handle_validity(connection)) ||
684             profile == NULL || callback == NULL) {
685                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
686                 return CONNECTION_ERROR_INVALID_PARAMETER;
687         }
688
689         return _connection_libnet_close_profile(profile, callback, user_data);
690 }
691
692 EXPORT_API int connection_add_route(connection_h connection, const char* interface_name, const char* host_address)
693 {
694         if (!(__connection_check_handle_validity(connection)) ||
695             interface_name == NULL || host_address == NULL) {
696                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
697                 return CONNECTION_ERROR_INVALID_PARAMETER;
698         }
699
700         return _connection_libnet_add_route(interface_name, host_address);
701 }
702
703
704 /* Connection Statistics module ******************************************************************/
705
706 static int __get_statistic(connection_type_e connection_type,
707                         connection_statistics_type_e statistics_type, long long* llsize)
708 {
709         int size;
710         unsigned long long ull_size;
711         int stat_type;
712         char *key = NULL;
713
714         if (llsize == NULL) {
715                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
716                 return CONNECTION_ERROR_INVALID_PARAMETER;
717         }
718
719         if (connection_type == CONNECTION_TYPE_CELLULAR) {
720                 switch (statistics_type) {
721                 case CONNECTION_STATISTICS_TYPE_LAST_SENT_DATA:
722                         key = VCONFKEY_NETWORK_CELLULAR_PKT_LAST_SNT;
723                         break;
724                 case CONNECTION_STATISTICS_TYPE_LAST_RECEIVED_DATA:
725                         key = VCONFKEY_NETWORK_CELLULAR_PKT_LAST_RCV;
726                         break;
727                 case CONNECTION_STATISTICS_TYPE_TOTAL_SENT_DATA:
728                         key = VCONFKEY_NETWORK_CELLULAR_PKT_TOTAL_SNT;
729                         break;
730                 case CONNECTION_STATISTICS_TYPE_TOTAL_RECEIVED_DATA:
731                         key = VCONFKEY_NETWORK_CELLULAR_PKT_TOTAL_RCV;
732                         break;
733                 default:
734                         return CONNECTION_ERROR_INVALID_PARAMETER;
735                 }
736
737                 if (vconf_get_int(key, &size)) {
738                         CONNECTION_LOG(CONNECTION_ERROR, "Cannot Get %s = %d\n", key, size);
739                         *llsize = 0;
740                         return CONNECTION_ERROR_OPERATION_FAILED;
741                 }
742
743                 CONNECTION_LOG(CONNECTION_INFO,"%s:%d bytes\n", key, size);
744                 *llsize = (long long)size;
745         } else if (connection_type == CONNECTION_TYPE_WIFI) {
746                 switch (statistics_type) {
747                 case CONNECTION_STATISTICS_TYPE_LAST_SENT_DATA:
748                         stat_type = NET_STATISTICS_TYPE_LAST_SENT_DATA;
749                         break;
750                 case CONNECTION_STATISTICS_TYPE_LAST_RECEIVED_DATA:
751                         stat_type = NET_STATISTICS_TYPE_LAST_RECEIVED_DATA;
752                         break;
753                 case CONNECTION_STATISTICS_TYPE_TOTAL_SENT_DATA:
754                         stat_type = NET_STATISTICS_TYPE_TOTAL_SENT_DATA;
755                         break;
756                 case CONNECTION_STATISTICS_TYPE_TOTAL_RECEIVED_DATA:
757                         stat_type = NET_STATISTICS_TYPE_TOTAL_RECEIVED_DATA;
758                         break;
759                 default:
760                         return CONNECTION_ERROR_INVALID_PARAMETER;
761                 }
762
763                 if (_connection_libnet_get_statistics(stat_type, &ull_size) != CONNECTION_ERROR_NONE) {
764                         CONNECTION_LOG(CONNECTION_ERROR, "Cannot Get Wi-Fi statistics : %d\n", ull_size);
765                         *llsize = 0;
766                         return CONNECTION_ERROR_OPERATION_FAILED;
767                 }
768
769                 CONNECTION_LOG(CONNECTION_INFO,"%d bytes\n", ull_size);
770                 *llsize = (long long)ull_size;
771         } else
772                 return CONNECTION_ERROR_INVALID_PARAMETER;
773
774         return CONNECTION_ERROR_NONE;
775 }
776
777 static int __reset_statistic(connection_type_e connection_type,
778                         connection_statistics_type_e statistics_type)
779 {
780         int conn_type;
781         int stat_type;
782         int rv;
783
784         if (connection_type == CONNECTION_TYPE_CELLULAR)
785                 conn_type = NET_DEVICE_CELLULAR;
786         else if (connection_type == CONNECTION_TYPE_WIFI)
787                 conn_type = NET_DEVICE_WIFI;
788         else
789                 return CONNECTION_ERROR_INVALID_PARAMETER;
790
791         switch (statistics_type) {
792         case CONNECTION_STATISTICS_TYPE_LAST_SENT_DATA:
793                 stat_type = NET_STATISTICS_TYPE_LAST_SENT_DATA;
794                 break;
795         case CONNECTION_STATISTICS_TYPE_LAST_RECEIVED_DATA:
796                 stat_type = NET_STATISTICS_TYPE_LAST_RECEIVED_DATA;
797                 break;
798         case CONNECTION_STATISTICS_TYPE_TOTAL_SENT_DATA:
799                 stat_type = NET_STATISTICS_TYPE_TOTAL_SENT_DATA;
800                 break;
801         case CONNECTION_STATISTICS_TYPE_TOTAL_RECEIVED_DATA:
802                 stat_type = NET_STATISTICS_TYPE_TOTAL_RECEIVED_DATA;
803                 break;
804         default:
805                 return CONNECTION_ERROR_INVALID_PARAMETER;
806         }
807
808         rv = _connection_libnet_set_statistics(conn_type, stat_type);
809         if(rv != CONNECTION_ERROR_NONE)
810                 return rv;
811
812
813         CONNECTION_LOG(CONNECTION_INFO,"connection_reset_statistics success\n");
814
815         return CONNECTION_ERROR_NONE;
816 }
817
818 EXPORT_API int connection_get_statistics(connection_h connection,
819                                 connection_type_e connection_type,
820                                 connection_statistics_type_e statistics_type, long long* size)
821 {
822         return __get_statistic(connection_type, statistics_type, size);
823 }
824
825 EXPORT_API int connection_reset_statistics(connection_h connection,
826                                 connection_type_e connection_type,
827                                 connection_statistics_type_e statistics_type)
828 {
829         return __reset_statistic(connection_type, statistics_type);
830 }