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
|
package ipmi
import (
"fmt"
"time"
)
// 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: %w", err)
}
case SELRecordTypeRangeTimestampedOEM:
if err := parseSELOEMTimestamped(msg, sel); err != nil {
return nil, fmt.Errorf("parseSELOEMTimestamped failed, err: %w", err)
}
case SELRecordTypeRangeNonTimestampedOEM:
if err := parseSELOEMNonTimestamped(msg, sel); err != nil {
return nil, fmt.Errorf("parseSELOEMNonTimestamped failed, err: %w", 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 generated from 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.EventData)
}
func (sel *SELStandard) EventSeverity() EventSeverity {
return sel.EventReadingType.EventSeverity(sel.SensorType, sel.EventData, sel.EventDir)
}
func parseSELDefault(msg []byte, sel *SEL) error {
if len(msg) < 16 {
return ErrUnpackedDataTooShortWith(len(msg), 16)
}
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 ErrUnpackedDataTooShortWith(len(msg), 16)
}
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 ErrUnpackedDataTooShortWith(len(msg), 16)
}
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.
// The sdrMap can be fetched by GetSDRsMap method.
func FormatSELs(records []*SEL, sdrMap SDRMapBySensorNumber) string {
var elistMode bool // extend list
if sdrMap != nil {
elistMode = true
}
rows := make([]map[string]string, 0)
for _, sel := range records {
recordTypeRange := sel.RecordType.Range()
switch recordTypeRange {
case SELRecordTypeRangeStandard:
s := sel.Standard
row := map[string]string{
"ID": fmt.Sprintf("%#04x", sel.RecordID),
"RecordType": sel.RecordType.String(),
"EvmRev": fmt.Sprintf("%#02x", s.EvMRev),
"Timestamp": fmt.Sprintf("%v", s.Timestamp),
"GID": fmt.Sprintf("%#04x", uint16(s.GeneratorID)),
"SensorNumber": fmt.Sprintf("%#02x", s.SensorNumber),
"SensorTypeCode": fmt.Sprintf("%#02x", uint8(s.SensorType)),
"SensorType": s.SensorType.String(),
"EventReadingType": fmt.Sprintf("%#02x (%s)", uint8(s.EventReadingType), s.EventReadingType.String()),
"EventDescription": s.EventString(),
"EventDirection": s.EventDir.String(),
"EventSeverity": string(s.EventSeverity()),
"EventData": s.EventData.String(),
}
if elistMode {
var sensorName string
sdr, ok := sdrMap[s.GeneratorID][s.SensorNumber]
if !ok {
sensorName = fmt.Sprintf("N/A %#04x, %#02x", uint16(s.GeneratorID), s.SensorNumber)
} else {
sensorName = sdr.SensorName()
}
row["SensorName"] = sensorName
}
rows = append(rows, row)
case SELRecordTypeRangeTimestampedOEM:
case SELRecordTypeRangeNonTimestampedOEM:
}
}
headers := []string{
"ID",
"RecordType",
"EvmRev",
"Timestamp",
"GID",
"SensorNumber",
"SensorTypeCode",
"SensorType",
"EventReadingType",
"EventDescription",
"EventDirection",
"EventSeverity",
"EventData",
}
if elistMode {
headers = append(headers, "SensorName")
}
return RenderTable(headers, rows)
}
|