|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# SPDX-License-Identifier: BSD-3-Clause |
| 4 | +# Copyright(c) 2021-2025 Intel Corporation. All rights reserved. |
| 5 | + |
| 6 | +# needs usbrelay package: https://github.com/darrylb123/usbrelay |
| 7 | +# param1: --debug | switch name |
| 8 | +# param2: switch state |
| 9 | +usbrelay_switch() |
| 10 | +{ |
| 11 | + if [[ "$1" == "--debug" ]]; then |
| 12 | + dlogi "Debug mode: Current status of all relays:" |
| 13 | + usbrelay || { |
| 14 | + die "Failed to get usbrelay status. |
| 15 | + The usbrelay hw module is not responding or no relays detected. |
| 16 | + Check hardware connection." |
| 17 | + } |
| 18 | + fi |
| 19 | + |
| 20 | + # Declare a constant for the relay settle time |
| 21 | + local USBRELAY_SETTLE_TIME=0.5 |
| 22 | + |
| 23 | + local switch_name=$1 |
| 24 | + local state=$2 |
| 25 | + |
| 26 | + dlogi "Setting usbrelay switch $switch_name to $state." |
| 27 | + usbrelay "$switch_name=$state" --quiet || { |
| 28 | + die "Failed to set usbrelay switch $switch_name to $state. |
| 29 | + The usbrelay hw module is not responding or no relays detected. |
| 30 | + Check hardware connection." |
| 31 | + } |
| 32 | + |
| 33 | + # wait for the switch to settle |
| 34 | + sleep "$USBRELAY_SETTLE_TIME" |
| 35 | + |
| 36 | + # Display current state of the switch |
| 37 | + current_state=$(usbrelay | awk -F= -v name="$switch_name" '$1 == name { print $2 }') |
| 38 | + |
| 39 | + # Check if current_state is equal to the requested state |
| 40 | + [[ "$current_state" == "$state" ]] || { |
| 41 | + die "usbrelay switch $switch_name failed to set to $state (current: $current_state)" |
| 42 | + } |
| 43 | + |
| 44 | + case "$current_state" in |
| 45 | + '1') dlogi "Current state of $switch_name is: on";; |
| 46 | + '0') dlogi "Current state of $switch_name is: off";; |
| 47 | + *) die "Invalid state for $switch_name: $current_state";; |
| 48 | + esac |
| 49 | +} |
0 commit comments