Use mutex instead of copying of network_info inside event handler
[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         CONN_LOCK;
174         bool is_requested = false;
175         connection_error_e result = CONNECTION_ERROR_NONE;
176         connection_handle_s *conn_handle = (connection_handle_s *)user_data;
177
178         if (!_connection_check_handle_validity(conn_handle)) {
179                 CONNECTION_LOG(CONNECTION_INFO, "Invalid handle");
180                 CONN_UNLOCK;
181                 return;
182         }
183
184         switch (event_cb->Event) {
185         case NET_EVENT_OPEN_RSP:
186                 is_requested = true;
187                 /* fall through */
188         case NET_EVENT_OPEN_IND:
189                 result = __libnet_convert_to_cp_error_type(event_cb->Error);
190                 CONNECTION_LOG(CONNECTION_INFO, "Connection opened %s[%s]",
191                                         (is_requested) ? "RSP" : "IND",
192                                         __libnet_convert_cp_error_type_to_string(result));
193
194                 if (is_requested) {
195                         if (conn_handle->opened_callback) {
196                                 conn_handle->opened_callback(result,
197                                         conn_handle->opened_user_data);
198
199                                 conn_handle->opened_callback = NULL;
200                                 conn_handle->opened_user_data = NULL;
201                         }
202                 }
203
204                 switch (event_cb->Error) {
205                 case NET_ERR_NONE:
206                 case NET_ERR_ACTIVE_CONNECTION_EXISTS:
207                         CONNECTION_LOG(CONNECTION_INFO, "Successfully open connection");
208
209                         __libnet_state_changed_cb(event_cb->ProfileName, CONNECTION_PROFILE_STATE_CONNECTED);
210                         CONN_UNLOCK;
211                         return;
212                 default:
213                         CONNECTION_LOG(CONNECTION_ERROR, "Failed to open connection[%s]",
214                                                 __libnet_convert_cp_error_type_to_string(result));
215                 }
216
217                 __libnet_state_changed_cb(event_cb->ProfileName, CONNECTION_PROFILE_STATE_DISCONNECTED);
218
219                 break;
220         case NET_EVENT_CLOSE_RSP:
221                 is_requested = true;
222                 /* fall through */
223         case NET_EVENT_CLOSE_IND:
224                 result = __libnet_convert_to_cp_error_type(event_cb->Error);
225                 CONNECTION_LOG(CONNECTION_INFO, "Connection closed %s[%s]",
226                                         (is_requested) ? "RSP" : "IND",
227                                         __libnet_convert_cp_error_type_to_string(result));
228
229                 if (is_requested) {
230                         if (conn_handle->closed_callback) {
231                                 conn_handle->closed_callback(result,
232                                         conn_handle->closed_user_data);
233
234                                 conn_handle->closed_callback = NULL;
235                                 conn_handle->closed_user_data = NULL;
236                         }
237                 }
238
239                 switch (event_cb->Error) {
240                 case NET_ERR_NONE:
241                         CONNECTION_LOG(CONNECTION_INFO, "Successfully closed connection");
242
243                         __libnet_state_changed_cb(event_cb->ProfileName, CONNECTION_PROFILE_STATE_DISCONNECTED);
244                         CONN_UNLOCK;
245                         return;
246                 default:
247                         CONNECTION_LOG(CONNECTION_ERROR, "Failed to close connection[%s]",
248                                                         __libnet_convert_cp_error_type_to_string(result));
249                 }
250
251                 break;
252         case NET_EVENT_NET_STATE_IND:
253                 CONNECTION_LOG(CONNECTION_INFO, "State changed IND");
254
255                 if (event_cb->Datalength != sizeof(net_state_type_t)) {
256                         CONN_UNLOCK;
257                         return;
258                 }
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         CONN_UNLOCK;
427 }
428 //LCOV_EXCL_STOP
429
430 static int __libnet_get_connected_count(struct _profile_list_s *profile_list)
431 {
432         int count = 0;
433         int i = 0;
434
435         for (; i < profile_list->count; i++) {
436                 if (profile_list->profiles[i].ProfileState == NET_STATE_TYPE_ONLINE ||
437                     profile_list->profiles[i].ProfileState == NET_STATE_TYPE_READY)
438                         count++;
439         }
440
441         return count;
442 }
443
444 static void __libnet_copy_connected_profile(net_profile_info_t **dest, struct _profile_list_s *source)
445 {
446         int i = 0;
447
448         for (; i < source->count; i++) {
449                 if (source->profiles[i].ProfileState == NET_STATE_TYPE_ONLINE ||
450                     source->profiles[i].ProfileState == NET_STATE_TYPE_READY) {
451                         memcpy(*dest, &source->profiles[i], sizeof(net_profile_info_t));
452                         (*dest)++;
453                 }
454         }
455 }
456
457 //LCOV_EXCL_START
458 static int __libnet_get_default_count(struct _profile_list_s *profile_list)
459 {
460         int count = 0;
461         int i = 0;
462
463         for (; i < profile_list->count; i++) {
464                 if (profile_list->profiles[i].profile_type == NET_DEVICE_CELLULAR) {
465                         if (profile_list->profiles[i].ProfileInfo.Pdp.DefaultConn == TRUE)
466                                 count++;
467                 }
468         }
469
470         return count;
471 }
472
473 static void __libnet_copy_default_profile(net_profile_info_t **dest, struct _profile_list_s *source)
474 {
475         int i = 0;
476
477         for (; i < source->count; i++) {
478                 if (source->profiles[i].profile_type == NET_DEVICE_CELLULAR) {
479                         if (source->profiles[i].ProfileInfo.Pdp.DefaultConn == TRUE) {
480                                 memcpy(*dest, &source->profiles[i], sizeof(net_profile_info_t));
481                                 (*dest)++;
482                         }
483                 }
484         }
485 }
486 //LCOV_EXCL_STOP
487
488 int _connection_libnet_init(connection_handle_s *conn_handle)
489 {
490         int rv;
491
492         rv = net_register_client(&(conn_handle->network_info_handle),
493                                 (net_event_cb_t)__libnet_evt_cb, conn_handle);
494         if (rv != NET_ERR_NONE)
495                 return rv;
496
497         if (profile_cb_table == NULL)
498                 profile_cb_table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
499
500         return NET_ERR_NONE;
501 }
502
503 bool _connection_libnet_deinit(connection_handle_s *conn_handle, bool is_empty)
504 {
505         net_deregister_client(conn_handle->network_info_handle);
506
507         if (is_empty) {
508                 if (profile_cb_table) {
509                         g_hash_table_destroy(profile_cb_table);
510                         profile_cb_table = NULL;
511                 }
512
513                 __libnet_clear_profile_list(&profile_iterator);
514
515                 if (prof_handle_list) {
516                         g_slist_free_full(prof_handle_list, g_free);
517                         prof_handle_list = NULL;
518                 }
519         }
520
521         return true;
522 }
523
524 //LCOV_EXCL_START
525 void _connection_set_cs_tid(int tid, connection_handle_s *conn_handle)
526 {
527         net_set_cs_tid(tid, conn_handle->network_info_handle);
528 }
529
530 void _connection_unset_cs_tid(int tid, connection_handle_s *conn_handle)
531 {
532         net_unset_cs_tid(tid, conn_handle->network_info_handle);
533 }
534 //LCOV_EXCL_STOP
535
536 bool _connection_libnet_check_profile_validity(connection_profile_h profile)
537 {
538         GSList *list;
539         int i = 0;
540
541         if (profile == NULL)
542                 return false;
543
544         for (list = prof_handle_list; list; list = list->next)
545                 if (profile == list->data) return true;
546
547         for (; i < profile_iterator.count; i++)
548                 if (profile == &profile_iterator.profiles[i]) return true;
549
550         return false;
551 }
552
553 int _connection_libnet_get_metered_state(connection_handle_s *conn_handle, bool* is_metered)
554 {
555         int rv = 0;
556         int status = 0;
557
558         rv = net_get_metered_state(conn_handle->network_info_handle, &status);
559         if (rv == NET_ERR_ACCESS_DENIED) {
560                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
561                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
562         } else if (rv != NET_ERR_NONE) {
563                 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get metered state[%d]", rv); //LCOV_EXCL_LINE
564                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
565         }
566
567         if (status == 1)
568                 *is_metered = true;
569         else
570                 *is_metered = false;
571         return CONNECTION_ERROR_NONE;
572 }
573
574 int _connection_libnet_get_wifi_state(connection_handle_s *conn_handle, connection_wifi_state_e *state)
575 {
576         int rv;
577         net_wifi_state_t wlan_state;
578
579         rv = net_get_wifi_state(conn_handle->network_info_handle, &wlan_state);
580         if (rv == NET_ERR_ACCESS_DENIED) {
581                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
582                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
583         } else if (rv != NET_ERR_NONE) {
584                 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get Wi-Fi state[%d]", rv); //LCOV_EXCL_LINE
585                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
586         }
587
588         switch (wlan_state) {
589         case WIFI_OFF:
590                 *state = CONNECTION_WIFI_STATE_DEACTIVATED;
591                 break;
592         case WIFI_ON:
593         case WIFI_ASSOCIATION:
594         case WIFI_CONFIGURATION:
595                 *state = CONNECTION_WIFI_STATE_DISCONNECTED;
596                 break;
597         case WIFI_CONNECTED:
598         case WIFI_DISCONNECTING:
599                 *state = CONNECTION_WIFI_STATE_CONNECTED;
600                 break;
601         default:
602                 CONNECTION_LOG(CONNECTION_ERROR, "Unknown Wi-Fi state"); //LCOV_EXCL_LINE
603                 return CONNECTION_ERROR_INVALID_OPERATION; //LCOV_EXCL_LINE
604         }
605
606         return CONNECTION_ERROR_NONE;
607 }
608
609 //LCOV_EXCL_START
610 int _connection_libnet_get_ethernet_state(connection_handle_s *conn_handle,
611                         connection_ethernet_state_e *state)
612 {
613         int rv;
614         struct _profile_list_s ethernet_profiles = {0, 0, NULL};
615         rv = net_get_profile_list(conn_handle->network_info_handle,
616                         NET_DEVICE_ETHERNET, &ethernet_profiles.profiles,
617                         &ethernet_profiles.count);
618         if (rv == NET_ERR_ACCESS_DENIED) {
619                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
620                 return CONNECTION_ERROR_PERMISSION_DENIED;
621         }
622
623         if (ethernet_profiles.count == 0) {
624                 *state = CONNECTION_ETHERNET_STATE_DEACTIVATED;
625                 return CONNECTION_ERROR_NONE;
626         }
627
628         switch (ethernet_profiles.profiles->ProfileState) {
629         case NET_STATE_TYPE_ONLINE:
630         case NET_STATE_TYPE_READY:
631                 *state = CONNECTION_ETHERNET_STATE_CONNECTED;
632                 break;
633         case NET_STATE_TYPE_IDLE:
634         case NET_STATE_TYPE_FAILURE:
635         case NET_STATE_TYPE_ASSOCIATION:
636         case NET_STATE_TYPE_CONFIGURATION:
637         case NET_STATE_TYPE_DISCONNECT:
638                 *state = CONNECTION_ETHERNET_STATE_DISCONNECTED;
639                 break;
640         default:
641                 __libnet_clear_profile_list(&ethernet_profiles);
642                 return CONNECTION_ERROR_OPERATION_FAILED;
643         }
644
645         __libnet_clear_profile_list(&ethernet_profiles);
646
647         return CONNECTION_ERROR_NONE;
648 }
649
650 int _connection_libnet_get_ethernet_cable_state(connection_handle_s *conn_handle,
651                         connection_ethernet_cable_state_e* state)
652 {
653         int rv = 0;
654         int status = 0;
655
656         rv = net_get_ethernet_cable_state(conn_handle->network_info_handle, &status);
657         if (rv == NET_ERR_ACCESS_DENIED) {
658                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
659                 return CONNECTION_ERROR_PERMISSION_DENIED;
660         } else if (rv != NET_ERR_NONE) {
661                 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get ethernet cable state[%d]", rv);
662                 return CONNECTION_ERROR_OPERATION_FAILED;
663         }
664
665         if (status == 1)
666                 *state = CONNECTION_ETHERNET_CABLE_ATTACHED;
667         else
668                 *state = CONNECTION_ETHERNET_CABLE_DETACHED;
669         return CONNECTION_ERROR_NONE;
670 }
671 //LCOV_EXCL_STOP
672
673 int _connection_libnet_get_bluetooth_state(connection_handle_s *conn_handle, connection_bt_state_e *state)
674 {
675         int i = 0;
676         int rv = 0;
677         struct _profile_list_s bluetooth_profiles = {0, 0, NULL};
678         rv = net_get_profile_list(conn_handle->network_info_handle,
679                         NET_DEVICE_BLUETOOTH, &bluetooth_profiles.profiles,
680                         &bluetooth_profiles.count);
681         if (rv == NET_ERR_ACCESS_DENIED) {
682                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
683                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
684         }
685
686         if (bluetooth_profiles.count == 0) {
687                 *state = CONNECTION_BT_STATE_DEACTIVATED;
688                 return CONNECTION_ERROR_NONE;
689         }
690
691         //LCOV_EXCL_START
692         for (; i < bluetooth_profiles.count; i++) {
693                 switch (bluetooth_profiles.profiles[i].ProfileState) {
694                 case NET_STATE_TYPE_ONLINE:
695                 case NET_STATE_TYPE_READY:
696                         *state = CONNECTION_BT_STATE_CONNECTED;
697                         goto done;
698                 case NET_STATE_TYPE_IDLE:
699                 case NET_STATE_TYPE_FAILURE:
700                 case NET_STATE_TYPE_ASSOCIATION:
701                 case NET_STATE_TYPE_CONFIGURATION:
702                 case NET_STATE_TYPE_DISCONNECT:
703                         *state = CONNECTION_BT_STATE_DISCONNECTED;
704                         break;
705                 default:
706                         __libnet_clear_profile_list(&bluetooth_profiles);
707                         return CONNECTION_ERROR_OPERATION_FAILED;
708                 }
709         }
710         //LCOV_EXCL_STOP
711
712 done:
713         __libnet_clear_profile_list(&bluetooth_profiles);
714
715         return CONNECTION_ERROR_NONE;
716 }
717
718 int _connection_libnet_get_profile_iterator(connection_handle_s *conn_handle,
719                         connection_iterator_type_e type, connection_profile_iterator_h* profile_iter_h)
720 {
721         int count = 0;
722         int rv;
723         net_profile_info_t *profiles = NULL;
724
725         struct _profile_list_s profile_list = {0, 0, NULL};
726
727         __libnet_clear_profile_list(&profile_iterator);
728
729         rv = net_get_all_profile_list(conn_handle->network_info_handle,
730                         &profile_list.profiles, &profile_list.count);
731         if (rv == NET_ERR_ACCESS_DENIED) {
732                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
733                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
734         } else if (rv != NET_ERR_NO_SERVICE && rv != NET_ERR_NONE)
735                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
736
737         *profile_iter_h = &profile_iterator;
738
739         switch (type) {
740         case CONNECTION_ITERATOR_TYPE_REGISTERED:
741                 count = profile_list.count;
742                 CONNECTION_LOG(CONNECTION_INFO, "Total profile count : %d", count);
743                 if (count == 0)
744                         return CONNECTION_ERROR_NONE;
745
746                 profiles = g_try_new0(net_profile_info_t, count);
747                 if (profiles == NULL) {
748                         __libnet_clear_profile_list(&profile_list); //LCOV_EXCL_LINE
749                         return CONNECTION_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
750                 }
751
752                 profile_iterator.profiles = profiles;
753
754                 memcpy(profiles, profile_list.profiles, sizeof(net_profile_info_t) * count);
755
756                 break;
757         case CONNECTION_ITERATOR_TYPE_CONNECTED:
758                 count = __libnet_get_connected_count(&profile_list);
759                 CONNECTION_LOG(CONNECTION_INFO, "Total connected profile count : %d", count);
760                 if (count == 0)
761                         return CONNECTION_ERROR_NONE;
762
763                 profiles = g_try_new0(net_profile_info_t, count);
764                 if (profiles == NULL) {
765                         __libnet_clear_profile_list(&profile_list); //LCOV_EXCL_LINE
766                         return CONNECTION_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
767                 }
768
769                 profile_iterator.profiles = profiles;
770
771                 __libnet_copy_connected_profile(&profiles, &profile_list);
772
773                 break;
774         case CONNECTION_ITERATOR_TYPE_DEFAULT:
775                 count = __libnet_get_default_count(&profile_list);
776                 CONNECTION_LOG(CONNECTION_INFO, "Total default profile count : %d", count); //LCOV_EXCL_LINE
777                 if (count == 0)
778                         return CONNECTION_ERROR_NONE;
779
780                 profiles = g_try_new0(net_profile_info_t, count);
781                 if (profiles == NULL) {
782                         __libnet_clear_profile_list(&profile_list);
783                         return CONNECTION_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
784                 }
785
786                 profile_iterator.profiles = profiles;
787
788                 __libnet_copy_default_profile(&profiles, &profile_list);
789
790                 break;
791         }
792
793         __libnet_clear_profile_list(&profile_list);
794
795         profile_iterator.count = count;
796
797         return CONNECTION_ERROR_NONE;
798 }
799
800 int _connection_libnet_get_iterator_next(connection_profile_iterator_h profile_iter_h, connection_profile_h *profile)
801 {
802         if (profile_iter_h != &profile_iterator)
803                 return CONNECTION_ERROR_INVALID_PARAMETER;
804
805         if (profile_iterator.count <= profile_iterator.next)
806                 return CONNECTION_ERROR_ITERATOR_END;
807
808         *profile = &profile_iterator.profiles[profile_iterator.next];
809         profile_iterator.next++;
810
811         return CONNECTION_ERROR_NONE;
812 }
813
814 bool _connection_libnet_iterator_has_next(connection_profile_iterator_h profile_iter_h)
815 {
816         if (profile_iter_h != &profile_iterator)
817                 return false;
818
819         if (profile_iterator.count <= profile_iterator.next)
820                 return false;
821
822         return true;
823 }
824
825 int _connection_libnet_destroy_iterator(connection_profile_iterator_h profile_iter_h)
826 {
827         if (profile_iter_h != &profile_iterator)
828                 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
829
830         __libnet_clear_profile_list(&profile_iterator);
831
832         return CONNECTION_ERROR_NONE;
833 }
834
835 int _connection_libnet_get_current_profile(connection_handle_s *conn_handle,
836                         connection_profile_h *profile)
837 {
838         net_profile_info_t active_profile;
839         int rv;
840
841         rv = net_get_active_net_info(conn_handle->network_info_handle, &active_profile);
842         if (rv == NET_ERR_NO_SERVICE)
843                 return CONNECTION_ERROR_NO_CONNECTION; //LCOV_EXCL_LINE
844         else 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                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
849
850         *profile = g_try_malloc0(sizeof(net_profile_info_t));
851         if (*profile == NULL)
852                 return CONNECTION_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
853
854         memcpy(*profile, &active_profile, sizeof(net_profile_info_t));
855         prof_handle_list = g_slist_append(prof_handle_list, *profile);
856
857         return CONNECTION_ERROR_NONE;
858 }
859
860 int _connection_libnet_reset_profile(connection_handle_s *conn_handle,
861                 connection_reset_option_e type, connection_cellular_subscriber_id_e id)
862 {
863         int rv;
864
865         rv = net_reset_profile(conn_handle->network_info_handle, type, id);
866         if (rv == NET_ERR_ACCESS_DENIED)
867                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
868         else if (rv != NET_ERR_NONE)
869                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
870
871         return CONNECTION_ERROR_NONE;
872 }
873
874 int _connection_libnet_open_profile(connection_handle_s *conn_handle,
875                         connection_profile_h profile)
876 {
877         int rv;
878
879         if (!(_connection_libnet_check_profile_validity(profile))) {
880                 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter"); //LCOV_EXCL_LINE
881                 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
882         }
883
884         net_profile_info_t *profile_info = profile;
885
886         if (profile_info->profile_type == NET_DEVICE_MESH)
887                 rv = net_open_mesh_connection_with_profile(conn_handle->network_info_handle, //LCOV_EXCL_LINE
888                                 profile_info->ProfileName);
889         else
890                 rv = net_open_connection_with_profile(conn_handle->network_info_handle,
891                                 profile_info->ProfileName);
892
893         if (rv == NET_ERR_ACCESS_DENIED)
894                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
895         else if (rv != NET_ERR_NONE)
896                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
897
898         return CONNECTION_ERROR_NONE;
899 }
900
901 int _connection_libnet_get_cellular_service_profile(connection_handle_s *conn_handle,
902                 connection_cellular_service_type_e type, connection_profile_h *profile)
903 {
904         int i = 0, j = 0;
905         int rv = NET_ERR_NONE;
906 #if defined TIZEN_DUALSIM_ENABLE
907         int default_subscriber_id = 0;
908         char subscriber_id[3];
909 #endif
910
911         struct _profile_list_s cellular_profiles = { 0, 0, NULL };
912         net_service_type_t service_type = _connection_profile_convert_to_libnet_cellular_service_type(type);
913
914         rv = net_get_profile_list(conn_handle->network_info_handle,
915                         NET_DEVICE_CELLULAR, &cellular_profiles.profiles,
916                         &cellular_profiles.count);
917         if (rv == NET_ERR_ACCESS_DENIED) {
918                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
919                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
920         } else if (rv != NET_ERR_NONE) {
921                 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get profile list (%d)", rv); //LCOV_EXCL_LINE
922                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
923         }
924
925 #if defined TIZEN_DUALSIM_ENABLE
926         if (vconf_get_int(VCONF_TELEPHONY_DEFAULT_DATA_SERVICE,
927                                                 &default_subscriber_id) != 0) {
928                 CONNECTION_LOG(CONNECTION_ERROR,
929                                                 "Failed to get VCONF_TELEPHONY_DEFAULT_DATA_SERVICE");
930                 __libnet_clear_profile_list(&cellular_profiles); //LCOV_EXCL_LINE
931                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
932         }
933
934         g_snprintf(subscriber_id, sizeof(subscriber_id), "%d", default_subscriber_id);
935 #endif
936
937         for (i = 0; i < cellular_profiles.count; i++)
938                 if (cellular_profiles.profiles[i].ProfileInfo.Pdp.ServiceType == service_type)
939 #if defined TIZEN_DUALSIM_ENABLE
940                         if (g_str_has_suffix(
941                                         cellular_profiles.profiles[i].ProfileInfo.Pdp.PSModemPath,
942                                         subscriber_id) == TRUE)
943 #endif
944                                 break;
945
946         if (i >= cellular_profiles.count) {
947                 __libnet_clear_profile_list(&cellular_profiles); //LCOV_EXCL_LINE
948                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
949         }
950
951         *profile = g_try_malloc0(sizeof(net_profile_info_t));
952         if (*profile == NULL) {
953                 __libnet_clear_profile_list(&cellular_profiles); //LCOV_EXCL_LINE
954                 return CONNECTION_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
955         }
956
957         memcpy(*profile, &cellular_profiles.profiles[i], sizeof(net_profile_info_t));
958
959         if (cellular_profiles.profiles[i].ProfileInfo.Pdp.DefaultConn)
960                 goto done;
961
962         //LCOV_EXCL_START
963         if (type != CONNECTION_CELLULAR_SERVICE_TYPE_INTERNET &&
964             type != CONNECTION_CELLULAR_SERVICE_TYPE_PREPAID_INTERNET)
965                 goto done;
966
967         for (j = 0; j < cellular_profiles.count; j++) {
968                 if (i == j)
969                         continue;
970
971                 if (cellular_profiles.profiles[j].ProfileInfo.Pdp.ServiceType != service_type)
972                         continue;
973
974                 if (cellular_profiles.profiles[j].ProfileInfo.Pdp.DefaultConn) {
975                         memcpy(*profile, &cellular_profiles.profiles[j], sizeof(net_profile_info_t));
976                         goto done;
977                 }
978         }
979         //LCOV_EXCL_STOP
980
981 done:
982         __libnet_clear_profile_list(&cellular_profiles);
983         prof_handle_list = g_slist_append(prof_handle_list, *profile);
984
985         return CONNECTION_ERROR_NONE;
986 }
987
988 int _connection_libnet_set_cellular_service_profile_sync(connection_handle_s *conn_handle,
989                         connection_cellular_service_type_e type, connection_profile_h profile)
990 {
991         int rv;
992
993         if (!(_connection_libnet_check_profile_validity(profile))) {
994                 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter"); //LCOV_EXCL_LINE
995                 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
996         }
997
998         net_profile_info_t *profile_info = profile;
999         connection_cellular_service_type_e service_type;
1000
1001         service_type = _profile_convert_to_connection_cellular_service_type(profile_info->ProfileInfo.Pdp.ServiceType);
1002
1003         if (service_type != type)
1004                 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1005
1006         rv = net_set_default_cellular_service_profile(conn_handle->network_info_handle,
1007                                 profile_info->ProfileName);
1008         if (rv == NET_ERR_ACCESS_DENIED) {
1009                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1010                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1011         } else if (rv != NET_ERR_NONE)
1012                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1013
1014         return CONNECTION_ERROR_NONE;
1015 }
1016
1017 int _connection_libnet_set_cellular_service_profile_async(connection_handle_s *conn_handle,
1018                         connection_cellular_service_type_e type, connection_profile_h profile)
1019 {
1020         int rv;
1021
1022         if (!(_connection_libnet_check_profile_validity(profile))) {
1023                 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter"); //LCOV_EXCL_LINE
1024                 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1025         }
1026
1027         net_profile_info_t *profile_info = profile;
1028         connection_cellular_service_type_e service_type;
1029
1030         service_type = _profile_convert_to_connection_cellular_service_type(profile_info->ProfileInfo.Pdp.ServiceType);
1031
1032         if (service_type != type)
1033                 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1034
1035         rv = net_set_default_cellular_service_profile_async(conn_handle->network_info_handle,
1036                                 profile_info->ProfileName);
1037         if (rv == NET_ERR_ACCESS_DENIED) {
1038                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1039         } else if (rv != NET_ERR_NONE)
1040                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1041
1042         return CONNECTION_ERROR_NONE;
1043 }
1044
1045 int _connection_libnet_close_profile(connection_handle_s *conn_handle, connection_profile_h profile)
1046 {
1047         int rv;
1048
1049         if (!(_connection_libnet_check_profile_validity(profile))) {
1050                 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter"); //LCOV_EXCL_LINE
1051                 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1052         }
1053
1054         net_profile_info_t *profile_info = profile;
1055
1056         if (profile_info->profile_type == NET_DEVICE_MESH)
1057                 rv = net_close_mesh_connection(conn_handle->network_info_handle, profile_info->ProfileName); //LCOV_EXCL_LINE
1058         else
1059                 rv = net_close_connection(conn_handle->network_info_handle, profile_info->ProfileName);
1060
1061         if (rv == NET_ERR_ACCESS_DENIED)
1062                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1063         else if (rv != NET_ERR_NONE)
1064                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1065
1066         return CONNECTION_ERROR_NONE;
1067 }
1068
1069 int _connection_libnet_add_route(connection_handle_s *conn_handle,
1070                         const char *interface_name, const char *host_address)
1071 {
1072         int rv;
1073         char *endstr = NULL;
1074         int address_family = 0;
1075
1076         address_family = AF_INET;
1077
1078         endstr = strrchr(host_address, '.');
1079         if (endstr == NULL ||
1080                         strcmp(endstr, ".0") == 0 ||
1081                         strncmp(host_address, "0.", 2) == 0 ||
1082                         strstr(host_address, "255") != NULL) {
1083                 CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed\n"); //LCOV_EXCL_LINE
1084                 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1085         }
1086
1087         rv = net_add_route(conn_handle->network_info_handle,
1088                                 host_address, interface_name, address_family);
1089         if (rv == NET_ERR_ACCESS_DENIED) {
1090                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1091                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1092         } else if (rv != NET_ERR_NONE)
1093                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1094
1095         return CONNECTION_ERROR_NONE;
1096 }
1097
1098 int _connection_libnet_remove_route(connection_handle_s *conn_handle,
1099                         const char *interface_name, const char *host_address)
1100 {
1101         int rv;
1102         char *endstr = strrchr(host_address, '.');
1103         int address_family = 0;
1104
1105         address_family = AF_INET;
1106
1107         endstr = strrchr(host_address, '.');
1108         if (endstr == NULL ||
1109                 strcmp(endstr, ".0") == 0 ||
1110                 strncmp(host_address, "0.", 2) == 0 ||
1111                 strstr(host_address, ".0.") != NULL || strstr(host_address, "255") != NULL) {
1112                 CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed"); //LCOV_EXCL_LINE
1113                 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1114         }
1115
1116         rv = net_remove_route(conn_handle->network_info_handle,
1117                                 host_address, interface_name, address_family);
1118         if (rv == NET_ERR_ACCESS_DENIED) {
1119                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1120                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1121         } else if (rv != NET_ERR_NONE)
1122                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1123
1124         return CONNECTION_ERROR_NONE;
1125 }
1126
1127 int _connection_libnet_add_route_ipv6(connection_handle_s *conn_handle,
1128                         const char *interface_name, const char *host_address, const char *gateway)
1129 {
1130         int rv;
1131         int address_family = 0;
1132
1133         address_family = AF_INET6;
1134
1135         if (strncmp(host_address, "fe80:", 5) == 0 ||
1136                 strncmp(host_address, "ff00:", 5) == 0 ||
1137                 strncmp(host_address, "::", 2) == 0) {
1138                 CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed\n"); //LCOV_EXCL_LINE
1139                 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1140         }
1141
1142         rv = net_add_route_ipv6(conn_handle->network_info_handle,
1143                                 host_address, interface_name, address_family, gateway);
1144         if (rv == NET_ERR_ACCESS_DENIED) {
1145                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1146                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1147         } else if (rv != NET_ERR_NONE)
1148                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1149
1150         return CONNECTION_ERROR_NONE;
1151 }
1152
1153 int _connection_libnet_remove_route_ipv6(connection_handle_s *conn_handle,
1154                         const char *interface_name, const char *host_address, const char *gateway)
1155 {
1156         int rv;
1157         int address_family = 0;
1158
1159         address_family = AF_INET6;
1160
1161         if (strncmp(host_address, "fe80:", 5) == 0 ||
1162                 strncmp(host_address, "ff00:", 5) == 0 ||
1163                 strncmp(host_address, "::", 2) == 0) {
1164                 CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed\n"); //LCOV_EXCL_LINE
1165                 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1166         }
1167
1168         rv = net_remove_route_ipv6(conn_handle->network_info_handle,
1169                                 host_address, interface_name, address_family, gateway);
1170         if (rv == NET_ERR_ACCESS_DENIED) {
1171                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1172                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1173         } else if (rv != NET_ERR_NONE)
1174                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1175
1176         return CONNECTION_ERROR_NONE;
1177 }
1178
1179 int _connection_libnet_add_route_entry(connection_handle_s *conn_handle,
1180                 connection_address_family_e address_family, const char *interface_name,
1181                 const char *host_address, const char *gateway)
1182 {
1183         int rv;
1184         char *endstr = NULL;
1185         int address_family_type = 0;
1186
1187         if (address_family == CONNECTION_ADDRESS_FAMILY_IPV4)
1188                 address_family_type = AF_INET;
1189         else
1190                 address_family_type = AF_INET6;
1191
1192         if (address_family == CONNECTION_ADDRESS_FAMILY_IPV4) {
1193
1194                 endstr = strrchr(host_address, '.');
1195                 if (endstr == NULL ||
1196                                 strcmp(endstr, ".0") == 0 ||
1197                                 strncmp(host_address, "0.", 2) == 0 ||
1198                                 strstr(host_address, "255") != NULL) {
1199                         CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed\n"); //LCOV_EXCL_LINE
1200                         return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1201                 }
1202
1203                 rv = net_add_route_entry(conn_handle->network_info_handle,
1204                                 host_address, interface_name, address_family_type, gateway);
1205                 if (rv == NET_ERR_ACCESS_DENIED) {
1206                         CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1207                         return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1208                 } else if (rv != NET_ERR_NONE)
1209                         return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1210
1211         } else {
1212
1213                 if (strncmp(host_address, "fe80:", 5) == 0 ||
1214                         strncmp(host_address, "ff00:", 5) == 0 ||
1215                         strncmp(host_address, "::", 2) == 0) {
1216                         CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed\n"); //LCOV_EXCL_LINE
1217                         return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1218                 }
1219
1220                 rv = net_add_route_ipv6(conn_handle->network_info_handle,
1221                                 host_address, interface_name, address_family_type, gateway);
1222                 if (rv == NET_ERR_ACCESS_DENIED) {
1223                         CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1224                         return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1225                 } else if (rv != NET_ERR_NONE)
1226                         return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1227         }
1228
1229         return CONNECTION_ERROR_NONE;
1230 }
1231
1232 int _connection_libnet_remove_route_entry(connection_handle_s *conn_handle,
1233                 connection_address_family_e address_family, const char *interface_name,
1234                 const char *host_address, const char *gateway)
1235 {
1236         int rv;
1237         char *endstr = strrchr(host_address, '.');
1238         int address_family_type = 0;
1239
1240         if (address_family == CONNECTION_ADDRESS_FAMILY_IPV4)
1241                 address_family_type = AF_INET;
1242         else
1243                 address_family_type = AF_INET6;
1244
1245         if (address_family == CONNECTION_ADDRESS_FAMILY_IPV4) {
1246                 endstr = strrchr(host_address, '.');
1247                 if (endstr == NULL ||
1248                         strcmp(endstr, ".0") == 0 ||
1249                         strncmp(host_address, "0.", 2) == 0 ||
1250                         strstr(host_address, ".0.") != NULL || strstr(host_address, "255") != NULL) {
1251                         CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed"); //LCOV_EXCL_LINE
1252                         return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1253                 }
1254
1255                 rv = net_remove_route_entry(conn_handle->network_info_handle, host_address,
1256                                         interface_name, address_family_type, gateway);
1257                 if (rv == NET_ERR_ACCESS_DENIED) {
1258                         CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1259                         return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1260                 } else if (rv != NET_ERR_NONE)
1261                         return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1262
1263         } else {
1264
1265                 if (strncmp(host_address, "fe80:", 5) == 0 ||
1266                         strncmp(host_address, "ff00:", 5) == 0 ||
1267                         strncmp(host_address, "::", 2) == 0) {
1268                         CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed\n"); //LCOV_EXCL_LINE
1269                         return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1270                 }
1271
1272                 rv = net_remove_route_ipv6(conn_handle->network_info_handle, host_address,
1273                                         interface_name, address_family_type, gateway);
1274                 if (rv == NET_ERR_ACCESS_DENIED) {
1275                         CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1276                         return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1277                 } else if (rv != NET_ERR_NONE)
1278                         return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1279         }
1280
1281         return CONNECTION_ERROR_NONE;
1282 }
1283
1284 void _connection_libnet_add_to_profile_list(connection_profile_h profile)
1285 {
1286         prof_handle_list = g_slist_append(prof_handle_list, profile);
1287 }
1288
1289 void _connection_libnet_remove_from_profile_list(connection_profile_h profile)
1290 {
1291         prof_handle_list = g_slist_remove(prof_handle_list, profile);
1292         g_free(profile);
1293 }
1294
1295 bool _connection_libnet_add_to_profile_cb_list(connection_profile_h profile,
1296                 connection_profile_state_changed_cb callback, void *user_data)
1297 {
1298         net_profile_info_t *profile_info = profile;
1299         char *profile_name = g_strdup(profile_info->ProfileName);
1300
1301         struct _profile_cb_s *profile_cb_info = g_try_malloc0(sizeof(struct _profile_cb_s));
1302         if (profile_cb_info == NULL) {
1303                 g_free(profile_name); //LCOV_EXCL_LINE
1304                 return false; //LCOV_EXCL_LINE
1305         }
1306
1307         profile_cb_info->callback = callback;
1308         profile_cb_info->user_data = user_data;
1309         profile_cb_info->state = _profile_convert_to_cp_state(profile_info->ProfileState);
1310
1311         g_hash_table_replace(profile_cb_table, profile_name, profile_cb_info);
1312
1313         return true;
1314 }
1315
1316 bool _connection_libnet_remove_from_profile_cb_list(connection_profile_h profile)
1317 {
1318         net_profile_info_t *profile_info = profile;
1319
1320         if (g_hash_table_remove(profile_cb_table, profile_info->ProfileName) == TRUE)
1321                 return true;
1322
1323         return false; //LCOV_EXCL_LINE
1324 }
1325
1326 int _connection_libnet_set_statistics(connection_handle_s *conn_handle,
1327                         net_device_t device_type, net_statistics_type_e statistics_type)
1328 {
1329         int rv;
1330         rv = net_set_statistics(conn_handle->network_info_handle,
1331                                 device_type, statistics_type);
1332         if (rv == NET_ERR_ACCESS_DENIED) {
1333                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1334                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1335         } else if (rv != NET_ERR_NONE)
1336                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1337
1338         return CONNECTION_ERROR_NONE;
1339 }
1340
1341 int _connection_libnet_get_statistics(connection_handle_s *conn_handle,
1342                         net_statistics_type_e statistics_type, unsigned long long *size)
1343 {
1344         int rv;
1345         rv = net_get_statistics(conn_handle->network_info_handle,
1346                                 NET_DEVICE_WIFI, statistics_type, size);
1347         if (rv == NET_ERR_ACCESS_DENIED) {
1348                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1349                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1350         } else if (rv != NET_ERR_NONE)
1351                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1352
1353         return CONNECTION_ERROR_NONE;
1354 }
1355
1356 int _connection_libnet_set_cellular_subscriber_id(connection_profile_h profile,
1357                 connection_cellular_subscriber_id_e sim_id)
1358 {
1359         char *modem_path = NULL;
1360         net_profile_info_t *profile_info = (net_profile_info_t *)profile;
1361
1362         if (net_get_cellular_modem_object_path(&modem_path, sim_id) != NET_ERR_NONE) {
1363                 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get subscriber[%d]", sim_id); //LCOV_EXCL_LINE
1364                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1365         }
1366
1367         if (!modem_path) {
1368                 CONNECTION_LOG(CONNECTION_ERROR, "NULL modem object path"); //LCOV_EXCL_LINE
1369                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1370         }
1371
1372         g_strlcpy(profile_info->ProfileInfo.Pdp.PSModemPath, modem_path,
1373                                 NET_PROFILE_NAME_LEN_MAX);
1374         g_free(modem_path);
1375
1376         return CONNECTION_ERROR_NONE;
1377 }
1378
1379 int _connection_libnet_enable_ethernet_eap(bool enable, const char *profilename)
1380 {
1381         int rv = 0;
1382
1383         rv = net_ethernet_eap_enable(enable, profilename);
1384         if (rv == NET_ERR_ACCESS_DENIED) {
1385                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
1386                 return CONNECTION_ERROR_PERMISSION_DENIED;
1387         } else if (rv != NET_ERR_NONE) {
1388                 CONNECTION_LOG(CONNECTION_ERROR, "Failed to enable EAP over ethernet[%d]", rv);
1389                 return CONNECTION_ERROR_OPERATION_FAILED;
1390         }
1391
1392         return CONNECTION_ERROR_NONE;
1393 }
1394
1395 int _connection_libnet_ethernet_eap_enabled(bool *enabled)
1396 {
1397         int rv = 0;
1398         gboolean eap_enabled = false;
1399
1400         rv = net_ethernet_eap_supported(&eap_enabled);
1401         if (rv == NET_ERR_ACCESS_DENIED) {
1402                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
1403                 return CONNECTION_ERROR_PERMISSION_DENIED;
1404         } else if (rv != NET_ERR_NONE) {
1405                 CONNECTION_LOG(CONNECTION_ERROR, "Failed to enable EAP over ethernet[%d]", rv);
1406                 return CONNECTION_ERROR_OPERATION_FAILED;
1407         }
1408
1409         *enabled = eap_enabled;
1410         return CONNECTION_ERROR_NONE;
1411 }
1412
1413 int _connection_libnet_profile_save_ethernet_eap_config(connection_handle_s *conn_handle,
1414                         connection_profile_h profile)
1415 {
1416         int rv;
1417
1418         net_profile_info_t *profile_info = profile;
1419
1420         rv = net_save_ethernet_eap_config(conn_handle->network_info_handle,
1421                         &profile_info->ProfileInfo.Ethernet.net_info);
1422
1423         if (rv == NET_ERR_ACCESS_DENIED) {
1424                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
1425                 return CONNECTION_ERROR_PERMISSION_DENIED;
1426         } else if (rv != NET_ERR_NONE) {
1427                 CONNECTION_LOG(CONNECTION_ERROR, "Failed to save eap config[%d]", rv);
1428                 return CONNECTION_ERROR_OPERATION_FAILED;
1429         }
1430
1431         return CONNECTION_ERROR_NONE;
1432 }
1433
1434 int _connection_libnet_check_get_privilege(void)
1435 {
1436         int rv;
1437
1438         rv = net_check_get_privilege();
1439         if (rv == NET_ERR_ACCESS_DENIED) {
1440                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1441                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1442         } else if (rv != NET_ERR_NONE)
1443                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1444
1445         return CONNECTION_ERROR_NONE;
1446 }
1447
1448 int _connection_libnet_check_profile_privilege(void)
1449 {
1450         int rv;
1451
1452         rv = net_check_profile_privilege();
1453         if (rv == NET_ERR_ACCESS_DENIED) {
1454                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1455                 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1456         } else if (rv != NET_ERR_NONE)
1457                 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1458
1459         return CONNECTION_ERROR_NONE;
1460 }
1461
1462 bool __libnet_check_feature_supported(const char *key, connection_supported_feature_e feature)
1463 {
1464         if (!connection_is_feature_checked[feature]) {
1465                 if (system_info_get_platform_bool(key, &connection_feature_supported[feature]) < 0) {
1466                         CONNECTION_LOG(CONNECTION_ERROR, "Error - Feature getting from System Info"); //LCOV_EXCL_LINE
1467                         set_last_result(CONNECTION_ERROR_OPERATION_FAILED); //LCOV_EXCL_LINE
1468                         return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1469                 }
1470                 connection_is_feature_checked[feature] = true;
1471         }
1472         return connection_feature_supported[feature];
1473 }
1474
1475 int _connection_check_feature_supported(const char *feature_name, ...)
1476 {
1477         va_list list;
1478         const char *key;
1479         bool value = false;
1480         bool feature_supported = false;
1481
1482         va_start(list, feature_name);
1483         key = feature_name;
1484         while (1) {
1485                 if (strcmp(key, TELEPHONY_FEATURE) == 0)
1486                         value = __libnet_check_feature_supported(key, CONNECTION_SUPPORTED_FEATURE_TELEPHONY);
1487                 if (strcmp(key, WIFI_FEATURE) == 0)
1488                         value = __libnet_check_feature_supported(key, CONNECTION_SUPPORTED_FEATURE_WIFI);
1489                 if (strcmp(key, TETHERING_BLUETOOTH_FEATURE) == 0)
1490                         value = __libnet_check_feature_supported(key, CONNECTION_SUPPORTED_FEATURE_TETHERING_BLUETOOTH);
1491                 if (strcmp(key, ETHERNET_FEATURE) == 0)
1492                         value = __libnet_check_feature_supported(key, CONNECTION_SUPPORTED_FEATURE_ETHERNET);
1493
1494                 feature_supported |= value;
1495                 key = va_arg(list, const char *);
1496                 if (!key) break;
1497         }
1498         if (!feature_supported) {
1499                 CONNECTION_LOG(CONNECTION_ERROR, "Error - Feature is not supported");
1500                 set_last_result(CONNECTION_ERROR_NOT_SUPPORTED);
1501                 va_end(list);
1502                 return CONNECTION_ERROR_NOT_SUPPORTED;
1503         }
1504
1505         va_end(list);
1506         set_last_result(CONNECTION_ERROR_NONE);
1507         return CONNECTION_ERROR_NONE;
1508 }
1509
1510 //LCOV_EXCL_START
1511 int _connection_libnet_start_tcpdump(connection_handle_s *conn_handle)
1512 {
1513         connection_error_e result = CONNECTION_ERROR_NONE;
1514         net_err_t ret = NET_ERR_NONE;
1515
1516         ret = net_start_tcpdump(conn_handle->network_info_handle);
1517         result = __libnet_convert_to_cp_error_type(ret);
1518
1519         return result;
1520 }
1521
1522 int _connection_libnet_stop_tcpdump(connection_handle_s *conn_handle)
1523 {
1524         connection_error_e result = CONNECTION_ERROR_NONE;
1525         net_err_t ret = NET_ERR_NONE;
1526
1527         ret = net_stop_tcpdump(conn_handle->network_info_handle);
1528         result = __libnet_convert_to_cp_error_type(ret);
1529
1530         return result;
1531 }
1532
1533 int _connection_libnet_get_tcpdump_state(connection_handle_s *conn_handle,
1534                         gboolean *tcpdump_state)
1535 {
1536         connection_error_e result = CONNECTION_ERROR_NONE;
1537         net_err_t ret = NET_ERR_NONE;
1538
1539         ret = net_get_tcpdump_state(conn_handle->network_info_handle, tcpdump_state);
1540         result = __libnet_convert_to_cp_error_type(ret);
1541
1542         return result;
1543 }
1544 //LCOV_EXCL_STOP
1545
1546 void _connection_lock(void)
1547 {
1548         if (g_conn_thread_mutex_ref == 0)
1549                 pthread_mutex_lock(&g_conn_thread_mutex);
1550
1551         g_conn_thread_mutex_ref++;
1552 }
1553
1554 void _connection_unlock(void)
1555 {
1556         if (g_conn_thread_mutex_ref == 1)
1557                 pthread_mutex_unlock(&g_conn_thread_mutex);
1558
1559         g_conn_thread_mutex_ref--;
1560
1561 //LCOV_EXCL_START
1562         if (g_conn_thread_mutex_ref < 0) {
1563                 CONNECTION_LOG(CONNECTION_ERROR,
1564                                 "Error scenario, thread specific mutex ref is negative !!!");
1565                 g_conn_thread_mutex_ref = 0;
1566         }
1567 //LCOV_EXCL_STOP
1568 }