diff options
author | Sven Gothel <[email protected]> | 2020-10-14 18:43:27 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2020-10-14 18:43:27 +0200 |
commit | 6ee66ac954a7e2c9f909e1f27debd02cf9954a4e (patch) | |
tree | 3e40e0cf4ed053735d8c50a3718815b15feb4996 | |
parent | db056cb4c7dd114aa11adecb0b660d86a3612650 (diff) |
AdapterInfo Cleanup: AdapterSetting current_setting -> atomic, drop its atomic-copy in DBTAdapter. Simplify related method[-names]s.
-rw-r--r-- | api/direct_bt/DBTAdapter.hpp | 1 | ||||
-rw-r--r-- | api/direct_bt/DBTTypes.hpp | 53 | ||||
-rw-r--r-- | api/direct_bt/MgmtTypes.hpp | 4 | ||||
-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 | 12 | ||||
-rw-r--r-- | src/direct_bt/DBTAdapter.cpp | 31 | ||||
-rw-r--r-- | src/direct_bt/DBTTypes.cpp | 6 |
8 files changed, 59 insertions, 60 deletions
diff --git a/api/direct_bt/DBTAdapter.hpp b/api/direct_bt/DBTAdapter.hpp index 0b7f0402..280a8478 100644 --- a/api/direct_bt/DBTAdapter.hpp +++ b/api/direct_bt/DBTAdapter.hpp @@ -168,7 +168,6 @@ namespace direct_bt { const bool debug_event; DBTManager& mgmt; std::shared_ptr<AdapterInfo> adapterInfo; - std::atomic<AdapterSetting> currentAdapterSettings; std::atomic<BTMode> btMode = BTMode::NONE; NameAndShortName localName; std::atomic<ScanType> currentMetaScanType; // = ScanType::NONE diff --git a/api/direct_bt/DBTTypes.hpp b/api/direct_bt/DBTTypes.hpp index a44e94ca..e94dceb6 100644 --- a/api/direct_bt/DBTTypes.hpp +++ b/api/direct_bt/DBTTypes.hpp @@ -165,26 +165,29 @@ namespace direct_bt { STATIC_ADDRESS = 0x00008000, PHY_CONFIGURATION = 0x00010000 }; - inline AdapterSetting operator ^(const AdapterSetting lhs, const AdapterSetting rhs) noexcept { + constexpr AdapterSetting operator ^(const AdapterSetting lhs, const AdapterSetting rhs) noexcept { return static_cast<AdapterSetting> ( static_cast<uint32_t>(lhs) ^ static_cast<uint32_t>(rhs) ); } - inline AdapterSetting operator |(const AdapterSetting lhs, const AdapterSetting rhs) noexcept { + constexpr AdapterSetting operator |(const AdapterSetting lhs, const AdapterSetting rhs) noexcept { return static_cast<AdapterSetting> ( static_cast<uint32_t>(lhs) | static_cast<uint32_t>(rhs) ); } - inline AdapterSetting operator &(const AdapterSetting lhs, const AdapterSetting rhs) noexcept { + constexpr AdapterSetting operator &(const AdapterSetting lhs, const AdapterSetting rhs) noexcept { return static_cast<AdapterSetting> ( static_cast<uint32_t>(lhs) & static_cast<uint32_t>(rhs) ); } - inline bool operator ==(const AdapterSetting lhs, const AdapterSetting rhs) noexcept { + constexpr bool operator ==(const AdapterSetting lhs, const AdapterSetting rhs) noexcept { return static_cast<uint32_t>(lhs) == static_cast<uint32_t>(rhs); } - inline bool operator !=(const AdapterSetting lhs, const AdapterSetting rhs) noexcept { + constexpr bool operator !=(const AdapterSetting lhs, const AdapterSetting rhs) noexcept { return !( lhs == rhs ); } - inline bool isAdapterSettingSet(const AdapterSetting mask, const AdapterSetting bit) noexcept { return AdapterSetting::NONE != ( mask & bit ); } - inline void setAdapterSettingSet(AdapterSetting &mask, const AdapterSetting bit) noexcept { mask = mask | bit; } + + constexpr AdapterSetting getAdapterSettingMaskDiff(const AdapterSetting setting_a, const AdapterSetting setting_b) noexcept { return setting_a ^ setting_b; } + + constexpr bool isAdapterSettingBitSet(const AdapterSetting mask, const AdapterSetting bit) noexcept { return AdapterSetting::NONE != ( mask & bit ); } + + inline void setAdapterSettingMaskBit(AdapterSetting &mask, const AdapterSetting bit) noexcept { mask = mask | bit; } std::string getAdapterSettingBitString(const AdapterSetting settingBit) noexcept; - std::string getAdapterSettingsString(const AdapterSetting settingBitMask) noexcept; - inline AdapterSetting getAdapterSettingsDelta(const AdapterSetting setting_a, const AdapterSetting setting_b) noexcept { return setting_a ^ setting_b; } + std::string getAdapterSettingMaskString(const AdapterSetting settingBitMask) noexcept; /** Maps the given {@link AdapterSetting} to {@link BTMode} */ BTMode getAdapterSettingsBTMode(const AdapterSetting settingMask) noexcept; @@ -202,22 +205,20 @@ namespace direct_bt { const AdapterSetting supported_setting; private: - AdapterSetting current_setting; + std::atomic<AdapterSetting> current_setting; uint32_t dev_class; std::string name; std::string short_name; /** - * Sets the current_setting and returns the changed AdapterSetting bit-mask. + * Assigns the given 'new_setting & supported_setting' to the current_setting. + * @param new_setting assigned to current_setting after masking with supported_setting. + * @return 'new_setting & supported_setting', i.e. the new current_setting. */ - AdapterSetting setCurrentSetting(AdapterSetting new_setting) noexcept { - new_setting = new_setting & supported_setting; - AdapterSetting changes = getAdapterSettingsDelta(new_setting, current_setting); - - if( AdapterSetting::NONE != changes ) { - current_setting = new_setting; - } - return changes; + AdapterSetting setCurrentSettingMask(const AdapterSetting new_setting) noexcept { + const AdapterSetting _current_setting = new_setting & supported_setting; + current_setting = _current_setting; + return _current_setting; } void setDevClass(const uint32_t v) noexcept { dev_class = v; } void setName(const std::string v) noexcept { name = v; } @@ -234,21 +235,23 @@ namespace direct_bt { name(name), short_name(short_name) { } - bool isSettingSupported(const AdapterSetting setting) const noexcept { + bool isSettingMaskSupported(const AdapterSetting setting) const noexcept { return setting == ( setting & supported_setting ); } - AdapterSetting getCurrentSetting() const noexcept { return current_setting; } + AdapterSetting getCurrentSettingMask() const noexcept { return current_setting; } + bool isCurrentSettingBitSet(const AdapterSetting bit) noexcept { return AdapterSetting::NONE != ( current_setting & bit ); } + + /** Map {@link #getCurrentSettingMask()} to {@link BTMode} */ + BTMode getCurrentBTMode() const noexcept { return getAdapterSettingsBTMode(current_setting); } + uint32_t getDevClass() const noexcept { return dev_class; } std::string getName() const noexcept { return name; } std::string getShortName() const noexcept { return short_name; } - /** Map {@link #getCurrentSetting()} to {@link BTMode} */ - BTMode getCurrentBTMode() const noexcept { return getAdapterSettingsBTMode(current_setting); } - std::string toString() const noexcept { return "Adapter[id "+std::to_string(dev_id)+", address "+address.toString()+", version "+std::to_string(version)+ ", manuf "+std::to_string(manufacturer)+ - ", settings[sup "+getAdapterSettingsString(supported_setting)+", cur "+getAdapterSettingsString(current_setting)+ + ", settings[sup "+getAdapterSettingMaskString(supported_setting)+", cur "+getAdapterSettingMaskString(current_setting)+ "], name '"+name+"', shortName '"+short_name+"']"; } }; diff --git a/api/direct_bt/MgmtTypes.hpp b/api/direct_bt/MgmtTypes.hpp index d76b3fb4..ab31ce4b 100644 --- a/api/direct_bt/MgmtTypes.hpp +++ b/api/direct_bt/MgmtTypes.hpp @@ -774,7 +774,7 @@ namespace direct_bt { protected: std::string baseString() const override { - return MgmtEvent::baseString()+", settings="+getAdapterSettingsString(getSettings()); + return MgmtEvent::baseString()+", settings="+getAdapterSettingMaskString(getSettings()); } public: @@ -1246,7 +1246,7 @@ namespace direct_bt { std::string valueString() const override { return getAddress().toString()+", version "+std::to_string(getVersion())+ ", manuf "+std::to_string(getManufacturer())+ - ", settings[sup "+getAdapterSettingsString(getSupportedSetting())+", cur "+getAdapterSettingsString(getCurrentSetting())+ + ", settings[sup "+getAdapterSettingMaskString(getSupportedSetting())+", cur "+getAdapterSettingMaskString(getCurrentSetting())+ "], name '"+getName()+"', shortName '"+getShortName()+"'"; } diff --git a/examples/direct_bt_scanner00/dbt_scanner00.cpp b/examples/direct_bt_scanner00/dbt_scanner00.cpp index 0940de7e..db42d2e6 100644 --- a/examples/direct_bt_scanner00/dbt_scanner00.cpp +++ b/examples/direct_bt_scanner00/dbt_scanner00.cpp @@ -54,9 +54,9 @@ class MyAdapterStatusListener : public AdapterStatusListener { 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(), - getAdapterSettingsString(newmask).c_str(), - getAdapterSettingsString(changedmask).c_str()); + getAdapterSettingMaskString(oldmask).c_str(), + getAdapterSettingMaskString(newmask).c_str(), + getAdapterSettingMaskString(changedmask).c_str()); fprintf(stderr, "Status DBTAdapter:\n"); fprintf(stderr, "%s\n", a.toString().c_str()); (void)timestamp; diff --git a/examples/direct_bt_scanner01/dbt_scanner01.cpp b/examples/direct_bt_scanner01/dbt_scanner01.cpp index a63deb25..7c666b28 100644 --- a/examples/direct_bt_scanner01/dbt_scanner01.cpp +++ b/examples/direct_bt_scanner01/dbt_scanner01.cpp @@ -51,9 +51,9 @@ class MyAdapterStatusListener : public AdapterStatusListener { 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(), - getAdapterSettingsString(newmask).c_str(), - getAdapterSettingsString(changedmask).c_str()); + getAdapterSettingMaskString(oldmask).c_str(), + getAdapterSettingMaskString(newmask).c_str(), + getAdapterSettingMaskString(changedmask).c_str()); fprintf(stderr, "Status DBTAdapter:\n"); fprintf(stderr, "%s\n", a.toString().c_str()); (void)timestamp; diff --git a/examples/direct_bt_scanner10/dbt_scanner10.cpp b/examples/direct_bt_scanner10/dbt_scanner10.cpp index a8a60646..f68b2e58 100644 --- a/examples/direct_bt_scanner10/dbt_scanner10.cpp +++ b/examples/direct_bt_scanner10/dbt_scanner10.cpp @@ -151,19 +151,19 @@ class MyAdapterStatusListener : public AdapterStatusListener { const AdapterSetting changedmask, const uint64_t timestamp) override { const bool initialSetting = AdapterSetting::NONE == oldmask; if( initialSetting ) { - fprintf(stderr, "****** SETTINGS_INITIAL: %s -> %s, changed %s\n", getAdapterSettingsString(oldmask).c_str(), - getAdapterSettingsString(newmask).c_str(), getAdapterSettingsString(changedmask).c_str()); + fprintf(stderr, "****** SETTINGS_INITIAL: %s -> %s, changed %s\n", getAdapterSettingMaskString(oldmask).c_str(), + getAdapterSettingMaskString(newmask).c_str(), getAdapterSettingMaskString(changedmask).c_str()); } else { - fprintf(stderr, "****** SETTINGS_CHANGED: %s -> %s, changed %s\n", getAdapterSettingsString(oldmask).c_str(), - getAdapterSettingsString(newmask).c_str(), getAdapterSettingsString(changedmask).c_str()); + fprintf(stderr, "****** SETTINGS_CHANGED: %s -> %s, changed %s\n", getAdapterSettingMaskString(oldmask).c_str(), + getAdapterSettingMaskString(newmask).c_str(), getAdapterSettingMaskString(changedmask).c_str()); } fprintf(stderr, "Status DBTAdapter:\n"); fprintf(stderr, "%s\n", a.toString().c_str()); (void)timestamp; if( !initialSetting && - isAdapterSettingSet(changedmask, AdapterSetting::POWERED) && - isAdapterSettingSet(newmask, AdapterSetting::POWERED) ) + isAdapterSettingBitSet(changedmask, AdapterSetting::POWERED) && + isAdapterSettingBitSet(newmask, AdapterSetting::POWERED) ) { HCIStatusCode status = a.startDiscovery( true ); if( HCIStatusCode::SUCCESS != status ) { diff --git a/src/direct_bt/DBTAdapter.cpp b/src/direct_bt/DBTAdapter.cpp index bde124a2..2f7720ff 100644 --- a/src/direct_bt/DBTAdapter.cpp +++ b/src/direct_bt/DBTAdapter.cpp @@ -188,7 +188,6 @@ bool DBTAdapter::validateDevInfo() noexcept { } adapterInfo = mgmt.getAdapterInfo(dev_id); - currentAdapterSettings = adapterInfo->getCurrentSetting(); btMode = adapterInfo->getCurrentBTMode(); if( BTMode::NONE == btMode ) { @@ -378,7 +377,7 @@ bool DBTAdapter::addStatusListener(std::shared_ptr<AdapterStatusListener> l) { } statusListenerList.push_back(l); } - sendAdapterSettingsChanged(*l, AdapterSetting::NONE, currentAdapterSettings, getCurrentMilliseconds()); + sendAdapterSettingsChanged(*l, AdapterSetting::NONE, adapterInfo->getCurrentSettingMask(), getCurrentMilliseconds()); return true; } @@ -686,7 +685,7 @@ void DBTAdapter::removeDevice(DBTDevice & device) noexcept { std::string DBTAdapter::toString(bool includeDiscoveredDevices) const noexcept { std::string out("Adapter[BTMode "+getBTModeString(btMode)+", "+getAddressString()+", '"+getName()+"', id "+std::to_string(dev_id)+ - ", curSettings"+getAdapterSettingsString(currentAdapterSettings)+ + ", curSettings"+getAdapterSettingMaskString(adapterInfo->getCurrentSettingMask())+ ", scanType[native "+getScanTypeString(currentNativeScanType)+", meta "+getScanTypeString(currentMetaScanType)+"]" ", "+javaObjectToString()+"]"); std::vector<std::shared_ptr<DBTDevice>> devices = getDiscoveredDevices(); @@ -748,17 +747,15 @@ bool DBTAdapter::mgmtEvDeviceDiscoveringMgmt(std::shared_ptr<MgmtEvent> e) noexc bool DBTAdapter::mgmtEvNewSettingsMgmt(std::shared_ptr<MgmtEvent> e) noexcept { COND_PRINT(debug_event, "DBTAdapter::EventCB:NewSettings: %s", e->toString().c_str()); const MgmtEvtNewSettings &event = *static_cast<const MgmtEvtNewSettings *>(e.get()); - AdapterSetting old_settings = adapterInfo->getCurrentSetting(); - /* AdapterSetting changes = */ adapterInfo->setCurrentSetting(event.getSettings()); - currentAdapterSettings = adapterInfo->getCurrentSetting(); + const AdapterSetting old_settings = adapterInfo->getCurrentSettingMask(); + const AdapterSetting new_settings = adapterInfo->setCurrentSettingMask(event.getSettings()); { - const BTMode _btMode = adapterInfo->getCurrentBTMode(); + const BTMode _btMode = getAdapterSettingsBTMode(new_settings); if( BTMode::NONE != _btMode ) { btMode = _btMode; } } - - sendAdapterSettingsChanged(old_settings, currentAdapterSettings, event.getTimestamp()); + sendAdapterSettingsChanged(old_settings, new_settings, event.getTimestamp()); if( !isPowered() ) { // Adapter has been powered off, close connections and cleanup off-thread. @@ -772,11 +769,11 @@ bool DBTAdapter::mgmtEvNewSettingsMgmt(std::shared_ptr<MgmtEvent> e) noexcept { void DBTAdapter::sendAdapterSettingsChanged(const AdapterSetting old_settings, const AdapterSetting current_settings, const uint64_t timestampMS) noexcept { - AdapterSetting changes = getAdapterSettingsDelta(current_settings, old_settings); + AdapterSetting changes = getAdapterSettingMaskDiff(current_settings, old_settings); COND_PRINT(debug_event, "DBTAdapter::sendAdapterSettingsChanged: %s -> %s, changes %s: %s", - getAdapterSettingsString(old_settings).c_str(), - getAdapterSettingsString(current_settings).c_str(), - getAdapterSettingsString(changes).c_str(), toString(false).c_str() ); + getAdapterSettingMaskString(old_settings).c_str(), + getAdapterSettingMaskString(current_settings).c_str(), + getAdapterSettingMaskString(changes).c_str(), toString(false).c_str() ); int i=0; for_each_idx_mtx(mtx_statusListenerList, statusListenerList, [&](std::shared_ptr<AdapterStatusListener> &l) { try { @@ -794,11 +791,11 @@ void DBTAdapter::sendAdapterSettingsChanged(AdapterStatusListener & asl, const AdapterSetting old_settings, const AdapterSetting current_settings, const uint64_t timestampMS) noexcept { - AdapterSetting changes = getAdapterSettingsDelta(current_settings, old_settings); + AdapterSetting changes = getAdapterSettingMaskDiff(current_settings, old_settings); COND_PRINT(debug_event, "DBTAdapter::sendAdapterSettingsChanged: %s -> %s, changes %s: %s", - getAdapterSettingsString(old_settings).c_str(), - getAdapterSettingsString(current_settings).c_str(), - getAdapterSettingsString(changes).c_str(), toString(false).c_str() ); + getAdapterSettingMaskString(old_settings).c_str(), + getAdapterSettingMaskString(current_settings).c_str(), + getAdapterSettingMaskString(changes).c_str(), toString(false).c_str() ); try { asl.adapterSettingsChanged(*this, old_settings, current_settings, changes, timestampMS); } catch (std::exception &e) { diff --git a/src/direct_bt/DBTTypes.cpp b/src/direct_bt/DBTTypes.cpp index cacd7bb7..ca3624cc 100644 --- a/src/direct_bt/DBTTypes.cpp +++ b/src/direct_bt/DBTTypes.cpp @@ -80,7 +80,7 @@ std::string direct_bt::getAdapterSettingBitString(const AdapterSetting settingBi return "Unknown Setting Bit"; } -std::string direct_bt::getAdapterSettingsString(const AdapterSetting settingMask) noexcept { +std::string direct_bt::getAdapterSettingMaskString(const AdapterSetting settingMask) noexcept { const uint32_t one = 1; bool has_pre = false; std::string out("["); @@ -97,8 +97,8 @@ std::string direct_bt::getAdapterSettingsString(const AdapterSetting settingMask } BTMode direct_bt::getAdapterSettingsBTMode(const AdapterSetting settingMask) noexcept { - const bool isBREDR = isAdapterSettingSet(settingMask, AdapterSetting::BREDR); - const bool isLE = isAdapterSettingSet(settingMask, AdapterSetting::LE); + const bool isBREDR = isAdapterSettingBitSet(settingMask, AdapterSetting::BREDR); + const bool isLE = isAdapterSettingBitSet(settingMask, AdapterSetting::LE); if( isBREDR && isLE ) { return BTMode::DUAL; } else if( isBREDR ) { |