Fixed wifi connection with Tizen API
authornshero.lee <nshero.lee@samsung.com>
Fri, 22 Dec 2017 04:20:47 +0000 (13:20 +0900)
committernshero.lee <nshero.lee@samsung.com>
Fri, 22 Dec 2017 04:20:47 +0000 (13:20 +0900)
Signed-off-by: nshero.lee <nshero.lee@samsung.com>
include/sa_common.h
src/adaptor.c
src/input_file.c
src/setup_network.c

index d169280c2650a9e453ef708119a719f8d91304a1..01de5e82c63b6cd951b118077bb8b10420e2bce1 100644 (file)
 
 #define LOG_TAG "SETUP-ADAPTOR"
 
+#ifdef MIN
+#undef MIN
+#endif
+
 #define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
 #ifndef TRUE
 #define TRUE 1
index 7935b0cdb9e29305b22a0f8ed9c5c3923b2626d9..99a1cc1d7bf9ef0e3022f850844b2fd779e5eb72 100644 (file)
@@ -325,9 +325,6 @@ void __monitor_file_state_cb(sa_file_state_e state, void *param1, void *param2)
 {
        sa_config_s config = {0,};      
        _D("******************__monitor_file_state_cb ******************");
-
-       //sa_network_test();
-
        switch (state) {
        case SA_FILE_STATE_REGISTERED:
                _D("SA_FILE_STATE_REGISTERED");
@@ -360,6 +357,11 @@ int main(int argc, char *argv[])
        sa_error_e ret = SA_ERROR_NONE;
 
        GMainLoop *mainloop;
+
+#if !GLIB_CHECK_VERSION(2, 36, 0)
+       g_type_init();
+#endif
+
        mainloop = g_main_loop_new(NULL, FALSE);
 
        _D("start setup-adaptor");
@@ -368,7 +370,7 @@ int main(int argc, char *argv[])
                // Waiting for booting
                sleep(1);
                // Register callback to receive file change event
-               ret = sa_inputfile_register_cb(__monitor_file_state_cb);
+               ret = sa_inputfile_register_cb((file_state_cb)__monitor_file_state_cb);
                if (ret == SA_ERROR_NONE) {
                        // Keep checking callback from input
                } else {
index 3cea729b2f95247cc5e24022c10f8bbd67fa37a7..4d501f1ece22ff6bba336324ae9a0da9375123f8 100644 (file)
@@ -187,35 +187,6 @@ static char *json_get_string_from_obj(json_object *inputObj, char *key)
        printf("\nstring object [%s]", ret_buf);
        return ret_buf;
 }
-static int __parse_system_proxy(json_object *inputObj, sa_system_s *system)
-{
-       char *httpProxyHost = NULL;
-       int httpProxyPort;
-       int ret = 0;
-
-       if (inputObj == NULL || system == NULL) {
-               printf("\n__parse_system_proxy input error");
-               return -1;
-       }
-
-       system->proxy = (sa_proxy_s *)malloc(sizeof(sa_proxy_s));
-       if (system->proxy != NULL) {
-               //httpProxyHost
-               httpProxyHost = json_get_string_from_obj(inputObj, SA_CONFIG_NETWORKDATA_HTTPPROXYHOST);
-               if (httpProxyHost != NULL) {
-                       memcpy(system->proxy->httpProxyHost, httpProxyHost, MIN(strlen(httpProxyHost), sizeof(system->proxy->httpProxyHost)-1));
-                       free(httpProxyHost);
-                       httpProxyHost = NULL;
-               }
-               //httpProxyPort
-               httpProxyPort = json_get_int_from_obj(inputObj, SA_CONFIG_NETWORKDATA_HTTPPROXYPORT);
-       } else {
-               ret = -1;
-               printf("\nsystem->proxy malloc fail");
-       }
-
-       return ret;
-}
 
 static int __parse_network_static_info(json_object *inputObj, sa_network_static_s *staticInfo)
 {
@@ -482,7 +453,7 @@ static int __parse_config(char *file, sa_config_s *setupConfig)
 
                                configObj = json_tokener_parse(jsonData);
 
-                               if (configObj != NULL) {
+                               if (configObj != NULL && readLen > 0) {
                                        // parse version
                                        ret = __parse_version(configObj, setupConfig);
                                        if (ret == 0) {
index f5af9f3aa47d6c414950e1d2a9bc7ac84e547127..e09a828124524fea5fd8861722b021acdcbd8ed2 100644 (file)
 #include "net_connection.h"
 #include "wifi-manager.h"
 
+static char wifi_password[99+1];
+static wifi_manager_h wifi_handle = NULL;
 
-char wifi_password[99+1];
-int initialize_setup_network_flag;
-sa_network_state_e setup_network_wifi_state; 
-connection_h connection_handle = NULL;
-wifi_manager_h wifi_handle = NULL;
-
-
-static void __tmp_do_connect(sa_wifi_s *info);
-
-static const char *__test_convert_error_to_string(wifi_manager_error_e err_type)
-{
-       switch (err_type) {
-       case WIFI_MANAGER_ERROR_NONE:
-               return "NONE";
-       case WIFI_MANAGER_ERROR_INVALID_PARAMETER:
-               return "INVALID_PARAMETER";
-       case WIFI_MANAGER_ERROR_OUT_OF_MEMORY:
-               return "OUT_OF_MEMORY";
-       case WIFI_MANAGER_ERROR_INVALID_OPERATION:
-               return "INVALID_OPERATION";
-       case WIFI_MANAGER_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED:
-               return "ADDRESS_FAMILY_NOT_SUPPORTED";
-       case WIFI_MANAGER_ERROR_OPERATION_FAILED:
-               return "OPERATION_FAILED";
-       case WIFI_MANAGER_ERROR_NO_CONNECTION:
-               return "NO_CONNECTION";
-       case WIFI_MANAGER_ERROR_NOW_IN_PROGRESS:
-               return "NOW_IN_PROGRESS";
-       case WIFI_MANAGER_ERROR_ALREADY_EXISTS:
-               return "ALREADY_EXISTS";
-       case WIFI_MANAGER_ERROR_OPERATION_ABORTED:
-               return "OPERATION_ABORTED";
-       case WIFI_MANAGER_ERROR_DHCP_FAILED:
-               return "DHCP_FAILED";
-       case WIFI_MANAGER_ERROR_INVALID_KEY:
-               return "INVALID_KEY";
-       case WIFI_MANAGER_ERROR_OUT_OF_RANGE:
-               return "OUT_OF_RANGE";
-       case WIFI_MANAGER_ERROR_PIN_MISSING:
-               return "PIN_MISSING";
-       case WIFI_MANAGER_ERROR_CONNECT_FAILED:
-               return "CONNECT_FAILED";
-       case WIFI_MANAGER_ERROR_LOGIN_FAILED:
-               return "LOGIN_FAILED";
-       case WIFI_MANAGER_ERROR_AUTHENTICATION_FAILED:
-               return "AUTH_FAILED";
-       case WIFI_MANAGER_ERROR_NO_REPLY:
-               return "NO_REPLY";
-       case WIFI_MANAGER_ERROR_SECURITY_RESTRICTED:
-               return "SECURITY_RESTRICTED";
-       case WIFI_MANAGER_ERROR_ALREADY_INITIALIZED:
-               return "ALREADY_INITIALIZED";
-       case WIFI_MANAGER_ERROR_PERMISSION_DENIED:
-               return "PERMISSION_DENIED";
-       case WIFI_MANAGER_ERROR_NOT_SUPPORTED:
-               return "NOT_SUPPORTED";
-       case WIFI_MANAGER_ERROR_WPS_OVERLAP:
-               return "WPS_OVERLAP";
-       case WIFI_MANAGER_ERROR_WPS_TIMEOUT:
-               return "WPS_TIMEOUT";
-       case WIFI_MANAGER_ERROR_WPS_WEP_PROHIBITED:
-               return "WPS_WEP_PROHIBITED";
-       default:
-               return "UNKNOWN";
-       }
-}
-
-static void __test_device_state_callback(wifi_manager_device_state_e state, void* user_data)
-{
-       printf("[%s] Device state changed callback", (char *)user_data);
-
-       if (state == WIFI_MANAGER_DEVICE_STATE_ACTIVATED)
-               printf(", state : Activated\n");
-       else
-               printf(", state : Deactivated\n");
-}
-
-static void __test_scan_changed_callback(wifi_manager_scan_state_e state, void* user_data)
-{
-       printf("Scan changed, scan state : %d\n", state);
-}
-
-static void __test_bg_scan_completed_callback(wifi_manager_error_e error_code, void* user_data)
-{
-       printf("[%s] Background Scan Completed, error code : %s\n",
-                       (char *)user_data, __test_convert_error_to_string(error_code));
-}
-
-static void __test_ip_conflict_callback(char *mac, wifi_manager_ip_conflict_state_e state, void *user_data)
-{
-       if (state == WIFI_MANAGER_IP_CONFLICT_STATE_CONFLICT_DETECTED)
-               printf("[%s] Ip conflict detected : %s\n", (char *)user_data, mac);
-       else if (state == WIFI_MANAGER_IP_CONFLICT_STATE_CONFLICT_NOT_DETECTED)
-               printf("Ip conflict removed\n");
-       else
-               printf("Ip conflict state unknown\n");
-}
-
-static void __test_scan_request_callback(wifi_manager_error_e error_code, void* user_data)
-{
-       if (user_data != NULL)
-               printf("user_data : %s\n", (char *)user_data);
-
-       printf("Scan Completed from scan request, error code : %s\n",
-                       __test_convert_error_to_string(error_code));
-}
-
-static void __test_bssid_scan_request_callback(wifi_manager_error_e error_code, void *user_data)
-{
-       printf("BSSID Scan Completed, error code : %s\n",
-                       __test_convert_error_to_string(error_code));
-
-}
-
-static void __test_netlink_scan_request_callback(wifi_manager_error_e error_code, void *user_data)
-{
-       printf("Netlink Scan Completed, error code : %s\n",
-                       __test_convert_error_to_string(error_code));
-
-}
-
-static void __test_netlink_scan_specific_ap_callback(wifi_manager_error_e error_code, void *user_data)
-{
-       printf("Netlink Specific Scan Completed, error code : %s\n",
-                       __test_convert_error_to_string(error_code));
-
-}
-
-static void __test_connection_state_callback(wifi_manager_connection_state_e state, wifi_manager_ap_h ap, void* user_data)
-{
-       int rv = 0;
-       char *ap_name = NULL;
-
-       printf("[%s] Connection state changed callback", (char *)user_data);
-
-       switch (state) {
-       case WIFI_MANAGER_CONNECTION_STATE_CONNECTED:
-               printf(", state : Connected");
-               break;
-       case WIFI_MANAGER_CONNECTION_STATE_ASSOCIATION:
-               printf(", state : Association");
-               break;
-       case WIFI_MANAGER_CONNECTION_STATE_CONFIGURATION:
-               printf(", state : Configuration");
-               break;
-       case WIFI_MANAGER_CONNECTION_STATE_DISCONNECTED:
-               printf(", state : Disconnected");
-               break;
-       default:
-               printf(", state : Unknown");
-       }
-
-       rv = wifi_manager_ap_get_essid(ap, &ap_name);
-       if (rv != WIFI_MANAGER_ERROR_NONE)
-               printf(", Fail to get AP name [%s]\n", __test_convert_error_to_string(rv));
-       else {
-               printf(", AP name : %s\n", ap_name);
-       }
-}
-
-static void __test_activated_callback(wifi_manager_error_e result, void* user_data)
-{
-       if (result == WIFI_MANAGER_ERROR_NONE)
-               printf("Wi-Fi Activation Succeeded\n");
-       else
-               printf("Wi-Fi Activation Failed! error : %s\n", __test_convert_error_to_string(result));
-}
-
-static void __test_deactivated_callback(wifi_manager_error_e result, void* user_data)
-{
-       if (result == WIFI_MANAGER_ERROR_NONE)
-               printf("Wi-Fi Deactivation Succeeded\n");
-       else
-               printf("Wi-Fi Deactivation Failed! error : %s\n", __test_convert_error_to_string(result));
-}
-
-static void __test_connected_callback(wifi_manager_error_e result, void* user_data)
-{
-       if (result == WIFI_MANAGER_ERROR_NONE)
-               printf("Wi-Fi Connection Succeeded\n");
-       else
-               printf("Wi-Fi Connection Failed! error : %s\n", __test_convert_error_to_string(result));
-}
-
-static void __test_disconnected_callback(wifi_manager_error_e result, void* user_data)
-{
-       if (result == WIFI_MANAGER_ERROR_NONE)
-               printf("Wi-Fi Disconnection Succeeded\n");
-       else
-               printf("Wi-Fi Disconnection Failed! error : %s\n", __test_convert_error_to_string(result));
-}
-
-static void __test_rssi_level_callback(wifi_manager_rssi_level_e rssi_level, void* user_data)
-{
-       printf("[%s] RSSI level changed callback, level = %d\n", (char *)user_data, rssi_level);
-}
-
-static void __test_tdls_discover_callback(wifi_manager_tdls_discovery_state_e state, char *peer_mac_add, void *user_data)
-{
-       printf("[%s] TDLS discover callback", (char *)user_data);
-
-       printf(", Peer Mac Address [%s], state :%d\n", peer_mac_add, state);
-       if (state == WIFI_MANAGER_TDLS_DISCOVERY_STATE_ONGOING)
-               printf(", Discovery is ongoing");
-       else
-               printf(", Discovery is finished");
-}
-
-static void __test_get_wifi_module_state_callback(wifi_manager_module_state_e state, void *user_data)
-{
-       printf("Wi-Fi Module State Changed callback : %d", state);
-
-       if (state == WIFI_MANAGER_MODULE_STATE_ATTACHED)
-               printf(", Wi-Fi Module Attached\n");
-       else
-               printf(", Wi-Fi Module Detached\n");
-}
-
-static void __test_tdls_state_callback(wifi_manager_tdls_state_e state, char *peer_mac_add, void *user_data)
-{
-       printf("[%s] TDLS state changed callback", (char *)user_data);
-
-       if (state == WIFI_MANAGER_TDLS_STATE_CONNECTED)
-               printf(", state : TDLS Connected, Peer Mac Address [%s]\n", peer_mac_add);
-       else
-               printf(", state : TDLS Disconnected, Peer Mac Address [%s]\n", peer_mac_add);
-}
-
-static const char* __test_print_state(wifi_manager_connection_state_e state)
-{
-       switch (state) {
-       case WIFI_MANAGER_CONNECTION_STATE_FAILURE:
-               return "Failure";
-       case WIFI_MANAGER_CONNECTION_STATE_DISCONNECTED:
-               return "Disconnected";
-       case WIFI_MANAGER_CONNECTION_STATE_ASSOCIATION:
-               return "Association";
-       case WIFI_MANAGER_CONNECTION_STATE_CONNECTED:
-               return "Connected";
-       case WIFI_MANAGER_CONNECTION_STATE_CONFIGURATION:
-               return "Configuration";
-       }
-
-       return "Unknown";
-}
-
-
-
-
-
-
-
-
-
-
-
-static int __get_setup_network_initialized(void)
-{
-       return initialize_setup_network_flag;
-}
-
-static int __set_setup_network_initialized(int val)
-{
-       if (val != __get_setup_network_initialized()) {
-               initialize_setup_network_flag = val;
-       }
-}
-
-static int __get_setup_network_wifi_state(void)
-{
-       return setup_network_wifi_state;
-}
-
-static void __set_setup_network_wifi_state(sa_network_state_e val)
-{
-       if (val != __get_setup_network_wifi_state()) {
-               setup_network_wifi_state = val;
-       }
-}
+static void __do_connect(sa_wifi_s *info);
+static void __do_exit(void);
 
 static void __set_setup_network_wifi_password(char *password)
 {
+       
        if (strcmp(wifi_password, password)) {
                memset(wifi_password, 0x00, sizeof(wifi_password));
                memcpy(wifi_password, password, MIN(strlen(password), sizeof(wifi_password)-1));
        }
+
+       printf("\n __set_setup_network_wifi_password[%s]", wifi_password);
+       return;
 }
 
 static char *__get_setup_network_wifi_password(void)
@@ -473,7 +202,7 @@ static void __activated_callback(wifi_manager_error_e result, void *user_data)
 {
        if (result == WIFI_MANAGER_ERROR_NONE) {
                printf("\nMY~~~Wi-Fi Activation Succeeded");
-               __tmp_do_connect((sa_wifi_s *)user_data);
+               
        }
        else
                printf("\nWi-Fi Activation Failed! error : %s", __print_wifi_error(result));
@@ -481,21 +210,38 @@ static void __activated_callback(wifi_manager_error_e result, void *user_data)
 
 static void __scan_request_callback(wifi_manager_error_e error_code, void* user_data)
 {      
-       if (user_data != NULL)
-               printf("\nuser_data : %s", (char *)user_data);
-               
-       printf("\nScan Completed from scan request [%s]", __print_wifi_error(error_code));
+       printf("\n __scan_request_callback from scan request [%s]", __print_wifi_error(error_code));
+       __do_connect((sa_wifi_s *)user_data);   
 }
 
 static void __deactivated_callback(wifi_manager_error_e result, void* user_data)
 {
-       if (result == WIFI_MANAGER_ERROR_NONE) {
-               __set_setup_network_wifi_state(NETWORK_WIFI_STATE_DEACTIVATED);
+       if (result == WIFI_MANAGER_ERROR_NONE)
                printf("\nWi-Fi Deactivation Succeeded");
-       else
+       else
                printf("\nWi-Fi Deactivation Failed! error : %s", __print_wifi_error(result));
 }
 
+static void __connection_state_callback(wifi_manager_connection_state_e state, wifi_manager_ap_h ap, void *user_data)
+{
+       switch (state) {
+       case WIFI_MANAGER_CONNECTION_STATE_CONNECTED:
+               printf("state : Connected");
+               break;
+       case WIFI_MANAGER_CONNECTION_STATE_ASSOCIATION:
+               printf(", state : Association");
+               break;
+       case WIFI_MANAGER_CONNECTION_STATE_CONFIGURATION:
+               printf(", state : Configuration");
+               break;
+       case WIFI_MANAGER_CONNECTION_STATE_DISCONNECTED:
+               printf(", state : Disconnected");
+               break;
+       default:
+               printf(", state : Unknown");
+       }
+}
+
 static int __compare_ap_name(const char *ap_name, const char *ap_name_part)
 {
        int ap_name_len = strlen(ap_name);
@@ -520,7 +266,7 @@ static void __scan_changed_callback(wifi_manager_scan_state_e state, void* user_
 {
        printf("\nScan changed, scan state : %d", state);
        if (state == WIFI_MANAGER_SCAN_STATE_NOT_SCANNING) {
-               __set_setup_network_wifi_state(NETWORK_WIFI_STATE_SCANNED);
+               wifi_manager_unset_scan_state_changed_cb(wifi_handle);
        }
 }
 
@@ -528,8 +274,10 @@ static void __found_connect_ap_callback(wifi_manager_ap_h ap, void *user_data)
 {
        int rv = 0;
        char *ap_name = NULL;
-       char *ap_name_part = (char*)user_data;
+       char *ap_name_part = (char *)user_data;
 
+
+       printf("\n __found_connect_ap_callback called");
        rv = wifi_manager_ap_get_essid(ap, &ap_name);
        if (rv != WIFI_MANAGER_ERROR_NONE) {
                printf("\nFail to get AP name [%s]\n", __print_wifi_error(rv));
@@ -544,136 +292,134 @@ static void __found_connect_ap_callback(wifi_manager_ap_h ap, void *user_data)
                if (rv == WIFI_MANAGER_ERROR_NONE) {
                        if (required) {
                                char *passphrase = __get_setup_network_wifi_password();
-                               printf("\npassword : [%s]", passphrase);
+                               printf("\n passphrase1 : [%s]", passphrase);
                                rv = wifi_manager_ap_set_passphrase(ap, passphrase);
+                               printf("\n passphrase2 : [%s]", passphrase);
                                if (rv != WIFI_MANAGER_ERROR_NONE) {
                                        printf("\nFail to set passphrase : %s", __print_wifi_error(rv));
                                        if (ap_name != NULL) 
                                                free(ap_name);;
-                                       return ;
+                                       return;
                                }
                        }
-
-                       //rv = wifi_manager_connect(wifi, ap, __connected_callback, NULL);
+                       rv = wifi_manager_connect(wifi_handle, ap, __connected_callback, NULL);
                        if (rv != WIFI_MANAGER_ERROR_NONE)
                                printf("\nFail to connection request [%s] : %s", ap_name, __print_wifi_error(rv));
                        else {
                                printf("\nSuccess to connection request [%s]", ap_name);
-                               __set_setup_network_wifi_state(NETWORK_WIFI_STATE_CONNECTED);
                        }
                }
-       
        }
 
        if (ap_name != NULL) 
                free(ap_name);
 
-       return;
-}
-
-static int __start_setup_network(void) 
-{
-       int err_code = 0;
-
-       if (!__get_setup_network_initialized()) {
-               err_code = wifi_manager_initialize(&wifi_handle);
-               printf("\nwifi_manager_initialize ret(%s)", __print_error(err_code));
-               if (err_code == WIFI_MANAGER_ERROR_NONE ||
-                       err_code == WIFI_MANAGER_ERROR_ALREADY_INITIALIZED) {
-                       if (connection_handle != NULL) {
-                               err_code = connection_destroy(connection_handle);
-                               printf("\nconnection handle was already created [%s]", __print_error(err_code));
-                       }
-
-                       err_code = connection_create(&connection_handle);
-                       printf("\nconnection_create : ret[%s]", __print_error(err_code));
-                       if (err_code != CONNECTION_ERROR_NONE) {
-                               printf("\ncreate connection handle error [%s]", __print_error(err_code));
-                               return -1;
-                       }
-                       __set_setup_network_initialized(TRUE);
-               } else {
-                       printf("\ncreate wifi handle error [%s]", __print_wifi_error(err_code));
-                       return -1;
-               }
-       }
+       __do_exit();
 
-       return 0;
+       return;
 }
 
-
-void __wifi_state_changed_cb(wifi_manager_device_state_e state, void *user_data)
+void __activate_state_changed_cb(wifi_manager_device_state_e state, void *user_data)
 {
        printf("\n__wifi_state_changed_cb() state=[%d]", state);
 
        if (state == WIFI_MANAGER_DEVICE_STATE_ACTIVATED) {
                printf("\n__wifi_state_changed_cb start scan...");
-               wifi_manager_scan(wifi_handle, __scan_request_callback, user_data);
-               wifi_manager_unset_device_state_changed_cb(wifi_handle);
+               wifi_manager_scan(wifi_handle, __scan_request_callback, (void *)user_data);
+               //wifi_manager_set_scan_state_changed_cb(wifi_handle, __test_scan_changed_callback, (void *)user_data);
+               //wifi_manager_set_background_scan_cb(wifi_handle, __test_bg_scan_completed_callback, (void *)info);
        }
 }
 
-static void __tmp_exit(void)
+static void __do_exit(void)
 {
        printf("\nExit Case :: To-Do");
 }
 
-static void __tmp_do_activate(sa_wifi_s *info)
+static int __do_activate(wifi_manager_h *wifi, sa_wifi_s *info)
 {
        int rv = 0;
-       sa_error_e ret = SA_ERROR_UNKNOWN;
 
        printf("\nativating state");
 
-       wifi_manager_set_device_state_changed_cb(wifi_handle, __wifi_state_changed_cb, (void *)info);
-       rv = wifi_manager_activate(wifi_handle, __activated_callback, (void *)info);
+       rv = wifi_manager_activate(*wifi, __activated_callback, (void *)info);
        printf("\nwifi_manager_activate:ret[%s]", __print_wifi_error(rv));
+
+       return rv;
 }
 
-static void __tmp_do_connect(sa_wifi_s *info)
+static void __do_connect(sa_wifi_s *info)
 {
        int rv = 0;
-       
        printf("\nscanned state");
+       
        __set_setup_network_wifi_password(info->password);
-       printf("\nconnecting state");
-       rv = wifi_manager_foreach_found_ap(wifi_handle, __found_connect_ap_callback, info->ssid);
+
+       printf("\nconnecting state[%s]", info->ssid);
+       rv = wifi_manager_foreach_found_ap(wifi_handle, (void *)__found_connect_ap_callback, (void *)info->ssid);
 
        if (rv != WIFI_MANAGER_ERROR_NONE) {
                printf("\nFail to connect (can't get AP list) [%s]", __print_wifi_error(rv));
-               __tmp_exit();
+               __do_exit();
        }
+       
 }
 
 static int __connect_wifi(sa_wifi_s *info)
 {
        int rv = 0;
-       bool state = false;
        sa_error_e ret = SA_ERROR_UNKNOWN;
-       sa_network_state_e file_state = NETWORK_WIFI_STATE_DEACTIVATED;
-
+       
        printf("\n------------------- __connect_wifi -------------------");
 
        if (info == NULL) {
                return -1;
        }
 
-       GMainLoop *mainloop;
-       mainloop = g_main_loop_new(NULL, FALSE);
-
-       __tmp_do_activate(info);
+       wifi_manager_h wifi = NULL;
+       GMainLoop *wifiLoop;
+       wifiLoop = g_main_loop_new(NULL, FALSE);
+       
        
-       g_main_loop_run(mainloop);
+       rv = wifi_manager_initialize(&wifi);
+
+       if (rv == WIFI_MANAGER_ERROR_NONE) {
+               wifi_manager_set_device_state_changed_cb(wifi, __activate_state_changed_cb, (void *)info);
+               wifi_manager_set_connection_state_changed_cb(wifi, __connection_state_callback, NULL);
+               wifi_handle = wifi;
+               rv = __do_activate(&wifi, info);
+               if (rv == WIFI_MANAGER_ERROR_ALREADY_EXISTS) {
+                       printf("\nalready existed [%s]", __print_wifi_error(rv));
+                       __do_connect(info);
+               }
+
+       } else {
+               printf("\\nWifi init failed [%s]", __print_wifi_error(rv));
+               return SA_ERROR_UNKNOWN;
+       }
+
+       g_main_loop_run(wifiLoop);
+
+       printf("\n thread exit");
+       wifi_manager_unset_scan_state_changed_cb(wifi);
+       wifi_manager_unset_connection_state_changed_cb(wifi);
+
+       rv = wifi_manager_deinitialize(wifi);
+
+       if (rv != WIFI_MANAGER_ERROR_NONE) {
+               printf("Fail to deinitialize.\n");
+               ret = SA_ERROR_UNKNOWN;
+       }
+
+       wifi_handle = NULL;
+
+       printf("\n end __connect_wifi");
+
        return ret;
 }
 
 static int __connect_ethernet(sa_eth_s *info)
 {
-       int rv = 0;
-       int err = 0;
-       int ret = 0;
-       connection_h connection = NULL;
-
        if (info == NULL) {
                return -1;
        }
@@ -713,30 +459,46 @@ static int __get_wifi_info(sa_wifi_s *info)
 }
 
 static int __get_ethernet_info(sa_eth_s *info) {
+       connection_h connection = NULL;
        char *ip_addr = NULL;
-       int input;
-       int rv;
+       int rv = 0;
+       int ret = 0;
 
        if (info != NULL) {
                // only for ipv4
-               rv = connection_get_ip_address(connection_handle, CONNECTION_ADDRESS_FAMILY_IPV4, &ip_addr);
-               if (ip_addr == NULL) {
-                       printf("\nIPv4 address does not exist");
+               rv = connection_create(&connection);
+               printf("\nconnection_create : ret[%s]", __print_error(rv));
+               if (rv != CONNECTION_ERROR_NONE) {
+                       printf("\ncreate connection handle error [%s]", __print_error(rv));
                        return -1;
                }
-               printf("\nIPv4 address : %s", ip_addr);
-               info->staticInfo = (sa_network_static_s *)malloc(sizeof(sa_network_static_s));
-               if (info->staticInfo != NULL) {
-                       memcpy(info->staticInfo->ipAddress, ip_addr, MIN(strlen(ip_addr), sizeof(info->staticInfo->ipAddress)-1));
+
+               rv = connection_get_ip_address(connection, CONNECTION_ADDRESS_FAMILY_IPV4, &ip_addr);
+               if (ip_addr != NULL) {
+                       printf("\nIPv4 address does not exist");
+                       printf("\nIPv4 address : %s", ip_addr);
+                       info->staticInfo = (sa_network_static_s *)malloc(sizeof(sa_network_static_s));
+                       if (info->staticInfo != NULL) {
+                               memcpy(info->staticInfo->ipAddress, ip_addr, MIN(strlen(ip_addr), sizeof(info->staticInfo->ipAddress)-1));
+                       }
+                       free(ip_addr);
+               } else {
+                       ret = -1;
+               }
+
+               rv = connection_destroy(connection);
+               if (rv != CONNECTION_ERROR_NONE) {
+                       printf("connection destroy fail [%s]\n", __print_error(rv));
+                       return -1;
                }
-               free(ip_addr);
        }
 
-       return 0;
+       return ret;
 }
 
 sa_error_e sa_network_get_state(sa_network_state_e *conn_state, sa_network_type_e *conn_type)
 {
+       connection_h connection = NULL;
        sa_error_e ret = SA_ERROR_UNKNOWN;
        connection_type_e net_state;
        connection_wifi_state_e wifi_state;
@@ -744,14 +506,15 @@ sa_error_e sa_network_get_state(sa_network_state_e *conn_state, sa_network_type_
 
        printf("\nsa_network_get_state~~~");
 
-
-       if(__start_setup_network()) {
-               printf("\nnot ready to use network api due to async reply from network api");
-               return SA_ERROR_NOT_AVAILABLE;
+       rv = connection_create(&connection);
+       printf("\nconnection_create : ret[%s]", __print_error(rv));
+       if (rv != CONNECTION_ERROR_NONE) {
+               printf("\ncreate connection handle error [%s]", __print_error(rv));
+               return -1;
        }
        
        // check network state(eth/wifi)
-       rv = connection_get_type(connection_handle, &net_state);
+       rv = connection_get_type(connection, &net_state);
        printf("\nconnection_get_type return(%d)", rv);
        if (rv != CONNECTION_ERROR_NONE) {
                printf("\nFail to get network state [%s]\n", __print_error(rv));
@@ -763,7 +526,7 @@ sa_error_e sa_network_get_state(sa_network_state_e *conn_state, sa_network_type_
                } else if (CONNECTION_TYPE_WIFI == net_state) {
                        *conn_type = SA_NETWORK_TYPE_WIFI;
                        // check wifi state
-                       if (!__check_wifi_state(connection_handle, wifi_state)) {
+                       if (!__check_wifi_state(connection, wifi_state)) {
                                if (wifi_state == CONNECTION_WIFI_STATE_CONNECTED) {
                                        *conn_state = SA_NETWORK_STATE_CONNECTED;
                                } else {
@@ -779,43 +542,43 @@ sa_error_e sa_network_get_state(sa_network_state_e *conn_state, sa_network_type_
                }
        }
 
+       rv = connection_destroy(connection);
+       if (rv != CONNECTION_ERROR_NONE) {
+               printf("connection destroy fail [%s]\n", __print_error(rv));
+               return -1;
+       }
+
        return ret;
 }
 
 sa_error_e sa_network_set_connection(sa_network_s *info)
 {
        sa_error_e ret = SA_ERROR_NONE;
-       connection_wifi_state_e wifi_state;
        int retWifi = 0;
        int retEth = 0;
 
        if (info == NULL) {
                return SA_ERROR_INVALID_PARAMETER;
        }
-
-       if(__start_setup_network()) {
-               printf("\nnot ready to use network api due to async reply from network api");
-       }
        
-       // first priority is wifi
        if (info->wifi != NULL) {
                if (info->wifi->enabled == TRUE) {
-                       // wifi setting and waiting to get response
                        retWifi = __connect_wifi(info->wifi);
                        printf("\nreturn wifi [%d]", retWifi);
-                       // or if wifi is enabled but fail to connect, try to connect eth
-
-                       /*
-                       if (info->eth != NULL) {
-                               if (wifi_state != CONNECTION_WIFI_STATE_CONNECTED && info->eth->enabled == TRUE) {
-                                       retEth = __connect_ethernet(info->eth);
-                               }       
-                       }*/
+
                } else {
                        printf("\nwifi enabled flag is false");
                }
        }
 
+       if (info->eth != NULL) {
+               // decide whether it will be set according to policy
+               if (info->eth->enabled == TRUE){
+                       retEth = __connect_ethernet(info->eth);
+                       printf("\nreturn ethernet [%d]", retEth);
+               }
+       }
+
        printf("\nEnd setting");
                
        return ret;
@@ -823,31 +586,29 @@ sa_error_e sa_network_set_connection(sa_network_s *info)
 
 sa_error_e sa_network_get_connection(sa_network_s *info)
 {
+       connection_h connection = NULL;
        sa_error_e ret = SA_ERROR_NONE;
        connection_type_e net_state;
-       connection_wifi_state_e wifi_state;
-       sa_network_state_e conn_state;
-       sa_network_type_e conn_type;
+
        int rv = 0;
 
-       if(__start_setup_network()) {
-               printf("\nnot ready to use network api due to async reply from network api");
-       }
        // get network state
-       if (connection_handle != NULL) {
-               rv = connection_get_type(connection_handle, &net_state);
+       rv = connection_create(&connection);
+       printf("\nconnection_create : ret[%s]", __print_error(rv));
+       if (rv != CONNECTION_ERROR_NONE) {
+               printf("\ncreate connection handle error[%s]", __print_error(rv));
+               return SA_ERROR_UNKNOWN;
+       }
+
+       if (connection != NULL) {
+               rv = connection_get_type(connection, &net_state);
                if (rv == CONNECTION_ERROR_NONE) {
                        // get detail information
                        if (CONNECTION_TYPE_WIFI == net_state) {
-                               // check wifi state
-                               if (!__check_wifi_state(connection_handle, wifi_state)) {
-                                       if (wifi_state == CONNECTION_WIFI_STATE_CONNECTED) {
-                                               // detail info for wifi
-                                               info->wifi = (sa_wifi_s *)malloc(sizeof(sa_wifi_s));
-                                               if (info->wifi != NULL) {
-                                                       ret = __get_wifi_info(info->wifi);
-                                               }
-                                       } 
+                               // detail info for wifi
+                               info->wifi = (sa_wifi_s *)malloc(sizeof(sa_wifi_s));
+                               if (info->wifi != NULL) {
+                                       ret = __get_wifi_info(info->wifi);
                                }
                        } else if (CONNECTION_TYPE_ETHERNET == net_state) {
                                // detail info for ethernet
@@ -855,12 +616,20 @@ sa_error_e sa_network_get_connection(sa_network_s *info)
                                if (info->eth != NULL) {
                                        ret = __get_ethernet_info(info->eth);
                                }
-                       } 
+                       }
+               } else {
+                       printf("\nconnection_get_type is err [%s]", __print_error(rv)); 
                }
        } else {
                printf("\nConnection handle is null");
        }
 
+       rv = connection_destroy(connection);
+       if (rv != CONNECTION_ERROR_NONE) {
+               printf("connection destroy fail [%s]\n", __print_error(rv));
+               return -1;
+       }
+
        return ret;
 }
 
@@ -869,91 +638,4 @@ sa_error_e sa_network_deactivate(void)
        sa_error_e ret = SA_ERROR_NONE;
 
        return ret;
-}
-
-
-
-
-
-
-
-
-
-
-
-
-wifi_manager_h wifi = NULL;
-
-
-int test_wifi_manager_init(void)
-{
-       int rv = wifi_manager_initialize(&wifi);
-
-       if (rv == WIFI_MANAGER_ERROR_NONE) {
-               wifi_manager_set_device_state_changed_cb(wifi, __test_device_state_callback, "1");
-               wifi_manager_set_scan_state_changed_cb(wifi, __test_scan_changed_callback, "1");
-               wifi_manager_set_background_scan_cb(wifi, __test_bg_scan_completed_callback, "1");
-               wifi_manager_set_ip_conflict_cb(wifi, __test_ip_conflict_callback, "1");
-               wifi_manager_set_connection_state_changed_cb(wifi, __test_connection_state_callback, "1");
-               wifi_manager_set_rssi_level_changed_cb(wifi, __test_rssi_level_callback, "1");
-               wifi_manager_tdls_set_state_changed_cb(wifi, __test_tdls_state_callback, "1");
-               wifi_manager_tdls_set_discovered_cb(wifi, __test_tdls_discover_callback, "1");
-               wifi_manager_set_module_state_changed_cb(wifi, __test_get_wifi_module_state_callback, "1");
-
-       } else {
-               printf("[1] Wifi init failed [%s]\n", __test_convert_error_to_string(rv));
-               return -1;
-       }
-
-       printf("Wifi init succeeded\n");
-       return 1;
-}
-
-int test_wifi_manager_activate(void)
-{
-       int rv = 0;
-
-       rv = wifi_manager_activate(wifi, __test_activated_callback, NULL);
-
-       if (rv != WIFI_MANAGER_ERROR_NONE) {
-               printf("Fail to activate Wi-Fi device [%s]\n", __test_convert_error_to_string(rv));
-               return -1;
-       }
-
-       printf("Success to activate Wi-Fi device\n");
-
-       return 1;
-}
-
-
-void sa_network_test(void)
-{
-       printf("\nsa_network_test ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
-       
-       GMainLoop *mainloop;
-       mainloop = g_main_loop_new(NULL, FALSE);
-
-       test_wifi_manager_init();
-       //test_wifi_manager_activate();
-
-
-       wifi_manager_initialize(&wifi_handle);
-       if(wifi_handle)
-       {
-               wifi_manager_set_device_state_changed_cb(wifi_handle, __test_device_state_callback, "2");
-               wifi_manager_set_scan_state_changed_cb(wifi_handle, __test_scan_changed_callback, "2");
-               wifi_manager_set_background_scan_cb(wifi_handle, __test_bg_scan_completed_callback, "2");
-               wifi_manager_set_ip_conflict_cb(wifi_handle, __test_ip_conflict_callback, "2");
-               wifi_manager_set_connection_state_changed_cb(wifi_handle, __test_connection_state_callback, "2");
-               wifi_manager_set_rssi_level_changed_cb(wifi_handle, __test_rssi_level_callback, "2");
-               wifi_manager_tdls_set_state_changed_cb(wifi_handle, __test_tdls_state_callback, "2");
-               wifi_manager_tdls_set_discovered_cb(wifi_handle, __test_tdls_discover_callback, "2");
-               wifi_manager_set_module_state_changed_cb(wifi_handle, __test_get_wifi_module_state_callback, "2");
-               
-               wifi_manager_activate(wifi_handle, __test_activated_callback, NULL);
-       }
-
-       g_main_loop_run(mainloop);
-       printf("\nsa_network_test ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<END>");
-}
-
+}
\ No newline at end of file