From 3de992c5b7247ac2da9c7129e6037a62a719dcc3 Mon Sep 17 00:00:00 2001 From: Wouter R Date: Thu, 22 Aug 2013 18:34:45 +0200 Subject: [PATCH] Add uhttpd configuration for lua handler; add interface to get/set 'system settings'; add configurable logging to file, with configurable level as well. --- Makefile | 3 ++ post-install.sh | 3 ++ src/main.lua | 54 +++++++++++++++++++++++++++++++++-- src/script/d3dapi | 4 ++- src/script/wifibox.uci.config | 3 ++ src/util/settings.lua | 32 ++++++++++++++++++++- 6 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 src/script/wifibox.uci.config diff --git a/Makefile b/Makefile index a93e0b2..688c872 100644 --- a/Makefile +++ b/Makefile @@ -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/ diff --git a/post-install.sh b/post-install.sh index 4fcddf0..f95bf80 100644 --- a/post-install.sh +++ b/post-install.sh @@ -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 diff --git a/src/main.lua b/src/main.lua index e5e0035..2d6ea14 100644 --- a/src/main.lua +++ b/src/main.lua @@ -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" diff --git a/src/script/d3dapi b/src/script/d3dapi index d9b6856..c851e6a 100755 --- a/src/script/d3dapi +++ b/src/script/d3dapi @@ -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 diff --git a/src/script/wifibox.uci.config b/src/script/wifibox.uci.config new file mode 100644 index 0000000..6e512c1 --- /dev/null +++ b/src/script/wifibox.uci.config @@ -0,0 +1,3 @@ +config settings 'system' + option logfile '/tmp/wifibox.log' + option loglevel 'debug' diff --git a/src/util/settings.lua b/src/util/settings.lua index e8798c0..ef78401 100644 --- a/src/util/settings.lua +++ b/src/util/settings.lua @@ -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