summaryrefslogtreecommitdiffstats
path: root/examples/java/DBTPeripheral00.java
blob: df4ac622767b5b159c73c25059821e34f585bf51 (plain)
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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
/**
 * Author: Sven Gothel <sgothel@jausoft.com>
 * Copyright (c) 2021 Gothel Software e.K.
 * Copyright (c) 2021 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.
 */

import java.lang.reflect.InvocationTargetException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import org.direct_bt.AdapterSettings;
import org.direct_bt.AdapterStatusListener;
import org.direct_bt.BDAddressAndType;
import org.direct_bt.BTMode;
import org.direct_bt.BTSecurityLevel;
import org.direct_bt.BTAdapter;
import org.direct_bt.BTDevice;
import org.direct_bt.BTDeviceRegistry;
import org.direct_bt.BTException;
import org.direct_bt.BTFactory;
import org.direct_bt.BTManager;
import org.direct_bt.BTSecurityRegistry;
import org.direct_bt.BTUtils;
import org.direct_bt.DBGattChar;
import org.direct_bt.DBGattDesc;
import org.direct_bt.DBGattServer;
import org.direct_bt.DBGattService;
import org.direct_bt.DBGattValue;
import org.direct_bt.DiscoveryPolicy;
import org.direct_bt.EIRDataTypeSet;
import org.direct_bt.EInfoReport;
import org.direct_bt.GAPFlags;
import org.direct_bt.GattCharPropertySet;
import org.direct_bt.HCIStatusCode;
import org.direct_bt.LE_Features;
import org.direct_bt.LE_PHYs;
import org.direct_bt.PairingMode;
import org.direct_bt.SMPIOCapability;
import org.direct_bt.SMPPairingState;
import org.direct_bt.ScanType;
import org.jau.net.EUI48;

/**
 * This Java peripheral {@link BTRole::Slave} GATT server example uses an event driven workflow.
 * <p>
 * See `dbt_peripheral00.cpp` for invocation examples, since both apps are fully compatible.
 * </p>
 */
public class DBTPeripheral00 {
    long timestamp_t0;

    EUI48 useAdapter = EUI48.ALL_DEVICE;
    BTMode btMode = BTMode.DUAL;
    BTAdapter chosenAdapter = null;

    boolean use_SC = true;
    String adapter_name = "TestDev001_J";
    String adapter_short_name = "TDev001J";
    BTSecurityLevel adapter_sec_level = BTSecurityLevel.UNSET;
    boolean SHOW_UPDATE_EVENTS = false;
    boolean RUN_ONLY_ONCE = false;
    private final Object sync_lock = new Object();
    private volatile BTDevice connectedDevice;

    volatile int servedConnections = 0;

    private final void setDevice(final BTDevice cd) {
        synchronized( sync_lock ) {
            connectedDevice = cd;
        }
    }

    private final BTDevice getDevice() {
        synchronized( sync_lock ) {
            return connectedDevice;
        }
    }

    private boolean matches(final BTDevice device) {
        final BTDevice d = getDevice();
        return null != d ? d.equals(device) : false;
    }
    boolean matches(final List<BDAddressAndType> cont, final BDAddressAndType mac) {
        for(final Iterator<BDAddressAndType> it = cont.iterator(); it.hasNext(); ) {
            if( it.next().matches(mac) ) {
                return true;
            }
        }
        return false;
    }

    static Thread executeOffThread(final Runnable runobj, final String threadName, final boolean detach) {
        final Thread t = new Thread( runobj, threadName );
        if( detach ) {
            t.setDaemon(true); // detach thread
        }
        t.start();
        return t;
    }
    static Thread executeOffThread(final Runnable runobj, final boolean detach) {
        final Thread t = new Thread( runobj );
        if( detach ) {
            t.setDaemon(true); // detach thread
        }
        t.start();
        return t;
    }

    static DBGattValue make_gvalue(final String name) {
        final byte[] p = name.getBytes(StandardCharsets.UTF_8);
        return new DBGattValue(p, p.length);
    }
    static DBGattValue make_gvalue(final String name, final int capacity) {
        final byte[] p = name.getBytes(StandardCharsets.UTF_8);
        return new DBGattValue(p, Math.max(capacity, p.length), capacity > p.length/* variable_length */);
    }
    static DBGattValue make_gvalue(final short v) {
        final byte[] p = { (byte)0, (byte)0 };
        p[0] = (byte)(v);
        p[1] = (byte)(v >> 8);
        return new DBGattValue(p, p.length);
    }
    static DBGattValue make_gvalue(final int capacity, final int size) {
        final byte[] p = new byte[size];
        Arrays.fill(p, (byte)0);
        return new DBGattValue(p, capacity, true /* variable_length */);
    }

    static String DataServiceUUID = "d0ca6bf3-3d50-4760-98e5-fc5883e93712";
    static String StaticDataUUID  = "d0ca6bf3-3d51-4760-98e5-fc5883e93712";
    static String CommandUUID     = "d0ca6bf3-3d52-4760-98e5-fc5883e93712";
    static String ResponseUUID    = "d0ca6bf3-3d53-4760-98e5-fc5883e93712";
    static String PulseDataUUID   = "d0ca6bf3-3d54-4760-98e5-fc5883e93712";

