From 104d93a5c8210e49faf1647a298cd042d1d00d8f Mon Sep 17 00:00:00 2001 From: shchen-Lab Date: Tue, 8 Aug 2023 18:39:53 +0800 Subject: [PATCH 1/2] Add BLE ManagerImpl ShutDown Function --- src/platform/bouffalolab/common/BLEManagerImpl.cpp | 8 +++++--- src/platform/bouffalolab/common/BLEManagerImpl.h | 2 +- .../bouffalolab/common/ConfigurationManagerImpl.cpp | 13 +++++++++++-- .../bouffalolab/common/ConfigurationManagerImpl.h | 2 +- 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/platform/bouffalolab/common/BLEManagerImpl.cpp b/src/platform/bouffalolab/common/BLEManagerImpl.cpp index 89199523acec0e..b9e9565d17e800 100644 --- a/src/platform/bouffalolab/common/BLEManagerImpl.cpp +++ b/src/platform/bouffalolab/common/BLEManagerImpl.cpp @@ -146,6 +146,10 @@ CHIP_ERROR BLEManagerImpl::_Init() return CHIP_NO_ERROR; } +void BLEManagerImpl::_Shutdown() +{ + bt_disable(); +} void BLEManagerImpl::DriveBLEState(intptr_t arg) { BLEMgrImpl().DriveBLEState(); @@ -435,7 +439,7 @@ CHIP_ERROR BLEManagerImpl::HandleGAPDisconnect(const ChipDeviceEvent * event) { case BT_HCI_ERR_REMOTE_USER_TERM_CONN: // Do not treat proper connection termination as an error and exit. - VerifyOrExit(!ConfigurationMgr().IsFullyProvisioned(), ); + VerifyOrExit(!ConfigurationMgr().IsFullyProvisioned(),BLEMgrImpl()._Shutdown()); disconReason = BLE_ERROR_REMOTE_DEVICE_DISCONNECTED; break; case BT_HCI_ERR_LOCALHOST_TERM_CONN: @@ -457,7 +461,6 @@ CHIP_ERROR BLEManagerImpl::HandleGAPDisconnect(const ChipDeviceEvent * event) ChipDeviceEvent disconnectEvent; disconnectEvent.Type = DeviceEventType::kCHIPoBLEConnectionClosed; ReturnErrorOnFailure(PlatformMgr().PostEvent(&disconnectEvent)); - // Force a reconfiguration of advertising in case we switched to non-connectable mode when // the BLE connection was established. mFlags.Set(Flags::kAdvertisingRefreshNeeded); @@ -592,7 +595,6 @@ void BLEManagerImpl::_OnPlatformEvent(const ChipDeviceEvent * event) case DeviceEventType::kPlatformZephyrBleC2IndDoneEvent: err = HandleTXCharComplete(event); break; - default: break; } diff --git a/src/platform/bouffalolab/common/BLEManagerImpl.h b/src/platform/bouffalolab/common/BLEManagerImpl.h index 1589d88006a19d..54dfe7fb081928 100644 --- a/src/platform/bouffalolab/common/BLEManagerImpl.h +++ b/src/platform/bouffalolab/common/BLEManagerImpl.h @@ -44,7 +44,7 @@ class BLEManagerImpl final : public BLEManager, private BleLayer, private BlePla std::conditional_t::value, bt_gatt_indicate_params *, const bt_gatt_attr *>; CHIP_ERROR _Init(void); - void _Shutdown() {} + void _Shutdown(); bool _IsAdvertisingEnabled(void); CHIP_ERROR _SetAdvertisingEnabled(bool val); bool _IsAdvertising(void); diff --git a/src/platform/bouffalolab/common/ConfigurationManagerImpl.cpp b/src/platform/bouffalolab/common/ConfigurationManagerImpl.cpp index 7068606274192d..8010d4110040ef 100644 --- a/src/platform/bouffalolab/common/ConfigurationManagerImpl.cpp +++ b/src/platform/bouffalolab/common/ConfigurationManagerImpl.cpp @@ -32,7 +32,17 @@ ConfigurationManagerImpl & ConfigurationManagerImpl::GetDefaultInstance() static ConfigurationManagerImpl sInstance; return sInstance; } - +bool ConfigurationManagerImpl::IsFullyProvisioned() +{ + return +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI_STATION + ConnectivityMgr().IsWiFiStationProvisioned() && +#endif +#if CHIP_DEVICE_CONFIG_ENABLE_THREAD + ConnectivityMgr().IsThreadProvisioned() && +#endif + true; +} CHIP_ERROR ConfigurationManagerImpl::Init() { CHIP_ERROR err; @@ -198,7 +208,6 @@ void ConfigurationManagerImpl::DoFactoryReset(intptr_t arg) hal_reboot(); } - ConfigurationManager & ConfigurationMgrImpl() { return ConfigurationManagerImpl::GetDefaultInstance(); diff --git a/src/platform/bouffalolab/common/ConfigurationManagerImpl.h b/src/platform/bouffalolab/common/ConfigurationManagerImpl.h index 5c3c3eff8272af..9205419e14c2ca 100644 --- a/src/platform/bouffalolab/common/ConfigurationManagerImpl.h +++ b/src/platform/bouffalolab/common/ConfigurationManagerImpl.h @@ -36,7 +36,7 @@ class ConfigurationManagerImpl : public Internal::GenericConfigurationManagerImp CHIP_ERROR StoreRebootCount(uint32_t rebootCount); CHIP_ERROR GetTotalOperationalHours(uint32_t & totalOperationalHours); CHIP_ERROR StoreTotalOperationalHours(uint32_t totalOperationalHours); - + bool IsFullyProvisioned(); private: // ===== Members that implement the ConfigurationManager private interface. From b465eaff488be7596fb2bfd78dc7129be4f18a5f Mon Sep 17 00:00:00 2001 From: shchen-Lab Date: Wed, 9 Aug 2023 15:39:27 +0800 Subject: [PATCH 2/2] Refactor BLE ManagerImpl ShutDown Function --- .../bouffalolab/common/BLEManagerImpl.cpp | 19 ++++++++++++++++++- .../bouffalolab/common/BLEManagerImpl.h | 1 + 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/platform/bouffalolab/common/BLEManagerImpl.cpp b/src/platform/bouffalolab/common/BLEManagerImpl.cpp index b9e9565d17e800..ac31a3eb2aee30 100644 --- a/src/platform/bouffalolab/common/BLEManagerImpl.cpp +++ b/src/platform/bouffalolab/common/BLEManagerImpl.cpp @@ -148,8 +148,11 @@ CHIP_ERROR BLEManagerImpl::_Init() void BLEManagerImpl::_Shutdown() { - bt_disable(); + // Release BLE Stack resources + mFlags.Set(Flags::kChipoBleShutDown); + } + void BLEManagerImpl::DriveBLEState(intptr_t arg) { BLEMgrImpl().DriveBLEState(); @@ -461,6 +464,20 @@ CHIP_ERROR BLEManagerImpl::HandleGAPDisconnect(const ChipDeviceEvent * event) ChipDeviceEvent disconnectEvent; disconnectEvent.Type = DeviceEventType::kCHIPoBLEConnectionClosed; ReturnErrorOnFailure(PlatformMgr().PostEvent(&disconnectEvent)); + + if (mFlags.Has(Flags::kChipoBleShutDown)) + { + int ret = bt_disable(); + if(ret) + { + ChipLogError(DeviceLayer, "CHIPoBLE Shutdown faild =%d",ret); + } + else + { + mFlags.Clear(Flags::kChipoBleShutDown); + } + return CHIP_NO_ERROR; + } // Force a reconfiguration of advertising in case we switched to non-connectable mode when // the BLE connection was established. mFlags.Set(Flags::kAdvertisingRefreshNeeded); diff --git a/src/platform/bouffalolab/common/BLEManagerImpl.h b/src/platform/bouffalolab/common/BLEManagerImpl.h index 54dfe7fb081928..561513e9067266 100644 --- a/src/platform/bouffalolab/common/BLEManagerImpl.h +++ b/src/platform/bouffalolab/common/BLEManagerImpl.h @@ -85,6 +85,7 @@ class BLEManagerImpl final : public BLEManager, private BleLayer, private BlePla kAdvertisingRefreshNeeded = 0x0010, /**< The advertising state/configuration has changed, but the SoftDevice has yet to be updated. */ kChipoBleGattServiceRegister = 0x0020, /**< The system has currently CHIPoBLE GATT service registered. */ + kChipoBleShutDown= 0x0040, /**< The system has disable ble stack. */ }; struct ServiceData;