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 libnet_type_changed_cb type_changed_cb;
49 libnet_ip_changed_cb ip_changed_cb;
50 libnet_proxy_changed_cb proxy_changed_cb;
51 void *opened_user_data;
52 void *closed_user_data;
53 void *set_default_user_data;
54 void *reset_profile_user_data;
58 struct _state_notify {
59 connection_profile_state_changed_cb callback;
60 connection_profile_state_e state;
64 struct managed_idle_data {
70 static __thread struct _profile_list_s profile_iterator = {0, 0, NULL};
71 static __thread struct _libnet_s libnet = {NULL, NULL, NULL, NULL, NULL, NULL,
72 NULL, NULL, NULL, NULL, NULL, NULL, false};
73 static __thread GSList *managed_idler_list = NULL;
74 static __thread bool connection_is_feature_checked[CONNECTION_SUPPORTED_FEATURE_MAX] = {0, };
75 static __thread bool connection_feature_supported[CONNECTION_SUPPORTED_FEATURE_MAX] = {0, };
77 bool _connection_is_created(void)
79 return libnet.is_created;
82 static void __connection_set_created(bool tag)
84 libnet.is_created = tag;
88 static connection_error_e __libnet_convert_to_cp_error_type(net_err_t err_type)
92 return CONNECTION_ERROR_NONE;
93 case NET_ERR_APP_ALREADY_REGISTERED:
94 return CONNECTION_ERROR_INVALID_OPERATION;
95 case NET_ERR_APP_NOT_REGISTERED:
96 return CONNECTION_ERROR_INVALID_OPERATION;
97 case NET_ERR_NO_ACTIVE_CONNECTIONS:
98 return CONNECTION_ERROR_NO_CONNECTION;
99 case NET_ERR_ACTIVE_CONNECTION_EXISTS:
100 return CONNECTION_ERROR_ALREADY_EXISTS;
101 case NET_ERR_CONNECTION_DHCP_FAILED:
102 return CONNECTION_ERROR_DHCP_FAILED;
103 case NET_ERR_CONNECTION_INVALID_KEY:
104 return CONNECTION_ERROR_INVALID_KEY;
105 case NET_ERR_IN_PROGRESS:
106 return CONNECTION_ERROR_NOW_IN_PROGRESS;
107 case NET_ERR_OPERATION_ABORTED:
108 return CONNECTION_ERROR_OPERATION_ABORTED;
109 case NET_ERR_TIME_OUT:
110 return CONNECTION_ERROR_NO_REPLY;
111 case NET_ERR_ACCESS_DENIED:
112 return CONNECTION_ERROR_PERMISSION_DENIED;
114 return CONNECTION_ERROR_OPERATION_FAILED;
118 static const char *__libnet_convert_cp_error_type_to_string(connection_error_e err_type)
121 case CONNECTION_ERROR_NONE:
123 case CONNECTION_ERROR_INVALID_PARAMETER:
124 return "INVALID_PARAMETER";
125 case CONNECTION_ERROR_OUT_OF_MEMORY:
126 return "OUT_OF_MEMORY";
127 case CONNECTION_ERROR_INVALID_OPERATION:
128 return "INVALID_OPERATION";
129 case CONNECTION_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED:
130 return "ADDRESS_FAMILY_NOT_SUPPORTED";
131 case CONNECTION_ERROR_OPERATION_FAILED:
132 return "OPERATION_FAILED";
133 case CONNECTION_ERROR_ITERATOR_END:
134 return "ITERATOR_END";
135 case CONNECTION_ERROR_NO_CONNECTION:
136 return "NO_CONNECTION";
137 case CONNECTION_ERROR_NOW_IN_PROGRESS:
138 return "NOW_IN_PROGRESS";
139 case CONNECTION_ERROR_ALREADY_EXISTS:
140 return "ALREADY_EXISTS";
141 case CONNECTION_ERROR_OPERATION_ABORTED:
142 return "OPERATION_ABORTED";
143 case CONNECTION_ERROR_DHCP_FAILED:
144 return "DHCP_FAILED";
145 case CONNECTION_ERROR_INVALID_KEY:
146 return "INVALID_KEY";
147 case CONNECTION_ERROR_NO_REPLY:
149 case CONNECTION_ERROR_PERMISSION_DENIED:
150 return "PERMISSION_DENIED";
151 case CONNECTION_ERROR_NOT_SUPPORTED:
152 return "NOT_SUPPORTED";
158 static const char *__libnet_convert_cp_state_to_string(connection_profile_state_e state)
161 case CONNECTION_PROFILE_STATE_DISCONNECTED:
162 return "DISCONNECTED";
163 case CONNECTION_PROFILE_STATE_ASSOCIATION:
164 return "ASSOCIATION";
165 case CONNECTION_PROFILE_STATE_CONFIGURATION:
166 return "CONFIGURATION";
167 case CONNECTION_PROFILE_STATE_CONNECTED:
174 static void __libnet_set_reset_profile_cb(connection_opened_cb user_cb, void *user_data)
176 if (user_cb != NULL) {
177 libnet.reset_profile_cb = user_cb;
178 libnet.reset_profile_user_data = user_data;
182 static gboolean __libnet_reset_profile_cb_idle(gpointer data)
184 connection_error_e result = (connection_error_e)data;
186 if (libnet.reset_profile_cb != NULL)
187 libnet.reset_profile_cb(result, libnet.reset_profile_user_data);
189 libnet.reset_profile_cb = NULL;
190 libnet.reset_profile_user_data = NULL;
195 static void __libnet_reset_profile_cb(connection_error_e result)
197 if (_connection_is_created() != true) {
198 CONNECTION_LOG(CONNECTION_ERROR, "Application is not registered"
199 "If multi-threaded, thread integrity be broken.");
203 if (libnet.reset_profile_cb != NULL)
204 _connection_callback_add(__libnet_reset_profile_cb_idle, (gpointer)result);
207 static void __libnet_set_opened_cb(connection_opened_cb user_cb, void *user_data)
209 if (user_cb != NULL) {
210 libnet.opened_cb = user_cb;
211 libnet.opened_user_data = user_data;
215 static gboolean __libnet_opened_cb_idle(gpointer data)
217 connection_error_e result = (connection_error_e)data;
219 if (libnet.opened_cb != NULL)
220 libnet.opened_cb(result, libnet.opened_user_data);
222 libnet.opened_cb = NULL;
223 libnet.opened_user_data = NULL;
228 static void __libnet_opened_cb(connection_error_e result)
230 if (_connection_is_created() != true) {
231 CONNECTION_LOG(CONNECTION_ERROR, "Application is not registered"
232 "If multi-threaded, thread integrity be broken.");
236 if (libnet.opened_cb != NULL)
237 _connection_callback_add(__libnet_opened_cb_idle, (gpointer)result);
240 static void __libnet_set_closed_cb(connection_closed_cb user_cb, void *user_data)
242 if (user_cb != NULL) {
243 libnet.closed_cb = user_cb;
244 libnet.closed_user_data = user_data;
248 static gboolean __libnet_closed_cb_idle(gpointer data)
250 connection_error_e result = (connection_error_e)data;
252 if (libnet.closed_cb != NULL)
253 libnet.closed_cb(result, libnet.closed_user_data);
255 libnet.closed_cb = NULL;
256 libnet.closed_user_data = NULL;
261 static void __libnet_closed_cb(connection_error_e result)
263 if (_connection_is_created() != true) {
264 CONNECTION_LOG(CONNECTION_ERROR, "Application is not registered"
265 "If multi-threaded, thread integrity be broken.");
269 if (libnet.closed_cb != NULL)
270 _connection_callback_add(__libnet_closed_cb_idle, (gpointer)result);
273 static void __libnet_set_default_cb(connection_set_default_cb user_cb, void *user_data)
275 if (user_cb != NULL) {
276 libnet.set_default_cb = user_cb;
277 libnet.set_default_user_data = user_data;
281 static gboolean __libnet_default_cb_idle(gpointer data)
283 connection_error_e result = (connection_error_e)data;
285 if (libnet.set_default_cb != NULL)
286 libnet.set_default_cb(result, libnet.set_default_user_data);
288 libnet.set_default_cb = NULL;
289 libnet.set_default_user_data = NULL;
294 static void __libnet_default_cb(connection_error_e result)
296 if (_connection_is_created() != true) {
297 CONNECTION_LOG(CONNECTION_ERROR, "Application is not registered"
298 "If multi-threaded, thread integrity be broken.");
302 if (libnet.set_default_cb != NULL)
303 _connection_callback_add(__libnet_default_cb_idle, (gpointer)result);
306 static void __libnet_set_ethernet_cable_state_changed_cb(
307 libnet_ethernet_cable_state_changed_cb user_cb)
309 libnet.ethernet_cable_state_changed_cb = user_cb;
312 static void __libnet_ethernet_cable_state_changed_cb(
313 connection_ethernet_cable_state_e state)
315 if (libnet.ethernet_cable_state_changed_cb)
316 libnet.ethernet_cable_state_changed_cb(state);
319 static void __libnet_type_changed_cb(int type)
321 if (libnet.type_changed_cb)
322 libnet.type_changed_cb(type);
325 static void __libnet_ip_changed_cb(connection_address_family_e addr_family,
328 if (libnet.ip_changed_cb)
329 libnet.ip_changed_cb(addr_family, ip_addr);
332 static void __libnet_proxy_changed_cb(char *proxy_addr)
334 if (libnet.proxy_changed_cb)
335 libnet.proxy_changed_cb(proxy_addr);
338 static gboolean __libnet_state_changed_cb_idle(gpointer data)
340 struct _state_notify *notify = (struct _state_notify *)data;
345 if (notify->callback != NULL)
346 notify->callback(notify->state, notify->user_data);
353 static void __libnet_state_changed_cb(char *profile_name, connection_profile_state_e state)
356 struct _state_notify *notify;
357 struct _profile_cb_s *cb_info;
359 if (_connection_is_created() != true) {
360 CONNECTION_LOG(CONNECTION_ERROR, "Application is not registered"
361 "If multi-threaded, thread integrity be broken.");
365 if (profile_name == NULL)
368 cb_info = g_hash_table_lookup(profile_cb_table, profile_name);
372 if (cb_info->state == state)
375 cb_info->state = state;
377 if (state < 0 || cb_info->callback == NULL)
380 notify = g_try_new0(struct _state_notify, 1);
384 notify->callback = cb_info->callback;
385 notify->state = state;
386 notify->user_data = cb_info->user_data;
388 id = _connection_callback_add(__libnet_state_changed_cb_idle,
394 static void __libnet_clear_profile_list(struct _profile_list_s *profile_list)
396 if (profile_list->count > 0)
397 g_free(profile_list->profiles);
399 profile_list->count = 0;
400 profile_list->next = 0;
401 profile_list->profiles = NULL;
404 static void __libnet_evt_cb(net_event_info_t *event_cb, void *user_data)
406 bool is_requested = false;
407 connection_error_e result = CONNECTION_ERROR_NONE;
409 switch (event_cb->Event) {
410 case NET_EVENT_OPEN_RSP:
413 case NET_EVENT_OPEN_IND:
414 result = __libnet_convert_to_cp_error_type(event_cb->Error);
415 CONNECTION_LOG(CONNECTION_INFO, "Connection opened %s[%s]",
416 (is_requested) ? "RSP" : "IND",
417 __libnet_convert_cp_error_type_to_string(result));
420 __libnet_opened_cb(result);
422 switch (event_cb->Error) {
424 case NET_ERR_ACTIVE_CONNECTION_EXISTS:
425 CONNECTION_LOG(CONNECTION_INFO, "Successfully open connection");
427 __libnet_state_changed_cb(event_cb->ProfileName, CONNECTION_PROFILE_STATE_CONNECTED);
430 CONNECTION_LOG(CONNECTION_ERROR, "Failed to open connection[%s]",
431 __libnet_convert_cp_error_type_to_string(result));
434 __libnet_state_changed_cb(event_cb->ProfileName, CONNECTION_PROFILE_STATE_DISCONNECTED);
437 case NET_EVENT_CLOSE_RSP:
440 case NET_EVENT_CLOSE_IND:
441 result = __libnet_convert_to_cp_error_type(event_cb->Error);
442 CONNECTION_LOG(CONNECTION_INFO, "Connection closed %s[%s]",
443 (is_requested) ? "RSP" : "IND",
444 __libnet_convert_cp_error_type_to_string(result));
447 __libnet_closed_cb(result);
449 switch (event_cb->Error) {
451 CONNECTION_LOG(CONNECTION_INFO, "Successfully closed connection");
453 __libnet_state_changed_cb(event_cb->ProfileName, CONNECTION_PROFILE_STATE_DISCONNECTED);
456 CONNECTION_LOG(CONNECTION_ERROR, "Failed to close connection[%s]",
457 __libnet_convert_cp_error_type_to_string(result));
461 case NET_EVENT_NET_STATE_IND:
462 CONNECTION_LOG(CONNECTION_INFO, "State changed IND");
464 if (event_cb->Datalength != sizeof(net_state_type_t))
467 net_state_type_t *profile_state = (net_state_type_t *)event_cb->Data;
468 connection_profile_state_e cp_state = _profile_convert_to_cp_state(*profile_state);
470 CONNECTION_LOG(CONNECTION_INFO, "state: %s", __libnet_convert_cp_state_to_string(cp_state));
471 SECURE_CONNECTION_LOG(CONNECTION_INFO, "profile name: %s", event_cb->ProfileName);
473 __libnet_state_changed_cb(event_cb->ProfileName, cp_state);
476 case NET_EVENT_CELLULAR_SET_DEFAULT_RSP:
477 result = __libnet_convert_to_cp_error_type(event_cb->Error);
478 CONNECTION_LOG(CONNECTION_INFO, "Got set default profile RSP %d", result);
479 __libnet_default_cb(result);
482 case NET_EVENT_CELLULAR_RESET_DEFAULT_RSP:
483 result = __libnet_convert_to_cp_error_type(event_cb->Error);
484 CONNECTION_LOG(CONNECTION_INFO, "Got reset default profile RSP %d", result);
485 __libnet_reset_profile_cb(result);
487 case NET_EVENT_ETHERNET_CABLE_ATTACHED:
488 CONNECTION_LOG(CONNECTION_INFO, "Got Ethernet cable Attached Indication\n");
489 __libnet_ethernet_cable_state_changed_cb(CONNECTION_ETHERNET_CABLE_ATTACHED);
491 case NET_EVENT_ETHERNET_CABLE_DETACHED:
492 CONNECTION_LOG(CONNECTION_INFO, "Got Ethernet cable detached Indication\n");
493 __libnet_ethernet_cable_state_changed_cb(CONNECTION_ETHERNET_CABLE_DETACHED);
495 case NET_EVENT_NETWORK_TYPE_CHANGED:
496 CONNECTION_LOG(CONNECTION_INFO, "Got Network Type Changed Indication");
497 int *state = (int *) event_cb->Data;
498 __libnet_type_changed_cb(*state);
500 case NET_EVENT_IPV4_ADDRESS_CHANGED:
501 CONNECTION_LOG(CONNECTION_INFO, "Got IPv4 Address Changed Indication");
502 char *ipv4_addr = (char *)event_cb->Data;
503 __libnet_ip_changed_cb(CONNECTION_ADDRESS_FAMILY_IPV4, ipv4_addr);
505 case NET_EVENT_IPV6_ADDRESS_CHANGED:
506 CONNECTION_LOG(CONNECTION_INFO, "Got IPv6 Address Changed Indication");
507 char *ipv6_addr = (char *)event_cb->Data;
508 __libnet_ip_changed_cb(CONNECTION_ADDRESS_FAMILY_IPV6, ipv6_addr);
510 case NET_EVENT_PROXY_ADDRESS_CHANGED:
511 CONNECTION_LOG(CONNECTION_INFO, "Got Proxy Changed Indication");
512 char *proxy_addr = (char *)event_cb->Data;
513 __libnet_proxy_changed_cb(proxy_addr);
522 int __libnet_get_connected_count(struct _profile_list_s *profile_list)
527 for (; i < profile_list->count; i++) {
528 if (profile_list->profiles[i].ProfileState == NET_STATE_TYPE_ONLINE ||
529 profile_list->profiles[i].ProfileState == NET_STATE_TYPE_READY)
536 void __libnet_copy_connected_profile(net_profile_info_t **dest, struct _profile_list_s *source)
540 for (; i < source->count; i++) {
541 if (source->profiles[i].ProfileState == NET_STATE_TYPE_ONLINE ||
542 source->profiles[i].ProfileState == NET_STATE_TYPE_READY) {
543 memcpy(*dest, &source->profiles[i], sizeof(net_profile_info_t));
550 int __libnet_get_default_count(struct _profile_list_s *profile_list)
555 for (; i < profile_list->count; i++) {
556 if (profile_list->profiles[i].ProfileInfo.Pdp.DefaultConn == TRUE)
563 void __libnet_copy_default_profile(net_profile_info_t **dest, struct _profile_list_s *source)
567 for (; i < source->count; i++) {
568 if (source->profiles[i].ProfileInfo.Pdp.DefaultConn == TRUE) {
569 memcpy(*dest, &source->profiles[i], sizeof(net_profile_info_t));
576 int _connection_libnet_init(void)
580 if (_connection_is_created() != true) {
581 rv = net_register_client_ext((net_event_cb_t)__libnet_evt_cb, NET_DEVICE_DEFAULT, NULL);
582 if (rv != NET_ERR_NONE)
585 __connection_set_created(true);
587 if (profile_cb_table == NULL)
588 profile_cb_table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
594 bool _connection_libnet_deinit(void)
596 if (_connection_is_created() == true) {
597 if (net_deregister_client_ext(NET_DEVICE_DEFAULT) != NET_ERR_NONE)
600 __connection_set_created(false);
602 if (profile_cb_table) {
603 g_hash_table_destroy(profile_cb_table);
604 profile_cb_table = NULL;
607 __libnet_clear_profile_list(&profile_iterator);
609 if (prof_handle_list) {
610 g_slist_free_full(prof_handle_list, g_free);
611 prof_handle_list = NULL;
618 void _connection_set_cs_tid(int tid)
623 void _connection_unset_cs_tid(int tid)
625 net_unset_cs_tid(tid);
628 bool _connection_libnet_check_profile_validity(connection_profile_h profile)
636 for (list = prof_handle_list; list; list = list->next)
637 if (profile == list->data) return true;
639 for (; i < profile_iterator.count; i++)
640 if (profile == &profile_iterator.profiles[i]) return true;
646 bool _connection_libnet_check_profile_cb_validity(connection_profile_h profile)
648 struct _profile_cb_s *cb_info;
649 net_profile_info_t *profile_info = profile;
654 cb_info = g_hash_table_lookup(profile_cb_table, profile_info->ProfileName);
662 int _connection_libnet_get_metered_state(bool* is_metered)
667 rv = net_get_metered_state(&status);
668 if (rv == NET_ERR_ACCESS_DENIED) {
669 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
670 return CONNECTION_ERROR_PERMISSION_DENIED;
671 } else if (rv != NET_ERR_NONE) {
672 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get metered state[%d]", rv);
673 return CONNECTION_ERROR_OPERATION_FAILED;
680 return CONNECTION_ERROR_NONE;
683 int _connection_libnet_get_wifi_state(connection_wifi_state_e *state)
686 net_wifi_state_t wlan_state;
688 rv = net_get_wifi_state(&wlan_state);
689 if (rv == NET_ERR_ACCESS_DENIED) {
690 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
691 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
692 } else if (rv != NET_ERR_NONE) {
693 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get Wi-Fi state[%d]", rv); //LCOV_EXCL_LINE
694 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
697 switch (wlan_state) {
699 *state = CONNECTION_WIFI_STATE_DEACTIVATED;
702 case WIFI_ASSOCIATION:
703 case WIFI_CONFIGURATION:
704 *state = CONNECTION_WIFI_STATE_DISCONNECTED;
707 case WIFI_DISCONNECTING:
708 *state = CONNECTION_WIFI_STATE_CONNECTED;
711 CONNECTION_LOG(CONNECTION_ERROR, "Unknown Wi-Fi state"); //LCOV_EXCL_LINE
712 return CONNECTION_ERROR_INVALID_OPERATION; //LCOV_EXCL_LINE
715 return CONNECTION_ERROR_NONE;
718 void _connection_libnet_set_type_changed_cb(libnet_type_changed_cb callback)
720 libnet.type_changed_cb = callback;
723 void _connection_libnet_set_ip_changed_cb(libnet_ip_changed_cb callback)
725 libnet.ip_changed_cb = callback;
728 void _connection_libnet_set_proxy_changed_cb(libnet_proxy_changed_cb callback)
730 libnet.proxy_changed_cb = callback;
734 int _connection_libnet_get_ethernet_state(connection_ethernet_state_e *state)
737 struct _profile_list_s ethernet_profiles = {0, 0, NULL};
738 rv = net_get_profile_list(NET_DEVICE_ETHERNET, ðernet_profiles.profiles, ðernet_profiles.count);
739 if (rv == NET_ERR_ACCESS_DENIED) {
740 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
741 return CONNECTION_ERROR_PERMISSION_DENIED;
744 if (ethernet_profiles.count == 0) {
745 *state = CONNECTION_ETHERNET_STATE_DEACTIVATED;
746 return CONNECTION_ERROR_NONE;
749 switch (ethernet_profiles.profiles->ProfileState) {
750 case NET_STATE_TYPE_ONLINE:
751 case NET_STATE_TYPE_READY:
752 *state = CONNECTION_ETHERNET_STATE_CONNECTED;
754 case NET_STATE_TYPE_IDLE:
755 case NET_STATE_TYPE_FAILURE:
756 case NET_STATE_TYPE_ASSOCIATION:
757 case NET_STATE_TYPE_CONFIGURATION:
758 case NET_STATE_TYPE_DISCONNECT:
759 *state = CONNECTION_ETHERNET_STATE_DISCONNECTED;
762 __libnet_clear_profile_list(ðernet_profiles);
763 return CONNECTION_ERROR_OPERATION_FAILED;
766 __libnet_clear_profile_list(ðernet_profiles);
768 return CONNECTION_ERROR_NONE;
771 int _connection_libnet_get_ethernet_cable_state(connection_ethernet_cable_state_e* state)
776 rv = net_get_ethernet_cable_state(&status);
777 if (rv == NET_ERR_ACCESS_DENIED) {
778 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
779 return CONNECTION_ERROR_PERMISSION_DENIED;
780 } else if (rv != NET_ERR_NONE) {
781 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get ethernet cable state[%d]", rv);
782 return CONNECTION_ERROR_OPERATION_FAILED;
786 *state = CONNECTION_ETHERNET_CABLE_ATTACHED;
788 *state = CONNECTION_ETHERNET_CABLE_DETACHED;
789 return CONNECTION_ERROR_NONE;
792 int _connection_libnet_set_ethernet_cable_state_changed_cb(
793 libnet_ethernet_cable_state_changed_cb callback)
795 __libnet_set_ethernet_cable_state_changed_cb(callback);
797 return CONNECTION_ERROR_NONE;
801 int _connection_libnet_get_bluetooth_state(connection_bt_state_e *state)
805 struct _profile_list_s bluetooth_profiles = {0, 0, NULL};
806 rv = net_get_profile_list(NET_DEVICE_BLUETOOTH, &bluetooth_profiles.profiles, &bluetooth_profiles.count);
807 if (rv == NET_ERR_ACCESS_DENIED) {
808 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
809 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
812 if (bluetooth_profiles.count == 0) {
813 *state = CONNECTION_BT_STATE_DEACTIVATED;
814 return CONNECTION_ERROR_NONE;
818 for (; i < bluetooth_profiles.count; i++) {
819 switch (bluetooth_profiles.profiles[i].ProfileState) {
820 case NET_STATE_TYPE_ONLINE:
821 case NET_STATE_TYPE_READY:
822 *state = CONNECTION_BT_STATE_CONNECTED;
824 case NET_STATE_TYPE_IDLE:
825 case NET_STATE_TYPE_FAILURE:
826 case NET_STATE_TYPE_ASSOCIATION:
827 case NET_STATE_TYPE_CONFIGURATION:
828 case NET_STATE_TYPE_DISCONNECT:
829 *state = CONNECTION_BT_STATE_DISCONNECTED;
832 __libnet_clear_profile_list(&bluetooth_profiles);
833 return CONNECTION_ERROR_OPERATION_FAILED;
839 __libnet_clear_profile_list(&bluetooth_profiles);
841 return CONNECTION_ERROR_NONE;
844 int _connection_libnet_get_profile_iterator(connection_iterator_type_e type, connection_profile_iterator_h* profile_iter_h)
847 int rv1, rv2, rv3, rv4;
848 net_profile_info_t *profiles = NULL;
850 struct _profile_list_s wifi_profiles = {0, 0, NULL};
851 struct _profile_list_s cellular_profiles = {0, 0, NULL};
852 struct _profile_list_s ethernet_profiles = {0, 0, NULL};
853 struct _profile_list_s bluetooth_profiles = {0, 0, NULL};
855 __libnet_clear_profile_list(&profile_iterator);
857 rv1 = net_get_profile_list(NET_DEVICE_WIFI, &wifi_profiles.profiles, &wifi_profiles.count);
858 if (rv1 == NET_ERR_ACCESS_DENIED) {
859 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
860 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
861 } else if (rv1 != NET_ERR_NO_SERVICE && rv1 != NET_ERR_NONE)
862 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
864 CONNECTION_LOG(CONNECTION_INFO, "Wi-Fi profile count: %d", wifi_profiles.count);
866 rv2 = net_get_profile_list(NET_DEVICE_CELLULAR, &cellular_profiles.profiles, &cellular_profiles.count);
867 if (rv2 == NET_ERR_ACCESS_DENIED) {
868 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
869 __libnet_clear_profile_list(&wifi_profiles);
870 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
871 } else if (rv2 != NET_ERR_NO_SERVICE && rv2 != NET_ERR_NONE) {
872 __libnet_clear_profile_list(&wifi_profiles);
873 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
875 CONNECTION_LOG(CONNECTION_INFO, "Cellular profile count: %d", cellular_profiles.count);
877 rv3 = net_get_profile_list(NET_DEVICE_ETHERNET, ðernet_profiles.profiles, ðernet_profiles.count);
878 if (rv3 == NET_ERR_ACCESS_DENIED) {
879 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
880 __libnet_clear_profile_list(&wifi_profiles);
881 __libnet_clear_profile_list(&cellular_profiles);
882 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
883 } else if (rv3 != NET_ERR_NO_SERVICE && rv3 != NET_ERR_NONE) {
884 __libnet_clear_profile_list(&wifi_profiles);
885 __libnet_clear_profile_list(&cellular_profiles);
886 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
888 CONNECTION_LOG(CONNECTION_INFO, "Ethernet profile count : %d", ethernet_profiles.count);
890 rv4 = net_get_profile_list(NET_DEVICE_BLUETOOTH, &bluetooth_profiles.profiles, &bluetooth_profiles.count);
891 if (rv4 == NET_ERR_ACCESS_DENIED) {
892 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
893 __libnet_clear_profile_list(&wifi_profiles);
894 __libnet_clear_profile_list(&cellular_profiles);
895 __libnet_clear_profile_list(ðernet_profiles);
896 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
897 } else if (rv4 != NET_ERR_NO_SERVICE && rv4 != NET_ERR_NONE) {
898 __libnet_clear_profile_list(&wifi_profiles);
899 __libnet_clear_profile_list(&cellular_profiles);
900 __libnet_clear_profile_list(ðernet_profiles);
901 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
903 CONNECTION_LOG(CONNECTION_INFO, "Bluetooth profile count : %d", bluetooth_profiles.count);
905 *profile_iter_h = &profile_iterator;
908 case CONNECTION_ITERATOR_TYPE_REGISTERED:
909 count = wifi_profiles.count + cellular_profiles.count + ethernet_profiles.count + bluetooth_profiles.count;
910 CONNECTION_LOG(CONNECTION_INFO, "Total profile count : %d", count);
912 return CONNECTION_ERROR_NONE;
914 profiles = g_try_new0(net_profile_info_t, count);
915 if (profiles == NULL) {
916 __libnet_clear_profile_list(&wifi_profiles);
917 __libnet_clear_profile_list(&cellular_profiles);
918 __libnet_clear_profile_list(ðernet_profiles);
919 __libnet_clear_profile_list(&bluetooth_profiles);
920 return CONNECTION_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
923 profile_iterator.profiles = profiles;
925 if (wifi_profiles.count > 0) {
926 memcpy(profiles, wifi_profiles.profiles,
927 sizeof(net_profile_info_t) * wifi_profiles.count);
928 profiles += wifi_profiles.count;
931 if (cellular_profiles.count > 0) {
932 memcpy(profiles, cellular_profiles.profiles,
933 sizeof(net_profile_info_t) * cellular_profiles.count);
934 profiles += cellular_profiles.count;
937 if (ethernet_profiles.count > 0) {
938 memcpy(profiles, ethernet_profiles.profiles,
939 sizeof(net_profile_info_t) * ethernet_profiles.count);
940 profiles += ethernet_profiles.count;
943 if (bluetooth_profiles.count > 0)
944 memcpy(profiles, bluetooth_profiles.profiles,
945 sizeof(net_profile_info_t) * bluetooth_profiles.count);
948 case CONNECTION_ITERATOR_TYPE_CONNECTED:
949 count = __libnet_get_connected_count(&wifi_profiles);
950 count += __libnet_get_connected_count(&cellular_profiles);
951 count += __libnet_get_connected_count(ðernet_profiles);
952 count += __libnet_get_connected_count(&bluetooth_profiles);
953 CONNECTION_LOG(CONNECTION_INFO, "Total connected profile count : %d", count);
955 return CONNECTION_ERROR_NONE;
957 profiles = g_try_new0(net_profile_info_t, count);
958 if (profiles == NULL) {
959 __libnet_clear_profile_list(&wifi_profiles);
960 __libnet_clear_profile_list(&cellular_profiles);
961 __libnet_clear_profile_list(ðernet_profiles);
962 __libnet_clear_profile_list(&bluetooth_profiles);
963 return CONNECTION_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
966 profile_iterator.profiles = profiles;
968 if (wifi_profiles.count > 0)
969 __libnet_copy_connected_profile(&profiles, &wifi_profiles);
971 if (cellular_profiles.count > 0)
972 __libnet_copy_connected_profile(&profiles, &cellular_profiles);
974 if (ethernet_profiles.count > 0)
975 __libnet_copy_connected_profile(&profiles, ðernet_profiles);
977 if (bluetooth_profiles.count > 0)
978 __libnet_copy_connected_profile(&profiles, &bluetooth_profiles);
981 case CONNECTION_ITERATOR_TYPE_DEFAULT:
982 count = __libnet_get_default_count(&cellular_profiles);
983 CONNECTION_LOG(CONNECTION_INFO, "Total default profile count : %d", count); //LCOV_EXCL_LINE
985 return CONNECTION_ERROR_NONE;
987 profiles = g_try_new0(net_profile_info_t, count);
988 if (profiles == NULL) {
989 __libnet_clear_profile_list(&wifi_profiles);
990 __libnet_clear_profile_list(&cellular_profiles);
991 __libnet_clear_profile_list(ðernet_profiles);
992 __libnet_clear_profile_list(&bluetooth_profiles);
993 return CONNECTION_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
996 profile_iterator.profiles = profiles;
998 if (cellular_profiles.count > 0)
999 __libnet_copy_default_profile(&profiles, &cellular_profiles);
1003 __libnet_clear_profile_list(&wifi_profiles);
1004 __libnet_clear_profile_list(&cellular_profiles);
1005 __libnet_clear_profile_list(ðernet_profiles);
1006 __libnet_clear_profile_list(&bluetooth_profiles);
1008 profile_iterator.count = count;
1010 return CONNECTION_ERROR_NONE;
1013 int _connection_libnet_get_iterator_next(connection_profile_iterator_h profile_iter_h, connection_profile_h *profile)
1015 if (profile_iter_h != &profile_iterator)
1016 return CONNECTION_ERROR_INVALID_PARAMETER;
1018 if (profile_iterator.count <= profile_iterator.next)
1019 return CONNECTION_ERROR_ITERATOR_END;
1021 *profile = &profile_iterator.profiles[profile_iterator.next];
1022 profile_iterator.next++;
1024 return CONNECTION_ERROR_NONE;
1027 bool _connection_libnet_iterator_has_next(connection_profile_iterator_h profile_iter_h)
1029 if (profile_iter_h != &profile_iterator)
1032 if (profile_iterator.count <= profile_iterator.next)
1038 int _connection_libnet_destroy_iterator(connection_profile_iterator_h profile_iter_h)
1040 if (profile_iter_h != &profile_iterator)
1041 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1043 __libnet_clear_profile_list(&profile_iterator);
1045 return CONNECTION_ERROR_NONE;
1048 int _connection_libnet_get_current_profile(connection_profile_h *profile)
1050 net_profile_info_t active_profile;
1053 rv = net_get_active_net_info(&active_profile);
1054 if (rv == NET_ERR_NO_SERVICE)
1055 return CONNECTION_ERROR_NO_CONNECTION; //LCOV_EXCL_LINE
1056 else if (rv == NET_ERR_ACCESS_DENIED) {
1057 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1058 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1059 } else if (rv != NET_ERR_NONE)
1060 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1062 *profile = g_try_malloc0(sizeof(net_profile_info_t));
1063 if (*profile == NULL)
1064 return CONNECTION_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1066 memcpy(*profile, &active_profile, sizeof(net_profile_info_t));
1067 prof_handle_list = g_slist_append(prof_handle_list, *profile);
1069 return CONNECTION_ERROR_NONE;
1072 int _connection_libnet_reset_profile(connection_reset_option_e type,
1073 connection_cellular_subscriber_id_e id, connection_reset_cb callback, void *user_data)
1077 rv = net_reset_profile(type, id);
1078 if (rv == NET_ERR_ACCESS_DENIED) {
1079 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1080 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1081 } else if (rv != NET_ERR_NONE) {
1082 CONNECTION_LOG(CONNECTION_ERROR, "Failed to add profile[%d]", rv); //LCOV_EXCL_LINE
1083 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1086 __libnet_set_reset_profile_cb(callback, user_data);
1088 return CONNECTION_ERROR_NONE;
1091 int _connection_libnet_open_profile(connection_profile_h profile,
1092 connection_opened_cb callback, void* user_data)
1096 if (!(_connection_libnet_check_profile_validity(profile))) {
1097 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter"); //LCOV_EXCL_LINE
1098 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1101 net_profile_info_t *profile_info = profile;
1103 rv = net_open_connection_with_profile(profile_info->ProfileName);
1104 if (rv == NET_ERR_ACCESS_DENIED) {
1105 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1106 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1107 } else if (rv != NET_ERR_NONE)
1108 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1110 __libnet_set_opened_cb(callback, user_data);
1112 return CONNECTION_ERROR_NONE;
1115 int _connection_libnet_get_cellular_service_profile(
1116 connection_cellular_service_type_e type, connection_profile_h *profile)
1119 int rv = NET_ERR_NONE;
1120 #if defined TIZEN_DUALSIM_ENABLE
1121 int default_subscriber_id = 0;
1122 char subscriber_id[3];
1125 struct _profile_list_s cellular_profiles = { 0, 0, NULL };
1126 net_service_type_t service_type = _connection_profile_convert_to_libnet_cellular_service_type(type);
1128 rv = net_get_profile_list(NET_DEVICE_CELLULAR, &cellular_profiles.profiles, &cellular_profiles.count);
1129 if (rv == NET_ERR_ACCESS_DENIED) {
1130 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1131 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1132 } else if (rv != NET_ERR_NONE) {
1133 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get profile list (%d)", rv); //LCOV_EXCL_LINE
1134 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1137 #if defined TIZEN_DUALSIM_ENABLE
1138 if (vconf_get_int(VCONF_TELEPHONY_DEFAULT_DATA_SERVICE,
1139 &default_subscriber_id) != 0) {
1140 CONNECTION_LOG(CONNECTION_ERROR,
1141 "Failed to get VCONF_TELEPHONY_DEFAULT_DATA_SERVICE");
1142 __libnet_clear_profile_list(&cellular_profiles); //LCOV_EXCL_LINE
1143 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1146 g_snprintf(subscriber_id, sizeof(subscriber_id), "%d", default_subscriber_id);
1149 for (i = 0; i < cellular_profiles.count; i++)
1150 if (cellular_profiles.profiles[i].ProfileInfo.Pdp.ServiceType == service_type)
1151 #if defined TIZEN_DUALSIM_ENABLE
1152 if (g_str_has_suffix(
1153 cellular_profiles.profiles[i].ProfileInfo.Pdp.PSModemPath,
1154 subscriber_id) == TRUE)
1158 if (i >= cellular_profiles.count) {
1159 __libnet_clear_profile_list(&cellular_profiles); //LCOV_EXCL_LINE
1160 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1163 *profile = g_try_malloc0(sizeof(net_profile_info_t));
1164 if (*profile == NULL) {
1165 __libnet_clear_profile_list(&cellular_profiles); //LCOV_EXCL_LINE
1166 return CONNECTION_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1169 memcpy(*profile, &cellular_profiles.profiles[i], sizeof(net_profile_info_t));
1171 if (cellular_profiles.profiles[i].ProfileInfo.Pdp.DefaultConn)
1175 if (type != CONNECTION_CELLULAR_SERVICE_TYPE_INTERNET &&
1176 type != CONNECTION_CELLULAR_SERVICE_TYPE_PREPAID_INTERNET)
1179 for (j = 0; j < cellular_profiles.count; j++) {
1183 if (cellular_profiles.profiles[j].ProfileInfo.Pdp.ServiceType != service_type)
1186 if (cellular_profiles.profiles[j].ProfileInfo.Pdp.DefaultConn) {
1187 memcpy(*profile, &cellular_profiles.profiles[j], sizeof(net_profile_info_t));
1194 __libnet_clear_profile_list(&cellular_profiles);
1195 prof_handle_list = g_slist_append(prof_handle_list, *profile);
1197 return CONNECTION_ERROR_NONE;
1200 int _connection_libnet_set_cellular_service_profile_sync(connection_cellular_service_type_e type, connection_profile_h profile)
1204 if (!(_connection_libnet_check_profile_validity(profile))) {
1205 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter"); //LCOV_EXCL_LINE
1206 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1209 net_profile_info_t *profile_info = profile;
1210 connection_cellular_service_type_e service_type;
1212 service_type = _profile_convert_to_connection_cellular_service_type(profile_info->ProfileInfo.Pdp.ServiceType);
1214 if (service_type != type)
1215 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1217 rv = net_set_default_cellular_service_profile(profile_info->ProfileName);
1218 if (rv == NET_ERR_ACCESS_DENIED) {
1219 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1220 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1221 } else if (rv != NET_ERR_NONE)
1222 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1224 return CONNECTION_ERROR_NONE;
1227 int _connection_libnet_set_cellular_service_profile_async(connection_cellular_service_type_e type,
1228 connection_profile_h profile, connection_set_default_cb callback, void* user_data)
1232 if (!(_connection_libnet_check_profile_validity(profile))) {
1233 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter"); //LCOV_EXCL_LINE
1234 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1237 net_profile_info_t *profile_info = profile;
1238 connection_cellular_service_type_e service_type;
1240 service_type = _profile_convert_to_connection_cellular_service_type(profile_info->ProfileInfo.Pdp.ServiceType);
1242 if (service_type != type)
1243 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1245 rv = net_set_default_cellular_service_profile_async(profile_info->ProfileName);
1246 if (rv == NET_ERR_ACCESS_DENIED) {
1247 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1248 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1249 } else if (rv != NET_ERR_NONE)
1250 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1252 __libnet_set_default_cb(callback, user_data);
1254 return CONNECTION_ERROR_NONE;
1257 int _connection_libnet_close_profile(connection_profile_h profile, connection_closed_cb callback, void *user_data)
1261 if (!(_connection_libnet_check_profile_validity(profile))) {
1262 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter"); //LCOV_EXCL_LINE
1263 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1266 net_profile_info_t *profile_info = profile;
1268 rv = net_close_connection(profile_info->ProfileName);
1269 if (rv == NET_ERR_ACCESS_DENIED) {
1270 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1271 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1272 } else if (rv != NET_ERR_NONE)
1273 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1275 __libnet_set_closed_cb(callback, user_data);
1277 return CONNECTION_ERROR_NONE;
1280 int _connection_libnet_add_route(const char *interface_name, const char *host_address)
1283 char *endstr = NULL;
1284 int address_family = 0;
1286 address_family = AF_INET;
1288 endstr = strrchr(host_address, '.');
1289 if (endstr == NULL ||
1290 strcmp(endstr, ".0") == 0 ||
1291 strncmp(host_address, "0.", 2) == 0 ||
1292 strstr(host_address, "255") != NULL) {
1293 CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed\n"); //LCOV_EXCL_LINE
1294 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1297 rv = net_add_route(host_address, interface_name, address_family);
1298 if (rv == NET_ERR_ACCESS_DENIED) {
1299 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1300 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1301 } else if (rv != NET_ERR_NONE)
1302 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1304 return CONNECTION_ERROR_NONE;
1307 int _connection_libnet_remove_route(const char *interface_name, const char *host_address)
1310 char *endstr = strrchr(host_address, '.');
1311 int address_family = 0;
1313 address_family = AF_INET;
1315 endstr = strrchr(host_address, '.');
1316 if (endstr == NULL ||
1317 strcmp(endstr, ".0") == 0 ||
1318 strncmp(host_address, "0.", 2) == 0 ||
1319 strstr(host_address, ".0.") != NULL || strstr(host_address, "255") != NULL) {
1320 CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed"); //LCOV_EXCL_LINE
1321 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1324 rv = net_remove_route(host_address, interface_name, address_family);
1325 if (rv == NET_ERR_ACCESS_DENIED) {
1326 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1327 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1328 } else if (rv != NET_ERR_NONE)
1329 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1331 return CONNECTION_ERROR_NONE;
1334 int _connection_libnet_add_route_ipv6(const char *interface_name, const char *host_address, const char *gateway)
1337 int address_family = 0;
1339 address_family = AF_INET6;
1341 if (strncmp(host_address, "fe80:", 5) == 0 ||
1342 strncmp(host_address, "ff00:", 5) == 0 ||
1343 strncmp(host_address, "::", 2) == 0) {
1344 CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed\n"); //LCOV_EXCL_LINE
1345 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1348 rv = net_add_route_ipv6(host_address, interface_name, address_family, gateway);
1349 if (rv == NET_ERR_ACCESS_DENIED) {
1350 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1351 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1352 } else if (rv != NET_ERR_NONE)
1353 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1355 return CONNECTION_ERROR_NONE;
1358 int _connection_libnet_remove_route_ipv6(const char *interface_name, const char *host_address, const char *gateway)
1361 int address_family = 0;
1363 address_family = AF_INET6;
1365 if (strncmp(host_address, "fe80:", 5) == 0 ||
1366 strncmp(host_address, "ff00:", 5) == 0 ||
1367 strncmp(host_address, "::", 2) == 0) {
1368 CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed\n"); //LCOV_EXCL_LINE
1369 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1372 rv = net_remove_route_ipv6(host_address, interface_name, address_family, gateway);
1373 if (rv == NET_ERR_ACCESS_DENIED) {
1374 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1375 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1376 } else if (rv != NET_ERR_NONE)
1377 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1379 return CONNECTION_ERROR_NONE;
1382 int _connection_libnet_add_route_entry(connection_address_family_e address_family,
1383 const char *interface_name, const char *host_address, const char *gateway)
1386 char *endstr = NULL;
1387 int address_family_type = 0;
1389 if (address_family == CONNECTION_ADDRESS_FAMILY_IPV4)
1390 address_family_type = AF_INET;
1392 address_family_type = AF_INET6;
1394 if (address_family == CONNECTION_ADDRESS_FAMILY_IPV4) {
1396 endstr = strrchr(host_address, '.');
1397 if (endstr == NULL ||
1398 strcmp(endstr, ".0") == 0 ||
1399 strncmp(host_address, "0.", 2) == 0 ||
1400 strstr(host_address, "255") != NULL) {
1401 CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed\n"); //LCOV_EXCL_LINE
1402 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1405 rv = net_add_route_entry(host_address, interface_name, address_family_type, gateway);
1406 if (rv == NET_ERR_ACCESS_DENIED) {
1407 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1408 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1409 } else if (rv != NET_ERR_NONE)
1410 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1414 if (strncmp(host_address, "fe80:", 5) == 0 ||
1415 strncmp(host_address, "ff00:", 5) == 0 ||
1416 strncmp(host_address, "::", 2) == 0) {
1417 CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed\n"); //LCOV_EXCL_LINE
1418 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1421 rv = net_add_route_ipv6(host_address, interface_name, address_family_type, gateway);
1422 if (rv == NET_ERR_ACCESS_DENIED) {
1423 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1424 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1425 } else if (rv != NET_ERR_NONE)
1426 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1429 return CONNECTION_ERROR_NONE;
1432 int _connection_libnet_remove_route_entry(connection_address_family_e address_family,
1433 const char *interface_name, const char *host_address, const char *gateway)
1436 char *endstr = strrchr(host_address, '.');
1437 int address_family_type = 0;
1439 if (address_family == CONNECTION_ADDRESS_FAMILY_IPV4)
1440 address_family_type = AF_INET;
1442 address_family_type = AF_INET6;
1444 if (address_family == CONNECTION_ADDRESS_FAMILY_IPV4) {
1445 endstr = strrchr(host_address, '.');
1446 if (endstr == NULL ||
1447 strcmp(endstr, ".0") == 0 ||
1448 strncmp(host_address, "0.", 2) == 0 ||
1449 strstr(host_address, ".0.") != NULL || strstr(host_address, "255") != NULL) {
1450 CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed"); //LCOV_EXCL_LINE
1451 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1454 rv = net_remove_route_entry(host_address, interface_name, address_family_type, gateway);
1455 if (rv == NET_ERR_ACCESS_DENIED) {
1456 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1457 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1458 } else if (rv != NET_ERR_NONE)
1459 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1463 if (strncmp(host_address, "fe80:", 5) == 0 ||
1464 strncmp(host_address, "ff00:", 5) == 0 ||
1465 strncmp(host_address, "::", 2) == 0) {
1466 CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed\n"); //LCOV_EXCL_LINE
1467 return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1470 rv = net_remove_route_ipv6(host_address, interface_name, address_family_type, gateway);
1471 if (rv == NET_ERR_ACCESS_DENIED) {
1472 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1473 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1474 } else if (rv != NET_ERR_NONE)
1475 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1478 return CONNECTION_ERROR_NONE;
1481 void _connection_libnet_add_to_profile_list(connection_profile_h profile)
1483 prof_handle_list = g_slist_append(prof_handle_list, profile);
1486 void _connection_libnet_remove_from_profile_list(connection_profile_h profile)
1488 prof_handle_list = g_slist_remove(prof_handle_list, profile);
1492 bool _connection_libnet_add_to_profile_cb_list(connection_profile_h profile,
1493 connection_profile_state_changed_cb callback, void *user_data)
1495 net_profile_info_t *profile_info = profile;
1496 char *profile_name = g_strdup(profile_info->ProfileName);
1498 struct _profile_cb_s *profile_cb_info = g_try_malloc0(sizeof(struct _profile_cb_s));
1499 if (profile_cb_info == NULL) {
1500 g_free(profile_name); //LCOV_EXCL_LINE
1501 return false; //LCOV_EXCL_LINE
1504 profile_cb_info->callback = callback;
1505 profile_cb_info->user_data = user_data;
1506 profile_cb_info->state = _profile_convert_to_cp_state(profile_info->ProfileState);
1508 g_hash_table_replace(profile_cb_table, profile_name, profile_cb_info);
1513 bool _connection_libnet_remove_from_profile_cb_list(connection_profile_h profile)
1515 net_profile_info_t *profile_info = profile;
1517 if (g_hash_table_remove(profile_cb_table, profile_info->ProfileName) == TRUE)
1520 return false; //LCOV_EXCL_LINE
1523 int _connection_libnet_set_statistics(net_device_t device_type, net_statistics_type_e statistics_type)
1526 rv = net_set_statistics(device_type, statistics_type);
1527 if (rv == NET_ERR_ACCESS_DENIED) {
1528 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1529 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1530 } else if (rv != NET_ERR_NONE)
1531 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1533 return CONNECTION_ERROR_NONE;
1536 int _connection_libnet_get_statistics(net_statistics_type_e statistics_type, unsigned long long *size)
1539 rv = net_get_statistics(NET_DEVICE_WIFI, statistics_type, size);
1540 if (rv == NET_ERR_ACCESS_DENIED) {
1541 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1542 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1543 } else if (rv != NET_ERR_NONE)
1544 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1546 return CONNECTION_ERROR_NONE;
1549 int _connection_libnet_set_cellular_subscriber_id(connection_profile_h profile,
1550 connection_cellular_subscriber_id_e sim_id)
1552 char *modem_path = NULL;
1553 net_profile_info_t *profile_info = (net_profile_info_t *)profile;
1555 if (net_get_cellular_modem_object_path(&modem_path, sim_id) != NET_ERR_NONE) {
1556 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get subscriber[%d]", sim_id); //LCOV_EXCL_LINE
1557 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1561 CONNECTION_LOG(CONNECTION_ERROR, "NULL modem object path"); //LCOV_EXCL_LINE
1562 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1565 g_strlcpy(profile_info->ProfileInfo.Pdp.PSModemPath, modem_path,
1566 NET_PROFILE_NAME_LEN_MAX);
1569 return CONNECTION_ERROR_NONE;
1572 static void __connection_idle_destroy_cb(gpointer data)
1577 managed_idler_list = g_slist_remove(managed_idler_list, data);
1581 static gboolean __connection_idle_cb(gpointer user_data)
1583 struct managed_idle_data *data = (struct managed_idle_data *)user_data;
1588 return data->func(data->user_data);
1591 guint _connection_callback_add(GSourceFunc func, gpointer user_data)
1594 struct managed_idle_data *data;
1595 GMainContext *context;
1601 data = g_try_new0(struct managed_idle_data, 1);
1606 data->user_data = user_data;
1608 context = g_main_context_get_thread_default();
1609 src = g_idle_source_new();
1610 g_source_set_callback(src, __connection_idle_cb, data,
1611 __connection_idle_destroy_cb);
1612 id = g_source_attach(src, context);
1613 g_source_unref(src);
1621 managed_idler_list = g_slist_append(managed_idler_list, data);
1626 void _connection_callback_cleanup(void)
1628 GSList *cur = managed_idler_list;
1630 struct managed_idle_data *data;
1634 GSList *next = cur->next;
1635 data = (struct managed_idle_data *)cur->data;
1637 src = g_main_context_find_source_by_id(g_main_context_default(), data->id);
1639 g_source_destroy(src);
1640 cur = managed_idler_list;
1646 g_slist_free(managed_idler_list);
1647 managed_idler_list = NULL;
1650 int _connection_libnet_check_get_privilege()
1654 rv = net_check_get_privilege();
1655 if (rv == NET_ERR_ACCESS_DENIED) {
1656 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1657 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1658 } else if (rv != NET_ERR_NONE)
1659 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1661 return CONNECTION_ERROR_NONE;
1664 int _connection_libnet_check_profile_privilege()
1668 rv = net_check_profile_privilege();
1669 if (rv == NET_ERR_ACCESS_DENIED) {
1670 CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
1671 return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1672 } else if (rv != NET_ERR_NONE)
1673 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1675 return CONNECTION_ERROR_NONE;
1678 bool __libnet_check_feature_supported(const char *key, connection_supported_feature_e feature)
1680 if (!connection_is_feature_checked[feature]) {
1681 if (system_info_get_platform_bool(key, &connection_feature_supported[feature]) < 0) {
1682 CONNECTION_LOG(CONNECTION_ERROR, "Error - Feature getting from System Info"); //LCOV_EXCL_LINE
1683 set_last_result(CONNECTION_ERROR_OPERATION_FAILED); //LCOV_EXCL_LINE
1684 return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
1686 connection_is_feature_checked[feature] = true;
1688 return connection_feature_supported[feature];
1691 int _connection_check_feature_supported(const char *feature_name, ...)
1696 bool feature_supported = false;
1698 va_start(list, feature_name);
1701 if (strcmp(key, TELEPHONY_FEATURE) == 0)
1702 value = __libnet_check_feature_supported(key, CONNECTION_SUPPORTED_FEATURE_TELEPHONY);
1703 if (strcmp(key, WIFI_FEATURE) == 0)
1704 value = __libnet_check_feature_supported(key, CONNECTION_SUPPORTED_FEATURE_WIFI);
1705 if (strcmp(key, TETHERING_BLUETOOTH_FEATURE) == 0)
1706 value = __libnet_check_feature_supported(key, CONNECTION_SUPPORTED_FEATURE_TETHERING_BLUETOOTH);
1707 if (strcmp(key, ETHERNET_FEATURE) == 0)
1708 value = __libnet_check_feature_supported(key, CONNECTION_SUPPORTED_FEATURE_ETHERNET);
1710 feature_supported |= value;
1711 key = va_arg(list, const char *);
1714 if (!feature_supported) {
1715 CONNECTION_LOG(CONNECTION_ERROR, "Error - Feature is not supported");
1716 set_last_result(CONNECTION_ERROR_NOT_SUPPORTED);
1718 return CONNECTION_ERROR_NOT_SUPPORTED;
1722 set_last_result(CONNECTION_ERROR_NONE);
1723 return CONNECTION_ERROR_NONE;
1726 int _connection_libnet_start_tcpdump(void)
1728 connection_error_e result = CONNECTION_ERROR_NONE;
1729 net_err_t ret = NET_ERR_NONE;
1731 ret = net_start_tcpdump();
1732 result = __libnet_convert_to_cp_error_type(ret);
1737 int _connection_libnet_stop_tcpdump(void)
1739 connection_error_e result = CONNECTION_ERROR_NONE;
1740 net_err_t ret = NET_ERR_NONE;
1742 ret = net_stop_tcpdump();
1743 result = __libnet_convert_to_cp_error_type(ret);
1748 int _connection_libnet_get_tcpdump_state(gboolean *tcpdump_state)
1750 connection_error_e result = CONNECTION_ERROR_NONE;
1751 net_err_t ret = NET_ERR_NONE;
1753 ret = net_get_tcpdump_state(tcpdump_state);
1754 result = __libnet_convert_to_cp_error_type(ret);