[rdkb][common][app][Add wifi hal EHT support]

[Description]
Add wifi hal EHT support.
1. halinterface add EHT support patch.
2. wifi-test-tool add EHT support for config wifi.
3. uci init script align OpenWrt parsing EHT capab.

[Release-log]
N/A

Change-Id: I4b351a61f43f137cec9c7bcf9f3e8e78d5386aff
diff --git a/recipes-wifi/hal/files/0002-Add-EHT-support.patch b/recipes-wifi/hal/files/0002-Add-EHT-support.patch
new file mode 100644
index 0000000..50b6495
--- /dev/null
+++ b/recipes-wifi/hal/files/0002-Add-EHT-support.patch
@@ -0,0 +1,37 @@
+From 6a3fa092965c3b19f09592b8608dd9ded6089157 Mon Sep 17 00:00:00 2001
+From: "Allen.Ye" <allen.ye@mediatek.com>
+Date: Wed, 1 Mar 2023 18:05:49 +0800
+Subject: [PATCH] Add EHT support
+
+---
+ wifi_hal_generic.h | 7 +++++--
+ 1 file changed, 5 insertions(+), 2 deletions(-)
+
+diff --git a/wifi_hal_generic.h b/wifi_hal_generic.h
+index 01417bf..8d731c4 100644
+--- a/wifi_hal_generic.h
++++ b/wifi_hal_generic.h
+@@ -282,7 +282,9 @@ typedef enum{
+     WIFI_CHANNELBANDWIDTH_40MHZ = 0x2,
+     WIFI_CHANNELBANDWIDTH_80MHZ = 0x4,
+     WIFI_CHANNELBANDWIDTH_160MHZ = 0x8,
+-    WIFI_CHANNELBANDWIDTH_80_80MHZ = 0x10
++    WIFI_CHANNELBANDWIDTH_80_80MHZ = 0x10,
++    WIFI_CHANNELBANDWIDTH_320_1MHZ = 0x20,
++    WIFI_CHANNELBANDWIDTH_320_2MHZ = 0x40
+ } wifi_channelBandwidth_t;
+ 
+ typedef struct {
+@@ -311,7 +313,8 @@ typedef enum {
+     WIFI_80211_VARIANT_H = 0x10,
+     WIFI_80211_VARIANT_AC = 0x20,
+     WIFI_80211_VARIANT_AD = 0x40,
+-    WIFI_80211_VARIANT_AX = 0x80
++    WIFI_80211_VARIANT_AX = 0x80,
++    WIFI_80211_VARIANT_BE = 0x100
+ } wifi_ieee80211Variant_t;
+ 
+ #define MAXNUMBEROFTRANSMIPOWERSUPPORTED 21
+-- 
+2.18.0
+
diff --git a/recipes-wifi/hal/halinterface.bbappend b/recipes-wifi/hal/halinterface.bbappend
index 3a26d71..daabcb2 100644
--- a/recipes-wifi/hal/halinterface.bbappend
+++ b/recipes-wifi/hal/halinterface.bbappend
@@ -1,6 +1,6 @@
 FILESEXTRAPATHS_prepend := "${THISDIR}/files:"
-SRC_URI += "file://*.patch;apply=no \
-	"
+SRC_URI_append = " file://*.patch;apply=no \
+    "
 
 #This is workaround for missing do_patch when RDK uses external sources
 do_filogic_patches() {
@@ -9,6 +9,9 @@
             patch -p1 < ${WORKDIR}/sta-network.patch
             patch -p1 < ${WORKDIR}/add_undefined_dfs_function.patch
             patch -p1 < ${WORKDIR}/0001-Add-owe-mode.patch
+            if ${@bb.utils.contains('DISTRO_FEATURES','wifi_eht','true','false',d)}; then
+                patch -p1 < ${WORKDIR}/0002-Add-EHT-support.patch
+            fi
             touch patch_applied
         fi
 }
diff --git a/recipes-wifi/hostapd/files/mac80211-EHT.sh b/recipes-wifi/hostapd/files/mac80211-EHT.sh
new file mode 100644
index 0000000..c8de5bd
--- /dev/null
+++ b/recipes-wifi/hostapd/files/mac80211-EHT.sh
@@ -0,0 +1,245 @@
+#!/bin/sh
+
+#append DRIVERS "mac80211"
+
+lookup_phy() {
+	[ -n "$phy" ] && {
+		[ -d /sys/class/ieee80211/$phy ] && return
+	}
+
+	local devpath
+	config_get devpath "$device" path
+	[ -n "$devpath" ] && {
+		phy="$(iwinfo nl80211 phyname "path=$devpath")"
+		[ -n "$phy" ] && return
+	}
+
+	local macaddr="$(config_get "$device" macaddr | tr 'A-Z' 'a-z')"
+	[ -n "$macaddr" ] && {
+		for _phy in /sys/class/ieee80211/*; do
+			[ -e "$_phy" ] || continue
+
+			[ "$macaddr" = "$(cat ${_phy}/macaddress)" ] || continue
+			phy="${_phy##*/}"
+			return
+		done
+	}
+	phy=
+	return
+}
+
+find_mac80211_phy() {
+	local device="$1"
+
+	config_get phy "$device" phy
+	lookup_phy
+	[ -n "$phy" -a -d "/sys/class/ieee80211/$phy" ] || {
+		echo "PHY for wifi device $1 not found"
+		return 1
+	}
+	config_set "$device" phy "$phy"
+
+	config_get macaddr "$device" macaddr
+	[ -z "$macaddr" ] && {
+		config_set "$device" macaddr "$(cat /sys/class/ieee80211/${phy}/macaddress)"
+	}
+
+	return 0
+}
+
+check_mac80211_device() {
+	config_get phy "$1" phy
+	[ -z "$phy" ] && {
+		find_mac80211_phy "$1" >/dev/null || return 0
+		config_get phy "$1" phy
+	}
+	[ "$phy" = "$dev" ] && found=1
+}
+
+
+__get_band_defaults() {
+	local phy="$1"
+
+	( iw phy "$phy" info; echo ) | awk '
+BEGIN {
+        bands = ""
+}
+
+($1 == "Band" || $1 == "") && band {
+        if (channel) {
+		mode="NOHT"
+		if (ht) mode="HT20"
+		if (vht && band != "1:") mode="VHT80"
+		if (he) mode="HE80"
+		if (he && band == "1:") mode="HE20"
+		if (eht) mode="EHT80"
+		if (eht && band == "1:") mode="EHT20"
+                sub("\\[", "", channel)
+                sub("\\]", "", channel)
+                bands = bands band channel ":" mode " "
+        }
+        band=""
+}
+
+$1 == "Band" {
+        band = $2
+        channel = ""
+	vht = ""
+	ht = ""
+	he = ""
+	eht = ""
+}
+
+$0 ~ "Capabilities:" {
+	ht=1
+}
+
+$0 ~ "VHT Capabilities" {
+	vht=1
+}
+
+$0 ~ "HE Iftypes" {
+	he=1
+}
+
+$0 ~ "EHT Iftypes" {
+	eht=1
+}
+
+$1 == "*" && $3 == "MHz" && $0 !~ /disabled/ && band && !channel {
+        channel = $4
+}
+
+END {
+        print bands
+}'
+}
+
+get_band_defaults() {
+	local phy="$1"
+
+	for c in $(__get_band_defaults "$phy"); do
+		local band="${c%%:*}"
+		c="${c#*:}"
+		local chan="${c%%:*}"
+		c="${c#*:}"
+		local mode="${c%%:*}"
+
+		case "$band" in
+			1) band=2g;;
+			2) band=5g;;
+			3) band=60g;;
+			4) band=6g;;
+			*) band="";;
+		esac
+
+		[ -n "$band" ] || continue
+		[ -n "$mode_band" -a "$band" = "6g" ] && return
+
+		mode_band="$band"
+		channel="$chan"
+		htmode="$mode"
+		if [ "$band" = "6g" ]
+		then
+			encryption=sae
+			key=12345678
+		else
+			encryption=none
+		fi
+	done
+}
+
+detect_mac80211() {
+	devidx=0
+	#config_load wireless
+	while :; do
+		#config_get type "radio$devidx" type
+		[ -n "$type" ] || break
+		devidx=$(($devidx + 1))
+	done
+
+	if [ ! -f /etc/config/wireless ]; then
+		mkdir -p /etc/config
+		echo -n > /etc/config/wireless
+	fi
+
+	pcieCheck="$(uci get wireless.radio1.path)"
+	checkpath="$(realpath /sys/class/ieee80211/phy1/device | cut -d/ -f4-)"
+
+	if [[ "$pcieCheck"* != "$checkpath"* ]]; then
+		echo -n > /etc/config/wireless
+		rm /nvram/hostapd*
+	fi
+
+	old_path=""
+	for _dev in /sys/class/ieee80211/*; do
+		[ -e "$_dev" ] || continue
+
+		dev="${_dev##*/}"
+
+		found="$(uci get wireless.radio${devidx})"
+
+
+		if [ "$found" == "wifi-device" ]; then
+			devidx=$(($devidx + 1))
+			continue
+		fi
+
+		mode_band=""
+		channel=""
+		htmode=""
+		ht_capab=""
+		
+
+		get_band_defaults "$dev"
+
+		path="$(realpath /sys/class/ieee80211/"$dev"/device | cut -d/ -f4-)"
+		if [ -n "$path" ]; then
+			if [ "$path" == "$old_path" ]; then
+				dev_id="set wireless.radio${devidx}.path='$path'+1"
+			else
+				dev_id="set wireless.radio${devidx}.path='$path'"
+			fi	
+		else
+			dev_id="set wireless.radio${devidx}.macaddr=$(cat /sys/class/ieee80211/${dev}/macaddress)"
+		fi
+
+		if [ "$(cat /sys/class/ieee80211/"$dev"/device/device)" == "0x7906" ]; then
+			isMerlin=1
+		fi
+
+		if [ "$mode_band" = "6g" ]; then
+			channel="37"
+		fi
+		setdisable="set wireless.radio${devidx}.disabled=0"
+
+
+		if [ "$isMerlin" == "1" ] && [ "$devidx" == "2" ] && [ "$mode_band" == "2g" ]; then
+			setdisable="set wireless.radio${devidx}.disabled=1"
+		fi
+	
+		uci -q batch <<-EOF
+			set wireless.radio${devidx}=wifi-device
+			set wireless.radio${devidx}.type=mac80211
+			${dev_id}
+			set wireless.radio${devidx}.channel=${channel}
+			set wireless.radio${devidx}.band=${mode_band}
+			set wireless.radio${devidx}.htmode=$htmode
+			${setdisable}
+			set wireless.default_radio${devidx}=wifi-iface
+			set wireless.default_radio${devidx}.device=radio${devidx}
+			set wireless.default_radio${devidx}.network=lan
+			set wireless.default_radio${devidx}.mode=ap
+			set wireless.default_radio${devidx}.ssid=Filogic_${mode_band}
+			set wireless.default_radio${devidx}.encryption=${encryption}
+EOF
+		[ -n "$key" ] && {
+			uci -q set wireless.default_radio${devidx}.key=${key}
+		}
+		uci -q commit wireless
+
+		devidx=$(($devidx + 1))
+		old_path=$path
+	done
+}
+detect_mac80211
\ No newline at end of file
diff --git a/recipes-wifi/hostapd/files/mac80211.sh b/recipes-wifi/hostapd/files/mac80211.sh
index b83e7aa..6449516 100644
--- a/recipes-wifi/hostapd/files/mac80211.sh
+++ b/recipes-wifi/hostapd/files/mac80211.sh
@@ -132,6 +132,13 @@
 		mode_band="$band"
 		channel="$chan"
 		htmode="$mode"
