From 9ddcb2e9f4665fa0b0620fc7eb397917d029fdfd Mon Sep 17 00:00:00 2001 From: Manuel Ullmann Date: Wed, 6 Apr 2022 08:08:40 +0200 Subject: [PATCH 1/2] Ignore incorrectly reported batteries and ignore unhandled power_supply types fixup of whitespace --- usr/sbin/laptop_mode | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/usr/sbin/laptop_mode b/usr/sbin/laptop_mode index 5e2ef72..c4387ab 100755 --- a/usr/sbin/laptop_mode +++ b/usr/sbin/laptop_mode @@ -488,7 +488,7 @@ lmt_load_config () BATTERY_NOT_DISCHARGING=0 for POWER_SUPPLY in /sys/class/power_supply/* ; do if [ -f $POWER_SUPPLY/type ] ; then - SYSFS_POWER_SUPPLY=1 + SYSFS_POWER_SUPPLY=$(( SYSFS_POWER_SUPPLY + 1 )) if [ "$(cat $POWER_SUPPLY/type)" = "Mains" ]; then log "VERBOSE" "Determining power state from $POWER_SUPPLY/online." if [ "$(cat $POWER_SUPPLY/online)" = 1 ]; then @@ -497,6 +497,14 @@ lmt_load_config () fi elif [ "$(cat $POWER_SUPPLY/type)" = "Battery" ]; then log "VERBOSE" "Determining power state from status of battery $POWER_SUPPLY." + #INFO: Because there are and will always be br0ken drivers + if [ ! -f $POWER_SUPPLY/status ]; then + log "ERR" "Your power_supply is lacking a status node" + log "ERR" "Its driver might be reporting a wrong type" + log "ERR" "Ignoring this battery" + SYSFS_POWER_SUPPLY=$(( SYSFS_POWER_SUPPLY - 1 )) + continue + fi #INFO: Because there are and will always be br0ken batteries if [ "$(cat $POWER_SUPPLY/status)" != "Discharging" ]; then if [ "$(cat $POWER_SUPPLY/status)" = "Unknown" ]; then @@ -512,11 +520,15 @@ lmt_load_config () ON_AC=0 break fi + else + log "VERBOSE" "Power_supply has unexpected type $(cat $POWER_SUPPLY/type)" + log "VERBOSE" "Ignoring it" + SYSFS_POWER_SUPPLY=$(( SYSFS_POWER_SUPPLY - 1 )) fi fi done - if [ $SYSFS_POWER_SUPPLY = 1 ] ; then + if [ $SYSFS_POWER_SUPPLY -gt 0 ] ; then # Already found it! log "VERBOSE" "Not trying other options, already found a power supply." elif [ $BATTERY_NOT_DISCHARGING = 1 ]; then From be84bb770aa2e79ec8d6abbe274db1c2f3ab6452 Mon Sep 17 00:00:00 2001 From: Manuel Ullmann Date: Fri, 8 Apr 2022 23:39:36 +0200 Subject: [PATCH 2/2] Use kernel API correctly and filter out non-system batteries --- usr/sbin/laptop_mode | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/usr/sbin/laptop_mode b/usr/sbin/laptop_mode index c4387ab..c692b26 100755 --- a/usr/sbin/laptop_mode +++ b/usr/sbin/laptop_mode @@ -496,15 +496,12 @@ lmt_load_config () break fi elif [ "$(cat $POWER_SUPPLY/type)" = "Battery" ]; then - log "VERBOSE" "Determining power state from status of battery $POWER_SUPPLY." - #INFO: Because there are and will always be br0ken drivers - if [ ! -f $POWER_SUPPLY/status ]; then - log "ERR" "Your power_supply is lacking a status node" - log "ERR" "Its driver might be reporting a wrong type" - log "ERR" "Ignoring this battery" + if [ "$(cat $POWER_SUPPLY/scope)" != "System" ]; then + log "VERBOSE" "Ignoring battery $POWER_SUPPLY, because it is not a system battery" SYSFS_POWER_SUPPLY=$(( SYSFS_POWER_SUPPLY - 1 )) continue fi + log "VERBOSE" "Determining power state from status of battery $POWER_SUPPLY." #INFO: Because there are and will always be br0ken batteries if [ "$(cat $POWER_SUPPLY/status)" != "Discharging" ]; then if [ "$(cat $POWER_SUPPLY/status)" = "Unknown" ]; then @@ -521,7 +518,7 @@ lmt_load_config () break fi else - log "VERBOSE" "Power_supply has unexpected type $(cat $POWER_SUPPLY/type)" + log "VERBOSE" "Power_supply has unhandled type $(cat $POWER_SUPPLY/type)" log "VERBOSE" "Ignoring it" SYSFS_POWER_SUPPLY=$(( SYSFS_POWER_SUPPLY - 1 )) fi