2 * Copyright (c) 2010-2023 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.enocean.internal.messages;
15 import java.security.InvalidParameterException;
16 import java.util.Arrays;
20 * @author Daniel Weber - Initial contribution
22 public abstract class BasePacket {
24 public enum ESPPacketType {
25 RADIO_ERP1((byte) 0x01),
26 RESPONSE((byte) 0x02),
27 RADIO_SUB_TEL((byte) 0x03),
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);
37 private ESPPacketType(byte value) {
41 public byte getValue() {
45 public static boolean hasValue(byte value) {
46 for (ESPPacketType p : ESPPacketType.values()) {
47 if (p.value == value) {
55 public static ESPPacketType getPacketType(byte packetType) {
56 for (ESPPacketType p : ESPPacketType.values()) {
57 if (p.value == packetType) {
62 throw new InvalidParameterException("Unknown packetType value");
66 protected ESPPacketType packetType;
67 protected byte[] data;
68 protected byte[] optionalData;
70 public BasePacket(int dataLength, int optionalDataLength, ESPPacketType packetType, byte[] payload) {
71 this(dataLength, optionalDataLength, packetType.value, payload);
74 public BasePacket(int dataLength, int optionalDataLength, byte packetType, byte[] payload) {
75 if (!ESPPacketType.hasValue(packetType)) {
76 throw new InvalidParameterException("Packet type is unknown");
79 if (dataLength + optionalDataLength > payload.length) {
80 throw new InvalidParameterException("data length does not match provided lengths");
83 this.packetType = ESPPacketType.getPacketType(packetType);
85 this.data = new byte[dataLength];
86 System.arraycopy(payload, 0, this.data, 0, dataLength);
88 if (optionalDataLength > 0) {
89 this.optionalData = new byte[optionalDataLength];
90 System.arraycopy(payload, dataLength, optionalData, 0, optionalDataLength);
92 this.optionalData = new byte[0];
96 public ESPPacketType getPacketType() {
97 return this.packetType;
100 public byte[] getPayload(int offset, int length) {
101 return Arrays.copyOfRange(data, offset, offset + length);
104 public byte[] getPayload() {
108 public byte[] getOptionalPayload(int offset, int length) {
109 byte[] result = new byte[length];
110 System.arraycopy(optionalData, offset, result, 0, length);
114 public byte[] getOptionalPayload() {