forked from bougou/go-ipmi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_get_device_sdr.go
124 lines (105 loc) · 2.92 KB
/
cmd_get_device_sdr.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
package ipmi
import "fmt"
// 35.3 Get Device SDR Command
type GetDeviceSDRRequest struct {
ReservationID uint16
RecordID uint16
ReadOffset uint8
ReadBytes uint8 // FFh means read entire record
}
type GetDeviceSDRResponse struct {
NextRecordID uint16
RecordData []byte
}
func (req *GetDeviceSDRRequest) Command() Command {
return CommandGetDeviceSDR
}
func (req *GetDeviceSDRRequest) Pack() []byte {
out := make([]byte, 6)
packUint16L(req.ReservationID, out, 0)
packUint16L(req.RecordID, out, 2)
packUint8(req.ReadOffset, out, 4)
packUint8(req.ReadBytes, out, 5)
return out
}
func (res *GetDeviceSDRResponse) Unpack(msg []byte) error {
if len(msg) < 2 {
return ErrUnpackedDataTooShort
}
res.NextRecordID, _, _ = unpackUint16L(msg, 0)
res.RecordData, _, _ = unpackBytes(msg, 2, len(msg)-2)
return nil
}
func (r *GetDeviceSDRResponse) CompletionCodes() map[uint8]string {
return map[uint8]string{
0x80: "record changed",
}
}
func (res *GetDeviceSDRResponse) Format() string {
return ""
}
// This command returns general information about the collection of sensors in a Dynamic Sensor Device.
func (c *Client) GetDeviceSDR(recordID uint16) (response *GetDeviceSDRResponse, err error) {
request := &GetDeviceSDRRequest{
ReservationID: 0,
RecordID: recordID,
ReadOffset: 0,
ReadBytes: 0xff,
}
response = &GetDeviceSDRResponse{}
err = c.Exchange(request, response)
return
}
func (c *Client) GetDeviceSDRBySensorID(sensorNumber uint8) (*SDR, error) {
if SensorNumber(sensorNumber) == SensorNumberReserved {
return nil, fmt.Errorf("not valid sensorNumber, %#0x is reserved", sensorNumber)
}
var recordID uint16 = 0
for {
res, err := c.GetDeviceSDR(recordID)
if err != nil {
return nil, fmt.Errorf("GetDeviceSDR for recordID (%#0x) failed, err: %s", recordID, err)
}
sdr, err := ParseSDR(res.RecordData, res.NextRecordID)
if err != nil {
return nil, fmt.Errorf("ParseSDR for recordID (%#0x) failed, err: %s", recordID, err)
}
if uint8(sdr.SensorNumber()) == sensorNumber {
return sdr, nil
}
recordID = res.NextRecordID
if recordID == 0xffff {
break
}
}
return nil, fmt.Errorf("not found SDR for sensor id (%#0x)", sensorNumber)
}
func (c *Client) GetDeviceSDRs(recordTypes ...SDRRecordType) ([]*SDR, error) {
var out = make([]*SDR, 0)
var recordID uint16 = 0
for {
res, err := c.GetDeviceSDR(recordID)
if err != nil {
return nil, fmt.Errorf("GetDeviceSDR for recordID (%#0x) failed, err: %s", recordID, err)
}
sdr, err := ParseSDR(res.RecordData, res.NextRecordID)
if err != nil {
return nil, fmt.Errorf("ParseSDR for recordID (%#0x) failed, err: %s", recordID, err)
}
if len(recordTypes) == 0 {
out = append(out, sdr)
} else {
for _, v := range recordTypes {
if sdr.RecordHeader.RecordType == v {
out = append(out, sdr)
break
}
}
}
recordID = res.NextRecordID
if recordID == 0xffff {
break
}
}
return out, nil
}