#!/usr/bin/env clitest
# Test charge thresholds for laptops with ChromeOS EC (cmd v3)
# Requirements:
# * Hardware: yet unknown, possibly Chromebooks from July 2021
# * Software: kernel module cros_charge-control (Linux 6.12.8/6.13+)
# Copyright (c) 2025 Thomas Koch <linrunner at gmx.net>.
# SPDX-License-Identifier: GPL-2.0-or-later
#
$ # +++ ChromeOS EC (cmd v3) laptops +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
$ #
$ # --- tlp start
$ sudo tlp start -- ${xinc} START_CHARGE_THRESH_${bata}= STOP_CHARGE_THRESH_${bata}= START_CHARGE_THRESH_${batb}= STOP_CHARGE_THRESH_${batb}=
TLP started in AC mode (auto).
$ sudo tlp start -- ${xinc} START_CHARGE_THRESH_${bata}="60" STOP_CHARGE_THRESH_${bata}="100" START_CHARGE_THRESH_${batb}= STOP_CHARGE_THRESH_${batb}=
TLP started in AC mode (auto).
$ 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/"
Error in configuration at START_CHARGE_THRESH_BATA="100": not specified, invalid or out of range (0..99). Battery skipped.
TLP started in AC mode (auto).
$ sudo tlp start -- ${xinc} START_CHARGE_THRESH_${bata}="0" STOP_CHARGE_THRESH_${bata}="0" START_CHARGE_THRESH_${batb}= STOP_CHARGE_THRESH_${batb}= 2>&1 | sed -r "s/${bata}/BATA/"
Error in configuration at STOP_CHARGE_THRESH_BATA="0": not specified, invalid or out of range (1..100). Battery skipped.
TLP started in AC mode (auto).
$ sudo tlp start -- ${xinc} START_CHARGE_THRESH_${bata}="0" STOP_CHARGE_THRESH_${bata}="101" START_CHARGE_THRESH_${batb}= STOP_CHARGE_THRESH_${batb}= 2>&1 | sed -r "s/${bata}/BATA/"
Error in configuration at STOP_CHARGE_THRESH_BATA="101": not specified, invalid or out of range (1..100). Battery skipped.
TLP started in AC mode (auto).
$ sudo tlp start -- ${xinc} START_CHARGE_THRESH_${bata}="97" STOP_CHARGE_THRESH_${bata}="97" START_CHARGE_THRESH_${batb}= STOP_CHARGE_THRESH_${batb}= 2>&1 | sed -r "s/${bata}/BATA/g"
Error in configuration: START_CHARGE_THRESH_BATA >= STOP_CHARGE_THRESH_BATA. Battery skipped.
TLP started in AC mode (auto).
$ sudo tlp start -- ${xinc} START_CHARGE_THRESH_${bata}="95" STOP_CHARGE_THRESH_${bata}="96" START_CHARGE_THRESH_${batb}= STOP_CHARGE_THRESH_${batb}=
TLP started in AC mode (auto).
$ sudo tlp start -- ${xinc} START_CHARGE_THRESH_${bata}="DEF" STOP_CHARGE_THRESH_${bata}="DEF" START_CHARGE_THRESH_${batb}= STOP_CHARGE_THRESH_${batb}=
TLP started in AC mode (auto).
$ sudo tlp start -- ${xinc} NATACPI_ENABLE=0 START_CHARGE_THRESH_${bata}="DEF" STOP_CHARGE_THRESH_${bata}="DEF"
TLP started in AC mode (auto).
$ #
$ # --- tlp setcharge w/o arguments
$ 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 threshold(s) 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 out of range (0..99). Aborted.
$ sudo tlp setcharge -- ${xinc} START_CHARGE_THRESH_${bata}="0" 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 out of range (1..100). Aborted.
$ sudo tlp setcharge -- ${xinc} START_CHARGE_THRESH_${bata}="0" STOP_CHARGE_THRESH_${bata}="101" 2>&1 | sed -r "s/${bata}/BATA/"
Error in configuration at STOP_CHARGE_THRESH_BATA="101": not specified, invalid or out of range (1..100). Aborted.
$ sudo tlp setcharge -- ${xinc} START_CHARGE_THRESH_${bata}="97" STOP_CHARGE_THRESH_${bata}="97" 2>&1 | sed -r "s/${bata}/BATA/g"
Error in configuration: START_CHARGE_THRESH_BATA >= STOP_CHARGE_THRESH_BATA. Aborted.
$ sudo tlp setcharge -- ${xinc} START_CHARGE_THRESH_${bata}="95" STOP_CHARGE_THRESH_${bata}="96" 2>&1 | sed -r "s/${bata}/BATA/"
Setting temporary charge threshold(s) for battery BATA:
  start =  95
  stop  =  96
