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_INTERNET_ONLINE_IND:
580 case NET_EVENT_INTERNET_OFFLINE_IND:
581 CONNECTION_LOG(CONNECTION_INFO, "Got Internet State Changed Indication: %s",
582 event_cb->event == NET_EVENT_INTERNET_ONLINE_IND ? "Online" : "Offline");
583 net_device_t *device_type = (net_device_t *) event_cb->data;
585 if (conn_handle->internet_state_changed_callback) {
586 net_profile_info_t active_profile;
589 rv = net_get_active_net_info(conn_handle->network_info_handle, &active_profile);
591 if (rv == NET_ERR_NO_SERVICE && event_cb->event == NET_EVENT_INTERNET_OFFLINE_IND) {
593 _connection_handle_ref(conn_handle);
596 conn_handle->internet_state_changed_callback(CONNECTION_INTERNET_STATE_OFFLINE,
597 conn_handle->internet_state_changed_user_data); //LCOV_EXCL_LINE
600 _connection_handle_unref(conn_handle);
604 } else if (rv == NET_ERR_ACCESS_DENIED) {
605 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
607 } else if (rv != NET_ERR_NONE) {
608 CONNECTION_LOG(CONNECTION_ERROR, "Unable to get Default profile handle"); //LCOV_EXCL_LINE
609 break; //LCOV_EXCL_LINE
612 if (event_cb->event == NET_EVENT_INTERNET_ONLINE_IND) {
613 if (active_profile.profile_state == NET_STATE_TYPE_ONLINE &&
614 active_profile.profile_type == *device_type) {
616 _connection_handle_ref(conn_handle);
619 conn_handle->internet_state_changed_callback(CONNECTION_INTERNET_STATE_ONLINE,
620 conn_handle->internet_state_changed_user_data);
623 _connection_handle_unref(conn_handle);
629 if (active_profile.profile_state != NET_STATE_TYPE_ONLINE) {
631 _connection_handle_ref(conn_handle);
634 conn_handle->internet_state_changed_callback(CONNECTION_INTERNET_STATE_OFFLINE,
635 conn_handle->internet_state_changed_user_data);
638 _connection_handle_unref(conn_handle);
655 static int __libnet_get_connected_count(struct _profile_list_s *profile_list)
660 for (; i < profile_list->count; i++) {
661 if (profile_list->profiles[i].profile_state == NET_STATE_TYPE_ONLINE ||
662 profile_list->profiles[i].profile_state == NET_STATE_TYPE_READY)
669 static void __libnet_copy_connected_profile(net_profile_info_t **dest, struct _profile_list_s *source)
673 for (; i < source->count; i++) {
674 if (source->profiles[i].profile_state == NET_STATE_TYPE_ONLINE ||
675 source->profiles[i].profile_state == NET_STATE_TYPE_READY) {
676 memcpy(*dest, &source->profiles[i], sizeof(net_profile_info_t));
683 static int __libnet_get_default_count(struct _profile_list_s *profile_list)
688 for (; i < profile_list->count; i++) {
689 if (profile_list->profiles[i].profile_type == NET_DEVICE_CELLULAR) {
690 if (profile_list->profiles[i].profile_info.pdp.default_conn == TRUE)
698 static void __libnet_copy_default_profile(net_profile_info_t **dest, struct _profile_list_s *source)
702 for (; i < source->count; i++) {
703 if (source->profiles[i].profile_type == NET_DEVICE_CELLULAR) {
704 if (source->profiles[i].profile_info.pdp.default_conn == TRUE) {
705 memcpy(*dest, &source->profiles[i], sizeof(net_profile_info_t));
713 static void __connection_dummy_profile_state_changed_cb(connection_profile_state_e state,
716 CONNECTION_LOG(CONNECTION_INFO, "Dummy callback");
719 static int __profile_cb_table_value_destroy(gpointer data)
721 struct _profile_cb_s *profile_cb_data = (struct _profile_cb_s *)data;
723 g_free(profile_cb_data);
725 return G_SOURCE_REMOVE;
728 int _connection_libnet_init(connection_handle_s *conn_handle)
732 rv = net_register_client(&(conn_handle->network_info_handle),
733 (net_event_cb_t)__libnet_evt_cb, conn_handle);
734 if (rv != NET_ERR_NONE)
737 if (profile_cb_table == NULL)
738 profile_cb_table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
740 if (!in_container && access(CONTAINER_FILE, F_OK) == 0)
746 bool _connection_libnet_deinit(connection_handle_s *conn_handle, bool is_empty)
748 net_deregister_client(conn_handle->network_info_handle);
751 if (profile_cb_table) {
752 g_hash_table_destroy(profile_cb_table);
753 profile_cb_table = NULL;
756 __libnet_clear_profile_list(&profile_iterator);
758 if (prof_handle_list) {
759 g_slist_free_full(prof_handle_list, g_free);
760 prof_handle_list = NULL;
768 void _connection_set_cs_tid(int tid, connection_handle_s *conn_handle)
770 net_set_cs_tid(tid, conn_handle->network_info_handle);
773 void _connection_unset_cs_tid(int tid, connection_handle_s *conn_handle)
775 net_unset_cs_tid(tid, conn_handle->network_info_handle);
779 bool _connection_libnet_check_profile_validity(connection_profile_h profile)
787 for (list = prof_handle_list; list; list = list->next)
788 if (profile == list->data) return true;
790 for (; i < profile_iterator.count; i++)
791 if (profile == &profile_iterator.profiles[i]) return true;
796 int _connection_libnet_get_metered_state(connection_handle_s *conn_handle, bool* is_metered)
801 rv = net_get_metered_state(conn_handle->network_info_handle, &status);
802 if (rv == NET_ERR_ACCESS_DENIED) {
803 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
804 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
805 } else if (rv != NET_ERR_NONE) {
806 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get metered state[%d]", rv); //LCOV_EXCL_LINE
807 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
814 return CONNECTION_ERROR_NONE;
817 int _connection_libnet_get_wifi_state(connection_handle_s *conn_handle, connection_wifi_state_e *state)
820 net_wifi_state_t wlan_state;
822 rv = net_get_wifi_state(conn_handle->network_info_handle, &wlan_state);
823 if (rv == NET_ERR_ACCESS_DENIED) {
824 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
825 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
826 } else if (rv != NET_ERR_NONE) {
827 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get Wi-Fi state[%d]", rv); //LCOV_EXCL_LINE
828 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
831 switch (wlan_state) {
833 *state = CONNECTION_WIFI_STATE_DEACTIVATED;
836 case WIFI_ASSOCIATION:
837 case WIFI_CONFIGURATION:
838 *state = CONNECTION_WIFI_STATE_DISCONNECTED;
841 case WIFI_DISCONNECTING:
842 *state = CONNECTION_WIFI_STATE_CONNECTED;
845 CONNECTION_LOG(CONNECTION_ERROR, "Unknown Wi-Fi state"); //LCOV_EXCL_LINE
846 return CONNECTION_ERROR_INVALID_OPERATION; //LCOV_EXCL_LINE
849 return CONNECTION_ERROR_NONE;
853 int _connection_libnet_get_ethernet_state(connection_handle_s *conn_handle,
854 connection_ethernet_state_e *state)
857 struct _profile_list_s ethernet_profiles = {0, 0, NULL};
858 rv = net_get_profile_list(conn_handle->network_info_handle,
859 NET_DEVICE_ETHERNET, ðernet_profiles.profiles,
860 ðernet_profiles.count);
861 if (rv == NET_ERR_ACCESS_DENIED) {
862 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
863 return CONNECTION_ERROR_PERMISSION_DENIED;
866 if (ethernet_profiles.count == 0) {
867 *state = CONNECTION_ETHERNET_STATE_DEACTIVATED;
868 return CONNECTION_ERROR_NONE;
871 switch (ethernet_profiles.profiles->profile_state) {
872 case NET_STATE_TYPE_ONLINE:
873 case NET_STATE_TYPE_READY:
874 *state = CONNECTION_ETHERNET_STATE_CONNECTED;
876 case NET_STATE_TYPE_IDLE:
877 case NET_STATE_TYPE_FAILURE:
878 case NET_STATE_TYPE_ASSOCIATION:
879 case NET_STATE_TYPE_CONFIGURATION:
880 case NET_STATE_TYPE_DISCONNECT:
881 *state = CONNECTION_ETHERNET_STATE_DISCONNECTED;
884 __libnet_clear_profile_list(ðernet_profiles);
885 return CONNECTION_ERROR_OPERATION_FAILED;
888 __libnet_clear_profile_list(ðernet_profiles);
890 return CONNECTION_ERROR_NONE;
893 int _connection_libnet_get_ethernet_cable_state(connection_handle_s *conn_handle,
894 connection_ethernet_cable_state_e* state)
899 rv = net_get_ethernet_cable_state(conn_handle->network_info_handle, &status);
900 if (rv == NET_ERR_ACCESS_DENIED) {
901 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
902 return CONNECTION_ERROR_PERMISSION_DENIED;
903 } else if (rv != NET_ERR_NONE) {
904 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get ethernet cable state[%d]", rv);
905 return CONNECTION_ERROR_OPERATION_FAILED;
909 *state = CONNECTION_ETHERNET_CABLE_ATTACHED;
911 *state = CONNECTION_ETHERNET_CABLE_DETACHED;
912 return CONNECTION_ERROR_NONE;
916 int _connection_libnet_get_bluetooth_state(connection_handle_s *conn_handle, connection_bt_state_e *state)
920 struct _profile_list_s bluetooth_profiles = {0, 0, NULL};
921 rv = net_get_profile_list(conn_handle->network_info_handle,
922 NET_DEVICE_BLUETOOTH, &bluetooth_profiles.profiles,
923 &bluetooth_profiles.count);
924 if (rv == NET_ERR_ACCESS_DENIED) {
925 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
926 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
929 if (bluetooth_profiles.count == 0) {
930 *state = CONNECTION_BT_STATE_DEACTIVATED;
931 return CONNECTION_ERROR_NONE;
935 for (; i < bluetooth_profiles.count; i++) {
936 switch (bluetooth_profiles.profiles[i].profile_state) {
937 case NET_STATE_TYPE_ONLINE:
938 case NET_STATE_TYPE_READY:
939 *state = CONNECTION_BT_STATE_CONNECTED;
941 case NET_STATE_TYPE_IDLE:
942 case NET_STATE_TYPE_FAILURE:
943 case NET_STATE_TYPE_ASSOCIATION:
944 case NET_STATE_TYPE_CONFIGURATION:
945 case NET_STATE_TYPE_DISCONNECT:
946 *state = CONNECTION_BT_STATE_DISCONNECTED;
949 __libnet_clear_profile_list(&bluetooth_profiles);
950 return CONNECTION_ERROR_OPERATION_FAILED;
956 __libnet_clear_profile_list(&bluetooth_profiles);
958 return CONNECTION_ERROR_NONE;
961 int _connection_libnet_get_profile_iterator(connection_handle_s *conn_handle,
962 connection_iterator_type_e type, connection_profile_iterator_h* profile_iter_h)
966 net_profile_info_t *profiles = NULL;
968 struct _profile_list_s profile_list = {0, 0, NULL};
970 __libnet_clear_profile_list(&profile_iterator);
972 rv = net_get_all_profile_list(conn_handle->network_info_handle,
973 &profile_list.profiles, &profile_list.count);
974 if (rv == NET_ERR_ACCESS_DENIED) {
975 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
976 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
977 } else if (rv != NET_ERR_NO_SERVICE && rv != NET_ERR_NONE)
978 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
980 *profile_iter_h = &profile_iterator;
983 case CONNECTION_ITERATOR_TYPE_REGISTERED:
984 count = profile_list.count;
985 CONNECTION_LOG(CONNECTION_INFO, "Total profile count : %d", count);
987 return CONNECTION_ERROR_NONE;
989 profiles = g_try_new0(net_profile_info_t, count);
990 if (profiles == NULL) {
991 __libnet_clear_profile_list(&profile_list); //LCOV_EXCL_LINE
992 return CONNECTION_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
995 profile_iterator.profiles = profiles;
997 memcpy(profiles, profile_list.profiles, sizeof(net_profile_info_t) * count);
1000 case CONNECTION_ITERATOR_TYPE_CONNECTED:
1001 count = __libnet_get_connected_count(&profile_list);
1002 CONNECTION_LOG(CONNECTION_INFO, "Total connected profile count : %d", count);
1004 return CONNECTION_ERROR_NONE;
1006 profiles = g_try_new0(net_profile_info_t, count);
1007 if (profiles == NULL) {
1008 __libnet_clear_profile_list(&profile_list); //LCOV_EXCL_LINE
1009 return CONNECTION_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1012 profile_iterator.profiles = profiles;
1014 __libnet_copy_connected_profile(&profiles, &profile_list);
1017 case CONNECTION_ITERATOR_TYPE_DEFAULT:
1018 count = __libnet_get_default_count(&profile_list);
1019 CONNECTION_LOG(CONNECTION_INFO, "Total default profile count : %d", count); //LCOV_EXCL_LINE
1021 return CONNECTION_ERROR_NONE;
1023 profiles = g_try_new0(net_profile_info_t, count);
1024 if (profiles == NULL) {
1025 __libnet_clear_profile_list(&profile_list);
1026 return CONNECTION_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1029 profile_iterator.profiles = profiles;
1031 __libnet_copy_default_profile(&profiles, &profile_list);
1036 __libnet_clear_profile_list(&profile_list);
1038 profile_iterator.count = count;
1040 return CONNECTION_ERROR_NONE;
1043 int _connection_libnet_get_iterator_next(connection_profile_iterator_h profile_iter_h, connection_profile_h *profile)
1045 if (profile_iter_h != &profile_iterator)
1046 return CONNECTION_ERROR_INVALID_PARAMETER;
1048 if (profile_iterator.count <= profile_iterator.next)
1049 return CONNECTION_ERROR_ITERATOR_END;
1051 *profile = &profile_iterator.profiles[profile_iterator.next];
1052 profile_iterator.next++;
1054 return CONNECTION_ERROR_NONE;
1057 bool _connection_libnet_iterator_has_next(connection_profile_iterator_h profile_iter_h)
1059 if (profile_iter_h != &profile_iterator)
1062 if (profile_iterator.count <= profile_iterator.next)
1068 int _connection_libnet_destroy_iterator(connection_profile_iterator_h profile_iter_h)
1070 if (profile_iter_h != &profile_iterator)
1071 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1073 __libnet_clear_profile_list(&profile_iterator);
1075 return CONNECTION_ERROR_NONE;
1078 int _connection_libnet_get_current_profile(connection_handle_s *conn_handle,
1079 connection_profile_h *profile)
1081 net_profile_info_t active_profile;
1084 rv = net_get_active_net_info(conn_handle->network_info_handle, &active_profile);
1085 if (rv == NET_ERR_NO_SERVICE)
1086 return CONNECTION_ERROR_NO_CONNECTION; //LCOV_EXCL_LINE
1087 else if (rv == NET_ERR_ACCESS_DENIED) {
1088 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1089 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1090 } else if (rv != NET_ERR_NONE)
1091 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1093 *profile = g_try_malloc0(sizeof(net_profile_info_t));
1094 if (*profile == NULL)
1095 return CONNECTION_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1097 memcpy(*profile, &active_profile, sizeof(net_profile_info_t));
1098 prof_handle_list = g_slist_append(prof_handle_list, *profile);
1100 return CONNECTION_ERROR_NONE;
1103 int _connection_libnet_reset_profile(connection_handle_s *conn_handle,
1104 connection_reset_option_e type, connection_cellular_subscriber_id_e id)
1108 rv = net_reset_profile(conn_handle->network_info_handle, type, id);
1109 if (rv == NET_ERR_ACCESS_DENIED)
1110 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1111 else if (rv != NET_ERR_NONE)
1112 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1114 return CONNECTION_ERROR_NONE;
1117 int _connection_libnet_open_profile(connection_handle_s *conn_handle,
1118 connection_profile_h profile)
1122 if (!(_connection_libnet_check_profile_validity(profile))) {
1123 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter"); //LCOV_EXCL_LINE
1124 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1127 net_profile_info_t *profile_info = profile;
1129 if (profile_info->profile_type == NET_DEVICE_MESH)
1130 rv = net_open_mesh_connection_with_profile(conn_handle->network_info_handle, //LCOV_EXCL_LINE
1131 profile_info->profile_name);
1133 rv = net_open_connection_with_profile(conn_handle->network_info_handle,
1134 profile_info->profile_name);
1136 if (rv == NET_ERR_ACCESS_DENIED)
1137 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1138 else if (rv != NET_ERR_NONE)
1139 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1141 return CONNECTION_ERROR_NONE;
1144 int _connection_libnet_get_cellular_service_profile(connection_handle_s *conn_handle,
1145 connection_cellular_service_type_e type, connection_profile_h *profile)
1148 int rv = NET_ERR_NONE;
1149 #if defined TIZEN_DUALSIM_ENABLE
1150 int default_subscriber_id = 0;
1151 char subscriber_id[3];
1154 struct _profile_list_s cellular_profiles = { 0, 0, NULL };
1155 net_service_type_t service_type = _connection_profile_convert_to_libnet_cellular_service_type(type);
1157 rv = net_get_profile_list(conn_handle->network_info_handle,
1158 NET_DEVICE_CELLULAR, &cellular_profiles.profiles,
1159 &cellular_profiles.count);
1160 if (rv == NET_ERR_ACCESS_DENIED) {
1161 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1162 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1163 } else if (rv != NET_ERR_NONE) {
1164 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get profile list (%d)", rv); //LCOV_EXCL_LINE
1165 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1168 #if defined TIZEN_DUALSIM_ENABLE
1169 if (_connection_vconf_get_int(conn_handle, VCONF_TELEPHONY_DEFAULT_DATA_SERVICE,
1170 &default_subscriber_id) != 0) {
1171 CONNECTION_LOG(CONNECTION_ERROR,
1172 "Failed to get VCONF_TELEPHONY_DEFAULT_DATA_SERVICE");
1173 __libnet_clear_profile_list(&cellular_profiles); //LCOV_EXCL_LINE
1174 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1177 g_snprintf(subscriber_id, sizeof(subscriber_id), "%d", default_subscriber_id);
1180 for (i = 0; i < cellular_profiles.count; i++)
1181 if (cellular_profiles.profiles[i].profile_info.pdp.service_type == service_type)
1182 #if defined TIZEN_DUALSIM_ENABLE
1183 if (g_str_has_suffix(
1184 cellular_profiles.profiles[i].profile_info.pdp.ps_modem_path,
1185 subscriber_id) == TRUE)
1189 if (i >= cellular_profiles.count) {
1190 __libnet_clear_profile_list(&cellular_profiles); //LCOV_EXCL_LINE
1191 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1194 *profile = g_try_malloc0(sizeof(net_profile_info_t));
1195 if (*profile == NULL) {
1196 __libnet_clear_profile_list(&cellular_profiles); //LCOV_EXCL_LINE
1197 return CONNECTION_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1200 memcpy(*profile, &cellular_profiles.profiles[i], sizeof(net_profile_info_t));
1202 if (cellular_profiles.profiles[i].profile_info.pdp.default_conn)
1206 if (type != CONNECTION_CELLULAR_SERVICE_TYPE_INTERNET &&
1207 type != CONNECTION_CELLULAR_SERVICE_TYPE_PREPAID_INTERNET)
1210 for (j = 0; j < cellular_profiles.count; j++) {
1214 if (cellular_profiles.profiles[j].profile_info.pdp.service_type != service_type)
1217 if (cellular_profiles.profiles[j].profile_info.pdp.default_conn) {
1218 memcpy(*profile, &cellular_profiles.profiles[j], sizeof(net_profile_info_t));
1225 __libnet_clear_profile_list(&cellular_profiles);
1226 prof_handle_list = g_slist_append(prof_handle_list, *profile);
1228 return CONNECTION_ERROR_NONE;
1231 int _connection_libnet_set_cellular_service_profile_sync(connection_handle_s *conn_handle,
1232 connection_cellular_service_type_e type, connection_profile_h profile)
1236 if (!(_connection_libnet_check_profile_validity(profile))) {
1237 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter"); //LCOV_EXCL_LINE
1238 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1241 net_profile_info_t *profile_info = profile;
1242 connection_cellular_service_type_e service_type;
1244 service_type = _profile_convert_to_connection_cellular_service_type(profile_info->profile_info.pdp.service_type);
1246 if (service_type != type)
1247 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1249 rv = net_set_default_cellular_service_profile(conn_handle->network_info_handle,
1250 profile_info->profile_name);
1251 if (rv == NET_ERR_ACCESS_DENIED) {
1252 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1253 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1254 } else if (rv != NET_ERR_NONE)
1255 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1257 return CONNECTION_ERROR_NONE;
1260 int _connection_libnet_set_cellular_service_profile_async(connection_handle_s *conn_handle,
1261 connection_cellular_service_type_e type, connection_profile_h profile)
1265 if (!(_connection_libnet_check_profile_validity(profile))) {
1266 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter"); //LCOV_EXCL_LINE
1267 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1270 net_profile_info_t *profile_info = profile;
1271 connection_cellular_service_type_e service_type;
1273 service_type = _profile_convert_to_connection_cellular_service_type(profile_info->profile_info.pdp.service_type);
1275 if (service_type != type)
1276 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1278 rv = net_set_default_cellular_service_profile_async(conn_handle->network_info_handle,
1279 profile_info->profile_name);
1280 if (rv == NET_ERR_ACCESS_DENIED)
1281 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1282 else if (rv != NET_ERR_NONE)
1283 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1285 return CONNECTION_ERROR_NONE;
1288 int _connection_libnet_close_profile(connection_handle_s *conn_handle, connection_profile_h profile)
1292 if (!(_connection_libnet_check_profile_validity(profile))) {
1293 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter"); //LCOV_EXCL_LINE
1294 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1297 net_profile_info_t *profile_info = profile;
1299 if (profile_info->profile_type == NET_DEVICE_MESH)
1300 rv = net_close_mesh_connection(conn_handle->network_info_handle, profile_info->profile_name); //LCOV_EXCL_LINE
1302 rv = net_close_connection(conn_handle->network_info_handle, profile_info->profile_name);
1304 if (rv == NET_ERR_ACCESS_DENIED)
1305 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1306 else if (rv != NET_ERR_NONE)
1307 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1309 return CONNECTION_ERROR_NONE;
1312 int _connection_libnet_add_route(connection_handle_s *conn_handle,
1313 const char *interface_name, const char *host_address)
1316 char *endstr = NULL;
1317 int address_family = 0;
1319 address_family = AF_INET;
1321 endstr = strrchr(host_address, '.');
1322 if (endstr == NULL ||
1323 strcmp(endstr, ".0") == 0 ||
1324 strncmp(host_address, "0.", 2) == 0 ||
1325 strstr(host_address, "255") != NULL) {
1326 CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed\n"); //LCOV_EXCL_LINE
1327 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1330 rv = net_add_route(conn_handle->network_info_handle,
1331 host_address, interface_name, address_family);
1332 if (rv == NET_ERR_ACCESS_DENIED) {
1333 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1334 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1335 } else if (rv != NET_ERR_NONE)
1336 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1338 return CONNECTION_ERROR_NONE;
1341 int _connection_libnet_remove_route(connection_handle_s *conn_handle,
1342 const char *interface_name, const char *host_address)
1345 char *endstr = strrchr(host_address, '.');
1346 int address_family = 0;
1348 address_family = AF_INET;
1350 endstr = strrchr(host_address, '.');
1351 if (endstr == NULL ||
1352 strcmp(endstr, ".0") == 0 ||
1353 strncmp(host_address, "0.", 2) == 0 ||
1354 strstr(host_address, ".0.") != NULL || strstr(host_address, "255") != NULL) {
1355 CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed"); //LCOV_EXCL_LINE
1356 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1359 rv = net_remove_route(conn_handle->network_info_handle,
1360 host_address, interface_name, address_family);
1361 if (rv == NET_ERR_ACCESS_DENIED) {
1362 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1363 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1364 } else if (rv != NET_ERR_NONE)
1365 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1367 return CONNECTION_ERROR_NONE;
1370 int _connection_libnet_add_route_ipv6(connection_handle_s *conn_handle,
1371 const char *interface_name, const char *host_address, const char *gateway)
1374 int address_family = 0;
1376 address_family = AF_INET6;
1378 if (strncmp(host_address, "fe80:", 5) == 0 ||
1379 strncmp(host_address, "ff00:", 5) == 0 ||
1380 strncmp(host_address, "::", 2) == 0) {
1381 CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed\n"); //LCOV_EXCL_LINE
1382 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1385 rv = net_add_route_ipv6(conn_handle->network_info_handle,
1386 host_address, interface_name, address_family, gateway);
1387 if (rv == NET_ERR_ACCESS_DENIED) {
1388 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1389 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1390 } else if (rv != NET_ERR_NONE)
1391 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1393 return CONNECTION_ERROR_NONE;
1396 int _connection_libnet_remove_route_ipv6(connection_handle_s *conn_handle,
1397 const char *interface_name, const char *host_address, const char *gateway)
1400 int address_family = 0;
1402 address_family = AF_INET6;
1404 if (strncmp(host_address, "fe80:", 5) == 0 ||
1405 strncmp(host_address, "ff00:", 5) == 0 ||
1406 strncmp(host_address, "::", 2) == 0) {
1407 CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed\n"); //LCOV_EXCL_LINE
1408 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1411 rv = net_remove_route_ipv6(conn_handle->network_info_handle,
1412 host_address, interface_name, address_family, gateway);
1413 if (rv == NET_ERR_ACCESS_DENIED) {
1414 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1415 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1416 } else if (rv != NET_ERR_NONE)
1417 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1419 return CONNECTION_ERROR_NONE;
1422 int _connection_libnet_add_route_entry(connection_handle_s *conn_handle,
1423 connection_address_family_e address_family, const char *interface_name,
1424 const char *host_address, const char *gateway)
1427 char *endstr = NULL;
1428 int address_family_type = 0;
1430 if (address_family == CONNECTION_ADDRESS_FAMILY_IPV4)
1431 address_family_type = AF_INET;
1433 address_family_type = AF_INET6;
1435 if (address_family == CONNECTION_ADDRESS_FAMILY_IPV4) {
1437 endstr = strrchr(host_address, '.');
1438 if (endstr == NULL ||
1439 strcmp(endstr, ".0") == 0 ||
1440 strncmp(host_address, "0.", 2) == 0 ||
1441 strstr(host_address, "255") != NULL) {
1442 CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed\n"); //LCOV_EXCL_LINE
1443 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1446 rv = net_add_route_entry(conn_handle->network_info_handle,
1447 host_address, interface_name, address_family_type, gateway);
1448 if (rv == NET_ERR_ACCESS_DENIED) {
1449 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1450 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1451 } else if (rv != NET_ERR_NONE)
1452 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1456 if (strncmp(host_address, "fe80:", 5) == 0 ||
1457 strncmp(host_address, "ff00:", 5) == 0 ||
1458 strncmp(host_address, "::", 2) == 0) {
1459 CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed\n"); //LCOV_EXCL_LINE
1460 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1463 rv = net_add_route_ipv6(conn_handle->network_info_handle,
1464 host_address, interface_name, address_family_type, gateway);
1465 if (rv == NET_ERR_ACCESS_DENIED) {
1466 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1467 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1468 } else if (rv != NET_ERR_NONE)
1469 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1472 return CONNECTION_ERROR_NONE;
1475 int _connection_libnet_remove_route_entry(connection_handle_s *conn_handle,
1476 connection_address_family_e address_family, const char *interface_name,
1477 const char *host_address, const char *gateway)
1480 char *endstr = strrchr(host_address, '.');
1481 int address_family_type = 0;
1483 if (address_family == CONNECTION_ADDRESS_FAMILY_IPV4)
1484 address_family_type = AF_INET;
1486 address_family_type = AF_INET6;
1488 if (address_family == CONNECTION_ADDRESS_FAMILY_IPV4) {
1489 endstr = strrchr(host_address, '.');
1490 if (endstr == NULL ||
1491 strcmp(endstr, ".0") == 0 ||
1492 strncmp(host_address, "0.", 2) == 0 ||
1493 strstr(host_address, ".0.") != NULL || strstr(host_address, "255") != NULL) {
1494 CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed"); //LCOV_EXCL_LINE
1495 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1498 rv = net_remove_route_entry(conn_handle->network_info_handle, host_address,
1499 interface_name, address_family_type, gateway);
1500 if (rv == NET_ERR_ACCESS_DENIED) {
1501 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1502 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1503 } else if (rv != NET_ERR_NONE)
1504 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1508 if (strncmp(host_address, "fe80:", 5) == 0 ||
1509 strncmp(host_address, "ff00:", 5) == 0 ||
1510 strncmp(host_address, "::", 2) == 0) {
1511 CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed\n"); //LCOV_EXCL_LINE
1512 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1515 rv = net_remove_route_ipv6(conn_handle->network_info_handle, host_address,
1516 interface_name, address_family_type, gateway);
1517 if (rv == NET_ERR_ACCESS_DENIED) {
1518 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1519 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1520 } else if (rv != NET_ERR_NONE)
1521 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1524 return CONNECTION_ERROR_NONE;
1527 void _connection_libnet_add_to_profile_list(connection_profile_h profile)
1529 prof_handle_list = g_slist_append(prof_handle_list, profile);
1532 void _connection_libnet_remove_from_profile_list(connection_profile_h profile)
1534 prof_handle_list = g_slist_remove(prof_handle_list, profile);
1538 bool _connection_libnet_add_to_profile_cb_list(connection_profile_h profile,
1539 connection_profile_state_changed_cb callback, void *user_data)
1541 net_profile_info_t *profile_info = profile;
1542 char *profile_name = g_strdup(profile_info->profile_name);
1544 struct _profile_cb_s *profile_cb_info = g_try_malloc0(sizeof(struct _profile_cb_s));
1545 if (profile_cb_info == NULL) {
1546 g_free(profile_name); //LCOV_EXCL_LINE
1547 return false; //LCOV_EXCL_LINE
1550 profile_cb_info->callback = callback;
1551 profile_cb_info->user_data = user_data;
1552 profile_cb_info->state = _profile_convert_to_cp_state(profile_info->profile_state);
1554 g_hash_table_replace(profile_cb_table, profile_name, profile_cb_info);
1559 bool _connection_libnet_remove_from_profile_cb_list(connection_profile_h profile)
1561 net_profile_info_t *profile_info = profile;
1562 struct _profile_cb_s *profile_cb_info;
1564 profile_cb_info = g_hash_table_lookup(profile_cb_table, profile_info->profile_name);
1565 if (profile_cb_info == NULL)
1568 g_hash_table_remove(profile_cb_table, profile_info->profile_name);
1572 * Marking callback to dummy callback.
1575 * To avoid race condition between calling profile_state_changed_cb()
1576 * and _connection_libnet_remove_from_profile_cb_list()
1577 * in multi-threaded environment.
1579 profile_cb_info->callback = __connection_dummy_profile_state_changed_cb;
1580 profile_cb_info->user_data = NULL;
1581 profile_cb_info->state = -1;
1583 /* Timer to free hash table entry */
1584 g_timeout_add(200, __profile_cb_table_value_destroy, profile_cb_info);
1589 int _connection_libnet_set_statistics(connection_handle_s *conn_handle,
1590 net_device_t device_type, net_statistics_type_e statistics_type)
1593 rv = net_set_statistics(conn_handle->network_info_handle,
1594 device_type, statistics_type);
1595 if (rv == NET_ERR_ACCESS_DENIED) {
1596 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1597 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1598 } else if (rv != NET_ERR_NONE)
1599 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1601 return CONNECTION_ERROR_NONE;
1604 int _connection_libnet_get_statistics(connection_handle_s *conn_handle,
1605 net_statistics_type_e statistics_type, unsigned long long *size)
1608 rv = net_get_statistics(conn_handle->network_info_handle,
1609 NET_DEVICE_WIFI, statistics_type, size);
1610 if (rv == NET_ERR_ACCESS_DENIED) {
1611 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1612 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1613 } else if (rv != NET_ERR_NONE)
1614 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1616 return CONNECTION_ERROR_NONE;
1619 int _connection_libnet_set_cellular_subscriber_id(connection_profile_h profile,
1620 connection_cellular_subscriber_id_e sim_id)
1622 char *modem_path = NULL;
1623 net_profile_info_t *profile_info = (net_profile_info_t *)profile;
1625 if (net_get_cellular_modem_object_path(&modem_path, sim_id) != NET_ERR_NONE) {
1626 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get subscriber[%d]", sim_id); //LCOV_EXCL_LINE
1627 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1631 CONNECTION_LOG(CONNECTION_ERROR, "NULL modem object path"); //LCOV_EXCL_LINE
1632 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1635 g_strlcpy(profile_info->profile_info.pdp.ps_modem_path, modem_path,
1636 NET_PROFILE_NAME_LEN_MAX);
1639 return CONNECTION_ERROR_NONE;
1642 int _connection_libnet_enable_ethernet_eap(bool enable, const char *profilename)
1646 rv = net_ethernet_eap_enable(enable, profilename);
1647 if (rv == NET_ERR_ACCESS_DENIED) {
1648 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
1649 return CONNECTION_ERROR_PERMISSION_DENIED;
1650 } else if (rv != NET_ERR_NONE) {
1651 CONNECTION_LOG(CONNECTION_ERROR, "Failed to enable EAP over ethernet[%d]", rv);
1652 return CONNECTION_ERROR_OPERATION_FAILED;
1655 return CONNECTION_ERROR_NONE;
1658 int _connection_libnet_ethernet_eap_enabled(const char *profilename, bool *enabled)
1661 gboolean eap_enabled = false;
1663 rv = net_ethernet_eap_enabled(profilename, &eap_enabled);
1664 if (rv == NET_ERR_ACCESS_DENIED) {
1665 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
1666 return CONNECTION_ERROR_PERMISSION_DENIED;
1667 } else if (rv != NET_ERR_NONE) {
1668 CONNECTION_LOG(CONNECTION_ERROR, "Failed to enable EAP over ethernet[%d]", rv);
1669 return CONNECTION_ERROR_OPERATION_FAILED;
1672 *enabled = eap_enabled;
1673 return CONNECTION_ERROR_NONE;
1676 int _connection_libnet_profile_save_ethernet_eap_config(connection_handle_s *conn_handle,
1677 connection_profile_h profile)
1681 net_profile_info_t *profile_info = profile;
1683 rv = net_save_ethernet_eap_config(conn_handle->network_info_handle,
1684 &profile_info->profile_info.ethernet.net_info);
1686 if (rv == NET_ERR_ACCESS_DENIED) {
1687 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
1688 return CONNECTION_ERROR_PERMISSION_DENIED;
1689 } else if (rv != NET_ERR_NONE) {
1690 CONNECTION_LOG(CONNECTION_ERROR, "Failed to save eap config[%d]", rv);
1691 return CONNECTION_ERROR_OPERATION_FAILED;
1694 return CONNECTION_ERROR_NONE;
1697 int _connection_libnet_check_get_privilege(void)
1701 rv = net_check_get_privilege();
1702 if (rv == NET_ERR_ACCESS_DENIED) {
1703 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1704 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1705 } else if (rv != NET_ERR_NONE)
1706 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1708 return CONNECTION_ERROR_NONE;
1711 int _connection_libnet_check_profile_privilege(void)
1715 rv = net_check_profile_privilege();
1716 if (rv == NET_ERR_ACCESS_DENIED) {
1717 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1718 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1719 } else if (rv != NET_ERR_NONE)
1720 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1722 return CONNECTION_ERROR_NONE;
1725 bool __libnet_check_feature_supported(const char *key, connection_supported_feature_e feature)
1727 if (!connection_is_feature_checked[feature]) {
1728 if (system_info_get_platform_bool(key, &connection_feature_supported[feature]) < 0) {
1729 CONNECTION_LOG(CONNECTION_ERROR, "Error - Feature getting from System Info"); //LCOV_EXCL_LINE
1730 set_last_result(CONNECTION_ERROR_OPERATION_FAILED); //LCOV_EXCL_LINE
1731 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1733 connection_is_feature_checked[feature] = true;
1735 return connection_feature_supported[feature];
1738 int _connection_check_feature_supported(const char *feature_name, ...)
1743 bool feature_supported = false;
1745 va_start(list, feature_name);
1748 if (strcmp(key, TELEPHONY_FEATURE) == 0)
1749 value = __libnet_check_feature_supported(key, CONNECTION_SUPPORTED_FEATURE_TELEPHONY);
1750 if (strcmp(key, WIFI_FEATURE) == 0)
1751 value = __libnet_check_feature_supported(key, CONNECTION_SUPPORTED_FEATURE_WIFI);
1752 if (strcmp(key, TETHERING_BLUETOOTH_FEATURE) == 0)
1753 value = __libnet_check_feature_supported(key, CONNECTION_SUPPORTED_FEATURE_TETHERING_BLUETOOTH);
1754 if (strcmp(key, ETHERNET_FEATURE) == 0)
1755 value = __libnet_check_feature_supported(key, CONNECTION_SUPPORTED_FEATURE_ETHERNET);
1756 if (strcmp(key, ROUTE_FEATURE) == 0)
1757 value = __libnet_check_feature_supported(key, CONNECTION_SUPPORTED_FEATURE_ROUTE);
1759 feature_supported |= value;
1760 key = va_arg(list, const char *);
1763 if (!feature_supported) {
1764 CONNECTION_LOG(CONNECTION_ERROR, "Error - Feature is not supported");
1765 set_last_result(CONNECTION_ERROR_NOT_SUPPORTED);
1767 return CONNECTION_ERROR_NOT_SUPPORTED;
1771 set_last_result(CONNECTION_ERROR_NONE);
1772 return CONNECTION_ERROR_NONE;
1776 int _connection_libnet_start_tcpdump(connection_handle_s *conn_handle)
1778 connection_error_e result = CONNECTION_ERROR_NONE;
1779 net_err_t ret = NET_ERR_NONE;
1781 ret = net_start_tcpdump(conn_handle->network_info_handle);
1782 result = __libnet_convert_to_cp_error_type(ret);
1787 int _connection_libnet_stop_tcpdump(connection_handle_s *conn_handle)
1789 connection_error_e result = CONNECTION_ERROR_NONE;
1790 net_err_t ret = NET_ERR_NONE;
1792 ret = net_stop_tcpdump(conn_handle->network_info_handle);
1793 result = __libnet_convert_to_cp_error_type(ret);
1798 int _connection_libnet_get_tcpdump_state(connection_handle_s *conn_handle,
1799 gboolean *tcpdump_state)
1801 connection_error_e result = CONNECTION_ERROR_NONE;
1802 net_err_t ret = NET_ERR_NONE;
1804 ret = net_get_tcpdump_state(conn_handle->network_info_handle, tcpdump_state);
1805 result = __libnet_convert_to_cp_error_type(ret);
1811 int _connection_libnet_get_clock_updated(connection_handle_s *conn_handle, bool *updated)
1815 rv = net_get_clock_update_info(conn_handle->network_info_handle, updated);
1816 if (rv == NET_ERR_ACCESS_DENIED) {
1817 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1818 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1819 } else if (rv != NET_ERR_NONE) {
1820 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get clock update information[%d]", rv); //LCOV_EXCL_LINE
1821 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1824 return CONNECTION_ERROR_NONE;
1827 int _connection_libnet_set_ntp_server(connection_handle_s *conn_handle, const char *ntp_server)
1831 rv = net_set_ntp_server(conn_handle->network_info_handle, ntp_server);
1832 if (rv == NET_ERR_ACCESS_DENIED) {
1833 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1834 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1835 } else if (rv != NET_ERR_NONE) {
1836 CONNECTION_LOG(CONNECTION_ERROR, "Failed to set NTP server[%d]", rv); //LCOV_EXCL_LINE
1837 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1840 return CONNECTION_ERROR_NONE;
1843 int _connection_libnet_clear_ntp_server(connection_handle_s *conn_handle)
1847 rv = net_clear_ntp_server(conn_handle->network_info_handle);
1848 if (rv == NET_ERR_ACCESS_DENIED) {
1849 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1850 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1851 } else if (rv != NET_ERR_NONE) {
1852 CONNECTION_LOG(CONNECTION_ERROR, "Failed to clear NTP server[%d]", rv); //LCOV_EXCL_LINE
1853 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1856 return CONNECTION_ERROR_NONE;
1859 void _connection_lock(void)
1861 if (g_conn_thread_mutex_ref == 0)
1862 pthread_mutex_lock(&g_conn_thread_mutex);
1864 g_conn_thread_mutex_ref++;
1867 void _connection_unlock(void)
1869 if (g_conn_thread_mutex_ref == 1)
1870 pthread_mutex_unlock(&g_conn_thread_mutex);
1872 g_conn_thread_mutex_ref--;
1875 if (g_conn_thread_mutex_ref < 0) {
1876 CONNECTION_LOG(CONNECTION_ERROR,
1877 "Error scenario, thread specific mutex ref is negative !!!");
1878 g_conn_thread_mutex_ref = 0;