[core] use Min(), Max() to clip values (#9117)

This commit is contained in:
Abtin Keshavarzian
2023-06-01 13:04:09 -07:00
committed by GitHub
parent 8f5d4376c6
commit 87e3250a51
8 changed files with 17 additions and 65 deletions
+3 -14
View File
@@ -171,11 +171,7 @@ Error DataPollSender::SetExternalPollPeriod(uint32_t aPeriod)
{
VerifyOrExit(aPeriod >= OPENTHREAD_CONFIG_MAC_MINIMUM_POLL_PERIOD, error = kErrorInvalidArgs);
// Clipped by the maximal value.
if (aPeriod > kMaxExternalPeriod)
{
aPeriod = kMaxExternalPeriod;
}
aPeriod = Min(aPeriod, kMaxExternalPeriod);
}
if (mExternalPollPeriod != aPeriod)
@@ -414,15 +410,8 @@ void DataPollSender::SendFastPolls(uint8_t aNumFastPolls)
aNumFastPolls = kDefaultFastPolls;
}
if (aNumFastPolls > kMaxFastPolls)
{
aNumFastPolls = kMaxFastPolls;
}
if (mRemainingFastPolls < aNumFastPolls)
{
mRemainingFastPolls = aNumFastPolls;
}
aNumFastPolls = Min(aNumFastPolls, kMaxFastPolls);
mRemainingFastPolls = Max(mRemainingFastPolls, aNumFastPolls);
if (mEnabled && shouldRecalculatePollPeriod)
{
+2 -8
View File
@@ -229,17 +229,11 @@ uint32_t LinkFrameCounters::GetMaximum(void) const
uint32_t counter = 0;
#if OPENTHREAD_CONFIG_RADIO_LINK_IEEE_802_15_4_ENABLE
if (counter < m154Counter)
{
counter = m154Counter;
}
counter = Max(counter, m154Counter);
#endif
#if OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE
if (counter < mTrelCounter)
{
counter = mTrelCounter;
}
counter = Max(counter, mTrelCounter);
#endif
return counter;
+1 -4
View File
@@ -443,10 +443,7 @@ void SubMac::StartCsmaBackoff(void)
VerifyOrExit(ShouldHandleCsmaBackOff(), BeginTransmit());
if (backoffExponent > kCsmaMaxBe)
{
backoffExponent = kCsmaMaxBe;
}
backoffExponent = Min(backoffExponent, kCsmaMaxBe);
StartTimerForBackoff(backoffExponent);
+2 -9
View File
@@ -1054,10 +1054,7 @@ Error Client::AppendServiceInstructions(Message &aMessage, Info &aInfo)
// In such a case, we end up using `mDefaultLease` but then we need
// to make sure it is not greater than the selected `mKeyLease`.
if (mLease > mKeyLease)
{
mLease = mKeyLease;
}
mLease = Min(mLease, mKeyLease);
exit:
return error;
@@ -1844,11 +1841,7 @@ void Client::GrowRetryWaitInterval(void)
{
mRetryWaitInterval =
mRetryWaitInterval / kRetryIntervalGrowthFactorDenominator * kRetryIntervalGrowthFactorNumerator;
if (mRetryWaitInterval > kMaxRetryWaitInterval)
{
mRetryWaitInterval = kMaxRetryWaitInterval;
}
mRetryWaitInterval = Min(mRetryWaitInterval, kMaxRetryWaitInterval);
}
uint32_t Client::DetermineLeaseInterval(uint32_t aInterval, uint32_t aDefaultInterval) const
+1 -5
View File
@@ -959,11 +959,7 @@ void AddressResolver::HandleTimeTick(void)
entry->SetTimeout(retryDelay);
retryDelay <<= 1;
if (retryDelay > kAddressQueryMaxRetryDelay)
{
retryDelay = kAddressQueryMaxRetryDelay;
}
retryDelay = Min(retryDelay, kAddressQueryMaxRetryDelay);
entry->SetRetryDelay(retryDelay);
entry->SetCanEvict(true);
+3 -5
View File
@@ -66,11 +66,9 @@ Error RssAverager::Add(int8_t aRss)
VerifyOrExit(aRss != Radio::kInvalidRssi, error = kErrorInvalidArgs);
// Restrict the RSS value to the closed range [0, -128] so the RSS times precision multiple can fit in 11 bits.
if (aRss > 0)
{
aRss = 0;
}
// Restrict the RSS value to the closed range [-128, 0]
// so the RSS times precision multiple can fit in 11 bits.
aRss = Min<int8_t>(aRss, 0);
// Multiply the RSS value by a precision multiple (currently -8).
+4 -19
View File
@@ -781,12 +781,9 @@ exit:
void Mle::SetTimeout(uint32_t aTimeout)
{
VerifyOrExit(mTimeout != aTimeout);
aTimeout = Max(aTimeout, kMinTimeout);
if (aTimeout < kMinTimeout)
{
aTimeout = kMinTimeout;
}
VerifyOrExit(mTimeout != aTimeout);
mTimeout = aTimeout;
@@ -1542,12 +1539,7 @@ bool Mle::PrepareAnnounceState(void)
}
mAnnounceDelay = kAnnounceTimeout / (channelMask.GetNumberOfChannels() + 1);
if (mAnnounceDelay < kMinAnnounceDelay)
{
mAnnounceDelay = kMinAnnounceDelay;
}
mAnnounceDelay = Max(mAnnounceDelay, kMinAnnounceDelay);
shouldAnnounce = true;
exit:
@@ -3153,14 +3145,7 @@ void Mle::HandleParentResponse(RxInfo &aRxInfo)
// Link Margin
SuccessOrExit(error = Tlv::Find<LinkMarginTlv>(aRxInfo.mMessage, linkMarginFromTlv));
linkMargin = Get<Mac::Mac>().ComputeLinkMargin(rss);
if (linkMargin > linkMarginFromTlv)
{
linkMargin = linkMarginFromTlv;
}
linkMargin = Min(Get<Mac::Mac>().ComputeLinkMargin(rss), linkMarginFromTlv);
linkQuality = LinkQualityForLinkMargin(linkMargin);
// Connectivity
+1 -1
View File
@@ -93,7 +93,7 @@ constexpr uint32_t kChildIdResponseTimeout = 1250; ///< Wait time to rec
constexpr uint32_t kAttachStartJitter = 50; ///< Max jitter time added to start of attach (in msec)
constexpr uint32_t kAnnounceProcessTimeout = 250; ///< Delay after Announce rx before channel/pan-id change
constexpr uint32_t kAnnounceTimeout = 1400; ///< Total timeout for sending Announce messages (in msec)
constexpr uint32_t kMinAnnounceDelay = 80; ///< Min delay between Announcement messages (in msec)
constexpr uint16_t kMinAnnounceDelay = 80; ///< Min delay between Announcement messages (in msec)
constexpr uint32_t kParentResponseMaxDelayRouters = 500; ///< Max response delay for Parent Req to routers (in msec)
constexpr uint32_t kParentResponseMaxDelayAll = 1000; ///< Max response delay for Parent Req to all (in msec)
constexpr uint32_t kUnicastRetransmissionDelay = 1000; ///< Base delay before an MLE unicast retx (in msec)