Skip to content

Instantly share code, notes, and snippets.

@abstrask
Forked from jefferyto/00-button
Last active November 13, 2023 06:32
Show Gist options
  • Save abstrask/6a65184ca52d4b0c3c98 to your computer and use it in GitHub Desktop.
Save abstrask/6a65184ca52d4b0c3c98 to your computer and use it in GitHub Desktop.
Scripts for OpenWrt on TP-Link TL-MR3020 to change network configs using 3-position sliding switch. Configuration changes by UCI/LuCI overwrites the symbolic links used by original scripts. This version copies the scripts instead of using symbolic links. Added script attached to WPS button, to commit the updated config to the current config (3g,…
# 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
# Commits network config files to subfolder of current config (3g, ap or wisp)
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
DEST="/etc/config/$CONFIG"
logger -t "$ME" "Commited network config files to '$CONFIG'"
cp $SRC/network $DEST/network
cp $SRC/wireless $DEST/wireless
. /etc/diag.sh
get_status_led
status_led_on
sleep 2
status_led_off
#!/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=/etc/config
if [ ! -d "$SRC" ] ; then
logger -t "$ME" -p "user.warn" "$SRC is not a directory"
exit 1
fi
logger -t "$ME" "Using $CONFIG config"
cp $SRC/network $DEST/network
cp $SRC/wireless $DEST/wireless
# Forked from https://gist.github.com/jefferyto/8010733
# Configuration changes by UCI/LuCI overwrites the symbolic links used by original scripts.
# This version copies the scripts instead of using symbolic links.
# Added script attached to WPS button, to commit the updated config to the current config (3g, ap or wisp) subfolder.
# Instructions:
# 1. Copy all project files into /tmp, e.g. using SCP
# 2. Open a terminal session to the router
# 3. Run the commands below
#Prep files and sort them into appropriate folders
cd /tmp
chmod a+x BTN_0_pressed BTN_1_pressed set_network_config update_network_config network_config commit_config2folder
mkdir -p /usr/local/sbin
mv BTN_0_pressed BTN_1_pressed set_network_config update_network_config commit_config2folder /usr/local/sbin
mkdir -p /etc/hotplug.d/button
mv 00-button /etc/hotplug.d/button
mv network_config /etc/init.d
#Instal flock, needed for network config script
opkg update
opkg install flock
#Copy current 'network' and 'wireless' config into '3g', 'ap' and 'wisp' folders
cd /etc/config
mkdir /etc/config/3g
cp network wireless 3g
cp -r 3g wisp
cp -r 3g ap
/etc/init.d/network_config enable
#Add buttons for sliding switch
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
#Add WPS button to commit config to folder
uci add system button
uci set system.@button[-1].button=wps
uci set system.@button[-1].action=released
uci set system.@button[-1].handler=commit_config2folder
uci set system.@button[-1].min=1
uci set system.@button[-1].max=3
#Commit config and reboot
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
sleep 2
status_led_off
fi
) 9>&-
) 9>/tmp/update_network_config.lock
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment