checked wifi configuration. temp
authorjiehwan.park <jiehwan.park@samsung.com>
Tue, 26 Dec 2017 12:11:35 +0000 (21:11 +0900)
committerjiehwan.park <jiehwan.park@samsung.com>
Tue, 26 Dec 2017 12:11:35 +0000 (21:11 +0900)
Signed-off-by: jiehwan.park <jiehwan.park@samsung.com>
src/adaptor.c
src/input_file.c
src/sa_common.h
src/setup_network.c

index 17947c42c74dd0e7b2d8a1b891a15dd7271898bf..e7b7f909df675d6413650fbae70e044a0def9325 100644 (file)
@@ -32,19 +32,26 @@ static gboolean prepare(GSource *source, gint *timeout)
 
 gboolean setup(gpointer data)
 {
+       sa_error_e ret = SA_ERROR_NONE;
        sa_config_s config = {0,};
        sa_error_e file_read = SA_ERROR_NONE;
        
-       printf("callback>>>setup start !!!\n");
+       _D("callback>>>setup start !!!");
 
        file_read = sa_inputfile_get_config_info(&config);
        if (file_read == SA_ERROR_NONE) {
                if (config.systemData != NULL) {
-                       sa_setup_system(config.systemData);
+                       ret = sa_setup_system(config.systemData);
+                       if(ret != SA_ERROR_NONE) {
+                               _E("sa_setup_system return error(%d)", ret);
+                       }
                }
 
                if (config.networkData != NULL) {
-                       sa_setup_network(config.networkData);
+                       ret = sa_setup_network(config.networkData);
+                       if(ret != SA_ERROR_NONE) {
+                               _E("sa_setup_network return error(%d)", ret);
+                       }
                }
        }
 
@@ -56,11 +63,14 @@ gboolean Execution(GSource *source, GSourceFunc callbackFuntion, gpointer user_d
        GMainLoop* loop = user_data;
        GSource* src = NULL;
        
-       printf("GSourceFuncs>>>Execution !!!\n");
+       _D("GSourceFuncs>>>Execution !!!");
 
        callbackFuntion(NULL);
 
-
+       if(user_data) {
+               g_main_loop_quit( (GMainLoop*)user_data );
+       }
+       
        // if return value is "FALSE", function stop
        // if return value is "TRUE", function restart
        return FALSE;
@@ -79,7 +89,7 @@ static gpointer adder_thread_output (gpointer data)
 {
        GSource* src = NULL;
 
-       printf("adder_thread_output ~~~\n");
+       _D("adder_thread_output ~~~");
 
        GMainLoop* main_loop;
        GMainContext* context = NULL;
@@ -91,11 +101,16 @@ static gpointer adder_thread_output (gpointer data)
        
        g_source_set_name (src, "setup-adaptor-func");
        g_source_set_priority (src, G_PRIORITY_DEFAULT);
-       g_source_set_callback(src, (GSourceFunc) setup, (gpointer) NULL, NULL); // set callback function
+       g_source_set_callback(src, (GSourceFunc) setup, (gpointer) main_loop, NULL);    // set callback function
        g_source_attach (src, context);
 
        g_main_loop_run(main_loop);
 
+       g_main_loop_unref(main_loop);
+       if(data) {
+               g_main_loop_quit( (GMainLoop*)data );
+       }
+
        return NULL;
 }
 
@@ -105,12 +120,12 @@ int main(int argc, char *argv[])
        sa_file_state_e file_state = SA_FILE_STATE_NOT_EXISTED;
        sa_config_s config = {0,};
 
-       printf("main function~~~\n");
+       _D("main function~~~");
        // check if exists config file
        file_state = sa_inputfile_get_config_state();
-       printf("first check for config enable(%d)\n", file_state);
+       _D("first check for config enable(%d)", file_state);
        if (SA_FILE_STATE_NOT_EXISTED == file_state) {
-               printf("config file is not existed, disable setup-adaptor\n");
+               _D("config file is not existed, disable setup-adaptor");
                return 0;
        }
 
@@ -119,7 +134,7 @@ int main(int argc, char *argv[])
 
        main_loop = g_main_loop_new(NULL, FALSE);
 
-       g_thread_create((GThreadFunc) adder_thread_output, NULL, TRUE, NULL);
+       g_thread_create((GThreadFunc) adder_thread_output, main_loop, TRUE, NULL);
 
        g_main_loop_run(main_loop);
        g_main_loop_unref(main_loop);
index d7d54d714fad673bcfd55862c92d92bfa693a30e..74f016a5f87ca203e1e69bb4a12507d36bb36c37 100644 (file)
@@ -39,25 +39,25 @@ static void *__config_main_loop(void *arg)
        file_state_cb callback;
        char buffer[BUF_LEN];
 
-       printf("\n__config_main_loop start\n");
+       _D("__config_main_loop start\n");
 
        callback = (file_state_cb)arg;
 
        if (callback == NULL) {
-               printf("\nCcallback is null for event");
+               _D("Ccallback is null for event");
                return NULL;
        }
 
        fd = inotify_init();
 
        if (fd < 0) {
-               printf("\ninotify_init error");
+               _D("inotify_init error");
                return NULL;
        }
 
        wd = inotify_add_watch(fd, CONFIG_FOLDER, IN_MODIFY | IN_CREATE | IN_DELETE);
        // Start callack
-       printf("\nRegisterd Callback Triggered");
+       _D("Registerd Callback Triggered");
        callback(SA_FILE_STATE_REGISTERED, NULL, NULL);
        while (1) {
                int length, i = 0;
@@ -68,33 +68,35 @@ static void *__config_main_loop(void *arg)
 
                while (i < length) {
                        struct inotify_event *event = (struct inotify_event *)&buffer[i];
-                       printf("\n[debug] wd=%d mask=%d cookie=%d len=%d dir=%s", event->wd, event->mask, event->cookie, event->len, (event->mask & IN_ISDIR) ? "yes" : "no");
+                       _D("[debug] wd=%d mask=%d cookie=%d len=%d dir=%s", event->wd, event->mask, event->cookie, event->len, (event->mask & IN_ISDIR) ? "yes" : "no");
                        if (event->len) {
                                if (event->mask & IN_CREATE) {
-                                       if (event->mask & IN_ISDIR)
-                                               printf("\nThe directory %s was created", event->name);
+                                       if (event->mask & IN_ISDIR) {
+                                               _D("The directory %s was created", event->name);
+                                       }
                                        else {
-                                               printf("\nThe file %s was create.", event->name);
+                                               _D("The file %s was create.", event->name);
                                                if (!strcmp(event->name, CONFIG_NAME)) {
-                                                       printf("\nconfig.json is created!!");
+                                                       _D("config.json is created!!");
                                                }
                                        }
                                } else if (event->mask & IN_DELETE) {
-                                       if (event->mask & IN_ISDIR)
-                                               printf("\nThe directory %s was deleted.", event->name);
+                                       if (event->mask & IN_ISDIR) {
+                                               _D("The directory %s was deleted.", event->name);
+                                       }
                                        else {
-                                               printf("\nThe file %s was deleted", event->name);
+                                               _D("The file %s was deleted", event->name);
                                                if (!strcmp(event->name, CONFIG_NAME)) {
-                                                       printf("\nconfig.json is deleted!!");
+                                                       _D("config.json is deleted!!");
                                                }
                                        }
                                } else if (event->mask & IN_MODIFY) {
                                        if (event->mask & IN_ISDIR) {
-                                               printf("\nThe directory %s was modified", event->name);
+                                               _D("The directory %s was modified", event->name);
                                        }
                                        else {
                                                if (!strcmp(event->name, CONFIG_NAME)) {
-                                                       printf("\nconfig.json is modified!!");
+                                                       _D("config.json is modified!!");
                                                        callback(SA_FILE_STATE_CHANGED, NULL, NULL);
                                                }
                                        }
@@ -118,7 +120,7 @@ static int __init_event_listener(file_state_cb callback)
 
        // Start thread to create in order to receive event
        if (pthread_create(&p_thread, NULL, &__config_main_loop, callback) < 0) {
-               printf("\n__init_event_listener create error");
+               _D("__init_event_listener create error");
                return -1;
        }
 #else
@@ -184,7 +186,7 @@ static char *json_get_string_from_obj(json_object *inputObj, char *key)
                return NULL;
        }
 
-       printf("\nstring object [%s]", ret_buf);
+       _D("string object [%s]", ret_buf);
        return ret_buf;
 }
 
@@ -270,7 +272,7 @@ static int __parse_network_static_info(json_object *inputObj, sa_network_static_
        char *secondaryDnsServer = NULL;
 
        if (inputObj == NULL || staticInfo == NULL) {
-               printf("\n__parse_network_static_info input error");
+               _D("__parse_network_static_info input error");
                return -1;
        }
 
@@ -322,7 +324,7 @@ static int __parse_network_eth(json_object *inputObj, sa_eth_s *eth)
        int ret = 0;
 
        if (inputObj == NULL || eth == NULL) {
-               printf("\n__parse_network_eth input error");
+               _D("__parse_network_eth input error");
                return -1;
        }
 
@@ -338,8 +340,6 @@ static int __parse_network_eth(json_object *inputObj, sa_eth_s *eth)
                } else {
                        ret = -1;
                }
-       } else {
-               printf("\ndhcp is true");
        }
 
        return ret;
@@ -352,7 +352,7 @@ static int __parse_network_wifi(json_object *inputObj, sa_wifi_s *wifi)
        int ret = 0;
 
        if (inputObj == NULL || wifi == NULL) {
-               printf("\n__parse_network_wifi input error");
+               _D("__parse_network_wifi input error");
                return -1;
        }
        // enabled
@@ -387,9 +387,7 @@ static int __parse_network_wifi(json_object *inputObj, sa_wifi_s *wifi)
                } else {
                        ret = -1;
                }
-       } else {
-               printf("\ndhcp is true");
-       }
+       } 
 
        return ret;
 }
