#!/bin/sh
####################################################################
## makeVAP
##
## This script is used to create a Station instance (VAP).
##
## The form of the command is
##
## makeVAP <ESSID> <Channel_String>
##
## Where
##     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
##
##
## Examples:
##   STA with RF
##      makeVAP OpenAP 0:RF:6:11NGHT20
##
###################################################################

. ./apcfg

if [ "${1}" = "" ]; then
    echo "makeVAP usage"
    echo "makeVAP essid IFstr"
    echo
    echo "essid: up to 32 character ESSID string"
    echo "RF: Include RF commands"
    echo
    exit
fi

IFNUM=`echo $2 | cut -f 1 -d ':'`
RF=`echo $2 | cut -f 2 -d ':'`
PRI_CH=`echo $2 | cut -f 3 -d ':'`
CH_MODE=`echo $2 | cut -f 4 -d ':'`

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

ESSID=$1

echo Creating ${MODE} for ${ESSID}

##
## Create the instance
##

APNAME=`wlanconfig ath create wlandev wifi$IFNUM wlanmode sta`
APMODE="mode managed"

echo Added ${APNAME} ${APMODE}

##
## Disable Background Scan
##

iwpriv ${APNAME} bgscan 0

##
# set debug mode output
##

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

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 $SHORTGI

    iwpriv ${APNAME} mode $CH_MODE

    #
    # Check to see if we are in one of the 11NG bands that require
    # ANI processing
    #

    BAND=`echo $CH_MODE | grep 11NG`

####################
####### 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 ]; then
        iwpriv ${APNAME} cwmmode 0
    else
        iwpriv ${APNAME} cwmmode $CWMMODE
    fi

    #
    # Set Aggregation State
    #

    iwpriv wifi$IFNUM AMPDU $AMPDUENABLE

    # set number of sub-frames in an ampdu

    iwpriv wifi$IFNUM AMPDUFrames $AMPDUFRAMES

    # set ampdu limit

    iwpriv wifi$IFNUM AMPDULim $AMPDULIMIT
    
    #
    # 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
    #

    iwpriv wifi$IFNUM txchainmask $TX_CHAINMASK
    iwpriv wifi$IFNUM rxchainmask $RX_CHAINMASK

    #
    # An extra IE is provided for Intel interop
    #

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

    #
    # This is where extra commands are executed.
    #

    $AP_EXTRA

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

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

fi

##
## Script Complete
##

echo Created ${APNAME} mode ${MODE} for ${ESSID}
