2013-04-04 10:18:08 +02:00
|
|
|
--[[
|
|
|
|
Response format:
|
|
|
|
["OK" | "WARN" | "ERR"]<,{message}>
|
|
|
|
{comma-separated line 1}
|
|
|
|
...
|
|
|
|
{comma-separated line n}
|
|
|
|
|
|
|
|
- general info on wireless config: http://wiki.openwrt.org/doc/uci/wireless
|
|
|
|
- uci docs: http://wiki.openwrt.org/doc/techref/uci
|
|
|
|
- parse/generate urls: https://github.com/keplerproject/cgilua/blob/master/src/cgilua/urlcode.lua
|
|
|
|
- utility functions: http://luci.subsignal.org/trac/browser/luci/trunk/libs/sys/luasrc/sys.lua
|
|
|
|
- iwinfo tool source: http://luci.subsignal.org/trac/browser/luci/trunk/contrib/package/iwinfo/src/iwinfo.lua?rev=7919
|
|
|
|
- captive portal -> redirect all web traffic to one page for auth (or network selection)
|
|
|
|
http://wiki.openwrt.org/doc/howto/wireless.hotspot
|
|
|
|
]]
|
|
|
|
--print ("HTTP/1.0 200 OK")
|
2013-04-08 01:20:45 +02:00
|
|
|
io.write ("Content-type: text/plain\r\n\r\n")
|
2013-04-04 10:18:08 +02:00
|
|
|
|
2013-04-08 01:20:45 +02:00
|
|
|
local u = require("util")
|
2013-04-04 10:18:08 +02:00
|
|
|
local wifi = require("wifihelper")
|
2013-04-08 01:20:45 +02:00
|
|
|
local reconf = require("reconf")
|
2013-04-04 10:18:08 +02:00
|
|
|
local uci = require("uci").cursor()
|
|
|
|
local urlcode = require("urlcode")
|
|
|
|
local iwinfo = require("iwinfo")
|
|
|
|
|
|
|
|
local argOperation, argDevice, argSsid, argPhrase, argRecreate
|
|
|
|
local errortext = nil
|
|
|
|
|
|
|
|
function init()
|
2013-04-08 01:20:45 +02:00
|
|
|
u:initlog(u.LOG_LEVEL.debug, true, io.stderr)
|
2013-04-04 10:18:08 +02:00
|
|
|
local qs = os.getenv("QUERY_STRING")
|
|
|
|
local urlargs = {}
|
|
|
|
urlcode.parsequery(qs, urlargs)
|
|
|
|
|
|
|
|
--supplement urlargs with arguments from the command-line
|
|
|
|
for _, v in ipairs(arg) do
|
|
|
|
local split = v:find("=")
|
|
|
|
if split ~= nil then
|
|
|
|
urlargs[v:sub(1, split - 1)] = v:sub(split + 1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
argOperation = urlargs["op"]
|
|
|
|
argDevice = urlargs["dev"] or DFL_DEVICE
|
|
|
|
argSsid = urlargs["ssid"]
|
|
|
|
argPhrase = urlargs["phrase"]
|
|
|
|
argRecreate = urlargs["recreate"]
|
|
|
|
|
|
|
|
if urlargs["echo"] ~= nil then
|
|
|
|
print("[[echo: '"..qs.."']]");
|
|
|
|
end
|
|
|
|
|
|
|
|
if argOperation == nil then
|
|
|
|
errortext = "Missing operation specifier"
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2013-04-08 01:20:45 +02:00
|
|
|
return wifi.init() and reconf.init(wifi, true)
|
2013-04-04 10:18:08 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function main()
|
|
|
|
if argOperation == "getavl" then
|
|
|
|
local sr = wifi.getScanInfo()
|
|
|
|
local si, se
|
2013-04-08 01:20:45 +02:00
|
|
|
|
|
|
|
--TODO:
|
|
|
|
-- - extend reconf interface to support function arguments (as tables) so wifihelper functionality can be integrated
|
|
|
|
-- but how? idea: pass x_args={arg1="a",arg2="2342"} with component 'x'
|
2013-04-04 10:18:08 +02:00
|
|
|
if sr and #sr > 0 then
|
2013-04-08 01:20:45 +02:00
|
|
|
u.printWithSuccess(#sr .. " network(s) found");
|
2013-04-04 10:18:08 +02:00
|
|
|
for _, se in ipairs(sr) do
|
2013-04-08 01:20:45 +02:00
|
|
|
print("[[ " .. u.dump(se) .. " ]]") --TEMP
|
|
|
|
if se.mode ~= "ap" then
|
|
|
|
print(se.ssid .. "," .. se.bssid .. "," .. se.channel .. "," .. wifi.mapDeviceMode(se.mode))
|
|
|
|
end
|
2013-04-04 10:18:08 +02:00
|
|
|
end
|
|
|
|
else
|
2013-04-08 01:20:45 +02:00
|
|
|
u.exitWithError("No scan results or scanning not possible")
|
2013-04-04 10:18:08 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
elseif argOperation == "getknown" then
|
2013-04-08 01:20:45 +02:00
|
|
|
u.printWithSuccess("")
|
2013-04-04 10:18:08 +02:00
|
|
|
for _, net in ipairs(wifi.getConfigs()) do
|
|
|
|
if net.mode == "sta" then
|
|
|
|
local bssid = net.bssid or "<unknown BSSID>"
|
|
|
|
local channel = net.channel or "<unknown channel>"
|
|
|
|
print(net.ssid .. "," .. bssid .. "," .. channel)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
elseif argOperation == "getstate" then
|
|
|
|
local ds = wifi.getDeviceState()
|
|
|
|
local ssid = ds.ssid or "<unknown SSID>"
|
|
|
|
local bssid = ds.bssid or "<unknown BSSID>"
|
|
|
|
local channel = ds.channel or "<unknown channel>"
|
2013-04-08 01:20:45 +02:00
|
|
|
u.printWithSuccess("");
|
2013-04-04 10:18:08 +02:00
|
|
|
print(ssid .. "," .. bssid .. "," .. channel .. "," .. ds.mode)
|
|
|
|
|
|
|
|
elseif argOperation == "assoc" then
|
2013-04-08 01:20:45 +02:00
|
|
|
if argSsid == nil or argSsid == "" then u.exitWithError("Please supply an SSID to associate with") end
|
2013-04-04 10:18:08 +02:00
|
|
|
|
|
|
|
local cfg = nil
|
|
|
|
for _, net in ipairs(wifi.getConfigs()) do
|
|
|
|
if net.mode ~= "ap" and net.ssid == argSsid then
|
|
|
|
cfg = net
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if cfg == nil or argRecreate ~= nil then
|
2013-04-05 16:22:19 +02:00
|
|
|
local scanResult = wifi.getScanInfo(argSsid)
|
2013-04-04 10:18:08 +02:00
|
|
|
if scanResult ~= nil then
|
|
|
|
wifi.createConfigFromScanInfo(scanResult)
|
|
|
|
else
|
|
|
|
--check for error
|
2013-04-08 01:20:45 +02:00
|
|
|
u.exitWithError("No wireless network with SSID '" .. argSsid .. "' is available")
|
2013-04-04 10:18:08 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
wifi.activateConfig(argSsid)
|
2013-04-05 16:22:19 +02:00
|
|
|
local rv = wifi.restart()
|
2013-04-08 01:20:45 +02:00
|
|
|
u.exitWithSuccess("Wlan associated with network "..argSsid.."! [$?=" .. rv .. "]")
|
2013-04-04 10:18:08 +02:00
|
|
|
|
|
|
|
elseif argOperation == "disassoc" then
|
|
|
|
wifi.activateConfig()
|
2013-04-05 16:22:19 +02:00
|
|
|
local rv = wifi.restart()
|
2013-04-08 01:20:45 +02:00
|
|
|
u.exitWithSuccess("Deactivated all wireless networks [$?=" .. rv .. "]")
|
2013-04-05 16:22:19 +02:00
|
|
|
|
|
|
|
elseif argOperation == "openap" then
|
2013-04-08 01:20: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
|
|
|
|
reconf.switchConfiguration{apnet="add_noreload"}
|
2013-04-05 16:22:19 +02:00
|
|
|
wifi.activateConfig(wifi.AP_SSID)
|
2013-04-08 01:20:45 +02:00
|
|
|
reconf.switchConfiguration{ network="reload", staticaddr="add", dhcppool="add", wwwredir="add", dnsredir="add", wwwcaptive="add", natreflect="add" }
|
|
|
|
u.exitWithSuccess("Switched to AP mode (SSID: '" .. wifi.AP_SSID .. "')")
|
2013-04-04 10:18:08 +02:00
|
|
|
|
|
|
|
elseif argOperation == "rm" then
|
2013-04-08 01:20:45 +02:00
|
|
|
if argSsid == nil or argSsid == "" then u.exitWithError("Please supply an SSID to remove") end
|
2013-04-04 10:18:08 +02:00
|
|
|
if wifi.removeConfig(argSsid) then
|
2013-04-08 01:20:45 +02:00
|
|
|
u.exitWithSuccess("Removed wireless network with SSID " .. argSsid)
|
2013-04-04 10:18:08 +02:00
|
|
|
else
|
2013-04-08 01:20:45 +02:00
|
|
|
u.exitWithWarning("No wireless network with SSID " .. argSsid)
|
2013-04-04 10:18:08 +02:00
|
|
|
end
|
2013-04-08 01:20:45 +02:00
|
|
|
|
|
|
|
elseif argOperation == "test" then
|
|
|
|
reconf.switchConfiguration{ apnet="rm", staticaddr="rm", dhcppool="rm", wwwredir="rm", dnsredir="rm", wwwcaptive="rm", natreflect="rm" }
|
|
|
|
-- reconf.switchConfiguration{dnsredir="add"}
|
|
|
|
u.exitWithSuccess("nop")
|
2013-04-04 10:18:08 +02:00
|
|
|
|
|
|
|
elseif argOperation == "auto" then
|
2013-04-08 01:20:45 +02:00
|
|
|
u.exitWithWarning("Not implemented");
|
2013-04-04 10:18:08 +02:00
|
|
|
--scan nets
|
|
|
|
--take union of scan and known
|
|
|
|
--connect to first if not empty; setup ap otherwise
|
|
|
|
|
|
|
|
else
|
2013-04-08 01:20:45 +02:00
|
|
|
u.exitWithError("Unknown operation: '" .. argOperation .. "'")
|
2013-04-04 10:18:08 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
os.exit(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
--[[ START OF CODE ]]--
|
|
|
|
|
|
|
|
if init() == false then
|
2013-04-08 01:20:45 +02:00
|
|
|
u.exitWithError(errortext)
|
2013-04-04 10:18:08 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
main()
|