diff --git a/src/core/mac/mac.cpp b/src/core/mac/mac.cpp index 1cbae974b..f0434d1f5 100644 --- a/src/core/mac/mac.cpp +++ b/src/core/mac/mac.cpp @@ -946,6 +946,35 @@ void Mac::SendBeacon(Frame &aFrame) LogBeacon("Sending", *beaconPayload); } +bool Mac::ShouldSendBeacon(void) const +{ + bool shouldSend = false; + + VerifyOrExit(mEnabled); + + shouldSend = IsBeaconEnabled(); + +#if OPENTHREAD_CONFIG_ENABLE_BEACON_RSP_WHEN_JOINABLE + if (!shouldSend) + { + // When `ENABLE_BEACON_RSP_WHEN_JOINABLE` feature is enabled, + // the device should transmit IEEE 802.15.4 Beacons in response + // to IEEE 802.15.4 Beacon Requests even while the device is not + // router capable and detached (i.e., `IsBeaconeEnabled()` is + // false) but only if it is in joinable state (unsecure port + // list is not empty). + + uint8_t numUnsecurePorts; + + GetNetif().GetIp6Filter().GetUnsecurePorts(numUnsecurePorts); + shouldSend = (numUnsecurePorts != 0); + } +#endif + +exit: + return shouldSend; +} + void Mac::ProcessTransmitAesCcm(Frame &aFrame, const ExtAddress *aExtAddress) { uint32_t frameCounter = 0; @@ -2198,11 +2227,7 @@ otError Mac::HandleMacCommand(Frame &aFrame) mCounters.mRxBeaconRequest++; otLogInfoMac("Received Beacon Request"); - if (mEnabled && (mBeaconsEnabled -#if OPENTHREAD_CONFIG_ENABLE_BEACON_RSP_WHEN_JOINABLE - || IsBeaconJoinable() -#endif // OPENTHREAD_CONFIG_ENABLE_BEACON_RSP_WHEN_JOINABLE - )) + if (ShouldSendBeacon()) { StartOperation(kOperationTransmitBeacon); } @@ -2289,27 +2314,6 @@ int8_t Mac::GetNoiseFloor(void) return otPlatRadioGetReceiveSensitivity(&GetInstance()); } -#if OPENTHREAD_CONFIG_ENABLE_BEACON_RSP_WHEN_JOINABLE -bool Mac::IsBeaconJoinable(void) -{ - uint8_t numUnsecurePorts; - bool joinable = false; - - GetNetif().GetIp6Filter().GetUnsecurePorts(numUnsecurePorts); - - if (numUnsecurePorts) - { - joinable = true; - } - else - { - joinable = false; - } - - return joinable; -} -#endif // OPENTHREAD_CONFIG_ENABLE_BEACON_RSP_WHEN_JOINABLE - const char *Mac::OperationToString(Operation aOperation) { const char *retval = ""; diff --git a/src/core/mac/mac.hpp b/src/core/mac/mac.hpp index 9c73a0775..577cdc29c 100644 --- a/src/core/mac/mac.hpp +++ b/src/core/mac/mac.hpp @@ -832,17 +832,6 @@ public: */ void ResetCounters(void); -#if OPENTHREAD_CONFIG_ENABLE_BEACON_RSP_WHEN_JOINABLE - /** - * This method indicates if the device is in joinable state or not. - * - * @retval true Device is joinable. - * @retval false Device is non-joinable. - * - */ - bool IsBeaconJoinable(void); -#endif // OPENTHREAD_CONFIG_ENABLE_BEACON_RSP_WHEN_JOINABLE - /** * This method returns the MAC counter. * @@ -973,6 +962,7 @@ private: void FinishOperation(void); void SendBeaconRequest(Frame &aFrame); void SendBeacon(Frame &aFrame); + bool ShouldSendBeacon(void) const; void StartBackoff(void); void BeginTransmit(void); otError HandleMacCommand(Frame &aFrame);