#!/usr/bin/env clitest
# Test charge thresholds for Tuxedo laptops
# Requirements:
# * Hardware: Tuxedo Laptop (Clevo OEM chassis)
# * Software: kernel module clevo_acpi (out-of-tree)
# Copyright (c) 2025 Thomas Koch <linrunner at gmx.net>.
# SPDX-License-Identifier: GPL-2.0-or-later
#
$ # +++ Tuxedo laptops +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
$ #
$ # --- tlp start
$ sudo tlp start -- ${xinc} START_CHARGE_THRESH_${bata}= STOP_CHARGE_THRESH_${bata}= START_CHARGE_THRESH_${batb}= STOP_CHARGE_THRESH_${batb}= 2>&1 | sed -r "s/(TLP started).*/\1/"
TLP started
$ sudo tlp start -- ${xinc} START_CHARGE_THRESH_${bata}="60" STOP_CHARGE_THRESH_${bata}="100" START_CHARGE_THRESH_${batb}= STOP_CHARGE_THRESH_${batb}= 2>&1 | sed -r "s/${bata}/BATA/;s/(TLP started).*/\1/"
TLP started
$ sudo tlp start -- ${xinc} START_CHARGE_THRESH_${bata}="100" STOP_CHARGE_THRESH_${bata}="100" START_CHARGE_THRESH_${batb}= STOP_CHARGE_THRESH_${batb}= 2>&1 | sed -r "s/${bata}/BATA/;s/(TLP started).*/\1/"
Error in configuration at START_CHARGE_THRESH_BATA="100": not specified, invalid or not in {40, 50, 60, 70, 80, 95}. Battery skipped.
TLP started
$ sudo tlp start -- ${xinc} START_CHARGE_THRESH_${bata}="50" STOP_CHARGE_THRESH_${bata}="0" START_CHARGE_THRESH_${batb}= STOP_CHARGE_THRESH_${batb}= 2>&1 | sed -r "s/${bata}/BATA/;s/(TLP started).*/\1/"
Error in configuration at STOP_CHARGE_THRESH_BATA="0": not specified, invalid or not in {60, 70, 80, 90, 100}. Battery skipped.
TLP started
$ sudo tlp start -- ${xinc} START_CHARGE_THRESH_${bata}="50" STOP_CHARGE_THRESH_${bata}="101" START_CHARGE_THRESH_${batb}= STOP_CHARGE_THRESH_${batb}= 2>&1 | sed -r "s/${bata}/BATA/;s/(TLP started).*/\1/"
Error in configuration at STOP_CHARGE_THRESH_BATA="101": not specified, invalid or not in {60, 70, 80, 90, 100}. Battery skipped.
TLP started
$ sudo tlp start -- ${xinc} START_CHARGE_THRESH_${bata}="80" STOP_CHARGE_THRESH_${bata}="80" START_CHARGE_THRESH_${batb}= STOP_CHARGE_THRESH_${batb}= 2>&1 | sed -r "s/${bata}/BATA/g;s/(TLP started).*/\1/"
Error in configuration: START_CHARGE_THRESH_BATA >= STOP_CHARGE_THRESH_BATA. Battery skipped.
TLP started
$ sudo tlp start -- ${xinc} START_CHARGE_THRESH_${bata}="80" STOP_CHARGE_THRESH_${bata}="90" START_CHARGE_THRESH_${batb}= STOP_CHARGE_THRESH_${batb}= 2>&1 | sed -r "s/(TLP started).*/\1/"
TLP started
$ [ -n "${xinc}" ] && [ -n "$VWRITE_SLEEP" ] && sleep "$VWRITE_SLEEP" # workaround: mitigate void writes (in simulation)
$ sudo tlp start -- ${xinc} START_CHARGE_THRESH_${bata}="DEF" STOP_CHARGE_THRESH_${bata}="DEF" START_CHARGE_THRESH_${batb}= STOP_CHARGE_THRESH_${batb}= 2>&1 | sed -r "s/(TLP started).*/\1/"
TLP started
$ sudo tlp start -- ${xinc} NATACPI_ENABLE=0 START_CHARGE_THRESH_${bata}="DEF" STOP_CHARGE_THRESH_${bata}="DEF" 2>&1 | sed -r "s/(TLP started).*/\1/"
TLP started
$ #
$ # --- tlp setcharge w/o arguments
$ [ -n "${xinc}" ] && [ -n "$VWRITE_SLEEP" ] && sleep "$VWRITE_SLEEP" # workaround: mitigate void writes (in simulation)
$ sudo tlp setcharge -- ${xinc} START_CHARGE_THRESH_${bata}="60" STOP_CHARGE_THRESH_${bata}="100" X_SOC_CHECK=0 2>&1 | sed -r "s/${bata}/BATA/"
Setting temporary charge thresholds for battery BATA:
  start =  60
  stop  = 100 (no change)
