diff options
author | Sven Gothel <[email protected]> | 2020-08-22 18:32:18 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2020-08-22 18:32:18 +0200 |
commit | 1f39f6c21c84e466d2563d4ead1470c3c5b5143b (patch) | |
tree | 7166631a4f0081a6370d06488daad82130d88bb5 /examples | |
parent | 536975dd325723593e38efecde061d8c7c95c707 (diff) |
DBTAdapter: Have non-enabled adapter to be valid, use lazy HCI initialization; DBTManager: Validate enabled for non-user default adapter.
DBTAdapter: Have non-enabled adapter to be valid, use lazy HCI initialization
In a multi adapter setting, it is possible to have one adapter not being enabled,
i.e. blocked from operations (e.g. rfkill).
HCI communication is not possible in such cases and 'openHCI()' will fail.
We still need to treat such adapter as valid, as they are,
and distinguish the states: 'valid' and 'enabled'.
This allows 'delivery' of all adapters to a manager
and allows querying whether they are enabled.
Therefor, the native DBTAdapter will not issue an aggregated 'openHCI()'
at construction and initial validation, but treat the HCI instance as a singleton
to be lazily created via 'getHCI()' if not yet existing.
Here the required listener are also attached to the instance.
In the future, this shall also allow dynamically adding/removing
and enabling/disabling adapters.
Hence before using a chosen adapter, one should query its enabled state
via 'isEnabled()', which will also potentially initializing HCI.
+++
DBTManager: Validate enabled for non-user default adapter.
On the Java side, we can chose a default adapter via the property 'org.tinyb.default_adapter'.
If so chosen, it shall be the default adapter, regardless of its 'enabled' state.
Otherwise, the default adapter shall be the first enabled adapter in the list.
Diffstat (limited to 'examples')
-rw-r--r-- | examples/direct_bt_scanner00/dbt_scanner00.cpp | 5 | ||||
-rw-r--r-- | examples/direct_bt_scanner01/dbt_scanner01.cpp | 5 | ||||
-rw-r--r-- | examples/direct_bt_scanner10/dbt_scanner10.cpp | 6 | ||||
-rw-r--r-- | examples/java/ScannerTinyB00.java | 4 | ||||
-rw-r--r-- | examples/java/ScannerTinyB01.java | 4 | ||||
-rw-r--r-- | examples/java/ScannerTinyB02.java | 4 | ||||
-rw-r--r-- | examples/java/ScannerTinyB10.java | 20 |
7 files changed, 45 insertions, 3 deletions
diff --git a/examples/direct_bt_scanner00/dbt_scanner00.cpp b/examples/direct_bt_scanner00/dbt_scanner00.cpp index 48939bdc..e7348b30 100644 --- a/examples/direct_bt_scanner00/dbt_scanner00.cpp +++ b/examples/direct_bt_scanner00/dbt_scanner00.cpp @@ -185,6 +185,11 @@ int main(int argc, char *argv[]) fprintf(stderr, "Adapter invalid.\n"); exit(1); } + if( !adapter.isEnabled() ) { + fprintf(stderr, "Adapter not enabled: device %s, address %s: %s\n", + adapter.getName().c_str(), adapter.getAddressString().c_str(), adapter.toString().c_str()); + exit(1); + } fprintf(stderr, "Using adapter: device %s, address %s: %s\n", adapter.getName().c_str(), adapter.getAddressString().c_str(), adapter.toString().c_str()); diff --git a/examples/direct_bt_scanner01/dbt_scanner01.cpp b/examples/direct_bt_scanner01/dbt_scanner01.cpp index 5f57f3ee..ee1f0c39 100644 --- a/examples/direct_bt_scanner01/dbt_scanner01.cpp +++ b/examples/direct_bt_scanner01/dbt_scanner01.cpp @@ -181,6 +181,11 @@ int main(int argc, char *argv[]) fprintf(stderr, "Adapter invalid.\n"); exit(1); } + if( !adapter.isEnabled() ) { + fprintf(stderr, "Adapter not enabled: device %s, address %s: %s\n", + adapter.getName().c_str(), adapter.getAddressString().c_str(), adapter.toString().c_str()); + exit(1); + } fprintf(stderr, "Using adapter: device %s, address %s: %s\n", adapter.getName().c_str(), adapter.getAddressString().c_str(), adapter.toString().c_str()); diff --git a/examples/direct_bt_scanner10/dbt_scanner10.cpp b/examples/direct_bt_scanner10/dbt_scanner10.cpp index 929d5d49..f1490d23 100644 --- a/examples/direct_bt_scanner10/dbt_scanner10.cpp +++ b/examples/direct_bt_scanner10/dbt_scanner10.cpp @@ -158,6 +158,7 @@ class MyAdapterStatusListener : public AdapterStatusListener { } void deviceConnected(std::shared_ptr<DBTDevice> device, const uint16_t handle, const uint64_t timestamp) override { + (void)handle; (void)timestamp; if( !isDeviceProcessing( device->getAddress() ) && @@ -371,6 +372,11 @@ void test(int dev_id) { fprintf(stderr, "Adapter invalid.\n"); exit(1); } + if( !adapter.isEnabled() ) { + fprintf(stderr, "Adapter not enabled: device %s, address %s: %s\n", + adapter.getName().c_str(), adapter.getAddressString().c_str(), adapter.toString().c_str()); + exit(1); + } fprintf(stderr, "Using adapter: device %s, address %s: %s\n", adapter.getName().c_str(), adapter.getAddressString().c_str(), adapter.toString().c_str()); diff --git a/examples/java/ScannerTinyB00.java b/examples/java/ScannerTinyB00.java index e3b00648..286375ca 100644 --- a/examples/java/ScannerTinyB00.java +++ b/examples/java/ScannerTinyB00.java @@ -116,6 +116,10 @@ public class ScannerTinyB00 { System.exit(-1); } adapter = adapters.get(dev_id); + if( !adapter.isEnabled() ) { + System.err.println("Adapter not enabled: device "+adapter.getName()+", address "+adapter.getAddress()+": "+adapter.toString()); + System.exit(-1); + } } do { diff --git a/examples/java/ScannerTinyB01.java b/examples/java/ScannerTinyB01.java index 8e294561..94f75611 100644 --- a/examples/java/ScannerTinyB01.java +++ b/examples/java/ScannerTinyB01.java @@ -139,6 +139,10 @@ public class ScannerTinyB01 { System.exit(-1); } adapter = adapters.get(dev_id); + if( !adapter.isEnabled() ) { + System.err.println("Adapter not enabled: device "+adapter.getName()+", address "+adapter.getAddress()+": "+adapter.toString()); + System.exit(-1); + } } final BluetoothDevice[] matchingDiscoveredDeviceBucket = { null }; diff --git a/examples/java/ScannerTinyB02.java b/examples/java/ScannerTinyB02.java index 9ccfceec..75245920 100644 --- a/examples/java/ScannerTinyB02.java +++ b/examples/java/ScannerTinyB02.java @@ -129,6 +129,10 @@ public class ScannerTinyB02 { System.exit(-1); } adapter = adapters.get(dev_id); + if( !adapter.isEnabled() ) { + System.err.println("Adapter not enabled: device "+adapter.getName()+", address "+adapter.getAddress()+": "+adapter.toString()); + System.exit(-1); + } } final BluetoothDevice[] matchingDiscoveredDeviceBucket = { null }; diff --git a/examples/java/ScannerTinyB10.java b/examples/java/ScannerTinyB10.java index af57e867..844f169f 100644 --- a/examples/java/ScannerTinyB10.java +++ b/examples/java/ScannerTinyB10.java @@ -70,7 +70,7 @@ public class ScannerTinyB10 { boolean SHOW_UPDATE_EVENTS = false; - int dev_id = 0; // default + int dev_id = -1; // use default int shutdownTest = 0; @@ -407,7 +407,15 @@ public class ScannerTinyB10 { println("No adapter dev_id "+dev_id+" available, adapter count "+adapters.size()); System.exit(-1); } - adapter = adapters.get(dev_id); + if( 0 > dev_id ) { + adapter = manager.getDefaultAdapter(); + } else { + adapter = adapters.get(dev_id); + } + if( !adapter.isEnabled() ) { + println("Adapter not enabled: device "+adapter.getName()+", address "+adapter.getAddress()+": "+adapter.toString()); + System.exit(-1); + } } timestamp_t0 = BluetoothUtils.getCurrentMilliseconds(); @@ -470,6 +478,12 @@ public class ScannerTinyB10 { System.setProperty("org.tinyb.debug", "true"); } else if( arg.equals("-verbose") ) { System.setProperty("org.tinyb.verbose", "true"); + } else if( arg.equals("-default_dev_id") && args.length > (i+1) ) { + final int default_dev_id = Integer.valueOf(args[++i]).intValue(); + if( 0 <= default_dev_id ) { + System.setProperty("org.tinyb.default_adapter", String.valueOf(default_dev_id)); + System.err.println("Setting 'org.tinyb.default_adapter' to "+default_dev_id); + } } } // Drop BluetoothGattCharacteristic value cache and notification compatibility using direct_bt. @@ -512,7 +526,7 @@ public class ScannerTinyB10 { test.MULTI_MEASUREMENTS = -1; } } - println("Run with '[-dev_id <adapter-index>] (-mac <device_address>)* [-disconnect] [-count <number>] [-single] (-wl <device_address>)* (-char <uuid>)* [-show_update_events] [-bluetoothManager <BluetoothManager-Implementation-Class-Name>] [-verbose] [-debug] [-shutdown <int>]'"); + println("Run with '[-default_dev_id <adapter-index>] [-dev_id <adapter-index>] (-mac <device_address>)* [-disconnect] [-count <number>] [-single] (-wl <device_address>)* (-char <uuid>)* [-show_update_events] [-bluetoothManager <BluetoothManager-Implementation-Class-Name>] [-verbose] [-debug] [-shutdown <int>]'"); } println("BluetoothManager "+bluetoothManagerClazzName); |