@@ -401,7 +399,7 @@ static int __parse_network_data(json_object *inputObj, sa_config_s *setupConfig)
        int wifiRet = 0, ethRet = 0;
 
        if (inputObj == NULL) {
-               printf("\n__parse_network_data input error");
+               _D("__parse_network_data input error");
                return -1;
        }
 
@@ -415,7 +413,7 @@ static int __parse_network_data(json_object *inputObj, sa_config_s *setupConfig)
                                if (setupConfig->networkData->wifi != NULL) {
                                        wifiRet = __parse_network_wifi(wifiObj, setupConfig->networkData->wifi);
                                } else {
-                                       printf("\nnetwork->wifi malloc fail");
+                                       _D("network->wifi malloc fail");
                                }
                        }
                }
@@ -428,17 +426,17 @@ static int __parse_network_data(json_object *inputObj, sa_config_s *setupConfig)
                                if (setupConfig->networkData->eth != NULL) {
                                        ethRet = __parse_network_eth(ethernetObj, setupConfig->networkData->eth);
                                } else {
-                                       printf("\nnetwork->eth malloc fail");
+                                       _D("network->eth malloc fail");
                                }
                        }
                }
        } else {
-               printf("\nmalloc fail etupConfig->networkData");
+               _D("malloc fail etupConfig->networkData");
        }
 
        // if both of network interfaces are failed, it would return -1
        if (wifiRet != 0 && ethRet != 0) {
-               printf("\n__parse_network_data fail");
+               _D("__parse_network_data fail");
                return -1;
        }
        
