#!/bin/sh
####################################################################
## makeVAP
##
## This script is used to create AP or Station instances (VAPs).  It
## will NOT actually join the bridge or do any RF configuration.
##
## The form of the command is
##
## makeVAP <Mode> <ESSID> <Channel_String> <beaconint>
##
## Where
##     Mode:    Either ap, ap-wds, sta, or sta-wds
##              (access point or station)
##     ESSID:   ESSID String
##     Channel: String indicating the channel configuration.  This is in
##     String   the form inst:RF:channel:mode where
##              Inst = Interface instance (which radio, 0 or 1)
##              RF   = RF indicates radio should be configured with the specified parameters
##              channel = channel to put the AP on, use 11A or 11G to scan
##              mode = operating mode, one of
##              11AST         : 11 A Static Turbo (Legacy)
##              AUTO          : Legacy Scan Mode
##              11A           : Legacy 11A mode
##              11B
##              11G
##              FH
##              TA
##              TG
##              11NAHT20
##              11NGHT20
##              11NAHT40PLUS
##              11NAHT40MINUS
##              11NGHT40PLUS  
##              11NGHT40MINUS
##              11NAHT40 (valid only when channel=11na)
##              11NGHT40 (valid only when channel=11ng)
##
##
## beaconint:   This is the beacon interval desired for this VAP.  Note
##              that this is system wide, and will override the current
##              beacon interval for ALL vaps.  You MUST also include the
##              RF command for this option.
##
## Examples:
##   Access Point with RF
##      makeVAP ap OpenAP 0:RF:6:
##   Access Point with RF, beacon interval of 400 ms
##      makeVAP ap OpenAP RF 400
##   Access Point w/o RF
##      makeVAP ap NormAP
##   WDS Root AP
##      makeVAP ap-wds RootAP RF
##   WDS Repeater (two commands)
##      makeVAP sta-wds RPTR RF
##      makeVAP ap-wds RPTR
##
###################################################################

. /etc/ath/apcfg

if [ "${1}" = "" ]; then
    echo "makeVAP usage"
    echo "makeVAP mode essid IFstr"
    echo
    echo "mode: [ap | ap-wds | sta | sta-wds | sta-fwd]"
    echo "essid: up to 32 character ESSID string"
    echo "RF: Include RF commands"
    echo "beaconint: Beacon interval, milliseconds"
    echo
    exit
fi

MODE=`echo $1 | cut -f 1 -d '-'`
SUB_MODE=`echo $1 | cut -f 2 -d '-'`

IFNUM=`echo $3 | cut -f 1 -d ':'`
RF=`echo $3 | cut -f 2 -d ':'`
PRI_CH=`echo $3 | cut -f 3 -d ':'`
CH_MODE=`echo $3 | cut -f 4 -d ':'`
#NickChou add 0=pureg 1=puren
ONLY_MODE=`echo $3 | cut -f 5 -d ':'`


if [ "${IFNUM}" != "0" -a "${IFNUM}" != "1" ]; then
    IFNUM=0
fi

R_SHORTGI=$SHORTGI
R_CWMMODE=$CWMMODE
R_AMPDUENABLE=$AMPDUENABLE
R_AMPDUFRAMES=$AMPDUFRAMES
R_AMPDULIMIT=$AMPDULIMIT
R_AMPDUMIN=$AMPDUMIN
R_TXCHAIN=$TX_CHAINMASK
R_RXCHAIN=$RX_CHAINMASK

ESSID=$2
BEACONINT=$4

##
## First, let's see if we have the modules loaded.  If not, call the
## rc.wlan script to load them
##

MODLIST=`lsmod | grep ath_pci`

if [ "${MODLIST}" = "" ]; then
	echo "Loading Wireless Modules in MakeVAP"
    /etc/rc.d/rc.wlan up

    ##
	## Check for bad return value.  If so, exit
	##

	if [ $? != 0 ]; then
	    exit 255
	fi
else
    echo "Wireless Modules already loaded"
fi

#echo Creating ${MODE} for "${ESSID}" on ${BRIDGE}

