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 "net_connection_private.h"
23 static GSList *conn_handle_list = NULL;
25 static void __connection_cb_state_change_cb(keynode_t *node, void *user_data);
26 static void __connection_cb_ip_change_cb(keynode_t *node, void *user_data);
27 static void __connection_cb_proxy_change_cb(keynode_t *node, void *user_data);
29 static int __connection_convert_net_state(int status)
32 case VCONFKEY_NETWORK_CELLULAR:
33 return CONNECTION_TYPE_CELLULAR;
34 case VCONFKEY_NETWORK_WIFI:
35 return CONNECTION_TYPE_WIFI;
36 case VCONFKEY_NETWORK_ETHERNET:
37 return CONNECTION_TYPE_ETHERNET;
38 case VCONFKEY_NETWORK_BLUETOOTH:
39 return CONNECTION_TYPE_BT;
41 return CONNECTION_TYPE_DISCONNECTED;
45 static int __connection_convert_cellular_state(int status)
48 case VCONFKEY_NETWORK_CELLULAR_ON:
49 return CONNECTION_CELLULAR_STATE_AVAILABLE;
50 case VCONFKEY_NETWORK_CELLULAR_3G_OPTION_OFF:
51 return CONNECTION_CELLULAR_STATE_CALL_ONLY_AVAILABLE;
52 case VCONFKEY_NETWORK_CELLULAR_ROAMING_OFF:
53 return CONNECTION_CELLULAR_STATE_ROAMING_OFF;
54 case VCONFKEY_NETWORK_CELLULAR_FLIGHT_MODE:
55 return CONNECTION_CELLULAR_STATE_FLIGHT_MODE;
57 return CONNECTION_CELLULAR_STATE_OUT_OF_SERVICE;
61 static int __connection_get_type_changed_callback_count(void)
66 for (list = conn_handle_list; list; list = list->next) {
67 connection_handle_s *local_handle = (connection_handle_s *)list->data;
68 if (local_handle->type_changed_callback) count++;
74 static int __connection_get_ip_changed_callback_count(void)
79 for (list = conn_handle_list; list; list = list->next) {
80 connection_handle_s *local_handle = (connection_handle_s *)list->data;
81 if (local_handle->ip_changed_callback) count++;
87 static int __connection_get_proxy_changed_callback_count(void)
92 for (list = conn_handle_list; list; list = list->next) {
93 connection_handle_s *local_handle = (connection_handle_s *)list->data;
94 if (local_handle->proxy_changed_callback) count++;
100 static void __connection_cb_ethernet_cable_state_changed_cb(connection_ethernet_cable_state_e state)
102 CONNECTION_LOG(CONNECTION_INFO, "Ethernet Cable state Indication");
106 for (list = conn_handle_list; list; list = list->next) {
107 connection_handle_s *local_handle = (connection_handle_s *)list->data;
108 if (local_handle->ethernet_cable_state_changed_callback)
109 local_handle->ethernet_cable_state_changed_callback(state,
110 local_handle->ethernet_cable_state_changed_user_data);
114 static int __connection_get_ethernet_cable_state_changed_callback_count(void)
119 for (list = conn_handle_list; list; list = list->next) {
120 connection_handle_s *local_handle = (connection_handle_s *)list->data;
121 if (local_handle->ethernet_cable_state_changed_callback) count++;
127 static int __connection_set_type_changed_callback(connection_h connection,
128 void *callback, void *user_data)
130 connection_handle_s *local_handle = (connection_handle_s *)connection;
133 if (__connection_get_type_changed_callback_count() == 0)
134 if (vconf_notify_key_changed(VCONFKEY_NETWORK_STATUS ,
135 __connection_cb_state_change_cb, NULL))
136 return CONNECTION_ERROR_OPERATION_FAILED;
138 local_handle->type_changed_user_data = user_data;
140 if (local_handle->type_changed_callback &&
141 __connection_get_type_changed_callback_count() == 1)
142 if (vconf_ignore_key_changed(VCONFKEY_NETWORK_STATUS,
143 __connection_cb_state_change_cb))
144 return CONNECTION_ERROR_OPERATION_FAILED;
147 local_handle->type_changed_callback = callback;
148 return CONNECTION_ERROR_NONE;
151 static int __connection_set_ip_changed_callback(connection_h connection,
152 void *callback, void *user_data)
154 connection_handle_s *local_handle = (connection_handle_s *)connection;
157 if (__connection_get_ip_changed_callback_count() == 0)
158 if (vconf_notify_key_changed(VCONFKEY_NETWORK_IP,
159 __connection_cb_ip_change_cb, NULL))
160 return CONNECTION_ERROR_OPERATION_FAILED;
162 local_handle->ip_changed_user_data = user_data;
164 if (local_handle->ip_changed_callback &&
165 __connection_get_ip_changed_callback_count() == 1)
166 if (vconf_ignore_key_changed(VCONFKEY_NETWORK_IP,
167 __connection_cb_ip_change_cb))
168 return CONNECTION_ERROR_OPERATION_FAILED;
171 local_handle->ip_changed_callback = callback;
172 return CONNECTION_ERROR_NONE;
175 static int __connection_set_proxy_changed_callback(connection_h connection,
176 void *callback, void *user_data)
178 connection_handle_s *local_handle = (connection_handle_s *)connection;
181 if (__connection_get_proxy_changed_callback_count() == 0)
182 if (vconf_notify_key_changed(VCONFKEY_NETWORK_PROXY,
183 __connection_cb_proxy_change_cb, NULL))
184 return CONNECTION_ERROR_OPERATION_FAILED;
186 local_handle->proxy_changed_user_data = user_data;
188 if (local_handle->proxy_changed_callback &&
189 __connection_get_proxy_changed_callback_count() == 1)
190 if (vconf_ignore_key_changed(VCONFKEY_NETWORK_PROXY,
191 __connection_cb_proxy_change_cb))
192 return CONNECTION_ERROR_OPERATION_FAILED;
195 local_handle->proxy_changed_callback = callback;
196 return CONNECTION_ERROR_NONE;
199 static void __connection_cb_state_change_cb(keynode_t *node, void *user_data)
201 CONNECTION_LOG(CONNECTION_INFO, "Net Status Changed Indication\n");
204 int state = vconf_keynode_get_int(node);
206 for (list = conn_handle_list; list; list = list->next) {
207 connection_handle_s *local_handle = (connection_handle_s *)list->data;
208 if (local_handle->type_changed_callback)
209 local_handle->type_changed_callback(
210 __connection_convert_net_state(state),
211 local_handle->type_changed_user_data);
215 static void __connection_cb_ip_change_cb(keynode_t *node, void *user_data)
217 CONNECTION_LOG(CONNECTION_INFO, "Net IP Changed Indication\n");
220 char *ip_addr = vconf_keynode_get_str(node);
222 for (list = conn_handle_list; list; list = list->next) {
223 connection_handle_s *local_handle = (connection_handle_s *)list->data;
224 if (local_handle->ip_changed_callback)
225 local_handle->ip_changed_callback(
227 local_handle->ip_changed_user_data);
231 static void __connection_cb_proxy_change_cb(keynode_t *node, void *user_data)
233 CONNECTION_LOG(CONNECTION_INFO, "Net IP Changed Indication\n");
236 char *proxy = vconf_keynode_get_str(node);
238 for (list = conn_handle_list; list; list = list->next) {
239 connection_handle_s *local_handle = (connection_handle_s *)list->data;
240 if (local_handle->proxy_changed_callback)
241 local_handle->proxy_changed_callback(
243 local_handle->proxy_changed_user_data);
247 static bool __connection_check_handle_validity(connection_h connection)
251 if (connection == NULL)
254 if (g_slist_find(conn_handle_list, connection) != NULL)
260 static int __connection_set_ethernet_cable_state_changed_cb(connection_h connection,
261 connection_ethernet_cable_state_chaged_cb callback, void *user_data)
263 connection_handle_s *local_handle = (connection_handle_s *)connection;
266 if (__connection_get_ethernet_cable_state_changed_callback_count() == 0)
267 _connection_libnet_set_ethernet_cable_state_changed_cb(
268 __connection_cb_ethernet_cable_state_changed_cb);
271 if (__connection_get_ethernet_cable_state_changed_callback_count() == 1)
272 _connection_libnet_set_ethernet_cable_state_changed_cb(NULL);
275 local_handle->ethernet_cable_state_changed_callback = callback;
276 local_handle->ethernet_cable_state_changed_user_data = user_data;
277 return CONNECTION_ERROR_NONE;
280 static int __connection_get_handle_count(void)
285 if (!conn_handle_list)
288 for (list = conn_handle_list; list; list = list->next) count++;
293 /* Connection Manager ********************************************************/
294 EXPORT_API int connection_create(connection_h* connection)
296 CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE, TETHERING_BLUETOOTH_FEATURE, ETHERNET_FEATURE);
298 CONNECTION_MUTEX_LOCK;
300 if (connection == NULL || __connection_check_handle_validity(*connection)) {
301 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
302 CONNECTION_MUTEX_UNLOCK;
303 return CONNECTION_ERROR_INVALID_PARAMETER;
306 rv = _connection_libnet_init();
307 if (rv == NET_ERR_ACCESS_DENIED) {
308 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
309 CONNECTION_MUTEX_UNLOCK;
310 return CONNECTION_ERROR_PERMISSION_DENIED;
312 else if (rv != NET_ERR_NONE) {
313 CONNECTION_LOG(CONNECTION_ERROR, "Failed to create connection[%d]", rv);
314 CONNECTION_MUTEX_UNLOCK;
315 return CONNECTION_ERROR_OPERATION_FAILED;
318 *connection = g_try_malloc0(sizeof(connection_handle_s));
319 if (*connection != NULL) {
320 CONNECTION_LOG(CONNECTION_INFO, "New Handle Created %p\n", *connection);
322 CONNECTION_MUTEX_UNLOCK;
323 return CONNECTION_ERROR_OUT_OF_MEMORY;
326 conn_handle_list = g_slist_append(conn_handle_list, *connection);
328 CONNECTION_MUTEX_UNLOCK;
329 return CONNECTION_ERROR_NONE;
332 EXPORT_API int connection_destroy(connection_h connection)
334 CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE, TETHERING_BLUETOOTH_FEATURE, ETHERNET_FEATURE);
336 CONNECTION_MUTEX_LOCK;
338 if (connection == NULL || !(__connection_check_handle_validity(connection))) {
339 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
340 CONNECTION_MUTEX_UNLOCK;
341 return CONNECTION_ERROR_INVALID_PARAMETER;
344 CONNECTION_LOG(CONNECTION_INFO, "Destroy Handle : %p\n", connection);
346 __connection_set_type_changed_callback(connection, NULL, NULL);
347 __connection_set_ip_changed_callback(connection, NULL, NULL);
348 __connection_set_proxy_changed_callback(connection, NULL, NULL);
349 __connection_set_ethernet_cable_state_changed_cb(connection, NULL, NULL);
351 conn_handle_list = g_slist_remove(conn_handle_list, connection);
355 if (__connection_get_handle_count() == 0)
356 _connection_libnet_deinit();
358 CONNECTION_MUTEX_UNLOCK;
359 return CONNECTION_ERROR_NONE;
362 EXPORT_API int connection_get_type(connection_h connection, connection_type_e* type)
366 CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE, TETHERING_BLUETOOTH_FEATURE, ETHERNET_FEATURE);
368 if (type == NULL || !(__connection_check_handle_validity(connection))) {
369 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
370 return CONNECTION_ERROR_INVALID_PARAMETER;
373 if (vconf_get_int(VCONFKEY_NETWORK_STATUS, &status)) {
374 CONNECTION_LOG(CONNECTION_ERROR, "vconf_get_int Failed = %d\n", status);
375 return CONNECTION_ERROR_OPERATION_FAILED;
378 CONNECTION_LOG(CONNECTION_INFO, "Connected Network = %d\n", status);
380 *type = __connection_convert_net_state(status);
382 return CONNECTION_ERROR_NONE;
385 EXPORT_API int connection_get_ip_address(connection_h connection,
386 connection_address_family_e address_family, char **ip_address)
388 CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE, TETHERING_BLUETOOTH_FEATURE, ETHERNET_FEATURE);
390 if (ip_address == NULL || !(__connection_check_handle_validity(connection))) {
391 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
392 return CONNECTION_ERROR_INVALID_PARAMETER;
395 switch (address_family) {
396 case CONNECTION_ADDRESS_FAMILY_IPV4:
397 case CONNECTION_ADDRESS_FAMILY_IPV6:
398 *ip_address = vconf_get_str(VCONFKEY_NETWORK_IP);
402 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
403 return CONNECTION_ERROR_INVALID_PARAMETER;
406 if (*ip_address == NULL) {
407 CONNECTION_LOG(CONNECTION_ERROR, "vconf_get_str Failed");
408 return CONNECTION_ERROR_OPERATION_FAILED;
411 return CONNECTION_ERROR_NONE;
414 EXPORT_API int connection_get_proxy(connection_h connection,
415 connection_address_family_e address_family, char **proxy)
417 CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE, TETHERING_BLUETOOTH_FEATURE, ETHERNET_FEATURE);
419 if (proxy == NULL || !(__connection_check_handle_validity(connection))) {
420 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
421 return CONNECTION_ERROR_INVALID_PARAMETER;
424 switch (address_family) {
425 case CONNECTION_ADDRESS_FAMILY_IPV4:
426 case CONNECTION_ADDRESS_FAMILY_IPV6:
427 *proxy = vconf_get_str(VCONFKEY_NETWORK_PROXY);
431 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
432 return CONNECTION_ERROR_INVALID_PARAMETER;
435 if (*proxy == NULL) {
436 CONNECTION_LOG(CONNECTION_ERROR, "vconf_get_str Failed");
437 return CONNECTION_ERROR_OPERATION_FAILED;
440 return CONNECTION_ERROR_NONE;
443 EXPORT_API int connection_get_mac_address(connection_h connection, connection_type_e type, char** mac_addr)
446 char buf[CONNECTION_MAC_INFO_LENGTH + 1];
448 CHECK_FEATURE_SUPPORTED(WIFI_FEATURE, ETHERNET_FEATURE);
450 if(type == CONNECTION_TYPE_WIFI)
451 CHECK_FEATURE_SUPPORTED(WIFI_FEATURE);
452 else if(type == CONNECTION_TYPE_ETHERNET)
453 CHECK_FEATURE_SUPPORTED(ETHERNET_FEATURE);
455 if (mac_addr == NULL || !(__connection_check_handle_validity(connection))) {
456 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
457 return CONNECTION_ERROR_INVALID_PARAMETER;
461 case CONNECTION_TYPE_WIFI:
462 fp = fopen(WIFI_MAC_INFO_FILE, "r");
464 CONNECTION_LOG(CONNECTION_ERROR, "Failed to open file %s", WIFI_MAC_INFO_FILE);
465 return CONNECTION_ERROR_OUT_OF_MEMORY;
468 if (fgets(buf, sizeof(buf), fp) == NULL) {
469 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get MAC info from %s", WIFI_MAC_INFO_FILE);
471 return CONNECTION_ERROR_OPERATION_FAILED;
474 CONNECTION_LOG(CONNECTION_INFO, "%s : %s", WIFI_MAC_INFO_FILE, buf);
476 *mac_addr = (char *)malloc(CONNECTION_MAC_INFO_LENGTH + 1);
477 if (*mac_addr == NULL) {
478 CONNECTION_LOG(CONNECTION_ERROR, "malloc() failed");
480 return CONNECTION_ERROR_OUT_OF_MEMORY;
482 g_strlcpy(*mac_addr, buf, CONNECTION_MAC_INFO_LENGTH + 1);
485 case CONNECTION_TYPE_ETHERNET:
486 fp = fopen(ETHERNET_MAC_INFO_FILE, "r");
488 CONNECTION_LOG(CONNECTION_ERROR, "Failed to open file %s", ETHERNET_MAC_INFO_FILE);
489 return CONNECTION_ERROR_OUT_OF_MEMORY;
492 if (fgets(buf, sizeof(buf), fp) == NULL) {
493 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get MAC info from %s", ETHERNET_MAC_INFO_FILE);
495 return CONNECTION_ERROR_OPERATION_FAILED;
498 CONNECTION_LOG(CONNECTION_INFO, "%s : %s", ETHERNET_MAC_INFO_FILE, buf);
500 *mac_addr = (char *)malloc(CONNECTION_MAC_INFO_LENGTH + 1);
501 if (*mac_addr == NULL) {
502 CONNECTION_LOG(CONNECTION_ERROR, "malloc() failed");
504 return CONNECTION_ERROR_OUT_OF_MEMORY;
507 g_strlcpy(*mac_addr, buf,CONNECTION_MAC_INFO_LENGTH + 1);
512 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
513 return CONNECTION_ERROR_INVALID_PARAMETER;
516 /* Checking Invalid MAC Address */
517 if((strcmp(*mac_addr, "00:00:00:00:00:00") == 0) ||
518 (strcmp(*mac_addr, "ff:ff:ff:ff:ff:ff") == 0)) {
519 CONNECTION_LOG(CONNECTION_ERROR, "MAC Address(%s) is invalid", *mac_addr);
520 return CONNECTION_ERROR_INVALID_OPERATION;
523 CONNECTION_LOG(CONNECTION_INFO, "MAC Address %s", *mac_addr);
525 return CONNECTION_ERROR_NONE;
528 EXPORT_API int connection_get_cellular_state(connection_h connection, connection_cellular_state_e* state)
531 int cellular_state = 0;
533 CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE);
535 if (state == NULL || !(__connection_check_handle_validity(connection))) {
536 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
537 return CONNECTION_ERROR_INVALID_PARAMETER;
540 if (!vconf_get_int(VCONFKEY_NETWORK_CELLULAR_STATE, &status)) {
541 CONNECTION_LOG(CONNECTION_INFO, "Cellular = %d\n", status);
542 *state = __connection_convert_cellular_state(status);
544 if (*state == CONNECTION_CELLULAR_STATE_AVAILABLE) {
545 if (vconf_get_int(VCONFKEY_DNET_STATE, &cellular_state)) {
546 CONNECTION_LOG(CONNECTION_ERROR,
547 "vconf_get_int Failed = %d\n", cellular_state);
548 return CONNECTION_ERROR_OPERATION_FAILED;
552 CONNECTION_LOG(CONNECTION_INFO, "Connection state = %d\n", cellular_state);
554 if (cellular_state == VCONFKEY_DNET_NORMAL_CONNECTED ||
555 cellular_state == VCONFKEY_DNET_SECURE_CONNECTED ||
556 cellular_state == VCONFKEY_DNET_TRANSFER)
557 *state = CONNECTION_CELLULAR_STATE_CONNECTED;
559 return CONNECTION_ERROR_NONE;
561 CONNECTION_LOG(CONNECTION_ERROR, "vconf_get_int Failed = %d\n", status);
562 return CONNECTION_ERROR_OPERATION_FAILED;
566 EXPORT_API int connection_get_wifi_state(connection_h connection, connection_wifi_state_e* state)
568 CHECK_FEATURE_SUPPORTED(WIFI_FEATURE);
572 if (state == NULL || !(__connection_check_handle_validity(connection))) {
573 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
574 return CONNECTION_ERROR_INVALID_PARAMETER;
577 rv = _connection_libnet_get_wifi_state(state);
578 if (rv != CONNECTION_ERROR_NONE) {
579 CONNECTION_LOG(CONNECTION_ERROR, "Fail to get Wi-Fi state[%d]", rv);
583 CONNECTION_LOG(CONNECTION_INFO, "WiFi state = %d\n", *state);
585 return CONNECTION_ERROR_NONE;
588 EXPORT_API int connection_get_ethernet_state(connection_h connection, connection_ethernet_state_e* state)
590 CHECK_FEATURE_SUPPORTED(ETHERNET_FEATURE);
592 if (state == NULL || !(__connection_check_handle_validity(connection))) {
593 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
594 return CONNECTION_ERROR_INVALID_PARAMETER;
597 return _connection_libnet_get_ethernet_state(state);
600 EXPORT_API int connection_get_ethernet_cable_state(connection_h connection, connection_ethernet_cable_state_e *state)
602 CHECK_FEATURE_SUPPORTED(ETHERNET_FEATURE);
604 if (state == NULL || !(__connection_check_handle_validity(connection))) {
605 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
606 return CONNECTION_ERROR_INVALID_PARAMETER;
609 return _connection_libnet_get_ethernet_cable_state(state);
612 EXPORT_API int connection_set_ethernet_cable_state_chaged_cb(connection_h connection,
613 connection_ethernet_cable_state_chaged_cb callback, void *user_data)
615 CHECK_FEATURE_SUPPORTED(ETHERNET_FEATURE);
617 if (callback == NULL || !(__connection_check_handle_validity(connection))) {
618 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
619 return CONNECTION_ERROR_INVALID_PARAMETER;
622 return __connection_set_ethernet_cable_state_changed_cb(connection,
623 callback, user_data);
626 EXPORT_API int connection_unset_ethernet_cable_state_chaged_cb(connection_h connection)
628 CHECK_FEATURE_SUPPORTED(ETHERNET_FEATURE);
630 if ( !(__connection_check_handle_validity(connection)) ) {
631 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
632 return CONNECTION_ERROR_INVALID_PARAMETER;
635 return __connection_set_ethernet_cable_state_changed_cb(connection,
639 EXPORT_API int connection_get_bt_state(connection_h connection, connection_bt_state_e* state)
641 CHECK_FEATURE_SUPPORTED(TETHERING_BLUETOOTH_FEATURE);
643 if (state == NULL || !(__connection_check_handle_validity(connection))) {
644 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
645 return CONNECTION_ERROR_INVALID_PARAMETER;
648 return _connection_libnet_get_bluetooth_state(state);
652 EXPORT_API int connection_set_type_changed_cb(connection_h connection,
653 connection_type_changed_cb callback, void* user_data)
655 CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE, TETHERING_BLUETOOTH_FEATURE, ETHERNET_FEATURE);
657 if (callback == NULL || !(__connection_check_handle_validity(connection))) {
658 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
659 return CONNECTION_ERROR_INVALID_PARAMETER;
662 return __connection_set_type_changed_callback(connection, callback, user_data);
665 EXPORT_API int connection_unset_type_changed_cb(connection_h connection)
667 CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE, TETHERING_BLUETOOTH_FEATURE, ETHERNET_FEATURE);
669 if (!(__connection_check_handle_validity(connection))) {
670 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
671 return CONNECTION_ERROR_INVALID_PARAMETER;
674 return __connection_set_type_changed_callback(connection, NULL, NULL);
677 EXPORT_API int connection_set_ip_address_changed_cb(connection_h connection,
678 connection_address_changed_cb callback, void* user_data)
680 CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE, TETHERING_BLUETOOTH_FEATURE, ETHERNET_FEATURE);
682 if (callback == NULL || !(__connection_check_handle_validity(connection))) {
683 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
684 return CONNECTION_ERROR_INVALID_PARAMETER;
687 return __connection_set_ip_changed_callback(connection, callback, user_data);
690 EXPORT_API int connection_unset_ip_address_changed_cb(connection_h connection)
692 CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE, TETHERING_BLUETOOTH_FEATURE, ETHERNET_FEATURE);
694 if (!(__connection_check_handle_validity(connection))) {
695 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
696 return CONNECTION_ERROR_INVALID_PARAMETER;
699 return __connection_set_ip_changed_callback(connection, NULL, NULL);
702 EXPORT_API int connection_set_proxy_address_changed_cb(connection_h connection,
703 connection_address_changed_cb callback, void* user_data)
705 CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE, TETHERING_BLUETOOTH_FEATURE, ETHERNET_FEATURE);
707 if (callback == NULL || !(__connection_check_handle_validity(connection))) {
708 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
709 return CONNECTION_ERROR_INVALID_PARAMETER;
712 return __connection_set_proxy_changed_callback(connection, callback, user_data);
715 EXPORT_API int connection_unset_proxy_address_changed_cb(connection_h connection)
717 CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE, TETHERING_BLUETOOTH_FEATURE, ETHERNET_FEATURE);
719 if (!(__connection_check_handle_validity(connection))) {
720 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
721 return CONNECTION_ERROR_INVALID_PARAMETER;
724 return __connection_set_proxy_changed_callback(connection, NULL, NULL);
727 EXPORT_API int connection_add_profile(connection_h connection, connection_profile_h profile)
729 CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE);
731 if (!(__connection_check_handle_validity(connection)) ||
732 !(_connection_libnet_check_profile_validity(profile))) {
733 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
734 return CONNECTION_ERROR_INVALID_PARAMETER;
739 net_profile_info_t *profile_info = profile;
741 if (profile_info->profile_type != NET_DEVICE_CELLULAR) {
742 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
743 return CONNECTION_ERROR_INVALID_PARAMETER;
746 rv = net_add_profile(profile_info->ProfileInfo.Pdp.ServiceType, (net_profile_info_t*)profile);
747 if (rv == NET_ERR_ACCESS_DENIED) {
748 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
749 return CONNECTION_ERROR_PERMISSION_DENIED;
750 } else if (rv != NET_ERR_NONE) {
751 CONNECTION_LOG(CONNECTION_ERROR, "Failed to add profile[%d]", rv);
752 return CONNECTION_ERROR_OPERATION_FAILED;
755 return CONNECTION_ERROR_NONE;
758 EXPORT_API int connection_remove_profile(connection_h connection, connection_profile_h profile)
760 CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE);
762 if (!(__connection_check_handle_validity(connection)) ||
763 !(_connection_libnet_check_profile_validity(profile))) {
764 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
765 return CONNECTION_ERROR_INVALID_PARAMETER;
769 net_profile_info_t *profile_info = profile;
771 if (profile_info->profile_type != NET_DEVICE_CELLULAR &&
772 profile_info->profile_type != NET_DEVICE_WIFI) {
773 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
774 return CONNECTION_ERROR_INVALID_PARAMETER;
777 rv = net_delete_profile(profile_info->ProfileName);
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 delete profile[%d]", rv);
783 return CONNECTION_ERROR_OPERATION_FAILED;
786 return CONNECTION_ERROR_NONE;
789 EXPORT_API int connection_update_profile(connection_h connection, connection_profile_h profile)
791 CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE, ETHERNET_FEATURE);
793 if (!(__connection_check_handle_validity(connection)) ||
794 !(_connection_libnet_check_profile_validity(profile))) {
795 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
796 return CONNECTION_ERROR_INVALID_PARAMETER;
800 net_profile_info_t *profile_info = profile;
802 rv = net_modify_profile(profile_info->ProfileName, (net_profile_info_t*)profile);
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 CONNECTION_LOG(CONNECTION_ERROR, "Failed to modify profile[%d]", rv);
808 return CONNECTION_ERROR_OPERATION_FAILED;
811 return CONNECTION_ERROR_NONE;
814 EXPORT_API int connection_get_profile_iterator(connection_h connection,
815 connection_iterator_type_e type, connection_profile_iterator_h* profile_iterator)
817 CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE, TETHERING_BLUETOOTH_FEATURE, ETHERNET_FEATURE);
819 if (!(__connection_check_handle_validity(connection)) ||
820 (type != CONNECTION_ITERATOR_TYPE_REGISTERED &&
821 type != CONNECTION_ITERATOR_TYPE_CONNECTED &&
822 type != CONNECTION_ITERATOR_TYPE_DEFAULT)) {
823 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
824 return CONNECTION_ERROR_INVALID_PARAMETER;
827 return _connection_libnet_get_profile_iterator(type, profile_iterator);
830 EXPORT_API int connection_profile_iterator_next(connection_profile_iterator_h profile_iterator,
831 connection_profile_h* profile)
833 CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE, TETHERING_BLUETOOTH_FEATURE, ETHERNET_FEATURE);
835 return _connection_libnet_get_iterator_next(profile_iterator, profile);
838 EXPORT_API bool connection_profile_iterator_has_next(connection_profile_iterator_h profile_iterator)
840 CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE, TETHERING_BLUETOOTH_FEATURE, ETHERNET_FEATURE);
842 return _connection_libnet_iterator_has_next(profile_iterator);
845 EXPORT_API int connection_destroy_profile_iterator(connection_profile_iterator_h profile_iterator)
847 CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE, TETHERING_BLUETOOTH_FEATURE, ETHERNET_FEATURE);
849 return _connection_libnet_destroy_iterator(profile_iterator);
852 EXPORT_API int connection_get_current_profile(connection_h connection, connection_profile_h* profile)
854 CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE, TETHERING_BLUETOOTH_FEATURE, ETHERNET_FEATURE);
856 if (!(__connection_check_handle_validity(connection)) || profile == NULL) {
857 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
858 return CONNECTION_ERROR_INVALID_PARAMETER;
861 return _connection_libnet_get_current_profile(profile);
864 EXPORT_API int connection_get_default_cellular_service_profile(connection_h connection,
865 connection_cellular_service_type_e type, connection_profile_h* profile)
867 CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE);
869 if (!(__connection_check_handle_validity(connection)) || profile == NULL) {
870 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
871 return CONNECTION_ERROR_INVALID_PARAMETER;
874 return _connection_libnet_get_cellular_service_profile(type, profile);
877 EXPORT_API int connection_set_default_cellular_service_profile(connection_h connection,
878 connection_cellular_service_type_e type, connection_profile_h profile)
880 CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE);
882 if (!(__connection_check_handle_validity(connection)) || profile == NULL) {
883 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
884 return CONNECTION_ERROR_INVALID_PARAMETER;
887 return _connection_libnet_set_cellular_service_profile_sync(type, profile);
890 EXPORT_API int connection_set_default_cellular_service_profile_async(connection_h connection,
891 connection_cellular_service_type_e type, connection_profile_h profile,
892 connection_set_default_cb callback, void* user_data)
894 CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE);
896 if (!(__connection_check_handle_validity(connection)) ||
897 profile == NULL || callback == NULL) {
898 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
899 return CONNECTION_ERROR_INVALID_PARAMETER;
902 return _connection_libnet_set_cellular_service_profile_async(type, profile, callback, user_data);
905 EXPORT_API int connection_open_profile(connection_h connection, connection_profile_h profile,
906 connection_opened_cb callback, void* user_data)
908 CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE, TETHERING_BLUETOOTH_FEATURE);
910 if (!(__connection_check_handle_validity(connection)) ||
911 profile == NULL || callback == NULL) {
912 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
913 return CONNECTION_ERROR_INVALID_PARAMETER;
916 return _connection_libnet_open_profile(profile, callback, user_data);
919 EXPORT_API int connection_close_profile(connection_h connection, connection_profile_h profile,
920 connection_closed_cb callback, void* user_data)
922 CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE, TETHERING_BLUETOOTH_FEATURE);
924 if (!(__connection_check_handle_validity(connection)) ||
925 profile == NULL || callback == NULL) {
926 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
927 return CONNECTION_ERROR_INVALID_PARAMETER;
930 return _connection_libnet_close_profile(profile, callback, user_data);
933 EXPORT_API int connection_reset_profile(connection_h connection,
934 connection_reset_option_e type, int id, connection_reset_cb callback, void *user_data)
936 CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE);
938 if (!(__connection_check_handle_validity(connection))) {
939 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed");
940 return CONNECTION_ERROR_INVALID_PARAMETER;
943 if(id < 0 || id > 1) {
944 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed");
945 return CONNECTION_ERROR_INVALID_PARAMETER;
948 return _connection_libnet_reset_profile(type, id, callback, user_data);
951 EXPORT_API int connection_add_route(connection_h connection, const char* interface_name, const char* host_address)
953 CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE, TETHERING_BLUETOOTH_FEATURE, ETHERNET_FEATURE);
955 if (!(__connection_check_handle_validity(connection)) ||
956 interface_name == NULL || host_address == NULL) {
957 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
958 return CONNECTION_ERROR_INVALID_PARAMETER;
961 return _connection_libnet_add_route(interface_name, host_address);
964 EXPORT_API int connection_remove_route(connection_h connection, const char* interface_name, const char* host_address)
966 CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE, TETHERING_BLUETOOTH_FEATURE, ETHERNET_FEATURE);
968 if (!(__connection_check_handle_validity(connection)) ||
969 interface_name == NULL || host_address == NULL) {
970 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
971 return CONNECTION_ERROR_INVALID_PARAMETER;
974 return _connection_libnet_remove_route(interface_name, host_address);
977 EXPORT_API int connection_add_route_ipv6(connection_h connection, const char *interface_name, const char *host_address, const char * gateway)
979 CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE, ETHERNET_FEATURE);
981 if (!(__connection_check_handle_validity(connection)) ||
982 interface_name == NULL || host_address == NULL) {
983 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
984 return CONNECTION_ERROR_INVALID_PARAMETER;
987 return _connection_libnet_add_route_ipv6(interface_name, host_address, gateway);
990 EXPORT_API int connection_remove_route_ipv6(connection_h connection, const char *interface_name, const char *host_address, const char * gateway)
992 CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE, ETHERNET_FEATURE);
994 if (!(__connection_check_handle_validity(connection)) ||
995 interface_name == NULL || host_address == NULL) {
996 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
997 return CONNECTION_ERROR_INVALID_PARAMETER;
1000 return _connection_libnet_remove_route_ipv6(interface_name, host_address, gateway);
1003 /* Connection Statistics module ******************************************************************/
1005 static int __get_statistic(connection_type_e connection_type,
1006 connection_statistics_type_e statistics_type, long long* llsize)
1009 unsigned long long ull_size;
1013 if (llsize == NULL) {
1014 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
1015 return CONNECTION_ERROR_INVALID_PARAMETER;
1018 if (connection_type == CONNECTION_TYPE_CELLULAR) {
1019 switch (statistics_type) {
1020 case CONNECTION_STATISTICS_TYPE_LAST_SENT_DATA:
1021 key = VCONFKEY_NETWORK_CELLULAR_PKT_LAST_SNT;
1023 case CONNECTION_STATISTICS_TYPE_LAST_RECEIVED_DATA:
1024 key = VCONFKEY_NETWORK_CELLULAR_PKT_LAST_RCV;
1026 case CONNECTION_STATISTICS_TYPE_TOTAL_SENT_DATA:
1027 key = VCONFKEY_NETWORK_CELLULAR_PKT_TOTAL_SNT;
1029 case CONNECTION_STATISTICS_TYPE_TOTAL_RECEIVED_DATA:
1030 key = VCONFKEY_NETWORK_CELLULAR_PKT_TOTAL_RCV;
1033 return CONNECTION_ERROR_INVALID_PARAMETER;
1036 if (vconf_get_int(key, &size)) {
1037 CONNECTION_LOG(CONNECTION_ERROR, "Cannot Get %s = %d\n", key, size);
1039 return CONNECTION_ERROR_OPERATION_FAILED;
1042 CONNECTION_LOG(CONNECTION_INFO,"%s:%d bytes\n", key, size);
1043 *llsize = (long long)size;
1044 } else if (connection_type == CONNECTION_TYPE_WIFI) {
1045 switch (statistics_type) {
1046 case CONNECTION_STATISTICS_TYPE_LAST_SENT_DATA:
1047 stat_type = NET_STATISTICS_TYPE_LAST_SENT_DATA;
1049 case CONNECTION_STATISTICS_TYPE_LAST_RECEIVED_DATA:
1050 stat_type = NET_STATISTICS_TYPE_LAST_RECEIVED_DATA;
1052 case CONNECTION_STATISTICS_TYPE_TOTAL_SENT_DATA:
1053 stat_type = NET_STATISTICS_TYPE_TOTAL_SENT_DATA;
1055 case CONNECTION_STATISTICS_TYPE_TOTAL_RECEIVED_DATA:
1056 stat_type = NET_STATISTICS_TYPE_TOTAL_RECEIVED_DATA;
1059 return CONNECTION_ERROR_INVALID_PARAMETER;
1062 rv = _connection_libnet_get_statistics(stat_type, &ull_size);
1063 if (rv == CONNECTION_ERROR_PERMISSION_DENIED)
1065 else if (rv != CONNECTION_ERROR_NONE) {
1066 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get Wi-Fi statistics");
1068 return CONNECTION_ERROR_OPERATION_FAILED;
1071 CONNECTION_LOG(CONNECTION_INFO,"%d bytes\n", ull_size);
1072 *llsize = (long long)ull_size;
1074 return CONNECTION_ERROR_INVALID_PARAMETER;
1076 return CONNECTION_ERROR_NONE;
1079 static int __reset_statistic(connection_type_e connection_type,
1080 connection_statistics_type_e statistics_type)
1086 if (connection_type == CONNECTION_TYPE_CELLULAR)
1087 conn_type = NET_DEVICE_CELLULAR;
1088 else if (connection_type == CONNECTION_TYPE_WIFI)
1089 conn_type = NET_DEVICE_WIFI;
1091 return CONNECTION_ERROR_INVALID_PARAMETER;
1093 switch (statistics_type) {
1094 case CONNECTION_STATISTICS_TYPE_LAST_SENT_DATA:
1095 stat_type = NET_STATISTICS_TYPE_LAST_SENT_DATA;
1097 case CONNECTION_STATISTICS_TYPE_LAST_RECEIVED_DATA:
1098 stat_type = NET_STATISTICS_TYPE_LAST_RECEIVED_DATA;
1100 case CONNECTION_STATISTICS_TYPE_TOTAL_SENT_DATA:
1101 stat_type = NET_STATISTICS_TYPE_TOTAL_SENT_DATA;
1103 case CONNECTION_STATISTICS_TYPE_TOTAL_RECEIVED_DATA:
1104 stat_type = NET_STATISTICS_TYPE_TOTAL_RECEIVED_DATA;
1107 return CONNECTION_ERROR_INVALID_PARAMETER;
1110 rv = _connection_libnet_set_statistics(conn_type, stat_type);
1111 if(rv != CONNECTION_ERROR_NONE)
1115 CONNECTION_LOG(CONNECTION_INFO,"connection_reset_statistics success\n");
1117 return CONNECTION_ERROR_NONE;
1120 EXPORT_API int connection_get_statistics(connection_h connection,
1121 connection_type_e connection_type,
1122 connection_statistics_type_e statistics_type, long long* size)
1124 CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE);
1126 if(connection_type == CONNECTION_TYPE_CELLULAR )
1127 CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE);
1128 else if(connection_type == CONNECTION_TYPE_WIFI)
1129 CHECK_FEATURE_SUPPORTED(WIFI_FEATURE);
1131 if (!(__connection_check_handle_validity(connection)) || size == NULL) {
1132 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
1133 return CONNECTION_ERROR_INVALID_PARAMETER;
1136 return __get_statistic(connection_type, statistics_type, size);
1139 EXPORT_API int connection_reset_statistics(connection_h connection,
1140 connection_type_e connection_type,
1141 connection_statistics_type_e statistics_type)
1143 CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE);
1145 if(connection_type == CONNECTION_TYPE_CELLULAR )
1146 CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE);
1147 else if(connection_type == CONNECTION_TYPE_WIFI)
1148 CHECK_FEATURE_SUPPORTED(WIFI_FEATURE);
1150 if (!(__connection_check_handle_validity(connection))) {
1151 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed");
1152 return CONNECTION_ERROR_INVALID_PARAMETER;
1155 return __reset_statistic(connection_type, statistics_type);