@@ -451,7 +449,7 @@ static int __parse_system_data(json_object *inputObj, sa_config_s *setupConfig)
        int ret = 0;
        char *httpProxyHost = NULL;
        if (inputObj == NULL) {
-               printf("\n__parse_system_data input error");
+               _D("__parse_system_data input error");
                return -1;
        }
        
@@ -471,11 +469,11 @@ static int __parse_system_data(json_object *inputObj, sa_config_s *setupConfig)
 
                        } else {
                                ret = -1;
-                               printf("\nmalloc fail setupConfig->systemData->proxy");
+                               _D("malloc fail setupConfig->systemData->proxy");
                        }
                } else {
                        ret = -1;
-                       printf("\nmalloc fail setupConfig->systemData");
+                       _D("malloc fail setupConfig->systemData");
                }
        }
 
@@ -488,7 +486,7 @@ static int __parse_version(json_object *inputObj, sa_config_s *setupConfig)
        char *version = NULL;
 
        if (inputObj == NULL) {
-               printf("\n__parse_version input error");
+               _D("__parse_version input error");
                return -1;
        }
 
@@ -522,7 +520,7 @@ static int __parse_config(char *file, sa_config_s *setupConfig)
                        if (jsonData != NULL) {
                                memset(jsonData, 0x00, len + 1);
                                readLen = read(fd, jsonData, len);
-                               printf("\nJSON full data[%s]", jsonData);
+                               _D("JSON full data[%s]", jsonData);
 
                                configObj = json_tokener_parse(jsonData);
 
@@ -538,21 +536,21 @@ static int __parse_config(char *file, sa_config_s *setupConfig)
                                        }
                                } else {
                                        ret = -1;
-                                       printf("\nConfigObj is not existed");
+                                       _D("ConfigObj is not existed");
                                }
                                
                                json_object_put(configObj);
                                free(jsonData);
                        } else {
                                ret = -1;
-                               printf("\nmemory allocation fail for jsonData");
+                               _D("memory allocation fail for jsonData");
                        }
                }
 
                close(fd);
        } else {
                ret = -1;
-               printf("\nconfig file can't be opened");
+               _D("config file can't be opened");
        }
 
        return ret;
@@ -568,7 +566,7 @@ sa_error_e sa_inputfile_get_config_info(sa_config_s *config)
        // parsing and fill into struct
        ret = __parse_config(CONFIG_FILE, config);
        if (ret != 0) {
-               printf("\nConfig parsing error");
+               _D("Config parsing error");
                return SA_ERROR_PARSING;
        }
 
