0
0
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:
Wouter R 2013-08-22 17:15:44 +02:00
parent dc327ea420
commit 0730b442ac
3 changed files with 92 additions and 49 deletions

View File

@ -34,15 +34,16 @@ local function setupAutoWifiMode()
return nil, "autowifi: could not scan wifi networks (" .. msg .. ")" return nil, "autowifi: could not scan wifi networks (" .. msg .. ")"
end end
-- START TEMP -- mode should be ap or sta log:info("current wifi name/mode: " .. (netName or "<nil>") .. "/" .. netMode .. ", ssid of self: " .. apSsid)
print("wifi name: " .. netName .. ", wifi mode: " .. netMode .. ", expected AP ssid: " .. apSsid .. ", apmode: " .. (apMode and "yes" or "no")) local visNet, knownNet = {}, {}
for _,sn in ipairs(scanList) do for _,sn in ipairs(scanList) do
print("avl net: " .. sn.ssid) table.insert(visNet, sn.ssid)
end end
for _,kn in ipairs(knownSsids) do for _,kn in ipairs(knownSsids) do
print("known net: " .. kn.ssid .. " (mode: " .. kn.mode .. ")") table.insert(knownNet, kn.ssid .. "/" .. kn.mode)
end 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 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 if netMode == 'sta' and findSsidInList(scanList, netName) then
@ -60,13 +61,19 @@ local function setupAutoWifiMode()
if connectWith then if connectWith then
print("connectWith: " .. connectWith) --TEMP print("connectWith: " .. connectWith) --TEMP
-- TODO: refactor connect stuff into network:connect() function and adapt api_network_associate as well (and others?) local rv,msg = netconf.associateSsid(connectWith)
-- TODO: connect with network if rv then
-- return true, "autowifi: associated -- client mode with ssid '" .. connectWith .. "'" return true, "autowifi: associated -- client mode with ssid '" .. connectWith .. "'"
elseif netMode ~= 'ap' then else
print("shouldBeAp") --TEMP return nil, "autowifi: could not associate with ssid '" .. connectWith .. "' (" .. msg .. ")"
-- TODO: setup AP (refactor into network like with client connect) end
-- return true, "autowifi: configured as access point with ssid '" .. apSsid .. "'" 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 else
return true, "autowifi: no action - no known networks found, already in access point mode" return true, "autowifi: no action - no known networks found, already in access point mode"
end end

View File

@ -243,4 +243,61 @@ function reconf.natreflect_rm(dirtyList)
bothBits(dirtyList, 'firewall') bothBits(dirtyList, 'firewall')
end 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 return M

View File

@ -1,8 +1,8 @@
local settings = require("util.settings") local settings = require('util.settings')
local utils = require("util.utils") local utils = require('util.utils')
local netconf = require("network.netconfig") local netconf = require('network.netconfig')
local wifi = require("network.wlanconfig") local wifi = require('network.wlanconfig')
local ResponseClass = require("rest.response") local ResponseClass = require('rest.response')
local M = { local M = {
isApi = true isApi = true
@ -101,36 +101,13 @@ function M.associate_POST(request, response)
return return
end end
local cfg = nil local rv,msg = netconf.associateSsid(argSsid, argPhrase, argRecreate)
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
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) response:addData("ssid", argSsid)
if status.ssid and status.ssid == argSsid then if rv then
response:setSuccess("wlan associated") response:setSuccess("wlan associated")
else else
response:setFail("could not associate with network (incorrect pass phrase?)") response:setFail(msg)
end end
end end
@ -143,13 +120,15 @@ end
function M.openap_POST(request, response) function M.openap_POST(request, response)
local ssid = wifi.getSubstitutedSsid(settings.get('network.ap.ssid')) local ssid = wifi.getSubstitutedSsid(settings.get('network.ap.ssid'))
netconf.switchConfiguration{apnet="add_noreload"} local rv,msg = netconf.setupAccessPoint(ssid)
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")
response:addData("ssid", 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 end
--requires ssid(string) --requires ssid(string)