From: Michael Tremer Date: Thu, 20 Sep 2018 21:16:00 +0000 (+0100) Subject: Add support for LEDs X-Git-Tag: 010~61 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=208f745248b1e8162a7624d21169b26e66d96b5c;p=network.git Add support for LEDs This patch configures LEDs on some Wireless PHYs to flash on activity. This makes debugging easier. Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index a295e379..612c0bd5 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 index 00000000..0b5fd204 --- /dev/null +++ b/src/functions/functions.leds @@ -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 . # +# # +############################################################################### + +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}" +} diff --git a/src/functions/functions.phy b/src/functions/functions.phy index e80b1221..6ebfbb30 100644 --- a/src/functions/functions.phy +++ b/src/functions/functions.phy @@ -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} +} diff --git a/src/udev/network-hotplug b/src/udev/network-hotplug index 40d08cb9..9054069f 100644 --- a/src/udev/network-hotplug +++ b/src/udev/network-hotplug @@ -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} ;;