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.
20 #include <vconf/vconf.h>
21 #include <arpa/inet.h>
23 #include "net_connection_private.h"
25 static GSList *prof_handle_list = NULL;
26 static GHashTable *profile_cb_table = NULL;
28 struct _profile_cb_s {
29 connection_profile_state_changed_cb callback;
30 connection_profile_state_e state;
34 struct _profile_list_s {
37 net_profile_info_t *profiles;
41 connection_opened_cb opened_cb;
42 connection_closed_cb closed_cb;
43 connection_set_default_cb set_default_cb;
44 connection_reset_cb reset_profile_cb;
45 libnet_ethernet_cable_state_changed_cb ethernet_cable_state_changed_cb;
46 void *opened_user_data;
47 void *closed_user_data;
48 void *set_default_user_data;
49 void *reset_profile_user_data;
54 struct managed_idle_data {
63 bool tethering_bluetooth;
66 static struct _profile_list_s profile_iterator = {0, 0, NULL};
67 static struct _libnet_s libnet = {NULL, NULL, NULL, NULL, NULL, NULL, false};
68 static __thread GSList *managed_idler_list = NULL;
69 static __thread bool is_check_enable_feature = false;
70 static __thread struct feature_type enable_feature = {false, false, false};
72 bool _connection_is_created(void)
74 return libnet.is_created;
77 static connection_error_e __libnet_convert_to_cp_error_type(net_err_t err_type)
81 return CONNECTION_ERROR_NONE;
82 case NET_ERR_APP_ALREADY_REGISTERED:
83 return CONNECTION_ERROR_INVALID_OPERATION;
84 case NET_ERR_APP_NOT_REGISTERED:
85 return CONNECTION_ERROR_INVALID_OPERATION;
86 case NET_ERR_NO_ACTIVE_CONNECTIONS:
87 return CONNECTION_ERROR_NO_CONNECTION;
88 case NET_ERR_ACTIVE_CONNECTION_EXISTS:
89 return CONNECTION_ERROR_ALREADY_EXISTS;
90 case NET_ERR_CONNECTION_DHCP_FAILED:
91 return CONNECTION_ERROR_DHCP_FAILED;
92 case NET_ERR_CONNECTION_INVALID_KEY:
93 return CONNECTION_ERROR_INVALID_KEY;
94 case NET_ERR_IN_PROGRESS:
95 return CONNECTION_ERROR_NOW_IN_PROGRESS;
96 case NET_ERR_OPERATION_ABORTED:
97 return CONNECTION_ERROR_OPERATION_ABORTED;
98 case NET_ERR_TIME_OUT:
99 return CONNECTION_ERROR_NO_REPLY;
100 case NET_ERR_ACCESS_DENIED:
101 return CONNECTION_ERROR_PERMISSION_DENIED;
103 return CONNECTION_ERROR_OPERATION_FAILED;
107 static const char *__libnet_convert_cp_error_type_to_string(connection_error_e err_type)
110 case CONNECTION_ERROR_NONE:
112 case CONNECTION_ERROR_INVALID_PARAMETER:
113 return "INVALID_PARAMETER";
114 case CONNECTION_ERROR_OUT_OF_MEMORY:
115 return "OUT_OF_MEMORY";
116 case CONNECTION_ERROR_INVALID_OPERATION:
117 return "INVALID_OPERATION";
118 case CONNECTION_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED:
119 return "ADDRESS_FAMILY_NOT_SUPPORTED";
120 case CONNECTION_ERROR_OPERATION_FAILED:
121 return "OPERATION_FAILED";
122 case CONNECTION_ERROR_ITERATOR_END:
123 return "ITERATOR_END";
124 case CONNECTION_ERROR_NO_CONNECTION:
125 return "NO_CONNECTION";
126 case CONNECTION_ERROR_NOW_IN_PROGRESS:
127 return "NOW_IN_PROGRESS";
128 case CONNECTION_ERROR_ALREADY_EXISTS:
129 return "ALREADY_EXISTS";
130 case CONNECTION_ERROR_OPERATION_ABORTED:
131 return "OPERATION_ABORTED";
132 case CONNECTION_ERROR_DHCP_FAILED:
133 return "DHCP_FAILED";
134 case CONNECTION_ERROR_INVALID_KEY:
135 return "INVALID_KEY";
136 case CONNECTION_ERROR_NO_REPLY:
138 case CONNECTION_ERROR_PERMISSION_DENIED:
139 return "PERMISSION_DENIED";
140 case CONNECTION_ERROR_NOT_SUPPORTED:
141 return "NOT_SUPPORTED";
147 static const char *__libnet_convert_cp_state_to_string(connection_profile_state_e state)
150 case CONNECTION_PROFILE_STATE_DISCONNECTED:
151 return "DISCONNECTED";
152 case CONNECTION_PROFILE_STATE_ASSOCIATION:
153 return "ASSOCIATION";
154 case CONNECTION_PROFILE_STATE_CONFIGURATION:
155 return "CONFIGURATION";
156 case CONNECTION_PROFILE_STATE_CONNECTED:
163 static void __libnet_set_reset_profile_cb(connection_opened_cb user_cb, void *user_data)
165 if (user_cb != NULL) {
166 libnet.reset_profile_cb = user_cb;
167 libnet.reset_profile_user_data = user_data;
171 static void __libnet_set_opened_cb(connection_opened_cb user_cb, void *user_data)
174 libnet.opened_cb = user_cb;
175 libnet.opened_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_opened_cb(connection_error_e result)
206 if (libnet.opened_cb)
207 libnet.opened_cb(result, libnet.opened_user_data);
209 libnet.opened_cb = NULL;
210 libnet.opened_user_data = NULL;
213 static void __libnet_set_closed_cb(connection_closed_cb user_cb, void *user_data)
216 libnet.closed_cb = user_cb;
217 libnet.closed_user_data = user_data;
221 static void __libnet_closed_cb(connection_error_e result)
223 if (libnet.closed_cb)
224 libnet.closed_cb(result, libnet.closed_user_data);
226 libnet.closed_cb = NULL;
227 libnet.closed_user_data = NULL;
230 static void __libnet_set_default_cb(connection_set_default_cb user_cb, void *user_data)
233 libnet.set_default_cb = user_cb;
234 libnet.set_default_user_data = user_data;
238 static void __libnet_default_cb(connection_error_e result)
240 if (libnet.set_default_cb)
241 libnet.set_default_cb(result, libnet.set_default_user_data);
243 libnet.set_default_cb = NULL;
244 libnet.set_default_user_data = NULL;
247 static void __libnet_set_ethernet_cable_state_changed_cb(
248 libnet_ethernet_cable_state_changed_cb user_cb)
250 libnet.ethernet_cable_state_changed_cb = user_cb;
253 static void __libnet_ethernet_cable_state_changed_cb(
254 connection_ethernet_cable_state_e state)
256 if (libnet.ethernet_cable_state_changed_cb)
257 libnet.ethernet_cable_state_changed_cb(state);
260 static void __libnet_state_changed_cb(char *profile_name, connection_profile_state_e state)
262 if (profile_name == NULL)
265 struct _profile_cb_s *cb_info;
266 cb_info = g_hash_table_lookup(profile_cb_table, profile_name);
271 if (cb_info->state == state)
274 cb_info->state = state;
276 if (state >= 0 && cb_info->callback)
277 cb_info->callback(state, cb_info->user_data);
280 static void __libnet_clear_profile_list(struct _profile_list_s *profile_list)
282 if (profile_list->count > 0)
283 g_free(profile_list->profiles);
285 profile_list->count = 0;
286 profile_list->next = 0;
287 profile_list->profiles = NULL;
290 static void __libnet_evt_cb(net_event_info_t* event_cb, void* user_data)
292 bool is_requested = false;
293 connection_error_e result = CONNECTION_ERROR_NONE;
295 switch (event_cb->Event) {
296 case NET_EVENT_OPEN_RSP:
299 case NET_EVENT_OPEN_IND:
300 result = __libnet_convert_to_cp_error_type(event_cb->Error);
301 CONNECTION_LOG(CONNECTION_INFO, "Got connection open %s : %s\n",
302 (is_requested) ? "RSP":"IND",
303 __libnet_convert_cp_error_type_to_string(result));
306 __libnet_opened_cb(result);
308 switch (event_cb->Error) {
310 case NET_ERR_ACTIVE_CONNECTION_EXISTS:
311 CONNECTION_LOG(CONNECTION_INFO, "'Open connection' succeeded\n");
313 __libnet_state_changed_cb(event_cb->ProfileName, CONNECTION_PROFILE_STATE_CONNECTED);
316 CONNECTION_LOG(CONNECTION_ERROR, "'Open connection' failed!! [%s]\n",
317 __libnet_convert_cp_error_type_to_string(result));
320 __libnet_state_changed_cb(event_cb->ProfileName, CONNECTION_PROFILE_STATE_DISCONNECTED);
323 case NET_EVENT_CLOSE_RSP:
326 case NET_EVENT_CLOSE_IND:
327 result = __libnet_convert_to_cp_error_type(event_cb->Error);
328 CONNECTION_LOG(CONNECTION_INFO, "Got connection close %s : %s\n",
329 (is_requested) ? "RSP":"IND",
330 __libnet_convert_cp_error_type_to_string(result));
333 __libnet_closed_cb(result);
335 switch (event_cb->Error) {
337 CONNECTION_LOG(CONNECTION_INFO, "'Close connection' succeeded!\n");
339 __libnet_state_changed_cb(event_cb->ProfileName, CONNECTION_PROFILE_STATE_DISCONNECTED);
342 CONNECTION_LOG(CONNECTION_ERROR, "'Close connection' failed!! [%s]\n",
343 __libnet_convert_cp_error_type_to_string(result));
347 case NET_EVENT_NET_STATE_IND:
348 CONNECTION_LOG(CONNECTION_INFO, "Got State changed IND\n");
350 if (event_cb->Datalength != sizeof(net_state_type_t))
353 net_state_type_t *profile_state = (net_state_type_t*)event_cb->Data;
354 connection_profile_state_e cp_state = _profile_convert_to_cp_state(*profile_state);
356 CONNECTION_LOG(CONNECTION_INFO,
357 "Profile State : %s, profile name : %s\n",
358 __libnet_convert_cp_state_to_string(cp_state),
359 event_cb->ProfileName);
361 __libnet_state_changed_cb(event_cb->ProfileName, cp_state);
364 case NET_EVENT_WIFI_SCAN_IND:
365 case NET_EVENT_WIFI_SCAN_RSP:
366 CONNECTION_LOG(CONNECTION_INFO, "Got wifi scan IND\n");
368 case NET_EVENT_WIFI_POWER_IND:
369 case NET_EVENT_WIFI_POWER_RSP:
370 CONNECTION_LOG(CONNECTION_INFO, "Got wifi power IND\n");
372 case NET_EVENT_CELLULAR_SET_DEFAULT_RSP:
373 result = __libnet_convert_to_cp_error_type(event_cb->Error);
374 CONNECTION_LOG(CONNECTION_INFO, "Got set default profile RSP %d\n", result);
375 __libnet_default_cb(result);
377 case NET_EVENT_WIFI_WPS_RSP:
378 CONNECTION_LOG(CONNECTION_INFO, "Got wifi WPS RSP\n");
380 case NET_EVENT_CELLULAR_RESET_DEFAULT_RSP:
381 result = __libnet_convert_to_cp_error_type(event_cb->Error);
382 CONNECTION_LOG(CONNECTION_INFO, "Got reset default profile RSP %d", result);
383 __libnet_reset_profile_cb(result);
385 case NET_EVENT_ETHERNET_CABLE_ATTACHED:
386 CONNECTION_LOG(CONNECTION_INFO, "Got Ethernet cable Attached Indication\n");
387 __libnet_ethernet_cable_state_changed_cb(CONNECTION_ETHERNET_CABLE_ATTACHED);
389 case NET_EVENT_ETHERNET_CABLE_DETACHED:
390 CONNECTION_LOG(CONNECTION_INFO, "Got Ethernet cable detached Indication\n");
391 __libnet_ethernet_cable_state_changed_cb(CONNECTION_ETHERNET_CABLE_DETACHED);
394 CONNECTION_LOG(CONNECTION_ERROR, "Error! Unknown Event\n\n");
399 static int __libnet_check_address_type(int address_family, const char *address)
404 err = inet_pton(address_family, address, &buf);
411 int __libnet_get_connected_count(struct _profile_list_s *profile_list)
416 for (;i < profile_list->count;i++) {
417 if (profile_list->profiles[i].ProfileState == NET_STATE_TYPE_ONLINE ||
418 profile_list->profiles[i].ProfileState == NET_STATE_TYPE_READY)
425 void __libnet_copy_connected_profile(net_profile_info_t **dest, struct _profile_list_s *source)
429 for (;i < source->count;i++) {
430 if (source->profiles[i].ProfileState == NET_STATE_TYPE_ONLINE ||
431 source->profiles[i].ProfileState == NET_STATE_TYPE_READY) {
432 memcpy(*dest, &source->profiles[i], sizeof(net_profile_info_t));
438 int _connection_libnet_init(void)
442 if (!libnet.registered) {
443 rv = net_register_client_ext((net_event_cb_t)__libnet_evt_cb, NET_DEVICE_DEFAULT, NULL);
444 if (rv != NET_ERR_NONE)
447 libnet.registered = true;
449 if (profile_cb_table == NULL)
450 profile_cb_table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
456 bool _connection_libnet_deinit(void)
458 if (libnet.registered) {
459 if (net_deregister_client_ext(NET_DEVICE_DEFAULT) != NET_ERR_NONE)
462 libnet.registered = false;
464 if (profile_cb_table) {
465 g_hash_table_destroy(profile_cb_table);
466 profile_cb_table = NULL;
469 __libnet_clear_profile_list(&profile_iterator);
471 if (prof_handle_list) {
472 g_slist_free_full(prof_handle_list, g_free);
473 prof_handle_list = NULL;
480 bool _connection_libnet_check_profile_validity(connection_profile_h profile)
485 for (list = prof_handle_list; list; list = list->next)
486 if (profile == list->data) return true;
488 for (;i < profile_iterator.count;i++)
489 if (profile == &profile_iterator.profiles[i]) return true;
494 bool _connection_libnet_check_profile_cb_validity(connection_profile_h profile)
496 struct _profile_cb_s *cb_info;
497 net_profile_info_t *profile_info = profile;
502 cb_info = g_hash_table_lookup(profile_cb_table, profile_info->ProfileName);
510 int _connection_libnet_get_wifi_state(connection_wifi_state_e *state)
513 net_wifi_state_t wlan_state;
514 net_profile_name_t profile_name;
516 rv = net_get_wifi_state(&wlan_state, &profile_name);
517 if (rv == NET_ERR_ACCESS_DENIED) {
518 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
519 return CONNECTION_ERROR_PERMISSION_DENIED;
520 } else if (rv != NET_ERR_NONE) {
521 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get Wi-Fi state[%d]", rv);
522 return CONNECTION_ERROR_OPERATION_FAILED;
525 switch (wlan_state) {
527 *state = CONNECTION_WIFI_STATE_DEACTIVATED;
530 case WIFI_CONNECTING:
531 *state = CONNECTION_WIFI_STATE_DISCONNECTED;
534 case WIFI_DISCONNECTING:
535 *state = CONNECTION_WIFI_STATE_CONNECTED;
538 CONNECTION_LOG(CONNECTION_ERROR, "Error!! Unknown state\n");
539 return CONNECTION_ERROR_INVALID_OPERATION;
542 return CONNECTION_ERROR_NONE;
545 int _connection_libnet_get_ethernet_state(connection_ethernet_state_e* state)
548 struct _profile_list_s ethernet_profiles = {0, 0, NULL};
549 rv = net_get_profile_list(NET_DEVICE_ETHERNET, ðernet_profiles.profiles, ðernet_profiles.count);
550 if (rv == NET_ERR_ACCESS_DENIED) {
551 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
552 return CONNECTION_ERROR_PERMISSION_DENIED;
555 if (ethernet_profiles.count == 0) {
556 state = CONNECTION_ETHERNET_STATE_DEACTIVATED;
557 return CONNECTION_ERROR_NONE;
560 switch (ethernet_profiles.profiles->ProfileState) {
561 case NET_STATE_TYPE_ONLINE:
562 case NET_STATE_TYPE_READY:
563 *state = CONNECTION_ETHERNET_STATE_CONNECTED;
565 case NET_STATE_TYPE_IDLE:
566 case NET_STATE_TYPE_FAILURE:
567 case NET_STATE_TYPE_ASSOCIATION:
568 case NET_STATE_TYPE_CONFIGURATION:
569 case NET_STATE_TYPE_DISCONNECT:
570 *state = CONNECTION_ETHERNET_STATE_DISCONNECTED;
573 return CONNECTION_ERROR_OPERATION_FAILED;
576 __libnet_clear_profile_list(ðernet_profiles);
578 return CONNECTION_ERROR_NONE;
581 int _connection_libnet_get_ethernet_cable_state(connection_ethernet_cable_state_e* state)
586 rv = net_get_ethernet_cable_state(&status);
587 if (rv == NET_ERR_ACCESS_DENIED) {
588 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
589 return CONNECTION_ERROR_PERMISSION_DENIED;
590 } else if (rv != NET_ERR_NONE) {
591 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get ethernet cable state[%d]", rv);
592 return CONNECTION_ERROR_OPERATION_FAILED;
596 *state = CONNECTION_ETHERNET_CABLE_ATTACHED;
598 *state = CONNECTION_ETHERNET_CABLE_DETACHED;
599 return CONNECTION_ERROR_NONE;
602 int _connection_libnet_set_ethernet_cable_state_changed_cb(
603 libnet_ethernet_cable_state_changed_cb callback)
605 __libnet_set_ethernet_cable_state_changed_cb(callback);
607 return CONNECTION_ERROR_NONE;
610 int _connection_libnet_get_bluetooth_state(connection_bt_state_e* state)
614 struct _profile_list_s bluetooth_profiles = {0, 0, NULL};
615 rv = net_get_profile_list(NET_DEVICE_BLUETOOTH, &bluetooth_profiles.profiles, &bluetooth_profiles.count);
616 if (rv == NET_ERR_ACCESS_DENIED) {
617 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
618 return CONNECTION_ERROR_PERMISSION_DENIED;
621 if (bluetooth_profiles.count == 0) {
622 *state = CONNECTION_BT_STATE_DEACTIVATED;
623 return CONNECTION_ERROR_NONE;
626 for (; i < bluetooth_profiles.count; i++) {
627 switch (bluetooth_profiles.profiles[i].ProfileState) {
628 case NET_STATE_TYPE_ONLINE:
629 case NET_STATE_TYPE_READY:
630 *state = CONNECTION_BT_STATE_CONNECTED;
632 case NET_STATE_TYPE_IDLE:
633 case NET_STATE_TYPE_FAILURE:
634 case NET_STATE_TYPE_ASSOCIATION:
635 case NET_STATE_TYPE_CONFIGURATION:
636 case NET_STATE_TYPE_DISCONNECT:
637 *state = CONNECTION_BT_STATE_DISCONNECTED;
640 __libnet_clear_profile_list(&bluetooth_profiles);
641 return CONNECTION_ERROR_OPERATION_FAILED;
646 __libnet_clear_profile_list(&bluetooth_profiles);
648 return CONNECTION_ERROR_NONE;
651 int _connection_libnet_get_profile_iterator(connection_iterator_type_e type, connection_profile_iterator_h* profile_iter_h)
655 net_profile_info_t *profiles = NULL;
657 struct _profile_list_s all_profiles = {0, 0, NULL};
659 __libnet_clear_profile_list(&profile_iterator);
661 rv = net_get_profile_list(NET_DEVICE_MAX, &all_profiles.profiles, &all_profiles.count);
663 if (rv != NET_ERR_NONE) {
664 if (rv == NET_ERR_NO_SERVICE) {
665 *profile_iter_h = &profile_iterator;
666 return CONNECTION_ERROR_NONE;
668 return CONNECTION_ERROR_OPERATION_FAILED;
671 *profile_iter_h = &profile_iterator;
674 case CONNECTION_ITERATOR_TYPE_REGISTERED:
675 count = all_profiles.count;
676 CONNECTION_LOG(CONNECTION_INFO, "Total profile count : %d\n", count);
679 return CONNECTION_ERROR_NONE;
681 profile_iterator.profiles = all_profiles.profiles;
684 case CONNECTION_ITERATOR_TYPE_CONNECTED:
685 count = __libnet_get_connected_count(&all_profiles);
686 CONNECTION_LOG(CONNECTION_INFO, "Total connected profile count : %d\n", count);
689 return CONNECTION_ERROR_NONE;
691 profiles = g_try_new0(net_profile_info_t, count);
692 if (profiles == NULL) {
693 __libnet_clear_profile_list(&all_profiles);
694 return CONNECTION_ERROR_OUT_OF_MEMORY;
696 case CONNECTION_ITERATOR_TYPE_DEFAULT:
697 /* To do : Not supported yet */
701 profile_iterator.profiles = profiles;
703 __libnet_copy_connected_profile(&profiles, &all_profiles);
705 __libnet_clear_profile_list(&all_profiles);
708 profile_iterator.count = count;
710 return CONNECTION_ERROR_NONE;
713 int _connection_libnet_get_iterator_next(connection_profile_iterator_h profile_iter_h, connection_profile_h *profile)
715 if (profile_iter_h != &profile_iterator)
716 return CONNECTION_ERROR_INVALID_PARAMETER;
718 if (profile_iterator.count <= profile_iterator.next)
719 return CONNECTION_ERROR_ITERATOR_END;
721 *profile = &profile_iterator.profiles[profile_iterator.next];
722 profile_iterator.next++;
724 return CONNECTION_ERROR_NONE;
727 bool _connection_libnet_iterator_has_next(connection_profile_iterator_h profile_iter_h)
729 if (profile_iter_h != &profile_iterator)
732 if (profile_iterator.count <= profile_iterator.next)
738 int _connection_libnet_destroy_iterator(connection_profile_iterator_h profile_iter_h)
740 if (profile_iter_h != &profile_iterator)
741 return CONNECTION_ERROR_INVALID_PARAMETER;
743 __libnet_clear_profile_list(&profile_iterator);
745 return CONNECTION_ERROR_NONE;
748 int _connection_libnet_get_current_profile(connection_profile_h *profile)
750 net_profile_info_t active_profile;
753 rv = net_get_active_net_info(&active_profile);
754 if (rv == NET_ERR_NO_SERVICE)
755 return CONNECTION_ERROR_NO_CONNECTION;
756 else if (rv == NET_ERR_ACCESS_DENIED) {
757 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
758 return CONNECTION_ERROR_PERMISSION_DENIED;
759 } else if (rv != NET_ERR_NONE)
760 return CONNECTION_ERROR_OPERATION_FAILED;
762 *profile = g_try_malloc0(sizeof(net_profile_info_t));
763 if (*profile == NULL)
764 return CONNECTION_ERROR_OUT_OF_MEMORY;
766 memcpy(*profile, &active_profile, sizeof(net_profile_info_t));
767 prof_handle_list = g_slist_append(prof_handle_list, *profile);
769 return CONNECTION_ERROR_NONE;
772 int _connection_libnet_reset_profile(connection_reset_option_e type,
773 connection_cellular_subscriber_id_e id, connection_reset_cb callback, void *user_data)
777 rv = net_reset_profile(type, id);
778 if (rv == NET_ERR_ACCESS_DENIED) {
779 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
780 return CONNECTION_ERROR_PERMISSION_DENIED;
781 } else if (rv != NET_ERR_NONE) {
782 CONNECTION_LOG(CONNECTION_ERROR, "Failed to add profile[%d]", rv);
783 return CONNECTION_ERROR_OPERATION_FAILED;
786 __libnet_set_reset_profile_cb(callback, user_data);
788 return CONNECTION_ERROR_NONE;
791 int _connection_libnet_open_profile(connection_profile_h profile, connection_opened_cb callback, void* user_data)
795 if (!(_connection_libnet_check_profile_validity(profile))) {
796 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
797 return CONNECTION_ERROR_INVALID_PARAMETER;
800 net_profile_info_t *profile_info = profile;
802 rv = net_open_connection_with_profile(profile_info->ProfileName);
803 if (rv == NET_ERR_ACCESS_DENIED) {
804 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
805 return CONNECTION_ERROR_PERMISSION_DENIED;
806 } else if (rv != NET_ERR_NONE)
807 return CONNECTION_ERROR_OPERATION_FAILED;
809 __libnet_set_opened_cb(callback, user_data);
811 return CONNECTION_ERROR_NONE;
814 int _connection_libnet_get_cellular_service_profile(connection_cellular_service_type_e type, connection_profile_h *profile)
818 int rv = NET_ERR_NONE;
819 net_service_type_t service_type = _connection_profile_convert_to_libnet_cellular_service_type(type);
821 struct _profile_list_s cellular_profiles = {0, 0, NULL};
823 rv = net_get_profile_list(NET_DEVICE_CELLULAR, &cellular_profiles.profiles, &cellular_profiles.count);
824 if (rv == NET_ERR_ACCESS_DENIED) {
825 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
826 return CONNECTION_ERROR_PERMISSION_DENIED;
827 } else if (rv != NET_ERR_NONE) {
828 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get profile list (%d)", rv);
829 return CONNECTION_ERROR_OPERATION_FAILED;
832 for (;i < cellular_profiles.count;i++)
833 if (cellular_profiles.profiles[i].ProfileInfo.Pdp.ServiceType == service_type)
836 if (i >= cellular_profiles.count)
837 return CONNECTION_ERROR_OPERATION_FAILED;
839 *profile = g_try_malloc0(sizeof(net_profile_info_t));
840 if (*profile == NULL)
841 return CONNECTION_ERROR_OUT_OF_MEMORY;
843 memcpy(*profile, &cellular_profiles.profiles[i], sizeof(net_profile_info_t));
845 if (cellular_profiles.profiles[i].ProfileInfo.Pdp.DefaultConn)
848 if (type != CONNECTION_CELLULAR_SERVICE_TYPE_INTERNET &&
849 type != CONNECTION_CELLULAR_SERVICE_TYPE_PREPAID_INTERNET)
852 for (;j < cellular_profiles.count;j++) {
856 if (cellular_profiles.profiles[j].ProfileInfo.Pdp.ServiceType != service_type)
859 if (cellular_profiles.profiles[j].ProfileInfo.Pdp.DefaultConn) {
860 memcpy(*profile, &cellular_profiles.profiles[j], sizeof(net_profile_info_t));
866 prof_handle_list = g_slist_append(prof_handle_list, *profile);
868 return CONNECTION_ERROR_NONE;
871 int _connection_libnet_set_cellular_service_profile_sync(connection_cellular_service_type_e type, connection_profile_h profile)
875 if (!(_connection_libnet_check_profile_validity(profile))) {
876 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
877 return CONNECTION_ERROR_INVALID_PARAMETER;
880 net_profile_info_t *profile_info = profile;
881 connection_cellular_service_type_e service_type;
883 service_type = _profile_convert_to_connection_cellular_service_type(profile_info->ProfileInfo.Pdp.ServiceType);
885 if (service_type != type)
886 return CONNECTION_ERROR_INVALID_PARAMETER;
888 rv = net_set_default_cellular_service_profile(profile_info->ProfileName);
889 if (rv == NET_ERR_ACCESS_DENIED) {
890 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
891 return CONNECTION_ERROR_PERMISSION_DENIED;
892 } else if (rv != NET_ERR_NONE)
893 return CONNECTION_ERROR_OPERATION_FAILED;
895 return CONNECTION_ERROR_NONE;
898 int _connection_libnet_set_cellular_service_profile_async(connection_cellular_service_type_e type,
899 connection_profile_h profile, connection_set_default_cb callback, void* user_data)
903 if (!(_connection_libnet_check_profile_validity(profile))) {
904 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
905 return CONNECTION_ERROR_INVALID_PARAMETER;
908 net_profile_info_t *profile_info = profile;
909 connection_cellular_service_type_e service_type;
911 service_type = _profile_convert_to_connection_cellular_service_type(profile_info->ProfileInfo.Pdp.ServiceType);
913 if (service_type != type)
914 return CONNECTION_ERROR_INVALID_PARAMETER;
916 rv = net_set_default_cellular_service_profile_async(profile_info->ProfileName);
917 if (rv == NET_ERR_ACCESS_DENIED) {
918 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
919 return CONNECTION_ERROR_PERMISSION_DENIED;
920 } else if (rv != NET_ERR_NONE)
921 return CONNECTION_ERROR_OPERATION_FAILED;
923 __libnet_set_default_cb(callback, user_data);
925 return CONNECTION_ERROR_NONE;
928 int _connection_libnet_close_profile(connection_profile_h profile, connection_closed_cb callback, void *user_data)
932 if (!(_connection_libnet_check_profile_validity(profile))) {
933 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
934 return CONNECTION_ERROR_INVALID_PARAMETER;
937 net_profile_info_t *profile_info = profile;
939 rv = net_close_connection(profile_info->ProfileName);
940 if (rv == NET_ERR_ACCESS_DENIED) {
941 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
942 return CONNECTION_ERROR_PERMISSION_DENIED;
943 } else if (rv != NET_ERR_NONE)
944 return CONNECTION_ERROR_OPERATION_FAILED;
946 if (net_close_connection(profile_info->ProfileName) != NET_ERR_NONE)
947 return CONNECTION_ERROR_OPERATION_FAILED;
949 __libnet_set_closed_cb(callback, user_data);
951 return CONNECTION_ERROR_NONE;
954 int _connection_libnet_add_route(const char *interface_name, const char *host_address)
958 int address_family = 0;
960 if(__libnet_check_address_type(AF_INET, host_address))
961 address_family = AF_INET;
963 return CONNECTION_ERROR_INVALID_PARAMETER;
965 switch(address_family) {
967 endstr = strrchr(host_address, '.');
968 if (endstr == NULL ||
969 strcmp(endstr, ".0") == 0 ||
970 strncmp(host_address, "0.", 2) == 0 ||
971 strstr(host_address, "255") != NULL) {
972 CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed\n");
973 return CONNECTION_ERROR_INVALID_PARAMETER;
977 return CONNECTION_ERROR_OPERATION_FAILED;
980 rv = net_add_route(host_address, interface_name, address_family);
981 if (rv == NET_ERR_ACCESS_DENIED) {
982 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
983 return CONNECTION_ERROR_PERMISSION_DENIED;
984 } else if (rv != NET_ERR_NONE)
985 return CONNECTION_ERROR_OPERATION_FAILED;
987 return CONNECTION_ERROR_NONE;
990 int _connection_libnet_remove_route(const char *interface_name, const char *host_address)
993 char *endstr = strrchr(host_address, '.');
994 int address_family = 0;
996 if (__libnet_check_address_type(AF_INET, host_address))
997 address_family = AF_INET;
999 return CONNECTION_ERROR_INVALID_PARAMETER;
1001 switch(address_family) {
1003 endstr = strrchr(host_address, '.');
1004 if (endstr == NULL ||
1005 strcmp(endstr, ".0") == 0 ||
1006 strncmp(host_address, "0.", 2) == 0 ||
1007 strstr(host_address, ".0.") != NULL ||strstr(host_address, "255") != NULL) {
1008 CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed");
1009 return CONNECTION_ERROR_INVALID_PARAMETER;
1013 return CONNECTION_ERROR_OPERATION_FAILED;
1016 rv = net_remove_route(host_address, interface_name, address_family);
1017 if (rv == NET_ERR_ACCESS_DENIED) {
1018 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
1019 return CONNECTION_ERROR_PERMISSION_DENIED;
1020 } else if (rv != NET_ERR_NONE)
1021 return CONNECTION_ERROR_OPERATION_FAILED;
1023 return CONNECTION_ERROR_NONE;
1026 int _connection_libnet_add_route_ipv6(const char *interface_name, const char *host_address, const char *gateway)
1029 int address_family = 0;
1031 address_family = AF_INET6;
1032 /* if(__libnet_check_address_type(AF_INET6, host_address))
1033 address_family = AF_INET6;
1035 return CONNECTION_ERROR_INVALID_PARAMETER;*/
1037 switch(address_family) {
1039 if (strncmp(host_address, "fe80:", 5) == 0 ||
1040 strncmp(host_address, "ff00:", 5) == 0 ||
1041 strncmp(host_address, "::", 2) == 0) {
1042 CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed\n");
1043 return CONNECTION_ERROR_INVALID_PARAMETER;
1047 return CONNECTION_ERROR_OPERATION_FAILED;
1050 rv = net_add_route_ipv6(host_address, interface_name, address_family, gateway);
1051 if (rv == NET_ERR_ACCESS_DENIED) {
1052 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
1053 return CONNECTION_ERROR_PERMISSION_DENIED;
1054 } else if (rv != NET_ERR_NONE)
1055 return CONNECTION_ERROR_OPERATION_FAILED;
1057 return CONNECTION_ERROR_NONE;
1060 int _connection_libnet_remove_route_ipv6(const char *interface_name, const char *host_address, const char *gateway)
1063 int address_family = 0;
1065 address_family = AF_INET6;
1066 /* if (__libnet_check_address_type(AF_INET6, host_address))
1067 address_family = AF_INET6;
1069 return CONNECTION_ERROR_INVALID_PARAMETER;*/
1071 switch(address_family) {
1073 if (strncmp(host_address, "fe80:", 5) == 0 ||
1074 strncmp(host_address, "ff00:", 5) == 0 ||
1075 strncmp(host_address, "::", 2) == 0) {
1076 CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed\n");
1077 return CONNECTION_ERROR_INVALID_PARAMETER;
1081 return CONNECTION_ERROR_OPERATION_FAILED;
1084 rv = net_remove_route_ipv6(host_address, interface_name, address_family, gateway);
1085 if (rv == NET_ERR_ACCESS_DENIED) {
1086 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
1087 return CONNECTION_ERROR_PERMISSION_DENIED;
1088 } else if (rv != NET_ERR_NONE)
1089 return CONNECTION_ERROR_OPERATION_FAILED;
1091 return CONNECTION_ERROR_NONE;
1094 void _connection_libnet_add_to_profile_list(connection_profile_h profile)
1096 prof_handle_list = g_slist_append(prof_handle_list, profile);
1099 void _connection_libnet_remove_from_profile_list(connection_profile_h profile)
1101 prof_handle_list = g_slist_remove(prof_handle_list, profile);
1105 bool _connection_libnet_add_to_profile_cb_list(connection_profile_h profile,
1106 connection_profile_state_changed_cb callback, void *user_data)
1108 net_profile_info_t *profile_info = profile;
1109 char *profile_name = g_strdup(profile_info->ProfileName);
1111 struct _profile_cb_s *profile_cb_info = g_try_malloc0(sizeof(struct _profile_cb_s));
1112 if (profile_cb_info == NULL) {
1113 g_free(profile_name);
1117 profile_cb_info->callback = callback;
1118 profile_cb_info->user_data = user_data;
1120 g_hash_table_insert(profile_cb_table, profile_name, profile_cb_info);
1125 bool _connection_libnet_remove_from_profile_cb_list(connection_profile_h profile)
1127 net_profile_info_t *profile_info = profile;
1128 if (g_hash_table_remove(profile_cb_table, profile_info->ProfileName) == TRUE)
1134 int _connection_libnet_set_statistics(net_device_t device_type, net_statistics_type_e statistics_type)
1137 rv = net_set_statistics(device_type, statistics_type);
1138 if (rv == NET_ERR_ACCESS_DENIED) {
1139 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
1140 return CONNECTION_ERROR_PERMISSION_DENIED;
1141 } else if (rv != NET_ERR_NONE)
1142 return CONNECTION_ERROR_OPERATION_FAILED;
1144 return CONNECTION_ERROR_NONE;
1147 int _connection_libnet_get_statistics(net_statistics_type_e statistics_type, unsigned long long *size)
1150 rv = net_get_statistics(NET_DEVICE_WIFI, statistics_type, size);
1151 if (rv == NET_ERR_ACCESS_DENIED) {
1152 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
1153 return CONNECTION_ERROR_PERMISSION_DENIED;
1154 }else if (rv != NET_ERR_NONE)
1155 return CONNECTION_ERROR_OPERATION_FAILED;
1157 return CONNECTION_ERROR_NONE;
1160 int _connection_libnet_set_cellular_subscriber_id(connection_profile_h profile,
1161 connection_cellular_subscriber_id_e sim_id)
1163 char *modem_path = NULL;
1164 net_profile_info_t *profile_info = (net_profile_info_t *)profile;
1166 if (net_get_cellular_modem_object_path(&modem_path, sim_id) != NET_ERR_NONE) {
1167 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get subscriber[%d]", sim_id);
1168 return CONNECTION_ERROR_OPERATION_FAILED;
1172 CONNECTION_LOG(CONNECTION_ERROR, "NULL modem object path");
1173 return CONNECTION_ERROR_OPERATION_FAILED;
1176 g_strlcpy(profile_info->ProfileInfo.Pdp.PSModemPath, modem_path,
1177 NET_PROFILE_NAME_LEN_MAX);
1180 return CONNECTION_ERROR_NONE;
1183 static void __connection_idle_destroy_cb(gpointer data)
1188 managed_idler_list = g_slist_remove(managed_idler_list, data);
1192 static gboolean __connection_idle_cb(gpointer user_data)
1194 struct managed_idle_data *data = (struct managed_idle_data *)user_data;
1199 return data->func(data->user_data);
1202 guint _connection_callback_add(GSourceFunc func, gpointer user_data)
1205 struct managed_idle_data *data;
1210 data = g_try_new0(struct managed_idle_data, 1);
1215 data->user_data = user_data;
1217 id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, __connection_idle_cb, data,
1218 __connection_idle_destroy_cb);
1226 managed_idler_list = g_slist_append(managed_idler_list, data);
1231 void _connection_callback_cleanup(void)
1233 GSList *cur = managed_idler_list;
1235 struct managed_idle_data *data;
1238 GSList *next = cur->next;
1239 data = (struct managed_idle_data *)cur->data;
1241 src = g_main_context_find_source_by_id(g_main_context_default(), data->id);
1243 g_source_destroy(src);
1244 cur = managed_idler_list;
1249 g_slist_free(managed_idler_list);
1250 managed_idler_list = NULL;
1253 int _connection_libnet_check_get_privilege()
1257 rv = net_check_get_privilege();
1258 if (rv == NET_ERR_ACCESS_DENIED) {
1259 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
1260 return CONNECTION_ERROR_PERMISSION_DENIED;
1261 } else if (rv != NET_ERR_NONE)
1262 return CONNECTION_ERROR_OPERATION_FAILED;
1264 return CONNECTION_ERROR_NONE;
1267 int _connection_libnet_check_profile_privilege()
1271 rv = net_check_profile_privilege();
1272 if (rv == NET_ERR_ACCESS_DENIED) {
1273 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
1274 return CONNECTION_ERROR_PERMISSION_DENIED;
1275 } else if (rv != NET_ERR_NONE)
1276 return CONNECTION_ERROR_OPERATION_FAILED;
1278 return CONNECTION_ERROR_NONE;
1281 bool _connection_libnet_get_is_check_enable_feature()
1283 return is_check_enable_feature;
1286 bool _connection_libnet_get_enable_feature_state(enable_feature_type_e feature_type)
1288 if(is_check_enable_feature){
1289 switch(feature_type) {
1290 case FEATURE_TYPE_TELEPHONY:
1291 return enable_feature.telephony;
1292 case FEATURE_TYPE_WIFI:
1293 return enable_feature.wifi;
1294 case FEATURE_TYPE_TETHERING_BLUETOOTH:
1295 return enable_feature.tethering_bluetooth;
1297 CONNECTION_LOG(CONNECTION_ERROR, "Invalid feature type");
1301 CONNECTION_LOG(CONNECTION_ERROR, "Not checked enable feature yet");