diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/direct_bt/DBTAdapter.cpp | 66 | ||||
-rw-r--r-- | src/direct_bt/DBTDevice.cpp | 1 | ||||
-rw-r--r-- | src/direct_bt/DBTManager.cpp | 216 | ||||
-rw-r--r-- | src/direct_bt/GATTHandler.cpp | 1 | ||||
-rw-r--r-- | src/direct_bt/MgmtTypes.cpp | 4 |
5 files changed, 137 insertions, 151 deletions
diff --git a/src/direct_bt/DBTAdapter.cpp b/src/direct_bt/DBTAdapter.cpp index 389e55ee..82e498e3 100644 --- a/src/direct_bt/DBTAdapter.cpp +++ b/src/direct_bt/DBTAdapter.cpp @@ -40,6 +40,7 @@ #include "HCIComm.hpp" #include "DBTAdapter.hpp" +#include "DBTManager.hpp" extern "C" { #include <inttypes.h> @@ -122,10 +123,6 @@ bool DBTAdapter::validateDevInfo() noexcept { currentMetaScanType = ScanType::NONE; keep_le_scan_alive = false; - if( 0 > dev_id ) { - ERR_PRINT("DBTAdapter::validateDevInfo: Invalid negative dev_id %d", dev_id); - goto errout0; - } if( !mgmt.isOpen() ) { ERR_PRINT("DBTAdapter::validateDevInfo: Adapter[%d]: Manager not open", dev_id); goto errout0; @@ -135,34 +132,28 @@ bool DBTAdapter::validateDevInfo() noexcept { goto errout0; } - adapterInfo = mgmt.getAdapterInfo(dev_id); - if( nullptr == adapterInfo ) { - // fill in a dummy AdapterInfo for the sake of de-referencing throughout this adapter instance - ERR_PRINT("DBTAdapter::validateDevInfo: Adapter[%d]: Not existent", dev_id); - goto errout0; - } - old_settings = adapterInfo->getCurrentSettingMask(); + old_settings = adapterInfo.getCurrentSettingMask(); - btMode = adapterInfo->getCurrentBTMode(); + btMode = getBTMode(); if( BTMode::NONE == btMode ) { - ERR_PRINT("DBTAdapter::validateDevInfo: Adapter[%d]: BTMode invalid, BREDR nor LE set: %s", dev_id, adapterInfo->toString().c_str()); + ERR_PRINT("DBTAdapter::validateDevInfo: Adapter[%d]: BTMode invalid, BREDR nor LE set: %s", dev_id, adapterInfo.toString().c_str()); return false; } hci.setBTMode(btMode); - if( adapterInfo->isCurrentSettingBitSet(AdapterSetting::POWERED) ) { + if( adapterInfo.isCurrentSettingBitSet(AdapterSetting::POWERED) ) { HCILocalVersion version; HCIStatusCode status = hci.getLocalVersion(version); if( HCIStatusCode::SUCCESS != status ) { ERR_PRINT("DBTAdapter::validateDevInfo: Adapter[%d]: POWERED, LocalVersion failed %s - %s", - dev_id, getHCIStatusCodeString(status).c_str(), adapterInfo->toString().c_str()); + dev_id, getHCIStatusCodeString(status).c_str(), adapterInfo.toString().c_str()); return false; } else { WORDY_PRINT("DBTAdapter::validateDevInfo: Adapter[%d]: POWERED, %s - %s", - dev_id, version.toString().c_str(), adapterInfo->toString().c_str()); + dev_id, version.toString().c_str(), adapterInfo.toString().c_str()); } } else { - WORDY_PRINT("DBTAdapter::validateDevInfo: Adapter[%d]: Not POWERED: %s", dev_id, adapterInfo->toString().c_str()); + WORDY_PRINT("DBTAdapter::validateDevInfo: Adapter[%d]: Not POWERED: %s", dev_id, adapterInfo.toString().c_str()); } ok = true; ok = mgmt.addMgmtEventCallback(dev_id, MgmtEvent::Opcode::DISCOVERING, jau::bindMemberFunc(this, &DBTAdapter::mgmtEvDeviceDiscoveringMgmt)) && ok; @@ -203,36 +194,15 @@ bool DBTAdapter::validateDevInfo() noexcept { return true; errout0: - adapterInfo = std::make_shared<AdapterInfo>(dev_id, EUI48::ANY_DEVICE, 0, 0, - AdapterSetting::NONE, AdapterSetting::NONE, 0, "invalid", "invalid"); return false; } -DBTAdapter::DBTAdapter() noexcept -: debug_event(jau::environment::getBooleanProperty("direct_bt.debug.adapter.event", false)), - debug_lock(jau::environment::getBooleanProperty("direct_bt.debug.adapter.lock", false)), - mgmt( DBTManager::get(BTMode::NONE /* use env default */) ), - dev_id( mgmt.getDefaultAdapterDevID() ), - hci( dev_id ) -{ - valid = validateDevInfo(); -} - -DBTAdapter::DBTAdapter(EUI48 &mac) noexcept -: debug_event(jau::environment::getBooleanProperty("direct_bt.debug.adapter.event", false)), - debug_lock(jau::environment::getBooleanProperty("direct_bt.debug.adapter.lock", false)), - mgmt( DBTManager::get(BTMode::NONE /* use env default */) ), - dev_id( mgmt.findAdapterInfoDevId(mac) ), - hci( dev_id ) -{ - valid = validateDevInfo(); -} - -DBTAdapter::DBTAdapter(const int _dev_id) noexcept +DBTAdapter::DBTAdapter(DBTManager& mgmt_, const AdapterInfo& adapterInfo_) noexcept : debug_event(jau::environment::getBooleanProperty("direct_bt.debug.adapter.event", false)), debug_lock(jau::environment::getBooleanProperty("direct_bt.debug.adapter.lock", false)), - mgmt( DBTManager::get(BTMode::NONE /* use env default */) ), - dev_id( 0 <= _dev_id ? _dev_id : mgmt.getDefaultAdapterDevID() ), + mgmt( mgmt_ ), + adapterInfo( adapterInfo_ ), + dev_id( adapterInfo.dev_id ), hci( dev_id ) { valid = validateDevInfo(); @@ -241,10 +211,14 @@ DBTAdapter::DBTAdapter(const int _dev_id) noexcept DBTAdapter::~DBTAdapter() noexcept { if( !isValid() ) { DBG_PRINT("DBTAdapter::dtor: dev_id %d, invalid, %p", dev_id, this); + mgmt.removeAdapter(this); // remove this instance from manager return; } DBG_PRINT("DBTAdapter::dtor: ... %p %s", this, toString().c_str()); close(); + + mgmt.removeAdapter(this); // remove this instance from manager + DBG_PRINT("DBTAdapter::dtor: XXX"); } @@ -700,7 +674,7 @@ HCIStatusCode DBTAdapter::stopDiscovery() noexcept { } HCIStatusCode status; - if( !adapterInfo->isCurrentSettingBitSet(AdapterSetting::POWERED) ) { + if( !adapterInfo.isCurrentSettingBitSet(AdapterSetting::POWERED) ) { WARN_PRINT("DBTAdapter::stopDiscovery: Powered off: %s", toString().c_str()); hci.setCurrentScanType(ScanType::NONE); currentMetaScanType = ScanType::NONE; @@ -831,7 +805,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"+getAdapterSettingMaskString(adapterInfo->getCurrentSettingMask())+ + ", curSettings"+getAdapterSettingMaskString(adapterInfo.getCurrentSettingMask())+ ", scanType[native "+getScanTypeString(hci.getCurrentScanType())+", meta "+getScanTypeString(currentMetaScanType)+"]" ", valid "+std::to_string(isValid())+", open[mgmt, "+std::to_string(mgmt.isOpen())+", hci "+std::to_string(hci.isOpen())+ "], "+javaObjectToString()+"]"); @@ -868,7 +842,7 @@ void DBTAdapter::sendAdapterSettingsChanged(const AdapterSetting old_settings_, void DBTAdapter::sendAdapterSettingsInitial(AdapterStatusListener & asl, const uint64_t timestampMS) noexcept { - const AdapterSetting current_settings = adapterInfo->getCurrentSettingMask(); + const AdapterSetting current_settings = adapterInfo.getCurrentSettingMask(); COND_PRINT(debug_event, "DBTAdapter::sendAdapterSettingsInitial: NONE -> %s, changes NONE: %s", getAdapterSettingMaskString(current_settings).c_str(), toString(false).c_str() ); try { @@ -974,7 +948,7 @@ bool DBTAdapter::mgmtEvDeviceDiscoveringAny(const MgmtEvent& e, const bool hciSo bool DBTAdapter::mgmtEvNewSettingsMgmt(const MgmtEvent& e) noexcept { COND_PRINT(debug_event, "DBTAdapter:mgmt:NewSettings: %s", e.toString().c_str()); const MgmtEvtNewSettings &event = *static_cast<const MgmtEvtNewSettings *>(&e); - const AdapterSetting new_settings = adapterInfo->setCurrentSettingMask(event.getSettings()); // probably done by mgmt callback already + const AdapterSetting new_settings = adapterInfo.setCurrentSettingMask(event.getSettings()); // probably done by mgmt callback already { const BTMode _btMode = getAdapterSettingsBTMode(new_settings); if( BTMode::NONE != _btMode ) { diff --git a/src/direct_bt/DBTDevice.cpp b/src/direct_bt/DBTDevice.cpp index 0f91acf4..b4a1d60e 100644 --- a/src/direct_bt/DBTDevice.cpp +++ b/src/direct_bt/DBTDevice.cpp @@ -39,6 +39,7 @@ #include "DBTDevice.hpp" #include "DBTAdapter.hpp" +#include "DBTManager.hpp" using namespace direct_bt; diff --git a/src/direct_bt/DBTManager.cpp b/src/direct_bt/DBTManager.cpp index bbfaab89..0d4136e9 100644 --- a/src/direct_bt/DBTManager.cpp +++ b/src/direct_bt/DBTManager.cpp @@ -44,6 +44,7 @@ #include "HCIIoctl.hpp" #include "HCIComm.hpp" #include "DBTTypes.hpp" +#include "DBTAdapter.hpp" #include "SMPHandler.hpp" @@ -239,7 +240,7 @@ std::unique_ptr<MgmtEvent> DBTManager::sendWithReply(MgmtCommand &req) noexcept return nullptr; } -std::shared_ptr<AdapterInfo> DBTManager::initAdapter(const uint16_t dev_id, const BTMode btMode) noexcept { +std::unique_ptr<AdapterInfo> DBTManager::initAdapter(const uint16_t dev_id, const BTMode btMode) noexcept { /** * We weight on PairingMode::PASSKEY_ENTRY. FIXME: Have it configurable! * @@ -253,7 +254,7 @@ std::shared_ptr<AdapterInfo> DBTManager::initAdapter(const uint16_t dev_id, cons const uint8_t sc_on_param = 0x01; // SET_SECURE_CONN 0x00 disabled, 0x01 enables SC mixed, 0x02 enables SC only mode #endif - std::shared_ptr<AdapterInfo> adapterInfo = nullptr; + std::unique_ptr<AdapterInfo> adapterInfo(nullptr); // nullptr AdapterSetting current_settings; MgmtCommand req0(MgmtCommand::Opcode::READ_INFO, dev_id); { @@ -352,7 +353,11 @@ fail: return adapterInfo; } -void DBTManager::shutdownAdapter(const uint16_t dev_id) noexcept { +void DBTManager::shutdownAdapter(DBTAdapter& adapter) noexcept { + DBG_PRINT("DBTManager::shutdownAdapter: %s", adapter.toString().c_str()); + const uint16_t dev_id = adapter.dev_id; + adapter.close(); // also issues removeMgmtEventCallback(dev_id); + AdapterSetting current_settings; setMode(dev_id, MgmtCommand::Opcode::SET_POWERED, 0, current_settings); @@ -364,6 +369,7 @@ void DBTManager::shutdownAdapter(const uint16_t dev_id) noexcept { setMode(dev_id, MgmtCommand::Opcode::SET_IO_CAPABILITY, direct_bt::number(SMPIOCapability::DISPLAY_ONLY), current_settings); setMode(dev_id, MgmtCommand::Opcode::SET_SSP, 0, current_settings); setMode(dev_id, MgmtCommand::Opcode::SET_SECURE_CONN, 0, current_settings); + DBG_PRINT("DBTManager::shutdownAdapter: done: %s", adapter.toString().c_str()); } DBTManager::DBTManager(const BTMode _defaultBTMode) noexcept @@ -476,19 +482,18 @@ next1: ERR_PRINT("Insufficient data for %d adapter indices: res %s", num_adapter, res->toString().c_str()); goto fail; } - { - for(int i=0; i < num_adapter; i++) { - const uint16_t dev_id = jau::get_uint16(data, 2+i*2, true /* littleEndian */); - std::shared_ptr<AdapterInfo> adapterInfo = initAdapter(dev_id, defaultBTMode); - if( nullptr != adapterInfo ) { - adapterInfos.push_back(adapterInfo); - adapterIOCapability.push_back(defaultIOCapability); - DBG_PRINT("DBTManager::adapters %d/%d: dev_id %d: %s", i, num_adapter, dev_id, adapterInfo->toString().c_str()); - } else { - DBG_PRINT("DBTManager::adapters %d/%d: dev_id %d: FAILED", i, num_adapter, dev_id); - } + for(int i=0; i < num_adapter; i++) { + const uint16_t dev_id = jau::get_uint16(data, 2+i*2, true /* littleEndian */); + std::unique_ptr<AdapterInfo> adapterInfo = initAdapter(dev_id, defaultBTMode); + if( nullptr != adapterInfo ) { + // private: std::shared_ptr<DBTAdapter> adapter = std::make_shared<DBTAdapter>(*this, *adapterInfo); + std::shared_ptr<DBTAdapter> adapter( new DBTAdapter(*this, *adapterInfo) ); + adapters.push_back( adapter ); + adapterIOCapability.push_back(defaultIOCapability); + DBG_PRINT("DBTManager::adapters %d/%d: dev_id %d: %s", i, num_adapter, dev_id, adapter->toString().c_str()); + } else { + DBG_PRINT("DBTManager::adapters %d/%d: dev_id %d: FAILED", i, num_adapter, dev_id); } - // Not required: CTOR: adapterInfos.set_store(std::move(snapshot)); } } @@ -544,7 +549,7 @@ void DBTManager::close() noexcept { DBG_PRINT("DBTManager::close: Not open"); whitelist.clear(); clearAllCallbacks(); - adapterInfos.clear(); + adapters.clear(); adapterIOCapability.clear(); comm.close(); return; @@ -556,10 +561,17 @@ void DBTManager::close() noexcept { removeAllDevicesFromWhitelist(); clearAllCallbacks(); - jau::for_each_const(adapterInfos, [&](const std::shared_ptr<AdapterInfo> & a) { - shutdownAdapter(a->dev_id); - }); - adapterInfos.clear(); + { + int i=0; + jau::for_each_fidelity(adapters, [&](std::shared_ptr<DBTAdapter> & a) { + DBG_PRINT("DBTManager::close::shutdownAdapter: %d/%d processing: %s", i, adapters.size(), a->toString().c_str()); + shutdownAdapter(*a); + ++i; + }); + } + + adapters.clear(); + adapterIOCapability.clear(); // Interrupt DBTManager's HCIComm::read(..), avoiding prolonged hang // and pull all underlying hci read operations! @@ -605,35 +617,27 @@ void DBTManager::close() noexcept { DBG_PRINT("DBTManager::close: End"); } -int DBTManager::findAdapterInfoIndex(const uint16_t dev_id) const noexcept { - auto it = adapterInfos.cbegin(); - for (; !it.is_end(); ++it) { - if ( (*it)->dev_id == dev_id ) { - return it - it.cend(); - } - } - return -1; -} -int DBTManager::findAdapterInfoDevId(const EUI48 &mac) const noexcept { - auto it = adapterInfos.cbegin(); +std::shared_ptr<DBTAdapter> DBTManager::getDefaultAdapter() const noexcept { + typename adapters_t::const_iterator it = adapters.cbegin(); for (; !it.is_end(); ++it) { - if ( (*it)->address == mac ) { - return (*it)->dev_id; + if( (*it)->isPowered() ) { + return *it; } } - return -1; + return nullptr; } -std::shared_ptr<AdapterInfo> DBTManager::findAdapterInfo(const EUI48 &mac) const noexcept { - auto it = adapterInfos.cbegin(); + +std::shared_ptr<DBTAdapter> DBTManager::getAdapter(const EUI48 &mac) const noexcept { + typename adapters_t::const_iterator it = adapters.cbegin(); for (; !it.is_end(); ++it) { - if ( (*it)->address == mac ) { + if ( (*it)->adapterInfo.address == mac ) { return *it; } } return nullptr; } -std::shared_ptr<AdapterInfo> DBTManager::getAdapterInfo(const uint16_t dev_id) const noexcept { - auto it = adapterInfos.cbegin(); +std::shared_ptr<DBTAdapter> DBTManager::getAdapter(const uint16_t dev_id) const noexcept { + typename adapters_t::const_iterator it = adapters.cbegin(); for (; !it.is_end(); ++it) { if ( (*it)->dev_id == dev_id ) { return *it; @@ -641,76 +645,80 @@ std::shared_ptr<AdapterInfo> DBTManager::getAdapterInfo(const uint16_t dev_id) c } return nullptr; } -bool DBTManager::addAdapterInfo(std::shared_ptr<AdapterInfo> ai) noexcept { - auto it = adapterInfos.begin(); // lock mutex and copy_store + +std::shared_ptr<DBTAdapter> DBTManager::addAdapter(const AdapterInfo& ai ) noexcept { + typename adapters_t::iterator it = adapters.begin(); // lock mutex and copy_store for (; !it.is_end(); ++it) { - if ( (*it)->dev_id == ai->dev_id ) { - it.push_back(ai); - it.write_back(); - adapterIOCapability.push_back(defaultIOCapability); - return true; + if ( (*it)->dev_id == ai.dev_id ) { + break; } } - // already existing - return false; + if( it.is_end() ) { + // new entry + // private: std::shared_ptr<DBTAdapter> adapter = std::make_shared<DBTAdapter>(*this, ai); + std::shared_ptr<DBTAdapter> adapter( new DBTAdapter(*this, ai) ); + it.push_back( adapter ); + adapterIOCapability.push_back(defaultIOCapability); + DBG_PRINT("DBTManager::addAdapter: Adding new: %s", adapter->toString().c_str()) + it.write_back(); + return adapter; + } else { + // already existing + std::shared_ptr<DBTAdapter> adapter = *it; + WARN_PRINT("DBTManager::addAdapter: Already existing %s, overwriting %s", ai.toString().c_str(), adapter->toString().c_str()) + adapter->adapterInfo = ai; + return adapter; + } } -std::shared_ptr<AdapterInfo> DBTManager::removeAdapterInfo(const uint16_t dev_id) noexcept { - typename adapterInfos_t::iterator it = adapterInfos.begin(); // lock mutex and copy_store - while ( !it.is_end() ) { - std::shared_ptr<AdapterInfo> & ai = *it; + +std::shared_ptr<DBTAdapter> DBTManager::removeAdapter(const uint16_t dev_id) noexcept { + typename adapters_t::iterator it = adapters.begin(); // lock mutex and copy_store + for(; !it.is_end(); ++it ) { + std::shared_ptr<DBTAdapter> & ai = *it; if( ai->dev_id == dev_id ) { - adapterIOCapability.erase( adapterIOCapability.begin() + ( it - it.begin() ) ); - std::shared_ptr<AdapterInfo> res = ai; // copy + adapterIOCapability.erase( adapterIOCapability.cbegin() + it.dist_begin() ); + std::shared_ptr<DBTAdapter> res = ai; // copy + DBG_PRINT("DBTManager::removeAdapter: Remove: %s", res->toString().c_str()) it.erase(); it.write_back(); return res; - } else { - ++it; } } + DBG_PRINT("DBTManager::removeAdapter: Not found: dev_id %d", dev_id) return nullptr; } -BTMode DBTManager::getCurrentBTMode(uint16_t dev_id) const noexcept { - std::shared_ptr<AdapterInfo> ai = getAdapterInfo(dev_id); - if( nullptr == ai ) { - ERR_PRINT("dev_id %d not found", dev_id); - return BTMode::NONE; - } - return ai->getCurrentBTMode(); -} - -std::shared_ptr<AdapterInfo> DBTManager::getDefaultAdapterInfo() const noexcept { - auto it = adapterInfos.cbegin(); - for (; !it.is_end(); ++it) { - if( (*it)->isCurrentSettingBitSet(AdapterSetting::POWERED) ) { - return *it; +bool DBTManager::removeAdapter(DBTAdapter* adapter) noexcept { + typename adapters_t::iterator it = adapters.begin(); // lock mutex and copy_store + for(; !it.is_end(); ++it ) { + std::shared_ptr<DBTAdapter> & ai = *it; + if( ai.get() == adapter ) { + adapterIOCapability.erase( adapterIOCapability.cbegin() + it.dist_begin() ); + DBG_PRINT("DBTManager::removeAdapter: Remove: %p -> %s", adapter, ai->toString().c_str()) + it.erase(); + it.write_back(); + return true; } } - return nullptr; -} - -int DBTManager::getDefaultAdapterDevID() const noexcept { - std::shared_ptr<AdapterInfo> ai = getDefaultAdapterInfo(); - if( nullptr == ai ) { - return -1; - } - return ai->dev_id; + DBG_PRINT("DBTManager::removeAdapter: Not found: %p", adapter) + return false; } bool DBTManager::setIOCapability(const uint16_t dev_id, const SMPIOCapability io_cap, SMPIOCapability& pre_io_cap) noexcept { if( SMPIOCapability::UNSET != io_cap ) { #if USE_LINUX_BT_SECURITY - auto it = adapterInfos.cbegin(); + typename adapters_t::const_iterator it = adapters.cbegin(); for (; !it.is_end(); ++it) { if( (*it)->dev_id == dev_id ) { - const typename adapterInfos_t::difference_type index = it-it.cbegin(); + const typename adapters_t::difference_type index = it.dist_begin(); const SMPIOCapability o = adapterIOCapability.at(index); AdapterSetting current_settings { AdapterSetting::NONE }; // throw away return value, unchanged on SET_IO_CAPABILITY if( setMode(dev_id, MgmtCommand::Opcode::SET_IO_CAPABILITY, direct_bt::number(io_cap), current_settings) ) { adapterIOCapability.at(index) = io_cap; pre_io_cap = o; return true; + } else { + return false; } } } @@ -720,10 +728,10 @@ bool DBTManager::setIOCapability(const uint16_t dev_id, const SMPIOCapability io } SMPIOCapability DBTManager::getIOCapability(const uint16_t dev_id) const noexcept { - auto it = adapterInfos.cbegin(); + typename adapters_t::const_iterator it = adapters.cbegin(); for (; !it.is_end(); ++it) { if( (*it)->dev_id == dev_id ) { - return adapterIOCapability.at(it-it.cbegin()); + return adapterIOCapability.at( it.dist_begin() ); } } return SMPIOCapability::UNSET; @@ -979,7 +987,7 @@ int DBTManager::removeAllDevicesFromWhitelist() noexcept { int count = 0; DBG_PRINT("DBTManager::removeAllDevicesFromWhitelist.B: Start %d elements", count); whitelist.clear(); - jau::for_each_const(adapterInfos, [&](const std::shared_ptr<AdapterInfo> & a) { + jau::for_each_const(adapters, [&](const std::shared_ptr<DBTAdapter> & a) { if( removeDeviceFromWhitelist(a->dev_id, BDAddressAndType::ANY_BREDR_DEVICE) ) { // flush whitelist! ++count; } @@ -1136,30 +1144,33 @@ void DBTManager::clearAllCallbacks() noexcept { void DBTManager::processAdapterAdded(std::unique_ptr<MgmtEvent> e) noexcept { const uint16_t dev_id = e->getDevID(); - std::shared_ptr<AdapterInfo> ai = initAdapter(dev_id, defaultBTMode); - if( nullptr != ai ) { - const bool added = addAdapterInfo(ai); - DBG_PRINT("DBTManager::Adapter[%d] Added: Start %s, added %d", dev_id, ai->toString().c_str(), added); + + std::unique_ptr<AdapterInfo> adapterInfo = initAdapter(dev_id, defaultBTMode); + + if( nullptr != adapterInfo ) { + std::shared_ptr<DBTAdapter> adapter = addAdapter( *adapterInfo ); + DBG_PRINT("DBTManager::Adapter[%d] Added: Start %s, added %d", dev_id, adapter->toString().c_str()); sendMgmtEvent(*e); - DBG_PRINT("DBTManager::Adapter[%d] Added: User_ %s", dev_id, ai->toString().c_str()); + DBG_PRINT("DBTManager::Adapter[%d] Added: User_ %s", dev_id, adapter->toString().c_str()); jau::for_each_fidelity(mgmtChangedAdapterSetCallbackList, [&](ChangedAdapterSetCallback &cb) { - cb.invoke(true /* added */, *ai); + cb.invoke(true /* added */, adapter); }); - DBG_PRINT("DBTManager::Adapter[%d] Added: End__ %s", dev_id, ai->toString().c_str()); + DBG_PRINT("DBTManager::Adapter[%d] Added: End__ %s", dev_id, adapter->toString().c_str()); } else { DBG_PRINT("DBTManager::Adapter[%d] Added: InitAI failed", dev_id); } } void DBTManager::processAdapterRemoved(std::unique_ptr<MgmtEvent> e) noexcept { const uint16_t dev_id = e->getDevID(); - std::shared_ptr<AdapterInfo> ai = removeAdapterInfo(dev_id); + std::shared_ptr<DBTAdapter> ai = removeAdapter(dev_id); if( nullptr != ai ) { DBG_PRINT("DBTManager::Adapter[%d] Removed: Start: %s", dev_id, ai->toString().c_str()); sendMgmtEvent(*e); DBG_PRINT("DBTManager::Adapter[%d] Removed: User_: %s", dev_id, ai->toString().c_str()); jau::for_each_fidelity(mgmtChangedAdapterSetCallbackList, [&](ChangedAdapterSetCallback &cb) { - cb.invoke(false /* added */, *ai); + cb.invoke(false /* added */, ai); }); + ai->close(); // issuing dtor on DBTAdapter DBG_PRINT("DBTManager::Adapter[%d] Removed: End__: %s", dev_id, ai->toString().c_str()); } else { DBG_PRINT("DBTManager::Adapter[%d] Removed: RemoveAI failed", dev_id); @@ -1167,10 +1178,10 @@ void DBTManager::processAdapterRemoved(std::unique_ptr<MgmtEvent> e) noexcept { } bool DBTManager::mgmtEvNewSettingsCB(const MgmtEvent& e) noexcept { const MgmtEvtNewSettings &event = *static_cast<const MgmtEvtNewSettings *>(&e); - std::shared_ptr<AdapterInfo> adapterInfo = getAdapterInfo(event.getDevID()); - if( nullptr != adapterInfo ) { - const AdapterSetting old_settings = adapterInfo->getCurrentSettingMask(); - const AdapterSetting new_settings = adapterInfo->setCurrentSettingMask(event.getSettings()); + std::shared_ptr<DBTAdapter> adapter = getAdapter(event.getDevID()); + if( nullptr != adapter ) { + const AdapterSetting old_settings = adapter->adapterInfo.getCurrentSettingMask(); + const AdapterSetting new_settings = adapter->adapterInfo.setCurrentSettingMask(event.getSettings()); DBG_PRINT("DBTManager:mgmt:NewSettings: Adapter[%d] %s -> %s - %s", event.getDevID(), getAdapterSettingMaskString(old_settings).c_str(), @@ -1200,12 +1211,11 @@ static ChangedAdapterSetCallbackList::equal_comparator _changedAdapterSetCallbac void DBTManager::addChangedAdapterSetCallback(const ChangedAdapterSetCallback & l) { + ChangedAdapterSetCallback* l_p = const_cast<ChangedAdapterSetCallback*>(&l); mgmtChangedAdapterSetCallbackList.push_back(l); - jau::for_each_const(adapterInfos, [&](const std::shared_ptr<AdapterInfo>& ai) { - jau::for_each_fidelity(mgmtChangedAdapterSetCallbackList, [&](ChangedAdapterSetCallback &cb) { - cb.invoke(true /* added */, *ai); - }); + jau::for_each_fidelity(adapters, [&](std::shared_ptr<DBTAdapter>& ai) { + l_p->invoke(true /* added */, ai); }); } int DBTManager::removeChangedAdapterSetCallback(const ChangedAdapterSetCallback & l) { @@ -1215,10 +1225,10 @@ int DBTManager::removeChangedAdapterSetCallback(const ChangedAdapterSetCallback void DBTManager::addChangedAdapterSetCallback(ChangedAdapterSetFunc f) { addChangedAdapterSetCallback( ChangedAdapterSetCallback( - jau::bindPlainFunc<bool, bool, const AdapterInfo&>(f) + jau::bindPlainFunc<bool, bool, std::shared_ptr<DBTAdapter>&>(f) ) ); } int DBTManager::removeChangedAdapterSetCallback(ChangedAdapterSetFunc f) { - ChangedAdapterSetCallback l( jau::bindPlainFunc<bool, bool, const AdapterInfo&>(f) ); + ChangedAdapterSetCallback l( jau::bindPlainFunc<bool, bool, std::shared_ptr<DBTAdapter>&>(f) ); return mgmtChangedAdapterSetCallbackList.erase_matching(l, true /* all_matching */, _changedAdapterSetCallbackEqComp); } diff --git a/src/direct_bt/GATTHandler.cpp b/src/direct_bt/GATTHandler.cpp index 374176f3..9ffbad42 100644 --- a/src/direct_bt/GATTHandler.cpp +++ b/src/direct_bt/GATTHandler.cpp @@ -56,6 +56,7 @@ extern "C" { #include "DBTManager.hpp" #include "DBTAdapter.hpp" +#include "DBTManager.hpp" using namespace direct_bt; diff --git a/src/direct_bt/MgmtTypes.cpp b/src/direct_bt/MgmtTypes.cpp index a8f88535..dcd85839 100644 --- a/src/direct_bt/MgmtTypes.cpp +++ b/src/direct_bt/MgmtTypes.cpp @@ -483,8 +483,8 @@ std::shared_ptr<NameAndShortName> MgmtEvtLocalNameChanged::toNameAndShortName() return std::make_shared<NameAndShortName>(getName(), getShortName()); } -std::shared_ptr<AdapterInfo> MgmtEvtAdapterInfo::toAdapterInfo() const noexcept { - return std::make_shared<AdapterInfo>( +std::unique_ptr<AdapterInfo> MgmtEvtAdapterInfo::toAdapterInfo() const noexcept { + return std::make_unique<AdapterInfo>( getDevID(), getAddress(), getVersion(), getManufacturer(), getSupportedSetting(), getCurrentSetting(), getDevClass(), |