$ sudo tlp setcharge -- ${xinc} START_CHARGE_THRESH_${bata}="95" STOP_CHARGE_THRESH_${bata}="96" 2>&1 | sed -r "s/${bata}/BATA/"
Setting temporary charge threshold(s) for battery BATA:
  start =  95 (no change)
  stop  =  96 (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 threshold(s) for battery BATA:
  start =   0
  stop  = 100
$ sudo tlp setcharge -- ${xinc} NATACPI_ENABLE=0 START_CHARGE_THRESH_${bata}="DEF" STOP_CHARGE_THRESH_${bata}="DEF"
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 threshold(s) for battery BATA:
  start =  60
  stop  = 100 (no change)
$ 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 out of range (0..99). Aborted.
$ sudo tlp setcharge 0 0 -- ${xinc} 2>&1 | sed -r "s/${bata}/BATA/"
Error: stop charge threshold (0) for BATA is not specified, invalid or out of range (1..100). Aborted.
$ sudo tlp setcharge 0 101 -- ${xinc} 2>&1 | sed -r "s/${bata}/BATA/"
Error: stop charge threshold (101) for BATA is not specified, invalid or out of range (1..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 out of range (0..99). Aborted.
$ sudo tlp setcharge 0 XYZZY -- ${xinc} 2>&1 | sed -r "s/${bata}/BATA/"
Error: stop charge threshold (XYZZY) for BATA is not specified, invalid or out of range (1..100). Aborted.
$ sudo tlp setcharge 97 97 -- ${xinc} 2>&1 | sed -r "s/${bata}/BATA/"
Error: start threshold >= stop threshold for BATA. Aborted.
$ sudo tlp setcharge 95 96 -- ${xinc} 2>&1 | sed -r "s/${bata}/BATA/"
Setting temporary charge threshold(s) for battery BATA:
  start =  95
  stop  =  96
$ sudo tlp setcharge 95 96 -- ${xinc} X_THRESH_SIMULATE_READERR="1" 2>&1 | sed -r "s/${bata}/BATA/"
Error: could not read current charge threshold(s) for BATA. Aborted.
$ sudo tlp setcharge 95 96 -- ${xinc} 2>&1 | sed -r "s/${bata}/BATA/"
Setting temporary charge threshold(s) for battery BATA:
  start =  95 (no change)
  stop  =  96 (no change)
$ sudo tlp setcharge DEF DEF -- ${xinc} 2>&1 | sed -r "s/${bata}/BATA/"
Setting temporary charge threshold(s) for battery BATA:
  start =   0
  stop  = 100
$ 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 "${bata}/charge_(control|behaviour)" | sed -r "s/${bata}/BATA/"
/sys/class/power_supply/BATA/charge_control_start_threshold =      0 [%]
/sys/class/power_supply/BATA/charge_control_end_threshold   =    100 [%]
/sys/class/power_supply/BATA/charge_behaviour               = [auto] inhibit-charge force-discharge
$ sudo tlp-stat -b -- ${xinc} X_THRESH_SIMULATE_READERR=1 | grep -E "${bata}/charge_(control|behaviour)" | sed -r "s/${bata}/BATA/"
/sys/class/power_supply/BATA/charge_control_start_threshold = (not available) [%]
/sys/class/power_supply/BATA/charge_control_end_threshold   = (not available) [%]
/sys/class/power_supply/BATA/charge_behaviour               = [auto] inhibit-charge force-discharge
$ #
$ # --- Reset test machine to configured thresholds
$ sudo tlp setcharge -- ${xinc} > /dev/null 2>&1
$ #
