annotate tpbatctl.sh @ 0:fe9ad91c9f65 default tip

Initial revision
author Guido Berhoerster <guido+tpbatctl@berhoerster.name>
date Thu, 02 Aug 2018 16:30:59 +0200
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
1 #!/bin/sh
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
2 #
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
3 # tpbatctl - get or set properties of IBM/Lenovo ThinkPad batteries
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
4 #
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
5 # Copyright (C) 2018 Guido Berhoerster <guido+tpbatctl@berhoerster.name>
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
6 #
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
7 # Permission is hereby granted, free of charge, to any person obtaining
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
8 # a copy of this software and associated documentation files (the
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
9 # "Software"), to deal in the Software without restriction, including
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
10 # without limitation the rights to use, copy, modify, merge, publish,
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
11 # distribute, sublicense, and/or sell copies of the Software, and to
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
12 # permit persons to whom the Software is furnished to do so, subject to
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
13 # the following conditions:
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
14 #
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
15 # The above copyright notice and this permission notice shall be included
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
16 # in all copies or substantial portions of the Software.
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
17 #
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
18 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
19 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
20 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
21 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
22 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
23 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
24 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
25 #
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
26
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
27 PATH=/usr/sbin:/usr/bin:/sbin:/bin
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
28 LC_NUMERIC=C
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
29
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
30 #
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
31 # usage: is_bit_set value bit_pos
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
32 # returns: 0 if true, 1 if false
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
33 #
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
34 is_bit_set () {
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
35 [ $(( $1 & (1 << $2) )) -eq $(( 1 << $2 )) ]
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
36 }
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
37
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
38 #
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
39 # usage: is_num str
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
40 # returns: 0 if true, 1 if false
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
41 #
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
42 is_num () {
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
43 case $1 in
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
44 *[!0123456789]*)
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
45 return 1
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
46 esac
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
47 }
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
48
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
49 #
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
50 # usage: is_thinkpad
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
51 # returns: 0 if true, 1 if false
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
52 #
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
53 is_thinkpad () {
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
54 case $(head -1 /sys/class/dmi/id/sys_vendor) in
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
55 [Ii][Bb][Mm]|[Ll][Ee][Nn][Oo][Vv][Oo])
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
56 ;;
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
57 *)
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
58 return 1
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
59 esac
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
60
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
61 case $(head -1 /sys/class/dmi/id/product_version) in
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
62 ThinkPad*)
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
63 ;;
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
64 *)
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
65 return 1
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
66 esac
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
67 }
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
68
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
69 #
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
70 # usage: get_acpi_base_path
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
71 # returns: 0 if successful
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
72 #
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
73 get_acpi_base_path () {
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
74 for sys_device in /sys/class/power_supply/*; do
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
75 if [ ! -d "${sys_device}" ]; then
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
76 printf 'failed to obtain ACPI base path, battery not found\n' >&2
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
77 return 1;
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
78 fi
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
79
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
80 # strip trailing underscores from path components and remove the last
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
81 # component
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
82 sed -n -e '1s|_*\.|.|g' -e '1s|\.[^.]*$||' -e '1p' \
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
83 "${sys_device}/device/path" || return 1
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
84 return 0
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
85 done
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
86 }
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
87
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
88 #
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
89 # usage: acpi_call_exec acpi_method argument
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
90 # returns: 0 if successful
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
91 #
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
92 acpi_call_exec () {
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
93 printf '%s %s\n' "$1" "$2" >/proc/acpi/call
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
94 }
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
95
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
96 #
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
97 # usage: acpi_call_read_int
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
98 # returns: 0 if successful
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
99 #
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
100 acpi_call_read_int () {
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
101 acpi_response="$(head -1 /proc/acpi/call)"
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
102 case $acpi_response in
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
103 0x*)
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
104 # strip non-hexadecimal trailing chars
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
105 acpi_response="${acpi_response#0x}"
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
106 acpi_response="0x${acpi_response%%[!0123456789abcdef]*}"
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
107 printf '%d' "${acpi_response}"
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
108 ;;
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
109 Error:*)
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
110 printf 'ACPI method call failed: %s\n' "${acpi_response}" >&2
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
111 return 1
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
112 ;;
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
113 *)
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
114 printf 'ACPI method call failed, got unexpected response: %s\n' \
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
115 "${acpi_response}" >&2
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
116 return 1
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
117 esac
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
118 }
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
119
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
120 #
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
121 # usage: probe_battery acpi_base_path
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
122 # returns: 0 if successful
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
123 #
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
124 probe_battery () {
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
125 acpi_call_exec "${1}.HKEY.BCTG" "0x1" || return $?
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
126 result="$(acpi_call_read_int)" || return $?
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
127
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
128 if is_bit_set "${result}" 31; then
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
129 printf 'failed to probe battery' >&2
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
130 return 1
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
131 fi
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
132
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
133 # capabilities
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
134 printf '%d' $(( result >> 8 ))
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
135 return 0
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
136 }
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
137
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
138 #
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
139 # usage: get_threshold acpi_method battery
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
140 # returns: 0 if successful
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
141 #
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
142 get_threshold () {
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
143 acpi_call_exec "$1" "$(printf '0x%x' $2)"
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
144 result="$(acpi_call_read_int)" || return $?
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
145
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
146 if is_bit_set $result 31; then
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
147 return 1
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
148 fi
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
149
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
150 # threshold value
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
151 printf '%d' $(( result & ((1 << 7) - 1) ))
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
152 return 0
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
153 }
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
154
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
155 #
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
156 # usage: set_threshold acpi_method battery threshold
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
157 # returns: 0 if successful
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
158 #
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
159 set_threshold () {
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
160 acpi_call_exec "$1" "$(printf '0x%x' $(( $3 | ($2 << 8) )) )"
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
161 result="$(acpi_call_read_int)" || return $?
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
162
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
163 if is_bit_set $result 31; then
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
164 return 1
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
165 fi
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
166
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
167 return 0
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
168 }
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
169
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
170 #
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
171 # usage: get_start_threshold acpi_base_path battery
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
172 # returns: 0 if successful
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
173 #
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
174 get_start_threshold () {
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
175 if ! threshold="$(get_threshold "${1}.HKEY.BCTG" $2)"; then
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
176 printf 'failed to read the start threshold\n' >&2
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
177 return 1
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
178 fi
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
179 printf '%d' "${threshold}"
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
180 return 0
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
181 }
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
182
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
183 #
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
184 # usage: get_stop_threshold acpi_base_path battery
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
185 # returns: 0 if successful
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
186 #
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
187 get_stop_threshold () {
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
188 if ! threshold="$(get_threshold "${1}.HKEY.BCSG" $2)"; then
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
189 printf 'failed to read the stop threshold\n' >&2
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
190 return 1
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
191 fi
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
192 printf '%d' "${threshold}"
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
193 return 0
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
194 }
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
195
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
196 #
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
197 # usage: set_start_threshold acpi_base_path battery threshold
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
198 # returns: 0 if successful
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
199 #
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
200 set_start_threshold () {
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
201 if ! set_threshold "${1}.HKEY.BCCS" $2 $3; then
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
202 printf 'failed to set the stop threshold\n' >&2
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
203 return 1
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
204 fi
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
205 }
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
206
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
207 #
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
208 # usage: set_stop_threshold acpi_base_path battery threshold
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
209 # returns: 0 if successful
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
210 #
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
211 set_stop_threshold () {
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
212 if ! set_threshold "${1}.HKEY.BCSS" $2 $3; then
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
213 printf 'failed to set the stop threshold\n' >&2
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
214 return 1
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
215 fi
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
216 }
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
217
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
218 #
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
219 # usage: usage
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
220 # returns: nothing
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
221 #
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
222 usage () {
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
223 printf 'usage: %s [-g property]\n' "${0##*/}"
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
224 printf ' %s [-s property=value]\n' "${0##*/}"
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
225 }
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
226
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
227 if ! is_thinkpad; then
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
228 printf 'device not supported\n' >&2
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
229 exit 1
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
230 fi
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
231
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
232 if [ ! -f /proc/acpi/call ]; then
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
233 if ! modprobe acpi_call; then
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
234 printf 'failed to load acpi_call kernel module\n' >&2
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
235 exit 1
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
236 fi
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
237 fi
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
238
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
239 if [ ! -r /proc/acpi/call ] || [ ! -w /proc/acpi/call ]; then
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
240 printf 'insufficient privilege to access /proc/acpi/call\n' >&2
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
241 exit 1
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
242 fi
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
243
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
244 selected_batteries=
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
245 get_mode=false
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
246 set_mode=false
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
247 get_start_threshold=false
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
248 get_stop_threshold=false
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
249 assigned_start_threshold=
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
250 assigned_stop_threshold=
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
251
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
252 while getopts 'g:hs:' opt; do
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
253 case $opt in
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
254 g)
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
255 if $set_mode; then
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
256 usage
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
257 exit 1
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
258 fi
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
259
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
260 get_mode=true
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
261
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
262 case $OPTARG in
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
263 start_threshold)
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
264 get_start_threshold=true
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
265 ;;
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
266 stop_threshold)
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
267 get_stop_threshold=true
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
268 ;;
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
269 *)
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
270 printf 'unknown property: %s\n' "${OPTARG}"
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
271 exit 1
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
272 ;;
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
273 esac
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
274 ;;
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
275 h)
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
276 usage
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
277 exit 0
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
278 ;;
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
279 s)
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
280 if $get_mode; then
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
281 usage
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
282 exit 1
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
283 fi
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
284
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
285 set_mode=true
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
286
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
287 case $OPTARG in
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
288 start_threshold=*)
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
289 assigned_start_threshold="${OPTARG#*=}"
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
290 if ! is_num "${assigned_start_threshold}"; then
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
291 printf 'invalid threshold: %s\n' \
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
292 "${assigned_start_threshold}" >&2
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
293 exit 1
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
294 elif [ $assigned_start_threshold -gt 100 ]; then
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
295 printf 'invalid threshold: %s\n' \
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
296 "${assigned_start_threshold}" >&2
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
297 exit 1
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
298 elif [ $assigned_start_threshold -eq 100 ]; then
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
299 assigned_start_threshold=0
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
300 fi
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
301 ;;
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
302 stop_threshold=*)
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
303 assigned_stop_threshold="${OPTARG#*=}"
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
304 if ! is_num "${assigned_stop_threshold}"; then
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
305 printf 'invalid threshold: %s\n' \
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
306 "${assigned_stop_threshold}" >&2
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
307 exit 1
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
308 elif [ $assigned_stop_threshold -gt 100 ]; then
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
309 printf 'invalid threshold: %s\n' \
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
310 "${assigned_stop_threshold}" >&2
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
311 exit 1
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
312 elif [ $assigned_stop_threshold -eq 100 ]; then
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
313 assigned_stop_threshold=0
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
314 fi
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
315 ;;
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
316 *)
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
317 printf 'unknown property: %s\n' "${OPTARG}"
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
318 exit 1
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
319 ;;
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
320 esac
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
321 ;;
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
322 *)
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
323 usage
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
324 exit 1
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
325 esac
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
326 done
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
327 # default to get mode
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
328 if ! $get_mode && ! $set_mode; then
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
329 get_mode=true
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
330 get_start_threshold=true
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
331 get_stop_threshold=true
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
332 fi
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
333
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
334 shift $(( OPTIND - 1 ))
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
335
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
336 if [ $# -gt 1 ]; then
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
337 usage
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
338 exit 1
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
339 elif [ $# -eq 1 ]; then
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
340 if ! is_num "$1" || [ $1 -lt 1 ] || [ $1 -gt 2 ]; then
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
341 printf 'invalid battery number: %s\n' "$1"
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
342 exit 1
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
343 fi
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
344 selected_batteries=$1
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
345 if [ ! -d "/sys/class/power_supply/BAT$(( selected_batteries - 1))" ]; then
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
346 printf 'battery %d does not exist\n' $selected_batteries
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
347 exit 1
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
348 fi
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
349 else
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
350 # no explicitly selected battery means all batteries
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
351 for battery in 1 2; do
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
352 if [ -d "/sys/class/power_supply/BAT$(( battery - 1))" ]; then
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
353 selected_batteries="${selected_batteries} ${battery}"
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
354 fi
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
355 done
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
356 fi
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
357
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
358 if ! acpi_base_path="$(get_acpi_base_path)"; then
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
359 printf 'failed to obtain ACPI base path\n' >&2
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
360 exit 1
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
361 fi
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
362
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
363 if ! capabilities=$(probe_battery "${acpi_base_path}"); then
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
364 printf 'failed to obtain battery capabilities\n' >&2
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
365 exit 1
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
366 fi
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
367
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
368 if $get_mode; then
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
369 for battery in $selected_batteries; do
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
370 printf 'Battery %d\n' $battery
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
371 if $get_start_threshold; then
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
372 start_threshold="$(get_start_threshold \
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
373 $acpi_base_path $battery)" || exit $?
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
374 printf ' Charge Start Threshold: %d %%\n' $start_threshold
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
375 fi
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
376
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
377 if $get_stop_threshold; then
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
378 stop_threshold="$(get_stop_threshold $acpi_base_path $battery)" || \
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
379 exit $?
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
380 printf ' Charge Stop Threshold: %d %%\n' $stop_threshold
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
381 fi
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
382 done
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
383 else
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
384 for battery in $selected_batteries; do
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
385 if ! is_bit_set $capabilities 0; then
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
386 printf 'setting thresholds is not supported\n' >&2
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
387 exit 1
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
388 fi
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
389
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
390 start_threshold="$(get_start_threshold $acpi_base_path $battery)" || \
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
391 exit $?
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
392 stop_threshold="$(get_stop_threshold $acpi_base_path $battery)" || \
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
393 exit $?
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
394
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
395 if [ -n "${assigned_start_threshold}" ] && \
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
396 [ -n "${assigned_stop_threshold}" ]; then
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
397 if [ $assigned_start_threshold -gt $assigned_stop_threshold ]; then
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
398 printf 'the start threshold must be smaller than the stop threshold\n' >&2
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
399 exit 1
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
400 fi
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
401
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
402 if [ $assigned_start_threshold -gt $stop_threshold ]; then
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
403 # the new start threshold is greather then the old stop
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
404 # threshold, thus change the stop threshold first so that the
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
405 # start threshold always remains smaller than the stop
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
406 # threshold
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
407 set_stop_threshold $acpi_base_path $battery \
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
408 $assigned_stop_threshold || exit $?
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
409 set_start_threshold $acpi_base_path $battery \
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
410 $assigned_start_threshold || exit $?
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
411 else
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
412 # otherwise the new stop threshold is smaller than or equal to
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
413 # the old start threshold, thus change the start threshold
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
414 # first
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
415 set_start_threshold $acpi_base_path $battery \
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
416 $assigned_start_threshold || exit $?
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
417 set_stop_threshold $acpi_base_path $battery \
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
418 $assigned_stop_threshold || exit $?
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
419 fi
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
420 elif [ -n "${assigned_start_threshold}" ]; then
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
421 if [ $assigned_start_threshold -gt $stop_threshold ]; then
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
422 printf 'the start threshold must be smaller than the stop threshold\n' >&2
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
423 exit 1
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
424 fi
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
425
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
426 set_start_threshold $acpi_base_path $battery \
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
427 $assigned_start_threshold || exit $?
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
428 else
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
429 if [ $assigned_stop_threshold -lt $start_threshold ]; then
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
430 printf 'the start threshold must be smaller than the stop threshold\n' >&2
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
431 exit 1
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
432 fi
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
433
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
434 set_stop_threshold $acpi_base_path $battery \
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
435 $assigned_stop_threshold || exit $?
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
436 fi
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
437 done
fe9ad91c9f65 Initial revision
Guido Berhoerster <guido+tpbatctl@berhoerster.name>
parents:
diff changeset
438 fi