#!/usr/bin/env clitest
# Test charge thresholds for System76 laptops
# Requirements:
# * Hardware: System76 laptop w/ open source EC firmware
# * Software: kernel 5.16+
# Copyright (c) 2025 Thomas Koch <linrunner at gmx.net>.
# SPDX-License-Identifier: GPL-2.0-or-later
#
$ # +++ System76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
$ #
$ # --- tlp start
$ sudo tlp start -- START_CHARGE_THRESH_BAT0= STOP_CHARGE_THRESH_BAT0= START_CHARGE_THRESH_BAT1= STOP_CHARGE_THRESH_BAT1= 2>&1 | sed -r "s/(TLP started).*/\1/"
TLP started
$ sudo tlp start -- START_CHARGE_THRESH_BAT0="60" STOP_CHARGE_THRESH_BAT0="100" START_CHARGE_THRESH_BAT1= STOP_CHARGE_THRESH_BAT1= 2>&1 | sed -r "s/(TLP started).*/\1/"
TLP started
$ sudo tlp start -- START_CHARGE_THRESH_BAT0="100" STOP_CHARGE_THRESH_BAT0="100" START_CHARGE_THRESH_BAT1= STOP_CHARGE_THRESH_BAT1= 2>&1 | sed -r "s/(TLP started).*/\1/"
Error in configuration at START_CHARGE_THRESH_BAT0="100": not specified, invalid or out of range (0..99). Battery skipped.
TLP started
$ sudo tlp start -- START_CHARGE_THRESH_BAT0="0" STOP_CHARGE_THRESH_BAT0="0" START_CHARGE_THRESH_BAT1= STOP_CHARGE_THRESH_BAT1= 2>&1 | sed -r "s/(TLP started).*/\1/"
Error in configuration at STOP_CHARGE_THRESH_BAT0="0": not specified, invalid or out of range (1..100). Battery skipped.
TLP started
$ sudo tlp start -- START_CHARGE_THRESH_BAT0="0" STOP_CHARGE_THRESH_BAT0="101" START_CHARGE_THRESH_BAT1= STOP_CHARGE_THRESH_BAT1= 2>&1 | sed -r "s/(TLP started).*/\1/"
Error in configuration at STOP_CHARGE_THRESH_BAT0="101": not specified, invalid or out of range (1..100). Battery skipped.
TLP started
$ sudo tlp start -- START_CHARGE_THRESH_BAT0="97" STOP_CHARGE_THRESH_BAT0="97" START_CHARGE_THRESH_BAT1= STOP_CHARGE_THRESH_BAT1= 2>&1 | sed -r "s/(TLP started).*/\1/"
Error in configuration: START_CHARGE_THRESH_BAT0 >= STOP_CHARGE_THRESH_BAT0. Battery skipped.
TLP started
$ sudo tlp start -- START_CHARGE_THRESH_BAT0="95" STOP_CHARGE_THRESH_BAT0="96" START_CHARGE_THRESH_BAT1= STOP_CHARGE_THRESH_BAT1= 2>&1 | sed -r "s/(TLP started).*/\1/"
TLP started
$ sudo tlp start -- START_CHARGE_THRESH_BAT0="DEF" STOP_CHARGE_THRESH_BAT0="DEF" START_CHARGE_THRESH_BAT1= STOP_CHARGE_THRESH_BAT1= 2>&1 | sed -r "s/(TLP started).*/\1/"
TLP started
$ #
$ # --- tlp setcharge w/o arguments
$ sudo tlp setcharge --  START_CHARGE_THRESH_BAT0="60" STOP_CHARGE_THRESH_BAT0="100" X_SOC_CHECK=0
Setting temporary charge thresholds for battery BAT0:
  start =  60
  stop  = 100 (no change)
