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.Arrays;
16 import java.util.UUID;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.binding.bluetooth.bluegiga.internal.enumeration.BluetoothAddressType;
20 import org.openhab.binding.bluetooth.bluegiga.internal.enumeration.GapConnectableMode;
21 import org.openhab.binding.bluetooth.bluegiga.internal.enumeration.GapDiscoverMode;
22 import org.openhab.binding.bluetooth.bluegiga.internal.enumeration.GapDiscoverableMode;
23 import org.openhab.binding.bluetooth.bluegiga.internal.enumeration.SmpIoCapabilities;
26 * Abstract base class for all commands. This provides the serialisation methods for converting parameters from Java
27 * classes to wire data.
29 * @author Chris Jackson - Initial contribution and API
33 public abstract class BlueGigaCommand extends BlueGigaPacket {
34 protected int[] buffer = new int[131];
35 protected int length = 0;
37 protected void serializeHeader(int classId, int commandId) {
38 // Octet 0 7 1 bit Message Type (MT) 0: Command
39 // -------6:3 4 bits Technology Type (TT) 0000: Smart Bluetooth
40 // -------2:0 3 bits Length High (LH) Payload length (high bits)
43 // Octet 1 7:0 8 bits Length Low (LL) Payload length (low bits)
46 // Octet 2 7:0 8 bits Class ID (CID) Command class ID
47 buffer[length++] = classId;
49 // Octet 3 7:0 8 bits Command ID (CMD) Command ID
50 buffer[length++] = commandId;
54 * Adds a uint8 into the output stream
58 protected void serializeUInt8(int val) {
59 buffer[length++] = val & 0xFF;
62 protected void serializeBoolean(boolean val) {
63 buffer[length++] = val ? 1 : 0;
67 * Adds a uint16 into the output stream
71 protected void serializeUInt16(int val) {
72 buffer[length++] = val & 0xFF;
73 buffer[length++] = (val >> 8) & 0xFF;
77 * Adds a uint32 into the output stream
81 protected void serializeUInt32(long passkey) {
82 buffer[length++] = (int) (passkey & 0xFF);
83 buffer[length++] = (int) ((passkey >> 8) & 0xFF);
84 buffer[length++] = (int) ((passkey >> 16) & 0xFF);
85 buffer[length++] = (int) ((passkey >> 24) & 0xFF);
88 protected void serializeUInt8Array(int[] array) {
89 serializeUInt8(array.length);
91 for (int val : array) {
96 protected void serializeUuid(UUID uuid) {
97 // TODO this probably needs to support longer UUIDs?
99 long high = uuid.getMostSignificantBits();
101 buffer[length++] = (int) ((high >> 32) & 0xff);
102 buffer[length++] = (int) ((high >> 40) & 0xff);
105 protected void serializeAddress(String address) {
106 String[] bytes = address.split(":");
107 if (bytes.length == 0) {
118 for (int cnt = 5; cnt >= 0; cnt--) {
119 serializeUInt8(Integer.parseInt(bytes[cnt], 16));
123 protected void serializeSmpIoCapabilities(SmpIoCapabilities capabilities) {
124 serializeUInt8(capabilities.getKey());
127 protected void serializeBluetoothAddressType(BluetoothAddressType addrType) {
128 serializeUInt8(addrType.getKey());
131 protected void serializeGapDiscoverableMode(GapDiscoverableMode mode) {
132 serializeUInt8(mode.getKey());
135 protected void serializeGapConnectableMode(GapConnectableMode mode) {
136 serializeUInt8(mode.getKey());
139 protected void serializeGapDiscoverMode(GapDiscoverMode mode) {
140 serializeUInt8(mode.getKey());
143 protected int[] getPayload() {
144 buffer[1] = length - 4;
145 return Arrays.copyOfRange(buffer, 0, length);
148 public abstract int[] serialize();