Fix null dereferencing and unchecked return value
[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_DHCP_STATE_CHANGED:
580                 CONNECTION_LOG(CONNECTION_INFO, "Got DHCP Changed Indication");
581                 net_dhcp_state_info_t *dhcp_state_info = (net_dhcp_state_info_t*)event_cb->data;
582                 connection_dhcp_state_e dhcp_state = CONNECTION_DHCP_STATE_UNKNOWN;
583                 connection_error_e result = CONNECTION_ERROR_NONE;
584
585                 if (!dhcp_state_info || !dhcp_state_info->ifname || !dhcp_state_info->state) {
586                         CONN_UNLOCK;
587                         return;
588                 }
589
590                 if (conn_handle->dhcp_state_changed_callback) {
591                         if (g_strcmp0(dhcp_state_info->state, "DHCP_STARTED") == 0) {
592                                 result = CONNECTION_ERROR_NONE;
593                                 dhcp_state = CONNECTION_DHCP_STATE_STARTED;
594                         } else if (g_strcmp0(dhcp_state_info->state, "DHCP_SUCCESS") == 0) {
595                                 result = CONNECTION_ERROR_NONE;
596                                 dhcp_state = CONNECTION_DHCP_STATE_FINISHED;
597                         } else if (g_strcmp0(dhcp_state_info->state, "DHCP_FAIL") == 0) {
598                                 result = CONNECTION_ERROR_DHCP_FAILED;
599                                 dhcp_state = CONNECTION_DHCP_STATE_FINISHED;
600                         } else {
601                                 result = CONNECTION_ERROR_NONE;
602                                 dhcp_state = CONNECTION_DHCP_STATE_UNKNOWN;
603                         }
604
605                         _connection_handle_ref(conn_handle);
606                         CONN_UNLOCK;
607
608                         conn_handle->dhcp_state_changed_callback(
609                                         dhcp_state,
610                                         dhcp_state_info->ifname,
611                                         result,
612                                         conn_handle->dhcp_state_changed_user_data);
613
614                         CONN_LOCK;
615                         _connection_handle_unref(conn_handle);
616                         CONN_UNLOCK;
617
618                         return;
619                 }
620                 break;
621         case NET_EVENT_INTERNET_ONLINE_IND:
622         case NET_EVENT_INTERNET_OFFLINE_IND:
623                 CONNECTION_LOG(CONNECTION_INFO, "Got Internet State Changed Indication: %s",
624                                 event_cb->event == NET_EVENT_INTERNET_ONLINE_IND ? "Online" : "Offline");
625                 net_device_t *device_type = (net_device_t *) event_cb->data;
626
627                 if (conn_handle->internet_state_changed_callback) {
628                         net_profile_info_t active_profile;
629                         int rv;
630
631                         rv = net_get_active_net_info(conn_handle->network_info_handle, &active_profile);
632
633                         if (rv == NET_ERR_NO_SERVICE && event_cb->event == NET_EVENT_INTERNET_OFFLINE_IND) {
634
635                                 _connection_handle_ref(conn_handle);
636                                 CONN_UNLOCK;
637
638                                 conn_handle->internet_state_changed_callback(CONNECTION_INTERNET_STATE_OFFLINE,
639                                         conn_handle->internet_state_changed_user_data); //LCOV_EXCL_LINE
640
641                                 CONN_LOCK;
642                                 _connection_handle_unref(conn_handle);
643                                 CONN_UNLOCK;
644
645                                 return;
646                         } else if (rv == NET_ERR_ACCESS_DENIED) {
647                                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
648                                 break;
649                         } else if (rv != NET_ERR_NONE) {
650                                 CONNECTION_LOG(CONNECTION_ERROR, "Unable to get Default profile handle"); //LCOV_EXCL_LINE
651                                 break; //LCOV_EXCL_LINE
652                         }
653
654                         if (event_cb->event == NET_EVENT_INTERNET_ONLINE_IND) {
655                                 if (active_profile.profile_state == NET_STATE_TYPE_ONLINE &&
656                                                 active_profile.profile_type == *device_type) {
657
658                                         _connection_handle_ref(conn_handle);
659                                         CONN_UNLOCK;
660
661                                         conn_handle->internet_state_changed_callback(CONNECTION_INTERNET_STATE_ONLINE,
662                                                         conn_handle->internet_state_changed_user_data);
663
664                                         CONN_LOCK;
665                                         _connection_handle_unref(conn_handle);
666                                         CONN_UNLOCK;
667
668                                         return;
669                                 }
670                         } else {
671                                 if (active_profile.profile_state != NET_STATE_TYPE_ONLINE) {
672
673                                         _connection_handle_ref(conn_handle);
674                                         CONN_UNLOCK;
675
676                                         conn_handle->internet_state_changed_callback(CONNECTION_INTERNET_STATE_OFFLINE,
677                                                         conn_handle->internet_state_changed_user_data);
678
679                                         CONN_LOCK;
680                                         _connection_handle_unref(conn_handle);
681                                         CONN_UNLOCK;
682
683                                         return;
684                                 }
685                         }
686                 }
687                 break;
688
689         default:
690                 break;
691         }
692
693         CONN_UNLOCK;
694 }
695 //LCOV_EXCL_STOP
696
697 static int __libnet_get_connected_count(struct _profile_list_s *profile_list)
698 {
699         int count = 0;
700         int i = 0;
701
702         for (; i < profile_list->count; i++) {
703                 if (profile_list->profiles[i].profile_state == NET_STATE_TYPE_ONLINE ||
704                     profile_list->profiles[i].profile_state == NET_STATE_TYPE_READY)
705                         count++;
706         }
707
708         return count;
709 }
710
711 static void __libnet_copy_connected_profile(net_profile_info_t **dest, struct _profile_list_s *source)
712 {
713         int i = 0;
714
715         for (; i < source->count; i++) {
716                 if (source->profiles[i].profile_state == NET_STATE_TYPE_ONLINE ||
717                     source->profiles[i].profile_state == NET_STATE_TYPE_READY) {
718                         memcpy(*dest, &source->profiles[i], sizeof(net_profile_info_t));
719                         (*dest)++;
720                 }
721         }
722 }
723
724 //LCOV_EXCL_START
725 static int __libnet_get_default_count(struct _profile_list_s *profile_list)
726 {
727         int count = 0;
728         int i = 0;
729
730         for (; i < profile_list->count; i++) {
731                 if (profile_list->profiles[i].profile_type == NET_DEVICE_CELLULAR) {
732                         if (profile_list->profiles[i].profile_info.pdp.default_conn == TRUE)
733                                 count++;
734                 }
735         }
736
737         return count;
738 }
739
740 static void __libnet_copy_default_profile(net_profile_info_t **dest, struct _profile_list_s *source)
741 {
742         int i = 0;
743
744         for (; i < source->count; i++) {
745                 if (source->profiles[i].profile_type == NET_DEVICE_CELLULAR) {
746                         if (source->profiles[i].profile_info.pdp.default_conn == TRUE) {
747                                 memcpy(*dest, &source->profiles[i], sizeof(net_profile_info_t));
748                                 (*dest)++;
749                         }
750                 }
751         }
752 }
753
754 static void __connection_dummy_profile_state_changed_cb(connection_profile_state_e state,
755                 void *user_data)
756 {
757         CONNECTION_LOG(CONNECTION_INFO, "Dummy callback");
758 }
759 //LCOV_EXCL_STOP
760
761 static int __profile_cb_table_value_destroy(gpointer data)
762 {
763         struct _profile_cb_s *profile_cb_data = (struct _profile_cb_s *)data;
764
765         g_free(profile_cb_data);
766
767         return G_SOURCE_REMOVE;
768 }
769
770 int _connection_libnet_init(connection_handle_s *conn_handle)
771 {
772         int rv;
773
774         rv = net_register_client(&(conn_handle->network_info_handle),
775                                 (net_event_cb_t)__libnet_evt_cb, conn_handle);
776         if (rv != NET_ERR_NONE)
777                 return rv;
778
779         if (profile_cb_table == NULL)
780                 profile_cb_table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
781
782         if (!in_container && access(CONTAINER_FILE, F_OK) == 0)
783                 in_container = TRUE; //LCOV_EXCL_LINE
784
785         return NET_ERR_NONE;
786 }
787
788 bool _connection_libnet_deinit(connection_handle_s *conn_handle, bool is_empty)
789 {
790         net_deregister_client(conn_handle->network_info_handle);
791
792         if (is_empty) {
793                 if (profile_cb_table) {
794                         g_hash_table_destroy(profile_cb_table);
795                         profile_cb_table = NULL;
796                 }
797
798                 __libnet_clear_profile_list(&profile_iterator);
799
800                 if (prof_handle_list) {
801                         g_slist_free_full(prof_handle_list, g_free);
802                         prof_handle_list = NULL;
803                 }
804         }
805
806         return true;
807 }
808
809 //LCOV_EXCL_START
810 void _connection_set_cs_tid(int tid, connection_handle_s *conn_handle)
811 {
812         net_set_cs_tid(tid, conn_handle->network_info_handle);
813 }
814
815 void _connection_unset_cs_tid(int tid, connection_handle_s *conn_handle)
816 {
817         net_unset_cs_tid(tid, conn_handle->network_info_handle);
818 }
819 //LCOV_EXCL_STOP
820
821 bool _connection_libnet_check_profile_validity(connection_profile_h profile)
822 {
823         GSList *list;
824         int i = 0;
825
826         if (profile == NULL)
827                 return false;
828
829         for (list = prof_handle_list; list; list = list->next)
830                 if (profile == list->data) return true;
831
832         for (; i < profile_iterator.count; i++)
833                 if (profile == &profile_iterator.profiles[i]) return true;
834
835         return false;
836 }
837
838 int _connection_libnet_get_metered_state(connection_handle_s *conn_handle, bool* is_metered)
839 {
840         int rv = 0;
841         int status = 0;
842
843         rv = net_get_metered_state(conn_handle->network_info_handle, &status);
844         if (rv == NET_ERR_ACCESS_DENIED) {
845                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
846                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
847         } else if (rv != NET_ERR_NONE) {
848                 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get metered state[%d]", rv); //LCOV_EXCL_LINE
849                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
850         }
851
852         if (status == 1)
853                 *is_metered = true; //LCOV_EXCL_LINE
854         else
855                 *is_metered = false;
856         return CONNECTION_ERROR_NONE;
857 }
858
859 int _connection_libnet_get_wifi_state(connection_handle_s *conn_handle, connection_wifi_state_e *state)
860 {
861         int rv;
862         net_wifi_state_t wlan_state;
863
864         rv = net_get_wifi_state(conn_handle->network_info_handle, &wlan_state);
865         if (rv == NET_ERR_ACCESS_DENIED) {
866                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
867                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
868         } else if (rv != NET_ERR_NONE) {
869                 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get Wi-Fi state[%d]", rv); //LCOV_EXCL_LINE
870                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
871         }
872
873         switch (wlan_state) {
874         case WIFI_OFF:
875                 *state = CONNECTION_WIFI_STATE_DEACTIVATED;
876                 break;
877         case WIFI_ON:
878         case WIFI_ASSOCIATION:
879         case WIFI_CONFIGURATION:
880                 *state = CONNECTION_WIFI_STATE_DISCONNECTED;
881                 break;
882         case WIFI_CONNECTED:
883         case WIFI_DISCONNECTING:
884                 *state = CONNECTION_WIFI_STATE_CONNECTED;
885                 break;
886         default:
887                 CONNECTION_LOG(CONNECTION_ERROR, "Unknown Wi-Fi state"); //LCOV_EXCL_LINE
888                 return CONNECTION_ERROR_INVALID_OPERATION; //LCOV_EXCL_LINE
889         }
890
891         return CONNECTION_ERROR_NONE;
892 }
893
894 //LCOV_EXCL_START
895 int _connection_libnet_get_ethernet_state(connection_handle_s *conn_handle,
896                         connection_ethernet_state_e *state)
897 {
898         int rv;
899         struct _profile_list_s ethernet_profiles = {0, 0, NULL};
900         rv = net_get_profile_list(conn_handle->network_info_handle,
901                         NET_DEVICE_ETHERNET, &ethernet_profiles.profiles,
902                         &ethernet_profiles.count);
903         if (rv == NET_ERR_ACCESS_DENIED) {
904                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
905                 return CONNECTION_ERROR_PERMISSION_DENIED;
906         }
907
908         if (ethernet_profiles.count == 0) {
909                 *state = CONNECTION_ETHERNET_STATE_DEACTIVATED;
910                 return CONNECTION_ERROR_NONE;
911         }
912
913         switch (ethernet_profiles.profiles->profile_state) {
914         case NET_STATE_TYPE_ONLINE:
915         case NET_STATE_TYPE_READY:
916                 *state = CONNECTION_ETHERNET_STATE_CONNECTED;
917                 break;
918         case NET_STATE_TYPE_IDLE:
919         case NET_STATE_TYPE_FAILURE:
920         case NET_STATE_TYPE_ASSOCIATION:
921         case NET_STATE_TYPE_CONFIGURATION:
922         case NET_STATE_TYPE_DISCONNECT:
923                 *state = CONNECTION_ETHERNET_STATE_DISCONNECTED;
924                 break;
925         default:
926                 __libnet_clear_profile_list(&ethernet_profiles);
927                 return CONNECTION_ERROR_OPERATION_FAILED;
928         }
929
930         __libnet_clear_profile_list(&ethernet_profiles);
931
932         return CONNECTION_ERROR_NONE;
933 }
934
935 int _connection_libnet_get_ethernet_cable_state(connection_handle_s *conn_handle,
936                         connection_ethernet_cable_state_e* state)
937 {
938         int rv = 0;
939         int status = 0;
940
941         rv = net_get_ethernet_cable_state(conn_handle->network_info_handle, &status);
942         if (rv == NET_ERR_ACCESS_DENIED) {
943                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
944                 return CONNECTION_ERROR_PERMISSION_DENIED;
945         } else if (rv != NET_ERR_NONE) {
946                 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get ethernet cable state[%d]", rv);
947                 return CONNECTION_ERROR_OPERATION_FAILED;
948         }
949
950         if (status == 1)
951                 *state = CONNECTION_ETHERNET_CABLE_ATTACHED;
952         else
953                 *state = CONNECTION_ETHERNET_CABLE_DETACHED;
954         return CONNECTION_ERROR_NONE;
955 }
956
957 int _connection_libnet_get_dhcp_state(connection_handle_s *conn_handle,
958                         const char *interface_name, connection_dhcp_state_e *state)
959 {
960         int rv = 0;
961         char *status_str = NULL;
962
963         rv = net_get_dhcp_state(conn_handle->network_info_handle, interface_name, &status_str);
964         if (rv == NET_ERR_ACCESS_DENIED) {
965                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
966                 return CONNECTION_ERROR_PERMISSION_DENIED;
967         } else if (rv != NET_ERR_NONE) {
968                 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get DHCP state[%d]", rv);
969                 return CONNECTION_ERROR_OPERATION_FAILED;
970         }
971
972         if (g_strcmp0(status_str, "DHCP_STARTED") == 0)
973                 *state = CONNECTION_DHCP_STATE_STARTED;
974         else if (g_strcmp0(status_str, "DHCP_SUCCESS") == 0)
975                 *state = CONNECTION_DHCP_STATE_FINISHED;
976         else if (g_strcmp0(status_str, "DHCP_FAIL") == 0)
977                 *state = CONNECTION_DHCP_STATE_FINISHED;
978         else
979                 *state = CONNECTION_DHCP_STATE_UNKNOWN;
980
981         g_free(status_str);
982
983         return CONNECTION_ERROR_NONE;
984 }
985 //LCOV_EXCL_STOP
986
987 int _connection_libnet_get_bluetooth_state(connection_handle_s *conn_handle, connection_bt_state_e *state)
988 {
989         int i = 0;
990         int rv = 0;
991         struct _profile_list_s bluetooth_profiles = {0, 0, NULL};
992         rv = net_get_profile_list(conn_handle->network_info_handle,
993                         NET_DEVICE_BLUETOOTH, &bluetooth_profiles.profiles,
994                         &bluetooth_profiles.count);
995         if (rv == NET_ERR_ACCESS_DENIED) {
996                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
997                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
998         }
999
1000         if (bluetooth_profiles.count == 0) {
1001                 *state = CONNECTION_BT_STATE_DEACTIVATED; //LCOV_EXCL_LINE
1002                 return CONNECTION_ERROR_NONE; //LCOV_EXCL_LINE
1003         }
1004
1005         //LCOV_EXCL_START
1006         for (; i < bluetooth_profiles.count; i++) {
1007                 switch (bluetooth_profiles.profiles[i].profile_state) {
1008                 case NET_STATE_TYPE_ONLINE:
1009                 case NET_STATE_TYPE_READY:
1010                         *state = CONNECTION_BT_STATE_CONNECTED;
1011                         goto done;
1012                 case NET_STATE_TYPE_IDLE:
1013                 case NET_STATE_TYPE_FAILURE:
1014                 case NET_STATE_TYPE_ASSOCIATION:
1015                 case NET_STATE_TYPE_CONFIGURATION:
1016                 case NET_STATE_TYPE_DISCONNECT:
1017                         *state = CONNECTION_BT_STATE_DISCONNECTED;
1018                         break;
1019                 default:
1020                         __libnet_clear_profile_list(&bluetooth_profiles);
1021                         return CONNECTION_ERROR_OPERATION_FAILED;
1022                 }
1023         }
1024         //LCOV_EXCL_STOP
1025
1026 done:
1027         __libnet_clear_profile_list(&bluetooth_profiles);
1028
1029         return CONNECTION_ERROR_NONE;
1030 }
1031
1032 int _connection_libnet_get_profile_iterator(connection_handle_s *conn_handle,
1033                         connection_iterator_type_e type, connection_profile_iterator_h* profile_iter_h)
1034 {
1035         int count = 0;
1036         int rv;
1037         net_profile_info_t *profiles = NULL;
1038
1039         struct _profile_list_s profile_list = {0, 0, NULL};
1040
1041         __libnet_clear_profile_list(&profile_iterator);
1042
1043         rv = net_get_all_profile_list(conn_handle->network_info_handle,
1044                         &profile_list.profiles, &profile_list.count);
1045         if (rv == NET_ERR_ACCESS_DENIED) {
1046                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1047                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1048         } else if (rv != NET_ERR_NO_SERVICE && rv != NET_ERR_NONE)
1049                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1050
1051         *profile_iter_h = &profile_iterator;
1052
1053         switch (type) {
1054         case CONNECTION_ITERATOR_TYPE_REGISTERED:
1055                 count = profile_list.count;
1056                 CONNECTION_LOG(CONNECTION_INFO, "Total profile count : %d", count);
1057                 if (count == 0)
1058                         return CONNECTION_ERROR_NONE;
1059
1060                 profiles = g_try_new0(net_profile_info_t, count);
1061                 if (profiles == NULL) {
1062                         __libnet_clear_profile_list(&profile_list); //LCOV_EXCL_LINE
1063                         return CONNECTION_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1064                 }
1065
1066                 profile_iterator.profiles = profiles;
1067
1068                 memcpy(profiles, profile_list.profiles, sizeof(net_profile_info_t) * count);
1069
1070                 break;
1071         case CONNECTION_ITERATOR_TYPE_CONNECTED:
1072                 count = __libnet_get_connected_count(&profile_list);
1073                 CONNECTION_LOG(CONNECTION_INFO, "Total connected profile count : %d", count);
1074                 if (count == 0)
1075                         return CONNECTION_ERROR_NONE;
1076
1077                 profiles = g_try_new0(net_profile_info_t, count);
1078                 if (profiles == NULL) {
1079                         __libnet_clear_profile_list(&profile_list); //LCOV_EXCL_LINE
1080                         return CONNECTION_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1081                 }
1082
1083                 profile_iterator.profiles = profiles;
1084
1085                 __libnet_copy_connected_profile(&profiles, &profile_list);
1086
1087                 break;
1088         case CONNECTION_ITERATOR_TYPE_DEFAULT:
1089                 count = __libnet_get_default_count(&profile_list);
1090                 CONNECTION_LOG(CONNECTION_INFO, "Total default profile count : %d", count); //LCOV_EXCL_LINE
1091                 if (count == 0)
1092                         return CONNECTION_ERROR_NONE;
1093
1094                 profiles = g_try_new0(net_profile_info_t, count);
1095                 if (profiles == NULL) {
1096                         __libnet_clear_profile_list(&profile_list);
1097                         return CONNECTION_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1098                 }
1099
1100                 profile_iterator.profiles = profiles;
1101
1102                 __libnet_copy_default_profile(&profiles, &profile_list);
1103
1104                 break;
1105         }
1106
1107         __libnet_clear_profile_list(&profile_list);
1108
1109         profile_iterator.count = count;
1110
1111         return CONNECTION_ERROR_NONE;
1112 }
1113
1114 int _connection_libnet_get_iterator_next(connection_profile_iterator_h profile_iter_h, connection_profile_h *profile)
1115 {
1116         if (profile_iter_h != &profile_iterator)
1117                 return CONNECTION_ERROR_INVALID_PARAMETER;
1118
1119         if (profile_iterator.count <= profile_iterator.next)
1120                 return CONNECTION_ERROR_ITERATOR_END;
1121
1122         *profile = &profile_iterator.profiles[profile_iterator.next];
1123         profile_iterator.next++;
1124
1125         return CONNECTION_ERROR_NONE;
1126 }
1127
1128 bool _connection_libnet_iterator_has_next(connection_profile_iterator_h profile_iter_h)
1129 {
1130         if (profile_iter_h != &profile_iterator)
1131                 return false;
1132
1133         if (profile_iterator.count <= profile_iterator.next)
1134                 return false;
1135
1136         return true;
1137 }
1138
1139 int _connection_libnet_destroy_iterator(connection_profile_iterator_h profile_iter_h)
1140 {
1141         if (profile_iter_h != &profile_iterator)
1142                 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1143
1144         __libnet_clear_profile_list(&profile_iterator);
1145
1146         return CONNECTION_ERROR_NONE;
1147 }
1148
1149 int _connection_libnet_get_current_profile(connection_handle_s *conn_handle,
1150                         connection_profile_h *profile)
1151 {
1152         net_profile_info_t active_profile;
1153         int rv;
1154
1155         rv = net_get_active_net_info(conn_handle->network_info_handle, &active_profile);
1156         if (rv == NET_ERR_NO_SERVICE)
1157                 return CONNECTION_ERROR_NO_CONNECTION; //LCOV_EXCL_LINE
1158         else if (rv == NET_ERR_ACCESS_DENIED) {
1159                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1160                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1161         } else if (rv != NET_ERR_NONE)
1162                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1163
1164         *profile = g_try_malloc0(sizeof(net_profile_info_t));
1165         if (*profile == NULL)
1166                 return CONNECTION_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1167
1168         memcpy(*profile, &active_profile, sizeof(net_profile_info_t));
1169         prof_handle_list = g_slist_append(prof_handle_list, *profile);
1170
1171         return CONNECTION_ERROR_NONE;
1172 }
1173
1174 int _connection_libnet_reset_profile(connection_handle_s *conn_handle,
1175                 connection_reset_option_e type, connection_cellular_subscriber_id_e id)
1176 {
1177         int rv;
1178
1179         rv = net_reset_profile(conn_handle->network_info_handle, type, id);
1180         if (rv == NET_ERR_ACCESS_DENIED)
1181                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1182         else if (rv != NET_ERR_NONE)
1183                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1184
1185         return CONNECTION_ERROR_NONE;
1186 }
1187
1188 int _connection_libnet_open_profile(connection_handle_s *conn_handle,
1189                         connection_profile_h profile)
1190 {
1191         int rv;
1192
1193         if (!(_connection_libnet_check_profile_validity(profile))) {
1194                 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter"); //LCOV_EXCL_LINE
1195                 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1196         }
1197
1198         net_profile_info_t *profile_info = profile;
1199
1200         if (profile_info->profile_type == NET_DEVICE_MESH)
1201                 rv = net_open_mesh_connection_with_profile(conn_handle->network_info_handle, //LCOV_EXCL_LINE
1202                                 profile_info->profile_name);
1203         else
1204                 rv = net_open_connection_with_profile(conn_handle->network_info_handle,
1205                                 profile_info->profile_name);
1206
1207         if (rv == NET_ERR_ACCESS_DENIED)
1208                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1209         else if (rv != NET_ERR_NONE)
1210                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1211
1212         return CONNECTION_ERROR_NONE;
1213 }
1214
1215 int _connection_libnet_get_cellular_service_profile(connection_handle_s *conn_handle,
1216                 connection_cellular_service_type_e type, connection_profile_h *profile)
1217 {
1218         int i = 0, j = 0;
1219         int rv = NET_ERR_NONE;
1220 #if defined TIZEN_DUALSIM_ENABLE
1221         int default_subscriber_id = 0;
1222         char subscriber_id[3];
1223 #endif
1224
1225         struct _profile_list_s cellular_profiles = { 0, 0, NULL };
1226         net_service_type_t service_type = _connection_profile_convert_to_libnet_cellular_service_type(type);
1227
1228         rv = net_get_profile_list(conn_handle->network_info_handle,
1229                         NET_DEVICE_CELLULAR, &cellular_profiles.profiles,
1230                         &cellular_profiles.count);
1231         if (rv == NET_ERR_ACCESS_DENIED) {
1232                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1233                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1234         } else if (rv != NET_ERR_NONE) {
1235                 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get profile list (%d)", rv); //LCOV_EXCL_LINE
1236                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1237         }
1238
1239 #if defined TIZEN_DUALSIM_ENABLE
1240         if (_connection_vconf_get_int(conn_handle, VCONF_TELEPHONY_DEFAULT_DATA_SERVICE,
1241                                                 &default_subscriber_id) != 0) {
1242                 CONNECTION_LOG(CONNECTION_ERROR,
1243                                                 "Failed to get VCONF_TELEPHONY_DEFAULT_DATA_SERVICE");
1244                 __libnet_clear_profile_list(&cellular_profiles); //LCOV_EXCL_LINE
1245                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1246         }
1247
1248         g_snprintf(subscriber_id, sizeof(subscriber_id), "%d", default_subscriber_id);
1249 #endif
1250
1251         for (i = 0; i < cellular_profiles.count; i++)
1252                 if (cellular_profiles.profiles[i].profile_info.pdp.service_type == service_type)
1253 #if defined TIZEN_DUALSIM_ENABLE
1254                         if (g_str_has_suffix(
1255                                         cellular_profiles.profiles[i].profile_info.pdp.ps_modem_path,
1256                                         subscriber_id) == TRUE)
1257 #endif
1258                                 break;
1259
1260         if (i >= cellular_profiles.count) {
1261                 __libnet_clear_profile_list(&cellular_profiles); //LCOV_EXCL_LINE
1262                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1263         }
1264
1265         *profile = g_try_malloc0(sizeof(net_profile_info_t));
1266         if (*profile == NULL) {
1267                 __libnet_clear_profile_list(&cellular_profiles); //LCOV_EXCL_LINE
1268                 return CONNECTION_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1269         }
1270
1271         memcpy(*profile, &cellular_profiles.profiles[i], sizeof(net_profile_info_t));
1272
1273         if (cellular_profiles.profiles[i].profile_info.pdp.default_conn)
1274                 goto done;
1275
1276         //LCOV_EXCL_START
1277         if (type != CONNECTION_CELLULAR_SERVICE_TYPE_INTERNET &&
1278             type != CONNECTION_CELLULAR_SERVICE_TYPE_PREPAID_INTERNET)
1279                 goto done;
1280
1281         for (j = 0; j < cellular_profiles.count; j++) {
1282                 if (i == j)
1283                         continue;
1284
1285                 if (cellular_profiles.profiles[j].profile_info.pdp.service_type != service_type)
1286                         continue;
1287
1288                 if (cellular_profiles.profiles[j].profile_info.pdp.default_conn) {
1289                         memcpy(*profile, &cellular_profiles.profiles[j], sizeof(net_profile_info_t));
1290                         goto done;
1291                 }
1292         }
1293         //LCOV_EXCL_STOP
1294
1295 done:
1296         __libnet_clear_profile_list(&cellular_profiles);
1297         prof_handle_list = g_slist_append(prof_handle_list, *profile);
1298
1299         return CONNECTION_ERROR_NONE;
1300 }
1301
1302 int _connection_libnet_set_cellular_service_profile_sync(connection_handle_s *conn_handle,
1303                         connection_cellular_service_type_e type, connection_profile_h profile)
1304 {
1305         int rv;
1306
1307         if (!(_connection_libnet_check_profile_validity(profile))) {
1308                 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter"); //LCOV_EXCL_LINE
1309                 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1310         }
1311
1312         net_profile_info_t *profile_info = profile;
1313         connection_cellular_service_type_e service_type;
1314
1315         service_type = _profile_convert_to_connection_cellular_service_type(profile_info->profile_info.pdp.service_type);
1316
1317         if (service_type != type)
1318                 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1319
1320         rv = net_set_default_cellular_service_profile(conn_handle->network_info_handle,
1321                                 profile_info->profile_name);
1322         if (rv == NET_ERR_ACCESS_DENIED) {
1323                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1324                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1325         } else if (rv != NET_ERR_NONE)
1326                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1327
1328         return CONNECTION_ERROR_NONE;
1329 }
1330
1331 int _connection_libnet_set_cellular_service_profile_async(connection_handle_s *conn_handle,
1332                         connection_cellular_service_type_e type, connection_profile_h profile)
1333 {
1334         int rv;
1335
1336         if (!(_connection_libnet_check_profile_validity(profile))) {
1337                 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter"); //LCOV_EXCL_LINE
1338                 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1339         }
1340
1341         net_profile_info_t *profile_info = profile;
1342         connection_cellular_service_type_e service_type;
1343
1344         service_type = _profile_convert_to_connection_cellular_service_type(profile_info->profile_info.pdp.service_type);
1345
1346         if (service_type != type)
1347                 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1348
1349         rv = net_set_default_cellular_service_profile_async(conn_handle->network_info_handle,
1350                                 profile_info->profile_name);
1351         if (rv == NET_ERR_ACCESS_DENIED)
1352                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1353         else if (rv != NET_ERR_NONE)
1354                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1355
1356         return CONNECTION_ERROR_NONE;
1357 }
1358
1359 int _connection_libnet_close_profile(connection_handle_s *conn_handle, connection_profile_h profile)
1360 {
1361         int rv;
1362
1363         if (!(_connection_libnet_check_profile_validity(profile))) {
1364                 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter"); //LCOV_EXCL_LINE
1365                 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1366         }
1367
1368         net_profile_info_t *profile_info = profile;
1369
1370         if (profile_info->profile_type == NET_DEVICE_MESH)
1371                 rv = net_close_mesh_connection(conn_handle->network_info_handle, profile_info->profile_name); //LCOV_EXCL_LINE
1372         else
1373                 rv = net_close_connection(conn_handle->network_info_handle, profile_info->profile_name);
1374
1375         if (rv == NET_ERR_ACCESS_DENIED)
1376                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1377         else if (rv != NET_ERR_NONE)
1378                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1379
1380         return CONNECTION_ERROR_NONE;
1381 }
1382
1383 int _connection_libnet_add_route(connection_handle_s *conn_handle,
1384                         const char *interface_name, const char *host_address)
1385 {
1386         int rv;
1387         char *endstr = NULL;
1388         int address_family = 0;
1389
1390         address_family = AF_INET;
1391
1392         endstr = strrchr(host_address, '.');
1393         if (endstr == NULL ||
1394                         strcmp(endstr, ".0") == 0 ||
1395                         strncmp(host_address, "0.", 2) == 0 ||
1396                         strstr(host_address, "255") != NULL) {
1397                 CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed\n"); //LCOV_EXCL_LINE
1398                 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1399         }
1400
1401         rv = net_add_route(conn_handle->network_info_handle,
1402                                 host_address, interface_name, address_family);
1403         if (rv == NET_ERR_ACCESS_DENIED) {
1404                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1405                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1406         } else if (rv != NET_ERR_NONE)
1407                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1408
1409         return CONNECTION_ERROR_NONE;
1410 }
1411
1412 int _connection_libnet_remove_route(connection_handle_s *conn_handle,
1413                         const char *interface_name, const char *host_address)
1414 {
1415         int rv;
1416         char *endstr = strrchr(host_address, '.');
1417         int address_family = 0;
1418
1419         address_family = AF_INET;
1420
1421         endstr = strrchr(host_address, '.');
1422         if (endstr == NULL ||
1423                 strcmp(endstr, ".0") == 0 ||
1424                 strncmp(host_address, "0.", 2) == 0 ||
1425                 strstr(host_address, ".0.") != NULL || strstr(host_address, "255") != NULL) {
1426                 CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed"); //LCOV_EXCL_LINE
1427                 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1428         }
1429
1430         rv = net_remove_route(conn_handle->network_info_handle,
1431                                 host_address, interface_name, address_family);
1432         if (rv == NET_ERR_ACCESS_DENIED) {
1433                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1434                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1435         } else if (rv != NET_ERR_NONE)
1436                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1437
1438         return CONNECTION_ERROR_NONE;
1439 }
1440
1441 int _connection_libnet_add_route_ipv6(connection_handle_s *conn_handle,
1442                         const char *interface_name, const char *host_address, const char *gateway)
1443 {
1444         int rv;
1445         int address_family = 0;
1446
1447         address_family = AF_INET6;
1448
1449         if (strncmp(host_address, "fe80:", 5) == 0 ||
1450                 strncmp(host_address, "ff00:", 5) == 0 ||
1451                 strncmp(host_address, "::", 2) == 0) {
1452                 CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed\n"); //LCOV_EXCL_LINE
1453                 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1454         }
1455
1456         rv = net_add_route_ipv6(conn_handle->network_info_handle,
1457                                 host_address, interface_name, address_family, gateway);
1458         if (rv == NET_ERR_ACCESS_DENIED) {
1459                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1460                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1461         } else if (rv != NET_ERR_NONE)
1462                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1463
1464         return CONNECTION_ERROR_NONE;
1465 }
1466
1467 int _connection_libnet_remove_route_ipv6(connection_handle_s *conn_handle,
1468                         const char *interface_name, const char *host_address, const char *gateway)
1469 {
1470         int rv;
1471         int address_family = 0;
1472
1473         address_family = AF_INET6;
1474
1475         if (strncmp(host_address, "fe80:", 5) == 0 ||
1476                 strncmp(host_address, "ff00:", 5) == 0 ||
1477                 strncmp(host_address, "::", 2) == 0) {
1478                 CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed\n"); //LCOV_EXCL_LINE
1479                 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1480         }
1481
1482         rv = net_remove_route_ipv6(conn_handle->network_info_handle,
1483                                 host_address, interface_name, address_family, gateway);
1484         if (rv == NET_ERR_ACCESS_DENIED) {
1485                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1486                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1487         } else if (rv != NET_ERR_NONE)
1488                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1489
1490         return CONNECTION_ERROR_NONE;
1491 }
1492
1493 int _connection_libnet_add_route_entry(connection_handle_s *conn_handle,
1494                 connection_address_family_e address_family, const char *interface_name,
1495                 const char *host_address, const char *gateway)
1496 {
1497         int rv;
1498         char *endstr = NULL;
1499         int address_family_type = 0;
1500
1501         if (address_family == CONNECTION_ADDRESS_FAMILY_IPV4)
1502                 address_family_type = AF_INET;
1503         else
1504                 address_family_type = AF_INET6;
1505
1506         if (address_family == CONNECTION_ADDRESS_FAMILY_IPV4) {
1507
1508                 endstr = strrchr(host_address, '.');
1509                 if (endstr == NULL ||
1510                                 strcmp(endstr, ".0") == 0 ||
1511                                 strncmp(host_address, "0.", 2) == 0 ||
1512                                 strstr(host_address, "255") != NULL) {
1513                         CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed\n"); //LCOV_EXCL_LINE
1514                         return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1515                 }
1516
1517                 rv = net_add_route_entry(conn_handle->network_info_handle,
1518                                 host_address, interface_name, address_family_type, gateway);
1519                 if (rv == NET_ERR_ACCESS_DENIED) {
1520                         CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1521                         return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1522                 } else if (rv != NET_ERR_NONE)
1523                         return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1524
1525         } else {
1526
1527                 if (strncmp(host_address, "fe80:", 5) == 0 ||
1528                         strncmp(host_address, "ff00:", 5) == 0 ||
1529                         strncmp(host_address, "::", 2) == 0) {
1530                         CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed\n"); //LCOV_EXCL_LINE
1531                         return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1532                 }
1533
1534                 rv = net_add_route_ipv6(conn_handle->network_info_handle,
1535                                 host_address, interface_name, address_family_type, gateway);
1536                 if (rv == NET_ERR_ACCESS_DENIED) {
1537                         CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1538                         return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1539                 } else if (rv != NET_ERR_NONE)
1540                         return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1541         }
1542
1543         return CONNECTION_ERROR_NONE;
1544 }
1545
1546 int _connection_libnet_remove_route_entry(connection_handle_s *conn_handle,
1547                 connection_address_family_e address_family, const char *interface_name,
1548                 const char *host_address, const char *gateway)
1549 {
1550         int rv;
1551         char *endstr = strrchr(host_address, '.');
1552         int address_family_type = 0;
1553
1554         if (address_family == CONNECTION_ADDRESS_FAMILY_IPV4)
1555                 address_family_type = AF_INET;
1556         else
1557                 address_family_type = AF_INET6;
1558
1559         if (address_family == CONNECTION_ADDRESS_FAMILY_IPV4) {
1560                 endstr = strrchr(host_address, '.');
1561                 if (endstr == NULL ||
1562                         strcmp(endstr, ".0") == 0 ||
1563                         strncmp(host_address, "0.", 2) == 0 ||
1564                         strstr(host_address, ".0.") != NULL || strstr(host_address, "255") != NULL) {
1565                         CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed"); //LCOV_EXCL_LINE
1566                         return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1567                 }
1568
1569                 rv = net_remove_route_entry(conn_handle->network_info_handle, host_address,
1570                                         interface_name, address_family_type, gateway);
1571                 if (rv == NET_ERR_ACCESS_DENIED) {
1572                         CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1573                         return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1574                 } else if (rv != NET_ERR_NONE)
1575                         return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1576
1577         } else {
1578
1579                 if (strncmp(host_address, "fe80:", 5) == 0 ||
1580                         strncmp(host_address, "ff00:", 5) == 0 ||
1581                         strncmp(host_address, "::", 2) == 0) {
1582                         CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed\n"); //LCOV_EXCL_LINE
1583                         return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1584                 }
1585
1586                 rv = net_remove_route_ipv6(conn_handle->network_info_handle, host_address,
1587                                         interface_name, address_family_type, gateway);
1588                 if (rv == NET_ERR_ACCESS_DENIED) {
1589                         CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1590                         return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1591                 } else if (rv != NET_ERR_NONE)
1592                         return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1593         }
1594
1595         return CONNECTION_ERROR_NONE;
1596 }
1597
1598 void _connection_libnet_add_to_profile_list(connection_profile_h profile)
1599 {
1600         prof_handle_list = g_slist_append(prof_handle_list, profile);
1601 }
1602
1603 void _connection_libnet_remove_from_profile_list(connection_profile_h profile)
1604 {
1605         prof_handle_list = g_slist_remove(prof_handle_list, profile);
1606         g_free(profile);
1607 }
1608
1609 bool _connection_libnet_add_to_profile_cb_list(connection_profile_h profile,
1610                 connection_profile_state_changed_cb callback, void *user_data)
1611 {
1612         net_profile_info_t *profile_info = profile;
1613         char *profile_name = g_strdup(profile_info->profile_name);
1614
1615         struct _profile_cb_s *profile_cb_info = g_try_malloc0(sizeof(struct _profile_cb_s));
1616         if (profile_cb_info == NULL) {
1617                 g_free(profile_name); //LCOV_EXCL_LINE
1618                 return false; //LCOV_EXCL_LINE
1619         }
1620
1621         profile_cb_info->callback = callback;
1622         profile_cb_info->user_data = user_data;
1623         profile_cb_info->state = _profile_convert_to_cp_state(profile_info->profile_state);
1624
1625         g_hash_table_replace(profile_cb_table, profile_name, profile_cb_info);
1626
1627         return true;
1628 }
1629
1630 bool _connection_libnet_remove_from_profile_cb_list(connection_profile_h profile)
1631 {
1632         net_profile_info_t *profile_info = profile;
1633         struct _profile_cb_s *profile_cb_info;
1634
1635         profile_cb_info = g_hash_table_lookup(profile_cb_table, profile_info->profile_name);
1636         if (profile_cb_info == NULL)
1637                 return false;
1638
1639         g_hash_table_remove(profile_cb_table, profile_info->profile_name);
1640
1641         /**
1642          * Intentional:
1643          * Marking callback to dummy callback.
1644          *
1645          * Reason:
1646          * To avoid race condition between calling profile_state_changed_cb()
1647          * and _connection_libnet_remove_from_profile_cb_list()
1648          * in multi-threaded environment.
1649          */
1650         profile_cb_info->callback = __connection_dummy_profile_state_changed_cb;
1651         profile_cb_info->user_data = NULL;
1652         profile_cb_info->state = -1;
1653
1654         /* Timer to free hash table entry */
1655         g_timeout_add(200, __profile_cb_table_value_destroy, profile_cb_info);
1656
1657         return true;
1658 }
1659
1660 int _connection_libnet_set_statistics(connection_handle_s *conn_handle,
1661                         net_device_t device_type, net_statistics_type_e statistics_type)
1662 {
1663         int rv;
1664         rv = net_set_statistics(conn_handle->network_info_handle,
1665                                 device_type, statistics_type);
1666         if (rv == NET_ERR_ACCESS_DENIED) {
1667                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1668                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1669         } else if (rv != NET_ERR_NONE)
1670                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1671
1672         return CONNECTION_ERROR_NONE;
1673 }
1674
1675 int _connection_libnet_get_statistics(connection_handle_s *conn_handle,
1676                         net_statistics_type_e statistics_type, unsigned long long *size)
1677 {
1678         int rv;
1679         rv = net_get_statistics(conn_handle->network_info_handle,
1680                                 NET_DEVICE_WIFI, statistics_type, size);
1681         if (rv == NET_ERR_ACCESS_DENIED) {
1682                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1683                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1684         } else if (rv != NET_ERR_NONE)
1685                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1686
1687         return CONNECTION_ERROR_NONE;
1688 }
1689
1690 //LCOV_EXCL_START
1691 int _connection_libnet_set_cellular_subscriber_id(connection_profile_h profile,
1692                 connection_cellular_subscriber_id_e sim_id)
1693 {
1694         char *modem_path = NULL;
1695         net_profile_info_t *profile_info = (net_profile_info_t *)profile;
1696
1697         if (net_get_cellular_modem_object_path(&modem_path, sim_id) != NET_ERR_NONE) {
1698                 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get subscriber[%d]", sim_id); //LCOV_EXCL_LINE
1699                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1700         }
1701
1702         if (!modem_path) {
1703                 CONNECTION_LOG(CONNECTION_ERROR, "NULL modem object path"); //LCOV_EXCL_LINE
1704                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1705         }
1706
1707         g_strlcpy(profile_info->profile_info.pdp.ps_modem_path, modem_path,
1708                                 NET_PROFILE_NAME_LEN_MAX);
1709         g_free(modem_path);
1710
1711         return CONNECTION_ERROR_NONE;
1712 }
1713 //LCOV_EXCL_STOP
1714
1715 int _connection_libnet_enable_ethernet_eap(bool enable, const char *profilename)
1716 {
1717         int rv = 0;
1718
1719         rv = net_ethernet_eap_enable(enable, profilename);
1720         if (rv == NET_ERR_ACCESS_DENIED) {
1721                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1722                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1723         } else if (rv != NET_ERR_NONE) {
1724                 CONNECTION_LOG(CONNECTION_ERROR, "Failed to enable EAP over ethernet[%d]", rv); //LCOV_EXCL_LINE
1725                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1726         }
1727
1728         return CONNECTION_ERROR_NONE;
1729 }
1730
1731 int _connection_libnet_ethernet_eap_enabled(const char *profilename, bool *enabled)
1732 {
1733         int rv = 0;
1734         gboolean eap_enabled = false;
1735
1736         rv = net_ethernet_eap_enabled(profilename, &eap_enabled);
1737         if (rv == NET_ERR_ACCESS_DENIED) {
1738                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1739                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1740         } else if (rv != NET_ERR_NONE) {
1741                 CONNECTION_LOG(CONNECTION_ERROR, "Failed to enable EAP over ethernet[%d]", rv); //LCOV_EXCL_LINE
1742                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1743         }
1744
1745         *enabled = eap_enabled;
1746         return CONNECTION_ERROR_NONE;
1747 }
1748
1749 int _connection_libnet_profile_save_ethernet_eap_config(connection_handle_s *conn_handle,
1750                         connection_profile_h profile)
1751 {
1752         int rv;
1753
1754         net_profile_info_t *profile_info = profile;
1755
1756         rv = net_save_ethernet_eap_config(conn_handle->network_info_handle,
1757                         &profile_info->profile_info.ethernet.net_info);
1758
1759         if (rv == NET_ERR_ACCESS_DENIED) {
1760                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1761                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1762         } else if (rv != NET_ERR_NONE) {
1763                 CONNECTION_LOG(CONNECTION_ERROR, "Failed to save eap config[%d]", rv); //LCOV_EXCL_LINE
1764                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1765         }
1766
1767         return CONNECTION_ERROR_NONE;
1768 }
1769
1770 int _connection_libnet_check_get_privilege(void)
1771 {
1772         int rv;
1773
1774         rv = net_check_get_privilege();
1775         if (rv == NET_ERR_ACCESS_DENIED) {
1776                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1777                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1778         } else if (rv != NET_ERR_NONE)
1779                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1780
1781         return CONNECTION_ERROR_NONE;
1782 }
1783
1784 int _connection_libnet_check_profile_privilege(void)
1785 {
1786         int rv;
1787
1788         rv = net_check_profile_privilege();
1789         if (rv == NET_ERR_ACCESS_DENIED) {
1790                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1791                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1792         } else if (rv != NET_ERR_NONE)
1793                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1794
1795         return CONNECTION_ERROR_NONE;
1796 }
1797
1798 bool __libnet_check_feature_supported(const char *key, connection_supported_feature_e feature)
1799 {
1800         if (!connection_is_feature_checked[feature]) {
1801                 if (system_info_get_platform_bool(key, &connection_feature_supported[feature]) < 0) {
1802                         CONNECTION_LOG(CONNECTION_ERROR, "Error - Feature getting from System Info"); //LCOV_EXCL_LINE
1803                         set_last_result(CONNECTION_ERROR_OPERATION_FAILED); //LCOV_EXCL_LINE
1804                         return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1805                 }
1806                 connection_is_feature_checked[feature] = true;
1807         }
1808         return connection_feature_supported[feature];
1809 }
1810
1811 int _connection_check_feature_supported(const char *feature_name, ...)
1812 {
1813         va_list list;
1814         const char *key;
1815         bool value = false;
1816         bool feature_supported = false;
1817
1818         va_start(list, feature_name);
1819         key = feature_name;
1820         while (1) {
1821                 if (strcmp(key, TELEPHONY_FEATURE) == 0)
1822                         value = __libnet_check_feature_supported(key, CONNECTION_SUPPORTED_FEATURE_TELEPHONY);
1823                 if (strcmp(key, WIFI_FEATURE) == 0)
1824                         value = __libnet_check_feature_supported(key, CONNECTION_SUPPORTED_FEATURE_WIFI);
1825                 if (strcmp(key, TETHERING_BLUETOOTH_FEATURE) == 0)
1826                         value = __libnet_check_feature_supported(key, CONNECTION_SUPPORTED_FEATURE_TETHERING_BLUETOOTH);
1827                 if (strcmp(key, ETHERNET_FEATURE) == 0)
1828                         value = __libnet_check_feature_supported(key, CONNECTION_SUPPORTED_FEATURE_ETHERNET);
1829                 if (strcmp(key, ROUTE_FEATURE) == 0)
1830                         value = __libnet_check_feature_supported(key, CONNECTION_SUPPORTED_FEATURE_ROUTE);
1831
1832                 feature_supported |= value;
1833                 key = va_arg(list, const char *);
1834                 if (!key) break;
1835         }
1836         if (!feature_supported) {
1837                 CONNECTION_LOG(CONNECTION_ERROR, "Error - Feature is not supported");
1838                 set_last_result(CONNECTION_ERROR_NOT_SUPPORTED);
1839                 va_end(list);
1840                 return CONNECTION_ERROR_NOT_SUPPORTED;
1841         }
1842
1843         va_end(list);
1844         set_last_result(CONNECTION_ERROR_NONE);
1845         return CONNECTION_ERROR_NONE;
1846 }
1847
1848 //LCOV_EXCL_START
1849 int _connection_libnet_start_tcpdump(connection_handle_s *conn_handle)
1850 {
1851         connection_error_e result = CONNECTION_ERROR_NONE;
1852         net_err_t ret = NET_ERR_NONE;
1853
1854         ret = net_start_tcpdump(conn_handle->network_info_handle);
1855         result = __libnet_convert_to_cp_error_type(ret);
1856
1857         return result;
1858 }
1859
1860 int _connection_libnet_stop_tcpdump(connection_handle_s *conn_handle)
1861 {
1862         connection_error_e result = CONNECTION_ERROR_NONE;
1863         net_err_t ret = NET_ERR_NONE;
1864
1865         ret = net_stop_tcpdump(conn_handle->network_info_handle);
1866         result = __libnet_convert_to_cp_error_type(ret);
1867
1868         return result;
1869 }
1870
1871 int _connection_libnet_get_tcpdump_state(connection_handle_s *conn_handle,
1872                         gboolean *tcpdump_state)
1873 {
1874         connection_error_e result = CONNECTION_ERROR_NONE;
1875         net_err_t ret = NET_ERR_NONE;
1876
1877         ret = net_get_tcpdump_state(conn_handle->network_info_handle, tcpdump_state);
1878         result = __libnet_convert_to_cp_error_type(ret);
1879
1880         return result;
1881 }
1882
1883 int _connection_libnet_get_clock_updated(connection_handle_s *conn_handle, bool *updated)
1884 {
1885         int rv = 0;
1886
1887         rv = net_get_clock_update_info(conn_handle->network_info_handle, updated);
1888         if (rv == NET_ERR_ACCESS_DENIED) {
1889                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1890                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1891         } else if (rv != NET_ERR_NONE) {
1892                 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get clock update information[%d]", rv); //LCOV_EXCL_LINE
1893                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1894         }
1895
1896         return CONNECTION_ERROR_NONE;
1897 }
1898
1899 int _connection_libnet_get_ntp_server(connection_handle_s *conn_handle, char **ntp_server)
1900 {
1901         int rv =0;
1902
1903         rv = net_get_ntp_server_info(conn_handle->network_info_handle, ntp_server);
1904         if (rv == NET_ERR_ACCESS_DENIED) {
1905                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1906                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1907         } else if (rv != NET_ERR_NONE) {
1908                 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get NTP Server [%d]", rv); //LCOV_EXCL_LINE
1909                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1910         }
1911
1912         return CONNECTION_ERROR_NONE;
1913 }
1914
1915 int _connection_libnet_set_ntp_server(connection_handle_s *conn_handle, const char *ntp_server)
1916 {
1917         int rv = 0;
1918
1919         rv = net_set_ntp_server(conn_handle->network_info_handle, ntp_server);
1920         if (rv == NET_ERR_ACCESS_DENIED) {
1921                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1922                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1923         } else if (rv != NET_ERR_NONE) {
1924                 CONNECTION_LOG(CONNECTION_ERROR, "Failed to set NTP server[%d]", rv); //LCOV_EXCL_LINE
1925                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1926         }
1927
1928         return CONNECTION_ERROR_NONE;
1929 }
1930
1931 int _connection_libnet_clear_ntp_server(connection_handle_s *conn_handle)
1932 {
1933         int rv = 0;
1934
1935         rv = net_clear_ntp_server(conn_handle->network_info_handle);
1936         if (rv == NET_ERR_ACCESS_DENIED) {
1937                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1938                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1939         } else if (rv != NET_ERR_NONE) {
1940                 CONNECTION_LOG(CONNECTION_ERROR, "Failed to clear NTP server[%d]", rv); //LCOV_EXCL_LINE
1941                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1942         }
1943
1944         return CONNECTION_ERROR_NONE;
1945 }
1946 //LCOV_EXCL_STOP
1947
1948 void _connection_lock(void)
1949 {
1950         if (g_conn_thread_mutex_ref == 0)
1951                 pthread_mutex_lock(&g_conn_thread_mutex);
1952
1953         g_conn_thread_mutex_ref++;
1954 }
1955
1956 void _connection_unlock(void)
1957 {
1958         if (g_conn_thread_mutex_ref == 1)
1959                 pthread_mutex_unlock(&g_conn_thread_mutex);
1960
1961         g_conn_thread_mutex_ref--;
1962
1963 //LCOV_EXCL_START
1964         if (g_conn_thread_mutex_ref < 0) {
1965                 CONNECTION_LOG(CONNECTION_ERROR,
1966                                 "Error scenario, thread specific mutex ref is negative !!!");
1967                 g_conn_thread_mutex_ref = 0;
1968         }
1969 //LCOV_EXCL_STOP
1970 }