##!/bin/sh
##149  0  0 a,n,ac,ax  WPAand11i TKIPandAESEncryption 12345678
#wlan_inst_config=/tmp/hostapd_wlan0.conf
#touch $wlan_inst_config
##for CN channel 2.4G 1-13  5G 36,40,44,48,52,56,60,64  149,153,157,161,165
#g_channel=157
##0-20MHZ  1-40MHZ 2-80MHZ
#g_bandwidth=0
##0,1
#g_shortgi=1
##a b g n b,g n,g b,g,n b,g,n,ax  a,n,ac a,n,ac,ax a,n
#g_standard=a,n,ac,ax
##None WPA 11i WPAand11i WPA3 11iandWPA3 WPA_Ent 11i_Ent WPAand11i_Ent  WPA3_Ent 11iandWPA3_Ent
#g_encrypt_value=None
##TKIPEncryption AESEncryption TKIPandAESEncryption
#g_cipher=TKIPandAESEncryption
##str sample  12345678
#g_wpa_passphrase_value=12345678
#g_auth_server_addr=
#g_auth_server_port=
#g_auth_server_shared_secret=
#g_wpa_group_rekey=
#g_wep_default_key_index=
#g_wep_key0=
#g_ifname=wlan0
#g_ssid=EG5200_11AX_Demo_test001
#g_hidessid=0
#g_isolation=0
#g_beacon_interval=100
#g_rts_threshold=2347
#g_fragment_threshold=2346
#g_dtim=1
#g_wmm=0
#g_macaddr_acl=
#g_deny_mac_file=
#g_accept_mac_file=
#g_max_station_num=

defval()
{
	local v="$1"
	local d="$2"
	if [ -z "$v" ]; then
		echo -n "$d"
	else
		echo -n "$v"
	fi
}

HOSTAPD_11BGN_CHANNEL_LIMIT=5
HOSTAPD_11BGN_CHANNEL_BOUNDARY=14
WlanSecondaryChannelIsPlus()
{
	local channel=$1
	
	if [ $HOSTAPD_11BGN_CHANNEL_BOUNDARY -gt $channel ]; then
		if [ $channel -lt $HOSTAPD_11BGN_CHANNEL_LIMIT ]; then
			return 1
		fi
	else
		for c in 36 44 52 60 100 108 116 124 132 140 149 157 165 173;
		do
			if [ $c -eq $channel ]; then
				return 1
			fi
		done
	fi
	
	return 0
}

WlanSet5G80MCapab()
{
	local channel=$1
	local boundary=36

	if [ $channel -ge 149 ]; then
		boundary=149
	fi

	let minus=channel-boundary
	let minus=minus%8

	if [ $minus -ne 0 ];then
		echo -n "[HT40-]"
	else
		echo -n "[HT40+]"
	fi
}

WlanCalcCenterIndex()
{
	local channel=$1
	local center=0
	if [ $channel -lt 149 ]; then
		let center=channel-36
		let center=center/16*16+42
	else
		let center=channel-149
		let center=center/16*16+155
	fi
	echo -n $center
}

change_value()
{
	local name=$1;
	local value=$2;
	local quote=$3
	local file=$wlan_inst_config

	if [ -z "$value" ]; then
		sed -i "/"$name"=/d" $file
		return
	fi
	
	local found=`sed -n "/^"$name"=/p" $file | awk -F '=' '{print $1}'`
	if [ -z "$found" ]; then
		# echo "new add ....."
		if [ -n "$quote" ]; then
			echo "$name=\"$value\"" >> $file
		else
			echo "$name=$value" >> $file
		fi
		
	else
		if [ -n "$quote" ]; then
			eval "sed -i 's/^\($name=\).*/\1\"$value\"/g' $file"
		else
			eval "sed -i 's/^\($name=\).*/\1$value/g' $file"
		fi
		
	fi
}

