diff --git a/include/openthread/coap.h b/include/openthread/coap.h index 6f355e863..bb34b2631 100644 --- a/include/openthread/coap.h +++ b/include/openthread/coap.h @@ -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. * diff --git a/src/core/api/coap_api.cpp b/src/core/api/coap_api.cpp index 6358a45da..2a5a81d19 100644 --- a/src/core/api/coap_api.cpp +++ b/src/core/api/coap_api.cpp @@ -169,6 +169,11 @@ void otCoapRemoveResource(otInstance *aInstance, otCoapResource *aResource) aInstance->mApplicationCoap.RemoveResource(*static_cast(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( diff --git a/src/core/coap/coap.cpp b/src/core/coap/coap.cpp index aa593eb17..6c4bce76d 100644 --- a/src/core/coap/coap.cpp +++ b/src/core/coap/coap.cpp @@ -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(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) diff --git a/src/core/coap/coap.hpp b/src/core/coap/coap.hpp index 96add28df..22c4e0a96 100644 --- a/src/core/coap/coap.hpp +++ b/src/core/coap/coap.hpp @@ -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