Skip to content

Commit

Permalink
bat.d (thinkpad): use new sysfs attribute 'charge_behaviour' for reca…
Browse files Browse the repository at this point in the history
  • Loading branch information
linrunner committed Nov 16, 2021
1 parent 027c30f commit f0bf18f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 15 deletions.
48 changes: 35 additions & 13 deletions bat.d/05-thinkpad
Expand Up @@ -237,11 +237,11 @@ batdrv_init () {
_natacpi=1
_bm_thresh="natacpi"

if readable_sysf $bd/force_discharge; then
# forced discharge exists and is actually readable
if readable_sysf $bd/charge_behaviour; then
# sysfile for force-discharge exists and is actually readable
_natacpi=0
_bm_dischg="natacpi"
_bn_dischg="force_discharge"
_bn_dischg="charge_behaviour"
fi
fi
fi
Expand Down Expand Up @@ -815,19 +815,30 @@ batdrv_apply_configured_thresholds () {
}

batdrv_read_force_discharge () {
# read and print force discharge state
# read and print force-discharge state
# $1: error string
# global param: $_bm_dischg, $_bf_dischg, $_bat_idx
# out: 0=off/1=on/"$1" on error
# rc: 0=ok/4=read error/255=no api
# retval: $_bt_charge_behaviour
# prerequisite: batdrv_init(), batdrv_select_battery()

local rc=0 out

case $_bm_dischg in
natacpi)
# read force discharge from sysfile, "$1" if not readable/non-existent
out=$(read_sysf $_bf_dischg "$1");
# read state from sysfile
if _bt_charge_behaviour=$(read_sysf $_bf_dischg); then
if echo "$_bt_charge_behaviour" | grep -q "\[force-discharge\]"; then
out=1
else
out=0
fi
else
# not readable/non-existent
out="$1"
rc=4
fi
;;

tpacpi) # read via tpacpi-bat
Expand All @@ -848,7 +859,7 @@ batdrv_read_force_discharge () {
esac
printf "%s" "$out"

echo_debug "bat" "batdrv.${_batdrv_plugin}.read_force_discharge($_bat_str): bm_dischg=$_bm_dischg; bf_dischg=$_bf_dischg; bat_idx=$_bat_idx; out=$out; rc=$rc"
echo_debug "bat" "batdrv.${_batdrv_plugin}.read_force_discharge($_bat_str): bm_dischg=$_bm_dischg; bf_dischg=$_bf_dischg; bat_idx=$_bat_idx; out=$out; cb=$_bt_charge_behaviour; rc=$rc"
return $rc
}

Expand All @@ -864,7 +875,10 @@ batdrv_write_force_discharge () {
case $_bm_dischg in
natacpi)
# write force_discharge
write_sysf "$1" $_bf_dischg || rc=5
case "$1" in
0) write_sysf "auto" $_bf_dischg || rc=5 ;;
1) write_sysf "force-discharge" $_bf_dischg || rc=5 ;;
esac
;; # natacpi

tpacpi) # use tpacpi-bat
Expand Down Expand Up @@ -1014,7 +1028,7 @@ batdrv_discharge () {
;; # tpsmapi

esac
printf "force discharge = %s\n" "$(batdrv_read_force_discharge \"\")"
printf "force-discharge = %s\n" "$(batdrv_read_force_discharge \"\")"

echo "Press Ctrl+C to cancel."
sleep 5
Expand Down Expand Up @@ -1265,13 +1279,21 @@ batdrv_show_battery_data () {
esac
if [ "$_bm_dischg" != "none" ]; then
if dc=$(batdrv_read_force_discharge ""); then
# valid force_discharge read
# valid force-discharge read
case $_bm_dischg in
natacpi) printf "%-59s = %6d\n" "$_bf_dischg" "$dc" ;;
tpacpi) printf "%-59s = %6d\n" "tpacpi-bat.${_bat_str}.forceDischarge" "$dc" ;;
natacpi)
# call batdrv_read_force_discharge again without subshell
# to obtain the retval $_bt_charge_behaviour
batdrv_read_force_discharge "" > /dev/null
printf "%-59s = %s\n" "$_bf_dischg" "$_bt_charge_behaviour"
;;

tpacpi)
printf "%-59s = %6d\n" "tpacpi-bat.${_bat_str}.forceDischarge" "$dc"
;;
esac
else
# force_discharge read failed
# force-discharge read failed
case $_bm_dischg in
natacpi) printf "%-59s = (not available)\n" "$_bf_dischg" ;;
tpacpi) printf "%-59s = %6d\n" "tpacpi-bat.${_bat_str}.forceDischarge" "(not available)" ;;
Expand Down
5 changes: 3 additions & 2 deletions changelog
@@ -1,8 +1,9 @@
+++ <unreleased> --- 15.11.2021 +++
+++ <unreleased> --- 16.11.2021 +++

* Features
Battery Care:
Battery:
- Sony laptops: stop threshold 50/80/100% aka "battery care limiter"
- ThinkPads: use new sysfs attribute 'charge_behaviour' for recalibration
Radio Devices:
- Add support for switching NFC devices
- Remove support for wireless-tools (iwconfig)
Expand Down

0 comments on commit f0bf18f

Please sign in to comment.