From 039fbbb4c35cf58611a75e667e5d995840153ec0 Mon Sep 17 00:00:00 2001 From: olijf Date: Tue, 5 Apr 2016 14:24:29 +0200 Subject: [PATCH 1/9] Use WPS button to switch configs Added a functionality for the WPS button. (next to the leds) --- Makefile | 4 ++++ src/script/wifi-switch-netconfig.sh | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100755 src/script/wifi-switch-netconfig.sh diff --git a/Makefile b/Makefile index abb567e..6d22833 100644 --- a/Makefile +++ b/Makefile @@ -101,6 +101,10 @@ define Package/wifibox/install #$(INSTALL_DIR) $(1)/www $(INSTALL_DIR) $(1)/www/cgi-bin + #copy wps button script to procd folder + $(INSTALL_DIR) $(1)/etc/rc.button/ + $(INSTALL_BIN) $(WIFIBOX_BASE_DIR)/script/wifi-switch-netconfig.sh $(1)/etc/rc.button/wps + ### create all files in /usr/share/lua/autowifi (autowifi) $(CP) $(WIFIBOX_BASE_DIR)/opkg.conf $(1)/$(TGT_LUA_DIR_SUFFIX)/ diff --git a/src/script/wifi-switch-netconfig.sh b/src/script/wifi-switch-netconfig.sh new file mode 100755 index 0000000..41ebc3a --- /dev/null +++ b/src/script/wifi-switch-netconfig.sh @@ -0,0 +1,22 @@ +#!/bin/sh + +[ "${ACTION}" = "released" ] || exit 0 + +. /lib/functions.sh + +logger "$BUTTON pressed for $SEEN seconds" +if [ "$SEEN" -lt 1 ] +then + #see https://github.com/Doodle3D/doodle3d-firmware/blob/master/src/network/wlanconfig.lua#L188 for reference + #check if network on top is enabled + uci get wireless.@wifi-iface[0].disabled + RESULT=$? + if [ $RESULT -eq 0 ] + then + uci set wireless.@wifi-iface[0].disabled=1 #disable current config + uci set wireless.@wifi-iface[1].disabled=0 #enable last used network + uci reorder wireless.@wifi-iface[0]=2 #reorder networks so last used config goes to the top again + uci commit wireless #commit changes + /etc/init.d/network reload #reload network module so changes become effective + fi +fi From 05bb813f2943774d82f6c05a59d501b3f9baeb97 Mon Sep 17 00:00:00 2001 From: olijf Date: Tue, 5 Apr 2016 16:44:58 +0200 Subject: [PATCH 2/9] Added dhcp config Configure the dhcp server in the correct way, so when switching between wifi configs the it still connect. --- src/script/wifi-switch-netconfig.sh | 32 +++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/src/script/wifi-switch-netconfig.sh b/src/script/wifi-switch-netconfig.sh index 41ebc3a..fba353e 100755 --- a/src/script/wifi-switch-netconfig.sh +++ b/src/script/wifi-switch-netconfig.sh @@ -9,14 +9,38 @@ if [ "$SEEN" -lt 1 ] then #see https://github.com/Doodle3D/doodle3d-firmware/blob/master/src/network/wlanconfig.lua#L188 for reference #check if network on top is enabled - uci get wireless.@wifi-iface[0].disabled - RESULT=$? - if [ $RESULT -eq 0 ] + if [ $(uci get wireless.@wifi-iface[0].mode) == "sta" ]; then + logger "switching to AP" + #configure dhcp + uci delete network.wlan + uci set network.wlan=interface + uci set network.wlan.netmask=$(uci get wifibox.general.network_ap_netmask) + uci set network.wlan.proto=static + uci set network.wlan.ipaddr=$(uci get wifibox.general.network_ap_address) + + uci set dhcp.wlan=dhcp + uci set dhcp.wlan.start=100 + uci set dhcp.wlan.leasetime=12h + uci set dhcp.wlan.limit=150 + uci set dhcp.wlan.interface=wlan + uci set wireless.@wifi-iface[0].disabled=1 #disable current config uci set wireless.@wifi-iface[1].disabled=0 #enable last used network uci reorder wireless.@wifi-iface[0]=2 #reorder networks so last used config goes to the top again - uci commit wireless #commit changes + uci commit #commit changes + /etc/init.d/network reload #reload network module so changes become effective + else + logger "switching to STA $(uci get wireless.@wifi-iface[0].mode)" + uci set wireless.@wifi-iface[0].disabled=1 #disable current config + uci set wireless.@wifi-iface[1].disabled=0 #enable last used network + uci reorder wireless.@wifi-iface[0]=2 #reorder networks so last used config goes to the top again + + uci delete network.wlan + uci set network.wlan=interface + uci set network.wlan.proto=dhcp + uci delete dhcp.wlan + uci commit #commit changes /etc/init.d/network reload #reload network module so changes become effective fi fi From 7397066ad9112607b7f159938c1ff7a07d48ce8b Mon Sep 17 00:00:00 2001 From: olijf Date: Tue, 5 Apr 2016 17:22:37 +0200 Subject: [PATCH 3/9] Added doodle3d-client fix now the status is correctly displayed in the web ui. currently the script works because it assumes certain things. would be nice if i can add some more checks to it. --- src/script/wifi-switch-netconfig.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/script/wifi-switch-netconfig.sh b/src/script/wifi-switch-netconfig.sh index fba353e..b37211f 100755 --- a/src/script/wifi-switch-netconfig.sh +++ b/src/script/wifi-switch-netconfig.sh @@ -8,7 +8,7 @@ logger "$BUTTON pressed for $SEEN seconds" if [ "$SEEN" -lt 1 ] then #see https://github.com/Doodle3D/doodle3d-firmware/blob/master/src/network/wlanconfig.lua#L188 for reference - #check if network on top is enabled + #check if network on top is in STA mode if [ $(uci get wireless.@wifi-iface[0].mode) == "sta" ]; then logger "switching to AP" @@ -30,6 +30,7 @@ then uci reorder wireless.@wifi-iface[0]=2 #reorder networks so last used config goes to the top again uci commit #commit changes /etc/init.d/network reload #reload network module so changes become effective + echo "4|" > /tmp/networkstatus.txt else logger "switching to STA $(uci get wireless.@wifi-iface[0].mode)" uci set wireless.@wifi-iface[0].disabled=1 #disable current config @@ -42,5 +43,6 @@ then uci delete dhcp.wlan uci commit #commit changes /etc/init.d/network reload #reload network module so changes become effective + echo "2|" > /tmp/networkstatus.txt fi fi From 4160441a4bce974c591fd0ce72f8f3099f719b56 Mon Sep 17 00:00:00 2001 From: olijf Date: Thu, 7 Apr 2016 11:16:31 +0200 Subject: [PATCH 4/9] Fixed check to see if it has to switch to ap mode --- src/script/wifi-switch-netconfig.sh | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/script/wifi-switch-netconfig.sh b/src/script/wifi-switch-netconfig.sh index b37211f..570cd9b 100755 --- a/src/script/wifi-switch-netconfig.sh +++ b/src/script/wifi-switch-netconfig.sh @@ -9,9 +9,13 @@ if [ "$SEEN" -lt 1 ] then #see https://github.com/Doodle3D/doodle3d-firmware/blob/master/src/network/wlanconfig.lua#L188 for reference #check if network on top is in STA mode - if [ $(uci get wireless.@wifi-iface[0].mode) == "sta" ]; + if [ $(uci get wireless.@wifi-iface[1].mode) == "ap" ] then logger "switching to AP" + if [ $(uci get wireless.@wifi-iface[1].network) != "wlan" ] #edge case when only the factory default openwrt network is available + then + uci set wireless.@wifi-iface[1].network=wlan + fi #configure dhcp uci delete network.wlan uci set network.wlan=interface From 52650da0fe6e8dfeb42e246a5407539607ce15f0 Mon Sep 17 00:00:00 2001 From: olijf Date: Thu, 7 Apr 2016 16:23:22 +0200 Subject: [PATCH 5/9] Changed the script so that the wifinetworks rotate --- src/script/wifi-switch-netconfig.sh | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/script/wifi-switch-netconfig.sh b/src/script/wifi-switch-netconfig.sh index 570cd9b..352686f 100755 --- a/src/script/wifi-switch-netconfig.sh +++ b/src/script/wifi-switch-netconfig.sh @@ -4,6 +4,13 @@ . /lib/functions.sh +handle_interface() { + local config="$1" + logger $config + count=$((count + 1)) + # run commands for every interface section +} + logger "$BUTTON pressed for $SEEN seconds" if [ "$SEEN" -lt 1 ] then @@ -11,7 +18,7 @@ then #check if network on top is in STA mode if [ $(uci get wireless.@wifi-iface[1].mode) == "ap" ] then - logger "switching to AP" + logger "switching to AP $(uci get wireless.@wifi-iface[1].ssid)" if [ $(uci get wireless.@wifi-iface[1].network) != "wlan" ] #edge case when only the factory default openwrt network is available then uci set wireless.@wifi-iface[1].network=wlan @@ -31,15 +38,26 @@ then uci set wireless.@wifi-iface[0].disabled=1 #disable current config uci set wireless.@wifi-iface[1].disabled=0 #enable last used network - uci reorder wireless.@wifi-iface[0]=2 #reorder networks so last used config goes to the top again + + config_load wireless + config_foreach handle_interface wifi-iface + logger $count + + uci reorder wireless.@wifi-iface[0]=$count #reorder networks so last used config goes to the top again uci commit #commit changes /etc/init.d/network reload #reload network module so changes become effective + logger "setting status flag in /tmp/networkstatus.txt" echo "4|" > /tmp/networkstatus.txt else - logger "switching to STA $(uci get wireless.@wifi-iface[0].mode)" + logger "switching to STA $(uci get wireless.@wifi-iface[1].ssid)" uci set wireless.@wifi-iface[0].disabled=1 #disable current config uci set wireless.@wifi-iface[1].disabled=0 #enable last used network - uci reorder wireless.@wifi-iface[0]=2 #reorder networks so last used config goes to the top again + + config_load wireless + config_foreach handle_interface wifi-iface + logger $count + + uci reorder wireless.@wifi-iface[0]=$count #reorder networks so last used config goes to the top again uci delete network.wlan uci set network.wlan=interface From a8bdd5c188b7acec42bdcd7dcf92d6f25d15ca77 Mon Sep 17 00:00:00 2001 From: olijf Date: Fri, 8 Apr 2016 14:47:06 +0200 Subject: [PATCH 6/9] Changed logic Now instead of rotinging through all wifi configs the button just removes all other configs and only leaves the doodle3d AP --- src/script/wifi-switch-netconfig.sh | 67 +++++++++++++---------------- 1 file changed, 30 insertions(+), 37 deletions(-) diff --git a/src/script/wifi-switch-netconfig.sh b/src/script/wifi-switch-netconfig.sh index 352686f..78f72b6 100755 --- a/src/script/wifi-switch-netconfig.sh +++ b/src/script/wifi-switch-netconfig.sh @@ -4,24 +4,43 @@ . /lib/functions.sh -handle_interface() { +count_networks() { local config="$1" - logger $config - count=$((count + 1)) - # run commands for every interface section + logger $config + count=$((count + 1)) + # run commands for every interface section +} +find_network() { + local config="$1" + succes=false + #logger $(uci get wireless.$config.ssid) + network_name=$(uci get wireless.$config.ssid) + if [ ${network_name:0:8} != "Doodle3D" ] + then + logger "deleting network $network_name" + uci delete wireless.$config + else + $succes=true + logger "enabled $network_name" + uci set wireless.$config.disabled=0 + fi } logger "$BUTTON pressed for $SEEN seconds" -if [ "$SEEN" -lt 1 ] +if [ "$SEEN" -gt 4 ] then - #see https://github.com/Doodle3D/doodle3d-firmware/blob/master/src/network/wlanconfig.lua#L188 for reference - #check if network on top is in STA mode - if [ $(uci get wireless.@wifi-iface[1].mode) == "ap" ] + #count number of networks + config_load wireless + config_foreach count_networks wifi-iface + logger $count + #if number of networks is less than 1 (or 1) do nothing + if [ $count -gt 1 ] then - logger "switching to AP $(uci get wireless.@wifi-iface[1].ssid)" - if [ $(uci get wireless.@wifi-iface[1].network) != "wlan" ] #edge case when only the factory default openwrt network is available + logger "switching to AP" + config_foreach find_network wifi-iface + if [ $succes != false ] then - uci set wireless.@wifi-iface[1].network=wlan + logger "wireless somehow not found" fi #configure dhcp uci delete network.wlan @@ -36,35 +55,9 @@ then uci set dhcp.wlan.limit=150 uci set dhcp.wlan.interface=wlan - uci set wireless.@wifi-iface[0].disabled=1 #disable current config - uci set wireless.@wifi-iface[1].disabled=0 #enable last used network - - config_load wireless - config_foreach handle_interface wifi-iface - logger $count - - uci reorder wireless.@wifi-iface[0]=$count #reorder networks so last used config goes to the top again uci commit #commit changes /etc/init.d/network reload #reload network module so changes become effective logger "setting status flag in /tmp/networkstatus.txt" echo "4|" > /tmp/networkstatus.txt - else - logger "switching to STA $(uci get wireless.@wifi-iface[1].ssid)" - uci set wireless.@wifi-iface[0].disabled=1 #disable current config - uci set wireless.@wifi-iface[1].disabled=0 #enable last used network - - config_load wireless - config_foreach handle_interface wifi-iface - logger $count - - uci reorder wireless.@wifi-iface[0]=$count #reorder networks so last used config goes to the top again - - uci delete network.wlan - uci set network.wlan=interface - uci set network.wlan.proto=dhcp - uci delete dhcp.wlan - uci commit #commit changes - /etc/init.d/network reload #reload network module so changes become effective - echo "2|" > /tmp/networkstatus.txt fi fi From 2b097996e0fc3c3f584575e922cc355cff3fc13e Mon Sep 17 00:00:00 2001 From: olijf Date: Tue, 12 Apr 2016 12:05:01 +0200 Subject: [PATCH 7/9] Added LUA API call Added Lua API call to reset the network configuration. --- src/rest/api/api_network.lua | 14 ++++++++ src/script/wifi-switch-netconfig.sh | 55 ++--------------------------- 2 files changed, 16 insertions(+), 53 deletions(-) diff --git a/src/rest/api/api_network.lua b/src/rest/api/api_network.lua index 2c4992a..e265a5d 100644 --- a/src/rest/api/api_network.lua +++ b/src/rest/api/api_network.lua @@ -218,4 +218,18 @@ function M.alive(request, response) response:setSuccess("alive") end +function M.reset(request, response) + response:setSuccess("Resetting networks") + + log:info(MOD_ABBR, "Resetting networks") + for _, net in ipairs(wifi.getConfigs()) do + print(net.ssid) + wifi.removeConfig(net.ssid) + end + + local ssid = wifi.getSubstitutedSsid(settings.get('network.ap.ssid')) + netconf.setupAccessPoint(ssid) + netconf.enableAccessPoint(ssid) +end + return M diff --git a/src/script/wifi-switch-netconfig.sh b/src/script/wifi-switch-netconfig.sh index 78f72b6..a516308 100755 --- a/src/script/wifi-switch-netconfig.sh +++ b/src/script/wifi-switch-netconfig.sh @@ -4,60 +4,9 @@ . /lib/functions.sh -count_networks() { - local config="$1" - logger $config - count=$((count + 1)) - # run commands for every interface section -} -find_network() { - local config="$1" - succes=false - #logger $(uci get wireless.$config.ssid) - network_name=$(uci get wireless.$config.ssid) - if [ ${network_name:0:8} != "Doodle3D" ] - then - logger "deleting network $network_name" - uci delete wireless.$config - else - $succes=true - logger "enabled $network_name" - uci set wireless.$config.disabled=0 - fi -} - logger "$BUTTON pressed for $SEEN seconds" if [ "$SEEN" -gt 4 ] then - #count number of networks - config_load wireless - config_foreach count_networks wifi-iface - logger $count - #if number of networks is less than 1 (or 1) do nothing - if [ $count -gt 1 ] - then - logger "switching to AP" - config_foreach find_network wifi-iface - if [ $succes != false ] - then - logger "wireless somehow not found" - fi - #configure dhcp - uci delete network.wlan - uci set network.wlan=interface - uci set network.wlan.netmask=$(uci get wifibox.general.network_ap_netmask) - uci set network.wlan.proto=static - uci set network.wlan.ipaddr=$(uci get wifibox.general.network_ap_address) - - uci set dhcp.wlan=dhcp - uci set dhcp.wlan.start=100 - uci set dhcp.wlan.leasetime=12h - uci set dhcp.wlan.limit=150 - uci set dhcp.wlan.interface=wlan - - uci commit #commit changes - /etc/init.d/network reload #reload network module so changes become effective - logger "setting status flag in /tmp/networkstatus.txt" - echo "4|" > /tmp/networkstatus.txt - fi + logger "Resetting Wireless" + /usr/share/lua/wifibox/script/d3dapi p=/network/reset r=GET fi From d3d8ebdeda7f28912fc8b7ca080f8130a822c627 Mon Sep 17 00:00:00 2001 From: olijf Date: Tue, 12 Apr 2016 12:22:14 +0200 Subject: [PATCH 8/9] Changed method from GET to POST --- src/rest/api/api_network.lua | 3 +-- src/script/wifi-switch-netconfig.sh | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/rest/api/api_network.lua b/src/rest/api/api_network.lua index e265a5d..4deec6c 100644 --- a/src/rest/api/api_network.lua +++ b/src/rest/api/api_network.lua @@ -218,12 +218,11 @@ function M.alive(request, response) response:setSuccess("alive") end -function M.reset(request, response) +function M.reset_POST(request, response) response:setSuccess("Resetting networks") log:info(MOD_ABBR, "Resetting networks") for _, net in ipairs(wifi.getConfigs()) do - print(net.ssid) wifi.removeConfig(net.ssid) end diff --git a/src/script/wifi-switch-netconfig.sh b/src/script/wifi-switch-netconfig.sh index a516308..6a59613 100755 --- a/src/script/wifi-switch-netconfig.sh +++ b/src/script/wifi-switch-netconfig.sh @@ -8,5 +8,5 @@ logger "$BUTTON pressed for $SEEN seconds" if [ "$SEEN" -gt 4 ] then logger "Resetting Wireless" - /usr/share/lua/wifibox/script/d3dapi p=/network/reset r=GET + /usr/share/lua/wifibox/script/d3dapi p=/network/reset r=POST fi From 205fc7460d8fdff29b670e56b2ca727fb56d52e7 Mon Sep 17 00:00:00 2001 From: olijf Date: Tue, 12 Apr 2016 12:49:38 +0200 Subject: [PATCH 9/9] Added symlink to d3dapi --- Makefile | 1 + post-install.sh | 1 - src/script/wifi-switch-netconfig.sh | 2 +- 3 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 6d22833..c89fe98 100644 --- a/Makefile +++ b/Makefile @@ -122,6 +122,7 @@ define Package/wifibox/install $(INSTALL_BIN) $(WIFIBOX_BASE_DIR)/script/wifibox_init $(1)/etc/init.d/wifibox $(INSTALL_BIN) $(WIFIBOX_BASE_DIR)/script/dhcpcheck_init $(1)/etc/init.d/dhcpcheck $(INSTALL_BIN) $(WIFIBOX_BASE_DIR)/script/d3dapi $(1)/$(TGT_LUA_DIR_SUFFIX)/script + $(LN) -s /$(TGT_LUA_DIR_SUFFIX)/script/d3dapi $(1)/bin/d3dapi $(INSTALL_BIN) $(WIFIBOX_BASE_DIR)/script/signin.sh $(1)/$(TGT_LUA_DIR_SUFFIX)/script $(CP) $(WIFIBOX_BASE_DIR)/script/logrotate-wifibox.conf $(1)/etc/logrotate.d/wifibox.conf diff --git a/post-install.sh b/post-install.sh index 2f4282f..b273471 100644 --- a/post-install.sh +++ b/post-install.sh @@ -55,7 +55,6 @@ if [ $? -gt 0 ]; then cat <<-\EOM >> $IPKG_INSTROOT/root/.profile # DO NOT MODIFY - this block of lines has been added by the wifibox package. alias d='ls -la' - alias d3dapi='/usr/share/lua/wifibox/script/d3dapi' alias encore='ulimit -c unlimited' alias wopkg='opkg -f /usr/share/lua/wifibox/opkg.conf' diff --git a/src/script/wifi-switch-netconfig.sh b/src/script/wifi-switch-netconfig.sh index 6a59613..8589d46 100755 --- a/src/script/wifi-switch-netconfig.sh +++ b/src/script/wifi-switch-netconfig.sh @@ -8,5 +8,5 @@ logger "$BUTTON pressed for $SEEN seconds" if [ "$SEEN" -gt 4 ] then logger "Resetting Wireless" - /usr/share/lua/wifibox/script/d3dapi p=/network/reset r=POST + d3dapi p=/network/reset r=POST fi