CoAP: add default request handler (#1718)

This commit is contained in:
Giedrius
2017-05-15 12:01:21 -07:00
committed by Jonathan Hui
parent 7049af1082
commit 8fa20a51f1
4 changed files with 38 additions and 1 deletions
+9
View File
@@ -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.
*
+5
View File
@@ -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
View File
@@ -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)
+10
View File
@@ -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