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