FEATURE REQ: WiFi on/off with side switch?

okay so if you leave it in router mode you have a firewall to protect you and all your other devices on your own subnet (192.168.8.0/24). do you have one device or multiple devices accessing the network through the router. is the connectivity requiring a local ip address from the visited network environment

note it does say you have to be careful that you nolonger have access to the router.

If in router mode, it would likely only be my laptop and phone, maybe those of anyone I’m traveling with. Not an issue, and part of why I bought the Slate was to be able to have a travel router, since that’s the main thing it does.

However, it does also have the ability to function as a wireless access point (WAP), which is what I was replacing when I bought it. In that case, I’m only trying to gain access to an extant local network, hence needing a local IP to be assigned to whatever device (work laptop) I’m connecting to my Slate. Being an extant network I’m only trying to gain access to, they already have a firewall, DHCP server, etc. So no need for the router functionality, and beyond that, the Slate acting as a router actually totally defeats the purpose of what I need.

Think of it this way; most home users (though maybe not as many people on these forums) have what they call a “router” at home. And that’s not a total misnomer, as it does function as a router. But it’s also a wireless access point. And, often, a modem, but we’ll ignore that as it’s not germane to this conversation. When in its “router” form, the Slate does the same thing; it routes traffic between networks, and provides WiFi access to said network. However, when it’s in its WAP form, it performs no router functionality, it just provides WiFi access to an already in-place network, which has the necessary means to protect devices connected to it, and to assign IP addresses, and already has its own router for gaining access to the Internet and beyond.

I need the ability to use this as a WAP, but also - occasionally - as a router. Having a nice, built-in little switch that I can just flip as needed would be killer. If I can’t make it happen, oh well, too bad so sad. However, this is Linux. It’s likely not impossible, just need to tell it to do the right things at the right times!

Hope that’s a little clearer for you, and I hope I didn’t misinterpret your question.

While the alternative solution does break the WiFi, it looks like that first bit might actually be useful. I feel like that might be overkill, as there are already bash scripts in place for the BTN activation/position, but that first script might be handy, if I can make it be in WAP mode instead of Bridge mode. Thank you for that, I think this is another step forward in figuring out how to do this the best way possible.

+Peace be with you.

Could you help?..

Need a “walk-through,” for how to edit scripts (for this topic’s function), as I am (more or less) clueless & new to all of this.

History:

@Nikolas I just recently tried copying and pasting your script as a 100% replacement of the “switchaction” file on my AR150 mini-router.

(Yes, I am completely new to this).

Unfortunately, I wasn’t able to determine that this — alone — worked perfectly, for the desired outcome… — (that desired outcome being, like this thread is named, to have one of my two external buttons be changed, in order now to be able to switch the wifi on or off).

If you don’t mind… :

Could you actually walk me through — (step by step) — describing how to modify this script (or scripts), to get this to function properly?..

This is my first time ever messing around with these codes. So, I need every bit of help (& detail) you can mention.

In fact, it’s seems like a miracle that I successfully SSH’d into my router, and then looked into the files using WinSCP; (etc.)

But, beyond that, I’m pretty clueless as to what the code there (that I’m looking at) actually means; how to modify it; how to test it (and to verify that any modifications are then working); etc.

In case anyone can offer to help, here are the particulars:

(1) Router model = gl-AR150
(2) Firmware = openwrt-ar150-3.100-1218.bin
(3) I just completely re-flashed the above firmware (3.100), in order to “start clean” — (after having somehow disabled[?] my iPhone-tethering function, using WinSCP, just before that)

Would you need to know anything else?..

Any help, out there?..

Many thanks, in advance.
+May God reward you.

First you need to know how to use winscp to edit files on the router.

Then check these files.

/etc/rc.button/BTN_8
This is the file of AR150 switch button. Do not change it. Its contents should be like this:

#!/bin/sh

#This is the switch of AR150, left pressed, release right
# echo "$BUTTON ${ACTION}" > /dev/console
# /etc/init.d/initswitch restart

[ $SEEN -gt 0 ] || exit 0

