Skip to content

Commit 4df9a5b

Browse files
authored
[trel] refactor Peer class and enhance PeerInfo parsing (openthread#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.
1 parent be879f1 commit 4df9a5b

7 files changed

Lines changed: 240 additions & 108 deletions

File tree

src/core/BUILD.gn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,8 @@ openthread_core_files = [
617617
"radio/trel_link.hpp",
618618
"radio/trel_packet.cpp",
619619
"radio/trel_packet.hpp",
620+
"radio/trel_peer.cpp",
621+
"radio/trel_peer.hpp",
620622
"thread/address_resolver.cpp",
621623
"thread/address_resolver.hpp",
622624
"thread/announce_begin_server.cpp",

src/core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ set(COMMON_SOURCES
203203
radio/trel_interface.cpp
204204
radio/trel_link.cpp
205205
radio/trel_packet.cpp
206+
radio/trel_peer.cpp
206207
thread/address_resolver.cpp
207208
thread/announce_begin_server.cpp
208209
thread/announce_sender.cpp

src/core/radio/trel_interface.cpp

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ void Interface::ClearPeerList(void)
115115
mPeerPool.FreeAll();
116116
}
117117

118-
Interface::Peer *Interface::FindPeer(const Mac::ExtAddress &aExtAddress) { return mPeerList.FindMatching(aExtAddress); }
118+
Peer *Interface::FindPeer(const Mac::ExtAddress &aExtAddress) { return mPeerList.FindMatching(aExtAddress); }
119119

120120
void Interface::NotifyPeerSocketAddressDifference(const Ip6::SockAddr &aPeerSockAddr, const Ip6::SockAddr &aRxSockAddr)
121121
{
@@ -175,13 +175,13 @@ extern "C" void otPlatTrelHandleDiscoveredPeerInfo(otInstance *aInstance, const
175175
Instance &instance = AsCoreType(aInstance);
176176

177177
VerifyOrExit(instance.IsInitialized());
178-
instance.Get<Interface>().HandleDiscoveredPeerInfo(*static_cast<const Interface::Peer::Info *>(aInfo));
178+
instance.Get<Interface>().HandleDiscoveredPeerInfo(*static_cast<const Interface::PeerInfo *>(aInfo));
179179

180180
exit:
181181
return;
182182
}
183183

184-
void Interface::HandleDiscoveredPeerInfo(const Peer::Info &aInfo)
184+
void Interface::HandleDiscoveredPeerInfo(const PeerInfo &aInfo)
185185
{
186186
Peer *entry;
187187
Mac::ExtAddress extAddress;
@@ -190,7 +190,7 @@ void Interface::HandleDiscoveredPeerInfo(const Peer::Info &aInfo)
190190

191191
VerifyOrExit(mInitialized && mEnabled);
192192

193-
SuccessOrExit(ParsePeerInfoTxtData(aInfo, extAddress, extPanId));
193+
SuccessOrExit(aInfo.ParseTxtData(extAddress, extPanId));
194194

195195
VerifyOrExit(extAddress != Get<Mac::Mac>().GetExtAddress());
196196

@@ -238,15 +238,13 @@ void Interface::HandleDiscoveredPeerInfo(const Peer::Info &aInfo)
238238
entry->SetExtPanId(extPanId);
239239
entry->SetSockAddr(aInfo.GetSockAddr());
240240

241-
entry->Log(isNew ? "Added" : "Updated");
241+
entry->Log(isNew ? Peer::kAdded : Peer::kUpdated);
242242

243243
exit:
244244
return;
245245
}
246246

247-
Error Interface::ParsePeerInfoTxtData(const Peer::Info &aInfo,
248-
Mac::ExtAddress &aExtAddress,
249-
MeshCoP::ExtendedPanId &aExtPanId) const
247+
Error Interface::PeerInfo::ParseTxtData(Mac::ExtAddress &aExtAddress, MeshCoP::ExtendedPanId &aExtPanId) const
250248
{
251249
Error error;
252250
Dns::TxtEntry entry;
@@ -256,7 +254,7 @@ Error Interface::ParsePeerInfoTxtData(const Peer::Info &aInfo,
256254

257255
aExtPanId.Clear();
258256

259-
iterator.Init(aInfo.GetTxtData(), aInfo.GetTxtLength());
257+
iterator.Init(mTxtData, mTxtLength);
260258

261259
while ((error = iterator.GetNextEntry(entry)) == kErrorNone)
262260
{
@@ -296,7 +294,7 @@ Error Interface::ParsePeerInfoTxtData(const Peer::Info &aInfo,
296294
return error;
297295
}
298296

299-
Interface::Peer *Interface::GetNewPeerEntry(void)
297+
Peer *Interface::GetNewPeerEntry(void)
300298
{
301299
Peer *peerEntry;
302300

@@ -342,7 +340,7 @@ Interface::Peer *Interface::GetNewPeerEntry(void)
342340

343341
void Interface::RemovePeerEntry(Peer &aEntry)
344342
{
345-
aEntry.Log("Removing");
343+
aEntry.Log(Peer::kRemoving);
346344

347345
if (mPeerList.Remove(aEntry) == kErrorNone)
348346
{
@@ -415,7 +413,7 @@ void Interface::HandleReceived(uint8_t *aBuffer, uint16_t aLength, const Ip6::So
415413
return;
416414
}
417415

418-
const Interface::Peer *Interface::GetNextPeer(PeerIterator &aIterator) const
416+
const Peer *Interface::GetNextPeer(PeerIterator &aIterator) const
419417
{
420418
const Peer *entry = static_cast<const Peer *>(aIterator);
421419

@@ -439,14 +437,6 @@ uint16_t Interface::GetNumberOfPeers(void) const
439437
return count;
440438
}
441439

442-
void Interface::Peer::Log(const char *aAction) const
443-
{
444-
OT_UNUSED_VARIABLE(aAction);
445-
446-
LogInfo("%s peer mac:%s, xpan:%s, %s", aAction, GetExtAddress().ToString().AsCString(),
447-
GetExtPanId().ToString().AsCString(), GetSockAddr().ToString().AsCString());
448-
}
449-
450440
} // namespace Trel
451441
} // namespace ot
452442

src/core/radio/trel_interface.hpp

Lines changed: 10 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,14 @@
4646
#include "common/pool.hpp"
4747
#include "common/tasklet.hpp"
4848
#include "common/time.hpp"
49-
#include "mac/mac_types.hpp"
50-
#include "net/ip6_address.hpp"
51-
#include "net/socket.hpp"
5249
#include "radio/trel_packet.hpp"
53-
#include "thread/mle_types.hpp"
50+
#include "radio/trel_peer.hpp"
5451

5552
namespace ot {
5653
namespace Trel {
5754

5855
class Link;
56+
class Interface;
5957

6058
extern "C" void otPlatTrelHandleReceived(otInstance *aInstance,
6159
uint8_t *aBuffer,
@@ -81,84 +79,6 @@ class Interface : public InstanceLocator
8179
friend void otPlatTrelHandleDiscoveredPeerInfo(otInstance *aInstance, const otPlatTrelPeerInfo *aInfo);
8280

8381
public:
84-
/**
85-
* Represents information about a discovered TREL peer.
86-
*/
87-
class Peer : public otTrelPeer, public LinkedListEntry<Peer>
88-
{
89-
friend class Interface;
90-
friend class LinkedListEntry<Peer>;
91-
friend void otPlatTrelHandleDiscoveredPeerInfo(otInstance *aInstance, const otPlatTrelPeerInfo *aInfo);
92-
93-
public:
94-
/**
95-
* Returns the Extended MAC Address of the discovered TREL peer.
96-
*
97-
* @returns The Extended MAC Address of the TREL peer.
98-
*/
99-
const Mac::ExtAddress &GetExtAddress(void) const { return static_cast<const Mac::ExtAddress &>(mExtAddress); }
100-
101-
/**
102-
* Returns the Extended PAN Identifier of the discovered TREL peer.
103-
*
104-
* @returns The Extended PAN Identifier of the TREL peer.
105-
*/
106-
const MeshCoP::ExtendedPanId &GetExtPanId(void) const
107-
{
108-
return static_cast<const MeshCoP::ExtendedPanId &>(mExtPanId);
109-
}
110-
111-
/**
112-
* Returns the IPv6 socket address of the discovered TREL peer.
113-
*
114-
* @returns The IPv6 socket address of the TREL peer.
115-
*/
116-
const Ip6::SockAddr &GetSockAddr(void) const { return static_cast<const Ip6::SockAddr &>(mSockAddr); }
117-
118-
/**
119-
* Set the IPv6 socket address of the discovered TREL peer.
120-
*
121-
* @param[in] aSockAddr The IPv6 socket address.
122-
*/
123-
void SetSockAddr(const Ip6::SockAddr &aSockAddr) { mSockAddr = aSockAddr; }
124-
125-
/**
126-
* Indicates whether the peer matches a given Extended Address.
127-
*
128-
* @param[in] aExtAddress A Extended Address to match with.
129-
*
130-
* @retval TRUE if the peer matches @p aExtAddress.
131-
* @retval FALSE if the peer does not match @p aExtAddress.
132-
*/
133-
bool Matches(const Mac::ExtAddress &aExtAddress) const { return GetExtAddress() == aExtAddress; }
134-
135-
/**
136-
* Indicates whether the peer matches a given Socket Address.
137-
*
138-
* @param[in] aSockAddr A Socket Address to match with.
139-
*
140-
* @retval TRUE if the peer matches @p aSockAddr.
141-
* @retval FALSE if the peer does not match @p aSockAddr.
142-
*/
143-
bool Matches(const Ip6::SockAddr &aSockAddr) const { return GetSockAddr() == aSockAddr; }
144-
145-
private:
146-
class Info : public otPlatTrelPeerInfo
147-
{
148-
public:
149-
bool IsRemoved(void) const { return mRemoved; }
150-
const uint8_t *GetTxtData(void) const { return mTxtData; }
151-
uint16_t GetTxtLength(void) const { return mTxtLength; }
152-
const Ip6::SockAddr &GetSockAddr(void) const { return static_cast<const Ip6::SockAddr &>(mSockAddr); }
153-
};
154-
155-
void SetExtAddress(const Mac::ExtAddress &aExtAddress) { mExtAddress = aExtAddress; }
156-
void SetExtPanId(const MeshCoP::ExtendedPanId &aExtPanId) { mExtPanId = aExtPanId; }
157-
void Log(const char *aAction) const;
158-
159-
Peer *mNext;
160-
};
161-
16282
/**
16383
* Represents an iterator for iterating over TREL peer table entries.
16484
*/
@@ -288,6 +208,13 @@ class Interface : public InstanceLocator
288208
static const char kTxtRecordExtAddressKey[];
289209
static const char kTxtRecordExtPanIdKey[];
290210

211+
struct PeerInfo : public otPlatTrelPeerInfo
212+
{
213+
bool IsRemoved(void) const { return mRemoved; }
214+
const Ip6::SockAddr &GetSockAddr(void) const { return AsCoreType(&mSockAddr); }
215+
Error ParseTxtData(Mac::ExtAddress &aExtAddress, MeshCoP::ExtendedPanId &aExtPanId) const;
216+
};
217+
291218
explicit Interface(Instance &aInstance);
292219

293220
// Methods used by `Trel::Link`.
@@ -298,12 +225,9 @@ class Interface : public InstanceLocator
298225

299226
// Callbacks from `otPlatTrel`.
300227
void HandleReceived(uint8_t *aBuffer, uint16_t aLength, const Ip6::SockAddr &aSenderAddr);
301-
void HandleDiscoveredPeerInfo(const Peer::Info &aInfo);
228+
void HandleDiscoveredPeerInfo(const PeerInfo &aInfo);
302229

303230
void RegisterService(void);
304-
Error ParsePeerInfoTxtData(const Peer::Info &aInfo,
305-
Mac::ExtAddress &aExtAddress,
306-
MeshCoP::ExtendedPanId &aExtPanId) const;
307231
Peer *GetNewPeerEntry(void);
308232
void RemovePeerEntry(Peer &aEntry);
309233
void ClearPeerList(void);

src/core/radio/trel_link.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,6 @@ class Link : public InstanceLocator
165165
static constexpr uint32_t kAckWaitWindow = 750; // (in msec)
166166
static constexpr uint16_t kFcfFramePending = 1 << 4;
167167

168-
typedef Interface::Peer Peer;
169-
170168
enum State : uint8_t
171169
{
172170
kStateDisabled,

src/core/radio/trel_peer.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright (c) 2025, The OpenThread Authors.
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
* 1. Redistributions of source code must retain the above copyright
8+
* notice, this list of conditions and the following disclaimer.
9+
* 2. Redistributions in binary form must reproduce the above copyright
10+
* notice, this list of conditions and the following disclaimer in the
11+
* documentation and/or other materials provided with the distribution.
12+
* 3. Neither the name of the copyright holder nor the
13+
* names of its contributors may be used to endorse or promote products
14+
* derived from this software without specific prior written permission.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20+
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23+
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
*/
27+
28+
/**
29+
* @file
30+
* This file implements Thread Radio Encapsulation Link (TREL) peer.
31+
*/
32+
33+
#include "trel_peer.hpp"
34+
35+
#if OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE
36+
37+
#include "instance/instance.hpp"
38+
39+
namespace ot {
40+
namespace Trel {
41+
42+
RegisterLogModule("TrelInterface");
43+
44+
#if OT_SHOULD_LOG_AT(OT_LOG_LEVEL_INFO)
45+
46+
void Peer::Log(Action aAction) const
47+
{
48+
LogInfo("%s peer mac:%s, xpan:%s, %s", ActionToString(aAction), GetExtAddress().ToString().AsCString(),
49+
GetExtPanId().ToString().AsCString(), GetSockAddr().ToString().AsCString());
50+
}
51+
52+
const char *Peer::ActionToString(Action aAction)
53+
{
54+
static const char *const kActionStrings[] = {
55+
"Added", // (0) kAdded
56+
"Updated", // (1) kUpdated
57+
"Removing", // (2) kRemoving
58+
};
59+
60+
struct EnumCheck
61+
{
62+
InitEnumValidatorCounter();
63+
ValidateNextEnum(kAdded);
64+
ValidateNextEnum(kUpdated);
65+
ValidateNextEnum(kRemoving);
66+
};
67+
68+
return kActionStrings[aAction];
69+
}
70+
71+
#endif // OT_SHOULD_LOG_AT(OT_LOG_LEVEL_INFO)
72+
73+
} // namespace Trel
74+
} // namespace ot
75+
76+
#endif // #if OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE

0 commit comments

Comments
 (0)