| #!/bin/sh |
| # |
| # This script monitors CONTROL_PATH folder and decides which is the |
| # led sequence that has to be echoed to the LEDS_FILE. |
| |
| . /etc/utils.sh |
| |
| # Output file. |
| BASE_PATH="/tmp/gpio" |
| LEDS_FILE="$BASE_PATH/leds" |
| CONTROL_PATH="$BASE_PATH/ledcontrol" |
| |
| LEDSTATE_FILE="$BASE_PATH/ledstate" |
| IP_FILE="$BASE_PATH/ipconnected" |
| LINK_DOWN_FILE="$BASE_PATH/link_down" |
| ACS_FILE="$CONTROL_PATH/acsconnected" |
| OVERTEMP_FILE="$CONTROL_PATH/overtemperature" |
| WAITPOWER_FILE="$CONTROL_PATH/waitpower" |
| |
| # Hardware verion set by manufacture process. |
| # HW_REV/HW_VER is 0/0 or non 0/1, gpio 15 should be ignored. |
| # HW_REV/HW_VER is 0/1, gpio 15 is used to control dim/bright. |
| HW_REV=$(sysvar_cmd --get HW_REV 2>/dev/null) |
| HW_VER=$(sysvar_cmd --get HW_VER 2>/dev/null) |
| |
| # Bit sequences. |
| ALL_LEDS_OFF="0" |
| HW_FAIL_LED_ON="1" # solid red |
| ACS_LED_ON="2" # solid blue |
| IP_LED_ON="2 0 2 0 2 0" # fast blue |
| LINK_DOWN_LED_ON="1 0 " # slow red blink |
| ANYTHING_ELSE="x2 2 0" # slow blink blue |
| |
| change_state() { |
| # Check if the new state is different from the previous one |
| if [ ! -e "$LEDSTATE_FILE" ] || [ "$last_ledstate" != "$1" ]; then |
| if [ "x$HW_REV" = "x0" ] && [ "x$HW_VER" = "x1" ]; then |
| if [ "x$1" = "xACSCONTACT" ]; then |
| diags set_led_dim on |
| else |
| diags set_led_dim off |
| fi |
| fi |
| leds $2 |
| last_leds="$2" |
| atomic $LEDSTATE_FILE "$1" |
| last_ledstate="$1" |
| echo "state changed to: $last_ledstate $last_leds" |
| fi |
| } |
| |
| does_interface_exist() { |
| [ -e "/sys/class/net/$1" ] |
| } |
| |
| has_ip_address() { |
| [ -n "$(ip -f inet6 addr show dev $1 scope global)" ] || |
| [ -n "$(ip -f inet addr show dev $1 scope global)" ] |
| } |
| |
| is_link_up() { |
| # If there is an explicit WAN link, use that. |
| # Otherwise for TV Boxes, check MoCA and Ethernet |
| # and for Storage Boxes check only Ethernet |
| if does_interface_exist wan0; then |
| is-interface-up wan0 |
| return $? |
| fi |
| is-interface-up eth1 |
| } |
| |
| watch-dir $CONTROL_PATH | |
| while read f; do |
| |
| if is_link_up; then |
| test ! -e "$LINK_DOWN_FILE" || rm -f "$LINK_DOWN_FILE" |
| success= |
| if does_interface_exist wan0; then |
| ifs="wan0 wan0.2" |
| else |
| ifs="br0" |
| fi |
| for ifname in $ifs; do |
| if has_ip_address $ifname; then |
| success="IP address acquired on $ifname" |
| break |
| fi |
| done |
| if [ -n "$success" ]; then |
| echo "$success" >"$IP_FILE" |
| else |
| rm -f "$ACS_FILE" "$IP_FILE" |
| fi |
| else |
| echo "Link down" >"$LINK_DOWN_FILE" |
| rm -f "$ACS_FILE" "$IP_FILE" |
| fi |
| |
| if [ -f "$OVERTEMP_FILE" ]; then |
| change_state OVERTEMP "$HW_FAIL_LED_ON" |
| elif [ -f "$WAITPOWER_FILE" ]; then |
| change_state POWEROFF "$ALL_LEDS_OFF" |
| elif [ -f "$LINK_DOWN_FILE" ]; then |
| change_state LINKDOWN "$LINK_DOWN_LED_ON" |
| elif [ ! -f "$IP_FILE" ]; then |
| change_state NOIP "$ANYTHING_ELSE" # progress: link but no ip |
| elif [ ! -f "$ACS_FILE" ]; then |
| change_state IPV6ACQUIRED "$IP_LED_ON" # progress: ip but no acs |
| else |
| change_state ACSCONTACT "$ACS_LED_ON" # success! |
| fi |
| done |