From 0f271fe51c8331bc43b9deed00fa30f650980877 Mon Sep 17 00:00:00 2001 From: Jintao Lin Date: Tue, 13 Apr 2021 14:06:03 +0800 Subject: [PATCH] update poll period when setting CSL timeout --- src/core/api/link_api.cpp | 4 +-- src/core/mac/data_poll_sender.cpp | 2 +- src/core/mac/mac.cpp | 15 --------- src/core/mac/mac.hpp | 16 ---------- src/core/mac/sub_mac.cpp | 6 ---- src/core/mac/sub_mac.hpp | 52 ++++++++++--------------------- src/core/thread/mle.cpp | 24 ++++++++++++-- src/core/thread/mle.hpp | 21 +++++++++++++ 8 files changed, 62 insertions(+), 78 deletions(-) diff --git a/src/core/api/link_api.cpp b/src/core/api/link_api.cpp index 7db89344cd3..eb7f714c6f3 100644 --- a/src/core/api/link_api.cpp +++ b/src/core/api/link_api.cpp @@ -520,7 +520,7 @@ otError otLinkCslSetPeriod(otInstance *aInstance, uint16_t aPeriod) uint32_t otLinkCslGetTimeout(otInstance *aInstance) { - return static_cast(aInstance)->Get().GetCslTimeout(); + return static_cast(aInstance)->Get().GetCslTimeout(); } otError otLinkCslSetTimeout(otInstance *aInstance, uint32_t aTimeout) @@ -529,7 +529,7 @@ otError otLinkCslSetTimeout(otInstance *aInstance, uint32_t aTimeout) Instance &instance = *static_cast(aInstance); VerifyOrExit(kMaxCslTimeout >= aTimeout, error = kErrorInvalidArgs); - instance.Get().SetCslTimeout(aTimeout); + instance.Get().SetCslTimeout(aTimeout); exit: return error; diff --git a/src/core/mac/data_poll_sender.cpp b/src/core/mac/data_poll_sender.cpp index 8c24a9fd6b5..585adbd0e6f 100644 --- a/src/core/mac/data_poll_sender.cpp +++ b/src/core/mac/data_poll_sender.cpp @@ -542,7 +542,7 @@ uint32_t DataPollSender::GetDefaultPollPeriod(void) const #if OPENTHREAD_CONFIG_MAC_CSL_RECEIVER_ENABLE if (Get().IsCslEnabled()) { - period = OT_MIN(period, Time::SecToMsec(Get().GetCslTimeout())); + period = OT_MIN(period, Time::SecToMsec(Get().GetCslTimeout())); } #endif diff --git a/src/core/mac/mac.cpp b/src/core/mac/mac.cpp index 4d333d68fbe..a0910bbad3b 100644 --- a/src/core/mac/mac.cpp +++ b/src/core/mac/mac.cpp @@ -2467,21 +2467,6 @@ void Mac::SetCslPeriod(uint16_t aPeriod) UpdateIdleMode(); } -void Mac::SetCslTimeout(uint32_t aTimeout) -{ - VerifyOrExit(GetCslTimeout() != aTimeout); - - mLinks.GetSubMac().SetCslTimeout(aTimeout); - - if (IsCslEnabled()) - { - Get().ScheduleChildUpdateRequest(); - } - -exit: - return; -} - bool Mac::IsCslEnabled(void) const { return (GetCslPeriod() > 0) && !GetRxOnWhenIdle() && Get().IsChild() && diff --git a/src/core/mac/mac.hpp b/src/core/mac/mac.hpp index e405a61f00d..78a69d179d9 100644 --- a/src/core/mac/mac.hpp +++ b/src/core/mac/mac.hpp @@ -720,22 +720,6 @@ class Mac : public InstanceLocator, private NonCopyable */ void SetCslPeriod(uint16_t aPeriod); - /** - * This method gets the CSL timeout. - * - * @returns CSL timeout in seconds. - * - */ - uint32_t GetCslTimeout(void) const { return mLinks.GetSubMac().GetCslTimeout(); } - - /** - * This method sets the CSL timeout. - * - * @param[in] aTimeout The CSL timeout in seconds. - * - */ - void SetCslTimeout(uint32_t aTimeout); - /** * This method indicates whether CSL is started at the moment. * diff --git a/src/core/mac/sub_mac.cpp b/src/core/mac/sub_mac.cpp index 40fd47cc4ef..d2720a61d14 100644 --- a/src/core/mac/sub_mac.cpp +++ b/src/core/mac/sub_mac.cpp @@ -67,7 +67,6 @@ SubMac::SubMac(Instance &aInstance) , mKeyId(0) , mTimer(aInstance, SubMac::HandleTimer) #if OPENTHREAD_CONFIG_MAC_CSL_RECEIVER_ENABLE - , mCslTimeout(OPENTHREAD_CONFIG_CSL_TIMEOUT) , mCslPeriod(0) , mCslChannel(0) , mIsCslChannelSpecified(false) @@ -972,11 +971,6 @@ void SubMac::SetCslPeriod(uint16_t aPeriod) return; } -void SubMac::SetCslTimeout(uint32_t aTimeout) -{ - mCslTimeout = aTimeout; -} - void SubMac::HandleCslTimer(Timer &aTimer) { aTimer.Get().HandleCslTimer(); diff --git a/src/core/mac/sub_mac.hpp b/src/core/mac/sub_mac.hpp index 48352d1a856..dabbe87a5d2 100644 --- a/src/core/mac/sub_mac.hpp +++ b/src/core/mac/sub_mac.hpp @@ -319,25 +319,6 @@ class SubMac : public InstanceLocator, private NonCopyable */ Error Receive(uint8_t aChannel); -#if OPENTHREAD_CONFIG_MAC_CSL_RECEIVER_ENABLE - /** - * This method lets `SubMac` start CSL sample. - * - * `SubMac` would switch the radio state between `Receive` and `Sleep` according the CSL timer. When CslSample is - * started, `mState` will become `kStateCslSample`. But it could be doing `Sleep` or `Receive` at this moment - * (depending on `mCslState`). - * - * @param[in] aPanChannel The current phy channel used by the device. This param will only take effect when CSL - * channel hasn't been explicitly specified. - * - * @retval kErrorNone Successfully entered CSL operation (sleep or receive according to CSL timer). - * @retval kErrorBusy The radio was transmitting. - * @retval kErrorInvalidState The radio was disabled. - * - */ - Error CslSample(uint8_t aPanChannel); -#endif - /** * This method gets the radio transmit frame. * @@ -398,6 +379,23 @@ class SubMac : public InstanceLocator, private NonCopyable #if OPENTHREAD_CONFIG_MAC_CSL_RECEIVER_ENABLE + /** + * This method lets `SubMac` start CSL sample. + * + * `SubMac` would switch the radio state between `Receive` and `Sleep` according the CSL timer. When CslSample is + * started, `mState` will become `kStateCslSample`. But it could be doing `Sleep` or `Receive` at this moment + * (depending on `mCslState`). + * + * @param[in] aPanChannel The current phy channel used by the device. This param will only take effect when CSL + * channel hasn't been explicitly specified. + * + * @retval kErrorNone Successfully entered CSL operation (sleep or receive according to CSL timer). + * @retval kErrorBusy The radio was transmitting. + * @retval kErrorInvalidState The radio was disabled. + * + */ + Error CslSample(uint8_t aPanChannel); + /** * This method gets the CSL channel. * @@ -444,21 +442,6 @@ class SubMac : public InstanceLocator, private NonCopyable */ void SetCslPeriod(uint16_t aPeriod); - /** - * This method gets the CSL timeout. - * - * @returns CSL timeout - * - */ - uint32_t GetCslTimeout(void) const { return mCslTimeout; } - - /** - * This method sets the CSL timeout. - * - * @param[in] aTimeout The CSL timeout in seconds. - * - */ - void SetCslTimeout(uint32_t aTimeout); #endif // OPENTHREAD_CONFIG_MAC_CSL_RECEIVER_ENABLE /** @@ -643,7 +626,6 @@ class SubMac : public InstanceLocator, private NonCopyable #endif #if OPENTHREAD_CONFIG_MAC_CSL_RECEIVER_ENABLE - uint32_t mCslTimeout; ///< The CSL synchronized timeout in seconds. uint16_t mCslPeriod; ///< The CSL sample period, in units of 10 symbols (160 microseconds). uint8_t mCslChannel : 7; ///< The actually CSL sample channel. If `mIsCslChannelSpecified` is 0, this should be ///< equal to the Pan channel of `Mac`. diff --git a/src/core/thread/mle.cpp b/src/core/thread/mle.cpp index a8e6befca4a..ff9eaad5422 100644 --- a/src/core/thread/mle.cpp +++ b/src/core/thread/mle.cpp @@ -93,6 +93,9 @@ Mle::Mle(Instance &aInstance) , mReceivedResponseFromParent(false) , mSocket(aInstance) , mTimeout(kMleEndDeviceTimeout) +#if OPENTHREAD_CONFIG_MAC_CSL_RECEIVER_ENABLE + , mCslTimeout(OPENTHREAD_CONFIG_CSL_TIMEOUT) +#endif #if OPENTHREAD_CONFIG_MLE_INFORM_PREVIOUS_PARENT_ON_REATTACH , mPreviousParentRloc(Mac::kShortAddrInvalid) #endif @@ -1451,8 +1454,24 @@ Error Mle::AppendCslChannel(Message &aMessage) Error Mle::AppendCslTimeout(Message &aMessage) { OT_ASSERT(Get().IsCslEnabled()); - return Tlv::Append(aMessage, Get().GetCslTimeout() == 0 ? mTimeout - : Get().GetCslTimeout()); + return Tlv::Append(aMessage, mCslTimeout == 0 ? mTimeout : mCslTimeout); +} + +void Mle::SetCslTimeout(uint32_t aTimeout) +{ + VerifyOrExit(mCslTimeout != aTimeout); + + mCslTimeout = aTimeout; + + Get().RecalculatePollPeriod(); + + if (Get().IsCslEnabled()) + { + ScheduleChildUpdateRequest(); + } + +exit: + return; } #endif // OPENTHREAD_CONFIG_MAC_CSL_RECEIVER_ENABLE @@ -2305,7 +2324,6 @@ Error Mle::SendChildUpdateRequest(void) if (!IsRxOnWhenIdle()) { #if OPENTHREAD_CONFIG_MAC_CSL_RECEIVER_ENABLE - Get().RecalculatePollPeriod(); Get().SetAttachMode(!Get().IsCslEnabled()); #else Get().SetAttachMode(true); diff --git a/src/core/thread/mle.hpp b/src/core/thread/mle.hpp index 0f50e8fd56a..69d20df2eb6 100644 --- a/src/core/thread/mle.hpp +++ b/src/core/thread/mle.hpp @@ -714,6 +714,24 @@ class Mle : public InstanceLocator, private NonCopyable */ bool HasRestored(void) const { return mHasRestored; } +#if OPENTHREAD_CONFIG_MAC_CSL_RECEIVER_ENABLE + /** + * This method gets the CSL timeout. + * + * @returns CSL timeout + * + */ + uint32_t GetCslTimeout(void) const { return mCslTimeout; } + + /** + * This method sets the CSL timeout. + * + * @param[in] aTimeout The CSL timeout in seconds. + * + */ + void SetCslTimeout(uint32_t aTimeout); +#endif // OPENTHREAD_CONFIG_MAC_CSL_RECEIVER_ENABLE + protected: /** * MLE Command Types. @@ -1815,6 +1833,9 @@ class Mle : public InstanceLocator, private NonCopyable Ip6::Udp::Socket mSocket; uint32_t mTimeout; +#if OPENTHREAD_CONFIG_MAC_CSL_RECEIVER_ENABLE + uint32_t mCslTimeout; +#endif #if OPENTHREAD_CONFIG_MLE_INFORM_PREVIOUS_PARENT_ON_REATTACH uint16_t mPreviousParentRloc;