[rdk-b][mt7986][wifi-hal][Add WiFi-test-tool]

[Description]
Add WiFi-test-tool for MSP, SQC test.
The tool now only support dual band.

[Release-log]
N/A

Change-Id: I6d3d4bf65dd9ae03ac70491840eebbbde0ac2ffc
diff --git a/recipes-devtools/wifi-test-tool/files/src/Makefile.am b/recipes-devtools/wifi-test-tool/files/src/Makefile.am
new file mode 100644
index 0000000..59dee99
--- /dev/null
+++ b/recipes-devtools/wifi-test-tool/files/src/Makefile.am
@@ -0,0 +1,13 @@
+AM_CFLAGS = -D_ANSC_LINUX
+AM_CFLAGS += -D_ANSC_USER
+AM_CFLAGS += -D_ANSC_LITTLE_ENDIAN_
+
+AM_CPPFLAGS = -Wall -Werror
+ACLOCAL_AMFLAGS = -I m4
+hardware_platform = armv7ahf-neon-rdk-linux-musleabi
+
+bin_PROGRAMS = wifi_test_tool
+wifi_test_tool_CPPFLAGS = -I$(top_srcdir)/include $(CPPFLAGS) -D_WIFI_HAL_TEST_
+wifi_test_tool_SOURCES = wifi-test-tool.c
+wifi_test_tool_LDFLAGS = -lhal_wifi
+
diff --git a/recipes-devtools/wifi-test-tool/files/src/configure.ac b/recipes-devtools/wifi-test-tool/files/src/configure.ac
new file mode 100644
index 0000000..8783d84
--- /dev/null
+++ b/recipes-devtools/wifi-test-tool/files/src/configure.ac
@@ -0,0 +1,33 @@
+AC_PREREQ([2.65])
+AC_INIT([hal], [1.0], [BUG-REPORT-ADDRESS])
+AM_INIT_AUTOMAKE([foreign])
+LT_INIT
+
+AC_PREFIX_DEFAULT(`pwd`)
+AC_ENABLE_SHARED
+AC_DISABLE_STATIC
+
+AC_CONFIG_HEADERS([config.h])
+AC_CONFIG_MACRO_DIR([m4])
+
+# Checks for programs.
+AC_PROG_CC
+AC_PROG_INSTALL
+AM_PROG_CC_C_O
+AM_PROG_LIBTOOL(libtool)
+
+# Checks for header files.
+AC_CHECK_HEADERS([stdlib.h string.h unistd.h])
+
+# Checks for typedefs, structures, and compiler characteristics.
+AC_HEADER_STDBOOL
+AC_C_INLINE
+
+# Checks for library functions.
+AC_FUNC_MALLOC
+
+AC_CONFIG_FILES(Makefile)
+
+
+AC_OUTPUT
+
diff --git a/recipes-devtools/wifi-test-tool/files/src/wifi-test-tool.c b/recipes-devtools/wifi-test-tool/files/src/wifi-test-tool.c
new file mode 100644
index 0000000..94fde80
--- /dev/null
+++ b/recipes-devtools/wifi-test-tool/files/src/wifi-test-tool.c
@@ -0,0 +1,336 @@
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <ctype.h>
+#include <uci.h>
+#include "wifi-test-tool.h"
+
+void set_channel(wifi_radio_param *radio_param, char *channel)
+{
+    if (strcmp(channel, "auto") != 0) {
+        radio_param->auto_channel = FALSE;
+        radio_param->channel = atoi(channel);
+    }
+}
+
+void set_band(wifi_radio_param *radio_param, char *country)
+{
+    strcpy(radio_param->country, country);
+}
+
+void set_country(wifi_radio_param *radio_param, char *band)
+{
+    strcpy(radio_param->band, band);
+}
+
+void set_hwmode(wifi_radio_param *radio_param, char *hwmode)
+{
+    if (strncmp(hwmode, "11a", 3) == 0)
+        strcpy(radio_param->hwmode, "a");
+    if (strncmp(hwmode, "11b", 3) == 0)
+        strcpy(radio_param->hwmode, "b");
+    if (strncmp(hwmode, "11g", 3) == 0)
+        strcpy(radio_param->hwmode, "g");
+}
+
+void set_htmode(wifi_radio_param *radio_param, char *htmode)
+{
+    char tmp[16] = {0};
+    char *ptr = htmode;
+    ULONG bandwidth = 0;
+    radio_param->bandwidth = 20;
+    while (*ptr) {
+        if (isdigit(*ptr)) {
+            bandwidth = strtoul(ptr, NULL, 10);
+            radio_param->bandwidth = bandwidth;
+            break;
+        }
+        ptr++;
+    }
+
+    // HT40     -> 11NGHT40PLUS
+    // VHT40+   -> 11ACVHT40PLUS
+    // HE80     -> 11AXHE80
+    if (strstr(htmode, "+") != NULL) {
+        strncpy(tmp, htmode, strlen(htmode) - 1);
+        strcat(tmp, "PLUS");
+    } else if (strstr(htmode, "-") != NULL) {
+        strncpy(tmp, htmode, strlen(htmode) - 1);
+        strcat(tmp, "MINUS");
+    } else 
+        strcpy(tmp, htmode);
+
+
+    if (strstr(htmode, "VHT") != NULL) {
+        snprintf(radio_param->htmode, sizeof(radio_param->htmode), "11AC%s", tmp);
+    } else if (strstr(htmode, "HT") != NULL && strstr(htmode, "NO") == NULL) {
+        snprintf(radio_param->htmode, sizeof(radio_param->htmode), "11NG%s", tmp);
+    } else if (strstr(htmode, "HE") != NULL) {
+        snprintf(radio_param->htmode, sizeof(radio_param->htmode), "11AX%s", tmp);
+    } else {        // NOHT or NONE should be parsed with the band, so just fill the original string.
+        strcpy(radio_param->htmode, tmp);
+    }
+
+}
+
+void set_disable(wifi_radio_param *radio_param, char *disable)
+{
+    if (strcmp(disable, "1") == 0)
+        radio_param->disabled = TRUE;
+    else
+        radio_param->disabled = FALSE;
+}
+
+void set_radionum(wifi_ap_param *ap_param, char *radio_name)
+{
+    int radio_num;
+    char *ptr = radio_name;
+
+    while (*ptr) {
+        if (isdigit(*ptr)) {
+            radio_num = strtoul(ptr, NULL, 10);
+            ap_param->radio_index = radio_num;
+            break;
+        }
+        ptr++;
+    }
+}
+
+void set_ssid(wifi_ap_param *ap_param, char *ssid)
+{
+    strncpy(ap_param->ssid, ssid, 32);
+}
+
+void set_encryption(wifi_ap_param *ap_param, char *encryption_mode)
+{
+    if (strstr(encryption_mode, "3") != NULL) {
+        strcpy(ap_param->enctyption_mode, "WPA3-");
+    } else if (strstr(encryption_mode, "2") != NULL) {
+        strcpy(ap_param->enctyption_mode, "WPA2-");
+    } else if (strstr(encryption_mode, "1") != NULL) {
+        strcpy(ap_param->enctyption_mode, "WPA-");
+    } else if (strstr(encryption_mode, "mix") != NULL) {
+        strcpy(ap_param->enctyption_mode, "WPA-WPA2-");
+    }
+
+    if (strstr(encryption_mode, "psk") != NULL) {
+        strcat(ap_param->enctyption_mode, "Personal");
+    } else if (strstr(encryption_mode, "wpa") != NULL) {
+        strcat(ap_param->enctyption_mode, "Enterprise");
+    }
+    
+    if (strcmp(encryption_mode, "none") == 0) {
+        strcpy(ap_param->enctyption_mode, "None");
+    }
+}
+
+void set_key(wifi_ap_param *ap_param, char *key)
+{
+    strncpy(ap_param->password, key, 64);
+}
+
+void set_radio_param(wifi_radio_param radio_parameter)
+{
+    BOOL enable;
+    BOOL current;
+    char config_file[64] = {0};
+    int ret = 0;
+    struct params param;
+
+    fprintf(stderr, "Start setting radio\n");
+    sprintf(config_file, "/nvram/hostapd%d.conf", radio_parameter.radio_index);
+
+    // Call hal to set in this function
+    // Channel
+    fprintf(stderr, "Set channel: %d, bandwidth: %d\n", radio_parameter.channel, radio_parameter.bandwidth);
+    if (radio_parameter.auto_channel == TRUE) {
+        ret = wifi_setRadioAutoChannelEnable(radio_parameter.radio_index, TRUE);
+    } else {
+        // wifi_setRadioChannel(radio_parameter.radio_index, radio_parameter.channel);
+        ret = wifi_pushRadioChannel2(radio_parameter.radio_index, radio_parameter.channel, radio_parameter.bandwidth, 30);
+    }
+    if (ret != RETURN_OK)
+        fprintf(stderr, "[Set channel failed!!!]\n");
+    ret = 0;
+
+    // Country
+    fprintf(stderr, "Set Country: %s\n", radio_parameter.country);
+    ret = wifi_setRadioCountryCode(radio_parameter.radio_index, radio_parameter.country);
+    if (ret != RETURN_OK)
+        fprintf(stderr, "[Set Country failed!!!]\n");
+    ret = 0;
+
+    // hwmode
+    fprintf(stderr, "Set hwmode: %s\n", radio_parameter.hwmode);
+    ret = wifi_setRadioHwMode(radio_parameter.radio_index, radio_parameter.hwmode);
+    if (ret != RETURN_OK)
+        fprintf(stderr, "[Set hwmode failed!!!]\n");
+    ret = 0;
+
+    // htmode
+    wifi_ieee80211_Mode pure_mode;
+    if (strcmp(radio_parameter.band, "2g") == 0) {
+
+        pure_mode |= WIFI_MODE_B | WIFI_MODE_G;
+        if (strcmp(radio_parameter.htmode, "NOHT") == 0 || strcmp(radio_parameter.htmode, "NONE") == 0)
+            strcpy(radio_parameter.htmode, "11G");
+
+    } else if (strcmp(radio_parameter.band, "5g") == 0) {
+
+        pure_mode |= WIFI_MODE_A;
+        if (strcmp(radio_parameter.htmode, "NOHT") == 0 || strcmp(radio_parameter.htmode, "NONE") == 0) 
+            strcpy(radio_parameter.htmode, "11A");
+    }
+
+    if (strstr(radio_parameter.htmode, "VHT") != NULL)
+        pure_mode |= WIFI_MODE_N | WIFI_MODE_AC;
+    else if (strstr(radio_parameter.htmode, "HT") != NULL && strstr(radio_parameter.htmode, "NO") == NULL)
+        pure_mode |= WIFI_MODE_N;
+    else if (strstr(radio_parameter.htmode, "HE") != NULL)
+        pure_mode |= WIFI_MODE_N | WIFI_MODE_AC | WIFI_MODE_AX;
+
+    radio_parameter.pure_mode = (int)pure_mode;
+    fprintf(stderr, "Set htmode: %s, puremode: %d\n", radio_parameter.htmode, radio_parameter.pure_mode);
+    ret = wifi_setRadioMode(radio_parameter.radio_index, radio_parameter.htmode, radio_parameter.pure_mode);
+    if (ret != RETURN_OK)
+        fprintf(stderr, "[Set htmode failed!!!]\n");
+    ret = 0;
+
+    // disabled
+    if (radio_parameter.disabled == FALSE)
+        enable = TRUE;
+    ret = wifi_getRadioEnable(radio_parameter.radio_index, &current);
+    fprintf(stderr, "radio %d, current: %s, set: %s, getstatus return: %s\n", radio_parameter.radio_index, current ? "enable":"disable", radio_parameter.disabled ? "disable":"enable",  (ret==RETURN_OK)? "success": "failed");
+    // fprintf(stderr, "In if closure. enable: %d, current: %d.\n", enable, current);
+    if (enable != current) {
+        ret = wifi_setRadioEnable(radio_parameter.radio_index, enable);
+    }
+    if (ret != RETURN_OK)
+        fprintf(stderr, "[Set radio %s failed!!!]\n", enable?"enble":"disable");
+    ret = 0;
+
+}
+
+void set_ap_param(wifi_ap_param ap_param)
+{
+    int ret = 0;
+
+    fprintf(stderr, "Start setting ap\n");
+    // Call hal to set in this function
+    // SSID
+    fprintf(stderr, "Set SSID: %s\n", ap_param.ssid);
+    ret = wifi_setSSIDName(ap_param.ap_index, ap_param.ssid);
+    if (ret != RETURN_OK)
+        fprintf(stderr, "[Set SSID failed!!!]\n");
+
+    // wpa and security mode
+    fprintf(stderr, "Set encryption mode: %s\n", ap_param.enctyption_mode);
+    ret = wifi_setApSecurityModeEnabled(ap_param.ap_index, ap_param.enctyption_mode);
+    if (ret != RETURN_OK)
+        fprintf(stderr, "[Set encryption mode failed!!!]\n");
+
+    // key
+    if (strlen(ap_param.password) > 0) {
+        fprintf(stderr, "Set password: %s\n", ap_param.password);
+        ret = wifi_setApSecurityKeyPassphrase(ap_param.ap_index, ap_param.password);
+        if (ret != RETURN_OK)
+            fprintf(stderr, "[Set password failed!!!]\n");
+    }
+
+    // restart ap
+    wifi_setApEnable(ap_param.ap_index, FALSE);
+    wifi_setApEnable(ap_param.ap_index, TRUE);
+}
+
+int apply_uci_config ()
+{
+    struct uci_context *uci_ctx = uci_alloc_context();
+    struct uci_package *uci_pkg = NULL;
+    struct uci_element *e;
+    // struct uci_section *s;
+    const char cfg_name[] = "wireless";
+    int max_radio_num = 0;
+    BOOL parsing_radio = FALSE;
+
+    wifi_getMaxRadioNumber(&max_radio_num);
+    fprintf(stderr, "max radio number: %d\n", max_radio_num);
+    if (uci_load(uci_ctx, cfg_name, &uci_pkg) != UCI_OK) {
+        uci_free_context(uci_ctx);
+        fprintf(stderr, "%s: load uci failed.\n", __func__);
+        return RETURN_ERR;
+    }
+
+    uci_foreach_element(&uci_pkg->sections, e) {
+
+        struct uci_section *s = uci_to_section(e);
+        struct uci_element *option = NULL;
+        wifi_radio_param radio_param = {0};
+        wifi_ap_param ap_param = {0};
+        radio_param.radio_index = -1;
+        ap_param.ap_index = -1;
+
+        if (strcmp(s->type, "wifi-device") == 0) {
+            sscanf(s->e.name, "radio%d", &radio_param.radio_index);
+            parsing_radio = TRUE;
+            fprintf(stderr, "\n----- Start parsing radio %d config. -----\n", radio_param.radio_index);
+        } else if (strcmp(s->type, "wifi-iface") == 0) {
+            sscanf(s->e.name, "default_radio%d", &ap_param.ap_index);
+            parsing_radio = FALSE;
+            fprintf(stderr, "\n----- Start parsing ap %d config. -----\n", ap_param.ap_index);
+        }
+
+        uci_foreach_element(&s->options, option) {
+
+            struct uci_option *op = uci_to_option(option);
+            if (parsing_radio == TRUE) {
+                // transform the type from input string and store the value in radio_param.
+                if (strcmp(op->e.name, "channel") == 0)
+                    set_channel(&radio_param, op->v.string);
+                else if (strcmp(op->e.name, "hwmode") == 0)
+                    set_hwmode(&radio_param, op->v.string);
+                else if (strcmp(op->e.name, "htmode") == 0)
+                    set_htmode(&radio_param, op->v.string);
+                else if (strcmp(op->e.name, "disabled") == 0)
+                    set_disable(&radio_param, op->v.string);
+                else if (strcmp(op->e.name, "band") == 0)
+                    set_band(&radio_param, op->v.string);
+                else if (strcmp(op->e.name, "country") == 0)
+                    set_country(&radio_param, op->v.string);
+                else if (strcmp(op->e.name, "noscan") == 0)
+                    set_band(&radio_param, op->v.string);
+                else
+                    fprintf(stderr, "[%s %s not set!]\n", op->e.name, op->v.string);
+            } else {        
+                // parsing iface
+                if (strcmp(op->e.name, "device") == 0)
+                    set_radionum(&ap_param, op->v.string);
+                else if (strcmp(op->e.name, "ssid") == 0)
+                    set_ssid(&ap_param, op->v.string);
+                else if (strcmp(op->e.name, "encryption") == 0)
+                    set_encryption(&ap_param, op->v.string);
+                else if (strcmp(op->e.name, "key") == 0)
+                    set_key(&ap_param, op->v.string);
+                else
+                    fprintf(stderr, "[%s %s not set!]\n", op->e.name, op->v.string);
+            }
+        }
+        if (parsing_radio == TRUE)
+            set_radio_param(radio_param);
+        else
+            set_ap_param(ap_param);
+    }
+
+    uci_unload(uci_ctx, uci_pkg);
+    uci_free_context(uci_ctx);
+    return RETURN_OK;
+}
+
+int main(int argc, char **argv)
+{
+    if (argc != 2 || strcmp(argv[1], "reload") != 0) {
+        fprintf(stderr, "Usage: wifi reload.\nThis tool is only for RDKB MSP/SQC test.\n");
+        return -1;
+    }
+    apply_uci_config();
+    return 0;
+}
diff --git a/recipes-devtools/wifi-test-tool/files/src/wifi-test-tool.h b/recipes-devtools/wifi-test-tool/files/src/wifi-test-tool.h
new file mode 100644
index 0000000..a8cc11c
--- /dev/null
+++ b/recipes-devtools/wifi-test-tool/files/src/wifi-test-tool.h
@@ -0,0 +1,62 @@
+#include "uci_config.h"
+#include "wifi_hal.h"
+
+typedef enum {
+    WIFI_MODE_A = 0x01,
+    WIFI_MODE_B = 0x02,
+    WIFI_MODE_G = 0x04,
+    WIFI_MODE_N = 0x08,
+    WIFI_MODE_AC = 0x10,
+    WIFI_MODE_AX = 0x20,
+} wifi_ieee80211_Mode;
+
+typedef struct {
+    int radio_index;
+    char type[16];
+    char phy[16];
+    char macaddr[18];
+    BOOL disabled;
+    int channel;
+    BOOL auto_channel;
+    char channels[64];      // list type
+    char hwmode[2];
+    char band[8];
+    char htmode[16];
+    int bandwidth;
+    int pure_mode;
+    int chanbw;
+    char ht_capab[8];
+    int txpower;
+    BOOL diversity;
+    int rxantenna;
+    int txantenna;
+    char country[3];
+    BOOL country_ie;
+    int distance;
+    int beacon_int;
+    BOOL legacy_rates;
+    char require_mode[2];
+    int cell_density;
+    char basic_rate[64];
+    char supported_rates[64];
+    int log_level;
+    char hostapd_options[64];
+} wifi_radio_param;
+
+typedef struct {
+    wifi_radio_param *radio_info;   // for multiple vap
+    int radio_index;
+    int ap_index;
+    char ssid[33];
+    int wpa;
+    char password[64];
+    char enctyption_mode[64];
+} wifi_ap_param;
+
+struct params
+{
+    char * name;
+    char * value;
+};
+
+INT wifi_getMaxRadioNumber(INT *max_radio_num);