From 9241922dfe4b9ef55aaf09607560f349b7886d0b Mon Sep 17 00:00:00 2001 From: Saurav Babu Date: Tue, 12 Dec 2017 19:53:16 +0530 Subject: [PATCH] connection: Notify network config changed from libnet-client's event This patch removes logic to notify network configuration changed event by using vconf_notify_key_changed() functions. VCONF logic only works in main thread of application, so this patch adds logic to get network configurations changed event from libnet-client so that callbacks can be called in respective threads. Change-Id: I8b2faaae0d6eb14655b6675a4d25c2ae71d7ee55 Signed-off-by: Saurav Babu --- include/net_connection_private.h | 10 ++ src/connection.c | 212 +++++++++++++++------------------------ src/libnetwork.c | 61 ++++++++++- 3 files changed, 148 insertions(+), 135 deletions(-) diff --git a/include/net_connection_private.h b/include/net_connection_private.h index 7fdf8c7..3f3f4ba 100755 --- a/include/net_connection_private.h +++ b/include/net_connection_private.h @@ -114,6 +114,13 @@ typedef struct _connection_handle_s { typedef void(*libnet_ethernet_cable_state_changed_cb) (connection_ethernet_cable_state_e state); +typedef void(*libnet_type_changed_cb) (int type); + +typedef void(*libnet_ip_changed_cb) (connection_address_family_e addr_family, + char *ip_addr); + +typedef void(*libnet_proxy_changed_cb) (char *proxy_addr); + bool _connection_is_created(void); int _connection_libnet_init(void); @@ -172,6 +179,9 @@ int _connection_libnet_start_tcpdump(void); int _connection_libnet_stop_tcpdump(void); int _connection_libnet_get_tcpdump_state(gboolean *tcpdump_state); +void _connection_libnet_set_type_changed_cb(libnet_type_changed_cb callback); +void _connection_libnet_set_ip_changed_cb(libnet_ip_changed_cb callback); +void _connection_libnet_set_proxy_changed_cb(libnet_proxy_changed_cb callback); #ifdef __cplusplus } diff --git a/src/connection.c b/src/connection.c index 0bf2f23..76147ab 100755 --- a/src/connection.c +++ b/src/connection.c @@ -86,33 +86,13 @@ static void *__connection_get_type_changed_userdata( return local_handle->type_changed_user_data; } -static gboolean __connection_cb_type_changed_cb_idle(gpointer user_data) -{ - int state, status; - void *data; - connection_type_changed_cb callback; - connection_handle_s *local_handle = (connection_handle_s *)user_data; - - if (__connection_check_handle_validity((connection_h)local_handle) != true) - return FALSE; - - if (vconf_get_int(VCONFKEY_NETWORK_STATUS, &status) != 0) - return FALSE; - - state = __connection_convert_net_state(status); - - callback = __connection_get_type_changed_callback(local_handle); - data = __connection_get_type_changed_userdata(local_handle); - if (callback) - callback(state, data); - - return FALSE; -} - -static void __connection_cb_type_change_cb(keynode_t *node, void *user_data) +static void __connection_cb_type_change_cb(int type) { GSList *list; connection_h handle; + void *data; + connection_type_changed_cb callback; + int state; if (_connection_is_created() != true) { CONNECTION_LOG(CONNECTION_ERROR, "Application is not registered" @@ -120,9 +100,15 @@ static void __connection_cb_type_change_cb(keynode_t *node, void *user_data) return; } + state = __connection_convert_net_state(type); + for (list = conn_handle_list; list; list = list->next) { handle = (connection_h)list->data; - _connection_callback_add(__connection_cb_type_changed_cb_idle, (gpointer)handle); + + callback = __connection_get_type_changed_callback(handle); + data = __connection_get_type_changed_userdata(handle); + if (callback) + callback(state, data); } } @@ -163,23 +149,19 @@ static int __connection_set_type_changed_callback(connection_h connection, if (callback) { if (refcount == 0) - vconf_notify_key_changed(VCONFKEY_NETWORK_STATUS, - __connection_cb_type_change_cb, NULL); + _connection_libnet_set_type_changed_cb( + __connection_cb_type_change_cb); refcount++; - CONNECTION_LOG(CONNECTION_INFO, "Successfully registered(%d)", refcount); + CONNECTION_LOG(CONNECTION_INFO, "Successfully registered(%d)", + refcount); } else { if (refcount > 0 && __connection_get_type_changed_callback(local_handle) != NULL) { if (--refcount == 0) { - if (vconf_ignore_key_changed(VCONFKEY_NETWORK_STATUS, - __connection_cb_type_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); - } + _connection_libnet_set_type_changed_cb(NULL); + CONNECTION_LOG(CONNECTION_INFO, + "Successfully de-registered(%d)", refcount); } } } @@ -202,43 +184,15 @@ static void *__connection_get_ip_changed_userdata( return local_handle->ip_changed_user_data; } -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; - - if (__connection_check_handle_validity((connection_h)local_handle) != true) - return FALSE; - - ip_addr = vconf_get_str(VCONFKEY_NETWORK_IP); - if (ip_addr == NULL) - 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); - - if (callback) - callback(ip_addr, ip6_addr, data); - - free(ip_addr); - free(ip6_addr); - - return FALSE; -} - -static void __connection_cb_ip_change_cb(keynode_t *node, void *user_data) +static void __connection_cb_ip_change_cb( + connection_address_family_e addr_family, char *ip_addr) { GSList *list; connection_h handle; + char *ip4_addr = NULL; + char *ip6_addr = NULL; + void *data; + connection_address_changed_cb callback; if (_connection_is_created() != true) { CONNECTION_LOG(CONNECTION_ERROR, "Application is not registered" @@ -246,10 +200,40 @@ static void __connection_cb_ip_change_cb(keynode_t *node, void *user_data) return; } + switch (addr_family) { + case CONNECTION_ADDRESS_FAMILY_IPV4: + ip4_addr = g_strdup(ip_addr); + + 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) failed"); + break; + case CONNECTION_ADDRESS_FAMILY_IPV6: + ip6_addr = g_strdup(ip_addr); + + ip4_addr = vconf_get_str(VCONFKEY_NETWORK_IP); + if (ip4_addr == NULL) + CONNECTION_LOG(CONNECTION_ERROR, //LCOV_EXCL_LINE + "vconf_get_str(VCONFKEY_NETWORK_IP) failed"); + break; + default: + CONNECTION_LOG(CONNECTION_ERROR, "Invalid Address Type"); + return; + } + for (list = conn_handle_list; list; list = list->next) { handle = (connection_h)list->data; - _connection_callback_add(__connection_cb_ip_changed_cb_idle, (gpointer)handle); + + callback = __connection_get_ip_changed_callback(handle); + data = __connection_get_ip_changed_userdata(handle); + + if (callback) + callback(ip4_addr, ip6_addr, data); } + + g_free(ip4_addr); + g_free(ip6_addr); } static int __connection_set_ip_changed_callback(connection_h connection, @@ -261,35 +245,20 @@ static int __connection_set_ip_changed_callback(connection_h connection, local_handle = (connection_handle_s *)connection; if (callback) { - 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); - } + if (refcount == 0) + _connection_libnet_set_ip_changed_cb( + __connection_cb_ip_change_cb); refcount++; - CONNECTION_LOG(CONNECTION_INFO, "Successfully registered(%d)", refcount); + CONNECTION_LOG(CONNECTION_INFO, "Successfully registered(%d)", + refcount); } else { if (refcount > 0 && __connection_get_ip_changed_callback(local_handle) != NULL) { if (--refcount == 0) { - if (vconf_ignore_key_changed(VCONFKEY_NETWORK_IP, - __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); - } - 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); - } + _connection_libnet_set_ip_changed_cb(NULL); + CONNECTION_LOG(CONNECTION_INFO, + "Successfully de-registered(%d)", refcount); } } } @@ -312,36 +281,12 @@ static void *__connection_get_proxy_changed_userdata( return local_handle->proxy_changed_user_data; } -static gboolean __connection_cb_proxy_changed_cb_idle(gpointer user_data) -{ - char *proxy; - void *data; - connection_address_changed_cb callback; - connection_handle_s *local_handle = (connection_handle_s *)user_data; - - if (__connection_check_handle_validity((connection_h)local_handle) != true) - return FALSE; - - proxy = vconf_get_str(VCONFKEY_NETWORK_PROXY); - if (proxy == NULL) - CONNECTION_LOG(CONNECTION_ERROR, //LCOV_EXCL_LINE - "vconf_get_str(VCONFKEY_NETWORK_PROXY) is Failed"); - - callback = __connection_get_proxy_changed_callback(local_handle); - data = __connection_get_proxy_changed_userdata(local_handle); - /* TODO: IPv6 should be supported */ - if (callback) - callback(proxy, NULL, data); - - free(proxy); - - return FALSE; -} - -static void __connection_cb_proxy_change_cb(keynode_t *node, void *user_data) +static void __connection_cb_proxy_change_cb(char *proxy_addr) { GSList *list; connection_h handle; + void *data; + connection_address_changed_cb callback; if (_connection_is_created() != true) { CONNECTION_LOG(CONNECTION_ERROR, "Application is not registered" @@ -351,7 +296,12 @@ static void __connection_cb_proxy_change_cb(keynode_t *node, void *user_data) for (list = conn_handle_list; list; list = list->next) { handle = (connection_h)list->data; - _connection_callback_add(__connection_cb_proxy_changed_cb_idle, (gpointer)handle); + + callback = __connection_get_proxy_changed_callback(handle); + data = __connection_get_proxy_changed_userdata(handle); + /* TODO: IPv6 should be supported */ + if (callback) + callback(proxy_addr, NULL, data); } } @@ -365,23 +315,19 @@ static int __connection_set_proxy_changed_callback(connection_h connection, if (callback) { if (refcount == 0) - vconf_notify_key_changed(VCONFKEY_NETWORK_PROXY, - __connection_cb_proxy_change_cb, NULL); + _connection_libnet_set_proxy_changed_cb( + __connection_cb_proxy_change_cb); refcount++; - CONNECTION_LOG(CONNECTION_INFO, "Successfully registered(%d)", refcount); + CONNECTION_LOG(CONNECTION_INFO, "Successfully registered(%d)", + refcount); } else { if (refcount > 0 && __connection_get_proxy_changed_callback(local_handle) != NULL) { if (--refcount == 0) { - if (vconf_ignore_key_changed(VCONFKEY_NETWORK_PROXY, - __connection_cb_proxy_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); - } + _connection_libnet_set_proxy_changed_cb(NULL); + CONNECTION_LOG(CONNECTION_INFO, + "Successfully de-registered(%d)", refcount); } } } diff --git a/src/libnetwork.c b/src/libnetwork.c index c573503..64ef839 100755 --- a/src/libnetwork.c +++ b/src/libnetwork.c @@ -45,6 +45,9 @@ struct _libnet_s { connection_set_default_cb set_default_cb; connection_reset_cb reset_profile_cb; libnet_ethernet_cable_state_changed_cb ethernet_cable_state_changed_cb; + libnet_type_changed_cb type_changed_cb; + libnet_ip_changed_cb ip_changed_cb; + libnet_proxy_changed_cb proxy_changed_cb; void *opened_user_data; void *closed_user_data; void *set_default_user_data; @@ -65,8 +68,8 @@ struct managed_idle_data { }; static __thread struct _profile_list_s profile_iterator = {0, 0, NULL}; -static __thread struct _libnet_s libnet = {NULL, NULL, NULL, NULL, NULL, - NULL, NULL, NULL, NULL, false}; +static __thread struct _libnet_s libnet = {NULL, NULL, NULL, NULL, NULL, NULL, + NULL, NULL, NULL, NULL, NULL, NULL, false}; static __thread GSList *managed_idler_list = NULL; static __thread bool connection_is_feature_checked[CONNECTION_SUPPORTED_FEATURE_MAX] = {0, }; static __thread bool connection_feature_supported[CONNECTION_SUPPORTED_FEATURE_MAX] = {0, }; @@ -313,6 +316,25 @@ static void __libnet_ethernet_cable_state_changed_cb( libnet.ethernet_cable_state_changed_cb(state); } +static void __libnet_type_changed_cb(int type) +{ + if (libnet.type_changed_cb) + libnet.type_changed_cb(type); +} + +static void __libnet_ip_changed_cb(connection_address_family_e addr_family, + char *ip_addr) +{ + if (libnet.ip_changed_cb) + libnet.ip_changed_cb(addr_family, ip_addr); +} + +static void __libnet_proxy_changed_cb(char *proxy_addr) +{ + if (libnet.proxy_changed_cb) + libnet.proxy_changed_cb(proxy_addr); +} + static gboolean __libnet_state_changed_cb_idle(gpointer data) { struct _state_notify *notify = (struct _state_notify *)data; @@ -470,6 +492,26 @@ static void __libnet_evt_cb(net_event_info_t *event_cb, void *user_data) CONNECTION_LOG(CONNECTION_INFO, "Got Ethernet cable detached Indication\n"); __libnet_ethernet_cable_state_changed_cb(CONNECTION_ETHERNET_CABLE_DETACHED); break; + case NET_EVENT_NETWORK_TYPE_CHANGED: + CONNECTION_LOG(CONNECTION_INFO, "Got Network Type Changed Indication"); + int *state = (int *) event_cb->Data; + __libnet_type_changed_cb(*state); + break; + case NET_EVENT_IPV4_ADDRESS_CHANGED: + CONNECTION_LOG(CONNECTION_INFO, "Got IPv4 Address Changed Indication"); + char *ipv4_addr = (char *)event_cb->Data; + __libnet_ip_changed_cb(CONNECTION_ADDRESS_FAMILY_IPV4, ipv4_addr); + break; + case NET_EVENT_IPV6_ADDRESS_CHANGED: + CONNECTION_LOG(CONNECTION_INFO, "Got IPv6 Address Changed Indication"); + char *ipv6_addr = (char *)event_cb->Data; + __libnet_ip_changed_cb(CONNECTION_ADDRESS_FAMILY_IPV6, ipv6_addr); + break; + case NET_EVENT_PROXY_ADDRESS_CHANGED: + CONNECTION_LOG(CONNECTION_INFO, "Got Proxy Changed Indication"); + char *proxy_addr = (char *)event_cb->Data; + __libnet_proxy_changed_cb(proxy_addr); + break; default: break; @@ -663,6 +705,21 @@ int _connection_libnet_get_wifi_state(connection_wifi_state_e *state) return CONNECTION_ERROR_NONE; } +void _connection_libnet_set_type_changed_cb(libnet_type_changed_cb callback) +{ + libnet.type_changed_cb = callback; +} + +void _connection_libnet_set_ip_changed_cb(libnet_ip_changed_cb callback) +{ + libnet.ip_changed_cb = callback; +} + +void _connection_libnet_set_proxy_changed_cb(libnet_proxy_changed_cb callback) +{ + libnet.proxy_changed_cb = callback; +} + //LCOV_EXCL_START int _connection_libnet_get_ethernet_state(connection_ethernet_state_e *state) { -- 2.7.4