]> git.ipfire.org Git - people/ms/network.git/commitdiff
Add support for LEDs
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 20 Sep 2018 21:16:00 +0000 (22:16 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 20 Sep 2018 21:16:00 +0000 (22:16 +0100)
This patch configures LEDs on some Wireless PHYs to flash
on activity. This makes debugging easier.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/functions/functions.leds [new file with mode: 0644]
src/functions/functions.phy
src/udev/network-hotplug

index a295e379e6146d3ec96855b58b09ab753e2fead8..612c0bd5fb1a73a53daa246aad1e19652c0ab36f 100644 (file)
@@ -161,6 +161,7 @@ dist_network_DATA = \
        src/functions/functions.ip-tunnel \
        src/functions/functions.ipv4 \
        src/functions/functions.ipv6 \
+       src/functions/functions.leds \
        src/functions/functions.list \
        src/functions/functions.lock \
        src/functions/functions.logging \
diff --git a/src/functions/functions.leds b/src/functions/functions.leds
new file mode 100644 (file)
index 0000000..0b5fd20
--- /dev/null
@@ -0,0 +1,58 @@
+#!/bin/bash
+###############################################################################
+#                                                                             #
+# IPFire.org - A linux based firewall                                         #
+# Copyright (C) 2018 IPFire Network Development Team                          #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+###############################################################################
+
+led_exists() {
+       local led="${1}"
+       assert isset led
+
+       file_exists "/sys/class/leds/${led}"
+}
+
+led_get_supported_triggers() {
+       local led="${1}"
+
+       if ! led_exists "${led}"; then
+               error "LED ${led} does not exist"
+               return ${EXIT_ERROR}
+       fi
+
+       local triggers="$(fread "/sys/class/leds/${led}/trigger")"
+
+       # Filter markers for active trigger
+       print "${triggers//[\[\]]/}"
+}
+
+led_set_trigger() {
+       local led="${1}"
+       local trigger="${2}"
+
+       assert isset led
+       assert isset trigger
+
+       local supported_triggers="$(led_get_supported_triggers "${led}")"
+       if ! list_match "${trigger}" ${supported_triggers}; then
+               log DEBUG "Trigger ${trigger} is not supported by LED ${led}"
+               return ${EXIT_ERROR}
+       fi
+
+       log DEBUG "Setting LED trigger ${trigger} for ${led}"
+       fwrite "/sys/class/leds/${led}/trigger" "${trigger}"
+}
index e80b1221528123fc84987e04537ce3f773435661..6ebfbb3012e383753c01a18abf7cf6b1b84fb6c1 100644 (file)
@@ -98,3 +98,50 @@ phy_get_devices() {
                fi
        done
 }
+
+phy_list_leds() {
+       local phy="${1}"
+
+       # Check if the PHY exists
+       assert phy_exists "${phy}"
+
+       local led
+       for led in $(list_directory /sys/class/leds); do
+               # Get basename of the LED
+               led=${led%*/}
+
+               if [[ ${led} =~ ${phy}(:.*)?$ ]]; then
+                       print "${led}"
+               fi
+       done
+}
+
+# This function tries to automatically configure LEDs to
+# something useful
+phy_leds_autoconf() {
+       local phy="${1}"
+       assert isset phy
+
+       local led
+       for led in $(phy_list_leds "${phy}"); do
+               # Skip some types of LEDs
+               case "${led}" in
+                       # Pretty much everything we tested from Ralink
+                       # locked up the kernel after a couple of seconds
+                       rt*)
+                               continue
+                               ;;
+               esac
+
+               # We try to set the LED into tpt mode (flashing on activity),
+               # but will fallback to tx mode if that isn't supported
+               local trigger
+               for trigger in "${phy}tpt" "${phy}tx"; do
+                       if led_set_trigger "${led}" "${trigger}"; then
+                               break
+                       fi
+               done
+       done
+
+       return ${EXIT_OK}
+}
index 40d08cb99cdaa7133601066af23e8fca83286133..9054069f4085582d1963c536805be8864d412264 100644 (file)
@@ -44,6 +44,9 @@ case "${SUBSYSTEM}" in
                        PHY=""
                fi
 
+               # Configure LEDs
+               [ "${ACTION}" = "add" ] && phy_leds_autoconf "${PHY}"
+
                # Propagate the hotplug event to all configured port hooks
                hotplug_propagate_all_ports || exit ${EXIT_ERROR}
                ;;