[Bugfix] change auth and passwd for wep, fix bug in callback thread. [299][282][281]

Change-Id: Icb39426d19a454f9c90f21ad313697815e1211dd
diff --git a/lib/liblynq-wifi6/libwifi6.c b/lib/liblynq-wifi6/libwifi6.c
index d0e8750..3672fc7 100755
--- a/lib/liblynq-wifi6/libwifi6.c
+++ b/lib/liblynq-wifi6/libwifi6.c
@@ -53,6 +53,8 @@
 //const char * CTRL_PATH[2] = {"/var/run/wpa_supplicant/wlan0", "/var/run/wpa_supplicant/wlan0"};
 const char * cmd_list_networks = "LIST_NETWORKS";
 const char * cmd_save_config = "SAVE_CONFIG";
+const char * cmd_disconnect = "DISCONNECT";
+const char * cmd_remove_all = "REMOVE_NETWORK all";
 const char * state_scan_result = "CTRL-EVENT-SCAN-RESULTS";
 
 static struct wpa_ctrl * g_lynq_wpa_ctrl[2] = {0};
@@ -123,17 +125,25 @@
     size_t len = MAX_RET;
     char msg_notify[MAX_RET];
 
-    struct wpa_ctrl *lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[CTRL_AP]); //@todo temp change
-    if (lynq_wpa_ctrl == NULL)
-        return;
-
-    wpa_ctrl_attach(lynq_wpa_ctrl);
+    struct wpa_ctrl *lynq_wpa_ctrl = NULL;
 
     while (g_ap_watcher_stop_flag == 0) {
+        if (lynq_wpa_ctrl == NULL) {
+            lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[CTRL_AP]); //@todo temp change
+            if (lynq_wpa_ctrl == NULL) {
+                usleep(100*1000);
+                continue;
+            }
+
+            wpa_ctrl_attach(lynq_wpa_ctrl);
+        }
+
         if ( 0 == wpa_ctrl_pending(lynq_wpa_ctrl)) {
             usleep(100*1000);
             continue;
         }
+        memset(msg_notify, 0, MAX_RET);
+        len = MAX_RET;
         if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len)) {
             msg_notify[len+1] = '\0';
             printf("ap------> %s\n", msg_notify);
@@ -158,17 +168,25 @@
     char *pReason;
     error_number_s error;
 
-    struct wpa_ctrl *lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[CTRL_STA]);
-    if (lynq_wpa_ctrl == NULL)
-        return;
-
-    wpa_ctrl_attach(lynq_wpa_ctrl);
+    struct wpa_ctrl *lynq_wpa_ctrl = NULL;
 
     while (g_sta_watcher_stop_flag == 0) {
+        if (lynq_wpa_ctrl == NULL) {
+            lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[CTRL_STA]);
+            if (lynq_wpa_ctrl == NULL) {
+                usleep(100*1000);
+                continue;
+            }
+
+            wpa_ctrl_attach(lynq_wpa_ctrl);
+        }
+
         if ( 0 == wpa_ctrl_pending(lynq_wpa_ctrl)) {
             usleep(100*1000);
             continue;
         }
