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