2 * Copyright (c) 2011-2013 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <vconf/vconf.h>
22 #include <system_info.h>
23 #include <arpa/inet.h>
26 #include "net_connection_private.h"
28 #define CONTAINER_FILE "/run/systemd/container"
30 static GSList *prof_handle_list = NULL;
31 static GHashTable *profile_cb_table = NULL;
32 static pthread_mutex_t g_conn_thread_mutex = PTHREAD_MUTEX_INITIALIZER;
33 static __thread int g_conn_thread_mutex_ref = 0;
34 static gboolean in_container = FALSE;
36 struct _profile_cb_s {
37 connection_profile_state_changed_cb callback;
38 connection_profile_state_e state;
42 struct _profile_list_s {
45 net_profile_info_t *profiles;
48 static struct _profile_list_s profile_iterator = {0, 0, NULL};
49 static bool connection_is_feature_checked[CONNECTION_SUPPORTED_FEATURE_MAX] = {0, };
50 static bool connection_feature_supported[CONNECTION_SUPPORTED_FEATURE_MAX] = {0, };
53 static connection_error_e __libnet_convert_to_cp_error_type(net_err_t err_type)
57 return CONNECTION_ERROR_NONE;
58 case NET_ERR_APP_ALREADY_REGISTERED:
59 return CONNECTION_ERROR_INVALID_OPERATION;
60 case NET_ERR_APP_NOT_REGISTERED:
61 return CONNECTION_ERROR_INVALID_OPERATION;
62 case NET_ERR_NO_ACTIVE_CONNECTIONS:
63 return CONNECTION_ERROR_NO_CONNECTION;
64 case NET_ERR_ACTIVE_CONNECTION_EXISTS:
65 return CONNECTION_ERROR_ALREADY_EXISTS;
66 case NET_ERR_CONNECTION_DHCP_FAILED:
67 return CONNECTION_ERROR_DHCP_FAILED;
68 case NET_ERR_CONNECTION_INVALID_KEY:
69 return CONNECTION_ERROR_INVALID_KEY;
70 case NET_ERR_IN_PROGRESS:
71 return CONNECTION_ERROR_NOW_IN_PROGRESS;
72 case NET_ERR_OPERATION_ABORTED:
73 return CONNECTION_ERROR_OPERATION_ABORTED;
74 case NET_ERR_TIME_OUT:
75 return CONNECTION_ERROR_NO_REPLY;
76 case NET_ERR_ACCESS_DENIED:
77 return CONNECTION_ERROR_PERMISSION_DENIED;
79 return CONNECTION_ERROR_OPERATION_FAILED;
83 static const char *__libnet_convert_cp_error_type_to_string(connection_error_e err_type)
86 case CONNECTION_ERROR_NONE:
88 case CONNECTION_ERROR_INVALID_PARAMETER:
89 return "INVALID_PARAMETER";
90 case CONNECTION_ERROR_OUT_OF_MEMORY:
91 return "OUT_OF_MEMORY";
92 case CONNECTION_ERROR_INVALID_OPERATION:
93 return "INVALID_OPERATION";
94 case CONNECTION_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED:
95 return "ADDRESS_FAMILY_NOT_SUPPORTED";
96 case CONNECTION_ERROR_OPERATION_FAILED:
97 return "OPERATION_FAILED";
98 case CONNECTION_ERROR_ITERATOR_END:
99 return "ITERATOR_END";
100 case CONNECTION_ERROR_NO_CONNECTION:
101 return "NO_CONNECTION";
102 case CONNECTION_ERROR_NOW_IN_PROGRESS:
103 return "NOW_IN_PROGRESS";
104 case CONNECTION_ERROR_ALREADY_EXISTS:
105 return "ALREADY_EXISTS";
106 case CONNECTION_ERROR_OPERATION_ABORTED:
107 return "OPERATION_ABORTED";
108 case CONNECTION_ERROR_DHCP_FAILED:
109 return "DHCP_FAILED";
110 case CONNECTION_ERROR_INVALID_KEY:
111 return "INVALID_KEY";
112 case CONNECTION_ERROR_NO_REPLY:
114 case CONNECTION_ERROR_PERMISSION_DENIED:
115 return "PERMISSION_DENIED";
116 case CONNECTION_ERROR_NOT_SUPPORTED:
117 return "NOT_SUPPORTED";
118 case CONNECTION_ERROR_ALREADY_INITIALIZED:
119 return "ALREADY_INITIALIZED";
120 case CONNECTION_ERROR_NOT_INITIALIZED:
121 return "NOT_INITIALIZED";
127 static const char *__libnet_convert_cp_state_to_string(connection_profile_state_e state)
130 case CONNECTION_PROFILE_STATE_DISCONNECTED:
131 return "DISCONNECTED";
132 case CONNECTION_PROFILE_STATE_ASSOCIATION:
133 return "ASSOCIATION";
134 case CONNECTION_PROFILE_STATE_CONFIGURATION:
135 return "CONFIGURATION";
136 case CONNECTION_PROFILE_STATE_CONNECTED:
143 char *_connection_vconf_get_str(connection_handle_s *conn_handle, const char *key)
147 char *str_value = NULL;
150 str_value = vconf_get_str(key);
152 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get vconfkey [%s] value", key);
157 if (conn_handle && net_get_vconf_value(conn_handle->network_info_handle,
158 key, "string", &ret, &int_value, &str_value) != NET_ERR_NONE)
164 int _connection_vconf_get_int(connection_handle_s *conn_handle, const char *key, int *value)
168 char *str_value = NULL;
171 ret = vconf_get_int(key, value);
173 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get vconfkey [%s] value", key);
178 if (conn_handle && net_get_vconf_value(conn_handle->network_info_handle,
179 key, "int", &ret, &int_value, &str_value) != NET_ERR_NONE)
190 int _connection_vconf_get_bool(connection_handle_s *conn_handle, const char *key, int *value)
194 char *str_value = NULL;
197 ret = vconf_get_bool(key, value);
199 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get vconfkey [%s] value", key);
204 if (conn_handle && net_get_vconf_value(conn_handle->network_info_handle,
205 key, "bool", &ret, &int_value, &str_value) != NET_ERR_NONE)
216 static void __libnet_state_changed_cb(char *profile_name, connection_profile_state_e state)
219 struct _profile_cb_s *cb_info;
221 if (profile_name == NULL) {
226 cb_info = g_hash_table_lookup(profile_cb_table, profile_name);
227 if (cb_info == NULL) {
232 if (cb_info->state == state) {
237 cb_info->state = state;
239 if (state < 0 || cb_info->callback == NULL) {
245 cb_info->callback(cb_info->state, cb_info->user_data);
248 static void __libnet_clear_profile_list(struct _profile_list_s *profile_list)
250 if (profile_list->count > 0)
251 g_free(profile_list->profiles);
253 profile_list->count = 0;
254 profile_list->next = 0;
255 profile_list->profiles = NULL;
258 static void __libnet_evt_cb(net_event_info_t *event_cb, void *user_data)
261 bool is_requested = false;
262 connection_error_e result = CONNECTION_ERROR_NONE;
263 connection_handle_s *conn_handle = (connection_handle_s *)user_data;
265 if (!_connection_check_handle_validity(conn_handle)) {
266 CONNECTION_LOG(CONNECTION_INFO, "Invalid handle");
271 switch (event_cb->event) {
272 case NET_EVENT_OPEN_RSP:
275 case NET_EVENT_OPEN_IND:
276 result = __libnet_convert_to_cp_error_type(event_cb->Error);
277 CONNECTION_LOG(CONNECTION_INFO, "Connection opened %s[%s]",
278 (is_requested) ? "RSP" : "IND",
279 __libnet_convert_cp_error_type_to_string(result));
282 if (conn_handle->opened_callback) {
284 _connection_handle_ref(conn_handle);
287 conn_handle->opened_callback(result,
288 conn_handle->opened_user_data);
291 conn_handle->opened_callback = NULL;
292 conn_handle->opened_user_data = NULL;
293 _connection_handle_unref(conn_handle);
297 switch (event_cb->Error) {
299 case NET_ERR_ACTIVE_CONNECTION_EXISTS:
300 CONNECTION_LOG(CONNECTION_INFO, "Successfully open connection");
302 _connection_handle_ref(conn_handle);
305 __libnet_state_changed_cb(event_cb->profile_name, CONNECTION_PROFILE_STATE_CONNECTED);
308 _connection_handle_unref(conn_handle);
313 CONNECTION_LOG(CONNECTION_ERROR, "Failed to open connection[%s]",
314 __libnet_convert_cp_error_type_to_string(result));
317 _connection_handle_ref(conn_handle);
320 __libnet_state_changed_cb(event_cb->profile_name, CONNECTION_PROFILE_STATE_DISCONNECTED);
323 _connection_handle_unref(conn_handle);
327 case NET_EVENT_CLOSE_RSP:
330 case NET_EVENT_CLOSE_IND:
331 result = __libnet_convert_to_cp_error_type(event_cb->Error);
332 CONNECTION_LOG(CONNECTION_INFO, "Connection closed %s[%s]",
333 (is_requested) ? "RSP" : "IND",
334 __libnet_convert_cp_error_type_to_string(result));
337 if (conn_handle->closed_callback) {
339 _connection_handle_ref(conn_handle);
342 conn_handle->closed_callback(result,
343 conn_handle->closed_user_data);
346 conn_handle->closed_callback = NULL;
347 conn_handle->closed_user_data = NULL;
348 _connection_handle_unref(conn_handle);
352 switch (event_cb->Error) {
354 CONNECTION_LOG(CONNECTION_INFO, "Successfully closed connection");
356 _connection_handle_ref(conn_handle);
359 __libnet_state_changed_cb(event_cb->profile_name, CONNECTION_PROFILE_STATE_DISCONNECTED);
362 _connection_handle_unref(conn_handle);
367 CONNECTION_LOG(CONNECTION_ERROR, "Failed to close connection[%s]",
368 __libnet_convert_cp_error_type_to_string(result));
372 case NET_EVENT_NET_STATE_IND:
373 CONNECTION_LOG(CONNECTION_INFO, "State changed IND");
375 if (event_cb->datalength != sizeof(net_state_type_t)) {
380 net_state_type_t *profile_state = (net_state_type_t *)event_cb->data;
381 connection_profile_state_e cp_state = _profile_convert_to_cp_state(*profile_state);
383 CONNECTION_LOG(CONNECTION_INFO, "state: %s", __libnet_convert_cp_state_to_string(cp_state));
384 SECURE_CONNECTION_LOG(CONNECTION_INFO, "profile name: %s", event_cb->profile_name);
386 _connection_handle_ref(conn_handle);
389 __libnet_state_changed_cb(event_cb->profile_name, cp_state);
392 _connection_handle_unref(conn_handle);
396 case NET_EVENT_CELLULAR_SET_DEFAULT_RSP:
397 result = __libnet_convert_to_cp_error_type(event_cb->Error);
398 CONNECTION_LOG(CONNECTION_INFO, "Got set default profile RSP %d", result);
399 if (conn_handle->set_default_callback) {
401 _connection_handle_ref(conn_handle);
404 conn_handle->set_default_callback(result,
405 conn_handle->set_default_user_data);
408 conn_handle->set_default_callback = NULL;
409 conn_handle->set_default_user_data = NULL;
410 _connection_handle_unref(conn_handle);
413 case NET_EVENT_CELLULAR_RESET_DEFAULT_RSP:
414 result = __libnet_convert_to_cp_error_type(event_cb->Error);
415 CONNECTION_LOG(CONNECTION_INFO, "Got reset default profile RSP %d", result);
416 if (conn_handle->reset_callback) {
418 _connection_handle_ref(conn_handle);
421 conn_handle->reset_callback(result,
422 conn_handle->reset_user_data);
425 conn_handle->reset_callback = NULL;
426 conn_handle->reset_user_data = NULL;
427 _connection_handle_unref(conn_handle);
430 case NET_EVENT_ETHERNET_CABLE_ATTACHED:
431 CONNECTION_LOG(CONNECTION_INFO, "Got Ethernet cable Attached Indication\n");
432 if (conn_handle->ethernet_cable_state_changed_callback) {
434 _connection_handle_ref(conn_handle);
437 conn_handle->ethernet_cable_state_changed_callback(CONNECTION_ETHERNET_CABLE_ATTACHED,
438 conn_handle->ethernet_cable_state_changed_user_data);
441 _connection_handle_unref(conn_handle);
447 case NET_EVENT_ETHERNET_CABLE_DETACHED:
448 CONNECTION_LOG(CONNECTION_INFO, "Got Ethernet cable detached Indication\n");
449 if (conn_handle->ethernet_cable_state_changed_callback) {
451 _connection_handle_ref(conn_handle);
454 conn_handle->ethernet_cable_state_changed_callback(CONNECTION_ETHERNET_CABLE_DETACHED,
455 conn_handle->ethernet_cable_state_changed_user_data);
458 _connection_handle_unref(conn_handle);
464 case NET_EVENT_NETWORK_TYPE_CHANGED:
465 CONNECTION_LOG(CONNECTION_INFO, "Got Network Type Changed Indication");
466 int *state = (int *) event_cb->data;
467 if (conn_handle->type_changed_callback) {
468 int type = CONNECTION_TYPE_DISCONNECTED;
471 case VCONFKEY_NETWORK_CELLULAR:
472 type = CONNECTION_TYPE_CELLULAR;
474 case VCONFKEY_NETWORK_WIFI:
475 type = CONNECTION_TYPE_WIFI;
477 case VCONFKEY_NETWORK_ETHERNET:
478 type = CONNECTION_TYPE_ETHERNET;
480 case VCONFKEY_NETWORK_BLUETOOTH:
481 type = CONNECTION_TYPE_BT;
483 case VCONFKEY_NETWORK_DEFAULT_PROXY:
484 type = CONNECTION_TYPE_NET_PROXY;
487 type = CONNECTION_TYPE_DISCONNECTED;
491 _connection_handle_ref(conn_handle);
494 conn_handle->type_changed_callback(type,
495 conn_handle->type_changed_user_data);
498 _connection_handle_unref(conn_handle);
504 case NET_EVENT_IPV4_ADDRESS_CHANGED:
505 CONNECTION_LOG(CONNECTION_INFO, "Got IPv4 Address Changed Indication");
506 if (conn_handle->ip_changed_callback) {
507 char *ipv4_addr = NULL;
508 char *ipv6_addr = NULL;
509 char *addr = (char *)event_cb->data;
511 ipv4_addr = g_strdup(addr);
512 ipv6_addr = _connection_vconf_get_str(conn_handle, VCONFKEY_NETWORK_IP6);
513 if (ipv6_addr == NULL)
514 CONNECTION_LOG(CONNECTION_ERROR, //LCOV_EXCL_LINE
515 "vconf_get_str(VCONFKEY_NETWORK_IP6) failed");
517 _connection_handle_ref(conn_handle);
520 conn_handle->ip_changed_callback(ipv4_addr, ipv6_addr,
521 conn_handle->ip_changed_user_data);
524 _connection_handle_unref(conn_handle);
532 case NET_EVENT_IPV6_ADDRESS_CHANGED:
533 CONNECTION_LOG(CONNECTION_INFO, "Got IPv6 Address Changed Indication");
534 if (conn_handle->ip_changed_callback) {
535 char *ipv4_addr = NULL;
536 char *ipv6_addr = NULL;
537 char *addr = (char *)event_cb->data;
539 ipv6_addr = g_strdup(addr);
540 ipv4_addr = _connection_vconf_get_str(conn_handle, VCONFKEY_NETWORK_IP);
541 if (ipv4_addr == NULL)
542 CONNECTION_LOG(CONNECTION_ERROR, //LCOV_EXCL_LINE
543 "vconf_get_str(VCONFKEY_NETWORK_IP) failed");
545 _connection_handle_ref(conn_handle);
548 conn_handle->ip_changed_callback(ipv4_addr, ipv6_addr,
549 conn_handle->ip_changed_user_data);
552 _connection_handle_unref(conn_handle);
560 case NET_EVENT_PROXY_ADDRESS_CHANGED:
561 CONNECTION_LOG(CONNECTION_INFO, "Got Proxy Changed Indication");
562 char *proxy_addr = (char *)event_cb->data;
564 if (conn_handle->proxy_changed_callback) {
566 _connection_handle_ref(conn_handle);
569 conn_handle->proxy_changed_callback(proxy_addr, NULL,
570 conn_handle->proxy_changed_user_data);
573 _connection_handle_unref(conn_handle);
579 case NET_EVENT_DHCP_STATE_CHANGED:
580 CONNECTION_LOG(CONNECTION_INFO, "Got DHCP Changed Indication");
581 net_dhcp_state_info_t *dhcp_state_info = (net_dhcp_state_info_t*)event_cb->data;
582 connection_dhcp_state_e dhcp_state = CONNECTION_DHCP_STATE_UNKNOWN;
583 connection_error_e result = CONNECTION_ERROR_NONE;
585 if (!dhcp_state_info || !dhcp_state_info->ifname || !dhcp_state_info->state) {
590 if (conn_handle->dhcp_state_changed_callback) {
591 if (g_strcmp0(dhcp_state_info->state, "DHCP_STARTED") == 0) {
592 result = CONNECTION_ERROR_NONE;
593 dhcp_state = CONNECTION_DHCP_STATE_STARTED;
594 } else if (g_strcmp0(dhcp_state_info->state, "DHCP_SUCCESS") == 0) {
595 result = CONNECTION_ERROR_NONE;
596 dhcp_state = CONNECTION_DHCP_STATE_FINISHED;
597 } else if (g_strcmp0(dhcp_state_info->state, "DHCP_FAIL") == 0) {
598 result = CONNECTION_ERROR_DHCP_FAILED;
599 dhcp_state = CONNECTION_DHCP_STATE_FINISHED;
601 result = CONNECTION_ERROR_NONE;
602 dhcp_state = CONNECTION_DHCP_STATE_UNKNOWN;
605 _connection_handle_ref(conn_handle);
608 conn_handle->dhcp_state_changed_callback(
610 dhcp_state_info->ifname,
612 conn_handle->dhcp_state_changed_user_data);
615 _connection_handle_unref(conn_handle);
621 case NET_EVENT_INTERNET_ONLINE_IND:
622 case NET_EVENT_INTERNET_OFFLINE_IND:
623 CONNECTION_LOG(CONNECTION_INFO, "Got Internet State Changed Indication: %s",
624 event_cb->event == NET_EVENT_INTERNET_ONLINE_IND ? "Online" : "Offline");
625 net_device_t *device_type = (net_device_t *) event_cb->data;
627 if (conn_handle->internet_state_changed_callback) {
628 net_profile_info_t active_profile;
631 rv = net_get_active_net_info(conn_handle->network_info_handle, &active_profile);
633 if (rv == NET_ERR_NO_SERVICE && event_cb->event == NET_EVENT_INTERNET_OFFLINE_IND) {
635 _connection_handle_ref(conn_handle);
638 conn_handle->internet_state_changed_callback(CONNECTION_INTERNET_STATE_OFFLINE,
639 conn_handle->internet_state_changed_user_data); //LCOV_EXCL_LINE
642 _connection_handle_unref(conn_handle);
646 } else if (rv == NET_ERR_ACCESS_DENIED) {
647 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
649 } else if (rv != NET_ERR_NONE) {
650 CONNECTION_LOG(CONNECTION_ERROR, "Unable to get Default profile handle"); //LCOV_EXCL_LINE
651 break; //LCOV_EXCL_LINE
654 if (event_cb->event == NET_EVENT_INTERNET_ONLINE_IND) {
655 if (active_profile.profile_state == NET_STATE_TYPE_ONLINE &&
656 active_profile.profile_type == *device_type) {
658 _connection_handle_ref(conn_handle);
661 conn_handle->internet_state_changed_callback(CONNECTION_INTERNET_STATE_ONLINE,
662 conn_handle->internet_state_changed_user_data);
665 _connection_handle_unref(conn_handle);
671 if (active_profile.profile_state != NET_STATE_TYPE_ONLINE) {
673 _connection_handle_ref(conn_handle);
676 conn_handle->internet_state_changed_callback(CONNECTION_INTERNET_STATE_OFFLINE,
677 conn_handle->internet_state_changed_user_data);
680 _connection_handle_unref(conn_handle);
697 static int __libnet_get_connected_count(struct _profile_list_s *profile_list)
702 for (; i < profile_list->count; i++) {
703 if (profile_list->profiles[i].profile_state == NET_STATE_TYPE_ONLINE ||
704 profile_list->profiles[i].profile_state == NET_STATE_TYPE_READY)
711 static void __libnet_copy_connected_profile(net_profile_info_t **dest, struct _profile_list_s *source)
715 for (; i < source->count; i++) {
716 if (source->profiles[i].profile_state == NET_STATE_TYPE_ONLINE ||
717 source->profiles[i].profile_state == NET_STATE_TYPE_READY) {
718 memcpy(*dest, &source->profiles[i], sizeof(net_profile_info_t));
725 static int __libnet_get_default_count(struct _profile_list_s *profile_list)
730 for (; i < profile_list->count; i++) {
731 if (profile_list->profiles[i].profile_type == NET_DEVICE_CELLULAR) {
732 if (profile_list->profiles[i].profile_info.pdp.default_conn == TRUE)
740 static void __libnet_copy_default_profile(net_profile_info_t **dest, struct _profile_list_s *source)
744 for (; i < source->count; i++) {
745 if (source->profiles[i].profile_type == NET_DEVICE_CELLULAR) {
746 if (source->profiles[i].profile_info.pdp.default_conn == TRUE) {
747 memcpy(*dest, &source->profiles[i], sizeof(net_profile_info_t));
754 static void __connection_dummy_profile_state_changed_cb(connection_profile_state_e state,
757 CONNECTION_LOG(CONNECTION_INFO, "Dummy callback");
761 static int __profile_cb_table_value_destroy(gpointer data)
763 struct _profile_cb_s *profile_cb_data = (struct _profile_cb_s *)data;
765 g_free(profile_cb_data);
767 return G_SOURCE_REMOVE;
770 int _connection_libnet_init(connection_handle_s *conn_handle)
774 rv = net_register_client(&(conn_handle->network_info_handle),
775 (net_event_cb_t)__libnet_evt_cb, conn_handle);
776 if (rv != NET_ERR_NONE)
779 if (profile_cb_table == NULL)
780 profile_cb_table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
782 if (!in_container && access(CONTAINER_FILE, F_OK) == 0)
783 in_container = TRUE; //LCOV_EXCL_LINE
788 bool _connection_libnet_deinit(connection_handle_s *conn_handle, bool is_empty)
790 net_deregister_client(conn_handle->network_info_handle);
793 if (profile_cb_table) {
794 g_hash_table_destroy(profile_cb_table);
795 profile_cb_table = NULL;
798 __libnet_clear_profile_list(&profile_iterator);
800 if (prof_handle_list) {
801 g_slist_free_full(prof_handle_list, g_free);
802 prof_handle_list = NULL;
810 void _connection_set_cs_tid(int tid, connection_handle_s *conn_handle)
812 net_set_cs_tid(tid, conn_handle->network_info_handle);
815 void _connection_unset_cs_tid(int tid, connection_handle_s *conn_handle)
817 net_unset_cs_tid(tid, conn_handle->network_info_handle);
821 bool _connection_libnet_check_profile_validity(connection_profile_h profile)
829 for (list = prof_handle_list; list; list = list->next)
830 if (profile == list->data) return true;
832 for (; i < profile_iterator.count; i++)
833 if (profile == &profile_iterator.profiles[i]) return true;
838 int _connection_libnet_get_metered_state(connection_handle_s *conn_handle, bool* is_metered)
843 rv = net_get_metered_state(conn_handle->network_info_handle, &status);
844 if (rv == NET_ERR_ACCESS_DENIED) {
845 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
846 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
847 } else if (rv != NET_ERR_NONE) {
848 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get metered state[%d]", rv); //LCOV_EXCL_LINE
849 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
853 *is_metered = true; //LCOV_EXCL_LINE
856 return CONNECTION_ERROR_NONE;
859 int _connection_libnet_get_wifi_state(connection_handle_s *conn_handle, connection_wifi_state_e *state)
862 net_wifi_state_t wlan_state;
864 rv = net_get_wifi_state(conn_handle->network_info_handle, &wlan_state);
865 if (rv == NET_ERR_ACCESS_DENIED) {
866 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
867 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
868 } else if (rv != NET_ERR_NONE) {
869 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get Wi-Fi state[%d]", rv); //LCOV_EXCL_LINE
870 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
873 switch (wlan_state) {
875 *state = CONNECTION_WIFI_STATE_DEACTIVATED;
878 case WIFI_ASSOCIATION:
879 case WIFI_CONFIGURATION:
880 *state = CONNECTION_WIFI_STATE_DISCONNECTED;
883 case WIFI_DISCONNECTING:
884 *state = CONNECTION_WIFI_STATE_CONNECTED;
887 CONNECTION_LOG(CONNECTION_ERROR, "Unknown Wi-Fi state"); //LCOV_EXCL_LINE
888 return CONNECTION_ERROR_INVALID_OPERATION; //LCOV_EXCL_LINE
891 return CONNECTION_ERROR_NONE;
895 int _connection_libnet_get_ethernet_state(connection_handle_s *conn_handle,
896 connection_ethernet_state_e *state)
899 struct _profile_list_s ethernet_profiles = {0, 0, NULL};
900 rv = net_get_profile_list(conn_handle->network_info_handle,
901 NET_DEVICE_ETHERNET, ðernet_profiles.profiles,
902 ðernet_profiles.count);
903 if (rv == NET_ERR_ACCESS_DENIED) {
904 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
905 return CONNECTION_ERROR_PERMISSION_DENIED;
908 if (ethernet_profiles.count == 0) {
909 *state = CONNECTION_ETHERNET_STATE_DEACTIVATED;
910 return CONNECTION_ERROR_NONE;
913 switch (ethernet_profiles.profiles->profile_state) {
914 case NET_STATE_TYPE_ONLINE:
915 case NET_STATE_TYPE_READY:
916 *state = CONNECTION_ETHERNET_STATE_CONNECTED;
918 case NET_STATE_TYPE_IDLE:
919 case NET_STATE_TYPE_FAILURE:
920 case NET_STATE_TYPE_ASSOCIATION:
921 case NET_STATE_TYPE_CONFIGURATION:
922 case NET_STATE_TYPE_DISCONNECT:
923 *state = CONNECTION_ETHERNET_STATE_DISCONNECTED;
926 __libnet_clear_profile_list(ðernet_profiles);
927 return CONNECTION_ERROR_OPERATION_FAILED;
930 __libnet_clear_profile_list(ðernet_profiles);
932 return CONNECTION_ERROR_NONE;
935 int _connection_libnet_get_ethernet_cable_state(connection_handle_s *conn_handle,
936 connection_ethernet_cable_state_e* state)
941 rv = net_get_ethernet_cable_state(conn_handle->network_info_handle, &status);
942 if (rv == NET_ERR_ACCESS_DENIED) {
943 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
944 return CONNECTION_ERROR_PERMISSION_DENIED;
945 } else if (rv != NET_ERR_NONE) {
946 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get ethernet cable state[%d]", rv);
947 return CONNECTION_ERROR_OPERATION_FAILED;
951 *state = CONNECTION_ETHERNET_CABLE_ATTACHED;
953 *state = CONNECTION_ETHERNET_CABLE_DETACHED;
954 return CONNECTION_ERROR_NONE;
957 int _connection_libnet_get_dhcp_state(connection_handle_s *conn_handle,
958 const char *interface_name, connection_dhcp_state_e *state)
961 char *status_str = NULL;
963 rv = net_get_dhcp_state(conn_handle->network_info_handle, interface_name, &status_str);
964 if (rv == NET_ERR_ACCESS_DENIED) {
965 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
966 return CONNECTION_ERROR_PERMISSION_DENIED;
967 } else if (rv != NET_ERR_NONE) {
968 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get DHCP state[%d]", rv);
969 return CONNECTION_ERROR_OPERATION_FAILED;
972 if (g_strcmp0(status_str, "DHCP_STARTED") == 0)
973 *state = CONNECTION_DHCP_STATE_STARTED;
974 else if (g_strcmp0(status_str, "DHCP_SUCCESS") == 0)
975 *state = CONNECTION_DHCP_STATE_FINISHED;
976 else if (g_strcmp0(status_str, "DHCP_FAIL") == 0)
977 *state = CONNECTION_DHCP_STATE_FINISHED;
979 *state = CONNECTION_DHCP_STATE_UNKNOWN;
983 return CONNECTION_ERROR_NONE;
987 int _connection_libnet_get_bluetooth_state(connection_handle_s *conn_handle, connection_bt_state_e *state)
991 struct _profile_list_s bluetooth_profiles = {0, 0, NULL};
992 rv = net_get_profile_list(conn_handle->network_info_handle,
993 NET_DEVICE_BLUETOOTH, &bluetooth_profiles.profiles,
994 &bluetooth_profiles.count);
995 if (rv == NET_ERR_ACCESS_DENIED) {
996 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
997 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1000 if (bluetooth_profiles.count == 0) {
1001 *state = CONNECTION_BT_STATE_DEACTIVATED; //LCOV_EXCL_LINE
1002 return CONNECTION_ERROR_NONE; //LCOV_EXCL_LINE
1006 for (; i < bluetooth_profiles.count; i++) {
1007 switch (bluetooth_profiles.profiles[i].profile_state) {
1008 case NET_STATE_TYPE_ONLINE:
1009 case NET_STATE_TYPE_READY:
1010 *state = CONNECTION_BT_STATE_CONNECTED;
1012 case NET_STATE_TYPE_IDLE:
1013 case NET_STATE_TYPE_FAILURE:
1014 case NET_STATE_TYPE_ASSOCIATION:
1015 case NET_STATE_TYPE_CONFIGURATION:
1016 case NET_STATE_TYPE_DISCONNECT:
1017 *state = CONNECTION_BT_STATE_DISCONNECTED;
1020 __libnet_clear_profile_list(&bluetooth_profiles);
1021 return CONNECTION_ERROR_OPERATION_FAILED;
1027 __libnet_clear_profile_list(&bluetooth_profiles);
1029 return CONNECTION_ERROR_NONE;
1032 int _connection_libnet_get_profile_iterator(connection_handle_s *conn_handle,
1033 connection_iterator_type_e type, connection_profile_iterator_h* profile_iter_h)
1037 net_profile_info_t *profiles = NULL;
1039 struct _profile_list_s profile_list = {0, 0, NULL};
1041 __libnet_clear_profile_list(&profile_iterator);
1043 rv = net_get_all_profile_list(conn_handle->network_info_handle,
1044 &profile_list.profiles, &profile_list.count);
1045 if (rv == NET_ERR_ACCESS_DENIED) {
1046 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1047 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1048 } else if (rv != NET_ERR_NO_SERVICE && rv != NET_ERR_NONE)
1049 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1051 *profile_iter_h = &profile_iterator;
1054 case CONNECTION_ITERATOR_TYPE_REGISTERED:
1055 count = profile_list.count;
1056 CONNECTION_LOG(CONNECTION_INFO, "Total profile count : %d", count);
1058 return CONNECTION_ERROR_NONE;
1060 profiles = g_try_new0(net_profile_info_t, count);
1061 if (profiles == NULL) {
1062 __libnet_clear_profile_list(&profile_list); //LCOV_EXCL_LINE
1063 return CONNECTION_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1066 profile_iterator.profiles = profiles;
1068 memcpy(profiles, profile_list.profiles, sizeof(net_profile_info_t) * count);
1071 case CONNECTION_ITERATOR_TYPE_CONNECTED:
1072 count = __libnet_get_connected_count(&profile_list);
1073 CONNECTION_LOG(CONNECTION_INFO, "Total connected profile count : %d", count);
1075 return CONNECTION_ERROR_NONE;
1077 profiles = g_try_new0(net_profile_info_t, count);
1078 if (profiles == NULL) {
1079 __libnet_clear_profile_list(&profile_list); //LCOV_EXCL_LINE
1080 return CONNECTION_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1083 profile_iterator.profiles = profiles;
1085 __libnet_copy_connected_profile(&profiles, &profile_list);
1088 case CONNECTION_ITERATOR_TYPE_DEFAULT:
1089 count = __libnet_get_default_count(&profile_list);
1090 CONNECTION_LOG(CONNECTION_INFO, "Total default profile count : %d", count); //LCOV_EXCL_LINE
1092 return CONNECTION_ERROR_NONE;
1094 profiles = g_try_new0(net_profile_info_t, count);
1095 if (profiles == NULL) {
1096 __libnet_clear_profile_list(&profile_list);
1097 return CONNECTION_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1100 profile_iterator.profiles = profiles;
1102 __libnet_copy_default_profile(&profiles, &profile_list);
1107 __libnet_clear_profile_list(&profile_list);
1109 profile_iterator.count = count;
1111 return CONNECTION_ERROR_NONE;
1114 int _connection_libnet_get_iterator_next(connection_profile_iterator_h profile_iter_h, connection_profile_h *profile)
1116 if (profile_iter_h != &profile_iterator)
1117 return CONNECTION_ERROR_INVALID_PARAMETER;
1119 if (profile_iterator.count <= profile_iterator.next)
1120 return CONNECTION_ERROR_ITERATOR_END;
1122 *profile = &profile_iterator.profiles[profile_iterator.next];
1123 profile_iterator.next++;
1125 return CONNECTION_ERROR_NONE;
1128 bool _connection_libnet_iterator_has_next(connection_profile_iterator_h profile_iter_h)
1130 if (profile_iter_h != &profile_iterator)
1133 if (profile_iterator.count <= profile_iterator.next)
1139 int _connection_libnet_destroy_iterator(connection_profile_iterator_h profile_iter_h)
1141 if (profile_iter_h != &profile_iterator)
1142 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1144 __libnet_clear_profile_list(&profile_iterator);
1146 return CONNECTION_ERROR_NONE;
1149 int _connection_libnet_get_current_profile(connection_handle_s *conn_handle,
1150 connection_profile_h *profile)
1152 net_profile_info_t active_profile;
1155 rv = net_get_active_net_info(conn_handle->network_info_handle, &active_profile);
1156 if (rv == NET_ERR_NO_SERVICE)
1157 return CONNECTION_ERROR_NO_CONNECTION; //LCOV_EXCL_LINE
1158 else if (rv == NET_ERR_ACCESS_DENIED) {
1159 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1160 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1161 } else if (rv != NET_ERR_NONE)
1162 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1164 *profile = g_try_malloc0(sizeof(net_profile_info_t));
1165 if (*profile == NULL)
1166 return CONNECTION_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1168 memcpy(*profile, &active_profile, sizeof(net_profile_info_t));
1169 prof_handle_list = g_slist_append(prof_handle_list, *profile);
1171 return CONNECTION_ERROR_NONE;
1174 int _connection_libnet_reset_profile(connection_handle_s *conn_handle,
1175 connection_reset_option_e type, connection_cellular_subscriber_id_e id)
1179 rv = net_reset_profile(conn_handle->network_info_handle, type, id);
1180 if (rv == NET_ERR_ACCESS_DENIED)
1181 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1182 else if (rv != NET_ERR_NONE)
1183 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1185 return CONNECTION_ERROR_NONE;
1188 int _connection_libnet_open_profile(connection_handle_s *conn_handle,
1189 connection_profile_h profile)
1193 if (!(_connection_libnet_check_profile_validity(profile))) {
1194 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter"); //LCOV_EXCL_LINE
1195 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1198 net_profile_info_t *profile_info = profile;
1200 if (profile_info->profile_type == NET_DEVICE_MESH)
1201 rv = net_open_mesh_connection_with_profile(conn_handle->network_info_handle, //LCOV_EXCL_LINE
1202 profile_info->profile_name);
1204 rv = net_open_connection_with_profile(conn_handle->network_info_handle,
1205 profile_info->profile_name);
1207 if (rv == NET_ERR_ACCESS_DENIED)
1208 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1209 else if (rv != NET_ERR_NONE)
1210 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1212 return CONNECTION_ERROR_NONE;
1215 int _connection_libnet_get_cellular_service_profile(connection_handle_s *conn_handle,
1216 connection_cellular_service_type_e type, connection_profile_h *profile)
1219 int rv = NET_ERR_NONE;
1220 #if defined TIZEN_DUALSIM_ENABLE
1221 int default_subscriber_id = 0;
1222 char subscriber_id[3];
1225 struct _profile_list_s cellular_profiles = { 0, 0, NULL };
1226 net_service_type_t service_type = _connection_profile_convert_to_libnet_cellular_service_type(type);
1228 rv = net_get_profile_list(conn_handle->network_info_handle,
1229 NET_DEVICE_CELLULAR, &cellular_profiles.profiles,
1230 &cellular_profiles.count);
1231 if (rv == NET_ERR_ACCESS_DENIED) {
1232 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1233 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1234 } else if (rv != NET_ERR_NONE) {
1235 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get profile list (%d)", rv); //LCOV_EXCL_LINE
1236 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1239 #if defined TIZEN_DUALSIM_ENABLE
1240 if (_connection_vconf_get_int(conn_handle, VCONF_TELEPHONY_DEFAULT_DATA_SERVICE,
1241 &default_subscriber_id) != 0) {
1242 CONNECTION_LOG(CONNECTION_ERROR,
1243 "Failed to get VCONF_TELEPHONY_DEFAULT_DATA_SERVICE");
1244 __libnet_clear_profile_list(&cellular_profiles); //LCOV_EXCL_LINE
1245 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1248 g_snprintf(subscriber_id, sizeof(subscriber_id), "%d", default_subscriber_id);
1251 for (i = 0; i < cellular_profiles.count; i++)
1252 if (cellular_profiles.profiles[i].profile_info.pdp.service_type == service_type)
1253 #if defined TIZEN_DUALSIM_ENABLE
1254 if (g_str_has_suffix(
1255 cellular_profiles.profiles[i].profile_info.pdp.ps_modem_path,
1256 subscriber_id) == TRUE)
1260 if (i >= cellular_profiles.count) {
1261 __libnet_clear_profile_list(&cellular_profiles); //LCOV_EXCL_LINE
1262 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1265 *profile = g_try_malloc0(sizeof(net_profile_info_t));
1266 if (*profile == NULL) {
1267 __libnet_clear_profile_list(&cellular_profiles); //LCOV_EXCL_LINE
1268 return CONNECTION_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1271 memcpy(*profile, &cellular_profiles.profiles[i], sizeof(net_profile_info_t));
1273 if (cellular_profiles.profiles[i].profile_info.pdp.default_conn)
1277 if (type != CONNECTION_CELLULAR_SERVICE_TYPE_INTERNET &&
1278 type != CONNECTION_CELLULAR_SERVICE_TYPE_PREPAID_INTERNET)
1281 for (j = 0; j < cellular_profiles.count; j++) {
1285 if (cellular_profiles.profiles[j].profile_info.pdp.service_type != service_type)
1288 if (cellular_profiles.profiles[j].profile_info.pdp.default_conn) {
1289 memcpy(*profile, &cellular_profiles.profiles[j], sizeof(net_profile_info_t));
1296 __libnet_clear_profile_list(&cellular_profiles);
1297 prof_handle_list = g_slist_append(prof_handle_list, *profile);
1299 return CONNECTION_ERROR_NONE;
1302 int _connection_libnet_set_cellular_service_profile_sync(connection_handle_s *conn_handle,
1303 connection_cellular_service_type_e type, connection_profile_h profile)
1307 if (!(_connection_libnet_check_profile_validity(profile))) {
1308 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter"); //LCOV_EXCL_LINE
1309 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1312 net_profile_info_t *profile_info = profile;
1313 connection_cellular_service_type_e service_type;
1315 service_type = _profile_convert_to_connection_cellular_service_type(profile_info->profile_info.pdp.service_type);
1317 if (service_type != type)
1318 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1320 rv = net_set_default_cellular_service_profile(conn_handle->network_info_handle,
1321 profile_info->profile_name);
1322 if (rv == NET_ERR_ACCESS_DENIED) {
1323 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1324 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1325 } else if (rv != NET_ERR_NONE)
1326 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1328 return CONNECTION_ERROR_NONE;
1331 int _connection_libnet_set_cellular_service_profile_async(connection_handle_s *conn_handle,
1332 connection_cellular_service_type_e type, connection_profile_h profile)
1336 if (!(_connection_libnet_check_profile_validity(profile))) {
1337 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter"); //LCOV_EXCL_LINE
1338 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1341 net_profile_info_t *profile_info = profile;
1342 connection_cellular_service_type_e service_type;
1344 service_type = _profile_convert_to_connection_cellular_service_type(profile_info->profile_info.pdp.service_type);
1346 if (service_type != type)
1347 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1349 rv = net_set_default_cellular_service_profile_async(conn_handle->network_info_handle,
1350 profile_info->profile_name);
1351 if (rv == NET_ERR_ACCESS_DENIED)
1352 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1353 else if (rv != NET_ERR_NONE)
1354 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1356 return CONNECTION_ERROR_NONE;
1359 int _connection_libnet_close_profile(connection_handle_s *conn_handle, connection_profile_h profile)
1363 if (!(_connection_libnet_check_profile_validity(profile))) {
1364 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter"); //LCOV_EXCL_LINE
1365 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1368 net_profile_info_t *profile_info = profile;
1370 if (profile_info->profile_type == NET_DEVICE_MESH)
1371 rv = net_close_mesh_connection(conn_handle->network_info_handle, profile_info->profile_name); //LCOV_EXCL_LINE
1373 rv = net_close_connection(conn_handle->network_info_handle, profile_info->profile_name);
1375 if (rv == NET_ERR_ACCESS_DENIED)
1376 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1377 else if (rv != NET_ERR_NONE)
1378 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1380 return CONNECTION_ERROR_NONE;
1383 int _connection_libnet_add_route(connection_handle_s *conn_handle,
1384 const char *interface_name, const char *host_address)
1387 char *endstr = NULL;
1388 int address_family = 0;
1390 address_family = AF_INET;
1392 endstr = strrchr(host_address, '.');
1393 if (endstr == NULL ||
1394 strcmp(endstr, ".0") == 0 ||
1395 strncmp(host_address, "0.", 2) == 0 ||
1396 strstr(host_address, "255") != NULL) {
1397 CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed\n"); //LCOV_EXCL_LINE
1398 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1401 rv = net_add_route(conn_handle->network_info_handle,
1402 host_address, interface_name, address_family);
1403 if (rv == NET_ERR_ACCESS_DENIED) {
1404 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1405 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1406 } else if (rv != NET_ERR_NONE)
1407 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1409 return CONNECTION_ERROR_NONE;
1412 int _connection_libnet_remove_route(connection_handle_s *conn_handle,
1413 const char *interface_name, const char *host_address)
1416 char *endstr = strrchr(host_address, '.');
1417 int address_family = 0;
1419 address_family = AF_INET;
1421 endstr = strrchr(host_address, '.');
1422 if (endstr == NULL ||
1423 strcmp(endstr, ".0") == 0 ||
1424 strncmp(host_address, "0.", 2) == 0 ||
1425 strstr(host_address, ".0.") != NULL || strstr(host_address, "255") != NULL) {
1426 CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed"); //LCOV_EXCL_LINE
1427 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1430 rv = net_remove_route(conn_handle->network_info_handle,
1431 host_address, interface_name, address_family);
1432 if (rv == NET_ERR_ACCESS_DENIED) {
1433 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1434 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1435 } else if (rv != NET_ERR_NONE)
1436 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1438 return CONNECTION_ERROR_NONE;
1441 int _connection_libnet_add_route_ipv6(connection_handle_s *conn_handle,
1442 const char *interface_name, const char *host_address, const char *gateway)
1445 int address_family = 0;
1447 address_family = AF_INET6;
1449 if (strncmp(host_address, "fe80:", 5) == 0 ||
1450 strncmp(host_address, "ff00:", 5) == 0 ||
1451 strncmp(host_address, "::", 2) == 0) {
1452 CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed\n"); //LCOV_EXCL_LINE
1453 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1456 rv = net_add_route_ipv6(conn_handle->network_info_handle,
1457 host_address, interface_name, address_family, gateway);
1458 if (rv == NET_ERR_ACCESS_DENIED) {
1459 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1460 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1461 } else if (rv != NET_ERR_NONE)
1462 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1464 return CONNECTION_ERROR_NONE;
1467 int _connection_libnet_remove_route_ipv6(connection_handle_s *conn_handle,
1468 const char *interface_name, const char *host_address, const char *gateway)
1471 int address_family = 0;
1473 address_family = AF_INET6;
1475 if (strncmp(host_address, "fe80:", 5) == 0 ||
1476 strncmp(host_address, "ff00:", 5) == 0 ||
1477 strncmp(host_address, "::", 2) == 0) {
1478 CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed\n"); //LCOV_EXCL_LINE
1479 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1482 rv = net_remove_route_ipv6(conn_handle->network_info_handle,
1483 host_address, interface_name, address_family, gateway);
1484 if (rv == NET_ERR_ACCESS_DENIED) {
1485 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1486 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1487 } else if (rv != NET_ERR_NONE)
1488 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1490 return CONNECTION_ERROR_NONE;
1493 int _connection_libnet_add_route_entry(connection_handle_s *conn_handle,
1494 connection_address_family_e address_family, const char *interface_name,
1495 const char *host_address, const char *gateway)
1498 char *endstr = NULL;
1499 int address_family_type = 0;
1501 if (address_family == CONNECTION_ADDRESS_FAMILY_IPV4)
1502 address_family_type = AF_INET;
1504 address_family_type = AF_INET6;
1506 if (address_family == CONNECTION_ADDRESS_FAMILY_IPV4) {
1508 endstr = strrchr(host_address, '.');
1509 if (endstr == NULL ||
1510 strcmp(endstr, ".0") == 0 ||
1511 strncmp(host_address, "0.", 2) == 0 ||
1512 strstr(host_address, "255") != NULL) {
1513 CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed\n"); //LCOV_EXCL_LINE
1514 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1517 rv = net_add_route_entry(conn_handle->network_info_handle,
1518 host_address, interface_name, address_family_type, gateway);
1519 if (rv == NET_ERR_ACCESS_DENIED) {
1520 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1521 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1522 } else if (rv != NET_ERR_NONE)
1523 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1527 if (strncmp(host_address, "fe80:", 5) == 0 ||
1528 strncmp(host_address, "ff00:", 5) == 0 ||
1529 strncmp(host_address, "::", 2) == 0) {
1530 CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed\n"); //LCOV_EXCL_LINE
1531 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1534 rv = net_add_route_ipv6(conn_handle->network_info_handle,
1535 host_address, interface_name, address_family_type, gateway);
1536 if (rv == NET_ERR_ACCESS_DENIED) {
1537 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1538 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1539 } else if (rv != NET_ERR_NONE)
1540 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1543 return CONNECTION_ERROR_NONE;
1546 int _connection_libnet_remove_route_entry(connection_handle_s *conn_handle,
1547 connection_address_family_e address_family, const char *interface_name,
1548 const char *host_address, const char *gateway)
1551 char *endstr = strrchr(host_address, '.');
1552 int address_family_type = 0;
1554 if (address_family == CONNECTION_ADDRESS_FAMILY_IPV4)
1555 address_family_type = AF_INET;
1557 address_family_type = AF_INET6;
1559 if (address_family == CONNECTION_ADDRESS_FAMILY_IPV4) {
1560 endstr = strrchr(host_address, '.');
1561 if (endstr == NULL ||
1562 strcmp(endstr, ".0") == 0 ||
1563 strncmp(host_address, "0.", 2) == 0 ||
1564 strstr(host_address, ".0.") != NULL || strstr(host_address, "255") != NULL) {
1565 CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed"); //LCOV_EXCL_LINE
1566 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1569 rv = net_remove_route_entry(conn_handle->network_info_handle, host_address,
1570 interface_name, address_family_type, gateway);
1571 if (rv == NET_ERR_ACCESS_DENIED) {
1572 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1573 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1574 } else if (rv != NET_ERR_NONE)
1575 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1579 if (strncmp(host_address, "fe80:", 5) == 0 ||
1580 strncmp(host_address, "ff00:", 5) == 0 ||
1581 strncmp(host_address, "::", 2) == 0) {
1582 CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed\n"); //LCOV_EXCL_LINE
1583 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1586 rv = net_remove_route_ipv6(conn_handle->network_info_handle, host_address,
1587 interface_name, address_family_type, gateway);
1588 if (rv == NET_ERR_ACCESS_DENIED) {
1589 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1590 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1591 } else if (rv != NET_ERR_NONE)
1592 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1595 return CONNECTION_ERROR_NONE;
1598 void _connection_libnet_add_to_profile_list(connection_profile_h profile)
1600 prof_handle_list = g_slist_append(prof_handle_list, profile);
1603 void _connection_libnet_remove_from_profile_list(connection_profile_h profile)
1605 prof_handle_list = g_slist_remove(prof_handle_list, profile);
1609 bool _connection_libnet_add_to_profile_cb_list(connection_profile_h profile,
1610 connection_profile_state_changed_cb callback, void *user_data)
1612 net_profile_info_t *profile_info = profile;
1613 char *profile_name = g_strdup(profile_info->profile_name);
1615 struct _profile_cb_s *profile_cb_info = g_try_malloc0(sizeof(struct _profile_cb_s));
1616 if (profile_cb_info == NULL) {
1617 g_free(profile_name); //LCOV_EXCL_LINE
1618 return false; //LCOV_EXCL_LINE
1621 profile_cb_info->callback = callback;
1622 profile_cb_info->user_data = user_data;
1623 profile_cb_info->state = _profile_convert_to_cp_state(profile_info->profile_state);
1625 g_hash_table_replace(profile_cb_table, profile_name, profile_cb_info);
1630 bool _connection_libnet_remove_from_profile_cb_list(connection_profile_h profile)
1632 net_profile_info_t *profile_info = profile;
1633 struct _profile_cb_s *profile_cb_info;
1635 profile_cb_info = g_hash_table_lookup(profile_cb_table, profile_info->profile_name);
1636 if (profile_cb_info == NULL)
1639 g_hash_table_remove(profile_cb_table, profile_info->profile_name);
1643 * Marking callback to dummy callback.
1646 * To avoid race condition between calling profile_state_changed_cb()
1647 * and _connection_libnet_remove_from_profile_cb_list()
1648 * in multi-threaded environment.
1650 profile_cb_info->callback = __connection_dummy_profile_state_changed_cb;
1651 profile_cb_info->user_data = NULL;
1652 profile_cb_info->state = -1;
1654 /* Timer to free hash table entry */
1655 g_timeout_add(200, __profile_cb_table_value_destroy, profile_cb_info);
1660 int _connection_libnet_set_statistics(connection_handle_s *conn_handle,
1661 net_device_t device_type, net_statistics_type_e statistics_type)
1664 rv = net_set_statistics(conn_handle->network_info_handle,
1665 device_type, statistics_type);
1666 if (rv == NET_ERR_ACCESS_DENIED) {
1667 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1668 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1669 } else if (rv != NET_ERR_NONE)
1670 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1672 return CONNECTION_ERROR_NONE;
1675 int _connection_libnet_get_statistics(connection_handle_s *conn_handle,
1676 net_statistics_type_e statistics_type, unsigned long long *size)
1679 rv = net_get_statistics(conn_handle->network_info_handle,
1680 NET_DEVICE_WIFI, statistics_type, size);
1681 if (rv == NET_ERR_ACCESS_DENIED) {
1682 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1683 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1684 } else if (rv != NET_ERR_NONE)
1685 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1687 return CONNECTION_ERROR_NONE;
1691 int _connection_libnet_set_cellular_subscriber_id(connection_profile_h profile,
1692 connection_cellular_subscriber_id_e sim_id)
1694 char *modem_path = NULL;
1695 net_profile_info_t *profile_info = (net_profile_info_t *)profile;
1697 if (net_get_cellular_modem_object_path(&modem_path, sim_id) != NET_ERR_NONE) {
1698 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get subscriber[%d]", sim_id); //LCOV_EXCL_LINE
1699 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1703 CONNECTION_LOG(CONNECTION_ERROR, "NULL modem object path"); //LCOV_EXCL_LINE
1704 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1707 g_strlcpy(profile_info->profile_info.pdp.ps_modem_path, modem_path,
1708 NET_PROFILE_NAME_LEN_MAX);
1711 return CONNECTION_ERROR_NONE;
1715 int _connection_libnet_enable_ethernet_eap(bool enable, const char *profilename)
1719 rv = net_ethernet_eap_enable(enable, profilename);
1720 if (rv == NET_ERR_ACCESS_DENIED) {
1721 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1722 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1723 } else if (rv != NET_ERR_NONE) {
1724 CONNECTION_LOG(CONNECTION_ERROR, "Failed to enable EAP over ethernet[%d]", rv); //LCOV_EXCL_LINE
1725 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1728 return CONNECTION_ERROR_NONE;
1731 int _connection_libnet_ethernet_eap_enabled(const char *profilename, bool *enabled)
1734 gboolean eap_enabled = false;
1736 rv = net_ethernet_eap_enabled(profilename, &eap_enabled);
1737 if (rv == NET_ERR_ACCESS_DENIED) {
1738 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1739 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1740 } else if (rv != NET_ERR_NONE) {
1741 CONNECTION_LOG(CONNECTION_ERROR, "Failed to enable EAP over ethernet[%d]", rv); //LCOV_EXCL_LINE
1742 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1745 *enabled = eap_enabled;
1746 return CONNECTION_ERROR_NONE;
1749 int _connection_libnet_profile_save_ethernet_eap_config(connection_handle_s *conn_handle,
1750 connection_profile_h profile)
1754 net_profile_info_t *profile_info = profile;
1756 rv = net_save_ethernet_eap_config(conn_handle->network_info_handle,
1757 &profile_info->profile_info.ethernet.net_info);
1759 if (rv == NET_ERR_ACCESS_DENIED) {
1760 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1761 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1762 } else if (rv != NET_ERR_NONE) {
1763 CONNECTION_LOG(CONNECTION_ERROR, "Failed to save eap config[%d]", rv); //LCOV_EXCL_LINE
1764 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1767 return CONNECTION_ERROR_NONE;
1770 int _connection_libnet_check_get_privilege(void)
1774 rv = net_check_get_privilege();
1775 if (rv == NET_ERR_ACCESS_DENIED) {
1776 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1777 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1778 } else if (rv != NET_ERR_NONE)
1779 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1781 return CONNECTION_ERROR_NONE;
1784 int _connection_libnet_check_profile_privilege(void)
1788 rv = net_check_profile_privilege();
1789 if (rv == NET_ERR_ACCESS_DENIED) {
1790 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1791 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1792 } else if (rv != NET_ERR_NONE)
1793 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1795 return CONNECTION_ERROR_NONE;
1798 bool __libnet_check_feature_supported(const char *key, connection_supported_feature_e feature)
1800 if (!connection_is_feature_checked[feature]) {
1801 if (system_info_get_platform_bool(key, &connection_feature_supported[feature]) < 0) {
1802 CONNECTION_LOG(CONNECTION_ERROR, "Error - Feature getting from System Info"); //LCOV_EXCL_LINE
1803 set_last_result(CONNECTION_ERROR_OPERATION_FAILED); //LCOV_EXCL_LINE
1804 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1806 connection_is_feature_checked[feature] = true;
1808 return connection_feature_supported[feature];
1811 int _connection_check_feature_supported(const char *feature_name, ...)
1816 bool feature_supported = false;
1818 va_start(list, feature_name);
1821 if (strcmp(key, TELEPHONY_FEATURE) == 0)
1822 value = __libnet_check_feature_supported(key, CONNECTION_SUPPORTED_FEATURE_TELEPHONY);
1823 if (strcmp(key, WIFI_FEATURE) == 0)
1824 value = __libnet_check_feature_supported(key, CONNECTION_SUPPORTED_FEATURE_WIFI);
1825 if (strcmp(key, TETHERING_BLUETOOTH_FEATURE) == 0)
1826 value = __libnet_check_feature_supported(key, CONNECTION_SUPPORTED_FEATURE_TETHERING_BLUETOOTH);
1827 if (strcmp(key, ETHERNET_FEATURE) == 0)
1828 value = __libnet_check_feature_supported(key, CONNECTION_SUPPORTED_FEATURE_ETHERNET);
1829 if (strcmp(key, ROUTE_FEATURE) == 0)
1830 value = __libnet_check_feature_supported(key, CONNECTION_SUPPORTED_FEATURE_ROUTE);
1832 feature_supported |= value;
1833 key = va_arg(list, const char *);
1836 if (!feature_supported) {
1837 CONNECTION_LOG(CONNECTION_ERROR, "Error - Feature is not supported");
1838 set_last_result(CONNECTION_ERROR_NOT_SUPPORTED);
1840 return CONNECTION_ERROR_NOT_SUPPORTED;
1844 set_last_result(CONNECTION_ERROR_NONE);
1845 return CONNECTION_ERROR_NONE;
1849 int _connection_libnet_start_tcpdump(connection_handle_s *conn_handle)
1851 connection_error_e result = CONNECTION_ERROR_NONE;
1852 net_err_t ret = NET_ERR_NONE;
1854 ret = net_start_tcpdump(conn_handle->network_info_handle);
1855 result = __libnet_convert_to_cp_error_type(ret);
1860 int _connection_libnet_stop_tcpdump(connection_handle_s *conn_handle)
1862 connection_error_e result = CONNECTION_ERROR_NONE;
1863 net_err_t ret = NET_ERR_NONE;
1865 ret = net_stop_tcpdump(conn_handle->network_info_handle);
1866 result = __libnet_convert_to_cp_error_type(ret);
1871 int _connection_libnet_get_tcpdump_state(connection_handle_s *conn_handle,
1872 gboolean *tcpdump_state)
1874 connection_error_e result = CONNECTION_ERROR_NONE;
1875 net_err_t ret = NET_ERR_NONE;
1877 ret = net_get_tcpdump_state(conn_handle->network_info_handle, tcpdump_state);
1878 result = __libnet_convert_to_cp_error_type(ret);
1883 int _connection_libnet_get_clock_updated(connection_handle_s *conn_handle, bool *updated)
1887 rv = net_get_clock_update_info(conn_handle->network_info_handle, updated);
1888 if (rv == NET_ERR_ACCESS_DENIED) {
1889 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1890 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1891 } else if (rv != NET_ERR_NONE) {
1892 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get clock update information[%d]", rv); //LCOV_EXCL_LINE
1893 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1896 return CONNECTION_ERROR_NONE;
1899 int _connection_libnet_get_ntp_server(connection_handle_s *conn_handle, char **ntp_server)
1903 rv = net_get_ntp_server_info(conn_handle->network_info_handle, ntp_server);
1904 if (rv == NET_ERR_ACCESS_DENIED) {
1905 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1906 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1907 } else if (rv != NET_ERR_NONE) {
1908 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get NTP Server [%d]", rv); //LCOV_EXCL_LINE
1909 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1912 return CONNECTION_ERROR_NONE;
1915 int _connection_libnet_set_ntp_server(connection_handle_s *conn_handle, const char *ntp_server)
1919 rv = net_set_ntp_server(conn_handle->network_info_handle, ntp_server);
1920 if (rv == NET_ERR_ACCESS_DENIED) {
1921 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1922 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1923 } else if (rv != NET_ERR_NONE) {
1924 CONNECTION_LOG(CONNECTION_ERROR, "Failed to set NTP server[%d]", rv); //LCOV_EXCL_LINE
1925 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1928 return CONNECTION_ERROR_NONE;
1931 int _connection_libnet_clear_ntp_server(connection_handle_s *conn_handle)
1935 rv = net_clear_ntp_server(conn_handle->network_info_handle);
1936 if (rv == NET_ERR_ACCESS_DENIED) {
1937 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1938 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1939 } else if (rv != NET_ERR_NONE) {
1940 CONNECTION_LOG(CONNECTION_ERROR, "Failed to clear NTP server[%d]", rv); //LCOV_EXCL_LINE
1941 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1944 return CONNECTION_ERROR_NONE;
1948 void _connection_lock(void)
1950 if (g_conn_thread_mutex_ref == 0)
1951 pthread_mutex_lock(&g_conn_thread_mutex);
1953 g_conn_thread_mutex_ref++;
1956 void _connection_unlock(void)
1958 if (g_conn_thread_mutex_ref == 1)
1959 pthread_mutex_unlock(&g_conn_thread_mutex);
1961 g_conn_thread_mutex_ref--;
1964 if (g_conn_thread_mutex_ref < 0) {
1965 CONNECTION_LOG(CONNECTION_ERROR,
1966 "Error scenario, thread specific mutex ref is negative !!!");
1967 g_conn_thread_mutex_ref = 0;