#!/usr/bin/env clitest
# Test charge thresholds for ThinkPads
# Requirements:
# * Hardware: non-legacy ThinkPad
# * Kernel: >= 5.17
# * Batteries: BAT1 + BAT0
# * Power source AC
# Copyright (c) 2025 Thomas Koch <linrunner at gmx.net>.
# SPDX-License-Identifier: GPL-2.0-or-later
#
$ # +++ ThinkPad (BAT1+BAT0) +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
$ #
$ # --- tlp start
$ sudo tlp start -- START_CHARGE_THRESH_BAT0= STOP_CHARGE_THRESH_BAT0= START_CHARGE_THRESH_BAT1= STOP_CHARGE_THRESH_BAT1=
TLP started in AC mode (auto).
$ sudo tlp start -- START_CHARGE_THRESH_BAT0="60" STOP_CHARGE_THRESH_BAT0="100" START_CHARGE_THRESH_BAT1="61" STOP_CHARGE_THRESH_BAT1="100"
TLP started in AC mode (auto).
$ sudo tlp start -- START_CHARGE_THRESH_BAT0="102" STOP_CHARGE_THRESH_BAT0="100" START_CHARGE_THRESH_BAT1="100" STOP_CHARGE_THRESH_BAT1="100"
Error in configuration at START_CHARGE_THRESH_BAT0="102": not specified, invalid or out of range (0..99). Battery skipped.
Error in configuration at START_CHARGE_THRESH_BAT1="100": not specified, invalid or out of range (0..99). Battery skipped.
TLP started in AC mode (auto).
$ sudo tlp start -- START_CHARGE_THRESH_BAT0="0" STOP_CHARGE_THRESH_BAT0="0" START_CHARGE_THRESH_BAT1="0" STOP_CHARGE_THRESH_BAT1="0"
Error in configuration at STOP_CHARGE_THRESH_BAT0="0": not specified, invalid or out of range (1..100). Battery skipped.
Error in configuration at STOP_CHARGE_THRESH_BAT1="0": not specified, invalid or out of range (1..100). Battery skipped.
TLP started in AC mode (auto).
$ sudo tlp start -- START_CHARGE_THRESH_BAT0="0" STOP_CHARGE_THRESH_BAT0="103" START_CHARGE_THRESH_BAT1="0" STOP_CHARGE_THRESH_BAT1="101"
Error in configuration at STOP_CHARGE_THRESH_BAT0="103": not specified, invalid or out of range (1..100). Battery skipped.
Error in configuration at STOP_CHARGE_THRESH_BAT1="101": not specified, invalid or out of range (1..100). Battery skipped.
TLP started in AC mode (auto).
$ sudo tlp start -- START_CHARGE_THRESH_BAT0="88" STOP_CHARGE_THRESH_BAT0="88" START_CHARGE_THRESH_BAT1="97" STOP_CHARGE_THRESH_BAT1="97"
Error in configuration: START_CHARGE_THRESH_BAT0 >= STOP_CHARGE_THRESH_BAT0. Battery skipped.
Error in configuration: START_CHARGE_THRESH_BAT1 >= STOP_CHARGE_THRESH_BAT1. Battery skipped.
TLP started in AC mode (auto).
$ sudo tlp start -- START_CHARGE_THRESH_BAT0="85" STOP_CHARGE_THRESH_BAT0="86" START_CHARGE_THRESH_BAT1="95" STOP_CHARGE_THRESH_BAT1="96"
TLP started in AC mode (auto).
$ sudo tlp start -- START_CHARGE_THRESH_BAT0="DEF" STOP_CHARGE_THRESH_BAT0="DEF" START_CHARGE_THRESH_BAT1="DEF" STOP_CHARGE_THRESH_BAT1="DEF"
TLP started in AC mode (auto).
$ #
$ # --- tlp setcharge w/o arguments
$ sudo tlp setcharge BAT1 -- START_CHARGE_THRESH_BAT1="60" STOP_CHARGE_THRESH_BAT1="100" X_SOC_CHECK=0
Setting temporary charge thresholds for battery BAT1:
  start =  60
  stop  = 100 (no change)
$ sudo tlp setcharge BAT1 -- START_CHARGE_THRESH_BAT1="100" STOP_CHARGE_THRESH_BAT1="100"
Error in configuration at START_CHARGE_THRESH_BAT1="100": not specified, invalid or out of range (0..99). Aborted.
$ sudo tlp setcharge BAT1 -- START_CHARGE_THRESH_BAT1="0" STOP_CHARGE_THRESH_BAT1="0"
Error in configuration at STOP_CHARGE_THRESH_BAT1="0": not specified, invalid or out of range (1..100). Aborted.
$ sudo tlp setcharge BAT1 -- START_CHARGE_THRESH_BAT1="0" STOP_CHARGE_THRESH_BAT1="101"
Error in configuration at STOP_CHARGE_THRESH_BAT1="101": not specified, invalid or out of range (1..100). Aborted.
$ sudo tlp setcharge BAT1 -- START_CHARGE_THRESH_BAT1="97" STOP_CHARGE_THRESH_BAT1="97"
Error in configuration: START_CHARGE_THRESH_BAT1 >= STOP_CHARGE_THRESH_BAT1. Aborted.
$ sudo tlp setcharge BAT1 -- START_CHARGE_THRESH_BAT1="95" STOP_CHARGE_THRESH_BAT1="96"
Setting temporary charge thresholds for battery BAT1:
  start =  95
  stop  =  96
