Skip to content

Instantly share code, notes, and snippets.

@jefferyto
Last active February 3, 2024 08:30
Show Gist options
  • Star 30 You must be signed in to star a gist
  • Fork 15 You must be signed in to fork a gist
  • Save jefferyto/8010733 to your computer and use it in GitHub Desktop.
Save jefferyto/8010733 to your computer and use it in GitHub Desktop.
Switching network configurations on a TP-Link TL-MR3020 with OpenWrt (12.09 Attitude Adjustment) using the sliding switch. Based on the sample scripts at: https://forum.openwrt.org/viewtopic.php?pid=172111#p172111 and https://forum.openwrt.org/viewtopic.php?pid=172110#p172110
# based on
# https://dev.openwrt.org/browser/trunk/target/linux/atheros/base-files/etc/hotplug.d/button/00-button
# https://forum.openwrt.org/viewtopic.php?pid=172110#p172110
. /lib/functions.sh
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
do_button () {
local button
local action
local handler
local min
local max
config_get button $1 button
config_get action $1 action
config_get handler $1 handler
config_get min $1 min
config_get max $1 max
if [ "$button" = "3g" -o "$button" = "wisp" -o "$button" = "ap" ] ; then
if [ "$ACTION" = "released" ] ; then
if [ "$BUTTON" = "BTN_1" ] ; then
export BUTTON=3g
export ACTION=pressed
elif [ "$BUTTON" = "BTN_0" ] ; then
export BUTTON=wisp
export ACTION=pressed
fi
elif [ "$BUTTON" = "BTN_0" -o "$BUTTON" = "BTN_1" ] ; then
if BTN_0_pressed && BTN_1_pressed ; then
export BUTTON=ap
fi
fi
fi
[ "$ACTION" = "$action" -a "$BUTTON" = "$button" -a -n "$handler" ] && {
[ -z "$min" -o -z "$max" ] && eval $handler
[ -n "$min" -a -n "$max" ] && {
[ $min -le $SEEN -a $max -ge $SEEN ] && eval $handler
}
}
}
config_load system
config_foreach do_button button
#!/bin/sh
grep -qe "sw1.*in hi" /sys/kernel/debug/gpio
#!/bin/sh
grep -qe "sw2.*in hi" /sys/kernel/debug/gpio
#!/bin/sh /etc/rc.common
START=15
boot() {
/usr/local/sbin/set_network_config
}
#!/bin/sh
# based on https://forum.openwrt.org/viewtopic.php?pid=172111#p172111
ME=$(basename $0)
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
if BTN_1_pressed ; then
if BTN_0_pressed ; then
CONFIG=ap
else
CONFIG=wisp
fi
else
CONFIG=3g
fi
SRC="/etc/config/$CONFIG"
DEST=/tmp/network_config
if [ ! -d "$SRC" ] ; then
logger -t "$ME" -p "user.warn" "$SRC is not a directory"
exit 1
fi
if [ -L "$DEST" ] ; then
CUR=$(cd "$DEST"; pwd -P)
if [ "$CUR" = "$SRC" ] ; then
logger -t "$ME" "Already using $CONFIG config"
exit 2
fi
fi
logger -t "$ME" "Using $CONFIG config"
ln -sfn "$SRC" "$DEST"
chmod a+x BTN_0_pressed BTN_1_pressed set_network_config update_network_config network_config
mkdir -p /usr/local/sbin
mv BTN_0_pressed BTN_1_pressed set_network_config update_network_config /usr/local/sbin
mkdir -p /etc/hotplug.d/button
mv 00-button /etc/hotplug.d/button
mv network_config /etc/init.d
opkg update
opkg install flock
cd /etc/config
mkdir 3g
mv network wireless 3g
cp -r 3g wisp
cp -r 3g ap
ln -s network /tmp/network_config/network
ln -s wireless /tmp/network_config/wireless
# configure files in 3g / wisp / ap directories for each corresponding mode
# to use uci / luci to configure, create the /tmp/network_config symbolic link to point to the correct directory, e.g.
# ln -s /tmp/network_config /etc/config/3g
# or configure the settings before moving the config files
/etc/init.d/network_config enable
uci add system button
uci set system.@button[-1].button=3g
uci set system.@button[-1].action=pressed
uci set system.@button[-1].handler=update_network_config
uci add system button
uci set system.@button[-1].button=wisp
uci set system.@button[-1].action=pressed
uci set system.@button[-1].handler=update_network_config
uci add system button
uci set system.@button[-1].button=ap
uci set system.@button[-1].action=pressed
uci set system.@button[-1].handler=update_network_config
uci commit system
reboot
#!/bin/sh
ME=$(basename $0)
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
(
flock -n 9 || exit 1
(
sleep 1
if set_network_config ; then
. /etc/diag.sh
get_status_led
status_led_set_timer 200 200
logger -t "$ME" "Reloading network"
/etc/init.d/network restart
status_led_on
fi
) 9>&-
) 9>/tmp/update_network_config.lock
@abstrask
Copy link

