Extract logger to separate module; update code and package Makefile to reflect new structure; rename wfcf to d3dapi; add dependency on usb ACM driver (required for communication with arduino).

This commit is contained in:
Wouter R 2013-06-30 19:06:55 +02:00
parent cf0f7f594d
commit f298fbf0db
15 changed files with 98 additions and 83 deletions

View File

@ -32,7 +32,7 @@ define Package/wifibox
# DEFAULT:=y
TITLE:=Doodle3D WifiBox firmware
URL:=http://www.doodle3d.com/wifibox
DEPENDS:=+lua +libuci-lua +libiwinfo-lua +uhttpd
DEPENDS:=+lua +libuci-lua +libiwinfo-lua +uhttpd +kmod-usb-acm
endef
define Package/wifibox/description
@ -71,45 +71,50 @@ endef
# command to copy the binary file from its current location (in our case the build
# directory) to the install directory.
AUTOWIFI_BASE_DIR := $(PKG_BUILD_DIR)/autowifi
WIFIBOX_BASE_DIR := $(PKG_BUILD_DIR)
GPX_BASE_DIR := $(PKG_BUILD_DIR)/util/GPX.git
TGT_LUA_DIR_SUFFIX := usr/share/lua/wifibox
define Package/wifibox/install
### create required directories (autowifi)
# $(INSTALL_DIR) $(1)/usr/share/lua/autowifi
$(INSTALL_DIR) $(1)/usr/share/lua/autowifi/admin
# $(INSTALL_DIR) $(1)/usr/share/lua/autowifi/ext
# $(INSTALL_DIR) $(1)/usr/share/lua/autowifi/ext/www
$(INSTALL_DIR) $(1)/usr/share/lua/autowifi/ext/www/cgi-bin
# $(INSTALL_DIR) $(1)/$(TGT_LUA_DIR_SUFFIX)
$(INSTALL_DIR) $(1)/$(TGT_LUA_DIR_SUFFIX)/network
$(INSTALL_DIR) $(1)/$(TGT_LUA_DIR_SUFFIX)/rest
$(INSTALL_DIR) $(1)/$(TGT_LUA_DIR_SUFFIX)/script
$(INSTALL_DIR) $(1)/$(TGT_LUA_DIR_SUFFIX)/util
# $(INSTALL_DIR) $(1)/$(TGT_LUA_DIR_SUFFIX)/www
# $(INSTALL_DIR) $(1)/$(TGT_LUA_DIR_SUFFIX)/www/cgi-bin
$(INSTALL_DIR) $(1)/$(TGT_LUA_DIR_SUFFIX)/www/wifibox
$(INSTALL_DIR) $(1)/etc/init.d
$(INSTALL_DIR) $(1)/www/cgi-bin
### create all files in /usr/share/lua/autowifi (autowifi)
$(CP) $(AUTOWIFI_BASE_DIR)/*.lua $(1)/usr/share/lua/autowifi/
$(CP) $(AUTOWIFI_BASE_DIR)/admin/* $(1)/usr/share/lua/autowifi/admin/
$(CP) $(WIFIBOX_BASE_DIR)/*.lua $(1)/$(TGT_LUA_DIR_SUFFIX)/
$(CP) $(WIFIBOX_BASE_DIR)/network/*.lua $(1)/$(TGT_LUA_DIR_SUFFIX)/network/
$(CP) $(WIFIBOX_BASE_DIR)/rest/*.lua $(1)/$(TGT_LUA_DIR_SUFFIX)/rest/
$(CP) $(WIFIBOX_BASE_DIR)/util/*.lua $(1)/$(TGT_LUA_DIR_SUFFIX)/util/
$(CP) $(AUTOWIFI_BASE_DIR)/ext/autowifi.js $(1)/usr/share/lua/autowifi/ext
$(CP) $(AUTOWIFI_BASE_DIR)/ext/autowifi_init $(1)/usr/share/lua/autowifi/ext
$(CP) $(AUTOWIFI_BASE_DIR)/ext/wfcf $(1)/usr/share/lua/autowifi/ext
$(CP) $(AUTOWIFI_BASE_DIR)/ext/www/.autowifi-inplace $(1)/usr/share/lua/autowifi/ext/www
$(CP) $(AUTOWIFI_BASE_DIR)/ext/www/index.html $(1)/usr/share/lua/autowifi/ext/www
$(LN) -s /usr/share/lua/autowifi/admin $(1)/usr/share/lua/autowifi/ext/www
$(LN) -s /usr/share/lua/autowifi/ext/wfcf $(1)/usr/share/lua/autowifi/ext/www/cgi-bin
$(CP) $(WIFIBOX_BASE_DIR)/script/wifibox_init $(1)/$(TGT_LUA_DIR_SUFFIX)/script
$(CP) $(WIFIBOX_BASE_DIR)/script/d3dapi $(1)/$(TGT_LUA_DIR_SUFFIX)/script
$(CP) $(WIFIBOX_BASE_DIR)/www/wifibox/* $(1)/$(TGT_LUA_DIR_SUFFIX)/www/wifibox/
$(CP) $(WIFIBOX_BASE_DIR)/script/d3dapi $(1)/www/cgi-bin
# $(CP) $(WIFIBOX_BASE_DIR)/www/.autowifi-inplace $(1)/$(TGT_LUA_DIR_SUFFIX)/www
ifeq ($(CONFIG_WIFIBOX_DEVEL_PACKAGE),y)
$(INSTALL_DIR) $(1)/usr/share/lua/autowifi/misc
$(CP) $(AUTOWIFI_BASE_DIR)/misc/collect-code.sh $(1)/usr/share/lua/autowifi/misc/
# $(INSTALL_DIR) $(1)/$(TGT_LUA_DIR_SUFFIX)/misc
# $(CP) $(WIFIBOX_BASE_DIR)/../misc/collect-code.sh $(1)/$(TGT_LUA_DIR_SUFFIX)/misc/
endif
### create links elsewhere in the system (autowifi)
$(LN) -s /usr/share/lua/autowifi/ext/wfcf $(1)/www/cgi-bin
$(LN) -s /usr/share/lua/autowifi/admin $(1)/www
$(LN) -s /usr/share/lua/autowifi/ext/autowifi_init $(1)/etc/init.d/autowifi_init
$(LN) -s /$(TGT_LUA_DIR_SUFFIX)/script/d3dapi $(1)/www/cgi-bin
$(LN) -s /$(TGT_LUA_DIR_SUFFIX)/www/wifibox $(1)/www
$(LN) -s /$(TGT_LUA_DIR_SUFFIX)/script/wifibox_init $(1)/etc/init.d/wifibox_init
### install gpx utility
$(INSTALL_DIR) $(1)/usr/bin

51
src/logger.lua Normal file
View File

@ -0,0 +1,51 @@
local M = {}
local logLevel, logVerbose, logStream
M.LEVEL = {"debug", "info", "warn", "error", "fatal"}
--M.LEVEL already has idx=>name entries, now create name=>idx entries
for i,v in ipairs(M.LEVEL) do
M.LEVEL[v] = i
end
function M:init(level, verbose, stream)
logLevel = level or M.LEVEL.warn
logVerbose = verbose or false
logStream = stream or io.stdout
end
local function log(level, msg, verbose)
if level >= logLevel then
local now = os.date("%m-%d %H:%M:%S")
local i = debug.getinfo(3) --the stack frame just above the logger call
local v = verbose
if v == nil then v = logVerbose end
local name = i.name or "(nil)"
local vVal = "nil"
local m = (type(msg) == "string") and msg or M:dump(msg)
if v then logStream:write(now .. " (" .. M.LEVEL[level] .. ") \t" .. m .. " [" .. name .. "@" .. i.short_src .. ":" .. i.linedefined .. "]\n")
else logStream:write(now .. " (" .. M.LEVEL[level] .. ") \t" .. m .. "\n") end
end
end
function M:dump(o)
if type(o) == 'table' then
local s = '{ '
for k,v in pairs(o) do
if type(k) ~= 'number' then k = '"'..k..'"' end
s = s .. '['..k..'] = ' .. M:dump(v) .. ','
end
return s .. '} '
else
return tostring(o)
end
end
function M:debug(msg, verbose) log(M.LEVEL.debug, msg, verbose); return true end
function M:info(msg, verbose) log(M.LEVEL.info, msg, verbose); return true end
function M:warn(msg, verbose) log(M.LEVEL.warn, msg, verbose); return true end
function M:error(msg, verbose) log(M.LEVEL.error, msg, verbose); return false end
function M:fatal(msg, verbose) log(M.LEVEL.fatal, msg, verbose); return false end
return M

View File

@ -17,6 +17,7 @@
io.write ("Content-type: text/plain\r\n\r\n")
local u = require("util")
local l = require("logger")
local wifi = require("wifihelper")
local reconf = require("reconf")
local uci = require("uci").cursor()
@ -27,7 +28,7 @@ local argOperation, argDevice, argSsid, argPhrase, argRecreate
local errortext = nil
function init()
u:initlog(u.LOG_LEVEL.debug, true, io.stderr)
l:init(l.LEVEL.debug, true, io.stderr)
local qs = os.getenv("QUERY_STRING")
local urlargs = {}
urlcode.parsequery(qs, urlargs)

View File

@ -1,4 +1,5 @@
local u = require("util")
local l = require("logger")
local uci = require("uci").cursor()
local M = {}
@ -44,10 +45,10 @@ function M.switchConfiguration(components)
for k,v in pairs(components) do
local fname = k .. "_" .. v
if type(reconf[fname]) == "function" then
u:logdebug("reconfiguring component '" .. k .. "' (" .. v .. ")")
l:debug("reconfiguring component '" .. k .. "' (" .. v .. ")")
reconf[fname](dirtyList)
else
u:logwarn("unknown component or action '" .. fname .. "' skipped")
l:warn("unknown component or action '" .. fname .. "' skipped")
end
end
@ -61,12 +62,12 @@ function M.switchConfiguration(components)
end
function M.commitComponent(c)
u:loginfo("committing component '" .. c .. "'")
l:info("committing component '" .. c .. "'")
uci:commit(c)
end
function M.reloadComponent(c, silent)
u:loginfo("reloading component '" .. c .. "'")
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
end
@ -115,7 +116,7 @@ function reconf.apnet_rm(dirtyList)
uci:foreach("wireless", "wifi-iface", function(s)
if s.ssid == wifi.AP_SSID then sname = s[".name"]; return false end
end)
if sname == nil then return u:loginfo("AP network configuration does not exist, nothing to remove") 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")
end
@ -178,15 +179,15 @@ end
function reconf.dnsredir_add(dirtyList)
local redirText = "/#/" .. wifi.AP_ADDRESS
local sname = u.getUciSectionName("dhcp", "dnsmasq")
if sname == nil then return u:logerror("dhcp config does not contain a dnsmasq section") end
if uci:get("dhcp", sname, "address") ~= nil then return u:logdebug("DNS address redirection already in place, not re-adding", false) end
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
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 u:logerror("dhcp config does not contain a dnsmasq section") end
if sname == nil then return l:error("dhcp config does not contain a dnsmasq section") end
uci:delete("dhcp", sname, "address")
commitBit(dirtyList, "dhcp"); reloadBit(dirtyList, "dnsmasq")
@ -196,21 +197,21 @@ 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 u:logdebug("WWW captive directory already in place, not redoing", false)
return l:debug("WWW captive directory already in place, not redoing", false)
end
local rv,reason = os.rename("/www", M.WWW_RENAME_NAME)
if rv == true then
u.symlink(M.WWW_CAPTIVE_PATH, "/www")
return true
else
return u:logerror("Could not rename /www to " .. M.WWW_RENAME_NAME .. "(" .. reason .. ")")
return l: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 u:logdebug("WWW captive directory not in place, not undoing", false) end
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 u:logerror("Could not rename " .. M.WWW_RENAME_NAME .. " to /www")
return l:error("Could not rename " .. M.WWW_RENAME_NAME .. " to /www")
end
return true
end

View File

@ -1,5 +1,6 @@
local reconf = require("reconf")
local util = require("util")
local l = require("logger")
local uci = require("uci").cursor()
local iwinfo = require("iwinfo")
@ -145,7 +146,7 @@ function M.createConfigFromScanInfo(info, passphrase, disabled)
uci:foreach("wireless", "wifi-iface", function(s)
if s.bssid == info.bssid then
util:logdebug("removing old wireless config for net '" .. s.ssid .. "(bssid: " .. s.bssid .. ")'")
l: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

View File

@ -2,7 +2,7 @@
#chmod 755 /www/cgi-bin/wfcf
LUA=lua
SCRIPT_PATH=/usr/share/lua/autowifi
SCRIPT_PATH=/usr/share/lua/wifibox
cd $SCRIPT_PATH
$LUA ./autowifi.lua $@
$LUA ./main.lua $@

View File

@ -2,19 +2,6 @@ local uci = require("uci").cursor()
local M = {}
function M.dump(o)
if type(o) == 'table' then
local s = '{ '
for k,v in pairs(o) do
if type(k) ~= 'number' then k = '"'..k..'"' end
s = s .. '['..k..'] = ' .. M.dump(v) .. ','
end
return s .. '} '
else
return tostring(o)
end
end
function M.printWithSuccess(msg)
if msg ~= nil and msg ~= "" then print("OK," .. msg)
else print("OK") end
@ -47,40 +34,11 @@ function M.exists(file)
return r ~= nil
end
--FIXME: somehow protect this function from running arbitrary commands
function M.symlink(from, to)
if from == nil or from == "" or to == nil or to == "" then return -1 end
local x = "ln -s " .. from .. " " .. to
return os.execute(x)
end
-- logging
M.LOG_LEVEL = {debug = 1, info = 2, warn = 3, error = 4, fatal = 5}
local logLevel, logVerbose, logStream
function M:initlog(level, verbose, stream)
logLevel = level or M.LOG_LEVEL.warn
logVerbose = verbose or false
logStream = stream or io.stdout
end
local function log(level, msg, verbose)
if level >= logLevel then
local now = os.date("%m-%d %H:%M:%S")
local i = debug.getinfo(3)
local v = verbose
if v == nil then v = logVerbose end
local name = i.name or "(nil)"
local vVal = "nil"
if v then logStream:write(now .. " (" .. level .. ") \t" .. msg .. " [" .. name .. "@" .. i.short_src .. ":" .. i.linedefined .. "]\n")
else logStream:write(now .. " (" .. level .. ") \t" .. msg .. "\n") end
end
end
function M:logdebug(msg, verbose) log(M.LOG_LEVEL.debug, msg, verbose); return true end
function M:loginfo(msg, verbose) log(M.LOG_LEVEL.info, msg, verbose); return true end
function M:logwarn(msg, verbose) log(M.LOG_LEVEL.warn, msg, verbose); return true end
function M:logerror(msg, verbose) log(M.LOG_LEVEL.error, msg, verbose); return false end
function M:logfatal(msg, verbose) log(M.LOG_LEVEL.fatal, msg, verbose); return false end
return M

View File

@ -1 +0,0 @@
/usr/share/lua/autowifi/admin

View File

@ -1 +0,0 @@
/usr/share/lua/autowifi/ext/wfcf