    // DBGattServerRef dbGattServer = std::make_shared<DBGattServer>(
    private final DBGattServer dbGattServer = new DBGattServer(
            /* services: */
            Arrays.asList( // DBGattService
              new DBGattService ( true /* primary */,
                  DBGattService.UUID16.GENERIC_ACCESS /* type_ */,
                  Arrays.asList( // DBGattChar
                      new DBGattChar( DBGattChar.UUID16.DEVICE_NAME /* value_type_ */,
                                  new GattCharPropertySet(GattCharPropertySet.Type.Read),
                                  new ArrayList<DBGattDesc>(/* intentionally w/o Desc */ ),
                                  make_gvalue(adapter_name, 128) /* value */ ),
                      new DBGattChar( DBGattChar.UUID16.APPEARANCE /* value_type_ */,
                                  new GattCharPropertySet(GattCharPropertySet.Type.Read),
                                  new ArrayList<DBGattDesc>(/* intentionally w/o Desc */ ),
                                  make_gvalue((short)0) /* value */ )
                  ) ),
              new DBGattService ( true /* primary */,
                  DBGattService.UUID16.DEVICE_INFORMATION /* type_ */,
                  Arrays.asList( // DBGattChar
                      new DBGattChar( DBGattChar.UUID16.MANUFACTURER_NAME_STRING /* value_type_ */,
                                  new GattCharPropertySet(GattCharPropertySet.Type.Read),
                                  new ArrayList<DBGattDesc>(/* intentionally w/o Desc */ ),
                                  make_gvalue("Gothel Software") /* value */ ),
                      new DBGattChar( DBGattChar.UUID16.MODEL_NUMBER_STRING /* value_type_ */,
                                  new GattCharPropertySet(GattCharPropertySet.Type.Read),
                                  new ArrayList<DBGattDesc>(/* intentionally w/o Desc */ ),
                                  make_gvalue("2.4.0-pre") /* value */ ),
                      new DBGattChar( DBGattChar.UUID16.SERIAL_NUMBER_STRING /* value_type_ */,
                                  new GattCharPropertySet(GattCharPropertySet.Type.Read),
                                  new ArrayList<DBGattDesc>(/* intentionally w/o Desc */ ),
                                  make_gvalue("sn:0123456789") /* value */ ),
                      new DBGattChar( DBGattChar.UUID16.HARDWARE_REVISION_STRING /* value_type_ */,
                                  new GattCharPropertySet(GattCharPropertySet.Type.Read),
                                  new ArrayList<DBGattDesc>(/* intentionally w/o Desc */ ),
                                  make_gvalue("hw:0123456789") /* value */ ),
                      new DBGattChar( DBGattChar.UUID16.FIRMWARE_REVISION_STRING /* value_type_ */,
                                  new GattCharPropertySet(GattCharPropertySet.Type.Read),
                                  new ArrayList<DBGattDesc>(/* intentionally w/o Desc */ ),
                                  make_gvalue("fw:0123456789") /* value */ ),
                      new DBGattChar( DBGattChar.UUID16.SOFTWARE_REVISION_STRING /* value_type_ */,
                                  new GattCharPropertySet(GattCharPropertySet.Type.Read),
                                  new ArrayList<DBGattDesc>(/* intentionally w/o Desc */ ),
                                  make_gvalue("sw:0123456789") /* value */ )
                  ) ),
              new DBGattService ( true /* primary */,
                  DataServiceUUID /* type_ */,
                  Arrays.asList( // DBGattChar
                      new DBGattChar( StaticDataUUID /* value_type_ */,
                                  new GattCharPropertySet(GattCharPropertySet.Type.Read),
                                  Arrays.asList( // DBGattDesc
                                      new DBGattDesc( DBGattDesc.UUID16.USER_DESC, make_gvalue("DATA_STATIC") )
                                  ),
                                make_gvalue("Proprietary Static Data 0x00010203") /* value */ ),
                      new DBGattChar( CommandUUID /* value_type_ */,
                                  new GattCharPropertySet(GattCharPropertySet.Type.WriteNoAck).set(GattCharPropertySet.Type.WriteWithAck),
                                  Arrays.asList( // DBGattDesc
                                      new DBGattDesc( DBGattDesc.UUID16.USER_DESC, make_gvalue("COMMAND") )
                                  ),
                                  make_gvalue(128, 64) /* value */ ),
                      new DBGattChar( ResponseUUID /* value_type_ */,
                                  new GattCharPropertySet(GattCharPropertySet.Type.Notify).set(GattCharPropertySet.Type.Indicate),
                                  Arrays.asList( // DBGattDesc
                                      new DBGattDesc( DBGattDesc.UUID16.USER_DESC, make_gvalue("RESPONSE") ),
                                      DBGattDesc.createClientCharConfig()
                                  ),
                                  make_gvalue((short)0) /* value */ ),
                      new DBGattChar( PulseDataUUID /* value_type_ */,
                                  new GattCharPropertySet(GattCharPropertySet.Type.Notify).set(GattCharPropertySet.Type.Indicate),
                                  Arrays.asList( // DBGattDesc
                                      new DBGattDesc( DBGattDesc.UUID16.USER_DESC, make_gvalue("DATA_PULSE") ),
                                      DBGattDesc.createClientCharConfig()
                                  ),
                                  make_gvalue("Synthethic Sensor 01") /* value */ )
                  ) )
            ) );


