1 package org.metricshub.ipmi.core.coding.payload.sol;
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.common.TypeConverter;
26
27 import java.util.HashSet;
28 import java.util.Set;
29
30 /**
31 * {@link SolInboundStatusField} is a transfer object for status sent by remote system to this application in
32 * {@link SolInboundMessage}.
33 */
34 public class SolInboundStatusField {
35
36 /**
37 * Acknowledge state of {@link SolMessage} that this message is response for.
38 */
39 private final SolAckState ackState;
40
41 /**
42 * Set of statuses indicated by BMC in this message.
43 */
44 private final Set<SolStatus> statuses;
45
46 /**
47 * Creates new instance if {@link SolInboundStatusField} without Acknowledge data.
48 *
49 * @param statuses
50 * Set of SOL specific statuses for inbound message
51 */
52 public SolInboundStatusField(Set<SolStatus> statuses) {
53 this.ackState = SolAckState.ACK;
54 this.statuses = statuses;
55 }
56
57 /**
58 * Creates new instance of {@link SolInboundStatusField} filled with given data.
59 *
60 * @param ackState
61 * Acknowledge state carried by this object
62 * @param statuses
63 * Set of SOL specific statuses for inbound message
64 */
65 public SolInboundStatusField(SolAckState ackState, Set<SolStatus> statuses) {
66 this.ackState = ackState;
67 this.statuses = statuses;
68 }
69
70 /**
71 * Creates new instance of {@link SolInboundStatusField} from raw byte.
72 *
73 * @param raw
74 * byte carrying information about SOL status
75 */
76 public SolInboundStatusField(byte raw) {
77 this.ackState = SolAckState.extractFromByte(raw);
78 this.statuses = extractStatusesFromByte(raw);
79 }
80
81 private Set<SolStatus> extractStatusesFromByte(byte raw) {
82 Set<SolStatus> result = new HashSet<SolStatus>();
83
84 for (SolStatus status : SolStatus.values()) {
85 if (TypeConverter.isBitSetOnPosition(status.getStatusNumber(), raw)) {
86 result.add(status);
87 }
88 }
89
90 return result;
91 }
92
93 public Set<SolStatus> getStatuses() {
94 return statuses;
95 }
96
97 public SolAckState getAckState() {
98 return ackState;
99 }
100
101 /**
102 * Convert this object to it's raw, byte representation.
103 */
104 public byte convertToByte() {
105 byte value = (byte) 0;
106
107 value = ackState.encodeInByte(value);
108
109 for (SolStatus status : statuses) {
110 value = TypeConverter.setBitOnPosition(status.getStatusNumber(), value);
111 }
112
113 return value;
114 }
115 }