1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
|
/**
* Author: Sven Gothel <sgothel@jausoft.com>
* Copyright (c) 2020 Gothel Software e.K.
* Copyright (c) 2020 ZAFENA AB
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package jau.direct_bt;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Consumer;
import java.util.function.Predicate;
import org.direct_bt.BTAdapter;
import org.direct_bt.BTDevice;
import org.direct_bt.BTException;
import org.direct_bt.BTFactory;
import org.direct_bt.BTGattChar;
import org.direct_bt.BTGattDesc;
import org.direct_bt.BTGattService;
import org.direct_bt.BTManager;
import org.direct_bt.BTObject;
import org.direct_bt.BTType;
import org.direct_bt.HCIStatusCode;
public class DBTManager implements BTManager
{
protected static final boolean DEBUG = BTFactory.DEBUG;
protected static final boolean VERBOSE = BTFactory.VERBOSE;
private static volatile boolean isJVMShuttingDown = false;
private static final List<Runnable> userShutdownHooks = new ArrayList<Runnable>();
private static boolean unifyUUID128Bit = true;
static {
AccessController.doPrivileged(new PrivilegedAction<Object>() {
@Override
public Object run() {
Runtime.getRuntime().addShutdownHook(
new Thread(null, new Runnable() {
@Override
public void run() {
DBTManager.shutdownImpl(true);
} }, "DBTManager_ShutdownHook" ) ) ;
return null;
} } ) ;
}
private static synchronized void shutdownImpl(final boolean _isJVMShuttingDown) {
isJVMShuttingDown = _isJVMShuttingDown;
if(DEBUG) {
System.err.println("DBTManager.shutdown() START: JVM Shutdown "+isJVMShuttingDown+", on thread "+Thread.currentThread().getName());
}
synchronized(userShutdownHooks) {
final int cshCount = userShutdownHooks.size();
for(int i=0; i < cshCount; i++) {
try {
if( DEBUG ) {
System.err.println("DBTManager.shutdown - userShutdownHook #"+(i+1)+"/"+cshCount);
}
userShutdownHooks.get(i).run();
} catch(final Throwable t) {
System.err.println("DBTManager.shutdown: Caught "+t.getClass().getName()+" during userShutdownHook #"+(i+1)+"/"+cshCount);
if( DEBUG ) {
t.printStackTrace();
}
}
}
userShutdownHooks.clear();
}
if(DEBUG) {
System.err.println("DBTManager.shutdown(): Post userShutdownHook");
}
try {
final BTManager mgmt = getManager();
mgmt.shutdown();
} catch(final Throwable t) {
System.err.println("DBTManager.shutdown: Caught "+t.getClass().getName()+" during DBTManager.shutdown()");
if( DEBUG ) {
t.printStackTrace();
}
}
if(DEBUG) {
System.err.println(Thread.currentThread().getName()+" - DBTManager.shutdown() END JVM Shutdown "+isJVMShuttingDown);
}
}
/** Returns true if the JVM is shutting down, otherwise false. */
public static final boolean isJVMShuttingDown() { return isJVMShuttingDown; }
/**
* Add a shutdown hook to be performed at JVM shutdown before shutting down DBTManager instance.
*
* @param head if true add runnable at the start, otherwise at the end
* @param runnable runnable to be added.
*/
public static void addShutdownHook(final boolean head, final Runnable runnable) {
synchronized( userShutdownHooks ) {
if( !userShutdownHooks.contains( runnable ) ) {
if( head ) {
userShutdownHooks.add(0, runnable);
} else {
userShutdownHooks.add( runnable );
}
}
}
}
/**
* Returns whether uuid128_t consolidation is enabled
* for native uuid16_t and uuid32_t values before string conversion.
* @see #setUnifyUUID128Bit(boolean)
*/
public static boolean getUnifyUUID128Bit() { return unifyUUID128Bit; }
/**
* Enables or disables uuid128_t consolidation
* for native uuid16_t and uuid32_t values before string conversion.
* <p>
* Default is {@code true}, as this represent compatibility with original TinyB D-Bus behavior.
* </p>
* <p>
* If desired, this value should be set once before the first call of {@link #getManager()}!
* </p>
* @see #getUnifyUUID128Bit()
*/
public static void setUnifyUUID128Bit(final boolean v) { unifyUUID128Bit=v; }
private long nativeInstance;
private final List<BTAdapter> adapters = new CopyOnWriteArrayList<BTAdapter>();
private final List<ChangedAdapterSetListener> changedAdapterSetListenerList = new CopyOnWriteArrayList<ChangedAdapterSetListener>();
private final Settings settings;
@Override
public final Settings getSettings() { return settings; }
public BTType getBluetoothType() { return BTType.NONE; }
@Override
public DBTObject find(final BTType type, final String name, final String identifier, final BTObject parent, final long timeoutMS) {
return findInCache((DBTObject)parent, type, name, identifier);
}
@Override
public DBTObject find(final BTType type, final String name, final String identifier, final BTObject parent) {
return find(type, name, identifier, parent, 0);
}
@Override
public <T extends BTObject> T find(final String name, final String identifier, final BTObject parent, final long timeoutMS) {
// Due to generic type erasure, we cannot determine the matching BluetoothType for the return parameter,
// hence this orig TinyB API method is rather misleading than useful.
throw new UnsupportedOperationException("Generic return type 'find' won't be implemented.");
// return (T) find(BluetoothType.NONE, name, identifier, parent, timeoutMS);
}
@Override
public <T extends BTObject> T find(final String name, final String identifier, final BTObject parent) {
// Due to generic type erasure, we cannot determine the matching BluetoothType for the return parameter,
// hence this orig TinyB API method is rather misleading than useful.
throw new UnsupportedOperationException("Generic return type 'find' won't be implemented.");
// return (T) find(BluetoothType.NONE, name, identifier, parent, 0);
}
@Override
public BTObject getObject(final BTType type, final String name,
final String identifier, final BTObject parent) {
return getObject(type.ordinal(), name, identifier, parent);
}
private BTObject getObject(final int type, final String name, final String identifier, final BTObject parent)
{ throw new UnsupportedOperationException(); } // FIXME
@Override
public List<BTObject> getObjects(final BTType type, final String name,
final String identifier, final BTObject parent) {
return getObjects(type.ordinal(), name, identifier, parent);
}
private List<BTObject> getObjects(final int type, final String name, final String identifier, final BTObject parent)
{ throw new UnsupportedOperationException(); } // FIXME
@Override
public List<BTAdapter> getAdapters() { return new ArrayList<BTAdapter>(adapters); }
@Override
public BTAdapter getAdapter(final int dev_id) {
for(final Iterator<BTAdapter> iter = adapters.iterator(); iter.hasNext(); ) {
final BTAdapter a = iter.next();
if( dev_id == a.getDevID() ) {
return a;
}
}
return null;
}
@Override
public List<BTDevice> getDevices() { return getDefaultAdapter().getDiscoveredDevices(); }
/**
* {@inheritDoc}
* <p>
* This call could be a quite expensive service query, see below.
* </p>
* <p>
* This implementation returns all {@link BTGattService} from all {@link BTDevice}s
* from the {@link #getDefaultAdapter()} using {@link BTDevice#getServices()}.
* </p>
* <p>
* This implementation does not {@link BTAdapter#startDiscovery() start} an explicit discovery,
* but previous {@link BTAdapter#getDiscoveredDevices() discovered devices} are being queried.
* </p>
*/
@Override
public List<BTGattService> getServices() {
final List<BTGattService> res = new ArrayList<BTGattService>();
for(final Iterator<BTAdapter> iterA=adapters.iterator(); iterA.hasNext(); ) {
final BTAdapter adapter = iterA.next();
final List<BTDevice> devices = adapter.getDiscoveredDevices();
for(final Iterator<BTDevice> iterD=devices.iterator(); iterD.hasNext(); ) {
final BTDevice device = iterD.next();
final List<BTGattService> devServices = device.getServices();
if( null != devServices ) {
res.addAll(devServices);
}
}
}
return res;
}
@Override
public boolean setDefaultAdapter(final BTAdapter adapter) {
return false;
}
@Override
public BTAdapter getDefaultAdapter() {
for(final Iterator<BTAdapter> iter = adapters.iterator(); iter.hasNext(); ) {
final BTAdapter a = iter.next();
if( a.isPowered() ) {
return a;
}
}
return null;
}
@Override
public boolean startDiscovery() throws BTException { return HCIStatusCode.SUCCESS == startDiscovery(true, false); }
@Override
public HCIStatusCode startDiscovery(final boolean keepAlive, final boolean le_scan_active) throws BTException {
return getDefaultAdapter().startDiscovery(keepAlive, le_scan_active);
}
@Override
public HCIStatusCode stopDiscovery() throws BTException { return getDefaultAdapter().stopDiscovery(); }
@SuppressWarnings("deprecation")
@Override
public boolean getDiscovering() throws BTException { return getDefaultAdapter().getDiscovering(); }
@Override
public final void addChangedAdapterSetListener(final ChangedAdapterSetListener l) {
changedAdapterSetListenerList.add(l);
adapters.forEach(new Consumer<BTAdapter>() {
@Override
public void accept(final BTAdapter adapter) {
l.adapterAdded(adapter);
}
});
}
@Override
public final int removeChangedAdapterSetListener(final ChangedAdapterSetListener l) {
// Using removeIf allows performing test on all object and remove within one write-lock cycle
final int count[] = { 0 };
changedAdapterSetListenerList.removeIf(new Predicate<ChangedAdapterSetListener>() {
@Override
public boolean test(final ChangedAdapterSetListener t) {
if( t.equals(l) ) {
count[0]++;
return true;
}
return false;
}
});
return count[0];
}
private native List<BTAdapter> getAdapterListImpl();
private native BTAdapter getAdapterImpl(int dev_id);
/**
* Removal entry for DBTAdapter.close()
* @param adapter ref to the close'ed adapter
* @return true if contained and removed, otherwise false
*/
/* pp */ final boolean removeAdapter(final DBTAdapter adapter) {
if( adapters.remove(adapter) ) {
if( DEBUG ) {
System.err.println("DBTManager.removeAdapter: Removed "+adapter);
}
return true;
} else {
if( DEBUG ) {
System.err.println("DBTManager.removeAdapter: Not found "+adapter);
}
return false;
}
}
/** callback from native adapter remove */
/* pp */ final void removeAdapterCB(final int dev_id, final int opc_reason) {
final BTAdapter[] removed = { null };
final int count[] = { 0 };
adapters.removeIf(new Predicate<BTAdapter>() {
@Override
public boolean test(final BTAdapter a) {
if( 0 == count[0] && dev_id == a.getDevID() ) {
removed[0] = a;
count[0]++;
return true;
} else {
return false;
}
}
});
if( null != removed[0] ) {
if( DEBUG ) {
System.err.println("DBTManager.removeAdapterCB[dev_id "+dev_id+", opc 0x"+Integer.toHexString(opc_reason)+
"]: removing "+removed[0].toString());
}
if( 0x0005 == opc_reason ) {
// MgmtEvent::Opcode::INDEX_REMOVED = 0x0005
changedAdapterSetListenerList.forEach(new Consumer<ChangedAdapterSetListener>() {
@Override
public void accept(final ChangedAdapterSetListener t) {
t.adapterRemoved( removed[0] );
} } );
}
removed[0] = null; // DBTAdapter::close() issued by DBTManager after all callbacks
}
if( DEBUG ) {
System.err.println("DBTManager.removeAdapterCB[dev_id "+dev_id+", opc 0x"+Integer.toHexString(opc_reason)+
"]: removed "+count[0]+" adapter, size "+adapters.size());
}
}
/** callback from native adapter add or POWERED on */
private final void updatedAdapterCB(final int dev_id, final int opc_reason) {
final BTAdapter preInstance = getAdapter(dev_id);
if( null != preInstance ) {
if( DEBUG ) {
System.err.println("DBTManager.updatedAdapterCB[dev_id "+dev_id+", opc 0x"+Integer.toHexString(opc_reason)+
"]: existing "+preInstance.toString()+", size "+adapters.size());
}
return;
}
final BTAdapter newInstance = getAdapterImpl(dev_id);
if( null == newInstance ) {
if( DEBUG ) {
System.err.println("DBTManager.updatedAdapterCB[dev_id "+dev_id+", opc 0x"+Integer.toHexString(opc_reason)+
"]: Adapter not found, size "+adapters.size());
}
return;
}
final boolean added = adapters.add(newInstance);
if( DEBUG ) {
System.err.println("DBTManager.updatedAdapterCB[dev_id "+dev_id+", opc 0x"+Integer.toHexString(opc_reason)+
"]: added "+added+": new "+newInstance.toString()+", size "+adapters.size());
}
if( added && 0x0004 == opc_reason ) {
// MgmtEvent::Opcode::INDEX_ADDED = 0x0004
changedAdapterSetListenerList.forEach(new Consumer<ChangedAdapterSetListener>() {
@Override
public void accept(final ChangedAdapterSetListener t) {
t.adapterAdded(newInstance);
} } );
}
}
private native void initImpl(final boolean unifyUUID128Bit, final int btMode) throws BTException;
private native void deleteImpl(long nativeInstance);
private DBTManager()
{
initImpl(unifyUUID128Bit, BTFactory.DEFAULT_BTMODE.value);
try {
adapters.addAll(getAdapterListImpl());
} catch (final BTException be) {
be.printStackTrace();
}
final boolean supCharValCacheNotify;
{
final String v = System.getProperty("jau.direct_bt.characteristic.compat", "false");
supCharValCacheNotify = Boolean.valueOf(v);
}
settings = new Settings() {
@Override
public final boolean isDirectBT() {
return true;
}
@Override
public boolean isTinyB() {
return false;
}
@Override
public boolean isCharValueCacheNotificationSupported() {
return supCharValCacheNotify;
}
@Override
public String toString() {
return "Settings[dbt true, tinyb false, charValueCacheNotify "+isCharValueCacheNotificationSupported()+"]";
}
};
}
/** Returns an instance of BluetoothManager, to be used instead of constructor.
* @return An initialized BluetoothManager instance.
*/
public static BTManager getManager() throws RuntimeException, BTException {
return LazySingletonHolder.singleton;
}
/** Initialize-On-Demand Holder Class, similar to C++11's "Magic Statics". */
private static class LazySingletonHolder {
private static final DBTManager singleton = new DBTManager();
}
@Override
protected void finalize() {
shutdown();
}
@Override
public void shutdown() {
for(final Iterator<BTAdapter> ia= adapters.iterator(); ia.hasNext(); ) {
final DBTAdapter a = (DBTAdapter)ia.next();
a.close();
}
adapters.clear();
deleteImpl(nativeInstance);
}
/**
* Returns the matching {@link DBTObject} from the internal cache if found,
* otherwise {@code null}.
* <p>
* The returned {@link DBTObject} may be of type
* <ul>
* <li>{@link DBTAdapter}</li>
* <li>{@link DBTDevice}</li>
* <li>{@link DBTGattService}</li>
* <li>{@link DBTGattChar}</li>
* <li>{@link DBTGattDesc}</li>
* </ul>
* or alternatively in {@link BTObject} space
* <ul>
* <li>{@link BTType#ADAPTER} -> {@link BTAdapter}</li>
* <li>{@link BTType#DEVICE} -> {@link BTDevice}</li>
* <li>{@link BTType#GATT_SERVICE} -> {@link BTGattService}</li>
* <li>{@link BTType#GATT_CHARACTERISTIC} -> {@link BTGattChar}</li>
* <li>{@link BTType#GATT_DESCRIPTOR} -> {@link BTGattDesc}</li>
* </ul>
* </p>
* @param name name of the desired {@link BTType#ADAPTER adapter} or {@link BTType#DEVICE device}.
* Maybe {@code null}.
* @param identifier EUI48 address of the desired {@link BTType#ADAPTER adapter} or {@link BTType#DEVICE device}
* or UUID of the desired {@link BTType#GATT_SERVICE service},
* {@link BTType#GATT_CHARACTERISTIC characteristic} or {@link BTType#GATT_DESCRIPTOR descriptor} to be found.
* Maybe {@code null}, in which case the first object of the desired type is being returned - if existing.
* @param type specify the type of the object to be found, either
* {@link BTType#ADAPTER adapter}, {@link BTType#DEVICE device},
* {@link BTType#GATT_SERVICE service}, {@link BTType#GATT_CHARACTERISTIC characteristic}
* or {@link BTType#GATT_DESCRIPTOR descriptor}.
* {@link BTType#NONE none} means anything.
*/
/* pp */ DBTObject findInCache(final String name, final String identifier, final BTType type) {
final boolean anyType = BTType.NONE == type;
final boolean adapterType = BTType.ADAPTER == type;
if( null == name && null == identifier && ( anyType || adapterType ) ) {
// special case for 1st valid adapter
if( adapters.size() > 0 ) {
return (DBTAdapter) adapters.get(0);
}
return null; // no adapter
}
for(final Iterator<BTAdapter> iter = adapters.iterator(); iter.hasNext(); ) {
final DBTAdapter adapter = (DBTAdapter) iter.next();
if( !adapter.isValid() ) {
continue;
}
if( ( anyType || adapterType ) ) {
if( null != name && null != identifier &&
adapter.getName().equals(name) &&
adapter.getAddressString().equals(identifier)
)
{
return adapter;
}
if( null != identifier &&
adapter.getAddressString().equals(identifier)
)
{
return adapter;
}
if( null != name &&
adapter.getName().equals(name)
)
{
return adapter;
}
}
final DBTObject dbtObj = adapter.findInCache(name, identifier, type);
if( null != dbtObj ) {
return dbtObj;
}
}
return null;
}
/* pp */ DBTObject findInCache(final DBTObject parent, final BTType type, final String name, final String identifier) {
if( null == parent ) {
return findInCache(name, identifier, type);
}
final boolean anyType = BTType.NONE == type;
final boolean deviceType = BTType.DEVICE == type;
final boolean serviceType = BTType.GATT_SERVICE == type;
final boolean charType = BTType.GATT_CHARACTERISTIC== type;
final boolean descType = BTType.GATT_DESCRIPTOR == type;
final BTType parentType = parent.getBluetoothType();
if( BTType.ADAPTER == parentType &&
( anyType || deviceType || serviceType || charType || descType )
)
{
return ((DBTAdapter) parent).findInCache(name, identifier, type);
}
if( BTType.DEVICE == parentType &&
( anyType || serviceType || charType || descType )
)
{
return ((DBTDevice) parent).findInCache(identifier, type);
}
if( BTType.GATT_SERVICE == parentType &&
( anyType || charType || descType )
)
{
final DBTGattService service = (DBTGattService) parent;
if( !service.checkServiceCache() ) {
return null;
}
return service.findInCache(identifier, type);
}
if( BTType.GATT_CHARACTERISTIC == parentType &&
( anyType || descType )
)
{
final DBTGattChar characteristic = (DBTGattChar) parent;
if( !characteristic.checkServiceCache() ) {
return null;
}
return characteristic.findInCache(identifier, type);
}
return null;
}
}
|