    class MyAdapterStatusListener extends AdapterStatusListener {
        @Override
        public void adapterSettingsChanged(final BTAdapter adapter, final AdapterSettings oldmask,
                                           final AdapterSettings newmask, final AdapterSettings changedmask, final long timestamp) {
            final boolean initialSetting = oldmask.isEmpty();
            if( initialSetting ) {
                BTUtils.println(System.err, "****** SETTINGS: "+oldmask+" -> "+newmask+", initial "+changedmask);
            } else {
                BTUtils.println(System.err, "****** SETTINGS: "+oldmask+" -> "+newmask+", changed "+changedmask);
            }
            BTUtils.println(System.err, "Status Adapter:");
            BTUtils.println(System.err, adapter.toString());
            if( !initialSetting &&
                changedmask.isSet(AdapterSettings.SettingType.POWERED) &&
                newmask.isSet(AdapterSettings.SettingType.POWERED) )
            {

                executeOffThread( () -> { startAdvertising(adapter, "powered-on"); },
                                  "DBT-StartAdvertising-"+adapter.getAddressAndType(), true /* detach */);
            }
        }

        @Override
        public void discoveringChanged(final BTAdapter adapter, final ScanType currentMeta, final ScanType changedType, final boolean changedEnabled, final DiscoveryPolicy policy, final long timestamp) {
            BTUtils.println(System.err, "****** DISCOVERING: meta "+currentMeta+", changed["+changedType+", enabled "+changedEnabled+", policy "+policy+"] on "+adapter);
        }

        @Override
        public boolean deviceFound(final BTDevice device, final long timestamp) {
            BTUtils.println(System.err, "****** FOUND__-1: NOP "+device.toString());
            return false;
        }

        @Override
        public void deviceUpdated(final BTDevice device, final EIRDataTypeSet updateMask, final long timestamp) {
            if( SHOW_UPDATE_EVENTS ) {
                BTUtils.println(System.err, "****** UPDATED: "+updateMask+" of "+device);
            }
        }

        @Override
        public void deviceConnected(final BTDevice device, final short handle, final long timestamp) {
            BTUtils.println(System.err, "****** CONNECTED: "+device.toString());
            final boolean available = null == getDevice();
            if( available ) {
                setDevice(device);
                BTDeviceRegistry.addToProcessingDevices(device.getAddressAndType(), device.getName());
            }
        }

        @Override
        public void devicePairingState(final BTDevice device, final SMPPairingState state, final PairingMode mode, final long timestamp) {
            BTUtils.println(System.err, "****** PAIRING_STATE: state "+state+", mode "+mode+": "+device);
            switch( state ) {
                case NONE:
                    // next: deviceReady(..)
                    break;
                case FAILED: {
                    // next: deviceReady() or deviceDisconnected(..)
                } break;
                case REQUESTED_BY_RESPONDER:
                    // next: FEATURE_EXCHANGE_STARTED
                    break;
                case FEATURE_EXCHANGE_STARTED:
                    // next: FEATURE_EXCHANGE_COMPLETED
                    break;
                case FEATURE_EXCHANGE_COMPLETED:
                    // next: PASSKEY_EXPECTED... or KEY_DISTRIBUTION
                    break;
                case PASSKEY_EXPECTED: {
                    final BTSecurityRegistry.Entry sec = BTSecurityRegistry.getStartOf(device.getAddressAndType().address, "");
                    if( null != sec && sec.getPairingPasskey() != BTSecurityRegistry.NO_PASSKEY ) {
                        executeOffThread( () -> { device.setPairingPasskey( sec.getPairingPasskey() ); }, "DBT-SetPasskey-"+device.getAddressAndType(), true /* detach */);
                    } else {
                        executeOffThread( () -> { device.setPairingPasskey( 0 ); }, "DBT-SetPasskey-"+device.getAddressAndType(), true /* detach */);
                        // 3s disconnect: executeOffThread( () -> { device.setPairingPasskeyNegative(); }, "DBT-SetPasskeyNegative-"+device.getAddressAndType(), true /* detach */);
                    }
                    // next: KEY_DISTRIBUTION or FAILED
                  } break;
                case NUMERIC_COMPARE_EXPECTED: {
                    final BTSecurityRegistry.Entry sec = BTSecurityRegistry.getStartOf(device.getAddressAndType().address, "");
                    if( null != sec ) {
                        executeOffThread( () -> { device.setPairingNumericComparison( sec.getPairingNumericComparison() ); }, "DBT-SetNumericComp-"+device.getAddressAndType(), true /* detach */);
                    } else {
                        executeOffThread( () -> { device.setPairingNumericComparison( false ); }, "DBT-SetNumericCompFalse-"+device.getAddressAndType(), true /* detach */);
                    }
                    // next: KEY_DISTRIBUTION or FAILED
                  } break;
                case OOB_EXPECTED:
                    // FIXME: ABORT
                    break;
                case KEY_DISTRIBUTION:
                    // next: COMPLETED or FAILED
                    break;
                case COMPLETED:
                    // next: deviceReady(..)
                    break;
                default: // nop
                    break;
            }
        }

        @Override
        public void deviceReady(final BTDevice device, final long timestamp) {
            BTUtils.println(System.err, "****** READY-1: NOP " + device.toString());
        }

