diff options
author | Sven Gothel <[email protected]> | 2020-10-13 02:19:02 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2020-10-13 02:19:02 +0200 |
commit | f3acf0e335bf65e67aa5b4d292bb5446ffbde271 (patch) | |
tree | a206c9b4aa0d550fe0d73f9d7e7d24e5680ad6d0 | |
parent | d5adce54eedbbe17f47db839f47280ba8fcf1f49 (diff) |
AdapterStatusListener[1] and DBTAdapter[2] API Refinement: [1] non const DBTAdapter, [2] use HCIStatusCode for start/stopDiscovery
and well as [2] return boolean for set[Discoverable|Bondable|Powered](..).
AdapterStatusListener needs to pass non-const DBTAdapter to allow actionable modification of adapter state,
e.g. start/stopDiscovery.
HCIStatusCode is desired for start/stopDiscovery, allowing user to read actual HCI status code on failure.
boolean return value for set[Discoverable|Bondable|Powered](..) reflecting general success or failure
on these basic commands.
-rw-r--r-- | api/direct_bt/DBTAdapter.hpp | 26 | ||||
-rw-r--r-- | examples/direct_bt_scanner00/dbt_scanner00.cpp | 6 | ||||
-rw-r--r-- | examples/direct_bt_scanner01/dbt_scanner01.cpp | 6 | ||||
-rw-r--r-- | examples/direct_bt_scanner10/dbt_scanner10.cpp | 29 | ||||
-rw-r--r-- | examples/java/DBTScanner10.java | 43 | ||||
-rw-r--r-- | examples/java/ScannerTinyB00.java | 4 | ||||
-rw-r--r-- | examples/java/ScannerTinyB01.java | 6 | ||||
-rw-r--r-- | java/direct_bt/tinyb/DBTAdapter.java | 24 | ||||
-rw-r--r-- | java/direct_bt/tinyb/DBTManager.java | 7 | ||||
-rw-r--r-- | java/jni/direct_bt/DBTAdapter.cxx | 31 | ||||
-rw-r--r-- | java/jni/tinyb/DBusAdapter.cxx | 22 | ||||
-rw-r--r-- | java/jni/tinyb/DBusManager.cxx | 2 | ||||
-rw-r--r-- | java/org/tinyb/BluetoothAdapter.java | 62 | ||||
-rw-r--r-- | java/org/tinyb/BluetoothManager.java | 16 | ||||
-rw-r--r-- | java/tinyb/dbus/DBusAdapter.java | 19 | ||||
-rw-r--r-- | java/tinyb/dbus/DBusManager.java | 10 | ||||
-rw-r--r-- | src/direct_bt/DBTAdapter.cpp | 70 |
17 files changed, 214 insertions, 169 deletions
diff --git a/api/direct_bt/DBTAdapter.hpp b/api/direct_bt/DBTAdapter.hpp index 46ff0c65..c137d676 100644 --- a/api/direct_bt/DBTAdapter.hpp +++ b/api/direct_bt/DBTAdapter.hpp @@ -77,23 +77,23 @@ namespace direct_bt { /** * DBTAdapter setting(s) changed. - * @param a the adapter which settings have changed. + * @param adapter the adapter which settings have changed. * @param oldmask the previous settings mask * @param newmask the new settings mask * @param changedmask the changes settings mask * @param timestamp the time in monotonic milliseconds when this event occurred. See BasicTypes::getCurrentMilliseconds(). */ - virtual void adapterSettingsChanged(DBTAdapter const &a, const AdapterSetting oldmask, const AdapterSetting newmask, + virtual void adapterSettingsChanged(DBTAdapter &adapter, const AdapterSetting oldmask, const AdapterSetting newmask, const AdapterSetting changedmask, const uint64_t timestamp) = 0; /** * DBTAdapter's discovery state has changed, i.e. enabled or disabled. - * @param a the adapter which discovering state has changed. + * @param adapter the adapter which discovering state has changed. * @param enabled the new discovery state * @param keepAlive if {@code true}, the discovery will be re-enabled if disabled by the underlying Bluetooth implementation. * @param timestamp the time in monotonic milliseconds when this event occurred. See BasicTypes::getCurrentMilliseconds(). */ - virtual void discoveringChanged(DBTAdapter const &a, const bool enabled, const bool keepAlive, const uint64_t timestamp) = 0; + virtual void discoveringChanged(DBTAdapter &adapter, const bool enabled, const bool keepAlive, const uint64_t timestamp) = 0; /** * A DBTDevice has been newly discovered. @@ -336,17 +336,17 @@ namespace direct_bt { /** * Set the discoverable state of the adapter. */ - void setDiscoverable(bool value) noexcept; + bool setDiscoverable(bool value) noexcept; /** * Set the bondable (aka pairable) state of the adapter. */ - void setBondable(bool value) noexcept; + bool setBondable(bool value) noexcept; /** * Set the power state of the adapter. */ - void setPowered(bool value) noexcept; + bool setPowered(bool value) noexcept; /** * Reset the adapter. @@ -444,7 +444,7 @@ namespace direct_bt { /** * Starts a new discovery session. * <p> - * Returns true if successful, otherwise false; + * Returns HCIStatusCode::SUCCESS if successful, otherwise the HCIStatusCode error state; * </p> * <p> * if {@code keepAlive} is {@code true}, discovery state will be re-enabled @@ -482,10 +482,10 @@ namespace direct_bt { * @param own_mac_type * @param le_scan_interval in units of 0.625ms, default value 24 for 15ms; Value range [4 .. 0x4000] for [2.5ms .. 10.24s] * @param le_scan_window in units of 0.625ms, default value 24 for 15ms; Value range [4 .. 0x4000] for [2.5ms .. 10.24s]. Shall be <= le_scan_interval - * @return + * @return HCIStatusCode::SUCCESS if successful, otherwise the HCIStatusCode error state */ - bool startDiscovery(const bool keepAlive=true, const HCILEOwnAddressType own_mac_type=HCILEOwnAddressType::PUBLIC, - const uint16_t le_scan_interval=24, const uint16_t le_scan_window=24); + HCIStatusCode startDiscovery(const bool keepAlive=true, const HCILEOwnAddressType own_mac_type=HCILEOwnAddressType::PUBLIC, + const uint16_t le_scan_interval=24, const uint16_t le_scan_window=24); /** * Closes the discovery session. @@ -493,9 +493,9 @@ namespace direct_bt { * This adapter's HCIHandler instance is used to stop scanning, * see HCIHandler::le_enable_scan(). * </p> - * @return true if no error, otherwise false. + * @return HCIStatusCode::SUCCESS if successful, otherwise the HCIStatusCode error state */ - bool stopDiscovery() noexcept; + HCIStatusCode stopDiscovery() noexcept; /** * Returns the meta discovering state. It can be modified through startDiscovery(..) and stopDiscovery(). diff --git a/examples/direct_bt_scanner00/dbt_scanner00.cpp b/examples/direct_bt_scanner00/dbt_scanner00.cpp index df37a103..0940de7e 100644 --- a/examples/direct_bt_scanner00/dbt_scanner00.cpp +++ b/examples/direct_bt_scanner00/dbt_scanner00.cpp @@ -51,7 +51,7 @@ std::mutex mtxDeviceFound; std::condition_variable cvDeviceFound; class MyAdapterStatusListener : public AdapterStatusListener { - void adapterSettingsChanged(DBTAdapter const &a, const AdapterSetting oldmask, const AdapterSetting newmask, + void adapterSettingsChanged(DBTAdapter &a, const AdapterSetting oldmask, const AdapterSetting newmask, const AdapterSetting changedmask, const uint64_t timestamp) override { fprintf(stderr, "****** Native Adapter SETTINGS_CHANGED: %s -> %s, changed %s\n", getAdapterSettingsString(oldmask).c_str(), @@ -62,7 +62,7 @@ class MyAdapterStatusListener : public AdapterStatusListener { (void)timestamp; } - void discoveringChanged(DBTAdapter const &a, const bool enabled, const bool keepAlive, const uint64_t timestamp) override { + void discoveringChanged(DBTAdapter &a, const bool enabled, const bool keepAlive, const uint64_t timestamp) override { fprintf(stderr, "****** DISCOVERING: enabled %d, keepAlive %d: %s\n", enabled, keepAlive, a.toString().c_str()); (void)timestamp; } @@ -204,7 +204,7 @@ int main(int argc, char *argv[]) const int64_t t0 = getCurrentMilliseconds(); while( ok && ( forever || !foundDevice ) ) { - ok = adapter.startDiscovery(true /* keepAlive */); + ok = HCIStatusCode::SUCCESS == adapter.startDiscovery(true /* keepAlive */); if( !ok) { perror("Adapter start discovery failed"); goto out; diff --git a/examples/direct_bt_scanner01/dbt_scanner01.cpp b/examples/direct_bt_scanner01/dbt_scanner01.cpp index db7fd78c..a63deb25 100644 --- a/examples/direct_bt_scanner01/dbt_scanner01.cpp +++ b/examples/direct_bt_scanner01/dbt_scanner01.cpp @@ -48,7 +48,7 @@ std::mutex mtxDeviceFound; std::condition_variable cvDeviceFound; class MyAdapterStatusListener : public AdapterStatusListener { - void adapterSettingsChanged(DBTAdapter const &a, const AdapterSetting oldmask, const AdapterSetting newmask, + void adapterSettingsChanged(DBTAdapter &a, const AdapterSetting oldmask, const AdapterSetting newmask, const AdapterSetting changedmask, const uint64_t timestamp) override { fprintf(stderr, "****** Native Adapter SETTINGS_CHANGED: %s -> %s, changed %s\n", getAdapterSettingsString(oldmask).c_str(), @@ -59,7 +59,7 @@ class MyAdapterStatusListener : public AdapterStatusListener { (void)timestamp; } - void discoveringChanged(DBTAdapter const &a, const bool enabled, const bool keepAlive, const uint64_t timestamp) override { + void discoveringChanged(DBTAdapter &a, const bool enabled, const bool keepAlive, const uint64_t timestamp) override { fprintf(stderr, "****** DISCOVERING: enabled %d, keepAlive %d: %s\n", enabled, keepAlive, a.toString().c_str()); (void)timestamp; } @@ -200,7 +200,7 @@ int main(int argc, char *argv[]) const int64_t t0 = getCurrentMilliseconds(); while( ok && ( forever || !foundDevice ) ) { - ok = adapter.startDiscovery(true /* keepAlive */); + ok = HCIStatusCode::SUCCESS == adapter.startDiscovery(true /* keepAlive */); if( !ok) { perror("Adapter start discovery failed"); goto out; diff --git a/examples/direct_bt_scanner10/dbt_scanner10.cpp b/examples/direct_bt_scanner10/dbt_scanner10.cpp index f8bbbc51..a7e0b505 100644 --- a/examples/direct_bt_scanner10/dbt_scanner10.cpp +++ b/examples/direct_bt_scanner10/dbt_scanner10.cpp @@ -77,7 +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); -static void resetAdapter(DBTAdapter *a); +static void resetAdapter(DBTAdapter *a, int mode); static std::vector<EUI48> devicesInProcessing; static std::recursive_mutex mtx_devicesProcessing; @@ -147,7 +147,7 @@ static size_t getDeviceProcessingCount() { class MyAdapterStatusListener : public AdapterStatusListener { - void adapterSettingsChanged(DBTAdapter const &a, const AdapterSetting oldmask, const AdapterSetting newmask, + void adapterSettingsChanged(DBTAdapter &a, const AdapterSetting oldmask, const AdapterSetting newmask, const AdapterSetting changedmask, const uint64_t timestamp) override { const bool initialSetting = AdapterSetting::NONE == oldmask; if( initialSetting ) { @@ -162,7 +162,7 @@ class MyAdapterStatusListener : public AdapterStatusListener { (void)timestamp; } - void discoveringChanged(DBTAdapter const &a, const bool enabled, const bool keepAlive, const uint64_t timestamp) override { + void discoveringChanged(DBTAdapter &a, const bool enabled, const bool keepAlive, const uint64_t timestamp) override { fprintf(stderr, "****** DISCOVERING: enabled %d, keepAlive %d: %s\n", enabled, keepAlive, a.toString().c_str()); (void)timestamp; } @@ -303,7 +303,8 @@ static void connectDiscoveredDevice(std::shared_ptr<DBTDevice> device) { } fprintf(stderr, "****** Connecting Device: End result %s of %s\n", getHCIStatusCodeString(res).c_str(), device->toString().c_str()); if( !USE_WHITELIST && 0 == getDeviceProcessingCount() && HCIStatusCode::SUCCESS != res ) { - device->getAdapter().startDiscovery( true ); + const HCIStatusCode r = device->getAdapter().startDiscovery( true ); + fprintf(stderr, "****** Connecting Device: startDiscovery result %s\n", getHCIStatusCodeString(r).c_str()); } } @@ -429,7 +430,8 @@ static void processConnectedDevice(std::shared_ptr<DBTDevice> device) { exit: if( !USE_WHITELIST && 0 == getDeviceProcessingCount() ) { - device->getAdapter().startDiscovery( true ); + const HCIStatusCode r = device->getAdapter().startDiscovery( true ); + fprintf(stderr, "****** Processing Device: startDiscovery.0 result %s\n", getHCIStatusCodeString(r).c_str()); } if( KEEP_CONNECTED && GATT_PING_ENABLED && success ) { @@ -457,7 +459,8 @@ exit: device->remove(); if( !USE_WHITELIST && 0 == getDeviceProcessingCount() ) { - device->getAdapter().startDiscovery( true ); + const HCIStatusCode r = device->getAdapter().startDiscovery( true ); + fprintf(stderr, "****** Processing Device: startDiscovery.1 result %s\n", getHCIStatusCodeString(r).c_str()); } } @@ -476,14 +479,15 @@ static void removeDevice(std::shared_ptr<DBTDevice> device) { device->remove(); if( !USE_WHITELIST && 0 == getDeviceProcessingCount() ) { - device->getAdapter().startDiscovery( true ); + const HCIStatusCode r = device->getAdapter().startDiscovery( true ); + fprintf(stderr, "****** Remove Device: startDiscovery result %s\n", getHCIStatusCodeString(r).c_str()); } } -static void resetAdapter(DBTAdapter *a) { - fprintf(stderr, "****** Reset Adapter: reset start: %s\n", a->toString().c_str()); +static void resetAdapter(DBTAdapter *a, int mode) { + fprintf(stderr, "****** Reset Adapter: reset[%d] start: %s\n", mode, 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()); + fprintf(stderr, "****** Reset Adapter: reset[%d] end: %s, %s\n", mode, getHCIStatusCodeString(res).c_str(), a->toString().c_str()); } void test(int dev_id) { @@ -516,8 +520,9 @@ void test(int dev_id) { fprintf(stderr, "Added to WHITELIST: res %d, address %s\n", res, it->toString().c_str()); } } else { - if( !adapter.startDiscovery( true ) ) { - perror("Adapter start discovery failed"); + HCIStatusCode status = adapter.startDiscovery( true ); + if( HCIStatusCode::SUCCESS != status ) { + fprintf(stderr, "Adapter: Start discovery failed: %s", getHCIStatusCodeString(status).c_str()); done = true; } } diff --git a/examples/java/DBTScanner10.java b/examples/java/DBTScanner10.java index a7027008..75a35f75 100644 --- a/examples/java/DBTScanner10.java +++ b/examples/java/DBTScanner10.java @@ -122,8 +122,9 @@ public class DBTScanner10 { newmask.isSet(AdapterSettings.SettingType.POWERED) ) { // powered on adapter .... - if( !adapter.startDiscovery( true ) ) { - println("Adapter (powered-on): Start discovery failed"); + final HCIStatusCode res = adapter.startDiscovery( true ); + if( res != HCIStatusCode.SUCCESS ) { + println("Adapter (powered-on): Start discovery failed: "+res); } } } @@ -186,7 +187,8 @@ public class DBTScanner10 { ) ) { - println("****** CONNECTED-0: Processing "+device.toString()); + connectionCount.incrementAndGet(); + println("****** CONNECTED-0: Processing["+connectionCount.get()+"] "+device.toString()); { final long td = BluetoothUtils.getCurrentMilliseconds() - timestamp_t0; // adapter-init -> now println("PERF: adapter-init -> CONNECTED-0 " + td + " ms"); @@ -221,11 +223,11 @@ public class DBTScanner10 { } else { devicesInProcessing.remove(device.getAddress()); } - if( 0 == connectionCount.addAndGet(1) % RESET_ADAPTER_EACH_CONN ) { + if( 0 < RESET_ADAPTER_EACH_CONN && 0 == connectionCount.get() % RESET_ADAPTER_EACH_CONN ) { final Thread adapterResetTask = new Thread( new Runnable() { @Override public void run() { - resetAdapter(device.getAdapter()); + resetAdapter(device.getAdapter(), 1); } }, "DBT-Reset-"+device.getAdapter().getAddress()); adapterResetTask.setDaemon(true); // detach thread @@ -237,7 +239,7 @@ public class DBTScanner10 { private void connectDiscoveredDevice(final BluetoothDevice device) { println("****** Connecting Device: Start " + device.toString()); { - final boolean r = device.getAdapter().stopDiscovery(); + final HCIStatusCode r = device.getAdapter().stopDiscovery(); println("****** Connecting Device: stopDiscovery result "+r); } HCIStatusCode res; @@ -248,7 +250,7 @@ public class DBTScanner10 { } println("****** Connecting Device Command, res "+res+": End result "+res+" of " + device.toString()); if( !USE_WHITELIST && 0 == devicesInProcessing.size() && HCIStatusCode.SUCCESS != res ) { - final boolean r = device.getAdapter().startDiscovery( true ); + final HCIStatusCode r = device.getAdapter().startDiscovery( true ); println("****** Connecting Device: startDiscovery result "+r); } } @@ -290,7 +292,7 @@ public class DBTScanner10 { println("****** Processing Device: Start " + device.toString()); { // make sure for pending connections on failed connect*(..) command - final boolean r = device.getAdapter().stopDiscovery(); + final HCIStatusCode r = device.getAdapter().stopDiscovery(); println("****** Processing Device: stopDiscovery result "+r); } @@ -454,8 +456,8 @@ public class DBTScanner10 { } if( !USE_WHITELIST && 0 == devicesInProcessing.size() ) { - final boolean r = device.getAdapter().startDiscovery( true ); - println("****** Processing Device: startDiscovery result "+r); + final HCIStatusCode r = device.getAdapter().startDiscovery( true ); + println("****** Processing Device: startDiscovery.0 result "+r); } if( KEEP_CONNECTED && GATT_PING_ENABLED && success ) { @@ -483,11 +485,15 @@ public class DBTScanner10 { device.remove(); if( !USE_WHITELIST && 0 == devicesInProcessing.size() ) { - final boolean r = device.getAdapter().startDiscovery( true ); - println("****** Processing Device: startDiscovery result "+r); + final HCIStatusCode r = device.getAdapter().startDiscovery( true ); + println("****** Processing Device: startDiscovery.1 result "+r); } } + if( 0 < RESET_ADAPTER_EACH_CONN && 0 == connectionCount.get() % RESET_ADAPTER_EACH_CONN ) { + resetAdapter(device.getAdapter(), 2); + } + if( 0 < MULTI_MEASUREMENTS.get() ) { MULTI_MEASUREMENTS.decrementAndGet(); println("****** Processing Device: MULTI_MEASUREMENTS left "+MULTI_MEASUREMENTS.get()+": "+device.getAddress()); @@ -503,15 +509,15 @@ public class DBTScanner10 { device.remove(); if( !USE_WHITELIST && 0 == devicesInProcessing.size() ) { - final boolean r = device.getAdapter().startDiscovery( true ); + final HCIStatusCode r = device.getAdapter().startDiscovery( true ); println("****** Remove Device: startDiscovery result "+r); } } - private void resetAdapter(final BluetoothAdapter adapter) { - println("****** Reset Adapter: reset start: "+adapter.toString()); + private void resetAdapter(final BluetoothAdapter adapter, final int mode) { + println("****** Reset Adapter: reset["+mode+"] start: "+adapter.toString()); final HCIStatusCode res = adapter.reset(); - println("****** Reset Adapter: reset end: "+res+", "+adapter.toString()); + println("****** Reset Adapter: reset["+mode+"] end: "+res+", "+adapter.toString()); } public void runTest(final BluetoothManager manager) { @@ -556,8 +562,9 @@ public class DBTScanner10 { println("Added to whitelist: res "+res+", address "+addr); } } else { - if( !adapter.startDiscovery( true ) ) { - println("Adapter start discovery failed"); + final HCIStatusCode status = adapter.startDiscovery( true ); + if( HCIStatusCode.SUCCESS != status ) { + println("Adapter start discovery failed: "+status); done = true; } } diff --git a/examples/java/ScannerTinyB00.java b/examples/java/ScannerTinyB00.java index 9b49fda5..fe7f2475 100644 --- a/examples/java/ScannerTinyB00.java +++ b/examples/java/ScannerTinyB00.java @@ -137,9 +137,9 @@ public class ScannerTinyB00 { if( useAdapter ) { adapter.removeDevices(); } - final boolean discoveryStarted = useAdapter ? adapter.startDiscovery(true) : manager.startDiscovery(); + final HCIStatusCode discoveryStatus = useAdapter ? adapter.startDiscovery(true) : manager.startDiscovery(true); - System.err.println("The discovery started: " + (discoveryStarted ? "true" : "false") + " for mac "+mac+", mode "+mode+", useAdapter "+useAdapter); + System.err.println("The discovery started: " + discoveryStatus + " for mac "+mac+", mode "+mode+", useAdapter "+useAdapter); BluetoothDevice sensor = null; if( 0 == mode ) { diff --git a/examples/java/ScannerTinyB01.java b/examples/java/ScannerTinyB01.java index d62aee88..d2a03ffb 100644 --- a/examples/java/ScannerTinyB01.java +++ b/examples/java/ScannerTinyB01.java @@ -225,10 +225,10 @@ public class ScannerTinyB01 { final long t0 = BluetoothUtils.getCurrentMilliseconds(); - final boolean discoveryStarted = adapter.startDiscovery(true); + final HCIStatusCode discoveryStatus = adapter.startDiscovery(true); - System.err.println("The discovery started: " + (discoveryStarted ? "true" : "false") + " for mac "+waitForDevice+", mode "+mode); - if( !discoveryStarted ) { + System.err.println("The discovery started: " + discoveryStatus + " for mac "+waitForDevice+", mode "+mode); + if( HCIStatusCode.SUCCESS != discoveryStatus ) { break; } BluetoothDevice sensor = null; diff --git a/java/direct_bt/tinyb/DBTAdapter.java b/java/direct_bt/tinyb/DBTAdapter.java index c53b712b..3e3e3973 100644 --- a/java/direct_bt/tinyb/DBTAdapter.java +++ b/java/direct_bt/tinyb/DBTAdapter.java @@ -199,13 +199,13 @@ public class DBTAdapter extends DBTObject implements BluetoothAdapter public long getDiscoverableTimeout() { throw new UnsupportedOperationException(); } // FIXME @Override - public void setDiscoverableTimout(final long value) { throw new UnsupportedOperationException(); } // FIXME + public boolean setDiscoverableTimout(final long value) { return false; } // FIXME @Override public long getPairableTimeout() { throw new UnsupportedOperationException(); } // FIXME @Override - public void setPairableTimeout(final long value) { throw new UnsupportedOperationException(); } // FIXME + public boolean setPairableTimeout(final long value) { return false; } // FIXME @Override public String getModalias() { throw new UnsupportedOperationException(); } // FIXME @@ -301,7 +301,7 @@ public class DBTAdapter extends DBTObject implements BluetoothAdapter private native String toStringImpl(); @Override - public native void setPowered(boolean value); + public native boolean setPowered(boolean value); @Override public final HCIStatusCode reset() { @@ -316,13 +316,13 @@ public class DBTAdapter extends DBTObject implements BluetoothAdapter public native void setAlias(final String value); @Override - public native void setDiscoverable(boolean value); + public native boolean setDiscoverable(boolean value); @Override public native BluetoothDevice connectDevice(String address, String addressType); @Override - public native void setPairable(boolean value); + public native boolean setPairable(boolean value); @Override public native boolean isEnabled(); @@ -336,28 +336,28 @@ public class DBTAdapter extends DBTObject implements BluetoothAdapter @Override public boolean startDiscovery() throws BluetoothException { - return startDiscovery(true); + return HCIStatusCode.SUCCESS == startDiscovery(true); } @Override - public boolean startDiscovery(final boolean keepAlive) throws BluetoothException { + public HCIStatusCode startDiscovery(final boolean keepAlive) throws BluetoothException { synchronized( discoveryLock ) { // Ignoring 'isDiscovering', as native implementation also handles change of 'keepAlive'. // The discoveredDevices shall always get cleared. removeDevices(); - return startDiscoveryImpl(keepAlive); // event callbacks will be generated by implementation + return HCIStatusCode.get( startDiscoveryImpl(keepAlive) ); // event callbacks will be generated by implementation } } - private native boolean startDiscoveryImpl(boolean keepAlive) throws BluetoothException; + private native byte startDiscoveryImpl(boolean keepAlive) throws BluetoothException; @Override - public boolean stopDiscovery() throws BluetoothException { + public HCIStatusCode stopDiscovery() throws BluetoothException { synchronized( discoveryLock ) { // Ignoring 'isDiscovering', be consistent with startDiscovery - return stopDiscoveryImpl(); // event callbacks will be generated by implementation + return HCIStatusCode.get( stopDiscoveryImpl() ); // event callbacks will be generated by implementation } } - private native boolean stopDiscoveryImpl() throws BluetoothException; + private native byte stopDiscoveryImpl() throws BluetoothException; @Override public List<BluetoothDevice> getDevices() { diff --git a/java/direct_bt/tinyb/DBTManager.java b/java/direct_bt/tinyb/DBTManager.java index c903b7ba..82b25166 100644 --- a/java/direct_bt/tinyb/DBTManager.java +++ b/java/direct_bt/tinyb/DBTManager.java @@ -41,6 +41,7 @@ import org.tinyb.BluetoothGattService; import org.tinyb.BluetoothObject; import org.tinyb.BluetoothManager; import org.tinyb.BluetoothType; +import org.tinyb.HCIStatusCode; public class DBTManager implements BluetoothManager { @@ -259,13 +260,13 @@ public class DBTManager implements BluetoothManager public BluetoothAdapter getDefaultAdapter() { return adapters.get(defaultAdapterIndex); } @Override - public boolean startDiscovery() throws BluetoothException { return startDiscovery(true); } + public boolean startDiscovery() throws BluetoothException { return HCIStatusCode.SUCCESS == startDiscovery(true); } @Override - public boolean startDiscovery(final boolean keepAlive) throws BluetoothException { return getDefaultAdapter().startDiscovery(keepAlive); } + public HCIStatusCode startDiscovery(final boolean keepAlive) throws BluetoothException { return getDefaultAdapter().startDiscovery(keepAlive); } @Override - public boolean stopDiscovery() throws BluetoothException { return getDefaultAdapter().stopDiscovery(); } + public HCIStatusCode stopDiscovery() throws BluetoothException { return getDefaultAdapter().stopDiscovery(); } @Override public boolean getDiscovering() throws BluetoothException { return getDefaultAdapter().getDiscovering(); } diff --git a/java/jni/direct_bt/DBTAdapter.cxx b/java/jni/direct_bt/DBTAdapter.cxx index 80ca0ccb..6bdf58fa 100644 --- a/java/jni/direct_bt/DBTAdapter.cxx +++ b/java/jni/direct_bt/DBTAdapter.cxx @@ -230,7 +230,7 @@ class JNIAdapterStatusListener : public AdapterStatusListener { return device == *deviceMatchRef; } - void adapterSettingsChanged(DBTAdapter const &a, const AdapterSetting oldmask, const AdapterSetting newmask, + void adapterSettingsChanged(DBTAdapter &a, const AdapterSetting oldmask, const AdapterSetting newmask, const AdapterSetting changedmask, const uint64_t timestamp) override { JNIEnv *env = *jni_env; (void)a; @@ -254,7 +254,7 @@ class JNIAdapterStatusListener : public AdapterStatusListener { env->DeleteLocalRef(adapterSettingChanged); } - void discoveringChanged(DBTAdapter const &a, const bool enabled, const bool keepAlive, const uint64_t timestamp) override { + void discoveringChanged(DBTAdapter &a, const bool enabled, const bool keepAlive, const uint64_t timestamp) override { JNIEnv *env = *jni_env; (void)a; env->CallVoidMethod(listenerObjRef.getObject(), mDiscoveringChanged, JavaGlobalObj::GetObject(adapterObjRef), @@ -545,26 +545,26 @@ jboolean Java_direct_1bt_tinyb_DBTAdapter_isEnabled(JNIEnv *env, jobject obj) return JNI_FALSE; } -jboolean Java_direct_1bt_tinyb_DBTAdapter_startDiscoveryImpl(JNIEnv *env, jobject obj, jboolean keepAlive) +jbyte Java_direct_1bt_tinyb_DBTAdapter_startDiscoveryImpl(JNIEnv *env, jobject obj, jboolean keepAlive) { try { DBTAdapter *adapter = getDBTObject<DBTAdapter>(env, obj); - return adapter->startDiscovery(keepAlive); + return (jbyte) number( adapter->startDiscovery(keepAlive) ); } catch(...) { rethrow_and_raise_java_exception(env); } - return JNI_FALSE; + return (jbyte) number(HCIStatusCode::INTERNAL_FAILURE); } -jboolean Java_direct_1bt_tinyb_DBTAdapter_stopDiscoveryImpl(JNIEnv *env, jobject obj) +jbyte Java_direct_1bt_tinyb_DBTAdapter_stopDiscoveryImpl(JNIEnv *env, jobject obj) { try { DBTAdapter *adapter = getDBTObject<DBTAdapter>(env, obj); - return adapter->stopDiscovery(); + return (jbyte) number( adapter->stopDiscovery() ); } catch(...) { rethrow_and_raise_java_exception(env); } - return JNI_FALSE; + return (jbyte) number(HCIStatusCode::INTERNAL_FAILURE); } jobject Java_direct_1bt_tinyb_DBTAdapter_getDiscoveredDevicesImpl(JNIEnv *env, jobject obj) @@ -594,14 +594,15 @@ jint Java_direct_1bt_tinyb_DBTAdapter_removeDevicesImpl(JNIEnv *env, jobject obj // misc // -void Java_direct_1bt_tinyb_DBTAdapter_setPowered(JNIEnv *env, jobject obj, jboolean value) { +jboolean Java_direct_1bt_tinyb_DBTAdapter_setPowered(JNIEnv *env, jobject obj, jboolean value) { try { DBTAdapter *adapter = getDBTObject<DBTAdapter>(env, obj); JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); - adapter->setPowered(JNI_TRUE == value ? true : false); + return adapter->setPowered(JNI_TRUE == value ? true : false) ? JNI_TRUE : JNI_FALSE; } catch(...) { rethrow_and_raise_java_exception(env); } + return JNI_FALSE; } jbyte Java_direct_1bt_tinyb_DBTAdapter_resetImpl(JNIEnv *env, jobject obj) { @@ -638,14 +639,15 @@ void Java_direct_1bt_tinyb_DBTAdapter_setAlias(JNIEnv *env, jobject obj, jstring } } -void Java_direct_1bt_tinyb_DBTAdapter_setDiscoverable(JNIEnv *env, jobject obj, jboolean value) { +jboolean Java_direct_1bt_tinyb_DBTAdapter_setDiscoverable(JNIEnv *env, jobject obj, jboolean value) { try { DBTAdapter *adapter = getDBTObject<DBTAdapter>(env, obj); JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); - adapter->setDiscoverable(JNI_TRUE == value ? true : false); + return adapter->setDiscoverable(JNI_TRUE == value ? true : false) ? JNI_TRUE : JNI_FALSE; } catch(...) { rethrow_and_raise_java_exception(env); } + return JNI_FALSE; } jobject Java_direct_1bt_tinyb_DBTAdapter_connectDevice(JNIEnv *env, jobject obj, jstring jaddress, jstring jaddressType) { @@ -673,14 +675,15 @@ jobject Java_direct_1bt_tinyb_DBTAdapter_connectDevice(JNIEnv *env, jobject obj, return nullptr; } -void Java_direct_1bt_tinyb_DBTAdapter_setPairable(JNIEnv *env, jobject obj, jboolean value) { +jboolean Java_direct_1bt_tinyb_DBTAdapter_setPairable(JNIEnv *env, jobject obj, jboolean value) { try { DBTAdapter *adapter = getDBTObject<DBTAdapter>(env, obj); JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); - adapter->setBondable(JNI_TRUE == value ? true : false); + return adapter->setBondable(JNI_TRUE == value ? true : false) ? JNI_TRUE : JNI_FALSE; } catch(...) { rethrow_and_raise_java_exception(env); } + return JNI_FALSE; } void Java_direct_1bt_tinyb_DBTAdapter_setDiscoveryFilter(JNIEnv *env, jobject obj, jobject juuids, jint rssi, jint pathloss, jint transportType) { diff --git a/java/jni/tinyb/DBusAdapter.cxx b/java/jni/tinyb/DBusAdapter.cxx index 6e0eda56..fdc40101 100644 --- a/java/jni/tinyb/DBusAdapter.cxx +++ b/java/jni/tinyb/DBusAdapter.cxx @@ -67,7 +67,7 @@ jboolean Java_tinyb_dbus_DBusAdapter_startDiscovery(JNIEnv *env, jobject obj) return JNI_FALSE; } -jboolean Java_tinyb_dbus_DBusAdapter_stopDiscovery(JNIEnv *env, jobject obj) +jboolean Java_tinyb_dbus_DBusAdapter_stopDiscoveryImpl(JNIEnv *env, jobject obj) { try { BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); @@ -192,16 +192,18 @@ jboolean Java_tinyb_dbus_DBusAdapter_getPowered(JNIEnv *env, jobject obj) return JNI_FALSE; } -void Java_tinyb_dbus_DBusAdapter_setPowered(JNIEnv *env, jobject obj, jboolean val) +jboolean Java_tinyb_dbus_DBusAdapter_setPowered(JNIEnv *env, jobject obj, jboolean val) { try { BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); bool val_to_write = from_jboolean_to_bool(val); obj_adapter->set_powered(val_to_write); + return JNI_TRUE; } catch(...) { rethrow_and_raise_java_exception(env); } + return JNI_FALSE; } void Java_tinyb_dbus_DBusAdapter_enablePoweredNotifications(JNIEnv *env, jobject obj, jobject callback) @@ -254,16 +256,18 @@ jboolean Java_tinyb_dbus_DBusAdapter_getDiscoverable(JNIEnv *env, jobject obj) return JNI_FALSE; } -void Java_tinyb_dbus_DBusAdapter_setDiscoverable(JNIEnv *env, jobject obj, jboolean val) +jboolean Java_tinyb_dbus_DBusAdapter_setDiscoverable(JNIEnv *env, jobject obj, jboolean val) { try { BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); bool val_to_write = from_jboolean_to_bool(val); obj_adapter->set_discoverable(val_to_write); + return JNI_TRUE; } catch(...) { rethrow_and_raise_java_exception(env); } + return JNI_FALSE; } void Java_tinyb_dbus_DBusAdapter_enableDiscoverableNotifications(JNIEnv *env, jobject obj, jobject callback) @@ -316,7 +320,7 @@ jlong Java_tinyb_dbus_DBusAdapter_getDiscoverableTimeout(JNIEnv *env, jobject ob return 0; } -void Java_tinyb_dbus_DBusAdapter_setDiscoverableTimout(JNIEnv *env, jobject obj, jlong timeout) +jboolean Java_tinyb_dbus_DBusAdapter_setDiscoverableTimout(JNIEnv *env, jobject obj, jlong timeout) { try { BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); @@ -326,9 +330,11 @@ void Java_tinyb_dbus_DBusAdapter_setDiscoverableTimout(JNIEnv *env, jobject obj, throw std::invalid_argument("timeout argument is negative\n"); } obj_adapter->set_discoverable_timeout((unsigned int)timeout); + return JNI_TRUE; } catch(...) { rethrow_and_raise_java_exception(env); } + return JNI_FALSE; } jboolean Java_tinyb_dbus_DBusAdapter_getPairable(JNIEnv *env, jobject obj) @@ -381,16 +387,18 @@ void Java_tinyb_dbus_DBusAdapter_disablePairableNotifications(JNIEnv *env, jobje } } -void Java_tinyb_dbus_DBusAdapter_setPairable(JNIEnv *env, jobject obj, jboolean val) +jboolean Java_tinyb_dbus_DBusAdapter_setPairable(JNIEnv *env, jobject obj, jboolean val) { try { BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); bool val_to_write = from_jboolean_to_bool(val); obj_adapter->set_pairable(val_to_write); + return JNI_TRUE; } catch(...) { rethrow_and_raise_java_exception(env); } + return JNI_FALSE; } jlong Java_tinyb_dbus_DBusAdapter_getPairableTimeout(JNIEnv *env, jobject obj) @@ -405,7 +413,7 @@ jlong Java_tinyb_dbus_DBusAdapter_getPairableTimeout(JNIEnv *env, jobject obj) return 0; } -void Java_tinyb_dbus_DBusAdapter_setPairableTimeout(JNIEnv *env, jobject obj, jlong timeout) +jboolean Java_tinyb_dbus_DBusAdapter_setPairableTimeout(JNIEnv *env, jobject obj, jlong timeout) { try { BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); @@ -415,9 +423,11 @@ void Java_tinyb_dbus_DBusAdapter_setPairableTimeout(JNIEnv *env, jobject obj, jl throw std::invalid_argument("timeout argument is negative\n"); } obj_adapter->set_pairable_timeout((unsigned int)timeout); + return JNI_TRUE; } catch(...) { rethrow_and_raise_java_exception(env); } + return JNI_FALSE; } jboolean Java_tinyb_dbus_DBusAdapter_getDiscovering(JNIEnv *env, jobject obj) diff --git a/java/jni/tinyb/DBusManager.cxx b/java/jni/tinyb/DBusManager.cxx index 28150a13..d8ee789e 100644 --- a/java/jni/tinyb/DBusManager.cxx +++ b/java/jni/tinyb/DBusManager.cxx @@ -293,7 +293,7 @@ jboolean Java_tinyb_dbus_DBusManager_startDiscovery(JNIEnv *env, jobject obj) return JNI_FALSE; } -jboolean Java_tinyb_dbus_DBusManager_stopDiscovery(JNIEnv *env, jobject obj) +jboolean Java_tinyb_dbus_DBusManager_stopDiscoveryImpl(JNIEnv *env, jobject obj) { try { BluetoothManager *manager = getInstance<BluetoothManager>(env, obj); diff --git a/java/org/tinyb/BluetoothAdapter.java b/java/org/tinyb/BluetoothAdapter.java index 5d9637a9..20915083 100644 --- a/java/org/tinyb/BluetoothAdapter.java +++ b/java/org/tinyb/BluetoothAdapter.java @@ -167,18 +167,21 @@ public interface BluetoothAdapter extends BluetoothObject * Using {@link #startDiscovery(boolean) startDiscovery}({@code keepAlive=true}) * and {@link #stopDiscovery()} is the recommended workflow * for a reliable discovery process. - * @return TRUE if discovery was successfully enabled + * @return {@link HCIStatusCode#SUCCESS} if successful, otherwise the {@link HCIStatusCode} error state * @throws BluetoothException * @since 2.0.0 * @implNote {@code keepAlive} not implemented in tinyb.dbus * @see #getDiscovering() */ - public boolean startDiscovery(final boolean keepAlive) throws BluetoothException; + public HCIStatusCode startDiscovery(final boolean keepAlive) throws BluetoothException; - /** Turns off device discovery if it is enabled. - * @return TRUE if discovery was successfully disabled - */ - public boolean stopDiscovery() throws BluetoothException; + /** + * Turns off device discovery if it is enabled. + * @return {@link HCIStatusCode#SUCCESS} if successful, otherwise the {@link HCIStatusCode} error state + * @apiNote return {@link HCIStatusCode} since 2.0.0 + * @since 2.0.0 + */ + public HCIStatusCode stopDiscovery() throws BluetoothException; /** Returns a list of BluetoothDevices visible from this adapter. * @return A list of BluetoothDevices visible on this adapter, @@ -249,9 +252,12 @@ public interface BluetoothAdapter extends BluetoothObject */ public void disablePoweredNotifications(); - /** Sets the power state the adapter. - */ - public void setPowered(boolean value); + /** + * Sets the power state the adapter. + * @apiNote return value boolean since 2.0.0 + * @since 2.0.0 + */ + public boolean setPowered(boolean value); /** * Reset the adapter. @@ -282,19 +288,25 @@ public interface BluetoothAdapter extends BluetoothObject */ public void disableDiscoverableNotifications(); - /** Sets the discoverable state the adapter. - */ - public void setDiscoverable(boolean value); + /** + * Sets the discoverable state the adapter. + * @apiNote return value boolean since 2.0.0 + * @since 2.0.0 + */ + public boolean setDiscoverable(boolean value); /** Returns the discoverable timeout the adapter. * @return The discoverable timeout of the adapter. */ public long getDiscoverableTimeout(); - /** Sets the discoverable timeout the adapter. A value of 0 disables - * the timeout. - */ - public void setDiscoverableTimout(long value); + /** + * Sets the discoverable timeout the adapter. A value of 0 disables + * the timeout. + * @apiNote return value boolean since 2.0.0 + * @since 2.0.0 + */ + public boolean setDiscoverableTimout(long value); /** * This method connects to device without need of @@ -342,9 +354,12 @@ public interface BluetoothAdapter extends BluetoothObject */ public void disablePairableNotifications(); - /** Sets the discoverable state the adapter. - */ - public void setPairable(boolean value); + /** + * Sets the discoverable state the adapter. + * @apiNote return value boolean since 2.0.0 + * @since 2.0.0 + */ + public boolean setPairable(boolean value); /** Returns the timeout in seconds after which pairable state turns off * automatically, 0 means never. @@ -352,9 +367,12 @@ public interface BluetoothAdapter extends BluetoothObject */ public long getPairableTimeout(); - /** Sets the timeout after which pairable state turns off automatically, 0 means never. - */ - public void setPairableTimeout(long value); + /** + * Sets the timeout after which pairable state turns off automatically, 0 means never. + * @apiNote return value boolean since 2.0.0 + * @since 2.0.0 + */ + public boolean setPairableTimeout(long value); /** * Returns the meta discovering state (of the adapter). diff --git a/java/org/tinyb/BluetoothManager.java b/java/org/tinyb/BluetoothManager.java index 2ddad81e..5c7eba23 100644 --- a/java/org/tinyb/BluetoothManager.java +++ b/java/org/tinyb/BluetoothManager.java @@ -229,18 +229,20 @@ public interface BluetoothManager * Using {@link #startDiscovery(boolean) startDiscovery}({@code keepAlive=true}) * and {@link #stopDiscovery()} is the recommended workflow * for a reliable discovery process. - * @return TRUE if discovery was successfully enabled + * @return {@link HCIStatusCode#SUCCESS} if successful, otherwise the {@link HCIStatusCode} error state * @throws BluetoothException * @since 2.0.0 * @implNote {@code keepAlive} not implemented in tinyb.dbus */ - public boolean startDiscovery(final boolean keepAlive) throws BluetoothException; + public HCIStatusCode startDiscovery(final boolean keepAlive) throws BluetoothException; - - /** Turns off device discovery on the default adapter if it is enabled. - * @return TRUE if discovery was successfully disabled - */ - public boolean stopDiscovery() throws BluetoothException; + /** + * Turns off device discovery on the default adapter if it is enabled. + * @return {@link HCIStatusCode#SUCCESS} if successful, otherwise the {@link HCIStatusCode} error state + * @apiNote return {@link HCIStatusCode} since 2.0.0 + * @since 2.0.0 + */ + public HCIStatusCode stopDiscovery() throws BluetoothException; /** Returns if the discovers is running or not. * @return TRUE if discovery is running diff --git a/java/tinyb/dbus/DBusAdapter.java b/java/tinyb/dbus/DBusAdapter.java index 964ee67a..a92beafa 100644 --- a/java/tinyb/dbus/DBusAdapter.java +++ b/java/tinyb/dbus/DBusAdapter.java @@ -105,12 +105,15 @@ public class DBusAdapter extends DBusObject implements BluetoothAdapter public native boolean startDiscovery() throws BluetoothException; @Override - public synchronized boolean startDiscovery(final boolean keepAlive) throws BluetoothException { - return startDiscovery(); // FIXME keepAlive + public synchronized HCIStatusCode startDiscovery(final boolean keepAlive) throws BluetoothException { + return startDiscovery() ? HCIStatusCode.SUCCESS : HCIStatusCode.INTERNAL_FAILURE; // FIXME keepAlive } @Override - public native boolean stopDiscovery() throws BluetoothException; + public HCIStatusCode stopDiscovery() throws BluetoothException { + return stopDiscoveryImpl() ? HCIStatusCode.SUCCESS : HCIStatusCode.INTERNAL_FAILURE; + } + private native boolean stopDiscoveryImpl() throws BluetoothException; @Override public native List<BluetoothDevice> getDevices(); @@ -145,7 +148,7 @@ public class DBusAdapter extends DBusObject implements BluetoothAdapter public native void disablePoweredNotifications(); @Override - public native void setPowered(boolean value); + public native boolean setPowered(boolean value); @Override public final HCIStatusCode reset() { return HCIStatusCode.INTERNAL_FAILURE; } @@ -160,13 +163,13 @@ public class DBusAdapter extends DBusObject implements BluetoothAdapter public native void disableDiscoverableNotifications(); @Override - public native void setDiscoverable(boolean value); + public native boolean setDiscoverable(boolean value); @Override public native long getDiscoverableTimeout(); @Override - public native void setDiscoverableTimout(long value); + public native boolean setDiscoverableTimout(long value); @Override public native BluetoothDevice connectDevice(String address, String addressType); @@ -181,13 +184,13 @@ public class DBusAdapter extends DBusObject implements BluetoothAdapter public native void disablePairableNotifications(); @Override - public native void setPairable(boolean value); + public native boolean setPairable(boolean value); @Override public native long getPairableTimeout(); @Override - public native void setPairableTimeout(long value); + public native boolean setPairableTimeout(long value); @Override public native boolean getDiscovering(); diff --git a/java/tinyb/dbus/DBusManager.java b/java/tinyb/dbus/DBusManager.java index 61d0935d..e815aeec 100644 --- a/java/tinyb/dbus/DBusManager.java +++ b/java/tinyb/dbus/DBusManager.java @@ -37,6 +37,7 @@ import org.tinyb.BluetoothGattService; import org.tinyb.BluetoothObject; import org.tinyb.BluetoothManager; import org.tinyb.BluetoothType; +import org.tinyb.HCIStatusCode; public class DBusManager implements BluetoothManager { @@ -109,12 +110,15 @@ public class DBusManager implements BluetoothManager public native boolean startDiscovery() throws BluetoothException; @Override - public boolean startDiscovery(final boolean keepAlive) throws BluetoothException { - return startDiscovery(); // FIXME keepAlive + public HCIStatusCode startDiscovery(final boolean keepAlive) throws BluetoothException { + return startDiscovery() ? HCIStatusCode.SUCCESS : HCIStatusCode.INTERNAL_FAILURE; // FIXME keepAlive } @Override - public native boolean stopDiscovery() throws BluetoothException; + public HCIStatusCode stopDiscovery() throws BluetoothException { + return stopDiscoveryImpl() ? HCIStatusCode.SUCCESS : HCIStatusCode.INTERNAL_FAILURE; + } + private native boolean stopDiscoveryImpl() throws BluetoothException; @Override public native boolean getDiscovering() throws BluetoothException; diff --git a/src/direct_bt/DBTAdapter.cpp b/src/direct_bt/DBTAdapter.cpp index 204b5560..8f3b0179 100644 --- a/src/direct_bt/DBTAdapter.cpp +++ b/src/direct_bt/DBTAdapter.cpp @@ -312,16 +312,16 @@ std::shared_ptr<NameAndShortName> DBTAdapter::setLocalName(const std::string &na return mgmt.setLocalName(dev_id, name, short_name); } -void DBTAdapter::setDiscoverable(bool value) noexcept { - mgmt.setMode(dev_id, MgmtOpcode::SET_DISCOVERABLE, value ? 1 : 0); +bool DBTAdapter::setDiscoverable(bool value) noexcept { + return mgmt.setMode(dev_id, MgmtOpcode::SET_DISCOVERABLE, value ? 1 : 0); } -void DBTAdapter::setBondable(bool value) noexcept { - mgmt.setMode(dev_id, MgmtOpcode::SET_BONDABLE, value ? 1 : 0); +bool DBTAdapter::setBondable(bool value) noexcept { + return mgmt.setMode(dev_id, MgmtOpcode::SET_BONDABLE, value ? 1 : 0); } -void DBTAdapter::setPowered(bool value) noexcept { - mgmt.setMode(dev_id, MgmtOpcode::SET_POWERED, value ? 1 : 0); +bool DBTAdapter::setPowered(bool value) noexcept { + return mgmt.setMode(dev_id, MgmtOpcode::SET_POWERED, value ? 1 : 0); } HCIStatusCode DBTAdapter::reset() noexcept { @@ -456,13 +456,13 @@ void DBTAdapter::checkDiscoveryState() noexcept { #define USE_HCI_DISCOVERY 1 -bool DBTAdapter::startDiscovery(const bool keepAlive, const HCILEOwnAddressType own_mac_type, - const uint16_t le_scan_interval, const uint16_t le_scan_window) +HCIStatusCode DBTAdapter::startDiscovery(const bool keepAlive, const HCILEOwnAddressType own_mac_type, + const uint16_t le_scan_interval, const uint16_t le_scan_window) { // FIXME: Respect DBTAdapter::btMode, i.e. BTMode::BREDR, BTMode::LE or BTMode::DUAL to setup BREDR, LE or DUAL scanning! if( !isEnabled() ) { ERR_PRINT("DBTAdapter::startDiscovery: Adapter not enabled/powered: %s", toString().c_str()); - return false; + return HCIStatusCode::INTERNAL_FAILURE; } const std::lock_guard<std::recursive_mutex> lock(mtx_discovery); // RAII-style acquire and relinquish via destructor if( ScanType::NONE != currentMetaScanType ) { @@ -478,7 +478,7 @@ bool DBTAdapter::startDiscovery(const bool keepAlive, const HCILEOwnAddressType keepDiscoveringAlive = keepAlive; } checkDiscoveryState(); - return true; + return HCIStatusCode::SUCCESS; } (void)own_mac_type; (void)le_scan_interval; @@ -494,30 +494,25 @@ bool DBTAdapter::startDiscovery(const bool keepAlive, const HCILEOwnAddressType std::shared_ptr<HCIHandler> hci = getHCI(); if( nullptr == hci ) { ERR_PRINT("DBTAdapter::startDiscovery: HCI not available: %s", toString().c_str()); - return false; + return HCIStatusCode::INTERNAL_FAILURE; } HCIStatusCode status = hci->le_set_scan_param(); if( HCIStatusCode::SUCCESS != status ) { ERR_PRINT("DBTAdapter::startDiscovery: le_set_scan_param failed: %s", getHCIStatusCodeString(status).c_str()); - } - - bool res; - // Will issue 'mgmtEvDeviceDiscoveringHCI(..)' immediately, don't change current scan-type state here - status = hci->le_enable_scan(true /* enable */); - if( HCIStatusCode::SUCCESS != status ) { - ERR_PRINT("DBTAdapter::startDiscovery: le_enable_scan failed: %s", getHCIStatusCodeString(status).c_str()); - res = false; } else { - res = true; + // Will issue 'mgmtEvDeviceDiscoveringHCI(..)' immediately, don't change current scan-type state here + status = hci->le_enable_scan(true /* enable */); + if( HCIStatusCode::SUCCESS != status ) { + ERR_PRINT("DBTAdapter::startDiscovery: le_enable_scan failed: %s", getHCIStatusCodeString(status).c_str()); + } } - - DBG_PRINT("DBTAdapter::startDiscovery: End: Result %d, keepAlive %d -> %d, currentScanType[native %s, meta %s] ...", - res, keepDiscoveringAlive.load(), keepAlive, + DBG_PRINT("DBTAdapter::startDiscovery: End: Result %s, keepAlive %d -> %d, currentScanType[native %s, meta %s] ...", + getHCIStatusCodeString(status).c_str(), keepDiscoveringAlive.load(), keepAlive, getScanTypeString(currentNativeScanType).c_str(), getScanTypeString(currentMetaScanType).c_str()); checkDiscoveryState(); - return res; + return status; } void DBTAdapter::startDiscoveryBackground() noexcept { @@ -542,7 +537,7 @@ void DBTAdapter::startDiscoveryBackground() noexcept { } } -bool DBTAdapter::stopDiscovery() noexcept { +HCIStatusCode DBTAdapter::stopDiscovery() noexcept { // We allow !isEnabled, to utilize method for adjusting discovery state and notifying listeners // FIXME: Respect DBTAdapter::btMode, i.e. BTMode::BREDR, BTMode::LE or BTMode::DUAL to stop BREDR, LE or DUAL scanning! const std::lock_guard<std::recursive_mutex> lock(mtx_discovery); // RAII-style acquire and relinquish via destructor @@ -575,47 +570,44 @@ bool DBTAdapter::stopDiscovery() noexcept { keepDiscoveringAlive.load(), getScanTypeString(currentNativeScanType).c_str(), getScanTypeString(currentMetaScanType).c_str()); checkDiscoveryState(); - return true; + return HCIStatusCode::SUCCESS; } - bool res; + HCIStatusCode status; std::shared_ptr<HCIHandler> hci = getHCI(); if( nullptr == hci ) { ERR_PRINT("DBTAdapter::stopDiscovery: HCI not available: %s", toString().c_str()); - res = false; // send event + status = HCIStatusCode::INTERNAL_FAILURE; goto exit; } if( discoveryTempDisabled ) { // meta state transition [4] -> [5], w/o native disabling currentMetaScanType = currentNativeScanType.load(); - res = true; // send event: discoveryTempDisabled + status = HCIStatusCode::SUCCESS; // send event: discoveryTempDisabled } else { // Actual disabling discovery HCIStatusCode status = hci->le_enable_scan(false /* enable */); if( HCIStatusCode::SUCCESS != status ) { - res = false; // send event ERR_PRINT("DBTAdapter::stopDiscovery: le_enable_scan failed: %s", getHCIStatusCodeString(status).c_str()); - } else { - res = true; } } exit: - if( discoveryTempDisabled || !res ) { + if( discoveryTempDisabled || HCIStatusCode::SUCCESS != status ) { // In case of discoveryTempDisabled, power-off, le_enable_scane failure // or already pulled HCIHandler, send the event directly. // SEND_EVENT: Perform off-thread to avoid potential deadlock w/ application callbacks (similar when sent from HCIHandler's reader-thread) - std::thread bg(&DBTAdapter::mgmtEvDeviceDiscoveringHCI, this, std::shared_ptr<MgmtEvent>( new MgmtEvtDiscovering(dev_id, ScanType::LE, false) ) ); + std::thread bg(&DBTAdapter::mgmtEvDeviceDiscoveringHCI, this, std::shared_ptr<MgmtEvent>( new MgmtEvtDiscovering(dev_id, ScanType::LE, false) ) ); // @suppress("Invalid arguments") bg.detach(); // mgmtEvDeviceDiscoveringHCI( std::shared_ptr<MgmtEvent>( new MgmtEvtDiscovering(dev_id, ScanType::LE, false) ) ); } - DBG_PRINT("DBTAdapter::stopDiscovery: End: Result %d, keepAlive %d, currentScanType[native %s, meta %s], discoveryTempDisabled %d ...", - res, keepDiscoveringAlive.load(), + DBG_PRINT("DBTAdapter::stopDiscovery: End: Result %s, keepAlive %d, currentScanType[native %s, meta %s], discoveryTempDisabled %d ...", + getHCIStatusCodeString(status).c_str(), keepDiscoveringAlive.load(), getScanTypeString(currentNativeScanType).c_str(), getScanTypeString(currentMetaScanType).c_str(), discoveryTempDisabled); checkDiscoveryState(); - return res; + return status; } std::shared_ptr<DBTDevice> DBTAdapter::findDiscoveredDevice (EUI48 const & mac, const BDAddressType macType) noexcept { @@ -754,7 +746,7 @@ bool DBTAdapter::mgmtEvDeviceDiscoveringMgmt(std::shared_ptr<MgmtEvent> e) noexc i++; }); if( ScanType::NONE == currentNativeScanType && keepDiscoveringAlive ) { - std::thread bg(&DBTAdapter::startDiscoveryBackground, this); + std::thread bg(&DBTAdapter::startDiscoveryBackground, this); // @suppress("Invalid arguments") bg.detach(); } return true; @@ -777,7 +769,7 @@ bool DBTAdapter::mgmtEvNewSettingsMgmt(std::shared_ptr<MgmtEvent> e) noexcept { if( !isPowered() ) { // Adapter has been powered off, close connections and cleanup off-thread. - std::thread bg(&DBTAdapter::poweredOff, this); + std::thread bg(&DBTAdapter::poweredOff, this); // @suppress("Invalid arguments") bg.detach(); } |