]> git.basschouten.com Git - openhab-addons.git/blob
5912a5d1100a4b8fb0842cd2d23059b33c5954e2
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.upb.internal.message;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16
17 /**
18  * Model for the first two bytes of UPB messages.
19  *
20  * @author cvanorman - Initial contribution
21  * @since 1.9.0
22  */
23 @NonNullByDefault
24 public class ControlWord {
25
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;
36
37     private byte hi = 0;
38     private byte lo = 0;
39
40     /**
41      * Sets the two bytes of the control word.
42      *
43      * @param lo
44      *            the low-order byte.
45      * @param hi
46      *            the high-order byte.
47      */
48     public void setBytes(final byte hi, final byte lo) {
49         this.hi = hi;
50         this.lo = lo;
51     }
52
53     /**
54      * @return the high byte of the control word
55      */
56     public byte getHi() {
57         return hi;
58     }
59
60     /**
61      * @return the low byte of the control word
62      */
63     public byte getLo() {
64         return lo;
65     }
66
67     /**
68      * @return the LNK bit
69      */
70     public boolean isLink() {
71         return (hi & LINK_MASK) > 0;
72     }
73
74     /**
75      * @param link
76      *            the link to set
77      */
78     public void setLink(boolean link) {
79         hi = (byte) (link ? hi | LINK_MASK : hi & ~LINK_MASK);
80     }
81
82     /**
83      * @return the repeaterCount
84      */
85     public int getRepeaterCount() {
86         return (hi & REPEATER_COUNT_MASK) >> REPEATER_COUNT_SHIFT;
87     }
88
89     /**
90      * @param repeaterCount
91      *            the repeaterCount to set
92      */
93     public void setRepeaterCount(int repeaterCount) {
94         hi = (byte) (hi | (repeaterCount << REPEATER_COUNT_SHIFT));
95     }
96
97     /**
98      * @return the packetLength
99      */
100     public int getPacketLength() {
101         return hi & PACKET_LENGTH_MASK;
102     }
103
104     /**
105      * @param packetLength
106      *            the packetLength to set
107      */
108     public void setPacketLength(int packetLength) {
109         hi = (byte) (hi | packetLength);
110     }
111
112     /**
113      * @return the transmitCount
114      */
115     public int getTransmitCount() {
116         return (lo & TRANSMIT_COUNT_MASK) >> TRANSMIT_COUNT_SHIFT;
117     }
118
119     /**
120      * @param transmitCount
121      *            the transmitCount to set
122      */
123     public void setTransmitCount(int transmitCount) {
124         lo = (byte) (lo | (transmitCount << TRANSMIT_COUNT_SHIFT));
125     }
126
127     /**
128      * @return the transmitSequence
129      */
130     public int getTransmitSequence() {
131         return lo & TRANSMIT_SEQUENCE_MASK;
132     }
133
134     /**
135      * @param transmitSequence
136      *            the transmitSequence to set
137      */
138     public void setTransmitSequence(int transmitSequence) {
139         lo = (byte) (lo | transmitSequence);
140     }
141
142     /**
143      * @return the ackPulse
144      */
145     public boolean isAckPulse() {
146         return (lo & ACK_PULSE_MASK) > 0;
147     }
148
149     /**
150      * @param ackPulse
151      *            the ackPulse to set
152      */
153     public void setAckPulse(boolean ackPulse) {
154         lo = (byte) (ackPulse ? lo | ACK_PULSE_MASK : lo & ~ACK_PULSE_MASK);
155     }
156
157     /**
158      * @return the idPulse
159      */
160     public boolean isIdPulse() {
161         return (lo & ID_PULSE_MASK) > 0;
162     }
163
164     /**
165      * @param idPulse
166      *            the idPulse to set
167      */
168     public void setIdPulse(boolean idPulse) {
169         lo = (byte) (idPulse ? lo | ID_PULSE_MASK : lo & ~ID_PULSE_MASK);
170     }
171
172     /**
173      * @return the ackMessage
174      */
175     public boolean isAckMessage() {
176         return (lo & ACK_MESSAGE_MASK) > 0;
177     }
178
179     /**
180      * @param ackMessage
181      *            the ackMessage to set
182      */
183     public void setAckMessage(boolean ackMessage) {
184         lo = (byte) (ackMessage ? lo | ACK_MESSAGE_MASK : lo & ~ACK_MESSAGE_MASK);
185     }
186 }