diff options
author | Sven Gothel <[email protected]> | 2020-10-09 12:32:37 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2020-10-09 12:32:37 +0200 |
commit | d5adce54eedbbe17f47db839f47280ba8fcf1f49 (patch) | |
tree | ea206362ddac47cec3ed4001cfb32912b4a3293b /examples | |
parent | a5128ab64f00b9156b4d0d868c1ec6785babd0c1 (diff) |
DBTAdapter::reset(): Promote HCIHandler::reset() to adapter incl Java binding
Ability to reset and power-on and off (the latter was already supported)
might help recovering congested or otherwise stuck adapter.
This is experimental work and has to be tested more thoroughly.
Diffstat (limited to 'examples')
-rw-r--r-- | examples/direct_bt_scanner10/dbt_scanner10.cpp | 32 | ||||
-rw-r--r-- | examples/java/DBTScanner10.java | 25 |
2 files changed, 50 insertions, 7 deletions
diff --git a/examples/direct_bt_scanner10/dbt_scanner10.cpp b/examples/direct_bt_scanner10/dbt_scanner10.cpp index 0009b42d..f8bbbc51 100644 --- a/examples/direct_bt_scanner10/dbt_scanner10.cpp +++ b/examples/direct_bt_scanner10/dbt_scanner10.cpp @@ -31,6 +31,8 @@ #include <cinttypes> +#include <pthread.h> + #include <direct_bt/DirectBT.hpp> #include "direct_bt/dfa_utf8_decode.hpp" @@ -52,6 +54,10 @@ using namespace direct_bt; static int64_t timestamp_t0; + +static int RESET_ADAPTER_EACH_CONN = 0; +static std::atomic<int> connectionCount = 0; + static std::atomic<int> MULTI_MEASUREMENTS = 8; static bool KEEP_CONNECTED = true; @@ -71,8 +77,7 @@ 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 void resetAdapter(DBTAdapter *a); static std::vector<EUI48> devicesInProcessing; static std::recursive_mutex mtx_devicesProcessing; @@ -184,7 +189,7 @@ class MyAdapterStatusListener : public AdapterStatusListener { const uint64_t td = getCurrentMilliseconds() - timestamp_t0; // adapter-init -> now fprintf(stderr, "PERF: adapter-init -> FOUND__-0 %" PRIu64 " ms\n", td); } - std::thread dc(::connectDiscoveredDevice, device); + std::thread dc(::connectDiscoveredDevice, device); // @suppress("Invalid arguments") dc.detach(); } else { fprintf(stderr, "****** FOUND__-1: NOP %s\n", device->toString(true).c_str()); @@ -216,7 +221,7 @@ class MyAdapterStatusListener : public AdapterStatusListener { fprintf(stderr, "PERF: adapter-init -> CONNECTED-0 %" PRIu64 " ms\n", td); } addToDevicesProcessing(device->getAddress()); - std::thread dc(::processConnectedDevice, device); + std::thread dc(::processConnectedDevice, device); // @suppress("Invalid arguments") dc.detach(); } else { fprintf(stderr, "****** CONNECTED-1: NOP %s\n", device->toString(true).c_str()); @@ -229,12 +234,16 @@ class MyAdapterStatusListener : public AdapterStatusListener { (void)timestamp; if( REMOVE_DEVICE ) { - std::thread dc(::removeDevice, device); + std::thread dc(::removeDevice, device); // @suppress("Invalid arguments") dc.detach(); } else { removeFromDevicesProcessing(device->getAddress()); } - + connectionCount++; + if( 0 == connectionCount % RESET_ADAPTER_EACH_CONN ) { + std::thread dc(::resetAdapter, &device->getAdapter()); // @suppress("Invalid arguments") + dc.detach(); + } } std::string toString() const override { @@ -471,6 +480,12 @@ static void removeDevice(std::shared_ptr<DBTDevice> device) { } } +static void resetAdapter(DBTAdapter *a) { + fprintf(stderr, "****** Reset Adapter: reset start: %s\n", a->toString().c_str()); + HCIStatusCode res = a->reset(); + fprintf(stderr, "****** Reset Adapter: reset end: %s, %s\n", getHCIStatusCodeString(res).c_str(), a->toString().c_str()); +} + void test(int dev_id) { bool done = false; @@ -578,12 +593,15 @@ int main(int argc, char *argv[]) MULTI_MEASUREMENTS = atoi(argv[++i]); } else if( !strcmp("-single", argv[i]) ) { MULTI_MEASUREMENTS = -1; + } else if( !strcmp("-resetEachCon", argv[i]) && argc > (i+1) ) { + RESET_ADAPTER_EACH_CONN = atoi(argv[++i]); } } fprintf(stderr, "pid %d\n", getpid()); fprintf(stderr, "Run with '[-dev_id <adapter-index>] [-btmode LE|BREDR|DUAL] " "[-disconnect] [-enableGATTPing] [-count <number>] [-single] [-show_update_events] [-quiet] " + "[-resetEachCon connectionCount] " "(-mac <device_address>)* (-wl <device_address>)* " "[-dbt_verbose true|false] " "[-dbt_debug true|false|adapter.event,gatt.data,hci.event,mgmt.event] " @@ -595,6 +613,7 @@ int main(int argc, char *argv[]) fprintf(stderr, "MULTI_MEASUREMENTS %d\n", MULTI_MEASUREMENTS.load()); fprintf(stderr, "KEEP_CONNECTED %d\n", KEEP_CONNECTED); + fprintf(stderr, "RESET_ADAPTER_EACH_CONN %d\n", RESET_ADAPTER_EACH_CONN); 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); @@ -602,6 +621,7 @@ int main(int argc, char *argv[]) fprintf(stderr, "QUIET %d\n", QUIET); fprintf(stderr, "dev_id %d\n", dev_id); fprintf(stderr, "btmode %s\n", getBTModeString(btMode).c_str()); + printList( "waitForDevice: ", waitForDevices); if( waitForEnter ) { diff --git a/examples/java/DBTScanner10.java b/examples/java/DBTScanner10.java index 81d73ed4..a7027008 100644 --- a/examples/java/DBTScanner10.java +++ b/examples/java/DBTScanner10.java @@ -71,6 +71,9 @@ public class DBTScanner10 { long timestamp_t0; + int RESET_ADAPTER_EACH_CONN = 0; + AtomicInteger connectionCount = new AtomicInteger(0); + AtomicInteger MULTI_MEASUREMENTS = new AtomicInteger(8); boolean KEEP_CONNECTED = true; boolean GATT_PING_ENABLED = false; @@ -218,6 +221,16 @@ public class DBTScanner10 { } else { devicesInProcessing.remove(device.getAddress()); } + if( 0 == connectionCount.addAndGet(1) % RESET_ADAPTER_EACH_CONN ) { + final Thread adapterResetTask = new Thread( new Runnable() { + @Override + public void run() { + resetAdapter(device.getAdapter()); + } + }, "DBT-Reset-"+device.getAdapter().getAddress()); + adapterResetTask.setDaemon(true); // detach thread + adapterResetTask.start(); + } } }; @@ -495,6 +508,12 @@ public class DBTScanner10 { } } + private void resetAdapter(final BluetoothAdapter adapter) { + println("****** Reset Adapter: reset start: "+adapter.toString()); + final HCIStatusCode res = adapter.reset(); + println("****** Reset Adapter: reset end: "+res+", "+adapter.toString()); + } + public void runTest(final BluetoothManager manager) { final BluetoothAdapter adapter; { @@ -646,11 +665,14 @@ public class DBTScanner10 { test.MULTI_MEASUREMENTS.set(Integer.valueOf(args[++i]).intValue()); } else if( arg.equals("-single") ) { test.MULTI_MEASUREMENTS.set(-1); + } else if( arg.equals("-resetEachCon") && args.length > (i+1) ) { + test.RESET_ADAPTER_EACH_CONN = Integer.valueOf(args[++i]).intValue(); } } println("Run with '[-default_dev_id <adapter-index>] [-dev_id <adapter-index>] [-btmode LE|BREDR|DUAL] "+ "[-bluetoothManager <BluetoothManager-Implementation-Class-Name>] "+ - "[-disconnect] [-enableGATTPing] [-count <number>] [-single] (-char <uuid>)* [-show_update_events] [-quiet]"+ + "[-disconnect] [-enableGATTPing] [-count <number>] [-single] (-char <uuid>)* [-show_update_events] [-quiet] "+ + "[-resetEachCon connectionCount] "+ "(-mac <device_address>)* (-wl <device_address>)* "+ "[-verbose] [-debug] "+ "[-dbt_verbose true|false] "+ @@ -664,6 +686,7 @@ public class DBTScanner10 { println("MULTI_MEASUREMENTS "+test.MULTI_MEASUREMENTS.get()); println("KEEP_CONNECTED "+test.KEEP_CONNECTED); + println("RESET_ADAPTER_EACH_CONN "+test.RESET_ADAPTER_EACH_CONN); println("GATT_PING_ENABLED "+test.GATT_PING_ENABLED); println("REMOVE_DEVICE "+test.REMOVE_DEVICE); println("USE_WHITELIST "+test.USE_WHITELIST); |