2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.upb.internal.message;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
18 * Model for the first two bytes of UPB messages.
20 * @author cvanorman - Initial contribution
24 public class ControlWord {
26 private static final int TRANSMIT_COUNT_SHIFT = 2;
27 private static final int TRANSMIT_COUNT_MASK = 0b00001100;
28 private static final int TRANSMIT_SEQUENCE_MASK = 0b00000011;
29 private static final int ACK_PULSE_MASK = 0b00010000;
30 private static final int ID_PULSE_MASK = 0b00100000;
31 private static final int ACK_MESSAGE_MASK = 0b01000000;
32 private static final int REPEATER_COUNT_SHIFT = 5;
33 private static final int REPEATER_COUNT_MASK = 0b01100000;
34 private static final int PACKET_LENGTH_MASK = 0b00011111;
35 private static final int LINK_MASK = 0b10000000;
41 * Sets the two bytes of the control word.
46 * the high-order byte.
48 public void setBytes(final byte hi, final byte lo) {
54 * @return the high byte of the control word
61 * @return the low byte of the control word
70 public boolean isLink() {
71 return (hi & LINK_MASK) > 0;
78 public void setLink(boolean link) {
79 hi = (byte) (link ? hi | LINK_MASK : hi & ~LINK_MASK);
83 * @return the repeaterCount
85 public int getRepeaterCount() {
86 return (hi & REPEATER_COUNT_MASK) >> REPEATER_COUNT_SHIFT;
90 * @param repeaterCount
91 * the repeaterCount to set
93 public void setRepeaterCount(int repeaterCount) {
94 hi = (byte) (hi | (repeaterCount << REPEATER_COUNT_SHIFT));
98 * @return the packetLength
100 public int getPacketLength() {
101 return hi & PACKET_LENGTH_MASK;
105 * @param packetLength
106 * the packetLength to set
108 public void setPacketLength(int packetLength) {
109 hi = (byte) (hi | packetLength);
113 * @return the transmitCount
115 public int getTransmitCount() {
116 return (lo & TRANSMIT_COUNT_MASK) >> TRANSMIT_COUNT_SHIFT;
120 * @param transmitCount
121 * the transmitCount to set
123 public void setTransmitCount(int transmitCount) {
124 lo = (byte) (lo | (transmitCount << TRANSMIT_COUNT_SHIFT));
128 * @return the transmitSequence
130 public int getTransmitSequence() {
131 return lo & TRANSMIT_SEQUENCE_MASK;
135 * @param transmitSequence
136 * the transmitSequence to set
138 public void setTransmitSequence(int transmitSequence) {
139 lo = (byte) (lo | transmitSequence);
143 * @return the ackPulse
145 public boolean isAckPulse() {
146 return (lo & ACK_PULSE_MASK) > 0;
151 * the ackPulse to set
153 public void setAckPulse(boolean ackPulse) {
154 lo = (byte) (ackPulse ? lo | ACK_PULSE_MASK : lo & ~ACK_PULSE_MASK);
158 * @return the idPulse
160 public boolean isIdPulse() {
161 return (lo & ID_PULSE_MASK) > 0;
168 public void setIdPulse(boolean idPulse) {
169 lo = (byte) (idPulse ? lo | ID_PULSE_MASK : lo & ~ID_PULSE_MASK);
173 * @return the ackMessage
175 public boolean isAckMessage() {
176 return (lo & ACK_MESSAGE_MASK) > 0;
181 * the ackMessage to set
183 public void setAckMessage(boolean ackMessage) {
184 lo = (byte) (ackMessage ? lo | ACK_MESSAGE_MASK : lo & ~ACK_MESSAGE_MASK);