        @Override
        public void deviceDisconnected(final BTDevice device, final HCIStatusCode reason, final short handle, final long timestamp) {
            servedConnections++;
            BTUtils.println(System.err, "****** DISCONNECTED (count "+servedConnections+"): Reason "+reason+", old handle 0x"+Integer.toHexString(handle)+": "+device+" on "+device.getAdapter());
            final boolean match = matches(device);
            if( match ) {
                setDevice(null);
            }
            executeOffThread( () -> { processDisconnectedDevice(device); },
                              "DBT-Disconnected-"+device.getAdapter().getAddressAndType(), true /* detach */);
        }

        @Override
        public String toString() {
            return "AdapterStatusListener[user, per-adapter]";
        }
    };

    class MyGATTServerListener extends DBGattServer.Listener implements AutoCloseable {
        private final Thread pulseSenderThread;
        private volatile boolean stopPulseSenderFlag = false;

        private volatile short handlePulseDataNotify = 0;
        private volatile short handlePulseDataIndicate = 0;
        private volatile short handleResponseDataNotify = 0;
        private volatile short handleResponseDataIndicate = 0;

        private int usedMTU = 23; // BTGattHandler::number(BTGattHandler::Defaults::MIN_ATT_MTU);

        private void clear() {
            synchronized( sync_lock ) {
                handlePulseDataNotify = 0;
                handlePulseDataIndicate = 0;
                handleResponseDataNotify = 0;
                handleResponseDataIndicate = 0;

                dbGattServer.resetGattClientCharConfig(DataServiceUUID, PulseDataUUID);
                dbGattServer.resetGattClientCharConfig(DataServiceUUID, ResponseUUID);
            }
        }

        private boolean shallStopPulseSender() {
            synchronized( sync_lock ) {
                return stopPulseSenderFlag;
            }
        }
        private void pulseSender() {
            while( !shallStopPulseSender() ) {
                {
                    final BTDevice connectedDevice_ = getDevice();
                    if( null != connectedDevice_ && connectedDevice_.getConnected() ) {
                        if( 0 != handlePulseDataNotify || 0 != handlePulseDataIndicate ) {
                            final String data = String.format("Dynamic Data Example. Elapsed Milliseconds: %,9d", BTUtils.elapsedTimeMillis());
                            final byte[] v = data.getBytes(StandardCharsets.UTF_8);
                            if( 0 != handlePulseDataNotify ) {
                                BTUtils.fprintf_td(System.err, "****** GATT::sendNotification: PULSE to %s\n", connectedDevice_.toString());
                                connectedDevice_.sendNotification(handlePulseDataNotify, v);
                            }
                            if( 0 != handlePulseDataIndicate ) {
                                BTUtils.fprintf_td(System.err, "****** GATT::sendIndication: PULSE to %s\n", connectedDevice_.toString());
                                connectedDevice_.sendIndication(handlePulseDataIndicate, v);
                            }
                        }
                    }
                }
                if( shallStopPulseSender() ) {
                    return;
                }
                try {
                    Thread.sleep(100); // 100ms
                } catch (final InterruptedException e) { }
            }
        }

        void sendResponse(final byte[] data) {
            final BTDevice connectedDevice_ = getDevice();
            if( null != connectedDevice_ && connectedDevice_.getConnected() ) {
                if( 0 != handleResponseDataNotify || 0 != handleResponseDataIndicate ) {
                    if( 0 != handleResponseDataNotify ) {
                        BTUtils.fprintf_td(System.err, "****** GATT::sendNotification: %s to %s\n",
                                BTUtils.bytesHexString(data, 0, data.length, true /* lsb */), connectedDevice_.toString());
                        connectedDevice_.sendNotification(handleResponseDataNotify, data);
                    }
                    if( 0 != handleResponseDataIndicate ) {
                        BTUtils.fprintf_td(System.err, "****** GATT::sendIndication: %s to %s\n",
                                BTUtils.bytesHexString(data, 0, data.length, true /* lsb */), connectedDevice_.toString());
                        connectedDevice_.sendIndication(handleResponseDataIndicate, data);
                    }
                }
            }
        }

        public MyGATTServerListener() {
            pulseSenderThread = executeOffThread( () -> { pulseSender(); }, "GattServer-PulseSender", false /* detach */);
        }

        @Override
        public void close() {
            synchronized( sync_lock ) {
                stopPulseSenderFlag = true;
            }
            if( !pulseSenderThread.isDaemon() ) {
                try {
                    pulseSenderThread.join(1000);
                } catch (final InterruptedException e) { }
            }
            super.close();
        }

        @Override
        public void connected(final BTDevice device, final int initialMTU) {
            final boolean match = matches(device);
            BTUtils.fprintf_td(System.err, "****** GATT::connected(match %b): initMTU %d, %s\n",
                    match, initialMTU, device.toString());
            if( match ) {
                synchronized( sync_lock ) {
                    usedMTU = initialMTU;
                }
            }
        }

        @Override
        public void disconnected(final BTDevice device) {
            final boolean match = matches(device);
            BTUtils.fprintf_td(System.err, "****** GATT::disconnected(match %b): %s\n", match, device.toString());
            if( match ) {
                clear();
            }
        }

