Add test case for ethernet profile
[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         bool ret = false;
223
224         if (connection == NULL)
225                 return false;
226
227         if (g_slist_find(conn_handle_list, connection) != NULL)
228                 ret = true;
229
230         return ret;
231 }
232
233 static int __connection_get_handle_count(void)
234 {
235         GSList *list;
236         int count = 0;
237
238         if (!conn_handle_list)
239                 return count;
240
241         for (list = conn_handle_list; list; list = list->next) count++;
242
243         return count;
244 }
245
246 /* Connection Manager ********************************************************/
247 EXPORT_API int connection_create(connection_h* connection)
248 {
249         CONNECTION_MUTEX_LOCK;
250         int rv;
251         if (connection == NULL || __connection_check_handle_validity(*connection)) {
252                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
253                 CONNECTION_MUTEX_UNLOCK;
254                 return CONNECTION_ERROR_INVALID_PARAMETER;
255         }
256
257         rv = _connection_libnet_init();
258         if (rv == NET_ERR_ACCESS_DENIED) {
259                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
260                 CONNECTION_MUTEX_UNLOCK;
261                 return CONNECTION_ERROR_PERMISSION_DENIED;
262         }
263         else if (rv != NET_ERR_NONE) {
264                 CONNECTION_LOG(CONNECTION_ERROR, "Failed to create connection[%d]", rv);
265                 CONNECTION_MUTEX_UNLOCK;
266                 return CONNECTION_ERROR_OPERATION_FAILED;
267         }
268
269         *connection = g_try_malloc0(sizeof(connection_handle_s));
270         if (*connection != NULL) {
271                 CONNECTION_LOG(CONNECTION_INFO, "New Handle Created %p\n", *connection);
272         } else {
273                 CONNECTION_MUTEX_UNLOCK;
274                 return CONNECTION_ERROR_OUT_OF_MEMORY;
275         }
276
277         conn_handle_list = g_slist_append(conn_handle_list, *connection);
278
279         CONNECTION_MUTEX_UNLOCK;
280         return CONNECTION_ERROR_NONE;
281 }
282
283 EXPORT_API int connection_destroy(connection_h connection)
284 {
285         CONNECTION_MUTEX_LOCK;
286
287         if (connection == NULL || !(__connection_check_handle_validity(connection))) {
288                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
289                 CONNECTION_MUTEX_UNLOCK;
290                 return CONNECTION_ERROR_INVALID_PARAMETER;
291         }
292
293         CONNECTION_LOG(CONNECTION_INFO, "Destroy Handle : %p\n", connection);
294
295         __connection_set_type_changed_callback(connection, NULL, NULL);
296         __connection_set_ip_changed_callback(connection, NULL, NULL);
297         __connection_set_proxy_changed_callback(connection, NULL, NULL);
298
299         conn_handle_list = g_slist_remove(conn_handle_list, connection);
300
301         g_free(connection);
302
303         if (__connection_get_handle_count() == 0)
304                 _connection_libnet_deinit();
305
306         CONNECTION_MUTEX_UNLOCK;
307         return CONNECTION_ERROR_NONE;
308 }
309
310 EXPORT_API int connection_get_type(connection_h connection, connection_type_e* type)
311 {
312         int status = 0;
313
314         if (type == NULL || !(__connection_check_handle_validity(connection))) {
315                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
316                 return CONNECTION_ERROR_INVALID_PARAMETER;
317         }
318
319         if (vconf_get_int(VCONFKEY_NETWORK_STATUS, &status)) {
320                 CONNECTION_LOG(CONNECTION_ERROR, "vconf_get_int Failed = %d\n", status);
321                 return CONNECTION_ERROR_OPERATION_FAILED;
322         }
323
324         CONNECTION_LOG(CONNECTION_INFO, "Connected Network = %d\n", status);
325
326         *type = __connection_convert_net_state(status);
327
328         return CONNECTION_ERROR_NONE;
329 }
330
331 EXPORT_API int connection_get_ip_address(connection_h connection,
332                                 connection_address_family_e address_family, char** ip_address)
333 {
334         if (ip_address == NULL || !(__connection_check_handle_validity(connection))) {
335                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
336                 return CONNECTION_ERROR_INVALID_PARAMETER;
337         }
338
339         switch (address_family) {
340         case CONNECTION_ADDRESS_FAMILY_IPV4:
341                 *ip_address = vconf_get_str(VCONFKEY_NETWORK_IP);
342                 break;
343         case CONNECTION_ADDRESS_FAMILY_IPV6:
344                 CONNECTION_LOG(CONNECTION_ERROR, "Not supported yet\n");
345                 return CONNECTION_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED;
346                 break;
347         default:
348                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
349                 return CONNECTION_ERROR_INVALID_PARAMETER;
350         }
351
352         if (*ip_address == NULL) {
353                 CONNECTION_LOG(CONNECTION_ERROR, "vconf_get_str Failed\n");
354                 return CONNECTION_ERROR_OPERATION_FAILED;
355         }
356
357         return CONNECTION_ERROR_NONE;
358 }
359
360 EXPORT_API int connection_get_proxy(connection_h connection,
361                                 connection_address_family_e address_family, char** proxy)
362 {
363         if (proxy == NULL || !(__connection_check_handle_validity(connection))) {
364                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
365                 return CONNECTION_ERROR_INVALID_PARAMETER;
366         }
367
368         switch (address_family) {
369         case CONNECTION_ADDRESS_FAMILY_IPV4:
370                 *proxy = vconf_get_str(VCONFKEY_NETWORK_PROXY);
371                 break;
372         case CONNECTION_ADDRESS_FAMILY_IPV6:
373                 CONNECTION_LOG(CONNECTION_ERROR, "Not supported yet\n");
374                 return CONNECTION_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED;
375                 break;
376         default:
377                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
378                 return CONNECTION_ERROR_INVALID_PARAMETER;
379         }
380
381         if (*proxy == NULL) {
382                 CONNECTION_LOG(CONNECTION_ERROR, "vconf_get_str Failed\n");
383                 return CONNECTION_ERROR_OPERATION_FAILED;
384         }
385
386         return CONNECTION_ERROR_NONE;
387 }
388
389 EXPORT_API int connection_get_cellular_state(connection_h connection, connection_cellular_state_e* state)
390 {
391         int status = 0;
392         int cellular_state = 0;
393
394         if (state == NULL || !(__connection_check_handle_validity(connection))) {
395                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
396                 return CONNECTION_ERROR_INVALID_PARAMETER;
397         }
398
399         if (!vconf_get_int(VCONFKEY_NETWORK_CELLULAR_STATE, &status)) {
400                 CONNECTION_LOG(CONNECTION_INFO, "Cellular = %d\n", status);
401                 *state = __connection_convert_cellular_state(status);
402
403                 if (*state == CONNECTION_CELLULAR_STATE_AVAILABLE) {
404                         if (vconf_get_int(VCONFKEY_DNET_STATE, &cellular_state)) {
405                                 CONNECTION_LOG(CONNECTION_ERROR,
406                                                 "vconf_get_int Failed = %d\n", cellular_state);
407                                 return CONNECTION_ERROR_OPERATION_FAILED;
408                         }
409                 }
410
411                 CONNECTION_LOG(CONNECTION_INFO, "Connection state = %d\n", cellular_state);
412
413                 if (cellular_state == VCONFKEY_DNET_NORMAL_CONNECTED ||
414                     cellular_state == VCONFKEY_DNET_SECURE_CONNECTED ||
415                     cellular_state == VCONFKEY_DNET_TRANSFER)
416                         *state = CONNECTION_CELLULAR_STATE_CONNECTED;
417
418                 return CONNECTION_ERROR_NONE;
419         } else {
420                 CONNECTION_LOG(CONNECTION_ERROR, "vconf_get_int Failed = %d\n", status);
421                 return CONNECTION_ERROR_OPERATION_FAILED;
422         }
423 }
424
425 EXPORT_API int connection_get_wifi_state(connection_h connection, connection_wifi_state_e* state)
426 {
427         int rv;
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         rv = _connection_libnet_get_wifi_state(state);
435         if (rv != CONNECTION_ERROR_NONE) {
436                 CONNECTION_LOG(CONNECTION_ERROR, "Fail to get Wi-Fi state[%d]", rv);
437                 return rv;
438         }
439
440         CONNECTION_LOG(CONNECTION_INFO, "WiFi state = %d\n", *state);
441
442         return CONNECTION_ERROR_NONE;
443 }
444
445 EXPORT_API int connection_get_ethernet_state(connection_h connection, connection_ethernet_state_e* state)
446 {
447         if (state == NULL || !(__connection_check_handle_validity(connection))) {
448                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
449                 return CONNECTION_ERROR_INVALID_PARAMETER;
450         }
451
452         return _connection_libnet_get_ethernet_state(state);
453 }
454
455 EXPORT_API int connection_get_bt_state(connection_h connection, connection_bt_state_e* state)
456 {
457         if (state == NULL || !(__connection_check_handle_validity(connection))) {
458                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
459                 return CONNECTION_ERROR_INVALID_PARAMETER;
460         }
461
462         return _connection_libnet_get_bluetooth_state(state);
463
464 }
465
466 EXPORT_API int connection_set_type_changed_cb(connection_h connection,
467                                         connection_type_changed_cb callback, void* user_data)
468 {
469         if (callback == NULL || !(__connection_check_handle_validity(connection))) {
470                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
471                 return CONNECTION_ERROR_INVALID_PARAMETER;
472         }
473
474         return __connection_set_type_changed_callback(connection, callback, user_data);
475 }
476
477 EXPORT_API int connection_unset_type_changed_cb(connection_h connection)
478 {
479         if (!(__connection_check_handle_validity(connection))) {
480                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
481                 return CONNECTION_ERROR_INVALID_PARAMETER;
482         }
483
484         return __connection_set_type_changed_callback(connection, NULL, NULL);
485 }
486
487 EXPORT_API int connection_set_ip_address_changed_cb(connection_h connection,
488                                 connection_address_changed_cb callback, void* user_data)
489 {
490         if (callback == NULL || !(__connection_check_handle_validity(connection))) {
491                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
492                 return CONNECTION_ERROR_INVALID_PARAMETER;
493         }
494
495         return __connection_set_ip_changed_callback(connection, callback, user_data);
496 }
497
498 EXPORT_API int connection_unset_ip_address_changed_cb(connection_h connection)
499 {
500         if (!(__connection_check_handle_validity(connection))) {
501                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
502                 return CONNECTION_ERROR_INVALID_PARAMETER;
503         }
504
505         return __connection_set_ip_changed_callback(connection, NULL, NULL);
506 }
507
508 EXPORT_API int connection_set_proxy_address_changed_cb(connection_h connection,
509                                 connection_address_changed_cb callback, void* user_data)
510 {
511         if (callback == NULL || !(__connection_check_handle_validity(connection))) {
512                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
513                 return CONNECTION_ERROR_INVALID_PARAMETER;
514         }
515
516         return __connection_set_proxy_changed_callback(connection, callback, user_data);
517 }
518
519 EXPORT_API int connection_unset_proxy_address_changed_cb(connection_h connection)
520 {
521         if (!(__connection_check_handle_validity(connection))) {
522                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
523                 return CONNECTION_ERROR_INVALID_PARAMETER;
524         }
525
526         return __connection_set_proxy_changed_callback(connection, NULL, NULL);
527 }
528
529 EXPORT_API int connection_add_profile(connection_h connection, connection_profile_h profile)
530 {
531         if (!(__connection_check_handle_validity(connection)) ||
532             !(_connection_libnet_check_profile_validity(profile))) {
533                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
534                 return CONNECTION_ERROR_INVALID_PARAMETER;
535         }
536
537         int rv = 0;
538
539         net_profile_info_t *profile_info = profile;
540
541         if (profile_info->profile_type != NET_DEVICE_CELLULAR) {
542                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
543                 return CONNECTION_ERROR_INVALID_PARAMETER;
544         }
545
546         rv = net_add_profile(profile_info->ProfileInfo.Pdp.ServiceType, (net_profile_info_t*)profile);
547         if (rv == NET_ERR_ACCESS_DENIED) {
548                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
549                 return CONNECTION_ERROR_PERMISSION_DENIED;
550         } else if (rv != NET_ERR_NONE) {
551                 CONNECTION_LOG(CONNECTION_ERROR, "Failed to add profile[%d]", rv);
552                 return CONNECTION_ERROR_OPERATION_FAILED;
553         }
554
555         return CONNECTION_ERROR_NONE;
556 }
557
558 EXPORT_API int connection_remove_profile(connection_h connection, connection_profile_h profile)
559 {
560         if (!(__connection_check_handle_validity(connection)) ||
561             !(_connection_libnet_check_profile_validity(profile))) {
562                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
563                 return CONNECTION_ERROR_INVALID_PARAMETER;
564         }
565
566         int rv = 0;
567         net_profile_info_t *profile_info = profile;
568
569         if (profile_info->profile_type != NET_DEVICE_CELLULAR &&
570             profile_info->profile_type != NET_DEVICE_WIFI) {
571                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
572                 return CONNECTION_ERROR_INVALID_PARAMETER;
573         }
574
575         rv = net_delete_profile(profile_info->ProfileName);
576         if (rv == NET_ERR_ACCESS_DENIED) {
577                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
578                 return CONNECTION_ERROR_PERMISSION_DENIED;
579         } else if (rv != NET_ERR_NONE) {
580                 CONNECTION_LOG(CONNECTION_ERROR, "Failed to delete profile[%d]", rv);
581                 return CONNECTION_ERROR_OPERATION_FAILED;
582         }
583
584         return CONNECTION_ERROR_NONE;
585 }
586
587 EXPORT_API int connection_update_profile(connection_h connection, connection_profile_h profile)
588 {
589         if (!(__connection_check_handle_validity(connection)) ||
590             !(_connection_libnet_check_profile_validity(profile))) {
591                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
592                 return CONNECTION_ERROR_INVALID_PARAMETER;
593         }
594
595         int rv = 0;
596         net_profile_info_t *profile_info = profile;
597
598         rv = net_modify_profile(profile_info->ProfileName, (net_profile_info_t*)profile);
599         if (rv == NET_ERR_ACCESS_DENIED) {
600                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
601                 return CONNECTION_ERROR_PERMISSION_DENIED;
602         } else if (rv != NET_ERR_NONE) {
603                 CONNECTION_LOG(CONNECTION_ERROR, "Failed to modify profile[%d]", rv);
604                 return CONNECTION_ERROR_OPERATION_FAILED;
605         }
606
607         return CONNECTION_ERROR_NONE;
608 }
609
610 EXPORT_API int connection_get_profile_iterator(connection_h connection,
611                 connection_iterator_type_e type, connection_profile_iterator_h* profile_iterator)
612 {
613         if (!(__connection_check_handle_validity(connection)) ||
614             (type != CONNECTION_ITERATOR_TYPE_REGISTERED &&
615              type != CONNECTION_ITERATOR_TYPE_CONNECTED &&
616              type != CONNECTION_ITERATOR_TYPE_DEFAULT)) {
617                 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
618                 return CONNECTION_ERROR_INVALID_PARAMETER;
619         }
620
621         return _connection_libnet_get_profile_iterator(type, profile_iterator);
622 }
623
624 EXPORT_API int connection_profile_iterator_next(connection_profile_iterator_h profile_iterator,
625                                                         connection_profile_h* profile)
626 {
627         return _connection_libnet_get_iterator_next(profile_iterator, profile);
628 }
629
630 EXPORT_API bool connection_profile_iterator_has_next(connection_profile_iterator_h profile_iterator)
631 {
632         return _connection_libnet_iterator_has_next(profile_iterator);
633 }
634
635 EXPORT_API int connection_destroy_profile_iterator(connection_profile_iterator_h profile_iterator)
636 {
637         return _connection_libnet_destroy_iterator(profile_iterator);
638 }
639
640 EXPORT_API int connection_get_current_profile(connection_h connection, connection_profile_h* profile)
641 {
642         if (!(__connection_check_handle_validity(connection)) || profile == NULL) {
643                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
644                 return CONNECTION_ERROR_INVALID_PARAMETER;
645         }
646
647         return _connection_libnet_get_current_profile(profile);
648 }
649
650 EXPORT_API 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 EXPORT_API 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 EXPORT_API 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 EXPORT_API int connection_open_profile(connection_h connection, connection_profile_h profile,
686                                         connection_opened_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_open_profile(profile, callback, user_data);
695 }
696
697 EXPORT_API int connection_close_profile(connection_h connection, connection_profile_h profile,
698                                         connection_closed_cb callback, void* user_data)
699 {
700         if (!(__connection_check_handle_validity(connection)) ||
701             profile == NULL || callback == NULL) {
702                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
703                 return CONNECTION_ERROR_INVALID_PARAMETER;
704         }
705
706         return _connection_libnet_close_profile(profile, callback, user_data);
707 }
708
709 EXPORT_API int connection_reset_profile(connection_h connection,
710                                 connection_reset_option_e type, int id, connection_reset_cb callback, void *user_data)
711 {
712         if (!(__connection_check_handle_validity(connection))) {
713                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed");
714                 return CONNECTION_ERROR_INVALID_PARAMETER;
715         }
716
717         if(id < 0 || id > 1) {
718                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed");
719                 return CONNECTION_ERROR_INVALID_PARAMETER;
720         }
721
722         return _connection_libnet_reset_profile(type, id, callback, user_data);
723 }
724
725 EXPORT_API int connection_add_route(connection_h connection, const char* interface_name, const char* host_address)
726 {
727         if (!(__connection_check_handle_validity(connection)) ||
728             interface_name == NULL || host_address == NULL) {
729                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
730                 return CONNECTION_ERROR_INVALID_PARAMETER;
731         }
732
733         return _connection_libnet_add_route(interface_name, host_address);
734 }
735
736 EXPORT_API int connection_remove_route(connection_h connection, const char* interface_name, const char* host_address)
737 {
738         if (!(__connection_check_handle_validity(connection)) ||
739             interface_name == NULL || host_address == NULL) {
740                 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
741                 return CONNECTION_ERROR_INVALID_PARAMETER;
742         }
743
744         return _connection_libnet_remove_route(interface_name, host_address);
745 }
746
747 /* Connection Statistics module ******************************************************************/
748
749 static int __get_statistic(connection_type_e connection_type,
750                         connection_statistics_type_e statistics_type, long long* llsize)
751 {
752         int rv, size;
753         unsigned long long ull_size;
754         int stat_type;
755         char *key = NULL;
756
757         if (llsize == NULL) {
758                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
759                 return CONNECTION_ERROR_INVALID_PARAMETER;
760         }
761
762         if (connection_type == CONNECTION_TYPE_CELLULAR) {
763                 switch (statistics_type) {
764                 case CONNECTION_STATISTICS_TYPE_LAST_SENT_DATA:
765                         key = VCONFKEY_NETWORK_CELLULAR_PKT_LAST_SNT;
766                         break;
767                 case CONNECTION_STATISTICS_TYPE_LAST_RECEIVED_DATA:
768                         key = VCONFKEY_NETWORK_CELLULAR_PKT_LAST_RCV;
769                         break;
770                 case CONNECTION_STATISTICS_TYPE_TOTAL_SENT_DATA:
771                         key = VCONFKEY_NETWORK_CELLULAR_PKT_TOTAL_SNT;
772                         break;
773                 case CONNECTION_STATISTICS_TYPE_TOTAL_RECEIVED_DATA:
774                         key = VCONFKEY_NETWORK_CELLULAR_PKT_TOTAL_RCV;
775                         break;
776                 default:
777                         return CONNECTION_ERROR_INVALID_PARAMETER;
778                 }
779
780                 if (vconf_get_int(key, &size)) {
781                         CONNECTION_LOG(CONNECTION_ERROR, "Cannot Get %s = %d\n", key, size);
782                         *llsize = 0;
783                         return CONNECTION_ERROR_OPERATION_FAILED;
784                 }
785
786                 CONNECTION_LOG(CONNECTION_INFO,"%s:%d bytes\n", key, size);
787                 *llsize = (long long)size;
788         } else if (connection_type == CONNECTION_TYPE_WIFI) {
789                 switch (statistics_type) {
790                 case CONNECTION_STATISTICS_TYPE_LAST_SENT_DATA:
791                         stat_type = NET_STATISTICS_TYPE_LAST_SENT_DATA;
792                         break;
793                 case CONNECTION_STATISTICS_TYPE_LAST_RECEIVED_DATA:
794                         stat_type = NET_STATISTICS_TYPE_LAST_RECEIVED_DATA;
795                         break;
796                 case CONNECTION_STATISTICS_TYPE_TOTAL_SENT_DATA:
797                         stat_type = NET_STATISTICS_TYPE_TOTAL_SENT_DATA;
798                         break;
799                 case CONNECTION_STATISTICS_TYPE_TOTAL_RECEIVED_DATA:
800                         stat_type = NET_STATISTICS_TYPE_TOTAL_RECEIVED_DATA;
801                         break;
802                 default:
803                         return CONNECTION_ERROR_INVALID_PARAMETER;
804                 }
805
806                 rv  = _connection_libnet_get_statistics(stat_type, &ull_size);
807                 if (rv == CONNECTION_ERROR_PERMISSION_DENIED)
808                         return rv;
809                 else if (rv != CONNECTION_ERROR_NONE) {
810                         CONNECTION_LOG(CONNECTION_ERROR, "Failed to get Wi-Fi statistics");
811                         *llsize = 0;
812                         return CONNECTION_ERROR_OPERATION_FAILED;
813                 }
814
815                 CONNECTION_LOG(CONNECTION_INFO,"%d bytes\n", ull_size);
816                 *llsize = (long long)ull_size;
817         } else
818                 return CONNECTION_ERROR_INVALID_PARAMETER;
819
820         return CONNECTION_ERROR_NONE;
821 }
822
823 static int __reset_statistic(connection_type_e connection_type,
824                         connection_statistics_type_e statistics_type)
825 {
826         int conn_type;
827         int stat_type;
828         int rv;
829
830         if (connection_type == CONNECTION_TYPE_CELLULAR)
831                 conn_type = NET_DEVICE_CELLULAR;
832         else if (connection_type == CONNECTION_TYPE_WIFI)
833                 conn_type = NET_DEVICE_WIFI;
834         else
835                 return CONNECTION_ERROR_INVALID_PARAMETER;
836
837         switch (statistics_type) {
838         case CONNECTION_STATISTICS_TYPE_LAST_SENT_DATA:
839                 stat_type = NET_STATISTICS_TYPE_LAST_SENT_DATA;
840                 break;
841         case CONNECTION_STATISTICS_TYPE_LAST_RECEIVED_DATA:
842                 stat_type = NET_STATISTICS_TYPE_LAST_RECEIVED_DATA;
843                 break;
844         case CONNECTION_STATISTICS_TYPE_TOTAL_SENT_DATA:
845                 stat_type = NET_STATISTICS_TYPE_TOTAL_SENT_DATA;
846                 break;
847         case CONNECTION_STATISTICS_TYPE_TOTAL_RECEIVED_DATA:
848                 stat_type = NET_STATISTICS_TYPE_TOTAL_RECEIVED_DATA;
849                 break;
850         default:
851                 return CONNECTION_ERROR_INVALID_PARAMETER;
852         }
853
854         rv = _connection_libnet_set_statistics(conn_type, stat_type);
855         if(rv != CONNECTION_ERROR_NONE)
856                 return rv;
857
858
859         CONNECTION_LOG(CONNECTION_INFO,"connection_reset_statistics success\n");
860
861         return CONNECTION_ERROR_NONE;
862 }
863
864 EXPORT_API int connection_get_statistics(connection_h connection,
865                                 connection_type_e connection_type,
866                                 connection_statistics_type_e statistics_type, long long* size)
867 {
868         if (!(__connection_check_handle_validity(connection)) || size == NULL) {
869                 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
870                 return CONNECTION_ERROR_INVALID_PARAMETER;
871         }
872
873         return __get_statistic(connection_type, statistics_type, size);
874 }
875
876 EXPORT_API int connection_reset_statistics(connection_h connection,
877                                 connection_type_e connection_type,
878                                 connection_statistics_type_e statistics_type)
879 {
880         if (!(__connection_check_handle_validity(connection))) {
881                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed");
882                 return CONNECTION_ERROR_INVALID_PARAMETER;
883         }
884
885         return __reset_statistic(connection_type, statistics_type);
886 }