Developing lightweight computation at the DSG edge

Commit 034d72e9 authored by p4u's avatar p4u
Browse files

New wifi API functions

parent 15d3ae11
...@@ -27,53 +27,157 @@ iwinfo = require "iwinfo" ...@@ -27,53 +27,157 @@ iwinfo = require "iwinfo"
wifi = {} wifi = {}
wifi.info = {} wifi.info = {}
wifi.template = {}
local _TEMPLATES_DIR="/etc/qmp/templates/wifi"
function wifi.apply(dev) function wifi.apply(dev)
debug.set_namespace("WiFi") debug.set_namespace("WiFi")
debug.logger(util.printf("Executing wifi.apply(%s)",dev)) debug.logger(util.printf("Executing wifi.apply(%s)",dev))
local index = model.get_indextype("wireless","device",dev)[1] wdev,msg = wifi.info.config(dev)
local wdev = model.get_type("wireless",index) if not wdev then
if msg == "Device configured as none" then
if wdev == nil then
debug.logger(util.printf("Device %s cannot be found!",dev))
end
if wdev.mode == "none" then
debug.logger("Device "..dev.." mode is none, qMp won't configure it") debug.logger("Device "..dev.." mode is none, qMp won't configure it")
return true return true
else
debug.logger(msg)
return false
end
end end
debug.logger(util.printf("From model: index=%s wdev=%s",index,wdev)) debug.logger(util.printf("Mode: %s | Channel: %s | Channel mode: %s | Country: %s | BSSID: %s | ESSID: %s | TXpower: %s | MAC: %s",
wdev.mode,wdev.channel,wdev.channel_mode,wdev.country,wdev.bssid,wdev.name,wdev.txpower,wdev.mac))
-- Getting all parameters and checking no one is nil local device = wifi.template.device(wdev)
local mode = wdev.mode local iface = wifi.template.iface(wdev)
local channel = util.replace(wdev.channel,{'+','-'},'') if not (device and iface) then
local channel_mode = util.replace(wdev.channel,"[0-9]",'') debug.logger("Cannot get device/iface template information")
local name = wdev.name
local mac = wdev.mac
local device = wdev.device
if not (mode and channel and channel_mode and name and mac and device) then
debug.logger("Some missing parameter, cannot apply")
return false return false
end end
end
local template = nil function wifi.template.filename(wdev)
debug.logger("Channel is " .. channel)
debug.logger("Channel mode is " .. channel_mode)
-- chnanel_mode: HT40 = 10+/- | 802.11b = 10b | 802.11ag or HT20 = 10 -- chnanel_mode: HT40 = 10+/- | 802.11b = 10b | 802.11ag or HT20 = 10
if channel_mode ~= "b" then -- mode = adhoc | ap | client
if #channel_mode ~= 0 and wifi.info.modes(dev).n then local template = {}
template = mode .. "-n"
if wdev.channel_mode ~= "b" then
if wifi.info.modes(wdev.device).n then
template.device = "device."..wdev.driver .. "-n"
else else
template = mode template.device = "device."..wdev.driver
end end
else else
template = mode.."-b" template.device = "device."..wdev.driver.."-b"
end end
debug.logger("Selected template is " .. template)
template.iface = "iface."..wdev.mode
debug.logger("Selected template is: " .. template.device .. " | " .. template.iface)
return template
end
function wifi.template.device(wdev)
local template = wifi.template.filename(wdev).device
local st,fd = pcall(io.open,_TEMPLATES_DIR.."/"..template,"r")
if not st then
debug.logger("Cannot open file ".._TEMPLATES_DIR.."/"..template)
return false
end
local t = fd:read("*all")
fd:close()
if #wdev.channel > 0 then t = util.replace(t,'#QMP_CHANNEL',wdev.channel) end
if #wdev.txpower > 0 then t = util.replace(t,'#QMP_TXPOWER',wdev.txpower) end
t = util.replace(t,'#QMP_MAC',wdev.mac)
t = util.replace(t,'#QMP_COUNTRY',wdev.country)
t = util.replace(t,'#QMP_DEVICE',wdev.device)
if wdev.channel_mode == 'b' then
t = util.replace(t,'#QMP_HWMODE','11b')
else
t = util.replace(t,'#QMP_HWMODE','auto')
local ht
if wifi.info.modes(wdev.device).n then
if wdev.channel_mode == "+" or wdev.channel_mode == "-" then
ht = "40"
else
ht = "20"
end
t = util.replace(t,'#QMP_HTMODE','ht'..ht..wdev.channel_mode)
t = util.replace(t,'#QMP_HT',ht)
end
end
if util.find(t,'#QMP_') then
debug.logger("CRITICAL: udefined template word which start with #QMP_ but it is not reconigzed")
return false
end
return t
end
function wifi.template.iface(wdev)
local template = wifi.template.filename(wdev).iface
local st,fd = pcall(io.open,_TEMPLATES_DIR.."/"..template,"r")
if not st then
debug.logger("Cannot open file ".._TEMPLATES_DIR.."/"..template)
return false
end
local t = fd:read("*all")
fd:close()
t = util.replace(t,'#QMP_DEVICE',wdev.device)
t = util.replace(t,'#QMP_SSID',wdev.name)
t = util.replace(t,'#QMP_BSSID',wdev.bssid)
if util.find(t,'#QMP_') then
debug.logger("CRITICAL: udefined template word which start with #QMP_ but it is not reconigzed")
return false
end
return t
end
function wifi.info.config(dev)
local index = model.get_indextype("wireless","device",dev)[1]
if index == nil then
return false,"Device not found"
end
local wdev = model.get_type("wireless",index)
if wdev == nil then
return false,"Device not found"
end
if wdev.mode == "none" then
return false,"Device configured as none"
end
local devconfig = {}
-- Getting all parameters and checking no one is nil
devconfig.driver = model.get("wireless","driver") or "nil"
devconfig.bssid = model.get("wireless","bssid") or "nil"
devconfig.country = model.get("wireless","country") or "nil"
devconfig.mode = wdev.mode or "nil"
devconfig.channel = util.replace(wdev.channel,{'+','-'},'') or "nil"
devconfig.channel_mode = util.replace(wdev.channel,"[0-9]",'') or ""
devconfig.name = wdev.name or "nil"
devconfig.mac = wdev.mac or "nil"
devconfig.txpower = wdev.txpower or ""
devconfig.device = wdev.device or "nil"
local i,v
for i,v in pairs(devconfig) do
if v == "nil" then
return false,"missing parameter "..i.." in device configuration"
end
end
return devconfig
end end
function wifi.info.modes(dev) function wifi.info.modes(dev)
......
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