2013-10-02 14:42:27 +02:00
|
|
|
local log = require('util.logger')
|
2013-08-22 17:15:44 +02:00
|
|
|
local settings = require('util.settings')
|
|
|
|
local utils = require('util.utils')
|
|
|
|
local netconf = require('network.netconfig')
|
|
|
|
local wifi = require('network.wlanconfig')
|
|
|
|
local ResponseClass = require('rest.response')
|
2013-10-02 14:42:27 +02:00
|
|
|
local signin = require('network.signin')
|
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
|
2013-07-25 01:06:00 +02:00
|
|
|
function M.scan(request, response)
|
2013-07-27 00:25:43 +02:00
|
|
|
local noFilter = utils.toboolean(request:get("nofilter"))
|
|
|
|
local withRaw = utils.toboolean(request:get("with_raw"))
|
2013-07-08 16:53:45 +02:00
|
|
|
local sr = wifi.getScanInfo()
|
|
|
|
local si, se
|
2013-09-02 15:01:16 +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-27 00:25:43 +02:00
|
|
|
if noFilter or se.mode ~= "ap" and se.ssid ~= wifi.getSubstitutedSsid(settings.get('network.ap.ssid')) then
|
2013-07-08 16:53:45 +02:00
|
|
|
local netInfo = {}
|
2013-09-02 15:01:16 +02:00
|
|
|
|
2013-07-08 16:53:45 +02:00
|
|
|
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-27 00:25:43 +02:00
|
|
|
if withRaw then netInfo["_raw"] = utils.dump(se) end
|
2013-09-02 15:01:16 +02:00
|
|
|
|
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)
|
2013-07-27 00:25:43 +02:00
|
|
|
local noFilter = utils.toboolean(request:get("nofilter"))
|
|
|
|
local withRaw = utils.toboolean(request:get("with_raw"))
|
2013-09-02 15:01:16 +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-27 00:25:43 +02:00
|
|
|
if withRaw then netInfo["_raw"] = utils.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
|
2013-07-25 01:06:00 +02:00
|
|
|
function M.status(request, response)
|
2013-07-27 00:25:43 +02:00
|
|
|
local withRaw = utils.toboolean(request:get("with_raw"))
|
2013-07-08 16:53:45 +02:00
|
|
|
local ds = wifi.getDeviceState()
|
2013-09-02 15:01:16 +02:00
|
|
|
|
2013-10-04 16:29:53 +02:00
|
|
|
if ds.ssid == nil then
|
|
|
|
response:setFail("Not connected")
|
|
|
|
else
|
|
|
|
response:setSuccess()
|
|
|
|
end
|
|
|
|
|
2013-07-10 00:32:43 +02:00
|
|
|
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-27 00:25:43 +02:00
|
|
|
if withRaw then response:addData("_raw", utils.dump(ds)) end
|
2013-09-30 18:08:02 +02:00
|
|
|
|
|
|
|
local localip = wifi.getLocalIP()
|
|
|
|
response:addData("localip", localip)
|
2013-07-08 16:53:45 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--requires ssid(string), accepts phrase(string), recreate(bool)
|
2013-07-25 01:06:00 +02:00
|
|
|
function M.associate_POST(request, response)
|
2013-09-02 15:01:16 +02:00
|
|
|
local utils = require('util.utils')
|
|
|
|
local log = require('util.logger')
|
|
|
|
log:info("API:Network:associate")
|
|
|
|
|
2013-07-10 00:32:43 +02:00
|
|
|
local argSsid = request:get("ssid")
|
|
|
|
local argPhrase = request:get("phrase")
|
|
|
|
local argRecreate = request:get("recreate")
|
2013-09-02 15:01:16 +02:00
|
|
|
|
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
|
|
|
|
|
2013-10-02 14:42:27 +02:00
|
|
|
local associate = function()
|
|
|
|
local rv,msg = netconf.associateSsid(argSsid, argPhrase, argRecreate)
|
2013-09-02 15:01:16 +02:00
|
|
|
end
|
2013-10-02 14:42:27 +02:00
|
|
|
response:addPostResponseFunction(associate)
|
|
|
|
|
|
|
|
|
2013-09-02 15:01:16 +02:00
|
|
|
|
2013-09-04 19:23:51 +02:00
|
|
|
--[[local helloA = function()
|
2013-09-02 15:01:16 +02:00
|
|
|
local log = require('util.logger')
|
|
|
|
log:info("HELLO A")
|
|
|
|
end
|
|
|
|
response:addPostResponseFunction(helloA)
|
|
|
|
|
|
|
|
local helloB = function()
|
|
|
|
local log = require('util.logger')
|
|
|
|
log:info("HELLO B")
|
|
|
|
end
|
2013-09-04 19:23:51 +02:00
|
|
|
response:addPostResponseFunction(helloB)]]--
|
2013-09-02 15:01:16 +02:00
|
|
|
|
|
|
|
--[[response:addData("ssid", argSsid)
|
2013-08-22 17:15:44 +02:00
|
|
|
if rv then
|
2013-07-29 13:48:56 +02:00
|
|
|
response:setSuccess("wlan associated")
|
|
|
|
else
|
2013-08-22 17:15:44 +02:00
|
|
|
response:setFail(msg)
|
2013-09-02 15:01:16 +02:00
|
|
|
end]]--
|
|
|
|
response:setSuccess("wlan is trying to associate")
|
2013-07-08 16:53:45 +02:00
|
|
|
end
|
|
|
|
|
2013-07-25 01:06:00 +02:00
|
|
|
function M.disassociate_POST(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
|
|
|
|
|
2013-07-25 01:06:00 +02:00
|
|
|
function M.openap_POST(request, response)
|
2013-07-27 00:25:43 +02:00
|
|
|
local ssid = wifi.getSubstitutedSsid(settings.get('network.ap.ssid'))
|
2013-08-22 17:15:44 +02:00
|
|
|
local rv,msg = netconf.setupAccessPoint(ssid)
|
2013-09-02 15:01:16 +02:00
|
|
|
|
2013-07-17 22:55:27 +02:00
|
|
|
response:addData("ssid", ssid)
|
2013-08-22 17:15:44 +02:00
|
|
|
if rv then
|
|
|
|
response:setSuccess("switched to Access Point mode")
|
|
|
|
else
|
|
|
|
response:setFail("could not switch to Access Point mode")
|
|
|
|
response:addData("msg", msg)
|
|
|
|
end
|
2013-07-08 16:53:45 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--requires ssid(string)
|
2013-07-25 01:06:00 +02:00
|
|
|
function M.remove_POST(request, response)
|
2013-07-10 00:32:43 +02:00
|
|
|
local argSsid = request:get("ssid")
|
2013-09-02 15:01:16 +02:00
|
|
|
|
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
|
2013-09-02 15:01:16 +02:00
|
|
|
|
2013-07-08 16:53:45 +02:00
|
|
|
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
|
|
|
|
2013-10-02 14:42:27 +02:00
|
|
|
function M.signin(request, response)
|
|
|
|
log:info("API:Network:signin")
|
2013-10-03 18:07:58 +02:00
|
|
|
local success, output = signin.signin()
|
|
|
|
if success then
|
2013-10-02 14:42:27 +02:00
|
|
|
log:info("API:Network:signed in")
|
|
|
|
response:setSuccess("API:Network:signed in")
|
2013-10-03 18:07:58 +02:00
|
|
|
response:addData("response", output)
|
2013-10-02 14:42:27 +02:00
|
|
|
else
|
|
|
|
log:info("API:Network:Signing in failed")
|
|
|
|
response:setError("Signing in failed")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-10-03 18:07:58 +02:00
|
|
|
function M.alive(request, response)
|
|
|
|
response:setSuccess("alive")
|
|
|
|
end
|
|
|
|
|
2013-07-08 13:34:27 +02:00
|
|
|
return M
|