From 53eef8bf45c0500c68af83a5c57f6b348d995c5c Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Thu, 24 Sep 2020 11:40:55 +0200 Subject: 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" --- examples/direct_bt_scanner10/dbt_scanner10.cpp | 46 +++++++++++++++++++------- 1 file changed, 34 insertions(+), 12 deletions(-) (limited to 'examples/direct_bt_scanner10') 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 device); static void processConnectedDevice(std::shared_ptr device); +static void removeDevice(std::shared_ptr device); + #include static std::vector devicesInProcessing; @@ -207,11 +210,20 @@ class MyAdapterStatusListener : public AdapterStatusListener { static_cast(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 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 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 ] [-btmode LE|BREDR|DUAL] " - "[-disconnect] [-count ] [-single] [-show_update_events] [-silent_gatt] " + "[-disconnect] [-enableGATTPing] [-count ] [-single] [-show_update_events] [-silent_gatt] " "(-mac )* (-wl )* " "[-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); -- cgit v1.2.3