##
## Create the instance
##

	#NickChou add to set MAC address
	if [ "$IFNUM" = "0" ]; then
		#echo NickChou: makeVAP: wlan0_mac=${WLAN0_MAC}
		ifconfig wifi0 hw ether ${WLAN0_MAC}
	fi
	
	if [ "$IFNUM" = "1" ]; then	
		#echo NickChou: makeVAP: wlan0_mac=${WLAN1_MAC}
		ifconfig wifi1 hw ether ${WLAN1_MAC}
	fi
		
APNAME=`wlanconfig ath create wlandev wifi$IFNUM wlanmode ${MODE}`
APMODE="mode master"

##
## Enable WDS if selected
##

if [ "${SUB_MODE}" = "wds" ]; then
    iwpriv ${APNAME} wds 1
fi

##
## Disable Background Scan
##

iwpriv ${APNAME} bgscan 0

##
# set various debug modes
##

if [ "${DEBUGMODE}" = "" ]; then
    DEBUGMODE=0x100
fi

if [ "${HALDEBUG}" = "" ]; then
    HALDEBUG=0x0
fi

if [ "${ATHDEBUG}" = "" ]; then
    ATHDEBUG=0x0
fi

#iwpriv wifi$IFNUM HALDbg $HALDEBUG
iwpriv wifi$IFNUM ATHDebug $ATHDEBUG
iwpriv ${APNAME} dbgLVL $DEBUGMODE

##
## Operating Mode passed in through call.  Determine the frequeny, or if a 
## scan is required
##

if [ $PRI_CH = 11na -o $PRI_CH = 11ng ]; then
    FREQ=""
else
    FREQ="freq $PRI_CH"
fi

#####################################################################
## Check for RF command. If so, set the RF parameters, else do the
## simple cofiguration.
##

if [ "${RF}" = "RF" ]; then

    #
    # 11n configuration section
    # increase queue length
    #

    ifconfig ${APNAME} txqueuelen $TXQUEUELEN
    ifconfig wifi$IFNUM txqueuelen $TXQUEUELEN

    # turn on halfgi
    iwpriv ${APNAME} shortgi $R_SHORTGI

    #iwpriv ${APNAME} noedgech $NO_EDGE_CH

    iwpriv ${APNAME} mode $CH_MODE

    # NickChou add: check wmm
	if [ "${WMM_ENABLE}" = "" ]; then
		echo "NickChou: makeVAP: No set WMM"
	else		
		#echo "NickChou: makeVAP: WMM_ENABLE=${WMM_ENABLE}"
		if [ ${WMM_ENABLE} = 0 ]; then
			#echo "NickChou: Set WMM_ENABLE=0"
			iwpriv ${APNAME} wmm 0
		else	
			if [ ${WMM_ENABLE} = 1 ]; then
				#echo "NickChou: Set WMM_ENABLE=1"
				iwpriv ${APNAME} wmm 1
			fi
		fi	
	fi
    
    #
    # Check to see if we are in one of the 11NG bands that require
    # ANI processing
    #

    BAND=`echo $CH_MODE | grep 11NG`

    if [ "${BAND}" != "" ]; then
        iwpriv wifi$IFNUM ForBiasAuto 1
    fi

####################
####### TEMP WORKAROUND
####################

    PLUS=`echo $CH_MODE | grep PLUS`
    MINUS=`echo $CH_MODE | grep MINUS`

    if [ "${PLUS}" != "" ]; then
        iwpriv ${APNAME} extoffset 1
    fi
    if [ "${MINUS}" != "" ]; then
        iwpriv ${APNAME} extoffset -1
    fi

#######################

    #
    # Channel Width Mode
    # cwmmode 0 is static 20; cwmmode 1 is dyn 2040; cwmmode 2 is static 40
    #
    
    if [ $CH_MODE = 11NGHT20 -o $CH_MODE = 11NAHT20 ]; then
        iwpriv ${APNAME} cwmmode 0
    else
        iwpriv ${APNAME} cwmmode $R_CWMMODE
    fi

    #
    # Set Aggregation State
    #

    iwpriv wifi$IFNUM AMPDU $R_AMPDUENABLE

    # set number of sub-frames in an ampdu

    iwpriv wifi$IFNUM AMPDUFrames $R_AMPDUFRAMES

    # set ampdu limit

    iwpriv wifi$IFNUM AMPDULim $R_AMPDULIMIT
	#iwpriv ${APNAME} ampdumin $R_AMPDUMIN 
    
    # 'g'-only mode (no 'b' stations)
    #iwpriv ${APNAME} pureg $PUREG

    # 'n'-only mode (no legacy b/g/a stations)
    #iwpriv ${APNAME} puren $PUREN
    
