Skip to content

Commit 252918b

Browse files
authored
[tmf] introduce SendResponseWithStateTlv() helper (openthread#13238)
This commit adds `SendResponseWithStateTlv()` to `Tmf::Agent` to streamline sending TMF responses that consist solely of a `StateTlv`. By encapsulating message allocation, TLV appending, and message transmission into a single method, this reduces duplicate boilerplate code across `DatasetManager`, `Leader`, and `NetworkData::Leader`.
1 parent b1d4150 commit 252918b

5 files changed

Lines changed: 33 additions & 30 deletions

File tree

src/core/meshcop/dataset_manager_ftd.cpp

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -202,20 +202,11 @@ Error DatasetManager::HandleSetOrReplace(MgmtCommand aCommand, const Coap::Msg &
202202

203203
void DatasetManager::SendSetOrReplaceResponse(const Coap::Msg &aMsg, StateTlv::State aState)
204204
{
205-
Error error = kErrorNone;
206-
Coap::Message *message;
207-
208-
message = Get<Tmf::Agent>().AllocateAndInitPriorityResponseFor(aMsg.mMessage);
209-
VerifyOrExit(message != nullptr, error = kErrorNoBufs);
210-
211-
SuccessOrExit(error = Tlv::Append<StateTlv>(*message, aState));
212-
213-
SuccessOrExit(error = Get<Tmf::Agent>().SendMessage(*message, aMsg.mMessageInfo));
214-
205+
SuccessOrExit(Get<Tmf::Agent>().SendResponseWithStateTlv(aMsg, aState));
215206
LogInfo("sent dataset set/replace response");
216207

217208
exit:
218-
FreeMessageOnError(message, error);
209+
return;
219210
}
220211

221212
//----------------------------------------------------------------------------------------------------------------------

src/core/meshcop/meshcop_leader.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -158,20 +158,12 @@ template <> void Leader::HandleTmf<kUriLeaderKeepAlive>(Coap::Msg &aMsg)
158158

159159
void Leader::SendKeepAliveResponse(const Coap::Msg &aMsg, StateTlv::State aState)
160160
{
161-
Error error = kErrorNone;
162-
Coap::Message *message;
163-
164-
message = Get<Tmf::Agent>().AllocateAndInitPriorityResponseFor(aMsg.mMessage);
165-
VerifyOrExit(message != nullptr, error = kErrorNoBufs);
166-
167-
SuccessOrExit(error = Tlv::Append<StateTlv>(*message, aState));
168-
169-
SuccessOrExit(error = Get<Tmf::Agent>().SendMessage(*message, aMsg.mMessageInfo));
161+
Error error;
170162

163+
SuccessOrExit(error = Get<Tmf::Agent>().SendResponseWithStateTlv(aMsg, aState));
171164
LogInfo("Sent %s response", UriToString<kUriLeaderKeepAlive>());
172165

173166
exit:
174-
FreeMessageOnError(message, error);
175167
LogWarnOnError(error, "send keep alive response");
176168
}
177169

src/core/thread/network_data_leader_ftd.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -324,18 +324,11 @@ template <> void Leader::HandleTmf<kUriCommissionerGet>(Coap::Msg &aMsg)
324324

325325
void Leader::SendCommissioningSetResponse(const Coap::Msg &aMsg, MeshCoP::StateTlv::State aState)
326326
{
327-
Coap::Message *message = Get<Tmf::Agent>().AllocateAndInitPriorityResponseFor(aMsg.mMessage);
328-
329-
VerifyOrExit(message != nullptr);
330-
SuccessOrExit(Tlv::Append<MeshCoP::StateTlv>(*message, aState));
331-
332-
SuccessOrExit(Get<Tmf::Agent>().SendMessage(*message, aMsg.mMessageInfo));
333-
message = nullptr; // `SendMessage` takes ownership on success
334-
327+
SuccessOrExit(Get<Tmf::Agent>().SendResponseWithStateTlv(aMsg, aState));
335328
LogInfo("Sent %s response", UriToString<kUriCommissionerSet>());
336329

337330
exit:
338-
FreeMessage(message);
331+
return;
339332
}
340333

341334
bool Leader::RlocMatch(uint16_t aFirstRloc16, uint16_t aSecondRloc16, MatchMode aMatchMode)

src/core/thread/tmf.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,22 @@ void Agent::PrepareMessageInfo(Ip6::MessageInfo &aMessageInfo) const
275275
: Get<Mle::Mle>().GetMeshLocalRloc());
276276
}
277277

278+
Error Agent::SendResponseWithStateTlv(const Msg &aRequest, uint8_t aState)
279+
{
280+
Error error = kErrorNone;
281+
Message *message;
282+
283+
message = AllocateAndInitPriorityResponseFor(aRequest.mMessage);
284+
VerifyOrExit(message != nullptr, error = kErrorNoBufs);
285+
286+
SuccessOrExit(error = Tlv::Append<MeshCoP::StateTlv>(*message, aState));
287+
SuccessOrExit(error = SendMessage(*message, aRequest.mMessageInfo));
288+
289+
exit:
290+
FreeMessageOnError(message, error);
291+
return error;
292+
}
293+
278294
uint8_t Agent::PriorityToDscp(Message::Priority aPriority)
279295
{
280296
uint8_t dscp = Ip6::kDscpTmfNormalPriority;

src/core/thread/tmf.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,17 @@ class Agent : public Coap::Coap
223223
*/
224224
Error SendMessageToLeaderAloc(Message &aMessage, ResponseHandler aHandler, void *aContext);
225225

226+
/**
227+
* Sends a TMF response containing a State TLV.
228+
*
229+
* @param[in] aRequest The incoming TMF request message.
230+
* @param[in] aState The state value to append in the State TLV.
231+
*
232+
* @retval kErrorNone Successfully sent the response.
233+
* @retval kErrorNoBufs Insufficient available buffers to allocate the response.
234+
*/
235+
Error SendResponseWithStateTlv(const Msg &aRequest, uint8_t aState);
236+
226237
/**
227238
* Converts a TMF message priority to IPv6 header DSCP value.
228239
*

0 commit comments

Comments
 (0)