Add new API to check whether metered or non metered
[platform/core/api/connection.git] / src / connection.c
index 8c49912..2b74071 100755 (executable)
@@ -205,6 +205,7 @@ static void *__connection_get_ip_changed_userdata(
 static gboolean __connection_cb_ip_changed_cb_idle(gpointer user_data)
 {
        char *ip_addr;
+       char *ip6_addr;
        void *data;
        connection_address_changed_cb callback;
        connection_handle_s *local_handle = (connection_handle_s *)user_data;
@@ -217,13 +218,19 @@ static gboolean __connection_cb_ip_changed_cb_idle(gpointer user_data)
                CONNECTION_LOG(CONNECTION_ERROR, //LCOV_EXCL_LINE
                        "vconf_get_str(VCONFKEY_NETWORK_IP) is Failed");
 
+       ip6_addr = vconf_get_str(VCONFKEY_NETWORK_IP6);
+       if (ip6_addr == NULL)
+               CONNECTION_LOG(CONNECTION_ERROR, //LCOV_EXCL_LINE
+                       "vconf_get_str(VCONFKEY_NETWORK_IP6) is Failed");
+
        callback = __connection_get_ip_changed_callback(local_handle);
        data = __connection_get_ip_changed_userdata(local_handle);
-       /* TODO: IPv6 should be supported */
+
        if (callback)
-               callback(ip_addr, NULL, data);
+               callback(ip_addr, ip6_addr, data);
 
        free(ip_addr);
+       free(ip6_addr);
 
        return FALSE;
 }
@@ -254,9 +261,12 @@ static int __connection_set_ip_changed_callback(connection_h connection,
        local_handle = (connection_handle_s *)connection;
 
        if (callback) {
-               if (refcount == 0)
+               if (refcount == 0) {
                        vconf_notify_key_changed(VCONFKEY_NETWORK_IP,
                                        __connection_cb_ip_change_cb, NULL);
+                       vconf_notify_key_changed(VCONFKEY_NETWORK_IP6,
+                                       __connection_cb_ip_change_cb, NULL);
+               }
 
                refcount++;
                CONNECTION_LOG(CONNECTION_INFO, "Successfully registered(%d)", refcount);
@@ -272,6 +282,14 @@ static int __connection_set_ip_changed_callback(connection_h connection,
                                        CONNECTION_LOG(CONNECTION_INFO,
                                                        "Successfully de-registered(%d)", refcount);
                                }
+                               if (vconf_ignore_key_changed(VCONFKEY_NETWORK_IP6,
+                                               __connection_cb_ip_change_cb) < 0) {
+                                       CONNECTION_LOG(CONNECTION_ERROR, //LCOV_EXCL_LINE
+                                                       "Error to de-register vconf callback(%d)", refcount);
+                               } else {
+                                       CONNECTION_LOG(CONNECTION_INFO,
+                                                       "Successfully de-registered(%d)", refcount);
+                               }
                        }
                }
        }
@@ -375,7 +393,7 @@ static int __connection_set_proxy_changed_callback(connection_h connection,
 }
 
 static int __connection_set_ethernet_cable_state_changed_cb(connection_h connection,
-               connection_ethernet_cable_state_chaged_cb callback, void *user_data)
+               connection_ethernet_cable_state_changed_cb callback, void *user_data)
 {
        connection_handle_s *local_handle = (connection_handle_s *)connection;
 
@@ -385,7 +403,8 @@ static int __connection_set_ethernet_cable_state_changed_cb(connection_h connect
                                        __connection_cb_ethernet_cable_state_changed_cb);
 
        } else {
-               if (__connection_get_ethernet_cable_state_changed_callback_count() == 1)
+               if (__connection_get_ethernet_cable_state_changed_callback_count() == 1 &&
+                                 local_handle->ethernet_cable_state_changed_callback)
                        _connection_libnet_set_ethernet_cable_state_changed_cb(NULL);
        }
 
