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.payload.IpmiPayload;
28  import org.metricshub.ipmi.core.coding.payload.lan.IPMIException;
29  import org.metricshub.ipmi.core.coding.payload.lan.IpmiLanRequest;
30  import org.metricshub.ipmi.core.coding.payload.lan.NetworkFunction;
31  import org.metricshub.ipmi.core.coding.protocol.IpmiMessage;
32  import org.metricshub.ipmi.core.common.TypeConverter;
33  
34  import java.security.InvalidKeyException;
35  import java.security.NoSuchAlgorithmException;
36  
37  /**
38   * Wrapper for Get Sel Entry request
39   */
40  public class GetSelEntry extends IpmiCommandCoder<GetSelEntryResponseData> {
41  
42  	private int reservationId;
43  
44  	private int recordId;
45  
46  	/**
47  	 * Initiates GetSelEntry for both encoding and decoding.
48  	 *
49  	 * @param reservationId
50  	 *        - SEL reservation ID received via {@link ReserveSel} command
51  	 *        or 0 if requesting for whole record
52  	 * @param recordId
53  	 *        - ID of the record to get
54  	 */
55  	public GetSelEntry(int reservationId, int recordId) {
56  		this.recordId = recordId;
57  		this.reservationId = reservationId;
58  	}
59  
60  	@Override
61  	public byte getCommandCode() {
62  		return CommandCodes.GET_SEL_ENTRY;
63  	}
64  
65  	@Override
66  	public NetworkFunction getNetworkFunction() {
67  		return NetworkFunction.StorageRequest;
68  	}
69  
70  	@Override
71  	protected IpmiPayload preparePayload(int sequenceNumber) throws NoSuchAlgorithmException, InvalidKeyException {
72  		byte[] payload = new byte[6];
73  
74  		byte[] buffer = TypeConverter.intToByteArray(reservationId);
75  
76  		payload[0] = buffer[3];
77  		payload[1] = buffer[2]; // reservation ID
78  
79  		buffer = TypeConverter.intToByteArray(recordId);
80  
81  		payload[2] = buffer[3];
82  		payload[3] = buffer[2]; // record ID
83  
84  		payload[4] = 0;
85  		payload[5] = TypeConverter.intToByte(0xff);
86  
87  		return new IpmiLanRequest(getNetworkFunction(), getCommandCode(), payload, TypeConverter.intToByte(sequenceNumber));
88  	}
89  
90  	@Override
91  	public GetSelEntryResponseData getResponseData(IpmiMessage message)
92  			throws IPMIException,
93  			NoSuchAlgorithmException,
94  			InvalidKeyException {
95  		byte[] raw = validateResponse(message);
96  
97  		if (raw == null || raw.length < 3) {
98  			throw new IllegalArgumentException("Invalid response payload length");
99  		}
100 
101 		GetSelEntryResponseData responseData = new GetSelEntryResponseData();
102 
103 		byte[] buffer = new byte[4];
104 
105 		buffer[0] = raw[0];
106 		buffer[1] = raw[1];
107 		buffer[2] = 0;
108 		buffer[3] = 0;
109 
110 		responseData.setNextRecordId(TypeConverter.littleEndianByteArrayToInt(buffer));
111 
112 		byte[] recordData = new byte[raw.length - 2];
113 
114 		System.arraycopy(raw, 2, recordData, 0, recordData.length);
115 
116 		responseData.setSelRecord(SelRecord.populateSelRecord(recordData));
117 
118 		return responseData;
119 	}
120 
121 }