b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | # Copyright (C) 2013 OpenWrt.org |
| 2 | |
| 3 | get_dt_led_path() { |
| 4 | local ledpath |
| 5 | local basepath="/proc/device-tree" |
| 6 | local nodepath="$basepath/aliases/led-$1" |
| 7 | |
| 8 | [ -f "$nodepath" ] && ledpath=$(cat "$nodepath") |
| 9 | [ -n "$ledpath" ] && ledpath="$basepath$ledpath" |
| 10 | |
| 11 | echo "$ledpath" |
| 12 | } |
| 13 | |
| 14 | get_dt_led_color_func() { |
| 15 | local enum |
| 16 | local func |
| 17 | local idx |
| 18 | local label |
| 19 | |
| 20 | [ -e "$1/function" ] && func=$(cat "$1/function") |
| 21 | [ -e "$1/color" ] && idx=$((0x$(hexdump -n 4 -e '4/1 "%02x"' "$1/color"))) |
| 22 | [ -e "$1/function-enumerator" ] && \ |
| 23 | enum=$((0x$(hexdump -n 4 -e '4/1 "%02x"' "$1/function-enumerator"))) |
| 24 | |
| 25 | [ -z "$idx" ] && [ -z "$func" ] && return 2 |
| 26 | |
| 27 | if [ -n "$idx" ]; then |
| 28 | for color in "white" "red" "green" "blue" "amber" \ |
| 29 | "violet" "yellow" "ir" "multicolor" "rgb" \ |
| 30 | "purple" "orange" "pink" "cyan" "lime" |
| 31 | do |
| 32 | [ $idx -eq 0 ] && label="$color" && break |
| 33 | idx=$((idx-1)) |
| 34 | done |
| 35 | fi |
| 36 | |
| 37 | label="$label:$func" |
| 38 | [ -n "$enum" ] && label="$label-$enum" |
| 39 | echo "$label" |
| 40 | |
| 41 | return 0 |
| 42 | } |
| 43 | |
| 44 | get_dt_led() { |
| 45 | local label |
| 46 | local ledpath=$(get_dt_led_path $1) |
| 47 | |
| 48 | [ -n "$ledpath" ] && \ |
| 49 | label=$(cat "$ledpath/label" 2>/dev/null) || \ |
| 50 | label=$(cat "$ledpath/chan-name" 2>/dev/null) || \ |
| 51 | label=$(get_dt_led_color_func "$ledpath") || \ |
| 52 | label=$(basename "$ledpath") |
| 53 | |
| 54 | echo "$label" |
| 55 | } |
| 56 | |
| 57 | led_set_attr() { |
| 58 | [ -f "/sys/class/leds/$1/$2" ] && echo "$3" > "/sys/class/leds/$1/$2" |
| 59 | } |
| 60 | |
| 61 | led_timer() { |
| 62 | led_set_attr $1 "trigger" "timer" |
| 63 | led_set_attr $1 "delay_on" "$2" |
| 64 | led_set_attr $1 "delay_off" "$3" |
| 65 | } |
| 66 | |
| 67 | led_on() { |
| 68 | led_set_attr $1 "trigger" "none" |
| 69 | led_set_attr $1 "brightness" 255 |
| 70 | } |
| 71 | |
| 72 | led_off() { |
| 73 | led_set_attr $1 "trigger" "none" |
| 74 | led_set_attr $1 "brightness" 0 |
| 75 | } |
| 76 | |
| 77 | status_led_restore_trigger() { |
| 78 | local trigger |
| 79 | local ledpath=$(get_dt_led_path $1) |
| 80 | |
| 81 | [ -n "$ledpath" ] && \ |
| 82 | trigger=$(cat "$ledpath/linux,default-trigger" 2>/dev/null) |
| 83 | |
| 84 | [ -n "$trigger" ] && \ |
| 85 | led_set_attr "$(get_dt_led $1)" "trigger" "$trigger" |
| 86 | } |
| 87 | |
| 88 | status_led_set_timer() { |
| 89 | led_timer $status_led "$1" "$2" |
| 90 | [ -n "$status_led2" ] && led_timer $status_led2 "$1" "$2" |
| 91 | } |
| 92 | |
| 93 | status_led_set_heartbeat() { |
| 94 | led_set_attr $status_led "trigger" "heartbeat" |
| 95 | } |
| 96 | |
| 97 | status_led_on() { |
| 98 | led_on $status_led |
| 99 | [ -n "$status_led2" ] && led_on $status_led2 |
| 100 | } |
| 101 | |
| 102 | status_led_off() { |
| 103 | led_off $status_led |
| 104 | [ -n "$status_led2" ] && led_off $status_led2 |
| 105 | } |
| 106 | |
| 107 | status_led_blink_slow() { |
| 108 | led_timer $status_led 1000 1000 |
| 109 | } |
| 110 | |
| 111 | status_led_blink_fast() { |
| 112 | led_timer $status_led 100 100 |
| 113 | } |
| 114 | |
| 115 | status_led_blink_preinit() { |
| 116 | led_timer $status_led 100 100 |
| 117 | } |
| 118 | |
| 119 | status_led_blink_failsafe() { |
| 120 | led_timer $status_led 50 50 |
| 121 | } |
| 122 | |
| 123 | status_led_blink_preinit_regular() { |
| 124 | led_timer $status_led 200 200 |
| 125 | } |