diff options
author | Sven Gothel <[email protected]> | 2020-12-10 07:02:10 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2020-12-10 07:02:10 +0100 |
commit | ab86d222890076ce26b8638411e490c047add780 (patch) | |
tree | dd7f37fc70f35fe2278b9b001d2741d00819ac97 /java/jni | |
parent | 118d564141d594e3f9fe0a082f1854289c95a7a8 (diff) |
BluetoothDevice: Support SMPLongTermKeyInfo via [get/set]LongTermKeyInfo(..), tested with DBTScanner10.java
Java + Native test are now on par, may share the LTK binary file in example.
On loading a pre-existing key when device found and before connecting,
BlueZ/Kernel will try the uploaded key and no pairing procedure is being performed.
This tested with explicit unpair before uploading the LTK,
to ensure the pre-existing keys have been deleted.
Diffstat (limited to 'java/jni')
-rw-r--r-- | java/jni/direct_bt/DBTDevice.cxx | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/java/jni/direct_bt/DBTDevice.cxx b/java/jni/direct_bt/DBTDevice.cxx index 39e2ede9..1ff0e6ee 100644 --- a/java/jni/direct_bt/DBTDevice.cxx +++ b/java/jni/direct_bt/DBTDevice.cxx @@ -378,6 +378,57 @@ jbyte Java_direct_1bt_tinyb_DBTDevice_connectLEImpl1(JNIEnv *env, jobject obj, return (jbyte) number(HCIStatusCode::INTERNAL_FAILURE); } +void Java_direct_1bt_tinyb_DBTDevice_getLongTermKeyInfoImpl(JNIEnv *env, jobject obj, jboolean responder, jbyteArray jsink) { + try { + DBTDevice *device = getJavaUplinkObject<DBTDevice>(env, obj); + JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); + + if( nullptr == jsink ) { + throw IllegalArgumentException("byte array null", E_FILE_LINE); + } + const size_t sink_size = env->GetArrayLength(jsink); + if( sizeof(SMPLongTermKeyInfo) > sink_size ) { + throw IllegalArgumentException("byte array "+std::to_string(sink_size)+" < "+std::to_string(sizeof(SMPLongTermKeyInfo)), E_FILE_LINE); + } + JNICriticalArray<uint8_t, jbyteArray> criticalArray(env); // RAII - release + uint8_t * sink_ptr = criticalArray.get(jsink, criticalArray.Mode::UPDATE_AND_RELEASE); + if( NULL == sink_ptr ) { + throw InternalError("GetPrimitiveArrayCritical(byte array) is null", E_FILE_LINE); + } + SMPLongTermKeyInfo& ltk_sink = *reinterpret_cast<SMPLongTermKeyInfo *>(sink_ptr); + ltk_sink = device->getLongTermKeyInfo(JNI_TRUE == responder); // assign data of new key copy to JNI critical-array + } catch(...) { + rethrow_and_raise_java_exception(env); + } +} + +jbyte Java_direct_1bt_tinyb_DBTDevice_setLongTermKeyInfoImpl(JNIEnv *env, jobject obj, jbyteArray jsource, jboolean responder) { + try { + DBTDevice *device = getJavaUplinkObject<DBTDevice>(env, obj); + JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); + + if( nullptr == jsource ) { + throw IllegalArgumentException("byte array null", E_FILE_LINE); + } + const size_t source_size = env->GetArrayLength(jsource); + if( sizeof(SMPLongTermKeyInfo) > source_size ) { + throw IllegalArgumentException("byte array "+std::to_string(source_size)+" < "+std::to_string(sizeof(SMPLongTermKeyInfo)), E_FILE_LINE); + } + JNICriticalArray<uint8_t, jbyteArray> criticalArray(env); // RAII - release + uint8_t * source_ptr = criticalArray.get(jsource, criticalArray.Mode::NO_UPDATE_AND_RELEASE); + if( NULL == source_ptr ) { + throw InternalError("GetPrimitiveArrayCritical(byte array) is null", E_FILE_LINE); + } + const SMPLongTermKeyInfo& ltk = *reinterpret_cast<SMPLongTermKeyInfo *>(source_ptr); + + const HCIStatusCode res = device->setLongTermKeyInfo(ltk, JNI_TRUE == responder); + return (jbyte) number(res); + } catch(...) { + rethrow_and_raise_java_exception(env); + } + return (jbyte) number(HCIStatusCode::INTERNAL_FAILURE); +} + jbyte Java_direct_1bt_tinyb_DBTDevice_unpairImpl(JNIEnv *env, jobject obj) { try { DBTDevice *device = getJavaUplinkObject<DBTDevice>(env, obj); |