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.CommandCodes;
26  import org.metricshub.ipmi.core.coding.commands.IpmiCommandCoder;
27  import org.metricshub.ipmi.core.coding.commands.PrivilegeLevel;
28  import org.metricshub.ipmi.core.coding.payload.IpmiPayload;
29  import org.metricshub.ipmi.core.coding.payload.lan.IPMIException;
30  import org.metricshub.ipmi.core.coding.payload.lan.IpmiLanRequest;
31  import org.metricshub.ipmi.core.coding.payload.lan.NetworkFunction;
32  import org.metricshub.ipmi.core.coding.protocol.IpmiMessage;
33  import org.metricshub.ipmi.core.common.TypeConverter;
34  
35  import java.security.InvalidKeyException;
36  import java.security.NoSuchAlgorithmException;
37  
38  /**
39   * Wrapper class for Set Session Privilege Level command
40   */
41  public class SetSessionPrivilegeLevel extends IpmiCommandCoder<SetSessionPrivilegeLevelResponseData> {
42  
43  	/** Completion code: the requested level is not available for this user (IPMI 2.0 table 22-24). */
44  	public static final int LEVEL_NOT_AVAILABLE = 0x80;
45  
46  	/** Completion code: the requested level exceeds the privilege limit of the channel or of the user. */
47  	public static final int LEVEL_ABOVE_LIMIT = 0x81;
48  
49  	private PrivilegeLevel privilegeLevel;
50  
51  	/**
52  	 * Says why the BMC refused the level: the meaning of the command-specific completion codes of Set Session
53  	 * Privilege Level (IPMI 2.0 table 22-24), or the message of the error reply for the other codes, then the code.
54  	 *
55  	 * @param refusal the error reply
56  	 * @return the reason, ending with the completion code in hexadecimal
57  	 */
58  	public static String describeRefusal(IPMIException refusal) {
59  		switch (refusal.getRawCode()) {
60  		case LEVEL_NOT_AVAILABLE:
61  			return IPMIException.describe("the level is not available for this user", LEVEL_NOT_AVAILABLE);
62  		case LEVEL_ABOVE_LIMIT:
63  			return IPMIException
64  					.describe("the level exceeds the privilege limit of the channel or of the user", LEVEL_ABOVE_LIMIT);
65  		default:
66  			return refusal.describe();
67  		}
68  	}
69  
70  	/**
71  	 * Initiates {@link SetSessionPrivilegeLevel} for encoding and decoding
72  	 *
73  	 * @param privilegeLevel
74  	 *        - Requested {@link PrivilegeLevel} to acquire. Can not be higher than level declared during starting
75  	 *        session.
76  	 */
77  	public SetSessionPrivilegeLevel(PrivilegeLevel privilegeLevel) {
78  		this.privilegeLevel = privilegeLevel;
79  	}
80  
81  	/**
82  	 * Returns the level the command requests.
83  	 *
84  	 * @return the privilege level, {@link PrivilegeLevel#MaximumAvailable} when the command only returns the present
85  	 *         level of the session
86  	 */
87  	public PrivilegeLevel getPrivilegeLevel() {
88  		return privilegeLevel;
89  	}
90  
91  	@Override
92  	public byte getCommandCode() {
93  		return CommandCodes.SET_SESSION_PRIVILEGE_LEVEL;
94  	}
95  
96  	@Override
97  	public NetworkFunction getNetworkFunction() {
98  		return NetworkFunction.ApplicationRequest;
99  	}
100 
101 	@Override
102 	protected IpmiPayload preparePayload(int sequenceNumber) throws NoSuchAlgorithmException, InvalidKeyException {
103 		byte[] requestData = new byte[1];
104 
105 		requestData[0] = TypeConverter.intToByte(getRequestedPrivilegeLevelEncoded());
106 
107 		return new IpmiLanRequest(
108 				getNetworkFunction(),
109 				getCommandCode(),
110 				requestData,
111 				TypeConverter.intToByte(sequenceNumber));
112 	}
113 
114 	@Override
115 	public SetSessionPrivilegeLevelResponseData getResponseData(IpmiMessage message)
116 			throws IPMIException,
117 			NoSuchAlgorithmException,
118 			InvalidKeyException {
119 		validateResponse(message);
120 
121 		return new SetSessionPrivilegeLevelResponseData();
122 	}
123 
124 	private byte getRequestedPrivilegeLevelEncoded() {
125 		switch (privilegeLevel) {
126 		case MaximumAvailable:
127 			return 0;
128 		case Callback:
129 			return TypeConverter.intToByte(0x1);
130 		case User:
131 			return TypeConverter.intToByte(0x2);
132 		case Operator:
133 			return TypeConverter.intToByte(0x3);
134 		case Administrator:
135 			return TypeConverter.intToByte(0x4);
136 		default:
137 			throw new IllegalArgumentException("Invalid privilege level");
138 		}
139 	}
140 }