]> git.basschouten.com Git - openhab-addons.git/blob
64a11def2aef7b3318659035110c8d6b23d12e5c
[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.enocean.internal.messages;
14
15 import java.nio.charset.Charset;
16 import java.util.Arrays;
17
18 import org.openhab.binding.enocean.internal.messages.BasePacket.ESPPacketType;
19 import org.openhab.binding.enocean.internal.messages.ERP1Message.RORG;
20 import org.openhab.binding.enocean.internal.messages.ESP2Packet.ESP2PacketType;
21 import org.openhab.binding.enocean.internal.messages.Response.ResponseType;
22 import org.openhab.core.util.HexUtils;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  *
28  * @author Daniel Weber - Initial contribution
29  */
30 public class ESP2PacketConverter {
31
32     protected static Logger logger = LoggerFactory.getLogger(ESP2PacketConverter.class);
33
34     private static final int ESP3PACKET_BASE_LENGTH = ESP3Packet.ESP3_RORG_LENGTH + ESP3Packet.ESP3_SENDERID_LENGTH
35             + ESP3Packet.ESP3_STATUS_LENGTH;
36
37     private static BasePacket handleRadioTelegram(int dataLength, byte packetType, byte[] payload) {
38         switch (ESP2Packet.ORG.getORG(payload[1])) {
39             case _RPS:
40                 return ESP3PacketFactory.BuildPacket(ESP3PACKET_BASE_LENGTH + RORG.RPS.getDataLength(), 0,
41                         ESPPacketType.RADIO_ERP1.getValue(), new byte[] { RORG.RPS.getValue(), payload[2], payload[6],
42                                 payload[7], payload[8], payload[9], payload[10] });
43             case _1BS:
44                 return ESP3PacketFactory.BuildPacket(ESP3PACKET_BASE_LENGTH + RORG._1BS.getDataLength(), 0,
45                         ESPPacketType.RADIO_ERP1.getValue(), new byte[] { RORG._1BS.getValue(), payload[2], payload[6],
46                                 payload[7], payload[8], payload[9], payload[10] });
47
48             case _4BS:
49                 return ESP3PacketFactory.BuildPacket(ESP3PACKET_BASE_LENGTH + RORG._4BS.getDataLength(), 0,
50                         ESPPacketType.RADIO_ERP1.getValue(), new byte[] { RORG._4BS.getValue(), payload[2], payload[3],
51                                 payload[4], payload[5], payload[6], payload[7], payload[8], payload[9], payload[10] });
52             default:
53                 logger.debug("Received unsupported ORG: {}", payload[1]);
54                 return null;
55         }
56     }
57
58     private static BasePacket handleMessageTelegram(int dataLength, byte packetType, byte[] payload) {
59         switch (ESP2Packet.ESP2Response.getResponse(payload[1])) {
60             case OK:
61                 return ESP3PacketFactory.BuildPacket(1, 0, ESPPacketType.RESPONSE.getValue(),
62                         new byte[] { ResponseType.RET_OK.getValue() });
63             case ERR:
64                 return ESP3PacketFactory.BuildPacket(1, 0, ESPPacketType.RESPONSE.getValue(),
65                         new byte[] { ResponseType.RET_ERROR.getValue() });
66             case INF_SW_VERSION: {
67                 byte[] data = new byte[33];
68                 Arrays.fill(data, (byte) 0);
69                 data[0] = ResponseType.RET_OK.getValue();
70                 System.arraycopy(payload, 1, data, 1, 4);
71
72                 byte[] description = "TCM 210".getBytes(Charset.forName("ASCII"));
73                 System.arraycopy(description, 0, data, 17, description.length);
74                 return ESP3PacketFactory.BuildPacket(data.length, 0, ESPPacketType.RESPONSE.getValue(), data);
75             }
76             case UNKOWN: // try to interpret it as a radio telegram
77                 return handleRadioTelegram(dataLength, packetType, payload);
78
79             case ERR_IDRANGE:
80             case ERR_MODEM_DUP_ID:
81             case ERR_MODEM_NOTACK:
82             case ERR_MODEM_NOTWANTEDACK:
83             case ERR_SYNTAX_CHECKSUM:
84             case ERR_SYNTAX_HSEQ:
85             case ERR_SYNTAX_LENGTH:
86             case ERR_SYNTAX_ORG:
87             case ERR_TX_IDRANGE:
88             case INF_IDBase:
89             case INF_MODEM_STATUS:
90             case INF_RX_SENSITIVITY:
91             default:
92                 logger.debug("Received unsupported message telegram: {}",
93                         ESP2Packet.ESP2Response.getResponse(payload[1]).name());
94                 return null;
95         }
96     }
97
98     public static BasePacket BuildPacket(int dataLength, byte packetType, byte[] payload) {
99         ESP2PacketType type = ESP2PacketType.getPacketType(packetType);
100
101         switch (type) {
102             case Receive_Radio_Telegram: // RRT
103                 logger.debug("Received ESP2 radio telegram: {}", HexUtils.bytesToHex(payload));
104                 return handleRadioTelegram(dataLength, packetType, payload);
105
106             case Receive_Message_Telegram: // RMT => Response
107                 logger.debug("Received ESP2 message telegram: {}", HexUtils.bytesToHex(payload));
108                 return handleMessageTelegram(dataLength, packetType, payload);
109
110             case Transmit_Radio_Telegram: // TRT
111                 // This should never happen, as this telegram is just for outbound data
112                 logger.trace("Received Transmit_Radio_Telegram: {}", HexUtils.bytesToHex(payload));
113                 break;
114
115             case Transmit_Command_Telegram: // TCT => CommonCommand
116                 // this should also never happen, as this telegram is also just for outbound data
117                 // however FAM14 receives periodically 0xABFC messages
118                 if (payload[1] == (byte) 0xFC) {
119                     return null;
120                 }
121
122                 logger.trace("Received Transmit_Command_Telegram: {}", HexUtils.bytesToHex(payload));
123                 break;
124         }
125
126         return null;
127     }
128 }