View Javadoc
1   package org.metricshub.ipmi.core.coding.commands.sdr;
2   
3   /*-
4    * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
5    * IPMI Java Client
6    * ჻჻჻჻჻჻
7    * Copyright 2023 Verax Systems, MetricsHub
8    * ჻჻჻჻჻჻
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   *
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Lesser Public License for more details.
18   *
19   * You should have received a copy of the GNU General Lesser Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
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   * Wrapper for Get SDR command.
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       * Initiates GetSdr for both encoding and decoding. 'Offset info record' and
58       * 'bytes to read' fields are set to read whole record.
59       *
60       * @param version
61       *            - IPMI version of the command.
62       * @param cipherSuite
63       *            - {@link CipherSuite} containing authentication,
64       *            confidentiality and integrity algorithms for this session.
65       * @param authenticationType
66       *            - Type of authentication used. Must be RMCPPlus for IPMI v2.0.
67       * @param reservationId
68       *            - SDR reservation ID received via {@link ReserveSdrRepository}
69       *            command
70       * @param recordId
71       *            - ID of the record to get
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       * Initiates GetSdr for both encoding and decoding.
82       *
83       * @param version
84       *            - IPMI version of the command.
85       * @param cipherSuite
86       *            - {@link CipherSuite} containing authentication,
87       *            confidentiality and integrity algorithms for this session.
88       * @param authenticationType
89       *            - Type of authentication used. Must be RMCPPlus for IPMI v2.0.
90       * @param reservationId
91       *            - SDR reservation ID received via {@link ReserveSdrRepository}
92       *            command
93       * @param recordId
94       *            - ID of the record to get
95       * @param offset
96       *            - the offset into record, at which reading should be started
97       * @param bytesToRead
98       *            - number of bytes to read
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]; // reservation ID
129 
130         buffer = TypeConverter.intToByteArray(recordId);
131 
132         payload[2] = buffer[3];
133         payload[3] = buffer[2]; // record ID
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 }