+        memset(msg_notify, 0, MAX_RET);
+        len = MAX_RET;
         if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len)) {
             msg_notify[len+1] = '\0';
             printf("sta ------> %s\n", msg_notify);
@@ -214,6 +232,7 @@
 int lynq_wifi_enable(void)
 {
     int ret = 0;
+    int i;
     const char * cmd_check_service =
             "state=`systemctl is-active wg870_drv_insmod.service`\n"
             "[ \"\"$state == \"active\" ] && exit 0\n"
@@ -224,9 +243,28 @@
 
     ret = system(cmd_check_service);
     if (ret != 0) {
+        printf("service state %d\n", ret);
         return -1;
     }
 
+    for (i=0; i<10; i++) {
+        if (system("connmanctl technologies | grep \"/net/connman/technology/wifi\"") == 0) {
+            break;
+        }
+        usleep(300*1000);
+    }
+
+    if (i >= 10) {
+        return -1;
+    }
+
+    if (0 != system("ifconfig | grep ap0")) {
+        printf("enable wifi\n");
+        system("connmanctl enable wifi");
+        system("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 DRIVER interface_create ap0");
+        system("connmanctl tether wifi on lynq 1qaz@WSX#$%^");
+    }
+
     if (g_ap_watcher_pid == 0 ) {
         ret=pthread_create(&g_ap_watcher_pid,NULL,APWatcherThreadProc,NULL);
         if(ret<0){
@@ -461,7 +499,7 @@
 }
 
 static int lynq_add_network(int ap_sta) {
-    int i=0;
+    size_t i=0;
     CHECK_WPA_CTRL(ap_sta);
     const char *lynq_wifi_add_network = "ADD_NETWORK";
 
@@ -470,7 +508,7 @@
         return -1;
     }
 
-    for(int i=0;i<reply_len;i++) {
+    for(i=0;i<reply_len;i++) {
         if(cmd_reply[i] == '\n') {
             cmd_reply[i] = '\0';
             break;
@@ -685,8 +723,19 @@
 
 int lynq_wifi_ap_ssid_get(lynq_wifi_index_e idx, char* ap_ssid)
 {
+    int len;
     CHECK_IDX(idx, CTRL_AP);
-    return inner_get_param(CTRL_AP, AP_NETWORK_0, "ssid", ap_ssid);
+    if (0 != inner_get_param(CTRL_AP, AP_NETWORK_0, "ssid", ap_ssid))
+        return -1;
+    len = strlen(ap_ssid);
+    if (ap_ssid[0] == '\"') {
+        memmove(ap_ssid, ap_ssid + 1, len - 1);
+        len -= 1;
+    }
+    if (len > 0 && ap_ssid[len-1] == '\"') {
+        ap_ssid[len-1] = '\0';
+    }
+    return 0;
 }
 
 int lynq_wifi_ap_frequency_set(lynq_wifi_index_e idx,int lynq_wifi_frequency)
@@ -711,10 +760,10 @@
     sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
     sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
 
+    DO_OK_FAIL_REQUEST(cmd_disconnect);
     DO_OK_FAIL_REQUEST(lynq_wifi_frequency_cmd);
     DO_OK_FAIL_REQUEST(lynq_cmd_mode);
     DO_OK_FAIL_REQUEST(cmd_save_config);
-    DO_OK_FAIL_REQUEST(lynq_cmd_slect);
 
 	return 0;
 }
@@ -871,38 +920,68 @@
 
 int lynq_wifi_ap_auth_set(lynq_wifi_index_e idx, lynq_wifi_auth_s auth)
 {
+    char ssid[MAX_CMD] = {0};
+    int freq = 0;
+    char lynq_auth_cmd[64]={0};
+    char lynq_auth_alg_cmd[64]={0};
+    char lynq_psk_cmd[64]={0};
+    char lynq_pairwise_cmd[64]={0};
+    lynq_wifi_auth_s org_auth;
     CHECK_IDX(idx, CTRL_AP);
 
+    CHECK_WPA_CTRL(CTRL_AP);
+
     if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != AP_NETWORK_0) {
         return -1;
     }
 
-    CHECK_WPA_CTRL(CTRL_AP);
+    if (0 == lynq_wifi_ap_auth_get(idx, &org_auth)) {
+        if (org_auth == auth) {
+            return 0;
+        }
+        else {
+            if (0 != lynq_wifi_ap_ssid_get(idx, ssid)) {
+                ssid[0] = '\0';
+            }
+            lynq_wifi_ap_frequency_get(idx, &freq);
+
+            DO_OK_FAIL_REQUEST(cmd_disconnect);
+            DO_OK_FAIL_REQUEST(cmd_remove_all);
+            if (ssid[0] != '\0') {
+                lynq_wifi_ap_ssid_set(idx, ssid);
+            }
+            if (freq != 0) {
+                lynq_wifi_ap_frequency_set(idx, freq);
+            }
+        }
+    }
 
 	switch(auth){
 		case LYNQ_WIFI_AUTH_OPEN:
-		{
-            char lynq_auth_cmd[64]={0};
+        {
             sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
 
             DO_OK_FAIL_REQUEST(lynq_auth_cmd);
-            DO_OK_FAIL_REQUEST(cmd_save_config);
 			break;
 		}
+        case LYNQ_WIFI_AUTH_WEP:
+        {
+            sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
+            sprintf(lynq_auth_alg_cmd,"SET_NETWORK %d auth_alg  SHARED", AP_NETWORK_0);
+
+            DO_OK_FAIL_REQUEST(lynq_auth_cmd);
+            DO_OK_FAIL_REQUEST(lynq_auth_alg_cmd);
+            break;
+        }
 		case LYNQ_WIFI_AUTH_WPA_PSK:
         case LYNQ_WIFI_AUTH_WPA2_PSK:
 		{
-
-			char lynq_auth_cmd[64]={0};
-			char lynq_psk_cmd[64]={0};
-            char lynq_pairwise_cmd[64]={0};
-
             if (auth == LYNQ_WIFI_AUTH_WPA_PSK) {
                 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
                 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
             }
             else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK) {
-                sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
+                sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", AP_NETWORK_0);
                 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
             }
 //            sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
@@ -912,7 +991,6 @@
             DO_OK_FAIL_REQUEST(lynq_auth_cmd);
             DO_OK_FAIL_REQUEST(lynq_psk_cmd);
             DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
-            DO_OK_FAIL_REQUEST(cmd_save_config);
 			break;
 		}		
 		default:
@@ -921,6 +999,7 @@
 			return -1;
         }
     }
+    DO_OK_FAIL_REQUEST(cmd_save_config);
 
 	return 0;
 }
@@ -928,6 +1007,8 @@
 int lynq_wifi_ap_auth_get(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
 {
     char lynq_auth_str[MAX_RET] = {0};
+    char lynq_auth_alg_str[MAX_RET] = {0};
+    char lynq_proto_str[MAX_RET] = {0};
 
     CHECK_IDX(idx, CTRL_AP);
 
@@ -936,13 +1017,29 @@
     }
 
     if (memcmp( lynq_auth_str, "NONE", 4) == 0) {
-        *auth = LYNQ_WIFI_AUTH_OPEN;
+        if (inner_get_param(CTRL_AP, AP_NETWORK_0, "auth_alg", lynq_auth_alg_str) != 0) {
+            *auth = LYNQ_WIFI_AUTH_OPEN;
+        }
+        else if (memcmp(lynq_auth_alg_str, "SHARED", 6) == 0){
+            *auth = LYNQ_WIFI_AUTH_WEP;
+        }
+        else {
+            *auth = LYNQ_WIFI_AUTH_OPEN;
+        }
     }
     else {
-        *auth = LYNQ_WIFI_AUTH_WPA_PSK;
+        if (inner_get_param(CTRL_AP, AP_NETWORK_0, "key_mgmt", lynq_proto_str) != 0) {
+            *auth = LYNQ_WIFI_AUTH_WPA_PSK;
+        }
+        else if (memcmp(lynq_proto_str, "RSN", 3) == 0) {
+            *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
+        }
+        else {
+            *auth = LYNQ_WIFI_AUTH_WPA_PSK;
+        }
     }
 
-	return 0;
+    return 0;
 }
 
 
@@ -1037,19 +1134,39 @@
 int lynq_ap_password_set(lynq_wifi_index_e idx,char *password)
 {
 	int pass_len;
-    char lynq_tmp_cmd[300]={0};
+    char lynq_tmp_cmd[MAX_CMD] = {0};
+    char lynq_wep_tx_keyidx_cmd[MAX_CMD] = {0};
 	pass_len=strlen(password);
+    lynq_wifi_auth_s auth = -1;
     if(pass_len < 8 || pass_len >= 64){
 		return -1;
     }
 
     CHECK_IDX(idx, CTRL_AP);
 
+    if (0 != lynq_wifi_ap_auth_get(idx, &auth)) {
+        return -1;
+    }
+    else if (auth == LYNQ_WIFI_AUTH_OPEN) {
+        return -1;
+    }
+
     CHECK_WPA_CTRL(CTRL_AP);
 
-    sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
+    if (auth == LYNQ_WIFI_AUTH_WEP) {
+        sprintf(lynq_tmp_cmd,"SET_NETWORK %d wep_key0 \"%s\"",AP_NETWORK_0, password);
+        sprintf(lynq_wep_tx_keyidx_cmd,"SET_NETWORK %d wep_tx_keyidx 0",AP_NETWORK_0);
+        DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
+        DO_OK_FAIL_REQUEST(lynq_wep_tx_keyidx_cmd);
+    }
+    else if (auth == LYNQ_WIFI_AUTH_WPA_PSK || auth == LYNQ_WIFI_AUTH_WPA2_PSK) {
+        sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
+        DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
+    }
+    else {
+        return -1;
+    }
 
-    DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
     DO_OK_FAIL_REQUEST(cmd_save_config);
 
 	return 0;
@@ -1097,12 +1214,20 @@
     ret = -1;
     for(index=0; index < count; index++) {
         p = strstr(split_lines[index], "psk=");
-        if (p == NULL) {
-            continue;
+        if (p != NULL) {
+            p += 4;
+            if (*p == '\"') {
+                p++;
+            }
         }
-        p += 4;
-        if (*p == '\"') {
-            p++;
+        else if (NULL != (p = strstr(split_lines[index], "wep_key0="))) {
+            p += 9;
+            if (*p == '\"') {
+                p++;
+            }
+        }
+        else {
+            continue;
         }
 
         strcpy(password, p);
@@ -1230,12 +1355,20 @@
     ret = -1;
     for(index=0; index < count; index++) {
         p = strstr(split_lines[index], "psk=");
-        if (p == NULL) {
-            continue;
+        if (p != NULL) {
+            p += 4;
+            if (*p == '\"') {
+                p++;
+            }
         }
-        p += 4;
-        if (*p == '\"') {
-            p++;
+        else if (NULL != (p = strstr(split_lines[index], "wep_key0="))) {
+            p += 9;
+            if (*p == '\"') {
+                p++;
+            }
+        }
+        else {
+            continue;
         }
 
         strcpy(password, p);
@@ -1286,7 +1419,8 @@
     }
 
     if (start_flag == 0) {
-        sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", net_no);
+//        sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", net_no);
+        sprintf(lynq_disable_cmd,"DISCONNECT");
         DO_OK_FAIL_REQUEST(lynq_disable_cmd);
     }
     else {
@@ -1301,7 +1435,17 @@
 {
     CHECK_IDX(idx, CTRL_STA);
 
-	return inner_get_param(CTRL_STA, idx, "ssid", sta_ssid);
+    curr_status_info curr_state;
+    ap_info_s ap_info;
+    curr_state.ap = &ap_info;
+    curr_state.state = NULL;
+
+    if (0 == inner_get_status_info(CTRL_STA, &curr_state)) {
+        strcpy(sta_ssid, ap_info.ap_ssid);
+        return 0;
+    }
+
+    return -1;
 }
 
 int lynq_wifi_get_sta_available_ap(lynq_wifi_index_e idx, ap_detail_info_s *info)
@@ -2014,7 +2158,7 @@
    char host[NI_MAXHOST];
    const char * ifaName = "wlan0";
    if (idx == 1) {
-       ifaName = "ap0";
+       ifaName = "tether";
    }
    else if (idx != 0) {
        return -1;