#!/usr/bin/env clitest
# Test charge thresholds for Legacy ThinkPads
# Requirements:
# * Hardware: Legacy ThinkPad (<= X201/T410)
# Copyright (c) 2024 Thomas Koch <linrunner at gmx.net>.
# SPDX-License-Identifier: GPL-2.0-or-later
#
$ # +++ Legacy ThinkPad +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
$ #
$ # --- 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= STOP_CHARGE_THRESH_BAT1=
TLP started in AC mode (auto).
$ sudo tlp start -- START_CHARGE_THRESH_BAT0="100" STOP_CHARGE_THRESH_BAT0="100" START_CHARGE_THRESH_BAT1= STOP_CHARGE_THRESH_BAT1=
Error in configuration at START_CHARGE_THRESH_BAT0="100": not specified, invalid or out of range (2..96). Battery skipped.
TLP started in AC mode (auto).
$ sudo tlp start -- START_CHARGE_THRESH_BAT0="2" STOP_CHARGE_THRESH_BAT0="2" START_CHARGE_THRESH_BAT1= STOP_CHARGE_THRESH_BAT1=
Error in configuration at STOP_CHARGE_THRESH_BAT0="2": not specified, invalid or out of range (6..100). Battery skipped.
TLP started in AC mode (auto).
$ sudo tlp start -- START_CHARGE_THRESH_BAT0="2" STOP_CHARGE_THRESH_BAT0="101" START_CHARGE_THRESH_BAT1= STOP_CHARGE_THRESH_BAT1=
Error in configuration at STOP_CHARGE_THRESH_BAT0="101": not specified, invalid or out of range (6..100). Battery skipped.
TLP started in AC mode (auto).
$ sudo tlp start -- START_CHARGE_THRESH_BAT0="96" STOP_CHARGE_THRESH_BAT0="99" START_CHARGE_THRESH_BAT1= STOP_CHARGE_THRESH_BAT1=
Error in configuration: START_CHARGE_THRESH_BAT0 > STOP_CHARGE_THRESH_BAT0 - 4. Battery skipped.
TLP started in AC mode (auto).
$ sudo tlp start -- START_CHARGE_THRESH_BAT0="95" STOP_CHARGE_THRESH_BAT0="99" START_CHARGE_THRESH_BAT1= STOP_CHARGE_THRESH_BAT1=
TLP started in AC mode (auto).
$ sudo tlp start -- START_CHARGE_THRESH_BAT0="DEF" STOP_CHARGE_THRESH_BAT0="DEF" START_CHARGE_THRESH_BAT1= STOP_CHARGE_THRESH_BAT1=
TLP started in AC mode (auto).
$ #
$ # --- 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 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 (2..96). Aborted.
$ sudo tlp setcharge -- START_CHARGE_THRESH_BAT0="2" STOP_CHARGE_THRESH_BAT0="2"
Error in configuration at STOP_CHARGE_THRESH_BAT0="2": not specified, invalid or out of range (6..100). Aborted.
$ sudo tlp setcharge -- START_CHARGE_THRESH_BAT0="2" STOP_CHARGE_THRESH_BAT0="101"
Error in configuration at STOP_CHARGE_THRESH_BAT0="101": not specified, invalid or out of range (6..100). Aborted.
$ sudo tlp setcharge -- START_CHARGE_THRESH_BAT0="96" STOP_CHARGE_THRESH_BAT0="99"
Error in configuration: START_CHARGE_THRESH_BAT0 > STOP_CHARGE_THRESH_BAT0 - 4. Aborted.
$ sudo tlp setcharge -- START_CHARGE_THRESH_BAT0="95" STOP_CHARGE_THRESH_BAT0="99"
Setting temporary charge thresholds for BAT0:
  start =  95
  stop  =  99
$ sudo tlp setcharge -- START_CHARGE_THRESH_BAT0="95" STOP_CHARGE_THRESH_BAT0="99"
Setting temporary charge thresholds for BAT0:
  start =  95 (no change)
  stop  =  99 (no change)
$ sudo tlp setcharge -- START_CHARGE_THRESH_BAT0="DEF" STOP_CHARGE_THRESH_BAT0="DEF"
Setting temporary charge thresholds for BAT0:
  start =  96
  stop  = 100
$ sudo tlp setcharge -- TPSMAPI_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 BAT0:
  start =  60
  stop  = 100 (no change)
