[rdkb][common][app][Add sta mode support in test tool]

[Description]
Add sta mode support in test tool.
And fix mac address get from phy index not radio index.

[Release-log]
N/A

Change-Id: I92efb7049195d47b12afefcef5510d1f8967a93b
diff --git a/recipes-wifi/wifi-test-tool/files/src/wifi-test-tool.c b/recipes-wifi/wifi-test-tool/files/src/wifi-test-tool.c
index 9ac42a2..a489bb5 100644
--- a/recipes-wifi/wifi-test-tool/files/src/wifi-test-tool.c
+++ b/recipes-wifi/wifi-test-tool/files/src/wifi-test-tool.c
@@ -5,6 +5,17 @@
 #include <uci.h>
 #include "wifi-test-tool.h"
 
+static int mac_addr_aton(unsigned char *mac_addr, char *arg)
+{
+    sscanf(arg, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", &mac_addr[0], &mac_addr[1], &mac_addr[2], &mac_addr[3], &mac_addr[4], &mac_addr[5]);
+    return 0;
+}
+
+static void mac_addr_ntoa(char *mac_addr, unsigned char *arg)
+{
+    snprintf(mac_addr, 20, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", arg[0], arg[1],arg[2],arg[3],arg[4],arg[5]);
+    return;
+}
 
 static int _syscmd(char *cmd, char *retBuf, int retBufSize)
 {
@@ -145,17 +156,17 @@
         radio_param->disabled = FALSE;
 }
 
-void set_radionum(wifi_ap_param *ap_param, char *radio_name)
+void set_radionum(wifi_ap_param *ap_param, char *phy_name)
 {
-    int radio_num;
-    char *ptr = radio_name;
+    int radio_num = 0;
+    char *ptr = phy_name;
     int phyId = 0;
 
     while (*ptr) {
         if (isdigit(*ptr)) {
-            radio_num = strtoul(ptr, NULL, 10);
-            phyId = phy_index_to_radio(radio_num);
-            ap_param->radio_index = phyId;
+            phyId = strtoul(ptr, NULL, 10);
+            radio_num = phy_index_to_radio(phyId);
+            ap_param->radio_index = radio_num;
             break;
         }
         ptr++;
@@ -221,31 +232,27 @@
     strncpy(ap_param->security.u.key.key, key, 64);
 }
 
-int set_ap_bssid(int radio_index, int offset, mac_address_t *bssid)
+int set_interface_bssid(int phy_index, int offset, mac_address_t *bssid)
 {
     FILE *f;
     char mac_file[64] = {0};
     char mac_address[20] = {0};
-    char *tmp = NULL;
 
-    sprintf(mac_file, "/sys/class/net/wlan%d/address", radio_index);
+    sprintf(mac_file, "/sys/class/net/wlan%d/address", phy_index);
     f = fopen(mac_file, "r");
     if (f == NULL)
         return -1;
     fgets(mac_address, 20, f);
     fclose(f);
 
-    sscanf(mac_address, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &(*bssid)[0], &(*bssid)[1], &(*bssid)[2], &(*bssid)[3], &(*bssid)[4], &(*bssid)[5]);
-    (*bssid)[0] += (offset + 1)*2;
+    mac_addr_aton(&(*bssid)[0], mac_address);
+    (*bssid)[0] += offset*2;
     return 0;
 }
 
 void set_radio_param(wifi_radio_param radio_parameter)
 {
-    BOOL enable;
-    BOOL current;
     int ret = 0;
-    struct params param;
     wifi_radio_operationParam_t operationParam = {0};
 
     if(radio_parameter.radio_index == -1)
@@ -341,6 +348,7 @@
 {
     int ret = 0;
     int vap_index_in_map = 0;
+    int phy_index = 0;
     wifi_vap_info_t vap_info = {0};
     BOOL radio_enable = FALSE;
 
@@ -365,9 +373,10 @@
 
     vap_info = map->vap_array[vap_index_in_map];
     vap_info.u.bss_info.enabled = TRUE;
-    if (set_ap_bssid(vap_info.radio_index, vap_index_in_map, &vap_info.u.bss_info.bssid) == -1) {
+    phy_index = radio_index_to_phy(vap_info.radio_index);
+    if (set_interface_bssid(phy_index, ap_param.mac_offset, &vap_info.u.bss_info.bssid) == -1) {
         fprintf(stderr, "Get mac address failed.\n");
-        return -1;
+        return;
     }
 
     // SSID
@@ -385,6 +394,60 @@
     map->vap_array[vap_index_in_map] = vap_info;
 }
 
+void set_sta_param(wifi_ap_param sta_param)
+{
+    wifi_sta_network_t *sta = NULL;
+    mac_address_t sta_mac = {0};
+    char sta_mac_str[20] = {0};
+    char key_mgmt[16] = {0};
+    char pairwise[16] = {0};
+    int phy_index = 0;
+
+    sta = calloc(1, sizeof(wifi_sta_network_t));
+
+    phy_index = radio_index_to_phy(sta_param.radio_index);
+    set_interface_bssid(phy_index, sta_param.mac_offset, &sta_mac);
+    mac_addr_ntoa(sta_mac_str, sta_mac);
+    snprintf(sta->ssid, 31, "%s", sta_param.ssid);
+    sta->ssid[31] = '\0';
+    snprintf(sta->psk, 64, "%s", sta_param.password);
+
+    if (sta_param.security.mode == wifi_security_mode_none)
+        strcpy(key_mgmt, "NONE");
+    else if (sta_param.security.mode == wifi_security_mode_wpa3_personal)
+        strcpy(key_mgmt, "SAE");
+    else
+        strcpy(key_mgmt, "WPA-PSK");
+    snprintf(sta->key_mgmt, 64, "%s", key_mgmt);
+
+    if (sta_param.security.encr == wifi_encryption_aes)
+        strcpy(pairwise, "CCMP");
+    else if (sta_param.security.encr == wifi_encryption_tkip)
+        strcpy(pairwise, "TKIP");
+    else
+        strcpy(pairwise, "CCMP TKIP");
+    snprintf(sta->pairwise, 64, "%s", pairwise);
+
+    if (strlen(sta_param.security.u.key.key) > 0)
+        strncpy(sta->psk, sta_param.security.u.key.key, 127);
+    sta->psk[127] = '\0';
+    sta->psk_len = strlen(sta->psk);
+
+    wifi_createSTAInterface(sta_param.sta_index, sta_mac_str);
+
+    if (wifi_setSTANetworks(sta_param.sta_index, &sta, 1, FALSE) == RETURN_ERR) {
+        fprintf(stderr, "Write to sta %d config file failed\n", sta_param.sta_index);
+        free(sta);
+        return;
+    }
+    free(sta);
+
+    if (wifi_setSTAEnabled(sta_param.sta_index, TRUE) == RETURN_ERR) {
+        fprintf(stderr, "Enable station failed\n");
+        return;
+    }
+}
+
 int apply_uci_config ()
 {
     struct uci_context *uci_ctx = uci_alloc_context();
@@ -395,6 +458,7 @@
     int max_radio_num = 0;
     BOOL parsing_radio = FALSE;
     int apCount[3] = {0};
+    int staCount[3] = {0};
     wifi_vap_info_map_t vap_map[3] = {0};
     int ret = 0;
     int i = 0;
@@ -458,11 +522,20 @@
                 // parsing iface
                 if (strcmp(op->e.name, "device") == 0){
                     set_radionum(&ap_param, op->v.string);
-                    if (ap_param.radio_index != -1){
+                }else if (strcmp(op->e.name, "mode") == 0){
+                    ap_param.mac_offset = staCount[ap_param.radio_index] + apCount[ap_param.radio_index];
+                    if (strncmp(op->v.string, "sta", 3) == 0) {
+                        ap_param.sta_mode = TRUE;
+                        ap_param.sta_index = ap_param.radio_index + staCount[ap_param.radio_index]*max_radio_num;
+                        staCount[ap_param.radio_index] ++ ;
+                        fprintf(stderr, "\n----- Start parsing sta %d config. -----\n", ap_param.sta_index);
+                    } else if (strncmp(op->v.string, "ap", 2) == 0) {
+                        ap_param.sta_mode = FALSE;
                         ap_param.ap_index = ap_param.radio_index + apCount[ap_param.radio_index]*max_radio_num;
-                        fprintf(stderr, "\n----- Start parsing ap %d config. -----\n", ap_param.ap_index);
                         apCount[ap_param.radio_index] ++ ;
-                    }   
+                        fprintf(stderr, "\n----- Start parsing ap %d config. -----\n", ap_param.ap_index);
+                    }
+                    ap_param.mac_offset = staCount[ap_param.radio_index] + apCount[ap_param.radio_index];
                 }else if (strcmp(op->e.name, "ssid") == 0){
                     set_ssid(&ap_param, op->v.string);
                 }else if (strcmp(op->e.name, "encryption") == 0){
@@ -476,9 +549,12 @@
         }
         if (parsing_radio == TRUE)
             set_radio_param(radio_param);
+        else if (ap_param.sta_mode == TRUE)
+            set_sta_param(ap_param);
         else
             set_ap_param(ap_param, &vap_map[ap_param.radio_index]);
     }
+    fprintf(stderr, "\n----- Start setting Vaps. -----\n");
 
     for (i = 0; i < max_radio_num ;i++ ){
         ret = wifi_createVAP(i, &vap_map[i]);
diff --git a/recipes-wifi/wifi-test-tool/files/src/wifi-test-tool.h b/recipes-wifi/wifi-test-tool/files/src/wifi-test-tool.h
index 19af44a..ef50315 100644
--- a/recipes-wifi/wifi-test-tool/files/src/wifi-test-tool.h
+++ b/recipes-wifi/wifi-test-tool/files/src/wifi-test-tool.h
@@ -46,12 +46,15 @@
 
 typedef struct {
     wifi_radio_param *radio_info;   // for multiple vap
+    bool sta_mode;
     int radio_index;
     int ap_index;
+    int sta_index;
     char ssid[33];
     int wpa;
     char password[64];
     wifi_vap_security_t security;
+    int mac_offset;
 } wifi_ap_param;
 
 struct params