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