$ sudo tlp setcharge 100 100
Error: start charge threshold (100) for BAT0 is not specified, invalid or out of range (2..96). Aborted.
$ sudo tlp setcharge 2 2
Error: stop charge threshold (2) for BAT0 is not specified, invalid or out of range (6..100). Aborted.
$ sudo tlp setcharge 2 101
Error: stop charge threshold (101) for BAT0 is not specified, invalid or out of range (6..100). Aborted.
$ sudo tlp setcharge XYZZY 0
Error: start charge threshold (XYZZY) for BAT0 is not specified, invalid or out of range (2..96). Aborted.
$ sudo tlp setcharge 2 XYZZY
Error: stop charge threshold (XYZZY) for BAT0 is not specified, invalid or out of range (6..100). Aborted.
$ sudo tlp setcharge 96 99
Error: start threshold > stop threshold - 4 for BAT0. Aborted.
$ sudo tlp setcharge 95 99
Setting temporary charge thresholds for BAT0:
  start =  95
  stop  =  99
$ sudo tlp setcharge 95 99 -- X_THRESH_SIMULATE_READERR="1"
Error: could not read current charge threshold(s) for BAT0. Aborted.
$ sudo tlp setcharge 95 99 -- X_THRESH_SIMULATE_START="60" X_THRESH_SIMULATE_STOP="100"
Setting temporary charge thresholds for BAT0:
  start =  95
  stop  =  99
$ sudo tlp setcharge 95 99
Setting temporary charge thresholds for BAT0:
  start =  95 (no change)
  stop  =  99 (no change)
$ sudo tlp setcharge DEF DEF
Setting temporary charge thresholds for BAT0:
  start =  96
  stop  = 100
$ 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.
$ #
$ # --- tlp-stat
$ sudo tlp-stat -b | grep -E 'charge_thresh|discharge'
/sys/devices/platform/smapi/BAT0/start_charge_thresh        =     96 [%]
/sys/devices/platform/smapi/BAT0/stop_charge_thresh         =    100 [%]
/sys/devices/platform/smapi/BAT0/force_discharge            =      0
$ sudo tlp-stat -b -- X_THRESH_SIMULATE_READERR=1 | grep -E 'charge_thresh|discharge'
/sys/devices/platform/smapi/BAT0/start_charge_thresh        = (not available) [%]
/sys/devices/platform/smapi/BAT0/stop_charge_thresh         = (not available) [%]
/sys/devices/platform/smapi/BAT0/force_discharge            =      0
$ #
$ # --- Feature Detection Edge Cases and Kernel Module Recommendations
$ sudo ./kmod-helper tp_smapi restore
$ sudo ./kmod-helper tp_smapi enable
$ sudo tlp-stat -b | head -7 | tail -4
Plugin: thinkpad-legacy
Supported features: charge thresholds, recalibration
Driver usage:
* tp-smapi (tp_smapi) = active (status, charge thresholds, recalibration)
$ sudo tlp-stat -b -- NATACPI_ENABLE=0 | head -7 | tail -4
Plugin: thinkpad-legacy
Supported features: charge thresholds, recalibration
Driver usage:
* tp-smapi (tp_smapi) = active (status, charge thresholds, recalibration)
$ sudo tlp-stat -b -- NATACPI_ENABLE=0 TPACPI_ENABLE=0 | head -7 | tail -4
Plugin: thinkpad-legacy
Supported features: charge thresholds, recalibration
Driver usage:
* tp-smapi (tp_smapi) = active (status, charge thresholds, recalibration)
$ sudo ./kmod-helper tp_smapi disable
$ sudo tlp-stat -b | head -7 | tail -4
Plugin: thinkpad-legacy
Supported features: none available
Driver usage:
* tp-smapi (tp_smapi) = inactive (kernel module 'tp_smapi' load error)
$ sudo ./kmod-helper tp_smapi enable
$ sudo ./kmod-helper tp_smapi remove
$ sudo tlp-stat -b | head -7 | tail -4
Plugin: thinkpad-legacy
Supported features: none available
Driver usage:
* tp-smapi (tp_smapi) = inactive (kernel module 'tp_smapi' not installed)
$ sudo tlp-stat -b | grep -A1 '+++ Recommendations'
+++ Recommendations
* Install tp-smapi kernel modules for ThinkPad battery thresholds and recalibration
$ sudo ./kmod-helper tp_smapi restore
$ #
$ # --- Reset test machine to configured thresholds
$ sudo tlp setcharge BAT0  > /dev/null 2>&1
$ #
