From a345a16add06352937d815af936b6e0fab2f9efe Mon Sep 17 00:00:00 2001 From: Arkadiusz Balys Date: Thu, 21 Sep 2023 18:27:48 +0200 Subject: [PATCH] Added mechanism for leaving the last fabric to the Lock example. For testing... --- config/nrfconnect/chip-module/Kconfig | 16 ++++++++++ .../nrfconnect/chip-module/Kconfig.defaults | 2 +- examples/lock-app/nrfconnect/main/AppTask.cpp | 31 +++++++++++++++++++ .../nrfconnect/main/include/AppEvent.h | 1 + .../nrfconnect/main/include/AppTask.h | 1 + src/include/platform/CHIPDeviceEvent.h | 6 ++++ ...nericThreadStackManagerImpl_OpenThread.hpp | 8 +++++ 7 files changed, 64 insertions(+), 1 deletion(-) diff --git a/config/nrfconnect/chip-module/Kconfig b/config/nrfconnect/chip-module/Kconfig index 95e49ac59e0262..17bbe8f8510abf 100644 --- a/config/nrfconnect/chip-module/Kconfig +++ b/config/nrfconnect/chip-module/Kconfig @@ -61,6 +61,22 @@ config CHIP_OTA_REQUESTOR imply STREAM_FLASH imply STREAM_FLASH_ERASE +config CHIP_LAST_FABRIC_REMOVED_REBOOT + bool "Reboot when the last fabric is removed" + default y + help + When the last fabric is removed this config allows to reboot the device + automatically. + +config CHIP_LAST_FABRIC_REMOVED_PAIRING_AUTOSTART + bool "Turn on Bluetooth LE advertising when the last fabric is removed" + default y if !CHIP_LAST_FABRIC_REMOVED_REBOOT + depends on !CHIP_LAST_FABRIC_REMOVED_REBOOT + help + After removing of the last fabric the device will start Bluetooth LE + advertising automatically if the reboot has not been requested by + CHIP_LAST_FABRIC_REMOVED_REBOOT config. + config CHIP_OTA_REQUESTOR_BUFFER_SIZE int "OTA Requestor image buffer size" default 1024 diff --git a/config/nrfconnect/chip-module/Kconfig.defaults b/config/nrfconnect/chip-module/Kconfig.defaults index d275d938021c63..30611686b1e41a 100644 --- a/config/nrfconnect/chip-module/Kconfig.defaults +++ b/config/nrfconnect/chip-module/Kconfig.defaults @@ -194,7 +194,7 @@ endif # BOARD_NRF7002DK_NRF5340_CPUAPP # Enable extended discovery config CHIP_EXTENDED_DISCOVERY - default y + default n config NVS_LOOKUP_CACHE default y diff --git a/examples/lock-app/nrfconnect/main/AppTask.cpp b/examples/lock-app/nrfconnect/main/AppTask.cpp index 3ef85657363043..0785a057e93dbb 100644 --- a/examples/lock-app/nrfconnect/main/AppTask.cpp +++ b/examples/lock-app/nrfconnect/main/AppTask.cpp @@ -469,6 +469,27 @@ void AppTask::StartBLEAdvertisementHandler(const AppEvent &) } } +void AppTask::LastFabricRemovedHandler(const AppEvent & event) +{ + sHaveBLEConnections = false; + +#ifdef CONFIG_CHIP_LAST_FABRIC_REMOVED_REBOOT + chip::Server::GetInstance().ScheduleFactoryReset(); +#else + /* Erase Matter data */ + PersistedStorage::KeyValueStoreMgrImpl().DoFactoryReset(); + /* Erase Thread credentials and disconnect */ + ConnectivityMgr().ErasePersistentInfo(); +#ifdef CONFIG_CHIP_LAST_FABRIC_REMOVED_PAIRING_AUTOSTART + /* Start the New BLE advertising */ + StartBLEAdvertisementHandler(event); +#endif +#endif + + /* Update the LED status */ + UpdateStatusLED(); +} + void AppTask::UpdateLedStateEventHandler(const AppEvent & event) { if (event.Type == AppEventType::UpdateLedState) @@ -543,6 +564,16 @@ void AppTask::ChipEventHandler(const ChipDeviceEvent * event, intptr_t /* arg */ InitBasicOTARequestor(); #endif // CONFIG_CHIP_OTA_REQUESTOR break; + case DeviceEventType::kThreadSrpUpdateEvent: + if (Server::GetInstance().GetFabricTable().FabricCount() == 0 && sIsNetworkProvisioned && sIsNetworkEnabled) + { + LOG_INF("\n\nLast fabric EVENT\n\n"); + AppEvent event; + event.Type = AppEventType::LastFabricRemove; + event.Handler = LastFabricRemovedHandler; + PostEvent(event); + } + break; case DeviceEventType::kThreadStateChange: sIsNetworkProvisioned = ConnectivityMgr().IsThreadProvisioned(); sIsNetworkEnabled = ConnectivityMgr().IsThreadEnabled(); diff --git a/examples/lock-app/nrfconnect/main/include/AppEvent.h b/examples/lock-app/nrfconnect/main/include/AppEvent.h index 3189e78d848deb..467674a17fc3dc 100644 --- a/examples/lock-app/nrfconnect/main/include/AppEvent.h +++ b/examples/lock-app/nrfconnect/main/include/AppEvent.h @@ -34,6 +34,7 @@ enum class AppEventType : uint8_t UpdateLedState, IdentifyStart, IdentifyStop, + LastFabricRemove }; enum class FunctionEvent : uint8_t diff --git a/examples/lock-app/nrfconnect/main/include/AppTask.h b/examples/lock-app/nrfconnect/main/include/AppTask.h index a0a50da9ed23db..5164a0785587f5 100644 --- a/examples/lock-app/nrfconnect/main/include/AppTask.h +++ b/examples/lock-app/nrfconnect/main/include/AppTask.h @@ -69,6 +69,7 @@ class AppTask static void LockActionEventHandler(const AppEvent & event); static void StartBLEAdvertisementHandler(const AppEvent & event); static void UpdateLedStateEventHandler(const AppEvent & event); + static void LastFabricRemovedHandler(const AppEvent & event); static void ChipEventHandler(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg); static void ButtonEventHandler(uint32_t buttonState, uint32_t hasChanged); diff --git a/src/include/platform/CHIPDeviceEvent.h b/src/include/platform/CHIPDeviceEvent.h index 47573ba348fd78..ba55c57e4a8bcc 100644 --- a/src/include/platform/CHIPDeviceEvent.h +++ b/src/include/platform/CHIPDeviceEvent.h @@ -288,6 +288,12 @@ enum PublicEventTypes * An application event occured that should wake up the system/device */ kAppWakeUpEvent, + + /** + * Signals that current session has been ended + * + */ + kThreadSrpUpdateEvent, }; /** diff --git a/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.hpp b/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.hpp index 4e89e6938520b4..8a145a61b9b2ec 100644 --- a/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.hpp +++ b/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.hpp @@ -1951,6 +1951,14 @@ void GenericThreadStackManagerImpl_OpenThread::OnSrpClientNotificatio otService = next; } while (otService); } + + DeviceLayer::ChipDeviceEvent event; + event.Type = DeviceLayer::DeviceEventType::PublicEventTypes::kThreadSrpUpdateEvent; + CHIP_ERROR status = DeviceLayer::PlatformMgr().PostEvent(&event); + if (status != CHIP_NO_ERROR) + { + ChipLogError(DeviceLayer, "Failed to post SRP update event: %" CHIP_ERROR_FORMAT, status.Format()); + } break; } case OT_ERROR_PARSE: