forked from k-sone/ipmigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsel.go
298 lines (258 loc) · 7.6 KB
/
sel.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package ipmigo
import (
"encoding/binary"
"encoding/hex"
"fmt"
)
const (
selFirstID uint16 = 0x0000
selLastID uint16 = 0xffff
selRecordSize = 16
)
// Sensor Event Log Record Type
type SELType uint8
func (t SELType) IsTimestampedOEM() bool { return t >= 0xc0 && t <= 0xdf }
func (t SELType) IsNonTimestampedOEM() bool { return t >= 0xe0 && t <= 0xff }
// Sensor Event Log Record
type SELRecord interface {
// Returns record type
Type() SELType
// Returns record id
ID() uint16
// Returns bytes of the record key and body
Data() []byte
}
// SEL Event Record (Section 32.1)
type SELEventRecord struct {
data []byte
RecordID uint16
RecordType SELType
Timestamp Timestamp
GeneratorID uint16
EvMRev uint8
SensorType SensorType
SensorNumber uint8
EventType EventType
EventDir uint8
EventData1 uint8 // (Table 29-6)
EventData2 uint8 // (Table 29-6)
EventData3 uint8 // (Table 29-6)
}
func (r *SELEventRecord) Type() SELType { return r.RecordType }
func (r *SELEventRecord) ID() uint16 { return r.RecordID }
func (r *SELEventRecord) Data() []byte { return r.data }
func (r *SELEventRecord) Unmarshal(buf []byte) ([]byte, error) {
if l := len(buf); l < selRecordSize {
return nil, &MessageError{
Message: fmt.Sprintf("Invalid SELEventRecord size : %d/%d", l, selRecordSize),
Detail: hex.EncodeToString(buf),
}
}
r.data = buf[:selRecordSize]
r.RecordID = binary.LittleEndian.Uint16(buf[0:2])
r.RecordType = SELType(buf[2])
r.Timestamp.Value = binary.LittleEndian.Uint32(buf[3:7])
r.GeneratorID = binary.LittleEndian.Uint16(buf[7:9])
r.EvMRev = buf[9]
r.SensorType = SensorType(buf[10])
r.SensorNumber = buf[11]
r.EventType = EventType(buf[12] & 0x7f)
r.EventDir = buf[12] & 0x80 >> 7
r.EventData1 = buf[13]
r.EventData2 = buf[14]
r.EventData3 = buf[15]
return buf[selRecordSize:], nil
}
// Returns `true'` if it is assertion event.
func (r *SELEventRecord) IsAssertionEvent() bool { return r.EventDir == 0 }
// Returns trigger reading of threshold-base sensor.
func (r *SELEventRecord) GetEventTriggerReading() (uint8, bool) {
if r.EventType.IsThreshold() && r.EventData1&0xc0 == 0x40 && r.EventData2 != 0xff {
return r.EventData2, true
}
return 0, false
}
// Returns trigger threshold value of threshold-base sensor.
func (r *SELEventRecord) GetEventTriggerThreshold() (uint8, bool) {
if r.EventType.IsThreshold() && r.EventData1&0x30 == 0x10 && r.EventData3 != 0xff {
return r.EventData3, true
}
return 0, false
}
// Returns event description.
func (r *SELEventRecord) Description() string {
var f func() (string, bool)
switch t := r.EventType; {
case t.IsGeneric() || t.IsThreshold():
f = func() (string, bool) {
offset := r.EventData1 & 0x0f
desc, ok := sensorGenericEventDesc[uint32(r.EventType)<<8|uint32(offset)]
return desc, ok
}
case t.IsSensorSpecific():
f = func() (string, bool) {
d2 := uint8(0xff)
if r.EventData1&0xc0 != 0 {
d2 = r.EventData2
}
d3 := uint8(0xff)
if r.EventData1&0x30 != 0 {
d3 = r.EventData3
}
offset := r.EventData1 & 0x0f
for {
// First, try to get a more detailed definition
desc, ok := sensorSpecificEventDesc[uint32(r.SensorType)<<24|uint32(offset)<<16|uint32(d2)<<8|uint32(d3)]
if !ok && (d2 != 0xff || d3 != 0xff) {
// If not found, get a general definition
d2, d3 = 0xff, 0xff
continue
}
return desc, ok
}
}
case t.IsOEM():
f = func() (string, bool) {
return fmt.Sprintf("OEM Event: Type=0x%02x, Data1=0x%02x, Data2=0x%02x, Data3=0x%02x",
r.EventType, r.EventData1, r.EventData2, r.EventData3), true
}
default:
f = func() (string, bool) { return "", false }
}
if desc, ok := f(); ok {
return desc
} else {
return fmt.Sprintf("Event: Type=0x%02x, Data1=0x%02x, Data2=0x%02x, Data3=0x%02x",
r.EventType, r.EventData1, r.EventData2, r.EventData3)
}
}
// Timestamped OEM SEL record (Section 32.2)
type SELTimestampedOEMRecord struct {
data []byte
RecordID uint16
RecordType SELType
Timestamp Timestamp
ManufacturerID uint32
OEMDefined []byte
}
func (r *SELTimestampedOEMRecord) Type() SELType { return r.RecordType }
func (r *SELTimestampedOEMRecord) ID() uint16 { return r.RecordID }
func (r *SELTimestampedOEMRecord) Data() []byte { return r.data }
func (r *SELTimestampedOEMRecord) Unmarshal(buf []byte) ([]byte, error) {
if l := len(buf); l < selRecordSize {
return nil, &MessageError{
Message: fmt.Sprintf("Invalid SELTimestampedOEMRecord size : %d/%d", l, selRecordSize),
Detail: hex.EncodeToString(buf),
}
}
r.data = buf[:selRecordSize]
r.RecordID = binary.LittleEndian.Uint16(buf[0:2])
r.RecordType = SELType(buf[2])
r.Timestamp.Value = binary.LittleEndian.Uint32(buf[3:7])
r.ManufacturerID = uint32(buf[7]) | uint32(buf[8])<<8 | uint32(buf[9])<<16
r.OEMDefined = buf[10:selRecordSize]
return buf[selRecordSize:], nil
}
// Non-Timestamped OEM SEL record (Section 32.3)
type SELNonTimestampedOEMRecord struct {
data []byte
RecordID uint16
RecordType SELType
OEM []byte
}
func (r *SELNonTimestampedOEMRecord) Type() SELType { return r.RecordType }
func (r *SELNonTimestampedOEMRecord) ID() uint16 { return r.RecordID }
func (r *SELNonTimestampedOEMRecord) Data() []byte { return r.data }
func (r *SELNonTimestampedOEMRecord) Unmarshal(buf []byte) ([]byte, error) {
if l := len(buf); l < selRecordSize {
return nil, &MessageError{
Message: fmt.Sprintf("Invalid SELNonTimestampedOEMRecord size : %d/%d", l, selRecordSize),
Detail: hex.EncodeToString(buf),
}
}
r.data = buf[:selRecordSize]
r.RecordID = binary.LittleEndian.Uint16(buf[0:2])
r.RecordType = SELType(buf[2])
r.OEM = buf[3:selRecordSize]
return buf[selRecordSize:], nil
}
func selGetRecord(c *Client, reservation, id uint16) (record SELRecord, nextID uint16, err error) {
nextID = selLastID
gse := &GetSELEntryCommand{
ReservationID: reservation,
RecordID: id,
RecordOffset: 0x00,
ReadBytes: 0xff,
}
if err = c.Execute(gse); err != nil {
return
}
if l := len(gse.RecordData); l < 3 {
err = &MessageError{Message: fmt.Sprintf("Invalid SELRecord size : %d", l)}
return
}
if t := SELType(gse.RecordData[2]); t.IsTimestampedOEM() {
r := &SELTimestampedOEMRecord{}
if _, err = r.Unmarshal(gse.RecordData); err != nil {
return
}
record = r
} else if t.IsNonTimestampedOEM() {
r := &SELNonTimestampedOEMRecord{}
if _, err = r.Unmarshal(gse.RecordData); err != nil {
return
}
record = r
} else {
r := &SELEventRecord{}
if _, err = r.Unmarshal(gse.RecordData); err != nil {
return
}
record = r
}
return record, gse.NextRecordID, nil
}
func SELGetEntries(c *Client, offset, num int) (records []SELRecord, total int, err error) {
gsi := &GetSELInfoCommand{}
if err = c.Execute(gsi); err != nil {
return
}
if v := gsi.SELVersion; v != 0x51 && v != 0x02 {
return nil, 0, &MessageError{
Message: fmt.Sprintf("Unknown SEL version : %d", v),
}
}
total = int(gsi.Entries)
if total == 0 || num <= 0 || offset < 0 || offset >= total {
return
}
if n := total - offset; num > n {
num = n
}
roffset := selFirstID
if offset > 0 {
// get first record
r, n, e := selGetRecord(c, 0x00, selFirstID)
if e != nil {
return
}
delta := n - r.ID()
roffset = delta*uint16(offset) + r.ID()
}
records = make([]SELRecord, 0, num)
rsc := &ReserveSELCommand{}
if err = c.Execute(rsc); err != nil {
return
}
for n, id := 0, roffset; n < num && id != selLastID; n++ {
var r SELRecord
if r, id, err = selGetRecord(c, rsc.ReservationID, id); err != nil {
return
}
records = append(records, r)
}
if l := len(records); l > num {
records = records[l-num:]
}
return
}