index 01de5e82c63b6cd951b118077bb8b10420e2bce1..06ea4ddcce95aea6d5c1513f578d29738e337476 100644 (file)
 #ifndef FALSE
 #define FALSE 0
 #endif
+
+#if 0
 #define _E(fmt, arg...) LOGE(fmt, ##arg)
 #define _D(fmt, arg...) LOGD(fmt, ##arg)
 #define _W(fmt, arg...) LOGW(fmt, ##arg)
+#else
+#define _E(fmt, arg...) {printf(fmt, ##arg);printf("\n");}
+#define _D(fmt, arg...) {printf(fmt, ##arg);printf("\n");}
+#define _W(fmt, arg...) {printf(fmt, ##arg);printf("\n");}
+#endif
 
 #endif                                                 /* __SA_COMMON_H__ */
index 8da7a0393a9f7b06cf499ab012da0d83e8b80583..af5117991cfb55c101fa31fe578519b7244460be 100644 (file)
 #include "net_connection.h"
 #include "wifi-manager.h"
 
-static char wifi_password[99+1];
-static wifi_manager_h wifi_handle = NULL;
+typedef struct {
+       wifi_manager_h wifi_h;
+       GMainLoop *loop;
+       char *ssid;
+       char *password;
+} _wifi_data_s;
 
-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)
-{
-       return wifi_password;
-}
-
-
-#define NETWORK_CHECK_RETRY_MAX 10
 
 static char *__print_connection_state(sa_network_state_e state)
 {
@@ -138,6 +120,18 @@ static const char *__print_connection_type(connection_type_e type)
        }
 }
 
+static const char *__print_network_type(sa_network_type_e type)
+{
+       switch (type) {
+       case SA_NETWORK_TYPE_ETH:
+               return "Ethernet";
+       case SA_NETWORK_TYPE_WIFI:
+               return "Wifi";
+       default:
+               return "Unknown";
+       }
+}
+
 static const char *__print_wifi_error(wifi_manager_error_e err_type)
 {
        switch (err_type) {
@@ -197,251 +191,190 @@ static const char *__print_wifi_error(wifi_manager_error_e err_type)
 }
 
 
-static int __check_wifi_state(connection_h connection, connection_wifi_state_e wifi_state)
-{
-       int rv = 0;
-
-       if (connection == NULL) {
-               return -1;
-       }
-
-       rv = connection_get_wifi_state(connection, &wifi_state);
-       if (rv != CONNECTION_ERROR_NONE) {
-               printf("\nFail to get WiFi state [%s]", __print_error(rv));
-       } else {
-               printf("\nRetval = [%s] WiFi state [%s]", __print_error(rv), __print_wifi_state(wifi_state));
-       }
-
-       return 0;
-}
-
-static void __activated_callback(wifi_manager_error_e result, void *user_data)
+static void __wifi_exit_loop(_wifi_data_s *wifi_data)
 {
-       if (result == WIFI_MANAGER_ERROR_NONE) {
-               printf("\nMY~~~Wi-Fi Activation Succeeded");
-               
+       _D("__wifi_exit_loop !!!");
+       if( wifi_data ) {
+               g_main_loop_quit( (GMainLoop*)wifi_data->loop );
        }
-       else
-               printf("\nWi-Fi Activation Failed! error : %s", __print_wifi_error(result));
 }
 
-static void __scan_request_callback(wifi_manager_error_e error_code, void* user_data)
-{      
-       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)
+static void __wifi_connected_cb(wifi_manager_error_e result, void *user_data)
 {
-       if (result == WIFI_MANAGER_ERROR_NONE)
-               printf("\nWi-Fi Deactivation Succeeded");
-       else
-               printf("\nWi-Fi Deactivation Failed! error : %s", __print_wifi_error(result));
-}
+       _wifi_data_s *wifi_data = (_wifi_data_s*)user_data;
 
-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");
+       if (result == WIFI_MANAGER_ERROR_NONE){
+               _D("__wifi_connected_cb() connected Successfully !!!");
        }
+       else {
+               _D("__wifi_connected_cb() connection Failed!(%s)", __print_wifi_error(result));
+       }
+       __wifi_exit_loop(wifi_data);
 }
 
-static int __compare_ap_name(const char *ap_name, const char *ap_name_part)
-{
-       int ap_name_len = strlen(ap_name);
-       int ap_name_part_len = strlen(ap_name_part);
-
-       if (strncmp(ap_name, ap_name_part,
-                               ap_name_len > ap_name_part_len ? ap_name_len : ap_name_part_len) == 0)
-               return TRUE;
-       else
-               return FALSE;
-}
 
-static void __connected_callback(wifi_manager_error_e result, void *user_data)
+// CAUTION : this callback function should be boolean type.
+// return true : continue search ap (not found)
+// return false : stop search ap (found)
+static bool __wifi_connect_ap_cb(wifi_manager_ap_h ap, void *user_data)
 {
-       if (result == WIFI_MANAGER_ERROR_NONE)
-               printf("\nWi-Fi Connection Succeeded");
-       else
-               printf("\nWi-Fi Connection Failed! error : %s", __print_wifi_error(result));
-}
+       _wifi_data_s *wifi_data = (_wifi_data_s*)user_data;
 
-static void __scan_changed_callback(wifi_manager_scan_state_e state, void* user_data)
-{
-       printf("\nScan changed, scan state : %d", state);
-       if (state == WIFI_MANAGER_SCAN_STATE_NOT_SCANNING) {
-               wifi_manager_unset_scan_state_changed_cb(wifi_handle);
-       }
-}
+       _D("__wifi_connect_ap_cb()");
 
-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;
-
 
-       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));
-               return;
+               _D("Fail to get AP name [%s]", __print_wifi_error((wifi_manager_error_e)rv));
+               g_free(ap_name);
+               return false;
+       } else {
+               _D("AP name = [%s]/[%s]", ap_name, wifi_data->ssid);
        }
 
