diff options
author | Sven Gothel <[email protected]> | 2022-09-14 16:09:53 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2022-09-14 16:09:53 +0200 |
commit | c1471e1260ac34a30961043c1e5d1745714c0225 (patch) | |
tree | 205121d0f66ef78f4cad3b18b586a77a017a51d0 | |
parent | 011fa250d7de5b3758ed3ab6baecacd5344cb19d (diff) |
BTAdapter: Use jau::service_runner to ensure singleton pattern on background discovery
startDiscoveryBackground() -> discovery_service + discoveryServerWork()
-rw-r--r-- | api/direct_bt/BTAdapter.hpp | 3 | ||||
-rw-r--r-- | src/direct_bt/BTAdapter.cpp | 43 |
2 files changed, 25 insertions, 21 deletions
diff --git a/api/direct_bt/BTAdapter.hpp b/api/direct_bt/BTAdapter.hpp index a0a52b52..23c5c162 100644 --- a/api/direct_bt/BTAdapter.hpp +++ b/api/direct_bt/BTAdapter.hpp @@ -559,7 +559,8 @@ namespace direct_bt { void notifyPairingStageDone(BTDeviceRef device, uint64_t timestamp) noexcept; void sendDeviceReady(BTDeviceRef device, uint64_t timestamp) noexcept; - void startDiscoveryBackground() noexcept; + jau::service_runner discovery_service; + void discoveryServerWork(jau::service_runner& sr) noexcept; void checkDiscoveryState() noexcept; void sendAdapterSettingsChanged(const AdapterSetting old_settings_, const AdapterSetting current_settings, AdapterSetting changes, diff --git a/src/direct_bt/BTAdapter.cpp b/src/direct_bt/BTAdapter.cpp index 28d32610..39065161 100644 --- a/src/direct_bt/BTAdapter.cpp +++ b/src/direct_bt/BTAdapter.cpp @@ -173,8 +173,7 @@ bool BTAdapter::removeDevicePausingDiscovery(const BTDevice & device) noexcept { } } if( removed_last ) { - std::thread bg(&BTAdapter::startDiscoveryBackground, this); // @suppress("Invalid arguments") - bg.detach(); + discovery_service.start(); return true; } else { return false; @@ -391,7 +390,10 @@ BTAdapter::BTAdapter(const BTAdapter::ctor_cookie& cc, const BTManagerRef& mgmt_ l2cap_service("BTAdapter::l2capServer", THREAD_SHUTDOWN_TIMEOUT_MS, jau::bindMemberFunc(this, &BTAdapter::l2capServerWork), jau::bindMemberFunc(this, &BTAdapter::l2capServerInit), - jau::bindMemberFunc(this, &BTAdapter::l2capServerEnd)) + jau::bindMemberFunc(this, &BTAdapter::l2capServerEnd)), + discovery_service("BTAdapter::discoveryServer", 400_ms, + jau::bindMemberFunc(this, &BTAdapter::discoveryServerWork)) + { (void)cc; @@ -449,7 +451,8 @@ void BTAdapter::close() noexcept { hci.close(); l2cap_service.stop(); l2cap_att_srv.close(); - DBG_PRINT("BTAdapter::close: close[HCI, l2cap_srv]: XXX"); + discovery_service.stop(); + DBG_PRINT("BTAdapter::close: close[HCI, l2cap_srv, discovery_srv]: XXX"); { const std::lock_guard<std::mutex> lock(mtx_discoveredDevices); // RAII-style acquire and relinquish via destructor @@ -1121,16 +1124,14 @@ HCIStatusCode BTAdapter::startDiscovery(const DiscoveryPolicy policy, const bool return status; } -void BTAdapter::startDiscoveryBackground() noexcept { - jau::nsize_t trial_count = 0; - bool retry; - do { - // FIXME: Respect BTAdapter::btMode, i.e. BTMode::BREDR, BTMode::LE or BTMode::DUAL to setup BREDR, LE or DUAL scanning! - if( !isPowered() ) { // isValid() && hci.isOpen() && POWERED - poweredOff(false /* active */); - return; - } - retry = false; +void BTAdapter::discoveryServerWork(jau::service_runner& sr) noexcept { + static jau::nsize_t trial_count = 0; + bool retry = false; + + // FIXME: Respect BTAdapter::btMode, i.e. BTMode::BREDR, BTMode::LE or BTMode::DUAL to setup BREDR, LE or DUAL scanning! + if( !isPowered() ) { // isValid() && hci.isOpen() && POWERED + poweredOff(false /* active */); + } else { { const std::lock_guard<std::mutex> lock(mtx_discovery); // RAII-style acquire and relinquish via destructor const ScanType currentNativeScanType = hci.getCurrentScanType(); @@ -1157,10 +1158,14 @@ void BTAdapter::startDiscoveryBackground() noexcept { checkDiscoveryState(); } } - if( retry ) { + if( retry && !sr.shall_stop() ) { std::this_thread::sleep_for(std::chrono::milliseconds(100)); // wait a little (FIXME) } - } while( retry ); + } + if( !retry ) { + trial_count=0; + sr.set_shall_stop(); + } } HCIStatusCode BTAdapter::stopDiscovery() noexcept { @@ -1708,8 +1713,7 @@ bool BTAdapter::mgmtEvDeviceDiscoveringAny(const ScanType eventScanType, const b DiscoveryPolicy::AUTO_OFF != discovery_policy && !hasDevicesPausingDiscovery() ) { - std::thread bg(&BTAdapter::startDiscoveryBackground, this); // @suppress("Invalid arguments") - bg.detach(); + discovery_service.start(); } return true; } @@ -2082,8 +2086,7 @@ bool BTAdapter::mgmtEvHCILERemoteUserFeaturesHCI(const MgmtEvent& e) noexcept { if constexpr ( SCAN_DISABLED_POST_CONNECT ) { updateDeviceDiscoveringState(ScanType::LE, false /* eventEnabled */); } else { - std::thread bg(&BTAdapter::startDiscoveryBackground, this); // @suppress("Invalid arguments") - bg.detach(); + discovery_service.start(); } } else { addDevicePausingDiscovery(device); |