echo "$BUTTON ${ACTION}" > /dev/console
/usr/bin/switchaction &

/usr/bin/switchaction
This is the script actually run when you toggle the switch. Just read and try to understand this script.

Pls modify as below.
Line 12, pls add

toggle_wifi(){
	local action=$1
	if [ "$action" = "OFF" ];then
		  uci set wireless.@wifi-iface[0].disabled=1

	else
		uci set wireless.@wifi-iface[0].disabled=0
	fi
	uci commit wireless
	wifi
}

image

Line 141 pls add

"wifi")
				toggle_wifi ON
			;;

image

Line 165 pls add

            "wifi")
				toggle_wifi OFF
			;;

image

/etc/config/glconfig
Edit this file

config service 'switch_button'
	option enable '1'
	option function 'wifi'

image

After you modify the 3 files as above, the switch button already works to turn on off wifi.
Left: wifi on
Right: wifi off

Can I get the reward from God?

4 Likes

Hi alzhao,

Thank you for the informative post of yours.
I am hesitating to edit the mentioned files because you are mentioning certain line numbers to insert the proper code but my file does not contais as many code lines.

Could you please clarify for me how can I edit in this case?

Sorry for the lame question :see_no_evil:

Can you just post your file contents?

1 Like

This is how it looks like my /usr/bin/switchaction/ :

#!/bin/sh

check_other_vpn(){
        wg_server=$(uci get wireguard_server.@servers[0].enable)
        ov_server=$(uci get vpn_service.global.enable)
        client=""
        if [ "$1" = "ovpn" ];then
                client=$(uci get wireguard.@proxy[0].enable)
        else
                client=$(uci get  glconfig.openvpn.enable)
        fi
  
        if [ "$wg_server" = "1" -o "$ov_server" = "1" -o "$client" = "1" ];then
                rm /var/lock/switch.lock
                exit 0
        fi

        if [ -f "/etc/config/shadowsocks" ]; then
        	ss_client=$(uci get shadowsocks.@transparent_proxy[0].main_server)
        	[ "$ss_client" != "nil" ] && {
                rm /var/lock/switch.lock
                exit 0
        	}
        fi

        if [ -f "/etc/config/ss-service" ]; then
        	ss_server=$(uci get ss-service.host.enable)
        	[ "$ss_server" = "1" ] && {
                rm /var/lock/switch.lock
                exit 0
        	}
        fi     
}

set_function(){

	# Using lock, avoid restart repeatedly
	LOCK=/var/lock/switch.lock
	if [ -f "$LOCK" ];then
		exit 0
	fi
	touch $LOCK

	model=$(awk -F': ' '/machine/ {print tolower($NF)}' /proc/cpuinfo| cut -d- -f2-)
	switch_left=
	switch_disabled="0"
	switch_enabled=$(uci get glconfig.switch_button.enable)
	switch_func=$(uci get glconfig.switch_button.function)

	case "$model" in
		"ar150")
			switch_left=$(grep -o "BTN_8.*hi" /sys/kernel/debug/gpio)
			;;
		"ar300m")
			switch_left=$(grep -o "left.*hi" /sys/kernel/debug/gpio)
			;;
		"mt300a")
			switch_left=$(grep -o "BTN_1.*hi" /sys/kernel/debug/gpio)
			;;
		"mt300n")
			switch_left=$(grep -o "BTN_0.*hi" /sys/kernel/debug/gpio)
			;;
		"mt300n-v2")
			switch_left=$(grep -o "BTN_0.*hi" /sys/kernel/debug/gpio)
			;;
		"mifi-v3"|\
		"ar750s")
			switch_left=$(grep -o "right.*lo" /sys/kernel/debug/gpio)
			;;
		"ar750")
			switch_left=$(grep -o "sw1.*lo" /sys/kernel/debug/gpio)
			;;
		"*")
			switch_disabled="1"
			;;
	esac

	if [ "$switch_disabled" = "1" ] || [ "$switch_enabled" != "1" ]; then
		rm $LOCK
		exit 0
	fi

	#if switch is on left
	if [ -n "$switch_left" ]; then
		case "$switch_func" in
			"wg")
				check_other_vpn wg
                wgswitch=`uci get wireguard.@proxy[0].enable`
                if [ "$wgswitch" != "1" -o "$wgswitch" != 1 ];then
                        uci set wireguard.@proxy[0].enable='1'
                        uci commit wireguard
                fi
                wgstat=`pidof "wg-crypt-wg0"`
                if [ "$wgstat" = "" -o "$wgstat" = " "  ];then
                        /etc/init.d/wireguard restart
                fi
			;;
			"vpn")
				check_other_vpn ovpn
				vpn_status=$(pidof openvpn)
				vpn_cfg=$(uci get glconfig.openvpn.ovpn)
				if [ -z $vpn_status ] && [ -n "$vpn_cfg" ]; then
					uci set glconfig.openvpn.enable='1'
					uci commit glconfig
					/etc/init.d/startvpn restart
				fi
			;;
			"*")
			;;
		esac
	else
		case "$switch_func" in
			"wg")
				uci set wireguard.@proxy[0].enable='0'
				uci commit wireguard
				/etc/init.d/wireguard stop
			;;
			"vpn")
				vpn_en=$(uci get glconfig.openvpn.enable)
				if [ "$vpn_en" = "1" ]; then
					uci set glconfig.openvpn.enable='0'
					uci commit glconfig
					/etc/init.d/startvpn stop
				fi
			;;
			"*")
			;;
		esac
	fi

	rm $LOCK
}
set_function

