0
0
mirror of https://github.com/Doodle3D/doodle3d-firmware.git synced 2024-12-22 11:03:48 +01:00

Split debug flags into DEBUG_PCALLS and DEBUG_API; fix more quotation style and import naming.

This commit is contained in:
Wouter R 2013-07-24 18:49:07 +02:00
parent 9fd13dda65
commit d4b9c39d60
5 changed files with 221 additions and 164 deletions

51
TODO.md
View File

@ -1,3 +1,54 @@
## te doen voor werkend prototype
- auto-update (zowel package als geheel image; <- kijken hoe luci dat doet)
- tekenen op redelijke subset van apparaten (wat hebben beta-testers?)
- printen (printserver; via dirpad gcode/commando's sturen en uitlezen via voortgangsbestand; zoals UltiFi; REST reageert op rq met poll-url in data waar voortgang te volgen is…eindigt met 'done')
## NU MEE BEZIG
- auto-update
- source-url in Makefile aanpassen (type aanpassen naar git en dan direct naar github repo)
- toevoegen aan /etc/opkg.conf via files in image: `src/gz wifibox http://doodle3d.com/static/wifibox-packages`
- of lokaal: `src/gz wifibox file:///tmp/wifibox-packages`
- (info) feed update-script: /Volumes/openwrt-image-10gb/update-feed-dir.sh
- (info) package-url: <http://doodle3d.com/static/wifibox-packages>
- (info) image-url: <http://doodle3d.com/static/wifibox-images>
- API:
api/info/currentVersion
api/info/latestVersion [beta=true]
api/system/update
api/system/flash
* wat als wij een verkeerd package releasen waardoor de API niet meer werkt?
- (ref) <http://wiki.openwrt.org/doc/devel/packages/opkg>
- (ref) <http://wiki.openwrt.org/doc/techref/opkg>
- (ref) <http://downloads.openwrt.org/snapshots/trunk/ar71xx/packages/>
- waar moeten debugvlaggen etc naartoe? (run_flags.lua?)
- require vars checken op conflicten
- in package postinst: hostname van kastje instellen op wifibox (met mac?)
- wiki bijwerken (links, structuur, API)
- Code documenteren <http://keplerproject.github.io/luadoc/>
- Lua programmeerstijl? (enkele quotes gebruiken behalve voor i18n)
- zoals het nu werkt wordt het lastig om een hiërarchische api te ondersteunen zoals dit: <http://www.restapitutorial.com/lessons/restfulresourcenaming.html>
- uhttpd ondersteunt geen PUT en DELETE, wel status codes. Beschrijving CGI-antwoorden: <http://docstore.mik.ua/orelly/linux/cgi/ch03_03.htm>
- voor captive portal: cgi 'Location' header voor redirect naar goede url?
- http statuscodes <https://blog.apigee.com/detail/restful_api_design_what_about_errors>; met relevante link in antwoord (meer: <https://blog.apigee.com/taglist/restful>)
- proposed status handling in response.lua:
fucntion setStatus(code, <msg>) -> sets http status+dfl msg and optional errmsg in data
## lokale notities
- in menuconfig: enabled uhttpd-mod-lua, disabled uhttpd-mod-ubus
- menuconfig: disable Network->6relayd and Network->odhcp6c
then disable Kernel modules->Network support->kmod ipv6
- menuconfig: disable Network->ppp, then disable Kernel modules->Network support->kmod-{ppp,pppoe,pppox}
- menuconfig: enabled Kernel Modules -> USB Support -> usb-kmod-serial -> …ftdi
- enabled luafilesystem, luasocket (luaposix results in a build error)
- <http://stackoverflow.com/questions/11732934/serial-connection-with-arduino-php-openwrt-bug>
- versies toevoegen als eerste padelement?
# TODO (new functionality)
- fix init script handling as described here: http://wiki.openwrt.org/doc/devel/packages#packaging.a.service
- implement (automated) test code where possible

View File

@ -7,6 +7,9 @@ local M = {}
--during debugging. This flag replaces them with a normal call so we can inspect stack traces.
M.DEBUG_PCALLS = true
--This enables debugging of the REST API from the command-line, specify the path and optionally the request method as follows: 'p=/mod/func rq=POST'
M.DEBUG_API = true
--REST responses will contain 'module' and 'function' keys describing what was requested
M.API_INCLUDE_ENDPOINT_INFO = false

View File

@ -1,8 +1,8 @@
package.path = package.path .. ';/usr/share/lua/wifibox/?.lua'
local confDefaults = require('conf_defaults')
local u = require('util.utils')
local l = require('util.logger')
local util = require('util.utils')
local log = require('util.logger')
local wifi = require('network.wlanconfig')
local netconf = require('network.netconfig')
local RequestClass = require('rest.request')
@ -16,13 +16,19 @@ local function setupAutoWifiMode()
end
local function init()
l:init(l.LEVEL.debug)
l:setStream(io.stderr)
log:init(log.LEVEL.debug)
log:setStream(io.stderr)
if confDefaults.DEBUG_PCALLS then l:info("Wifibox CGI handler started (pcall debugging enabled)")
else l:info("Wifibox CGI handler started")
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"
end
if dbgText ~= "" then dbgText = " (" .. dbgText .. " debugging enabled)" end
log:info("Wifibox CGI handler started" .. dbgText)
if (os.getenv('REQUEST_METHOD') == 'POST') then
local n = tonumber(os.getenv('CONTENT_LENGTH'))
postData = io.read(n)
@ -39,27 +45,24 @@ local function init()
end
local function main()
local rq = RequestClass.new(postData, confDefaults.DEBUG_PCALLS)
local rq = RequestClass.new(postData, confDefaults.DEBUG_API)
l:info("received request of type " .. rq:getRequestMethod() .. " for " .. (rq:getRequestedApiModule() or "<unknown>")
.. "/" .. (rq:getRealApiFunctionName() or "<unknown>") .. " with arguments: " .. u.dump(rq:getAll()))
log:info("received request of type " .. rq:getRequestMethod() .. " for " .. (rq:getRequestedApiModule() or "<unknown>")
.. "/" .. (rq:getRealApiFunctionName() or "<unknown>") .. " with arguments: " .. util.dump(rq:getAll()))
if rq:getRequestMethod() ~= 'CMDLINE' then
l:info("remote IP/port: " .. rq:getRemoteHost() .. "/" .. rq:getRemotePort())
l:debug("user agent: " .. rq:getUserAgent())
log:info("remote IP/port: " .. rq:getRemoteHost() .. "/" .. rq:getRemotePort())
log:debug("user agent: " .. rq:getUserAgent())
end
if (not confDefaults.DEBUG_PCALLS and rq:getRequestMethod() == 'CMDLINE') then
if rq:get('autowifi') ~= nil then
setupAutoWifiMode()
else
l:info("Nothing to do...bye.\n")
end
else
if rq:getRequestMethod() == 'CMDLINE' and rq:get('autowifi') ~= nil then
setupAutoWifiMode()
elseif rq:getRequestMethod() ~= 'CMDLINE' or confDefaults.DEBUG_API then
local response, err = rq:handle()
if err ~= nil then l:error(err) end
if err ~= nil then log:error(err) end
response:send()
else
log:info("Nothing to do...bye.\n")
end
end
@ -72,7 +75,7 @@ if s == false then
resp:setError("initialization failed" .. errSuffix)
io.write ("Content-type: text/plain\r\n\r\n")
resp:send()
l:error("initialization failed" .. errSuffix) --NOTE: this assumes the logger has been inited properly, despite init() having failed
log:error("initialization failed" .. errSuffix) --NOTE: this assumes the logger has been inited properly, despite init() having failed
os.exit(1)
else

View File

@ -1,33 +1,33 @@
local u = require("util.utils")
local l = require("util.logger")
local s = require("util.settings")
local wifi = require("network.wlanconfig")
local uci = require("uci").cursor()
local utils = require('util.utils')
local log = require('util.logger')
local settings = require('util.settings')
local wifi = require('network.wlanconfig')
local uci = require('uci').cursor()
local M = {}
local reconf = {}
local wifi
local reloadSilent
M.WWW_CAPTIVE_PATH = "/usr/share/lua/wifibox/www"
M.WWW_CAPTIVE_INDICATOR = "/www/.wifibox-inplace"
M.WWW_RENAME_NAME = "/www-regular"
M.WWW_CAPTIVE_PATH = '/usr/share/lua/wifibox/www'
M.WWW_CAPTIVE_INDICATOR = '/www/.wifibox-inplace'
M.WWW_RENAME_NAME = '/www-regular'
local function reloadBit(dlist, itemname)
if dlist[itemname] == nil then dlist[itemname] = "" end
if dlist[itemname] == "" then dlist[itemname] = "r"
elseif dlist[itemname] == "c" then dlist[itemname] = "b"
if dlist[itemname] == nil then dlist[itemname] = '' end
if dlist[itemname] == '' then dlist[itemname] = 'r'
elseif dlist[itemname] == 'c' then dlist[itemname] = 'b'
end
end
local function commitBit(dlist, itemname)
if dlist[itemname] == nil then dlist[itemname] = "" end
if dlist[itemname] == "" then dlist[itemname] = "c"
elseif dlist[itemname] == "r" then dlist[itemname] = "b"
if dlist[itemname] == nil then dlist[itemname] = '' end
if dlist[itemname] == '' then dlist[itemname] = 'c'
elseif dlist[itemname] == 'r' then dlist[itemname] = 'b'
end
end
local function bothBits(dlist, itemname) dlist[itemname] = "b" end
local function bothBits(dlist, itemname) dlist[itemname] = 'b' end
@ -45,33 +45,33 @@ function M.switchConfiguration(components)
local dirtyList = {} -- laundry list, add config/script name as key with value c (commit), r (reload) or b (both)
for k,v in pairs(components) do
local fname = k .. "_" .. v
if type(reconf[fname]) == "function" then
l:debug("reconfiguring component '" .. k .. "' (" .. v .. ")")
local fname = k .. '_' .. v
if type(reconf[fname]) == 'function' then
log:debug("reconfiguring component '" .. k .. "' (" .. v .. ")")
reconf[fname](dirtyList)
else
l:warn("unknown component or action '" .. fname .. "' skipped")
log:warn("unknown component or action '" .. fname .. "' skipped")
end
end
-- first run all commits, then perform reloads
for k,v in pairs(dirtyList) do
if v == "c" or v == "b" then M.commitComponent(k) end
if v == 'c' or v == 'b' then M.commitComponent(k) end
end
for k,v in pairs(dirtyList) do
if v == "r" or v == "b" then M.reloadComponent(k, silent) end
if v == 'r' or v == 'b' then M.reloadComponent(k, silent) end
end
end
function M.commitComponent(c)
l:info("committing component '" .. c .. "'")
log:info("committing component '" .. c .. "'")
uci:commit(c)
end
function M.reloadComponent(c, silent)
l:info("reloading component '" .. c .. "'")
if silent ~= nil and silent then os.execute("/etc/init.d/" .. c .. " reload &> /dev/null")
else os.execute("/etc/init.d/" .. c .. " reload") end
log:info("reloading component '" .. c .. "'")
if silent ~= nil and silent then os.execute('/etc/init.d/' .. c .. ' reload &> /dev/null')
else os.execute('/etc/init.d/' .. c .. ' reload') end
end
function M.uciTableSet(config, section, options)
@ -81,140 +81,140 @@ end
--[[ Issue '/etc/init.d/network reload' command ]]
function reconf.network_reload(dirtyList) reloadBit(dirtyList, "network") end
function reconf.network_reload(dirtyList) reloadBit(dirtyList, 'network') end
--[[ Issue '/etc/init.d/wireless reload' command ]]
function reconf.wireless_reload(dirtyList) reloadBit(dirtyList, "wireless") end
function reconf.wireless_reload(dirtyList) reloadBit(dirtyList, 'wireless') end
--[[ Add wlan interface declaration to /etc/config/network ]]
function reconf.wifiiface_add(dirtyList)
uci:set("network", wifi.NET, "interface")
commitBit(dirtyList, "network")
uci:set('network', wifi.NET, 'interface')
commitBit(dirtyList, 'network')
end
--[[ Add/remove access point network ]]
function reconf.apnet_add_noreload(dirtyList) reconf.apnet_add(dirtyList, true) end
function reconf.apnet_add(dirtyList, noReload)
local ourSsid = wifi.getSubstitutedSsid(s.get('apSsid'))
local ourSsid = wifi.getSubstitutedSsid(settings.get('apSsid'))
local sname = nil
uci:foreach("wireless", "wifi-iface", function(s)
if s.ssid == ourSsid then sname = s[".name"]; return false end
uci:foreach('wireless', 'wifi-iface', function(s)
if s.ssid == ourSsid then sname = s['.name']; return false end
end)
if sname == nil then sname = uci:add("wireless", "wifi-iface") end
if sname == nil then sname = uci:add('wireless', 'wifi-iface') end
M.uciTableSet("wireless", sname, {
M.uciTableSet('wireless', sname, {
network = wifi.NET,
ssid = ourSsid,
encryption = "none",
device = "radio0",
mode = "ap",
encryption = 'none',
device = 'radio0',
mode = 'ap',
})
commitBit(dirtyList, "wireless")
if noReload == nil or noReload == false then reloadBit(dirtyList, "network") end
commitBit(dirtyList, 'wireless')
if noReload == nil or noReload == false then reloadBit(dirtyList, 'network') end
end
function reconf.apnet_rm(dirtyList)
local sname = nil
uci:foreach("wireless", "wifi-iface", function(s)
if s.ssid == wifi.getSubstitutedSsid(s.get(apSsid)) then sname = s[".name"]; return false end
uci:foreach('wireless', 'wifi-iface', function(s)
if s.ssid == wifi.getSubstitutedSsid(settings.get('apSsid')) then sname = s['.name']; return false end
end)
if sname == nil then return l:info("AP network configuration does not exist, nothing to remove") end
uci:delete("wireless", sname)
reloadBit(dirtyList, "network"); commitBit(dirtyList, "wireless")
if sname == nil then return log:info("AP network configuration does not exist, nothing to remove") end
uci:delete('wireless', sname)
reloadBit(dirtyList, 'network'); commitBit(dirtyList, 'wireless')
end
--[[ Switch between wireless static IP and DHCP ]]
function reconf.staticaddr_add(dirtyList)
uci:set("network", wifi.NET, "interface")
uci:set('network', wifi.NET, 'interface')
--TODO: remove ifname on wlan interface?
--NOTE: 'type = "bridge"' should -not- be added as this prevents defining a separate dhcp pool (http://wiki.openwrt.org/doc/recipes/routedap)
M.uciTableSet("network", wifi.NET, {
proto = "static",
ipaddr = s.get('apAddress'),
netmask = s.get('apNetmask')
M.uciTableSet('network', wifi.NET, {
proto = 'static',
ipaddr = settings.get('apAddress'),
netmask = settings.get('apNetmask')
})
bothBits(dirtyList, "network")
bothBits(dirtyList, 'network')
end
--TODO: replace repeated deletes by M.uciTableDelete
function reconf.staticaddr_rm(dirtyList)
uci:set("network", wifi.NET, "interface")
uci:set("network", wifi.NET, "proto", "dhcp")
uci:delete("network", wifi.NET, "ipaddr")
uci:delete("network", wifi.NET, "netmask")
--uci:delete("network", wifi.NET, "type") --do not remove since it is not added anymore
bothBits(dirtyList, "network")
uci:set('network', wifi.NET, 'interface')
uci:set('network', wifi.NET, 'proto', 'dhcp')
uci:delete('network', wifi.NET, 'ipaddr')
uci:delete('network', wifi.NET, 'netmask')
--uci:delete('network', wifi.NET, 'type') --do not remove since it is not added anymore
bothBits(dirtyList, 'network')
end
--[[ Add/remove DHCP pool for wireless net ]]
function reconf.dhcppool_add(dirtyList)
uci:set("dhcp", wifi.NET, "dhcp") --create section
M.uciTableSet("dhcp", wifi.NET, {
uci:set('dhcp', wifi.NET, 'dhcp') --create section
M.uciTableSet('dhcp', wifi.NET, {
interface = wifi.NET,
start = "100",
limit = "150",
leasetime = "12h",
start = '100',
limit = '150',
leasetime = '12h',
})
commitBit(dirtyList, "dhcp"); reloadBit(dirtyList, "dnsmasq")
commitBit(dirtyList, 'dhcp'); reloadBit(dirtyList, 'dnsmasq')
end
function reconf.dhcppool_rm(dirtyList)
uci:delete("dhcp", wifi.NET)
commitBit(dirtyList, "dhcp"); reloadBit(dirtyList, "dnsmasq")
uci:delete('dhcp', wifi.NET)
commitBit(dirtyList, 'dhcp'); reloadBit(dirtyList, 'dnsmasq')
end
--[[ Add/remove webserver 404 redirection and denial of dirlisting ]]
function reconf.wwwredir_add(dirtyList)
uci:set("uhttpd", "main", "error_page", "/admin/autowifi.html")
uci:set("uhttpd", "main", "no_dirlist", "1")
bothBits(dirtyList, "uhttpd")
uci:set('uhttpd', 'main', 'error_page', '/www/index.html')
uci:set('uhttpd', 'main', 'no_dirlist', '1')
bothBits(dirtyList, 'uhttpd')
end
function reconf.wwwredir_rm(dirtyList)
uci:delete("uhttpd", "main", "error_page")
uci:delete("uhttpd", "main", "no_dirlist")
bothBits(dirtyList, "uhttpd")
uci:delete('uhttpd', 'main', 'error_page')
uci:delete('uhttpd', 'main', 'no_dirlist')
bothBits(dirtyList, 'uhttpd')
end
--[[ Add/remove redirecton of all DNS requests to self ]]
function reconf.dnsredir_add(dirtyList)
local redirText = "/#/" .. s.get('apAddress')
local sname = u.getUciSectionName("dhcp", "dnsmasq")
if sname == nil then return l:error("dhcp config does not contain a dnsmasq section") end
if uci:get("dhcp", sname, "address") ~= nil then return l:debug("DNS address redirection already in place, not re-adding", false) end
local redirText = '/#/' .. settings.get('apAddress')
local sname = utils.getUciSectionName('dhcp', 'dnsmasq')
if sname == nil then return log:error("dhcp config does not contain a dnsmasq section") end
if uci:get('dhcp', sname, 'address') ~= nil then return log:debug("DNS address redirection already in place, not re-adding", false) end
uci:set("dhcp", sname, "address", {redirText})
commitBit(dirtyList, "dhcp"); reloadBit(dirtyList, "dnsmasq")
uci:set('dhcp', sname, 'address', {redirText})
commitBit(dirtyList, 'dhcp'); reloadBit(dirtyList, 'dnsmasq')
end
function reconf.dnsredir_rm(dirtyList)
local sname = u.getUciSectionName("dhcp", "dnsmasq")
if sname == nil then return l:error("dhcp config does not contain a dnsmasq section") end
local sname = utils.getUciSectionName('dhcp', 'dnsmasq')
if sname == nil then return log:error("dhcp config does not contain a dnsmasq section") end
uci:delete("dhcp", sname, "address")
commitBit(dirtyList, "dhcp"); reloadBit(dirtyList, "dnsmasq")
uci:delete('dhcp', sname, 'address')
commitBit(dirtyList, 'dhcp'); reloadBit(dirtyList, 'dnsmasq')
end
--TODO: handle os.rename() return values (nil+msg on error)
function reconf.wwwcaptive_add(dirtyList)
if u.exists(M.WWW_CAPTIVE_INDICATOR) then
return l:debug("WWW captive directory already in place, not redoing", false)
if utils.exists(M.WWW_CAPTIVE_INDICATOR) then
return log:debug("WWW captive directory already in place, not redoing", false)
end
local rv,reason = os.rename("/www", M.WWW_RENAME_NAME)
local rv,reason = os.rename('/www', M.WWW_RENAME_NAME)
if rv == true then
u.symlink(M.WWW_CAPTIVE_PATH, "/www")
utils.symlink(M.WWW_CAPTIVE_PATH, '/www')
return true
else
return l:error("Could not rename /www to " .. M.WWW_RENAME_NAME .. "(" .. reason .. ")")
return log:error("Could not rename /www to " .. M.WWW_RENAME_NAME .. "(" .. reason .. ")")
end
end
function reconf.wwwcaptive_rm(dirtyList)
if not u.exists(M.WWW_CAPTIVE_INDICATOR) then return l:debug("WWW captive directory not in place, not undoing", false) end
os.remove("/www")
if os.rename(M.WWW_RENAME_NAME, "/www") ~= true then
return l:error("Could not rename " .. M.WWW_RENAME_NAME .. " to /www")
if not utils.exists(M.WWW_CAPTIVE_INDICATOR) then return log:debug("WWW captive directory not in place, not undoing", false) end
os.remove('/www')
if os.rename(M.WWW_RENAME_NAME, '/www') ~= true then
return log:error("Could not rename " .. M.WWW_RENAME_NAME .. " to /www")
end
return true
end
@ -222,20 +222,20 @@ end
--[[ Setup/remove NAT reflection to redirect all IPs ]]
function reconf.natreflect_add(dirtyList)
uci:set("firewall", "portalreflect", "redirect");
M.uciTableSet("firewall", "portalreflect", {
src = "lan",
proto = "tcp",
src_dport = "80",
dest_port = "80",
dest_ip = s.get('apAddress'),
target = "DNAT"
uci:set('firewall', 'portalreflect', 'redirect');
M.uciTableSet('firewall', 'portalreflect', {
src = 'lan',
proto = 'tcp',
src_dport = '80',
dest_port = '80',
dest_ip = settings.get('apAddress'),
target = 'DNAT'
})
bothBits(dirtyList, "firewall")
bothBits(dirtyList, 'firewall')
end
function reconf.natreflect_rm(dirtyList)
uci:delete("firewall", "portalreflect")
bothBits(dirtyList, "firewall")
uci:delete('firewall', 'portalreflect')
bothBits(dirtyList, 'firewall')
end
return M

View File

@ -1,14 +1,13 @@
local util = require("util.utils")
local l = require("util.logger")
local uci = require("uci").cursor()
local iwinfo = require("iwinfo")
local log = require('util.logger')
local uci = require('uci').cursor()
local iwinfo = require('iwinfo')
local M = {}
--NOTE: fallback device 'radio0' is required because sometimes the wlan0 device disappears
M.DFL_DEVICE = "wlan0"
M.DFL_DEVICE_FALLBACK = "radio0"
M.NET = "wlan"
M.DFL_DEVICE = 'wlan0'
M.DFL_DEVICE_FALLBACK = 'radio0'
M.NET = 'wlan'
local dev, dev_api
@ -17,6 +16,7 @@ local cachedApSsid, baseApSsid = nil, nil
function M.getSubstitutedSsid(unformattedSsid)
if unformattedSsid == baseApSsid and cachedApSsid ~= nil then return cachedApSsid end
if not unformattedSsid or type(unformattedSsid) ~= 'string' then return nil end
local macTail = M.getMacAddress():sub(7)
@ -33,9 +33,9 @@ end
-- @param masterIsAp set to true to map 'Master' to 'ap' instead of 'sta' (optional)
function M.mapDeviceMode(mode, masterIsAp)
local modeMap = {
["Master"] = masterIsAp and "ap" or "sta",
["Client"] = "sta",
["Ad-Hoc"] = "adhoc"
['Master'] = masterIsAp and 'ap' or 'sta',
['Client'] = 'sta',
['Ad-Hoc'] = 'adhoc'
}
return modeMap[mode] or mode
end
@ -45,10 +45,10 @@ end
- See: http://wiki.openwrt.org/doc/uci/wireless#wpa.modes
]]
function M.mapEncryptionType(scanEncrTbl)
local wpaModeMap = { [1] = "psk", [2] = "psk2", [3] = "mixed-psk" }
local wpaModeMap = { [1] = 'psk', [2] = 'psk2', [3] = 'mixed-psk' }
if scanEncrTbl.enabled == false then return "none" end
if scanEncrTbl.wep == true then return "wep" end
if scanEncrTbl.enabled == false then return 'none' end
if scanEncrTbl.wep == true then return 'wep' end
return wpaModeMap[scanEncrTbl.wpa] or scanEncrTbl.description
end
@ -58,7 +58,7 @@ end
-- @param device wireless device to operate on (optional, defaults to DFL_DEVICE)
-- @return true on success or false+error on failure
function M.init(device)
-- iwinfo = pcall(require, "iwinfo")
-- iwinfo = pcall(require, 'iwinfo')
dev = device or M.DFL_DEVICE
dev_api = iwinfo.type(dev)
if not dev_api then
@ -79,16 +79,16 @@ end
function M.getDeviceState()
local iw = iwinfo[dev_api]
local result = {
["ssid"] = iw.ssid(dev),
["bssid"] = iw.bssid(dev),
["channel"] = iw.channel(dev),
["mode"] = M.mapDeviceMode(iw.mode(dev), true),
["encryption"] = M.mapEncryptionType(iw.encryption(dev).description),
["quality"] = iw.quality(dev),
["quality_max"] = iw.quality_max(dev),
["txpower"] = iw.txpower(dev),
["signal"] = iw.signal(dev),
["noise"] = iw.noise(dev)
['ssid'] = iw.ssid(dev),
['bssid'] = iw.bssid(dev),
['channel'] = iw.channel(dev),
['mode'] = M.mapDeviceMode(iw.mode(dev), true),
['encryption'] = M.mapEncryptionType(iw.encryption(dev).description),
['quality'] = iw.quality(dev),
['quality_max'] = iw.quality_max(dev),
['txpower'] = iw.txpower(dev),
['signal'] = iw.signal(dev),
['noise'] = iw.noise(dev)
}
return result
end
@ -98,7 +98,7 @@ end
function M.getMacAddress()
local iw = iwinfo[dev_api]
local macText = iw.bssid(dev)
local out = ""
local out = ''
for i = 0, 5 do
local bt = string.sub(macText, i*3+1, i*3+2)
@ -136,7 +136,7 @@ end
--- Return all wireless networks configured in UCI
function M.getConfigs()
local l = {}
uci.foreach("wireless", "wifi-iface", function(s) table.insert(l, s) end)
uci.foreach('wireless', 'wifi-iface', function(s) table.insert(l, s) end)
return l
end
@ -144,25 +144,25 @@ end
-- @return true if successfully removed, false if no such config exists
function M.removeConfig(ssid)
local rv = false
uci:foreach("wireless", "wifi-iface", function(s)
uci:foreach('wireless', 'wifi-iface', function(s)
if s.ssid == ssid then
uci:delete("wireless", s[".name"])
uci:delete('wireless', s['.name'])
rv = true
return false
end
end)
uci:commit("wireless")
uci:commit('wireless')
return rv
end
--- Activate wireless section for given SSID and disable all others
-- @param ssid SSID of config to enable, or nil to disable all network configs
function M.activateConfig(ssid)
uci:foreach("wireless", "wifi-iface", function(s)
local disabled = s.ssid ~= ssid and "1" or "0"
uci:set("wireless", s[".name"], "disabled", disabled)
uci:foreach('wireless', 'wifi-iface', function(s)
local disabled = s.ssid ~= ssid and '1' or '0'
uci:set('wireless', s['.name'], 'disabled', disabled)
end)
uci:commit("wireless")
uci:commit('wireless')
end
--- Create a new UCI network from the given iwinfo data
@ -176,7 +176,7 @@ function M.createConfigFromScanInfo(info, passphrase, disabled)
local apconfig = {
network = M.NET,
device = "radio0",
device = 'radio0',
ssid = info.ssid,
bssid = info.bssid,
encryption = M.mapEncryptionType(info.encryption),
@ -185,19 +185,19 @@ function M.createConfigFromScanInfo(info, passphrase, disabled)
if passphrase ~= nil then apconfig.key = passphrase end
apconfig.disabled = disabled ~= nil and disabled and 1 or 0
uci:foreach("wireless", "wifi-iface", function(s)
uci:foreach('wireless', 'wifi-iface', function(s)
if s.bssid == info.bssid then
l:debug("removing old wireless config for net '" .. s.ssid .. "(bssid: " .. s.bssid .. ")'")
uci:delete("wireless", s[".name"])
log:debug("removing old wireless config for net '" .. s.ssid .. "(bssid: " .. s.bssid .. ")'")
uci:delete('wireless', s['.name'])
-- return false --keep looking, just in case multiple entries with this bssid exist
end
end)
local sname = uci:add("wireless", "wifi-iface");
local sname = uci:add('wireless', 'wifi-iface');
for k, v in pairs(apconfig) do
uci:set("wireless", sname, k, v)
uci:set('wireless', sname, k, v)
end
uci:commit("wireless")
uci:commit('wireless')
end
--- Reload network config to reflect contents of config
@ -205,11 +205,11 @@ end
-- * Network reload only restarts interfaces which need to be restarted so no
-- unneccesary interruptions there.
-- * ubus does not seem to work -- local c=ubus.connect();
-- c:call("network.interface.wlan", "down"); c:call("network.interface.wlan", "up"); c:close()
-- c:call('network.interface.wlan', 'down'); c:call('network.interface.wlan', 'up'); c:close()
-- @param dhcpToo also reload dnsmasq if true
function M.restart(dhcpToo)
os.execute("/etc/init.d/network reload") --always seems to return 0
if dhcpToo ~= nil and dhcpToo then os.execute("/etc/init.d/dnsmasq reload") end
os.execute('/etc/init.d/network reload') --always seems to return 0
if dhcpToo ~= nil and dhcpToo then os.execute('/etc/init.d/dnsmasq reload') end
return 0
end