function wireless_create() {
local device=${1}
- local phy=$(phy_get ${2})
- local type=${3}
- local mac=${4}
-
assert isset device
- assert isset phy
- assert isset type
+ shift
+
+ local address
+ local phy
+ local type="managed"
+
+ while [ $# -gt 0 ]; do
+ case "${1}" in
+ --address=*)
+ address=$(cli_get_val ${1})
+ ;;
+ --phy=*)
+ phy=$(cli_get_val ${1})
+ phy=$(phy_get ${phy})
+ ;;
+ --type=*)
+ type=$(cli_get_val ${1})
- isset mac || mac=$(mac_generate)
+ # ap --> __ap
+ [ "${type}" = "ap" ] && type="__ap"
+ ;;
+ *)
+ error "Unrecognized argument: ${1}"
+ return ${EXIT_ERROR}
+ ;;
+ esac
+ shift
+ done
+ assert isoneof type ibss managed __ap
assert phy_exists ${phy}
- assert isoneof type managed __ap
+ isset address || address=$(mac_generate)
- iw phy ${phy} interface add ${device} type ${type}
+ cmd_quiet iw phy ${phy} interface add ${device} type ${type}
+ local ret=$?
- if device_exists ${device}; then
- device_set_address ${device} ${mac}
+ if [ ${ret} -eq ${EXIT_OK} ]; then
+ log DEBUG "created wireless device '${device}' (${type})"
+
+ if isset address; then
+ device_set_address ${device} ${address}
+ fi
+ else
+ log ERROR "could not create wireless device '${device}' (${type}): ${ret}"
fi
- device_set_up ${device}
+ return ${ret}
}
function wireless_remove() {
local device=${1}
+ assert isset device
- assert device_exists ${device}
+ if ! device_exists ${device}; then
+ return ${EXIT_OK}
+ fi
+ # Tear down the device (if necessary).
device_set_down ${device}
- iw dev ${device} del
+ # Remove it.
+ cmd_quiet iw dev ${device} del
+ local ret=$?
+
+ if [ ${ret} -eq ${EXIT_OK} ]; then
+ log DEBUG "removed wireless device '${device}'"
+ else
+ log ERROR "could not remove wireless device '${device}': ${ret}"
+ fi
+
+ return ${ret}
+}
+
+function wireless_channel_to_frequency() {
+ # http://en.wikipedia.org/wiki/List_of_WLAN_channels
+
+ local channel=${1}
+ assert isset channel
+
+ # Channel number must be positive.
+ assert [ "${channel}" -gt 0 ]
+
+ # 2.4 GHz band
+ case "${channel}" in
+ [123456789]|1[0123])
+ print "$(( 2407 + (${channel} * 5)))"
+ return ${EXIT_OK}
+ ;;
+ 14)
+ print "2484"
+ return ${EXIT_OK}
+ ;;
+ esac
+
+ # 5 GHz band
+ case "${channel}" in
+ 3[68]|4[02468]|5[26]|6[04]|10[048]|11[26]|12[048]|13[26]|14[09]|15[37]|16[15])
+ print "$(( 5000 + (${channel} * 5)))"
+ return ${EXIT_OK}
+ ;;
+ esac
+
+ return ${EXIT_ERROR}
}
function wireless_set_channel() {
local device=${1}
+ assert isset device
+
local channel=${2}
+ assert isset channel
+
+ device_exists ${device} || return ${EXIT_ERROR}
+ cmd_quiet iw dev ${device} set channel ${channel}
+}
+
+function wireless_ibss_join() {
+ local device=${1}
assert isset device
- assert device_exists ${device}
- assert isset channel
+ shift
+
+ local bssid
+ local essid
+ local frequency
+
+ while [ $# -gt 0 ]; do
+ case "${1}" in
+ --bssid=*)
+ bssid="$(cli_get_val ${1})"
+ ;;
+ --essid=*)
+ essid="$(cli_get_val ${1})"
+ ;;
+ --channel=*)
+ local channel="$(cli_get_val ${1})"
+
+ # Save the frequency of the channel instead
+ # of the channel itself.
+ if isset channel; then
+ frequency="$(wireless_channel_to_frequency ${channel})"
+ fi
+ ;;
+ esac
+ shift
+ done
+
+ # Check input.
+ assert ismac bssid
+ assert isset essid
+ assert isinteger frequency
+
+ # Set device up.
+ device_set_up "${device}"
+
+ log INFO "${device} joining ibss network: ${essid} (${bssid})"
+ cmd_quiet iw dev "${device}" ibss join "${essid}" \
+ "${frequency}" fixed-freq "${bssid}"
+}
+
+function wireless_ibss_leave() {
+ local device=${1}
+ assert isset device
+
+ log INFO "${device} leaving ibss network"
+ cmd_quiet iw dev "${device}" ibss leave
+}
+
+function wireless_is_radar_frequency() {
+ local frequency="${1}"
+ assert isset frequency
- iw dev ${device} set channel ${channel} $@
+ [[ ${frequency} -ge 5260 ]] && [[ ${frequency} -le 5700 ]]
}