mirror of
https://github.com/espressif/openthread.git
synced 2026-08-05 10:27:49 +00:00
CoAP: add default request handler (#1718)
This commit is contained in:
@@ -487,6 +487,15 @@ ThreadError otCoapAddResource(otInstance *aInstance, otCoapResource *aResource);
|
||||
*/
|
||||
void otCoapRemoveResource(otInstance *aInstance, otCoapResource *aResource);
|
||||
|
||||
/**
|
||||
* This function sets the default handler for unhandled CoAP requests.
|
||||
*
|
||||
* @param[in] aInstance A pointer to an OpenThread instance.
|
||||
* @param[in] aHandler A function pointer that shall be called when an unhandled request arrives.
|
||||
* @param[in] aContext A pointer to arbitrary context information. May be NULL if not used.
|
||||
*/
|
||||
void otCoapSetDefaultHandler(otInstance *aInstance, otCoapRequestHandler aHandler, void *aContext);
|
||||
|
||||
/**
|
||||
* This function sends a CoAP response from the server.
|
||||
*
|
||||
|
||||
@@ -169,6 +169,11 @@ void otCoapRemoveResource(otInstance *aInstance, otCoapResource *aResource)
|
||||
aInstance->mApplicationCoap.RemoveResource(*static_cast<Coap::Resource *>(aResource));
|
||||
}
|
||||
|
||||
void otCoapSetDefaultHandler(otInstance *aInstance, otCoapRequestHandler aHandler, void *aContext)
|
||||
{
|
||||
aInstance->mApplicationCoap.SetDefaultHandler(aHandler, aContext);
|
||||
}
|
||||
|
||||
ThreadError otCoapSendResponse(otInstance *aInstance, otMessage *aMessage, const otMessageInfo *aMessageInfo)
|
||||
{
|
||||
return aInstance->mApplicationCoap.SendMessage(
|
||||
|
||||
+14
-1
@@ -59,7 +59,9 @@ Coap::Coap(ThreadNetif &aNetif):
|
||||
mRetransmissionTimer(aNetif.GetIp6().mTimerScheduler, &Coap::HandleRetransmissionTimer, this),
|
||||
mResources(NULL),
|
||||
mInterceptor(NULL),
|
||||
mResponsesQueue(aNetif)
|
||||
mResponsesQueue(aNetif),
|
||||
mDefaultHandler(NULL),
|
||||
mDefaultHandlerContext(NULL)
|
||||
{
|
||||
mMessageId = static_cast<uint16_t>(otPlatRandomGet());
|
||||
}
|
||||
@@ -136,6 +138,12 @@ exit:
|
||||
aResource.mNext = NULL;
|
||||
}
|
||||
|
||||
void Coap::SetDefaultHandler(otCoapRequestHandler aHandler, void *aContext)
|
||||
{
|
||||
mDefaultHandler = aHandler;
|
||||
mDefaultHandlerContext = aContext;
|
||||
}
|
||||
|
||||
Message *Coap::NewMessage(const Header &aHeader, uint8_t aPriority)
|
||||
{
|
||||
Message *message = NULL;
|
||||
@@ -666,6 +674,11 @@ void Coap::ProcessReceivedRequest(Header &aHeader, Message &aMessage, const Ip6:
|
||||
}
|
||||
}
|
||||
|
||||
if (mDefaultHandler)
|
||||
{
|
||||
mDefaultHandler(mDefaultHandlerContext, &aHeader, &aMessage, &aMessageInfo);
|
||||
}
|
||||
|
||||
exit:
|
||||
|
||||
if (error != kThreadError_None && response != NULL)
|
||||
|
||||
@@ -499,6 +499,13 @@ public:
|
||||
*/
|
||||
void RemoveResource(Resource &aResource);
|
||||
|
||||
/* This function sets the default handler for unhandled CoAP requests.
|
||||
*
|
||||
* @param[in] aHandler A function pointer that shall be called when an unhandled request arrives.
|
||||
* @param[in] aContext A pointer to arbitrary context information. May be NULL if not used.
|
||||
*/
|
||||
void SetDefaultHandler(otCoapRequestHandler aHandler, void *aContext);
|
||||
|
||||
/**
|
||||
* This method creates a new message with a CoAP header.
|
||||
*
|
||||
@@ -665,6 +672,9 @@ private:
|
||||
|
||||
Interceptor mInterceptor;
|
||||
ResponsesQueue mResponsesQueue;
|
||||
|
||||
otCoapRequestHandler mDefaultHandler;
|
||||
void *mDefaultHandlerContext;
|
||||
};
|
||||
|
||||
} // namespace Coap
|
||||
|
||||
Reference in New Issue
Block a user