View Javadoc
1   package org.metricshub.ipmi.core.coding.commands.sel;
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 org.metricshub.ipmi.core.coding.commands.CommandCodes;
26  import org.metricshub.ipmi.core.coding.commands.IpmiCommandCoder;
27  import org.metricshub.ipmi.core.coding.commands.IpmiVersion;
28  import org.metricshub.ipmi.core.coding.commands.ResponseData;
29  import org.metricshub.ipmi.core.coding.payload.CompletionCode;
30  import org.metricshub.ipmi.core.coding.payload.IpmiPayload;
31  import org.metricshub.ipmi.core.coding.payload.lan.IPMIException;
32  import org.metricshub.ipmi.core.coding.payload.lan.IpmiLanRequest;
33  import org.metricshub.ipmi.core.coding.payload.lan.IpmiLanResponse;
34  import org.metricshub.ipmi.core.coding.payload.lan.NetworkFunction;
35  import org.metricshub.ipmi.core.coding.protocol.AuthenticationType;
36  import org.metricshub.ipmi.core.coding.protocol.IpmiMessage;
37  import org.metricshub.ipmi.core.coding.security.CipherSuite;
38  import org.metricshub.ipmi.core.common.TypeConverter;
39  
40  import java.security.InvalidKeyException;
41  import java.security.NoSuchAlgorithmException;
42  
43  /**
44   * Wrapper class for Get SEL Info request.
45   */
46  public class GetSelInfo extends IpmiCommandCoder {
47  
48      /**
49       * Initiates GetSelInfo for both encoding and decoding.
50       *
51       * @param version
52       *            - IPMI version of the command.
53       * @param cipherSuite
54       *            - {@link CipherSuite} containing authentication,
55       *            confidentiality and integrity algorithms for this session.
56       * @param authenticationType
57       *            - Type of authentication used. Must be RMCPPlus for IPMI v2.0.
58       */
59      public GetSelInfo(IpmiVersion version, CipherSuite cipherSuite,
60              AuthenticationType authenticationType) {
61          super(version, cipherSuite, authenticationType);
62      }
63  
64      @Override
65      public byte getCommandCode() {
66          return CommandCodes.GET_SEL_INFO;
67      }
68  
69      @Override
70      public NetworkFunction getNetworkFunction() {
71          return NetworkFunction.StorageRequest;
72      }
73  
74      @Override
75      protected IpmiPayload preparePayload(int sequenceNumber)
76              throws NoSuchAlgorithmException, InvalidKeyException {
77          return new IpmiLanRequest(getNetworkFunction(), getCommandCode(), null,
78                  TypeConverter.intToByte(sequenceNumber));
79      }
80  
81      @Override
82      public ResponseData getResponseData(IpmiMessage message) throws IPMIException, NoSuchAlgorithmException, InvalidKeyException {
83          if (!isCommandResponse(message)) {
84              throw new IllegalArgumentException(
85                      "This is not a response for Get SEL Info command");
86          }
87          if (!(message.getPayload() instanceof IpmiLanResponse)) {
88              throw new IllegalArgumentException("Invalid response payload");
89          }
90          if (((IpmiLanResponse) message.getPayload()).getCompletionCode() != CompletionCode.Ok) {
91              throw new IPMIException(
92                      ((IpmiLanResponse) message.getPayload())
93                              .getCompletionCode());
94          }
95  
96          byte[] raw = message.getPayload().getIpmiCommandData();
97  
98          if (raw == null || raw.length != 14) {
99              throw new IllegalArgumentException("Invalid response payload length: " + (raw != null ? raw.length : "null"));
100         }
101 
102         GetSelInfoResponseData responseData = new GetSelInfoResponseData();
103 
104         responseData.setSelVersion(TypeConverter
105                 .littleEndianBcdByteToInt(raw[0]));
106 
107         byte[] buffer = new byte[4];
108 
109         buffer[0] = raw[1];
110         buffer[1] = raw[2];
111         buffer[2] = 0;
112         buffer[3] = 0;
113 
114         responseData.setEntriesCount(TypeConverter
115                 .littleEndianByteArrayToInt(buffer));
116 
117         System.arraycopy(raw, 5, buffer, 0, 4);
118 
119         responseData.setAdditionTimestamp(TypeConverter
120                 .decodeDate(TypeConverter.littleEndianByteArrayToInt(buffer)));
121 
122         System.arraycopy(raw, 9, buffer, 0, 4);
123 
124         responseData.setEraseTimestamp(TypeConverter.decodeDate(TypeConverter
125                 .littleEndianByteArrayToInt(buffer)));
126 
127         return responseData;
128     }
129 
130 }