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
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
|
/*
* 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.
*/
#include "jau_direct_bt_DBTAdapter.h"
// #define VERBOSE_ON 1
#include <jau/debug.hpp>
#include "helper_base.hpp"
#include "helper_dbt.hpp"
#include "direct_bt/BTAdapter.hpp"
#include "direct_bt/BTManager.hpp"
using namespace direct_bt;
static const std::string _adapterSettingsClassName("org/direct_bt/AdapterSettings");
static const std::string _adapterSettingsClazzCtorArgs("(I)V");
static const std::string _eirDataTypeSetClassName("org/direct_bt/EIRDataTypeSet");
static const std::string _eirDataTypeSetClazzCtorArgs("(I)V");
static const std::string _hciStatusCodeClassName("org/direct_bt/HCIStatusCode");
static const std::string _hciStatusCodeClazzGetArgs("(B)Lorg/direct_bt/HCIStatusCode;");
static const std::string _scanTypeClassName("org/direct_bt/ScanType");
static const std::string _scanTypeClazzGetArgs("(B)Lorg/direct_bt/ScanType;");
static const std::string _pairingModeClassName("org/direct_bt/PairingMode");
static const std::string _pairingModeClazzGetArgs("(B)Lorg/direct_bt/PairingMode;");
static const std::string _pairingStateClassName("org/direct_bt/SMPPairingState");
static const std::string _pairingStateClazzGetArgs("(B)Lorg/direct_bt/SMPPairingState;");
static const std::string _deviceClazzCtorArgs("(JLjau/direct_bt/DBTAdapter;[BBJLjava/lang/String;)V");
static const std::string _adapterSettingsChangedMethodArgs("(Lorg/direct_bt/BTAdapter;Lorg/direct_bt/AdapterSettings;Lorg/direct_bt/AdapterSettings;Lorg/direct_bt/AdapterSettings;J)V");
static const std::string _discoveringChangedMethodArgs("(Lorg/direct_bt/BTAdapter;Lorg/direct_bt/ScanType;Lorg/direct_bt/ScanType;ZZJ)V");
static const std::string _deviceFoundMethodArgs("(Lorg/direct_bt/BTDevice;J)Z");
static const std::string _deviceUpdatedMethodArgs("(Lorg/direct_bt/BTDevice;Lorg/direct_bt/EIRDataTypeSet;J)V");
static const std::string _deviceConnectedMethodArgs("(Lorg/direct_bt/BTDevice;SJ)V");
static const std::string _devicePairingStateMethodArgs("(Lorg/direct_bt/BTDevice;Lorg/direct_bt/SMPPairingState;Lorg/direct_bt/PairingMode;J)V");
static const std::string _deviceReadyMethodArgs("(Lorg/direct_bt/BTDevice;J)V");
static const std::string _deviceDisconnectedMethodArgs("(Lorg/direct_bt/BTDevice;Lorg/direct_bt/HCIStatusCode;SJ)V");
class JNIAdapterStatusListener : public AdapterStatusListener {
private:
/**
public abstract class AdapterStatusListener {
private long nativeInstance;
public void adapterSettingsChanged(final BluetoothAdapter adapter,
final AdapterSettings oldmask, final AdapterSettings newmask,
final AdapterSettings changedmask, final long timestamp) { }
public void discoveringChanged(final BluetoothAdapter adapter, final ScanType currentMeta, final ScanType changedType, final boolean changedEnabled,
final boolean keepAlive, final long timestamp) { }
public void deviceFound(final BluetoothDevice device, final long timestamp) { }
public void deviceUpdated(final BluetoothDevice device, final EIRDataTypeSet updateMask, final long timestamp) { }
public void deviceConnected(final BluetoothDevice device, final short handle, final long timestamp) { }
public void devicePairingState(final BluetoothDevice device, final SMPPairingState state, final PairingMode mode, final long timestamp) {}
public void deviceReady(final BluetoothDevice device, final long timestamp) {}
public void deviceDisconnected(final BluetoothDevice device, final HCIStatusCode reason, final short handle, final long timestamp) { }
};
*/
static std::atomic<int> iname_next;
int const iname;
BTDevice const * const deviceMatchRef;
jau::JavaGlobalObj listenerObjRef;
std::shared_ptr<jau::JavaAnon> adapterObjRef;
JNIGlobalRef adapterSettingsClazzRef;
jmethodID adapterSettingsClazzCtor;
JNIGlobalRef eirDataTypeSetClazzRef;
jmethodID eirDataTypeSetClazzCtor;
JNIGlobalRef hciStatusCodeClazzRef;
jmethodID hciStatusCodeClazzGet;
JNIGlobalRef scanTypeClazzRef;
jmethodID scanTypeClazzGet;
JNIGlobalRef pairingModeClazzRef;
jmethodID pairingModeClazzGet;
JNIGlobalRef pairingStateClazzRef;
jmethodID pairingStateClazzGet;
JNIGlobalRef deviceClazzRef;
jmethodID deviceClazzCtor;
jfieldID deviceClazzTSLastDiscoveryField;
jfieldID deviceClazzTSLastUpdateField;
jfieldID deviceClazzConnectionHandleField;
jmethodID mAdapterSettingsChanged = nullptr;
jmethodID mDiscoveringChanged = nullptr;
jmethodID mDeviceFound = nullptr;
jmethodID mDeviceUpdated = nullptr;
jmethodID mDeviceConnected= nullptr;
jmethodID mDevicePairingState= nullptr;
jmethodID mDeviceReady = nullptr;
jmethodID mDeviceDisconnected = nullptr;
public:
std::string toString() const override {
const std::string devMatchAddr = nullptr != deviceMatchRef ? deviceMatchRef->getAddressAndType().toString() : "nil";
return "JNIAdapterStatusListener[this "+jau::to_hexstring(this)+", iname "+std::to_string(iname)+", devMatchAddr "+devMatchAddr+"]";
}
~JNIAdapterStatusListener() override {
// listenerObjRef dtor will call notifyDelete and clears the nativeInstance handle
}
JNIAdapterStatusListener(JNIEnv *env, BTAdapter *adapter,
jclass listenerClazz, jobject statusListenerObj, jmethodID statusListenerNotifyDeleted,
const BTDevice * _deviceMatchRef)
: iname(iname_next.fetch_add(1)), deviceMatchRef(_deviceMatchRef),
listenerObjRef(statusListenerObj, statusListenerNotifyDeleted)
{
adapterObjRef = adapter->getJavaObject();
jau::JavaGlobalObj::check(adapterObjRef, E_FILE_LINE);
// adapterSettingsClazzRef, adapterSettingsClazzCtor
{
jclass adapterSettingsClazz = jau::search_class(env, _adapterSettingsClassName.c_str());
jau::java_exception_check_and_throw(env, E_FILE_LINE);
if( nullptr == adapterSettingsClazz ) {
throw jau::InternalError("BTDevice::java_class not found: "+_adapterSettingsClassName, E_FILE_LINE);
}
adapterSettingsClazzRef = JNIGlobalRef(adapterSettingsClazz);
env->DeleteLocalRef(adapterSettingsClazz);
}
adapterSettingsClazzCtor = jau::search_method(env, adapterSettingsClazzRef.getClass(), "<init>", _adapterSettingsClazzCtorArgs.c_str(), false);
jau::java_exception_check_and_throw(env, E_FILE_LINE);
if( nullptr == adapterSettingsClazzCtor ) {
throw jau::InternalError("AdapterSettings ctor not found: "+_adapterSettingsClassName+".<init>"+_adapterSettingsClazzCtorArgs, E_FILE_LINE);
}
// eirDataTypeSetClazzRef, eirDataTypeSetClazzCtor
{
jclass eirDataTypeSetClazz = jau::search_class(env, _eirDataTypeSetClassName.c_str());
jau::java_exception_check_and_throw(env, E_FILE_LINE);
if( nullptr == eirDataTypeSetClazz ) {
throw jau::InternalError("BTDevice::java_class not found: "+_eirDataTypeSetClassName, E_FILE_LINE);
}
eirDataTypeSetClazzRef = JNIGlobalRef(eirDataTypeSetClazz);
env->DeleteLocalRef(eirDataTypeSetClazz);
}
eirDataTypeSetClazzCtor = jau::search_method(env, eirDataTypeSetClazzRef.getClass(), "<init>", _eirDataTypeSetClazzCtorArgs.c_str(), false);
jau::java_exception_check_and_throw(env, E_FILE_LINE);
if( nullptr == eirDataTypeSetClazzCtor ) {
throw jau::InternalError("EIRDataType ctor not found: "+_eirDataTypeSetClassName+".<init>"+_eirDataTypeSetClazzCtorArgs, E_FILE_LINE);
}
// hciStatusCodeClazzRef, hciStatusCodeClazzGet
{
jclass hciErrorCodeClazz = jau::search_class(env, _hciStatusCodeClassName.c_str());
jau::java_exception_check_and_throw(env, E_FILE_LINE);
if( nullptr == hciErrorCodeClazz ) {
throw jau::InternalError("BTDevice::java_class not found: "+_hciStatusCodeClassName, E_FILE_LINE);
}
hciStatusCodeClazzRef = JNIGlobalRef(hciErrorCodeClazz);
env->DeleteLocalRef(hciErrorCodeClazz);
}
hciStatusCodeClazzGet = jau::search_method(env, hciStatusCodeClazzRef.getClass(), "get", _hciStatusCodeClazzGetArgs.c_str(), true);
jau::java_exception_check_and_throw(env, E_FILE_LINE);
if( nullptr == hciStatusCodeClazzGet ) {
throw jau::InternalError("Static method not found: "+_hciStatusCodeClassName+".get"+_hciStatusCodeClazzGetArgs, E_FILE_LINE);
}
// scanTypeClazzRef, scanTypeClazzGet
{
jclass scanTypeClazz = jau::search_class(env, _scanTypeClassName.c_str());
jau::java_exception_check_and_throw(env, E_FILE_LINE);
if( nullptr == scanTypeClazz ) {
throw jau::InternalError("BTDevice::java_class not found: "+_scanTypeClassName, E_FILE_LINE);
}
scanTypeClazzRef = JNIGlobalRef(scanTypeClazz);
env->DeleteLocalRef(scanTypeClazz);
}
scanTypeClazzGet = jau::search_method(env, scanTypeClazzRef.getClass(), "get", _scanTypeClazzGetArgs.c_str(), true);
jau::java_exception_check_and_throw(env, E_FILE_LINE);
if( nullptr == scanTypeClazzGet ) {
throw jau::InternalError("Static method not found: "+_scanTypeClassName+".get"+_scanTypeClazzGetArgs, E_FILE_LINE);
}
// pairingModeClazzRef, pairingModeClazzGet
{
jclass pairingModeClazz = jau::search_class(env, _pairingModeClassName.c_str());
jau::java_exception_check_and_throw(env, E_FILE_LINE);
if( nullptr == pairingModeClazz ) {
throw jau::InternalError("BTDevice::java_class not found: "+_pairingModeClassName, E_FILE_LINE);
}
pairingModeClazzRef = JNIGlobalRef(pairingModeClazz);
env->DeleteLocalRef(pairingModeClazz);
}
pairingModeClazzGet = jau::search_method(env, pairingModeClazzRef.getClass(), "get", _pairingModeClazzGetArgs.c_str(), true);
jau::java_exception_check_and_throw(env, E_FILE_LINE);
if( nullptr == pairingModeClazzGet ) {
throw jau::InternalError("Static method not found: "+_pairingModeClassName+".get"+_pairingModeClazzGetArgs, E_FILE_LINE);
}
// pairingStateClazzRef, pairingStateClazzGet
{
jclass pairingStateClazz = jau::search_class(env, _pairingStateClassName.c_str());
jau::java_exception_check_and_throw(env, E_FILE_LINE);
if( nullptr == pairingStateClazz ) {
throw jau::InternalError("BTDevice::java_class not found: "+_pairingStateClassName, E_FILE_LINE);
}
pairingStateClazzRef = JNIGlobalRef(pairingStateClazz);
env->DeleteLocalRef(pairingStateClazz);
}
pairingStateClazzGet = jau::search_method(env, pairingStateClazzRef.getClass(), "get", _pairingStateClazzGetArgs.c_str(), true);
jau::java_exception_check_and_throw(env, E_FILE_LINE);
if( nullptr == pairingStateClazzGet ) {
throw jau::InternalError("Static method not found: "+_pairingStateClassName+".get"+_pairingStateClazzGetArgs, E_FILE_LINE);
}
// deviceClazzRef, deviceClazzCtor
{
jclass deviceClazz = jau::search_class(env, BTDevice::java_class().c_str());
jau::java_exception_check_and_throw(env, E_FILE_LINE);
if( nullptr == deviceClazz ) {
throw jau::InternalError("BTDevice::java_class not found: "+BTDevice::java_class(), E_FILE_LINE);
}
deviceClazzRef = JNIGlobalRef(deviceClazz);
env->DeleteLocalRef(deviceClazz);
}
deviceClazzCtor = jau::search_method(env, deviceClazzRef.getClass(), "<init>", _deviceClazzCtorArgs.c_str(), false);
jau::java_exception_check_and_throw(env, E_FILE_LINE);
if( nullptr == deviceClazzCtor ) {
throw jau::InternalError("BTDevice::java_class ctor not found: "+BTDevice::java_class()+".<init>"+_deviceClazzCtorArgs, E_FILE_LINE);
}
deviceClazzTSLastDiscoveryField = env->GetFieldID(deviceClazzRef.getClass(), "ts_last_discovery", "J");
jau::java_exception_check_and_throw(env, E_FILE_LINE);
if( nullptr == deviceClazzTSLastDiscoveryField ) {
throw jau::InternalError("BTDevice::java_class field not found: "+BTDevice::java_class()+".ts_last_discovery", E_FILE_LINE);
}
deviceClazzTSLastUpdateField = env->GetFieldID(deviceClazzRef.getClass(), "ts_last_update", "J");
jau::java_exception_check_and_throw(env, E_FILE_LINE);
if( nullptr == deviceClazzTSLastUpdateField ) {
throw jau::InternalError("BTDevice::java_class field not found: "+BTDevice::java_class()+".ts_last_update", E_FILE_LINE);
}
deviceClazzConnectionHandleField = env->GetFieldID(deviceClazzRef.getClass(), "hciConnHandle", "S");
jau::java_exception_check_and_throw(env, E_FILE_LINE);
if( nullptr == deviceClazzConnectionHandleField ) {
throw jau::InternalError("BTDevice::java_class field not found: "+BTDevice::java_class()+".hciConnHandle", E_FILE_LINE);
}
mAdapterSettingsChanged = jau::search_method(env, listenerClazz, "adapterSettingsChanged", _adapterSettingsChangedMethodArgs.c_str(), false);
jau::java_exception_check_and_throw(env, E_FILE_LINE);
if( nullptr == mAdapterSettingsChanged ) {
throw jau::InternalError("AdapterStatusListener has no adapterSettingsChanged"+_adapterSettingsChangedMethodArgs+" method, for "+adapter->toString(), E_FILE_LINE);
}
mDiscoveringChanged = jau::search_method(env, listenerClazz, "discoveringChanged", _discoveringChangedMethodArgs.c_str(), false);
jau::java_exception_check_and_throw(env, E_FILE_LINE);
if( nullptr == mDiscoveringChanged ) {
throw jau::InternalError("AdapterStatusListener has no discoveringChanged"+_discoveringChangedMethodArgs+" method, for "+adapter->toString(), E_FILE_LINE);
}
mDeviceFound = jau::search_method(env, listenerClazz, "deviceFound", _deviceFoundMethodArgs.c_str(), false);
jau::java_exception_check_and_throw(env, E_FILE_LINE);
if( nullptr == mDeviceFound ) {
throw jau::InternalError("AdapterStatusListener has no deviceFound"+_deviceFoundMethodArgs+" method, for "+adapter->toString(), E_FILE_LINE);
}
mDeviceUpdated = jau::search_method(env, listenerClazz, "deviceUpdated", _deviceUpdatedMethodArgs.c_str(), false);
jau::java_exception_check_and_throw(env, E_FILE_LINE);
if( nullptr == mDeviceUpdated ) {
throw jau::InternalError("AdapterStatusListener has no deviceUpdated"+_deviceUpdatedMethodArgs+" method, for "+adapter->toString(), E_FILE_LINE);
}
mDeviceConnected = jau::search_method(env, listenerClazz, "deviceConnected", _deviceConnectedMethodArgs.c_str(), false);
jau::java_exception_check_and_throw(env, E_FILE_LINE);
if( nullptr == mDeviceConnected ) {
throw jau::InternalError("AdapterStatusListener has no deviceConnected"+_deviceConnectedMethodArgs+" method, for "+adapter->toString(), E_FILE_LINE);
}
mDevicePairingState = jau::search_method(env, listenerClazz, "devicePairingState", _devicePairingStateMethodArgs.c_str(), false);
jau::java_exception_check_and_throw(env, E_FILE_LINE);
if( nullptr == mDevicePairingState ) {
throw jau::InternalError("AdapterStatusListener has no devicePairingState"+_devicePairingStateMethodArgs+" method, for "+adapter->toString(), E_FILE_LINE);
}
mDeviceReady = jau::search_method(env, listenerClazz, "deviceReady", _deviceReadyMethodArgs.c_str(), false);
jau::java_exception_check_and_throw(env, E_FILE_LINE);
if( nullptr == mDeviceReady ) {
throw jau::InternalError("AdapterStatusListener has no deviceReady"+_deviceReadyMethodArgs+" method, for "+adapter->toString(), E_FILE_LINE);
}
mDeviceDisconnected = jau::search_method(env, listenerClazz, "deviceDisconnected", _deviceDisconnectedMethodArgs.c_str(), false);
jau::java_exception_check_and_throw(env, E_FILE_LINE);
if( nullptr == mDeviceDisconnected ) {
throw jau::InternalError("AdapterStatusListener has no deviceDisconnected"+_deviceDisconnectedMethodArgs+" method, for "+adapter->toString(), E_FILE_LINE);
}
}
bool matchDevice(const BTDevice & device) override {
if( nullptr == deviceMatchRef ) {
return true;
}
return device == *deviceMatchRef;
}
void adapterSettingsChanged(BTAdapter &a, const AdapterSetting oldmask, const AdapterSetting newmask,
const AdapterSetting changedmask, const uint64_t timestamp) override {
JNIEnv *env = *jni_env;
(void)a;
jobject adapterSettingOld = env->NewObject(adapterSettingsClazzRef.getClass(), adapterSettingsClazzCtor, (jint)oldmask);
jau::java_exception_check_and_throw(env, E_FILE_LINE);
JNIGlobalRef::check(adapterSettingOld, E_FILE_LINE);
jobject adapterSettingNew = env->NewObject(adapterSettingsClazzRef.getClass(), adapterSettingsClazzCtor, (jint)newmask);
jau::java_exception_check_and_throw(env, E_FILE_LINE);
JNIGlobalRef::check(adapterSettingNew, E_FILE_LINE);
jobject adapterSettingChanged = env->NewObject(adapterSettingsClazzRef.getClass(), adapterSettingsClazzCtor, (jint)changedmask);
jau::java_exception_check_and_throw(env, E_FILE_LINE);
JNIGlobalRef::check(adapterSettingChanged, E_FILE_LINE);
env->CallVoidMethod(listenerObjRef.getObject(), mAdapterSettingsChanged,
jau::JavaGlobalObj::GetObject(adapterObjRef), adapterSettingOld, adapterSettingNew, adapterSettingChanged, (jlong)timestamp);
jau::java_exception_check_and_throw(env, E_FILE_LINE);
env->DeleteLocalRef(adapterSettingOld);
env->DeleteLocalRef(adapterSettingNew);
env->DeleteLocalRef(adapterSettingChanged);
}
void discoveringChanged(BTAdapter &a, const ScanType currentMeta, const ScanType changedType, const bool changedEnabled, const bool keepAlive, const uint64_t timestamp) override {
JNIEnv *env = *jni_env;
(void)a;
jobject jcurrentMeta = env->CallStaticObjectMethod(scanTypeClazzRef.getClass(), scanTypeClazzGet, (jbyte)number(currentMeta));
jau::java_exception_check_and_throw(env, E_FILE_LINE);
JNIGlobalRef::check(jcurrentMeta, E_FILE_LINE);
jobject jchangedType = env->CallStaticObjectMethod(scanTypeClazzRef.getClass(), scanTypeClazzGet, (jbyte)number(changedType));
jau::java_exception_check_and_throw(env, E_FILE_LINE);
JNIGlobalRef::check(jchangedType, E_FILE_LINE);
env->CallVoidMethod(listenerObjRef.getObject(), mDiscoveringChanged, jau::JavaGlobalObj::GetObject(adapterObjRef),
jcurrentMeta, jchangedType, (jboolean)changedEnabled, (jboolean)keepAlive, (jlong)timestamp);
jau::java_exception_check_and_throw(env, E_FILE_LINE);
}
private:
jobject newJavaBTDevice(JNIEnv *env, std::shared_ptr<BTDevice> device, const uint64_t timestamp) {
// DBTDevice(final long nativeInstance, final DBTAdapter adptr, final byte byteAddress[/*6*/], final byte byteAddressType,
// final long ts_creation, final String name)
const EUI48 addr = device->getAddressAndType().address;
jbyteArray jaddr = env->NewByteArray(sizeof(addr));
env->SetByteArrayRegion(jaddr, 0, sizeof(addr), (const jbyte*)(addr.b));
jau::java_exception_check_and_throw(env, E_FILE_LINE);
const jstring name = jau::from_string_to_jstring(env, device->getName());
jau::java_exception_check_and_throw(env, E_FILE_LINE);
jobject tmp_jdevice = env->NewObject(deviceClazzRef.getClass(), deviceClazzCtor,
(jlong)device.get(), jau::JavaGlobalObj::GetObject(adapterObjRef),
jaddr, device->getAddressAndType().type, (jlong)timestamp, name);
jau::java_exception_check_and_throw(env, E_FILE_LINE);
JNIGlobalRef::check(tmp_jdevice, E_FILE_LINE);
std::shared_ptr<jau::JavaAnon> jDeviceRef1 = device->getJavaObject();
jau::JavaGlobalObj::check(jDeviceRef1, E_FILE_LINE);
jobject jdevice = jau::JavaGlobalObj::GetObject(jDeviceRef1);
env->DeleteLocalRef(jaddr);
env->DeleteLocalRef(name);
env->DeleteLocalRef(tmp_jdevice);
return jdevice;
}
public:
bool deviceFound(std::shared_ptr<BTDevice> device, const uint64_t timestamp) override {
JNIEnv *env = *jni_env;
jobject jdevice;
std::shared_ptr<jau::JavaAnon> jDeviceRef0 = device->getJavaObject();
if( jau::JavaGlobalObj::isValid(jDeviceRef0) ) {
// Reuse Java instance
jdevice = jau::JavaGlobalObj::GetObject(jDeviceRef0);
} else {
jdevice = newJavaBTDevice(env, device, timestamp);
}
env->SetLongField(jdevice, deviceClazzTSLastDiscoveryField, (jlong)device->getLastDiscoveryTimestamp());
jau::java_exception_check_and_throw(env, E_FILE_LINE);
jboolean res = env->CallBooleanMethod(listenerObjRef.getObject(), mDeviceFound, jdevice, (jlong)timestamp);
jau::java_exception_check_and_throw(env, E_FILE_LINE);
return JNI_TRUE == res;
}
void deviceUpdated(std::shared_ptr<BTDevice> device, const EIRDataType updateMask, const uint64_t timestamp) override {
std::shared_ptr<jau::JavaAnon> jDeviceRef = device->getJavaObject();
if( !jau::JavaGlobalObj::isValid(jDeviceRef) ) {
return; // java device has been pulled
}
JNIEnv *env = *jni_env;
env->SetLongField(jau::JavaGlobalObj::GetObject(jDeviceRef), deviceClazzTSLastUpdateField, (jlong)timestamp);
jau::java_exception_check_and_throw(env, E_FILE_LINE);
jobject eirDataTypeSet = env->NewObject(eirDataTypeSetClazzRef.getClass(), eirDataTypeSetClazzCtor, (jint)updateMask);
jau::java_exception_check_and_throw(env, E_FILE_LINE);
JNIGlobalRef::check(eirDataTypeSet, E_FILE_LINE);
env->CallVoidMethod(listenerObjRef.getObject(), mDeviceUpdated, jau::JavaGlobalObj::GetObject(jDeviceRef), eirDataTypeSet, (jlong)timestamp);
jau::java_exception_check_and_throw(env, E_FILE_LINE);
env->DeleteLocalRef(eirDataTypeSet);
}
void deviceConnected(std::shared_ptr<BTDevice> device, const uint16_t handle, const uint64_t timestamp) override {
JNIEnv *env = *jni_env;
jobject jdevice;
std::shared_ptr<jau::JavaAnon> jDeviceRef0 = device->getJavaObject();
if( jau::JavaGlobalObj::isValid(jDeviceRef0) ) {
// Reuse Java instance
jdevice = jau::JavaGlobalObj::GetObject(jDeviceRef0);
} else {
jdevice = newJavaBTDevice(env, device, timestamp);
}
env->SetShortField(jdevice, deviceClazzConnectionHandleField, (jshort)handle);
jau::java_exception_check_and_throw(env, E_FILE_LINE);
env->SetLongField(jdevice, deviceClazzTSLastDiscoveryField, (jlong)device->getLastDiscoveryTimestamp());
jau::java_exception_check_and_throw(env, E_FILE_LINE);
env->SetLongField(jdevice, deviceClazzTSLastUpdateField, (jlong)timestamp);
jau::java_exception_check_and_throw(env, E_FILE_LINE);
env->CallVoidMethod(listenerObjRef.getObject(), mDeviceConnected, jdevice, (jshort)handle, (jlong)timestamp);
jau::java_exception_check_and_throw(env, E_FILE_LINE);
}
void devicePairingState(std::shared_ptr<BTDevice> device, const SMPPairingState state, const PairingMode mode, const uint64_t timestamp) override {
std::shared_ptr<jau::JavaAnon> jDeviceRef = device->getJavaObject();
if( !jau::JavaGlobalObj::isValid(jDeviceRef) ) {
return; // java device has been pulled
}
JNIEnv *env = *jni_env;
jobject jdevice = jau::JavaGlobalObj::GetObject(jDeviceRef);
env->SetLongField(jdevice, deviceClazzTSLastUpdateField, (jlong)timestamp);
jau::java_exception_check_and_throw(env, E_FILE_LINE);
jobject jstate = env->CallStaticObjectMethod(pairingStateClazzRef.getClass(), pairingStateClazzGet, static_cast<uint8_t>(state));
jau::java_exception_check_and_throw(env, E_FILE_LINE);
JNIGlobalRef::check(jstate, E_FILE_LINE);
jobject jmode = env->CallStaticObjectMethod(pairingModeClazzRef.getClass(), pairingModeClazzGet, static_cast<uint8_t>(mode));
jau::java_exception_check_and_throw(env, E_FILE_LINE);
JNIGlobalRef::check(jmode, E_FILE_LINE);
env->CallVoidMethod(listenerObjRef.getObject(), mDevicePairingState, jdevice, jstate, jmode, (jlong)timestamp);
jau::java_exception_check_and_throw(env, E_FILE_LINE);
}
void deviceReady(std::shared_ptr<BTDevice> device, const uint64_t timestamp) override {
std::shared_ptr<jau::JavaAnon> jDeviceRef = device->getJavaObject();
if( !jau::JavaGlobalObj::isValid(jDeviceRef) ) {
return; // java device has been pulled
}
JNIEnv *env = *jni_env;
jobject jdevice = jau::JavaGlobalObj::GetObject(jDeviceRef);
env->SetLongField(jdevice, deviceClazzTSLastUpdateField, (jlong)timestamp);
jau::java_exception_check_and_throw(env, E_FILE_LINE);
env->CallVoidMethod(listenerObjRef.getObject(), mDeviceReady, jdevice, (jlong)timestamp);
jau::java_exception_check_and_throw(env, E_FILE_LINE);
}
void deviceDisconnected(std::shared_ptr<BTDevice> device, const HCIStatusCode reason, const uint16_t handle, const uint64_t timestamp) override {
std::shared_ptr<jau::JavaAnon> jDeviceRef = device->getJavaObject();
if( !jau::JavaGlobalObj::isValid(jDeviceRef) ) {
return; // java device has been pulled
}
JNIEnv *env = *jni_env;
jobject jdevice = jau::JavaGlobalObj::GetObject(jDeviceRef);
env->SetLongField(jdevice, deviceClazzTSLastUpdateField, (jlong)timestamp);
jau::java_exception_check_and_throw(env, E_FILE_LINE);
jobject hciErrorCode = env->CallStaticObjectMethod(hciStatusCodeClazzRef.getClass(), hciStatusCodeClazzGet, (jbyte)static_cast<uint8_t>(reason));
jau::java_exception_check_and_throw(env, E_FILE_LINE);
JNIGlobalRef::check(hciErrorCode, E_FILE_LINE);
env->SetShortField(jdevice, deviceClazzConnectionHandleField, (jshort)0); // zero out, disconnected
jau::java_exception_check_and_throw(env, E_FILE_LINE);
env->CallVoidMethod(listenerObjRef.getObject(), mDeviceDisconnected, jdevice, hciErrorCode, (jshort)handle, (jlong)timestamp);
jau::java_exception_check_and_throw(env, E_FILE_LINE);
}
};
std::atomic<int> JNIAdapterStatusListener::iname_next(0);
jboolean Java_jau_direct_1bt_DBTAdapter_addStatusListenerImpl(JNIEnv *env, jobject obj, jobject jdeviceOwnerAndMatch, jobject statusListener)
{
try {
if( nullptr == statusListener ) {
throw jau::IllegalArgumentException("JNIAdapterStatusListener::addStatusListener: statusListener is null", E_FILE_LINE);
}
{
JNIAdapterStatusListener * pre = jau::getInstanceUnchecked<JNIAdapterStatusListener>(env, statusListener);
if( nullptr != pre ) {
throw jau::IllegalStateException("JNIAdapterStatusListener::addStatusListener: statusListener's nativeInstance not null, already in use", E_FILE_LINE);
return false;
}
}
BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj);
jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE);
BTDevice * deviceOwnerAndMatchRef = nullptr;
if( nullptr != jdeviceOwnerAndMatch ) {
deviceOwnerAndMatchRef = jau::getJavaUplinkObject<BTDevice>(env, jdeviceOwnerAndMatch);
jau::JavaGlobalObj::check(deviceOwnerAndMatchRef->getJavaObject(), E_FILE_LINE);
}
jclass listenerClazz = jau::search_class(env, statusListener);
jau::java_exception_check_and_throw(env, E_FILE_LINE);
if( nullptr == listenerClazz ) {
throw jau::InternalError("AdapterStatusListener not found", E_FILE_LINE);
}
jmethodID mStatusListenerNotifyDeleted = jau::search_method(env, listenerClazz, "notifyDeleted", "()V", false);
jau::java_exception_check_and_throw(env, E_FILE_LINE);
if( nullptr == mStatusListenerNotifyDeleted ) {
throw jau::InternalError("AdapterStatusListener class has no notifyDeleted() method", E_FILE_LINE);
}
std::shared_ptr<AdapterStatusListener> l =
std::shared_ptr<AdapterStatusListener>( new JNIAdapterStatusListener(env, adapter,
listenerClazz, statusListener, mStatusListenerNotifyDeleted, deviceOwnerAndMatchRef) );
env->DeleteLocalRef(listenerClazz);
jau::setInstance(env, statusListener, l.get());
bool addRes;
if( nullptr != deviceOwnerAndMatchRef ) {
addRes = adapter->addStatusListener( *deviceOwnerAndMatchRef, l );
} else {
addRes = adapter->addStatusListener( l );
}
if( addRes ) {
return JNI_TRUE;
}
jau::clearInstance(env, statusListener);
ERR_PRINT("JNIAdapterStatusListener::addStatusListener: FAILED: %s", l->toString().c_str());
} catch(...) {
jau::clearInstance(env, statusListener);
rethrow_and_raise_java_exception(env);
}
return JNI_FALSE;
}
jboolean Java_jau_direct_1bt_DBTAdapter_removeStatusListenerImpl(JNIEnv *env, jobject obj, jobject statusListener)
{
try {
if( nullptr == statusListener ) {
throw jau::IllegalArgumentException("statusListener is null", E_FILE_LINE);
}
JNIAdapterStatusListener * pre = jau::getInstanceUnchecked<JNIAdapterStatusListener>(env, statusListener);
if( nullptr == pre ) {
DBG_PRINT("JNIAdapterStatusListener::removeStatusListener: statusListener's nativeInstance is null, not in use");
return false;
}
jau::clearInstance(env, statusListener);
BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj);
jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE);
if( ! adapter->removeStatusListener( pre ) ) {
WARN_PRINT("Failed to remove statusListener with nativeInstance: %p at %s", pre, adapter->toString().c_str());
return false;
}
{
JNIAdapterStatusListener * post = jau::getInstanceUnchecked<JNIAdapterStatusListener>(env, statusListener);
if( nullptr != post ) {
ERR_PRINT("JNIAdapterStatusListener::removeStatusListener: statusListener's nativeInstance not null post native removal");
return false;
}
}
return true;
} catch(...) {
rethrow_and_raise_java_exception(env);
}
return JNI_FALSE;
}
jint Java_jau_direct_1bt_DBTAdapter_removeAllStatusListener(JNIEnv *env, jobject obj) {
try {
BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj);
jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE);
return adapter->removeAllStatusListener();
} catch(...) {
rethrow_and_raise_java_exception(env);
}
return 0;
}
jboolean Java_jau_direct_1bt_DBTAdapter_isDeviceWhitelisted(JNIEnv *env, jobject obj, jbyteArray jaddress, jbyte jaddressType) {
try {
BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj);
jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE);
if( nullptr == jaddress ) {
throw jau::IllegalArgumentException("address null", E_FILE_LINE);
}
const size_t address_size = env->GetArrayLength(jaddress);
if( sizeof(EUI48) > address_size ) {
throw jau::IllegalArgumentException("address byte size "+std::to_string(address_size)+" < "+std::to_string(sizeof(SMPLongTermKeyInfo)), E_FILE_LINE);
}
JNICriticalArray<uint8_t, jbyteArray> criticalArray(env); // RAII - release
uint8_t * address_ptr = criticalArray.get(jaddress, criticalArray.Mode::NO_UPDATE_AND_RELEASE);
if( NULL == address_ptr ) {
throw jau::InternalError("GetPrimitiveArrayCritical(address byte array) is null", E_FILE_LINE);
}
const EUI48& address = *reinterpret_cast<EUI48 *>(address_ptr);
const BDAddressAndType addressAndType(address, static_cast<BDAddressType>( jaddressType ));
return adapter->isDeviceWhitelisted(addressAndType);
} catch(...) {
rethrow_and_raise_java_exception(env);
}
return JNI_FALSE;
}
jboolean Java_jau_direct_1bt_DBTAdapter_addDeviceToWhitelistImpl1(JNIEnv *env, jobject obj,
jbyteArray jaddress, jbyte jaddressType, int jctype,
jshort min_interval, jshort max_interval,
jshort latency, jshort timeout) {
try {
BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj);
jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE);
if( nullptr == jaddress ) {
throw jau::IllegalArgumentException("address null", E_FILE_LINE);
}
const size_t address_size = env->GetArrayLength(jaddress);
if( sizeof(EUI48) > address_size ) {
throw jau::IllegalArgumentException("address byte size "+std::to_string(address_size)+" < "+std::to_string(sizeof(SMPLongTermKeyInfo)), E_FILE_LINE);
}
JNICriticalArray<uint8_t, jbyteArray> criticalArray(env); // RAII - release
uint8_t * address_ptr = criticalArray.get(jaddress, criticalArray.Mode::NO_UPDATE_AND_RELEASE);
if( NULL == address_ptr ) {
throw jau::InternalError("GetPrimitiveArrayCritical(address byte array) is null", E_FILE_LINE);
}
const EUI48& address = *reinterpret_cast<EUI48 *>(address_ptr);
const BDAddressAndType addressAndType(address, static_cast<BDAddressType>( jaddressType ));
const HCIWhitelistConnectType ctype = static_cast<HCIWhitelistConnectType>( jctype );
return adapter->addDeviceToWhitelist(addressAndType, ctype, (uint16_t)min_interval, (uint16_t)max_interval, (uint16_t)latency, (uint16_t)timeout);
} catch(...) {
rethrow_and_raise_java_exception(env);
}
return JNI_FALSE;
}
jboolean Java_jau_direct_1bt_DBTAdapter_addDeviceToWhitelistImpl2(JNIEnv *env, jobject obj,
jbyteArray jaddress, jbyte jaddressType, int jctype) {
try {
BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj);
jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE);
if( nullptr == jaddress ) {
throw jau::IllegalArgumentException("address null", E_FILE_LINE);
}
const size_t address_size = env->GetArrayLength(jaddress);
if( sizeof(EUI48) > address_size ) {
throw jau::IllegalArgumentException("address byte size "+std::to_string(address_size)+" < "+std::to_string(sizeof(SMPLongTermKeyInfo)), E_FILE_LINE);
}
JNICriticalArray<uint8_t, jbyteArray> criticalArray(env); // RAII - release
uint8_t * address_ptr = criticalArray.get(jaddress, criticalArray.Mode::NO_UPDATE_AND_RELEASE);
if( NULL == address_ptr ) {
throw jau::InternalError("GetPrimitiveArrayCritical(address byte array) is null", E_FILE_LINE);
}
const EUI48& address = *reinterpret_cast<EUI48 *>(address_ptr);
const BDAddressAndType addressAndType(address, static_cast<BDAddressType>( jaddressType ));
const HCIWhitelistConnectType ctype = static_cast<HCIWhitelistConnectType>( jctype );
return adapter->addDeviceToWhitelist(addressAndType, ctype);
} catch(...) {
rethrow_and_raise_java_exception(env);
}
return JNI_FALSE;
}
jboolean Java_jau_direct_1bt_DBTAdapter_removeDeviceFromWhitelistImpl(JNIEnv *env, jobject obj, jbyteArray jaddress, jbyte jaddressType) {
try {
BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj);
jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE);
if( nullptr == jaddress ) {
throw jau::IllegalArgumentException("address null", E_FILE_LINE);
}
const size_t address_size = env->GetArrayLength(jaddress);
if( sizeof(EUI48) > address_size ) {
throw jau::IllegalArgumentException("address byte size "+std::to_string(address_size)+" < "+std::to_string(sizeof(SMPLongTermKeyInfo)), E_FILE_LINE);
}
JNICriticalArray<uint8_t, jbyteArray> criticalArray(env); // RAII - release
uint8_t * address_ptr = criticalArray.get(jaddress, criticalArray.Mode::NO_UPDATE_AND_RELEASE);
if( NULL == address_ptr ) {
throw jau::InternalError("GetPrimitiveArrayCritical(address byte array) is null", E_FILE_LINE);
}
const EUI48& address = *reinterpret_cast<EUI48 *>(address_ptr);
const BDAddressAndType addressAndType(address, static_cast<BDAddressType>( jaddressType ));
return adapter->removeDeviceFromWhitelist(addressAndType);
} catch(...) {
rethrow_and_raise_java_exception(env);
}
return JNI_FALSE;
}
jstring Java_jau_direct_1bt_DBTAdapter_toStringImpl(JNIEnv *env, jobject obj) {
try {
BTAdapter *nativePtr = jau::getJavaUplinkObject<BTAdapter>(env, obj);
jau::JavaGlobalObj::check(nativePtr->getJavaObject(), E_FILE_LINE);
return jau::from_string_to_jstring(env, nativePtr->toString());
} catch(...) {
rethrow_and_raise_java_exception(env);
}
return nullptr;
}
void Java_jau_direct_1bt_DBTAdapter_deleteImpl(JNIEnv *env, jobject obj, jlong nativeInstance)
{
(void)obj;
try {
BTAdapter *adapter = jau::castInstance<BTAdapter>(nativeInstance);
DBG_PRINT("Java_jau_direct_1bt_DBTAdapter_deleteImpl (close only) %s", adapter->toString().c_str());
adapter->close();
// No delete: BTAdapter instance owned by DBTManager
// However, adapter->close() cleans up most..
} catch(...) {
rethrow_and_raise_java_exception(env);
}
}
jboolean Java_jau_direct_1bt_DBTAdapter_isPoweredImpl(JNIEnv *env, jobject obj)
{
try {
BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj);
return adapter->isPowered();
} catch(...) {
rethrow_and_raise_java_exception(env);
}
return JNI_FALSE;
}
jboolean Java_jau_direct_1bt_DBTAdapter_isSuspendedImpl(JNIEnv *env, jobject obj)
{
try {
BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj);
return adapter->isSuspended();
} catch(...) {
rethrow_and_raise_java_exception(env);
}
return JNI_FALSE;
}
jboolean Java_jau_direct_1bt_DBTAdapter_isValidImpl(JNIEnv *env, jobject obj)
{
try {
BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj);
return adapter->isValid();
} catch(...) {
rethrow_and_raise_java_exception(env);
}
return JNI_FALSE;
}
jlong Java_jau_direct_1bt_DBTAdapter_getLEFeaturesImpl(JNIEnv *env, jobject obj)
{
try {
BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj);
return (jlong) number( adapter->getLEFeatures() );
} catch(...) {
rethrow_and_raise_java_exception(env);
}
return JNI_FALSE;
}
jbyte Java_jau_direct_1bt_DBTAdapter_startDiscoveryImpl(JNIEnv *env, jobject obj, jboolean keepAlive, jboolean le_scan_active,
jshort le_scan_interval, jshort le_scan_window,
jbyte filter_policy)
{
try {
BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj);
return (jbyte) number( adapter->startDiscovery(keepAlive, le_scan_active, le_scan_interval, le_scan_window, filter_policy) );
} catch(...) {
rethrow_and_raise_java_exception(env);
}
return (jbyte) number(HCIStatusCode::INTERNAL_FAILURE);
}
jbyte Java_jau_direct_1bt_DBTAdapter_stopDiscoveryImpl(JNIEnv *env, jobject obj)
{
try {
BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj);
return (jbyte) number( adapter->stopDiscovery() );
} catch(...) {
rethrow_and_raise_java_exception(env);
}
return (jbyte) number(HCIStatusCode::INTERNAL_FAILURE);
}
jbyte Java_jau_direct_1bt_DBTAdapter_getRoleImpl(JNIEnv *env, jobject obj)
{
try {
BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj);
return (jbyte) number( adapter->getRole() );
} catch(...) {
rethrow_and_raise_java_exception(env);
}
return (jbyte) number( BTRole::None );
}
jbyte Java_jau_direct_1bt_DBTAdapter_getBTModeImpl(JNIEnv *env, jobject obj)
{
try {
BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj);
return (jbyte) number( adapter->getBTMode() );
} catch(...) {
rethrow_and_raise_java_exception(env);
}
return (jbyte) number( BTMode::NONE );
}
jobject Java_jau_direct_1bt_DBTAdapter_getDiscoveredDevicesImpl(JNIEnv *env, jobject obj)
{
try {
BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj);
jau::darray<std::shared_ptr<BTDevice>> array = adapter->getDiscoveredDevices();
return convert_vector_sharedptr_to_jarraylist(env, array);
} catch(...) {
rethrow_and_raise_java_exception(env);
}
return nullptr;
}
jint Java_jau_direct_1bt_DBTAdapter_removeDiscoveredDevicesImpl1(JNIEnv *env, jobject obj)
{
try {
BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj);
return adapter->removeDiscoveredDevices();
} catch(...) {
rethrow_and_raise_java_exception(env);
}
return 0;
}
jboolean Java_jau_direct_1bt_DBTAdapter_removeDiscoveredDeviceImpl1(JNIEnv *env, jobject obj, jbyteArray jaddress, jbyte jaddressType)
{
try {
BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj);
jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE);
if( nullptr == jaddress ) {
throw jau::IllegalArgumentException("address null", E_FILE_LINE);
}
const size_t address_size = env->GetArrayLength(jaddress);
if( sizeof(EUI48) > address_size ) {
throw jau::IllegalArgumentException("address byte size "+std::to_string(address_size)+" < "+std::to_string(sizeof(SMPLongTermKeyInfo)), E_FILE_LINE);
}
JNICriticalArray<uint8_t, jbyteArray> criticalArray(env); // RAII - release
uint8_t * address_ptr = criticalArray.get(jaddress, criticalArray.Mode::NO_UPDATE_AND_RELEASE);
if( NULL == address_ptr ) {
throw jau::InternalError("GetPrimitiveArrayCritical(address byte array) is null", E_FILE_LINE);
}
const EUI48& address = *reinterpret_cast<EUI48 *>(address_ptr);
const BDAddressAndType addressAndType(address, static_cast<BDAddressType>( jaddressType ));
return adapter->removeDiscoveredDevice(addressAndType);
} catch(...) {
rethrow_and_raise_java_exception(env);
}
return JNI_FALSE;
}
//
// misc
//
jboolean Java_jau_direct_1bt_DBTAdapter_setPowered(JNIEnv *env, jobject obj, jboolean power_on) {
try {
BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj);
jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE);
return adapter->setPowered(JNI_TRUE == power_on ? true : false) ? JNI_TRUE : JNI_FALSE;
} catch(...) {
rethrow_and_raise_java_exception(env);
}
return JNI_FALSE;
}
jbyte Java_jau_direct_1bt_DBTAdapter_initializeImpl(JNIEnv *env, jobject obj, jbyte jbtMode) {
try {
BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj);
jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE);
const BTMode btMode = static_cast<BTMode>(jbtMode);
HCIStatusCode res = adapter->initialize(btMode);
return (jbyte) number(res);
} catch(...) {
rethrow_and_raise_java_exception(env);
}
return (jbyte) number(HCIStatusCode::INTERNAL_FAILURE);
}
jboolean Java_jau_direct_1bt_DBTAdapter_isInitialized(JNIEnv *env, jobject obj) {
try {
BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj);
jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE);
return adapter->isInitialized();
} catch(...) {
rethrow_and_raise_java_exception(env);
}
return JNI_FALSE;
}
jbyte Java_jau_direct_1bt_DBTAdapter_resetImpl(JNIEnv *env, jobject obj) {
try {
BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj);
jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE);
HCIStatusCode res = adapter->reset();
return (jbyte) number(res);
} catch(...) {
rethrow_and_raise_java_exception(env);
}
return (jbyte) number(HCIStatusCode::INTERNAL_FAILURE);
}
jbyte Java_jau_direct_1bt_DBTAdapter_setDefaultLE_1PHYImpl(JNIEnv *env, jobject obj,
jboolean tryTx, jboolean tryRx,
jbyte jTx, jbyte jRx) {
try {
BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj);
jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE);
const LE_PHYs Tx = static_cast<LE_PHYs>(jTx);
const LE_PHYs Rx = static_cast<LE_PHYs>(jRx);
return number ( adapter->setDefaultLE_PHY(tryTx, tryRx, Tx, Rx) );
} catch(...) {
rethrow_and_raise_java_exception(env);
}
return (jbyte) number(HCIStatusCode::INTERNAL_FAILURE);
}
jstring Java_jau_direct_1bt_DBTAdapter_getNameImpl(JNIEnv *env, jobject obj) {
try {
BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj);
jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE);
return jau::from_string_to_jstring(env, adapter->getName());
} catch(...) {
rethrow_and_raise_java_exception(env);
}
return nullptr;
}
jstring Java_jau_direct_1bt_DBTAdapter_getShortNameImpl(JNIEnv *env, jobject obj) {
try {
BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj);
jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE);
return jau::from_string_to_jstring(env, adapter->getShortName());
} catch(...) {
rethrow_and_raise_java_exception(env);
}
return nullptr;
}
jbyte Java_jau_direct_1bt_DBTAdapter_setNameImpl(JNIEnv *env, jobject obj, jstring jname, jstring jshort_name) {
try {
BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj);
jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE);
std::string name = jau::from_jstring_to_string(env, jname);
std::string short_name = jau::from_jstring_to_string(env, jshort_name);
return (jbyte) number( adapter->setName(name, short_name) );
} catch(...) {
rethrow_and_raise_java_exception(env);
}
return (jbyte) number(HCIStatusCode::INTERNAL_FAILURE);
}
jboolean Java_jau_direct_1bt_DBTAdapter_setDiscoverable(JNIEnv *env, jobject obj, jboolean value) {
try {
BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj);
jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE);
return adapter->setDiscoverable(JNI_TRUE == value ? true : false) ? JNI_TRUE : JNI_FALSE;
} catch(...) {
rethrow_and_raise_java_exception(env);
}
return JNI_FALSE;
}
jobject Java_jau_direct_1bt_DBTAdapter_connectDeviceImpl(JNIEnv *env, jobject obj, jbyteArray jaddress, jbyte jaddressType) {
try {
BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj);
jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE);
if( nullptr == jaddress ) {
throw jau::IllegalArgumentException("address null", E_FILE_LINE);
}
const size_t address_size = env->GetArrayLength(jaddress);
if( sizeof(EUI48) > address_size ) {
throw jau::IllegalArgumentException("address byte size "+std::to_string(address_size)+" < "+std::to_string(sizeof(SMPLongTermKeyInfo)), E_FILE_LINE);
}
JNICriticalArray<uint8_t, jbyteArray> criticalArray(env); // RAII - release
uint8_t * address_ptr = criticalArray.get(jaddress, criticalArray.Mode::NO_UPDATE_AND_RELEASE);
if( NULL == address_ptr ) {
throw jau::InternalError("GetPrimitiveArrayCritical(address byte array) is null", E_FILE_LINE);
}
const EUI48& address = *reinterpret_cast<EUI48 *>(address_ptr);
const BDAddressType addressType = static_cast<BDAddressType>( jaddressType );
std::shared_ptr<BTDevice> device = adapter->findSharedDevice(address, addressType);
if( nullptr == device ) {
device = adapter->findDiscoveredDevice(address, addressType);
}
if( nullptr != device ) {
direct_bt::HCIHandler & hci = adapter->getHCI();
if( !hci.isOpen() ) {
throw BTException("Adapter's HCI closed "+adapter->toString(), E_FILE_LINE);
}
std::shared_ptr<jau::JavaAnon> jDeviceRef = device->getJavaObject();
jau::JavaGlobalObj::check(jDeviceRef, E_FILE_LINE);
device->connectDefault();
return jau::JavaGlobalObj::GetObject(jDeviceRef);
}
} catch(...) {
rethrow_and_raise_java_exception(env);
}
return nullptr;
}
jboolean Java_jau_direct_1bt_DBTAdapter_setPairable(JNIEnv *env, jobject obj, jboolean value) {
try {
BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj);
jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE);
return adapter->setBondable(JNI_TRUE == value ? true : false) ? JNI_TRUE : JNI_FALSE;
} catch(...) {
rethrow_and_raise_java_exception(env);
}
return JNI_FALSE;
}
void Java_jau_direct_1bt_DBTAdapter_printDeviceListsImpl(JNIEnv *env, jobject obj) {
try {
BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj);
jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE);
adapter->printDeviceLists();
} catch(...) {
rethrow_and_raise_java_exception(env);
}
}
jbyte Java_jau_direct_1bt_DBTAdapter_startAdvertisingImpl(JNIEnv *env, jobject obj,
jshort adv_interval_min, jshort adv_interval_max, jbyte jadv_type, jbyte adv_chan_map,
jbyte filter_policy) {
try {
BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj);
jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE);
const AD_PDU_Type adv_type = static_cast<AD_PDU_Type>(jadv_type);
HCIStatusCode res = adapter->startAdvertising(adv_interval_min, adv_interval_max, adv_type, adv_chan_map, filter_policy);
return (jbyte) number(res);
} catch(...) {
rethrow_and_raise_java_exception(env);
}
return (jbyte) number(HCIStatusCode::INTERNAL_FAILURE);
}
jbyte Java_jau_direct_1bt_DBTAdapter_stopAdvertisingImpl(JNIEnv *env, jobject obj) {
try {
BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj);
jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE);
HCIStatusCode res = adapter->stopAdvertising();
return (jbyte) number(res);
} catch(...) {
rethrow_and_raise_java_exception(env);
}
return (jbyte) number(HCIStatusCode::INTERNAL_FAILURE);
}
jboolean Java_jau_direct_1bt_DBTAdapter_isAdvertising(JNIEnv *env, jobject obj) {
try {
BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj);
jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE);
return adapter->isAdvertising() ? JNI_TRUE : JNI_FALSE;
} catch(...) {
rethrow_and_raise_java_exception(env);
}
return JNI_FALSE;
}
|