diff --git a/src/core/mac/link_raw.cpp b/src/core/mac/link_raw.cpp index bbce9f2ac..5857da877 100644 --- a/src/core/mac/link_raw.cpp +++ b/src/core/mac/link_raw.cpp @@ -274,7 +274,7 @@ exit: #if OT_SHOULD_LOG_AT(OT_LOG_LEVEL_INFO) void LinkRaw::RecordFrameTransmitStatus(const TxFrame &aFrame, - const RxFrame *aAckFrame, + RxFrame *aAckFrame, Error aError, uint8_t aRetryCount, bool aWillRetx) diff --git a/src/core/mac/link_raw.hpp b/src/core/mac/link_raw.hpp index e0a66c99f..29b6d528b 100644 --- a/src/core/mac/link_raw.hpp +++ b/src/core/mac/link_raw.hpp @@ -298,12 +298,12 @@ public: * */ void RecordFrameTransmitStatus(const TxFrame &aFrame, - const RxFrame *aAckFrame, + RxFrame *aAckFrame, Error aError, uint8_t aRetryCount, bool aWillRetx); #else - void RecordFrameTransmitStatus(const TxFrame &, const RxFrame *, Error, uint8_t, bool) {} + void RecordFrameTransmitStatus(const TxFrame &, RxFrame *, Error, uint8_t, bool) {} #endif private: diff --git a/src/core/mac/mac.cpp b/src/core/mac/mac.cpp index 6d9b3a227..7e7480a7e 100644 --- a/src/core/mac/mac.cpp +++ b/src/core/mac/mac.cpp @@ -1124,7 +1124,7 @@ void Mac::RecordCcaStatus(bool aCcaSuccess, uint8_t aChannel) } void Mac::RecordFrameTransmitStatus(const TxFrame &aFrame, - const RxFrame *aAckFrame, + RxFrame *aAckFrame, Error aError, uint8_t aRetryCount, bool aWillRetx) @@ -1187,6 +1187,10 @@ void Mac::RecordFrameTransmitStatus(const TxFrame &aFrame, if ((aError == kErrorNone) && ackRequested && (aAckFrame != nullptr) && (neighbor != nullptr)) { +#if OPENTHREAD_CONFIG_MAC_FILTER_ENABLE + SuccessOrExit(mFilter.ApplyToRxFrame(*aAckFrame, neighbor->GetExtAddress(), neighbor)); +#endif + neighbor->GetLinkInfo().AddRss(aAckFrame->GetRssi()); #if OPENTHREAD_CONFIG_MLE_LINK_METRICS_SUBJECT_ENABLE neighbor->AggregateLinkMetrics(/* aSeriesId */ 0, aAckFrame->GetType(), aAckFrame->GetLqi(), @@ -1787,23 +1791,7 @@ void Mac::HandleReceivedFrame(RxFrame *aFrame, Error aError) VerifyOrExit(srcaddr.GetExtended() != GetExtAddress(), error = kErrorInvalidSourceAddress); #if OPENTHREAD_CONFIG_MAC_FILTER_ENABLE - { - int8_t fixedRss; - - SuccessOrExit(error = mFilter.Apply(srcaddr.GetExtended(), fixedRss)); - - if (fixedRss != Filter::kFixedRssDisabled) - { - aFrame->SetRssi(fixedRss); - - // Clear any previous link info to ensure the fixed RSSI - // value takes effect quickly. - if (neighbor != nullptr) - { - neighbor->GetLinkInfo().Clear(); - } - } - } + SuccessOrExit(error = mFilter.ApplyToRxFrame(*aFrame, srcaddr.GetExtended(), neighbor)); #endif break; diff --git a/src/core/mac/mac.hpp b/src/core/mac/mac.hpp index cb7867e58..1abff07fa 100644 --- a/src/core/mac/mac.hpp +++ b/src/core/mac/mac.hpp @@ -412,7 +412,7 @@ public: * */ void RecordFrameTransmitStatus(const TxFrame &aFrame, - const RxFrame *aAckFrame, + RxFrame *aAckFrame, Error aError, uint8_t aRetryCount, bool aWillRetx); diff --git a/src/core/mac/mac_filter.cpp b/src/core/mac/mac_filter.cpp index c119c425b..a5df54ae8 100644 --- a/src/core/mac/mac_filter.cpp +++ b/src/core/mac/mac_filter.cpp @@ -38,6 +38,7 @@ #include "common/array.hpp" #include "common/as_core_type.hpp" #include "common/code_utils.hpp" +#include "thread/topology.hpp" namespace ot { namespace Mac { @@ -53,11 +54,11 @@ Filter::Filter(void) } } -Filter::FilterEntry *Filter::FindEntry(const ExtAddress &aExtAddress) +const Filter::FilterEntry *Filter::FindEntry(const ExtAddress &aExtAddress) const { - FilterEntry *rval = nullptr; + const FilterEntry *rval = nullptr; - for (FilterEntry &entry : mFilterEntries) + for (const FilterEntry &entry : mFilterEntries) { if (entry.IsInUse() && (aExtAddress == entry.mExtAddress)) { @@ -182,13 +183,13 @@ void Filter::ClearAllRssIn(void) mDefaultRssIn = kFixedRssDisabled; } -Error Filter::GetNextRssIn(Iterator &aIterator, Entry &aEntry) +Error Filter::GetNextRssIn(Iterator &aIterator, Entry &aEntry) const { Error error = kErrorNotFound; for (; aIterator < GetArrayLength(mFilterEntries); aIterator++) { - FilterEntry &entry = mFilterEntries[aIterator]; + const FilterEntry &entry = mFilterEntries[aIterator]; if (entry.mRssIn != kFixedRssDisabled) { @@ -213,11 +214,11 @@ exit: return error; } -Error Filter::Apply(const ExtAddress &aExtAddress, int8_t &aRss) +Error Filter::Apply(const ExtAddress &aExtAddress, int8_t &aRss) const { - Error error = kErrorNone; - FilterEntry *entry = FindEntry(aExtAddress); - bool isInFilterList; + Error error = kErrorNone; + const FilterEntry *entry = FindEntry(aExtAddress); + bool isInFilterList; // Use the default RssIn setting for all receiving messages first. aRss = mDefaultRssIn; @@ -250,6 +251,28 @@ exit: return error; } +Error Filter::ApplyToRxFrame(RxFrame &aRxFrame, const ExtAddress &aExtAddress, Neighbor *aNeighbor) const +{ + Error error; + int8_t fixedRss; + + SuccessOrExit(error = Apply(aExtAddress, fixedRss)); + + VerifyOrExit(fixedRss != kFixedRssDisabled); + + aRxFrame.SetRssi(fixedRss); + + if (aNeighbor != nullptr) + { + // Clear the previous RSS average to ensure the fixed RSS + // value takes effect quickly. + aNeighbor->GetLinkInfo().CleaAverageRss(); + } + +exit: + return error; +} + } // namespace Mac } // namespace ot diff --git a/src/core/mac/mac_filter.hpp b/src/core/mac/mac_filter.hpp index 2eaef71aa..d5ff8bba2 100644 --- a/src/core/mac/mac_filter.hpp +++ b/src/core/mac/mac_filter.hpp @@ -41,10 +41,14 @@ #include #include "common/as_core_type.hpp" +#include "common/const_cast.hpp" #include "common/non_copyable.hpp" #include "mac/mac_frame.hpp" namespace ot { + +class Neighbor; + namespace Mac { /** @@ -208,7 +212,7 @@ public: * @retval kErrorNotFound No subsequent entry exists. * */ - Error GetNextRssIn(Iterator &aIterator, Entry &aEntry); + Error GetNextRssIn(Iterator &aIterator, Entry &aEntry) const; /** * This method applies the filter rules on a given Extended Address. @@ -220,7 +224,24 @@ public: * @retval kErrorAddressFiltered Address filter (allowlist or denylist) is enabled and @p aExtAddress is filtered. * */ - Error Apply(const ExtAddress &aExtAddress, int8_t &aRss); + Error Apply(const ExtAddress &aExtAddress, int8_t &aRss) const; + + /** + * This method applies the filter rules to a received frame from a given Extended Address. + * + * This method can potentially update the signal strength value on the received frame @p aRxFrame. If @p aNeighbor + * is not `nullptr` and filter applies a fixed RSS to the @p aRxFrame, this method will also clear the current RSS + * average on @p aNeighbor to ensure that the new fixed RSS takes effect quickly. + * + * @param[out] aRxFrame The received frame. + * @param[in] aExtAddress The extended address from which @p aRxFrame was received. + * @param[in] aNeighbor A pointer to the neighbor (can be `nullptr` if not known). + * + * @retval kErrorNone Successfully applied the filter, @p aRxFrame RSS may be updated. + * @retval kErrorAddressFiltered Address filter (allowlist or denylist) is enabled and @p aExtAddress is filtered. + * + */ + Error ApplyToRxFrame(RxFrame &aRxFrame, const ExtAddress &aExtAddress, Neighbor *aNeighbor = nullptr) const; private: static constexpr uint16_t kMaxEntries = OPENTHREAD_CONFIG_MAC_FILTER_SIZE; @@ -234,8 +255,9 @@ private: bool IsInUse(void) const { return mFiltered || (mRssIn != kFixedRssDisabled); } }; - FilterEntry *FindAvailableEntry(void); - FilterEntry *FindEntry(const ExtAddress &aExtAddress); + FilterEntry *FindAvailableEntry(void); + const FilterEntry *FindEntry(const ExtAddress &aExtAddress) const; + FilterEntry *FindEntry(const ExtAddress &aExtAddress) { return AsNonConst(AsConst(this)->FindEntry(aExtAddress)); } Mode mMode; int8_t mDefaultRssIn; diff --git a/src/core/mac/sub_mac.hpp b/src/core/mac/sub_mac.hpp index 1bcfc7691..2822a4be4 100644 --- a/src/core/mac/sub_mac.hpp +++ b/src/core/mac/sub_mac.hpp @@ -166,7 +166,7 @@ public: * */ void RecordFrameTransmitStatus(const TxFrame &aFrame, - const RxFrame *aAckFrame, + RxFrame *aAckFrame, Error aError, uint8_t aRetryCount, bool aWillRetx); diff --git a/src/core/mac/sub_mac_callbacks.cpp b/src/core/mac/sub_mac_callbacks.cpp index 7580b6e26..4a3053dd2 100644 --- a/src/core/mac/sub_mac_callbacks.cpp +++ b/src/core/mac/sub_mac_callbacks.cpp @@ -71,7 +71,7 @@ void SubMac::Callbacks::RecordCcaStatus(bool aCcaSuccess, uint8_t aChannel) } void SubMac::Callbacks::RecordFrameTransmitStatus(const TxFrame &aFrame, - const RxFrame *aAckFrame, + RxFrame *aAckFrame, Error aError, uint8_t aRetryCount, bool aWillRetx) @@ -119,7 +119,7 @@ void SubMac::Callbacks::ReceiveDone(RxFrame *aFrame, Error aError) { Get