@@ -41,15 +41,11 @@ namespace Trel {
4141
4242RegisterLogModule (" TrelInterface" );
4343
44- const char Interface::kTxtRecordExtAddressKey [] = " xa" ;
45- const char Interface::kTxtRecordExtPanIdKey [] = " xp" ;
46-
4744Interface::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
9389exit:
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
233105exit:
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-
287109const Counters *Interface::GetCounters (void ) const { return otPlatTrelGetCounters (&GetInstance ()); }
288110
289111void Interface::ResetCounters (void ) { otPlatTrelResetCounters (&GetInstance ()); }
0 commit comments