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