1 package org.metricshub.ipmi.core.coding.commands.sdr;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import java.security.InvalidKeyException;
26 import java.security.NoSuchAlgorithmException;
27
28 import org.metricshub.ipmi.core.coding.commands.CommandCodes;
29 import org.metricshub.ipmi.core.coding.commands.IpmiCommandCoder;
30 import org.metricshub.ipmi.core.coding.commands.IpmiVersion;
31 import org.metricshub.ipmi.core.coding.commands.ResponseData;
32 import org.metricshub.ipmi.core.coding.payload.CompletionCode;
33 import org.metricshub.ipmi.core.coding.payload.IpmiPayload;
34 import org.metricshub.ipmi.core.coding.payload.lan.IPMIException;
35 import org.metricshub.ipmi.core.coding.payload.lan.IpmiLanRequest;
36 import org.metricshub.ipmi.core.coding.payload.lan.IpmiLanResponse;
37 import org.metricshub.ipmi.core.coding.payload.lan.NetworkFunction;
38 import org.metricshub.ipmi.core.coding.protocol.AuthenticationType;
39 import org.metricshub.ipmi.core.coding.protocol.IpmiMessage;
40 import org.metricshub.ipmi.core.coding.security.CipherSuite;
41 import org.metricshub.ipmi.core.common.TypeConverter;
42
43
44
45
46 public class GetSdr extends IpmiCommandCoder {
47
48 private int reservationId;
49
50 private int recordId;
51
52 private int offset;
53
54 private int bytesToRead;
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73 public GetSdr(IpmiVersion version, CipherSuite cipherSuite,
74 AuthenticationType authenticationType, int reservationId,
75 int recordId) {
76 this(version, cipherSuite, authenticationType, reservationId, recordId,
77 0, 0xFF);
78 }
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100 public GetSdr(IpmiVersion version, CipherSuite cipherSuite,
101 AuthenticationType authenticationType, int reservationId,
102 int recordId, int offset, int bytesToRead) {
103 super(version, cipherSuite, authenticationType);
104 this.recordId = recordId;
105 this.reservationId = reservationId;
106 this.offset = offset;
107 this.bytesToRead = bytesToRead;
108 }
109
110 @Override
111 public byte getCommandCode() {
112 return CommandCodes.GET_SDR;
113 }
114
115 @Override
116 public NetworkFunction getNetworkFunction() {
117 return NetworkFunction.StorageRequest;
118 }
119
120 @Override
121 protected IpmiPayload preparePayload(int sequenceNumber)
122 throws NoSuchAlgorithmException, InvalidKeyException {
123 byte[] payload = new byte[6];
124
125 byte[] buffer = TypeConverter.intToByteArray(reservationId);
126
127 payload[0] = buffer[3];
128 payload[1] = buffer[2];
129
130 buffer = TypeConverter.intToByteArray(recordId);
131
132 payload[2] = buffer[3];
133 payload[3] = buffer[2];
134
135 payload[4] = TypeConverter.intToByte(offset);
136 payload[5] = TypeConverter.intToByte(bytesToRead);
137
138 return new IpmiLanRequest(getNetworkFunction(), getCommandCode(),
139 payload, TypeConverter.intToByte(sequenceNumber));
140 }
141
142 @Override
143 public ResponseData getResponseData(IpmiMessage message) throws IPMIException, NoSuchAlgorithmException, InvalidKeyException {
144 if (!isCommandResponse(message)) {
145 throw new IllegalArgumentException(
146 "This is not a response for Get SDR command");
147 }
148 if (!(message.getPayload() instanceof IpmiLanResponse)) {
149 throw new IllegalArgumentException("Invalid response payload");
150 }
151 if (((IpmiLanResponse) message.getPayload()).getCompletionCode() != CompletionCode.Ok) {
152 throw new IPMIException(
153 ((IpmiLanResponse) message.getPayload())
154 .getCompletionCode());
155 }
156
157 byte[] raw = message.getPayload().getIpmiCommandData();
158
159 if (raw == null || raw.length < 3) {
160 throw new IllegalArgumentException(
161 "Invalid response payload length");
162 }
163
164 GetSdrResponseData responseData = new GetSdrResponseData();
165
166 byte[] buffer = new byte[4];
167
168 buffer[0] = raw[0];
169 buffer[1] = raw[1];
170 buffer[2] = 0;
171 buffer[3] = 0;
172
173 responseData.setNextRecordId(TypeConverter
174 .littleEndianByteArrayToInt(buffer));
175
176 byte[] recordData = new byte[raw.length - 2];
177
178 System.arraycopy(raw, 2, recordData, 0, recordData.length);
179
180 responseData.setSensorRecordData(recordData);
181
182 return responseData;
183 }
184
185 }