2 * Copyright (c) 2010-2021 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.powermax.internal.connector;
15 import java.io.IOException;
16 import java.io.InterruptedIOException;
17 import java.util.Arrays;
19 import org.openhab.binding.powermax.internal.message.PowermaxCommManager;
20 import org.openhab.binding.powermax.internal.message.PowermaxReceiveType;
21 import org.openhab.core.util.HexUtils;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
26 * A class that reads messages from the Visonic alarm panel in a dedicated thread
28 * @author Laurent Garnier - Initial contribution
30 public class PowermaxReaderThread extends Thread {
32 private final Logger logger = LoggerFactory.getLogger(PowermaxReaderThread.class);
34 private static final int READ_BUFFER_SIZE = 20;
35 private static final int MAX_MSG_SIZE = 0xC0;
37 private PowermaxConnector connector;
42 * @param in the input stream
43 * @param connector the object that should handle the received message
44 * @param threadName the name of the thread
46 public PowermaxReaderThread(PowermaxConnector connector, String threadName) {
48 this.connector = connector;
53 logger.info("Data listener started");
55 byte[] readDataBuffer = new byte[READ_BUFFER_SIZE];
56 byte[] dataBuffer = new byte[MAX_MSG_SIZE];
59 boolean variableLen = false;
62 while (!Thread.interrupted()) {
63 int len = connector.read(readDataBuffer);
65 for (int i = 0; i < len; i++) {
66 if (index >= MAX_MSG_SIZE) {
67 // too many bytes received, try to find new start
68 if (logger.isDebugEnabled()) {
69 byte[] logData = Arrays.copyOf(dataBuffer, index);
70 logger.debug("Truncating message {}", HexUtils.bytesToHex(logData));
75 if (index == 0 && readDataBuffer[i] == 0x0D) {
78 dataBuffer[index++] = readDataBuffer[i];
79 } else if (index > 0) {
80 dataBuffer[index++] = readDataBuffer[i];
84 PowermaxReceiveType msgType = PowermaxReceiveType.fromCode(readDataBuffer[i]);
85 msgLen = msgType.getLength();
86 variableLen = ((readDataBuffer[i] & 0x000000FF) > 0x10) && (msgLen == 0);
87 } catch (IllegalArgumentException arg0) {
91 } else if (index == 5 && variableLen) {
92 msgLen = (readDataBuffer[i] & 0x000000FF) + 7;
93 } else if ((msgLen == 0 && readDataBuffer[i] == 0x0A) || (index == msgLen)) {
96 if (readDataBuffer[i] != 0x0A && dataBuffer[index - 1] == 0x43) {
97 // adjust message length for 0x43
99 } else if (checkCRC(dataBuffer, index)) {
100 // whole message received with a right CRC
102 byte[] msg = new byte[index];
103 msg = Arrays.copyOf(dataBuffer, index);
105 connector.setWaitingForResponse(System.currentTimeMillis());
106 connector.handleIncomingMessage(msg);
110 } else if (msgLen == 0) {
111 // CRC check failed for a message with an unknown length
112 logger.debug("Message length is now {} but message is apparently not complete",
115 // CRC check failed for a message with a known length
117 connector.setWaitingForResponse(System.currentTimeMillis());
127 } catch (InterruptedIOException e) {
128 Thread.currentThread().interrupt();
129 logger.debug("Interrupted via InterruptedIOException");
130 } catch (IOException e) {
131 logger.debug("Reading failed: {}", e.getMessage(), e);
132 connector.handleCommunicationFailure(e.getMessage());
133 } catch (Exception e) {
134 String msg = e.getMessage() != null ? e.getMessage() : e.toString();
135 logger.debug("Error reading or processing message: {}", msg, e);
136 connector.handleCommunicationFailure(msg);
139 logger.info("Data listener stopped");
143 * Check if the CRC inside a received message is valid or not
145 * @param data the buffer containing the message
146 * @param len the size of the message in the buffer
148 * @return true if the CRC is valid or false if not
150 private boolean checkCRC(byte[] data, int len) {
151 // Messages of type 0xF1 are always sent with a bad CRC (possible panel bug?)
152 if (len == 9 && (data[1] & 0xFF) == 0xF1) {
156 byte checksum = PowermaxCommManager.computeCRC(data, len);
157 byte expected = data[len - 2];
158 if (checksum != expected) {
159 byte[] logData = Arrays.copyOf(data, len);
160 logger.warn("Powermax alarm binding: message CRC check failed (expected {}, got {}, message {})",
161 String.format("%02X", expected), String.format("%02X", checksum), HexUtils.bytesToHex(logData));
163 return (checksum == expected);