WlanSetPhyCapab()
{
	local channel=$1
	local bandwidth=$2
	local shortgi=$3
	local standard=$4
	local ht_capab=""
	local vht_capab=""
	local vht_oper_chwidth=""
	local vht_oper_centr_freq_seg0_idx=""
	local he_oper_chwidth=""
	local he_oper_centr_freq_seg0_idx=""
        local force_40mhz=""

	local ret=0
	
	#0: 20, 1:40,  2:80
	case "$bandwidth" in
	"0")
		if [ "$shortgi" == "1" ]; then
			ht_capab="[SHORT-GI-20]"
		else
			ht_capab="[HT20]"
		fi
		;;
	"1")
		if [ "$shortgi" == "1" ]; then
			ht_capab="[SHORT-GI-40]"
		fi
		
		`WlanSecondaryChannelIsPlus $channel`; ret=$?
		if [ "$ret" == "1" ]; then
			ht_capab=$ht_capab"[HT40+]"
		else
			ht_capab=$ht_capab"[HT40-]"
		fi
                force_40mhz=1
		;;
	"2")
		ht_capab=`WlanSet5G80MCapab $channel`
		if [ "$shortgi" == "1" ]; then
			vht_capab="[SHORT-GI-80]"
		fi
	        if [ "$standard" == "11ax" ];then
                    he_oper_chwidth=1
                    he_oper_centr_freq_seg0_idx=`WlanCalcCenterIndex $channel`
                else
		    vht_oper_chwidth=1
		    vht_oper_centr_freq_seg0_idx=`WlanCalcCenterIndex $channel`
                fi
		;;
	esac
	
        change_value force_40mhz $force_40mhz
	change_value ht_capab $ht_capab
	change_value vht_capab $vht_capab
	change_value vht_oper_chwidth $vht_oper_chwidth
	change_value vht_oper_centr_freq_seg0_idx $vht_oper_centr_freq_seg0_idx
	change_value he_oper_chwidth $he_oper_chwidth
	change_value he_oper_centr_freq_seg0_idx $he_oper_centr_freq_seg0_idx
}

