Fix wrong function name
[platform/core/api/connection.git] / src / libnetwork.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 <glib.h>
18 #include <stdio.h>
19 #include <stdarg.h>
20 #include <string.h>
21 #include <vconf/vconf.h>
22 #include <system_info.h>
23 #include <arpa/inet.h>
24
25 #include "net_connection_private.h"
26
27 static __thread GSList *prof_handle_list = NULL;
28 static __thread GHashTable *profile_cb_table = NULL;
29
30 struct _profile_cb_s {
31         connection_profile_state_changed_cb callback;
32         connection_profile_state_e state;
33         void *user_data;
34 };
35
36 struct _profile_list_s {
37         int count;
38         int next;
39         net_profile_info_t *profiles;
40 };
41
42 struct _libnet_s {
43         connection_opened_cb opened_cb;
44         connection_closed_cb closed_cb;
45         connection_set_default_cb set_default_cb;
46         connection_reset_cb reset_profile_cb;
47         libnet_ethernet_cable_state_changed_cb ethernet_cable_state_changed_cb;
48         libnet_type_changed_cb type_changed_cb;
49         libnet_ip_changed_cb ip_changed_cb;
50         libnet_proxy_changed_cb proxy_changed_cb;
51         void *opened_user_data;
52         void *closed_user_data;
53         void *set_default_user_data;
54         void *reset_profile_user_data;
55         bool is_created;
56 };
57
58 struct _state_notify {
59         connection_profile_state_changed_cb callback;
60         connection_profile_state_e state;
61         void *user_data;
62 };
63
64 struct managed_idle_data {
65         GSourceFunc func;
66         gpointer user_data;
67         guint id;
68 };
69
70 static __thread struct _profile_list_s profile_iterator = {0, 0, NULL};
71 static __thread struct _libnet_s libnet = {NULL, NULL, NULL, NULL, NULL, NULL,
72                                         NULL, NULL, NULL, NULL, NULL, NULL, false};
73 static __thread GSList *managed_idler_list = NULL;
74 static __thread bool connection_is_feature_checked[CONNECTION_SUPPORTED_FEATURE_MAX] = {0, };
75 static __thread bool connection_feature_supported[CONNECTION_SUPPORTED_FEATURE_MAX] = {0, };
76
77 bool _connection_is_created(void)
78 {
79         return libnet.is_created;
80 }
81
82 static void __connection_set_created(bool tag)
83 {
84         libnet.is_created = tag;
85 }
86
87 //LCOV_EXCL_START
88 static connection_error_e __libnet_convert_to_cp_error_type(net_err_t err_type)
89 {
90         switch (err_type) {
91         case NET_ERR_NONE:
92                 return CONNECTION_ERROR_NONE;
93         case NET_ERR_APP_ALREADY_REGISTERED:
94                 return CONNECTION_ERROR_INVALID_OPERATION;
95         case NET_ERR_APP_NOT_REGISTERED:
96                 return CONNECTION_ERROR_INVALID_OPERATION;
97         case NET_ERR_NO_ACTIVE_CONNECTIONS:
98                 return CONNECTION_ERROR_NO_CONNECTION;
99         case NET_ERR_ACTIVE_CONNECTION_EXISTS:
100                 return CONNECTION_ERROR_ALREADY_EXISTS;
101         case NET_ERR_CONNECTION_DHCP_FAILED:
102                 return CONNECTION_ERROR_DHCP_FAILED;
103         case NET_ERR_CONNECTION_INVALID_KEY:
104                 return CONNECTION_ERROR_INVALID_KEY;
105         case NET_ERR_IN_PROGRESS:
106                 return CONNECTION_ERROR_NOW_IN_PROGRESS;
107         case NET_ERR_OPERATION_ABORTED:
108                 return CONNECTION_ERROR_OPERATION_ABORTED;
109         case NET_ERR_TIME_OUT:
110                 return CONNECTION_ERROR_NO_REPLY;
111         case NET_ERR_ACCESS_DENIED:
112                 return CONNECTION_ERROR_PERMISSION_DENIED;
113         default:
114                 return CONNECTION_ERROR_OPERATION_FAILED;
115         }
116 }
117
118 static const char *__libnet_convert_cp_error_type_to_string(connection_error_e err_type)
119 {
120         switch (err_type) {
121         case CONNECTION_ERROR_NONE:
122                 return "NONE";
123         case CONNECTION_ERROR_INVALID_PARAMETER:
124                 return "INVALID_PARAMETER";
125         case CONNECTION_ERROR_OUT_OF_MEMORY:
126                 return "OUT_OF_MEMORY";
127         case CONNECTION_ERROR_INVALID_OPERATION:
128                 return "INVALID_OPERATION";
129         case CONNECTION_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED:
130                 return "ADDRESS_FAMILY_NOT_SUPPORTED";
131         case CONNECTION_ERROR_OPERATION_FAILED:
132                 return "OPERATION_FAILED";
133         case CONNECTION_ERROR_ITERATOR_END:
134                 return "ITERATOR_END";
135         case CONNECTION_ERROR_NO_CONNECTION:
136                 return "NO_CONNECTION";
137         case CONNECTION_ERROR_NOW_IN_PROGRESS:
138                 return "NOW_IN_PROGRESS";
139         case CONNECTION_ERROR_ALREADY_EXISTS:
140                 return "ALREADY_EXISTS";
141         case CONNECTION_ERROR_OPERATION_ABORTED:
142                 return "OPERATION_ABORTED";
143         case CONNECTION_ERROR_DHCP_FAILED:
144                 return "DHCP_FAILED";
145         case CONNECTION_ERROR_INVALID_KEY:
146                 return "INVALID_KEY";
147         case CONNECTION_ERROR_NO_REPLY:
148                 return "NO_REPLY";
149         case CONNECTION_ERROR_PERMISSION_DENIED:
150                 return "PERMISSION_DENIED";
151         case CONNECTION_ERROR_NOT_SUPPORTED:
152                 return "NOT_SUPPORTED";
153         case CONNECTION_ERROR_ALREADY_INITIALIZED:
154                 return "ALREADY_INITIALIZED";
155         case CONNECTION_ERROR_NOT_INITIALIZED:
156                 return "NOT_INITIALIZED";
157         }
158
159         return "UNKNOWN";
160 }
161
162 static const char *__libnet_convert_cp_state_to_string(connection_profile_state_e state)
163 {
164         switch (state) {
165         case CONNECTION_PROFILE_STATE_DISCONNECTED:
166                 return "DISCONNECTED";
167         case CONNECTION_PROFILE_STATE_ASSOCIATION:
168                 return "ASSOCIATION";
169         case CONNECTION_PROFILE_STATE_CONFIGURATION:
170                 return "CONFIGURATION";
171         case CONNECTION_PROFILE_STATE_CONNECTED:
172                 return "CONNECTED";
173         default:
174                 return "UNKNOWN";
175         }
176 }
177
178 static void __libnet_set_reset_profile_cb(connection_opened_cb user_cb, void *user_data)
179 {
180         if (user_cb != NULL) {
181                 libnet.reset_profile_cb = user_cb;
182                 libnet.reset_profile_user_data = user_data;
183         }
184 }
185
186 static gboolean __libnet_reset_profile_cb_idle(gpointer data)
187 {
188         connection_error_e result = (connection_error_e)data;
189
190         if (libnet.reset_profile_cb != NULL)
191                 libnet.reset_profile_cb(result, libnet.reset_profile_user_data);
192
193         libnet.reset_profile_cb = NULL;
194         libnet.reset_profile_user_data = NULL;
195
196         return FALSE;
197 }
198
199 static void __libnet_reset_profile_cb(connection_error_e result)
200 {
201         if (_connection_is_created() != true) {
202                 CONNECTION_LOG(CONNECTION_ERROR, "Application is not registered"
203                                 "If multi-threaded, thread integrity be broken.");
204                 return;
205         }
206
207         if (libnet.reset_profile_cb != NULL)
208                 _connection_callback_add(__libnet_reset_profile_cb_idle, (gpointer)result);
209 }
210
211 static void __libnet_set_opened_cb(connection_opened_cb user_cb, void *user_data)
212 {
213         if (user_cb != NULL) {
214                 libnet.opened_cb = user_cb;
215                 libnet.opened_user_data = user_data;
216         }
217 }
218
219 static gboolean __libnet_opened_cb_idle(gpointer data)
220 {
221         connection_error_e result = (connection_error_e)data;
222
223         if (libnet.opened_cb != NULL)
224                 libnet.opened_cb(result, libnet.opened_user_data);
225
226         libnet.opened_cb = NULL;
227         libnet.opened_user_data = NULL;
228
229         return FALSE;
230 }
231
232 static void __libnet_opened_cb(connection_error_e result)
233 {
234         if (_connection_is_created() != true) {
235                 CONNECTION_LOG(CONNECTION_ERROR, "Application is not registered"
236                                 "If multi-threaded, thread integrity be broken.");
237                 return;
238         }
239
240         if (libnet.opened_cb != NULL)
241                 _connection_callback_add(__libnet_opened_cb_idle, (gpointer)result);
242 }
243
244 static void __libnet_set_closed_cb(connection_closed_cb user_cb, void *user_data)
245 {
246         if (user_cb != NULL) {
247                 libnet.closed_cb = user_cb;
248                 libnet.closed_user_data = user_data;
249         }
250 }
251
252 static gboolean __libnet_closed_cb_idle(gpointer data)
253 {
254         connection_error_e result = (connection_error_e)data;
255
256         if (libnet.closed_cb != NULL)
257                 libnet.closed_cb(result, libnet.closed_user_data);
258
259         libnet.closed_cb = NULL;
260         libnet.closed_user_data = NULL;
261
262         return FALSE;
263 }
264
265 static void __libnet_closed_cb(connection_error_e result)
266 {
267         if (_connection_is_created() != true) {
268                 CONNECTION_LOG(CONNECTION_ERROR, "Application is not registered"
269                                 "If multi-threaded, thread integrity be broken.");
270                 return;
271         }
272
273         if (libnet.closed_cb != NULL)
274                 _connection_callback_add(__libnet_closed_cb_idle, (gpointer)result);
275 }
276
277 static void __libnet_set_default_cb(connection_set_default_cb user_cb, void *user_data)
278 {
279         if (user_cb != NULL) {
280                 libnet.set_default_cb = user_cb;
281                 libnet.set_default_user_data = user_data;
282         }
283 }
284
285 static gboolean __libnet_default_cb_idle(gpointer data)
286 {
287         connection_error_e result = (connection_error_e)data;
288
289         if (libnet.set_default_cb != NULL)
290                 libnet.set_default_cb(result, libnet.set_default_user_data);
291
292         libnet.set_default_cb = NULL;
293         libnet.set_default_user_data = NULL;
294
295         return FALSE;
296 }
297
298 static void __libnet_default_cb(connection_error_e result)
299 {
300         if (_connection_is_created() != true) {
301                 CONNECTION_LOG(CONNECTION_ERROR, "Application is not registered"
302                                 "If multi-threaded, thread integrity be broken.");
303                 return;
304         }
305
306         if (libnet.set_default_cb != NULL)
307                 _connection_callback_add(__libnet_default_cb_idle, (gpointer)result);
308 }
309
310 static void __libnet_set_ethernet_cable_state_changed_cb(
311                 libnet_ethernet_cable_state_changed_cb user_cb)
312 {
313         libnet.ethernet_cable_state_changed_cb = user_cb;
314 }
315
316 static void __libnet_ethernet_cable_state_changed_cb(
317                 connection_ethernet_cable_state_e state)
318 {
319         if (libnet.ethernet_cable_state_changed_cb)
320                 libnet.ethernet_cable_state_changed_cb(state);
321 }
322
323 static void __libnet_type_changed_cb(int type)
324 {
325         if (libnet.type_changed_cb)
326                 libnet.type_changed_cb(type);
327 }
328
329 static void __libnet_ip_changed_cb(connection_address_family_e addr_family,
330                                                                    char *ip_addr)
331 {
332         if (libnet.ip_changed_cb)
333                 libnet.ip_changed_cb(addr_family, ip_addr);
334 }
335
336 static void __libnet_proxy_changed_cb(char *proxy_addr)
337 {
338         if (libnet.proxy_changed_cb)
339                 libnet.proxy_changed_cb(proxy_addr);
340 }
341
342 static gboolean __libnet_state_changed_cb_idle(gpointer data)
343 {
344         struct _state_notify *notify = (struct _state_notify *)data;
345
346         if (notify == NULL)
347                 return FALSE;
348
349         if (notify->callback != NULL)
350                 notify->callback(notify->state, notify->user_data);
351
352         g_free(notify);
353
354         return FALSE;
355 }
356
357 static void __libnet_state_changed_cb(char *profile_name, connection_profile_state_e state)
358 {
359         guint id;
360         struct _state_notify *notify;
361         struct _profile_cb_s *cb_info;
362
363         if (_connection_is_created() != true) {
364                 CONNECTION_LOG(CONNECTION_ERROR, "Application is not registered"
365                                 "If multi-threaded, thread integrity be broken.");
366                 return;
367         }
368
369         if (profile_name == NULL)
370                 return;
371
372         cb_info = g_hash_table_lookup(profile_cb_table, profile_name);
373         if (cb_info == NULL)
374                 return;
375
376         if (cb_info->state == state)
377                 return;
378
379         cb_info->state = state;
380
381         if (state < 0 || cb_info->callback == NULL)
382                 return;
383
384         notify = g_try_new0(struct _state_notify, 1);
385         if (notify == NULL)
386                 return;
387
388         notify->callback = cb_info->callback;
389         notify->state = state;
390         notify->user_data = cb_info->user_data;
391
392         id = _connection_callback_add(__libnet_state_changed_cb_idle,
393                         (gpointer)notify);
394         if (!id)
395                 g_free(notify);
396 }
397
398 static void __libnet_clear_profile_list(struct _profile_list_s *profile_list)
399 {
400         if (profile_list->count > 0)
401                 g_free(profile_list->profiles);
402
403         profile_list->count = 0;
404         profile_list->next = 0;
405         profile_list->profiles = NULL;
406 }
407
408 static void __libnet_evt_cb(net_event_info_t *event_cb, void *user_data)
409 {
410         bool is_requested = false;
411         connection_error_e result = CONNECTION_ERROR_NONE;
412
413         switch (event_cb->Event) {
414         case NET_EVENT_OPEN_RSP:
415                 is_requested = true;
416                 /* fall through */
417         case NET_EVENT_OPEN_IND:
418                 result = __libnet_convert_to_cp_error_type(event_cb->Error);
419                 CONNECTION_LOG(CONNECTION_INFO, "Connection opened %s[%s]",
420                                         (is_requested) ? "RSP" : "IND",
421                                         __libnet_convert_cp_error_type_to_string(result));
422
423                 if (is_requested)
424                         __libnet_opened_cb(result);
425
426                 switch (event_cb->Error) {
427                 case NET_ERR_NONE:
428                 case NET_ERR_ACTIVE_CONNECTION_EXISTS:
429                         CONNECTION_LOG(CONNECTION_INFO, "Successfully open connection");
430
431                         __libnet_state_changed_cb(event_cb->ProfileName, CONNECTION_PROFILE_STATE_CONNECTED);
432                         return;
433                 default:
434                         CONNECTION_LOG(CONNECTION_ERROR, "Failed to open connection[%s]",
435                                                 __libnet_convert_cp_error_type_to_string(result));
436                 }
437
438                 __libnet_state_changed_cb(event_cb->ProfileName, CONNECTION_PROFILE_STATE_DISCONNECTED);
439
440                 break;
441         case NET_EVENT_CLOSE_RSP:
442                 is_requested = true;
443                 /* fall through */
444         case NET_EVENT_CLOSE_IND:
445                 result = __libnet_convert_to_cp_error_type(event_cb->Error);
446                 CONNECTION_LOG(CONNECTION_INFO, "Connection closed %s[%s]",
447                                         (is_requested) ? "RSP" : "IND",
448                                         __libnet_convert_cp_error_type_to_string(result));
449
450                 if (is_requested)
451                         __libnet_closed_cb(result);
452
453                 switch (event_cb->Error) {
454                 case NET_ERR_NONE:
455                         CONNECTION_LOG(CONNECTION_INFO, "Successfully closed connection");
456
457                         __libnet_state_changed_cb(event_cb->ProfileName, CONNECTION_PROFILE_STATE_DISCONNECTED);
458                         return;
459                 default:
460                         CONNECTION_LOG(CONNECTION_ERROR, "Failed to close connection[%s]",
461                                                         __libnet_convert_cp_error_type_to_string(result));
462                 }
463
464                 break;
465         case NET_EVENT_NET_STATE_IND:
466                 CONNECTION_LOG(CONNECTION_INFO, "State changed IND");
467
468                 if (event_cb->Datalength != sizeof(net_state_type_t))
469                         return;
470
471                 net_state_type_t *profile_state = (net_state_type_t *)event_cb->Data;
472                 connection_profile_state_e cp_state = _profile_convert_to_cp_state(*profile_state);
473
474                 CONNECTION_LOG(CONNECTION_INFO, "state: %s", __libnet_convert_cp_state_to_string(cp_state));
475                 SECURE_CONNECTION_LOG(CONNECTION_INFO, "profile name: %s", event_cb->ProfileName);
476
477                 __libnet_state_changed_cb(event_cb->ProfileName, cp_state);
478
479                 break;
480         case NET_EVENT_CELLULAR_SET_DEFAULT_RSP:
481                 result = __libnet_convert_to_cp_error_type(event_cb->Error);
482                 CONNECTION_LOG(CONNECTION_INFO, "Got set default profile RSP %d", result);
483                 __libnet_default_cb(result);
484                 break;
485
486         case NET_EVENT_CELLULAR_RESET_DEFAULT_RSP:
487                 result = __libnet_convert_to_cp_error_type(event_cb->Error);
488                 CONNECTION_LOG(CONNECTION_INFO, "Got reset default profile RSP %d", result);
489                 __libnet_reset_profile_cb(result);
490                 break;
491         case NET_EVENT_ETHERNET_CABLE_ATTACHED:
492                 CONNECTION_LOG(CONNECTION_INFO, "Got Ethernet cable Attached Indication\n");
493                 __libnet_ethernet_cable_state_changed_cb(CONNECTION_ETHERNET_CABLE_ATTACHED);
494                 break;
495         case NET_EVENT_ETHERNET_CABLE_DETACHED:
496                 CONNECTION_LOG(CONNECTION_INFO, "Got Ethernet cable detached Indication\n");
497                 __libnet_ethernet_cable_state_changed_cb(CONNECTION_ETHERNET_CABLE_DETACHED);
498                 break;
499         case NET_EVENT_NETWORK_TYPE_CHANGED:
500                 CONNECTION_LOG(CONNECTION_INFO, "Got Network Type Changed Indication");
501                 int *state = (int *) event_cb->Data;
502                 __libnet_type_changed_cb(*state);
503                 break;
504         case NET_EVENT_IPV4_ADDRESS_CHANGED:
505                 CONNECTION_LOG(CONNECTION_INFO, "Got IPv4 Address Changed Indication");
506                 char *ipv4_addr = (char *)event_cb->Data;
507                 __libnet_ip_changed_cb(CONNECTION_ADDRESS_FAMILY_IPV4, ipv4_addr);
508                 break;
509         case NET_EVENT_IPV6_ADDRESS_CHANGED:
510                 CONNECTION_LOG(CONNECTION_INFO, "Got IPv6 Address Changed Indication");
511                 char *ipv6_addr = (char *)event_cb->Data;
512                 __libnet_ip_changed_cb(CONNECTION_ADDRESS_FAMILY_IPV6, ipv6_addr);
513                 break;
514         case NET_EVENT_PROXY_ADDRESS_CHANGED:
515                 CONNECTION_LOG(CONNECTION_INFO, "Got Proxy Changed Indication");
516                 char *proxy_addr = (char *)event_cb->Data;
517                 __libnet_proxy_changed_cb(proxy_addr);
518                 break;
519
520         default:
521                 break;
522         }
523 }
524 //LCOV_EXCL_STOP
525
526 int __libnet_get_connected_count(struct _profile_list_s *profile_list)
527 {
528         int count = 0;
529         int i = 0;
530
531         for (; i < profile_list->count; i++) {
532                 if (profile_list->profiles[i].ProfileState == NET_STATE_TYPE_ONLINE ||
533                     profile_list->profiles[i].ProfileState == NET_STATE_TYPE_READY)
534                         count++;
535         }
536
537         return count;
538 }
539
540 void __libnet_copy_connected_profile(net_profile_info_t **dest, struct _profile_list_s *source)
541 {
542         int i = 0;
543
544         for (; i < source->count; i++) {
545                 if (source->profiles[i].ProfileState == NET_STATE_TYPE_ONLINE ||
546                     source->profiles[i].ProfileState == NET_STATE_TYPE_READY) {
547                         memcpy(*dest, &source->profiles[i], sizeof(net_profile_info_t));
548                         (*dest)++;
549                 }
550         }
551 }
552
553 //LCOV_EXCL_START
554 int __libnet_get_default_count(struct _profile_list_s *profile_list)
555 {
556         int count = 0;
557         int i = 0;
558
559         for (; i < profile_list->count; i++) {
560                 if (profile_list->profiles[i].ProfileInfo.Pdp.DefaultConn == TRUE)
561                         count++;
562         }
563
564         return count;
565 }
566
567 void __libnet_copy_default_profile(net_profile_info_t **dest, struct _profile_list_s *source)
568 {
569         int i = 0;
570
571         for (; i < source->count; i++) {
572                 if (source->profiles[i].ProfileInfo.Pdp.DefaultConn == TRUE) {
573                         memcpy(*dest, &source->profiles[i], sizeof(net_profile_info_t));
574                         (*dest)++;
575                 }
576         }
577 }
578 //LCOV_EXCL_STOP
579
580 int _connection_libnet_init(void)
581 {
582         int rv;
583
584         if (_connection_is_created() != true) {
585                 rv = net_register_client_ext((net_event_cb_t)__libnet_evt_cb, NET_DEVICE_DEFAULT, NULL);
586                 if (rv != NET_ERR_NONE)
587                         return rv;
588
589                 __connection_set_created(true);
590
591                 if (profile_cb_table == NULL)
592                         profile_cb_table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
593         }
594
595         return NET_ERR_NONE;
596 }
597
598 bool _connection_libnet_deinit(void)
599 {
600         if (_connection_is_created() == true) {
601                 if (net_deregister_client_ext(NET_DEVICE_DEFAULT) != NET_ERR_NONE)
602                         return false;
603
604                 __connection_set_created(false);
605
606                 if (profile_cb_table) {
607                         g_hash_table_destroy(profile_cb_table);
608                         profile_cb_table = NULL;
609                 }
610
611                 __libnet_clear_profile_list(&profile_iterator);
612
613                 if (prof_handle_list) {
614                         g_slist_free_full(prof_handle_list, g_free);
615                         prof_handle_list = NULL;
616                 }
617         }
618
619         return true;
620 }
621
622 void _connection_set_cs_tid(int tid)
623 {
624         net_set_cs_tid(tid);
625 }
626
627 void _connection_unset_cs_tid(int tid)
628 {
629         net_unset_cs_tid(tid);
630 }
631
632 bool _connection_libnet_check_profile_validity(connection_profile_h profile)
633 {
634         GSList *list;
635         int i = 0;
636
637         if (profile == NULL)
638                 return false;
639
640         for (list = prof_handle_list; list; list = list->next)
641                 if (profile == list->data) return true;
642
643         for (; i < profile_iterator.count; i++)
644                 if (profile == &profile_iterator.profiles[i]) return true;
645
646         return false;
647 }
648
649 //LCOV_EXCL_START
650 bool _connection_libnet_check_profile_cb_validity(connection_profile_h profile)
651 {
652         struct _profile_cb_s *cb_info;
653         net_profile_info_t *profile_info = profile;
654
655         if (profile == NULL)
656                 return false;
657
658         cb_info = g_hash_table_lookup(profile_cb_table, profile_info->ProfileName);
659         if (cb_info != NULL)
660                 return true;
661
662         return false;
663 }
664 //LCOV_EXCL_STOP
665
666 int _connection_libnet_get_metered_state(bool* is_metered)
667 {
668         int rv = 0;
669         int status = 0;
670
671         rv = net_get_metered_state(&status);
672         if (rv == NET_ERR_ACCESS_DENIED) {
673                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
674                 return CONNECTION_ERROR_PERMISSION_DENIED;
675         } else if (rv != NET_ERR_NONE) {
676                 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get metered state[%d]", rv);
677                 return CONNECTION_ERROR_OPERATION_FAILED;
678         }
679
680         if (status == 1)
681                 *is_metered = true;
682         else
683                 *is_metered = false;
684         return CONNECTION_ERROR_NONE;
685 }
686
687 int _connection_libnet_get_wifi_state(connection_wifi_state_e *state)
688 {
689         int rv;
690         net_wifi_state_t wlan_state;
691
692         rv = net_get_wifi_state(&wlan_state);
693         if (rv == NET_ERR_ACCESS_DENIED) {
694                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
695                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
696         } else if (rv != NET_ERR_NONE) {
697                 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get Wi-Fi state[%d]", rv); //LCOV_EXCL_LINE
698                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
699         }
700
701         switch (wlan_state) {
702         case WIFI_OFF:
703                 *state = CONNECTION_WIFI_STATE_DEACTIVATED;
704                 break;
705         case WIFI_ON:
706         case WIFI_ASSOCIATION:
707         case WIFI_CONFIGURATION:
708                 *state = CONNECTION_WIFI_STATE_DISCONNECTED;
709                 break;
710         case WIFI_CONNECTED:
711         case WIFI_DISCONNECTING:
712                 *state = CONNECTION_WIFI_STATE_CONNECTED;
713                 break;
714         default:
715                 CONNECTION_LOG(CONNECTION_ERROR, "Unknown Wi-Fi state"); //LCOV_EXCL_LINE
716                 return CONNECTION_ERROR_INVALID_OPERATION; //LCOV_EXCL_LINE
717         }
718
719         return CONNECTION_ERROR_NONE;
720 }
721
722 void _connection_libnet_set_type_changed_cb(libnet_type_changed_cb callback)
723 {
724         libnet.type_changed_cb = callback;
725 }
726
727 void _connection_libnet_set_ip_changed_cb(libnet_ip_changed_cb callback)
728 {
729         libnet.ip_changed_cb = callback;
730 }
731
732 void _connection_libnet_set_proxy_changed_cb(libnet_proxy_changed_cb callback)
733 {
734         libnet.proxy_changed_cb = callback;
735 }
736
737 //LCOV_EXCL_START
738 int _connection_libnet_get_ethernet_state(connection_ethernet_state_e *state)
739 {
740         int rv;
741         struct _profile_list_s ethernet_profiles = {0, 0, NULL};
742         rv = net_get_profile_list(NET_DEVICE_ETHERNET, &ethernet_profiles.profiles, &ethernet_profiles.count);
743         if (rv == NET_ERR_ACCESS_DENIED) {
744                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
745                 return CONNECTION_ERROR_PERMISSION_DENIED;
746         }
747
748         if (ethernet_profiles.count == 0) {
749                 *state = CONNECTION_ETHERNET_STATE_DEACTIVATED;
750                 return CONNECTION_ERROR_NONE;
751         }
752
753         switch (ethernet_profiles.profiles->ProfileState) {
754         case NET_STATE_TYPE_ONLINE:
755         case NET_STATE_TYPE_READY:
756                 *state = CONNECTION_ETHERNET_STATE_CONNECTED;
757                 break;
758         case NET_STATE_TYPE_IDLE:
759         case NET_STATE_TYPE_FAILURE:
760         case NET_STATE_TYPE_ASSOCIATION:
761         case NET_STATE_TYPE_CONFIGURATION:
762         case NET_STATE_TYPE_DISCONNECT:
763                 *state = CONNECTION_ETHERNET_STATE_DISCONNECTED;
764                 break;
765         default:
766                 __libnet_clear_profile_list(&ethernet_profiles);
767                 return CONNECTION_ERROR_OPERATION_FAILED;
768         }
769
770         __libnet_clear_profile_list(&ethernet_profiles);
771
772         return CONNECTION_ERROR_NONE;
773 }
774
775 int _connection_libnet_get_ethernet_cable_state(connection_ethernet_cable_state_e* state)
776 {
777         int rv = 0;
778         int status = 0;
779
780         rv = net_get_ethernet_cable_state(&status);
781         if (rv == NET_ERR_ACCESS_DENIED) {
782                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
783                 return CONNECTION_ERROR_PERMISSION_DENIED;
784         } else if (rv != NET_ERR_NONE) {
785                 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get ethernet cable state[%d]", rv);
786                 return CONNECTION_ERROR_OPERATION_FAILED;
787         }
788
789         if (status == 1)
790                 *state = CONNECTION_ETHERNET_CABLE_ATTACHED;
791         else
792                 *state = CONNECTION_ETHERNET_CABLE_DETACHED;
793         return CONNECTION_ERROR_NONE;
794 }
795
796 int _connection_libnet_set_ethernet_cable_state_changed_cb(
797                 libnet_ethernet_cable_state_changed_cb callback)
798 {
799         __libnet_set_ethernet_cable_state_changed_cb(callback);
800
801         return CONNECTION_ERROR_NONE;
802 }
803 //LCOV_EXCL_STOP
804
805 int _connection_libnet_get_bluetooth_state(connection_bt_state_e *state)
806 {
807         int i = 0;
808         int rv = 0;
809         struct _profile_list_s bluetooth_profiles = {0, 0, NULL};
810         rv = net_get_profile_list(NET_DEVICE_BLUETOOTH, &bluetooth_profiles.profiles, &bluetooth_profiles.count);
811         if (rv == NET_ERR_ACCESS_DENIED) {
812                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
813                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
814         }
815
816         if (bluetooth_profiles.count == 0) {
817                 *state = CONNECTION_BT_STATE_DEACTIVATED;
818                 return CONNECTION_ERROR_NONE;
819         }
820
821         //LCOV_EXCL_START
822         for (; i < bluetooth_profiles.count; i++) {
823                 switch (bluetooth_profiles.profiles[i].ProfileState) {
824                 case NET_STATE_TYPE_ONLINE:
825                 case NET_STATE_TYPE_READY:
826                         *state = CONNECTION_BT_STATE_CONNECTED;
827                         goto done;
828                 case NET_STATE_TYPE_IDLE:
829                 case NET_STATE_TYPE_FAILURE:
830                 case NET_STATE_TYPE_ASSOCIATION:
831                 case NET_STATE_TYPE_CONFIGURATION:
832                 case NET_STATE_TYPE_DISCONNECT:
833                         *state = CONNECTION_BT_STATE_DISCONNECTED;
834                         break;
835                 default:
836                         __libnet_clear_profile_list(&bluetooth_profiles);
837                         return CONNECTION_ERROR_OPERATION_FAILED;
838                 }
839         }
840         //LCOV_EXCL_STOP
841
842 done:
843         __libnet_clear_profile_list(&bluetooth_profiles);
844
845         return CONNECTION_ERROR_NONE;
846 }
847
848 int _connection_libnet_get_profile_iterator(connection_iterator_type_e type, connection_profile_iterator_h* profile_iter_h)
849 {
850         int count = 0;
851         int rv1, rv2, rv3, rv4, rv5;
852         net_profile_info_t *profiles = NULL;
853
854         struct _profile_list_s wifi_profiles = {0, 0, NULL};
855         struct _profile_list_s cellular_profiles = {0, 0, NULL};
856         struct _profile_list_s ethernet_profiles = {0, 0, NULL};
857         struct _profile_list_s bluetooth_profiles = {0, 0, NULL};
858         struct _profile_list_s mesh_profiles = {0, 0, NULL};
859
860         __libnet_clear_profile_list(&profile_iterator);
861
862         rv1 = net_get_profile_list(NET_DEVICE_WIFI, &wifi_profiles.profiles, &wifi_profiles.count);
863         if (rv1 == NET_ERR_ACCESS_DENIED) {
864                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
865                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
866         } else if (rv1 != NET_ERR_NO_SERVICE && rv1 != NET_ERR_NONE)
867                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
868
869         CONNECTION_LOG(CONNECTION_INFO, "Wi-Fi profile count: %d", wifi_profiles.count);
870
871         rv2 = net_get_profile_list(NET_DEVICE_CELLULAR, &cellular_profiles.profiles, &cellular_profiles.count);
872         if (rv2 == NET_ERR_ACCESS_DENIED) {
873                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
874                 __libnet_clear_profile_list(&wifi_profiles);
875                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
876         } else if (rv2 != NET_ERR_NO_SERVICE && rv2 != NET_ERR_NONE) {
877                 __libnet_clear_profile_list(&wifi_profiles);
878                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
879         }
880         CONNECTION_LOG(CONNECTION_INFO, "Cellular profile count: %d", cellular_profiles.count);
881
882         rv3 = net_get_profile_list(NET_DEVICE_ETHERNET, &ethernet_profiles.profiles, &ethernet_profiles.count);
883         if (rv3 == NET_ERR_ACCESS_DENIED) {
884                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
885                 __libnet_clear_profile_list(&wifi_profiles);
886                 __libnet_clear_profile_list(&cellular_profiles);
887                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
888         } else if (rv3 != NET_ERR_NO_SERVICE && rv3 != NET_ERR_NONE) {
889                 __libnet_clear_profile_list(&wifi_profiles);
890                 __libnet_clear_profile_list(&cellular_profiles);
891                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
892         }
893         CONNECTION_LOG(CONNECTION_INFO, "Ethernet profile count : %d", ethernet_profiles.count);
894
895         rv4 = net_get_profile_list(NET_DEVICE_BLUETOOTH, &bluetooth_profiles.profiles, &bluetooth_profiles.count);
896         if (rv4 == NET_ERR_ACCESS_DENIED) {
897                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
898                 __libnet_clear_profile_list(&wifi_profiles);
899                 __libnet_clear_profile_list(&cellular_profiles);
900                 __libnet_clear_profile_list(&ethernet_profiles);
901                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
902         } else if (rv4 != NET_ERR_NO_SERVICE && rv4 != NET_ERR_NONE) {
903                 __libnet_clear_profile_list(&wifi_profiles);
904                 __libnet_clear_profile_list(&cellular_profiles);
905                 __libnet_clear_profile_list(&ethernet_profiles);
906                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
907         }
908         CONNECTION_LOG(CONNECTION_INFO, "Bluetooth profile count : %d", bluetooth_profiles.count);
909
910         rv5 = net_get_profile_list(NET_DEVICE_MESH, &mesh_profiles.profiles, &mesh_profiles.count);
911         if (rv5 == NET_ERR_ACCESS_DENIED) {
912                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
913                 __libnet_clear_profile_list(&wifi_profiles);
914                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
915         } else if (rv5 != NET_ERR_NO_SERVICE && rv5 != NET_ERR_NONE) {
916                 __libnet_clear_profile_list(&wifi_profiles);
917                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
918         }
919
920         CONNECTION_LOG(CONNECTION_INFO, "Mesh profile count: %d", mesh_profiles.count);
921         *profile_iter_h = &profile_iterator;
922
923         switch (type) {
924         case CONNECTION_ITERATOR_TYPE_REGISTERED:
925                 count = wifi_profiles.count + cellular_profiles.count + ethernet_profiles.count + bluetooth_profiles.count + mesh_profiles.count;
926                 CONNECTION_LOG(CONNECTION_INFO, "Total profile count : %d", count);
927                 if (count == 0)
928                         return CONNECTION_ERROR_NONE;
929
930                 profiles = g_try_new0(net_profile_info_t, count);
931                 if (profiles == NULL) {
932                         __libnet_clear_profile_list(&wifi_profiles);
933                         __libnet_clear_profile_list(&cellular_profiles);
934                         __libnet_clear_profile_list(&ethernet_profiles);
935                         __libnet_clear_profile_list(&bluetooth_profiles);
936                         __libnet_clear_profile_list(&mesh_profiles);
937                         return CONNECTION_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
938                 }
939
940                 profile_iterator.profiles = profiles;
941
942                 if (wifi_profiles.count > 0) {
943                         memcpy(profiles, wifi_profiles.profiles,
944                                         sizeof(net_profile_info_t) * wifi_profiles.count);
945                         profiles += wifi_profiles.count;
946                 }
947
948                 if (cellular_profiles.count > 0) {
949                         memcpy(profiles, cellular_profiles.profiles,
950                                         sizeof(net_profile_info_t) * cellular_profiles.count);
951                         profiles += cellular_profiles.count;
952                 }
953
954                 if (ethernet_profiles.count > 0) {
955                         memcpy(profiles, ethernet_profiles.profiles,
956                                         sizeof(net_profile_info_t) * ethernet_profiles.count);
957                         profiles += ethernet_profiles.count;
958                 }
959
960                 if (mesh_profiles.count > 0) {
961                         memcpy(profiles, mesh_profiles.profiles,
962                                         sizeof(net_profile_info_t) * mesh_profiles.count);
963                         profiles += mesh_profiles.count;
964                 }
965
966                 if (bluetooth_profiles.count > 0)
967                         memcpy(profiles, bluetooth_profiles.profiles,
968                                         sizeof(net_profile_info_t) * bluetooth_profiles.count);
969
970                 break;
971         case CONNECTION_ITERATOR_TYPE_CONNECTED:
972                 count = __libnet_get_connected_count(&wifi_profiles);
973                 count += __libnet_get_connected_count(&cellular_profiles);
974                 count += __libnet_get_connected_count(&ethernet_profiles);
975                 count += __libnet_get_connected_count(&bluetooth_profiles);
976                 count += __libnet_get_connected_count(&mesh_profiles);
977                 CONNECTION_LOG(CONNECTION_INFO, "Total connected profile count : %d", count);
978                 if (count == 0)
979                         return CONNECTION_ERROR_NONE;
980
981                 profiles = g_try_new0(net_profile_info_t, count);
982                 if (profiles == NULL) {
983                         __libnet_clear_profile_list(&wifi_profiles);
984                         __libnet_clear_profile_list(&cellular_profiles);
985                         __libnet_clear_profile_list(&ethernet_profiles);
986                         __libnet_clear_profile_list(&bluetooth_profiles);
987                         __libnet_clear_profile_list(&mesh_profiles);
988                         return CONNECTION_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
989                 }
990
991                 profile_iterator.profiles = profiles;
992
993                 if (wifi_profiles.count > 0)
994                         __libnet_copy_connected_profile(&profiles, &wifi_profiles);
995
996                 if (cellular_profiles.count > 0)
997                         __libnet_copy_connected_profile(&profiles, &cellular_profiles);
998
999                 if (ethernet_profiles.count > 0)
1000                         __libnet_copy_connected_profile(&profiles, &ethernet_profiles);
1001
1002                 if (bluetooth_profiles.count > 0)
1003                         __libnet_copy_connected_profile(&profiles, &bluetooth_profiles);
1004
1005                 if (mesh_profiles.count > 0)
1006                         __libnet_copy_connected_profile(&profiles, &mesh_profiles);
1007
1008                 break;
1009         case CONNECTION_ITERATOR_TYPE_DEFAULT:
1010                 count = __libnet_get_default_count(&cellular_profiles);
1011                 CONNECTION_LOG(CONNECTION_INFO, "Total default profile count : %d", count); //LCOV_EXCL_LINE
1012                 if (count == 0)
1013                         return CONNECTION_ERROR_NONE;
1014
1015                 profiles = g_try_new0(net_profile_info_t, count);
1016                 if (profiles == NULL) {
1017                         __libnet_clear_profile_list(&wifi_profiles);
1018                         __libnet_clear_profile_list(&cellular_profiles);
1019                         __libnet_clear_profile_list(&ethernet_profiles);
1020                         __libnet_clear_profile_list(&bluetooth_profiles);
1021                         __libnet_clear_profile_list(&mesh_profiles);
1022                         return CONNECTION_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1023                 }
1024
1025                 profile_iterator.profiles = profiles;
1026
1027                 if (cellular_profiles.count > 0)
1028                         __libnet_copy_default_profile(&profiles, &cellular_profiles);
1029                 break;
1030         }
1031
1032         __libnet_clear_profile_list(&wifi_profiles);
1033         __libnet_clear_profile_list(&cellular_profiles);
1034         __libnet_clear_profile_list(&ethernet_profiles);
1035         __libnet_clear_profile_list(&bluetooth_profiles);
1036         __libnet_clear_profile_list(&mesh_profiles);
1037
1038         profile_iterator.count = count;
1039
1040         return CONNECTION_ERROR_NONE;
1041 }
1042
1043 int _connection_libnet_get_iterator_next(connection_profile_iterator_h profile_iter_h, connection_profile_h *profile)
1044 {
1045         if (profile_iter_h != &profile_iterator)
1046                 return CONNECTION_ERROR_INVALID_PARAMETER;
1047
1048         if (profile_iterator.count <= profile_iterator.next)
1049                 return CONNECTION_ERROR_ITERATOR_END;
1050
1051         *profile = &profile_iterator.profiles[profile_iterator.next];
1052         profile_iterator.next++;
1053
1054         return CONNECTION_ERROR_NONE;
1055 }
1056
1057 bool _connection_libnet_iterator_has_next(connection_profile_iterator_h profile_iter_h)
1058 {
1059         if (profile_iter_h != &profile_iterator)
1060                 return false;
1061
1062         if (profile_iterator.count <= profile_iterator.next)
1063                 return false;
1064
1065         return true;
1066 }
1067
1068 int _connection_libnet_destroy_iterator(connection_profile_iterator_h profile_iter_h)
1069 {
1070         if (profile_iter_h != &profile_iterator)
1071                 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1072
1073         __libnet_clear_profile_list(&profile_iterator);
1074
1075         return CONNECTION_ERROR_NONE;
1076 }
1077
1078 int _connection_libnet_get_current_profile(connection_profile_h *profile)
1079 {
1080         net_profile_info_t active_profile;
1081         int rv;
1082
1083         rv = net_get_active_net_info(&active_profile);
1084         if (rv == NET_ERR_NO_SERVICE)
1085                 return CONNECTION_ERROR_NO_CONNECTION; //LCOV_EXCL_LINE
1086         else if (rv == NET_ERR_ACCESS_DENIED) {
1087                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1088                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1089         } else if (rv != NET_ERR_NONE)
1090                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1091
1092         *profile = g_try_malloc0(sizeof(net_profile_info_t));
1093         if (*profile == NULL)
1094                 return CONNECTION_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1095
1096         memcpy(*profile, &active_profile, sizeof(net_profile_info_t));
1097         prof_handle_list = g_slist_append(prof_handle_list, *profile);
1098
1099         return CONNECTION_ERROR_NONE;
1100 }
1101
1102 int _connection_libnet_reset_profile(connection_reset_option_e type,
1103                 connection_cellular_subscriber_id_e id, connection_reset_cb callback, void *user_data)
1104 {
1105         int rv;
1106
1107         rv = net_reset_profile(type, id);
1108         if (rv == NET_ERR_ACCESS_DENIED) {
1109                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1110                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1111         } else if (rv != NET_ERR_NONE) {
1112                 CONNECTION_LOG(CONNECTION_ERROR, "Failed to add profile[%d]", rv); //LCOV_EXCL_LINE
1113                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1114         }
1115
1116         __libnet_set_reset_profile_cb(callback, user_data);
1117
1118         return CONNECTION_ERROR_NONE;
1119 }
1120
1121 int _connection_libnet_open_profile(connection_profile_h profile,
1122                 connection_opened_cb callback, void* user_data)
1123 {
1124         int rv;
1125
1126         if (!(_connection_libnet_check_profile_validity(profile))) {
1127                 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter"); //LCOV_EXCL_LINE
1128                 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1129         }
1130
1131         net_profile_info_t *profile_info = profile;
1132
1133         if (profile_info->profile_type == NET_DEVICE_MESH)
1134                 rv = net_open_mesh_connection_with_profile(profile_info->ProfileName);
1135         else
1136                 rv = net_open_connection_with_profile(profile_info->ProfileName);
1137
1138         if (rv == NET_ERR_ACCESS_DENIED) {
1139                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1140                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1141         } else if (rv != NET_ERR_NONE)
1142                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1143
1144         __libnet_set_opened_cb(callback, user_data);
1145
1146         return CONNECTION_ERROR_NONE;
1147 }
1148
1149 int _connection_libnet_get_cellular_service_profile(
1150                 connection_cellular_service_type_e type, connection_profile_h *profile)
1151 {
1152         int i = 0, j = 0;
1153         int rv = NET_ERR_NONE;
1154 #if defined TIZEN_DUALSIM_ENABLE
1155         int default_subscriber_id = 0;
1156         char subscriber_id[3];
1157 #endif
1158
1159         struct _profile_list_s cellular_profiles = { 0, 0, NULL };
1160         net_service_type_t service_type = _connection_profile_convert_to_libnet_cellular_service_type(type);
1161
1162         rv = net_get_profile_list(NET_DEVICE_CELLULAR, &cellular_profiles.profiles, &cellular_profiles.count);
1163         if (rv == NET_ERR_ACCESS_DENIED) {
1164                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1165                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1166         } else if (rv != NET_ERR_NONE) {
1167                 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get profile list (%d)", rv); //LCOV_EXCL_LINE
1168                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1169         }
1170
1171 #if defined TIZEN_DUALSIM_ENABLE
1172         if (vconf_get_int(VCONF_TELEPHONY_DEFAULT_DATA_SERVICE,
1173                                                 &default_subscriber_id) != 0) {
1174                 CONNECTION_LOG(CONNECTION_ERROR,
1175                                                 "Failed to get VCONF_TELEPHONY_DEFAULT_DATA_SERVICE");
1176                 __libnet_clear_profile_list(&cellular_profiles); //LCOV_EXCL_LINE
1177                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1178         }
1179
1180         g_snprintf(subscriber_id, sizeof(subscriber_id), "%d", default_subscriber_id);
1181 #endif
1182
1183         for (i = 0; i < cellular_profiles.count; i++)
1184                 if (cellular_profiles.profiles[i].ProfileInfo.Pdp.ServiceType == service_type)
1185 #if defined TIZEN_DUALSIM_ENABLE
1186                         if (g_str_has_suffix(
1187                                         cellular_profiles.profiles[i].ProfileInfo.Pdp.PSModemPath,
1188                                         subscriber_id) == TRUE)
1189 #endif
1190                                 break;
1191
1192         if (i >= cellular_profiles.count) {
1193                 __libnet_clear_profile_list(&cellular_profiles); //LCOV_EXCL_LINE
1194                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1195         }
1196
1197         *profile = g_try_malloc0(sizeof(net_profile_info_t));
1198         if (*profile == NULL) {
1199                 __libnet_clear_profile_list(&cellular_profiles); //LCOV_EXCL_LINE
1200                 return CONNECTION_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1201         }
1202
1203         memcpy(*profile, &cellular_profiles.profiles[i], sizeof(net_profile_info_t));
1204
1205         if (cellular_profiles.profiles[i].ProfileInfo.Pdp.DefaultConn)
1206                 goto done;
1207
1208         //LCOV_EXCL_START
1209         if (type != CONNECTION_CELLULAR_SERVICE_TYPE_INTERNET &&
1210             type != CONNECTION_CELLULAR_SERVICE_TYPE_PREPAID_INTERNET)
1211                 goto done;
1212
1213         for (j = 0; j < cellular_profiles.count; j++) {
1214                 if (i == j)
1215                         continue;
1216
1217                 if (cellular_profiles.profiles[j].ProfileInfo.Pdp.ServiceType != service_type)
1218                         continue;
1219
1220                 if (cellular_profiles.profiles[j].ProfileInfo.Pdp.DefaultConn) {
1221                         memcpy(*profile, &cellular_profiles.profiles[j], sizeof(net_profile_info_t));
1222                         goto done;
1223                 }
1224         }
1225         //LCOV_EXCL_STOP
1226
1227 done:
1228         __libnet_clear_profile_list(&cellular_profiles);
1229         prof_handle_list = g_slist_append(prof_handle_list, *profile);
1230
1231         return CONNECTION_ERROR_NONE;
1232 }
1233
1234 int _connection_libnet_set_cellular_service_profile_sync(connection_cellular_service_type_e type, connection_profile_h profile)
1235 {
1236         int rv;
1237
1238         if (!(_connection_libnet_check_profile_validity(profile))) {
1239                 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter"); //LCOV_EXCL_LINE
1240                 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1241         }
1242
1243         net_profile_info_t *profile_info = profile;
1244         connection_cellular_service_type_e service_type;
1245
1246         service_type = _profile_convert_to_connection_cellular_service_type(profile_info->ProfileInfo.Pdp.ServiceType);
1247
1248         if (service_type != type)
1249                 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1250
1251         rv = net_set_default_cellular_service_profile(profile_info->ProfileName);
1252         if (rv == NET_ERR_ACCESS_DENIED) {
1253                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1254                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1255         } else if (rv != NET_ERR_NONE)
1256                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1257
1258         return CONNECTION_ERROR_NONE;
1259 }
1260
1261 int _connection_libnet_set_cellular_service_profile_async(connection_cellular_service_type_e type,
1262                         connection_profile_h profile, connection_set_default_cb callback, void* user_data)
1263 {
1264         int rv;
1265
1266         if (!(_connection_libnet_check_profile_validity(profile))) {
1267                 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter"); //LCOV_EXCL_LINE
1268                 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1269         }
1270
1271         net_profile_info_t *profile_info = profile;
1272         connection_cellular_service_type_e service_type;
1273
1274         service_type = _profile_convert_to_connection_cellular_service_type(profile_info->ProfileInfo.Pdp.ServiceType);
1275
1276         if (service_type != type)
1277                 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1278
1279         rv = net_set_default_cellular_service_profile_async(profile_info->ProfileName);
1280         if (rv == NET_ERR_ACCESS_DENIED) {
1281                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1282                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1283         } else if (rv != NET_ERR_NONE)
1284                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1285
1286         __libnet_set_default_cb(callback, user_data);
1287
1288         return CONNECTION_ERROR_NONE;
1289 }
1290
1291 int _connection_libnet_close_profile(connection_profile_h profile, connection_closed_cb callback, void *user_data)
1292 {
1293         int rv;
1294
1295         if (!(_connection_libnet_check_profile_validity(profile))) {
1296                 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter"); //LCOV_EXCL_LINE
1297                 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1298         }
1299
1300         net_profile_info_t *profile_info = profile;
1301
1302         if (profile_info->profile_type == NET_DEVICE_MESH)
1303                 rv = net_close_mesh_connection(profile_info->ProfileName);
1304         else
1305                 rv = net_close_connection(profile_info->ProfileName);
1306
1307         if (rv == NET_ERR_ACCESS_DENIED) {
1308                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1309                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1310         } else if (rv != NET_ERR_NONE)
1311                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1312
1313         __libnet_set_closed_cb(callback, user_data);
1314
1315         return CONNECTION_ERROR_NONE;
1316 }
1317
1318 int _connection_libnet_add_route(const char *interface_name, const char *host_address)
1319 {
1320         int rv;
1321         char *endstr = NULL;
1322         int address_family = 0;
1323
1324         address_family = AF_INET;
1325
1326         endstr = strrchr(host_address, '.');
1327         if (endstr == NULL ||
1328                         strcmp(endstr, ".0") == 0 ||
1329                         strncmp(host_address, "0.", 2) == 0 ||
1330                         strstr(host_address, "255") != NULL) {
1331                 CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed\n"); //LCOV_EXCL_LINE
1332                 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1333         }
1334
1335         rv = net_add_route(host_address, interface_name, address_family);
1336         if (rv == NET_ERR_ACCESS_DENIED) {
1337                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1338                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1339         } else if (rv != NET_ERR_NONE)
1340                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1341
1342         return CONNECTION_ERROR_NONE;
1343 }
1344
1345 int _connection_libnet_remove_route(const char *interface_name, const char *host_address)
1346 {
1347         int rv;
1348         char *endstr = strrchr(host_address, '.');
1349         int address_family = 0;
1350
1351         address_family = AF_INET;
1352
1353         endstr = strrchr(host_address, '.');
1354         if (endstr == NULL ||
1355                 strcmp(endstr, ".0") == 0 ||
1356                 strncmp(host_address, "0.", 2) == 0 ||
1357                 strstr(host_address, ".0.") != NULL || strstr(host_address, "255") != NULL) {
1358                 CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed"); //LCOV_EXCL_LINE
1359                 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1360         }
1361
1362         rv = net_remove_route(host_address, interface_name, address_family);
1363         if (rv == NET_ERR_ACCESS_DENIED) {
1364                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1365                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1366         } else if (rv != NET_ERR_NONE)
1367                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1368
1369         return CONNECTION_ERROR_NONE;
1370 }
1371
1372 int _connection_libnet_add_route_ipv6(const char *interface_name, const char *host_address, const char *gateway)
1373 {
1374         int rv;
1375         int address_family = 0;
1376
1377         address_family = AF_INET6;
1378
1379         if (strncmp(host_address, "fe80:", 5) == 0 ||
1380                 strncmp(host_address, "ff00:", 5) == 0 ||
1381                 strncmp(host_address, "::", 2) == 0) {
1382                 CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed\n"); //LCOV_EXCL_LINE
1383                 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1384         }
1385
1386         rv = net_add_route_ipv6(host_address, interface_name, address_family, gateway);
1387         if (rv == NET_ERR_ACCESS_DENIED) {
1388                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1389                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1390         } else if (rv != NET_ERR_NONE)
1391                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1392
1393         return CONNECTION_ERROR_NONE;
1394 }
1395
1396 int _connection_libnet_remove_route_ipv6(const char *interface_name, const char *host_address, const char *gateway)
1397 {
1398         int rv;
1399         int address_family = 0;
1400
1401         address_family = AF_INET6;
1402
1403         if (strncmp(host_address, "fe80:", 5) == 0 ||
1404                 strncmp(host_address, "ff00:", 5) == 0 ||
1405                 strncmp(host_address, "::", 2) == 0) {
1406                 CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed\n"); //LCOV_EXCL_LINE
1407                 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1408         }
1409
1410         rv = net_remove_route_ipv6(host_address, interface_name, address_family, gateway);
1411         if (rv == NET_ERR_ACCESS_DENIED) {
1412                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1413                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1414         } else if (rv != NET_ERR_NONE)
1415                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1416
1417         return CONNECTION_ERROR_NONE;
1418 }
1419
1420 int _connection_libnet_add_route_entry(connection_address_family_e address_family,
1421                 const char *interface_name, const char *host_address, const char *gateway)
1422 {
1423         int rv;
1424         char *endstr = NULL;
1425         int address_family_type = 0;
1426
1427         if (address_family == CONNECTION_ADDRESS_FAMILY_IPV4)
1428                 address_family_type = AF_INET;
1429         else
1430                 address_family_type = AF_INET6;
1431
1432         if (address_family == CONNECTION_ADDRESS_FAMILY_IPV4) {
1433
1434                 endstr = strrchr(host_address, '.');
1435                 if (endstr == NULL ||
1436                                 strcmp(endstr, ".0") == 0 ||
1437                                 strncmp(host_address, "0.", 2) == 0 ||
1438                                 strstr(host_address, "255") != NULL) {
1439                         CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed\n"); //LCOV_EXCL_LINE
1440                         return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1441                 }
1442
1443                 rv = net_add_route_entry(host_address, interface_name, address_family_type, gateway);
1444                 if (rv == NET_ERR_ACCESS_DENIED) {
1445                         CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1446                         return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1447                 } else if (rv != NET_ERR_NONE)
1448                         return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1449
1450         } else {
1451
1452                 if (strncmp(host_address, "fe80:", 5) == 0 ||
1453                         strncmp(host_address, "ff00:", 5) == 0 ||
1454                         strncmp(host_address, "::", 2) == 0) {
1455                         CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed\n"); //LCOV_EXCL_LINE
1456                         return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1457                 }
1458
1459                 rv = net_add_route_ipv6(host_address, interface_name, address_family_type, gateway);
1460                 if (rv == NET_ERR_ACCESS_DENIED) {
1461                         CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1462                         return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1463                 } else if (rv != NET_ERR_NONE)
1464                         return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1465         }
1466
1467         return CONNECTION_ERROR_NONE;
1468 }
1469
1470 int _connection_libnet_remove_route_entry(connection_address_family_e address_family,
1471                 const char *interface_name, const char *host_address, const char *gateway)
1472 {
1473         int rv;
1474         char *endstr = strrchr(host_address, '.');
1475         int address_family_type = 0;
1476
1477         if (address_family == CONNECTION_ADDRESS_FAMILY_IPV4)
1478                 address_family_type = AF_INET;
1479         else
1480                 address_family_type = AF_INET6;
1481
1482         if (address_family == CONNECTION_ADDRESS_FAMILY_IPV4) {
1483                 endstr = strrchr(host_address, '.');
1484                 if (endstr == NULL ||
1485                         strcmp(endstr, ".0") == 0 ||
1486                         strncmp(host_address, "0.", 2) == 0 ||
1487                         strstr(host_address, ".0.") != NULL || strstr(host_address, "255") != NULL) {
1488                         CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed"); //LCOV_EXCL_LINE
1489                         return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1490                 }
1491
1492                 rv = net_remove_route_entry(host_address, interface_name, address_family_type, gateway);
1493                 if (rv == NET_ERR_ACCESS_DENIED) {
1494                         CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1495                         return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1496                 } else if (rv != NET_ERR_NONE)
1497                         return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1498
1499         } else {
1500
1501                 if (strncmp(host_address, "fe80:", 5) == 0 ||
1502                         strncmp(host_address, "ff00:", 5) == 0 ||
1503                         strncmp(host_address, "::", 2) == 0) {
1504                         CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed\n"); //LCOV_EXCL_LINE
1505                         return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1506                 }
1507
1508                 rv = net_remove_route_ipv6(host_address, interface_name, address_family_type, gateway);
1509                 if (rv == NET_ERR_ACCESS_DENIED) {
1510                         CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1511                         return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1512                 } else if (rv != NET_ERR_NONE)
1513                         return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1514         }
1515
1516         return CONNECTION_ERROR_NONE;
1517 }
1518
1519 void _connection_libnet_add_to_profile_list(connection_profile_h profile)
1520 {
1521         prof_handle_list = g_slist_append(prof_handle_list, profile);
1522 }
1523
1524 void _connection_libnet_remove_from_profile_list(connection_profile_h profile)
1525 {
1526         prof_handle_list = g_slist_remove(prof_handle_list, profile);
1527         g_free(profile);
1528 }
1529
1530 bool _connection_libnet_add_to_profile_cb_list(connection_profile_h profile,
1531                 connection_profile_state_changed_cb callback, void *user_data)
1532 {
1533         net_profile_info_t *profile_info = profile;
1534         char *profile_name = g_strdup(profile_info->ProfileName);
1535
1536         struct _profile_cb_s *profile_cb_info = g_try_malloc0(sizeof(struct _profile_cb_s));
1537         if (profile_cb_info == NULL) {
1538                 g_free(profile_name); //LCOV_EXCL_LINE
1539                 return false; //LCOV_EXCL_LINE
1540         }
1541
1542         profile_cb_info->callback = callback;
1543         profile_cb_info->user_data = user_data;
1544         profile_cb_info->state = _profile_convert_to_cp_state(profile_info->ProfileState);
1545
1546         g_hash_table_replace(profile_cb_table, profile_name, profile_cb_info);
1547
1548         return true;
1549 }
1550
1551 bool _connection_libnet_remove_from_profile_cb_list(connection_profile_h profile)
1552 {
1553         net_profile_info_t *profile_info = profile;
1554
1555         if (g_hash_table_remove(profile_cb_table, profile_info->ProfileName) == TRUE)
1556                 return true;
1557
1558         return false; //LCOV_EXCL_LINE
1559 }
1560
1561 int _connection_libnet_set_statistics(net_device_t device_type, net_statistics_type_e statistics_type)
1562 {
1563         int rv;
1564         rv = net_set_statistics(device_type, statistics_type);
1565         if (rv == NET_ERR_ACCESS_DENIED) {
1566                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1567                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1568         } else if (rv != NET_ERR_NONE)
1569                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1570
1571         return CONNECTION_ERROR_NONE;
1572 }
1573
1574 int _connection_libnet_get_statistics(net_statistics_type_e statistics_type, unsigned long long *size)
1575 {
1576         int rv;
1577         rv = net_get_statistics(NET_DEVICE_WIFI, statistics_type, size);
1578         if (rv == NET_ERR_ACCESS_DENIED) {
1579                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1580                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1581         } else if (rv != NET_ERR_NONE)
1582                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1583
1584         return CONNECTION_ERROR_NONE;
1585 }
1586
1587 int _connection_libnet_set_cellular_subscriber_id(connection_profile_h profile,
1588                 connection_cellular_subscriber_id_e sim_id)
1589 {
1590         char *modem_path = NULL;
1591         net_profile_info_t *profile_info = (net_profile_info_t *)profile;
1592
1593         if (net_get_cellular_modem_object_path(&modem_path, sim_id) != NET_ERR_NONE) {
1594                 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get subscriber[%d]", sim_id); //LCOV_EXCL_LINE
1595                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1596         }
1597
1598         if (!modem_path) {
1599                 CONNECTION_LOG(CONNECTION_ERROR, "NULL modem object path"); //LCOV_EXCL_LINE
1600                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1601         }
1602
1603         g_strlcpy(profile_info->ProfileInfo.Pdp.PSModemPath, modem_path,
1604                                 NET_PROFILE_NAME_LEN_MAX);
1605         g_free(modem_path);
1606
1607         return CONNECTION_ERROR_NONE;
1608 }
1609
1610 static void __connection_idle_destroy_cb(gpointer data)
1611 {
1612         if (!data)
1613                 return;
1614
1615         managed_idler_list = g_slist_remove(managed_idler_list, data);
1616         g_free(data);
1617 }
1618
1619 static gboolean __connection_idle_cb(gpointer user_data)
1620 {
1621         struct managed_idle_data *data = (struct managed_idle_data *)user_data;
1622
1623         if (!data)
1624                 return FALSE;
1625
1626         return data->func(data->user_data);
1627 }
1628
1629 guint _connection_callback_add(GSourceFunc func, gpointer user_data)
1630 {
1631         guint id;
1632         struct managed_idle_data *data;
1633         GMainContext *context;
1634         GSource *src;
1635
1636         if (!func)
1637                 return 0;
1638
1639         data = g_try_new0(struct managed_idle_data, 1);
1640         if (!data)
1641                 return 0;
1642
1643         data->func = func;
1644         data->user_data = user_data;
1645
1646         context = g_main_context_get_thread_default();
1647         src = g_idle_source_new();
1648         g_source_set_callback(src, __connection_idle_cb, data,
1649                 __connection_idle_destroy_cb);
1650         id = g_source_attach(src, context);
1651         g_source_unref(src);
1652         if (!id) {
1653                 g_free(data);
1654                 return id;
1655         }
1656
1657         data->id = id;
1658
1659         managed_idler_list = g_slist_append(managed_idler_list, data);
1660
1661         return id;
1662 }
1663
1664 void _connection_callback_cleanup(void)
1665 {
1666         GSList *cur = managed_idler_list;
1667         GSource *src;
1668         struct managed_idle_data *data;
1669
1670         while (cur) {
1671                 //LCOV_EXCL_START
1672                 GSList *next = cur->next;
1673                 data = (struct managed_idle_data *)cur->data;
1674
1675                 src = g_main_context_find_source_by_id(g_main_context_default(), data->id);
1676                 if (src) {
1677                         g_source_destroy(src);
1678                         cur = managed_idler_list;
1679                 } else
1680                         cur = next;
1681                 //LCOV_EXCL_STOP
1682         }
1683
1684         g_slist_free(managed_idler_list);
1685         managed_idler_list = NULL;
1686 }
1687
1688 int _connection_libnet_check_get_privilege()
1689 {
1690         int rv;
1691
1692         rv = net_check_get_privilege();
1693         if (rv == NET_ERR_ACCESS_DENIED) {
1694                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1695                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1696         } else if (rv != NET_ERR_NONE)
1697                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1698
1699         return CONNECTION_ERROR_NONE;
1700 }
1701
1702 int _connection_libnet_check_profile_privilege()
1703 {
1704         int rv;
1705
1706         rv = net_check_profile_privilege();
1707         if (rv == NET_ERR_ACCESS_DENIED) {
1708                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1709                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1710         } else if (rv != NET_ERR_NONE)
1711                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1712
1713         return CONNECTION_ERROR_NONE;
1714 }
1715
1716 bool __libnet_check_feature_supported(const char *key, connection_supported_feature_e feature)
1717 {
1718         if (!connection_is_feature_checked[feature]) {
1719                 if (system_info_get_platform_bool(key, &connection_feature_supported[feature]) < 0) {
1720                         CONNECTION_LOG(CONNECTION_ERROR, "Error - Feature getting from System Info"); //LCOV_EXCL_LINE
1721                         set_last_result(CONNECTION_ERROR_OPERATION_FAILED); //LCOV_EXCL_LINE
1722                         return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1723                 }
1724                 connection_is_feature_checked[feature] = true;
1725         }
1726         return connection_feature_supported[feature];
1727 }
1728
1729 int _connection_check_feature_supported(const char *feature_name, ...)
1730 {
1731         va_list list;
1732         const char *key;
1733         bool value = false;
1734         bool feature_supported = false;
1735
1736         va_start(list, feature_name);
1737         key = feature_name;
1738         while (1) {
1739                 if (strcmp(key, TELEPHONY_FEATURE) == 0)
1740                         value = __libnet_check_feature_supported(key, CONNECTION_SUPPORTED_FEATURE_TELEPHONY);
1741                 if (strcmp(key, WIFI_FEATURE) == 0)
1742                         value = __libnet_check_feature_supported(key, CONNECTION_SUPPORTED_FEATURE_WIFI);
1743                 if (strcmp(key, TETHERING_BLUETOOTH_FEATURE) == 0)
1744                         value = __libnet_check_feature_supported(key, CONNECTION_SUPPORTED_FEATURE_TETHERING_BLUETOOTH);
1745                 if (strcmp(key, ETHERNET_FEATURE) == 0)
1746                         value = __libnet_check_feature_supported(key, CONNECTION_SUPPORTED_FEATURE_ETHERNET);
1747
1748                 feature_supported |= value;
1749                 key = va_arg(list, const char *);
1750                 if (!key) break;
1751         }
1752         if (!feature_supported) {
1753                 CONNECTION_LOG(CONNECTION_ERROR, "Error - Feature is not supported");
1754                 set_last_result(CONNECTION_ERROR_NOT_SUPPORTED);
1755                 va_end(list);
1756                 return CONNECTION_ERROR_NOT_SUPPORTED;
1757         }
1758
1759         va_end(list);
1760         set_last_result(CONNECTION_ERROR_NONE);
1761         return CONNECTION_ERROR_NONE;
1762 }
1763
1764 int _connection_libnet_start_tcpdump(void)
1765 {
1766         connection_error_e result = CONNECTION_ERROR_NONE;
1767         net_err_t ret = NET_ERR_NONE;
1768
1769         ret = net_start_tcpdump();
1770         result = __libnet_convert_to_cp_error_type(ret);
1771
1772         return result;
1773 }
1774
1775 int _connection_libnet_stop_tcpdump(void)
1776 {
1777         connection_error_e result = CONNECTION_ERROR_NONE;
1778         net_err_t ret = NET_ERR_NONE;
1779
1780         ret = net_stop_tcpdump();
1781         result = __libnet_convert_to_cp_error_type(ret);
1782
1783         return result;
1784 }
1785
1786 int _connection_libnet_get_tcpdump_state(gboolean *tcpdump_state)
1787 {
1788         connection_error_e result = CONNECTION_ERROR_NONE;
1789         net_err_t ret = NET_ERR_NONE;
1790
1791         ret = net_get_tcpdump_state(tcpdump_state);
1792         result = __libnet_convert_to_cp_error_type(ret);
1793
1794         return result;
1795 }