+		if [ "$band" = "6g" ]
+		then
+			encryption=sae
+			key=12345678
+		else
+			encryption=none
+		fi
 	done
 }
 
@@ -217,8 +224,11 @@
 			set wireless.default_radio${devidx}.network=lan
 			set wireless.default_radio${devidx}.mode=ap
 			set wireless.default_radio${devidx}.ssid=Filogic_${mode_band}
-			set wireless.default_radio${devidx}.encryption=none
+			set wireless.default_radio${devidx}.encryption=${encryption}
 EOF
+		[ -n "$key" ] && {
+			uci -q set wireless.default_radio${devidx}.key=${key}
+		}
 		uci -q commit wireless
 
 		devidx=$(($devidx + 1))
diff --git a/recipes-wifi/hostapd/hostapd_2.10.3.bb b/recipes-wifi/hostapd/hostapd_2.10.3.bb
index df99b78..5f9d72f 100644
--- a/recipes-wifi/hostapd/hostapd_2.10.3.bb
+++ b/recipes-wifi/hostapd/hostapd_2.10.3.bb
@@ -23,7 +23,7 @@
     file://hostapd-5G-7916.conf \
     file://hostapd.service \
     file://hostapd-init.sh \
-    file://mac80211.sh \
+    file://mac80211-EHT.sh \
     file://init-uci-config.service \
     file://src \
     file://001-rdkb-remove-ubus-support.patch;apply=no \
