[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.
This commit is contained in:
Abtin Keshavarzian
2025-05-08 10:49:49 -07:00
committed by GitHub
parent be879f198d
commit 4df9a5b9c2
7 changed files with 240 additions and 108 deletions
+2
View File
@@ -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",
+1
View File
@@ -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
+10 -20
View File
@@ -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<Interface>().HandleDiscoveredPeerInfo(*static_cast<const Interface::Peer::Info *>(aInfo));
instance.Get<Interface>().HandleDiscoveredPeerInfo(*static_cast<const Interface::PeerInfo *>(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<Mac::Mac>().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<const Peer *>(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
+10 -86
View File
@@ -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<Peer>
{
friend class Interface;
friend class LinkedListEntry<Peer>;
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<const Mac::ExtAddress &>(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<const MeshCoP::ExtendedPanId &>(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<const Ip6::SockAddr &>(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<const Ip6::SockAddr &>(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);
-2
View File
@@ -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,
+76
View File
@@ -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
+141
View File
@@ -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 <openthread/trel.h>
#include <openthread/platform/trel.h>
#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<Peer>, private NonCopyable
{
friend class Interface;
friend class LinkedListEntry<Peer>;
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_