[tmf] introduce DeclareTmfResponseHandlerIn helper macros (#12187)

This commit introduces two new macros, `DeclareTmfResponseHandlerIn`
and `DeclareTmfResponseHandlerFullParamIn`, to simplify the
definition of TMF/CoAP response handlers.

These macros generate the boilerplate code for the `static` callback
method within a class and delegate the call to a non-static member
method with the same name. This removes the repetitive pattern of
manually defining the `static` wrapper in each class.

The existing response handlers across various core components are
updated to use the new helper macros.
This commit is contained in:
Abtin Keshavarzian
2025-12-01 10:21:01 -08:00
committed by GitHub
parent 578467b78c
commit c0f4cccc65
19 changed files with 77 additions and 212 deletions
+4 -56
View File
@@ -630,21 +630,8 @@ exit:
return error;
}
void Commissioner::HandleMgmtCommissionerGetResponse(void *aContext,
otMessage *aMessage,
const otMessageInfo *aMessageInfo,
otError aResult)
void Commissioner::HandleMgmtCommissionerGetResponse(Coap::Message *aMessage, Error aResult)
{
static_cast<Commissioner *>(aContext)->HandleMgmtCommissionerGetResponse(AsCoapMessagePtr(aMessage),
AsCoreTypePtr(aMessageInfo), aResult);
}
void Commissioner::HandleMgmtCommissionerGetResponse(Coap::Message *aMessage,
const Ip6::MessageInfo *aMessageInfo,
Error aResult)
{
OT_UNUSED_VARIABLE(aMessageInfo);
VerifyOrExit(aResult == kErrorNone && aMessage->GetCode() == Coap::kCodeChanged);
LogInfo("Received %s response", UriToString<kUriCommissionerGet>());
@@ -701,21 +688,8 @@ exit:
return error;
}
void Commissioner::HandleMgmtCommissionerSetResponse(void *aContext,
otMessage *aMessage,
const otMessageInfo *aMessageInfo,
otError aResult)
void Commissioner::HandleMgmtCommissionerSetResponse(Coap::Message *aMessage, Error aResult)
{
static_cast<Commissioner *>(aContext)->HandleMgmtCommissionerSetResponse(AsCoapMessagePtr(aMessage),
AsCoreTypePtr(aMessageInfo), aResult);
}
void Commissioner::HandleMgmtCommissionerSetResponse(Coap::Message *aMessage,
const Ip6::MessageInfo *aMessageInfo,
Error aResult)
{
OT_UNUSED_VARIABLE(aMessageInfo);
Error error;
uint8_t state;
@@ -754,21 +728,8 @@ exit:
return error;
}
void Commissioner::HandleLeaderPetitionResponse(void *aContext,
otMessage *aMessage,
const otMessageInfo *aMessageInfo,
otError aResult)
void Commissioner::HandleLeaderPetitionResponse(Coap::Message *aMessage, Error aResult)
{
static_cast<Commissioner *>(aContext)->HandleLeaderPetitionResponse(AsCoapMessagePtr(aMessage),
AsCoreTypePtr(aMessageInfo), aResult);
}
void Commissioner::HandleLeaderPetitionResponse(Coap::Message *aMessage,
const Ip6::MessageInfo *aMessageInfo,
Error aResult)
{
OT_UNUSED_VARIABLE(aMessageInfo);
uint8_t state;
bool retransmit = false;
@@ -841,21 +802,8 @@ exit:
LogWarnOnError(error, "send keep alive");
}
void Commissioner::HandleLeaderKeepAliveResponse(void *aContext,
otMessage *aMessage,
const otMessageInfo *aMessageInfo,
otError aResult)
void Commissioner::HandleLeaderKeepAliveResponse(Coap::Message *aMessage, Error aResult)
{
static_cast<Commissioner *>(aContext)->HandleLeaderKeepAliveResponse(AsCoapMessagePtr(aMessage),
AsCoreTypePtr(aMessageInfo), aResult);
}
void Commissioner::HandleLeaderKeepAliveResponse(Coap::Message *aMessage,
const Ip6::MessageInfo *aMessageInfo,
Error aResult)
{
OT_UNUSED_VARIABLE(aMessageInfo);
uint8_t state;
VerifyOrExit(mState == kStateActive);
+4 -24
View File
@@ -393,30 +393,10 @@ private:
void HandleTimer(void);
void HandleJoinerExpirationTimer(void);
static void HandleMgmtCommissionerSetResponse(void *aContext,
otMessage *aMessage,
const otMessageInfo *aMessageInfo,
otError aResult);
void HandleMgmtCommissionerSetResponse(Coap::Message *aMessage,
const Ip6::MessageInfo *aMessageInfo,
Error aResult);
static void HandleMgmtCommissionerGetResponse(void *aContext,
otMessage *aMessage,
const otMessageInfo *aMessageInfo,
otError aResult);
void HandleMgmtCommissionerGetResponse(Coap::Message *aMessage,
const Ip6::MessageInfo *aMessageInfo,
Error aResult);
static void HandleLeaderPetitionResponse(void *aContext,
otMessage *aMessage,
const otMessageInfo *aMessageInfo,
otError aResult);
void HandleLeaderPetitionResponse(Coap::Message *aMessage, const Ip6::MessageInfo *aMessageInfo, otError aResult);
static void HandleLeaderKeepAliveResponse(void *aContext,
otMessage *aMessage,
const otMessageInfo *aMessageInfo,
Error aResult);
void HandleLeaderKeepAliveResponse(Coap::Message *aMessage, const Ip6::MessageInfo *aMessageInfo, Error aResult);
DeclareTmfResponseHandlerIn(Commissioner, HandleMgmtCommissionerSetResponse);
DeclareTmfResponseHandlerIn(Commissioner, HandleMgmtCommissionerGetResponse);
DeclareTmfResponseHandlerIn(Commissioner, HandleLeaderPetitionResponse);
DeclareTmfResponseHandlerIn(Commissioner, HandleLeaderKeepAliveResponse);
static void HandleSecureAgentConnectEvent(Dtls::Session::ConnectEvent aEvent, void *aContext);
void HandleSecureAgentConnectEvent(Dtls::Session::ConnectEvent aEvent);
+1 -12
View File
@@ -472,19 +472,8 @@ exit:
return;
}
void Joiner::HandleJoinerFinalizeResponse(void *aContext,
otMessage *aMessage,
const otMessageInfo *aMessageInfo,
otError aResult)
void Joiner::HandleJoinerFinalizeResponse(Coap::Message *aMessage, Error aResult)
{
static_cast<Joiner *>(aContext)->HandleJoinerFinalizeResponse(AsCoapMessagePtr(aMessage), &AsCoreType(aMessageInfo),
aResult);
}
void Joiner::HandleJoinerFinalizeResponse(Coap::Message *aMessage, const Ip6::MessageInfo *aMessageInfo, Error aResult)
{
OT_UNUSED_VARIABLE(aMessageInfo);
uint8_t state;
VerifyOrExit(mState == kStateConnected && aResult == kErrorNone);
+1 -5
View File
@@ -196,11 +196,7 @@ private:
static void HandleSecureCoapClientConnect(Dtls::Session::ConnectEvent aEvent, void *aContext);
void HandleSecureCoapClientConnect(Dtls::Session::ConnectEvent aEvent);
static void HandleJoinerFinalizeResponse(void *aContext,
otMessage *aMessage,
const otMessageInfo *aMessageInfo,
otError aResult);
void HandleJoinerFinalizeResponse(Coap::Message *aMessage, const Ip6::MessageInfo *aMessageInfo, Error aResult);
DeclareTmfResponseHandlerIn(Joiner, HandleJoinerFinalizeResponse);
template <Uri kUri> void HandleTmf(Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo);
+1 -14
View File
@@ -306,21 +306,8 @@ exit:
return message;
}
void JoinerRouter::HandleJoinerEntrustResponse(void *aContext,
otMessage *aMessage,
const otMessageInfo *aMessageInfo,
otError aResult)
void JoinerRouter::HandleJoinerEntrustResponse(Coap::Message *aMessage, Error aResult)
{
static_cast<JoinerRouter *>(aContext)->HandleJoinerEntrustResponse(AsCoapMessagePtr(aMessage),
AsCoreTypePtr(aMessageInfo), aResult);
}
void JoinerRouter::HandleJoinerEntrustResponse(Coap::Message *aMessage,
const Ip6::MessageInfo *aMessageInfo,
Error aResult)
{
OT_UNUSED_VARIABLE(aMessageInfo);
SendDelayedJoinerEntrust();
VerifyOrExit(aResult == kErrorNone && aMessage != nullptr);
+1 -5
View File
@@ -98,11 +98,7 @@ private:
template <Uri kUri> void HandleTmf(Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo);
static void HandleJoinerEntrustResponse(void *aContext,
otMessage *aMessage,
const otMessageInfo *aMessageInfo,
otError aResult);
void HandleJoinerEntrustResponse(Coap::Message *aMessage, const Ip6::MessageInfo *aMessageInfo, Error aResult);
DeclareTmfResponseHandlerIn(JoinerRouter, HandleJoinerEntrustResponse);
void HandleTimer(void);
+1 -11
View File
@@ -539,18 +539,8 @@ exit:
FreeMessageOnError(message, error);
}
void DuaManager::HandleDuaResponse(void *aContext,
otMessage *aMessage,
const otMessageInfo *aMessageInfo,
otError aResult)
void DuaManager::HandleDuaResponse(Coap::Message *aMessage, Error aResult)
{
static_cast<DuaManager *>(aContext)->HandleDuaResponse(AsCoapMessagePtr(aMessage), AsCoreTypePtr(aMessageInfo),
aResult);
}
void DuaManager::HandleDuaResponse(Coap::Message *aMessage, const Ip6::MessageInfo *aMessageInfo, Error aResult)
{
OT_UNUSED_VARIABLE(aMessageInfo);
Error error;
mIsDuaPending = false;
+1 -5
View File
@@ -219,11 +219,7 @@ private:
void UpdateTimeTickerRegistration(void);
static void HandleDuaResponse(void *aContext,
otMessage *aMessage,
const otMessageInfo *aMessageInfo,
otError aResult);
void HandleDuaResponse(Coap::Message *aMessage, const Ip6::MessageInfo *aMessageInfo, Error aResult);
DeclareTmfResponseHandlerIn(DuaManager, HandleDuaResponse);
template <Uri kUri> void HandleTmf(Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo);
+2 -5
View File
@@ -2414,11 +2414,12 @@ private:
bool ShouldDowngrade(uint8_t aNeighborId, const RouteTlv &aRouteTlv) const;
bool NeighborHasComparableConnectivity(const RouteTlv &aRouteTlv, uint8_t aNeighborId) const;
void HandleAdvertiseTrickleTimer(void);
void HandleAddressSolicitResponse(Coap::Message *aMessage, const Ip6::MessageInfo *aMessageInfo, Error aResult);
void HandleTimeTick(void);
template <Uri kUri> void HandleTmf(Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo);
DeclareTmfResponseHandlerFullParamIn(Mle, HandleAddressSolicitResponse);
#if OPENTHREAD_CONFIG_TMF_PROXY_DUA_ENABLE
void SignalDuaAddressEvent(const Child &aChild, const Ip6::Address &aOldDua) const;
#endif
@@ -2426,10 +2427,6 @@ private:
static bool IsMessageMleSubType(const Message &aMessage);
static bool IsMessageChildUpdateRequest(const Message &aMessage);
static void HandleAdvertiseTrickleTimer(TrickleTimer &aTimer);
static void HandleAddressSolicitResponse(void *aContext,
otMessage *aMessage,
const otMessageInfo *aMessageInfo,
otError aResult);
#if OT_SHOULD_LOG_AT(OT_LOG_LEVEL_INFO)
const char *RouterUpgradeReasonToString(uint8_t aReason);
-9
View File
@@ -3332,15 +3332,6 @@ exit:
LogSendError(kTypeAddressRelease, error);
}
void Mle::HandleAddressSolicitResponse(void *aContext,
otMessage *aMessage,
const otMessageInfo *aMessageInfo,
otError aResult)
{
static_cast<Mle *>(aContext)->HandleAddressSolicitResponse(AsCoapMessagePtr(aMessage), AsCoreTypePtr(aMessageInfo),
aResult);
}
void Mle::HandleAddressSolicitResponse(Coap::Message *aMessage, const Ip6::MessageInfo *aMessageInfo, Error aResult)
{
uint8_t status;
+2 -13
View File
@@ -335,26 +335,15 @@ exit:
return error;
}
void MlrManager::HandleRegisterResponse(void *aContext,
otMessage *aMessage,
const otMessageInfo *aMessageInfo,
otError aResult)
void MlrManager::HandleRegisterResponse(Coap::Message *aMessage, Error aResult)
{
static_cast<MlrManager *>(aContext)->HandleRegisterResponse(AsCoapMessagePtr(aMessage), AsCoreTypePtr(aMessageInfo),
aResult);
}
void MlrManager::HandleRegisterResponse(otMessage *aMessage, const otMessageInfo *aMessageInfo, otError aResult)
{
OT_UNUSED_VARIABLE(aMessageInfo);
uint8_t status;
Error error;
AddressArray failedAddresses;
mRegisterPending = false;
error = ParseMlrResponse(aResult, AsCoapMessagePtr(aMessage), status, failedAddresses);
error = ParseMlrResponse(aResult, aMessage, status, failedAddresses);
mRegisterCallback.InvokeAndClearIfSet(error, status, failedAddresses.GetArrayBuffer(), failedAddresses.GetLength());
}
+2 -5
View File
@@ -54,6 +54,7 @@
#include "net/netif.hpp"
#include "thread/child.hpp"
#include "thread/thread_tlvs.hpp"
#include "thread/tmf.hpp"
namespace ot {
@@ -167,11 +168,7 @@ private:
AddressArray &aFailedAddresses);
#if OPENTHREAD_FTD && OPENTHREAD_CONFIG_COMMISSIONER_ENABLE
static void HandleRegisterResponse(void *aContext,
otMessage *aMessage,
const otMessageInfo *aMessageInfo,
otError aResult);
void HandleRegisterResponse(otMessage *aMessage, const otMessageInfo *aMessageInfo, otError aResult);
DeclareTmfResponseHandlerIn(MlrManager, HandleRegisterResponse);
#endif
#if OPENTHREAD_CONFIG_MLR_ENABLE
+1 -10
View File
@@ -246,19 +246,10 @@ void Notifier::HandleNotifierEvents(Events aEvents)
void Notifier::HandleTimer(void) { SynchronizeServerData(); }
void Notifier::HandleCoapResponse(void *aContext,
otMessage *aMessage,
const otMessageInfo *aMessageInfo,
otError aResult)
void Notifier::HandleCoapResponse(Coap::Message *aMessage, Error aResult)
{
OT_UNUSED_VARIABLE(aMessage);
OT_UNUSED_VARIABLE(aMessageInfo);
static_cast<Notifier *>(aContext)->HandleCoapResponse(aResult);
}
void Notifier::HandleCoapResponse(Error aResult)
{
mWaitingForResponse = false;
switch (aResult)
+5 -8
View File
@@ -40,12 +40,12 @@
#include <openthread/border_router.h>
#include "coap/coap.hpp"
#include "common/message.hpp"
#include "common/non_copyable.hpp"
#include "common/notifier.hpp"
#include "common/tasklet.hpp"
#include "common/time_ticker.hpp"
#include "thread/tmf.hpp"
namespace ot {
namespace NetworkData {
@@ -130,13 +130,10 @@ private:
Error UpdateInconsistentData(void);
#endif
void HandleNotifierEvents(Events aEvents);
void HandleTimer(void);
static void HandleCoapResponse(void *aContext,
otMessage *aMessage,
const otMessageInfo *aMessageInfo,
otError aResult);
void HandleCoapResponse(Error aResult);
void HandleNotifierEvents(Events aEvents);
void HandleTimer(void);
DeclareTmfResponseHandlerIn(Notifier, HandleCoapResponse);
#if OPENTHREAD_CONFIG_BORDER_ROUTER_SIGNAL_NETWORK_DATA_FULL
void HandleNetDataFull(void);
-6
View File
@@ -1220,12 +1220,6 @@ exit:
return error;
}
void Client::HandleGetResponse(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo, otError aResult)
{
static_cast<Client *>(aContext)->HandleGetResponse(AsCoapMessagePtr(aMessage), AsCoreTypePtr(aMessageInfo),
aResult);
}
void Client::HandleGetResponse(Coap::Message *aMessage, const Ip6::MessageInfo *aMessageInfo, Error aResult)
{
SuccessOrExit(aResult);
+1 -5
View File
@@ -372,11 +372,7 @@ private:
Coap::ResponseHandler aHandler = nullptr,
void *aContext = nullptr);
static void HandleGetResponse(void *aContext,
otMessage *aMessage,
const otMessageInfo *aMessageInfo,
otError aResult);
void HandleGetResponse(Coap::Message *aMessage, const Ip6::MessageInfo *aMessageInfo, Error aResult);
DeclareTmfResponseHandlerFullParamIn(Client, HandleGetResponse);
template <Uri kUri> void HandleTmf(Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo);
+47
View File
@@ -38,6 +38,7 @@
#include "coap/coap.hpp"
#include "coap/coap_secure.hpp"
#include "common/as_core_type.hpp"
#include "common/locator.hpp"
namespace ot {
@@ -56,6 +57,52 @@ namespace Tmf {
#define DeclareTmfHandler(Type, kUri) \
template <> void Type::HandleTmf<kUri>(Coap::Message & aMessage, const Ip6::MessageInfo &aMessageInfo)
/**
* Declares a TMF/CoAP response handler method in a given class `Type`.
*
* This macro simplifies the definition of a TMF/CoAP response handler. It defines a `static` handler method which
* can be used as a callback function pointer (`Coap::ResponseHandler`). The `static` handler acts as a wrapper,
* casting the `aContext` pointer back to a `Type` object and invoking its member method with the same `MethodName`.
*
* This macro is intended for cases where the response handler method does not require the `aMessageInfo`.
*
* The `Type` class MUST implement the following member method which will be invoked by the `static` handler:
*
* void MethodName(Coap::Message *aMessage, Error aResult);
*
* @param[in] Type The class `Type` in which the TMF response handler is declared.
* @param[in] MethodName The handler method name.
*/
#define DeclareTmfResponseHandlerIn(Type, MethodName) \
static void MethodName(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo, otError aResult) \
{ \
OT_UNUSED_VARIABLE(aMessageInfo); \
static_cast<Type *>(aContext)->MethodName(AsCoapMessagePtr(aMessage), aResult); \
} \
\
void MethodName(Coap::Message *aMessage, Error aResult)
/**
* Declares a TMF/CoAP response handler with access to `MessageInfo` in a given class `Type`.
*
* This macro is a variant of `DeclareTmfResponseHandlerIn` and is intended for cases where the response handler needs
* access to the full parameters including `Ip6::MessageInfo`.
*
* The `Type` class MUST implement the following member method which will be invoked by the `static` handler:
*
* void MethodName(Coap::Message *aMessage, const Ip6::MessageInfo *aMessageInfo, Error aResult);
*
* @param[in] Type The class `Type` in which the TMF response handler is declared.
* @param[in] MethodName The handler method name.
*/
#define DeclareTmfResponseHandlerFullParamIn(Type, MethodName) \
static void MethodName(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo, otError aResult) \
{ \
static_cast<Type *>(aContext)->MethodName(AsCoapMessagePtr(aMessage), AsCoreTypePtr(aMessageInfo), aResult); \
} \
\
void MethodName(Coap::Message *aMessage, const Ip6::MessageInfo *aMessageInfo, Error aResult)
constexpr uint16_t kUdpPort = 61631; ///< TMF UDP Port
typedef Coap::Message Message; ///< A TMF message.
+1 -12
View File
@@ -113,19 +113,8 @@ exit:
return error;
}
void MeshDiag::HandleDiagGetResponse(void *aContext,
otMessage *aMessage,
const otMessageInfo *aMessageInfo,
otError aResult)
void MeshDiag::HandleDiagGetResponse(Coap::Message *aMessage, Error aResult)
{
static_cast<MeshDiag *>(aContext)->HandleDiagGetResponse(AsCoapMessagePtr(aMessage), AsCoreTypePtr(aMessageInfo),
aResult);
}
void MeshDiag::HandleDiagGetResponse(Coap::Message *aMessage, const Ip6::MessageInfo *aMessageInfo, Error aResult)
{
OT_UNUSED_VARIABLE(aMessageInfo);
Error error;
RouterInfo routerInfo;
Ip6AddrIterator ip6AddrIterator;
+2 -7
View File
@@ -44,7 +44,6 @@
#include <openthread/mesh_diag.h>
#include "coap/coap.hpp"
#include "common/callback.hpp"
#include "common/locator.hpp"
#include "common/message.hpp"
@@ -52,6 +51,7 @@
#include "net/ip6_address.hpp"
#include "thread/network_diagnostic.hpp"
#include "thread/network_diagnostic_tlvs.hpp"
#include "thread/tmf.hpp"
struct otMeshDiagIp6AddrIterator
{
@@ -306,12 +306,7 @@ private:
bool ProcessChildrenIp6AddrsAnswer(Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo);
bool ProcessRouterNeighborTableAnswer(Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo);
void HandleDiagGetResponse(Coap::Message *aMessage, const Ip6::MessageInfo *aMessageInfo, Error aResult);
static void HandleDiagGetResponse(void *aContext,
otMessage *aMessage,
const otMessageInfo *aMessageInfo,
otError aResult);
DeclareTmfResponseHandlerIn(MeshDiag, HandleDiagGetResponse);
using TimeoutTimer = TimerMilliIn<MeshDiag, &MeshDiag::HandleTimer>;