Can you pls check what is your firmware version? This seems old.

OpenWrt 18.06.1 r7258-5eb055306f / LuCI openwrt-18.06 branch (git-18.196.56128-9112198)

This an AR-150 mini router.

Sorry I mean on the default UI.

Pls find your model https://dl.gl-inet.com/ and download the latest beta firmware to try.

@alzhao For the AR-150 mini router the firmware 3.017 and 3.024 both show what @Bonami posted from the switchaction file. The upgrade section of the router shows 3.017 as being the stable release for the AR-150.

What is the best way to add the code to get the side switch to turn the wifi on/off?

Thanks.

You’d better change /usr/bin/switchaction/ manually and keep a copy and restore it after upgrade.

Since your example differs from the code in the 3.017 and 3.024 switchaction, I’m not sure what changes need to be made. Can you explain how to make the edits using the code @Bonami provided above?

alzhao, thanks for your code.

Tried your modifications on my ar750s 3.105 and it kind of works. When I reboot the device, the wifi is off by default regardless of the position of the switch. So if I leave it in the on position (right) and reboot, the gui will say wifi is on but it is not. To get wifi on, I have to move the switch to off (left) and then back to on (right). Now the gui is in sync with the actual state of the wifi.

Perhaps some sort of initiation code is needed.

Also, I was hoping your code would add wifi on/off as an additional option in the gui for configuring the function of the switch but it doesn’t. So does the wifi toggle replace the other switch options or works in parallel with them?

I did figure out that if I want both 2.4ghz and 5ghz wifi to be toggled, I need to make this change:

toggle_wifi(){
	local action=$1
	if [ "$action" = "OFF" ];then
		  uci set wireless.@wifi-iface[0].disabled=1
		  uci set wireless.@wifi-iface[1].disabled=1
	else
		uci set wireless.@wifi-iface[0].disabled=0
		uci set wireless.@wifi-iface[1].disabled=0
	fi
	uci commit wireless
	wifi
}

After reboot, can you move the switch on/off one more time and it works?

Also can you send me the system log? ssh to the router and use “logread” to get the log.

Both wifi channels come on if I manually toggle the switch to off and then on. Sometimes maybe one channel comes up after reboot so maybe it’s a timing issue?

Here is my boot log: 2020122_750_log - Pastebin.com

Legend, this is exactly what I was looking for.
I should add that with the very last step, there is already a config service 'switch_button' entry, so that needs to be commented out for anyone who didn’t notice it.

I would like the slide switch to turn wifi on/off on my GL-MT300N-V2-58b running 3.102

What should I have for the whole /usr/bin/switchaction ?

Anything else needed to change?

Thanks.