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