View Javadoc
1   package org.metricshub.ipmi.core.coding.commands.session;
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.IpmiCommandCoder;
26  import org.metricshub.ipmi.core.coding.payload.IpmiPayload;
27  import org.metricshub.ipmi.core.coding.payload.lan.IPMIException;
28  import org.metricshub.ipmi.core.coding.payload.lan.IpmiLanRequest;
29  import org.metricshub.ipmi.core.coding.payload.lan.NetworkFunction;
30  import org.metricshub.ipmi.core.coding.protocol.IpmiMessage;
31  import org.metricshub.ipmi.core.coding.payload.CompletionCode;
32  import org.metricshub.ipmi.core.common.TypeConverter;
33  
34  import java.security.InvalidKeyException;
35  import java.security.NoSuchAlgorithmException;
36  
37  /**
38   * Wrapper for Close Session request
39   */
40  public class CloseSession extends IpmiCommandCoder<CloseSessionResponseData> {
41  
42  	private static final int INVALID_SESSION_ID = 0x87;
43  
44  	private static final int INVALID_SESSION_HANDLE = 0x88;
45  
46  	private int sessionId;
47  
48  	/**
49  	 * Initiates CloseSession for both encoding and decoding.
50  	 *
51  	 * @param sessionId
52  	 *        - Generated by managed system ID of the session to close.
53  	 */
54  	public CloseSession(int sessionId) {
55  
56  		if (sessionId == 0) {
57  			throw new IllegalArgumentException("Cannot close session '0'");
58  		}
59  
60  		this.sessionId = sessionId;
61  	}
62  
63  	@Override
64  	public byte getCommandCode() {
65  		return TypeConverter.intToByte(0x3c);
66  	}
67  
68  	@Override
69  	public NetworkFunction getNetworkFunction() {
70  		return NetworkFunction.ApplicationRequest;
71  	}
72  
73  	@Override
74  	protected IpmiPayload preparePayload(int sequenceNumber) throws NoSuchAlgorithmException, InvalidKeyException {
75  
76  		byte[] payload = TypeConverter.intToLittleEndianByteArray(sessionId);
77  
78  		return new IpmiLanRequest(getNetworkFunction(), getCommandCode(), payload, TypeConverter.intToByte(sequenceNumber));
79  	}
80  
81  	@Override
82  	protected CompletionCode decodeCommandSpecificCompletionCode(int rawCode) {
83  		// IPMI 2.0 Table 22-19
84  		switch (rawCode) {
85  		case INVALID_SESSION_ID:
86  			return CompletionCode.InvalidSessionId;
87  		case INVALID_SESSION_HANDLE:
88  			return CompletionCode.InvalidSessionHandle;
89  		default:
90  			return CompletionCode.Unknown;
91  		}
92  	}
93  
94  	@Override
95  	public CloseSessionResponseData getResponseData(IpmiMessage message)
96  			throws IPMIException,
97  			NoSuchAlgorithmException,
98  			InvalidKeyException {
99  		validateResponse(message);
100 
101 		return new CloseSessionResponseData();
102 	}
103 
104 }