$ sudo tlp setcharge -- ${xinc} START_CHARGE_THRESH_${bata}="100" STOP_CHARGE_THRESH_${bata}="100" 2>&1 | sed -r "s/${bata}/BATA/"
Error in configuration at START_CHARGE_THRESH_BATA="100": not specified, invalid or not in {40, 50, 60, 70, 80, 95}. Aborted.
$ sudo tlp setcharge -- ${xinc} START_CHARGE_THRESH_${bata}="50" STOP_CHARGE_THRESH_${bata}="0" 2>&1 | sed -r "s/${bata}/BATA/"
Error in configuration at STOP_CHARGE_THRESH_BATA="0": not specified, invalid or not in {60, 70, 80, 90, 100}. Aborted.
$ sudo tlp setcharge -- ${xinc} START_CHARGE_THRESH_${bata}="40" STOP_CHARGE_THRESH_${bata}="85" 2>&1 | sed -r "s/${bata}/BATA/"
Error in configuration at STOP_CHARGE_THRESH_BATA="85": not specified, invalid or not in {60, 70, 80, 90, 100}. Aborted.
$ sudo tlp setcharge -- ${xinc} START_CHARGE_THRESH_${bata}="45" STOP_CHARGE_THRESH_${bata}="90" 2>&1 | sed -r "s/${bata}/BATA/"
Error in configuration at START_CHARGE_THRESH_BATA="45": not specified, invalid or not in {40, 50, 60, 70, 80, 95}. Aborted.
$ sudo tlp setcharge -- ${xinc} START_CHARGE_THRESH_${bata}="80" STOP_CHARGE_THRESH_${bata}="70" 2>&1 | sed -r "s/${bata}/BATA/"
Error in configuration: START_CHARGE_THRESH_BATA >= STOP_CHARGE_THRESH_BAT0. Aborted.
$ sudo tlp setcharge -- ${xinc} START_CHARGE_THRESH_${bata}="70" STOP_CHARGE_THRESH_${bata}="90" 2>&1 | sed -r "s/${bata}/BATA/"
Setting temporary charge thresholds for battery BATA:
  start =  70
  stop  =  90
$ sudo tlp setcharge -- ${xinc} START_CHARGE_THRESH_${bata}="70" STOP_CHARGE_THRESH_${bata}="90" 2>&1 | sed -r "s/${bata}/BATA/"
Setting temporary charge thresholds for battery BATA:
  start =  70 (no change)
  stop  =  90 (no change)
$ sudo tlp setcharge -- ${xinc} START_CHARGE_THRESH_${bata}="DEF" STOP_CHARGE_THRESH_${bata}="DEF" 2>&1 | sed -r "s/${bata}/BATA/"
Setting temporary charge thresholds for battery BATA:
  stop  = 100
  start =  95
$ sudo tlp setcharge -- ${xinc} NATACPI_ENABLE=0 START_CHARGE_THRESH_${bata}="DEF" STOP_CHARGE_THRESH_${bata}="DEF" 2>&1 | sed -r "s/${bata}/BATA/"
Error: there is no hardware driver support for charge thresholds.
$ #
$ # --- tlp setcharge w/ arguments
$ sudo tlp setcharge 60 100 -- ${xinc} X_SOC_CHECK=0 2>&1 | sed -r "s/${bata}/BATA/"
Setting temporary charge thresholds for battery BATA:
  start =  60
  stop  = 100 (no change)
