Developing lightweight computation at the DSG edge

Commit eaa4a3fa authored by hakais's avatar hakais
Browse files

New script qmpinfo, and some changes and improves in autoconf system

parent da333f4a
......@@ -24,14 +24,12 @@ config 'qmp' 'wireless'
option 'bssid' '02:CA:FF:EE:BA:BE'
config 'wireless'
option 'channel' '44'
option 'mode' 'adhoc'
option 'name' 'qmp'
config 'wireless'
option 'channel' '10'
option 'mode' 'adhoc'
option 'name' 'qmp'
option 'mode' 'ap'
option 'name' 'qmp.cat'
......
#!/bin/sh
#######################
# UCI related commands
#######################
qmp_uci_get() {
u="$(uci -q get qmp.$1)"
r=$?
......@@ -60,23 +64,54 @@ qmp_uci_import() {
return $?
}
##################################
# Log and errors related commnads
##################################
# Exit from execution and shows an error
# qmp_error The device is burning
qmp_error() {
logger -s -t qMp "ERROR: $1"
logger -s -t qMp "ERROR: $@"
exit 1
}
# Send info to system log
# qmp_log qMp is the best
qmp_log() {
logger -s -t qMp "$@"
}
#######################################
# Networking and Wifi related commands
#######################################
# Returns the names of the wifi devices from the system
qmp_get_wifi_devices() {
echo "$(ip link | grep -E ": (wifi|wlan).: "| cut -d: -f2)"
}
# Returns the MAC address of the wifi devices
qmp_get_wifi_mac_devices() {
echo "$(ip link | grep -A1 -E ": (wifi|wlan).: " | grep link | cut -d' ' -f6)"
}
# Returns the device name that corresponds to the MAC address
# qmp_get_dev_from_mac 00:22:11:33:44:55
qmp_get_dev_from_mac() {
ip l | grep $1 -i -B1 | grep -v \@ | grep -v ether | awk '{print $2}' | tr -d :
}
#########################
# Other kind of commands
#########################
# Print the content of the parameters in reverse order (separed by spaces)
reverse_order() {
echo "$@" | awk '{for (i=NF; i>0; i--) printf("%s ",$i);print ""}'
}
# Print the output of the command parameter in reverse order (separed by lines)
qmp_tac() {
$@ | awk '{a[NR]=$0} END {for(i=NR;i>0;i--)print a[i]}'
}
......@@ -5,6 +5,7 @@ OWRT_WIRELESS_CONFIG="/etc/config/wireless"
TEMPLATE_BASE="$QMP_PATH/templates/wireless" # followed by .driver.mode (wireless.mac80211.adhoc)
WIFI_DEFAULT_CONFIG="$QMP_PATH/templates/wireless.default.config"
TMP="/tmp"
QMPINFO="/etc/qmp/qmpinfo"
#Importing files
. $QMP_PATH/qmp_common.sh
......@@ -71,12 +72,19 @@ qmp_configure_wifi_device() {
id=$1
device=$2
#Checking if device is configured as "none"
# checking if device is configured as "none"
mode="$(qmp_uci_get @wireless[$id].mode)"
[ "$mode" == "none" ] && { echo "Interface $device disabled by qmp system"; return 0; }
[ "$mode" == "none" ] && { echo "Interface $device disabled by qmp system"; return; }
# spliting channel in channel number and ht40 mode
channel_raw="$(qmp_uci_get @wireless[$id].channel)"
channel="$(echo $channel_raw | tr -d +-)"
# is ht40 (802.11n) enabled?
ht40="$(echo $channel_raw | tr -d [0-9])"
[ ! -z "$ht40" ] && { mode="${mode}-n"; htmode="HT40$ht40"; }
mac="$(qmp_uci_get @wireless[$id].mac)"
channel="$(qmp_uci_get @wireless[$id].channel)"
name="$(qmp_uci_get @wireless[$id].name)"
driver="$(qmp_uci_get wireless.driver)"
country="$(qmp_uci_get wireless.country)"
......@@ -103,6 +111,7 @@ qmp_configure_wifi_device() {
-e s/"#QMP_CHANNEL"/"$channel"/ \
-e s/"#QMP_COUNTRY"/"$country"/ \
-e s/"#QMP_SSID"/"$name"/ \
-e s/"#QMP_HTMODE"/"$htmode"/ \
-e s/"#QMP_BSSID"/"$bssid"/ \
-e s/"#QMP_INDEX"/"$index"/ \
-e s/"#QMP_MODE"/"$mode"/ > $TMP/qmp_wireless_temp
......@@ -141,9 +150,70 @@ qmp_configure_wifi() {
echo "Done. All devices configured according qmp configuration"
}
#This function returns the default values
# first parameter is always what are you asking for (mode, channel, name,...)
# second one is device name, only needed by mode and channel
# third one is configured mode, only needed by chanel
qmp_wifi_get_default() {
[ ! -f "$WIFI_DEFAULT_CONFIG" ] && qmp_error "Template not found $WIFI_DEFAULT_CONFIG"
cat $WIFI_DEFAULT_CONFIG | grep $1 | cut -d' ' -f2
what="$1"
device="$2"
# MODE
# default mode for the second card (index=1) is AP
# the rest are ad-hoc
if [ "$what" == "mode" ]; then
index=$(echo $device | tr -d [A-z])
if [ $index -eq 1 ]; then
echo "ap"
else
echo "adhoc"
fi
# CHANNEL
# Default channel depends on the card and in configured mode
# Highest channel -> adhoc or not-configured
# Lower channel -> ap
elif [ "$what" == "channel" ]; then
[ -z "$device" ] && qmp_error "Device not found?"
mode="$3"
# with index var we are using the devices in differents channels
index=$(echo $device | tr -d [A-z])
index=$(( $index * 2 ))
# QMPINFO returns a list of avaiable channels in this format: 130 ht40+ adhoc
[ "$mode" == "adhoc" ] || [ -z "$mode" ] && channel_info="$(qmp_tac $QMPINFO channels $device | grep adhoc | awk NR==$index+1)"
[ "$mode" == "ap" ] && channel_info="$($QMPINFO channels $device | awk NR==$index+1 )"
# if there is some problem, channel 6 is used
if [ -z "$channel_info" ]; then
qmp_log "Warning, not usable channels found in device $device "
[ "$1" == "channel" ] && echo "6"
return
fi
# let's see if we can use ht40 mode
# if it is avaiable, channel must be configured with + or - symbol
mode_n="$($QMPINFO modes $2 | grep n)"
channel="$(echo $channel_info | cut -d' ' -f1)"
if [ ! -z "$mode_n" ]; then
ht40="$(echo $channel_info | cut -d' ' -f2)"
[ "$ht40" == "ht40+" ] && channel="${channel}+"
[ "$ht40" == "ht40-" ] && channel="${channel}-"
fi
echo "$channel"
# REST OF DEFAULT VAULES
# The rest of default values are taken from the template
else
[ ! -f "$WIFI_DEFAULT_CONFIG" ] && qmp_error "Template not found $WIFI_DEFAULT_CONFIG"
cat $WIFI_DEFAULT_CONFIG | grep $what | cut -d' ' -f2
fi
}
qmp_configure_wifi_initial() {
......@@ -161,12 +231,14 @@ qmp_configure_wifi_initial() {
if [ "$configured_mac" == "$m" ]; then
#If we found configured device, we are going to check all needed parameters
found=1
device="$(qmp_get_dev_from_mac $m)"
id_configured="$id_configured $j"
echo "Found configured device: $m"
[ -z "$(qmp_uci_get @wireless[$j].channel)" ] && qmp_uci_set @wireless[$j].channel $(qmp_wifi_get_default channel)
[ -z "$(qmp_uci_get @wireless[$j].mode)" ] && qmp_uci_set @wireless[$j].mode $(qmp_wifi_get_default mode)
[ -z "$(qmp_uci_get @wireless[$j].mode)" ] && qmp_uci_set @wireless[$j].mode $(qmp_wifi_get_default mode $device)
mode="$(qmp_uci_get @wireless[$j].mode)"
[ -z "$(qmp_uci_get @wireless[$j].channel)" ] && qmp_uci_set @wireless[$j].channel $(qmp_wifi_get_default channel $device $mode)
[ -z "$(qmp_uci_get @wireless[$j].name)" ] && qmp_uci_set @wireless[$j].name $(qmp_wifi_get_default name)
qmp_uci_set @wireless[$j].device $(qmp_get_dev_from_mac $m)
qmp_uci_set @wireless[$j].device $device
break
fi
j=$(( $j + 1 ))
......@@ -177,17 +249,18 @@ qmp_configure_wifi_initial() {
#Configuring devices not found before
for m in $to_configure; do
echo "Configuring device $m"
device=$(qmp_get_dev_from_mac $m)
echo "Configuring device: $device | $m"
#Looking for a free slot to put new configuration
j=0
while [ ! -z "$(echo $id_configured | grep $j)" ]; do j=$(( $j +1 )); done
#Now we have a free slot, let's go to configure device
[ -z "$(qmp_uci_get @wireless[$j])" ] && qmp_uci_add wireless
[ -z "$(qmp_uci_get @wireless[$j].channel)" ] && qmp_uci_set @wireless[$j].channel $(qmp_wifi_get_default channel)
[ -z "$(qmp_uci_get @wireless[$j].mode)" ] && qmp_uci_set @wireless[$j].mode $(qmp_wifi_get_default mode)
[ -z "$(qmp_uci_get @wireless[$j].name)" ] && qmp_uci_set @wireless[$j].name $(qmp_wifi_get_default name)
[ -z "$(qmp_uci_get @wireless[$j].channel)" ] && qmp_uci_set @wireless[$j].channel $(qmp_wifi_get_default channel $device)
[ -z "$(qmp_uci_get @wireless[$j].mode)" ] && qmp_uci_set @wireless[$j].mode $(qmp_wifi_get_default mode $device)
[ -z "$(qmp_uci_get @wireless[$j].name)" ] && qmp_uci_set @wireless[$j].name $(qmp_wifi_get_default name $device)
qmp_uci_set @wireless[$j].mac $m
qmp_uci_set @wireless[$j].device $(qmp_get_dev_from_mac $m)
qmp_uci_set @wireless[$j].device $device
id_configured="$id_configured $j"
done
......
#!/usr/bin/lua
package.path = package.path .. ";/etc/qmp/?.lua"
qmp = require "qmpinfo"
function print_help()
print("qMpinfo -> a dummy lua script to get some information about the wireless devices")
print("Usage: qmpinfo <question> <device>")
print("Possible questions:")
print(" modes: supported modes by the wireless card")
print(" channels: supported channels and supported HT40 type (+/-) ")
print("")
end
function print_modes()
local data = qmp.get_modes(dev)
local output = ""
if data.b then output = output .. "b " end
if data.g then output = output .. "g " end
if data.a then output = output .. "a " end
if data.n then output = output .. "n " end
print(output)
end
function print_channels()
local partial
local output = ""
local data = qmp.get_channels(dev)
for _,d in ipairs(data) do
partial = d.channel
if d.ht40p then partial = partial .. " ht40+" end
if d.ht40m then partial = partial .. " ht40-" end
if d.adhoc then partial = partial .. " adhoc" end
output = output .. partial .. "\n"
end
print(output)
end
if #arg ~= 2 then
print_help()
os.exit(1)
end
local question = arg[1]
dev = arg[2]
if question == "modes" then
print_modes()
elseif question == "channels" then
print_channels()
else
print_help()
end
#!/usr/bin/lua
iwinfo = require "iwinfo"
qmpinfo = {}
function qmpinfo.get_modes(dev)
local iw = iwinfo[iwinfo.type(dev)]
local ma = "" -- modes avaiable
return iw.hwmodelist(dev)
end
function qmpinfo.get_channels(dev)
local iw = iwinfo[iwinfo.type(dev)]
local freqs = iw.freqlist(dev) --freqs list
local c -- current channel
local nc = 0 -- next channel
local pc = 0 -- previous channel
local clist = {} -- output channel list
local adhoc
for i,f in ipairs(freqs) do
c = f.channel
ch = {}
ch.channel = c
ch.ht40p = false
ch.ht40m = false
ch.adhoc = false
-- 2.4Ghz band
if c < 15 then
if c < 4 then
ch.ht40p = true
ch.adhoc = true
elseif c < 10 then
ch.ht40m = true
ch.ht40p = true
ch.adhoc = true
else
ch.ht40m = true
ch.adhoc = true
end
-- 5Ghz band
elseif c > 14 then
if #freqs == i then nc = nil
else nc = freqs[i+1].channel
end
if i == 1 then pc = nil
else pc = freqs[i-1].channel
end
if nc ~= nil and nc-c == 4 then
ch.ht40p = true
end
if pc ~= nil and c-pc == 4 then
ch.ht40m = true
end
adhoc = os.execute("iw list | grep \"no IBSS\" | grep -v disabled | grep -q " .. f.mhz .. " 2>/dev/null")
if adhoc ~= 0 then
ch.adhoc = true
end
end
table.insert(clist,ch)
end
return clist
end
return qmpinfo
driver mac80211
country UZ
bssid 02:CA:FF:EE:BA:BE
channel 1
mode adhoc
name qmp
......@@ -8,7 +8,7 @@ wireless.#QMP_DEVICE.rxantenna=0
wireless.#QMP_DEVICE.country=#QMP_COUNTRY
wireless.#QMP_DEVICE.outdoor=1
wireless.#QMP_DEVICE.hwmode=auto
wireless.#QMP_DEVICE.htmode=HT40+
wireless.#QMP_DEVICE.htmode=#QMP_HTMODE
wireless.@wifi-iface[#QMP_INDEX].device=#QMP_DEVICE
wireless.@wifi-iface[#QMP_INDEX].mode=adhoc
wireless.@wifi-iface[#QMP_INDEX].ssid=#QMP_SSID
......
......@@ -8,7 +8,7 @@ wireless.#QMP_DEVICE.rxantenna=0
wireless.#QMP_DEVICE.country=#QMP_COUNTRY
wireless.#QMP_DEVICE.outdoor=1
wireless.#QMP_DEVICE.hwmode=auto
wireless.#QMP_DEVICE.htmode=HT40+
wireless.#QMP_DEVICE.htmode=#QMP_HTMODE
wireless.@wifi-iface[#QMP_INDEX].mode=ap
wireless.@wifi-iface[#QMP_INDEX].ssid=#QMP_SSID
wireless.@wifi-iface[#QMP_INDEX].encryption=none
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment