From f73b6b93373b7f590a17850f383e357bd85bd84b Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Mon, 12 Aug 2019 09:03:04 -0700 Subject: [PATCH] [mle] save network info on mle mode change (#4078) This commit ensures to save the network info in non-volatile settings when the MLE device mode is changed (`Mle::SetDevcieMode()`) independent of whether the device is currently attached or not. This helps address the situation where there is a single router/leader device in network and user changes the MLE thread mode to make it a sleepy (where due to device being the only node in the network it would not attach). Then, later upon device reset (without the change in this commit) the device would have started again as router/leader. Note that network info is saved/updated in non-volatile memory after a successful attach. --- src/core/thread/mle.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/core/thread/mle.cpp b/src/core/thread/mle.cpp index b0b79c7fc..2b6e89a56 100644 --- a/src/core/thread/mle.cpp +++ b/src/core/thread/mle.cpp @@ -382,6 +382,7 @@ otError Mle::Restore(void) Get().SetCurrentKeySequence(networkInfo.mKeySequence); Get().SetMleFrameCounter(networkInfo.mMleFrameCounter); Get().SetMacFrameCounter(networkInfo.mMacFrameCounter); + mDeviceMode.Set(networkInfo.mDeviceMode); switch (networkInfo.mRole) { @@ -394,7 +395,6 @@ otError Mle::Restore(void) ExitNow(); } - mDeviceMode.Set(networkInfo.mDeviceMode); Get().SetShortAddress(networkInfo.mRloc16); Get().SetExtAddress(networkInfo.mExtAddress); @@ -458,7 +458,6 @@ otError Mle::Store(void) // avoid losing/overwriting previous information when a reboot // occurs after a message is sent but before attaching. - networkInfo.mDeviceMode = mDeviceMode.Get(); networkInfo.mRole = mRole; networkInfo.mRloc16 = GetRloc16(); networkInfo.mPreviousPartitionId = mLeaderData.GetPartitionId(); @@ -491,6 +490,7 @@ otError Mle::Store(void) networkInfo.mKeySequence = Get().GetCurrentKeySequence(); networkInfo.mMleFrameCounter = Get().GetMleFrameCounter() + OPENTHREAD_CONFIG_STORE_FRAME_COUNTER_AHEAD; networkInfo.mMacFrameCounter = Get().GetMacFrameCounter() + OPENTHREAD_CONFIG_STORE_FRAME_COUNTER_AHEAD; + networkInfo.mDeviceMode = mDeviceMode.Get(); SuccessOrExit(error = Get().SaveNetworkInfo(networkInfo)); @@ -842,6 +842,8 @@ otError Mle::SetDeviceMode(DeviceMode aDeviceMode) otLogNoteMle("Mode 0x%02x -> 0x%02x [%s]", oldMode.Get(), mDeviceMode.Get(), mDeviceMode.ToString().AsCString()); + Store(); + switch (mRole) { case OT_DEVICE_ROLE_DISABLED: