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      protected 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      public NetworkFunction getNetworkFunction() {
62          return NetworkFunction.parseInt(TypeConverter.byteToInt(networkFunction));
63      }
64  
65      public void setResponderLogicalUnitNumber(byte responderLogicalUnitNumber) {
66          this.responderLogicalUnitNumber = responderLogicalUnitNumber;
67      }
68  
69      public byte getResponderLogicalUnitNumber() {
70          return responderLogicalUnitNumber;
71      }
72  
73      public void setSequenceNumber(byte sequenceAddress) {
74          this.sequenceNumber = sequenceAddress;
75      }
76  
77      public byte getSequenceNumber() {
78          return sequenceNumber;
79      }
80  
81      public void setRequesterAddress(byte requesterAddress) {
82          this.requesterAddress = requesterAddress;
83      }
84  
85      public byte getRequesterAddress() {
86          return requesterAddress;
87      }
88  
89      public void setRequesterLogicalUnitNumber(byte requesterLogicalUnitNumber) {
90          this.requesterLogicalUnitNumber = requesterLogicalUnitNumber;
91      }
92  
93      public byte getRequesterLogicalUnitNumber() {
94          return requesterLogicalUnitNumber;
95      }
96  
97      public void setCommand(byte command) {
98          this.command = command;
99      }
100 
101     public byte getCommand() {
102         return command;
103     }
104     
105     /**
106      * Gets expected size of LAN message in bytes.
107      */
108     @Override
109     public abstract int getPayloadLength();
110     
111     /**
112      * Converts IpmiLanMessage to byte array. 
113      */
114     @Override
115     public abstract byte[] getPayloadData();
116         
117     protected byte getChecksum1(byte[] message) {
118         int checksum = 0;
119         for(int i = 0; i < 2; ++i) {
120             checksum = (checksum + TypeConverter.byteToInt(message[i])) % 256;
121         }
122         return (byte) -TypeConverter.intToByte(checksum);
123     }
124     
125     protected byte getChecksum2(byte[] message) {
126         int checksum = 0;
127         for(int i = 3; i < message.length-1; ++i) {
128             checksum = ((checksum + TypeConverter.byteToInt(message[i])) % 256);
129         }
130         return (byte)-TypeConverter.intToByte(checksum);
131     }
132     
133     @Override
134     public byte[] getIpmiCommandData() {
135         return getData();
136     }
137 }