Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
AdapterSettings.java
Go to the documentation of this file.
1/*! \package org.direct_bt
2 * - - - - - - - - - - - - - - -
3 * # Direct-BT Overview
4 *
5 * Direct-BT provides direct Bluetooth LE and BREDR programming,
6 * offering robust high-performance support for embedded & desktop with zero overhead via C++ and Java.
7 *
8 * Direct-BT follows the official [Bluetooth Specification](https://www.bluetooth.com/specifications/bluetooth-core-specification/)
9 * and its C++ implementation contains detailed references.
10 *
11 * Direct-BT supports a fully event driven workflow from adapter management via device discovery to GATT programming,
12 * using its platform agnostic HCI, GATT, SMP and L2CAP client-side protocol implementation.
13 *
14 * - - - - - - - - - - - - - - -
15 *
16 * ## Direct-BT Layers
17 *
18 * Direct-BT implements the following layers
19 * - BTManager for adapter configuration and adapter add/removal notifications (BTManager::ChangedAdapterSetListener)
20 * - Using *BlueZ Kernel Manager Control Channel* via MgmtMsg communication.
21 * - *HCI Handling* via HCIHandler using HCIPacket implementing connect/disconnect w/ tracking, device discovery, etc
22 * - *ATT PDU* AttPDUMsg via L2CAP for low level packet communication
23 * - *GATT Support* via BTGattHandler using AttPDUMsg over L2CAPComm, ...
24 * - Central-Client Functionality, i.e. GATT client role or BTAdapter in {@link BTRole#Master}:
25 * - BTGattService
26 * - BTGattChar
27 * - BTGattDesc
28 * - Peripheral-Server Functionality, i.e. GATT server role or BTAdapter in {@link BTRole#Slave}:
29 * - DBGattServer
30 * - DBGattService
31 * - DBGattChar
32 * - DBGattDesc
33 * - *SMP PDU* SMPPDUMsg via L2CAP for Security Manager Protocol (SMP) communication
34 * - *SMP Support* via SMPHandler using SMPPDUMsg over L2CAPComm, providing (Not yet supported by Linux/BlueZ)
35 * - LE Secure Connections
36 * - LE legacy pairing
37 * - On Linux/BlueZ, LE Secure Connections and LE legacy pairing is supported using
38 * - BTSecurityLevel setting via BTDevice / L2CAPComm per connection and
39 * - SMPIOCapability via BTManager (per adapter) and BTDevice (per connection)
40 * - SMPPDUMsg SMP event tracking over HCI/ACL/L2CAP, observing operations
41 *
42 * BTManager utilizes the *BlueZ Kernel Manager Control Channel*
43 * for adapter configuration and adapter add/removal notifications (ChangedAdapterSetFunc()).
44 *
45 * To support other platforms than Linux/BlueZ, we will have to
46 * - Move specified HCI host features used in BTManager to HCIHandler, SMPHandler,.. - and -
47 * - Add specialization for each new platform using their non-platform-agnostic features.
48 *
49 * - - - - - - - - - - - - - - -
50 *
51 * ## Direct-BT User Hierarchy
52 *
53 * From a user central-client perspective the following hierarchy is provided,
54 * i.e. GATT client role or BTAdapter in {@link BTRole#Master}:
55 * - BTManager has zero or more
56 * - BTAdapter has zero or more
57 * - BTDevice has zero or more
58 * - BTGattService has zero or more
59 * - BTGattChar has zero or more
60 * - BTGattDesc
61 *
62 * From a user peripheral-server perspective the following hierarchy is provided,
63 * i.e. GATT server role or BTAdapter in {@link BTRole#Slave}:
64 * - BTManager has zero or more
65 * - BTAdapter has zero or one
66 * - DBGattServer has zero or more
67 * - DBGattService has zero or more
68 * - DBGattChar has zero or more
69 * - DBGattDesc
70 *
71 * - - - - - - - - - - - - - - -
72 *
73 * ## Direct-BT Object Lifecycle
74 *
75 * Object lifecycle with all instances and marked weak back-references to their owner
76 * - BTManager singleton instance for all
77 * - BTAdapter ownership by DBTManager
78 * - BTDevice ownership by DBTAdapter
79 * - BTGattHandler ownership by BTDevice, with weak BTDevice back-reference
80 * - BTGattService ownership by BTGattHandler, with weak BTGattHandler back-reference
81 * - BTGattChar ownership by BTGattService, with weak BTGattService back-reference
82 * - BTGattDesc ownership by BTGattChar, with weak BTGattChar back-reference
83 *
84 * User application instantiates for peripheral-server functionality:
85 * - DBGattServer ownership by user
86 * - DBGattService ownership by user
87 * - DBGattChar ownership by user
88 * - DBGattDesc
89 *
90 * - - - - - - - - - - - - - - -
91 *
92 * ## Direct-BT Mapped Names C++ vs Java
93 *
94 * Mapped names from C++ implementation to Java implementation and to Java interface:
95 *
96 * C++ <br> `direct_bt` | Java Implementation <br> `jau.direct_bt` | Java Interface <br> `org.direct_bt` |
97 * :-----------------------| :-----------------| :------------------------------------|
98 * BTManager | DBTManager | BTManager |
99 * BTAdapter | DBTAdapter | BTAdapter |
100 * BTDevice | DBTDevice | BTDevice |
101 * BTGattService | DBTGattService | BTGattService |
102 * BTGattChar | DBTGattChar | BTGattChar |
103 * BTGattDesc | DBTGattDesc | BTGattDesc |
104 * DBGattService | | DBGattService |
105 * DBGattChar | | DBGattChar |
106 * DBGattDesc | | DBGattDesc |
107 * AdapterStatusListener | | AdapterStatusListener |
108 * BTGattCharListener | | BTGattCharListener |
109 * ChangedAdapterSetFunc() | | BTManager::ChangedAdapterSetListener |
110 *
111 * - - - - - - - - - - - - - - -
112 *
113 * ## Direct-BT Event Driven Workflow
114 *
115 * A fully event driven workflow from adapter management via device discovery to GATT programming is supported.
116 *
117 * BTManager::ChangedAdapterSetListener allows listening to added and removed BTAdapter via BTManager.
118 *
119 * AdapterStatusListener allows listening to BTAdapter changes and BTDevice discovery.
120 *
121 * BTGattCharListener allows listening to GATT indications and notifications.
122 *
123 * Main event listener can be attached to these objects
124 * which maintain a set of unique listener instances without duplicates.
125 *
126 * - BTManager
127 * - BTManager::ChangedAdapterSetListener
128 *
129 * - BTAdapter
130 * - AdapterStatusListener
131 *
132 * - BTGattChar or BTDevice
133 * - BTGattCharListener
134 *
135 * Other API attachment method exists for BTGattCharListener,
136 * however, they only exists for convenience and end up to be attached to BTGattHandler.
137 */
138package org.direct_bt;
139
140/**
141 * Author: Sven Gothel <sgothel@jausoft.com>
142 * Copyright (c) 2020 Gothel Software e.K.
143 * Copyright (c) 2020 ZAFENA AB
144 *
145 * Permission is hereby granted, free of charge, to any person obtaining
146 * a copy of this software and associated documentation files (the
147 * "Software"), to deal in the Software without restriction, including
148 * without limitation the rights to use, copy, modify, merge, publish,
149 * distribute, sublicense, and/or sell copies of the Software, and to
150 * permit persons to whom the Software is furnished to do so, subject to
151 * the following conditions:
152 *
153 * The above copyright notice and this permission notice shall be
154 * included in all copies or substantial portions of the Software.
155 *
156 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
157 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
158 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
159 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
160 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
161 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
162 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
163 */
164
165/**
166 * Bit mask of '{@link BTAdapter} setting' data fields,
167 * indicating a set of related data.
168 *
169 * @since 2.0.0
170 */
171public class AdapterSettings {
172
173 /**
174 * Bits representing '{@link BTAdapter} setting' data fields.
175 *
176 * @since 2.0.0
177 */
178 public enum SettingType {
179 NONE ( 0),
180 POWERED (0x00000001),
181 CONNECTABLE (0x00000002),
182 FAST_CONNECTABLE (0x00000004),
183 DISCOVERABLE (0x00000008),
184 BONDABLE (0x00000010),
185 LINK_SECURITY (0x00000020),
186 SSP (0x00000040),
187 BREDR (0x00000080),
188 HS (0x00000100),
189 LE (0x00000200),
190 ADVERTISING (0x00000400),
191 SECURE_CONN (0x00000800),
192 DEBUG_KEYS (0x00001000),
193 PRIVACY (0x00002000),
194 CONFIGURATION (0x00004000),
195 STATIC_ADDRESS (0x00008000),
196 PHY_CONFIGURATION (0x00010000);
197
198 SettingType(final int v) { value = v; }
199 public final int value;
200 }
201
202 public int mask;
203
204 public AdapterSettings(final int v) {
205 mask = v;
206 }
207
208 public boolean isEmpty() { return 0 == mask; }
209 public boolean isSet(final SettingType bit) { return 0 != ( mask & bit.value ); }
210 public void set(final SettingType bit) { mask = mask | bit.value; }
211
212 @Override
213 public String toString() {
214 int count = 0;
215 final StringBuilder out = new StringBuilder();
216 if( isSet(SettingType.POWERED) ) {
217 out.append(SettingType.POWERED.name()); count++;
218 }
220 if( 0 < count ) { out.append(", "); }
221 out.append(SettingType.CONNECTABLE.name()); count++;
222 }
224 if( 0 < count ) { out.append(", "); }
225 out.append(SettingType.FAST_CONNECTABLE.name()); count++;
226 }
228 if( 0 < count ) { out.append(", "); }
229 out.append(SettingType.DISCOVERABLE.name()); count++;
230 }
231 if( isSet(SettingType.BONDABLE) ) {
232 if( 0 < count ) { out.append(", "); }
233 out.append(SettingType.BONDABLE.name()); count++;
234 }
236 if( 0 < count ) { out.append(", "); }
237 out.append(SettingType.LINK_SECURITY.name()); count++;
238 }
239 if( isSet(SettingType.SSP) ) {
240 if( 0 < count ) { out.append(", "); }
241 out.append(SettingType.SSP.name()); count++;
242 }
243 if( isSet(SettingType.BREDR) ) {
244 if( 0 < count ) { out.append(", "); }
245 out.append(SettingType.BREDR.name()); count++;
246 }
247 if( isSet(SettingType.HS) ) {
248 if( 0 < count ) { out.append(", "); }
249 out.append(SettingType.HS.name()); count++;
250 }
251 if( isSet(SettingType.LE) ) {
252 if( 0 < count ) { out.append(", "); }
253 out.append(SettingType.LE.name()); count++;
254 }
256 if( 0 < count ) { out.append(", "); }
257 out.append(SettingType.ADVERTISING.name()); count++;
258 }
260 if( 0 < count ) { out.append(", "); }
261 out.append(SettingType.SECURE_CONN.name()); count++;
262 }
264 if( 0 < count ) { out.append(", "); }
265 out.append(SettingType.DEBUG_KEYS.name()); count++;
266 }
267 if( isSet(SettingType.PRIVACY) ) {
268 if( 0 < count ) { out.append(", "); }
269 out.append(SettingType.PRIVACY.name()); count++;
270 }
272 if( 0 < count ) { out.append(", "); }
273 out.append(SettingType.CONFIGURATION.name()); count++;
274 }
276 if( 0 < count ) { out.append(", "); }
277 out.append(SettingType.STATIC_ADDRESS.name()); count++;
278 }
280 if( 0 < count ) { out.append(", "); }
281 out.append(SettingType.PHY_CONFIGURATION.name()); count++;
282 }
283 return "["+out.toString()+"]";
284 }
285}
Author: Sven Gothel sgothel@jausoft.com Copyright (c) 2020 Gothel Software e.K.
boolean isSet(final SettingType bit)
Bits representing 'BTAdapter setting' data fields.