# NickChou add: check 11n-only or 11g-only
	if [ "${ONLY_MODE}" = "" ]; then
#		#echo "NickChou: makeVAP: No set Pure mode"
		iwpriv ${APNAME} pureg 0
		iwpriv ${APNAME} puren 0
	else		
#		echo "NickChou: makeVAP: ONLY_MODE=${ONLY_MODE}"
		if [ ${ONLY_MODE} = 0 ]; then
			echo "NickChou: Set Pure G mode"
			iwpriv ${APNAME} pureg 1
		else	
			if [ ${ONLY_MODE} = 1 ]; then
				echo "NickChou: Set Pure N mode"
				iwpriv ${APNAME} puren 1
			fi
		fi	
	fi
    #
    # set SSID and frequency
    #
    iwconfig ${APNAME} essid "${ESSID}" ${APMODE} ${FREQ}

    #
    # If rate control is not auto, set the manual settings
    #
    
    if [ "${RATECTL}" != "auto" ]; then
        iwpriv ${APNAME} set11NRates $MANRATE
        iwpriv ${APNAME} set11NRetries $MANRETRIES
    fi

    #
    # Set the chain masks
    #

    if [ "${R_TXCHAIN}" != "" -a "${R_TXCHAIN}" != "0" ]; then
        iwpriv wifi$IFNUM txchainmask $R_TXCHAIN
	fi

    if [ "${R_RXCHAIN}" != ""  -a "${R_RXCHAIN}" != "0" ]; then
        iwpriv wifi$IFNUM rxchainmask $R_RXCHAIN
    fi

    #
    # An extra IE is provided for Intel interop
    #

    echo 1 > /proc/sys/dev/ath/htdupieenable

    #
    # This is where extra commands are executed.
    #

    $AP_EXTRA

	# NickChou Add, are they RF setting ??

	if [ "${HIDE_SSID}" != "" ]; then
    	iwpriv ${APNAME} hide_ssid ${HIDE_SSID}
    	#echo "NickChou: makeVAP: hide_ssid=${HIDE_SSID}"
	fi

	if [ "${DTIM_PERIOD}" != "" ]; then
	    iwpriv ${APNAME} dtim_period ${DTIM_PERIOD}
	    #echo "NickChou: makeVAP: dtim_period=${DTIM_PERIOD}"
	fi

	if [ "${RTS_THRESHOLD}" != "" ]; then
		iwconfig ${APNAME} rts ${RTS_THRESHOLD}
		#echo "NickChou: makeVAP: rts=${RTS_THRESHOLD}"
	fi

	if [ "${FRAGMENTATION}" != "" ]; then	
		iwconfig ${APNAME} frag ${FRAGMENTATION}
		#echo "NickChou: makeVAP: frag=${FRAGMENTATION}"
	fi

	if [ "${WLAN_PARTITION}" != "" ]; then	
		iwpriv ${APNAME} ap_bridge ${WLAN_PARTITION}
		#echo "NickChou: makeVAP: ap_bridge=${WLAN_PARTITION}"
	fi

    #echo NickChou: makeVAP: set the RF parameters: end

else
    ####
    # set SSID only
    ###

    iwpriv ${APNAME} mode ${CH_MODE}
    iwconfig ${APNAME} essid "${ESSID}" ${APMODE} ${FREQ}
fi

##
## Check for multiple VAPs.  If the VAP name is ath2 we assume we want the
## beacon interval to be 400 ms
##

if [ "${BEACONINT}" != "" ]; then
    #
    # Beacon interval was specified
    #

    iwpriv ${APNAME} bintval ${BEACONINT}
#    echo makeVAP ${APNAME} BINTVAL=${BINTVAL} BEACONINT=${BEACONINT}
fi

##
## Script Complete
##

echo makeVAP Created ${APNAME} : ssid=${ESSID} : mode=${MODE} 