WlanSetPhyMode()
{

	local channel=$g_channel
	channel=`defval "$channel" 36`
	local bandwidth=$g_bandwidth
	bandwidth=`defval "$bandwidth" 2`
	local shortgi=$g_shortgi
	shortgi=`defval "$shortgi" 0`
	local standard=$g_standard
	standard=`defval "$standard" "a,n,ac,ax"`
	change_value channel $channel
	
	# 2. mode
	local hw_mode="" #2.4: g, 5: a
	local ieee80211n=""
	local ieee80211ac=""
	local ieee80211ax=""
	local require_ht=""
	case "${standard}" in
	"a")
		hw_mode="a"
		;;
	"a,n")
		hw_mode="a"
		ieee80211n="1"
                WlanSetPhyCapab $channel $bandwidth $shortgi
		;;
	"b")
		hw_mode="b"
		;;
	"g")
		hw_mode="g"
		;;
	"n")
		hw_mode="g"
		ieee80211n="1"
		require_ht="1"
                WlanSetPhyCapab $channel $bandwidth $shortgi
		;;
	"b,g")
		hw_mode="g"
		;;
	"n,g")
		hw_mode="g"
		ieee80211n="1"
                WlanSetPhyCapab $channel $bandwidth $shortgi
		;;
	"b,g,n")
		hw_mode="g"
		ieee80211n="1"
                WlanSetPhyCapab $channel $bandwidth $shortgi
		;;
	"b,g,n,ax")
		hw_mode="g"
		ieee80211n="1"
		ieee80211ax="1"
                WlanSetPhyCapab $channel $bandwidth $shortgi "11ax"
		;;
	"a,n,ac")
		hw_mode="a"
		ieee80211n="1"
		ieee80211ac="1"
                WlanSetPhyCapab $channel $bandwidth $shortgi
		;;
	"a,n,ac,ax")
		hw_mode="a"
		ieee80211n="1"
		ieee80211ac="1"
		ieee80211ax="1"
                WlanSetPhyCapab $channel $bandwidth $shortgi "11ax"
		;;
	esac

	change_value hw_mode $hw_mode
	change_value ieee80211n $ieee80211n
	change_value ieee80211ac $ieee80211ac
	change_value ieee80211ax $ieee80211ax
	change_value require_ht $require_ht
}
WlanSetSecurity()
{
	
	local auth_algs=""
	local wpa=""
	local wpa_key_mgmt=""
	local wpa_pairwise=""
	local rsn_pairwise=""
	local wpa_passphrase=""
	local wpa3=0

        local ieee8021x=""

	local encrypt_value=$g_encrypt_value
	encrypt_value=`defval "$encrypt_value" WPAand11i`
	local cipher=$g_cipher
	cipher=`defval "$cipher" TKIPandAESEncryption`
	local wpa_passphrase_value=$g_wpa_passphrase_value
	wpa_passphrase_value=`defval "$wpa_passphrase_value" 12345678`

	local tmp_auth_server_addr=$g_auth_server_addr
	local tmp_auth_server_port=$g_auth_server_port
	local tmp_auth_server_shared_secret=$g_auth_server_shared_secret
	local tmp_wpa_group_rekey=$g_wpa_group_rekey

	local wep_default_key_index=$g_wep_default_key_index
	local wep_key0=$g_wep_key0

	case "$encrypt_value" in
	"None")
		wpa=0
		auth_algs=1
		wep_default_key=0
		;;
	"WPA" | "11i" | "WPAand11i" | "WPA3" | "11iandWPA3")
		auth_algs=1
		# 鉴权
		local pairwise_name=""
		case "$encrypt_value" in
		"WPA")
		        wpa_key_mgmt="WPA-PSK"
			wpa=1
			pairwise_name="wpa_pairwise"
			;;
		"11i")
		        wpa_key_mgmt="WPA-PSK"
			wpa=2
			pairwise_name="rsn_pairwise"
			;;
		"WPAand11i")
		        wpa_key_mgmt="WPA-PSK"
			wpa=3
			pairwise_name="wpa_pairwise rsn_pairwise"
			;;

		"WPA3")
		        wpa_key_mgmt="SAE"
			wpa=2
			pairwise_name="rsn_pairwise"
                        ieee80211w=2
                        wpa3=1
			;;
		"11iandWPA3")
		        wpa_key_mgmt="WPA-PSK SAE"
			wpa=2
			pairwise_name="rsn_pairwise"
                        ieee80211w=1
                        wpa3=1
			;;
		esac

		# 加密
		local pairwise_value=""
		case $cipher in
		"TKIPEncryption")
			pairwise_value="TKIP"
			;;
		"AESEncryption")
			pairwise_value="CCMP"
			;;
		"TKIPandAESEncryption")
			pairwise_value="TKIP CCMP"
			;;
		esac
                #TKIP is not supported in WPA3
                if [ "$wpa3" = "1" ];then
                    pairwise_value="CCMP"
                fi
		
		for pairwise in $pairwise_name
		do
			eval $pairwise="\"$pairwise_value\""
		done
		
		wpa_passphrase=$wpa_passphrase_value
                wpa_group_rekey=$tmp_wpa_group_rekey
                ;;
	"WPA_Ent" | "11i_Ent" | "WPAand11i_Ent" | "WPA3_Ent" | "11iandWPA3_Ent" )
		auth_algs=1
		# 鉴权
		local pairwise_name=""
		case "$encrypt_value" in
		"WPA_Ent")
		        wpa_key_mgmt="WPA-EAP"
			wpa=1
			pairwise_name="wpa_pairwise"
			;;
		"11i_Ent")
		        wpa_key_mgmt="WPA-EAP"
			wpa=2
			pairwise_name="rsn_pairwise"
			;;
		"WPAand11i_Ent")
		        wpa_key_mgmt="WPA-EAP"
			wpa=3
			pairwise_name="wpa_pairwise rsn_pairwise"
			;;
		"WPA3_Ent")
		        wpa_key_mgmt="WPA-EAP-SUITE-B-192"
			wpa=2
			pairwise_name="rsn_pairwise"
                        ieee80211w=2
                        wpa3=1
			;;
		"11iandWPA3_Ent")
		        wpa_key_mgmt="WPA-EAP WPA-EAP-SUITE-B-192"
			wpa=2
			pairwise_name="rsn_pairwise"
                        ieee80211w=1
                        wpa3=1
			;;
		esac
		

		# 加密
		local pairwise_value=""
		case $cipher in
		"TKIPEncryption")
			pairwise_value="TKIP"
			;;
		"AESEncryption")
			pairwise_value="CCMP"
			;;
		"TKIPandAESEncryption")
			pairwise_value="TKIP CCMP"
			;;
		esac

                #TKIP is not supported in WPA3
                if [ "$wpa3" = "1" ];then
                    pairwise_value="CCMP"
                fi
		
		for pairwise in $pairwise_name
		do
			eval $pairwise="\"$pairwise_value\""
		done
                ieee8021x=1
                auth_server_addr=$tmp_auth_server_addr
                auth_server_port=$tmp_auth_server_port
                auth_server_shared_secret=$tmp_auth_server_shared_secret
                wpa_group_rekey=$tmp_wpa_group_rekey
		
                ;;

          "WEP")
		# 鉴权
		wpa=0
		auth_algs=2
		
		# 加密 WEP
                wep_default_key_index=$wep_default_key_index
                wep_key0=$wep_key0

	esac

	change_value auth_algs $auth_algs
	change_value wpa $wpa
	change_value wpa_key_mgmt "$wpa_key_mgmt"
	change_value wpa_pairwise "$wpa_pairwise"
	change_value rsn_pairwise "$rsn_pairwise"
	change_value wpa_passphrase $wpa_passphrase

	change_value wep_default_key $wep_default_key_index
        #change_value wep_key0 "$wep_key0" 1
        change_value wep_key0 "$wep_key0" $g_wep_key0_type
        
        change_value ieee8021x $ieee8021x
        change_value auth_server_addr $auth_server_addr
        change_value auth_server_port $auth_server_port
        change_value auth_server_shared_secret $auth_server_shared_secret
        change_value wpa_group_rekey $wpa_group_rekey
        #For WPA3
        change_value ieee80211w $ieee80211w
}

