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