Add uhttpd configuration for lua handler; add interface to get/set 'system settings'; add configurable logging to file, with configurable level as well.

This commit is contained in:
Wouter R 2013-08-22 18:34:45 +02:00
parent 0730b442ac
commit 3de992c5b7
6 changed files with 95 additions and 4 deletions

View File

@ -86,6 +86,7 @@ define Package/wifibox/install
$(INSTALL_DIR) $(1)/$(TGT_LUA_DIR_SUFFIX)/script
$(INSTALL_DIR) $(1)/$(TGT_LUA_DIR_SUFFIX)/util
$(INSTALL_DIR) $(1)/etc/init.d
$(INSTALL_DIR) $(1)/etc/config
$(INSTALL_DIR) $(1)/www/cgi-bin
### create all files in /usr/share/lua/autowifi (autowifi)
@ -99,6 +100,8 @@ define Package/wifibox/install
$(INSTALL_BIN) $(WIFIBOX_BASE_DIR)/script/wifibox_init $(1)/etc/init.d/wifibox # copy directly to init dir (required for post-inst enabling)
$(INSTALL_BIN) $(WIFIBOX_BASE_DIR)/script/d3dapi $(1)/$(TGT_LUA_DIR_SUFFIX)/script
$(CP) $(WIFIBOX_BASE_DIR)/script/wifibox.uci.config $(1)/etc/config/wifibox # copy base configuration to uci config dir
# $(INSTALL_DIR) $(1)/$(TGT_LUA_DIR_SUFFIX)/www
# $(CP) $(WIFIBOX_BASE_DIR)/www/* $(1)/$(TGT_LUA_DIR_SUFFIX)/www/

View File

@ -82,6 +82,9 @@ else
uci set network.lan.ipaddr=192.168.5.1
echo -e "beta\nbeta" | passwd root
uci set uhttpd.main.lua_handler='/usr/share/lua/wifibox/main.lua'
uci set uhttpd.main.lua_prefix='/d3dapi'
uci set wireless.@wifi-device[0].disabled=0
# TODO: add firewall net
uci set network.wlan=interface

View File

@ -81,9 +81,59 @@ local function setupAutoWifiMode()
return nil, "autowifi: uh oh - bad situation in autowifi function"
end
local function setupLogger()
local logStream = io.stderr -- use stderr as hard-coded default target
local logLevel = log.LEVEL.debug -- use debug logging as hard-coded default level
local logTargetSetting = settings.getSystemKey('logfile')
local logLevelSetting = settings.getSystemKey('loglevel')
local logTargetError, logLevelError = nil, nil
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+')
if f then logStream = f
else logTargetError = msg
end
end
end
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
log:init(logLevel)
log:setStream(logStream)
local rv = true
if logTargetError then
log:error("could not open logfile '" .. logTargetSetting .. "', using stderr as fallback (" .. logTargetError .. ")")
rv = false
end
if logLevelError then
log:error("uci config specifies invalid log level '" .. logLevelSetting .. "', using debug level as fallback")
rv = false
end
return rv
end
local function init(environment)
log:init(log.LEVEL.debug)
log:setStream(io.stderr)
setupLogger()
local dbgText = ""
if confDefaults.DEBUG_API and confDefaults.DEBUG_PCALLS then dbgText = "pcall and api"

View File

@ -1,5 +1,7 @@
#!/bin/sh
#chmod 755 /www/cgi-bin/d3dapi
# NOTE: redirection to $LOG_FILE is not required anymore, it is configurable now.
# It is left in just as a precaution for a badly configured firmware invocation.
LUA=lua
SCRIPT_PATH=/usr/share/lua/wifibox

View File

@ -0,0 +1,3 @@
config settings 'system'
option logfile '/tmp/wifibox.log'
option loglevel 'debug'

View File

@ -26,9 +26,12 @@ local UCI_CONFIG_FILE = '/etc/config/' .. UCI_CONFIG_NAME
--- Section type that will be used in UCI\_CONFIG\_FILE
local UCI_CONFIG_TYPE = 'settings'
--- Section name that will be used in UCI\_CONFIG\_FILE
--- Section name that will be used for 'public' settings (as predefined in conf_defaults.lua) in UCI\_CONFIG\_FILE
local UCI_CONFIG_SECTION = 'general'
--- Section name that will be used for 'firmware-local' settings in UCI\_CONFIG\_FILE
local UCI_CONFIG_SYSTEM_SECTION = 'system'
local ERR_NO_SUCH_KEY = "key does not exist"
@ -215,4 +218,31 @@ function M.set(key, value)
return true
end
--- Returns a UCI configuration key from the system section.
-- @tparam string key The key for which to return the value, must be non-empty.
-- @return Requested value or false if it does not exist or nil on invalid key.
function M.getSystemKey(key)
if type(key) ~= 'string' or key:len() == 0 then return nil end
local v = uci:get(UCI_CONFIG_NAME, UCI_CONFIG_SYSTEM_SECTION, key)
return v or false
end
--- Sets the given key to the given value.
-- Note that unlike the public settings, system keys are untyped and value must
-- be of type string; UCI generally uses '1' and '0' for boolean values.
-- @tparam string key The key to set, must be non-empty.
-- @tparam string value The value to set key to.
-- @return True on success or false if key or value arguments are invalid.
function M.setSystemKey(key, value)
if type(key) ~= 'string' or key:len() == 0 then return nil end
if type(value) ~= 'string' then return nil end
local r = utils.create(UCI_CONFIG_FILE) -- make sure the file exists for uci to write to
uci:set(UCI_CONFIG_NAME, UCI_CONFIG_SYSTEM_SECTION, UCI_CONFIG_TYPE)
uci:set(UCI_CONFIG_NAME, UCI_CONFIG_SYSTEM_SECTION, key, value)
uci:commit(UCI_CONFIG_NAME)
return true
end
return M