View Javadoc
1   package org.sentrysoftware.ipmi.core.coding.rmcp;
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.common.TypeConverter;
26  
27  /**
28   * Encodes RMCPMessage into RMCP packet.
29   */
30  public final class RmcpEncoder {
31  
32      private RmcpEncoder() {
33      }
34  
35      /**
36       * Encodes RMCPMessage into RMCP packet.
37       *
38       * @param message
39       *            - RMCP message to be encoded
40       * @return byte data containing ready to send RMCP packet
41       */
42      public static byte[] encode(RmcpMessage message) {
43          byte[] data = new byte[message.getData().length + 4];
44  
45          data[0] = encodeVersion(message.getVersion());
46  
47          data[1] = 0; // reserved
48  
49          data[2] = encodeSequenceNumber(message.getSequenceNumber());
50  
51          data[3] = encodeRMCPClassOfMessage(message.getClassOfMessage());
52  
53          encodeData(message.getData(), data);
54  
55          return data;
56      }
57  
58      private static byte encodeVersion(RmcpVersion version) {
59          switch (version) {
60          case RMCP1_0:
61              return RmcpConstants.RMCP_V1_0;
62          default:
63              throw new IllegalArgumentException("Invalid RMCP version");
64          }
65      }
66  
67      private static byte encodeSequenceNumber(byte sequenceNumber) {
68          return sequenceNumber;
69      }
70  
71      private static byte encodeRMCPClassOfMessage(
72              RmcpClassOfMessage classOfMessage) {
73          return TypeConverter.intToByte(classOfMessage.getCode());
74      }
75  
76      /**
77       * Copies data to message
78       *
79       * @param data
80       *            - source data of RMCPMessage
81       * @param message
82       *            - result message
83       */
84      private static void encodeData(byte[] data, byte[] message) {
85          System.arraycopy(data, 0, message, 4, data.length);
86      }
87  }