Skip to content

Commit 6961ad1

Browse files
authored
[trel] introduce Trel::PeerDiscoverer class (openthread#11493)
This commit introduces `PeerDiscoverer` as a class responsible for TREL peer discovery, separating this logic from `Trel::Interface`. The new class currently handles the preparation of TXT data, calling the platform API to register the TREL service, and handling callbacks from the platform layer with newly discovered or updated peer information. This separation helps with TREL module organization and enables future extensions, such as performing discovery using the native OpenThread mDNS module or the platform-specific DNS-SD (`otPlatDnssd`) module, in addition to the existing approach where discovery is delegated to the platform layer.
1 parent 8244bc7 commit 6961ad1

10 files changed

Lines changed: 403 additions & 248 deletions

src/core/BUILD.gn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,8 @@ openthread_core_files = [
619619
"radio/trel_packet.hpp",
620620
"radio/trel_peer.cpp",
621621
"radio/trel_peer.hpp",
622+
"radio/trel_peer_discoverer.cpp",
623+
"radio/trel_peer_discoverer.hpp",
622624
"thread/address_resolver.cpp",
623625
"thread/address_resolver.hpp",
624626
"thread/announce_begin_server.cpp",

src/core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ set(COMMON_SOURCES
204204
radio/trel_link.cpp
205205
radio/trel_packet.cpp
206206
radio/trel_peer.cpp
207+
radio/trel_peer_discoverer.cpp
207208
thread/address_resolver.cpp
208209
thread/announce_begin_server.cpp
209210
thread/announce_sender.cpp

src/core/instance/instance.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,8 @@ template <> inline Trel::Link &Instance::Get(void) { return mMac.mLinks.mTrel; }
801801
template <> inline Trel::Interface &Instance::Get(void) { return mMac.mLinks.mTrel.mInterface; }
802802

803803
template <> inline Trel::PeerTable &Instance::Get(void) { return mMac.mLinks.mTrel.mPeerTable; }
804+
805+
template <> inline Trel::PeerDiscoverer &Instance::Get(void) { return mMac.mLinks.mTrel.mPeerDiscoverer; }
804806
#endif
805807

806808
#if OPENTHREAD_CONFIG_MAC_FILTER_ENABLE

src/core/radio/trel_interface.cpp

Lines changed: 3 additions & 181 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,11 @@ namespace Trel {
4141

4242
RegisterLogModule("TrelInterface");
4343

44-
const char Interface::kTxtRecordExtAddressKey[] = "xa";
45-
const char Interface::kTxtRecordExtPanIdKey[] = "xp";
46-
4744
Interface::Interface(Instance &aInstance)
4845
: InstanceLocator(aInstance)
4946
, mInitialized(false)
5047
, mEnabled(false)
5148
, mFiltered(false)
52-
, mRegisterServiceTask(aInstance)
5349
{
5450
}
5551

@@ -86,9 +82,9 @@ void Interface::Enable(void)
8682
VerifyOrExit(mInitialized);
8783

8884
otPlatTrelEnable(&GetInstance(), &mUdpPort);
85+
Get<PeerDiscoverer>().Start();
8986

9087
LogInfo("Enabled interface, local port:%u", mUdpPort);
91-
mRegisterServiceTask.Post();
9288

9389
exit:
9490
return;
@@ -102,188 +98,14 @@ void Interface::Disable(void)
10298
VerifyOrExit(mInitialized);
10399

104100
otPlatTrelDisable(&GetInstance());
105-
Get<PeerTable>().Clear();
106-
LogDebg("Disabled interface");
107-
108-
exit:
109-
return;
110-
}
111-
112-
void Interface::NotifyPeerSocketAddressDifference(const Ip6::SockAddr &aPeerSockAddr, const Ip6::SockAddr &aRxSockAddr)
113-
{
114-
otPlatTrelNotifyPeerSocketAddressDifference(&GetInstance(), &aPeerSockAddr, &aRxSockAddr);
115-
}
116-
117-
void Interface::HandleExtAddressChange(void)
118-
{
119-
VerifyOrExit(mInitialized && mEnabled);
120-
LogDebg("Extended Address changed, re-registering DNS-SD service");
121-
mRegisterServiceTask.Post();
122-
123-
exit:
124-
return;
125-
}
126-
127-
void Interface::HandleExtPanIdChange(void)
128-
{
129-
VerifyOrExit(mInitialized && mEnabled);
130-
LogDebg("Extended PAN ID changed, re-registering DNS-SD service");
131-
mRegisterServiceTask.Post();
132-
133-
exit:
134-
return;
135-
}
136-
137-
void Interface::RegisterService(void)
138-
{
139-
// TXT data consists of two entries: the length fields, the
140-
// "key" string, "=" char, and binary representation of the MAC
141-
// or Extended PAN ID values.
142-
static constexpr uint8_t kTxtDataSize =
143-
/* ExtAddr */ sizeof(uint8_t) + sizeof(kTxtRecordExtAddressKey) - 1 + sizeof(char) + sizeof(Mac::ExtAddress) +
144-
/* ExtPanId */ sizeof(uint8_t) + sizeof(kTxtRecordExtPanIdKey) - 1 + sizeof(char) +
145-
sizeof(MeshCoP::ExtendedPanId);
146-
147-
uint8_t txtData[kTxtDataSize];
148-
Dns::TxtDataEncoder encoder(txtData, sizeof(txtData));
149-
150-
VerifyOrExit(mInitialized && mEnabled);
151-
152-
SuccessOrAssert(encoder.AppendEntry(kTxtRecordExtAddressKey, Get<Mac::Mac>().GetExtAddress()));
153-
SuccessOrAssert(encoder.AppendEntry(kTxtRecordExtPanIdKey, Get<MeshCoP::ExtendedPanIdManager>().GetExtPanId()));
154-
155-
LogInfo("Registering DNS-SD service: port:%u, txt:\"%s=%s, %s=%s\"", mUdpPort, kTxtRecordExtAddressKey,
156-
Get<Mac::Mac>().GetExtAddress().ToString().AsCString(), kTxtRecordExtPanIdKey,
157-
Get<MeshCoP::ExtendedPanIdManager>().GetExtPanId().ToString().AsCString());
158-
159-
otPlatTrelRegisterService(&GetInstance(), mUdpPort, txtData, static_cast<uint8_t>(encoder.GetLength()));
160-
161-
exit:
162-
return;
163-
}
164-
165-
extern "C" void otPlatTrelHandleDiscoveredPeerInfo(otInstance *aInstance, const otPlatTrelPeerInfo *aInfo)
166-
{
167-
Instance &instance = AsCoreType(aInstance);
168-
169-
VerifyOrExit(instance.IsInitialized());
170-
instance.Get<Interface>().HandleDiscoveredPeerInfo(*static_cast<const Interface::PeerInfo *>(aInfo));
171-
172-
exit:
173-
return;
174-
}
175-
176-
void Interface::HandleDiscoveredPeerInfo(const PeerInfo &aInfo)
177-
{
178-
Peer *peer;
179-
Mac::ExtAddress extAddress;
180-
MeshCoP::ExtendedPanId extPanId;
181-
bool isNew = false;
182-
183-
VerifyOrExit(mInitialized && mEnabled);
101+
Get<PeerDiscoverer>().Stop();
184102

185-
SuccessOrExit(aInfo.ParseTxtData(extAddress, extPanId));
186-
187-
VerifyOrExit(extAddress != Get<Mac::Mac>().GetExtAddress());
188-
189-
if (aInfo.IsRemoved())
190-
{
191-
Get<PeerTable>().RemoveAndFreeAllMatching(extAddress);
192-
ExitNow();
193-
}
194-
195-
// It is a new entry or an update to an existing entry. First
196-
// check whether we have an existing entry that matches the same
197-
// socket address, and remove it if it is associated with a
198-
// different Extended MAC address. This ensures that we do not
199-
// keep stale entries in the peer table.
200-
201-
peer = Get<PeerTable>().FindMatching(aInfo.GetSockAddr());
202-
203-
if ((peer != nullptr) && !peer->Matches(extAddress))
204-
{
205-
Get<PeerTable>().RemoveMatching(aInfo.GetSockAddr());
206-
peer = nullptr;
207-
}
208-
209-
if (peer == nullptr)
210-
{
211-
peer = Get<PeerTable>().FindMatching(extAddress);
212-
}
213-
214-
if (peer == nullptr)
215-
{
216-
peer = Get<PeerTable>().AllocateAndAddNewPeer();
217-
VerifyOrExit(peer != nullptr);
218-
219-
peer->SetExtAddress(extAddress);
220-
isNew = true;
221-
}
222-
223-
if (!isNew)
224-
{
225-
VerifyOrExit((peer->GetExtPanId() != extPanId) || (peer->GetSockAddr() != aInfo.GetSockAddr()));
226-
}
227-
228-
peer->SetExtPanId(extPanId);
229-
peer->SetSockAddr(aInfo.GetSockAddr());
230-
231-
peer->Log(isNew ? Peer::kAdded : Peer::kUpdated);
103+
LogDebg("Disabled interface");
232104

233105
exit:
234106
return;
235107
}
236108

237-
Error Interface::PeerInfo::ParseTxtData(Mac::ExtAddress &aExtAddress, MeshCoP::ExtendedPanId &aExtPanId) const
238-
{
239-
Error error;
240-
Dns::TxtEntry entry;
241-
Dns::TxtEntry::Iterator iterator;
242-
bool parsedExtAddress = false;
243-
bool parsedExtPanId = false;
244-
245-
aExtPanId.Clear();
246-
247-
iterator.Init(mTxtData, mTxtLength);
248-
249-
while ((error = iterator.GetNextEntry(entry)) == kErrorNone)
250-
{
251-
// If the TXT data happens to have entries with key longer
252-
// than `kMaxIterKeyLength`, `mKey` would be `nullptr` and full
253-
// entry would be placed in `mValue`. We skip over such
254-
// entries.
255-
if (entry.mKey == nullptr)
256-
{
257-
continue;
258-
}
259-
260-
if (StringMatch(entry.mKey, kTxtRecordExtAddressKey))
261-
{
262-
VerifyOrExit(!parsedExtAddress, error = kErrorParse);
263-
VerifyOrExit(entry.mValueLength >= sizeof(Mac::ExtAddress), error = kErrorParse);
264-
aExtAddress.Set(entry.mValue);
265-
parsedExtAddress = true;
266-
}
267-
else if (StringMatch(entry.mKey, kTxtRecordExtPanIdKey))
268-
{
269-
VerifyOrExit(!parsedExtPanId, error = kErrorParse);
270-
VerifyOrExit(entry.mValueLength >= sizeof(MeshCoP::ExtendedPanId), error = kErrorParse);
271-
memcpy(aExtPanId.m8, entry.mValue, sizeof(MeshCoP::ExtendedPanId));
272-
parsedExtPanId = true;
273-
}
274-
275-
// Skip over and ignore any unknown keys.
276-
}
277-
278-
VerifyOrExit(error == kErrorNotFound);
279-
error = kErrorNone;
280-
281-
VerifyOrExit(parsedExtAddress && parsedExtPanId, error = kErrorParse);
282-
283-
exit:
284-
return error;
285-
}
286-
287109
const Counters *Interface::GetCounters(void) const { return otPlatTrelGetCounters(&GetInstance()); }
288110

289111
void Interface::ResetCounters(void) { otPlatTrelResetCounters(&GetInstance()); }

src/core/radio/trel_interface.hpp

Lines changed: 6 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -41,26 +41,19 @@
4141
#include <openthread/trel.h>
4242
#include <openthread/platform/trel.h>
4343

44-
#include "common/linked_list.hpp"
4544
#include "common/locator.hpp"
46-
#include "common/pool.hpp"
47-
#include "common/tasklet.hpp"
48-
#include "common/time.hpp"
45+
#include "net/socket.hpp"
4946
#include "radio/trel_packet.hpp"
50-
#include "radio/trel_peer.hpp"
5147

5248
namespace ot {
5349
namespace Trel {
5450

5551
class Link;
56-
class Interface;
5752

5853
extern "C" void otPlatTrelHandleReceived(otInstance *aInstance,
5954
uint8_t *aBuffer,
6055
uint16_t aLength,
6156
const otSockAddr *aSenderAddr);
62-
extern "C" void otPlatTrelHandleDiscoveredPeerInfo(otInstance *aInstance, const otPlatTrelPeerInfo *aInfo);
63-
6457
/**
6558
* Represents a group of TREL counters.
6659
*/
@@ -76,7 +69,6 @@ class Interface : public InstanceLocator
7669
uint8_t *aBuffer,
7770
uint16_t aLength,
7871
const otSockAddr *aSenderAddr);
79-
friend void otPlatTrelHandleDiscoveredPeerInfo(otInstance *aInstance, const otPlatTrelPeerInfo *aInfo);
8072

8173
public:
8274
/**
@@ -152,54 +144,21 @@ class Interface : public InstanceLocator
152144
*/
153145
uint16_t GetUdpPort(void) const { return mUdpPort; }
154146

155-
/**
156-
* Notifies platform that a TREL packet is received from a peer using a different socket address than the one
157-
* reported earlier.
158-
*
159-
* @param[in] aPeerSockAddr The previously reported peer sock addr.
160-
* @param[in] aRxSockAddr The address of received packet from the same peer.
161-
*/
162-
void NotifyPeerSocketAddressDifference(const Ip6::SockAddr &aPeerSockAddr, const Ip6::SockAddr &aRxSockAddr);
163-
164147
private:
165-
#if OPENTHREAD_CONFIG_TREL_PEER_TABLE_SIZE != 0
166-
static constexpr uint16_t kPeerPoolSize = OPENTHREAD_CONFIG_TREL_PEER_TABLE_SIZE;
167-
#else
168-
static constexpr uint16_t kExtraEntries = 32;
169-
static constexpr uint16_t kPeerPoolSize = Mle::kMaxRouters + Mle::kMaxChildren + kExtraEntries;
170-
#endif
171-
static const char kTxtRecordExtAddressKey[];
172-
static const char kTxtRecordExtPanIdKey[];
173-
174-
struct PeerInfo : public otPlatTrelPeerInfo
175-
{
176-
bool IsRemoved(void) const { return mRemoved; }
177-
const Ip6::SockAddr &GetSockAddr(void) const { return AsCoreType(&mSockAddr); }
178-
Error ParseTxtData(Mac::ExtAddress &aExtAddress, MeshCoP::ExtendedPanId &aExtPanId) const;
179-
};
180-
181148
explicit Interface(Instance &aInstance);
182149

183150
// Methods used by `Trel::Link`.
184151
void Init(void);
185-
void HandleExtAddressChange(void);
186-
void HandleExtPanIdChange(void);
187152
Error Send(const Packet &aPacket, bool aIsDiscovery = false);
188153

189154
// Callbacks from `otPlatTrel`.
190155
void HandleReceived(uint8_t *aBuffer, uint16_t aLength, const Ip6::SockAddr &aSenderAddr);
191-
void HandleDiscoveredPeerInfo(const PeerInfo &aInfo);
192-
193-
void RegisterService(void);
194-
195-
using RegisterServiceTask = TaskletIn<Interface, &Interface::RegisterService>;
196156

197-
bool mInitialized : 1;
198-
bool mEnabled : 1;
199-
bool mFiltered : 1;
200-
RegisterServiceTask mRegisterServiceTask;
201-
uint16_t mUdpPort;
202-
Packet mRxPacket;
157+
bool mInitialized : 1;
158+
bool mEnabled : 1;
159+
bool mFiltered : 1;
160+
uint16_t mUdpPort;
161+
Packet mRxPacket;
203162
};
204163

205164
} // namespace Trel

src/core/radio/trel_link.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Link::Link(Instance &aInstance)
5151
, mTimer(aInstance)
5252
, mInterface(aInstance)
5353
, mPeerTable(aInstance)
54+
, mPeerDiscoverer(aInstance)
5455
{
5556
ClearAllBytes(mTxFrame);
5657
ClearAllBytes(mRxFrame);
@@ -409,7 +410,7 @@ void Link::CheckPeerAddrOnRxSuccess(PeerSockAddrUpdateMode aMode)
409410
mRxPacketPeer->SetSockAddr(mRxPacketSenderAddr);
410411
}
411412

412-
Get<Interface>().NotifyPeerSocketAddressDifference(prevSockAddr, mRxPacketSenderAddr);
413+
mPeerDiscoverer.NotifyPeerSocketAddressDifference(prevSockAddr, mRxPacketSenderAddr);
413414

414415
exit:
415416
mRxPacketPeer = nullptr;
@@ -496,7 +497,7 @@ void Link::HandleNotifierEvents(Events aEvents)
496497
{
497498
if (aEvents.Contains(kEventThreadExtPanIdChanged))
498499
{
499-
mInterface.HandleExtPanIdChange();
500+
mPeerDiscoverer.HandleExtPanIdChange();
500501
}
501502
}
502503

0 commit comments

Comments
 (0)