From 1400618d20bdeea5603e06eb975139a71440d42a Mon Sep 17 00:00:00 2001 From: Alex Tsitsiura Date: Thu, 1 Jun 2023 18:23:27 +0300 Subject: [PATCH] [Telink]: Shut down BLE after commissioning window closes (#26786) * [Telink]: Added delegate to shut down BLE after commissioning window timed out Signed-off-by: Misha Tkachenko * Restyled by clang-format * [Telink]: Fixed wrong behavior on Commissioning window close Signed-off-by: Misha Tkachenko * Restyled by whitespace * Restyled by clang-format --------- Signed-off-by: Misha Tkachenko Co-authored-by: Misha Tkachenko Co-authored-by: Restyled.io Co-authored-by: Misha Tkachenko <36621698+mishadesh@users.noreply.github.com> --- .../telink/common/src/AppTaskCommon.cpp | 18 ++++++++++++++++++ src/platform/telink/BLEManagerImpl.cpp | 6 ++++++ src/platform/telink/BLEManagerImpl.h | 2 +- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/examples/platform/telink/common/src/AppTaskCommon.cpp b/examples/platform/telink/common/src/AppTaskCommon.cpp index 6f9aae5f4ca5d6..bb97871883187e 100644 --- a/examples/platform/telink/common/src/AppTaskCommon.cpp +++ b/examples/platform/telink/common/src/AppTaskCommon.cpp @@ -19,6 +19,7 @@ #include "AppTaskCommon.h" #include "AppTask.h" +#include "BLEManagerImpl.h" #include "ButtonManager.h" #include "ThreadUtil.h" @@ -131,6 +132,22 @@ Identify sIdentify = { uint8_t sTestEventTriggerEnableKey[TestEventTriggerDelegate::kEnableKeyLength] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff }; #endif + +class AppCallbacks : public AppDelegate +{ + bool isComissioningStarted; + +public: + void OnCommissioningSessionStarted() override { isComissioningStarted = true; } + void OnCommissioningSessionStopped() override { isComissioningStarted = false; } + void OnCommissioningWindowClosed() override + { + if (!isComissioningStarted) + chip::DeviceLayer::Internal::BLEMgr().Shutdown(); + } +}; + +AppCallbacks sCallbacks; } // namespace class AppFabricTableDelegate : public FabricTable::Delegate @@ -276,6 +293,7 @@ CHIP_ERROR AppTaskCommon::InitCommonParts(void) // Init ZCL Data Model and start server static CommonCaseDeviceServerInitParams initParams; (void) initParams.InitializeStaticResourcesBeforeServerInit(); + initParams.appDelegate = &sCallbacks; ReturnErrorOnFailure(chip::Server::GetInstance().Init(initParams)); #if APP_SET_DEVICE_INFO_PROVIDER diff --git a/src/platform/telink/BLEManagerImpl.cpp b/src/platform/telink/BLEManagerImpl.cpp index dd912e5e581b3c..0d488c5c917c31 100644 --- a/src/platform/telink/BLEManagerImpl.cpp +++ b/src/platform/telink/BLEManagerImpl.cpp @@ -176,6 +176,12 @@ CHIP_ERROR BLEManagerImpl::_Init(void) return CHIP_NO_ERROR; } +void BLEManagerImpl::_Shutdown() +{ + bt_disable(); + mBLERadioInitialized = false; +} + void BLEManagerImpl::DriveBLEState(intptr_t arg) { BLEMgrImpl().DriveBLEState(); diff --git a/src/platform/telink/BLEManagerImpl.h b/src/platform/telink/BLEManagerImpl.h index a2fd31fc6d8a5a..1e1c4c94d67638 100644 --- a/src/platform/telink/BLEManagerImpl.h +++ b/src/platform/telink/BLEManagerImpl.h @@ -54,7 +54,7 @@ class BLEManagerImpl final : public BLEManager, private BleLayer, private BlePla // ===== Members that implement the BLEManager internal interface. CHIP_ERROR _Init(void); - void _Shutdown() {} + void _Shutdown(); bool _IsAdvertisingEnabled(void); CHIP_ERROR _SetAdvertisingEnabled(bool val); bool _IsAdvertising(void);