config_reset()
{
cat > ${wlan_inst_config} <<EOL
ctrl_interface=/var/run/${g_ifname}_hostapd
logger_syslog=0
logger_stdout_level=2
logger_stdout=-1
auth_server_addr=192.168.0.1
auth_server_port=1812
auth_server_shared_secret=12345678
wpa_group_rekey=3600
wep_default_key=0
wep_key0=vwxyz
EOL
}

WlanDoBasicUnique()
{
	local ifname=$g_ifname
	ifname=`defval "$ifname" wlan0`
	local ssid="$g_ssid"
	ssid=`defval "$ssid" ssid`
	local hidessid=$g_hidessid
	hidessid=`defval "$hidessid" 0`
	local isolation=$g_isolation
	isolation=`defval "$isolation" 0`
	change_value interface $ifname
	change_value ssid "$ssid"
	change_value ignore_broadcast_ssid ${hidessid}
	change_value ap_isolate $isolation
}

WlanDoBasicCommon()
{
	local beacon_interval=$g_beacon_interval
	beacon_interval=`defval "$beacon_interval" 100`
	local rts_threshold=$g_rts_threshold
	rts_threshold=`defval "$rts_threshold" 2347`
	local fragment_threshold=$g_fragment_threshold
	fragment_threshold=`defval "$fragment_threshold" 2346`
	local dtim=$g_dtim
	dtim=`defval "$dtim" 1`
	#local country_code=CN
	local wmm=$g_wmm
	wmm=`defval "$wmm" 0`
	
	change_value beacon_int $beacon_interval
	change_value rts_threshold $rts_threshold
	change_value fragm_threshold $fragment_threshold
	change_value dtim_period $dtim
	#change_value country_code $country_code
	
	change_value wmm_enabled $wmm

	change_value max_num_sta $g_max_station_num
}

WlanDoAdvanced()
{
    WlanSetPhyMode
}

WlanSetAcl()
{
    change_value macaddr_acl $g_macaddr_acl
    change_value deny_mac_file $g_deny_mac_file
    change_value accept_mac_file $g_accept_mac_file
}

WlanSetBridge()
{
    change_value bridge $g_bridge
}

config_reset
WlanSetBridge
WlanDoBasicUnique 
WlanDoBasicCommon
WlanDoAdvanced
WlanSetSecurity 
WlanSetAcl

#insmod wlan_cnss_core_pcie.ko
#insmod wlan.ko country_code=CN
#sleep 5
#ifconfig wlan0 up
#killall hostapd
#sleep 3
#hostapd -dd $wlan_inst_config &
cat $wlan_inst_config
