Blink led in different color for 100 or 1000 GBit

The TP-Link T2500G-10TS has a panel with 10 bicolor LEDs. Normally they blink in green for GE and yellow for 10/100 Mbit Ethernet.
The Leds are defined like this in the device-tree:

	leds {
		compatible = "gpio-leds";

		led_sys: sys {
			label = "t2500g-10ts:green:sys";
			gpios = <&gpio0 148 GPIO_ACTIVE_HIGH>;
		};
		led_lan1ge: lan1ge {
			label = "t2500g-10ts:green:lan1ge";
			gpios = <&gpio0 118 GPIO_ACTIVE_HIGH>;
		};
		led_lan2ge: lan2ge {
			label = "t2500g-10ts:green:lan2ge";
			gpios = <&gpio0 119 GPIO_ACTIVE_HIGH>;
		};
                /* more leds not shown here */
		led_lan9ge: lan9ge {
			label = "t2500g-10ts:green:lan9ge";
		 	gpios = <&gpio0 126 GPIO_ACTIVE_HIGH>;
		};
		led_lan10ge: lan10ge {
			label = "t2500g-10ts:green:lan10ge";
			gpios = <&gpio0 127 GPIO_ACTIVE_HIGH>;
		};
		led_lan1fe: lan1fe {
			label = "t2500g-10ts:amber:lan1fe";
			gpios = <&gpio0 150 GPIO_ACTIVE_HIGH>;
		};
		/* more leds not shown here */
		led_lan10fe: lan10fe {
			label = "t2500g-10ts:amber:lan10fe";
 			gpios = <&gpio0 159 GPIO_ACTIVE_HIGH>;
		};
	};

For GBit ethernet, the following shows the activity of the leds (/etc/board.d/01-leds):

#!/bin/sh

. /lib/functions/leds.sh
. /lib/functions/uci-defaults.sh


board=$(board_name)
boardname="${board##*,}"

board_config_update

case $board in
tp-link,t2500g-10ts)
        ucidef_set_led_netdev "lan1ge" "LAN1 act" "$boardname:green:lan1ge" "lan1" "tx rx"
        ucidef_set_led_netdev "lan2ge" "LAN2 act" "$boardname:green:lan2ge" "lan2" "tx rx"
        ucidef_set_led_netdev "lan3ge" "LAN3 act" "$boardname:green:lan3ge" "lan3" "tx rx"
        ucidef_set_led_netdev "lan4ge" "LAN4 act" "$boardname:green:lan4ge" "lan4" "tx rx"
        ucidef_set_led_netdev "lan5ge" "LAN5 act" "$boardname:green:lan5ge" "lan5" "tx rx"
        ucidef_set_led_netdev "lan6ge" "LAN6 act" "$boardname:green:lan6ge" "lan6" "tx rx"
        ucidef_set_led_netdev "lan7ge" "LAN7 act" "$boardname:green:lan7ge" "lan7" "tx rx"
        ucidef_set_led_netdev "lan8ge" "LAN8 act" "$boardname:green:lan8ge" "lan8" "tx rx"
        ucidef_set_led_netdev "lan9ge" "LAN9 act" "$boardname:green:lan9ge" "lan9" "tx rx"
        ucidef_set_led_netdev "lan10ge" "LAN10 act" "$boardname:green:lan10ge" "lan10" "tx rx"
        ;;
esac

board_config_flush

exit 0

Is there a possibility to distinguish between GBit activity and 100MBit activity and have the yellow leds show the 100MBit activity? Alternatively, is there a possibility to show in yellow a link state with less than GBit, so that the led would be yellow and blink green when there is activity?

Kobi

You could write a hotplug scripts that gets run when an interface goes up. It could then check the line speed of that interface and adjust LED triggers accordingly.

I write this in 2012 (message #481) for WNDR3700: https://forum.archive.openwrt.org/viewtopic.php?id=22311&p=20

The script monitors 'ifup' actions from 'wan' interface, then queries the link speed and sets the wan LED to green if speed=1000, otherwise to yellow.

Place this into a new file named /etc/hotplug.d/iface/99-wanspeed and it should work:

#!/bin/sh

if [ "$ACTION" = ifup ]; then
        if [ "$INTERFACE" = "wan" ] ; then
                SPEED=$(cat /sys/class/net/eth1/speed)
                if [ "$SPEED" = "1000" ] ; then
                        logger "ifup $INTERFACE, speed $SPEED: set wan LED to green"
                        echo "1" > /sys/class/leds/netgear\:green\:wan/brightness
                else
                        logger "ifup $INTERFACE, speed $SPEED: set wan LED to yellow"
                        echo "0" > /sys/class/leds/netgear\:green\:wan/brightness
                fi
        fi
fi

Here you should likely change triggers on green to none, and amber to "switch0" with "tx rx" passed earlier as parameters.
Looking at my current R7800, there are white and amber WAN LEDs, and currently white has a trigger attached.

root@router1:~# cat /sys/class/leds/r7800\:amber\:wan/trigger
[none] switch0 timer default-on netdev heartbeat usbport phy0rx phy0tx phy0assoc phy0radio phy0tpt phy1rx phy1tx phy1assoc phy1radio phy1tpt

root@router1:~# cat /sys/class/leds/r7800\:white\:wan/trigger
none [switch0] timer default-on netdev heartbeat usbport phy0rx phy0tx phy0assoc phy0radio phy0tpt phy1rx phy1tx phy1assoc phy1radio phy1tpt

You need to delve a bit in to the source code so that you can mimic the necessary steps for both LEDs.

Thanks hnyman, this looks like the solution. I'll try that!

I have tried putting event handlers into both /etc/hotplug.d/iface and /etc/hotplug.d/net but neither are called when a cable is plugged/unplugged on the switch. The problem is that the eth0 interface of the switch does not go up or down when (un-)plugging cables. The device uses DSA, so there is a bridge over the 10 lan interfaces and eth0 always stays up. The use-case is that if someone moves a cable from one port of the switch to another, then the color of the LED should stay consistent.

I also tried listening to ubus events with "ubus listen" but I am not able to see any plug/unplug events either. One could poll the states of /sys/class/net/lan*/carrier and read out speed, but I am wondering whether there is a more elegant way:

root@OpenWrt:/etc/hotplug.d# cat /sys/class/net/lan*/carrier
1
0
0
0
1
0
0
0
0
0
root@OpenWrt:/etc/hotplug.d# cat /sys/class/net/lan*/speed
1000
-1
-1
-1
1000
-1
-1
-1
-1
-1