summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2020-09-24 11:40:55 +0200
committerSven Gothel <[email protected]>2020-09-24 11:40:55 +0200
commit53eef8bf45c0500c68af83a5c57f6b348d995c5c (patch)
treee63cf9c1b3291309a06cd36b290dc8f67df507ba
parent0dff786ec6b19a0c2e79a6dfac2a36ff9fb1e8eb (diff)
dbt_scanner10 + ScannerTinyB10: Make gatt-ping optional and always cleanup on disconnect event
This is possible due to commit a2096bf3baf8fb5fc8ed37ce4bdb1ec1112b6e8d "LE connection parameters for higher transmission robusteness w/o using a ping-gatt link-loss check"
-rw-r--r--examples/direct_bt_scanner10/dbt_scanner10.cpp46
-rw-r--r--examples/java/ScannerTinyB10.java48
2 files changed, 70 insertions, 24 deletions
diff --git a/examples/direct_bt_scanner10/dbt_scanner10.cpp b/examples/direct_bt_scanner10/dbt_scanner10.cpp
index 81770ee2..52245207 100644
--- a/examples/direct_bt_scanner10/dbt_scanner10.cpp
+++ b/examples/direct_bt_scanner10/dbt_scanner10.cpp
@@ -55,6 +55,7 @@ static int64_t timestamp_t0;
static int MULTI_MEASUREMENTS = 8;
static bool KEEP_CONNECTED = true;
+static bool GATT_PING_ENABLED = false;
static bool REMOVE_DEVICE = true;
static bool USE_WHITELIST = false;
@@ -69,6 +70,8 @@ static void connectDiscoveredDevice(std::shared_ptr<DBTDevice> device);
static void processConnectedDevice(std::shared_ptr<DBTDevice> device);
+static void removeDevice(std::shared_ptr<DBTDevice> device);
+
#include <pthread.h>
static std::vector<EUI48> devicesInProcessing;
@@ -207,11 +210,20 @@ class MyAdapterStatusListener : public AdapterStatusListener {
static_cast<uint8_t>(reason), getHCIStatusCodeString(reason).c_str(),
uint16HexString(handle).c_str(), device->toString(true).c_str());
(void)timestamp;
+
+ if( REMOVE_DEVICE ) {
+ std::thread dc(::removeDevice, device);
+ dc.detach();
+ } else {
+ removeFromDevicesProcessing(device->getAddress());
+ }
+
}
std::string toString() const override {
return "MyAdapterStatusListener[this "+aptrHexString(this)+"]";
}
+
};
static const uuid16_t _TEMPERATURE_MEASUREMENT(GattCharacteristicType::TEMPERATURE_MEASUREMENT);
@@ -395,25 +407,17 @@ static void processConnectedDevice(std::shared_ptr<DBTDevice> device) {
}
exit:
- removeFromDevicesProcessing(device->getAddress());
if( !USE_WHITELIST && 0 == devicesInProcessing.size() ) {
device->getAdapter().startDiscovery( true );
}
- if( KEEP_CONNECTED ) {
+ if( KEEP_CONNECTED && GATT_PING_ENABLED && success ) {
while( device->pingGATT() ) {
fprintf(stderr, "****** Processing Device: pingGATT OK: %s\n", device->getAddressString().c_str());
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
- fprintf(stderr, "****** Processing Device: pingGATT failed: %s\n", device->getAddressString().c_str());
- }
-
- if( REMOVE_DEVICE ) {
- fprintf(stderr, "****** Processing Device: removing: %s\n", device->getAddressString().c_str());
- device->remove();
- } else {
- fprintf(stderr, "****** Processing Device: disconnecting: %s\n", device->getAddressString().c_str());
- device->disconnect(); // will implicitly purge the GATT data, including GATTCharacteristic listener.
+ fprintf(stderr, "****** Processing Device: pingGATT failed, waiting for disconnect: %s\n", device->getAddressString().c_str());
+ // Even w/ GATT_PING_ENABLED, we utilize disconnect event to clean up -> remove
}
if( !SILENT_GATT ) {
@@ -424,13 +428,28 @@ exit:
MULTI_MEASUREMENTS--;
fprintf(stderr, "****** Processing Device: MULTI_MEASUREMENTS left %d: %s\n", MULTI_MEASUREMENTS, device->getAddressString().c_str());
}
+
fprintf(stderr, "****** Processing Device: End: Success %d on %s; devInProc %zd\n",
success, device->toString().c_str(), devicesInProcessing.size());
+
if( success ) {
addToDevicesProcessed(device->getAddress());
}
}
+static void removeDevice(std::shared_ptr<DBTDevice> device) {
+ fprintf(stderr, "****** Remove Device: removing: %s\n", device->getAddressString().c_str());
+ device->getAdapter().stopDiscovery();
+
+ removeFromDevicesProcessing(device->getAddress());
+
+ device->remove();
+
+ if( !USE_WHITELIST && 0 == devicesInProcessing.size() ) {
+ device->getAdapter().startDiscovery( true );
+ }
+}
+
void test(int dev_id) {
bool done = false;
@@ -530,6 +549,8 @@ int main(int argc, char *argv[])
USE_WHITELIST = true;
} else if( !strcmp("-disconnect", argv[i]) ) {
KEEP_CONNECTED = false;
+ } else if( !strcmp("-enableGATTPing", argv[i]) ) {
+ GATT_PING_ENABLED = true;
} else if( !strcmp("-keepDevice", argv[i]) ) {
REMOVE_DEVICE = false;
} else if( !strcmp("-count", argv[i]) && argc > (i+1) ) {
@@ -541,7 +562,7 @@ int main(int argc, char *argv[])
fprintf(stderr, "pid %d\n", getpid());
fprintf(stderr, "Run with '[-dev_id <adapter-index>] [-btmode LE|BREDR|DUAL] "
- "[-disconnect] [-count <number>] [-single] [-show_update_events] [-silent_gatt] "
+ "[-disconnect] [-enableGATTPing] [-count <number>] [-single] [-show_update_events] [-silent_gatt] "
"(-mac <device_address>)* (-wl <device_address>)* "
"[-dbt_verbose true|false] "
"[-dbt_debug true|false|adapter.event,gatt.data,hci.event,mgmt.event] "
@@ -553,6 +574,7 @@ int main(int argc, char *argv[])
fprintf(stderr, "MULTI_MEASUREMENTS %d\n", MULTI_MEASUREMENTS);
fprintf(stderr, "KEEP_CONNECTED %d\n", KEEP_CONNECTED);
+ fprintf(stderr, "GATT_PING_ENABLED %d\n", GATT_PING_ENABLED);
fprintf(stderr, "REMOVE_DEVICE %d\n", REMOVE_DEVICE);
fprintf(stderr, "USE_WHITELIST %d\n", USE_WHITELIST);
fprintf(stderr, "SHOW_UPDATE_EVENTS %d\n", SHOW_UPDATE_EVENTS);
diff --git a/examples/java/ScannerTinyB10.java b/examples/java/ScannerTinyB10.java
index 55e71af3..82d41a89 100644
--- a/examples/java/ScannerTinyB10.java
+++ b/examples/java/ScannerTinyB10.java
@@ -74,6 +74,7 @@ public class ScannerTinyB10 {
int MULTI_MEASUREMENTS = 8;
boolean KEEP_CONNECTED = true;
+ boolean GATT_PING_ENABLED = false;
boolean REMOVE_DEVICE = true;
boolean USE_WHITELIST = false;
final List<String> whitelist = new ArrayList<String>();
@@ -199,6 +200,19 @@ public class ScannerTinyB10 {
@Override
public void deviceDisconnected(final BluetoothDevice device, final HCIStatusCode reason, final short handle, final long timestamp) {
println("****** DISCONNECTED: Reason "+reason+", old handle 0x"+Integer.toHexString(handle)+": "+device+" on "+device.getAdapter());
+
+ if( REMOVE_DEVICE ) {
+ final Thread deviceRemoverProcessingTask = new Thread( new Runnable() {
+ @Override
+ public void run() {
+ removeDevice(device);
+ }
+ }, "DBT-Remove-"+device.getAddress());
+ deviceRemoverProcessingTask.setDaemon(true); // detach thread
+ deviceRemoverProcessingTask.start();
+ } else {
+ devicesInProcessing.remove(device.getAddress());
+ }
}
};
@@ -430,13 +444,12 @@ public class ScannerTinyB10 {
t.printStackTrace();
}
- devicesInProcessing.remove(device.getAddress());
if( !USE_WHITELIST && 0 == devicesInProcessing.size() ) {
final boolean r = device.getAdapter().startDiscovery( true );
println("****** Processing Device: startDiscovery result "+r);
}
- if( KEEP_CONNECTED && success ) {
+ if( KEEP_CONNECTED && GATT_PING_ENABLED && success ) {
while( device.pingGATT() ) {
println("****** Processing Device: pingGATT OK: "+device.getAddress());
try {
@@ -445,21 +458,15 @@ public class ScannerTinyB10 {
e.printStackTrace();
}
}
- println("****** Processing Device: pingGATT failed: "+device.getAddress());
- }
-
- if( REMOVE_DEVICE ) {
- println("****** Processing Device: removing: "+device.getAddress());
- device.remove();
- } else {
- println("****** Processing Device: disconnecting: "+device.getAddress());
- device.disconnect(); // will implicitly purge the GATT data, including GATTCharacteristic listener.
+ println("****** Processing Device: pingGATT failed, waiting for disconnect: "+device.getAddress());
+ // Even w/ GATT_PING_ENABLED, we utilize disconnect event to clean up -> remove
}
if( 0 < MULTI_MEASUREMENTS ) {
MULTI_MEASUREMENTS--;
println("****** Processing Device: MULTI_MEASUREMENTS left "+MULTI_MEASUREMENTS+": "+device.getAddress());
}
+
println("****** Processing Device: End: Success " + success +
" on " + device.toString() + "; devInProc "+devicesInProcessing.size());
if( success ) {
@@ -467,6 +474,20 @@ public class ScannerTinyB10 {
}
}
+ private void removeDevice(final BluetoothDevice device) {
+ println("****** Remove Device: removing: "+device.getAddress());
+ device.getAdapter().stopDiscovery();
+
+ devicesInProcessing.remove(device.getAddress());
+
+ device.remove();
+
+ if( !USE_WHITELIST && 0 == devicesInProcessing.size() ) {
+ final boolean r = device.getAdapter().startDiscovery( true );
+ println("****** Remove Device: startDiscovery result "+r);
+ }
+ }
+
public ScannerTinyB10(final String bluetoothManagerClazzName) {
BluetoothManager _manager = null;
final BluetoothFactory.ImplementationIdentifier implID = BluetoothFactory.getImplementationIdentifier(bluetoothManagerClazzName);
@@ -630,6 +651,8 @@ public class ScannerTinyB10 {
test.charIdentifierList.add(args[++i]);
} else if( arg.equals("-disconnect") ) {
test.KEEP_CONNECTED = false;
+ } else if( arg.equals("-enableGATTPing") ) {
+ test.GATT_PING_ENABLED = true;
} else if( arg.equals("-keepDevice") ) {
test.REMOVE_DEVICE = false;
} else if( arg.equals("-count") && args.length > (i+1) ) {
@@ -640,7 +663,7 @@ public class ScannerTinyB10 {
}
println("Run with '[-default_dev_id <adapter-index>] [-dev_id <adapter-index>] [-btmode LE|BREDR|DUAL] "+
"[-bluetoothManager <BluetoothManager-Implementation-Class-Name>] "+
- "[-disconnect] [-count <number>] [-single] (-char <uuid>)* [-show_update_events] [-silent_gatt]"+
+ "[-disconnect] [-enableGATTPing] [-count <number>] [-single] (-char <uuid>)* [-show_update_events] [-silent_gatt]"+
"(-mac <device_address>)* (-wl <device_address>)* "+
"[-verbose] [-debug] "+
"[-dbt_verbose true|false] "+
@@ -655,6 +678,7 @@ public class ScannerTinyB10 {
println("BluetoothManager "+bluetoothManagerClazzName);
println("MULTI_MEASUREMENTS "+test.MULTI_MEASUREMENTS);
println("KEEP_CONNECTED "+test.KEEP_CONNECTED);
+ println("GATT_PING_ENABLED "+test.GATT_PING_ENABLED);
println("REMOVE_DEVICE "+test.REMOVE_DEVICE);
println("USE_WHITELIST "+test.USE_WHITELIST);
println("SHOW_UPDATE_EVENTS "+test.SHOW_UPDATE_EVENTS);