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