@@ -98,7 +98,7 @@
          install -m 0644 ${WORKDIR}/hostapd.service ${D}${systemd_unitdir}/system
          install -m 0755 ${WORKDIR}/hostapd-init.sh ${D}${base_libdir}/rdk
          install -m 0644 ${WORKDIR}/init-uci-config.service ${D}${systemd_unitdir}/system
-         install -m 0755 ${WORKDIR}/mac80211.sh ${D}${sbindir}
+         install -m 0755 ${WORKDIR}/mac80211-EHT.sh ${D}${sbindir}/mac80211.sh
 }
 
 FILES_${PN} += " \
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 1887aca..8828184 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
@@ -125,10 +125,11 @@
     // HT40     -> 11NGHT40PLUS
     // VHT40+   -> 11ACVHT40PLUS
     // HE80     -> 11AXHE80
-    if (strstr(htmode, "+") != NULL) {
+    // EHT320-1 -> 11BEEHT320-1
+    if (strstr(htmode, "40+") != NULL) {
         strncpy(tmp, htmode, strlen(htmode) - 1);
         strcat(tmp, "PLUS");
-    } else if (strstr(htmode, "-") != NULL) {
+    } else if (strstr(htmode, "40-") != NULL) {
         strncpy(tmp, htmode, strlen(htmode) - 1);
         strcat(tmp, "MINUS");
     } else 
@@ -137,6 +138,14 @@
 
     if (strstr(htmode, "VHT") != NULL) {
         snprintf(radio_param->htmode, sizeof(radio_param->htmode), "11AC%s", tmp);
+    } else if (strstr(htmode, "EHT") != NULL) {
+        snprintf(radio_param->htmode, sizeof(radio_param->htmode), "11BE%s", tmp);
+        if (strstr(htmode, "320-1") != NULL)
+            radio_param->eht_320_conf = 1;
+        else if (strstr(htmode, "320") != NULL)     // EHT320 or EHT320-2
+            radio_param->eht_320_conf = 2;
+        else
+            radio_param->eht_320_conf = 0;
     } 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) {
@@ -355,7 +364,7 @@
     operationParam.autoChannelEnabled = radio_parameter.auto_channel;
     operationParam.channel = radio_parameter.channel;
 
-    //bandwidth
+    // bandwidth
     if (radio_parameter.bandwidth == 20){
         operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_20MHZ;
     }else if (radio_parameter.bandwidth == 40){
@@ -364,6 +373,13 @@
         operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_80MHZ;
     }else if (radio_parameter.bandwidth == 160){
         operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_160MHZ;
+    }else if (radio_parameter.bandwidth == 320){
+        if ((radio_parameter.eht_320_conf == 1 || radio_parameter.channel <= 29) && radio_parameter.channel < 193)
+            operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_320_1MHZ;
+        else if (radio_parameter.eht_320_conf == 2 || radio_parameter.channel >= 193)
+            operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_320_2MHZ;
+        else
+            fprintf(stderr, "[Set EHT 320 bandwidth error with conf %d!!!]\n", radio_parameter.eht_320_conf);
     }
 
     // htmode
@@ -372,25 +388,30 @@
         mode |= WIFI_80211_VARIANT_B | WIFI_80211_VARIANT_G;
         if (strcmp(radio_parameter.htmode, "NOHT") == 0 || strcmp(radio_parameter.htmode, "NONE") == 0)
             strcpy(radio_parameter.htmode, "11G");
-
-        if (strstr(radio_parameter.htmode, "HE") != NULL)
+        else if (strstr(radio_parameter.htmode, "HE") != NULL)
             mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AX;
+        else if (strstr(radio_parameter.htmode, "EHT") != NULL)
+            mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AX | WIFI_80211_VARIANT_BE;
+        else if (strstr(radio_parameter.htmode, "HT") != NULL)
+            mode |= WIFI_80211_VARIANT_N;
 
     } else if (strcmp(radio_parameter.band, "5g") == 0) {
         mode |= WIFI_80211_VARIANT_A;
         if (strcmp(radio_parameter.htmode, "NOHT") == 0 || strcmp(radio_parameter.htmode, "NONE") == 0) 
             strcpy(radio_parameter.htmode, "11A");
-
-        if (strstr(radio_parameter.htmode, "HE") != NULL)
+        else if (strstr(radio_parameter.htmode, "VHT") != NULL)
+            mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AC;
+        else if (strstr(radio_parameter.htmode, "HE") != NULL)
             mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AC | WIFI_80211_VARIANT_AX;
-    }else if (strcmp(radio_parameter.band, "6g") == 0) {
-        mode |= WIFI_80211_VARIANT_A | WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AC | WIFI_80211_VARIANT_AX;;
-    }    
-
-    if (strstr(radio_parameter.htmode, "VHT") != NULL)
-        mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AC;
-    else if (strstr(radio_parameter.htmode, "HT") != NULL && strstr(radio_parameter.htmode, "NO") == NULL)
-        mode |= WIFI_80211_VARIANT_N;
+        else if (strstr(radio_parameter.htmode, "EHT") != NULL)
+            mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AC | WIFI_80211_VARIANT_AX | WIFI_80211_VARIANT_BE;
+        else if (strstr(radio_parameter.htmode, "HT") != NULL)
+            mode |= WIFI_80211_VARIANT_N;
+    } else if (strcmp(radio_parameter.band, "6g") == 0) {
+        mode |= WIFI_80211_VARIANT_AX;
+        if (strstr(radio_parameter.htmode, "EHT") != NULL)
+            mode |= WIFI_80211_VARIANT_BE;
+    }
 
     operationParam.variant = mode;
 
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 2ca1331..a235ed0 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
@@ -23,6 +23,7 @@
     char band[8];
     char htmode[16];
     int bandwidth;
+    int eht_320_conf;
     int pure_mode;
     int chanbw;
     char ht_capab[8];