forked from bougou/go-ipmi
/
types_sel.go
308 lines (248 loc) · 7.9 KB
/
types_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
299
300
301
302
303
304
305
306
307
308
package ipmi
import (
"bytes"
"fmt"
"time"
"github.com/olekukonko/tablewriter"
)
// 32. SEL Record Formats
type SEL struct {
// SEL Record IDs 0000h and FFFFh are reserved for functional use and are not legal ID values.
// Record IDs are handles. They are not required to be sequential or consecutive.
// Applications should not assume that SEL Record IDs will follow any particular numeric ordering.
RecordID uint16
RecordType SELRecordType
Standard *SELStandard
OEMTimestamped *SELOEMTimestamped
OEMNonTimestamped *SELOEMNonTimestamped
}
func (sel *SEL) Pack() []byte {
msg := make([]byte, 16)
packUint16L(sel.RecordID, msg, 0)
packUint8(uint8(sel.RecordType), msg, 2)
switch sel.RecordType.Range() {
case SELRecordTypeRangeStandard:
if sel.Standard != nil {
msg = append(msg[0:3], sel.Standard.Pack()...)
}
case SELRecordTypeRangeTimestampedOEM:
if sel.OEMTimestamped != nil {
msg = append(msg[0:3], sel.OEMTimestamped.Pack()...)
}
case SELRecordTypeRangeNonTimestampedOEM:
if sel.OEMNonTimestamped != nil {
msg = append(msg[0:3], sel.OEMNonTimestamped.Pack()...)
}
}
return msg
}
func ParseSEL(msg []byte) (*SEL, error) {
if len(msg) != 16 {
return nil, fmt.Errorf("SEL Record msg should be 16 bytes in length")
}
recordID, _, _ := unpackUint16L(msg, 0)
recordType, _, _ := unpackUint8(msg, 2)
sel := &SEL{
RecordID: recordID,
RecordType: SELRecordType(recordType),
}
recordTypeRange := sel.RecordType.Range()
switch recordTypeRange {
case SELRecordTypeRangeStandard:
if err := parseSELDefault(msg, sel); err != nil {
return nil, fmt.Errorf("parseSELDefault failed, err: %s", err)
}
case SELRecordTypeRangeTimestampedOEM:
if err := parseSELOEMTimestamped(msg, sel); err != nil {
return nil, fmt.Errorf("parseSELOEMTimestamped failed, err: %s", err)
}
case SELRecordTypeRangeNonTimestampedOEM:
if err := parseSELOEMNonTimestamped(msg, sel); err != nil {
return nil, fmt.Errorf("parseSELOEMNonTimestamped failed, err: %s", err)
}
}
return sel, nil
}
// 32.2 OEM SEL Record - Type C0h-DFh
type SELOEMTimestamped struct {
Timestamp time.Time // Time when event was logged. uint32 LS byte first.
ManufacturerID uint32 // only 3 bytes
OEMDefined [6]byte
}
func (oemTimestamped *SELOEMTimestamped) Pack() []byte {
var msg = make([]byte, 13)
packUint32L(uint32(oemTimestamped.Timestamp.Unix()), msg, 0)
packUint24L(oemTimestamped.ManufacturerID, msg, 4)
packBytes(oemTimestamped.OEMDefined[:], msg, 7)
return msg
}
type SELOEMNonTimestamped struct {
OEM [13]byte
}
func (oemNonTimestamped *SELOEMNonTimestamped) Pack() []byte {
return oemNonTimestamped.OEM[:]
}
// 32.1 SEL Standard Event Records
type SELStandard struct {
Timestamp time.Time // Time when event was logged. uint32 LS byte first.
GeneratorID GeneratorID // RqSA & LUN if event was generated from IPMB. Software ID if event was generatedfrom system software.
EvMRev uint8 // Event Message Revision (format version)
SensorType SensorType // Sensor Type Code for sensor that generated the event
SensorNumber SensorNumber // Number of sensor that generated the event
EventDir EventDir // Event Direction. [7] -0b = Assertion event. 1b = Deassertion event.
EventReadingType EventReadingType // Type of trigger for the event. [6:0] - Event Type Code
// 29.7 Event Data Field Formats
//
// The sensor class determines the corresponding Event Data format.
// The sensor class can be extracted from EventReadingType.
EventData EventData
}
func (standard *SELStandard) Pack() []byte {
var msg = make([]byte, 13)
packUint32L(uint32(standard.Timestamp.Unix()), msg, 0)
packUint16L(uint16(standard.GeneratorID), msg, 4)
packUint8(standard.EvMRev, msg, 6)
packUint8(uint8(standard.SensorType), msg, 7)
packUint8(uint8(standard.SensorNumber), msg, 8)
var eventType = uint8(standard.EventReadingType)
if standard.EventDir {
eventType = eventType | 0x80
}
packUint8(eventType, msg, 9)
packUint8(standard.EventData.EventData1, msg, 10)
packUint8(standard.EventData.EventData2, msg, 11)
packUint8(standard.EventData.EventData3, msg, 12)
return msg
}
// EventString return string description of the event.
func (sel *SELStandard) EventString() string {
return sel.EventReadingType.EventString(sel.SensorType, sel.SensorNumber, sel.EventData)
}
func (sel *SELStandard) EventSeverity() EventSeverity {
return sel.EventReadingType.EventSeverity(sel.SensorType, sel.SensorNumber, sel.EventData, sel.EventDir)
}
func parseSELDefault(msg []byte, sel *SEL) error {
if len(msg) < 16 {
return ErrUnpackedDataTooShort
}
var s = &SELStandard{}
sel.Standard = s
ts, _, _ := unpackUint32L(msg, 3)
s.Timestamp = parseTimestamp(ts)
gid, _, _ := unpackUint16L(msg, 7)
s.GeneratorID = GeneratorID(gid)
s.EvMRev, _, _ = unpackUint8(msg, 9)
sensorType, _, _ := unpackUint8(msg, 10)
s.SensorType = SensorType(sensorType)
sensorNumber, _, _ := unpackUint8(msg, 11)
s.SensorNumber = SensorNumber(sensorNumber)
b, _, _ := unpackUint8(msg, 12)
s.EventDir = EventDir(isBit7Set(b))
s.EventReadingType = EventReadingType(b & 0x7f) // clear bit 7
s.EventData.EventData1, _, _ = unpackUint8(msg, 13)
s.EventData.EventData2, _, _ = unpackUint8(msg, 14)
s.EventData.EventData3, _, _ = unpackUint8(msg, 15)
return nil
}
func parseSELOEMTimestamped(msg []byte, sel *SEL) error {
if len(msg) < 16 {
return ErrUnpackedDataTooShort
}
var s = &SELOEMTimestamped{}
sel.OEMTimestamped = s
ts, _, _ := unpackUint32L(msg, 3)
s.Timestamp = parseTimestamp(ts)
id, _, _ := unpackUint24L(msg, 7)
s.ManufacturerID = id
s.OEMDefined = [6]byte{}
b, _, _ := unpackBytes(msg, 10, 6)
for i := 0; i < 6; i++ {
s.OEMDefined[i] = b[i]
}
return nil
}
func parseSELOEMNonTimestamped(msg []byte, sel *SEL) error {
if len(msg) < 16 {
return ErrUnpackedDataTooShort
}
var s = &SELOEMNonTimestamped{}
sel.OEMNonTimestamped = s
s.OEM = [13]byte{}
b, _, _ := unpackBytes(msg, 3, 13)
for i := 0; i < 6; i++ {
s.OEM[i] = b[i]
}
return nil
}
// FormatSELs print sel records in table format.
// The second sdrMap is optional. If the sdrMap is not nil,
// it will also print sensor number, entity id and instance, and asserted discrete states.
// sdrMap can be get by client GetSDRsMap method.
func FormatSELs(records []*SEL, sdrMap SDRMapBySensorNumber) string {
var elistMode bool
if sdrMap != nil {
elistMode = true
}
var buf = new(bytes.Buffer)
table := tablewriter.NewWriter(buf)
table.SetAutoWrapText(false)
headers := []string{
"ID",
"RecordType",
"EvmRev",
"Timestamp",
"GID",
"SensorNumber",
"SensorTypeCode",
"SensorType",
"EventReadingType",
"EventReadingType",
"EventDescription",
"EventDir",
"EventSeverity",
"EventData",
}
if elistMode {
headers = append(headers, "SensorName")
}
table.SetHeader(headers)
table.SetFooter(headers)
for _, sel := range records {
recordTypeRange := sel.RecordType.Range()
switch recordTypeRange {
case SELRecordTypeRangeStandard:
s := sel.Standard
content := []string{
fmt.Sprintf("%#04x", sel.RecordID),
sel.RecordType.String(),
fmt.Sprintf("%#02x", s.EvMRev),
fmt.Sprintf("%v", s.Timestamp),
fmt.Sprintf("%#04x", s.GeneratorID),
fmt.Sprintf("%#02x", s.SensorNumber),
fmt.Sprintf("%#02x", uint8(s.SensorType)),
s.SensorType.String(),
fmt.Sprintf("%#02x", uint8(s.EventReadingType)),
s.EventReadingType.String(),
s.EventString(),
s.EventDir.String(),
string(s.EventSeverity()),
s.EventData.String(),
}
if elistMode {
var sensorName string
sdr, ok := sdrMap[s.GeneratorID][s.SensorNumber]
if !ok {
sensorName = fmt.Sprintf("N/A %#04x, %#02x", s.GeneratorID, s.SensorNumber)
} else {
sensorName = sdr.SensorName()
}
content = append(content, sensorName)
}
table.Append(content)
case SELRecordTypeRangeTimestampedOEM:
case SELRecordTypeRangeNonTimestampedOEM:
}
}
table.Render()
return buf.String()
}