2013-07-17 22:55:27 +02:00
|
|
|
local s = require("util.settings")
|
2013-07-08 16:53:45 +02:00
|
|
|
local u = require("util.utils")
|
2013-07-17 08:25:24 +02:00
|
|
|
local l = require("util.logger")
|
2013-07-08 16:53:45 +02:00
|
|
|
local netconf = require("network.netconfig")
|
|
|
|
local wifi = require("network.wlanconfig")
|
|
|
|
local ResponseClass = require("rest.response")
|
2013-07-08 13:34:27 +02:00
|
|
|
|
2013-07-17 22:55:27 +02:00
|
|
|
local M = {
|
|
|
|
isApi = true
|
|
|
|
}
|
2013-07-08 13:34:27 +02:00
|
|
|
|
|
|
|
|
2013-07-10 00:32:43 +02:00
|
|
|
function M._global(request, response)
|
|
|
|
response:setError("not implemented")
|
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'
|
2013-07-10 00:32:43 +02:00
|
|
|
--accepts with_raw(bool) to include raw table dump
|
|
|
|
function M.available(request, response)
|
|
|
|
local noFilter = u.toboolean(request:get("nofilter"))
|
|
|
|
local withRaw = u.toboolean(request:get("with_raw"))
|
2013-07-08 16:53:45 +02:00
|
|
|
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
|
2013-07-10 00:32:43 +02:00
|
|
|
response:setSuccess("")
|
2013-07-08 16:53:45 +02:00
|
|
|
local netInfoList = {}
|
|
|
|
for _, se in ipairs(sr) do
|
2013-07-17 22:55:27 +02:00
|
|
|
if noFilter or se.mode ~= "ap" and se.ssid ~= wifi.getSubstitutedSsid(s.get('apSsid')) then
|
2013-07-08 16:53:45 +02:00
|
|
|
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
|
2013-07-17 08:25:24 +02:00
|
|
|
if withRaw then netInfo["_raw"] = u.dump(se) end
|
2013-07-08 16:53:45 +02:00
|
|
|
|
|
|
|
table.insert(netInfoList, netInfo)
|
2013-07-08 13:34:27 +02:00
|
|
|
end
|
|
|
|
end
|
2013-07-10 00:32:43 +02:00
|
|
|
response:addData("count", #netInfoList)
|
|
|
|
response:addData("networks", netInfoList)
|
2013-07-08 16:53:45 +02:00
|
|
|
else
|
2013-07-10 00:32:43 +02:00
|
|
|
response:setFail("No scan results or scanning not possible")
|
2013-07-08 16:53:45 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--accepts API argument 'nofilter'(bool) to disable filtering of APs and 'self'
|
2013-07-10 00:32:43 +02:00
|
|
|
--accepts with_raw(bool) to include raw table dump
|
|
|
|
function M.known(request, response)
|
|
|
|
local noFilter = u.toboolean(request:get("nofilter"))
|
|
|
|
local withRaw = u.toboolean(request:get("with_raw"))
|
2013-07-08 16:53:45 +02:00
|
|
|
|
2013-07-10 00:32:43 +02:00
|
|
|
response:setSuccess()
|
2013-07-08 16:53:45 +02:00
|
|
|
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-17 08:25:24 +02:00
|
|
|
if withRaw then netInfo["_raw"] = u.dump(net) end
|
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
|
2013-07-10 00:32:43 +02:00
|
|
|
response:addData("count", #netInfoList)
|
|
|
|
response:addData("networks", netInfoList)
|
2013-07-08 16:53:45 +02:00
|
|
|
end
|
|
|
|
|
2013-07-10 00:32:43 +02:00
|
|
|
--accepts with_raw(bool) to include raw table dump
|
|
|
|
function M.state(request, response)
|
|
|
|
local withRaw = u.toboolean(request:get("with_raw"))
|
2013-07-08 16:53:45 +02:00
|
|
|
local ds = wifi.getDeviceState()
|
|
|
|
|
2013-07-10 00:32:43 +02:00
|
|
|
response:setSuccess()
|
|
|
|
response:addData("ssid", ds.ssid or "")
|
|
|
|
response:addData("bssid", ds.bssid or "")
|
|
|
|
response:addData("channel", ds.channel or "")
|
|
|
|
response:addData("mode", ds.mode)
|
|
|
|
response:addData("encryption", ds.encryption)
|
|
|
|
response:addData("quality", ds.quality)
|
|
|
|
response:addData("quality_max", ds.quality_max)
|
|
|
|
response:addData("txpower", ds.txpower)
|
|
|
|
response:addData("signal", ds.signal)
|
|
|
|
response:addData("noise", ds.noise)
|
2013-07-17 08:25:24 +02:00
|
|
|
if withRaw then response:addData("_raw", u.dump(ds)) end
|
2013-07-08 16:53:45 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--UNTESTED
|
|
|
|
--requires ssid(string), accepts phrase(string), recreate(bool)
|
2013-07-10 00:32:43 +02:00
|
|
|
function M.assoc(request, response)
|
|
|
|
local argSsid = request:get("ssid")
|
|
|
|
local argPhrase = request:get("phrase")
|
|
|
|
local argRecreate = request:get("recreate")
|
2013-07-08 16:53:45 +02:00
|
|
|
|
|
|
|
if argSsid == nil or argSsid == "" then
|
2013-07-10 00:32:43 +02:00
|
|
|
response:setError("missing ssid argument")
|
|
|
|
return
|
2013-07-08 16:53:45 +02:00
|
|
|
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-10 00:32:43 +02:00
|
|
|
response:setFail("no wireless network with requested SSID is available")
|
|
|
|
response:addData("ssid", argSsid)
|
|
|
|
return
|
2013-07-08 13:34:27 +02:00
|
|
|
end
|
2013-07-08 16:53:45 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
wifi.activateConfig(argSsid)
|
2013-07-17 22:55:27 +02:00
|
|
|
--netconf.switchConfiguration{ wifiiface="add", apnet="rm", staticaddr="rm", dhcppool="rm", wwwredir="rm", dnsredir="rm", wwwcaptive="rm", wireless="reload" }
|
|
|
|
netconf.switchConfiguration{ wifiiface="add", apnet="rm", staticaddr="rm", dhcppool="rm", wwwredir="rm", dnsredir="rm", wireless="reload" }
|
2013-07-10 00:32:43 +02:00
|
|
|
response:setSuccess("wlan associated")
|
|
|
|
response:addData("ssid", argSsid)
|
2013-07-08 16:53:45 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--UNTESTED
|
2013-07-10 00:32:43 +02:00
|
|
|
function M.disassoc(request, response)
|
2013-07-08 16:53:45 +02:00
|
|
|
wifi.activateConfig()
|
|
|
|
local rv = wifi.restart()
|
2013-07-10 00:32:43 +02:00
|
|
|
response:setSuccess("all wireless networks deactivated")
|
|
|
|
response:addData("wifi_restart_result", rv)
|
2013-07-08 16:53:45 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--UNTESTED
|
2013-07-10 00:32:43 +02:00
|
|
|
function M.openap(request, response)
|
2013-07-08 16:53:45 +02:00
|
|
|
--add AP net, activate it, deactivate all others, reload network/wireless config, add all dhcp and captive settings and reload as needed
|
2013-07-17 22:55:27 +02:00
|
|
|
local ssid = wifi.getSubstitutedSsid(s.get('apSsid'))
|
2013-07-08 16:53:45 +02:00
|
|
|
netconf.switchConfiguration{apnet="add_noreload"}
|
2013-07-17 22:55:27 +02:00
|
|
|
wifi.activateConfig(ssid)
|
|
|
|
--netconf.switchConfiguration{ wifiiface="add", network="reload", staticaddr="add", dhcppool="add", wwwredir="add", dnsredir="add", wwwcaptive="add" }
|
|
|
|
netconf.switchConfiguration{ wifiiface="add", network="reload", staticaddr="add", dhcppool="add", wwwredir="add", dnsredir="add" }
|
2013-07-10 00:32:43 +02:00
|
|
|
response:setSuccess("switched to Access Point mode")
|
2013-07-17 22:55:27 +02:00
|
|
|
response:addData("ssid", ssid)
|
2013-07-08 16:53:45 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--UNTESTED
|
|
|
|
--requires ssid(string)
|
2013-07-10 00:32:43 +02:00
|
|
|
function M.rm(request, response)
|
|
|
|
local argSsid = request:get("ssid")
|
2013-07-08 16:53:45 +02:00
|
|
|
|
|
|
|
if argSsid == nil or argSsid == "" then
|
2013-07-10 00:32:43 +02:00
|
|
|
response:setError("missing ssid argument")
|
|
|
|
return
|
2013-07-08 16:53:45 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
if wifi.removeConfig(argSsid) then
|
2013-07-10 00:32:43 +02:00
|
|
|
response:setSuccess("removed wireless network with requested SSID")
|
|
|
|
response:addData("ssid", argSsid)
|
2013-07-08 16:53:45 +02:00
|
|
|
else
|
2013-07-10 00:32:43 +02:00
|
|
|
response:setFail("no wireless network with requested SSID") --this used to be a warning instead of an error...
|
|
|
|
response:addData("ssid", argSsid)
|
2013-07-08 16:53:45 +02:00
|
|
|
end
|
|
|
|
end
|
2013-07-08 13:34:27 +02:00
|
|
|
|
|
|
|
return M
|