Hi Jeffery,

Thanks for your work - it works like a charm!

I don't have any experience with shell scripts, so it took me a while to figure exactly how to install your scripts.

If I understand 'ln' and what you're trying to do correctly, I belive you mixed up the paths in line 16-17 of the setup script. So:

ln -s network /tmp/network_config/network --> ln -s /tmp/network_config/network network
ln -s wireless /tmp/network_config/wireless --> ln -s /tmp/network_config/wireless wireless

At least that works for me, whereas the original produced an error.

Now, I don't know which version of OpenWrt you wrote this for, but the workaround to direct LuCI configuration changes to one of the three stored configurations, doesn't seem to work in Barrier Breaker 14.07 - LuCI simply overwrites the symbolic links with the updated configuration files.

With that in mind, I'm thinking of modifying the scripts, so that the configuration is actually copied when the switch change, rather than updating the symbolic links. The goal would be be able to reconfigure using LuCI, yet being able to preserve the changes in the current config. A couple of questions spring to mind:

  1. Is there a particular reason why you chose symbolic links, rather than copying the actual files? Do you see a problem with overwriting current config file as you change configuration, instead of updating the symbolic links?

  2. In order to capture configuration changes, I would need to trigger a copy of the config files (to the folder of the current config), when LuCI updates the files, but I'm not aware of how to do that. Any ideas?

Hope to hear from you and thanks again, for all the groundwork! :)

@abstrask
Copy link

After putting my questions into writing, I had a few ideas on my own and have made a fork of your gist.

I have solved by adding a 'commit' script, that is triggered using the WPS button. It copies the current network config, to the subfolder corresponding to the sliding switch's current position.

Thanks!

@danielo515
Copy link

Hello,

I find this approach a bit too complex compared to the scripts explained here:
http://www.linux-magazine.com/Online/Features/The-One-Watt-Server

Could you let me know any disadvantage of the one I linked?
Regards

@anastazya
Copy link

Somehow i cannot get this setup to detect "ap" pressed.. Any hints ? i just used your scripts, except that in /etc/config/system , i just log the actions , i will define other handlers as needed . Like so :

config button
option button '3g'
option action 'pressed'
option handler 'logger 3g selected'

config button
option button 'wisp'
option action 'pressed'
option handler 'logger Wisp selected'

config button
option button 'ap'
option action 'pressed'
option handler 'logger AP selected'

Thanks !!

@cesarmorisco
Copy link

Hello
I clicked the mjpg-streamer on my tl-MR3020 with the C270 camera image is tremento and after a few minutes out of the screen someone has gone through this problem
Here is the video with the defect
https://www.youtube.com/watch?v=LXSlWS_zwsA
Thank you all

@jefferyto
Copy link
Author

Not sure if folks are still using these scripts, but I wrote a package that makes detecting slide switch changes easier: slide-switch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment