1 package org.sentrysoftware.ipmi.core.coding.payload.lan;
2
3 /*-
4 * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
5 * IPMI Java Client
6 * ჻჻჻჻჻჻
7 * Copyright 2023 Verax Systems, Sentry Software
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.sentrysoftware.ipmi.core.coding.commands.CommandCodes;
26 import org.sentrysoftware.ipmi.core.common.TypeConverter;
27
28 /**
29 * A wrapper class for IPMB LAN message
30 */
31 public class IpmiLanRequest extends IpmiLanMessage {
32 /**
33 * Builds IpmiLanRequest addressed at LUN 0.
34 *
35 * @param networkFunction
36 * - command specific {@link NetworkFunction}
37 * @param commandCode
38 * - command specific {@link CommandCodes}
39 * @param requestData
40 * - command specific payload
41 * @param sequenceNumber
42 * - used to match request and response - must be in range [0-63]
43 */
44 public IpmiLanRequest(NetworkFunction networkFunction, byte commandCode,
45 byte[] requestData, byte sequenceNumber) {
46 this(networkFunction, commandCode, requestData, sequenceNumber,
47 TypeConverter.intToByte(0));
48 }
49
50 /**
51 * Builds IpmiLanRequest.
52 *
53 * @param networkFunction
54 * - command specific {@link NetworkFunction}
55 * @param commandCode
56 * - command specific {@link CommandCodes}
57 * @param requestData
58 * - command specific payload
59 * @param sequenceNumber
60 * - used to match request and response - must be in range [0-63]
61 * @param lun
62 * - target Logical Unit Number. Must be in range [0-3].
63 */
64 public IpmiLanRequest(NetworkFunction networkFunction, byte commandCode,
65 byte[] requestData, byte sequenceNumber, byte lun) {
66 if (lun < 0 || lun > 3) {
67 throw new IllegalArgumentException("Invalid LUN");
68 }
69 setResponderAddress(IpmiLanConstants.BMC_ADDRESS);
70 setNetworkFunction(networkFunction);
71 setResponderLogicalUnitNumber(TypeConverter.intToByte(lun));
72 setRequesterAddress(IpmiLanConstants.REMOTE_CONSOLE_ADDRESS);
73 setRequesterLogicalUnitNumber(TypeConverter.intToByte(0));
74 setSequenceNumber(sequenceNumber);
75 setData(requestData);
76 setCommand(commandCode);
77 }
78
79 @Override
80 public int getPayloadLength() {
81 int length = 7;
82
83 if (getData() != null) {
84 length += getData().length;
85 }
86 return length;
87 }
88
89 @Override
90 public byte[] getPayloadData() {
91 byte[] message = new byte[getPayloadLength()];
92
93 message[0] = getResponderAddress();
94 message[1] = TypeConverter.intToByte((networkFunction << 2)
95 | getResponderLogicalUnitNumber());
96 message[2] = getChecksum1(message);
97 message[3] = getRequesterAddress();
98 message[4] = TypeConverter
99 .intToByte(((getSequenceNumber() & 0x3f) << 2)
100 | getResponderLogicalUnitNumber());
101 message[5] = getCommand();
102
103 if (getData() != null) {
104 System.arraycopy(getData(), 0, message, 6, getData().length);
105 }
106
107 message[message.length - 1] = getChecksum2(message);
108
109 return message;
110 }
111
112 }