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.echonetlite.internal;
15 import java.net.SocketAddress;
16 import java.nio.ByteBuffer;
17 import java.nio.ByteOrder;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
23 * @author Michael Barker - Initial contribution
26 public class EchonetMessage {
27 public static final int TID_OFFSET = 2;
28 public static final int GROUP_OFFSET = 4;
29 public static final int CLASS_OFFSET = 5;
30 public static final int INSTANCE_OFFSET = 6;
31 public static final int ESV_OFFSET = 10;
32 public static final int OPC_OFFSET = 11;
33 public static final int PROPERTY_OFFSET = 12;
35 private final ByteBuffer messageData = ByteBuffer.allocateDirect(65536);
36 private final ByteBuffer propertyData = messageData.duplicate();
37 private int propertyCursor = 0;
38 private int currentProperty = -1;
41 private SocketAddress address;
43 public ByteBuffer bufferForRead() {
48 private void reset() {
50 messageData.order(ByteOrder.BIG_ENDIAN);
55 public void sourceAddress(final SocketAddress address) {
56 this.address = address;
59 public @Nullable SocketAddress sourceAddress() {
63 public @Nullable EchonetClass sourceClass() {
64 return EchonetClassIndex.INSTANCE.lookup(messageData.get(GROUP_OFFSET), messageData.get(CLASS_OFFSET));
67 public byte instance() {
68 return messageData.get(INSTANCE_OFFSET);
72 return Esv.forCode(messageData.get(ESV_OFFSET));
75 public int numProperties() {
76 return 0xFF & messageData.get(OPC_OFFSET);
79 public boolean moveNext() {
80 if (propertyCursor < numProperties()) {
82 if (-1 == currentProperty) {
83 currentProperty = PROPERTY_OFFSET;
85 int pdc = 0xFF & messageData.get(currentProperty + 1);
86 currentProperty = currentProperty + 2 + pdc;
94 public int currentEpc() {
95 return messageData.get(currentProperty) & 0xFF;
98 public int currentPdc() {
99 return messageData.get(currentProperty + 1) & 0xFF;
102 public ByteBuffer currentEdt() {
103 propertyData.clear();
104 propertyData.position(currentProperty + 2).limit(currentProperty + 2 + currentPdc());
109 return messageData.getShort(TID_OFFSET);
112 public String toDebug() {
113 return "EchonetMessage{" + "sourceAddress=" + sourceAddress() + ", class=" + sourceClass() + ", instance="
114 + instance() + ", num properties=" + numProperties() + ", data=" + dumpData() + '}';
117 private String dumpData() {
118 final byte[] bs = new byte[messageData.limit()];
119 final ByteBuffer duplicate = messageData.duplicate();
120 duplicate.position(0).limit(messageData.limit());
123 final StringBuilder sb = new StringBuilder();
127 sb.append("0x").append(Integer.toHexString(0xFF & b)).append(", ");
129 sb.setLength(sb.length() - 2);
132 return sb.toString();