2013-12-20 16:29:46 +01:00
|
|
|
--
|
|
|
|
-- This file is part of the Doodle3D project (http://doodle3d.com).
|
|
|
|
--
|
|
|
|
-- @copyright 2013, Doodle3D
|
|
|
|
-- @license This software is licensed under the terms of the GNU GPL v2 or later.
|
|
|
|
-- See file LICENSE.txt or visit http://www.gnu.org/licenses/gpl.html for full license details.
|
|
|
|
|
|
|
|
|
2013-12-13 14:23:31 +01:00
|
|
|
----
|
2013-11-08 18:54:57 +01:00
|
|
|
-- Entry code of the REST API and secondary functionality.
|
|
|
|
-- Primarily, this sets up the environment, processes a REST request and responds appropiately.
|
|
|
|
-- Secondary functions are to auto-switch between access point and client (@{setupAutoWifiMode})
|
|
|
|
-- and to signin to [connect.doodle3d.com](http://connect.doodle3d.com/) (@{network.signin}).
|
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-09-27 18:38:31 +02:00
|
|
|
local Signin = require('network.signin')
|
2013-04-04 10:18:08 +02:00
|
|
|
|
2013-12-13 14:23:31 +01:00
|
|
|
-- NOTE: the updater module 'detects' command-line invocation by existence of 'arg', so we have to make sure it is not defined.
|
|
|
|
argStash = arg
|
|
|
|
arg = nil
|
|
|
|
local updater = require('script.d3d-updater')
|
|
|
|
arg = argStash
|
|
|
|
|
2013-07-08 13:34:27 +02:00
|
|
|
local postData = nil
|
2013-04-04 10:18:08 +02:00
|
|
|
|
|
|
|
|
2013-11-08 18:54:57 +01:00
|
|
|
--- Switches to wifi client mode or to access point mode based on availability of known wifi networks.
|
|
|
|
--
|
|
|
|
-- If the configuration has actively been set to access point mode, that will always be selected.
|
|
|
|
-- If not, it will be attempted to connect to a known network (in order of recency) and only if
|
|
|
|
-- that fails, access point mode will be selected as fall-back.
|
2013-07-08 13:34:27 +02:00
|
|
|
local function setupAutoWifiMode()
|
2013-08-23 01:58:09 +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-09-02 15:01:16 +02:00
|
|
|
|
2013-08-20 22:38:16 +02:00
|
|
|
local wifiState = wifi.getDeviceState()
|
|
|
|
local netName, netMode = wifiState.ssid, wifiState.mode
|
2015-06-12 14:46:38 +02:00
|
|
|
log:info("current wifi name: " .. (netName or "<nil>") .. ", mode: " .. netMode)
|
2013-09-02 15:01:16 +02:00
|
|
|
|
2013-08-20 22:38:16 +02:00
|
|
|
local apSsid = wifi.getSubstitutedSsid(settings.get('network.ap.ssid'))
|
2014-05-01 16:14:36 +02:00
|
|
|
local apMode = (apSsid == netName) and (netMode == 'ap')
|
2015-06-12 14:46:38 +02:00
|
|
|
log:info("ssid of self: " .. apSsid)
|
2013-09-02 15:01:16 +02:00
|
|
|
|
2013-08-20 22:38:16 +02:00
|
|
|
local scanList,msg = wifi.getScanInfo()
|
|
|
|
if not scanList then
|
|
|
|
return nil, "autowifi: could not scan wifi networks (" .. msg .. ")"
|
|
|
|
end
|
2013-09-02 15:01:16 +02:00
|
|
|
|
2015-06-12 14:46:38 +02:00
|
|
|
local knownSsids = wifi.getConfigs()
|
|
|
|
-- log:info("current wifi name: " .. (netName or "<nil>") .. ", mode: " .. netMode .. ", ssid of self: " .. apSsid)
|
2013-08-22 17:15:44 +02:00
|
|
|
local visNet, knownNet = {}, {}
|
2013-08-20 22:38:16 +02:00
|
|
|
for _,sn in ipairs(scanList) do
|
2013-08-22 17:15:44 +02:00
|
|
|
table.insert(visNet, sn.ssid)
|
2013-08-20 22:38:16 +02:00
|
|
|
end
|
|
|
|
for _,kn in ipairs(knownSsids) do
|
2013-08-22 17:15:44 +02:00
|
|
|
table.insert(knownNet, kn.ssid .. "/" .. kn.mode)
|
2013-08-20 22:38:16 +02:00
|
|
|
end
|
2013-08-22 17:15:44 +02:00
|
|
|
log:info("visible networks: " .. table.concat(visNet, ", "))
|
|
|
|
log:info("known networks: " .. table.concat(knownNet, ", "))
|
2013-09-02 15:01:16 +02:00
|
|
|
|
2013-08-20 22:38:16 +02:00
|
|
|
-- if the currently active network is client mode and is also visible, do nothing since it will connect automatically further along the boot process
|
2013-08-23 01:58:09 +02:00
|
|
|
if netMode == 'sta' and netName ~= nil and netName ~= "" and findSsidInList(scanList, netName) then
|
2015-06-16 16:49:18 +02:00
|
|
|
-- signin to connect.doodle3d.com
|
|
|
|
local success, output = Signin.signin()
|
|
|
|
if success then
|
|
|
|
log:info("Signed in")
|
|
|
|
else
|
|
|
|
log:info("Signing in failed")
|
|
|
|
end
|
|
|
|
-- report we are connected after signin attempt
|
|
|
|
netconf.setStatus(netconf.CONNECTED,"Connected");
|
2013-08-20 22:38:16 +02:00
|
|
|
return true, "autowifi: no action - existing configuration found for currently wifi visible network (" .. netName .. ")"
|
|
|
|
end
|
2013-09-02 15:01:16 +02:00
|
|
|
|
2013-08-20 22:38:16 +02:00
|
|
|
-- try to find a known network which is also visible (ordered by known network definitions)
|
2014-01-10 11:13:26 +01:00
|
|
|
-- when it finds a access point configuration first, it will use that
|
2013-08-20 22:38:16 +02:00
|
|
|
local connectWith = nil
|
|
|
|
for _,kn in ipairs(knownSsids) do
|
2014-01-10 11:13:26 +01:00
|
|
|
if kn.mode == 'ap' and kn.ssid == apSsid then break end
|
2014-05-01 16:14:36 +02:00
|
|
|
if findSsidInList(scanList, kn.ssid) and kn.mode == 'sta' then
|
2013-08-20 22:38:16 +02:00
|
|
|
connectWith = kn.ssid
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2013-10-08 16:55:50 +02:00
|
|
|
|
2013-08-20 22:38:16 +02:00
|
|
|
if connectWith then
|
2014-03-12 18:23:10 +01:00
|
|
|
local rv,msg = netconf.associateSsid(connectWith,nil,nil)
|
2013-08-22 17:15:44 +02:00
|
|
|
if rv then
|
|
|
|
return true, "autowifi: associated -- client mode with ssid '" .. connectWith .. "'"
|
|
|
|
else
|
|
|
|
return nil, "autowifi: could not associate with ssid '" .. connectWith .. "' (" .. msg .. ")"
|
|
|
|
end
|
|
|
|
elseif netMode ~= 'ap' or netName ~= apSsid then
|
2014-03-12 18:23:10 +01:00
|
|
|
local rv,msg = netconf.setupAccessPoint(apSsid)
|
2013-08-22 17:15:44 +02:00
|
|
|
if rv then
|
|
|
|
return true, "autowifi: configured as access point with ssid '" .. apSsid .. "'"
|
|
|
|
else
|
|
|
|
return nil, "autowifi: failed to configure as access point with ssid '" .. apSsid .. "' (" .. msg .. ")"
|
|
|
|
end
|
2013-08-20 22:38:16 +02:00
|
|
|
else
|
2015-06-16 16:49:18 +02:00
|
|
|
netconf.setStatus(netconf.CREATED,"Access point created");
|
2013-08-20 22:38:16 +02:00
|
|
|
return true, "autowifi: no action - no known networks found, already in access point mode"
|
|
|
|
end
|
2013-09-02 15:01:16 +02:00
|
|
|
|
2013-08-20 22:38:16 +02:00
|
|
|
return nil, "autowifi: uh oh - bad situation in autowifi function"
|
2013-04-04 10:18:08 +02:00
|
|
|
end
|
|
|
|
|
2013-11-08 18:54:57 +01:00
|
|
|
--- Initializes the logging system to use the file and level defined in the system settings.
|
|
|
|
-- The settings used are `logfile` and `loglevel`. The former may either be a
|
|
|
|
-- reular file path, or `<stdout>` or `<stderr>`.
|
|
|
|
-- @see util.settings.getSystemKey
|
|
|
|
-- @treturn bool True on success, false on error.
|
2013-08-22 18:34:45 +02:00
|
|
|
local function setupLogger()
|
|
|
|
local logStream = io.stderr -- use stderr as hard-coded default target
|
2014-04-17 14:15:36 +02:00
|
|
|
local logLevel = log.LEVEL.verbose -- use verbose logging as hard-coded default level
|
2013-09-02 15:01:16 +02:00
|
|
|
|
2013-08-22 18:34:45 +02:00
|
|
|
local logTargetSetting = settings.getSystemKey('logfile')
|
2016-01-05 18:25:38 +01:00
|
|
|
local logLevelSetting = settings.get('system.log.level')
|
2013-08-22 18:34:45 +02:00
|
|
|
local logTargetError, logLevelError = nil, nil
|
2016-01-05 18:25:38 +01:00
|
|
|
|
|
|
|
-- TEMP: for now, translate print3d log level to firmware level, these will be unfiied in the future
|
|
|
|
-- we get (print3d): quiet,error,warning,info,verbose,bulk -- and we need (firmware): debug,info,warn,error,fatal
|
2016-01-06 15:01:01 +01:00
|
|
|
logLevelMapping = { quiet='fatal', error='error', warning='warn', info='warn', verbose='info', bulk='debug' }
|
2016-01-05 18:25:38 +01:00
|
|
|
logLevelSetting = logLevelMapping[logLevelSetting]
|
2013-09-02 15:01:16 +02:00
|
|
|
|
2013-08-22 18:34:45 +02:00
|
|
|
if type(logTargetSetting) == 'string' then
|
|
|
|
local specialTarget = logTargetSetting:match('^<(.*)>$')
|
|
|
|
if specialTarget then
|
|
|
|
if specialTarget == 'stdout' then logStream = io.stdout
|
|
|
|
elseif specialTarget == 'stderr' then logStream = io.stderr
|
|
|
|
end
|
|
|
|
elseif logTargetSetting:sub(1, 1) == '/' then
|
|
|
|
local f,msg = io.open(logTargetSetting, 'a+')
|
2013-09-02 15:01:16 +02:00
|
|
|
|
2013-08-22 18:34:45 +02:00
|
|
|
if f then logStream = f
|
|
|
|
else logTargetError = msg
|
|
|
|
end
|
|
|
|
end
|
2013-12-23 14:35:03 +01:00
|
|
|
else
|
|
|
|
-- if uci config not available, fallback to /tmp/wifibox.log
|
|
|
|
local f,msg = io.open('/tmp/wifibox.log', 'a+')
|
|
|
|
if f then logStream = f
|
|
|
|
else logTargetError = msg
|
|
|
|
end
|
2013-08-22 18:34:45 +02:00
|
|
|
end
|
2013-09-02 15:01:16 +02:00
|
|
|
|
2013-08-22 18:34:45 +02:00
|
|
|
if type(logLevelSetting) == 'string' and logLevelSetting:len() > 0 then
|
|
|
|
local valid = false
|
|
|
|
for idx,lvl in ipairs(log.LEVEL) do
|
|
|
|
if logLevelSetting == lvl then
|
|
|
|
logLevel = idx
|
|
|
|
valid = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if not valid then logLevelError = true end
|
|
|
|
end
|
2013-09-02 15:01:16 +02:00
|
|
|
|
2013-08-22 18:34:45 +02:00
|
|
|
log:init(logLevel)
|
|
|
|
log:setStream(logStream)
|
2013-09-02 15:01:16 +02:00
|
|
|
|
2013-08-22 18:34:45 +02:00
|
|
|
local rv = true
|
|
|
|
if logTargetError then
|
|
|
|
log:error("could not open logfile '" .. logTargetSetting .. "', using stderr as fallback (" .. logTargetError .. ")")
|
|
|
|
rv = false
|
|
|
|
end
|
2013-09-02 15:01:16 +02:00
|
|
|
|
2013-08-22 18:34:45 +02:00
|
|
|
if logLevelError then
|
2014-04-17 14:15:36 +02:00
|
|
|
log:error("uci config specifies invalid log level '" .. logLevelSetting .. "', using verbose level as fallback")
|
2013-08-22 18:34:45 +02:00
|
|
|
rv = false
|
|
|
|
end
|
2013-09-02 15:01:16 +02:00
|
|
|
|
2013-08-22 18:34:45 +02:00
|
|
|
return rv
|
|
|
|
end
|
|
|
|
|
2013-11-08 18:54:57 +01:00
|
|
|
--- Initializes the environment.
|
|
|
|
-- The logger is set up, any POST data is read and several other subsystems are initialized.
|
|
|
|
-- @tparam table environment The 'shell' environment containing all CGI variables. Note that @{cmdmain} simulates this.
|
2013-08-21 22:49:17 +02:00
|
|
|
local function init(environment)
|
2013-08-22 18:34:45 +02:00
|
|
|
setupLogger()
|
2013-09-02 15:01:16 +02:00
|
|
|
|
2013-07-24 18:49:07 +02:00
|
|
|
local dbgText = ""
|
2013-10-08 16:55:50 +02:00
|
|
|
if confDefaults.DEBUG_API and confDefaults.DEBUG_PCALLS then dbgText = "pcall+api"
|
2013-07-24 18:49:07 +02:00
|
|
|
elseif confDefaults.DEBUG_API then dbgText = "api"
|
|
|
|
elseif confDefaults.DEBUG_PCALL then dbgText = "pcall"
|
2013-07-08 13:34:27 +02:00
|
|
|
end
|
2013-09-02 15:01:16 +02:00
|
|
|
|
2013-10-08 16:55:50 +02:00
|
|
|
if dbgText ~= "" then dbgText = " (" .. dbgText .. " debugging)" end
|
|
|
|
log:info("=======rest api" .. dbgText .. "=======")
|
2013-09-02 15:01:16 +02:00
|
|
|
|
2013-08-21 22:49:17 +02:00
|
|
|
if (environment['REQUEST_METHOD'] == 'POST') then
|
|
|
|
local n = tonumber(environment['CONTENT_LENGTH'])
|
2013-07-08 13:34:27 +02:00
|
|
|
postData = io.read(n)
|
|
|
|
end
|
2013-09-02 15:01:16 +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
|
2013-09-02 15:01:16 +02:00
|
|
|
|
2013-12-09 21:15:02 +01:00
|
|
|
s, msg = netconf.init(wifi, false)
|
2013-07-08 19:02:20 +02:00
|
|
|
if not s then return s, msg end
|
2013-09-02 15:01:16 +02:00
|
|
|
|
2013-07-08 19:02:20 +02:00
|
|
|
return true
|
2013-07-08 13:34:27 +02:00
|
|
|
end
|
2013-04-04 10:18:08 +02:00
|
|
|
|
2013-11-08 18:54:57 +01:00
|
|
|
--- Decides what action to take based on shell/CGI parameters.
|
|
|
|
-- Either executes a REST request, or calls @{setupAutoWifiMode} or @{network.signin}.
|
|
|
|
-- @tparam table environment The CGI environment table.
|
2013-09-20 23:38:20 +02:00
|
|
|
local function main(environment)
|
2013-08-21 22:49:17 +02:00
|
|
|
local rq = RequestClass.new(environment, postData, confDefaults.DEBUG_API)
|
2013-09-02 15:01:16 +02:00
|
|
|
|
2013-07-24 18:49:07 +02:00
|
|
|
if rq:getRequestMethod() == 'CMDLINE' and rq:get('autowifi') ~= nil then
|
2014-04-17 14:15:36 +02:00
|
|
|
|
2013-12-13 14:23:31 +01:00
|
|
|
local version = updater.formatVersion(updater.getCurrentVersion());
|
|
|
|
log:info("Doodle3D version: "..util.dump(version))
|
2014-04-17 14:15:36 +02:00
|
|
|
|
2013-08-20 22:38:16 +02:00
|
|
|
log:info("running in autowifi mode")
|
|
|
|
local rv,msg = setupAutoWifiMode()
|
2013-09-02 15:01:16 +02:00
|
|
|
|
2013-08-20 22:38:16 +02:00
|
|
|
if rv then
|
|
|
|
log:info("autowifi setup done (" .. msg .. ")")
|
|
|
|
else
|
|
|
|
log:error("autowifi setup failed (" .. msg .. ")")
|
|
|
|
end
|
2013-09-27 18:38:31 +02:00
|
|
|
elseif rq:getRequestMethod() == 'CMDLINE' and rq:get('signin') ~= nil then
|
|
|
|
log:info("running in signin mode")
|
2013-10-08 16:55:50 +02:00
|
|
|
|
2013-09-27 18:38:31 +02:00
|
|
|
local ds = wifi.getDeviceState()
|
2013-10-04 16:29:53 +02:00
|
|
|
log:info(" ds.mode: "..util.dump(ds.mode))
|
2013-10-08 16:55:50 +02:00
|
|
|
if ds.mode == "sta" then
|
2013-10-04 16:29:53 +02:00
|
|
|
log:info(" attempting signin")
|
|
|
|
local success,msg = Signin.signin()
|
|
|
|
if success then
|
|
|
|
log:info("Signin successful")
|
2013-10-08 16:55:50 +02:00
|
|
|
else
|
2013-10-04 16:29:53 +02:00
|
|
|
log:info("Signin failed: "..util.dump(msg))
|
|
|
|
end
|
2013-09-27 18:38:31 +02:00
|
|
|
end
|
2013-07-24 18:49:07 +02:00
|
|
|
elseif rq:getRequestMethod() ~= 'CMDLINE' or confDefaults.DEBUG_API then
|
2013-08-23 01:58:09 +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()))
|
|
|
|
log:info("received request of type " .. rq:getRequestMethod() .. " for " .. (rq:getRequestedApiModule() or "<unknown>")
|
|
|
|
.. "/" .. (rq:getRealApiFunctionName() or "<unknown>"))
|
|
|
|
if rq:getRequestMethod() ~= 'CMDLINE' then
|
|
|
|
log:info("remote IP/port: " .. rq:getRemoteHost() .. "/" .. rq:getRemotePort())
|
2014-04-17 14:15:36 +02:00
|
|
|
--log:verbose("user agent: " .. rq:getUserAgent())
|
2013-08-23 01:58:09 +02:00
|
|
|
end
|
2013-09-02 15:01:16 +02:00
|
|
|
|
2013-07-09 01:49:56 +02:00
|
|
|
local response, err = rq:handle()
|
2013-09-02 15:01:16 +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-09-02 15:01:16 +02:00
|
|
|
|
2013-10-10 12:11:16 +02:00
|
|
|
response:executePostResponseQueue()
|
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-08-21 22:49:17 +02:00
|
|
|
|
2013-11-08 18:54:57 +01:00
|
|
|
--- Firmware entry point. Runs @{init} and calls @{main}.
|
|
|
|
--
|
2013-11-05 01:46:32 +01:00
|
|
|
-- This is either used by [uhttp-mod-lua](http://wiki.openwrt.org/doc/uci/uhttpd#embedded.lua)
|
|
|
|
-- directly, or by the d3dapi cgi-bin wrapper script which builds the env table
|
|
|
|
-- from the shell environment. The wrapper script also handles command-line invocation.
|
|
|
|
-- @tparam table env The CGI environment table.
|
2013-08-21 22:49:17 +02:00
|
|
|
-- @treturn number A Z+ return value suitable to return from wrapper script. Note that this value is ignored by uhttpd-mod-lua.
|
|
|
|
function handle_request(env)
|
|
|
|
local s, msg = init(env)
|
2013-09-02 15:01:16 +02:00
|
|
|
|
2013-08-21 22:49:17 +02:00
|
|
|
if s == false then
|
|
|
|
local resp = ResponseClass.new()
|
|
|
|
local errSuffix = msg and " (" .. msg .. ")" or ""
|
2013-09-02 15:01:16 +02:00
|
|
|
|
2013-08-21 22:49:17 +02:00
|
|
|
resp:setError("initialization failed" .. errSuffix)
|
|
|
|
resp:send()
|
|
|
|
log:error("initialization failed" .. errSuffix) --NOTE: this assumes the logger has been initialized properly, despite init() having failed
|
2013-09-02 15:01:16 +02:00
|
|
|
|
2013-08-21 22:49:17 +02:00
|
|
|
return 1
|
|
|
|
else
|
|
|
|
main(env)
|
|
|
|
return 0
|
|
|
|
end
|
2013-07-08 16:53:45 +02:00
|
|
|
end
|