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