-       if (__compare_ap_name(ap_name, ap_name_part)) {
+       if (g_strcmp0(ap_name, wifi_data->ssid) == 0) {
                int required = FALSE;
 
+               _D("OK!!! connect AP [%s]", ap_name);
+
                rv = wifi_manager_ap_is_passphrase_required(ap, &required);
                
                if (rv == WIFI_MANAGER_ERROR_NONE) {
                        if (required) {
-                               char *passphrase = __get_setup_network_wifi_password();
-                               printf("\n passphrase1 : [%s]", passphrase);
-                               rv = wifi_manager_ap_set_passphrase(ap, passphrase);
-                               printf("\n passphrase2 : [%s]", passphrase);
+                               _D(" passphrase1 : [%s]", wifi_data->password);
+                               rv = wifi_manager_ap_set_passphrase(ap, wifi_data->password);
                                if (rv != WIFI_MANAGER_ERROR_NONE) {
-                                       printf("\nFail to set passphrase : %s", __print_wifi_error(rv));
-                                       if (ap_name != NULL) 
-                                               free(ap_name);;
-                                       return;
+                                       _D("Fail to set passphrase : %s", __print_wifi_error(rv));
+                                       __wifi_exit_loop(wifi_data);
                                }
                        }
-                       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);
+                       if (rv == WIFI_MANAGER_ERROR_NONE)
+                       {
+                               rv = wifi_manager_connect(wifi_data->wifi_h, ap, __wifi_connected_cb, wifi_data);
+                               if (rv != WIFI_MANAGER_ERROR_NONE) {
+                                       _D("Fail to connection request [%s] : %s", ap_name, __print_wifi_error(rv));
+                                       __wifi_exit_loop(wifi_data);
+                               }
+                               else {
+                                       _D("Success to connection request [%s]", ap_name);
+                               }
                        }
                }
-       }
-
-       if (ap_name != NULL) 
-               free(ap_name);
+               else {
+                       _D("Fail to passphrase_required request [%s]", __print_wifi_error(rv));
+                       __wifi_exit_loop(wifi_data);
+               }
 
-       __do_exit();
+               g_free(ap_name);
+               return false;
+       }
 
-       return;
+       g_free(ap_name);
+       return true;
 }
 
-void __activate_state_changed_cb(wifi_manager_device_state_e state, void *user_data)
+static void __wifi_scan_finished_cb(wifi_manager_error_e result, void *user_data)
 {
-       printf("\n__wifi_state_changed_cb() state=[%d]", state);
+       _wifi_data_s *wifi_data = (_wifi_data_s*)user_data;
 
-       if (state == WIFI_MANAGER_DEVICE_STATE_ACTIVATED) {
-               printf("\n__wifi_state_changed_cb start scan...");
-               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);
-       }
-}
+       _D("__wifi_scan_finished_cb() result=[%s]", __print_wifi_error(result));
 
-static void __do_exit(void)
-{
-       printf("\nExit Case :: To-Do");
+       wifi_manager_foreach_found_ap(wifi_data->wifi_h, __wifi_connect_ap_cb, wifi_data);
 }
 
-static int __do_activate(wifi_manager_h *wifi, sa_wifi_s *info)
+static void __wifi_state_changed_cb(wifi_manager_device_state_e state, void *user_data)
 {
-       int rv = 0;
-
-       printf("\nativating state");
+       _wifi_data_s *wifi_data = (_wifi_data_s*)user_data;
 
-       rv = wifi_manager_activate(*wifi, __activated_callback, (void *)info);
-       printf("\nwifi_manager_activate:ret[%s]", __print_wifi_error(rv));
+       _D("__wifi_state_changed_cb() state=[%d]", state);
 
-       return rv;
+       if (state == WIFI_MANAGER_DEVICE_STATE_ACTIVATED) {
+               wifi_manager_scan(wifi_data->wifi_h, __wifi_scan_finished_cb, wifi_data);
+               wifi_manager_unset_device_state_changed_cb(wifi_data->wifi_h);
+       }
 }
 