        @Override
        public void mtuChanged(final BTDevice device, final int mtu) {
            final boolean match = matches(device);
            BTUtils.fprintf_td(System.err, "****** GATT::mtuChanged(match %b): %d -> %d, %s\n",
                    match, match ? (int)usedMTU : 0, mtu, device.toString());
            if( match ) {
                synchronized( sync_lock ) {
                    usedMTU = mtu;
                }
            }
        }

        @Override
        public boolean readCharValue(final BTDevice device, final DBGattService s, final DBGattChar c) {
            final boolean match = matches(device);
            BTUtils.fprintf_td(System.err, "****** GATT::readCharValue(match %b): to %s, from\n  %s\n    %s\n",
                    match, device.toString(), s.toString(), c.toString());
            return match;
        }

        @Override
        public boolean readDescValue(final BTDevice device, final DBGattService s, final DBGattChar c, final DBGattDesc d) {
            final boolean match = matches(device);
            BTUtils.fprintf_td(System.err, "****** GATT::readDescValue(match %b): to %s, from\n  %s\n    %s\n      %s\n",
                    match, device.toString(), s.toString(), c.toString(), d.toString());
            return match;
        }

        @Override
        public boolean writeCharValue(final BTDevice device, final DBGattService s, final DBGattChar c, final byte[] value, final int value_offset) {
            final boolean match = matches(device);
            final String value_s = BTUtils.bytesHexString(value, 0, value.length, true /* lsbFirst */)+
                                   " '"+BTUtils.decodeUTF8String(value, 0, value.length)+"'";
            BTUtils.fprintf_td(System.err, "****** GATT::writeCharValue(match %b): %s @ %d from %s, to\n  %s\n    %s\n",
                    match, value_s, value_offset,
                    device.toString(), s.toString(), c.toString());

            return match;
        }

        @Override
        public void writeCharValueDone(final BTDevice device, final DBGattService s, final DBGattChar c) {
            final boolean match = matches(device);
            final DBGattValue value = c.getValue();
            BTUtils.fprintf_td(System.err, "****** GATT::writeCharValueDone(match %b): From %s, to\n  %s\n    %s\n    Char-Value: %s\n",
                    match, device.toString(), s.toString(), c.toString(), value.toString());

            if( match &&
                c.getValueType().equals( CommandUUID ) &&
                ( 0 != handleResponseDataNotify || 0 != handleResponseDataIndicate ) )
            {
                executeOffThread( () -> { sendResponse( value.data() ); }, true /* detach */);
            }
        }

        @Override
        public boolean writeDescValue(final BTDevice device, final DBGattService s, final DBGattChar c, final DBGattDesc d,
                                      final byte[] value, final int value_offset)
        {
            final boolean match = matches(device);
            final String value_s = BTUtils.bytesHexString(value, 0, value.length, true /* lsbFirst */)+
                                   " '"+BTUtils.decodeUTF8String(value, 0, value.length)+"'";
            BTUtils.fprintf_td(System.err, "****** GATT::writeDescValue(match %b): %s @ %d from %s\n  %s\n    %s\n      %s\n",
                    match, value_s, value_offset,
                    device.toString(), s.toString(), c.toString(), d.toString());
            return match;
        }

        @Override
        public void writeDescValueDone(final BTDevice device, final DBGattService s, final DBGattChar c, final DBGattDesc d) {
            final boolean match = matches(device);
            final DBGattValue value = d.getValue();
            BTUtils.fprintf_td(System.err, "****** GATT::writeDescValueDone(match %b): From %s\n  %s\n    %s\n      %s\n      Desc-Value: %s\n",
                    match, device.toString(), s.toString(), c.toString(), d.toString(), value.toString());
        }

        @Override
        public void clientCharConfigChanged(final BTDevice device, final DBGattService s, final DBGattChar c, final DBGattDesc d,
                                            final boolean notificationEnabled, final boolean indicationEnabled)
        {
            final boolean match = matches(device);
            final DBGattValue value = d.getValue();
            BTUtils.fprintf_td(System.err, "****** GATT::clientCharConfigChanged(match %b): notify %b, indicate %b from %s\n  %s\n    %s\n      %s\n      Desc-Value: %s\n",
                    match, notificationEnabled, indicationEnabled,
                    device.toString(), s.toString(), c.toString(), d.toString(), value.toString());

            if( match ) {
                final String value_type = c.getValueType();
                final short value_handle = c.getValueHandle();
                if( value_type.equals( PulseDataUUID ) ) {
                    synchronized( sync_lock ) {
                        handlePulseDataNotify = notificationEnabled ? value_handle : 0;
                        handlePulseDataIndicate = indicationEnabled ? value_handle : 0;
                    }
                } else if( value_type.equals( ResponseUUID ) ) {
                    synchronized( sync_lock ) {
                        handleResponseDataNotify = notificationEnabled ? value_handle : 0;
                        handleResponseDataIndicate = indicationEnabled ? value_handle : 0;
                    }
                }
            }
        }
    }
    static final short adv_interval_min=(short)160; // x0.625 = 100ms
    static final short adv_interval_max=(short)480; // x0.625 = 300ms
    static final byte adv_type=(byte)0; // AD_PDU_Type::ADV_IND;
    static final byte adv_chan_map=(byte)0x07;
    static final byte filter_policy=(byte)0x00;

