View Javadoc
1   package org.metricshub.ipmi.core.coding.commands.chassis;
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.lan.IPMIException;
34  import org.metricshub.ipmi.core.coding.payload.lan.IpmiLanMessage;
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 class for Get Chassis Status request.
45   */
46  public class GetChassisStatus extends IpmiCommandCoder {
47  
48      /**
49       * Initiates GetChassisStatus for 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 GetChassisStatus(IpmiVersion version, CipherSuite cipherSuite,
60              AuthenticationType authenticationType) {
61          super(version, cipherSuite, authenticationType);
62  
63          if (version == IpmiVersion.V20
64                  && authenticationType != AuthenticationType.RMCPPlus) {
65              throw new IllegalArgumentException(
66                      "Authentication Type must be RMCPPlus for IPMI v2.0 messages");
67          }
68      }
69  
70      @Override
71      protected IpmiLanMessage preparePayload(int sequenceNumber) {
72          return new IpmiLanRequest(getNetworkFunction(), getCommandCode(), null,
73                  TypeConverter.intToByte(sequenceNumber));
74      }
75  
76      @Override
77      public byte getCommandCode() {
78          return CommandCodes.GET_CHASSIS_STATUS;
79      }
80  
81      @Override
82      public NetworkFunction getNetworkFunction() {
83          return NetworkFunction.ChassisRequest;
84      }
85  
86      @Override
87      public ResponseData getResponseData(IpmiMessage message) throws IPMIException, NoSuchAlgorithmException, InvalidKeyException {
88          if (!isCommandResponse(message)) {
89              throw new IllegalArgumentException(
90                      "This is not a response for Get Chassis Status command");
91          }
92          if (!(message.getPayload() instanceof IpmiLanResponse)) {
93              throw new IllegalArgumentException("Invalid response payload");
94          }
95          if (((IpmiLanResponse) message.getPayload()).getCompletionCode() != CompletionCode.Ok) {
96              throw new IPMIException(
97                      ((IpmiLanResponse) message.getPayload())
98                              .getCompletionCode());
99          }
100 
101         byte[] raw = message.getPayload().getIpmiCommandData();
102 
103         if (raw == null || (raw.length != 3 && raw.length != 4)) {
104             throw new IllegalArgumentException(
105                     "Invalid response payload length");
106         }
107 
108         GetChassisStatusResponseData responseData = new GetChassisStatusResponseData();
109 
110         responseData.setCurrentPowerState(raw[0]);
111         responseData.setLastPowerEvent(raw[1]);
112         responseData.setMiscChassisState(raw[2]);
113 
114         if (raw.length == 4) {
115             responseData.setFrontPanelButtonCapabilities(raw[3]);
116         }
117 
118         return responseData;
119     }
120 
121 }