$ sudo tlp setcharge -- START_CHARGE_THRESH_BAT0="100" STOP_CHARGE_THRESH_BAT0="100"
Error in configuration at START_CHARGE_THRESH_BAT0="100": not specified, invalid or out of range (0..99). Aborted.
$ sudo tlp setcharge -- START_CHARGE_THRESH_BAT0="ABCDE" STOP_CHARGE_THRESH_BAT0="100"
Error in configuration at START_CHARGE_THRESH_BAT0="ABCDE": not specified, invalid or out of range (0..99). Aborted.
$ sudo tlp setcharge -- START_CHARGE_THRESH_BAT0="0" STOP_CHARGE_THRESH_BAT0="0"
Error in configuration at STOP_CHARGE_THRESH_BAT0="0": not specified, invalid or out of range (1..100). Aborted.
$ sudo tlp setcharge -- START_CHARGE_THRESH_BAT0="0" STOP_CHARGE_THRESH_BAT0="101"
Error in configuration at STOP_CHARGE_THRESH_BAT0="101": not specified, invalid or out of range (1..100). Aborted.
$ sudo tlp setcharge -- START_CHARGE_THRESH_BAT0="0" STOP_CHARGE_THRESH_BAT0="XYZZY"
Error in configuration at STOP_CHARGE_THRESH_BAT0="XYZZY": not specified, invalid or out of range (1..100). Aborted.
$ sudo tlp setcharge -- START_CHARGE_THRESH_BAT0="97" STOP_CHARGE_THRESH_BAT0="97"
Error in configuration: START_CHARGE_THRESH_BAT0 >= STOP_CHARGE_THRESH_BAT0. Aborted.
$ sudo tlp setcharge -- START_CHARGE_THRESH_BAT0="95" STOP_CHARGE_THRESH_BAT0="96"
Setting temporary charge thresholds for battery BAT0:
  start =  95
  stop  =  96
$ sudo tlp setcharge -- START_CHARGE_THRESH_BAT0="95" STOP_CHARGE_THRESH_BAT0="96"
Setting temporary charge thresholds for battery BAT0:
  start =  95 (no change)
  stop  =  96 (no change)
$ sudo tlp setcharge -- START_CHARGE_THRESH_BAT0="DEF" STOP_CHARGE_THRESH_BAT0="DEF"
Setting temporary charge thresholds for battery BAT0:
  start =   0
  stop  = 100
$ sudo tlp setcharge -- NATACPI_ENABLE=0 START_CHARGE_THRESH_BAT0="DEF" STOP_CHARGE_THRESH_BAT0="DEF"
Error: battery charge thresholds not available.
$ #
$ # --- tlp setcharge w/ arguments
$ sudo tlp setcharge 60 100 -- X_SOC_CHECK=0
Setting temporary charge thresholds for battery BAT0:
  start =  60
  stop  = 100 (no change)
$ sudo tlp setcharge 100 100
Error: start charge threshold (100) for battery BAT0 is not specified, invalid or out of range (0..99). Aborted.
$ sudo tlp setcharge 0 0
Error: stop charge threshold (0) for battery BAT0 is not specified, invalid or out of range (1..100). Aborted.
$ sudo tlp setcharge 0 101
Error: stop charge threshold (101) for battery BAT0 is not specified, invalid or out of range (1..100). Aborted.
$ sudo tlp setcharge ABCDE 0
Error: start charge threshold (ABCDE) for battery BAT0 is not specified, invalid or out of range (0..99). Aborted.
$ sudo tlp setcharge 0 XYZZY
Error: stop charge threshold (XYZZY) for battery BAT0 is not specified, invalid or out of range (1..100). Aborted.
$ sudo tlp setcharge 97 97
Error: start threshold >= stop threshold for battery BAT0. Aborted.
$ sudo tlp setcharge 95 96
Setting temporary charge thresholds for battery BAT0:
  start =  95
  stop  =  96
$ sudo tlp setcharge 95 96 -- X_THRESH_SIMULATE_READERR="1"
Error: could not read current charge threshold(s) for battery BAT0. Aborted.
$ sudo tlp setcharge 95 96 -- X_THRESH_SIMULATE_START="60" X_THRESH_SIMULATE_STOP="100"
Setting temporary charge thresholds for battery BAT0:
  start =  95
  stop  =  96
$ sudo tlp setcharge 95 96
Setting temporary charge thresholds for battery BAT0:
  start =  95 (no change)
  stop  =  96 (no change)
$ sudo tlp setcharge BAT1
Error: battery BAT1 not present.
$ sudo tlp setcharge 0 3 BAT1
Error: battery BAT1 not present.
$ sudo tlp setcharge XYZZY ABCDE BAT1
Error: battery BAT1 not present.
$ #
$ # --- Reset to hardware defaults
$ sudo tlp setcharge DEF DEF
Setting temporary charge thresholds for battery BAT0:
  start =  90
  stop  = 100
$ #
$ # --- tlp-stat
$ sudo tlp-stat -b -- | grep -E 'charge_control'
/sys/class/power_supply/BAT0/charge_control_start_threshold =     90 [%]
/sys/class/power_supply/BAT0/charge_control_end_threshold   =    100 [%]
$ sudo tlp-stat -b -- X_THRESH_SIMULATE_READERR=1 | grep -E 'charge_control'
/sys/class/power_supply/BAT0/charge_control_start_threshold = (not available) [%]
/sys/class/power_supply/BAT0/charge_control_end_threshold   = (not available) [%]
