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

Change-Id: Icb39426d19a454f9c90f21ad313697815e1211dd
diff --git a/src/lynq/lib/liblynq-wifi6/libwifi6.c b/src/lynq/lib/liblynq-wifi6/libwifi6.c
index 682fa6d..3672fc7 100755
--- a/src/lynq/lib/liblynq-wifi6/libwifi6.c
+++ b/src/lynq/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};
@@ -99,7 +101,7 @@
         printf("to call [%s]\n", cmd_str);  \
         ret = wpa_ctrl_request(lynq_wpa_ctrl, cmd_str, strlen(cmd_str), cmd_reply, &reply_len, NULL); \
         if (ret != 0) { \
-            printf("call ##cmd_str fail %d\n", ret); \
+            printf("call "#cmd_str" fail %d\n", ret); \
             return ret; \
         } \
         cmd_reply[reply_len+1] = '\0'; \
@@ -109,10 +111,10 @@
 #define DO_OK_FAIL_REQUEST(cmd_str) do { \
         DO_REQUEST(cmd_str); \
         if (reply_len >=4 && memcmp(cmd_reply, "FAIL", 4) == 0 ) {\
-            printf("cmd ##cmd_str return FAIL\n"); \
+            printf("cmd "#cmd_str" return FAIL\n"); \
             return -1; \
         } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0) { \
-            printf("cmd ##cmd_str return not OK|FAIL\n"); \
+            printf("cmd "#cmd_str" return not OK|FAIL\n"); \
             return -1; \
         } \
     }while (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,16 +499,16 @@
 }
 
 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";
 
     DO_REQUEST(lynq_wifi_add_network);
-    if (memcpy(cmd_reply, "FAIL", 4) == 0) {
+    if (memcmp(cmd_reply, "FAIL", 4) == 0) {
         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;
@@ -478,6 +516,7 @@
     }
     return atoi(cmd_reply);
 }
+
 static int lynq_check_network_number(lynq_wifi_index_e idx, int ap_sta, int net_no)
 {
     int count, index;
@@ -501,6 +540,10 @@
         if (index >= net_no) { // required network no created
             return 0;
         }
+        else if( index < 0) {
+            printf("add network fail\n");
+            return -1;
+        }
     }
 
     if (index < 0)
@@ -638,7 +681,7 @@
             }
 
         } //end else if (state != NULL)
-        if ((p = strstr(split_lines[i], FLAG_ID)) != NULL) {
+        if ((p = strstr(split_lines[i], FLAG_ID)) == split_lines[i]) {
             ret = 0;
             curr_state->net_no = atoi(p);
             printf("net_no %d, -- %s\n", curr_state->net_no, p);
@@ -680,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)
@@ -706,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;
 }
@@ -866,39 +920,69 @@
 
 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_psk_cmd,"SET_NETWORK %d key_mgmt WPA2-PSK", 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);
 //            sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
@@ -907,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:
@@ -916,6 +999,7 @@
 			return -1;
         }
     }
+    DO_OK_FAIL_REQUEST(cmd_save_config);
 
 	return 0;
 }
@@ -923,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);
 
@@ -931,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;
 }
 
 
@@ -1032,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;
@@ -1092,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);
@@ -1118,12 +1248,21 @@
 
 static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth) {
     char lynq_auth_str[MAX_RET] = {0};
+    char lynq_proto_str[MAX_RET] = {0};
 
     if (inner_get_param(interface, net_no, "key_mgmt", lynq_auth_str) != 0) {
         return -1;
     }
 
     *auth = convert_auth_from_key_mgmt(lynq_auth_str);
+
+    if (*auth == LYNQ_WIFI_AUTH_WPA_PSK) {
+        if (inner_get_param(interface, net_no, "proto", lynq_proto_str) == 0) {
+            if (strcmp(lynq_proto_str, "RSN") == 0) {
+                *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
+            }
+        }
+    }
     return 0;
 }
 
@@ -1216,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);
@@ -1272,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 {
@@ -1287,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)
@@ -1378,7 +1536,7 @@
                 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", net_no);
             }
             else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK) {
-                sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", net_no);
+                sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", net_no);
             }
             sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", net_no);
             sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
@@ -1504,21 +1662,47 @@
     return inner_sta_start_stop(curr_state.net_no, 0, 0);
 }
 
-int lynq_wifi_sta_start(lynq_wifi_index_e idx) {
+int lynq_wifi_sta_start(lynq_wifi_index_e idx)
+{
+//    const char *lynq_reconfigure_cmd = "RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
 
     CHECK_IDX(idx, CTRL_STA);
+    CHECK_WPA_CTRL(CTRL_STA);
+
+    system("connmanctl enable wifi");
+
     if (system("ifconfig | grep wlan0") != 0) {
         return -1;
     }
 
-    return system("connmanctl enable wifi");
+//    DO_OK_FAIL_REQUEST(lynq_reconfigure_cmd);
+
+    return 0;
 }
 
 int lynq_wifi_sta_stop(lynq_wifi_index_e idx)
 {
-    CHECK_IDX(idx, CTRL_STA);
+    char lynq_disable_network_cmd[MAX_CMD];
+    curr_status_info curr_state;
+    ap_info_s ap_info;
 
-    return system("connmanctl disable wifi");
+    CHECK_IDX(idx, CTRL_STA);
+    CHECK_WPA_CTRL(CTRL_STA);
+
+    curr_state.ap = &ap_info;
+    curr_state.state = NULL;
+
+    if (0 != inner_get_status_info(CTRL_STA, &curr_state) || curr_state.net_no < 0) {
+        return 0;
+    }
+
+    sprintf(lynq_disable_network_cmd, "DISABLE_NETWORK %d", curr_state.net_no);
+    DO_OK_FAIL_REQUEST(lynq_disable_network_cmd);
+
+    DO_OK_FAIL_REQUEST(cmd_save_config);
+
+    return 0;
+//    return system("connmanctl disable wifi");
 }
 
 //static int inner_get_sta_info(lynq_wifi_index_e idx, const char * bssid, device_info_s *dev) {
@@ -1974,7 +2158,7 @@
    char host[NI_MAXHOST];
    const char * ifaName = "wlan0";
    if (idx == 1) {
-       ifaName = "ap0";
+       ifaName = "tether";
    }
    else if (idx != 0) {
        return -1;