    private boolean stopAdvertising(final BTAdapter adapter, final String msg) {
        if( !useAdapter.equals(EUI48.ALL_DEVICE) && !useAdapter.equals(adapter.getAddressAndType().address) ) {
            BTUtils.fprintf_td(System.err, "****** Stop advertising (%s): Adapter not selected: %s\n", msg, adapter.toString());
            return false;
        }
        final HCIStatusCode status = adapter.stopAdvertising();
        BTUtils.println(System.err, "****** Stop advertising ("+msg+") result: "+status+": "+adapter.toString());
        return HCIStatusCode.SUCCESS == status;
    }

    private boolean startAdvertising(final BTAdapter adapter, final String msg) {
        if( !useAdapter.equals(EUI48.ALL_DEVICE) && !useAdapter.equals(adapter.getAddressAndType().address) ) {
            BTUtils.fprintf_td(System.err, "****** Start advertising (%s): Adapter not selected: %s\n", msg, adapter.toString());
            return false;
        }
        final EInfoReport eir = new EInfoReport();
        final EIRDataTypeSet adv_mask = new EIRDataTypeSet();
        final EIRDataTypeSet scanrsp_mask = new EIRDataTypeSet();

        adv_mask.set(EIRDataTypeSet.DataType.FLAGS);
        adv_mask.set(EIRDataTypeSet.DataType.SERVICE_UUID);

        scanrsp_mask.set(EIRDataTypeSet.DataType.NAME);
        scanrsp_mask.set(EIRDataTypeSet.DataType.CONN_IVAL);

        eir.addFlag(GAPFlags.Bit.LE_Gen_Disc);
        eir.addFlag(GAPFlags.Bit.BREDR_UNSUP);

        eir.addService(DataServiceUUID);
        eir.setServicesComplete(false);

        eir.setName(adapter.getName());
        eir.setConnInterval((short)8, (short)12); // 10ms - 15ms

        final DBGattChar gattDevNameChar = dbGattServer.findGattChar(DBGattService.UUID16.GENERIC_ACCESS, DBGattChar.UUID16.DEVICE_NAME);
        if( null != gattDevNameChar ) {
            final byte[] aname_bytes = adapter.getName().getBytes(StandardCharsets.UTF_8);
            gattDevNameChar.setValue(aname_bytes, 0, aname_bytes.length, 0);
        }

        BTUtils.println(System.err, "****** Start advertising ("+msg+"): EIR "+eir.toString());
        BTUtils.println(System.err, "****** Start advertising ("+msg+"): adv "+adv_mask.toString()+", scanrsp "+scanrsp_mask.toString());

        final HCIStatusCode status = adapter.startAdvertising(dbGattServer, eir, adv_mask, scanrsp_mask,
                                                              adv_interval_min, adv_interval_max,
                                                              adv_type, adv_chan_map, filter_policy);
        BTUtils.println(System.err, "****** Start advertising ("+msg+") result: "+status+": "+adapter.toString());
        BTUtils.println(System.err, dbGattServer.toFullString());
        return HCIStatusCode.SUCCESS == status;
    }

    private void processDisconnectedDevice(final BTDevice device) {
        BTUtils.println(System.err, "****** Disconnected Device (count "+servedConnections+"): Start "+device.toString());

        // already unpaired
        stopAdvertising(device.getAdapter(), "device-disconnected");
        device.remove();
        BTDeviceRegistry.removeFromProcessingDevices(device.getAddressAndType());

        try {
            Thread.sleep(100); // wait a little (FIXME: Fast restart of advertising error)
        } catch (final InterruptedException e) { }

        if( !RUN_ONLY_ONCE ) {
            startAdvertising(device.getAdapter(), "device-disconnected");
        }

        BTUtils.println(System.err, "****** Disonnected Device: End "+device.toString());
    }

