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>
25 #include "net_connection_private.h"
27 static __thread GSList *prof_handle_list = NULL;
28 static __thread GHashTable *profile_cb_table = NULL;
30 struct _profile_cb_s {
31 connection_profile_state_changed_cb callback;
32 connection_profile_state_e state;
36 struct _profile_list_s {
39 net_profile_info_t *profiles;
43 connection_opened_cb opened_cb;
44 connection_closed_cb closed_cb;
45 connection_set_default_cb set_default_cb;
46 connection_reset_cb reset_profile_cb;
47 libnet_ethernet_cable_state_changed_cb ethernet_cable_state_changed_cb;
48 void *opened_user_data;
49 void *closed_user_data;
50 void *set_default_user_data;
51 void *reset_profile_user_data;
55 struct _state_notify {
56 connection_profile_state_changed_cb callback;
57 connection_profile_state_e state;
61 struct managed_idle_data {
67 static __thread struct _profile_list_s profile_iterator = {0, 0, NULL};
68 static __thread struct _libnet_s libnet = {NULL, NULL, NULL, NULL, NULL,
69 NULL, NULL, NULL, NULL, false};
70 static __thread GSList *managed_idler_list = NULL;
71 static __thread bool connection_is_feature_checked[CONNECTION_SUPPORTED_FEATURE_MAX] = {0, };
72 static __thread bool connection_feature_supported[CONNECTION_SUPPORTED_FEATURE_MAX] = {0, };
74 bool _connection_is_created(void)
76 return libnet.is_created;
79 static void __connection_set_created(bool tag)
81 libnet.is_created = tag;
85 static connection_error_e __libnet_convert_to_cp_error_type(net_err_t err_type)
89 return CONNECTION_ERROR_NONE;
90 case NET_ERR_APP_ALREADY_REGISTERED:
91 return CONNECTION_ERROR_INVALID_OPERATION;
92 case NET_ERR_APP_NOT_REGISTERED:
93 return CONNECTION_ERROR_INVALID_OPERATION;
94 case NET_ERR_NO_ACTIVE_CONNECTIONS:
95 return CONNECTION_ERROR_NO_CONNECTION;
96 case NET_ERR_ACTIVE_CONNECTION_EXISTS:
97 return CONNECTION_ERROR_ALREADY_EXISTS;
98 case NET_ERR_CONNECTION_DHCP_FAILED:
99 return CONNECTION_ERROR_DHCP_FAILED;
100 case NET_ERR_CONNECTION_INVALID_KEY:
101 return CONNECTION_ERROR_INVALID_KEY;
102 case NET_ERR_IN_PROGRESS:
103 return CONNECTION_ERROR_NOW_IN_PROGRESS;
104 case NET_ERR_OPERATION_ABORTED:
105 return CONNECTION_ERROR_OPERATION_ABORTED;
106 case NET_ERR_TIME_OUT:
107 return CONNECTION_ERROR_NO_REPLY;
108 case NET_ERR_ACCESS_DENIED:
109 return CONNECTION_ERROR_PERMISSION_DENIED;
111 return CONNECTION_ERROR_OPERATION_FAILED;
115 static const char *__libnet_convert_cp_error_type_to_string(connection_error_e err_type)
118 case CONNECTION_ERROR_NONE:
120 case CONNECTION_ERROR_INVALID_PARAMETER:
121 return "INVALID_PARAMETER";
122 case CONNECTION_ERROR_OUT_OF_MEMORY:
123 return "OUT_OF_MEMORY";
124 case CONNECTION_ERROR_INVALID_OPERATION:
125 return "INVALID_OPERATION";
126 case CONNECTION_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED:
127 return "ADDRESS_FAMILY_NOT_SUPPORTED";
128 case CONNECTION_ERROR_OPERATION_FAILED:
129 return "OPERATION_FAILED";
130 case CONNECTION_ERROR_ITERATOR_END:
131 return "ITERATOR_END";
132 case CONNECTION_ERROR_NO_CONNECTION:
133 return "NO_CONNECTION";
134 case CONNECTION_ERROR_NOW_IN_PROGRESS:
135 return "NOW_IN_PROGRESS";
136 case CONNECTION_ERROR_ALREADY_EXISTS:
137 return "ALREADY_EXISTS";
138 case CONNECTION_ERROR_OPERATION_ABORTED:
139 return "OPERATION_ABORTED";
140 case CONNECTION_ERROR_DHCP_FAILED:
141 return "DHCP_FAILED";
142 case CONNECTION_ERROR_INVALID_KEY:
143 return "INVALID_KEY";
144 case CONNECTION_ERROR_NO_REPLY:
146 case CONNECTION_ERROR_PERMISSION_DENIED:
147 return "PERMISSION_DENIED";
148 case CONNECTION_ERROR_NOT_SUPPORTED:
149 return "NOT_SUPPORTED";
155 static const char *__libnet_convert_cp_state_to_string(connection_profile_state_e state)
158 case CONNECTION_PROFILE_STATE_DISCONNECTED:
159 return "DISCONNECTED";
160 case CONNECTION_PROFILE_STATE_ASSOCIATION:
161 return "ASSOCIATION";
162 case CONNECTION_PROFILE_STATE_CONFIGURATION:
163 return "CONFIGURATION";
164 case CONNECTION_PROFILE_STATE_CONNECTED:
171 static void __libnet_set_reset_profile_cb(connection_opened_cb user_cb, void *user_data)
173 if (user_cb != NULL) {
174 libnet.reset_profile_cb = user_cb;
175 libnet.reset_profile_user_data = user_data;
179 static gboolean __libnet_reset_profile_cb_idle(gpointer data)
181 connection_error_e result = (connection_error_e)data;
183 if (libnet.reset_profile_cb != NULL)
184 libnet.reset_profile_cb(result, libnet.reset_profile_user_data);
186 libnet.reset_profile_cb = NULL;
187 libnet.reset_profile_user_data = NULL;
192 static void __libnet_reset_profile_cb(connection_error_e result)
194 if (_connection_is_created() != true) {
195 CONNECTION_LOG(CONNECTION_ERROR, "Application is not registered"
196 "If multi-threaded, thread integrity be broken.");
200 if (libnet.reset_profile_cb != NULL)
201 _connection_callback_add(__libnet_reset_profile_cb_idle, (gpointer)result);
204 static void __libnet_set_opened_cb(connection_opened_cb user_cb, void *user_data)
206 if (user_cb != NULL) {
207 libnet.opened_cb = user_cb;
208 libnet.opened_user_data = user_data;
212 static gboolean __libnet_opened_cb_idle(gpointer data)
214 connection_error_e result = (connection_error_e)data;
216 if (libnet.opened_cb != NULL)
217 libnet.opened_cb(result, libnet.opened_user_data);
219 libnet.opened_cb = NULL;
220 libnet.opened_user_data = NULL;
225 static void __libnet_opened_cb(connection_error_e result)
227 if (_connection_is_created() != true) {
228 CONNECTION_LOG(CONNECTION_ERROR, "Application is not registered"
229 "If multi-threaded, thread integrity be broken.");
233 if (libnet.opened_cb != NULL)
234 _connection_callback_add(__libnet_opened_cb_idle, (gpointer)result);
237 static void __libnet_set_closed_cb(connection_closed_cb user_cb, void *user_data)
239 if (user_cb != NULL) {
240 libnet.closed_cb = user_cb;
241 libnet.closed_user_data = user_data;
245 static gboolean __libnet_closed_cb_idle(gpointer data)
247 connection_error_e result = (connection_error_e)data;
249 if (libnet.closed_cb != NULL)
250 libnet.closed_cb(result, libnet.closed_user_data);
252 libnet.closed_cb = NULL;
253 libnet.closed_user_data = NULL;
258 static void __libnet_closed_cb(connection_error_e result)
260 if (_connection_is_created() != true) {
261 CONNECTION_LOG(CONNECTION_ERROR, "Application is not registered"
262 "If multi-threaded, thread integrity be broken.");
266 if (libnet.closed_cb != NULL)
267 _connection_callback_add(__libnet_closed_cb_idle, (gpointer)result);
270 static void __libnet_set_default_cb(connection_set_default_cb user_cb, void *user_data)
272 if (user_cb != NULL) {
273 libnet.set_default_cb = user_cb;
274 libnet.set_default_user_data = user_data;
278 static gboolean __libnet_default_cb_idle(gpointer data)
280 connection_error_e result = (connection_error_e)data;
282 if (libnet.set_default_cb != NULL)
283 libnet.set_default_cb(result, libnet.set_default_user_data);
285 libnet.set_default_cb = NULL;
286 libnet.set_default_user_data = NULL;
291 static void __libnet_default_cb(connection_error_e result)
293 if (_connection_is_created() != true) {
294 CONNECTION_LOG(CONNECTION_ERROR, "Application is not registered"
295 "If multi-threaded, thread integrity be broken.");
299 if (libnet.set_default_cb != NULL)
300 _connection_callback_add(__libnet_default_cb_idle, (gpointer)result);
303 static void __libnet_set_ethernet_cable_state_changed_cb(
304 libnet_ethernet_cable_state_changed_cb user_cb)
306 libnet.ethernet_cable_state_changed_cb = user_cb;
309 static void __libnet_ethernet_cable_state_changed_cb(
310 connection_ethernet_cable_state_e state)
312 if (libnet.ethernet_cable_state_changed_cb)
313 libnet.ethernet_cable_state_changed_cb(state);
316 static gboolean __libnet_state_changed_cb_idle(gpointer data)
318 struct _state_notify *notify = (struct _state_notify *)data;
323 if (notify->callback != NULL)
324 notify->callback(notify->state, notify->user_data);
331 static void __libnet_state_changed_cb(char *profile_name, connection_profile_state_e state)
334 struct _state_notify *notify;
335 struct _profile_cb_s *cb_info;
337 if (_connection_is_created() != true) {
338 CONNECTION_LOG(CONNECTION_ERROR, "Application is not registered"
339 "If multi-threaded, thread integrity be broken.");
343 if (profile_name == NULL)
346 cb_info = g_hash_table_lookup(profile_cb_table, profile_name);
350 if (cb_info->state == state)
353 cb_info->state = state;
355 if (state < 0 || cb_info->callback == NULL)
358 notify = g_try_new0(struct _state_notify, 1);
362 notify->callback = cb_info->callback;
363 notify->state = state;
364 notify->user_data = cb_info->user_data;
366 id = _connection_callback_add(__libnet_state_changed_cb_idle,
372 static void __libnet_clear_profile_list(struct _profile_list_s *profile_list)
374 if (profile_list->count > 0)
375 g_free(profile_list->profiles);
377 profile_list->count = 0;
378 profile_list->next = 0;
379 profile_list->profiles = NULL;
382 static void __libnet_evt_cb(net_event_info_t *event_cb, void *user_data)
384 bool is_requested = false;
385 connection_error_e result = CONNECTION_ERROR_NONE;
387 switch (event_cb->Event) {
388 case NET_EVENT_OPEN_RSP:
391 case NET_EVENT_OPEN_IND:
392 result = __libnet_convert_to_cp_error_type(event_cb->Error);
393 CONNECTION_LOG(CONNECTION_INFO, "Connection opened %s[%s]",
394 (is_requested) ? "RSP" : "IND",
395 __libnet_convert_cp_error_type_to_string(result));
398 __libnet_opened_cb(result);
400 switch (event_cb->Error) {
402 case NET_ERR_ACTIVE_CONNECTION_EXISTS:
403 CONNECTION_LOG(CONNECTION_INFO, "Successfully open connection");
405 __libnet_state_changed_cb(event_cb->ProfileName, CONNECTION_PROFILE_STATE_CONNECTED);
408 CONNECTION_LOG(CONNECTION_ERROR, "Failed to open connection[%s]",
409 __libnet_convert_cp_error_type_to_string(result));
412 __libnet_state_changed_cb(event_cb->ProfileName, CONNECTION_PROFILE_STATE_DISCONNECTED);
415 case NET_EVENT_CLOSE_RSP:
418 case NET_EVENT_CLOSE_IND:
419 result = __libnet_convert_to_cp_error_type(event_cb->Error);
420 CONNECTION_LOG(CONNECTION_INFO, "Connection closed %s[%s]",
421 (is_requested) ? "RSP" : "IND",
422 __libnet_convert_cp_error_type_to_string(result));
425 __libnet_closed_cb(result);
427 switch (event_cb->Error) {
429 CONNECTION_LOG(CONNECTION_INFO, "Successfully closed connection");
431 __libnet_state_changed_cb(event_cb->ProfileName, CONNECTION_PROFILE_STATE_DISCONNECTED);
434 CONNECTION_LOG(CONNECTION_ERROR, "Failed to close connection[%s]",
435 __libnet_convert_cp_error_type_to_string(result));
439 case NET_EVENT_NET_STATE_IND:
440 CONNECTION_LOG(CONNECTION_INFO, "State changed IND");
442 if (event_cb->Datalength != sizeof(net_state_type_t))
445 net_state_type_t *profile_state = (net_state_type_t *)event_cb->Data;
446 connection_profile_state_e cp_state = _profile_convert_to_cp_state(*profile_state);
448 CONNECTION_LOG(CONNECTION_INFO, "state: %s", __libnet_convert_cp_state_to_string(cp_state));
449 SECURE_CONNECTION_LOG(CONNECTION_INFO, "profile name: %s", event_cb->ProfileName);
451 __libnet_state_changed_cb(event_cb->ProfileName, cp_state);
454 case NET_EVENT_CELLULAR_SET_DEFAULT_RSP:
455 result = __libnet_convert_to_cp_error_type(event_cb->Error);
456 CONNECTION_LOG(CONNECTION_INFO, "Got set default profile RSP %d", result);
457 __libnet_default_cb(result);
460 case NET_EVENT_CELLULAR_RESET_DEFAULT_RSP:
461 result = __libnet_convert_to_cp_error_type(event_cb->Error);
462 CONNECTION_LOG(CONNECTION_INFO, "Got reset default profile RSP %d", result);
463 __libnet_reset_profile_cb(result);
465 case NET_EVENT_ETHERNET_CABLE_ATTACHED:
466 CONNECTION_LOG(CONNECTION_INFO, "Got Ethernet cable Attached Indication\n");
467 __libnet_ethernet_cable_state_changed_cb(CONNECTION_ETHERNET_CABLE_ATTACHED);
469 case NET_EVENT_ETHERNET_CABLE_DETACHED:
470 CONNECTION_LOG(CONNECTION_INFO, "Got Ethernet cable detached Indication\n");
471 __libnet_ethernet_cable_state_changed_cb(CONNECTION_ETHERNET_CABLE_DETACHED);
480 int __libnet_get_connected_count(struct _profile_list_s *profile_list)
485 for (; i < profile_list->count; i++) {
486 if (profile_list->profiles[i].ProfileState == NET_STATE_TYPE_ONLINE ||
487 profile_list->profiles[i].ProfileState == NET_STATE_TYPE_READY)
494 void __libnet_copy_connected_profile(net_profile_info_t **dest, struct _profile_list_s *source)
498 for (; i < source->count; i++) {
499 if (source->profiles[i].ProfileState == NET_STATE_TYPE_ONLINE ||
500 source->profiles[i].ProfileState == NET_STATE_TYPE_READY) {
501 memcpy(*dest, &source->profiles[i], sizeof(net_profile_info_t));
508 int __libnet_get_default_count(struct _profile_list_s *profile_list)
513 for (; i < profile_list->count; i++) {
514 if (profile_list->profiles[i].ProfileInfo.Pdp.DefaultConn == TRUE)
521 void __libnet_copy_default_profile(net_profile_info_t **dest, struct _profile_list_s *source)
525 for (; i < source->count; i++) {
526 if (source->profiles[i].ProfileInfo.Pdp.DefaultConn == TRUE) {
527 memcpy(*dest, &source->profiles[i], sizeof(net_profile_info_t));
534 int _connection_libnet_init(void)
538 if (_connection_is_created() != true) {
539 rv = net_register_client_ext((net_event_cb_t)__libnet_evt_cb, NET_DEVICE_DEFAULT, NULL);
540 if (rv != NET_ERR_NONE)
543 __connection_set_created(true);
545 if (profile_cb_table == NULL)
546 profile_cb_table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
552 bool _connection_libnet_deinit(void)
554 if (_connection_is_created() == true) {
555 if (net_deregister_client_ext(NET_DEVICE_DEFAULT) != NET_ERR_NONE)
558 __connection_set_created(false);
560 if (profile_cb_table) {
561 g_hash_table_destroy(profile_cb_table);
562 profile_cb_table = NULL;
565 __libnet_clear_profile_list(&profile_iterator);
567 if (prof_handle_list) {
568 g_slist_free_full(prof_handle_list, g_free);
569 prof_handle_list = NULL;
576 bool _connection_libnet_check_profile_validity(connection_profile_h profile)
584 for (list = prof_handle_list; list; list = list->next)
585 if (profile == list->data) return true;
587 for (; i < profile_iterator.count; i++)
588 if (profile == &profile_iterator.profiles[i]) return true;
594 bool _connection_libnet_check_profile_cb_validity(connection_profile_h profile)
596 struct _profile_cb_s *cb_info;
597 net_profile_info_t *profile_info = profile;
602 cb_info = g_hash_table_lookup(profile_cb_table, profile_info->ProfileName);
610 int _connection_libnet_get_metered_state(bool* is_metered)
615 rv = net_get_metered_state(&status);
616 if (rv == NET_ERR_ACCESS_DENIED) {
617 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
618 return CONNECTION_ERROR_PERMISSION_DENIED;
619 } else if (rv != NET_ERR_NONE) {
620 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get metered state[%d]", rv);
621 return CONNECTION_ERROR_OPERATION_FAILED;
628 return CONNECTION_ERROR_NONE;
631 int _connection_libnet_get_wifi_state(connection_wifi_state_e *state)
634 net_wifi_state_t wlan_state;
636 rv = net_get_wifi_state(&wlan_state);
637 if (rv == NET_ERR_ACCESS_DENIED) {
638 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
639 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
640 } else if (rv != NET_ERR_NONE) {
641 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get Wi-Fi state[%d]", rv); //LCOV_EXCL_LINE
642 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
645 switch (wlan_state) {
647 *state = CONNECTION_WIFI_STATE_DEACTIVATED;
650 case WIFI_ASSOCIATION:
651 case WIFI_CONFIGURATION:
652 *state = CONNECTION_WIFI_STATE_DISCONNECTED;
655 case WIFI_DISCONNECTING:
656 *state = CONNECTION_WIFI_STATE_CONNECTED;
659 CONNECTION_LOG(CONNECTION_ERROR, "Unknown Wi-Fi state"); //LCOV_EXCL_LINE
660 return CONNECTION_ERROR_INVALID_OPERATION; //LCOV_EXCL_LINE
663 return CONNECTION_ERROR_NONE;
667 int _connection_libnet_get_ethernet_state(connection_ethernet_state_e *state)
670 struct _profile_list_s ethernet_profiles = {0, 0, NULL};
671 rv = net_get_profile_list(NET_DEVICE_ETHERNET, ðernet_profiles.profiles, ðernet_profiles.count);
672 if (rv == NET_ERR_ACCESS_DENIED) {
673 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
674 return CONNECTION_ERROR_PERMISSION_DENIED;
677 if (ethernet_profiles.count == 0) {
678 *state = CONNECTION_ETHERNET_STATE_DEACTIVATED;
679 return CONNECTION_ERROR_NONE;
682 switch (ethernet_profiles.profiles->ProfileState) {
683 case NET_STATE_TYPE_ONLINE:
684 case NET_STATE_TYPE_READY:
685 *state = CONNECTION_ETHERNET_STATE_CONNECTED;
687 case NET_STATE_TYPE_IDLE:
688 case NET_STATE_TYPE_FAILURE:
689 case NET_STATE_TYPE_ASSOCIATION:
690 case NET_STATE_TYPE_CONFIGURATION:
691 case NET_STATE_TYPE_DISCONNECT:
692 *state = CONNECTION_ETHERNET_STATE_DISCONNECTED;
695 __libnet_clear_profile_list(ðernet_profiles);
696 return CONNECTION_ERROR_OPERATION_FAILED;
699 __libnet_clear_profile_list(ðernet_profiles);
701 return CONNECTION_ERROR_NONE;
704 int _connection_libnet_get_ethernet_cable_state(connection_ethernet_cable_state_e* state)
709 rv = net_get_ethernet_cable_state(&status);
710 if (rv == NET_ERR_ACCESS_DENIED) {
711 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
712 return CONNECTION_ERROR_PERMISSION_DENIED;
713 } else if (rv != NET_ERR_NONE) {
714 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get ethernet cable state[%d]", rv);
715 return CONNECTION_ERROR_OPERATION_FAILED;
719 *state = CONNECTION_ETHERNET_CABLE_ATTACHED;
721 *state = CONNECTION_ETHERNET_CABLE_DETACHED;
722 return CONNECTION_ERROR_NONE;
725 int _connection_libnet_set_ethernet_cable_state_changed_cb(
726 libnet_ethernet_cable_state_changed_cb callback)
728 __libnet_set_ethernet_cable_state_changed_cb(callback);
730 return CONNECTION_ERROR_NONE;
734 int _connection_libnet_get_bluetooth_state(connection_bt_state_e *state)
738 struct _profile_list_s bluetooth_profiles = {0, 0, NULL};
739 rv = net_get_profile_list(NET_DEVICE_BLUETOOTH, &bluetooth_profiles.profiles, &bluetooth_profiles.count);
740 if (rv == NET_ERR_ACCESS_DENIED) {
741 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
742 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
745 if (bluetooth_profiles.count == 0) {
746 *state = CONNECTION_BT_STATE_DEACTIVATED;
747 return CONNECTION_ERROR_NONE;
751 for (; i < bluetooth_profiles.count; i++) {
752 switch (bluetooth_profiles.profiles[i].ProfileState) {
753 case NET_STATE_TYPE_ONLINE:
754 case NET_STATE_TYPE_READY:
755 *state = CONNECTION_BT_STATE_CONNECTED;
757 case NET_STATE_TYPE_IDLE:
758 case NET_STATE_TYPE_FAILURE:
759 case NET_STATE_TYPE_ASSOCIATION:
760 case NET_STATE_TYPE_CONFIGURATION:
761 case NET_STATE_TYPE_DISCONNECT:
762 *state = CONNECTION_BT_STATE_DISCONNECTED;
765 __libnet_clear_profile_list(&bluetooth_profiles);
766 return CONNECTION_ERROR_OPERATION_FAILED;
772 __libnet_clear_profile_list(&bluetooth_profiles);
774 return CONNECTION_ERROR_NONE;
777 int _connection_libnet_get_profile_iterator(connection_iterator_type_e type, connection_profile_iterator_h* profile_iter_h)
780 int rv1, rv2, rv3, rv4;
781 net_profile_info_t *profiles = NULL;
783 struct _profile_list_s wifi_profiles = {0, 0, NULL};
784 struct _profile_list_s cellular_profiles = {0, 0, NULL};
785 struct _profile_list_s ethernet_profiles = {0, 0, NULL};
786 struct _profile_list_s bluetooth_profiles = {0, 0, NULL};
788 __libnet_clear_profile_list(&profile_iterator);
790 rv1 = net_get_profile_list(NET_DEVICE_WIFI, &wifi_profiles.profiles, &wifi_profiles.count);
791 if (rv1 == NET_ERR_ACCESS_DENIED) {
792 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
793 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
794 } else if (rv1 != NET_ERR_NO_SERVICE && rv1 != NET_ERR_NONE)
795 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
797 CONNECTION_LOG(CONNECTION_INFO, "Wi-Fi profile count: %d", wifi_profiles.count);
799 rv2 = net_get_profile_list(NET_DEVICE_CELLULAR, &cellular_profiles.profiles, &cellular_profiles.count);
800 if (rv2 == NET_ERR_ACCESS_DENIED) {
801 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
802 __libnet_clear_profile_list(&wifi_profiles);
803 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
804 } else if (rv2 != NET_ERR_NO_SERVICE && rv2 != NET_ERR_NONE) {
805 __libnet_clear_profile_list(&wifi_profiles);
806 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
808 CONNECTION_LOG(CONNECTION_INFO, "Cellular profile count: %d", cellular_profiles.count);
810 rv3 = net_get_profile_list(NET_DEVICE_ETHERNET, ðernet_profiles.profiles, ðernet_profiles.count);
811 if (rv3 == NET_ERR_ACCESS_DENIED) {
812 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
813 __libnet_clear_profile_list(&wifi_profiles);
814 __libnet_clear_profile_list(&cellular_profiles);
815 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
816 } else if (rv3 != NET_ERR_NO_SERVICE && rv3 != NET_ERR_NONE) {
817 __libnet_clear_profile_list(&wifi_profiles);
818 __libnet_clear_profile_list(&cellular_profiles);
819 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
821 CONNECTION_LOG(CONNECTION_INFO, "Ethernet profile count : %d", ethernet_profiles.count);
823 rv4 = net_get_profile_list(NET_DEVICE_BLUETOOTH, &bluetooth_profiles.profiles, &bluetooth_profiles.count);
824 if (rv4 == NET_ERR_ACCESS_DENIED) {
825 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
826 __libnet_clear_profile_list(&wifi_profiles);
827 __libnet_clear_profile_list(&cellular_profiles);
828 __libnet_clear_profile_list(ðernet_profiles);
829 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
830 } else if (rv4 != NET_ERR_NO_SERVICE && rv4 != NET_ERR_NONE) {
831 __libnet_clear_profile_list(&wifi_profiles);
832 __libnet_clear_profile_list(&cellular_profiles);
833 __libnet_clear_profile_list(ðernet_profiles);
834 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
836 CONNECTION_LOG(CONNECTION_INFO, "Bluetooth profile count : %d", bluetooth_profiles.count);
838 *profile_iter_h = &profile_iterator;
841 case CONNECTION_ITERATOR_TYPE_REGISTERED:
842 count = wifi_profiles.count + cellular_profiles.count + ethernet_profiles.count + bluetooth_profiles.count;
843 CONNECTION_LOG(CONNECTION_INFO, "Total profile count : %d", count);
845 return CONNECTION_ERROR_NONE;
847 profiles = g_try_new0(net_profile_info_t, count);
848 if (profiles == NULL) {
849 __libnet_clear_profile_list(&wifi_profiles);
850 __libnet_clear_profile_list(&cellular_profiles);
851 __libnet_clear_profile_list(ðernet_profiles);
852 __libnet_clear_profile_list(&bluetooth_profiles);
853 return CONNECTION_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
856 profile_iterator.profiles = profiles;
858 if (wifi_profiles.count > 0) {
859 memcpy(profiles, wifi_profiles.profiles,
860 sizeof(net_profile_info_t) * wifi_profiles.count);
861 profiles += wifi_profiles.count;
864 if (cellular_profiles.count > 0) {
865 memcpy(profiles, cellular_profiles.profiles,
866 sizeof(net_profile_info_t) * cellular_profiles.count);
867 profiles += cellular_profiles.count;
870 if (ethernet_profiles.count > 0) {
871 memcpy(profiles, ethernet_profiles.profiles,
872 sizeof(net_profile_info_t) * ethernet_profiles.count);
873 profiles += ethernet_profiles.count;
876 if (bluetooth_profiles.count > 0)
877 memcpy(profiles, bluetooth_profiles.profiles,
878 sizeof(net_profile_info_t) * bluetooth_profiles.count);
881 case CONNECTION_ITERATOR_TYPE_CONNECTED:
882 count = __libnet_get_connected_count(&wifi_profiles);
883 count += __libnet_get_connected_count(&cellular_profiles);
884 count += __libnet_get_connected_count(ðernet_profiles);
885 count += __libnet_get_connected_count(&bluetooth_profiles);
886 CONNECTION_LOG(CONNECTION_INFO, "Total connected profile count : %d", count);
888 return CONNECTION_ERROR_NONE;
890 profiles = g_try_new0(net_profile_info_t, count);
891 if (profiles == NULL) {
892 __libnet_clear_profile_list(&wifi_profiles);
893 __libnet_clear_profile_list(&cellular_profiles);
894 __libnet_clear_profile_list(ðernet_profiles);
895 __libnet_clear_profile_list(&bluetooth_profiles);
896 return CONNECTION_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
899 profile_iterator.profiles = profiles;
901 if (wifi_profiles.count > 0)
902 __libnet_copy_connected_profile(&profiles, &wifi_profiles);
904 if (cellular_profiles.count > 0)
905 __libnet_copy_connected_profile(&profiles, &cellular_profiles);
907 if (ethernet_profiles.count > 0)
908 __libnet_copy_connected_profile(&profiles, ðernet_profiles);
910 if (bluetooth_profiles.count > 0)
911 __libnet_copy_connected_profile(&profiles, &bluetooth_profiles);
914 case CONNECTION_ITERATOR_TYPE_DEFAULT:
915 count = __libnet_get_default_count(&cellular_profiles);
916 CONNECTION_LOG(CONNECTION_INFO, "Total default profile count : %d", count); //LCOV_EXCL_LINE
918 return CONNECTION_ERROR_NONE;
920 profiles = g_try_new0(net_profile_info_t, count);
921 if (profiles == NULL) {
922 __libnet_clear_profile_list(&wifi_profiles);
923 __libnet_clear_profile_list(&cellular_profiles);
924 __libnet_clear_profile_list(ðernet_profiles);
925 __libnet_clear_profile_list(&bluetooth_profiles);
926 return CONNECTION_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
929 profile_iterator.profiles = profiles;
931 if (cellular_profiles.count > 0)
932 __libnet_copy_default_profile(&profiles, &cellular_profiles);
936 __libnet_clear_profile_list(&wifi_profiles);
937 __libnet_clear_profile_list(&cellular_profiles);
938 __libnet_clear_profile_list(ðernet_profiles);
939 __libnet_clear_profile_list(&bluetooth_profiles);
941 profile_iterator.count = count;
943 return CONNECTION_ERROR_NONE;
946 int _connection_libnet_get_iterator_next(connection_profile_iterator_h profile_iter_h, connection_profile_h *profile)
948 if (profile_iter_h != &profile_iterator)
949 return CONNECTION_ERROR_INVALID_PARAMETER;
951 if (profile_iterator.count <= profile_iterator.next)
952 return CONNECTION_ERROR_ITERATOR_END;
954 *profile = &profile_iterator.profiles[profile_iterator.next];
955 profile_iterator.next++;
957 return CONNECTION_ERROR_NONE;
960 bool _connection_libnet_iterator_has_next(connection_profile_iterator_h profile_iter_h)
962 if (profile_iter_h != &profile_iterator)
965 if (profile_iterator.count <= profile_iterator.next)
971 int _connection_libnet_destroy_iterator(connection_profile_iterator_h profile_iter_h)
973 if (profile_iter_h != &profile_iterator)
974 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
976 __libnet_clear_profile_list(&profile_iterator);
978 return CONNECTION_ERROR_NONE;
981 int _connection_libnet_get_current_profile(connection_profile_h *profile)
983 net_profile_info_t active_profile;
986 rv = net_get_active_net_info(&active_profile);
987 if (rv == NET_ERR_NO_SERVICE)
988 return CONNECTION_ERROR_NO_CONNECTION; //LCOV_EXCL_LINE
989 else if (rv == NET_ERR_ACCESS_DENIED) {
990 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
991 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
992 } else if (rv != NET_ERR_NONE)
993 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
995 *profile = g_try_malloc0(sizeof(net_profile_info_t));
996 if (*profile == NULL)
997 return CONNECTION_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
999 memcpy(*profile, &active_profile, sizeof(net_profile_info_t));
1000 prof_handle_list = g_slist_append(prof_handle_list, *profile);
1002 return CONNECTION_ERROR_NONE;
1005 int _connection_libnet_reset_profile(connection_reset_option_e type,
1006 connection_cellular_subscriber_id_e id, connection_reset_cb callback, void *user_data)
1010 rv = net_reset_profile(type, id);
1011 if (rv == NET_ERR_ACCESS_DENIED) {
1012 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1013 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1014 } else if (rv != NET_ERR_NONE) {
1015 CONNECTION_LOG(CONNECTION_ERROR, "Failed to add profile[%d]", rv); //LCOV_EXCL_LINE
1016 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1019 __libnet_set_reset_profile_cb(callback, user_data);
1021 return CONNECTION_ERROR_NONE;
1024 int _connection_libnet_open_profile(connection_profile_h profile,
1025 connection_opened_cb callback, void* user_data)
1029 if (!(_connection_libnet_check_profile_validity(profile))) {
1030 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter"); //LCOV_EXCL_LINE
1031 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1034 net_profile_info_t *profile_info = profile;
1036 rv = net_open_connection_with_profile(profile_info->ProfileName);
1037 if (rv == NET_ERR_ACCESS_DENIED) {
1038 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1039 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1040 } else if (rv != NET_ERR_NONE)
1041 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1043 __libnet_set_opened_cb(callback, user_data);
1045 return CONNECTION_ERROR_NONE;
1048 int _connection_libnet_get_cellular_service_profile(
1049 connection_cellular_service_type_e type, connection_profile_h *profile)
1052 int rv = NET_ERR_NONE;
1053 #if defined TIZEN_DUALSIM_ENABLE
1054 int default_subscriber_id = 0;
1055 char subscriber_id[3];
1058 struct _profile_list_s cellular_profiles = { 0, 0, NULL };
1059 net_service_type_t service_type = _connection_profile_convert_to_libnet_cellular_service_type(type);
1061 rv = net_get_profile_list(NET_DEVICE_CELLULAR, &cellular_profiles.profiles, &cellular_profiles.count);
1062 if (rv == NET_ERR_ACCESS_DENIED) {
1063 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1064 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1065 } else if (rv != NET_ERR_NONE) {
1066 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get profile list (%d)", rv); //LCOV_EXCL_LINE
1067 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1070 #if defined TIZEN_DUALSIM_ENABLE
1071 if (vconf_get_int(VCONF_TELEPHONY_DEFAULT_DATA_SERVICE,
1072 &default_subscriber_id) != 0) {
1073 CONNECTION_LOG(CONNECTION_ERROR,
1074 "Failed to get VCONF_TELEPHONY_DEFAULT_DATA_SERVICE");
1075 __libnet_clear_profile_list(&cellular_profiles); //LCOV_EXCL_LINE
1076 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1079 g_snprintf(subscriber_id, sizeof(subscriber_id), "%d", default_subscriber_id);
1082 for (i = 0; i < cellular_profiles.count; i++)
1083 if (cellular_profiles.profiles[i].ProfileInfo.Pdp.ServiceType == service_type)
1084 #if defined TIZEN_DUALSIM_ENABLE
1085 if (g_str_has_suffix(
1086 cellular_profiles.profiles[i].ProfileInfo.Pdp.PSModemPath,
1087 subscriber_id) == TRUE)
1091 if (i >= cellular_profiles.count) {
1092 __libnet_clear_profile_list(&cellular_profiles); //LCOV_EXCL_LINE
1093 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1096 *profile = g_try_malloc0(sizeof(net_profile_info_t));
1097 if (*profile == NULL) {
1098 __libnet_clear_profile_list(&cellular_profiles); //LCOV_EXCL_LINE
1099 return CONNECTION_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1102 memcpy(*profile, &cellular_profiles.profiles[i], sizeof(net_profile_info_t));
1104 if (cellular_profiles.profiles[i].ProfileInfo.Pdp.DefaultConn)
1108 if (type != CONNECTION_CELLULAR_SERVICE_TYPE_INTERNET &&
1109 type != CONNECTION_CELLULAR_SERVICE_TYPE_PREPAID_INTERNET)
1112 for (j = 0; j < cellular_profiles.count; j++) {
1116 if (cellular_profiles.profiles[j].ProfileInfo.Pdp.ServiceType != service_type)
1119 if (cellular_profiles.profiles[j].ProfileInfo.Pdp.DefaultConn) {
1120 memcpy(*profile, &cellular_profiles.profiles[j], sizeof(net_profile_info_t));
1127 __libnet_clear_profile_list(&cellular_profiles);
1128 prof_handle_list = g_slist_append(prof_handle_list, *profile);
1130 return CONNECTION_ERROR_NONE;
1133 int _connection_libnet_set_cellular_service_profile_sync(connection_cellular_service_type_e type, connection_profile_h profile)
1137 if (!(_connection_libnet_check_profile_validity(profile))) {
1138 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter"); //LCOV_EXCL_LINE
1139 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1142 net_profile_info_t *profile_info = profile;
1143 connection_cellular_service_type_e service_type;
1145 service_type = _profile_convert_to_connection_cellular_service_type(profile_info->ProfileInfo.Pdp.ServiceType);
1147 if (service_type != type)
1148 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1150 rv = net_set_default_cellular_service_profile(profile_info->ProfileName);
1151 if (rv == NET_ERR_ACCESS_DENIED) {
1152 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1153 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1154 } else if (rv != NET_ERR_NONE)
1155 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1157 return CONNECTION_ERROR_NONE;
1160 int _connection_libnet_set_cellular_service_profile_async(connection_cellular_service_type_e type,
1161 connection_profile_h profile, connection_set_default_cb callback, void* user_data)
1165 if (!(_connection_libnet_check_profile_validity(profile))) {
1166 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter"); //LCOV_EXCL_LINE
1167 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1170 net_profile_info_t *profile_info = profile;
1171 connection_cellular_service_type_e service_type;
1173 service_type = _profile_convert_to_connection_cellular_service_type(profile_info->ProfileInfo.Pdp.ServiceType);
1175 if (service_type != type)
1176 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1178 rv = net_set_default_cellular_service_profile_async(profile_info->ProfileName);
1179 if (rv == NET_ERR_ACCESS_DENIED) {
1180 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
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 __libnet_set_default_cb(callback, user_data);
1187 return CONNECTION_ERROR_NONE;
1190 int _connection_libnet_close_profile(connection_profile_h profile, connection_closed_cb callback, void *user_data)
1194 if (!(_connection_libnet_check_profile_validity(profile))) {
1195 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter"); //LCOV_EXCL_LINE
1196 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1199 net_profile_info_t *profile_info = profile;
1201 rv = net_close_connection(profile_info->ProfileName);
1202 if (rv == NET_ERR_ACCESS_DENIED) {
1203 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1204 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1205 } else if (rv != NET_ERR_NONE)
1206 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1208 __libnet_set_closed_cb(callback, user_data);
1210 return CONNECTION_ERROR_NONE;
1213 int _connection_libnet_add_route(const char *interface_name, const char *host_address)
1216 char *endstr = NULL;
1217 int address_family = 0;
1219 address_family = AF_INET;
1221 endstr = strrchr(host_address, '.');
1222 if (endstr == NULL ||
1223 strcmp(endstr, ".0") == 0 ||
1224 strncmp(host_address, "0.", 2) == 0 ||
1225 strstr(host_address, "255") != NULL) {
1226 CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed\n"); //LCOV_EXCL_LINE
1227 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1230 rv = net_add_route(host_address, interface_name, address_family);
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 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1237 return CONNECTION_ERROR_NONE;
1240 int _connection_libnet_remove_route(const char *interface_name, const char *host_address)
1243 char *endstr = strrchr(host_address, '.');
1244 int address_family = 0;
1246 address_family = AF_INET;
1248 endstr = strrchr(host_address, '.');
1249 if (endstr == NULL ||
1250 strcmp(endstr, ".0") == 0 ||
1251 strncmp(host_address, "0.", 2) == 0 ||
1252 strstr(host_address, ".0.") != NULL || strstr(host_address, "255") != NULL) {
1253 CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed"); //LCOV_EXCL_LINE
1254 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1257 rv = net_remove_route(host_address, interface_name, address_family);
1258 if (rv == NET_ERR_ACCESS_DENIED) {
1259 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1260 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1261 } else if (rv != NET_ERR_NONE)
1262 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1264 return CONNECTION_ERROR_NONE;
1267 int _connection_libnet_add_route_ipv6(const char *interface_name, const char *host_address, const char *gateway)
1270 int address_family = 0;
1272 address_family = AF_INET6;
1274 if (strncmp(host_address, "fe80:", 5) == 0 ||
1275 strncmp(host_address, "ff00:", 5) == 0 ||
1276 strncmp(host_address, "::", 2) == 0) {
1277 CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed\n"); //LCOV_EXCL_LINE
1278 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1281 rv = net_add_route_ipv6(host_address, interface_name, address_family, gateway);
1282 if (rv == NET_ERR_ACCESS_DENIED) {
1283 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1284 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1285 } else if (rv != NET_ERR_NONE)
1286 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1288 return CONNECTION_ERROR_NONE;
1291 int _connection_libnet_remove_route_ipv6(const char *interface_name, const char *host_address, const char *gateway)
1294 int address_family = 0;
1296 address_family = AF_INET6;
1298 if (strncmp(host_address, "fe80:", 5) == 0 ||
1299 strncmp(host_address, "ff00:", 5) == 0 ||
1300 strncmp(host_address, "::", 2) == 0) {
1301 CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed\n"); //LCOV_EXCL_LINE
1302 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1305 rv = net_remove_route_ipv6(host_address, interface_name, address_family, gateway);
1306 if (rv == NET_ERR_ACCESS_DENIED) {
1307 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1308 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1309 } else if (rv != NET_ERR_NONE)
1310 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1312 return CONNECTION_ERROR_NONE;
1315 int _connection_libnet_add_route_entry(connection_address_family_e address_family,
1316 const char *interface_name, const char *host_address, const char *gateway)
1319 char *endstr = NULL;
1320 int address_family_type = 0;
1322 if (address_family == CONNECTION_ADDRESS_FAMILY_IPV4)
1323 address_family_type = AF_INET;
1325 address_family_type = AF_INET6;
1327 if (address_family == CONNECTION_ADDRESS_FAMILY_IPV4) {
1329 endstr = strrchr(host_address, '.');
1330 if (endstr == NULL ||
1331 strcmp(endstr, ".0") == 0 ||
1332 strncmp(host_address, "0.", 2) == 0 ||
1333 strstr(host_address, "255") != NULL) {
1334 CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed\n"); //LCOV_EXCL_LINE
1335 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1338 rv = net_add_route_entry(host_address, interface_name, address_family_type, gateway);
1339 if (rv == NET_ERR_ACCESS_DENIED) {
1340 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1341 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1342 } else if (rv != NET_ERR_NONE)
1343 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1347 if (strncmp(host_address, "fe80:", 5) == 0 ||
1348 strncmp(host_address, "ff00:", 5) == 0 ||
1349 strncmp(host_address, "::", 2) == 0) {
1350 CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed\n"); //LCOV_EXCL_LINE
1351 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1354 rv = net_add_route_ipv6(host_address, interface_name, address_family_type, gateway);
1355 if (rv == NET_ERR_ACCESS_DENIED) {
1356 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1357 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1358 } else if (rv != NET_ERR_NONE)
1359 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1362 return CONNECTION_ERROR_NONE;
1365 int _connection_libnet_remove_route_entry(connection_address_family_e address_family,
1366 const char *interface_name, const char *host_address, const char *gateway)
1369 char *endstr = strrchr(host_address, '.');
1370 int address_family_type = 0;
1372 if (address_family == CONNECTION_ADDRESS_FAMILY_IPV4)
1373 address_family_type = AF_INET;
1375 address_family_type = AF_INET6;
1377 if (address_family == CONNECTION_ADDRESS_FAMILY_IPV4) {
1378 endstr = strrchr(host_address, '.');
1379 if (endstr == NULL ||
1380 strcmp(endstr, ".0") == 0 ||
1381 strncmp(host_address, "0.", 2) == 0 ||
1382 strstr(host_address, ".0.") != NULL || strstr(host_address, "255") != NULL) {
1383 CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed"); //LCOV_EXCL_LINE
1384 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1387 rv = net_remove_route_entry(host_address, interface_name, address_family_type, gateway);
1388 if (rv == NET_ERR_ACCESS_DENIED) {
1389 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1390 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1391 } else if (rv != NET_ERR_NONE)
1392 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1396 if (strncmp(host_address, "fe80:", 5) == 0 ||
1397 strncmp(host_address, "ff00:", 5) == 0 ||
1398 strncmp(host_address, "::", 2) == 0) {
1399 CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed\n"); //LCOV_EXCL_LINE
1400 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1403 rv = net_remove_route_ipv6(host_address, interface_name, address_family_type, gateway);
1404 if (rv == NET_ERR_ACCESS_DENIED) {
1405 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1406 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1407 } else if (rv != NET_ERR_NONE)
1408 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1411 return CONNECTION_ERROR_NONE;
1414 void _connection_libnet_add_to_profile_list(connection_profile_h profile)
1416 prof_handle_list = g_slist_append(prof_handle_list, profile);
1419 void _connection_libnet_remove_from_profile_list(connection_profile_h profile)
1421 prof_handle_list = g_slist_remove(prof_handle_list, profile);
1425 bool _connection_libnet_add_to_profile_cb_list(connection_profile_h profile,
1426 connection_profile_state_changed_cb callback, void *user_data)
1428 net_profile_info_t *profile_info = profile;
1429 char *profile_name = g_strdup(profile_info->ProfileName);
1431 struct _profile_cb_s *profile_cb_info = g_try_malloc0(sizeof(struct _profile_cb_s));
1432 if (profile_cb_info == NULL) {
1433 g_free(profile_name); //LCOV_EXCL_LINE
1434 return false; //LCOV_EXCL_LINE
1437 profile_cb_info->callback = callback;
1438 profile_cb_info->user_data = user_data;
1439 profile_cb_info->state = _profile_convert_to_cp_state(profile_info->ProfileState);
1441 g_hash_table_replace(profile_cb_table, profile_name, profile_cb_info);
1446 bool _connection_libnet_remove_from_profile_cb_list(connection_profile_h profile)
1448 net_profile_info_t *profile_info = profile;
1450 if (g_hash_table_remove(profile_cb_table, profile_info->ProfileName) == TRUE)
1453 return false; //LCOV_EXCL_LINE
1456 int _connection_libnet_set_statistics(net_device_t device_type, net_statistics_type_e statistics_type)
1459 rv = net_set_statistics(device_type, statistics_type);
1460 if (rv == NET_ERR_ACCESS_DENIED) {
1461 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1462 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1463 } else if (rv != NET_ERR_NONE)
1464 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1466 return CONNECTION_ERROR_NONE;
1469 int _connection_libnet_get_statistics(net_statistics_type_e statistics_type, unsigned long long *size)
1472 rv = net_get_statistics(NET_DEVICE_WIFI, statistics_type, size);
1473 if (rv == NET_ERR_ACCESS_DENIED) {
1474 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1475 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1476 } else if (rv != NET_ERR_NONE)
1477 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1479 return CONNECTION_ERROR_NONE;
1482 int _connection_libnet_set_cellular_subscriber_id(connection_profile_h profile,
1483 connection_cellular_subscriber_id_e sim_id)
1485 char *modem_path = NULL;
1486 net_profile_info_t *profile_info = (net_profile_info_t *)profile;
1488 if (net_get_cellular_modem_object_path(&modem_path, sim_id) != NET_ERR_NONE) {
1489 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get subscriber[%d]", sim_id); //LCOV_EXCL_LINE
1490 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1494 CONNECTION_LOG(CONNECTION_ERROR, "NULL modem object path"); //LCOV_EXCL_LINE
1495 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1498 g_strlcpy(profile_info->ProfileInfo.Pdp.PSModemPath, modem_path,
1499 NET_PROFILE_NAME_LEN_MAX);
1502 return CONNECTION_ERROR_NONE;
1505 static void __connection_idle_destroy_cb(gpointer data)
1510 managed_idler_list = g_slist_remove(managed_idler_list, data);
1514 static gboolean __connection_idle_cb(gpointer user_data)
1516 struct managed_idle_data *data = (struct managed_idle_data *)user_data;
1521 return data->func(data->user_data);
1524 guint _connection_callback_add(GSourceFunc func, gpointer user_data)
1527 struct managed_idle_data *data;
1528 GMainContext *context;
1534 data = g_try_new0(struct managed_idle_data, 1);
1539 data->user_data = user_data;
1541 context = g_main_context_get_thread_default();
1542 src = g_idle_source_new();
1543 g_source_set_callback(src, __connection_idle_cb, data,
1544 __connection_idle_destroy_cb);
1545 id = g_source_attach(src, context);
1546 g_source_unref(src);
1554 managed_idler_list = g_slist_append(managed_idler_list, data);
1559 void _connection_callback_cleanup(void)
1561 GSList *cur = managed_idler_list;
1563 struct managed_idle_data *data;
1567 GSList *next = cur->next;
1568 data = (struct managed_idle_data *)cur->data;
1570 src = g_main_context_find_source_by_id(g_main_context_default(), data->id);
1572 g_source_destroy(src);
1573 cur = managed_idler_list;
1579 g_slist_free(managed_idler_list);
1580 managed_idler_list = NULL;
1583 int _connection_libnet_check_get_privilege()
1587 rv = net_check_get_privilege();
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
1594 return CONNECTION_ERROR_NONE;
1597 int _connection_libnet_check_profile_privilege()
1601 rv = net_check_profile_privilege();
1602 if (rv == NET_ERR_ACCESS_DENIED) {
1603 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1604 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1605 } else if (rv != NET_ERR_NONE)
1606 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1608 return CONNECTION_ERROR_NONE;
1611 bool __libnet_check_feature_supported(const char *key, connection_supported_feature_e feature)
1613 if (!connection_is_feature_checked[feature]) {
1614 if (system_info_get_platform_bool(key, &connection_feature_supported[feature]) < 0) {
1615 CONNECTION_LOG(CONNECTION_ERROR, "Error - Feature getting from System Info"); //LCOV_EXCL_LINE
1616 set_last_result(CONNECTION_ERROR_OPERATION_FAILED); //LCOV_EXCL_LINE
1617 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1619 connection_is_feature_checked[feature] = true;
1621 return connection_feature_supported[feature];
1624 int _connection_check_feature_supported(const char *feature_name, ...)
1629 bool feature_supported = false;
1631 va_start(list, feature_name);
1634 if (strcmp(key, TELEPHONY_FEATURE) == 0)
1635 value = __libnet_check_feature_supported(key, CONNECTION_SUPPORTED_FEATURE_TELEPHONY);
1636 if (strcmp(key, WIFI_FEATURE) == 0)
1637 value = __libnet_check_feature_supported(key, CONNECTION_SUPPORTED_FEATURE_WIFI);
1638 if (strcmp(key, TETHERING_BLUETOOTH_FEATURE) == 0)
1639 value = __libnet_check_feature_supported(key, CONNECTION_SUPPORTED_FEATURE_TETHERING_BLUETOOTH);
1640 if (strcmp(key, ETHERNET_FEATURE) == 0)
1641 value = __libnet_check_feature_supported(key, CONNECTION_SUPPORTED_FEATURE_ETHERNET);
1643 feature_supported |= value;
1644 key = va_arg(list, const char *);
1647 if (!feature_supported) {
1648 CONNECTION_LOG(CONNECTION_ERROR, "Error - Feature is not supported");
1649 set_last_result(CONNECTION_ERROR_NOT_SUPPORTED);
1651 return CONNECTION_ERROR_NOT_SUPPORTED;
1655 set_last_result(CONNECTION_ERROR_NONE);
1656 return CONNECTION_ERROR_NONE;
1659 int _connection_libnet_start_tcpdump(void)
1661 connection_error_e result = CONNECTION_ERROR_NONE;
1662 net_err_t ret = NET_ERR_NONE;
1664 ret = net_start_tcpdump();
1665 result = __libnet_convert_to_cp_error_type(ret);
1670 int _connection_libnet_stop_tcpdump(void)
1672 connection_error_e result = CONNECTION_ERROR_NONE;
1673 net_err_t ret = NET_ERR_NONE;
1675 ret = net_stop_tcpdump();
1676 result = __libnet_convert_to_cp_error_type(ret);
1681 int _connection_libnet_get_tcpdump_state(gboolean *tcpdump_state)
1683 connection_error_e result = CONNECTION_ERROR_NONE;
1684 net_err_t ret = NET_ERR_NONE;
1686 ret = net_get_tcpdump_state(tcpdump_state);
1687 result = __libnet_convert_to_cp_error_type(ret);