-static void __do_connect(sa_wifi_s *info)
+static void __wifi_activate_cb(wifi_manager_error_e result, void *user_data)
 {
-       int rv = 0;
-       printf("\nscanned state");
-       
-       __set_setup_network_wifi_password(info->password);
-
-       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));
-               __do_exit();
-       }
-       
+       _D("__wifi_activate_cb() result=[%s]", __print_wifi_error(result));
 }
 
-static int connect_wifi(sa_wifi_s *info)
+static int __wifi_connect_main(sa_wifi_s *info)
 {
-       int rv = 0;
-       sa_error_e ret = SA_ERROR_UNKNOWN;
-       
+       GMainLoop *gmain_loop = NULL;
        GMainContext *context;
-       GMainLoop *main_loop;
 
-       context = g_main_context_new ();
-    g_main_context_push_thread_default (context);      // should be.. to get mainloop message in thread
-    main_loop = g_main_loop_new (context, FALSE);
+       _wifi_data_s wifi_data = {0,};
 
-       printf("\n------------------- connect_wifi -------------------");
+       int rv = 0;
+       sa_error_e ret = SA_ERROR_UNKNOWN;
+       wifi_manager_h wifi_h = NULL;
+       
+       _D("__wifi_connect_main (ssid=%s, pw=%s)", info->ssid, info->password);
 
        if (info == NULL) {
+               _E("invalid input (sa_wifi_s*)");
                return -1;
        }
 
-       wifi_manager_h wifi = NULL;
-       
-       rv = wifi_manager_initialize(&wifi);
+       context = g_main_context_new ();
+    g_main_context_push_thread_default (context);      // should be.. to get mainloop message in thread
+    gmain_loop = g_main_loop_new (context, FALSE);
+    
+       rv = wifi_manager_initialize(&wifi_h);
 
        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);
+               // fill the token data
+               wifi_data.ssid = info->ssid;
+               wifi_data.password = info->password;
+               wifi_data.loop = gmain_loop;
+               wifi_data.wifi_h = wifi_h;
+
+               rv = wifi_manager_set_device_state_changed_cb(wifi_h, __wifi_state_changed_cb, (void *)&wifi_data);
+               rv = wifi_manager_activate(wifi_h, __wifi_activate_cb, NULL);
+               if (rv != WIFI_MANAGER_ERROR_NONE) {
+                       _E("wifi_manager_activated failed.[%s]", __print_wifi_error(rv));
+                       return SA_ERROR_UNKNOWN;
                }
-
-       else {
-               printf("\\nWifi init failed [%s]", __print_wifi_error(rv));
+       } 
+       else {
+               _E("Wifi init failed [%s]", __print_wifi_error(rv));
                return SA_ERROR_UNKNOWN;
        }
 
-       g_main_loop_run (main_loop);
+       g_main_loop_run (gmain_loop);
 
+       gmain_loop = NULL;
 
-/*
-       printf("\n thread exit");
-       wifi_manager_unset_scan_state_changed_cb(wifi);
-       wifi_manager_unset_connection_state_changed_cb(wifi);
+       wifi_manager_unset_scan_state_changed_cb(wifi_h);
+       wifi_manager_unset_connection_state_changed_cb(wifi_h);
 
-       rv = wifi_manager_deinitialize(wifi);
+       rv = wifi_manager_deinitialize(wifi_h);
 
        if (rv != WIFI_MANAGER_ERROR_NONE) {
-               printf("Fail to deinitialize.\n");
+               _D("Fail to deinitialize.");
                ret = SA_ERROR_UNKNOWN;
        }
 
-       wifi_handle = NULL;
+       _D("__wifi_connect_main completed.");
 
-       printf("\n end connect_wifi");
-*/
        return ret;
 }
 
