on_ac_power: support multiple power_supply devices in sysfs

Newer devices have multiple power_supply devices in sysfs:

$ grep ^ /sys/class/power_supply/*/type
/sys/class/power_supply/AC/type:Mains
/sys/class/power_supply/BAT0/type:Battery
/sys/class/power_supply/ucsi-source-psy-USBC000:001/type:USB
/sys/class/power_supply/ucsi-source-psy-USBC000:002/type:USB

There are two "USB" Type-C ports than can supply power and both are
aggregated into the "Mains" power supply by the firmware. The "Battery"
also counts as a power supply, but is missing the online attribute.

The -f check with a wildcard pattern results in an error, when multiple
devices are present:

/lib/rc/bin/on_ac_power: line 21: [: too many arguments

When the power_supply class is registered, check for a "Mains" device.

Fixes #427.
This commit is contained in:
Sven Wegener 2021-07-04 13:19:43 +02:00 committed by William Hubbs
parent 1e975be896
commit 63955056ec

View File

@ -18,9 +18,10 @@ if [ -f /proc/acpi/ac_adapter/*/state ]; then
"state:"*"off-line") exit 128;; "state:"*"off-line") exit 128;;
esac esac
done done
elif [ -f /sys/class/power_supply/*/online ]; then elif [ -d /sys/class/power_supply ]; then
cat /sys/class/power_supply/*/online | while read line; do for dir in /sys/class/power_supply/*/; do
[ "${line}" = 0 ] && exit 128 [ "$(cat "${dir}/type")" != "Mains" ] && continue
[ "$(cat "${dir}/online")" = 0 ] && exit 128
done done
elif [ -f /proc/pmu/info ]; then elif [ -f /proc/pmu/info ]; then
cat /proc/pmu/info | while read line; do cat /proc/pmu/info | while read line; do