@@ -496,9 +515,11 @@ EXPORT_API int connection_get_ip_address(connection_h connection,
 
        switch (address_family) {
        case CONNECTION_ADDRESS_FAMILY_IPV4:
-       case CONNECTION_ADDRESS_FAMILY_IPV6:
                *ip_address = vconf_get_str(VCONFKEY_NETWORK_IP);
                break;
+       case CONNECTION_ADDRESS_FAMILY_IPV6:
+               *ip_address = vconf_get_str(VCONFKEY_NETWORK_IP6);
+               break;
        default:
                CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
                return CONNECTION_ERROR_INVALID_PARAMETER;
@@ -628,7 +649,7 @@ EXPORT_API int connection_get_mac_address(connection_h connection, connection_ty
 
                break;
        //LCOV_EXCL_STOP
-       default :
+       default:
                CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter"); //LCOV_EXCL_LINE
                return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
        }
@@ -646,6 +667,26 @@ EXPORT_API int connection_get_mac_address(connection_h connection, connection_ty
 }
 
 
+EXPORT_API int connection_is_metered_network(connection_h connection, bool* is_metered)
+{
+       CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE);
+
+       if (is_metered == NULL || !(__connection_check_handle_validity(connection))) {
+               CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
+               return CONNECTION_ERROR_INVALID_PARAMETER;
+       }
+
+       int rv = _connection_libnet_get_metered_state(is_metered);
+       if (rv != CONNECTION_ERROR_NONE) {
+               CONNECTION_LOG(CONNECTION_ERROR, "Fail to get metered state[%d]", rv); //LCOV_EXCL_LINE
+               return rv; //LCOV_EXCL_LINE
+       }
+
+       CONNECTION_LOG(CONNECTION_INFO, "metered state: %s", is_metered ? "true" : "false");
+       return CONNECTION_ERROR_NONE;
+}
+
+
 EXPORT_API int connection_get_cellular_state(connection_h connection, connection_cellular_state_e* state)
 {
        int rv = 0;
@@ -760,6 +801,7 @@ EXPORT_API int connection_get_ethernet_cable_state(connection_h connection, conn
 EXPORT_API int connection_set_ethernet_cable_state_chaged_cb(connection_h connection,
                          connection_ethernet_cable_state_chaged_cb callback, void *user_data)
 {
+       DEPRECATED_LOG(__FUNCTION__, "connection_set_ethernet_cable_state_changed_cb");
        CHECK_FEATURE_SUPPORTED(ETHERNET_FEATURE);
 
        if (callback == NULL || !(__connection_check_handle_validity(connection))) {
@@ -767,12 +809,43 @@ EXPORT_API int connection_set_ethernet_cable_state_chaged_cb(connection_h connec
                return CONNECTION_ERROR_INVALID_PARAMETER;
        }
 
+       DEPRECATED_LOG("connection_ethernet_cable_state_chaged_cb",
+                       "connection_ethernet_cable_state_changed_cb");
+
        return __connection_set_ethernet_cable_state_changed_cb(connection,
-                                                       callback, user_data);
+                       (connection_ethernet_cable_state_changed_cb)callback, user_data);
 }
 
 EXPORT_API int connection_unset_ethernet_cable_state_chaged_cb(connection_h connection)
 {
+       DEPRECATED_LOG(__FUNCTION__, "connection_unset_ethernet_cable_state_changed_cb");
+       CHECK_FEATURE_SUPPORTED(ETHERNET_FEATURE);
+
+       if (!(__connection_check_handle_validity(connection))) {
+               CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
+               return CONNECTION_ERROR_INVALID_PARAMETER;
+       }
+
+       return __connection_set_ethernet_cable_state_changed_cb(connection,
+                                                       NULL, NULL);
+}
+
+EXPORT_API int connection_set_ethernet_cable_state_changed_cb(connection_h connection,
+                         connection_ethernet_cable_state_changed_cb callback, void *user_data)
+{
+       CHECK_FEATURE_SUPPORTED(ETHERNET_FEATURE);
+
+       if (callback == NULL || !(__connection_check_handle_validity(connection))) {
+               CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
+               return CONNECTION_ERROR_INVALID_PARAMETER;
+       }
+
+       return __connection_set_ethernet_cable_state_changed_cb(connection,
+                                                       callback, user_data);
+}
+
+EXPORT_API int connection_unset_ethernet_cable_state_changed_cb(connection_h connection)
+{
        CHECK_FEATURE_SUPPORTED(ETHERNET_FEATURE);
 
        if (!(__connection_check_handle_validity(connection))) {
@@ -1155,6 +1228,54 @@ EXPORT_API int connection_remove_route_ipv6(connection_h connection, const char
        return _connection_libnet_remove_route_ipv6(interface_name, host_address, gateway);
 }
 
+EXPORT_API int connection_add_route_entry(connection_h connection,
+               connection_address_family_e address_family,     const char *interface_name,
+               const char *host_address, const char *gateway)
+{
+       CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE, ETHERNET_FEATURE);
+
+       if (!(__connection_check_handle_validity(connection)) ||
+               (address_family != CONNECTION_ADDRESS_FAMILY_IPV4 &&
+            address_family != CONNECTION_ADDRESS_FAMILY_IPV6) ||
+           interface_name == NULL || host_address == NULL) {
+               CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
+               return CONNECTION_ERROR_INVALID_PARAMETER;
+       }
+
+       if (address_family == CONNECTION_ADDRESS_FAMILY_IPV4)
+               return _connection_libnet_add_route_entry(CONNECTION_ADDRESS_FAMILY_IPV4,
+                                                               interface_name, host_address, gateway);
+       else
+               return _connection_libnet_add_route_entry(CONNECTION_ADDRESS_FAMILY_IPV6,
+                                                               interface_name, host_address, gateway);
+
+       return CONNECTION_ERROR_NONE;
+}
+
+EXPORT_API int connection_remove_route_entry(connection_h connection,
+               connection_address_family_e address_family,     const char *interface_name,
+               const char *host_address, const char *gateway)
+{
+       CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE, ETHERNET_FEATURE);
+
+       if (!(__connection_check_handle_validity(connection)) ||
+               (address_family != CONNECTION_ADDRESS_FAMILY_IPV4 &&
+            address_family != CONNECTION_ADDRESS_FAMILY_IPV6) ||
+           interface_name == NULL || host_address == NULL) {
+               CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
+               return CONNECTION_ERROR_INVALID_PARAMETER;
+       }
+
+       if (address_family == CONNECTION_ADDRESS_FAMILY_IPV4)
+               return _connection_libnet_remove_route_entry(CONNECTION_ADDRESS_FAMILY_IPV4,
+                                                               interface_name, host_address, gateway);
+       else
+               return _connection_libnet_remove_route_entry(CONNECTION_ADDRESS_FAMILY_IPV6,
+                                                               interface_name, host_address, gateway);
+
+       return CONNECTION_ERROR_NONE;
+}
+
 static int __get_cellular_statistic(connection_statistics_type_e statistics_type, long long *llsize)
 {
        int rv = VCONF_OK, rv1 = VCONF_OK;
@@ -1377,3 +1498,60 @@ EXPORT_API int connection_reset_statistics(connection_h connection,
        return __reset_statistic(connection_type, statistics_type);
 }
 
+EXPORT_API int connection_foreach_ipv6_address(connection_h connection,
+               connection_type_e connection_type, connection_ipv6_address_cb callback,
+               void *user_data)
+{
+       CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE,
+                       TETHERING_BLUETOOTH_FEATURE, ETHERNET_FEATURE);
+
+       GSList *ipv6_address_list = NULL;
+
+       if (!(__connection_check_handle_validity(connection))) {
+               CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
+               return CONNECTION_ERROR_INVALID_PARAMETER;
+       }
+
+       int rv = CONNECTION_ERROR_NONE;
+
+       switch (connection_type) {
+       case CONNECTION_TYPE_WIFI:
+               rv = net_foreach_ipv6_address(NET_DEVICE_WIFI,
+                               &ipv6_address_list);
+               break;
+       case CONNECTION_TYPE_CELLULAR:
+               rv = net_foreach_ipv6_address(NET_DEVICE_CELLULAR,
+                               &ipv6_address_list);
+               break;
+       case CONNECTION_TYPE_ETHERNET:
+               rv = net_foreach_ipv6_address(NET_DEVICE_ETHERNET,
+                               &ipv6_address_list);
+               break;
+       case CONNECTION_TYPE_BT:
+               rv = net_foreach_ipv6_address(NET_DEVICE_BLUETOOTH,
+                               &ipv6_address_list);
+               break;
+       default:
+               CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
+               return CONNECTION_ERROR_INVALID_PARAMETER;
+       }
+
+       if (rv != NET_ERR_NONE) {
+               CONNECTION_LOG(CONNECTION_ERROR, "net_get_multiple_id_address"
+                               " Failed = %d\n", rv);
+               return CONNECTION_ERROR_OPERATION_FAILED;
+       }
+
+       GSList *list;
+       for (list = ipv6_address_list; list; list = list->next) {
+               rv = callback((char *)list->data, user_data);
+               if (rv == false)
+                       break;
+       }
+
+       g_slist_free_full(ipv6_address_list, g_free);
+       ipv6_address_list = NULL;
+
+       return CONNECTION_ERROR_NONE;
+}
+