-static int connect_ethernet(sa_eth_s *info)
+
+
+
+
+
+
+
+
+
+
+
+static int __ethernet_connect_main(sa_eth_s *info)
 {
        if (info == NULL) {
                return -1;
@@ -454,72 +387,34 @@ static int connect_ethernet(sa_eth_s *info)
        return 0;
 }
 
-static int __get_wifi_info(sa_wifi_s *info)
-{
-       int rv = 0;
-       char *ap_name = NULL;
-       wifi_manager_ap_h ap_h;
 
-       rv = wifi_manager_get_connected_ap(wifi_handle, &ap_h);
-       if (rv != WIFI_MANAGER_ERROR_NONE) {
-               printf("\nFail to get connected AP [%s]", __print_wifi_error(rv));
-               return -1;
-       }
 
-       rv = wifi_manager_ap_get_essid(ap_h, &ap_name);
-       if (rv != WIFI_MANAGER_ERROR_NONE) {
-               printf("\nFail to get essid [%s]", __print_wifi_error(rv));
-               wifi_manager_ap_destroy(ap_h);
-               return -1;
-       }
 
-       printf("\nConnected AP : %s\n", ap_name);
-       memcpy(info->ssid, ap_name, MIN(strlen(ap_name), sizeof(info->ssid)-1));
-       free(ap_name);
-       wifi_manager_ap_destroy(ap_h);
 
-       return 0;
-}
 
-static int __get_ethernet_info(sa_eth_s *info) {
-       connection_h connection = NULL;
-       char *ip_addr = NULL;
+
+
+
+
+static int __network_get_wifi_state(connection_h connection, connection_wifi_state_e *wifi_state)
+{
        int rv = 0;
-       int ret = 0;
-
-       if (info != NULL) {
-               // only for ipv4
-               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;
-               }
 
-               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;
-               }
+       if (connection == NULL) {
+               return -1;
+       }
 
-               rv = connection_destroy(connection);
-               if (rv != CONNECTION_ERROR_NONE) {
-                       printf("connection destroy fail [%s]\n", __print_error(rv));
-                       return -1;
-               }
+       rv = connection_get_wifi_state(connection, wifi_state);
+       if (rv != CONNECTION_ERROR_NONE) {
+               _D("Fail to get WiFi state [%s]", __print_error(rv));
+       } else {
+               _D("Retval = [%s] WiFi state [%s]", __print_error(rv), __print_wifi_state(*wifi_state));
        }
 
-       return ret;
+       return 0;
 }
 
-static sa_error_e network_get_state(sa_network_state_e *conn_state, sa_network_type_e *conn_type)
+static sa_error_e __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;
@@ -527,29 +422,25 @@ static sa_error_e network_get_state(sa_network_state_e *conn_state, sa_network_t
        connection_wifi_state_e wifi_state;
        int rv = 0;
 
-       printf("\nnetwork_get_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));
+               _E("create connection handle error [%s]", __print_error(rv));
                return -1;
        }
        
        // check network state(eth/wifi)
        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));
