From: Jiung Yu Date: Thu, 4 May 2017 06:09:05 +0000 (+0900) Subject: Add set/unset state callback APIs X-Git-Tag: submit/tizen/20170612.082510^2~1 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F24%2F128024%2F1;p=platform%2Fcore%2Fapi%2Fvpn-setting.git Add set/unset state callback APIs Change-Id: Id7eb263173645c47d534f9b373e2599599568458 Signed-off-by: Yu jiung --- diff --git a/include/vpn.h b/include/vpn.h index 90f2d20..e888eed 100755 --- a/include/vpn.h +++ b/include/vpn.h @@ -54,6 +54,18 @@ typedef enum { VPN_ERROR_SECURITY_RESTRICTED = TIZEN_ERROR_NETWORK_CLASS|0x0309, /**< Restricted by security system policy */ } vpn_error_e; +/** +* @brief Enumeration for general VPN state. +*/ +typedef enum { + VPN_STATE_IDLE, /**< VPN state idle */ + VPN_STATE_READY, /**< VPN state ready */ + VPN_STATE_CONFIGURATION, /**< VPN state configuration */ + VPN_STATE_DISCONNECT, /**< VPN state disconnect */ + VPN_STATE_FAILURE, /**< VPN state failure */ + VPN_STATE_UNKNOWN, /**< VPN state unknown */ +} vpn_state_e; + /** * @} */ @@ -101,6 +113,17 @@ typedef void(*vpn_connect_cb)(vpn_error_e result, void *user_data); * @see vpn_disconnect() */ typedef void(*vpn_disconnect_cb)(vpn_error_e result, void *user_data); + +/** +* @brief Called when the state of VPN is changed. +* @param[in] handle The vpn for which state is reported; +* the handle to the same object for which the callback was set +* @param[in] state The vpn connection state +* @param[in] user_data The user data passed from the callback registration function +* @pre The callback must be registered using vpn_set_state_callback() +* @see vpn_set_state_callback() +*/ +typedef void(*vpn_state_cb)(const vpn_h handle, vpn_state_e state, void *user_data); /** * @} */ @@ -371,6 +394,31 @@ int vpn_get_vpn_info_host(const vpn_h handle, const char **host); */ int vpn_get_vpn_info_domain(const vpn_h handle, const char **domain); +/** +* @brief Registers the callback called when the state of the vpn is changed. +* @param[in] handle The VPN handle for the Request +* @param[in] cb The callback function to invoke +* @param[in] user_data The user data passed from the callback registration function +* @return 0 on success, otherwise negative error value. +* @retval #VPN_ERROR_NONE Successful +* @retval #VPN_ERROR_INVALID_OPERATION Invalid operation +* @retval #VPN_ERROR_INVALID_PARAMETER Operation failed +* @see vpn_initialize() +* @see vpn_unset_state_callback() +*/ +int vpn_set_state_callback(const vpn_h handle, vpn_state_cb cb, void *user_data); + +/** +* @brief Unregisters the callback called when the state of the vpn is changed. +* @param[in] handle The VPN handle for the Request +* @return 0 on success, otherwise negative error value. +* @retval #VPN_ERROR_NONE Successful +* @retval #VPN_ERROR_INVALID_OPERATION Invalid operation +* @retval #VPN_ERROR_INVALID_PARAMETER Operation failed +* @see vpn_initialize() +* @see vpn_set_state_callback() +*/ +int vpn_unset_state_callback(const vpn_h handle); /** * @} */ diff --git a/src/include/vpn-internal.h b/src/include/vpn-internal.h index f1395f9..37f1b17 100755 --- a/src/include/vpn-internal.h +++ b/src/include/vpn-internal.h @@ -76,6 +76,8 @@ int _vpn_get_vpn_info_name(vpn_h handle, const char **name); int _vpn_get_vpn_info_type(vpn_h handle, const char **type); int _vpn_get_vpn_info_host(vpn_h handle, const char **host); int _vpn_get_vpn_info_domain(vpn_h handle, const char **domain); +int _vpn_set_state_callback(const vpn_h handle, vpn_state_cb cb, void *user_data); +int _vpn_unset_state_callback(const vpn_h handle); #ifdef __cplusplus } #endif /* __cplusplus */ diff --git a/src/vpn-internal.c b/src/vpn-internal.c index dd3ad57..0c4a872 100755 --- a/src/vpn-internal.c +++ b/src/vpn-internal.c @@ -37,6 +37,7 @@ struct _vpn_cb_s { void *connect_user_data; vpn_disconnect_cb disconnect_cb; void *disconnect_user_data; + GHashTable *state_cb_datas; }; static struct _vpn_cb_s vpn_callbacks = {0,}; @@ -95,12 +96,19 @@ bool _vpn_init(void) if (rv != 0) return false; + vpn_callbacks.state_cb_datas = g_hash_table_new_full(g_direct_hash, + g_direct_equal, + NULL, + NULL); return true; } bool _vpn_deinit(void) { dvpnlib_vpn_deinit(); + + g_hash_table_remove_all(vpn_callbacks.state_cb_datas); + return true; } @@ -415,3 +423,69 @@ int _vpn_get_vpn_info_domain(vpn_h handle, const char **domain) *domain = vpn_connection_get_domain(handle); return VPN_ERROR_NONE; } + +/* + *State Callbacks + */ + +static void __vpn_state_cb(struct vpn_connection *connection, void *user_data) +{ + vpn_state_cb cb = NULL; + cb = (vpn_state_cb)g_hash_table_lookup(vpn_callbacks.state_cb_datas, connection); + if (cb) + cb(connection, vpn_connection_get_state(connection), user_data); +} + +static void vpn_manager_state_cb(struct vpn_connection *connection, void *user_data) +{ + VPN_LOG(VPN_INFO, "connection: %p user_data: %p\n", connection, user_data); + + __vpn_state_cb(connection, user_data); +} + +/* + *Set state changed callback to VPN handle + */ + +int _vpn_set_state_callback(const vpn_h handle, vpn_state_cb cb, void *user_data) +{ + enum dvpnlib_err err = DVPNLIB_ERR_NONE; + + VPN_LOG(VPN_INFO, ""); + + GList *connections = vpn_get_connections(); + if (NULL == g_list_find(connections, handle)) { + VPN_LOG(VPN_ERROR, "No Connections with the %p Handle", handle); + return VPN_ERROR_INVALID_PARAMETER; + } + + g_hash_table_replace(vpn_callbacks.state_cb_datas, handle, cb); + err = vpn_connection_set_property_changed_cb(handle, VPN_CONN_PROP_STATE, vpn_manager_state_cb, (void *)user_data); + if (err != DVPNLIB_ERR_NONE) + return _dvpnlib_error2vpn_error(err); + + return VPN_ERROR_NONE; +} + +/* + *Unset state changed callback to VPN handle + */ + +int _vpn_unset_state_callback(const vpn_h handle) +{ + enum dvpnlib_err err = DVPNLIB_ERR_NONE; + + VPN_LOG(VPN_INFO, ""); + + GList *connections = vpn_get_connections(); + if (NULL == g_list_find(connections, handle)) { + VPN_LOG(VPN_ERROR, "No Connections with the %p Handle", handle); + return VPN_ERROR_INVALID_PARAMETER; + } + + err = vpn_connection_unset_property_changed_cb(handle, VPN_CONN_PROP_STATE); + if (err != DVPNLIB_ERR_NONE) + return _dvpnlib_error2vpn_error(err); + + return VPN_ERROR_NONE; +} diff --git a/src/vpn.c b/src/vpn.c index cfd7e31..27169d6 100755 --- a/src/vpn.c +++ b/src/vpn.c @@ -386,3 +386,44 @@ int vpn_get_vpn_info_domain(const vpn_h handle, const char **domain) return rv; } +EXPORT_API +int vpn_set_state_callback(const vpn_h handle, vpn_state_cb cb, void *user_data) +{ + int rv; + + if (is_init == false) { + VPN_LOG(VPN_ERROR, "Not initialized\n"); + return VPN_ERROR_INVALID_OPERATION; + } + + if (handle == NULL || cb == NULL) + return VPN_ERROR_INVALID_PARAMETER; + + rv = _vpn_set_state_callback(handle, cb, user_data); + + if (rv != VPN_ERROR_NONE) + VPN_LOG(VPN_ERROR, "Error!! VPN Set state callback failed.\n"); + + return rv; +} + +EXPORT_API +int vpn_unset_state_callback(const vpn_h handle) +{ + int rv; + + if (is_init == false) { + VPN_LOG(VPN_ERROR, "Not initialized\n"); + return VPN_ERROR_INVALID_OPERATION; + } + + if (handle == NULL) + return VPN_ERROR_INVALID_PARAMETER; + + rv = _vpn_unset_state_callback(handle); + + if (rv != VPN_ERROR_NONE) + VPN_LOG(VPN_ERROR, "Error!! VPN Unset state callback failed.\n"); + + return rv; +} diff --git a/test/vpn_test.c b/test/vpn_test.c index 45a87cf..6bfb336 100755 --- a/test/vpn_test.c +++ b/test/vpn_test.c @@ -79,6 +79,27 @@ static const char *__test_convert_error_to_string(vpn_error_e err_type) return "UNKNOWN"; } + +static const char *__test_convert_state_to_string(vpn_state_e state_type) +{ + switch (state_type) { + case VPN_STATE_IDLE: + return "IDLE"; + case VPN_STATE_READY: + return "READY"; + case VPN_STATE_CONFIGURATION: + return "CONFIGURATION"; + case VPN_STATE_DISCONNECT: + return "DISCONNECT"; + case VPN_STATE_FAILURE: + return "FAILURE"; + case VPN_STATE_UNKNOWN: + return "UNKNOWN"; + } + + return "UNKNOWN"; +} + #if defined(IPSEC_TEST) typedef enum { IPSEC_HYBRID_RSA, @@ -225,6 +246,26 @@ static void __test_disconnect_callback(vpn_error_e result, __test_convert_error_to_string(result)); } +static void __test_state_callback(vpn_h handle_ptr, vpn_state_e state, void *user_data) +{ + const char *name = NULL; + const char *type = NULL; + const char *host = NULL; + const char *domain = NULL; + + printf("\n Handle = %p\n", handle_ptr); + vpn_get_vpn_info_name(handle_ptr, &name); + vpn_get_vpn_info_type(handle_ptr, &type); + vpn_get_vpn_info_host(handle_ptr, &host); + vpn_get_vpn_info_domain(handle_ptr, &domain); + printf(" Name[%p] - %s\n", handle_ptr, name); + printf(" Type[%p] - %s\n", handle_ptr, type); + printf(" Host[%p] - %s\n", handle_ptr, host); + printf(" Domain[%p] - %s\n", handle_ptr, domain); + printf("VPN state changed! : %s", + __test_convert_state_to_string(state)); +} + static void _test_get_vpn_handle(vpn_h *handle_ptr) { assert(handle_ptr != NULL); @@ -421,11 +462,11 @@ int test_vpn_create(void) int test_vpn_remove(void) { int rv = 0; + vpn_h handle = NULL; - /*TODO: Get the List of VPN Profiles Identifiers - * Prompt user to which one to be deleted */ + _test_get_vpn_handle(&handle); - rv = vpn_remove(NULL, __test_removed_callback, NULL); + rv = vpn_remove(handle, __test_removed_callback, NULL); if (rv != VPN_ERROR_NONE) { printf("Fail to Remove VPN Profile [%s]\n", @@ -438,6 +479,46 @@ int test_vpn_remove(void) return 1; } +int test_vpn_set_state_callback(void) +{ + int rv = 0; + vpn_h handle = NULL; + + _test_get_vpn_handle(&handle); + + rv = vpn_set_state_callback(handle, __test_state_callback, NULL); + + if (rv != VPN_ERROR_NONE) { + printf("Fail to Set VPN state callback [%s]\n", + __test_convert_error_to_string(rv)); + return -1; + } + + printf("Success to Set VPN state callback\n"); + + return 1; +} + +int test_vpn_unset_state_callback(void) +{ + int rv = 0; + vpn_h handle = NULL; + + _test_get_vpn_handle(&handle); + + rv = vpn_unset_state_callback(handle); + + if (rv != VPN_ERROR_NONE) { + printf("Fail to Unset VPN state callback [%s]\n", + __test_convert_error_to_string(rv)); + return -1; + } + + printf("Success to Unset VPN state callback\n"); + + return 1; +} + int test_vpn_connect(void) { int rv = 0; @@ -504,6 +585,10 @@ static int __test_init() static int __test_deinit() { int rv = 0; + int i = 0; + + for (i = 0; i < IPSEC_KVS_MAX; i++) + g_free(ipsec_setting_kvs[i].value) rv = vpn_settings_deinit(); if (rv != VPN_ERROR_NONE) { @@ -811,6 +896,8 @@ gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data) printf("e\t- VPN Test Create IPSec IKEv2 PSK - Create IPSec IKEv2 PSK\n"); printf("f\t- VPN Test Create IPSec IKEv2 RSA - Create IPSec IKEv2 RSA\n"); #endif + printf("g\t- VPN Set state callback - Set the VPN state callback\n"); + printf("h\t- VPN Unset state callback - Unset the VPN state callback\n"); printf("0\t- Exit\n"); printf("ENTER - Show options menu.......\n"); @@ -864,6 +951,12 @@ gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data) rv = test_create_ipsec(data, IPSEC_IKEV2_RSA); break; #endif + case 'g': + rv = test_vpn_set_state_callback(); + break; + case 'h': + rv = test_vpn_unset_state_callback(); + break; default: break; }