1 package org.metricshub.ipmi.core.coding.commands;
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.PayloadCoder;
26 import org.metricshub.ipmi.core.coding.payload.IpmiPayload;
27 import org.metricshub.ipmi.core.coding.payload.PlainMessage;
28 import org.metricshub.ipmi.core.coding.payload.lan.IpmiLanResponse;
29 import org.metricshub.ipmi.core.coding.payload.lan.NetworkFunction;
30 import org.metricshub.ipmi.core.coding.protocol.AuthenticationType;
31 import org.metricshub.ipmi.core.coding.protocol.IpmiMessage;
32 import org.metricshub.ipmi.core.coding.protocol.PayloadType;
33 import org.metricshub.ipmi.core.coding.security.CipherSuite;
34
35 /**
36 * A wrapper for IPMI command.
37 *
38 * Parameterless constructors in classes derived from IpmiCommandCoder are meant
39 * to be used for decoding. To avoid omitting setting an important parameter
40 * when encoding message use parametered constructors rather than the
41 * parameterless ones.
42 *
43 */
44 public abstract class IpmiCommandCoder extends PayloadCoder {
45
46 public IpmiCommandCoder() {
47
48 }
49
50 public IpmiCommandCoder(IpmiVersion version, CipherSuite cipherSuite,
51 AuthenticationType authenticationType) {
52 super(version, cipherSuite, authenticationType);
53 }
54
55 @Override
56 public PayloadType getSupportedPayloadType() {
57 return PayloadType.Ipmi;
58 }
59
60 /**
61 * Checks if given message contains response command specific for this
62 * class.
63 *
64 * @param message {@link IpmiMessage} wrapping the IPMI message
65 * @return True if message contains response command specific for this
66 * class, false otherwise.
67 */
68 public boolean isCommandResponse(IpmiMessage message) {
69 if (message.getPayload() instanceof IpmiPayload) {
70 if (message.getPayload() instanceof IpmiLanResponse) {
71 return ((IpmiLanResponse) message.getPayload()).getCommand() == getCommandCode();
72 } else {
73 return message.getPayload() instanceof PlainMessage;
74 }
75 } else {
76 return false;
77 }
78 }
79
80 /**
81 * Retrieves command code specific for command represented by this class
82 *
83 * @return command code
84 */
85 public abstract byte getCommandCode();
86
87 /**
88 * Retrieves network function specific for command represented by this
89 * class.
90 *
91 * @return network function
92 * @see NetworkFunction
93 */
94 public abstract NetworkFunction getNetworkFunction();
95
96 /**
97 * Used in several derived classes - converts {@link PrivilegeLevel} to
98 * byte.
99 *
100 * @param privilegeLevel
101 * @return privilegeLevel encoded as a byte due to {@link CommandsConstants}
102 */
103 protected byte encodePrivilegeLevel(PrivilegeLevel privilegeLevel) {
104 switch (privilegeLevel) {
105 case MaximumAvailable:
106 return CommandsConstants.AL_HIGHEST_AVAILABLE;
107 case Callback:
108 return CommandsConstants.AL_CALLBACK;
109 case User:
110 return CommandsConstants.AL_USER;
111 case Operator:
112 return CommandsConstants.AL_OPERATOR;
113 case Administrator:
114 return CommandsConstants.AL_ADMINISTRATOR;
115 default:
116 throw new IllegalArgumentException("Invalid privilege level");
117 }
118 }
119 }