+               _E("Fail to get network state [%s]", __print_error(rv));
        } else {
                ret = SA_ERROR_NONE;
-               printf("\nRetval = [%s] network connection state [%s]\n", __print_error(rv), __print_connection_type(net_state));       
+               _D("Retval = [%s] network connection state [%s]", __print_error(rv), __print_connection_type(net_state));       
                if (CONNECTION_TYPE_DISCONNECTED == net_state) {
                        *conn_state = SA_NETWORK_STATE_DISCONNECTED;
                } else if (CONNECTION_TYPE_WIFI == net_state) {
                        *conn_type = SA_NETWORK_TYPE_WIFI;
                        // check wifi state
-                       if (!__check_wifi_state(connection, wifi_state)) {
+                       if (!__network_get_wifi_state(connection, &wifi_state)) {
                                if (wifi_state == CONNECTION_WIFI_STATE_CONNECTED) {
                                        *conn_state = SA_NETWORK_STATE_CONNECTED;
                                } else {
@@ -567,14 +458,15 @@ static sa_error_e network_get_state(sa_network_state_e *conn_state, sa_network_t
 
        rv = connection_destroy(connection);
        if (rv != CONNECTION_ERROR_NONE) {
-               printf("connection destroy fail [%s]\n", __print_error(rv));
+               _E("connection destroy fail [%s]", __print_error(rv));
                return -1;
        }
 
        return ret;
 }
 
-static sa_error_e network_set_connection(sa_network_s *info)
+
+static sa_error_e __network_connect(sa_network_s *info)
 {
        sa_error_e ret = SA_ERROR_NONE;
        int retWifi = 0;
@@ -586,186 +478,45 @@ static sa_error_e network_set_connection(sa_network_s *info)
        
        if (info->wifi != NULL) {
                if (info->wifi->enabled == TRUE) {
-                       retWifi = connect_wifi(info->wifi);
-                       printf("\nreturn wifi [%d]", retWifi);
+                       retWifi = __wifi_connect_main(info->wifi);
+                       _D("return wifi [%d]", retWifi);
 
                } else {
-                       printf("\nwifi enabled flag is false");
+                       _D("wifi 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);
+                       retEth = __ethernet_connect_main(info->eth);
+                       _D("return ethernet [%d]", retEth);
                }
        }
 
-       printf("\nEnd setting");
-               
        return ret;
 }
 
-static sa_error_e network_get_current_connection(sa_network_s *info)
-{
-       connection_h connection = NULL;
-       sa_error_e ret = SA_ERROR_NONE;
-       connection_type_e net_state;
-
-       int rv = 0;
-
-       // get network 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) {
-                               // 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
-                               info->eth = (sa_eth_s *)malloc(sizeof(sa_eth_s));
-                               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;
-}
-
-static sa_error_e network_deactivate(void)
-{
-       sa_error_e ret = SA_ERROR_NONE;
-
-       return ret;
-}
-
-
-static int compare_network_info(sa_network_s *devNetwork, sa_network_s *newNetwork)
-{
-       int needChange = FALSE;
-
-       if (devNetwork == NULL || newNetwork == NULL) {
-               return -1;
-       }
-
-       // compare wifi
-       // wifi has higher priority to compare. 
-       // If wifi info is changed, it doesn't have to compare ethernet info
-       if (newNetwork->wifi != NULL) {
-               if (newNetwork->wifi->enabled == TRUE) {
-                       // in case of using wifi / check dev wifi
-                       if (devNetwork->wifi != NULL) {
-                               if (newNetwork->wifi->enabled == TRUE) {
-                                       // check ssid/password only
-                                       if (strcmp(devNetwork->wifi->ssid, newNetwork->wifi->ssid)) {
-                                               needChange = TRUE;
-                                       }
-
-                                       if (needChange == FALSE && strcmp(devNetwork->wifi->password, newNetwork->wifi->password)) {
-                                               needChange = TRUE;
-                                       }
-                                       // TBD for checking other members such as, ipaddress, dns, gateway....
-                               }
-                       } else {
-                               needChange = TRUE;
-                       }
-               }
-       }
-
-       
-       if (needChange == FALSE && newNetwork->eth != NULL) {
-               if (newNetwork->eth->enabled == TRUE) {
-                       if (devNetwork->eth != NULL) {
-                               // check ip address only
-                               if (devNetwork->eth->staticInfo != NULL && newNetwork->eth->staticInfo != NULL) {
-                                       if (strcmp(devNetwork->eth->staticInfo->ipAddress, newNetwork->eth->staticInfo->ipAddress)) {
-                                               needChange = TRUE;
-                                       }
-                               }
-                       } else {
-                               needChange = TRUE;
-                       }
-               }
-       }
-       
-       return needChange;      
-}
-
 sa_error_e sa_setup_network(sa_network_s *network)
 {
-       sa_network_s currentNetwork = {0,};
        sa_error_e ret = SA_ERROR_NONE;
        sa_network_type_e conn_type;
        sa_network_state_e conn_state;
 
        if (network == NULL) {
                _E("__set_network is null");
-               return -1;
+               return SA_ERROR_INVALID_PARAMETER;
        }
        // 1. check network state
        // 2. if it is connected, read detail info
-       ret = network_get_state(&conn_state, &conn_type);
-       _D("network_get_state return(%d)", ret);
-       if (ret == SA_ERROR_NONE) {
-               _D("Device Network : T[%s] S[%s]", __print_connection_type(conn_type), __print_connection_state(conn_state));
-               if (conn_state == SA_NETWORK_STATE_CONNECTED) {
-               // read current network info
-                       ret = network_get_current_connection(&currentNetwork);
-                       if (ret == SA_ERROR_NONE) {
-                               // compare network info
-                               if (compare_network_info(&currentNetwork, network)) {
-                                       // set network connection
-                                       _D("Set Network Connection !!");
-                                       ret = network_set_connection(network);
-                               }
-                       } else {
-                               _E("__get_current_network_info return error");
-                       }
-
-                       if (currentNetwork.wifi != NULL) {
-                               if (currentNetwork.wifi->staticInfo != NULL)
-                                       free(currentNetwork.wifi->staticInfo);
-                               
-                               free(currentNetwork.wifi);
-                       }
-
-                       if (currentNetwork.eth != NULL) {
-                               if (currentNetwork.eth->staticInfo != NULL)
-                                       free(currentNetwork.eth->staticInfo);
+       ret = __network_connect(network);
 
-                               free(currentNetwork.eth);
-                       }
-               } else {
-                       // in case of disconnect state
-                       _D("Set Network Connection !!");
-                       ret = network_set_connection(network);
-               }
-       } 
-       
+       // need to delay to sync with setting...
+       sleep(2);
+       ret = __network_get_state(&conn_state, &conn_type);
+       if (ret == SA_ERROR_NONE) {
+               _D("Device Network : T[%s] S[%s]", __print_network_type(conn_type), __print_connection_state(conn_state));
+       }       
 
        return ret;
 }
\ No newline at end of file