mirror of
https://github.com/espressif/openthread.git
synced 2026-08-21 09:59:52 +00:00
[coap] update Coap/CoapSecure to use Context tracking Timer/Tasklet (#3364)
This commit simplifies `ResponseQueue`, `CoapBase`, `CoapSecure` to use `TimerMilliContext` and `TaskeltContext`. This change removes the need for sub-classes to provide the timer and tasklet callback handlers.
This commit is contained in:
committed by
Jonathan Hui
parent
8d4b424418
commit
2610b9d5e0
+17
-29
@@ -48,16 +48,14 @@
|
||||
namespace ot {
|
||||
namespace Coap {
|
||||
|
||||
CoapBase::CoapBase(Instance & aInstance,
|
||||
Timer::Handler aRetransmissionTimerHandler,
|
||||
Timer::Handler aResponsesQueueTimerHandler)
|
||||
CoapBase::CoapBase(Instance &aInstance)
|
||||
: InstanceLocator(aInstance)
|
||||
, mSocket(aInstance.GetThreadNetif().GetIp6().GetUdp())
|
||||
, mRetransmissionTimer(aInstance, aRetransmissionTimerHandler, this)
|
||||
, mRetransmissionTimer(aInstance, &CoapBase::HandleRetransmissionTimer, this)
|
||||
, mResources(NULL)
|
||||
, mContext(NULL)
|
||||
, mInterceptor(NULL)
|
||||
, mResponsesQueue(aInstance, aResponsesQueueTimerHandler, this)
|
||||
, mResponsesQueue(aInstance)
|
||||
, mDefaultHandler(NULL)
|
||||
, mDefaultHandlerContext(NULL)
|
||||
{
|
||||
@@ -292,6 +290,11 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
void CoapBase::HandleRetransmissionTimer(Timer &aTimer)
|
||||
{
|
||||
static_cast<CoapBase *>(static_cast<TimerMilliContext &>(aTimer).GetContext())->HandleRetransmissionTimer();
|
||||
}
|
||||
|
||||
void CoapBase::HandleRetransmissionTimer(void)
|
||||
{
|
||||
uint32_t now = TimerMilli::GetNow();
|
||||
@@ -756,9 +759,9 @@ CoapMetadata::CoapMetadata(bool aConfirmable,
|
||||
mConfirmable = aConfirmable;
|
||||
}
|
||||
|
||||
ResponsesQueue::ResponsesQueue(Instance &aInstance, Timer::Handler aHandler, void *aContext)
|
||||
ResponsesQueue::ResponsesQueue(Instance &aInstance)
|
||||
: mQueue()
|
||||
, mTimer(aInstance, aHandler, aContext)
|
||||
, mTimer(aInstance, &ResponsesQueue::HandleTimer, this)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -895,6 +898,11 @@ void ResponsesQueue::DequeueAllResponses(void)
|
||||
}
|
||||
}
|
||||
|
||||
void ResponsesQueue::HandleTimer(Timer &aTimer)
|
||||
{
|
||||
static_cast<ResponsesQueue *>(static_cast<TimerMilliContext &>(aTimer).GetContext())->HandleTimer();
|
||||
}
|
||||
|
||||
void ResponsesQueue::HandleTimer(void)
|
||||
{
|
||||
Message * message;
|
||||
@@ -924,37 +932,17 @@ uint32_t EnqueuedResponseHeader::GetRemainingTime(void) const
|
||||
}
|
||||
|
||||
Coap::Coap(Instance &aInstance)
|
||||
: CoapBase(aInstance, &Coap::HandleRetransmissionTimer, &Coap::HandleResponsesQueueTimer)
|
||||
: CoapBase(aInstance)
|
||||
{
|
||||
}
|
||||
|
||||
void Coap::HandleRetransmissionTimer(Timer &aTimer)
|
||||
{
|
||||
aTimer.GetOwner<Coap>().CoapBase::HandleRetransmissionTimer();
|
||||
}
|
||||
|
||||
void Coap::HandleResponsesQueueTimer(Timer &aTimer)
|
||||
{
|
||||
aTimer.GetOwner<Coap>().CoapBase::HandleResponsesQueueTimer();
|
||||
}
|
||||
|
||||
#if OPENTHREAD_ENABLE_APPLICATION_COAP
|
||||
|
||||
ApplicationCoap::ApplicationCoap(Instance &aInstance)
|
||||
: CoapBase(aInstance, &ApplicationCoap::HandleRetransmissionTimer, &ApplicationCoap::HandleResponsesQueueTimer)
|
||||
: CoapBase(aInstance)
|
||||
{
|
||||
}
|
||||
|
||||
void ApplicationCoap::HandleRetransmissionTimer(Timer &aTimer)
|
||||
{
|
||||
aTimer.GetOwner<ApplicationCoap>().CoapBase::HandleRetransmissionTimer();
|
||||
}
|
||||
|
||||
void ApplicationCoap::HandleResponsesQueueTimer(Timer &aTimer)
|
||||
{
|
||||
aTimer.GetOwner<ApplicationCoap>().CoapBase::HandleResponsesQueueTimer();
|
||||
}
|
||||
|
||||
#endif // OPENTHREAD_ENABLE_APPLICATION_COAP
|
||||
|
||||
} // namespace Coap
|
||||
|
||||
+14
-49
@@ -352,11 +352,9 @@ public:
|
||||
* Default class constructor.
|
||||
*
|
||||
* @param[in] aInstance A reference to the OpenThread instance.
|
||||
* @param[in] aHandler A timer handler provided by owner of `RespponseQueue`.
|
||||
* @param[in] aContext A pointer to arbitrary context information (used along with timer handler).
|
||||
*
|
||||
*/
|
||||
ResponsesQueue(Instance &aInstance, Timer::Handler aHandler, void *aContext);
|
||||
explicit ResponsesQueue(Instance &aInstance);
|
||||
|
||||
/**
|
||||
* Add given response to the cache.
|
||||
@@ -420,15 +418,6 @@ public:
|
||||
*/
|
||||
const MessageQueue &GetResponses(void) const { return mQueue; }
|
||||
|
||||
/**
|
||||
* Callback handler for timer.
|
||||
*
|
||||
* This method must be invoked by the owner of `ResponsesQueue` instance when the timer expires from the `aHandler`
|
||||
* callback function provided in the constructor.
|
||||
*
|
||||
*/
|
||||
void HandleTimer(void);
|
||||
|
||||
private:
|
||||
enum
|
||||
{
|
||||
@@ -441,8 +430,11 @@ private:
|
||||
aMessage.Free();
|
||||
}
|
||||
|
||||
MessageQueue mQueue;
|
||||
TimerMilli mTimer;
|
||||
static void HandleTimer(Timer &aTimer);
|
||||
void HandleTimer(void);
|
||||
|
||||
MessageQueue mQueue;
|
||||
TimerMilliContext mTimer;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -674,32 +666,10 @@ protected:
|
||||
/**
|
||||
* This constructor initializes the object.
|
||||
*
|
||||
* @param[in] aInstance A reference to the OpenThread instance.
|
||||
* @param[in] aRetransmissionTimerHandler A timer handler provided by sub-class for `mRetranmissionTimer`.
|
||||
* @param[in] aResponsesQueueTimerHandler A timer handler provided by sub-class for `mReponsesQueue` timer.
|
||||
* @param[in] aInstance A reference to the OpenThread instance.
|
||||
*
|
||||
*/
|
||||
CoapBase(Instance & aInstance,
|
||||
Timer::Handler aRetransmissionTimerHandler,
|
||||
Timer::Handler aResponsesQueueTimerHandler);
|
||||
|
||||
/**
|
||||
* Retransmission timer handler.
|
||||
*
|
||||
* This method must be invoked by sub-class when the timer expires from the `aRetransmissionTimerHandler`
|
||||
* callback function provided in the constructor.
|
||||
*
|
||||
*/
|
||||
void HandleRetransmissionTimer(void);
|
||||
|
||||
/**
|
||||
* `ResponsesQueue` timer handler.
|
||||
*
|
||||
* This method must be invoked by sub-class when the timer expires from the `aResponsesQueueTimerHandler`
|
||||
* callback function provided in the constructor.
|
||||
*
|
||||
*/
|
||||
void HandleResponsesQueueTimer(void) { mResponsesQueue.HandleTimer(); }
|
||||
explicit CoapBase(Instance &aInstance);
|
||||
|
||||
/**
|
||||
* This method sends a message.
|
||||
@@ -722,6 +692,9 @@ protected:
|
||||
Ip6::UdpSocket mSocket;
|
||||
|
||||
private:
|
||||
static void HandleRetransmissionTimer(Timer &aTimer);
|
||||
void HandleRetransmissionTimer(void);
|
||||
|
||||
static void HandleUdpReceive(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo);
|
||||
|
||||
Message *CopyAndEnqueueMessage(const Message &aMessage, uint16_t aCopyLength, const CoapMetadata &aCoapMetadata);
|
||||
@@ -743,9 +716,9 @@ private:
|
||||
otError SendCopy(const Message &aMessage, const Ip6::MessageInfo &aMessageInfo);
|
||||
otError SendEmptyMessage(Header::Type aType, const Header &aRequestHeader, const Ip6::MessageInfo &aMessageInfo);
|
||||
|
||||
MessageQueue mPendingRequests;
|
||||
uint16_t mMessageId;
|
||||
TimerMilli mRetransmissionTimer;
|
||||
MessageQueue mPendingRequests;
|
||||
uint16_t mMessageId;
|
||||
TimerMilliContext mRetransmissionTimer;
|
||||
|
||||
Resource *mResources;
|
||||
|
||||
@@ -771,10 +744,6 @@ public:
|
||||
*
|
||||
*/
|
||||
explicit Coap(Instance &aInstance);
|
||||
|
||||
private:
|
||||
static void HandleRetransmissionTimer(Timer &aTimer);
|
||||
static void HandleResponsesQueueTimer(Timer &aTimer);
|
||||
};
|
||||
|
||||
#if OPENTHREAD_ENABLE_APPLICATION_COAP
|
||||
@@ -793,10 +762,6 @@ public:
|
||||
*
|
||||
*/
|
||||
explicit ApplicationCoap(Instance &aInstance);
|
||||
|
||||
private:
|
||||
static void HandleRetransmissionTimer(Timer &aTimer);
|
||||
static void HandleResponsesQueueTimer(Timer &aTimer);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -47,35 +47,18 @@
|
||||
namespace ot {
|
||||
namespace Coap {
|
||||
|
||||
CoapSecure::CoapSecure(Instance &aInstance)
|
||||
: CoapBase(aInstance, &CoapSecure::HandleRetransmissionTimer, &CoapSecure::HandleResponsesQueueTimer)
|
||||
CoapSecure::CoapSecure(Instance &aInstance, bool aLayerTwoSecurity)
|
||||
: CoapBase(aInstance)
|
||||
, mConnectedCallback(NULL)
|
||||
, mConnectedContext(NULL)
|
||||
, mTransportCallback(NULL)
|
||||
, mTransportContext(NULL)
|
||||
, mTransmitQueue()
|
||||
, mTransmitTask(aInstance, &CoapSecure::HandleTransmit, this)
|
||||
, mLayerTwoSecurity(false)
|
||||
, mLayerTwoSecurity(aLayerTwoSecurity)
|
||||
{
|
||||
}
|
||||
|
||||
#if OPENTHREAD_ENABLE_APPLICATION_COAP_SECURE
|
||||
CoapSecure::CoapSecure(Instance & aInstance,
|
||||
Tasklet::Handler aHandleTransmit,
|
||||
Timer::Handler aRetransmissionTimer,
|
||||
Timer::Handler aResponsesQueueTimer)
|
||||
: CoapBase(aInstance, aRetransmissionTimer, aResponsesQueueTimer)
|
||||
, mConnectedCallback(NULL)
|
||||
, mConnectedContext(NULL)
|
||||
, mTransportCallback(NULL)
|
||||
, mTransportContext(NULL)
|
||||
, mTransmitQueue()
|
||||
, mTransmitTask(aInstance, aHandleTransmit, this)
|
||||
, mLayerTwoSecurity(true)
|
||||
{
|
||||
}
|
||||
#endif // OPENTHREAD_ENABLE_APPLICATION_COAP_SECURE
|
||||
|
||||
otError CoapSecure::Start(uint16_t aPort, TransportCallback aCallback, void *aContext)
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
@@ -369,7 +352,7 @@ exit:
|
||||
|
||||
void CoapSecure::HandleTransmit(Tasklet &aTasklet)
|
||||
{
|
||||
aTasklet.GetOwner<CoapSecure>().HandleTransmit();
|
||||
static_cast<CoapSecure *>(static_cast<TaskletContext &>(aTasklet).GetContext())->HandleTransmit();
|
||||
}
|
||||
|
||||
void CoapSecure::HandleTransmit(void)
|
||||
@@ -399,41 +382,13 @@ exit:
|
||||
}
|
||||
}
|
||||
|
||||
void CoapSecure::HandleRetransmissionTimer(Timer &aTimer)
|
||||
{
|
||||
aTimer.GetOwner<CoapSecure>().CoapBase::HandleRetransmissionTimer();
|
||||
}
|
||||
|
||||
void CoapSecure::HandleResponsesQueueTimer(Timer &aTimer)
|
||||
{
|
||||
aTimer.GetOwner<CoapSecure>().CoapBase::HandleResponsesQueueTimer();
|
||||
}
|
||||
|
||||
#if OPENTHREAD_ENABLE_APPLICATION_COAP_SECURE
|
||||
|
||||
ApplicationCoapSecure::ApplicationCoapSecure(Instance &aInstance)
|
||||
: CoapSecure(aInstance,
|
||||
&ApplicationCoapSecure::HandleTransmit,
|
||||
&ApplicationCoapSecure::HandleRetransmissionTimer,
|
||||
&ApplicationCoapSecure::HandleResponsesQueueTimer)
|
||||
: CoapSecure(aInstance, /* aLayerTwoSecurity */ true)
|
||||
{
|
||||
}
|
||||
|
||||
void ApplicationCoapSecure::HandleTransmit(Tasklet &aTasklet)
|
||||
{
|
||||
aTasklet.GetOwner<ApplicationCoapSecure>().CoapSecure::HandleTransmit();
|
||||
}
|
||||
|
||||
void ApplicationCoapSecure::HandleRetransmissionTimer(Timer &aTimer)
|
||||
{
|
||||
aTimer.GetOwner<ApplicationCoapSecure>().CoapBase::HandleRetransmissionTimer();
|
||||
}
|
||||
|
||||
void ApplicationCoapSecure::HandleResponsesQueueTimer(Timer &aTimer)
|
||||
{
|
||||
aTimer.GetOwner<ApplicationCoapSecure>().CoapBase::HandleResponsesQueueTimer();
|
||||
}
|
||||
|
||||
#endif // OPENTHREAD_ENABLE_APPLICATION_COAP_SECURE
|
||||
|
||||
} // namespace Coap
|
||||
|
||||
@@ -70,27 +70,11 @@ public:
|
||||
/**
|
||||
* This constructor initializes the object.
|
||||
*
|
||||
* @param[in] aInstance A reference to the OpenThread instance.
|
||||
* @param[in] aInstance A reference to the OpenThread instance.
|
||||
* @param[in] aLayerTwoSecurity Specifies whether to use layer two security or not.
|
||||
*
|
||||
*/
|
||||
explicit CoapSecure(Instance &aInstance);
|
||||
|
||||
#if OPENTHREAD_ENABLE_APPLICATION_COAP_SECURE
|
||||
/**
|
||||
* This constructor initializes the object.
|
||||
* (Used for Application CoAPS)
|
||||
*
|
||||
* @param[in] aInstance A reference to the OpenThread instance.
|
||||
* @param[in] aUdpTransmitHandle Handler for udp transmit.
|
||||
* @param[in] aRetransmissionTimer Handler for retransmission.
|
||||
* @param[in] aResponsesQueueTimer Handler for Queue Responses.
|
||||
*
|
||||
*/
|
||||
explicit CoapSecure(Instance & aInstance,
|
||||
Tasklet::Handler aUdpTransmitHandle,
|
||||
Timer::Handler aRetransmissionTimer,
|
||||
Timer::Handler aResponsesQueueTimer);
|
||||
#endif // OPENTHREAD_ENABLE_APPLICATION_COAP_SECURE
|
||||
explicit CoapSecure(Instance &aInstance, bool aLayerTwoSecurity = false);
|
||||
|
||||
/**
|
||||
* This method starts the secure CoAP agent.
|
||||
@@ -333,9 +317,6 @@ public:
|
||||
*/
|
||||
const Ip6::MessageInfo &GetPeerMessageInfo(void) const { return mPeerAddress; }
|
||||
|
||||
protected:
|
||||
void HandleTransmit(void);
|
||||
|
||||
private:
|
||||
virtual otError Send(Message &aMessage, const Ip6::MessageInfo &aMessageInfo);
|
||||
|
||||
@@ -348,9 +329,8 @@ private:
|
||||
static otError HandleDtlsSend(void *aContext, const uint8_t *aBuf, uint16_t aLength, uint8_t aMessageSubType);
|
||||
otError HandleDtlsSend(const uint8_t *aBuf, uint16_t aLength, uint8_t aMessageSubType);
|
||||
|
||||
static void HandleRetransmissionTimer(Timer &aTimer);
|
||||
static void HandleResponsesQueueTimer(Timer &aTimer);
|
||||
static void HandleTransmit(Tasklet &aTasklet);
|
||||
void HandleTransmit(void);
|
||||
|
||||
Ip6::MessageInfo mPeerAddress;
|
||||
ConnectedCallback mConnectedCallback;
|
||||
@@ -358,7 +338,7 @@ private:
|
||||
TransportCallback mTransportCallback;
|
||||
void * mTransportContext;
|
||||
MessageQueue mTransmitQueue;
|
||||
Tasklet mTransmitTask;
|
||||
TaskletContext mTransmitTask;
|
||||
|
||||
bool mLayerTwoSecurity : 1;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user