2013-07-17 08:06:04 +02:00
|
|
|
package.path = package.path .. ';/usr/share/lua/wifibox/?.lua'
|
|
|
|
|
2013-07-17 22:55:27 +02:00
|
|
|
local confDefaults = require('conf_defaults')
|
2013-07-24 18:49:07 +02:00
|
|
|
local log = require('util.logger')
|
2013-08-20 22:38:16 +02:00
|
|
|
local settings = require('util.settings')
|
|
|
|
local util = require('util.utils')
|
2013-07-17 08:25:24 +02:00
|
|
|
local wifi = require('network.wlanconfig')
|
|
|
|
local netconf = require('network.netconfig')
|
|
|
|
local RequestClass = require('rest.request')
|
|
|
|
local ResponseClass = require('rest.response')
|
2013-04-04 10:18:08 +02:00
|
|
|
|
2013-07-08 13:34:27 +02:00
|
|
|
local postData = nil
|
2013-04-04 10:18:08 +02:00
|
|
|
|
|
|
|
|
2013-08-20 22:38:16 +02:00
|
|
|
-- expects list with tables containing 'ssid' key as values and returns index key if found or nil if not found
|
|
|
|
local function findSsidInList(list, name)
|
|
|
|
for k,v in ipairs(list) do
|
|
|
|
if v.ssid == name then return k end
|
|
|
|
end
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2013-07-08 13:34:27 +02:00
|
|
|
local function setupAutoWifiMode()
|
2013-08-20 22:38:16 +02:00
|
|
|
local wifiState = wifi.getDeviceState()
|
|
|
|
local netName, netMode = wifiState.ssid, wifiState.mode
|
|
|
|
|
|
|
|
local apSsid = wifi.getSubstitutedSsid(settings.get('network.ap.ssid'))
|
|
|
|
local apMode = (apSsid == netName) and netMode == 'ap'
|
|
|
|
|
|
|
|
local scanList,msg = wifi.getScanInfo()
|
|
|
|
local knownSsids = wifi.getConfigs()
|
|
|
|
|
|
|
|
if not scanList then
|
|
|
|
return nil, "autowifi: could not scan wifi networks (" .. msg .. ")"
|
|
|
|
end
|
|
|
|
|
|
|
|
-- START TEMP -- mode should be ap or sta
|
|
|
|
print("wifi name: " .. netName .. ", wifi mode: " .. netMode .. ", expected AP ssid: " .. apSsid .. ", apmode: " .. (apMode and "yes" or "no"))
|
|
|
|
for _,sn in ipairs(scanList) do
|
|
|
|
print("avl net: " .. sn.ssid)
|
|
|
|
end
|
|
|
|
for _,kn in ipairs(knownSsids) do
|
|
|
|
print("known net: " .. kn.ssid .. " (mode: " .. kn.mode .. ")")
|
|
|
|
end
|
|
|
|
-- END TEMP
|
|
|
|
|
|
|
|
-- if the currently active network is client mode and is also visible, do nothing since it will connect automatically further along the boot process
|
|
|
|
if netMode == 'sta' and findSsidInList(scanList, netName) then
|
|
|
|
return true, "autowifi: no action - existing configuration found for currently wifi visible network (" .. netName .. ")"
|
|
|
|
end
|
|
|
|
|
|
|
|
-- try to find a known network which is also visible (ordered by known network definitions)
|
|
|
|
local connectWith = nil
|
|
|
|
for _,kn in ipairs(knownSsids) do
|
|
|
|
if findSsidInList(scanList, kn.ssid) then
|
|
|
|
connectWith = kn.ssid
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if connectWith then
|
|
|
|
print("connectWith: " .. connectWith) --TEMP
|
|
|
|
-- TODO: refactor connect stuff into network:connect() function and adapt api_network_associate as well (and others?)
|
|
|
|
-- TODO: connect with network
|
|
|
|
-- return true, "autowifi: associated -- client mode with ssid '" .. connectWith .. "'"
|
|
|
|
elseif netMode ~= 'ap' then
|
|
|
|
print("shouldBeAp") --TEMP
|
|
|
|
-- TODO: setup AP (refactor into network like with client connect)
|
|
|
|
-- return true, "autowifi: configured as access point with ssid '" .. apSsid .. "'"
|
|
|
|
else
|
|
|
|
return true, "autowifi: no action - no known networks found, already in access point mode"
|
|
|
|
end
|
|
|
|
|
|
|
|
return nil, "autowifi: uh oh - bad situation in autowifi function"
|
2013-04-04 10:18:08 +02:00
|
|
|
end
|
|
|
|
|
2013-07-08 13:34:27 +02:00
|
|
|
local function init()
|
2013-07-24 18:49:07 +02:00
|
|
|
log:init(log.LEVEL.debug)
|
|
|
|
log:setStream(io.stderr)
|
2013-07-08 13:34:27 +02:00
|
|
|
|
2013-07-24 18:49:07 +02:00
|
|
|
local dbgText = ""
|
|
|
|
if confDefaults.DEBUG_API and confDefaults.DEBUG_PCALLS then dbgText = "pcall and api"
|
|
|
|
elseif confDefaults.DEBUG_API then dbgText = "api"
|
|
|
|
elseif confDefaults.DEBUG_PCALL then dbgText = "pcall"
|
2013-07-08 13:34:27 +02:00
|
|
|
end
|
|
|
|
|
2013-07-24 18:49:07 +02:00
|
|
|
if dbgText ~= "" then dbgText = " (" .. dbgText .. " debugging enabled)" end
|
|
|
|
|
|
|
|
log:info("Wifibox CGI handler started" .. dbgText)
|
|
|
|
|
2013-07-17 08:25:24 +02:00
|
|
|
if (os.getenv('REQUEST_METHOD') == 'POST') then
|
|
|
|
local n = tonumber(os.getenv('CONTENT_LENGTH'))
|
2013-07-08 13:34:27 +02:00
|
|
|
postData = io.read(n)
|
|
|
|
end
|
2013-07-08 16:53:45 +02:00
|
|
|
|
2013-07-08 19:02:20 +02:00
|
|
|
local s, msg
|
|
|
|
s, msg = wifi.init()
|
|
|
|
if not s then return s, msg end
|
|
|
|
|
|
|
|
s, msg = netconf.init(wifi, true)
|
|
|
|
if not s then return s, msg end
|
|
|
|
|
|
|
|
return true
|
2013-07-08 13:34:27 +02:00
|
|
|
end
|
2013-04-04 10:18:08 +02:00
|
|
|
|
2013-07-08 13:34:27 +02:00
|
|
|
local function main()
|
2013-07-24 18:49:07 +02:00
|
|
|
local rq = RequestClass.new(postData, confDefaults.DEBUG_API)
|
2013-04-04 10:18:08 +02:00
|
|
|
|
2013-07-27 00:25:43 +02:00
|
|
|
-- log:info("received request of type " .. rq:getRequestMethod() .. " for " .. (rq:getRequestedApiModule() or "<unknown>")
|
|
|
|
-- .. "/" .. (rq:getRealApiFunctionName() or "<unknown>") .. " with arguments: " .. util.dump(rq:getAll()))
|
2013-07-24 18:49:07 +02:00
|
|
|
log:info("received request of type " .. rq:getRequestMethod() .. " for " .. (rq:getRequestedApiModule() or "<unknown>")
|
2013-07-27 00:25:43 +02:00
|
|
|
.. "/" .. (rq:getRealApiFunctionName() or "<unknown>"))
|
2013-07-17 08:25:24 +02:00
|
|
|
if rq:getRequestMethod() ~= 'CMDLINE' then
|
2013-07-24 18:49:07 +02:00
|
|
|
log:info("remote IP/port: " .. rq:getRemoteHost() .. "/" .. rq:getRemotePort())
|
|
|
|
log:debug("user agent: " .. rq:getUserAgent())
|
2013-07-08 13:34:27 +02:00
|
|
|
end
|
|
|
|
|
2013-07-24 18:49:07 +02:00
|
|
|
if rq:getRequestMethod() == 'CMDLINE' and rq:get('autowifi') ~= nil then
|
2013-08-20 22:38:16 +02:00
|
|
|
log:info("running in autowifi mode")
|
|
|
|
local rv,msg = setupAutoWifiMode()
|
|
|
|
|
|
|
|
if rv then
|
|
|
|
log:info("autowifi setup done (" .. msg .. ")")
|
|
|
|
else
|
|
|
|
log:error("autowifi setup failed (" .. msg .. ")")
|
|
|
|
end
|
2013-07-24 18:49:07 +02:00
|
|
|
elseif rq:getRequestMethod() ~= 'CMDLINE' or confDefaults.DEBUG_API then
|
2013-07-09 01:49:56 +02:00
|
|
|
local response, err = rq:handle()
|
2013-07-08 13:34:27 +02:00
|
|
|
|
2013-07-24 18:49:07 +02:00
|
|
|
if err ~= nil then log:error(err) end
|
2013-07-09 01:49:56 +02:00
|
|
|
response:send()
|
2013-07-24 18:49:07 +02:00
|
|
|
else
|
|
|
|
log:info("Nothing to do...bye.\n")
|
2013-04-04 10:18:08 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-10 00:32:43 +02:00
|
|
|
---'entry point'---
|
2013-07-08 19:02:20 +02:00
|
|
|
local s, msg = init()
|
|
|
|
if s == false then
|
2013-07-08 16:53:45 +02:00
|
|
|
local resp = ResponseClass.new()
|
2013-07-10 00:32:43 +02:00
|
|
|
local errSuffix = msg and " (" .. msg .. ")" or ""
|
|
|
|
|
|
|
|
resp:setError("initialization failed" .. errSuffix)
|
|
|
|
io.write ("Content-type: text/plain\r\n\r\n")
|
|
|
|
resp:send()
|
2013-07-24 18:49:07 +02:00
|
|
|
log:error("initialization failed" .. errSuffix) --NOTE: this assumes the logger has been inited properly, despite init() having failed
|
2013-07-10 00:32:43 +02:00
|
|
|
|
2013-07-08 16:53:45 +02:00
|
|
|
os.exit(1)
|
|
|
|
else
|
|
|
|
main()
|
|
|
|
os.exit(0)
|
|
|
|
end
|