From fe3caaf19c429a2fda0a747603e46c39dde4716c Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Mon, 1 Feb 2021 16:36:59 +0100 Subject: DBTDevice.hpp/cpp: Remove const_cast on now explicit mutable mutex and atomic-sync members in const methods. --- api/direct_bt/BTDevice.hpp | 10 +++++----- src/direct_bt/BTDevice.cpp | 24 ++++++++++++------------ 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/api/direct_bt/BTDevice.hpp b/api/direct_bt/BTDevice.hpp index ae12e33b..f48fee8d 100644 --- a/api/direct_bt/BTDevice.hpp +++ b/api/direct_bt/BTDevice.hpp @@ -75,9 +75,9 @@ namespace direct_bt { std::recursive_mutex mtx_smpHandler; #endif std::shared_ptr gattHandler = nullptr; - std::recursive_mutex mtx_gattHandler; - std::recursive_mutex mtx_connect; - std::recursive_mutex mtx_data; + mutable std::recursive_mutex mtx_gattHandler; + mutable std::recursive_mutex mtx_connect; + mutable std::recursive_mutex mtx_data; std::atomic isConnected; std::atomic allowDisconnect; // allowDisconnect = isConnected || 'isConnectIssued' @@ -111,8 +111,8 @@ namespace direct_bt { SMPSignatureResolvingKeyInfo csrk_init, csrk_resp; }; PairingData pairing_data; - std::mutex mtx_pairing; - jau::sc_atomic_bool sync_pairing; + mutable std::mutex mtx_pairing; + mutable jau::sc_atomic_bool sync_pairing; /** Private class only for private make_shared(). */ class ctor_cookie { friend BTDevice; ctor_cookie(const uint16_t secret) { (void)secret; } }; diff --git a/src/direct_bt/BTDevice.cpp b/src/direct_bt/BTDevice.cpp index 27fd5be4..bc01a65e 100644 --- a/src/direct_bt/BTDevice.cpp +++ b/src/direct_bt/BTDevice.cpp @@ -120,24 +120,24 @@ int BTDevice::findAdvService(std::shared_ptr const &uuid) const noexcept } std::string const BTDevice::getName() const noexcept { - const std::lock_guard lock(const_cast(this)->mtx_data); // RAII-style acquire and relinquish via destructor + const std::lock_guard lock(mtx_data); // RAII-style acquire and relinquish via destructor return name; } std::shared_ptr const BTDevice::getManufactureSpecificData() const noexcept { - const std::lock_guard lock(const_cast(this)->mtx_data); // RAII-style acquire and relinquish via destructor + const std::lock_guard lock(mtx_data); // RAII-style acquire and relinquish via destructor return advMSD; } jau::darray> BTDevice::getAdvertisedServices() const noexcept { - const std::lock_guard lock(const_cast(this)->mtx_data); // RAII-style acquire and relinquish via destructor + const std::lock_guard lock(mtx_data); // RAII-style acquire and relinquish via destructor return advServices; } std::string BTDevice::toString(bool includeDiscoveredServices) const noexcept { - const std::lock_guard lock(const_cast(this)->mtx_data); // RAII-style acquire and relinquish via destructor + const std::lock_guard lock(mtx_data); // RAII-style acquire and relinquish via destructor const uint64_t t0 = jau::getCurrentMilliseconds(); - jau::sc_atomic_critical sync(const_cast(this)->sync_pairing); + jau::sc_atomic_critical sync(sync_pairing); std::string msdstr = nullptr != advMSD ? advMSD->toString() : "MSD[null]"; std::string out("Device["+addressAndType.toString()+", name['"+name+ "'], age[total "+std::to_string(t0-ts_creation)+", ldisc "+std::to_string(t0-ts_last_discovery)+", lup "+std::to_string(t0-ts_last_update)+ @@ -960,7 +960,7 @@ void BTDevice::hciSMPMsgCallback(std::shared_ptr sthis, const SMPPDUMs } SMPKeyType BTDevice::getAvailableSMPKeys(const bool responder) const noexcept { - jau::sc_atomic_critical sync(const_cast(this)->sync_pairing); + jau::sc_atomic_critical sync(sync_pairing); if( responder ) { return pairing_data.keys_resp_has; } else { @@ -969,7 +969,7 @@ SMPKeyType BTDevice::getAvailableSMPKeys(const bool responder) const noexcept { } SMPLongTermKeyInfo BTDevice::getLongTermKeyInfo(const bool responder) const noexcept { - jau::sc_atomic_critical sync(const_cast(this)->sync_pairing); + jau::sc_atomic_critical sync(sync_pairing); return responder ? pairing_data.ltk_resp : pairing_data.ltk_init; } @@ -991,7 +991,7 @@ HCIStatusCode BTDevice::setLongTermKeyInfo(const SMPLongTermKeyInfo& ltk) noexce } SMPSignatureResolvingKeyInfo BTDevice::getSignatureResolvingKeyInfo(const bool responder) const noexcept { - jau::sc_atomic_critical sync(const_cast(this)->sync_pairing); + jau::sc_atomic_critical sync(sync_pairing); return responder ? pairing_data.csrk_resp : pairing_data.csrk_init; } @@ -1044,7 +1044,7 @@ bool BTDevice::setConnSecurityLevel(const BTSecurityLevel sec_level) noexcept { } BTSecurityLevel BTDevice::getConnSecurityLevel() const noexcept { - jau::sc_atomic_critical sync(const_cast(this)->sync_pairing); + jau::sc_atomic_critical sync(sync_pairing); return pairing_data.sec_level_conn; } @@ -1071,7 +1071,7 @@ bool BTDevice::setConnIOCapability(const SMPIOCapability io_cap) noexcept { } SMPIOCapability BTDevice::getConnIOCapability() const noexcept { - jau::sc_atomic_critical sync(const_cast(this)->sync_pairing); + jau::sc_atomic_critical sync(sync_pairing); return pairing_data.ioCap_conn; } @@ -1172,12 +1172,12 @@ HCIStatusCode BTDevice::setPairingNumericComparison(const bool positive) noexcep } PairingMode BTDevice::getPairingMode() const noexcept { - jau::sc_atomic_critical sync(const_cast(this)->sync_pairing); + jau::sc_atomic_critical sync(sync_pairing); return pairing_data.mode; } SMPPairingState BTDevice::getPairingState() const noexcept { - jau::sc_atomic_critical sync(const_cast(this)->sync_pairing); + jau::sc_atomic_critical sync(sync_pairing); return pairing_data.state; } -- cgit v1.2.3