diff --git a/Makefile b/Makefile index 66c2138..a93e0b2 100644 --- a/Makefile +++ b/Makefile @@ -16,7 +16,7 @@ IncludeWithNewlines = $(subst # Name and release number of this package PKG_NAME := wifibox PKG_VERSION := 0.1.0 -PKG_RELEASE := 6 +PKG_RELEASE := 7 # This specifies the directory where we're going to build the program. # The root build directory, $(BUILD_DIR), is by default the build_mipsel @@ -85,7 +85,6 @@ define Package/wifibox/install $(INSTALL_DIR) $(1)/$(TGT_LUA_DIR_SUFFIX)/rest/api $(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)/etc/init.d $(INSTALL_DIR) $(1)/www/cgi-bin @@ -98,11 +97,12 @@ define Package/wifibox/install $(CP) $(WIFIBOX_BASE_DIR)/rest/api/*.lua $(1)/$(TGT_LUA_DIR_SUFFIX)/rest/api/ $(CP) $(WIFIBOX_BASE_DIR)/util/*.lua $(1)/$(TGT_LUA_DIR_SUFFIX)/util/ - $(INSTALL_BIN) $(WIFIBOX_BASE_DIR)/script/wifibox_init $(1)/$(TGT_LUA_DIR_SUFFIX)/script + $(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 +# $(INSTALL_DIR) $(1)/$(TGT_LUA_DIR_SUFFIX)/www # $(CP) $(WIFIBOX_BASE_DIR)/www/* $(1)/$(TGT_LUA_DIR_SUFFIX)/www/ - $(CP) $(WIFIBOX_BASE_DIR)/www/* $(1)/www/ #copy www files directly to /www + $(CP) $(WIFIBOX_BASE_DIR)/www/* $(1)/www/ #copy www files directly to /www ifeq ($(CONFIG_WIFIBOX_DEVEL_PACKAGE),y) # $(INSTALL_DIR) $(1)/$(TGT_LUA_DIR_SUFFIX)/misc @@ -115,9 +115,7 @@ endif ### create links elsewhere in the system (autowifi) - $(LN) -s /$(TGT_LUA_DIR_SUFFIX)/script/d3dapi $(1)/www/cgi-bin - $(LN) -s /$(TGT_LUA_DIR_SUFFIX)/script/wifibox_init $(1)/etc/init.d/wifibox ### install gpx utility $(INSTALL_DIR) $(1)/usr/bin diff --git a/post-install.sh b/post-install.sh index 8b30694..4fcddf0 100644 --- a/post-install.sh +++ b/post-install.sh @@ -49,7 +49,7 @@ EOM fi ### Add some convenience aliases to root's profile -mkdir $IPKG_INSTROOT/root +mkdir -p $IPKG_INSTROOT/root grep '^# DO NOT MODIFY.*wifibox package.$' $IPKG_INSTROOT/root/.profile >/dev/null 2>&1 if [ $? -gt 0 ]; then cat <<-EOM >> $IPKG_INSTROOT/root/.profile @@ -63,7 +63,7 @@ fi ### Finally make sure basic configuration is set correctly -/etc/init.d/wifibox enable +$IPKG_INSTROOT/etc/init.d/wifibox enable if [ -z "$IPKG_INSTROOT" ]; then echo "Enabling wifi device..." diff --git a/post-remove.sh b/post-remove.sh index e68350a..a024576 100644 --- a/post-remove.sh +++ b/post-remove.sh @@ -4,15 +4,25 @@ if [ -f /etc/banner.default ]; then mv /etc/banner.default /etc/banner fi -rmdir /usr/share/lua/wifibox/www/wifibox -rmdir /usr/share/lua/wifibox/www rmdir /usr/share/lua/wifibox/network +rmdir /usr/share/lua/wifibox/rest/api rmdir /usr/share/lua/wifibox/rest rmdir /usr/share/lua/wifibox/script +rmdir /usr/share/lua/wifibox/test/www 2> /dev/null +rmdir /usr/share/lua/wifibox/test 2> /dev/null rmdir /usr/share/lua/wifibox/util rmdir /usr/share/lua/wifibox rmdir /usr/share/lua +# Try to remove all www directories used by the package, and intentionally leave anything non-empty alone +rmdir /www/js/libs +rmdir /www/js +rmdir /www/img/buttons +rmdir /www/img +rmdir /www/css +rm /www/test 2> /dev/null +rmdir /www/cgi-bin + echo "The wifibox banner has been removed. Changes to the root profile however, have" echo "not been reverted, as haven't the wlan firewall zone and the radio0 device state." echo "NOTE: config changes have not been implemented yet." diff --git a/pre-remove.sh b/pre-remove.sh index 652d333..f39ecfa 100644 --- a/pre-remove.sh +++ b/pre-remove.sh @@ -1,7 +1,5 @@ #!/bin/sh -if [ -z "$$IPKG_INSTROOT" ]; then - /etc/init.d/wifibox disable -fi +$IPKG_INSTROOT/etc/init.d/wifibox disable exit 0 diff --git a/src/main.lua b/src/main.lua index 348602c..8c4abce 100644 --- a/src/main.lua +++ b/src/main.lua @@ -1,8 +1,9 @@ package.path = package.path .. ';/usr/share/lua/wifibox/?.lua' local confDefaults = require('conf_defaults') -local util = require('util.utils') local log = require('util.logger') +local settings = require('util.settings') +local util = require('util.utils') local wifi = require('network.wlanconfig') local netconf = require('network.netconfig') local RequestClass = require('rest.request') @@ -11,8 +12,66 @@ local ResponseClass = require('rest.response') local postData = nil +-- expects list with tables containing 'ssid' key as values and returns index key if found or nil if not found +local function findSsidInList(list, name) + for k,v in ipairs(list) do + if v.ssid == name then return k end + end + return nil +end + local function setupAutoWifiMode() - io.write("--TODO: join known network if present, fall back to access point otherwise\n") + local wifiState = wifi.getDeviceState() + local netName, netMode = wifiState.ssid, wifiState.mode + + local apSsid = wifi.getSubstitutedSsid(settings.get('network.ap.ssid')) + local apMode = (apSsid == netName) and netMode == 'ap' + + local scanList,msg = wifi.getScanInfo() + local knownSsids = wifi.getConfigs() + + if not scanList then + return nil, "autowifi: could not scan wifi networks (" .. msg .. ")" + end + + -- START TEMP -- mode should be ap or sta + print("wifi name: " .. netName .. ", wifi mode: " .. netMode .. ", expected AP ssid: " .. apSsid .. ", apmode: " .. (apMode and "yes" or "no")) + for _,sn in ipairs(scanList) do + print("avl net: " .. sn.ssid) + end + for _,kn in ipairs(knownSsids) do + print("known net: " .. kn.ssid .. " (mode: " .. kn.mode .. ")") + end + -- END TEMP + + -- if the currently active network is client mode and is also visible, do nothing since it will connect automatically further along the boot process + if netMode == 'sta' and findSsidInList(scanList, netName) then + return true, "autowifi: no action - existing configuration found for currently wifi visible network (" .. netName .. ")" + end + + -- try to find a known network which is also visible (ordered by known network definitions) + local connectWith = nil + for _,kn in ipairs(knownSsids) do + if findSsidInList(scanList, kn.ssid) then + connectWith = kn.ssid + break + end + end + + if connectWith then + print("connectWith: " .. connectWith) --TEMP + -- TODO: refactor connect stuff into network:connect() function and adapt api_network_associate as well (and others?) + -- TODO: connect with network + -- return true, "autowifi: associated -- client mode with ssid '" .. connectWith .. "'" + elseif netMode ~= 'ap' then + print("shouldBeAp") --TEMP + -- TODO: setup AP (refactor into network like with client connect) + -- return true, "autowifi: configured as access point with ssid '" .. apSsid .. "'" + else + return true, "autowifi: no action - no known networks found, already in access point mode" + end + + return nil, "autowifi: uh oh - bad situation in autowifi function" end local function init() @@ -57,7 +116,14 @@ end end if rq:getRequestMethod() == 'CMDLINE' and rq:get('autowifi') ~= nil then - setupAutoWifiMode() + log:info("running in autowifi mode") + local rv,msg = setupAutoWifiMode() + + if rv then + log:info("autowifi setup done (" .. msg .. ")") + else + log:error("autowifi setup failed (" .. msg .. ")") + end elseif rq:getRequestMethod() ~= 'CMDLINE' or confDefaults.DEBUG_API then local response, err = rq:handle() diff --git a/src/rest/request.lua b/src/rest/request.lua index 54f11fe..8f477b7 100644 --- a/src/rest/request.lua +++ b/src/rest/request.lua @@ -174,8 +174,8 @@ function M.new(postData, debugEnabled) self.postArgs = kvTableFromUrlEncodedString(postData) self.pathArgs = arrayFromPath(os.getenv('PATH_INFO')) - -- override path arguments with command line parameter and allow to emulate GET/POST if debugging is enabled - if debugEnabled and self.requestMethod == 'CMDLINE' then + -- override path arguments with command line parameter and allow to emulate GET/POST if debugging is enabled *and* if the autowifi special command wasn't mentioned + if debugEnabled and self.requestMethod == 'CMDLINE' and self:get('autowifi') == nil then self.pathArgs = arrayFromPath(self.cmdLineArgs['p']) if self.cmdLineArgs['r'] == 'GET' or self.cmdLineArgs['r'] == nil then diff --git a/src/script/wifibox_init b/src/script/wifibox_init index 75f1ec4..0e8382f 100755 --- a/src/script/wifibox_init +++ b/src/script/wifibox_init @@ -4,14 +4,14 @@ START=18 LOGGER="logger -s -t autowifi -p 6" boot() { - $LOGGER "dummy boot" + $LOGGER "Invoking Doodle3D WiFi box network auto-initialization..." /usr/share/lua/wifibox/script/d3dapi autowifi } -start() { - $LOGGER "dummy start" -} +#start() { +# $LOGGER "dummy start" +#} -stop() { - $LOGGER "dummy stop" -} +#stop() { +# $LOGGER "dummy stop" +#} diff --git a/src/util/setportspeed/setportspeed.c b/src/util/setportspeed/setportspeed.c index 3a8b31a..06cbf95 100644 --- a/src/util/setportspeed/setportspeed.c +++ b/src/util/setportspeed/setportspeed.c @@ -10,7 +10,7 @@ #include #include -/* these includes are for ioctl() and cfmakeraw() but disabled to avoid conflicts with asm/* includes */ +//these includes are for ioctl() and cfmakeraw() but disabled to avoid conflicts with asm/* includes //#include //#include