public
Last active

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

  • Download Gist
00-button
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
# 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
BTN_0_pressed
Shell
1 2
#!/bin/sh
grep -qe "sw1.*in hi" /sys/kernel/debug/gpio
BTN_1_pressed
Shell
1 2
#!/bin/sh
grep -qe "sw2.*in hi" /sys/kernel/debug/gpio
network_config
Shell
1 2 3 4 5 6
#!/bin/sh /etc/rc.common
 
START=15
boot() {
/usr/local/sbin/set_network_config
}
set_network_config
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
#!/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"
setup
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
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
update_network_config
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#!/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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.