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 CONNECTION_MUTEX_LOCK;
298 if (connection == NULL || __connection_check_handle_validity(*connection)) {
299 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
300 CONNECTION_MUTEX_UNLOCK;
301 return CONNECTION_ERROR_INVALID_PARAMETER;
304 rv = _connection_libnet_init();
305 if (rv == NET_ERR_ACCESS_DENIED) {
306 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
307 CONNECTION_MUTEX_UNLOCK;
308 return CONNECTION_ERROR_PERMISSION_DENIED;
310 else if (rv != NET_ERR_NONE) {
311 CONNECTION_LOG(CONNECTION_ERROR, "Failed to create connection[%d]", rv);
312 CONNECTION_MUTEX_UNLOCK;
313 return CONNECTION_ERROR_OPERATION_FAILED;
316 *connection = g_try_malloc0(sizeof(connection_handle_s));
317 if (*connection != NULL) {
318 CONNECTION_LOG(CONNECTION_INFO, "New Handle Created %p\n", *connection);
320 CONNECTION_MUTEX_UNLOCK;
321 return CONNECTION_ERROR_OUT_OF_MEMORY;
324 conn_handle_list = g_slist_append(conn_handle_list, *connection);
326 CONNECTION_MUTEX_UNLOCK;
327 return CONNECTION_ERROR_NONE;
330 EXPORT_API int connection_destroy(connection_h connection)
332 CONNECTION_MUTEX_LOCK;
334 if (connection == NULL || !(__connection_check_handle_validity(connection))) {
335 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
336 CONNECTION_MUTEX_UNLOCK;
337 return CONNECTION_ERROR_INVALID_PARAMETER;
340 CONNECTION_LOG(CONNECTION_INFO, "Destroy Handle : %p\n", connection);
342 __connection_set_type_changed_callback(connection, NULL, NULL);
343 __connection_set_ip_changed_callback(connection, NULL, NULL);
344 __connection_set_proxy_changed_callback(connection, NULL, NULL);
345 __connection_set_ethernet_cable_state_changed_cb(connection, NULL, NULL);
347 conn_handle_list = g_slist_remove(conn_handle_list, connection);
351 if (__connection_get_handle_count() == 0)
352 _connection_libnet_deinit();
354 CONNECTION_MUTEX_UNLOCK;
355 return CONNECTION_ERROR_NONE;
358 EXPORT_API int connection_get_type(connection_h connection, connection_type_e* type)
362 if (type == NULL || !(__connection_check_handle_validity(connection))) {
363 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
364 return CONNECTION_ERROR_INVALID_PARAMETER;
367 if (vconf_get_int(VCONFKEY_NETWORK_STATUS, &status)) {
368 CONNECTION_LOG(CONNECTION_ERROR, "vconf_get_int Failed = %d\n", status);
369 return CONNECTION_ERROR_OPERATION_FAILED;
372 CONNECTION_LOG(CONNECTION_INFO, "Connected Network = %d\n", status);
374 *type = __connection_convert_net_state(status);
376 return CONNECTION_ERROR_NONE;
379 EXPORT_API int connection_get_ip_address(connection_h connection,
380 connection_address_family_e address_family, char **ip_address)
382 if (ip_address == NULL || !(__connection_check_handle_validity(connection))) {
383 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
384 return CONNECTION_ERROR_INVALID_PARAMETER;
387 switch (address_family) {
388 case CONNECTION_ADDRESS_FAMILY_IPV4:
389 case CONNECTION_ADDRESS_FAMILY_IPV6:
390 *ip_address = vconf_get_str(VCONFKEY_NETWORK_IP);
394 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
395 return CONNECTION_ERROR_INVALID_PARAMETER;
398 if (*ip_address == NULL) {
399 CONNECTION_LOG(CONNECTION_ERROR, "vconf_get_str Failed");
400 return CONNECTION_ERROR_OPERATION_FAILED;
403 return CONNECTION_ERROR_NONE;
406 EXPORT_API int connection_get_proxy(connection_h connection,
407 connection_address_family_e address_family, char **proxy)
409 if (proxy == NULL || !(__connection_check_handle_validity(connection))) {
410 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
411 return CONNECTION_ERROR_INVALID_PARAMETER;
414 switch (address_family) {
415 case CONNECTION_ADDRESS_FAMILY_IPV4:
416 case CONNECTION_ADDRESS_FAMILY_IPV6:
417 *proxy = vconf_get_str(VCONFKEY_NETWORK_PROXY);
421 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
422 return CONNECTION_ERROR_INVALID_PARAMETER;
425 if (*proxy == NULL) {
426 CONNECTION_LOG(CONNECTION_ERROR, "vconf_get_str Failed");
427 return CONNECTION_ERROR_OPERATION_FAILED;
430 return CONNECTION_ERROR_NONE;
433 EXPORT_API int connection_get_mac_address(connection_h connection, connection_type_e type, char** mac_addr)
436 char buf[CONNECTION_MAC_INFO_LENGTH + 1];
438 if (mac_addr == NULL || !(__connection_check_handle_validity(connection))) {
439 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
440 return CONNECTION_ERROR_INVALID_PARAMETER;
444 case CONNECTION_TYPE_WIFI:
445 fp = fopen(WIFI_MAC_INFO_FILE, "r");
447 CONNECTION_LOG(CONNECTION_ERROR, "Failed to open file %s", WIFI_MAC_INFO_FILE);
448 return CONNECTION_ERROR_OUT_OF_MEMORY;
451 if (fgets(buf, sizeof(buf), fp) == NULL) {
452 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get MAC info from %s", WIFI_MAC_INFO_FILE);
454 return CONNECTION_ERROR_OPERATION_FAILED;
457 CONNECTION_LOG(CONNECTION_INFO, "%s : %s", WIFI_MAC_INFO_FILE, buf);
459 *mac_addr = (char *)malloc(CONNECTION_MAC_INFO_LENGTH + 1);
460 if (*mac_addr == NULL) {
461 CONNECTION_LOG(CONNECTION_ERROR, "malloc() failed");
463 return CONNECTION_ERROR_OUT_OF_MEMORY;
465 g_strlcpy(*mac_addr, buf, CONNECTION_MAC_INFO_LENGTH + 1);
468 case CONNECTION_TYPE_ETHERNET:
469 fp = fopen(ETHERNET_MAC_INFO_FILE, "r");
471 CONNECTION_LOG(CONNECTION_ERROR, "Failed to open file %s", ETHERNET_MAC_INFO_FILE);
472 return CONNECTION_ERROR_OUT_OF_MEMORY;
475 if (fgets(buf, sizeof(buf), fp) == NULL) {
476 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get MAC info from %s", ETHERNET_MAC_INFO_FILE);
478 return CONNECTION_ERROR_OPERATION_FAILED;
481 CONNECTION_LOG(CONNECTION_INFO, "%s : %s", ETHERNET_MAC_INFO_FILE, buf);
483 *mac_addr = (char *)malloc(CONNECTION_MAC_INFO_LENGTH + 1);
484 if (*mac_addr == NULL) {
485 CONNECTION_LOG(CONNECTION_ERROR, "malloc() failed");
487 return CONNECTION_ERROR_OUT_OF_MEMORY;
490 g_strlcpy(*mac_addr, buf,CONNECTION_MAC_INFO_LENGTH + 1);
495 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
496 return CONNECTION_ERROR_INVALID_PARAMETER;
499 /* Checking Invalid MAC Address */
500 if((strcmp(*mac_addr, "00:00:00:00:00:00") == 0) ||
501 (strcmp(*mac_addr, "ff:ff:ff:ff:ff:ff") == 0)) {
502 CONNECTION_LOG(CONNECTION_ERROR, "MAC Address(%s) is invalid", *mac_addr);
503 return CONNECTION_ERROR_INVALID_OPERATION;
506 CONNECTION_LOG(CONNECTION_INFO, "MAC Address %s", *mac_addr);
508 return CONNECTION_ERROR_NONE;
511 EXPORT_API int connection_get_cellular_state(connection_h connection, connection_cellular_state_e* state)
514 int cellular_state = 0;
516 if (state == NULL || !(__connection_check_handle_validity(connection))) {
517 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
518 return CONNECTION_ERROR_INVALID_PARAMETER;
521 if (!vconf_get_int(VCONFKEY_NETWORK_CELLULAR_STATE, &status)) {
522 CONNECTION_LOG(CONNECTION_INFO, "Cellular = %d\n", status);
523 *state = __connection_convert_cellular_state(status);
525 if (*state == CONNECTION_CELLULAR_STATE_AVAILABLE) {
526 if (vconf_get_int(VCONFKEY_DNET_STATE, &cellular_state)) {
527 CONNECTION_LOG(CONNECTION_ERROR,
528 "vconf_get_int Failed = %d\n", cellular_state);
529 return CONNECTION_ERROR_OPERATION_FAILED;
533 CONNECTION_LOG(CONNECTION_INFO, "Connection state = %d\n", cellular_state);
535 if (cellular_state == VCONFKEY_DNET_NORMAL_CONNECTED ||
536 cellular_state == VCONFKEY_DNET_SECURE_CONNECTED ||
537 cellular_state == VCONFKEY_DNET_TRANSFER)
538 *state = CONNECTION_CELLULAR_STATE_CONNECTED;
540 return CONNECTION_ERROR_NONE;
542 CONNECTION_LOG(CONNECTION_ERROR, "vconf_get_int Failed = %d\n", status);
543 return CONNECTION_ERROR_OPERATION_FAILED;
547 EXPORT_API int connection_get_wifi_state(connection_h connection, connection_wifi_state_e* state)
551 if (state == NULL || !(__connection_check_handle_validity(connection))) {
552 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
553 return CONNECTION_ERROR_INVALID_PARAMETER;
556 rv = _connection_libnet_get_wifi_state(state);
557 if (rv != CONNECTION_ERROR_NONE) {
558 CONNECTION_LOG(CONNECTION_ERROR, "Fail to get Wi-Fi state[%d]", rv);
562 CONNECTION_LOG(CONNECTION_INFO, "WiFi state = %d\n", *state);
564 return CONNECTION_ERROR_NONE;
567 EXPORT_API int connection_get_ethernet_state(connection_h connection, connection_ethernet_state_e* state)
569 if (state == NULL || !(__connection_check_handle_validity(connection))) {
570 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
571 return CONNECTION_ERROR_INVALID_PARAMETER;
574 return _connection_libnet_get_ethernet_state(state);
577 EXPORT_API int connection_get_ethernet_cable_state(connection_h connection, connection_ethernet_cable_state_e *state)
579 if (state == NULL || !(__connection_check_handle_validity(connection))) {
580 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
581 return CONNECTION_ERROR_INVALID_PARAMETER;
584 return _connection_libnet_get_ethernet_cable_state(state);
587 EXPORT_API int connection_set_ethernet_cable_state_chaged_cb(connection_h connection,
588 connection_ethernet_cable_state_chaged_cb callback, void *user_data)
590 if (callback == NULL || !(__connection_check_handle_validity(connection))) {
591 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
592 return CONNECTION_ERROR_INVALID_PARAMETER;
595 return __connection_set_ethernet_cable_state_changed_cb(connection,
596 callback, user_data);
599 EXPORT_API int connection_unset_ethernet_cable_state_chaged_cb(connection_h connection)
601 if ( !(__connection_check_handle_validity(connection)) ) {
602 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
603 return CONNECTION_ERROR_INVALID_PARAMETER;
606 return __connection_set_ethernet_cable_state_changed_cb(connection,
610 EXPORT_API int connection_get_bt_state(connection_h connection, connection_bt_state_e* state)
612 if (state == NULL || !(__connection_check_handle_validity(connection))) {
613 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
614 return CONNECTION_ERROR_INVALID_PARAMETER;
617 return _connection_libnet_get_bluetooth_state(state);
621 EXPORT_API int connection_set_type_changed_cb(connection_h connection,
622 connection_type_changed_cb callback, void* user_data)
624 if (callback == NULL || !(__connection_check_handle_validity(connection))) {
625 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
626 return CONNECTION_ERROR_INVALID_PARAMETER;
629 return __connection_set_type_changed_callback(connection, callback, user_data);
632 EXPORT_API int connection_unset_type_changed_cb(connection_h connection)
634 if (!(__connection_check_handle_validity(connection))) {
635 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
636 return CONNECTION_ERROR_INVALID_PARAMETER;
639 return __connection_set_type_changed_callback(connection, NULL, NULL);
642 EXPORT_API int connection_set_ip_address_changed_cb(connection_h connection,
643 connection_address_changed_cb callback, void* user_data)
645 if (callback == NULL || !(__connection_check_handle_validity(connection))) {
646 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
647 return CONNECTION_ERROR_INVALID_PARAMETER;
650 return __connection_set_ip_changed_callback(connection, callback, user_data);
653 EXPORT_API int connection_unset_ip_address_changed_cb(connection_h connection)
655 if (!(__connection_check_handle_validity(connection))) {
656 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
657 return CONNECTION_ERROR_INVALID_PARAMETER;
660 return __connection_set_ip_changed_callback(connection, NULL, NULL);
663 EXPORT_API int connection_set_proxy_address_changed_cb(connection_h connection,
664 connection_address_changed_cb callback, void* user_data)
666 if (callback == NULL || !(__connection_check_handle_validity(connection))) {
667 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
668 return CONNECTION_ERROR_INVALID_PARAMETER;
671 return __connection_set_proxy_changed_callback(connection, callback, user_data);
674 EXPORT_API int connection_unset_proxy_address_changed_cb(connection_h connection)
676 if (!(__connection_check_handle_validity(connection))) {
677 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
678 return CONNECTION_ERROR_INVALID_PARAMETER;
681 return __connection_set_proxy_changed_callback(connection, NULL, NULL);
684 EXPORT_API int connection_add_profile(connection_h connection, connection_profile_h profile)
686 if (!(__connection_check_handle_validity(connection)) ||
687 !(_connection_libnet_check_profile_validity(profile))) {
688 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
689 return CONNECTION_ERROR_INVALID_PARAMETER;
694 net_profile_info_t *profile_info = profile;
696 if (profile_info->profile_type != NET_DEVICE_CELLULAR) {
697 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
698 return CONNECTION_ERROR_INVALID_PARAMETER;
701 rv = net_add_profile(profile_info->ProfileInfo.Pdp.ServiceType, (net_profile_info_t*)profile);
702 if (rv == NET_ERR_ACCESS_DENIED) {
703 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
704 return CONNECTION_ERROR_PERMISSION_DENIED;
705 } else if (rv != NET_ERR_NONE) {
706 CONNECTION_LOG(CONNECTION_ERROR, "Failed to add profile[%d]", rv);
707 return CONNECTION_ERROR_OPERATION_FAILED;
710 return CONNECTION_ERROR_NONE;
713 EXPORT_API int connection_remove_profile(connection_h connection, connection_profile_h profile)
715 if (!(__connection_check_handle_validity(connection)) ||
716 !(_connection_libnet_check_profile_validity(profile))) {
717 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
718 return CONNECTION_ERROR_INVALID_PARAMETER;
722 net_profile_info_t *profile_info = profile;
724 if (profile_info->profile_type != NET_DEVICE_CELLULAR &&
725 profile_info->profile_type != NET_DEVICE_WIFI) {
726 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
727 return CONNECTION_ERROR_INVALID_PARAMETER;
730 rv = net_delete_profile(profile_info->ProfileName);
731 if (rv == NET_ERR_ACCESS_DENIED) {
732 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
733 return CONNECTION_ERROR_PERMISSION_DENIED;
734 } else if (rv != NET_ERR_NONE) {
735 CONNECTION_LOG(CONNECTION_ERROR, "Failed to delete profile[%d]", rv);
736 return CONNECTION_ERROR_OPERATION_FAILED;
739 return CONNECTION_ERROR_NONE;
742 EXPORT_API int connection_update_profile(connection_h connection, connection_profile_h profile)
744 if (!(__connection_check_handle_validity(connection)) ||
745 !(_connection_libnet_check_profile_validity(profile))) {
746 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
747 return CONNECTION_ERROR_INVALID_PARAMETER;
751 net_profile_info_t *profile_info = profile;
753 rv = net_modify_profile(profile_info->ProfileName, (net_profile_info_t*)profile);
754 if (rv == NET_ERR_ACCESS_DENIED) {
755 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
756 return CONNECTION_ERROR_PERMISSION_DENIED;
757 } else if (rv != NET_ERR_NONE) {
758 CONNECTION_LOG(CONNECTION_ERROR, "Failed to modify profile[%d]", rv);
759 return CONNECTION_ERROR_OPERATION_FAILED;
762 return CONNECTION_ERROR_NONE;
765 EXPORT_API int connection_get_profile_iterator(connection_h connection,
766 connection_iterator_type_e type, connection_profile_iterator_h* profile_iterator)
768 if (!(__connection_check_handle_validity(connection)) ||
769 (type != CONNECTION_ITERATOR_TYPE_REGISTERED &&
770 type != CONNECTION_ITERATOR_TYPE_CONNECTED &&
771 type != CONNECTION_ITERATOR_TYPE_DEFAULT)) {
772 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
773 return CONNECTION_ERROR_INVALID_PARAMETER;
776 return _connection_libnet_get_profile_iterator(type, profile_iterator);
779 EXPORT_API int connection_profile_iterator_next(connection_profile_iterator_h profile_iterator,
780 connection_profile_h* profile)
782 return _connection_libnet_get_iterator_next(profile_iterator, profile);
785 EXPORT_API bool connection_profile_iterator_has_next(connection_profile_iterator_h profile_iterator)
787 return _connection_libnet_iterator_has_next(profile_iterator);
790 EXPORT_API int connection_destroy_profile_iterator(connection_profile_iterator_h profile_iterator)
792 return _connection_libnet_destroy_iterator(profile_iterator);
795 EXPORT_API int connection_get_current_profile(connection_h connection, connection_profile_h* profile)
797 if (!(__connection_check_handle_validity(connection)) || profile == NULL) {
798 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
799 return CONNECTION_ERROR_INVALID_PARAMETER;
802 return _connection_libnet_get_current_profile(profile);
805 EXPORT_API int connection_get_default_cellular_service_profile(connection_h connection,
806 connection_cellular_service_type_e type, connection_profile_h* profile)
808 if (!(__connection_check_handle_validity(connection)) || profile == NULL) {
809 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
810 return CONNECTION_ERROR_INVALID_PARAMETER;
813 return _connection_libnet_get_cellular_service_profile(type, profile);
816 EXPORT_API int connection_set_default_cellular_service_profile(connection_h connection,
817 connection_cellular_service_type_e type, connection_profile_h profile)
819 if (!(__connection_check_handle_validity(connection)) || profile == NULL) {
820 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
821 return CONNECTION_ERROR_INVALID_PARAMETER;
824 return _connection_libnet_set_cellular_service_profile_sync(type, profile);
827 EXPORT_API int connection_set_default_cellular_service_profile_async(connection_h connection,
828 connection_cellular_service_type_e type, connection_profile_h profile,
829 connection_set_default_cb callback, void* user_data)
831 if (!(__connection_check_handle_validity(connection)) ||
832 profile == NULL || callback == NULL) {
833 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
834 return CONNECTION_ERROR_INVALID_PARAMETER;
837 return _connection_libnet_set_cellular_service_profile_async(type, profile, callback, user_data);
840 EXPORT_API int connection_open_profile(connection_h connection, connection_profile_h profile,
841 connection_opened_cb callback, void* user_data)
843 if (!(__connection_check_handle_validity(connection)) ||
844 profile == NULL || callback == NULL) {
845 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
846 return CONNECTION_ERROR_INVALID_PARAMETER;
849 return _connection_libnet_open_profile(profile, callback, user_data);
852 EXPORT_API int connection_close_profile(connection_h connection, connection_profile_h profile,
853 connection_closed_cb callback, void* user_data)
855 if (!(__connection_check_handle_validity(connection)) ||
856 profile == NULL || callback == NULL) {
857 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
858 return CONNECTION_ERROR_INVALID_PARAMETER;
861 return _connection_libnet_close_profile(profile, callback, user_data);
864 EXPORT_API int connection_reset_profile(connection_h connection,
865 connection_reset_option_e type, int id, connection_reset_cb callback, void *user_data)
867 if (!(__connection_check_handle_validity(connection))) {
868 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed");
869 return CONNECTION_ERROR_INVALID_PARAMETER;
872 if(id < 0 || id > 1) {
873 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed");
874 return CONNECTION_ERROR_INVALID_PARAMETER;
877 return _connection_libnet_reset_profile(type, id, callback, user_data);
880 EXPORT_API int connection_add_route(connection_h connection, const char* interface_name, const char* host_address)
882 if (!(__connection_check_handle_validity(connection)) ||
883 interface_name == NULL || host_address == NULL) {
884 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
885 return CONNECTION_ERROR_INVALID_PARAMETER;
888 return _connection_libnet_add_route(interface_name, host_address);
891 EXPORT_API int connection_remove_route(connection_h connection, const char* interface_name, const char* host_address)
893 if (!(__connection_check_handle_validity(connection)) ||
894 interface_name == NULL || host_address == NULL) {
895 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
896 return CONNECTION_ERROR_INVALID_PARAMETER;
899 return _connection_libnet_remove_route(interface_name, host_address);
902 EXPORT_API int connection_add_route_ipv6(connection_h connection, const char *interface_name, const char *host_address, const char * gateway)
904 if (!(__connection_check_handle_validity(connection)) ||
905 interface_name == NULL || host_address == NULL) {
906 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
907 return CONNECTION_ERROR_INVALID_PARAMETER;
910 return _connection_libnet_add_route_ipv6(interface_name, host_address, gateway);
913 EXPORT_API int connection_remove_route_ipv6(connection_h connection, const char *interface_name, const char *host_address, const char * gateway)
915 if (!(__connection_check_handle_validity(connection)) ||
916 interface_name == NULL || host_address == NULL) {
917 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
918 return CONNECTION_ERROR_INVALID_PARAMETER;
921 return _connection_libnet_remove_route_ipv6(interface_name, host_address, gateway);
924 /* Connection Statistics module ******************************************************************/
926 static int __get_statistic(connection_type_e connection_type,
927 connection_statistics_type_e statistics_type, long long* llsize)
930 unsigned long long ull_size;
934 if (llsize == NULL) {
935 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
936 return CONNECTION_ERROR_INVALID_PARAMETER;
939 if (connection_type == CONNECTION_TYPE_CELLULAR) {
940 switch (statistics_type) {
941 case CONNECTION_STATISTICS_TYPE_LAST_SENT_DATA:
942 key = VCONFKEY_NETWORK_CELLULAR_PKT_LAST_SNT;
944 case CONNECTION_STATISTICS_TYPE_LAST_RECEIVED_DATA:
945 key = VCONFKEY_NETWORK_CELLULAR_PKT_LAST_RCV;
947 case CONNECTION_STATISTICS_TYPE_TOTAL_SENT_DATA:
948 key = VCONFKEY_NETWORK_CELLULAR_PKT_TOTAL_SNT;
950 case CONNECTION_STATISTICS_TYPE_TOTAL_RECEIVED_DATA:
951 key = VCONFKEY_NETWORK_CELLULAR_PKT_TOTAL_RCV;
954 return CONNECTION_ERROR_INVALID_PARAMETER;
957 if (vconf_get_int(key, &size)) {
958 CONNECTION_LOG(CONNECTION_ERROR, "Cannot Get %s = %d\n", key, size);
960 return CONNECTION_ERROR_OPERATION_FAILED;
963 CONNECTION_LOG(CONNECTION_INFO,"%s:%d bytes\n", key, size);
964 *llsize = (long long)size;
965 } else if (connection_type == CONNECTION_TYPE_WIFI) {
966 switch (statistics_type) {
967 case CONNECTION_STATISTICS_TYPE_LAST_SENT_DATA:
968 stat_type = NET_STATISTICS_TYPE_LAST_SENT_DATA;
970 case CONNECTION_STATISTICS_TYPE_LAST_RECEIVED_DATA:
971 stat_type = NET_STATISTICS_TYPE_LAST_RECEIVED_DATA;
973 case CONNECTION_STATISTICS_TYPE_TOTAL_SENT_DATA:
974 stat_type = NET_STATISTICS_TYPE_TOTAL_SENT_DATA;
976 case CONNECTION_STATISTICS_TYPE_TOTAL_RECEIVED_DATA:
977 stat_type = NET_STATISTICS_TYPE_TOTAL_RECEIVED_DATA;
980 return CONNECTION_ERROR_INVALID_PARAMETER;
983 rv = _connection_libnet_get_statistics(stat_type, &ull_size);
984 if (rv == CONNECTION_ERROR_PERMISSION_DENIED)
986 else if (rv != CONNECTION_ERROR_NONE) {
987 CONNECTION_LOG(CONNECTION_ERROR, "Failed to get Wi-Fi statistics");
989 return CONNECTION_ERROR_OPERATION_FAILED;
992 CONNECTION_LOG(CONNECTION_INFO,"%d bytes\n", ull_size);
993 *llsize = (long long)ull_size;
995 return CONNECTION_ERROR_INVALID_PARAMETER;
997 return CONNECTION_ERROR_NONE;
1000 static int __reset_statistic(connection_type_e connection_type,
1001 connection_statistics_type_e statistics_type)
1007 if (connection_type == CONNECTION_TYPE_CELLULAR)
1008 conn_type = NET_DEVICE_CELLULAR;
1009 else if (connection_type == CONNECTION_TYPE_WIFI)
1010 conn_type = NET_DEVICE_WIFI;
1012 return CONNECTION_ERROR_INVALID_PARAMETER;
1014 switch (statistics_type) {
1015 case CONNECTION_STATISTICS_TYPE_LAST_SENT_DATA:
1016 stat_type = NET_STATISTICS_TYPE_LAST_SENT_DATA;
1018 case CONNECTION_STATISTICS_TYPE_LAST_RECEIVED_DATA:
1019 stat_type = NET_STATISTICS_TYPE_LAST_RECEIVED_DATA;
1021 case CONNECTION_STATISTICS_TYPE_TOTAL_SENT_DATA:
1022 stat_type = NET_STATISTICS_TYPE_TOTAL_SENT_DATA;
1024 case CONNECTION_STATISTICS_TYPE_TOTAL_RECEIVED_DATA:
1025 stat_type = NET_STATISTICS_TYPE_TOTAL_RECEIVED_DATA;
1028 return CONNECTION_ERROR_INVALID_PARAMETER;
1031 rv = _connection_libnet_set_statistics(conn_type, stat_type);
1032 if(rv != CONNECTION_ERROR_NONE)
1036 CONNECTION_LOG(CONNECTION_INFO,"connection_reset_statistics success\n");
1038 return CONNECTION_ERROR_NONE;
1041 EXPORT_API int connection_get_statistics(connection_h connection,
1042 connection_type_e connection_type,
1043 connection_statistics_type_e statistics_type, long long* size)
1045 if (!(__connection_check_handle_validity(connection)) || size == NULL) {
1046 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
1047 return CONNECTION_ERROR_INVALID_PARAMETER;
1050 return __get_statistic(connection_type, statistics_type, size);
1053 EXPORT_API int connection_reset_statistics(connection_h connection,
1054 connection_type_e connection_type,
1055 connection_statistics_type_e statistics_type)
1057 if (!(__connection_check_handle_validity(connection))) {
1058 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed");
1059 return CONNECTION_ERROR_INVALID_PARAMETER;
1062 return __reset_statistic(connection_type, statistics_type);