From 4df9a5b9c23c4cd25095b1a3f28256dc9b8a89ca Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Thu, 8 May 2025 10:49:49 -0700 Subject: [PATCH] [trel] refactor `Peer` class and enhance `PeerInfo` parsing (#11477) This commit contains smaller changes related to the TREL `Peer` class and the parsing of TXT data within the `PeerInfo` class. The `Peer` class definition is now moved into its own `trel_peer.hpp` and `trel_peer.cpp` header and source files, separating it from the `Trel::Interface` class. Additionally, the `Log()` method within the `Peer` class has been enhanced (now using an `Action` enum). The `PeerInfo` class remains a nested class of `Interface` and now provides a `ParseTxtData()` method to parse the included TXT data entries. --- src/core/BUILD.gn | 2 + src/core/CMakeLists.txt | 1 + src/core/radio/trel_interface.cpp | 30 +++---- src/core/radio/trel_interface.hpp | 96 +++----------------- src/core/radio/trel_link.hpp | 2 - src/core/radio/trel_peer.cpp | 76 ++++++++++++++++ src/core/radio/trel_peer.hpp | 141 ++++++++++++++++++++++++++++++ 7 files changed, 240 insertions(+), 108 deletions(-) create mode 100644 src/core/radio/trel_peer.cpp create mode 100644 src/core/radio/trel_peer.hpp diff --git a/src/core/BUILD.gn b/src/core/BUILD.gn index 4977f85c5..80b6e275d 100644 --- a/src/core/BUILD.gn +++ b/src/core/BUILD.gn @@ -617,6 +617,8 @@ openthread_core_files = [ "radio/trel_link.hpp", "radio/trel_packet.cpp", "radio/trel_packet.hpp", + "radio/trel_peer.cpp", + "radio/trel_peer.hpp", "thread/address_resolver.cpp", "thread/address_resolver.hpp", "thread/announce_begin_server.cpp", diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index b5975e5de..4fdb436a8 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -203,6 +203,7 @@ set(COMMON_SOURCES radio/trel_interface.cpp radio/trel_link.cpp radio/trel_packet.cpp + radio/trel_peer.cpp thread/address_resolver.cpp thread/announce_begin_server.cpp thread/announce_sender.cpp diff --git a/src/core/radio/trel_interface.cpp b/src/core/radio/trel_interface.cpp index 696138b88..12f20c089 100644 --- a/src/core/radio/trel_interface.cpp +++ b/src/core/radio/trel_interface.cpp @@ -115,7 +115,7 @@ void Interface::ClearPeerList(void) mPeerPool.FreeAll(); } -Interface::Peer *Interface::FindPeer(const Mac::ExtAddress &aExtAddress) { return mPeerList.FindMatching(aExtAddress); } +Peer *Interface::FindPeer(const Mac::ExtAddress &aExtAddress) { return mPeerList.FindMatching(aExtAddress); } void Interface::NotifyPeerSocketAddressDifference(const Ip6::SockAddr &aPeerSockAddr, const Ip6::SockAddr &aRxSockAddr) { @@ -175,13 +175,13 @@ extern "C" void otPlatTrelHandleDiscoveredPeerInfo(otInstance *aInstance, const Instance &instance = AsCoreType(aInstance); VerifyOrExit(instance.IsInitialized()); - instance.Get().HandleDiscoveredPeerInfo(*static_cast(aInfo)); + instance.Get().HandleDiscoveredPeerInfo(*static_cast(aInfo)); exit: return; } -void Interface::HandleDiscoveredPeerInfo(const Peer::Info &aInfo) +void Interface::HandleDiscoveredPeerInfo(const PeerInfo &aInfo) { Peer *entry; Mac::ExtAddress extAddress; @@ -190,7 +190,7 @@ void Interface::HandleDiscoveredPeerInfo(const Peer::Info &aInfo) VerifyOrExit(mInitialized && mEnabled); - SuccessOrExit(ParsePeerInfoTxtData(aInfo, extAddress, extPanId)); + SuccessOrExit(aInfo.ParseTxtData(extAddress, extPanId)); VerifyOrExit(extAddress != Get().GetExtAddress()); @@ -238,15 +238,13 @@ void Interface::HandleDiscoveredPeerInfo(const Peer::Info &aInfo) entry->SetExtPanId(extPanId); entry->SetSockAddr(aInfo.GetSockAddr()); - entry->Log(isNew ? "Added" : "Updated"); + entry->Log(isNew ? Peer::kAdded : Peer::kUpdated); exit: return; } -Error Interface::ParsePeerInfoTxtData(const Peer::Info &aInfo, - Mac::ExtAddress &aExtAddress, - MeshCoP::ExtendedPanId &aExtPanId) const +Error Interface::PeerInfo::ParseTxtData(Mac::ExtAddress &aExtAddress, MeshCoP::ExtendedPanId &aExtPanId) const { Error error; Dns::TxtEntry entry; @@ -256,7 +254,7 @@ Error Interface::ParsePeerInfoTxtData(const Peer::Info &aInfo, aExtPanId.Clear(); - iterator.Init(aInfo.GetTxtData(), aInfo.GetTxtLength()); + iterator.Init(mTxtData, mTxtLength); while ((error = iterator.GetNextEntry(entry)) == kErrorNone) { @@ -296,7 +294,7 @@ exit: return error; } -Interface::Peer *Interface::GetNewPeerEntry(void) +Peer *Interface::GetNewPeerEntry(void) { Peer *peerEntry; @@ -342,7 +340,7 @@ exit: void Interface::RemovePeerEntry(Peer &aEntry) { - aEntry.Log("Removing"); + aEntry.Log(Peer::kRemoving); if (mPeerList.Remove(aEntry) == kErrorNone) { @@ -415,7 +413,7 @@ exit: return; } -const Interface::Peer *Interface::GetNextPeer(PeerIterator &aIterator) const +const Peer *Interface::GetNextPeer(PeerIterator &aIterator) const { const Peer *entry = static_cast(aIterator); @@ -439,14 +437,6 @@ uint16_t Interface::GetNumberOfPeers(void) const return count; } -void Interface::Peer::Log(const char *aAction) const -{ - OT_UNUSED_VARIABLE(aAction); - - LogInfo("%s peer mac:%s, xpan:%s, %s", aAction, GetExtAddress().ToString().AsCString(), - GetExtPanId().ToString().AsCString(), GetSockAddr().ToString().AsCString()); -} - } // namespace Trel } // namespace ot diff --git a/src/core/radio/trel_interface.hpp b/src/core/radio/trel_interface.hpp index 1f9a7ce95..41e2e336b 100644 --- a/src/core/radio/trel_interface.hpp +++ b/src/core/radio/trel_interface.hpp @@ -46,16 +46,14 @@ #include "common/pool.hpp" #include "common/tasklet.hpp" #include "common/time.hpp" -#include "mac/mac_types.hpp" -#include "net/ip6_address.hpp" -#include "net/socket.hpp" #include "radio/trel_packet.hpp" -#include "thread/mle_types.hpp" +#include "radio/trel_peer.hpp" namespace ot { namespace Trel { class Link; +class Interface; extern "C" void otPlatTrelHandleReceived(otInstance *aInstance, uint8_t *aBuffer, @@ -81,84 +79,6 @@ class Interface : public InstanceLocator friend void otPlatTrelHandleDiscoveredPeerInfo(otInstance *aInstance, const otPlatTrelPeerInfo *aInfo); public: - /** - * Represents information about a discovered TREL peer. - */ - class Peer : public otTrelPeer, public LinkedListEntry - { - friend class Interface; - friend class LinkedListEntry; - friend void otPlatTrelHandleDiscoveredPeerInfo(otInstance *aInstance, const otPlatTrelPeerInfo *aInfo); - - public: - /** - * Returns the Extended MAC Address of the discovered TREL peer. - * - * @returns The Extended MAC Address of the TREL peer. - */ - const Mac::ExtAddress &GetExtAddress(void) const { return static_cast(mExtAddress); } - - /** - * Returns the Extended PAN Identifier of the discovered TREL peer. - * - * @returns The Extended PAN Identifier of the TREL peer. - */ - const MeshCoP::ExtendedPanId &GetExtPanId(void) const - { - return static_cast(mExtPanId); - } - - /** - * Returns the IPv6 socket address of the discovered TREL peer. - * - * @returns The IPv6 socket address of the TREL peer. - */ - const Ip6::SockAddr &GetSockAddr(void) const { return static_cast(mSockAddr); } - - /** - * Set the IPv6 socket address of the discovered TREL peer. - * - * @param[in] aSockAddr The IPv6 socket address. - */ - void SetSockAddr(const Ip6::SockAddr &aSockAddr) { mSockAddr = aSockAddr; } - - /** - * Indicates whether the peer matches a given Extended Address. - * - * @param[in] aExtAddress A Extended Address to match with. - * - * @retval TRUE if the peer matches @p aExtAddress. - * @retval FALSE if the peer does not match @p aExtAddress. - */ - bool Matches(const Mac::ExtAddress &aExtAddress) const { return GetExtAddress() == aExtAddress; } - - /** - * Indicates whether the peer matches a given Socket Address. - * - * @param[in] aSockAddr A Socket Address to match with. - * - * @retval TRUE if the peer matches @p aSockAddr. - * @retval FALSE if the peer does not match @p aSockAddr. - */ - bool Matches(const Ip6::SockAddr &aSockAddr) const { return GetSockAddr() == aSockAddr; } - - private: - class Info : public otPlatTrelPeerInfo - { - public: - bool IsRemoved(void) const { return mRemoved; } - const uint8_t *GetTxtData(void) const { return mTxtData; } - uint16_t GetTxtLength(void) const { return mTxtLength; } - const Ip6::SockAddr &GetSockAddr(void) const { return static_cast(mSockAddr); } - }; - - void SetExtAddress(const Mac::ExtAddress &aExtAddress) { mExtAddress = aExtAddress; } - void SetExtPanId(const MeshCoP::ExtendedPanId &aExtPanId) { mExtPanId = aExtPanId; } - void Log(const char *aAction) const; - - Peer *mNext; - }; - /** * Represents an iterator for iterating over TREL peer table entries. */ @@ -288,6 +208,13 @@ private: static const char kTxtRecordExtAddressKey[]; static const char kTxtRecordExtPanIdKey[]; + struct PeerInfo : public otPlatTrelPeerInfo + { + bool IsRemoved(void) const { return mRemoved; } + const Ip6::SockAddr &GetSockAddr(void) const { return AsCoreType(&mSockAddr); } + Error ParseTxtData(Mac::ExtAddress &aExtAddress, MeshCoP::ExtendedPanId &aExtPanId) const; + }; + explicit Interface(Instance &aInstance); // Methods used by `Trel::Link`. @@ -298,12 +225,9 @@ private: // Callbacks from `otPlatTrel`. void HandleReceived(uint8_t *aBuffer, uint16_t aLength, const Ip6::SockAddr &aSenderAddr); - void HandleDiscoveredPeerInfo(const Peer::Info &aInfo); + void HandleDiscoveredPeerInfo(const PeerInfo &aInfo); void RegisterService(void); - Error ParsePeerInfoTxtData(const Peer::Info &aInfo, - Mac::ExtAddress &aExtAddress, - MeshCoP::ExtendedPanId &aExtPanId) const; Peer *GetNewPeerEntry(void); void RemovePeerEntry(Peer &aEntry); void ClearPeerList(void); diff --git a/src/core/radio/trel_link.hpp b/src/core/radio/trel_link.hpp index f5ccf9857..bc787cb2d 100644 --- a/src/core/radio/trel_link.hpp +++ b/src/core/radio/trel_link.hpp @@ -165,8 +165,6 @@ private: static constexpr uint32_t kAckWaitWindow = 750; // (in msec) static constexpr uint16_t kFcfFramePending = 1 << 4; - typedef Interface::Peer Peer; - enum State : uint8_t { kStateDisabled, diff --git a/src/core/radio/trel_peer.cpp b/src/core/radio/trel_peer.cpp new file mode 100644 index 000000000..972fe792b --- /dev/null +++ b/src/core/radio/trel_peer.cpp @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2025, The OpenThread Authors. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * @file + * This file implements Thread Radio Encapsulation Link (TREL) peer. + */ + +#include "trel_peer.hpp" + +#if OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE + +#include "instance/instance.hpp" + +namespace ot { +namespace Trel { + +RegisterLogModule("TrelInterface"); + +#if OT_SHOULD_LOG_AT(OT_LOG_LEVEL_INFO) + +void Peer::Log(Action aAction) const +{ + LogInfo("%s peer mac:%s, xpan:%s, %s", ActionToString(aAction), GetExtAddress().ToString().AsCString(), + GetExtPanId().ToString().AsCString(), GetSockAddr().ToString().AsCString()); +} + +const char *Peer::ActionToString(Action aAction) +{ + static const char *const kActionStrings[] = { + "Added", // (0) kAdded + "Updated", // (1) kUpdated + "Removing", // (2) kRemoving + }; + + struct EnumCheck + { + InitEnumValidatorCounter(); + ValidateNextEnum(kAdded); + ValidateNextEnum(kUpdated); + ValidateNextEnum(kRemoving); + }; + + return kActionStrings[aAction]; +} + +#endif // OT_SHOULD_LOG_AT(OT_LOG_LEVEL_INFO) + +} // namespace Trel +} // namespace ot + +#endif // #if OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE diff --git a/src/core/radio/trel_peer.hpp b/src/core/radio/trel_peer.hpp new file mode 100644 index 000000000..4b22cdcb1 --- /dev/null +++ b/src/core/radio/trel_peer.hpp @@ -0,0 +1,141 @@ +/* + * Copyright (c) 2019-2025, The OpenThread Authors. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * @file + * This file includes definitions for Thread Radio Encapsulation Link (TREL) peer. + */ + +#ifndef TREL_PEER_HPP_ +#define TREL_PEER_HPP_ + +#include "openthread-core-config.h" + +#if OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE + +#include +#include + +#include "common/as_core_type.hpp" +#include "common/linked_list.hpp" +#include "common/log.hpp" +#include "common/non_copyable.hpp" +#include "mac/mac_types.hpp" +#include "meshcop/extended_panid.hpp" +#include "net/socket.hpp" +#include "thread/mle_types.hpp" + +namespace ot { +namespace Trel { + +class Interface; + +/** + * Represents a discovered TREL peer. + */ +class Peer : public otTrelPeer, public LinkedListEntry, private NonCopyable +{ + friend class Interface; + friend class LinkedListEntry; + +public: + /** + * Returns the Extended MAC Address of the discovered TREL peer. + * + * @returns The Extended MAC Address of the TREL peer. + */ + const Mac::ExtAddress &GetExtAddress(void) const { return AsCoreType(&mExtAddress); } + + /** + * Returns the Extended PAN Identifier of the discovered TREL peer. + * + * @returns The Extended PAN Identifier of the TREL peer. + */ + const MeshCoP::ExtendedPanId &GetExtPanId(void) const { return AsCoreType(&mExtPanId); } + + /** + * Returns the IPv6 socket address of the discovered TREL peer. + * + * @returns The IPv6 socket address of the TREL peer. + */ + const Ip6::SockAddr &GetSockAddr(void) const { return AsCoreType(&mSockAddr); } + + /** + * Set the IPv6 socket address of the discovered TREL peer. + * + * @param[in] aSockAddr The IPv6 socket address. + */ + void SetSockAddr(const Ip6::SockAddr &aSockAddr) { mSockAddr = aSockAddr; } + + /** + * Indicates whether the peer matches a given Extended Address. + * + * @param[in] aExtAddress A Extended Address to match with. + * + * @retval TRUE if the peer matches @p aExtAddress. + * @retval FALSE if the peer does not match @p aExtAddress. + */ + bool Matches(const Mac::ExtAddress &aExtAddress) const { return GetExtAddress() == aExtAddress; } + + /** + * Indicates whether the peer matches a given Socket Address. + * + * @param[in] aSockAddr A Socket Address to match with. + * + * @retval TRUE if the peer matches @p aSockAddr. + * @retval FALSE if the peer does not match @p aSockAddr. + */ + bool Matches(const Ip6::SockAddr &aSockAddr) const { return GetSockAddr() == aSockAddr; } + +private: + enum Action : uint8_t + { + kAdded, + kUpdated, + kRemoving, + }; + + void SetExtAddress(const Mac::ExtAddress &aExtAddress) { mExtAddress = aExtAddress; } + void SetExtPanId(const MeshCoP::ExtendedPanId &aExtPanId) { mExtPanId = aExtPanId; } + +#if OT_SHOULD_LOG_AT(OT_LOG_LEVEL_INFO) + void Log(Action aAction) const; + static const char *ActionToString(Action aAction); +#else + void Log(Action) const {} +#endif + + Peer *mNext; +}; + +} // namespace Trel +} // namespace ot + +#endif // #if OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE + +#endif // TREL_PEER_HPP_