]> git.basschouten.com Git - openhab-addons.git/blob
9eb9cef356874028dbeb50f383114f7cc25870f8
[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.security.InvalidParameterException;
16 import java.util.Arrays;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19
20 /**
21  *
22  * @author Daniel Weber - Initial contribution
23  */
24 @NonNullByDefault
25 public abstract class BasePacket {
26
27     public enum ESPPacketType {
28         RADIO_ERP1((byte) 0x01),
29         RESPONSE((byte) 0x02),
30         RADIO_SUB_TEL((byte) 0x03),
31         EVENT((byte) 0x04),
32         COMMON_COMMAND((byte) 0x05),
33         SMART_ACK_COMMAND((byte) 0x06),
34         REMOTE_MAN_COMMAND((byte) 0x07),
35         RADIO_MESSAGE((byte) 0x09),
36         RADIO_ERP2((byte) 0x0A);
37
38         private byte value;
39
40         private ESPPacketType(byte value) {
41             this.value = value;
42         }
43
44         public byte getValue() {
45             return value;
46         }
47
48         public static boolean hasValue(byte value) {
49             for (ESPPacketType p : ESPPacketType.values()) {
50                 if (p.value == value) {
51                     return true;
52                 }
53             }
54
55             return false;
56         }
57
58         public static ESPPacketType getPacketType(byte packetType) {
59             for (ESPPacketType p : ESPPacketType.values()) {
60                 if (p.value == packetType) {
61                     return p;
62                 }
63             }
64
65             throw new InvalidParameterException("Unknown packetType value");
66         }
67     }
68
69     protected ESPPacketType packetType;
70     protected byte[] data;
71     protected byte[] optionalData = new byte[0];
72
73     public BasePacket(int dataLength, int optionalDataLength, ESPPacketType packetType, byte[] payload) {
74         this(dataLength, optionalDataLength, packetType.value, payload);
75     }
76
77     public BasePacket(int dataLength, int optionalDataLength, byte packetType, byte[] payload) {
78         if (!ESPPacketType.hasValue(packetType)) {
79             throw new InvalidParameterException("Packet type is unknown");
80         }
81
82         if (dataLength + optionalDataLength > payload.length) {
83             throw new InvalidParameterException("data length does not match provided lengths");
84         }
85
86         this.packetType = ESPPacketType.getPacketType(packetType);
87
88         this.data = new byte[dataLength];
89         System.arraycopy(payload, 0, this.data, 0, dataLength);
90
91         if (optionalDataLength > 0) {
92             this.optionalData = new byte[optionalDataLength];
93             System.arraycopy(payload, dataLength, optionalData, 0, optionalDataLength);
94         } else {
95             this.optionalData = new byte[0];
96         }
97     }
98
99     public ESPPacketType getPacketType() {
100         return this.packetType;
101     }
102
103     public byte[] getPayload(int offset, int length) {
104         return Arrays.copyOfRange(data, offset, offset + length);
105     }
106
107     public byte[] getPayload() {
108         return data;
109     }
110
111     public byte[] getOptionalPayload(int offset, int length) {
112         byte[] result = new byte[length];
113         System.arraycopy(optionalData, offset, result, 0, length);
114         return result;
115     }
116
117     public byte[] getOptionalPayload() {
118         return optionalData;
119     }
120 }