    private boolean initAdapter(final BTAdapter adapter) {
        if( !useAdapter.equals(EUI48.ALL_DEVICE) && !useAdapter.equals(adapter.getAddressAndType().address) ) {
            BTUtils.fprintf_td(System.err, "initAdapter: Adapter not selected: %s\n", adapter.toString());
            return false;
        }
        if( !adapter.isInitialized() ) {
            // Initialize with defaults and power-on
            final HCIStatusCode status = adapter.initialize( btMode );
            if( HCIStatusCode.SUCCESS != status ) {
                BTUtils.fprintf_td(System.err, "initAdapter: initialization failed: %s: %s\n",
                        status.toString(), adapter.toString());
                return false;
            }
        } else if( !adapter.setPowered( true ) ) {
            BTUtils.fprintf_td(System.err, "initAdapter: setPower.1 on failed: %s\n", adapter.toString());
            return false;
        }
        // adapter is powered-on
        BTUtils.println(System.err, "initAdapter.1: "+adapter.toString());

        if( adapter.setPowered(false) ) {
            HCIStatusCode status = adapter.setName(adapter_name, adapter_short_name);
            if( HCIStatusCode.SUCCESS == status ) {
                BTUtils.fprintf_td(System.err, "initAdapter: setLocalName OK: %s\n", adapter.toString());
            } else {
                BTUtils.fprintf_td(System.err, "initAdapter: setLocalName failed: %s\n", adapter.toString());
                return false;
            }

            status = adapter.setSecureConnections( use_SC );
            if( HCIStatusCode.SUCCESS == status ) {
                BTUtils.fprintf_td(System.err, "initAdapter: setSecureConnections OK: %s\n", adapter.toString());
            } else {
                BTUtils.fprintf_td(System.err, "initAdapter: setSecureConnections failed: %s\n", adapter.toString());
                return false;
            }

            final short conn_min_interval = 8;  // 10ms
            final short conn_max_interval = 40; // 50ms
            final short conn_latency = 0;
            final short supervision_timeout = 50; // 500ms
            status = adapter.setDefaultConnParam(conn_min_interval, conn_max_interval, conn_latency, supervision_timeout);
            if( HCIStatusCode.SUCCESS == status ) {
                BTUtils.fprintf_td(System.err, "initAdapter: setDefaultConnParam OK: %s\n", adapter.toString());
            } else {
                BTUtils.fprintf_td(System.err, "initAdapter: setDefaultConnParam failed: %s\n", adapter.toString());
                return false;
            }

            if( !adapter.setPowered( true ) ) {
                BTUtils.fprintf_td(System.err, "initAdapter: setPower.2 on failed: %s\n", adapter.toString());
                return false;
            }
        } else {
            BTUtils.fprintf_td(System.err, "initAdapter: setPowered.2 off failed: %s\n", adapter.toString());
        }
        BTUtils.println(System.err, "initAdapter.2: "+adapter.toString());

        {
            final LE_Features le_feats = adapter.getLEFeatures();
            BTUtils.fprintf_td(System.err, "initAdapter: LE_Features %s\n", le_feats.toString());
        }
        if( adapter.getBTMajorVersion() > 4 ) {
            final LE_PHYs Tx = new LE_PHYs( LE_PHYs.PHY.LE_2M );
            final LE_PHYs Rx = new LE_PHYs( LE_PHYs.PHY.LE_2M );
            final HCIStatusCode res = adapter.setDefaultLE_PHY(Tx, Rx);
            BTUtils.fprintf_td(System.err, "initAdapter: Set Default LE PHY: status %s: Tx %s, Rx %s\n",
                                res.toString(), Tx.toString(), Rx.toString());
        }
        adapter.setSMPKeyPath(DBTConstants.SERVER_KEY_PATH);

        // adapter is powered-on
        final AdapterStatusListener asl = new MyAdapterStatusListener();
        adapter.addStatusListener( asl );

        adapter.setServerConnSecurity(adapter_sec_level, SMPIOCapability.UNSET);

        if( !startAdvertising(adapter, "initAdapter") ) {
            adapter.removeStatusListener( asl );
            return false;
        }
        return true;
    }

    private final BTManager.ChangedAdapterSetListener myChangedAdapterSetListener =
            new BTManager.ChangedAdapterSetListener() {
                @Override
                public void adapterAdded(final BTAdapter adapter) {
                    if( null == chosenAdapter ) {
                        if( initAdapter( adapter ) ) {
                            chosenAdapter = adapter;
                            BTUtils.println(System.err, "****** Adapter ADDED__: InitOK: " + adapter);
                        } else {
                            BTUtils.println(System.err, "****** Adapter ADDED__: Ignored: " + adapter);
                        }
                    } else {
                        BTUtils.println(System.err, "****** Adapter ADDED__: Ignored (other): " + adapter);
                    }
                }

                @Override
                public void adapterRemoved(final BTAdapter adapter) {
                    if( null != chosenAdapter && adapter == chosenAdapter ) {
                        chosenAdapter = null;
                        BTUtils.println(System.err, "****** Adapter REMOVED: " + adapter);
                    } else {
                        BTUtils.println(System.err, "****** Adapter REMOVED (other): " + adapter);
                    }
                }
    };

    public void runTest(final BTManager manager) {
        timestamp_t0 = BTUtils.currentTimeMillis();

        BTUtils.println(System.err, "****** Test Start");

        final MyGATTServerListener listener = new MyGATTServerListener();
        dbGattServer.addListener( listener );

        manager.addChangedAdapterSetListener(myChangedAdapterSetListener);

        while( !RUN_ONLY_ONCE || 0 == servedConnections ) {
            try {
                Thread.sleep(2000);
            } catch (final InterruptedException e) {
                e.printStackTrace();
            }
        }

        BTUtils.println(System.err, "****** Shutdown.01 (DBGattServer.remove-listener)");
        dbGattServer.removeListener( listener );

        BTUtils.println(System.err, "****** Test Shutdown.02 (listener.close)");
        listener.close();

        BTUtils.println(System.err, "****** Test Shutdown.03 (DBGattServer.close)");
        dbGattServer.close();

        // chosenAdapter = null;

        BTUtils.println(System.err, "****** Test End");
    }

