FEATURE REQ: WiFi on/off with side switch?

Here is a working switchaction that should work on most models:

#!/bin/sh

set_function(){
        model=$(awk -F': ' '/machine/ {print tolower($NF)}' /proc/cpuinfo| cut -d- -f2-)
        switch_left=
        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)
                        ;;
                "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 is on left
        if [ -n "$switch_left" ]; then
                wifi down
        else
                wifi up
        fi
}
set_function

this is just what I have been looking for. if all we need to do is modify the last if and else section I would love to be able to set the following addtional features. what commands like “wifi down” and “wifi up” are equivelent to the following without requiring a reboot and restarts network fully for them to take effect.

enable/disable guestwireless = ?

enable/disable ssproxy client = ?
enable/disable ssproxy server = ?
enable/disable wireguard server = ?
enable/disable openvpn server = ?

of course setting button any vpnserver or vpnclient will require shutting down all other vpn client services and vpn servers before the action.

adding features to gl-web button interface would be awesome and might show value add for non programmers. for middle of the road users like myself just listing the commands here would be great. I could just installl luci-app-command and create each button setting as a separate file and use a shell script with a cp command for each selection.

Yes, it can be programmed.

@Nikolas

I know this topic is pretty old at this point, but you seem to be getting very close to what I’m trying to do, only with a different action programmed.

I’m wondering if I can use the switch on the AR750S to swap between router and WAP modes. I can see from your script here that, I think, all I’d need to change from what you’ve written in

 #if switch is on left
    if [ -n "$switch_left" ]; then
            wifi down
    else
            wifi up
    fi
}
set_function

is making “wifi down” and “wifi up” be instead whatever command I would need to change the functioning mode of the router.

Do you know if this is possible? If so, do you know what I would put there at the end instead of the wifi commands?

Thank you very much for this post, either way. You’ve gotten me at least 10 steps closer to what I’m trying to do!

can you explain this in more detail because what I think what your trying to achieve may already be there.

For sure!

In router mode, the 750 will assign any devices that end up on its WLAN/SSIDs their own IP addresses from its DHCP pool (i.e. 192.168.8.0/24, maybe .200~.250?). In WAP mode, it will just act as a wireless access point, deferring DHCP assignment to the default DHCP server on the network to which it is attached, or requiring the entry of a statically-assigned IP, as the case may be. This is all pretty self-explanatory, of course.

I know I can log in to the admin portal for the device and set it to WAP mode, thereby allowing me to take it on-site to a client’s, plug it into an active network port, and set up my own little WLAN for use working on whatever it is I’m working on. But, AFAIK, in order to get it back in to router mode, for use when I’m out traveling or whatever, I would need to factory reset the device. Or hold the button down for 3 seconds, which I’m not sure what all that does. I guess maybe it’s not a factory reset? Still, would be a pain to have to log in to the thing every time I want to switch it back to WAP mode.

Instead, what I’d like to do is utilize the mode switch next to the reset button to swap between router and WAP modes, if at all possible.

I’ve come across a different-but-related post and added a comment (much like I did here), and received a response from @ThH stating

If you press the reset button 3 sec (power led flashing the fourth time) you can get back to router mode. Somewhere in the /usr/bin/button_released is the script doing this.

If you find that part, you could copy it to a new file in /usr/bin/(example:router_off) and execute that file by adding router_off to the /usr/bin/switchaction.

Haven’t found the scripts that enable other modes (like extender or bridge) but if you find them you could do the same as mentioned above.

So, in combination with the information from @Nikolas above, if I’m able to track down what variables are changed by that button press, I may be able to track down what I need to put in as the action to be performed by the switch moving from position 1 to position 0 or whatever. I plan on looking into this later on, when I’m at home. Currently at work, so I haven’t had any real chance to go digging through those configs in vi.

However, if one of you lovely folks already knows that information, that would be killer!

If no one does, and I figure it out myself, I’ll make sure to make a post about it, just in case someone else needs this information. Though, given my search results so far, I’m apparently the only person who’s ever wanted this particular functionality!

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.