From 50aaf9d4c908ff9357cc24b887ec4a962c03bb9d Mon Sep 17 00:00:00 2001 From: Jaehyun Kim Date: Fri, 8 Sep 2017 19:58:37 +0900 Subject: [PATCH] Add new API to check whether metered or non metered Change-Id: I36909e3bea7ec991913ed1218d2489f455b01ce2 Signed-off-by: Jaehyun Kim --- include/net_connection.h | 31 ++++++++++++++++++++++++++----- include/net_connection_private.h | 1 + packaging/capi-network-connection.spec | 2 +- src/connection.c | 20 ++++++++++++++++++++ src/libnetwork.c | 21 +++++++++++++++++++++ test/connection_test.c | 21 +++++++++++++++++++++ 6 files changed, 90 insertions(+), 6 deletions(-) diff --git a/include/net_connection.h b/include/net_connection.h index 2e0c5a3..7db93d7 100755 --- a/include/net_connection.h +++ b/include/net_connection.h @@ -19,7 +19,7 @@ #define __NET_CONNECTION_INTF_H__ -#include "connection_profile.h" +#include #ifdef __cplusplus @@ -340,7 +340,8 @@ typedef void(*connection_address_changed_cb)(const char* ipv4_address, */ typedef void(*connection_set_default_cb)(connection_error_e result, void* user_data); -/* @brief Called with an IPv6 address. +/** + * @brief Called with an IPv6 address. * @since_tizen 4.0 * @remarks If @a ipv6_address is needed outside the callback, a copy should be made. \n * @a ipv6_address will be freed automatically after the execution of this callback. @@ -424,6 +425,24 @@ int connection_get_mac_address(connection_h connection, /** + * @brief Gets if the current connection is metered. + * @since_tizen 4.0 + * @privlevel public + * @privilege %http://tizen.org/privilege/network.get + * @param[in] connection The connection handle + * @param[out] is_metered The value indicating whether it is metered + * @return @c 0 on success, + * otherwise a negative error value + * @retval #CONNECTION_ERROR_NONE Successful + * @retval #CONNECTION_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #CONNECTION_ERROR_OPERATION_FAILED Operation failed + * @retval #CONNECTION_ERROR_PERMISSION_DENIED Permission denied + * @retval #CONNECTION_ERROR_NOT_SUPPORTED Not supported + */ +int connection_is_metered_network(connection_h connection, bool* is_metered); + + +/** * @brief Gets the state of cellular connection. * @details The returned state is for the cellular connection state. * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif @@ -453,7 +472,7 @@ int connection_get_cellular_state(connection_h connection, connection_cellular_s * @retval #CONNECTION_ERROR_INVALID_PARAMETER Invalid parameter * @retval #CONNECTION_ERROR_OPERATION_FAILED Operation failed * @retval #CONNECTION_ERROR_PERMISSION_DENIED Permission denied - * @retval #CONNECTION_ERROR_NOT_SUPPORTED Not supported + * @retval #CONNECTION_ERROR_NOT_SUPPORTED Not supported */ int connection_get_wifi_state(connection_h connection, connection_wifi_state_e* state); @@ -796,6 +815,9 @@ int connection_profile_iterator_next(connection_profile_iterator_h profile_itera * @param[in] profile_iterator The iterator of profile * @return @c true if next element exists, * otherwise @c false if next element doesn't exist + * @exception #CONNECTION_ERROR_NONE Successful + * @exception #CONNECTION_ERROR_NOT_SUPPORTED Not supported + * @see get_last_result() */ bool connection_profile_iterator_has_next(connection_profile_iterator_h profile_iterator); @@ -1165,7 +1187,7 @@ 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); -/* +/** * @brief Gets all IPv6 addresses assigned to the network interface. * @since_tizen 4.0 * @param[in] connection The connection handle @@ -1219,7 +1241,6 @@ int connection_get_statistics(connection_h connection, connection_type_e connect * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif * @privlevel public * @privilege %http://tizen.org/privilege/network.set - * @remarks This API needs both privileges. * @param[in] connection The connection handle * @param[in] connection_type The type of connection \n * Only CONNECTION_TYPE_WIFI and CONNECTION_TYPE_CELLULAR are supported diff --git a/include/net_connection_private.h b/include/net_connection_private.h index 19f132d..797893f 100755 --- a/include/net_connection_private.h +++ b/include/net_connection_private.h @@ -118,6 +118,7 @@ bool _connection_is_created(void); int _connection_libnet_init(void); bool _connection_libnet_deinit(void); +int _connection_libnet_get_metered_state(bool* is_metered); int _connection_libnet_get_wifi_state(connection_wifi_state_e *state); int _connection_libnet_get_ethernet_state(connection_ethernet_state_e *state); int _connection_libnet_get_ethernet_cable_state(connection_ethernet_cable_state_e* state); diff --git a/packaging/capi-network-connection.spec b/packaging/capi-network-connection.spec index 28f179a..0927c0f 100755 --- a/packaging/capi-network-connection.spec +++ b/packaging/capi-network-connection.spec @@ -1,6 +1,6 @@ Name: capi-network-connection Summary: Network Connection library in TIZEN C API -Version: 1.0.102 +Version: 1.0.103 Release: 1 Group: System/Network License: Apache-2.0 diff --git a/src/connection.c b/src/connection.c index 32c4033..2b74071 100755 --- a/src/connection.c +++ b/src/connection.c @@ -667,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; diff --git a/src/libnetwork.c b/src/libnetwork.c index 86c94d7..9e5b09a 100755 --- a/src/libnetwork.c +++ b/src/libnetwork.c @@ -606,6 +606,27 @@ bool _connection_libnet_check_profile_cb_validity(connection_profile_h profile) } //LCOV_EXCL_STOP +int _connection_libnet_get_metered_state(bool* is_metered) +{ + int rv = 0; + int status = 0; + + rv = net_get_metered_state(&status); + if (rv == NET_ERR_ACCESS_DENIED) { + CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); + return CONNECTION_ERROR_PERMISSION_DENIED; + } else if (rv != NET_ERR_NONE) { + CONNECTION_LOG(CONNECTION_ERROR, "Failed to get metered state[%d]", rv); + return CONNECTION_ERROR_OPERATION_FAILED; + } + + if (status == 1) + *is_metered = true; + else + *is_metered = false; + return CONNECTION_ERROR_NONE; +} + int _connection_libnet_get_wifi_state(connection_wifi_state_e *state) { int rv; diff --git a/test/connection_test.c b/test/connection_test.c index 087ce68..815a918 100755 --- a/test/connection_test.c +++ b/test/connection_test.c @@ -2107,6 +2107,23 @@ int test_foreach_ipv6_address(void) return 1; } +int test_is_metered_network(void) +{ + int rv = 0; + bool metered_state; + + rv = connection_is_metered_network(connection, &metered_state); + + if (rv != CONNECTION_ERROR_NONE) { + printf("Fail to get metered state [%s]\n", test_print_error(rv)); + return -1; + } + + printf("Retval = [%s] metered state [%s]\n", + test_print_error(rv), metered_state ? "TRUE" : "FALSE"); + + return 1; +} int main(int argc, char **argv) { @@ -2181,6 +2198,7 @@ gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data) printf("D - Add new route entry\n"); printf("E - Remove route entry\n"); printf("F - Get all IPv6 address\n"); + printf("G - Get metered state\n"); printf(LOG_RED "0 - Exit \n" LOG_END); printf("ENTER - Show options menu.......\n"); } @@ -2300,6 +2318,9 @@ gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data) case 'F': rv = test_foreach_ipv6_address(); break; + case 'G': + rv = test_is_metered_network(); + break; } if (rv == 1) -- 2.7.4