Add set/unset state callback APIs 24/128024/1
authorJiung Yu <jiung.yu@samsung.com>
Thu, 4 May 2017 06:09:05 +0000 (15:09 +0900)
committerJiung Yu <jiung.yu@samsung.com>
Thu, 4 May 2017 06:09:17 +0000 (15:09 +0900)
Change-Id: Id7eb263173645c47d534f9b373e2599599568458
Signed-off-by: Yu jiung <jiung.yu@samsung.com>
include/vpn.h
src/include/vpn-internal.h
src/vpn-internal.c
src/vpn.c
test/vpn_test.c

index 90f2d20885d084183c891279cf1bbe5357b317f6..e888eed8c4199e0dfa44d2c22c00220d01a80819 100755 (executable)
@@ -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);
 /**
 * @}
 */
index f1395f9dfe2aad193c1b1d29e29725fda8ca7456..37f1b17b886be634a1444f75a60aafcc30d7d45d 100755 (executable)
@@ -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 */
index dd3ad57058d6a22fbc5f68806384a30f285cb9b3..0c4a8725906acbc29ac14c783389bc2ce5ee2176 100755 (executable)
@@ -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;
+}
index cfd7e318dd9c8c02e1206af160fac9232be537d1..27169d61a7f42f1e0d62efbcb5759ed1d6f794d5 100755 (executable)
--- 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;
+}
index 45a87cf14b7164a0725fff9e54106eb46cae48ca..6bfb33622d98ea1ddb2854e6058fa62c426706bd 100755 (executable)
@@ -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;
        }