From 0ff1dac23391bd3e5cbe4e9af16d361c81263723 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Sun, 22 Oct 2023 05:37:59 +0200 Subject: BTAdapter/BTManager: Initialize BTAdapter w/ optional power-on, allowing subsequent power-off settings w/o toggling power again --- src/direct_bt/BTAdapter.cpp | 22 ++++++++-------------- src/direct_bt/BTManager.cpp | 9 +++++---- 2 files changed, 13 insertions(+), 18 deletions(-) (limited to 'src/direct_bt') diff --git a/src/direct_bt/BTAdapter.cpp b/src/direct_bt/BTAdapter.cpp index 6ba8f67c..5dc5670d 100644 --- a/src/direct_bt/BTAdapter.cpp +++ b/src/direct_bt/BTAdapter.cpp @@ -376,7 +376,7 @@ BTAdapter::BTAdapter(const BTAdapter::ctor_cookie& cc, BTManagerRef mgmt_, Adapt debug_lock(jau::environment::getBooleanProperty("direct_bt.debug.adapter.lock", false)), mgmt( std::move(mgmt_) ), adapterInfo( std::move(adapterInfo_) ), - adapter_initialized( false ), adapter_poweredon_at_init( false ), + adapter_initialized( false ), adapter_poweredoff_at_init( true ), le_features( LE_Features::NONE ), hci_uses_ext_scan( false ), hci_uses_ext_conn( false ), hci_uses_ext_adv( false ), visibleAddressAndType( adapterInfo_.addressAndType ), @@ -444,11 +444,8 @@ void BTAdapter::close() noexcept { poweredOff(true /* active */, "close"); - if( adapter_poweredon_at_init ) { - adapter_poweredon_at_init = false; - if( isPowered() ) { - setPowered(false); - } + if( adapter_poweredoff_at_init && isPowered() ) { + setPowered(false); } DBG_PRINT("BTAdapter::close: close[HCI, l2cap_srv]: ..."); @@ -711,12 +708,13 @@ HCIStatusCode BTAdapter::uploadKeys(SMPKeyBin& bin, const bool write) noexcept { return HCIStatusCode::SUCCESS; } -HCIStatusCode BTAdapter::initialize(const BTMode btMode) noexcept { +HCIStatusCode BTAdapter::initialize(const BTMode btMode, const bool powerOn) noexcept { const bool was_powered = adapterInfo.isCurrentSettingBitSet(AdapterSetting::POWERED); adapter_initialized = true; + adapter_poweredoff_at_init = was_powered; // Also fails if unable to power-on and not powered-on! - HCIStatusCode status = mgmt->initializeAdapter(adapterInfo, dev_id, BTRole::None /* unused */, btMode); + HCIStatusCode status = mgmt->initializeAdapter(adapterInfo, dev_id, btMode, powerOn); if( HCIStatusCode::SUCCESS != status ) { WARN_PRINT("Adapter[%d]: Failed initializing (1): res0 %s, powered[before %d, now %d], %s - %s", dev_id, to_string(status).c_str(), @@ -725,17 +723,13 @@ HCIStatusCode BTAdapter::initialize(const BTMode btMode) noexcept { return status; } const bool is_powered = adapterInfo.isCurrentSettingBitSet(AdapterSetting::POWERED); - if( !was_powered ) { - adapter_poweredon_at_init = true; - } if( !enableListening(true) ) { return HCIStatusCode::INTERNAL_FAILURE; } updateAdapterSettings(false /* off_thread */, adapterInfo.getCurrentSettingMask(), false /* sendEvent */, 0); - WORDY_PRINT("BTAdapter::initialize: Adapter[%d]: OK: powered[before %d, init_on %d, now %d], %s", - dev_id, was_powered, adapter_poweredon_at_init.load(), is_powered, - toString().c_str()); + WORDY_PRINT("BTAdapter::initialize: Adapter[%d]: OK: powered[%d -> %d], %s", + dev_id, was_powered, is_powered, toString().c_str()); return HCIStatusCode::SUCCESS; } diff --git a/src/direct_bt/BTManager.cpp b/src/direct_bt/BTManager.cpp index 7db3b425..943dd801 100644 --- a/src/direct_bt/BTManager.cpp +++ b/src/direct_bt/BTManager.cpp @@ -223,7 +223,7 @@ fail: } HCIStatusCode BTManager::initializeAdapter(AdapterInfo& adapterInfo, const uint16_t dev_id, - const BTRole btRole, const BTMode btMode) noexcept { + const BTMode btMode, const bool powerOn) noexcept { /** * We set BTManager::defaultIOCapability, i.e. SMPIOCapability::NO_INPUT_NO_OUTPUT, * which may be overridden for each connection by BTDevice/BTAdapter! @@ -307,7 +307,6 @@ HCIStatusCode BTManager::initializeAdapter(AdapterInfo& adapterInfo, const uint1 setMode(dev_id, MgmtCommand::Opcode::SET_CONNECTABLE, 0, current_settings); } #else - (void)btRole; setMode(dev_id, MgmtCommand::Opcode::SET_CONNECTABLE, 0, current_settings); // '1' not required for BTRole::Slave #endif setMode(dev_id, MgmtCommand::Opcode::SET_FAST_CONNECTABLE, 0, current_settings); @@ -330,7 +329,9 @@ HCIStatusCode BTManager::initializeAdapter(AdapterInfo& adapterInfo, const uint1 } } - setMode(dev_id, MgmtCommand::Opcode::SET_POWERED, 1, current_settings); + if( powerOn ) { + setMode(dev_id, MgmtCommand::Opcode::SET_POWERED, 1, current_settings); + } /** * Update AdapterSettings post settings @@ -352,7 +353,7 @@ HCIStatusCode BTManager::initializeAdapter(AdapterInfo& adapterInfo, const uint1 ABORT("initializeAdapter dev_id=%d != dev_id=%d: %s", adapterInfo.dev_id, dev_id, adapterInfo.toString().c_str()); } } - if( !adapterInfo.isCurrentSettingBitSet(AdapterSetting::POWERED) ) { + if( powerOn && !adapterInfo.isCurrentSettingBitSet(AdapterSetting::POWERED) ) { ERR_PRINT("initializeAdapter[%d, BTMode %s]: Fail: Couldn't power-on: %s", dev_id, to_string(btMode).c_str(), adapterInfo.toString().c_str()); goto fail; -- cgit v1.2.3