2013-07-08 13:34:27 +02:00
|
|
|
local l = require("logger")
|
2013-07-08 16:53:45 +02:00
|
|
|
local u = require("util.utils")
|
|
|
|
local netconf = require("network.netconfig")
|
|
|
|
local wifi = require("network.wlanconfig")
|
|
|
|
local ResponseClass = require("rest.response")
|
2013-07-08 13:34:27 +02:00
|
|
|
|
|
|
|
local M = {}
|
|
|
|
|
|
|
|
M.isApi = true
|
|
|
|
|
|
|
|
function M._global(d)
|
2013-07-08 16:53:45 +02:00
|
|
|
local r = ResponseClass.new(d)
|
|
|
|
r:setError("not implemented")
|
|
|
|
return r
|
2013-07-08 13:34:27 +02:00
|
|
|
end
|
|
|
|
|
2013-07-08 16:53:45 +02:00
|
|
|
--accepts API argument 'nofilter'(bool) to disable filtering of APs and 'self'
|
|
|
|
function M.available(d)
|
|
|
|
local r = ResponseClass.new(d)
|
|
|
|
local noFilter = u.toboolean(d:get("nofilter"))
|
|
|
|
local sr = wifi.getScanInfo()
|
|
|
|
local si, se
|
2013-07-08 13:34:27 +02:00
|
|
|
|
2013-07-08 16:53:45 +02:00
|
|
|
if sr and #sr > 0 then
|
|
|
|
r:setSuccess("")
|
|
|
|
local netInfoList = {}
|
|
|
|
for _, se in ipairs(sr) do
|
|
|
|
if noFilter or se.mode ~= "ap" and se.ssid ~= wifi.AP_SSID then
|
|
|
|
local netInfo = {}
|
|
|
|
|
|
|
|
netInfo["ssid"] = se.ssid
|
|
|
|
netInfo["bssid"] = se.bssid
|
|
|
|
netInfo["channel"] = se.channel
|
|
|
|
netInfo["mode"] = wifi.mapDeviceMode(se.mode)
|
|
|
|
netInfo["encryption"] = wifi.mapEncryptionType(se.encryption)
|
|
|
|
netInfo["signal"] = se.signal
|
|
|
|
netInfo["quality"] = se.quality
|
|
|
|
netInfo["quality_max"] = se.quality_max
|
|
|
|
--netInfo["raw"] = l:dump(se) --TEMP for debugging only
|
|
|
|
|
|
|
|
table.insert(netInfoList, netInfo)
|
2013-07-08 13:34:27 +02:00
|
|
|
end
|
|
|
|
end
|
2013-07-08 16:53:45 +02:00
|
|
|
r:addData("count", #netInfoList)
|
|
|
|
r:addData("networks", netInfoList)
|
|
|
|
else
|
2013-07-08 19:02:20 +02:00
|
|
|
r:setFail("No scan results or scanning not possible")
|
2013-07-08 16:53:45 +02:00
|
|
|
end
|
2013-07-08 13:34:27 +02:00
|
|
|
|
2013-07-08 16:53:45 +02:00
|
|
|
return r
|
|
|
|
end
|
|
|
|
|
|
|
|
--accepts API argument 'nofilter'(bool) to disable filtering of APs and 'self'
|
|
|
|
function M.known(d)
|
|
|
|
local r = ResponseClass.new(d)
|
|
|
|
local noFilter = u.toboolean(d:get("nofilter"))
|
|
|
|
|
|
|
|
r:setSuccess()
|
|
|
|
local netInfoList = {}
|
|
|
|
for _, net in ipairs(wifi.getConfigs()) do
|
|
|
|
if noFilter or net.mode == "sta" then
|
|
|
|
local netInfo = {}
|
|
|
|
netInfo["ssid"] = net.ssid
|
|
|
|
netInfo["bssid"] = net.bssid or ""
|
|
|
|
netInfo["channel"] = net.channel or ""
|
|
|
|
netInfo["encryption"] = net.encryption
|
2013-07-08 18:12:05 +02:00
|
|
|
--netInfo["raw"] = l:dump(net) --TEMP for debugging only
|
2013-07-08 16:53:45 +02:00
|
|
|
table.insert(netInfoList, netInfo)
|
2013-07-08 13:34:27 +02:00
|
|
|
end
|
2013-07-08 16:53:45 +02:00
|
|
|
end
|
|
|
|
r:addData("count", #netInfoList)
|
|
|
|
r:addData("networks", netInfoList)
|
|
|
|
|
|
|
|
return r
|
|
|
|
end
|
|
|
|
|
|
|
|
function M.state(d)
|
|
|
|
local r = ResponseClass.new(d)
|
|
|
|
local ds = wifi.getDeviceState()
|
|
|
|
|
|
|
|
r:setSuccess()
|
|
|
|
r:addData("ssid", ds.ssid or "")
|
|
|
|
r:addData("bssid", ds.bssid or "")
|
|
|
|
r:addData("channel", ds.channel or "")
|
|
|
|
r:addData("mode", ds.mode)
|
2013-07-08 18:12:05 +02:00
|
|
|
r:addData("encryption", ds.encryption)
|
|
|
|
r:addData("quality", ds.quality)
|
|
|
|
r:addData("quality_max", ds.quality_max)
|
|
|
|
r:addData("txpower", ds.txpower)
|
|
|
|
r:addData("signal", ds.signal)
|
|
|
|
r:addData("noise", ds.noise)
|
|
|
|
--r:addData("raw", l:dump(ds)) --TEMP for debugging only
|
2013-07-08 16:53:45 +02:00
|
|
|
|
|
|
|
return r
|
|
|
|
end
|
|
|
|
|
|
|
|
--UNTESTED
|
|
|
|
--requires ssid(string), accepts phrase(string), recreate(bool)
|
|
|
|
function M.assoc(d)
|
|
|
|
local r = ResponseClass.new(d)
|
|
|
|
local argSsid = d:get("ssid")
|
|
|
|
local argPhrase = d:get("phrase")
|
|
|
|
local argRecreate = d:get("recreate")
|
|
|
|
|
|
|
|
if argSsid == nil or argSsid == "" then
|
|
|
|
r:setError("missing ssid argument")
|
|
|
|
return r
|
|
|
|
end
|
|
|
|
|
|
|
|
local cfg = nil
|
|
|
|
for _, net in ipairs(wifi.getConfigs()) do
|
|
|
|
if net.mode ~= "ap" and net.ssid == argSsid then
|
|
|
|
cfg = net
|
|
|
|
break
|
2013-07-08 13:34:27 +02:00
|
|
|
end
|
2013-07-08 16:53:45 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
if cfg == nil or argRecreate ~= nil then
|
|
|
|
local scanResult = wifi.getScanInfo(argSsid)
|
|
|
|
if scanResult ~= nil then
|
|
|
|
wifi.createConfigFromScanInfo(scanResult, argPhrase)
|
2013-07-08 13:34:27 +02:00
|
|
|
else
|
2013-07-08 16:53:45 +02:00
|
|
|
--check for error
|
2013-07-08 19:02:20 +02:00
|
|
|
r:setFail("no wireless network with requested SSID is available")
|
2013-07-08 16:53:45 +02:00
|
|
|
r:addData("ssid", argSsid)
|
2013-07-08 13:34:27 +02:00
|
|
|
end
|
2013-07-08 16:53:45 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
wifi.activateConfig(argSsid)
|
|
|
|
netconf.switchConfiguration{ wifiiface="add", apnet="rm", staticaddr="rm", dhcppool="rm", wwwredir="rm", dnsredir="rm", wwwcaptive="rm", wireless="reload" }
|
|
|
|
r:setSuccess("wlan associated")
|
|
|
|
r:addData("ssid", argSsid)
|
|
|
|
|
|
|
|
return r
|
|
|
|
end
|
|
|
|
|
|
|
|
--UNTESTED
|
|
|
|
function M.disassoc(d)
|
|
|
|
local r = ResponseClass.new(d)
|
|
|
|
|
|
|
|
wifi.activateConfig()
|
|
|
|
local rv = wifi.restart()
|
|
|
|
r:setSuccess("all wireless networks deactivated")
|
|
|
|
r:addData("wifi_restart_result", rv)
|
|
|
|
|
|
|
|
return r
|
|
|
|
end
|
|
|
|
|
|
|
|
--UNTESTED
|
|
|
|
function M.openap(d)
|
|
|
|
local r = ResponseClass.new(d)
|
|
|
|
|
|
|
|
--add AP net, activate it, deactivate all others, reload network/wireless config, add all dhcp and captive settings and reload as needed
|
|
|
|
netconf.switchConfiguration{apnet="add_noreload"}
|
|
|
|
wifi.activateConfig(wifi.AP_SSID)
|
|
|
|
netconf.switchConfiguration{ wifiiface="add", network="reload", staticaddr="add", dhcppool="add", wwwredir="add", dnsredir="add", wwwcaptive="add" }
|
|
|
|
r:setSuccess("switched to Access Point mode")
|
|
|
|
r:addData("ssid", wifi.AP_SSID)
|
|
|
|
|
|
|
|
return r
|
|
|
|
end
|
|
|
|
|
|
|
|
--UNTESTED
|
|
|
|
--requires ssid(string)
|
|
|
|
function M.rm(d)
|
|
|
|
local r = ResponseClass.new(d)
|
|
|
|
local argSsid = d:get("ssid")
|
|
|
|
|
|
|
|
if argSsid == nil or argSsid == "" then
|
|
|
|
r:setError("missing ssid argument")
|
|
|
|
return r
|
|
|
|
end
|
|
|
|
|
|
|
|
if wifi.removeConfig(argSsid) then
|
|
|
|
r:setSuccess("removed wireless network with requested SSID")
|
|
|
|
r:addData("ssid", argSsid)
|
|
|
|
else
|
2013-07-08 19:02:20 +02:00
|
|
|
r:setFail("no wireless network with requested SSID") --this used to be a warning instead of an error...
|
2013-07-08 16:53:45 +02:00
|
|
|
r:addData("ssid", argSsid)
|
|
|
|
end
|
|
|
|
|
|
|
|
return r
|
|
|
|
end
|
2013-07-08 13:34:27 +02:00
|
|
|
|
|
|
|
return M
|