$ sudo tlp setcharge 50 90 -- ${xinc} X_THRESH_SIMULATE_WRITEERR=1 X_SOC_CHECK=0 2>&1 | sed -r "s/${bata}/BATA/"
Setting temporary charge thresholds for battery BATA:
  start =  50 (Error: write failed)
  stop  =  90 (Error: write failed)
$ sudo tlp setcharge 100 100 -- ${xinc} 2>&1 | sed -r "s/${bata}/BATA/"
Error: start charge threshold (100) for BATA is not specified, invalid or not in {40, 50, 60, 70, 80, 95}. Aborted.
$ sudo tlp setcharge 50 0 -- ${xinc} 2>&1 | sed -r "s/${bata}/BATA/"
Error: stop charge threshold (0) for BATA is not specified, invalid or not in {60, 70, 80, 90, 100}. Aborted.
$ sudo tlp setcharge 40 85 -- ${xinc} 2>&1 | sed -r "s/${bata}/BATA/"
Error: stop charge threshold (85) for BATA is not specified, invalid or not in {60, 70, 80, 90, 100}. Aborted.
$ sudo tlp setcharge XYZZY 0 -- ${xinc} 2>&1 | sed -r "s/${bata}/BATA/"
Error: start charge threshold (XYZZY) for BATA is not specified, invalid or not in {40, 50, 60, 70, 80, 95}. Aborted.
$ sudo tlp setcharge 50 XYZZY -- ${xinc} 2>&1 | sed -r "s/${bata}/BATA/"
Error: stop charge threshold (XYZZY) for BATA is not specified, invalid or not in {60, 70, 80, 90, 100}. Aborted.
$ sudo tlp setcharge 80 70 -- ${xinc} 2>&1 | sed -r "s/${bata}/BATA/"
Error: start threshold >= stop threshold for BATA. Aborted.
$ sudo tlp setcharge 70 90 -- ${xinc} 2>&1 | sed -r "s/${bata}/BATA/"
Setting temporary charge thresholds for battery BATA:
  start =  70
  stop  =  90
$ sudo tlp setcharge 70 90 -- ${xinc} X_THRESH_SIMULATE_READERR="1" 2>&1 | sed -r "s/${bata}/BATA/"
Error: could not read current charge threshold(s) for battery BATA. Aborted.
$ sudo tlp setcharge 70 90 -- ${xinc} X_THRESH_SIMULATE_START="60" X_THRESH_SIMULATE_STOP="100" 2>&1 | sed -r "s/${bata}/BATA/"
Setting temporary charge thresholds for battery BATA:
  start =  70
  stop  =  90
$ sudo tlp setcharge 70 90 -- ${xinc} 2>&1 | sed -r "s/${bata}/BATA/"
Setting temporary charge thresholds for battery BATA:
  start =  70 (no change)
  stop  =  90 (no change)
$ sudo tlp setcharge DEF DEF -- ${xinc} 2>&1 | sed -r "s/${bata}/BATA/"
Setting temporary charge thresholds for battery BATA:
  stop  = 100
  start =  95
$ sudo tlp setcharge ${batb} -- ${xinc} 2>&1 | sed -r "s/${batb}/BATB/"
Error: battery BATB not present.
$ sudo tlp setcharge 0 3 ${batb} -- ${xinc} 2>&1 | sed -r "s/${batb}/BATB/"
Error: battery BATB not present.
$ sudo tlp setcharge XYZZY ABCDE ${batb} -- ${xinc} 2>&1 | sed -r "s/${batb}/BATB/"
Error: battery BATB not present.
$ #
$ # --- tlp-stat
$ sudo tlp-stat -b -- ${xinc} | grep -E 'BAT0/charge_(control_end|behaviour)'
/sys/class/power_supply/BAT0/charge_control_end_threshold   =    100 [%]
$ sudo tlp-stat -b -- ${xinc} X_THRESH_SIMULATE_READERR=1 | grep -E 'BAT0/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) [%]
$ #
$ # --- Reset test machine to configured thresholds
$ sudo tlp setcharge ${bata} - ${xinc} > /dev/null 2>&1
$ #
