diff options
author | Sven Gothel <[email protected]> | 2020-06-09 20:55:50 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2020-06-09 20:55:50 +0200 |
commit | 888cda5041d45c98b93c2ed506d08f3f453240f8 (patch) | |
tree | b6944321d87c4499ce07cf8f57beea8ec4e1334e | |
parent | 0ae1a240086654ada3f9805c5fd4a38003007696 (diff) |
C++: Utilize 'currentScanType' for start/stop discovery same as for Java code; use atomic<ScanType> avoiding race conditions
C++/Java: Only update the discovering state for false in DISCOVERING listener if keepAlive == false.
C++ like Java pendant, only perform start/stop discovery in appropriate discovery state
C++ expose discovery state 'currentScanType'
-rw-r--r-- | api/direct_bt/DBTAdapter.hpp | 18 | ||||
-rw-r--r-- | java/direct_bt/tinyb/DBTAdapter.java | 4 | ||||
-rw-r--r-- | src/direct_bt/DBTAdapter.cpp | 13 |
3 files changed, 32 insertions, 3 deletions
diff --git a/api/direct_bt/DBTAdapter.hpp b/api/direct_bt/DBTAdapter.hpp index 48585a81..17bb9e43 100644 --- a/api/direct_bt/DBTAdapter.hpp +++ b/api/direct_bt/DBTAdapter.hpp @@ -155,7 +155,8 @@ namespace direct_bt { DBTManager& mgmt; std::shared_ptr<AdapterInfo> adapterInfo; NameAndShortName localName; - ScanType currentScanType = ScanType::SCAN_TYPE_NONE; + std::atomic<ScanType> currentScanType; // = ScanType::SCAN_TYPE_NONE + std::atomic<bool> keepDiscoveringAlive; // = false; std::shared_ptr<HCIHandler> hci; std::vector<std::shared_ptr<DBTDevice>> connectedDevices; @@ -167,7 +168,6 @@ namespace direct_bt { std::recursive_mutex mtx_discoveredDevices; std::recursive_mutex mtx_sharedDevices; std::recursive_mutex mtx_statusListenerList; - std::atomic<bool> keepDiscoveringAlive; // = false; bool validateDevInfo(); @@ -417,6 +417,20 @@ namespace direct_bt { void stopDiscovery(); /** + * Returns the discovering state the adapter. It can be modified through startDiscovery(..) and stopDiscovery(). + */ + ScanType getDiscoveringScanType() const { + return currentScanType; + } + + /** + * Returns the discovering state the adapter. It can be modified through startDiscovery(..) and stopDiscovery(). + */ + bool getDiscovering() const { + return ScanType::SCAN_TYPE_NONE != currentScanType; + } + + /** * Returns discovered devices from the last discovery. * <p> * Note that this list will be cleared when a new discovery is started over via startDiscovery(). diff --git a/java/direct_bt/tinyb/DBTAdapter.java b/java/direct_bt/tinyb/DBTAdapter.java index 4f0d1025..4cbc071f 100644 --- a/java/direct_bt/tinyb/DBTAdapter.java +++ b/java/direct_bt/tinyb/DBTAdapter.java @@ -429,6 +429,10 @@ public class DBTAdapter extends DBTObject implements BluetoothAdapter if( DEBUG ) { System.err.println("Adapter.StatusListener.DISCOVERING: enabled "+enabled+", keepAlive "+keepAlive+" on "+adapter); } + if( !enabled && keepAlive ) { + // Don't update isDiscovering:=false and don't notify user IF keepAlive! + return; + } if( isDiscovering.compareAndSet(!enabled, enabled) ) { synchronized(userCallbackLock) { if( null != userDiscoveringNotificationCB ) { diff --git a/src/direct_bt/DBTAdapter.cpp b/src/direct_bt/DBTAdapter.cpp index 293937d8..5b2a7643 100644 --- a/src/direct_bt/DBTAdapter.cpp +++ b/src/direct_bt/DBTAdapter.cpp @@ -106,6 +106,7 @@ std::shared_ptr<DBTDevice> DBTAdapter::findConnectedDevice (EUI48 const & mac) c // ************************************************* bool DBTAdapter::validateDevInfo() { + currentScanType = ScanType::SCAN_TYPE_NONE; keepDiscoveringAlive = false; if( !mgmt.isOpen() || 0 > dev_id ) { @@ -302,6 +303,9 @@ int DBTAdapter::removeAllStatusListener() { bool DBTAdapter::startDiscovery(const bool keepAlive, const HCIAddressType own_mac_type, const uint16_t le_scan_interval, const uint16_t le_scan_window) { + if( ScanType::SCAN_TYPE_NONE != currentScanType ) { + return true; + } (void)own_mac_type; (void)le_scan_interval; (void)le_scan_window; @@ -318,8 +322,11 @@ void DBTAdapter::startDiscoveryBackground() { } void DBTAdapter::stopDiscovery() { - DBG_PRINT("DBTAdapter::stopDiscovery: ..."); keepDiscoveringAlive = false; + if( ScanType::SCAN_TYPE_NONE == currentScanType ) { + return; + } + DBG_PRINT("DBTAdapter::stopDiscovery: ..."); if( mgmt.stopDiscovery(dev_id, currentScanType) ) { currentScanType = ScanType::SCAN_TYPE_NONE; } @@ -441,6 +448,10 @@ bool DBTAdapter::mgmtEvDeviceDiscoveringCB(std::shared_ptr<MgmtEvent> e) { dev_id, keepDiscoveringAlive.load(), e->toString().c_str()); const MgmtEvtDiscovering &event = *static_cast<const MgmtEvtDiscovering *>(e.get()); const bool enabled = event.getEnabled(); + if( !enabled && !keepDiscoveringAlive ) { + // Only update currentScanType:=false IF keepAlive==false! + currentScanType = ScanType::SCAN_TYPE_NONE; + } int i=0; for_each_idx_mtx(mtx_statusListenerList, statusListenerList, [&](std::shared_ptr<AdapterStatusListener> &l) { try { |