]> git.basschouten.com Git - openhab-addons.git/blob
1e0032feca7fd3e28d5411649da83ff2f59dce38
[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.bluetooth.bluegiga.internal;
14
15 import java.util.Arrays;
16 import java.util.UUID;
17
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;
24
25 /**
26  * Abstract base class for all commands. This provides the serialisation methods for converting parameters from Java
27  * classes to wire data.
28  *
29  * @author Chris Jackson - Initial contribution and API
30  *
31  */
32 @NonNullByDefault
33 public abstract class BlueGigaCommand extends BlueGigaPacket {
34     protected int[] buffer = new int[131];
35     protected int length = 0;
36
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)
41         buffer[length++] = 0;
42
43         // Octet 1 7:0 8 bits Length Low (LL) Payload length (low bits)
44         buffer[length++] = 0;
45
46         // Octet 2 7:0 8 bits Class ID (CID) Command class ID
47         buffer[length++] = classId;
48
49         // Octet 3 7:0 8 bits Command ID (CMD) Command ID
50         buffer[length++] = commandId;
51     }
52
53     /**
54      * Adds a uint8 into the output stream
55      *
56      * @param val
57      */
58     protected void serializeUInt8(int val) {
59         buffer[length++] = val & 0xFF;
60     }
61
62     protected void serializeBoolean(boolean val) {
63         buffer[length++] = val ? 1 : 0;
64     }
65
66     /**
67      * Adds a uint16 into the output stream
68      *
69      * @param val
70      */
71     protected void serializeUInt16(int val) {
72         buffer[length++] = val & 0xFF;
73         buffer[length++] = (val >> 8) & 0xFF;
74     }
75
76     /**
77      * Adds a uint32 into the output stream
78      *
79      * @param passkey
80      */
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);
86     }
87
88     protected void serializeUInt8Array(int[] array) {
89         serializeUInt8(array.length);
90
91         for (int val : array) {
92             serializeUInt8(val);
93         }
94     }
95
96     protected void serializeUuid(UUID uuid) {
97         // TODO this probably needs to support longer UUIDs?
98         buffer[length++] = 2;
99         long high = uuid.getMostSignificantBits();
100
101         buffer[length++] = (int) ((high >> 32) & 0xff);
102         buffer[length++] = (int) ((high >> 40) & 0xff);
103     }
104
105     protected void serializeAddress(String address) {
106         String[] bytes = address.split(":");
107         if (bytes.length == 0) {
108             serializeUInt8(0);
109             serializeUInt8(0);
110             serializeUInt8(0);
111             serializeUInt8(0);
112             serializeUInt8(0);
113             serializeUInt8(0);
114
115             return;
116         }
117
118         for (int cnt = 5; cnt >= 0; cnt--) {
119             serializeUInt8(Integer.parseInt(bytes[cnt], 16));
120         }
121     }
122
123     protected void serializeSmpIoCapabilities(SmpIoCapabilities capabilities) {
124         serializeUInt8(capabilities.getKey());
125     }
126
127     protected void serializeBluetoothAddressType(BluetoothAddressType addrType) {
128         serializeUInt8(addrType.getKey());
129     }
130
131     protected void serializeGapDiscoverableMode(GapDiscoverableMode mode) {
132         serializeUInt8(mode.getKey());
133     }
134
135     protected void serializeGapConnectableMode(GapConnectableMode mode) {
136         serializeUInt8(mode.getKey());
137     }
138
139     protected void serializeGapDiscoverMode(GapDiscoverMode mode) {
140         serializeUInt8(mode.getKey());
141     }
142
143     protected int[] getPayload() {
144         buffer[1] = length - 4;
145         return Arrays.copyOfRange(buffer, 0, length);
146     }
147
148     public abstract int[] serialize();
149 }