-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathcmd_get_device_sdr.go
204 lines (172 loc) · 5.3 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
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
package ipmi
import (
"context"
"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 ErrUnpackedDataTooShortWith(len(msg), 2)
}
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 ""
}
// The Get Device SDR command allows SDR information for sensors for a Sensor Device
// (typically implemented in a satellite management controller) to be returned.
//
// The Get Device SDR Command can return any type of SDR, not just Types 01h and 02h.
// This is an optional command for Static Sensor Devices, and mandatory for Dynamic Sensor Devices.
// The format and action of this command is similar to that for the Get SDR command
// for SDR Repository Devices.
//
// Sensor Devices that support the Get Device SDR command return SDR Records that
// match the SDR Repository formats.
func (c *Client) GetDeviceSDR(ctx context.Context, recordID uint16) (response *GetDeviceSDRResponse, err error) {
request := &GetDeviceSDRRequest{
ReservationID: 0,
RecordID: recordID,
ReadOffset: 0,
ReadBytes: 0xff,
}
response = &GetDeviceSDRResponse{}
err = c.Exchange(ctx, request, response)
if resErr, ok := err.(*ResponseError); ok {
if resErr.CompletionCode() == CompletionCodeCannotReturnRequestedDataBytes {
return c.getDeviceSDR(ctx, recordID)
}
}
return
}
// getDeviceSDR reads the Device SDR record in partial read way.
func (c *Client) getDeviceSDR(ctx context.Context, recordID uint16) (response *GetDeviceSDRResponse, err error) {
var data []byte
// the actual data length of the SDR can only be determined after the first GetSDR request/response.
dataLength := uint8(0)
reservationID := uint16(0)
readBytes := uint8(16)
readTotal := uint8(0)
readOffset := uint8(0)
for {
request := &GetDeviceSDRRequest{
ReservationID: reservationID,
RecordID: recordID,
ReadOffset: readOffset,
ReadBytes: readBytes,
}
response = &GetDeviceSDRResponse{}
if err = c.Exchange(ctx, request, response); err != nil {
return
}
// determine the total data length by parsing the SDR Header part
if readOffset == 0 {
if len(response.RecordData) < SDRRecordHeaderSize {
return nil, fmt.Errorf("too short record data for SDR header (%d/%d)", len(response.RecordData), SDRRecordHeaderSize)
}
dataLength = response.RecordData[4] + uint8(SDRRecordHeaderSize)
data = make([]byte, dataLength)
}
copy(data[readOffset:readOffset+readBytes], response.RecordData[:])
readOffset += uint8(len(response.RecordData))
readTotal += uint8(len(response.RecordData))
if readTotal >= dataLength {
break
}
if readOffset+readBytes > dataLength {
// decrease the readBytes for the last read.
readBytes = dataLength - readOffset
}
rsp, err := c.ReserveDeviceSDRRepo(ctx)
if err == nil {
reservationID = rsp.ReservationID
} else {
reservationID = 0
}
}
return &GetDeviceSDRResponse{
NextRecordID: response.NextRecordID,
RecordData: data,
}, nil
}
func (c *Client) GetDeviceSDRBySensorID(ctx context.Context, 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(ctx, 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(ctx context.Context, recordTypes ...SDRRecordType) ([]*SDR, error) {
var out = make([]*SDR, 0)
var recordID uint16 = 0
for {
res, err := c.GetDeviceSDR(ctx, 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
}