    public static void main(final String[] args) throws InterruptedException {
        for(int i=0; i< args.length; i++) {
            final String arg = args[i];
            if( arg.equals("-debug") ) {
                System.setProperty("org.tinyb.verbose", "true");
                System.setProperty("org.tinyb.debug", "true");
            } else if( arg.equals("-verbose") ) {
                System.setProperty("org.tinyb.verbose", "true");
            } else if( arg.equals("-dbt_debug") && args.length > (i+1) ) {
                System.setProperty("direct_bt.debug", args[++i]);
            } else if( arg.equals("-dbt_verbose") && args.length > (i+1) ) {
                System.setProperty("direct_bt.verbose", args[++i]);
            } else if( arg.equals("-dbt_gatt") && args.length > (i+1) ) {
                System.setProperty("direct_bt.gatt", args[++i]);
            } else if( arg.equals("-dbt_l2cap") && args.length > (i+1) ) {
                System.setProperty("direct_bt.l2cap", args[++i]);
            } else if( arg.equals("-dbt_hci") && args.length > (i+1) ) {
                System.setProperty("direct_bt.hci", args[++i]);
            } else if( arg.equals("-dbt_mgmt") && args.length > (i+1) ) {
                System.setProperty("direct_bt.mgmt", args[++i]);
            }
        }
        final BTManager manager;
        try {
            manager = BTFactory.getDirectBTManager();
        } catch (BTException | NoSuchMethodException | SecurityException
                | IllegalAccessException | IllegalArgumentException
                | InvocationTargetException | ClassNotFoundException e) {
            System.err.println("Unable to instantiate Direct-BT BluetoothManager");
            e.printStackTrace();
            System.exit(-1);
            return;
        }
        BTUtils.println(System.err, "Direct-BT BluetoothManager initialized!");
        BTUtils.println(System.err, "Direct-BT Native Version "+BTFactory.getNativeVersion()+" (API "+BTFactory.getNativeAPIVersion()+")");
        BTUtils.println(System.err, "Direct-BT Java Version "+BTFactory.getImplVersion()+" (API "+BTFactory.getAPIVersion()+")");

        final DBTPeripheral00 test = new DBTPeripheral00();

        boolean waitForEnter=false;
        {
            for(int i=0; i< args.length; i++) {
                final String arg = args[i];

                if( arg.equals("-wait") ) {
                    waitForEnter = true;
                } else if( arg.equals("-show_update_events") ) {
                    test.SHOW_UPDATE_EVENTS = true;
                } else if( arg.equals("-btmode") && args.length > (i+1) ) {
                    test.btMode = BTMode.get(args[++i]);
                } else if( arg.equals("-use_sc") && args.length > (i+1) ) {
                    test.use_SC = 0 != Integer.valueOf(args[++i]);
                } else if( arg.equals("-adapter") && args.length > (i+1) ) {
                    test.useAdapter = new EUI48( args[++i] );
                } else if( arg.equals("-name") && args.length > (i+1) ) {
                    test.adapter_name = args[++i];
                } else if( arg.equals("-short_name") && args.length > (i+1) ) {
                    test.adapter_short_name = args[++i];
                } else if( arg.equals("-mtu") && args.length > (i+1) ) {
                    test.dbGattServer.setMaxAttMTU( Integer.valueOf( args[++i] ) );
                } else if( arg.equals("-seclevel") && args.length > (i+1) ) {
                    final int sec_level_i = Integer.valueOf(args[++i]).intValue();
                    test.adapter_sec_level = BTSecurityLevel.get( (byte)( sec_level_i & 0xff ) );
                    System.err.println("Set adapter sec_level "+test.adapter_sec_level);
                } else if( arg.equals("-once") ) {
                    test.RUN_ONLY_ONCE = true;
                }
            }
            BTUtils.println(System.err, "Run with '[-btmode LE|BREDR|DUAL] [-use_sc 0|1] "+
                    "[-adapter <adapter_address>] "+
                    "[-name <adapter_name>] "+
                    "[-short_name <adapter_short_name>] "+
                    "[-seclevel <int_sec_level>]* "+
                    "[-mtu <max att_mtu>] " +
                    "[-once] "+
                    "[-verbose] [-debug] "+
                    "[-dbt_verbose true|false] "+
                    "[-dbt_debug true|false|adapter.event,gatt.data,hci.event,hci.scan_ad_eir,mgmt.event] "+
                    "[-dbt_mgmt cmd.timeout=3000,ringsize=64,...] "+
                    "[-dbt_hci cmd.complete.timeout=10000,cmd.status.timeout=3000,ringsize=64,...] "+
                    "[-dbt_gatt cmd.read.timeout=500,cmd.write.timeout=500,cmd.init.timeout=2500,ringsize=128,...] "+
                    "[-dbt_l2cap reader.timeout=10000,restart.count=0,...] "+
                    "[-shutdown <int>]'");
        }

        BTUtils.println(System.err, "SHOW_UPDATE_EVENTS "+test.SHOW_UPDATE_EVENTS);
        BTUtils.println(System.err, "adapter "+test.useAdapter);
        BTUtils.println(System.err, "adapter btmode "+test.btMode.toString());
        BTUtils.println(System.err, "adapter SC "+test.use_SC);
        BTUtils.fprintf_td(System.err, "name %s (short %s)\n", test.adapter_name, test.adapter_short_name);
        BTUtils.println(System.err, "adapter mtu "+test.dbGattServer.getMaxAttMTU());
        BTUtils.println(System.err, "adapter sec_level "+test.adapter_sec_level);
        BTUtils.println(System.err, "once "+test.RUN_ONLY_ONCE);
        BTUtils.println(System.err, "GattServer "+test.dbGattServer.toString());

        if( waitForEnter ) {
            BTUtils.println(System.err, "Press ENTER to continue\n");
            try{ System.in.read();
            } catch(final Exception e) { }
        }
        test.runTest(manager);
    }
}