diff --git a/src/platform/OpenThread/GenericNetworkCommissioningThreadDriver.cpp b/src/platform/OpenThread/GenericNetworkCommissioningThreadDriver.cpp index 0859c3f723d5d5..0a961a9f8e4afa 100644 --- a/src/platform/OpenThread/GenericNetworkCommissioningThreadDriver.cpp +++ b/src/platform/OpenThread/GenericNetworkCommissioningThreadDriver.cpp @@ -47,6 +47,7 @@ CHIP_ERROR GenericThreadDriver::Init(Internal::BaseDriver::NetworkStatusChangeCa { ThreadStackMgrImpl().SetNetworkStatusChangeCallback(statusChangeCallback); ThreadStackMgrImpl().GetThreadProvision(mStagingNetwork); + ReturnErrorOnFailure(PlatformMgr().AddEventHandler(OnThreadStateChangeHandler, reinterpret_cast(this))); // If the network configuration backup exists, it means that the device has been rebooted with // the fail-safe armed. Since OpenThread persists all operational dataset changes, the backup @@ -56,6 +57,16 @@ CHIP_ERROR GenericThreadDriver::Init(Internal::BaseDriver::NetworkStatusChangeCa return CHIP_NO_ERROR; } +void GenericThreadDriver::OnThreadStateChangeHandler(const ChipDeviceEvent * event, intptr_t arg) +{ + if ((event->Type == DeviceEventType::kThreadStateChange) && + (event->ThreadStateChange.OpenThread.Flags & OT_CHANGED_THREAD_PANID)) + { + // Update the mStagingNetwork when thread panid changed + ThreadStackMgrImpl().GetThreadProvision(reinterpret_cast(arg)->mStagingNetwork); + } +} + void GenericThreadDriver::Shutdown() { ThreadStackMgrImpl().SetNetworkStatusChangeCallback(nullptr); diff --git a/src/platform/OpenThread/GenericNetworkCommissioningThreadDriver.h b/src/platform/OpenThread/GenericNetworkCommissioningThreadDriver.h index bbfec6a44ff879..28dc8f176ddd1f 100644 --- a/src/platform/OpenThread/GenericNetworkCommissioningThreadDriver.h +++ b/src/platform/OpenThread/GenericNetworkCommissioningThreadDriver.h @@ -116,6 +116,7 @@ class GenericThreadDriver final : public ThreadDriver private: uint8_t scanNetworkTimeoutSeconds; uint8_t connectNetworkTimeout; + static void OnThreadStateChangeHandler(const ChipDeviceEvent * event, intptr_t arg); Status MatchesNetworkId(const Thread::OperationalDataset & dataset, const ByteSpan & networkId) const; CHIP_ERROR BackupConfiguration();