Direct-BT  2.3.1
Direct-BT - Direct Bluetooth Programming.
DBTDevice.cxx
Go to the documentation of this file.
1 /*
2  * Author: Sven Gothel <sgothel@jausoft.com>
3  * Copyright (c) 2020 Gothel Software e.K.
4  * Copyright (c) 2020 ZAFENA AB
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25 
26 #include "jau_direct_bt_DBTDevice.h"
27 
28 // #define VERBOSE_ON 1
29 #include <jau/debug.hpp>
30 
31 #include "helper_base.hpp"
32 #include "helper_dbt.hpp"
33 
34 #include "direct_bt/BTDevice.hpp"
35 #include "direct_bt/BTAdapter.hpp"
36 #include "direct_bt/BTManager.hpp"
37 
38 using namespace direct_bt;
39 using namespace jau;
40 
41 static const std::string _notificationReceivedMethodArgs("(Lorg/direct_bt/BTGattChar;[BJ)V");
42 static const std::string _indicationReceivedMethodArgs("(Lorg/direct_bt/BTGattChar;[BJZ)V");
43 
45  private:
46  /**
47  public abstract class BTGattCharListener {
48  long nativeInstance;
49 
50  public void notificationReceived(final BTGattChar charDecl,
51  final byte[] value, final long timestamp) {
52  }
53 
54  public void indicationReceived(final BTGattChar charDecl,
55  final byte[] value, final long timestamp,
56  final boolean confirmationSent) {
57  }
58 
59  };
60  */
61  const BTGattChar * associatedCharacteristicRef;
62  JNIGlobalRef listenerObj; // keep listener instance alive
63  JNIGlobalRef associatedCharacteristicObj; // keeps associated characteristic alive, if not null
64  jmethodID mNotificationReceived = nullptr;
65  jmethodID mIndicationReceived = nullptr;
66 
67  public:
68 
69  JNIGattCharListener(JNIEnv *env, BTDevice *device, jobject listener, BTGattChar * associatedCharacteristicRef_)
70  : associatedCharacteristicRef(associatedCharacteristicRef_),
71  listenerObj(listener)
72  {
73  jclass listenerClazz = search_class(env, listenerObj.getObject());
75  if( nullptr == listenerClazz ) {
76  throw InternalError("BTGattCharListener not found", E_FILE_LINE);
77  }
78 
79  if( nullptr != associatedCharacteristicRef_ ) {
80  JavaGlobalObj::check(associatedCharacteristicRef_->getJavaObject(), E_FILE_LINE);
81  associatedCharacteristicObj = JavaGlobalObj::GetJavaObject(associatedCharacteristicRef_->getJavaObject()); // new global ref
82  }
83 
84  mNotificationReceived = search_method(env, listenerClazz, "notificationReceived", _notificationReceivedMethodArgs.c_str(), false);
86  if( nullptr == mNotificationReceived ) {
87  throw InternalError("BTGattCharListener has no notificationReceived"+_notificationReceivedMethodArgs+" method, for "+device->toString(), E_FILE_LINE);
88  }
89  mIndicationReceived = search_method(env, listenerClazz, "indicationReceived", _indicationReceivedMethodArgs.c_str(), false);
91  if( nullptr == mNotificationReceived ) {
92  throw InternalError("BTGattCharListener has no indicationReceived"+_indicationReceivedMethodArgs+" method, for "+device->toString(), E_FILE_LINE);
93  }
94  }
95 
96  bool match(const BTGattChar & characteristic) noexcept override {
97  if( nullptr == associatedCharacteristicRef ) {
98  return true;
99  }
100  return characteristic == *associatedCharacteristicRef;
101  }
102 
104  const TROOctets& charValue, const uint64_t timestamp) override {
105  std::shared_ptr<jau::JavaAnon> jCharDeclRef = charDecl->getJavaObject();
106  if( !jau::JavaGlobalObj::isValid(jCharDeclRef) ) {
107  return; // java object has been pulled
108  }
109  JNIEnv *env = *jni_env;
110  jobject jCharDecl = jau::JavaGlobalObj::GetObject(jCharDeclRef);
111 
112  const size_t value_size = charValue.getSize();
113  jbyteArray jval = env->NewByteArray((jsize)value_size);
114  env->SetByteArrayRegion(jval, 0, (jsize)value_size, (const jbyte *)charValue.get_ptr());
116 
117  env->CallVoidMethod(listenerObj.getObject(), mNotificationReceived,
118  jCharDecl, jval, (jlong)timestamp);
120  env->DeleteLocalRef(jval);
121  }
122 
124  const TROOctets& charValue, const uint64_t timestamp,
125  const bool confirmationSent) override {
126  std::shared_ptr<jau::JavaAnon> jCharDeclRef = charDecl->getJavaObject();
127  if( !jau::JavaGlobalObj::isValid(jCharDeclRef) ) {
128  return; // java object has been pulled
129  }
130  JNIEnv *env = *jni_env;
131  jobject jCharDecl = jau::JavaGlobalObj::GetObject(jCharDeclRef);
132 
133  const size_t value_size = charValue.getSize();
134  jbyteArray jval = env->NewByteArray((jsize)value_size);
135  env->SetByteArrayRegion(jval, 0, (jsize)value_size, (const jbyte *)charValue.get_ptr());
137 
138  env->CallVoidMethod(listenerObj.getObject(), mIndicationReceived,
139  jCharDecl, jval, (jlong)timestamp, (jboolean)confirmationSent);
141  env->DeleteLocalRef(jval);
142  }
143 };
144 
145 
146 void Java_jau_direct_1bt_DBTDevice_initImpl(JNIEnv *env, jobject obj)
147 {
148  try {
149  BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
150  JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
151  } catch(...) {
153  }
154 }
155 
156 jstring Java_jau_direct_1bt_DBTDevice_getNameImpl(JNIEnv *env, jobject obj) {
157  try {
158  BTDevice *nativePtr = getJavaUplinkObject<BTDevice>(env, obj);
159  JavaGlobalObj::check(nativePtr->getJavaObject(), E_FILE_LINE);
160  return from_string_to_jstring(env, nativePtr->getName());
161  } catch(...) {
163  }
164  return nullptr;
165 }
166 
167 jstring Java_jau_direct_1bt_DBTDevice_toStringImpl(JNIEnv *env, jobject obj) {
168  try {
169  BTDevice *nativePtr = getJavaUplinkObject<BTDevice>(env, obj);
170  JavaGlobalObj::check(nativePtr->getJavaObject(), E_FILE_LINE);
171  return from_string_to_jstring(env, nativePtr->toString());
172  } catch(...) {
174  }
175  return nullptr;
176 }
177 
178 jboolean Java_jau_direct_1bt_DBTDevice_addCharListener(JNIEnv *env, jobject obj, jobject listener, jobject jAssociatedCharacteristic) {
179  try {
180  if( nullptr == listener ) {
181  throw IllegalArgumentException("BTGattCharListener argument is null", E_FILE_LINE);
182  }
183  {
184  JNIGattCharListener * pre =
185  getObjectRef<JNIGattCharListener>(env, listener, "nativeInstance");
186  if( nullptr != pre ) {
187  throw IllegalStateException("BTGattCharListener's nativeInstance not null, already in use", E_FILE_LINE);
188  return false;
189  }
190  }
191  BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
192  JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
193  std::shared_ptr<BTGattHandler> gatt = device->getGattHandler();
194  if( nullptr == gatt ) {
195  throw IllegalStateException("BTGattChar's device GATTHandle not connected: "+ device->toString(), E_FILE_LINE);
196  }
197 
198  BTGattChar * associatedCharacteristicRef = nullptr;
199  if( nullptr != jAssociatedCharacteristic ) {
200  associatedCharacteristicRef = getJavaUplinkObject<BTGattChar>(env, jAssociatedCharacteristic);
201  }
202 
203  std::shared_ptr<BTGattCharListener> l =
204  std::shared_ptr<BTGattCharListener>( new JNIGattCharListener(env, device, listener, associatedCharacteristicRef) );
205 
206  if( gatt->addCharListener(l) ) {
207  setInstance(env, listener, l.get());
208  return JNI_TRUE;
209  }
210  } catch(...) {
212  }
213  return JNI_FALSE;
214 }
215 
216 jboolean Java_jau_direct_1bt_DBTDevice_removeCharListener(JNIEnv *env, jobject obj, jobject jlistener) {
217  try {
218  if( nullptr == jlistener ) {
219  throw IllegalArgumentException("BTGattCharListener argument is null", E_FILE_LINE);
220  }
221  JNIGattCharListener * pre =
222  getObjectRef<JNIGattCharListener>(env, jlistener, "nativeInstance");
223  if( nullptr == pre ) {
224  WARN_PRINT("BTGattCharListener's nativeInstance is null, not in use");
225  return false;
226  }
227  setObjectRef<JNIGattCharListener>(env, jlistener, nullptr, "nativeInstance");
228 
229  BTDevice *device = getJavaUplinkObjectUnchecked<BTDevice>(env, obj);
230  if( nullptr == device ) {
231  // OK to have device being deleted already @ shutdown
232  return 0;
233  }
234  JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
235  std::shared_ptr<BTGattHandler> gatt = device->getGattHandler();
236  if( nullptr == gatt ) {
237  // OK to have BTGattHandler being shutdown @ disable
238  DBG_PRINT("BTGattChar's device GATTHandle not connected: %s", device->toString().c_str());
239  return false;
240  }
241 
242  if( ! gatt->removeCharListener(pre) ) {
243  WARN_PRINT("Failed to remove BTGattCharListener with nativeInstance: %p at %s", pre, device->toString().c_str());
244  return false;
245  }
246  return true;
247  } catch(...) {
249  }
250  return JNI_FALSE;
251 }
252 
253 jint Java_jau_direct_1bt_DBTDevice_removeAllAssociatedCharListener(JNIEnv *env, jobject obj, jobject jAssociatedCharacteristic) {
254  try {
255  if( nullptr == jAssociatedCharacteristic ) {
256  throw IllegalArgumentException("associatedCharacteristic argument is null", E_FILE_LINE);
257  }
258  BTDevice *device = getJavaUplinkObjectUnchecked<BTDevice>(env, obj);
259  if( nullptr == device ) {
260  // OK to have device being deleted already @ shutdown
261  return 0;
262  }
263  JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
264  std::shared_ptr<BTGattHandler> gatt = device->getGattHandler();
265  if( nullptr == gatt ) {
266  // OK to have BTGattHandler being shutdown @ disable
267  DBG_PRINT("BTGattChar's device GATTHandle not connected: %s", device->toString().c_str());
268  return 0;
269  }
270 
271  BTGattChar * associatedCharacteristicRef = getJavaUplinkObject<BTGattChar>(env, jAssociatedCharacteristic);
272  JavaGlobalObj::check(associatedCharacteristicRef->getJavaObject(), E_FILE_LINE);
273 
274  return gatt->removeAllAssociatedCharListener(associatedCharacteristicRef);
275  } catch(...) {
277  }
278  return 0;
279 }
280 
282  try {
283  BTDevice *device = getJavaUplinkObjectUnchecked<BTDevice>(env, obj);
284  if( nullptr == device ) {
285  // OK to have device being deleted already @ shutdown
286  return 0;
287  }
288  JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
289  std::shared_ptr<BTGattHandler> gatt = device->getGattHandler();
290  if( nullptr == gatt ) {
291  // OK to have BTGattHandler being shutdown @ disable
292  DBG_PRINT("BTGattChar's device GATTHandle not connected: %s", device->toString().c_str());
293  return 0;
294  }
295  return gatt->removeAllCharListener();
296  } catch(...) {
298  }
299  return 0;
300 }
301 
302 void Java_jau_direct_1bt_DBTDevice_deleteImpl(JNIEnv *env, jobject obj, jlong nativeInstance)
303 {
304  (void)obj;
305  try {
306  BTDevice *device = castInstance<BTDevice>(nativeInstance);
307  DBG_PRINT("Java_jau_direct_1bt_DBTDevice_deleteImpl (remove only) %s", device->toString().c_str());
308  device->remove();
309  // No delete: BTDevice instance owned by BTAdapter
310  // However, device->remove() might issue destruction
311  } catch(...) {
313  }
314 }
315 
316 jbyte Java_jau_direct_1bt_DBTDevice_disconnectImpl(JNIEnv *env, jobject obj)
317 {
318  try {
319  BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
320  JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
321  return (jint) number( device->disconnect() );
322  } catch(...) {
324  }
325  return (jbyte) number(HCIStatusCode::INTERNAL_FAILURE);
326 }
327 
328 jboolean Java_jau_direct_1bt_DBTDevice_removeImpl(JNIEnv *env, jobject obj)
329 {
330  try {
331  BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
332  JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
333  device->remove();
334  } catch(...) {
336  }
337  return JNI_TRUE;
338 }
339 
341 {
342  try {
343  BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
344  JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
345  return (jbyte) number( device->connectDefault() );
346  } catch(...) {
348  }
349  return (jbyte) number(HCIStatusCode::INTERNAL_FAILURE);
350 }
351 
352 jbyte Java_jau_direct_1bt_DBTDevice_connectLEImpl0(JNIEnv *env, jobject obj)
353 {
354  try {
355  BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
356  JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
357  HCIStatusCode res = device->connectLE();
358  return (jbyte) number(res);
359  } catch(...) {
361  }
362  return (jbyte) number(HCIStatusCode::INTERNAL_FAILURE);
363 }
364 
365 jbyte Java_jau_direct_1bt_DBTDevice_connectLEImpl1(JNIEnv *env, jobject obj,
366  jshort interval, jshort window,
367  jshort min_interval, jshort max_interval,
368  jshort latency, jshort timeout)
369 {
370  try {
371  BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
372  JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
373  HCIStatusCode res = device->connectLE(interval, window, min_interval, max_interval, latency, timeout);
374  return (jbyte) number(res);
375  } catch(...) {
377  }
378  return (jbyte) number(HCIStatusCode::INTERNAL_FAILURE);
379 }
380 
381 jbyte Java_jau_direct_1bt_DBTDevice_getAvailableSMPKeysImpl(JNIEnv *env, jobject obj, jboolean responder) {
382  try {
383  BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
384  JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
385 
386  return number( device->getAvailableSMPKeys(JNI_TRUE == responder) ); // assign data of new key copy to JNI critical-array
387  } catch(...) {
389  }
390  return 0;
391 }
392 
393 void Java_jau_direct_1bt_DBTDevice_getLongTermKeyInfoImpl(JNIEnv *env, jobject obj, jboolean responder, jbyteArray jsink) {
394  try {
395  BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
396  JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
397 
398  if( nullptr == jsink ) {
399  throw IllegalArgumentException("byte array null", E_FILE_LINE);
400  }
401  const size_t sink_size = env->GetArrayLength(jsink);
402  if( sizeof(SMPLongTermKeyInfo) > sink_size ) {
403  throw IllegalArgumentException("byte array "+std::to_string(sink_size)+" < "+std::to_string(sizeof(SMPLongTermKeyInfo)), E_FILE_LINE);
404  }
405  JNICriticalArray<uint8_t, jbyteArray> criticalArray(env); // RAII - release
406  uint8_t * sink_ptr = criticalArray.get(jsink, criticalArray.Mode::UPDATE_AND_RELEASE);
407  if( NULL == sink_ptr ) {
408  throw InternalError("GetPrimitiveArrayCritical(byte array) is null", E_FILE_LINE);
409  }
410  SMPLongTermKeyInfo& ltk_sink = *reinterpret_cast<SMPLongTermKeyInfo *>(sink_ptr);
411  ltk_sink = device->getLongTermKeyInfo(JNI_TRUE == responder); // assign data of new key copy to JNI critical-array
412  } catch(...) {
414  }
415 }
416 
417 jbyte Java_jau_direct_1bt_DBTDevice_setLongTermKeyInfoImpl(JNIEnv *env, jobject obj, jbyteArray jsource) {
418  try {
419  BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
420  JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
421 
422  if( nullptr == jsource ) {
423  throw IllegalArgumentException("byte array null", E_FILE_LINE);
424  }
425  const size_t source_size = env->GetArrayLength(jsource);
426  if( sizeof(SMPLongTermKeyInfo) > source_size ) {
427  throw IllegalArgumentException("byte array "+std::to_string(source_size)+" < "+std::to_string(sizeof(SMPLongTermKeyInfo)), E_FILE_LINE);
428  }
429  JNICriticalArray<uint8_t, jbyteArray> criticalArray(env); // RAII - release
430  uint8_t * source_ptr = criticalArray.get(jsource, criticalArray.Mode::NO_UPDATE_AND_RELEASE);
431  if( NULL == source_ptr ) {
432  throw InternalError("GetPrimitiveArrayCritical(byte array) is null", E_FILE_LINE);
433  }
434  const SMPLongTermKeyInfo& ltk = *reinterpret_cast<SMPLongTermKeyInfo *>(source_ptr);
435 
436  const HCIStatusCode res = device->setLongTermKeyInfo(ltk);
437  return (jbyte) number(res);
438  } catch(...) {
440  }
441  return (jbyte) number(HCIStatusCode::INTERNAL_FAILURE);
442 }
443 
444 void Java_jau_direct_1bt_DBTDevice_getSignatureResolvingKeyInfoImpl(JNIEnv *env, jobject obj, jboolean responder, jbyteArray jsink) {
445  try {
446  BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
447  JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
448 
449  if( nullptr == jsink ) {
450  throw IllegalArgumentException("byte array null", E_FILE_LINE);
451  }
452  const size_t sink_size = env->GetArrayLength(jsink);
453  if( sizeof(SMPSignatureResolvingKeyInfo) > sink_size ) {
455  }
456  JNICriticalArray<uint8_t, jbyteArray> criticalArray(env); // RAII - release
457  uint8_t * sink_ptr = criticalArray.get(jsink, criticalArray.Mode::UPDATE_AND_RELEASE);
458  if( NULL == sink_ptr ) {
459  throw InternalError("GetPrimitiveArrayCritical(byte array) is null", E_FILE_LINE);
460  }
461  SMPSignatureResolvingKeyInfo& csrk_sink = *reinterpret_cast<SMPSignatureResolvingKeyInfo *>(sink_ptr);
462  csrk_sink = device->getSignatureResolvingKeyInfo(JNI_TRUE == responder); // assign data of new key copy to JNI critical-array
463  } catch(...) {
465  }
466 }
467 
468 jbyte Java_jau_direct_1bt_DBTDevice_unpairImpl(JNIEnv *env, jobject obj) {
469  try {
470  BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
471  JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
472  HCIStatusCode res = device->unpair();
473  return (jbyte) number(res);
474  } catch(...) {
476  }
477  return (jbyte) number(HCIStatusCode::INTERNAL_FAILURE);
478 }
479 
480 jboolean Java_jau_direct_1bt_DBTDevice_setConnSecurityLevelImpl(JNIEnv *env, jobject obj, jbyte jsec_level) {
481  try {
482  BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
483  JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
484 
485  return device->setConnSecurityLevel( to_BTSecurityLevel( static_cast<uint8_t>(jsec_level) ));
486  } catch(...) {
488  }
489  return JNI_FALSE;
490 }
491 
493  try {
494  BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
495  JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
496 
497  return number( device->getConnSecurityLevel() );
498  } catch(...) {
500  }
501  return number( BTSecurityLevel::UNSET );
502 }
503 
504 jboolean Java_jau_direct_1bt_DBTDevice_setConnIOCapabilityImpl(JNIEnv *env, jobject obj, jbyte jio_cap) {
505  try {
506  BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
507  JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
508 
509  return device->setConnIOCapability( to_SMPIOCapability( static_cast<uint8_t>(jio_cap) ));
510  } catch(...) {
512  }
513  return JNI_FALSE;
514 }
515 
516 jboolean Java_jau_direct_1bt_DBTDevice_setConnSecurityImpl(JNIEnv *env, jobject obj, jbyte jsec_level, jbyte jio_cap) {
517  try {
518  BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
519  JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
520 
521  return device->setConnSecurity( to_BTSecurityLevel( static_cast<uint8_t>(jsec_level) ),
522  to_SMPIOCapability( static_cast<uint8_t>(jio_cap) ) );
523  } catch(...) {
525  }
526  return JNI_FALSE;
527 }
528 
530  try {
531  BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
532  JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
533 
534  return number( device->getConnIOCapability() );
535  } catch(...) {
537  }
538  return number( SMPIOCapability::UNSET );
539 }
540 
541 jbyte Java_jau_direct_1bt_DBTDevice_getPairingModeImpl(JNIEnv *env, jobject obj) {
542  try {
543  BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
544  JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
545 
546  return number( device->getPairingMode() );
547  } catch(...) {
549  }
550  return number( PairingMode::NONE );
551 }
552 
553 jbyte Java_jau_direct_1bt_DBTDevice_getPairingStateImpl(JNIEnv *env, jobject obj) {
554  try {
555  BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
556  JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
557 
558  return static_cast<uint8_t>( device->getPairingState() );
559  } catch(...) {
561  }
562  return static_cast<uint8_t>( SMPPairingState::NONE );
563 }
564 
565 jboolean Java_jau_direct_1bt_DBTDevice_setConnSecurityAutoImpl(JNIEnv *env, jobject obj, jbyte jio_cap) {
566  try {
567  BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
568  JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
569 
570  return device->setConnSecurityAuto( to_SMPIOCapability( static_cast<uint8_t>(jio_cap) ) );
571  } catch(...) {
573  }
574  return JNI_FALSE;
575 }
576 
578  try {
579  BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
580  JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
581 
582  return device->isConnSecurityAutoEnabled();
583  } catch(...) {
585  }
586  return JNI_FALSE;
587 }
588 
589 jbyte Java_jau_direct_1bt_DBTDevice_setPairingPasskeyImpl(JNIEnv *env, jobject obj, jint jpasskey) {
590  try {
591  BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
592  JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
593 
594  return number( device->setPairingPasskey( static_cast<uint32_t>(jpasskey) ) );
595  } catch(...) {
597  }
598  return static_cast<uint8_t>( HCIStatusCode::INTERNAL_FAILURE );
599 }
600 
602  try {
603  BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
604  JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
605 
606  return number( device->setPairingPasskeyNegative() );
607  } catch(...) {
609  }
610  return static_cast<uint8_t>( HCIStatusCode::INTERNAL_FAILURE );
611 }
612 
613 jbyte Java_jau_direct_1bt_DBTDevice_setPairingNumericComparisonImpl(JNIEnv *env, jobject obj, jboolean jequal) {
614  try {
615  BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
616  JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
617 
618  return number( device->setPairingNumericComparison( JNI_TRUE == jequal ? true : false ) );
619  } catch(...) {
621  }
622  return static_cast<uint8_t>( HCIStatusCode::INTERNAL_FAILURE );
623 }
624 
625 //
626 // getter
627 //
628 
629 static const std::string _serviceClazzCtorArgs("(JLjau/direct_bt/DBTDevice;ZLjava/lang/String;SS)V");
630 
631 jobject Java_jau_direct_1bt_DBTDevice_getServicesImpl(JNIEnv *env, jobject obj) {
632  try {
633  BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
634  JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
635 
636  jau::darray<BTGattServiceRef> services = device->getGattServices(); // implicit GATT connect and discovery if required incl GenericAccess retrieval
637  if( services.size() > 0 ) {
638  std::shared_ptr<GattGenericAccessSvc> ga = device->getGattGenericAccess();
639  if( nullptr != ga ) {
640  DBG_PRINT("BTDevice.getServices(): GenericAccess: %s", ga->toString().c_str());
641  }
642  } else {
643  return nullptr;
644  }
645 
646  // BTGattService(final long nativeInstance, final BTDevice device, final boolean isPrimary,
647  // final String type_uuid, final short handleStart, final short handleEnd)
648 
649  std::function<jobject(JNIEnv*, jclass, jmethodID, BTGattService*)> ctor_service =
650  [](JNIEnv *env_, jclass clazz, jmethodID clazz_ctor, BTGattService *service)->jobject {
651  // prepare adapter ctor
652  std::shared_ptr<BTDevice> _device = service->getDeviceChecked();
653  JavaGlobalObj::check(_device->getJavaObject(), E_FILE_LINE);
654  jobject jdevice = JavaGlobalObj::GetObject(_device->getJavaObject());
655  const jboolean isPrimary = service->isPrimary;
656  const jstring juuid = from_string_to_jstring(env_,
657  directBTJNISettings.getUnifyUUID128Bit() ? service->type->toUUID128String() :
658  service->type->toString());
660 
661  jobject jservice = env_->NewObject(clazz, clazz_ctor, (jlong)service, jdevice, isPrimary,
662  juuid, service->startHandle, service->endHandle);
664  JNIGlobalRef::check(jservice, E_FILE_LINE);
665  std::shared_ptr<JavaAnon> jServiceRef = service->getJavaObject(); // GlobalRef
666  JavaGlobalObj::check(jServiceRef, E_FILE_LINE);
667  env_->DeleteLocalRef(juuid);
668  env_->DeleteLocalRef(jservice);
669  return JavaGlobalObj::GetObject(jServiceRef);
670  };
671  return convert_vector_sharedptr_to_jarraylist<jau::darray<BTGattServiceRef>, BTGattService>(
672  env, services, _serviceClazzCtorArgs.c_str(), ctor_service);
673  } catch(...) {
675  }
676  return nullptr;
677 }
678 
679 jboolean Java_jau_direct_1bt_DBTDevice_pingGATTImpl(JNIEnv *env, jobject obj)
680 {
681  try {
682  BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
683  JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
684 
685  return device->pingGATT() ? JNI_TRUE : JNI_FALSE;
686  } catch(...) {
688  }
689  return JNI_FALSE;
690 }
691 
692 jshort Java_jau_direct_1bt_DBTDevice_getRSSI(JNIEnv *env, jobject obj)
693 {
694  try {
695  BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
696  JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
697  return (jshort) device->getRSSI();
698  } catch(...) {
700  }
701  return 0;
702 }
703 
704 jobject Java_jau_direct_1bt_DBTDevice_getManufacturerData(JNIEnv *env, jobject obj)
705 {
706  try {
707  BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
708  JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
709  std::shared_ptr<ManufactureSpecificData> mdata = device->getManufactureSpecificData();
710 
711  jclass map_cls = search_class(env, "java/util/HashMap");
712  jmethodID map_ctor = search_method(env, map_cls, "<init>", "(I)V", false);
713  jmethodID map_put = search_method(env, map_cls, "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", false);
714 
715  jclass short_cls = search_class(env, "java/lang/Short");
716  jmethodID short_ctor = search_method(env, short_cls, "<init>", "(S)V", false);
717  jobject result = nullptr;
718 
719  if( nullptr != mdata ) {
720  result = env->NewObject(map_cls, map_ctor, 1);
721  jbyteArray arr = env->NewByteArray(mdata->data.getSize());
722  env->SetByteArrayRegion(arr, 0, mdata->data.getSize(), (const jbyte *)mdata->data.get_ptr());
723  jobject key = env->NewObject(short_cls, short_ctor, mdata->company);
724  env->CallObjectMethod(result, map_put, key, arr);
725 
726  env->DeleteLocalRef(arr);
727  env->DeleteLocalRef(key);
728  } else {
729  result = env->NewObject(map_cls, map_ctor, 0);
730  }
731  if (nullptr == result) {
732  throw jau::OutOfMemoryError("new HashMap() returned null", E_FILE_LINE);
733  }
734  return result;
735  } catch(...) {
737  }
738  return nullptr;
739 }
740 
741 jshort Java_jau_direct_1bt_DBTDevice_getTxPower(JNIEnv *env, jobject obj)
742 {
743  try {
744  BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
745  JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
746  return (jshort) device->getTxPower();
747  } catch(...) {
749  }
750  return 0;
751 }
752 
753 #if 0
754 //
755 // Leave below code 'in' disabled, as an example of how to bind Java callback functions to C++ callback functions ad-hoc.
756 
757 //
758 // BooleanDeviceCBContext
759 //
760 
761 struct BooleanDeviceCBContext {
762  BDAddressAndType addressAndType;
763  JNIGlobalRef javaCallback_ref;
764  jmethodID mRun;
765  JNIGlobalRef boolean_cls_ref;
766  jmethodID boolean_ctor;
767 
768  BooleanDeviceCBContext(
769  BDAddressAndType addressAndType_,
770  JNIGlobalRef javaCallback_ref_,
771  jmethodID mRun_,
772  JNIGlobalRef boolean_cls_ref_,
773  jmethodID boolean_ctor_)
774  : addressAndType(addressAndType_), javaCallback_ref(javaCallback_ref_),
775  mRun(mRun_), boolean_cls_ref(boolean_cls_ref_), boolean_ctor(boolean_ctor_)
776  { }
777 
778 
779  bool operator==(const BooleanDeviceCBContext& rhs) const
780  {
781  if( &rhs == this ) {
782  return true;
783  }
784  return rhs.addressAndType == addressAndType &&
785  rhs.javaCallback_ref == javaCallback_ref;
786  }
787 
788  bool operator!=(const BooleanDeviceCBContext& rhs) const
789  { return !( *this == rhs ); }
790 
791 };
792 typedef std::shared_ptr<BooleanDeviceCBContext> BooleanDeviceCBContextRef;
793 
794 
795 //
796 // Paired
797 //
798 static void disablePairedNotifications(JNIEnv *env, jobject obj, BTManager &mgmt)
799 {
801  getObjectRef<InvocationFunc<bool, const MgmtEvent&>>(env, obj, "pairedNotificationRef");
802  if( nullptr != funcptr ) {
803  FunctionDef<bool, const MgmtEvent&> funcDef( funcptr );
804  funcptr = nullptr;
805  setObjectRef(env, obj, funcptr, "pairedNotificationRef"); // clear java ref
806  int count;
807  if( 1 != ( count = mgmt.removeMgmtEventCallback(MgmtEvent::Opcode::DEVICE_UNPAIRED, funcDef) ) ) {
808  throw InternalError(std::string("removeMgmtEventCallback of ")+funcDef.toString()+" not 1 but "+std::to_string(count), E_FILE_LINE);
809  }
810  }
811 }
812 void Java_jau_direct_1bt_DBTDevice_disablePairedNotificationsImpl(JNIEnv *env, jobject obj)
813 {
814  try {
815  BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
816  JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
817  BTManager & mgmt = device->getAdapter().getManager();
818 
819  disablePairedNotifications(env, obj, mgmt);
820  } catch(...) {
822  }
823 }
824 void Java_jau_direct_1bt_DBTDevice_enablePairedNotificationsImpl(JNIEnv *env, jobject obj, jobject javaCallback)
825 {
826  try {
827  BTDevice *device= getJavaUplinkObject<BTDevice>(env, obj);
828  JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
829  BTAdapter & adapter = device->getAdapter();
830  BTManager & mgmt = adapter.getManager();
831 
832  disablePairedNotifications(env, obj, mgmt);
833 
834  bool(*nativeCallback)(BooleanDeviceCBContextRef&, const MgmtEvent&) =
835  [](BooleanDeviceCBContextRef& ctx_ref, const MgmtEvent& e)->bool {
836  const MgmtEvtDeviceUnpaired &event = *static_cast<const MgmtEvtDeviceUnpaired *>(&e);
837  if( event.getAddress() != ctx_ref->addressAndType.address || event.getAddressType() != ctx_ref->addressAndType.type ) {
838  return false; // not this device
839  }
840  jobject result = jni_env->NewObject(ctx_ref->boolean_cls_ref.getClass(), ctx_ref->boolean_ctor, JNI_FALSE);
841  jni_env->CallVoidMethod(*(ctx_ref->javaCallback_ref), ctx_ref->mRun, result);
842  jni_env->DeleteLocalRef(result);
843  return true;
844  };
845  jclass notification = search_class(*jni_env, javaCallback);
846  jmethodID mRun = search_method(*jni_env, notification, "run", "(Ljava/lang/Object;)V", false);
848  jni_env->DeleteLocalRef(notification);
849 
850  jclass boolean_cls = search_class(*jni_env, "java/lang/Boolean");
851  jmethodID boolean_ctor = search_method(*jni_env, boolean_cls, "<init>", "(Z)V", false);
853 
854  BooleanDeviceCBContext * ctx = new BooleanDeviceCBContext{
855  device->getAddressAndType(), JNIGlobalRef(javaCallback), mRun, JNIGlobalRef(boolean_cls), boolean_ctor };
856  jni_env->DeleteLocalRef(boolean_cls);
857 
858  // move BooleanDeviceCBContextRef into CaptureInvocationFunc and operator== includes javaCallback comparison
859  FunctionDef<bool, const MgmtEvent&> funcDef = bindCaptureFunc(BooleanDeviceCBContextRef(ctx), nativeCallback);
860  setObjectRef(env, obj, funcDef.cloneFunction(), "pairedNotificationRef"); // set java ref
861  // Note that this is only called natively for unpaired, i.e. paired:=false. Using deviceConnected for paired:=true on Java side
863  } catch(...) {
865  }
866 }
867 #endif
direct_bt::SMPIOCapability::UNSET
@ UNSET
Denoting unset value, i.e.
jau::FunctionDef::cloneFunction
InvocationFunc< R, A... > * cloneFunction() const noexcept
Returns a new instance of the held InvocationFunc<R, A...> function.
Definition: function_def.hpp:348
direct_bt::BTDevice::connectDefault
HCIStatusCode connectDefault() noexcept
Establish a default HCI connection to this device, using certain default parameter.
Definition: BTDevice.cpp:532
direct_bt::BTGattHandler::removeAllCharListener
int removeAllCharListener() noexcept
Remove all event listener from the list.
Definition: BTGattHandler.cpp:166
JNIGattCharListener::JNIGattCharListener
JNIGattCharListener(JNIEnv *env, BTDevice *device, jobject listener, BTGattChar *associatedCharacteristicRef_)
Definition: DBTDevice.cxx:69
Java_jau_direct_1bt_DBTDevice_unpairImpl
jbyte Java_jau_direct_1bt_DBTDevice_unpairImpl(JNIEnv *env, jobject obj)
Definition: DBTDevice.cxx:468
direct_bt::BTDevice::getPairingMode
PairingMode getPairingMode() const noexcept
Returns the current PairingMode used by the device.
Definition: BTDevice.cpp:1373
direct_bt::BTDevice::remove
void remove() noexcept
Disconnects this device via disconnect(..) if getConnected()==true and explicitly removes its shared ...
Definition: BTDevice.cpp:1784
jau::setObjectRef
void setObjectRef(JNIEnv *env, jobject obj, T *t, const char *field_name)
Definition: helper_jni.hpp:260
direct_bt::BTDevice::getPairingState
SMPPairingState getPairingState() const noexcept
Returns the current SMPPairingState.
Definition: BTDevice.cpp:1378
direct_bt::GattGenericAccessSvc::toString
std::string toString() const noexcept
Definition: GATTNumbers.cpp:382
direct_bt::BTDevice::setConnSecurityAuto
bool setConnSecurityAuto(const SMPIOCapability iocap_auto) noexcept
Set automatic security negotiation of BTSecurityLevel and SMPIOCapability pairing mode.
Definition: BTDevice.cpp:1274
_indicationReceivedMethodArgs
static const std::string _indicationReceivedMethodArgs("(Lorg/direct_bt/BTGattChar;[BJZ)V")
direct_bt::TROOctets::getSize
constexpr jau::nsize_t getSize() const noexcept
Returns the used memory size for read and write operations, may be zero.
Definition: OctetTypes.hpp:118
direct_bt::BTDevice::getSignatureResolvingKeyInfo
SMPSignatureResolvingKeyInfo getSignatureResolvingKeyInfo(const bool responder) const noexcept
Returns a copy of the Signature Resolving Key (LTK) info, valid after connection and SMP pairing has ...
Definition: BTDevice.cpp:1144
Java_jau_direct_1bt_DBTDevice_getNameImpl
jstring Java_jau_direct_1bt_DBTDevice_getNameImpl(JNIEnv *env, jobject obj)
Definition: DBTDevice.cxx:156
direct_bt::BTDevice::getTxPower
int8_t getTxPower() const noexcept
Return Tx Power of device as recognized at discovery and connect.
Definition: BTDevice.hpp:284
direct_bt::BTDevice::getLongTermKeyInfo
SMPLongTermKeyInfo getLongTermKeyInfo(const bool responder) const noexcept
Returns a copy of the Long Term Key (LTK) info, valid after connection and SMP pairing has been compl...
Definition: BTDevice.cpp:1116
direct_bt::to_BTSecurityLevel
constexpr BTSecurityLevel to_BTSecurityLevel(const uint8_t v) noexcept
Definition: BTTypes0.hpp:244
JNIGattCharListener
Definition: DBTDevice.cxx:44
jau::bindCaptureFunc
jau::FunctionDef< R, A... > bindCaptureFunc(const I &data, R(*func)(I &, A...), bool dataIsIdentity=true) noexcept
const I& data will be copied into the InvocationFunc<..> specialization and hence captured by copy.
Definition: function_def.hpp:380
jau::JavaGlobalObj::isValid
static bool isValid(const std::shared_ptr< JavaAnon > &shref) noexcept
Definition: helper_jni.hpp:154
BTManager.hpp
JNIGattCharListener::notificationReceived
void notificationReceived(BTGattCharRef charDecl, const TROOctets &charValue, const uint64_t timestamp) override
Called from native BLE stack, initiated by a received notification associated with the given BTGattCh...
Definition: DBTDevice.cxx:103
jau::IllegalArgumentException
Definition: basic_types.hpp:111
jau::from_string_to_jstring
jstring from_string_to_jstring(JNIEnv *env, const std::string &str)
helper_dbt.hpp
JNIGattCharListener::indicationReceived
void indicationReceived(BTGattCharRef charDecl, const TROOctets &charValue, const uint64_t timestamp, const bool confirmationSent) override
Called from native BLE stack, initiated by a received indication associated with the given BTGattChar...
Definition: DBTDevice.cxx:123
direct_bt::PairingMode::NONE
@ NONE
No pairing mode, implying no secure connections, no encryption and no MITM protection.
helper_base.hpp
direct_bt
Definition: ATTPDUTypes.hpp:171
JNIGlobalRef::getObject
jobject getObject() const noexcept
Definition: jni_mem.hpp:108
direct_bt::BTDevice::setConnSecurity
bool setConnSecurity(const BTSecurityLevel sec_level, const SMPIOCapability io_cap) noexcept
Sets the given BTSecurityLevel and SMPIOCapability used to connect to this device on the upcoming con...
Definition: BTDevice.cpp:1237
direct_bt::BTGattChar
Definition: BTGattChar.hpp:75
direct_bt::operator==
bool operator==(const EUI48Sub &lhs, const EUI48Sub &rhs) noexcept
Definition: BTAddress.hpp:265
jau::InternalError
Definition: basic_types.hpp:93
Java_jau_direct_1bt_DBTDevice_removeAllCharListener
jint Java_jau_direct_1bt_DBTDevice_removeAllCharListener(JNIEnv *env, jobject obj)
Definition: DBTDevice.cxx:281
direct_bt::DirectBTJNISettings::getUnifyUUID128Bit
bool getUnifyUUID128Bit()
Enables or disables uuid128_t consolidation for native uuid16_t and uuid32_t values before string con...
Definition: helper_dbt.hpp:47
direct_bt::BTDevice::setPairingNumericComparison
HCIStatusCode setPairingNumericComparison(const bool equal) noexcept
Method sets the numeric comparison result, see PairingMode::NUMERIC_COMPARE_ini.
Definition: BTDevice.cpp:1350
JNIGlobalRef
Definition: jni_mem.hpp:75
direct_bt::BTDevice::pingGATT
bool pingGATT() noexcept
Issues a GATT ping to the device, validating whether it is still reachable.
Definition: BTDevice.cpp:1575
direct_bt::BTDevice::connectLE
HCIStatusCode connectLE(const uint16_t le_scan_interval=24, const uint16_t le_scan_window=24, const uint16_t conn_interval_min=12, const uint16_t conn_interval_max=12, const uint16_t conn_latency=0, const uint16_t supervision_timeout=getHCIConnSupervisorTimeout(0, 15)) noexcept
Establish a HCI BDADDR_LE_PUBLIC or BDADDR_LE_RANDOM connection to this device.
Definition: BTDevice.cpp:283
direct_bt::to_SMPIOCapability
constexpr SMPIOCapability to_SMPIOCapability(const uint8_t v) noexcept
Definition: SMPTypes.hpp:199
direct_bt::BTManager::removeMgmtEventCallback
int removeMgmtEventCallback(const MgmtEvent::Opcode opc, const MgmtEventCallback &cb) noexcept
Returns count of removed given MgmtEventCallback from the named MgmtEvent::Opcode list.
Definition: BTManager.cpp:1126
_notificationReceivedMethodArgs
static const std::string _notificationReceivedMethodArgs("(Lorg/direct_bt/BTGattChar;[BJ)V")
direct_bt::TROOctets::get_ptr
constexpr uint8_t const * get_ptr() const noexcept
Definition: OctetTypes.hpp:228
direct_bt::BTDevice::getManufactureSpecificData
std::shared_ptr< ManufactureSpecificData > const getManufactureSpecificData() const noexcept
Return shared ManufactureSpecificData as recognized at discovery, pre GATT discovery.
Definition: BTDevice.cpp:130
Java_jau_direct_1bt_DBTDevice_setConnSecurityLevelImpl
jboolean Java_jau_direct_1bt_DBTDevice_setConnSecurityLevelImpl(JNIEnv *env, jobject obj, jbyte jsec_level)
Definition: DBTDevice.cxx:480
jau
Definition: basic_algos.hpp:34
direct_bt::BTDevice::getConnIOCapability
SMPIOCapability getConnIOCapability() const noexcept
Return the set SMPIOCapability value, determined when the connection is established.
Definition: BTDevice.cpp:1232
jau::InvocationFunc
One goal to produce the member-function type instance is to be class type agnostic for storing in the...
Definition: function_def.hpp:78
direct_bt::BTGattService
Representing a complete [Primary] Service Declaration including its list of Characteristic Declaratio...
Definition: BTGattService.hpp:67
jau::search_class
jclass search_class(JNIEnv *env, const char *clazz_name)
direct_bt::BTDevice::toString
std::string toString() const noexcept override
Definition: BTDevice.hpp:303
direct_bt::BTDevice::getAddressAndType
constexpr BDAddressAndType const & getAddressAndType() const noexcept
Returns the unique device EUI48 address and BDAddressType type.
Definition: BTDevice.hpp:278
jau::java_exception_check_and_throw
void java_exception_check_and_throw(JNIEnv *env, const char *file, int line)
Throws a C++ exception if a java exception occurred, otherwise do nothing.
direct_bt::BTAdapter::dev_id
const uint16_t dev_id
Adapter's internal temporary device id.
Definition: BTAdapter.hpp:273
jau::to_string
PRAGMA_DISABLE_WARNING_POP constexpr_cxx20 std::string to_string(const endian &v) noexcept
Return std::string representation of the given jau::endian.
Definition: byte_util.hpp:198
Java_jau_direct_1bt_DBTDevice_disconnectImpl
jbyte Java_jau_direct_1bt_DBTDevice_disconnectImpl(JNIEnv *env, jobject obj)
Definition: DBTDevice.cxx:316
Java_jau_direct_1bt_DBTDevice_getManufacturerData
jobject Java_jau_direct_1bt_DBTDevice_getManufacturerData(JNIEnv *env, jobject obj)
Definition: DBTDevice.cxx:704
Java_jau_direct_1bt_DBTDevice_getLongTermKeyInfoImpl
void Java_jau_direct_1bt_DBTDevice_getLongTermKeyInfoImpl(JNIEnv *env, jobject obj, jboolean responder, jbyteArray jsink)
Definition: DBTDevice.cxx:393
jau::FunctionDef< bool, const MgmtEvent & >
direct_bt::MgmtEvent
uint16_t opcode, uint16_t dev-id, uint16_t param_size
Definition: MgmtTypes.hpp:1083
Java_jau_direct_1bt_DBTDevice_pingGATTImpl
jboolean Java_jau_direct_1bt_DBTDevice_pingGATTImpl(JNIEnv *env, jobject obj)
Definition: DBTDevice.cxx:679
direct_bt::HCIStatusCode
HCIStatusCode
BT Core Spec v5.2: Vol 1, Part F Controller Error Codes: 1.3 List of Error Codes.
Definition: HCITypes.hpp:124
jni_env
thread_local JNIEnvContainer jni_env
E_FILE_LINE
#define E_FILE_LINE
Definition: basic_types.hpp:64
direct_bt::BTDevice::getAvailableSMPKeys
SMPKeyType getAvailableSMPKeys(const bool responder) const noexcept
Returns the available SMPKeyType mask for the responder (LL slave) or initiator (LL master).
Definition: BTDevice.cpp:1107
direct_bt::directBTJNISettings
DirectBTJNISettings directBTJNISettings
Definition: helper_dbt.cxx:35
jau::darray< BTGattServiceRef >
Java_jau_direct_1bt_DBTDevice_setPairingNumericComparisonImpl
jbyte Java_jau_direct_1bt_DBTDevice_setPairingNumericComparisonImpl(JNIEnv *env, jobject obj, jboolean jequal)
Definition: DBTDevice.cxx:613
direct_bt::BTGattCharRef
std::shared_ptr< BTGattChar > BTGattCharRef
Definition: BTGattChar.hpp:409
direct_bt::MgmtEvtDeviceUnpaired
mgmt_addr_info { EUI48, uint8_t type },
Definition: MgmtTypes.hpp:1875
jau::search_method
jmethodID search_method(JNIEnv *env, jclass clazz, const char *method_name, const char *prototype, bool is_static)
Java_jau_direct_1bt_DBTDevice_getServicesImpl
jobject Java_jau_direct_1bt_DBTDevice_getServicesImpl(JNIEnv *env, jobject obj)
Definition: DBTDevice.cxx:631
Java_jau_direct_1bt_DBTDevice_getAvailableSMPKeysImpl
jbyte Java_jau_direct_1bt_DBTDevice_getAvailableSMPKeysImpl(JNIEnv *env, jobject obj, jboolean responder)
Definition: DBTDevice.cxx:381
direct_bt::BTDevice::setConnSecurityLevel
bool setConnSecurityLevel(const BTSecurityLevel sec_level) noexcept
Set the BTSecurityLevel used to connect to this device on the upcoming connection.
Definition: BTDevice.cpp:1182
direct_bt::BTDevice::getGattGenericAccess
std::shared_ptr< GattGenericAccessSvc > getGattGenericAccess()
Returns the shared GenericAccess instance, retrieved by getGattService() or nullptr if not available.
Definition: BTDevice.cpp:1590
BTAdapter.hpp
direct_bt::BTGattCharListener
BTGattChar event listener for notification and indication events.
Definition: BTGattChar.hpp:435
direct_bt::BTDevice::setPairingPasskey
HCIStatusCode setPairingPasskey(const uint32_t passkey) noexcept
Method sets the given passkey entry, see PairingMode::PASSKEY_ENTRY_ini.
Definition: BTDevice.cpp:1304
WARN_PRINT
#define WARN_PRINT(...)
Use for unconditional warning messages, prefix '[elapsed_time] Warning @ FILE:LINE: '.
Definition: debug.hpp:146
JNIGlobalRef::check
static void check(jobject object, const char *file, int line)
Definition: jni_mem.hpp:80
jau::setInstance
void setInstance(JNIEnv *env, jobject obj, T *t)
Definition: helper_jni.hpp:286
direct_bt::BTDevice::disconnect
HCIStatusCode disconnect(const HCIStatusCode reason=HCIStatusCode::REMOTE_USER_TERMINATED_CONNECTION) noexcept
Disconnect the LE or BREDR peer's GATT and HCI connection.
Definition: BTDevice.cpp:1701
Java_jau_direct_1bt_DBTDevice_removeCharListener
jboolean Java_jau_direct_1bt_DBTDevice_removeCharListener(JNIEnv *env, jobject obj, jobject jlistener)
Definition: DBTDevice.cxx:216
direct_bt::number
constexpr uint8_t number(const BDAddressType rhs) noexcept
Definition: BTAddress.hpp:67
Java_jau_direct_1bt_DBTDevice_setPairingPasskeyImpl
jbyte Java_jau_direct_1bt_DBTDevice_setPairingPasskeyImpl(JNIEnv *env, jobject obj, jint jpasskey)
Definition: DBTDevice.cxx:589
Java_jau_direct_1bt_DBTDevice_getRSSI
jshort Java_jau_direct_1bt_DBTDevice_getRSSI(JNIEnv *env, jobject obj)
Definition: DBTDevice.cxx:692
Java_jau_direct_1bt_DBTDevice_connectLEImpl1
jbyte Java_jau_direct_1bt_DBTDevice_connectLEImpl1(JNIEnv *env, jobject obj, jshort interval, jshort window, jshort min_interval, jshort max_interval, jshort latency, jshort timeout)
Definition: DBTDevice.cxx:365
Java_jau_direct_1bt_DBTDevice_getPairingModeImpl
jbyte Java_jau_direct_1bt_DBTDevice_getPairingModeImpl(JNIEnv *env, jobject obj)
Definition: DBTDevice.cxx:541
direct_bt::ManufactureSpecificData::data
POctets data
Definition: BTTypes0.hpp:709
direct_bt::SMPSignatureResolvingKeyInfo
SMP Signature Resolving Key Info, used for platform agnostic persistence.
Definition: SMPTypes.hpp:598
direct_bt::BTSecurityLevel::UNSET
@ UNSET
Security Level not set, value 0.
Java_jau_direct_1bt_DBTDevice_initImpl
void Java_jau_direct_1bt_DBTDevice_initImpl(JNIEnv *env, jobject obj)
Definition: DBTDevice.cxx:146
jau::darray::size
constexpr size_type size() const noexcept
Like std::vector::size().
Definition: darray.hpp:668
JNICriticalArray::get
T * get(U jarray_val, Mode mode_val=UPDATE_AND_RELEASE)
Acquired the primitive array.
Definition: jni_mem.hpp:177
Java_jau_direct_1bt_DBTDevice_removeAllAssociatedCharListener
jint Java_jau_direct_1bt_DBTDevice_removeAllAssociatedCharListener(JNIEnv *env, jobject obj, jobject jAssociatedCharacteristic)
Definition: DBTDevice.cxx:253
Java_jau_direct_1bt_DBTDevice_addCharListener
jboolean Java_jau_direct_1bt_DBTDevice_addCharListener(JNIEnv *env, jobject obj, jobject listener, jobject jAssociatedCharacteristic)
Definition: DBTDevice.cxx:178
Java_jau_direct_1bt_DBTDevice_deleteImpl
void Java_jau_direct_1bt_DBTDevice_deleteImpl(JNIEnv *env, jobject obj, jlong nativeInstance)
Definition: DBTDevice.cxx:302
Java_jau_direct_1bt_DBTDevice_getConnIOCapabilityImpl
jbyte Java_jau_direct_1bt_DBTDevice_getConnIOCapabilityImpl(JNIEnv *env, jobject obj)
Definition: DBTDevice.cxx:529
direct_bt::BTManager
A thread safe singleton handler of the Linux Kernel's BlueZ manager control channel.
Definition: BTManager.hpp:201
jau::JavaGlobalObj::GetObject
static jobject GetObject(const std::shared_ptr< JavaAnon > &shref) noexcept
Definition: helper_jni.hpp:194
Java_jau_direct_1bt_DBTDevice_setLongTermKeyInfoImpl
jbyte Java_jau_direct_1bt_DBTDevice_setLongTermKeyInfoImpl(JNIEnv *env, jobject obj, jbyteArray jsource)
Definition: DBTDevice.cxx:417
direct_bt::HCIStatusCode::INTERNAL_FAILURE
@ INTERNAL_FAILURE
direct_bt::BTGattHandler::addCharListener
bool addCharListener(std::shared_ptr< BTGattCharListener > l)
Add the given listener to the list if not already present.
Definition: BTGattHandler.cpp:105
JNICriticalArray
Definition: jni_mem.hpp:126
direct_bt::BTDevice::isConnSecurityAutoEnabled
bool isConnSecurityAutoEnabled() const noexcept
Returns true if automatic security negotiation has been enabled via setConnSecurityAuto(),...
Definition: BTDevice.cpp:1299
debug.hpp
direct_bt::TROOctets
Transient read only octet data, i.e.
Definition: OctetTypes.hpp:59
rethrow_and_raise_java_exception
#define rethrow_and_raise_java_exception(E)
Re-throw current exception and raise respective java exception using any matching function above.
Definition: helper_base.hpp:55
direct_bt::BTAdapter
BTAdapter represents one Bluetooth Controller.
Definition: BTAdapter.hpp:250
jau::IllegalStateException
Definition: basic_types.hpp:117
charValue
static int charValue
Definition: dbt_scanner10.cpp:121
Java_jau_direct_1bt_DBTDevice_connectDefaultImpl
jbyte Java_jau_direct_1bt_DBTDevice_connectDefaultImpl(JNIEnv *env, jobject obj)
Definition: DBTDevice.cxx:340
direct_bt::BTAdapter::getManager
BTManager & getManager() const noexcept
Returns a reference to the used singleton BTManager instance, used to create this adapter.
Definition: BTAdapter.hpp:570
direct_bt::BTDevice::setConnIOCapability
bool setConnIOCapability(const SMPIOCapability io_cap) noexcept
Sets the given SMPIOCapability used to connect to this device on the upcoming connection.
Definition: BTDevice.cpp:1209
Java_jau_direct_1bt_DBTDevice_setPairingPasskeyNegativeImpl
jbyte Java_jau_direct_1bt_DBTDevice_setPairingPasskeyNegativeImpl(JNIEnv *env, jobject obj)
Definition: DBTDevice.cxx:601
Java_jau_direct_1bt_DBTDevice_getSignatureResolvingKeyInfoImpl
void Java_jau_direct_1bt_DBTDevice_getSignatureResolvingKeyInfoImpl(JNIEnv *env, jobject obj, jboolean responder, jbyteArray jsink)
Definition: DBTDevice.cxx:444
direct_bt::BTDevice::getGattServices
jau::darray< std::shared_ptr< BTGattService > > getGattServices() noexcept
Returns a list of shared GATTService available on this device if successful, otherwise returns an emp...
Definition: BTDevice.cpp:1525
jau::OutOfMemoryError
Definition: basic_types.hpp:99
direct_bt::BTDevice::setPairingPasskeyNegative
HCIStatusCode setPairingPasskeyNegative() noexcept
Method replies with a negative passkey response, i.e.
Definition: BTDevice.cpp:1327
Java_jau_direct_1bt_DBTDevice_removeImpl
jboolean Java_jau_direct_1bt_DBTDevice_removeImpl(JNIEnv *env, jobject obj)
Definition: DBTDevice.cxx:328
Java_jau_direct_1bt_DBTDevice_connectLEImpl0
jbyte Java_jau_direct_1bt_DBTDevice_connectLEImpl0(JNIEnv *env, jobject obj)
Definition: DBTDevice.cxx:352
direct_bt::SMPPairingState::NONE
@ NONE
No pairing in process.
_serviceClazzCtorArgs
static const std::string _serviceClazzCtorArgs("(JLjau/direct_bt/DBTDevice;ZLjava/lang/String;SS)V")
Java_jau_direct_1bt_DBTDevice_setConnSecurityImpl
jboolean Java_jau_direct_1bt_DBTDevice_setConnSecurityImpl(JNIEnv *env, jobject obj, jbyte jsec_level, jbyte jio_cap)
Definition: DBTDevice.cxx:516
direct_bt::BTDevice::getGattHandler
std::shared_ptr< BTGattHandler > getGattHandler() noexcept
Returns the connected GATTHandler or nullptr, see connectGATT(), getGattService() and disconnect().
Definition: BTDevice.cpp:1520
Java_jau_direct_1bt_DBTDevice_getConnSecurityLevelImpl
jbyte Java_jau_direct_1bt_DBTDevice_getConnSecurityLevelImpl(JNIEnv *env, jobject obj)
Definition: DBTDevice.cxx:492
Java_jau_direct_1bt_DBTDevice_getPairingStateImpl
jbyte Java_jau_direct_1bt_DBTDevice_getPairingStateImpl(JNIEnv *env, jobject obj)
Definition: DBTDevice.cxx:553
direct_bt::BTDevice::getRSSI
int8_t getRSSI() const noexcept
Return RSSI of device as recognized at discovery and connect.
Definition: BTDevice.hpp:281
direct_bt::BTDevice::getName
std::string const getName() const noexcept
Definition: BTDevice.cpp:124
direct_bt::BTGattHandler::removeAllAssociatedCharListener
int removeAllAssociatedCharListener(std::shared_ptr< BTGattChar > associatedChar) noexcept
Remove all BTGattCharListener from the list, which are associated to the given BTGattChar.
Definition: BTGattHandler.cpp:137
Java_jau_direct_1bt_DBTDevice_setConnIOCapabilityImpl
jboolean Java_jau_direct_1bt_DBTDevice_setConnIOCapabilityImpl(JNIEnv *env, jobject obj, jbyte jio_cap)
Definition: DBTDevice.cxx:504
direct_bt::BTManager::addMgmtEventCallback
bool addMgmtEventCallback(const int dev_id, const MgmtEvent::Opcode opc, const MgmtEventCallback &cb) noexcept
MgmtEventCallback handling
Definition: BTManager.cpp:1117
JNIGattCharListener::match
bool match(const BTGattChar &characteristic) noexcept override
Custom filter for all event methods, which will not be called if this method returns false.
Definition: DBTDevice.cxx:96
Java_jau_direct_1bt_DBTDevice_isConnSecurityAutoEnabled
jboolean Java_jau_direct_1bt_DBTDevice_isConnSecurityAutoEnabled(JNIEnv *env, jobject obj)
Definition: DBTDevice.cxx:577
direct_bt::operator!=
bool operator!=(const EUI48Sub &lhs, const EUI48Sub &rhs) noexcept
Definition: BTAddress.hpp:275
direct_bt::ManufactureSpecificData::company
uint16_t const company
Definition: BTTypes0.hpp:707
direct_bt::BTDevice::unpair
HCIStatusCode unpair() noexcept
Unpairs this device from the adapter while staying connected.
Definition: BTDevice.cpp:1772
direct_bt::BTDevice::getConnSecurityLevel
BTSecurityLevel getConnSecurityLevel() const noexcept
Return the BTSecurityLevel, determined when the connection is established.
Definition: BTDevice.cpp:1204
direct_bt::BTDevice::setLongTermKeyInfo
HCIStatusCode setLongTermKeyInfo(const SMPLongTermKeyInfo &ltk) noexcept
Sets the long term ket (LTK) info of this device to reuse pre-paired encryption.
Definition: BTDevice.cpp:1121
direct_bt::BDAddressAndType
Unique Bluetooth EUI48 address and BDAddressType tuple.
Definition: BTAddress.hpp:417
direct_bt::BTDevice
Definition: BTDevice.hpp:57
BTDevice.hpp
Java_jau_direct_1bt_DBTDevice_toStringImpl
jstring Java_jau_direct_1bt_DBTDevice_toStringImpl(JNIEnv *env, jobject obj)
Definition: DBTDevice.cxx:167
direct_bt::BTDevice::getAdapter
BTAdapter & getAdapter() const
Returns the managing adapter.
Definition: BTDevice.hpp:243
Java_jau_direct_1bt_DBTDevice_getTxPower
jshort Java_jau_direct_1bt_DBTDevice_getTxPower(JNIEnv *env, jobject obj)
Definition: DBTDevice.cxx:741
direct_bt::MgmtEvent::Opcode::DEVICE_UNPAIRED
@ DEVICE_UNPAIRED
Java_jau_direct_1bt_DBTDevice_setConnSecurityAutoImpl
jboolean Java_jau_direct_1bt_DBTDevice_setConnSecurityAutoImpl(JNIEnv *env, jobject obj, jbyte jio_cap)
Definition: DBTDevice.cxx:565
DBG_PRINT
#define DBG_PRINT(...)
Use for environment-variable environment::DEBUG conditional debug messages, prefix '[elapsed_time] De...
Definition: debug.hpp:78
direct_bt::BTGattHandler::removeCharListener
bool removeCharListener(std::shared_ptr< BTGattCharListener > l) noexcept
Remove the given listener from the list.
Definition: BTGattHandler.cpp:112
direct_bt::SMPLongTermKeyInfo
SMP Long Term Key Info, used for platform agnostic persistence.
Definition: SMPTypes.hpp:522