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.bluetooth.bluegiga.internal;
15 import java.util.HashSet;
17 import java.util.UUID;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.bluetooth.bluegiga.internal.enumeration.AttributeChangeReason;
21 import org.openhab.binding.bluetooth.bluegiga.internal.enumeration.AttributeValueType;
22 import org.openhab.binding.bluetooth.bluegiga.internal.enumeration.BgApiResponse;
23 import org.openhab.binding.bluetooth.bluegiga.internal.enumeration.BluetoothAddressType;
24 import org.openhab.binding.bluetooth.bluegiga.internal.enumeration.ConnectionStatusFlag;
25 import org.openhab.binding.bluetooth.bluegiga.internal.enumeration.ScanResponseType;
28 * Abstract class for response and event packets. This provides the deserialization methods to convert wire data to Java
31 * @author Chris Jackson - Initial contribution and API
35 public abstract class BlueGigaResponse extends BlueGigaPacket {
36 private int[] buffer = new int[131];
37 private int position = 0;
38 protected boolean event = false;
40 protected BlueGigaResponse(int[] inputBuffer) {
41 // TODO Auto-generated constructor stub
47 * Returns true if this response is an event, or false if it is a response to a command
49 * @return true if this is an event
51 public boolean isEvent() {
56 * Reads an int8 from the output stream
58 * @return value read from input
60 protected int deserializeInt8() {
61 if (buffer[position] >= 128) {
62 return buffer[position++] - 256;
64 return buffer[position++];
69 * Reads a uint8 from the output stream
71 * @return value read from input
73 protected int deserializeUInt8() {
74 return buffer[position++];
77 protected boolean deserializeBoolean() {
78 return buffer[position++] != 0;
82 * Reads a uint16 from the output stream
84 * @return value read from input
86 protected int deserializeUInt16() {
87 return buffer[position++] + (buffer[position++] << 8);
90 protected UUID deserializeUuid() {
94 // This is a uint8array type so first byte is the length
95 int length = buffer[position++];
98 // 0000xxxx-0000-1000-8000-00805F9B34FB
99 low = 0x800000805f9b34fbL;
100 high = ((long) buffer[position++] << 32) + ((long) buffer[position++] << 40) + 0x00001000L;
103 // xxxxxxxx-0000-1000-8000-00805F9B34FB
104 low = 0x800000805f9b34fbL;
105 high = ((long) buffer[position++] << 32) + ((long) buffer[position++] << 40)
106 + ((long) buffer[position++] << 48) + ((long) buffer[position++] << 56) + 0x00001000L;
109 low = (buffer[position++]) + ((long) buffer[position++] << 8) + ((long) buffer[position++] << 16)
110 + ((long) buffer[position++] << 24) + ((long) buffer[position++] << 32)
111 + ((long) buffer[position++] << 40) + ((long) buffer[position++] << 48)
112 + ((long) buffer[position++] << 56);
113 high = (buffer[position++]) + ((long) buffer[position++] << 8) + ((long) buffer[position++] << 16)
114 + ((long) buffer[position++] << 24) + ((long) buffer[position++] << 32)
115 + ((long) buffer[position++] << 40) + ((long) buffer[position++] << 48)
116 + ((long) buffer[position++] << 56);
124 return new UUID(high, low);
127 protected BgApiResponse deserializeBgApiResponse() {
128 return BgApiResponse.getBgApiResponse(deserializeUInt16());
131 public Set<ConnectionStatusFlag> deserializeConnectionStatusFlag() {
132 int val = deserializeUInt8();
133 Set<ConnectionStatusFlag> options = new HashSet<>();
134 for (ConnectionStatusFlag option : ConnectionStatusFlag.values()) {
135 if (option == ConnectionStatusFlag.UNKNOWN) {
138 if ((option.getKey() & val) != 0) {
145 protected AttributeValueType deserializeAttributeValueType() {
146 return AttributeValueType.getAttributeValueType(deserializeUInt8());
149 protected BluetoothAddressType deserializeBluetoothAddressType() {
150 return BluetoothAddressType.getBluetoothAddressType(deserializeUInt8());
153 protected AttributeChangeReason deserializeAttributeChangeReason() {
154 return AttributeChangeReason.getAttributeChangeReason(deserializeUInt8());
157 protected ScanResponseType deserializeScanResponseType() {
158 return ScanResponseType.getScanResponseType(deserializeUInt8());
161 protected long deserializeUInt32() {
162 return buffer[position++] + (buffer[position++] << 8) + (buffer[position++] << 16) + (buffer[position++] << 24);
165 protected int[] deserializeUInt8Array() {
166 int length = buffer[position++];
167 int[] val = new int[length];
169 for (int cnt = 0; cnt < length; cnt++) {
170 val[cnt] = deserializeUInt8();
176 protected String deserializeAddress() {
177 StringBuilder builder = new StringBuilder();
179 for (int cnt = 5; cnt >= 0; cnt--) {
183 builder.append(String.format("%02X", buffer[position + cnt]));
187 return builder.toString();