forked from linrunner/TLP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tlp-func-base.in
787 lines (643 loc) · 22.4 KB
/
tlp-func-base.in
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
#!/bin/sh
# tlp - Base Functions
#
# Copyright (c) 2021 Thomas Koch <linrunner at gmx.net> and others.
# This software is licensed under the GPL v2 or later.
# shellcheck disable=SC2034,SC2086
# ----------------------------------------------------------------------------
# Constants
readonly TLPVER="@TLPVER@"
readonly RUNDIR=@TLP_RUN@
readonly VARDIR=@TLP_VAR@
readonly CONF_DEF=@TLP_CONFDEF@
readonly CONF_DIR=@TLP_CONFDIR@
readonly CONF_USR=@TLP_CONFUSR@
readonly CONF_OLD=@TLP_CONF@
readonly CONF_RUN="$RUNDIR/run.conf"
readonly FLOCK=flock
readonly HDPARM=hdparm
readonly LAPMODE=laptop_mode
readonly LOGGER=logger
readonly MKTEMP=mktemp
readonly MODPRO=modprobe
readonly READCONFS=@TLP_TLIB@/tlp-readconfs
readonly TPACPIBAT=@TPACPIBAT@
readonly UDEVADM=udevadm
readonly LOCKFILE=$RUNDIR/lock
readonly LOCKTIMEOUT=2
readonly PWRRUNFILE=$RUNDIR/last_pwr
readonly MANUALMODEFILE=$RUNDIR/manual_mode
readonly MOD_MSR="msr"
readonly MOD_TEMP="coretemp"
readonly DMID=/sys/class/dmi/id/
readonly NETD=/sys/class/net
readonly RE_PARAM='^[A-Z_]+[0-9]*=[0-9a-zA-Z _\-\:\.]*$'
# power supplies: ignore MacBook Pro 2017 sbs-charger and hid devices
readonly RE_PS_IGNORE='sbs-charger|hidpp_battery|hid-'
readonly DEBUG_TAGS_ALL="arg bat cfg disk lock nm path pm ps rf run sysfs udev usb"
# ----------------------------------------------------------------------------
# Control
_nodebug=0
# ----------------------------------------------------------------------------
# Functions
# -- Exit
do_exit () { # cleanup and exit -- $1: rc
# remove temporary runconf
[ -f "$_conf_tmp" ] && rm -rf "$_conf_tmp"
exit $1
}
# --- Debug
echo_debug () { # write debug msg if tag matches -- $1: tag; $2: msg;
[ "$_nodebug" = "1" ] && return 0
if wordinlist "$1" "$TLP_DEBUG"; then
$LOGGER -p debug -t "tlp" --id=$$ -- "$2" > /dev/null 2>&1
fi
}
# --- Strings
tolower () { # echo string in lowercase -- $1: string
printf "%s" "$1" | tr "[:upper:]" "[:lower:]"
}
toupper () { # echo string in lowercase -- $1: string
printf "%s" "$1" | tr "[:lower:]" "[:upper:]"
}
wordinlist () { # test if word in list
# $1: word, $2: whitespace-separated list of words
local word
if [ -n "${1-}" ]; then
for word in ${2-}; do
[ "${word}" != "${1}" ] || return 0 # exact match
done
fi
return 1 # no match
}
# --- Sysfiles
check_sysfs () { # debug: check if sysfile exists -- $1: routine; $2: sysfs path
if wordinlist "sysfs" "$TLP_DEBUG"; then
if [ ! -e $2 ]; then
$LOGGER -p debug -t "tlp" --id=$$ -- "$1: $2 nonexistent" > /dev/null 2>&1
fi
fi
}
read_sysf () { # read and echo contents of sysfile
# return 1 and echo default if read fails
# $1: sysfile, $2: default; rc: 0=ok/1=error
if ! cat "$1" 2> /dev/null; then
printf "%s" "$2"
return 1
else
return 0
fi
}
readable_sysf () { # check if file is actually readable -- $1: file
# rc: 0=readable/1=read error
cat "$1" > /dev/null 2>&1
}
read_sysval () { # read and echo contents of sysfile
# echo '0' if file is non-existent, read fails or is non-numeric
# $1: sysfile; rc: 0=ok/1=error
printf "%d" "$(read_sysf $1)" 2> /dev/null
}
write_sysf () { # write string to a sysfile
# $1: string, $2: sysfile
# rc: 0=ok/1=error
{ printf '%s\n' "$1" > "$2"; } 2> /dev/null
}
# --- Tests
cmd_exists () { # test if command exists -- $1: command
command -v $1 > /dev/null 2>&1
}
test_root () { # test root privilege -- rc: 0=root, 1=not root
[ "$(id -u)" = "0" ]
}
check_root () { # show error message and quit when root privilege missing
if ! test_root; then
echo "Error: missing root privilege." 1>&2
do_exit 1
fi
}
check_tlp_enabled () { # check if TLP is enabled in config file
# $1: 1=verbose (default: 0)
# rc: 0=disabled/1=enabled
if [ "$TLP_ENABLE" = "1" ]; then
return 0
else
[ "${1:-0}" = "1" ] && echo "Error: TLP power save is disabled. Set TLP_ENABLE=1 in ${CONF_USR}." 1>&2
return 1
fi
}
# --- Type and value checking
is_uint () { # check for unsigned integer -- $1: string; $2: max digits
printf "%s" "$1" | grep -E -q "^[0-9]{1,$2}$" 2> /dev/null
}
is_within_bounds () { # check condition min <= value <= max
# $1: value; $2: min; $3: max (all unsigned int)
# rc: 0=within/1=below/2=above/255=invalid
#
# value, min or max undefined/non-numeric means that this branch of the
# condition is fulfilled
is_uint $1 || return 255
if is_uint $2; then
[ $1 -ge $2 ] || return 1
fi
if is_uint $3; then
[ $1 -le $3 ] || return 2
fi
return 0
}
# --- Locking and Semaphores
set_run_flag () { # set flag -- $1: flag name
# rc: 0=success/1,2=failed
local rc
create_rundir
touch $RUNDIR/$1; rc=$?
echo_debug "lock" "set_run_flag.touch: $1; rc=$rc"
return $rc
}
reset_run_flag () { # reset flag -- $1: flag name
if rm $RUNDIR/$1 2> /dev/null 1>&2 ; then
echo_debug "lock" "reset_run_flag($1).remove"
else
echo_debug "lock" "reset_run_flag($1).not_found"
fi
return 0
}
check_run_flag () { # check flag -- $1: flag name
# rc: 0=flag set/1=flag not set
local rc
[ -f $RUNDIR/$1 ]; rc=$?
echo_debug "lock" "check_run_flag($1): rc=$rc"
return $rc
}
lock_tlp () { # get exclusive lock: blocking with timeout
# $1: lock id (default: tlp)
# rc: 0=success/1=failed
create_rundir
# open file for writing and attach fd 9
# when successful lock fd 9 exclusive and blocking
# wait $LOCKTIMEOUT secs to obtain the lock
if { exec 9> ${LOCKFILE}_${1:-tlp} ; } 2> /dev/null && $FLOCK -x -w $LOCKTIMEOUT 9 ; then
echo_debug "lock" "lock_tlp($1).success"
return 0
else
echo_debug "lock" "lock_tlp($1).failed"
return 1
fi
}
lock_tlp_nb () { # get exclusive lock: non-blocking
# $1: lock id (default: tlp)
# rc: 0=success/1=failed
create_rundir
# open file for writing and attach fd 9
# when successful lock fd 9 exclusive and non-blocking
if { exec 9> ${LOCKFILE}_${1:-tlp} ; } 2> /dev/null && $FLOCK -x -n 9 ; then
echo_debug "lock" "lock_tlp_nb($1).success"
return 0
else
echo_debug "lock" "lock_tlp_nb($1).failed"
return 1
fi
}
unlock_tlp () { # free exclusive lock
# $1: lock id (default: tlp)
# defer unlock for $X_DEFER_UNLOCK seconds -- debugging only
[ -n "$X_DEFER_UNLOCK" ] && sleep $X_DEFER_UNLOCK
# free fd 9 and scrap lockfile
{ exec 9>&- ; } 2> /dev/null
rm -f ${LOCKFILE}_${1:-tlp}
echo_debug "lock" "unlock_tlp($1)"
return 0
}
lockpeek_tlp () { # check for pending lock (by looking for the lockfile)
# $1: lock id (default: tlp)
if [ -f ${LOCKFILE}_${1:-tlp} ]; then
echo_debug "lock" "lockpeek_tlp($1).locked"
return 0
else
echo_debug "lock" "lockpeek_tlp($1).not_locked"
return 1
fi
}
echo_tlp_locked () { # print "locked" message
echo "Error: TLP is locked by another operation." 1>&2
return 0
}
set_timed_lock () { # create timestamp n seconds in the future
# $1: lock id, $2: lock duration [s]
local lock rc time
lock=${1}_timed_lock_$(date +%s -d "+${2} seconds")
set_run_flag $lock; rc=$?
echo_debug "lock" "set_timed_lock($1, $2): $lock; rc=$rc"
# cleanup obsolete locks
time=$(date +%s)
for lockfile in "$RUNDIR/${1}_timed_lock_"*; do
if [ -f "$lockfile" ]; then
locktime=${lockfile#${RUNDIR}/${1}_timed_lock_}
if [ $time -ge $locktime ]; then
rm -f $lockfile
echo_debug "lock" "set_timed_lock($1, $2).remove_obsolete: ${lockfile#${RUNDIR}/}"
fi
fi
done
return $rc
}
check_timed_lock () { # check if active timestamp exists
# $1: lock id; rc: 0=locked/1=not locked
local lockfile locktime time
time=$(date +%s)
for lockfile in "$RUNDIR/${1}_timed_lock_"*; do
if [ -f "$lockfile" ]; then
locktime=${lockfile#${RUNDIR}/${1}_timed_lock_}
if [ $time -lt $(( locktime - 120 )) ]; then
# timestamp is more than 120 secs in the future,
# something weird has happened -> remove it
rm -f $lockfile
echo_debug "lock" "check_timed_lock($1).remove_invalid: ${lockfile#${RUNDIR}/}"
elif [ $time -lt $locktime ]; then
# timestamp in the future -> we're locked
echo_debug "lock" "check_timed_lock($1).locked: $time, $locktime"
return 0
else
# obsolete timestamp -> remove it
rm -f $lockfile
echo_debug "lock" "check_timed_lock($1).remove_obsolete: ${lockfile#${RUNDIR}/}"
fi
fi
done
echo_debug "lock" "check_timed_lock($1).not_locked: $time"
return 1
}
# --- PATH
add_sbin2path () { # check if /sbin /usr/sbin in $PATH, otherwise add them
# retval: $PATH, $_oldpath, $_addpath
local sp
# shellcheck disable=SC2034
_oldpath="$PATH"
_addpath=""
for sp in /usr/sbin /sbin; do
if [ -d $sp ] && [ ! -h $sp ]; then
# dir exists and is not a symlink
case ":$PATH:" in
*":$sp:"*) # $sp already in $PATH
;;
*) # $sp not in $PATH, add it
_addpath="$_addpath:$sp"
;;
esac
fi
done
if [ -n "$_addpath" ]; then
export PATH="${PATH}${_addpath}"
fi
return 0
}
create_rundir () { # make sure $RUNDIR exists
[ -d $RUNDIR ] || mkdir -p $RUNDIR 2> /dev/null 1>&2
}
# --- Configuration
read_config () { # read all config files and write temporary runconf file
# $1: 0=continue/1=quit on error
# $2: 1=no trace
# rc: 0=ok/5=tlp.conf missing/6=defaults.conf missing/7=file creation error
# retval: config parameters;
# _conf_tmp: runconf
local rc=0
local tmpdir
if test_root; then
tmpdir=$RUNDIR
create_rundir
else
tmpdir=${TMPDIR:-/tmp}
fi
if _conf_tmp=$($MKTEMP -p "$tmpdir" "tlp-run.conf_tmpXXXXXX"); then
# external perl script: merge all config files to $cf
if [ "$2" = "1" ]; then
$READCONFS --outfile "$_conf_tmp" --notrace; rc=$?
else
$READCONFS --outfile "$_conf_tmp"; rc=$?
fi
# shellcheck disable=SC1090
[ $rc -eq 0 ] && . "$_conf_tmp"
else
rc=7
fi
if [ $rc -ne 0 ]; then
case $rc in
5) echo "Error: cannot read user configuration from $CONF_USR or $CONF_OLD." 1>&2 ;;
6) echo "Error: cannot read default configuration from $CONF_DEF." 1>&2 ;;
7) echo "Error: cannot write runtime configuration to $_conf_tmp." 1>&2 ;;
esac
if [ "$1" = "1" ]; then
do_exit $rc
fi
fi
return 0
}
parse_args4config () { # parse command-line arguments: everything after the
# delimiter '--' is interpreted as a config parameter
# retval: config parameters
local dflag=0 param value
echo_debug "arg" "parse_args4config: ${0##/*/} $*"
# iterate arguments
while [ $# -gt 0 ]; do
if [ $dflag -eq 1 ]; then
# delimiter was passed --> sanitize and parse argument:
# quotes stripped by the shell calling tlp
# format is PARAMETER=value
# PARAMETER allows 'A'..'Z' and '_' only, may end in a number (_BAT0)
# value allows 'A'..'Z', 'a'..'z', '0'..'9', ' ', '_', '-', ':'
# value may be an empty string
if printf "%s" "$1" | grep -E -q "$RE_PARAM"; then
param="${1%%=*}"
value="${1#*=}"
if [ -n "$param" ]; then
echo_debug "cfg" "parse_args4config.param: $param=$value"
eval "$param='$value'" 2> /dev/null
fi
fi
elif [ "$1" = "--" ]; then
# delimiter reached --> begin interpretation
dflag=1
fi
shift # next argument
done # while arguments
return 0
}
print_config () { # print runtime conf based on all config files
# external perl script: merge all config files to $cf
$READCONFS --notrace
}
save_runconf () { # copy temporary to final runconf
create_rundir
if cp --preserve=timestamps "$_conf_tmp" $CONF_RUN > /dev/null 2>&1; then
chmod 664 "$_conf_tmp" $CONF_RUN > /dev/null 2>&1
echo_debug "run" "save_runconf.ok: $_conf_tmp -> $CONF_RUN"
else
echo_debug "run" "save_runconf.failed: $_conf_tmp -> $CONF_RUN"
fi
}
# --- Kernel
kernel_version_ge () { # check if running kernel version >= $1: minimum version
[ "$1" = "$(printf "%s\n%s\n" "$1" "$(uname -r)" | sort -V | head -n 1)" ]
}
load_modules () { # load kernel module(s) -- $*: modules
local mod
# verify module loading is allowed (else explicitly disabled)
# and possible (else implicitly disabled)
[ "${TLP_LOAD_MODULES:-y}" = "y" ] && [ -e /proc/modules ] || return 0
# load modules, ignore any errors
# shellcheck disable=SC2048
for mod in $*; do
$MODPRO $mod > /dev/null 2>&1
done
return 0
}
# --- DMI
read_dmi () { # read DMI data -- $*: keywords; stdout: dmi strings
local ds key outr
outr=""
# shellcheck disable=SC2048
for key in $*; do
ds="$(read_sysf ${DMID}/$key | \
grep -E -v -i 'not available|to be filled|DMI table is broken')"
if [ -n "$outr" ]; then
[ -n "$ds" ] && outr="$outr $ds"
else
outr="$ds"
fi
done
printf '%s' "$outr"
return 0
}
# --- Power Source
get_sys_power_supply () { # get current power supply
# retval/rc: $_syspwr (0=ac, 1=battery, 2=unknown)
# _syspwr is determined by iterating all power sources:
#
# AC status: | none | 1 | 0 |
# | found | online | offline |
# +------------ +-------+---------+---------+
# battery | none found | 2 | 0 *s | *o |
# status | Charging | *r | 0 *s | *r |
# | Discharging | *d | 0 *s | *d |
# | Full | *r | 0 *s | *r |
# | Unknown | *r | 0 *s | *r |
#
# special cases:
# *o) if battery status "Unknown" was seen previously then _syspwr=1;
# else remember AC offline state
# *r) re-check battery status after 0.5 sec -> if battery status
# "Discharging" (not forced) then _syspwr=1 and stop looking;
# else _syspwr=0
# *d) if forced discharge in progress then _syspwr=0; else _syspwr=1;
# stop looking
# *s) stop looking (for other power sources)
local bs psrc psrc_name
local ac0seen=
_syspwr=
for psrc in /sys/class/power_supply/*; do
# -f $psrc/type not necessary - read_sysf() handles this
psrc_name="${psrc##*/}"
# ignore atypical power supplies and batteries
printf '%s\n' "$psrc_name" | grep -E -q "$RE_PS_IGNORE" && continue
case "$(read_sysf $psrc/type)" in
Mains|USB)
# AC detected
# skip device to ignore incorrect AC status
if [ "$TLP_PS_IGNORE" = "AC" ]; then
echo_debug "ps" "get_sys_power_supply(${psrc_name}).ac_ignored: syspwr=$_syspwr"
continue
fi
# check AC status
if [ "$(read_sysf $psrc/online)" = "1" ]; then
# AC online --> end iteration
_syspwr=0
echo_debug "ps" "get_sys_power_supply(${psrc_name}).ac_online: syspwr=$_syspwr"
break
else
# AC offline could mean battery, but quirky hardware exists
# --> remember and continue looking (for batteries)
ac0seen=1
echo_debug "ps" "get_sys_power_supply(${psrc_name}).ac_offline_remembered"
fi
;;
Battery)
# battery detected
# skip device to ignore incorrect battery status
if [ "$TLP_PS_IGNORE" = "BAT" ]; then
echo_debug "ps" "get_sys_power_supply(${psrc_name}).bat_ignored: syspwr=$_syspwr"
continue
fi
# check battery status
bs="$(read_sysf $psrc/status)"
case "$bs" in
Discharging)
if ! lockpeek_tlp tlp_discharge; then
# battery status "Discharging" means battery mode ...
_syspwr=1
echo_debug "ps" "get_sys_power_supply(${psrc_name}).bat_discharging: syspwr=$_syspwr"
else
# ... unless forced discharge is in progress, which means AC
_syspwr=0
echo_debug "ps" "get_sys_power_supply(${psrc_name}).forced_discharge: syspwr=$_syspwr"
fi
break # --> end iteration
;;
*) # assume AC mode for everything else - e.g. "Charging", "Full", "Unknown"
# --> continue looking
_syspwr=0
echo_debug "ps" "get_sys_power_supply(${psrc_name}).bat_not_discharging: bs=$bs; syspwr=$_syspwr"
;;
esac
;;
*) # unknown power source type --> ignore
;;
esac
done
if [ -z "$_syspwr" ]; then
# _syspwr result yet undecided
if [ "$ac0seen" = "1" ]; then
# AC offline remembered --> battery mode
_syspwr=1
echo_debug "ps" "get_sys_power_supply(${ac0seen##/*/}).ac_offline: syspwr=$_syspwr"
else
# we have seen neither a AC nor a battery power source --> unknown mode
_syspwr=2
echo_debug "ps" "get_sys_power_supply.none_found: syspwr=$_syspwr"
fi
fi
return $_syspwr
}
get_persist_mode () { # get persistent operation mode
# rc: 0=persistent/1=not persistent
# retval: $_persist_mode (0=ac, 1=battery, none)
local rc=1
_persist_mode="none"
if [ "$TLP_PERSISTENT_DEFAULT" = "1" ]; then
# persistent mode = configured default mode
case "$TLP_DEFAULT_MODE" in
ac|AC) _persist_mode=0; rc=0 ;;
bat|BAT) _persist_mode=1; rc=0 ;;
esac
fi
return $rc
}
get_power_mode () { # get current operation mode -- rc: 0=AC, 1=battery
# similar to get_sys_power_supply(),
# but maps unknown power source to TLP_DEFAULT_MODE or returns
# persistent mode when enabled
get_sys_power_supply
local rc=$?
if get_persist_mode; then
# persistent mode
rc=$_persist_mode
else
# non-persistent mode, use current power source
if [ $rc -eq 2 ]; then
# unknown power supply, use configured default mode
case "$TLP_DEFAULT_MODE" in
ac|AC) rc=0 ;;
bat|BAT) rc=1 ;;
*) rc=0 ;; # use AC if none or invalid mode
esac
fi
fi
return $rc
}
compare_and_save_power_state() { # compare $1 to last saved power state,
# save $1 afterwards when different
# $1: new state 0=ac, 1=battery
# rc: 0=different, 1=equal
local lp
# intercept invalid states
case $1 in
0|1) ;; # valid state
*) # invalid new state --> return "different"
echo_debug "ps" "compare_and_save_power_state($1).invalid"
return 0
;;
esac
# read saved state
lp=$(read_sysf $PWRRUNFILE)
# compare
if [ -z "$lp" ] || [ "$lp" != "$1" ]; then
# saved state is nonexistent/empty or is different --> save new state
create_rundir
write_sysf "$1" $PWRRUNFILE
echo_debug "ps" "compare_and_save_power_state($1).different: old=$lp"
return 0
else
# touch file for last run
touch $PWRRUNFILE
echo_debug "ps" "compare_and_save_power_state($1).equal"
return 1
fi
}
clear_saved_power_state() { # remove last saved power state
rm -f $PWRRUNFILE 2> /dev/null
return 0
}
check_ac_power () { # check if ac power connected -- $1: function
if ! get_sys_power_supply ; then
echo_debug "bat" "check_ac_power($1).no_ac_power"
echo "Error: $1 is possible on AC power only." 1>&2
return 1
fi
return 0
}
echo_started_mode () { # print operation mode -- $1: 0=ac mode, 1=battery mode
if [ "$1" = "0" ]; then
printf "TLP started in AC mode"
else
printf "TLP started in battery mode"
fi
if [ "$_manual_mode" != "none" ]; then
printf " (manual).\n"
else
printf " (auto).\n"
fi
return 0
}
set_manual_mode () { # set manual operation mode
# $1: 0=ac mode, 1=battery mode
# retval: $_manual_mode (0=ac, 1=battery, none=not found)
create_rundir
write_sysf "$1" $MANUALMODEFILE
_manual_mode="$1"
echo_debug "pm" "set_manual_mode($1)"
return 0
}
clear_manual_mode () { # remove manual operation mode
# retval: $_manual_mode (none)
rm -f $MANUALMODEFILE 2> /dev/null
_manual_mode="none"
echo_debug "pm" "clear_manual_mode"
return 0
}
get_manual_mode () { # get manual operation mode
# rc: 0=ok/1=not found
# retval: $_manual_mode (0=ac, 1=battery, none=not found)
local rc=1
_manual_mode="none"
if [ -f $MANUALMODEFILE ]; then
# read mode file
_manual_mode=$(read_sysf $MANUALMODEFILE)
case $_manual_mode in
0|1) rc=0 ;;
*) _manual_mode="none" ;;
esac
fi
return $rc
}
# --- Misc Checks
check_laptop_mode_tools () { # check if lmt installed -- rc: 0=not installed, 1=installed
if cmd_exists $LAPMODE; then
echo 1>&2
echo "***Warning: laptop-mode-tools detected, this may cause conflicts with TLP." 1>&2
echo " Please uninstall laptop-mode-tools." 1>&2
echo 1>&2
echo_debug "pm" "check_laptop_mode_tools: yes"
return 1
else
return 0
fi
}