View Javadoc
1   package org.metricshub.ipmi.core.coding.payload.lan;
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.payload.IpmiPayload;
26  import org.metricshub.ipmi.core.common.TypeConverter;
27  
28  /**
29   * A wrapper class for IPMI LAN message
30   */
31  public abstract class IpmiLanMessage extends IpmiPayload {
32  	public static final int MIN_SEQUENCE_NUMBER = 1;
33  	public static final int MAX_SEQUENCE_NUMBER = 63;
34  
35  	private byte responderAddress;
36  
37  	private byte networkFunction;
38  
39  	private byte responderLogicalUnitNumber;
40  
41  	private byte requesterAddress;
42  
43  	private byte requesterLogicalUnitNumber;
44  
45  	private byte sequenceNumber;
46  
47  	private byte command;
48  
49  	public void setResponderAddress(byte responderAddress) {
50  		this.responderAddress = responderAddress;
51  	}
52  
53  	public byte getResponderAddress() {
54  		return responderAddress;
55  	}
56  
57  	public void setNetworkFunction(NetworkFunction networkFunction) {
58  		this.networkFunction = TypeConverter.intToByte(networkFunction.getCode());
59  	}
60  
61  	/**
62  	 * Returns the network function, when it is one {@link NetworkFunction} lists.
63  	 *
64  	 * @return the network function
65  	 * @throws IllegalArgumentException when the code is one {@link NetworkFunction} does not list: see
66  	 *         {@link #getNetworkFunctionCode()}
67  	 */
68  	public NetworkFunction getNetworkFunction() {
69  		return NetworkFunction.parseInt(TypeConverter.byteToInt(networkFunction));
70  	}
71  
72  	/**
73  	 * Sets the raw network function code (6 bits), which may not map to a known {@link NetworkFunction}.
74  	 *
75  	 * @param networkFunctionCode the network function code
76  	 */
77  	protected void setNetworkFunctionCode(byte networkFunctionCode) {
78  		this.networkFunction = networkFunctionCode;
79  	}
80  
81  	/**
82  	 * Returns the raw network function code (6 bits), which may be one {@link NetworkFunction} does not list:
83  	 * Transport (0Ch), Group Extension (2Ch), OEM/Group (2Eh), controller-specific OEM (30h-3Fh), IPMI 2.0 table 5-1.
84  	 *
85  	 * @return the network function code
86  	 */
87  	public byte getNetworkFunctionCode() {
88  		return networkFunction;
89  	}
90  
91  	public void setResponderLogicalUnitNumber(byte responderLogicalUnitNumber) {
92  		this.responderLogicalUnitNumber = responderLogicalUnitNumber;
93  	}
94  
95  	public byte getResponderLogicalUnitNumber() {
96  		return responderLogicalUnitNumber;
97  	}
98  
99  	public void setSequenceNumber(byte sequenceAddress) {
100 		this.sequenceNumber = sequenceAddress;
101 	}
102 
103 	public byte getSequenceNumber() {
104 		return sequenceNumber;
105 	}
106 
107 	public void setRequesterAddress(byte requesterAddress) {
108 		this.requesterAddress = requesterAddress;
109 	}
110 
111 	public byte getRequesterAddress() {
112 		return requesterAddress;
113 	}
114 
115 	public void setRequesterLogicalUnitNumber(byte requesterLogicalUnitNumber) {
116 		this.requesterLogicalUnitNumber = requesterLogicalUnitNumber;
117 	}
118 
119 	public byte getRequesterLogicalUnitNumber() {
120 		return requesterLogicalUnitNumber;
121 	}
122 
123 	public void setCommand(byte command) {
124 		this.command = command;
125 	}
126 
127 	public byte getCommand() {
128 		return command;
129 	}
130 
131 	/**
132 	 * Gets expected size of LAN message in bytes.
133 	 */
134 	@Override
135 	public abstract int getPayloadLength();
136 
137 	/**
138 	 * Converts IpmiLanMessage to byte array.
139 	 */
140 	@Override
141 	public abstract byte[] getPayloadData();
142 
143 	protected byte getChecksum1(byte[] message) {
144 		int checksum = 0;
145 		for (int i = 0; i < 2; ++i) {
146 			checksum = (checksum + TypeConverter.byteToInt(message[i])) % 256;
147 		}
148 		return (byte) -TypeConverter.intToByte(checksum);
149 	}
150 
151 	protected byte getChecksum2(byte[] message) {
152 		int checksum = 0;
153 		for (int i = 3; i < message.length - 1; ++i) {
154 			checksum = ((checksum + TypeConverter.byteToInt(message[i])) % 256);
155 		}
156 		return (byte) -TypeConverter.intToByte(checksum);
157 	}
158 
159 	@Override
160 	public byte[] getIpmiCommandData() {
161 		return getData();
162 	}
163 }