]> git.basschouten.com Git - openhab-addons.git/blob
78caead8a1142e88ac647c4b3f48b6ea9b93d35c
[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.velbus.internal;
14
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.util.ArrayList;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.velbus.internal.packets.VelbusPacket;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * The {@link VelbusPacketInputStream} is a wrapper around an InputStream that
27  * aggregates bytes from the input stream to meaningfull packets in the Velbus system.
28  *
29  * @author Cedric Boon - Initial contribution
30  */
31 @NonNullByDefault
32 public class VelbusPacketInputStream {
33     private final Logger logger = LoggerFactory.getLogger(VelbusPacketInputStream.class);
34
35     public InputStream inputStream;
36
37     private ArrayList<Byte> currentData = new ArrayList<Byte>();
38     private @Nullable Byte currentSTX = null;
39     private @Nullable Byte currentPriority = null;
40     private @Nullable Byte currentAddress = null;
41     private @Nullable Byte currentDataLength = null;
42     private @Nullable Byte currentChecksum = null;
43
44     public VelbusPacketInputStream(InputStream inputStream) {
45         this.inputStream = inputStream;
46     }
47
48     public byte[] readPacket() throws IOException {
49         int currentDataByte;
50
51         while ((currentDataByte = inputStream.read()) > -1) {
52             if (currentSTX == null) {
53                 if (((byte) currentDataByte) == VelbusPacket.STX) {
54                     currentSTX = (byte) currentDataByte;
55                 } else {
56                     resetCurrentState();
57                     logger.debug("Packet with invalid start byte: {}", currentDataByte);
58                 }
59             } else if (currentPriority == null) {
60                 if (((byte) currentDataByte) == VelbusPacket.PRIO_HI
61                         || ((byte) currentDataByte) == VelbusPacket.PRIO_LOW) {
62                     currentPriority = (byte) currentDataByte;
63                 } else {
64                     resetCurrentState();
65                     logger.debug("Packet with invalid priority received: {}", currentDataByte);
66                 }
67             } else if (currentAddress == null) {
68                 currentAddress = (byte) currentDataByte;
69             } else if (currentDataLength == null && currentDataByte <= 8) {
70                 currentDataLength = (byte) currentDataByte;
71             } else if (currentDataLength == null) {
72                 currentDataLength = 1;
73                 currentData.add((byte) currentDataByte);
74             } else if (currentDataLength != null && (currentData.size() < currentDataLength)) {
75                 currentData.add((byte) currentDataByte);
76             } else if (currentChecksum == null) {
77                 currentChecksum = (byte) currentDataByte;
78                 byte[] packet = getCurrentPacket();
79                 byte expectedChecksum = VelbusPacket.computeCRCByte(packet);
80
81                 if (currentChecksum != expectedChecksum) {
82                     resetCurrentState();
83                     logger.debug("Packet with invalid checksum received: {} instead of {}", currentChecksum,
84                             expectedChecksum);
85                 }
86             } else if (((byte) currentDataByte) == VelbusPacket.ETX) {
87                 byte[] packet = getCurrentPacket();
88
89                 resetCurrentState();
90
91                 return packet;
92             } else {
93                 resetCurrentState();
94                 logger.debug("Packet with invalid ETX received: {}", currentDataByte);
95             }
96         }
97
98         return new byte[0];
99     }
100
101     public void close() throws IOException {
102         inputStream.close();
103     }
104
105     protected byte[] getCurrentPacket() {
106         if (currentDataLength != null && currentSTX != null && currentPriority != null && currentAddress != null
107                 && currentChecksum != null) {
108             byte[] packet = new byte[6 + currentDataLength];
109             packet[0] = currentSTX;
110             packet[1] = currentPriority;
111             packet[2] = currentAddress;
112             packet[3] = currentDataLength;
113
114             for (int i = 0; i < currentDataLength; i++) {
115                 packet[4 + i] = currentData.get(i);
116             }
117
118             packet[4 + currentDataLength] = currentChecksum;
119             packet[5 + currentDataLength] = VelbusPacket.ETX;
120
121             return packet;
122         }
123
124         return new byte[0];
125     }
126
127     protected void resetCurrentState() {
128         currentSTX = null;
129         currentPriority = null;
130         currentAddress = null;
131         currentDataLength = null;
132         currentData = new ArrayList<>();
133         currentChecksum = null;
134     }
135 }