]> git.basschouten.com Git - openhab-addons.git/blob
eb0d2033eb7e2a29b5e49ff1936ceb364d9dcae0
[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.command.gap;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.binding.bluetooth.bluegiga.internal.BlueGigaCommand;
17 import org.openhab.binding.bluetooth.bluegiga.internal.enumeration.BluetoothAddressType;
18
19 /**
20  * Class to implement the BlueGiga command <b>connectDirect</b>.
21  * <p>
22  * This command will start the GAP direct connection establishment procedure to a dedicated
23  * Smart Bluetooth device. The module will enter a state where it continuously scans for the
24  * connectable advertisement packets Bluetooth from the remote device which matches the
25  * Bluetooth address gives as a parameter. Upon receiving the advertisement packet, the
26  * module will send a connection request packet to the target device to imitate a Bluetooth
27  * connection. A successful connection will be indicated by an event. Status If the device is
28  * configured to support more than one connection, the smallest connection interval which is
29  * divisible by maximum_connections * 2.5ms will be selected. Thus, it is important to provide
30  * minimum and maximum connection intervals so that such a connection interval is available
31  * within the range. The connection establishment procedure can be cancelled with End
32  * Procedure command.
33  * <p>
34  * This class provides methods for processing BlueGiga API commands.
35  * <p>
36  * Note that this code is autogenerated. Manual changes may be overwritten.
37  *
38  * @author Chris Jackson - Initial contribution of Java code generator
39  * @author Pauli Anttila - Added message builder
40  */
41 @NonNullByDefault
42 public class BlueGigaConnectDirectCommand extends BlueGigaCommand {
43     public static final int COMMAND_CLASS = 0x06;
44     public static final int COMMAND_METHOD = 0x03;
45
46     private BlueGigaConnectDirectCommand(CommandBuilder builder) {
47         this.address = builder.address;
48         this.addrType = builder.addrType;
49         this.connIntervalMin = builder.connIntervalMin;
50         this.connIntervalMax = builder.connIntervalMax;
51         this.timeout = builder.timeout;
52         this.latency = builder.latency;
53     }
54
55     /**
56      * Bluetooth address of the target device.
57      * <p>
58      * BlueGiga API type is <i>bd_addr</i> - Java type is {@link String}
59      */
60     private String address;
61
62     /**
63      * see: Bluetooth Address Types.
64      * <p>
65      * BlueGiga API type is <i>BluetoothAddressType</i> - Java type is {@link BluetoothAddressType}
66      */
67     private BluetoothAddressType addrType;
68
69     /**
70      * Minimum Connection Interval (in units of 1.25ms). Range: 6 - 3200 The lowest possible
71      * Connection Interval is 7.50ms and the largest is 4000ms.
72      * <p>
73      * BlueGiga API type is <i>uint16</i> - Java type is {@link int}
74      */
75     private int connIntervalMin;
76
77     /**
78      * Maximum Connection Interval (in units of 1.25ms). Range: 6 - 3200 Must be equal or bigger than
79      * minimum Connection Interval.
80      * <p>
81      * BlueGiga API type is <i>uint16</i> - Java type is {@link int}
82      */
83     private int connIntervalMax;
84
85     /**
86      * Supervision Timeout (in units of 10ms). The Supervision Timeout defines how long the
87      * devices can be out of range before the connection is closed. Range: 10 - 3200 Minimum time for
88      * the Supervision Timeout is 100ms and maximum value is 32000ms. According to the
89      * specification, the Supervision Timeout in milliseconds shall be larger than (1 + latency) *
90      * conn_interval_max * 2, where conn_interval_max is given in milliseconds.
91      * <p>
92      * BlueGiga API type is <i>uint16</i> - Java type is {@link int}
93      */
94     private int timeout;
95
96     /**
97      * This parameter configures the slave latency. Slave latency defines how many connection
98      * intervals a slave device can skip. Increasing slave latency will decrease the energy
99      * consumption of the slave in scenarios where slave does not have data to send at every
100      * connection interval. Range: 0 - 500 0 : Slave latency is disabled. Example: Connection
101      * interval is 10ms and slave latency is 9: this means that the slave is allowed to communicate
102      * every 100ms, but it can communicate every 10ms if needed.
103      * <p>
104      * BlueGiga API type is <i>uint16</i> - Java type is {@link int}
105      */
106     private int latency;
107
108     @Override
109     public int[] serialize() {
110         // Serialize the header
111         serializeHeader(COMMAND_CLASS, COMMAND_METHOD);
112
113         // Serialize the fields
114         serializeAddress(address);
115         serializeBluetoothAddressType(addrType);
116         serializeUInt16(connIntervalMin);
117         serializeUInt16(connIntervalMax);
118         serializeUInt16(timeout);
119         serializeUInt16(latency);
120
121         return getPayload();
122     }
123
124     @Override
125     public String toString() {
126         final StringBuilder builder = new StringBuilder();
127         builder.append("BlueGigaConnectDirectCommand [address=");
128         builder.append(address);
129         builder.append(", addrType=");
130         builder.append(addrType);
131         builder.append(", connIntervalMin=");
132         builder.append(connIntervalMin);
133         builder.append(", connIntervalMax=");
134         builder.append(connIntervalMax);
135         builder.append(", timeout=");
136         builder.append(timeout);
137         builder.append(", latency=");
138         builder.append(latency);
139         builder.append(']');
140         return builder.toString();
141     }
142
143     public static class CommandBuilder {
144         private String address = "";
145         private BluetoothAddressType addrType = BluetoothAddressType.UNKNOWN;
146         private int connIntervalMin;
147         private int connIntervalMax;
148         private int timeout;
149         private int latency;
150
151         /**
152          * Bluetooth address of the target device.
153          *
154          * @param address the address to set as {@link String}
155          */
156         public CommandBuilder withAddress(String address) {
157             this.address = address;
158             return this;
159         }
160
161         /**
162          * see: Bluetooth Address Types.
163          *
164          * @param addrType the addrType to set as {@link BluetoothAddressType}
165          */
166         public CommandBuilder withAddrType(BluetoothAddressType addrType) {
167             this.addrType = addrType;
168             return this;
169         }
170
171         /**
172          * Minimum Connection Interval (in units of 1.25ms). Range: 6 - 3200 The lowest possible
173          * Connection Interval is 7.50ms and the largest is 4000ms.
174          *
175          * @param connIntervalMin the connIntervalMin to set as {@link int}
176          */
177         public CommandBuilder withConnIntervalMin(int connIntervalMin) {
178             this.connIntervalMin = connIntervalMin;
179             return this;
180         }
181
182         /**
183          * Maximum Connection Interval (in units of 1.25ms). Range: 6 - 3200 Must be equal or bigger than
184          * minimum Connection Interval.
185          *
186          * @param connIntervalMax the connIntervalMax to set as {@link int}
187          */
188         public CommandBuilder withConnIntervalMax(int connIntervalMax) {
189             this.connIntervalMax = connIntervalMax;
190             return this;
191         }
192
193         /**
194          * Supervision Timeout (in units of 10ms). The Supervision Timeout defines how long the
195          * devices can be out of range before the connection is closed. Range: 10 - 3200 Minimum time for
196          * the Supervision Timeout is 100ms and maximum value is 32000ms. According to the
197          * specification, the Supervision Timeout in milliseconds shall be larger than (1 + latency) *
198          * conn_interval_max * 2, where conn_interval_max is given in milliseconds.
199          *
200          * @param timeout the timeout to set as {@link int}
201          */
202         public CommandBuilder withTimeout(int timeout) {
203             this.timeout = timeout;
204             return this;
205         }
206
207         /**
208          * This parameter configures the slave latency. Slave latency defines how many connection
209          * intervals a slave device can skip. Increasing slave latency will decrease the energy
210          * consumption of the slave in scenarios where slave does not have data to send at every
211          * connection interval. Range: 0 - 500 0 : Slave latency is disabled. Example: Connection
212          * interval is 10ms and slave latency is 9: this means that the slave is allowed to communicate
213          * every 100ms, but it can communicate every 10ms if needed.
214          *
215          * @param latency the latency to set as {@link int}
216          */
217         public CommandBuilder withLatency(int latency) {
218             this.latency = latency;
219             return this;
220         }
221
222         public BlueGigaConnectDirectCommand build() {
223             return new BlueGigaConnectDirectCommand(this);
224         }
225     }
226 }