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