diff --git a/Makefile b/Makefile index ef76ff8..a0efb49 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/src/logger.lua b/src/logger.lua new file mode 100644 index 0000000..d529ccf --- /dev/null +++ b/src/logger.lua @@ -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 diff --git a/src/main.lua b/src/main.lua index b3ac39a..7ed0751 100644 --- a/src/main.lua +++ b/src/main.lua @@ -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) diff --git a/src/network/netconfig.lua b/src/network/netconfig.lua index 550eb17..59cea0b 100644 --- a/src/network/netconfig.lua +++ b/src/network/netconfig.lua @@ -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 diff --git a/src/network/wlanconfig.lua b/src/network/wlanconfig.lua index ae19c8b..3124a7a 100644 --- a/src/network/wlanconfig.lua +++ b/src/network/wlanconfig.lua @@ -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 diff --git a/src/script/wfcf b/src/script/d3dapi similarity index 52% rename from src/script/wfcf rename to src/script/d3dapi index ffb527c..754a2cf 100755 --- a/src/script/wfcf +++ b/src/script/d3dapi @@ -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 $@ diff --git a/src/script/autowifi_init b/src/script/wifibox_init similarity index 100% rename from src/script/autowifi_init rename to src/script/wifibox_init diff --git a/src/util.lua b/src/util.lua index c8ada67..eedd891 100644 --- a/src/util.lua +++ b/src/util.lua @@ -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 diff --git a/src/www/captive.keep/admin b/src/www/captive.keep/admin deleted file mode 120000 index 3432e42..0000000 --- a/src/www/captive.keep/admin +++ /dev/null @@ -1 +0,0 @@ -/usr/share/lua/autowifi/admin \ No newline at end of file diff --git a/src/www/captive.keep/cgi-bin/wfcf b/src/www/captive.keep/cgi-bin/wfcf deleted file mode 120000 index 96ba373..0000000 --- a/src/www/captive.keep/cgi-bin/wfcf +++ /dev/null @@ -1 +0,0 @@ -/usr/share/lua/autowifi/ext/wfcf \ No newline at end of file diff --git a/src/www/index.html b/src/www/wifibox/index.html similarity index 100% rename from src/www/index.html rename to src/www/wifibox/index.html diff --git a/src/www/jquery-1.9.1.min.js b/src/www/wifibox/jquery-1.9.1.min.js similarity index 100% rename from src/www/jquery-1.9.1.min.js rename to src/www/wifibox/jquery-1.9.1.min.js diff --git a/src/www/autowifi.css b/src/www/wifibox/wifibox.css similarity index 100% rename from src/www/autowifi.css rename to src/www/wifibox/wifibox.css diff --git a/src/www/autowifi.html b/src/www/wifibox/wifibox.html similarity index 100% rename from src/www/autowifi.html rename to src/www/wifibox/wifibox.html diff --git a/src/www/autowifi.js b/src/www/wifibox/wifibox.js similarity index 100% rename from src/www/autowifi.js rename to src/www/wifibox/wifibox.js