mirror of
https://github.com/Doodle3D/doodle3d-firmware.git
synced 2024-12-22 11:03:48 +01:00
Finish autowifi function.
This commit is contained in:
parent
dc327ea420
commit
0730b442ac
31
src/main.lua
31
src/main.lua
@ -34,15 +34,16 @@ local function setupAutoWifiMode()
|
||||
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"))
|
||||
log:info("current wifi name/mode: " .. (netName or "<nil>") .. "/" .. netMode .. ", ssid of self: " .. apSsid)
|
||||
local visNet, knownNet = {}, {}
|
||||
for _,sn in ipairs(scanList) do
|
||||
print("avl net: " .. sn.ssid)
|
||||
table.insert(visNet, sn.ssid)
|
||||
end
|
||||
for _,kn in ipairs(knownSsids) do
|
||||
print("known net: " .. kn.ssid .. " (mode: " .. kn.mode .. ")")
|
||||
table.insert(knownNet, kn.ssid .. "/" .. kn.mode)
|
||||
end
|
||||
-- END TEMP
|
||||
log:info("visible networks: " .. table.concat(visNet, ", "))
|
||||
log:info("known networks: " .. table.concat(knownNet, ", "))
|
||||
|
||||
-- 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
|
||||
@ -60,13 +61,19 @@ local function setupAutoWifiMode()
|
||||
|
||||
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 .. "'"
|
||||
local rv,msg = netconf.associateSsid(connectWith)
|
||||
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
|
||||
local rv,msg = netconf.setupAccessPoint(apSsid)
|
||||
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
|
||||
else
|
||||
return true, "autowifi: no action - no known networks found, already in access point mode"
|
||||
end
|
||||
|
@ -243,4 +243,61 @@ function reconf.natreflect_rm(dirtyList)
|
||||
bothBits(dirtyList, 'firewall')
|
||||
end
|
||||
|
||||
--- Sets up access point mode.
|
||||
-- Note: this function might belong in the wlanconfig module but that would introduce
|
||||
-- a circular dependency, easiest solution is to place the function here.
|
||||
-- @tparam string ssid The SSID to use for the access point.
|
||||
-- @return True on success or nil+msg on error.
|
||||
function M.setupAccessPoint(ssid)
|
||||
M.switchConfiguration{apnet="add_noreload"}
|
||||
wifi.activateConfig(ssid)
|
||||
-- NOTE: dnsmasq must be reloaded after network or it will be unable to serve IP addresses
|
||||
M.switchConfiguration{ wifiiface="add", network="reload", staticaddr="add", dhcppool="add_noreload", wwwredir="add", dnsredir="add" }
|
||||
M.switchConfiguration{dhcp="reload"}
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
--- Associates wlan device as client with the given SSID.
|
||||
-- Note: this function might belong in the wlanconfig module but that would introduce
|
||||
-- a circular dependency, easiest solution is to place the function here.
|
||||
-- @tparam string ssid The SSID to associate with.
|
||||
-- @tparam string passphrase The passphrase (if any) to use, may be left out if a UCI configuration exists.
|
||||
-- @tparam boolean recreate If true, a new UCI configuration based on scan data will always be created, otherwise an attempt will be made to use an existing configuration.
|
||||
-- @return True on success or nil+msg on error.
|
||||
function M.associateSsid(ssid, passphrase, recreate)
|
||||
-- see if previously configured network for given ssid exists
|
||||
local cfg = nil
|
||||
for _, net in ipairs(wifi.getConfigs()) do
|
||||
if net.mode ~= "ap" and net.ssid == ssid then
|
||||
cfg = net
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
-- if not, or if newly created configuration is requested, create a new configuration
|
||||
if cfg == nil or recreate ~= nil then
|
||||
local scanResult = wifi.getScanInfo(ssid)
|
||||
if scanResult ~= nil then
|
||||
wifi.createConfigFromScanInfo(scanResult, passphrase)
|
||||
else
|
||||
--check for error
|
||||
return nil,"no wireless network with requested SSID is available"
|
||||
end
|
||||
end
|
||||
|
||||
-- try to associate with the network
|
||||
wifi.activateConfig(ssid)
|
||||
--M.switchConfiguration{ wifiiface="add", apnet="rm", staticaddr="rm", dhcppool="rm", wwwredir="rm", dnsredir="rm", wwwcaptive="rm", wireless="reload" }
|
||||
M.switchConfiguration{ wifiiface="add", apnet="rm", staticaddr="rm", dhcppool="rm", wwwredir="rm", dnsredir="rm", wireless="reload" }
|
||||
|
||||
-- check if we are actually associated
|
||||
local status = wifi.getDeviceState()
|
||||
if not status.ssid or status.ssid ~= ssid then
|
||||
return nil,"could not associate with network (incorrect passphrase?)"
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
return M
|
||||
|
@ -1,8 +1,8 @@
|
||||
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")
|
||||
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')
|
||||
|
||||
local M = {
|
||||
isApi = true
|
||||
@ -101,36 +101,13 @@ function M.associate_POST(request, response)
|
||||
return
|
||||
end
|
||||
|
||||
local cfg = nil
|
||||
for _, net in ipairs(wifi.getConfigs()) do
|
||||
if net.mode ~= "ap" and net.ssid == argSsid then
|
||||
cfg = net
|
||||
break
|
||||
end
|
||||
end
|
||||
local rv,msg = netconf.associateSsid(argSsid, argPhrase, argRecreate)
|
||||
|
||||
if cfg == nil or argRecreate ~= nil then
|
||||
local scanResult = wifi.getScanInfo(argSsid)
|
||||
if scanResult ~= nil then
|
||||
wifi.createConfigFromScanInfo(scanResult, argPhrase)
|
||||
else
|
||||
--check for error
|
||||
response:setFail("no wireless network with requested SSID is available")
|
||||
response:addData("ssid", argSsid)
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
wifi.activateConfig(argSsid)
|
||||
--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" }
|
||||
|
||||
local status = wifi.getDeviceState()
|
||||
response:addData("ssid", argSsid)
|
||||
if status.ssid and status.ssid == argSsid then
|
||||
if rv then
|
||||
response:setSuccess("wlan associated")
|
||||
else
|
||||
response:setFail("could not associate with network (incorrect pass phrase?)")
|
||||
response:setFail(msg)
|
||||
end
|
||||
end
|
||||
|
||||
@ -143,13 +120,15 @@ end
|
||||
|
||||
function M.openap_POST(request, response)
|
||||
local ssid = wifi.getSubstitutedSsid(settings.get('network.ap.ssid'))
|
||||
netconf.switchConfiguration{apnet="add_noreload"}
|
||||
wifi.activateConfig(ssid)
|
||||
-- NOTE: dnsmasq must be reloaded after network or it will be unable to serve IP addresses
|
||||
netconf.switchConfiguration{ wifiiface="add", network="reload", staticaddr="add", dhcppool="add_noreload", wwwredir="add", dnsredir="add" }
|
||||
netconf.switchConfiguration{dhcp="reload"}
|
||||
response:setSuccess("switched to Access Point mode")
|
||||
local rv,msg = netconf.setupAccessPoint(ssid)
|
||||
|
||||
response:addData("ssid", ssid)
|
||||
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
|
||||
end
|
||||
|
||||
--requires ssid(string)
|
||||
|
Loading…
Reference in New Issue
Block a user