-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
battery_netbsd.go
221 lines (190 loc) · 5.34 KB
/
battery_netbsd.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
// battery
// Copyright (C) 2016-2017,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 (
"errors"
"fmt"
"math"
"sort"
"strings"
"unsafe"
"golang.org/x/sys/unix"
plist "howett.net/plist"
)
type plistref struct {
pref_plist unsafe.Pointer
pref_len uint64
}
type values struct {
Description string `plist:"description"`
CurValue int `plist:"cur-value"`
MaxValue int `plist:"max-value"`
State string `plist:"state"`
Type string `plist:"type"`
}
type prop []values
type props map[string]prop
func readBytes(ptr unsafe.Pointer, length uint64) []byte {
buf := make([]byte, length-1)
var i uint64
for ; i < length-1; i++ {
buf[i] = *(*byte)(unsafe.Pointer(uintptr(ptr) + uintptr(i)))
}
return buf
}
func readProps() (props, error) {
fd, err := unix.Open("/dev/sysmon", unix.O_RDONLY, 0777)
if err != nil {
return nil, err
}
defer unix.Close(fd)
var retptr plistref
if err = ioctl(fd, 0, 'E', unsafe.Sizeof(retptr), unsafe.Pointer(&retptr)); err != nil {
return nil, err
}
bytes := readBytes(retptr.pref_plist, retptr.pref_len)
var props props
if _, err = plist.Unmarshal(bytes, &props); err != nil {
return nil, err
}
return props, nil
}
func handleValue(val values, div float64, res *float64, amps *[]string) error {
if val.State == "invalid" || val.State == "unknown" {
return errors.New("Unknown value received")
}
*res = float64(val.CurValue) / div
if amps != nil && strings.HasPrefix(val.Type, "Amp") {
*amps = append(*amps, val.Description)
}
return nil
}
func deriveState(cr1, cr2 error, current float64, max int) (AgnosticState, error) {
if cr1 == nil && cr2 != nil {
return Charging, nil
}
if cr1 != nil && cr2 == nil {
return Discharging, nil
}
if cr1 != nil && cr2 != nil && current == float64(max)/1000 {
return Full, nil
}
return Unknown, errors.New("Contradicting values received")
}
func handleVoltage(amps []string, b *Battery, e *ErrPartial) {
if e.DesignVoltage != nil && e.Voltage == nil {
b.DesignVoltage, e.DesignVoltage = b.Voltage, nil
}
for _, val := range amps {
switch val {
case "design cap":
if e.DesignVoltage == nil {
b.Design *= b.DesignVoltage
} else {
e.Design = e.DesignVoltage
}
case "last full cap":
if e.Voltage == nil {
b.Full *= b.Voltage
} else {
e.Full = e.Voltage
}
case "charge":
if e.Voltage == nil {
b.Current *= b.Voltage
} else {
e.Current = e.Voltage
}
case "charge rate", "discharge rate":
if e.Voltage == nil {
b.ChargeRate *= b.Voltage
} else {
e.ChargeRate = e.Voltage
}
}
}
}
func sortFilterProps(props props) []string {
var keys []string
for key := range props {
if key[:7] != "acpibat" {
continue
}
keys = append(keys, key)
}
sort.Strings(keys)
return keys
}
func convertBattery(prop prop) (*Battery, error) {
b := &Battery{}
e := ErrPartial{}
amps := []string{}
var cr1, cr2 error
var maxCharge int
for _, val := range prop {
switch val.Description {
case "voltage":
e.Voltage = handleValue(val, 1000000, &b.Voltage, nil)
case "design voltage":
e.DesignVoltage = handleValue(val, 1000000, &b.DesignVoltage, nil)
case "design cap":
e.Design = handleValue(val, 1000, &b.Design, &s)
case "last full cap":
e.Full = handleValue(val, 1000, &b.Full, &s)
case "charge":
e.Current = handleValue(val, 1000, &b.Current, &s)
maxCharge = val.MaxValue
case "charge rate":
cr1 = handleValue(val, 1000, &b.ChargeRate, &s)
case "discharge rate":
cr2 = handleValue(val, 1000, &b.ChargeRate, &s)
b.ChargeRate = math.Abs(b.ChargeRate)
}
}
b.State.Raw, e.State = deriveState(cr1, cr2, b.Current, maxCharge)
b.State.specific = fmt.Sprintf("cr1: %v, cr2: %v")
handleVoltage(amps, b, &e)
return b, e
}
func systemGet(idx int) (*Battery, error) {
props, err := readProps()
if err != nil {
return nil, err
}
keys := sortFilterProps(props)
if idx >= len(keys) {
return nil, ErrNotFound
}
return convertBattery(props[keys[idx]])
}
func systemGetAll() ([]*Battery, error) {
props, err := readProps()
if err != nil {
return nil, err
}
keys := sortFilterProps(props)
batteries := make([]*Battery, len(keys))
errors := make(Errors, len(keys))
for i, key := range keys {
batteries[i], errors[i] = convertBattery(props[key])
}
return batteries, errors
}