-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
battery_openbsd.go
282 lines (246 loc) · 6.74 KB
/
battery_openbsd.go
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
// battery
// Copyright (C) 2016-2017,2019,2023 Karol 'Kenji Takahashi' Woźniak
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package battery
import (
"bytes"
"fmt"
"strings"
"unsafe"
"golang.org/x/sys/unix"
)
func sysctl(mib []int32, out unsafe.Pointer, n uintptr) unix.Errno {
_, _, e := unix.Syscall6(
unix.SYS___SYSCTL,
uintptr(unsafe.Pointer(&mib[0])),
uintptr(len(mib)),
uintptr(out),
uintptr(unsafe.Pointer(&n)),
uintptr(unsafe.Pointer(nil)),
0,
)
return e
}
var errValueNotFound = fmt.Errorf("Value not found")
func setErr(err *error, newErr error) {
if *err == errValueNotFound {
*err = newErr
}
}
var sensorW = [4]int32{
2, // SENSOR_VOLTS_DC (uV)
5, // SENSOR_WATTS (uW)
7, // SENSOR_WATTHOUR (uWh)
10, // SENSOR_INTEGER
}
const (
sensorA = 6 // SENSOR_AMPS (uA)
sensorAH = 8 // SENSOR_AMPHOUR (uAh)
)
type sensorStatus int32
const (
unspecified sensorStatus = iota
ok
warning
critical
unknown
)
type sensor struct {
desc [32]byte
tv [16]byte // struct timeval
value int64
typ [4]byte // enum sensor_type
status sensorStatus
numt int32
flags int32
}
func (s *sensor) readValue(div float64) (float64, error) {
if s.status == unknown {
return 0, fmt.Errorf("Unknown value received")
}
return float64(s.value) / div, nil
}
func (s *sensor) handleA(factor float64, field *float64, fieldErr *error) {
*field, *fieldErr = s.readValue(1000)
if *fieldErr == nil {
*field *= factor
}
}
func (s *sensor) handleAH(factor float64, factorErr error, field *float64, fieldErr *error) {
if factorErr != nil {
*fieldErr = factorErr
return
}
s.handleA(factor, field, fieldErr)
}
type sensordev struct {
num int32
xname [16]byte
maxnumt [23]int32
sensors_count int32
}
func (sd *sensordev) get() (*Battery, error) {
var battery Battery
err := ErrPartial{
Design: errValueNotFound,
Full: errValueNotFound,
Current: errValueNotFound,
ChargeRate: errValueNotFound,
State: errValueNotFound,
Voltage: errValueNotFound,
DesignVoltage: errValueNotFound,
}
mib := []int32{unix.CTL_HW, 11, sd.num, 0, 0}
var s sensor
iter := func(maxnumtIdx int32, cb func(string)) {
mib[3] = maxnumtIdx
for i := int32(0); i < sd.maxnumt[maxnumtIdx]; i++ {
mib[4] = i
if errno := sysctl(mib, unsafe.Pointer(&s), unsafe.Sizeof(s)); errno != 0 {
setErr(&err.Design, errno)
setErr(&err.Full, errno)
setErr(&err.Current, errno)
setErr(&err.ChargeRate, errno)
setErr(&err.Voltage, errno)
setErr(&err.DesignVoltage, errno)
setErr(&err.State, errno)
}
// Convert 0-terminated C-string to a Go string
cb(string(s.desc[:bytes.IndexByte(s.desc[:], 0)]))
}
}
for _, w := range sensorW {
mib[3] = w
iter(w, func(desc string) {
if strings.HasPrefix(desc, "battery ") {
battery.State.specific, err.State = desc, nil
switch desc[8:] {
case "unknown":
battery.State.Raw = Unknown
case "full":
battery.State.Raw = Full
case "charging":
battery.State.Raw = Charging
case "discharging":
battery.State.Raw = Discharging
case "idle":
battery.State.Raw = Idle
case "critical":
battery.State.Raw = Empty
default:
battery.State.Raw = Undefined
}
return
}
switch desc {
case "rate":
battery.ChargeRate, err.ChargeRate = s.readValue(1000)
case "design capacity":
battery.Design, err.Design = s.readValue(1000)
case "last full capacity":
battery.Full, err.Full = s.readValue(1000)
case "remaining capacity":
battery.Current, err.Current = s.readValue(1000)
case "current voltage":
battery.Voltage, err.Voltage = s.readValue(1000_000)
case "voltage":
battery.DesignVoltage, err.DesignVoltage = s.readValue(1000_000)
}
})
}
if err.DesignVoltage != nil && err.Voltage == nil {
battery.DesignVoltage, err.DesignVoltage = battery.Voltage, nil
}
if err.ChargeRate == errValueNotFound {
if err.Voltage == nil {
iter(sensorA, func(desc string) {
if desc == "rate" {
s.handleA(battery.Voltage, &battery.ChargeRate, &err.ChargeRate)
}
})
} else {
err.ChargeRate = err.Voltage
}
}
if err.Design == errValueNotFound || err.Full == errValueNotFound || err.Current == errValueNotFound {
iter(sensorAH, func(desc string) {
switch desc {
case "design capacity":
s.handleAH(battery.DesignVoltage, err.DesignVoltage, &battery.Design, &err.Design)
case "last full capacity":
s.handleAH(battery.Voltage, err.Voltage, &battery.Full, &err.Full)
case "remaining capacity":
s.handleAH(battery.Voltage, err.Voltage, &battery.Current, &err.Current)
}
})
}
return &battery, err
}
func getBatteryAtMIBIndex(i int32) (*Battery, int32, error) {
mib := []int32{
unix.CTL_HW,
11, // HW_SENSORS
0,
}
var sd sensordev
for ; ; i++ {
mib[2] = i
errno := sysctl(mib, unsafe.Pointer(&sd), unsafe.Sizeof(sd))
if errno == unix.ENOENT {
break
}
if errno != 0 {
continue
}
if bytes.HasPrefix(sd.xname[:], []byte("acpibat")) {
battery, err := sd.get()
return battery, i + 1, err
}
}
return nil, i, nil
}
func systemGet(idx int) (*Battery, error) {
var i int32
for idxCurr := 0; ; idxCurr++ {
battery, iNext, err := getBatteryAtMIBIndex(i)
// If this is the index we look for, grab it.
// Otherwise just move on, regardless of errors etc.
if idxCurr == idx {
return battery, err
}
i = iNext
}
return nil, ErrNotFound
}
func systemGetAll() ([]*Battery, error) {
var batteries []*Battery
var errors Errors
var i int32
for {
battery, iNext, err := getBatteryAtMIBIndex(i)
if battery == nil && err == nil { // no more batteries
break
}
batteries = append(batteries, battery)
errors = append(errors, err)
i = iNext
}
return batteries, errors
}