-
Notifications
You must be signed in to change notification settings - Fork 24
/
batpro
executable file
·160 lines (141 loc) · 2.73 KB
/
batpro
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
#!/bin/bash
# Copyright (C) 2019 Ayman Bagabas (ayman.bagabas@gmail.com), Evgeny Kuznetsov (evgeny@kuznetsov.md)
# Licensed under GNU GPL v2.0
# This script is designed to control battery protection feature on Huawei MateBook 13.
_usage() {
cat <<EOF
USAGE:
$0 [help|status|off|home|office|travel]
$0 custom [1-100] [1-100]
EOF
for i in "${!options[@]}"; do
echo " $i - ${options[$i]}";
done
}
_error() {
echo "wrong input!
"
_usage
exit 1
}
wait_read() {
for i in {1..10000}; do
if ! [ $((0x$(inb --hex $1) & 0x01)) -eq 0 ]; then
break
fi
sleep 0.01
done
! [ $i -eq 10000 ]
}
wait_write() {
for i in {1..10000}; do
if [ $((0x$(inb --hex $1) & 0x02)) -eq 0 ]; then
break
fi
sleep 0.01
done
! [ $i -eq 10000 ]
}
wait_read_ec() {
wait_read 0x66
return $?
}
wait_write_ec() {
wait_write 0x66
return $?
}
read_ec() {
wait_write_ec && outb 0x66 0x80
wait_write_ec && outb 0x62 $1
wait_read_ec && res=$(inb 0x62)
echo "REG[$1]==$(printf '0x%x' $res)" >&2
return "$res"
}
write_ec() {
read_ec $1
echo "REG[$1]:=$2" >&2
wait_write_ec && outb 0x66 0x81
wait_write_ec && outb 0x62 $1
wait_write_ec && outb 0x62 $2
read_ec $1
}
thresh_set() {
write_ec 0xe4 "$(printf '0x%x' "$1")"
write_ec 0xe5 "$(printf '0x%x' "$2")"
}
bp_off() {
thresh_set 0 100
}
# Check root
if [ $(id -u) -ne 0 ]; then
echo "This script must be run as root"
exit 1
fi
# Check necessary packages
if ! [ -x "$(command -v inb)" -a -x "$(command -v outb)" ]; then
echo "ioport was not found!"
echo "Please install ioport"
exit 1
fi
case "$1" in
"help")
cat <<EOF
When switched on, battery protection will allow charging
only if battery level is below minimum,
and only until maximum level is reached.
Huawei provides 3 presets for levels, and you may also specify your own.
EOF
_usage
cat <<EOF
Options are:
help - this help information
status - display current battery protection status
off - switch off battery protection
home - set minimum threshold to 40, maximum to 70
office - set minimum threshold to 70, maximum to 90
travel - set minimum threshold to 95, maximum to 100
custom - set custom thresholds: min max
EOF
;;
"status")
read_ec 0xe4
vmin="$?"
read_ec 0xe5
vmax="$?"
if [ "$vmin" -eq 0 ]; then
echo "battery protection is off"
else
echo "battery protection is on"
echo "thresholds:"
echo "minimum" $vmin "%"
echo "maximum" $vmax "%"
fi
;;
"off")
bp_off
;;
"home")
bp_off
thresh_set 40 70
;;
"office")
bp_off
thresh_set 70 90
;;
"travel")
bp_off
thresh_set 95 100
;;
"custom")
if [ "$2" -ge 1 -a "$2" -le 100 -a "$3" -ge 1 -a "$3" -le 100 ]; then
bp_off
thresh_set "$2" "$3"
else
_error
fi
;;
*)
_usage
;;
esac
exit 0