summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2020-12-10 09:46:45 +0100
committerSven Gothel <[email protected]>2020-12-10 09:46:45 +0100
commitde11465791d2766525febcaa6934154a31d9c733 (patch)
tree203db2f9f4b336f5d883608eb6af8c4cc66475b9 /examples
parentab86d222890076ce26b8638411e490c047add780 (diff)
SMPLongTermKeyInfo: Encode RESPONDER (or INITIATOR) within its Property bit mask, determining key role; ...
DBTDevice::setLongTermKeyInfo() writes given key to pairing_data.ltk_resp or pairing_data.ltk_init to reflect proper state.
Diffstat (limited to 'examples')
-rw-r--r--examples/direct_bt_scanner10/dbt_scanner10.cpp16
-rw-r--r--examples/java/DBTScanner10.java10
2 files changed, 20 insertions, 6 deletions
diff --git a/examples/direct_bt_scanner10/dbt_scanner10.cpp b/examples/direct_bt_scanner10/dbt_scanner10.cpp
index 27ef64e8..1336c71e 100644
--- a/examples/direct_bt_scanner10/dbt_scanner10.cpp
+++ b/examples/direct_bt_scanner10/dbt_scanner10.cpp
@@ -167,11 +167,17 @@ __pack( struct MyLongTermKeyInfo {
bool write(const std::string filename) {
if( !smp_ltk.isValid() ) {
+ fprintf(stderr, "****** WRITE LTK [%s %s, invalid, skipped]: %s\n",
+ address.toString().c_str(), getBDAddressTypeString(address_type).c_str(),
+ smp_ltk.toString().c_str());
return false;
}
std::ofstream file(filename, std::ios::binary | std::ios::trunc);
file.write((char*)this, sizeof(*this));
file.close();
+ fprintf(stderr, "****** WRITE LTK [%s %s, valid, written]: %s\n",
+ address.toString().c_str(), getBDAddressTypeString(address_type).c_str(),
+ smp_ltk.toString().c_str());
return true;
}
@@ -182,6 +188,9 @@ __pack( struct MyLongTermKeyInfo {
}
file.read((char*)this, sizeof(*this));
file.close();
+ fprintf(stderr, "****** READ LTK [%s %s, valid %d]: %s\n",
+ address.toString().c_str(), getBDAddressTypeString(address_type).c_str(),
+ smp_ltk.isValid(), smp_ltk.toString().c_str());
return smp_ltk.isValid();
}
} );
@@ -406,8 +415,8 @@ static void connectDiscoveredDevice(std::shared_ptr<DBTDevice> device) {
MyLongTermKeyInfo my_ltk_init;
if( my_ltk_init.read(device->getAddress().toString()+".init.ltk") &&
my_ltk_resp.read(device->getAddress().toString()+".resp.ltk") &&
- HCIStatusCode::SUCCESS == device->setLongTermKeyInfo(my_ltk_init.smp_ltk, false /* responder */) &&
- HCIStatusCode::SUCCESS == device->setLongTermKeyInfo(my_ltk_resp.smp_ltk, true /* responder */) ) {
+ HCIStatusCode::SUCCESS == device->setLongTermKeyInfo(my_ltk_init.smp_ltk) &&
+ HCIStatusCode::SUCCESS == device->setLongTermKeyInfo(my_ltk_resp.smp_ltk) ) {
fprintf(stderr, "****** Connecting Device: Loaded LTKs from file successfully\n");
} else {
fprintf(stderr, "****** Connecting Device: Error loading LTKs from file\n");
@@ -437,7 +446,8 @@ static void processReadyDevice(std::shared_ptr<DBTDevice> device) {
{
const SMPPairingState pstate = device->getPairingState();
- if( SMPPairingState::COMPLETED == pstate) {
+ const PairingMode pmode = device->getPairingMode(); // Skip PairingMode::PRE_PAIRED (write again)
+ if( SMPPairingState::COMPLETED == pstate && PairingMode::PRE_PAIRED != pmode ) {
{
MyLongTermKeyInfo my_ltk { device->getAddress(), device->getAddressType(),
device->getLongTermKeyInfo(false /* responder */) };
diff --git a/examples/java/DBTScanner10.java b/examples/java/DBTScanner10.java
index 4371ad02..f995bce8 100644
--- a/examples/java/DBTScanner10.java
+++ b/examples/java/DBTScanner10.java
@@ -152,6 +152,7 @@ public class DBTScanner10 {
boolean write(final String filename) {
if( !smp_ltk.isValid() ) {
+ println("****** WRITE LTK ["+address+" "+address_type+", invalid, skipped]: "+smp_ltk+" (write)");
return false;
}
final File file = new File(filename);
@@ -164,6 +165,7 @@ public class DBTScanner10 {
final byte[] smp_ltk_b = new byte[SMPLongTermKeyInfo.byte_size];
smp_ltk.getStream(smp_ltk_b, 0);
out.write(smp_ltk_b);
+ println("****** WRITE LTK ["+address+" "+address_type+", valid, written]: "+smp_ltk);
return true;
} catch (final Exception ex) {
ex.printStackTrace();
@@ -192,6 +194,7 @@ public class DBTScanner10 {
address.putStream(buffer, 0);
address_type = BluetoothAddressType.get(buffer[6]);
smp_ltk.putStream(buffer, 6+1);
+ println("****** READ LTK ["+address+" "+address_type+", valid "+smp_ltk.isValid()+"]: "+smp_ltk);
return smp_ltk.isValid();
} catch (final Exception ex) {
ex.printStackTrace();
@@ -379,8 +382,8 @@ public class DBTScanner10 {
final MyLongTermKeyInfo my_ltk_init = new MyLongTermKeyInfo();
if( my_ltk_init.read(device.getAddress().toString()+".init.ltk") &&
my_ltk_resp.read(device.getAddress().toString()+".resp.ltk") &&
- HCIStatusCode.SUCCESS == device.setLongTermKeyInfo(my_ltk_init.smp_ltk, false /* responder */) &&
- HCIStatusCode.SUCCESS == device.setLongTermKeyInfo(my_ltk_resp.smp_ltk, true /* responder */) ) {
+ HCIStatusCode.SUCCESS == device.setLongTermKeyInfo(my_ltk_init.smp_ltk) &&
+ HCIStatusCode.SUCCESS == device.setLongTermKeyInfo(my_ltk_resp.smp_ltk) ) {
println("****** Connecting Device: Loaded LTKs from file successfully\n");
} else {
println("****** Connecting Device: Error loading LTKs from file\n");
@@ -437,7 +440,8 @@ public class DBTScanner10 {
{
final SMPPairingState pstate = device.getPairingState();
- if( SMPPairingState.COMPLETED == pstate) {
+ final PairingMode pmode = device.getPairingMode(); // Skip PairingMode::PRE_PAIRED (write again)
+ if( SMPPairingState.COMPLETED == pstate && PairingMode.PRE_PAIRED != pmode ) {
{
final MyLongTermKeyInfo my_ltk = new MyLongTermKeyInfo();
my_ltk.address = device.getAddress();