Merge "[rdk-b][mt7986][wifi-hal][Refactor set and get radius server functions]"
diff --git a/recipes-connectivity/hostapd/files/wireless b/recipes-connectivity/hostapd/files/wireless
deleted file mode 100644
index 5fea015..0000000
--- a/recipes-connectivity/hostapd/files/wireless
+++ /dev/null
@@ -1,29 +0,0 @@
-config wifi-device 'radio0'

-        option type 'mac80211'

-        option path 'platform/18000000.wbsys'

-        option channel '1'

-        option band '2g'

-        option htmode 'HE20'

-        option disabled '1'

-

-config wifi-iface 'default_radio0'

-        option device 'radio0'

-        option network 'lan'

-        option mode 'ap'

-        option ssid 'Filogic'

-        option encryption 'none'

-

-config wifi-device 'radio1'

-        option type 'mac80211'

-        option path 'platform/18000000.wbsys+1'

-        option channel '36'

-        option band '5g'

-        option htmode 'HE80'

-        option disabled '1'

-

-config wifi-iface 'default_radio1'

-        option device 'radio1'

-        option network 'lan'

-        option mode 'ap'

-        option ssid 'Filogic_5'

-        option encryption 'none'
\ No newline at end of file
diff --git a/recipes-connectivity/hostapd/hostapd_2.10.bb b/recipes-connectivity/hostapd/hostapd_2.10.bb
index 4cbab01..7497f07 100644
--- a/recipes-connectivity/hostapd/hostapd_2.10.bb
+++ b/recipes-connectivity/hostapd/hostapd_2.10.bb
@@ -24,7 +24,6 @@
     file://hostapd-init.sh \
     file://src \
     file://001-rdkb-remove-ubus-support.patch;apply=no \
-    file://wireless \
 "
 require files/patches/patches.inc
 
@@ -99,8 +98,6 @@
          install -m 0644 ${WORKDIR}/hostapd-bhaul5G.conf ${D}${sysconfdir}
          install -m 0644 ${WORKDIR}/hostapd.service ${D}${systemd_unitdir}/system
          install -m 0755 ${WORKDIR}/hostapd-init.sh ${D}${base_libdir}/rdk
-         install -d ${D}${sysconfdir}/config
-         install -m 0644 ${WORKDIR}/wireless ${D}${sysconfdir}/config
 }
 
 FILES_${PN} += " \
@@ -110,5 +107,4 @@
                 ${sysconfdir}/hostapd-bhaul2G.conf \
                 ${sysconfdir}/hostapd-bhaul5G.conf \
                 ${base_libdir}/rdk/hostapd-init.sh \
-                ${sysconfdir}/config/wireless \
 "
diff --git a/recipes-devtools/init-filogic/files/init-uci-config.service b/recipes-devtools/init-filogic/files/init-uci-config.service
new file mode 100644
index 0000000..aaa6415
--- /dev/null
+++ b/recipes-devtools/init-filogic/files/init-uci-config.service
@@ -0,0 +1,14 @@
+[Unit]

+Description=Init UCI wireless config

+DefaultDependencies=no

+After=hostapd.service

+Before=RdkWanManager.service

+

+[Service]

+Type=oneshot

+ExecStart=/bin/sh /usr/sbin/mac80211.sh

+RemainAfterExit=yes

+

+[Install]

+WantedBy=multi-user.target

+

diff --git a/recipes-devtools/init-filogic/files/mac80211.sh b/recipes-devtools/init-filogic/files/mac80211.sh
new file mode 100644
index 0000000..437a3cc
--- /dev/null
+++ b/recipes-devtools/init-filogic/files/mac80211.sh
@@ -0,0 +1,206 @@
+#!/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"
+                sub("\\[", "", channel)
+                sub("\\]", "", channel)
+                bands = bands band channel ":" mode " "
+        }
+        band=""
+}
+
+$1 == "Band" {
+        band = $2
+        channel = ""
+	vht = ""
+	ht = ""
+	he = ""
+}
+
+$0 ~ "Capabilities:" {
+	ht=1
+}
+
+$0 ~ "VHT Capabilities" {
+	vht=1
+}
+
+$0 ~ "HE Iftypes" {
+	he=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"
+	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
+
+	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
+
+		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
+			set wireless.radio${devidx}.disabled=1
+
+			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=none
+EOF
+		uci -q commit wireless
+
+		devidx=$(($devidx + 1))
+		old_path=$path
+	done
+}
+detect_mac80211
\ No newline at end of file
diff --git a/recipes-devtools/init-filogic/init-filogic.bb b/recipes-devtools/init-filogic/init-filogic.bb
index fa3b038..31df79b 100644
--- a/recipes-devtools/init-filogic/init-filogic.bb
+++ b/recipes-devtools/init-filogic/init-filogic.bb
@@ -10,15 +10,23 @@
     file://COPYING \
     file://init-IPv6.sh \
     file://init-IPv6.service \
+    file://mac80211.sh \
+    file://init-uci-config.service \
     "
 
+RDEPENDS_${PN} += "bash"
+
 SYSTEMD_PACKAGES = "${PN}"
 SYSTEMD_SERVICE_${PN} = " init-IPv6.service"
+SYSTEMD_SERVICE_${PN} += " init-uci-config.service"
 FILES_${PN} += "{systemd_unitdir}/system/init-IPv6.service"
+FILES_${PN} += "{systemd_unitdir}/system/init-uci-config.service"
 
 do_install() {
     install -d ${D}${sbindir}
     install -m 0755 ${WORKDIR}/init-IPv6.sh ${D}${sbindir}
+    install -m 0755 ${WORKDIR}/mac80211.sh ${D}${sbindir}
     install -d ${D}${systemd_unitdir}/system/
     install -m 0644 ${S}/init-IPv6.service ${D}${systemd_unitdir}/system
+    install -m 0644 ${S}/init-uci-config.service ${D}${systemd_unitdir}/system
 }