#!/bin/sh

IWPRIV=iwpriv

usage()
{
    cat <<EOF
Usage: prism2_param <interface> <parameter> [value]

Wrapper for setting prism2.o parameters using iwpriv and prism2_param ioctl.
This wrapper converts the given text parameter (see list below) to
corresponding prism2_param ioctl() value and uses iwpriv to set the given
value.

If value argument is not given, the current value is shown (if available).

interface:
	interface name for the wireless LAN device to be used (e.g., wlan0)

parameter:
	txratectrl:
		0 = use host driver based TX rate control (default),
		1 = use f/w based TX rate control
	beacon_int: beacon interval (1 = 1024 usec)
	dtim_period: DTIM period, i.e., number of beacon intervals between
		successive delivery traffic identification maps (DTIMs),
		used for power saving and multicast/broadcast delivery
	pseudo_ibss:
		0 = use IEEE 802.11 IBSS mode (default),
		1 = use pseudo adhoc mode (no management frames)
	other_ap_policy:
		0 = skip all beacons
		1 = accept beacons with our SSID
		2 = accept beacons from all APs
		3 = accept all beacons (even from IBSS)
	dump: set RX/TX/TXEXC debug dump header bitfield
		0 = do not dump frame headers
		1 = dump RX frame headers
		2 = dump TX frame headers
		4 = dump TX error frame headers
		(these values can be bitwise ORed; e.g. 3 = both RX and TX)
	ap_max_inactivity: Time (in seconds) after which inactive stations
		can be removed from AP's station list
	ap_bridge_packets:
		0 = do not bridge packets between associated stations, i.e.,
			just pass them to upper layers for handling
		1 = bridge packets directly between associated stations, i.e.,
			upper layers do not even see these packets
	ap_nullfunc_ack:
		0 = let station firmware take care of data::nullfunc ACKs
		1 = send "extra" ACKs for data::nullfunc frames to workaround
			problems with stations using PS mode
		(default 1 if STA f/w version is 0.8.0, otherwise 0)
	max_wds: maximum number of allowed WDS connections (default 16)
	autom_ap_wds:
		0 = add WDS connections manually
		1 = add WDS connections automatically to all recorded APs
			(based on other_ap_policy)
	ap_auth_algs: allowed authentication algorithms
		0 = none (no authentication will succeed)
		1 = only open
		2 = only shared key
		3 = open or shared key (default)
	monitor_allow_fcserr:
		0 = drop frames with FCS errors in monitor mode
		1 = pass also frames with FCS errors
	host_encrypt:
		0 = do not use host encryption unless in Host AP mode
		1 = use host encryption in all modes
	host_decrypt:
		0 = use WLAN card firmware to decrypt frames
		1 = use host driver to decrypt frames
	bus_master_threshold_rx:
		packet length threshold for using PCI bus master on RX
		(only used with hostap_pci.o and if PRISM2_BUS_MASTER is set)
	bus_master_threshold_tx:
		packet length threshold for using PCI bus master on TX
		(only used with hostap_pci.o and if PRISM2_BUS_MASTER is set)
	host_roaming:
		0 = use station firmware for roaming decision (default)
		1 = use host driver roaming decision
	bcrx_sta_key:
		0 = use station specific key (WEP key mapping) to override
		    default keys only for RX frames sent to unicast address
		    ("individual RA") (default)
		1 = use station specific key also with broadcast RX frames
		    (this is against IEEE 802.11, but makes it easier to use
		    different keys with stations that do not support WEP key
		    mapping)
	ieee_802_1x:
		0 = do not use IEEE 802.1X specific functionality (default)
		1 = use IEEE 802.1X: require 802.1X auth, filter EAPOL packets
	antsel_tx:
		0 = do not touch TX AntSel, i.e., use card default (default)
		1 = use antenna diversity
		2 = force TX AntSel pin low
		3 = force TX AntSel pin high
	antsel_rx:
		0 = do not touch RX AntSel, i.e., use card default (default)
		1 = use antenna diversity
		2 = force RX AntSel pin low
		3 = force RX AntSel pin high
	monitor_type:
		0 = IEEE 802.11 headers (ARPHRD_IEEE80211)
		1 = Prism2 + IEEE 802.11 headers (ARPHRD_IEEE80211_PRISM)
	wds_type: WDS type bitfield
		0 = options disabled (default)
		1 = use broadcast RA (WDS frame destination) for broadcast and
			multicast frames
		2 = use AP client mode in 'Managed mode'

	Following parameters are for debug use only;
	do not use unless you are sure what you are doing!

	ptype: port type
	alc: 0=disabled ALC, 1=enable ALC
	txpower: TX power (normally, 'iwconfig wlan# txpower <arg>' is used)
EOF
    exit 1;
}


if [ $# != 3 -a $# != 2 ]; then
	usage
fi

if ! $IWPRIV $1 | grep -q prism2_param; then
	echo "Interface '$1' does not seem to support prism2_param command."
	echo "Run '$0' without any arguments for usage information."
	exit 2
fi

case $2 in
	ptype) param=1 ;;
	txratectrl) param=2 ;;
	beacon_int) param=3 ;;
	pseudo_ibss) param=4 ;;
	alc) param=5 ;;
	txpower) param=6 ;;
	dump) param=7 ;;
	other_ap_policy) param=8 ;;
	ap_max_inactivity) param=9 ;;
	ap_bridge_packets) param=10 ;;
	dtim_period) param=11 ;;
	ap_nullfunc_ack) param=12 ;;
	max_wds) param=13 ;;
	autom_ap_wds) param=14 ;;
	ap_auth_algs) param=15 ;;
	monitor_allow_fcserr) param=16 ;;
	host_encrypt) param=17 ;;
	host_decrypt) param=18 ;;
	bus_master_threshold_rx) param=19 ;;
	bus_master_threshold_tx) param=20 ;;
	host_roaming) param=21 ;;
	bcrx_sta_key) param=22 ;;
	ieee_802_1x) param=23 ;;
	antsel_tx) param=24 ;;
	antsel_rx) param=25 ;;
	monitor_type) param=26 ;;
	wds_type) param=27 ;;

	*)
		echo "Unknown parameter '$2'."
		echo "Run '$0' without any arguments for usage information."
		exit 3
esac

if [ $# = 3 ]; then

	$IWPRIV $1 prism2_param $param $3

else

	$IWPRIV $1 getprism2_param $param

fi