$ sudo tlp setcharge BAT1 -- START_CHARGE_THRESH_BAT1="95" STOP_CHARGE_THRESH_BAT1="96"
Setting temporary charge thresholds for battery BAT1:
  start =  95 (no change)
  stop  =  96 (no change)
$ sudo tlp setcharge BAT1 -- START_CHARGE_THRESH_BAT1="DEF" STOP_CHARGE_THRESH_BAT1="DEF"
Setting temporary charge thresholds for battery BAT1:
  stop  = 100
  start =  96
$ sudo tlp setcharge BAT1 -- NATACPI_ENABLE=0 TPACPI_ENABLE=0 START_CHARGE_THRESH_BAT0="DEF" STOP_CHARGE_THRESH_BAT0="DEF"
Error: there is no hardware driver support for charge thresholds.
$ #
$ # --- tlp setcharge w/ arguments
$ sudo tlp setcharge 60 100 BAT1 -- X_SOC_CHECK=0
Setting temporary charge thresholds for battery BAT1:
  start =  60
  stop  = 100 (no change)
$ sudo tlp setcharge 100 100 BAT1
Error: start charge threshold (100) for battery BAT1 is not specified, invalid or out of range (0..99). Aborted.
$ sudo tlp setcharge 0 0 BAT1
Error: stop charge threshold (0) for battery BAT1 is not specified, invalid or out of range (1..100). Aborted.
$ sudo tlp setcharge 0 101 BAT1
Error: stop charge threshold (101) for battery BAT1 is not specified, invalid or out of range (1..100). Aborted.
$ sudo tlp setcharge XYZZY 0 BAT1
Error: start charge threshold (XYZZY) for battery BAT1 is not specified, invalid or out of range (0..99). Aborted.
$ sudo tlp setcharge 0 XYZZY BAT1
Error: stop charge threshold (XYZZY) for battery BAT1 is not specified, invalid or out of range (1..100). Aborted.
$ sudo tlp setcharge 97 97 BAT1
Error: start threshold >= stop threshold for battery BAT1. Aborted.
$ sudo tlp setcharge 95 96 BAT1
Setting temporary charge thresholds for battery BAT1:
  start =  95
  stop  =  96
$ sudo tlp setcharge 95 96 BAT1 -- X_THRESH_SIMULATE_READERR="1"
Error: could not read current charge threshold(s) for battery BAT1. Aborted.
$ sudo tlp setcharge 95 96 BAT1 -- X_THRESH_SIMULATE_START="60" X_THRESH_SIMULATE_STOP="100"
Setting temporary charge thresholds for battery BAT1:
  start =  95
  stop  =  96
$ sudo tlp setcharge 95 96 BAT1
Setting temporary charge thresholds for battery BAT1:
  start =  95 (no change)
  stop  =  96 (no change)
$ sudo tlp setcharge DEF DEF BAT1
Setting temporary charge thresholds for battery BAT1:
  stop  = 100
  start =  96
$ sudo tlp setcharge 61 91 BAT1 -- X_SOC_CHECK=0
Setting temporary charge thresholds for battery BAT1:
  start =  61
  stop  =  91
$ sudo tlp setcharge 60 90 BAT0 -- X_SOC_CHECK=0
Setting temporary charge thresholds for battery BAT0:
  start =  60
  stop  =  90
$ #
$ # --- tlp discharge
$ sudo tlp discharge 100
Error: target charge level (100) for battery BAT0 is out of range (0..99).
$ sudo tlp discharge BAT0 100
Error: target charge level (100) for battery BAT0 is out of range (0..99).
$ sudo tlp discharge BAT1 100
Error: target charge level (100) for battery BAT1 is out of range (0..99).
$ #
$ # --- tlp-stat
$ # steps require a kernel >= 5.17 -- with 'charge_behaviour'
$ sudo tlp-stat -b | grep -E 'charge_(control|behaviour)'
/sys/class/power_supply/BAT0/charge_control_start_threshold =     60 [%]
/sys/class/power_supply/BAT0/charge_control_end_threshold   =     90 [%]
/sys/class/power_supply/BAT0/charge_behaviour               = [auto] inhibit-charge force-discharge
/sys/class/power_supply/BAT1/charge_control_start_threshold =     61 [%]
/sys/class/power_supply/BAT1/charge_control_end_threshold   =     91 [%]
/sys/class/power_supply/BAT1/charge_behaviour               = [auto] inhibit-charge force-discharge
$ sudo tlp-stat -b -- X_THRESH_SIMULATE_READERR=1 | grep -E 'charge_(control|behaviour)'
/sys/class/power_supply/BAT0/charge_control_start_threshold = (not available) [%]
/sys/class/power_supply/BAT0/charge_control_end_threshold   = (not available) [%]
/sys/class/power_supply/BAT0/charge_behaviour               = [auto] inhibit-charge force-discharge
/sys/class/power_supply/BAT1/charge_control_start_threshold = (not available) [%]
/sys/class/power_supply/BAT1/charge_control_end_threshold   = (not available) [%]
/sys/class/power_supply/BAT1/charge_behaviour               = [auto] inhibit-charge force-discharge
$ #
$ # --- Reset test machine to configured thresholds
$ sudo tlp setcharge BAT0  > /dev/null 2>&1
$ sudo tlp setcharge BAT1  > /dev/null 2>&1
$ #
