diff --git a/include/openthread/udp.h b/include/openthread/udp.h index 0c81088fb..1d18fea95 100644 --- a/include/openthread/udp.h +++ b/include/openthread/udp.h @@ -73,6 +73,30 @@ typedef struct otUdpReceiver void * mContext; ///< A pointer to application-specific context. } otUdpReceiver; +/** + * This function adds a UDP receiver. + * + * @param[in] aInstance A pointer to an OpenThread instance. + * @param[in] aUdpReceiver A pointer to the UDP receiver. + * + * @retval OT_ERROR_NONE The receiver is successfully added. + * @retval OT_ERROR_ALREADY The UDP receiver was already added. + * + */ +otError otUdpAddReceiver(otInstance *aInstance, otUdpReceiver *aUdpReceiver); + +/** + * This function removes a UDP receiver. + * + * @param[in] aInstance A pointer to an OpenThread instance. + * @param[in] aUdpReceiver A pointer to the UDP receiver. + * + * @retval OT_ERROR_NONE The receiver is successfully removed. + * @retval OT_ERROR_NOT_FOUND The UDP receiver was not added. + * + */ +otError otUdpRemoveReceiver(otInstance *aInstance, otUdpReceiver *aUdpReceiver); + /** * This callback allows OpenThread to inform the application of a received UDP message. * diff --git a/src/core/api/udp_api.cpp b/src/core/api/udp_api.cpp index 11fda2255..8a1b272f1 100644 --- a/src/core/api/udp_api.cpp +++ b/src/core/api/udp_api.cpp @@ -37,6 +37,7 @@ #include "common/instance.hpp" #include "common/new.hpp" +#include "net/udp6.hpp" using namespace ot; @@ -136,3 +137,17 @@ otUdpSocket *otUdpGetSockets(otInstance *aInstance) return instance.Get().GetUdp().GetUdpSockets(); } #endif + +otError otUdpAddReceiver(otInstance *aInstance, otUdpReceiver *aUdpReceiver) +{ + Instance &instance = *static_cast(aInstance); + + return instance.GetIp6().GetUdp().AddReceiver(*static_cast(aUdpReceiver)); +} + +otError otUdpRemoveReceiver(otInstance *aInstance, otUdpReceiver *aUdpReceiver) +{ + Instance &instance = *static_cast(aInstance); + + return instance.GetIp6().GetUdp().RemoveReceiver(*static_cast(aUdpReceiver)); +} diff --git a/src/core/net/udp6.cpp b/src/core/net/udp6.cpp index 5fa2194a9..3d5b33953 100644 --- a/src/core/net/udp6.cpp +++ b/src/core/net/udp6.cpp @@ -209,11 +209,13 @@ Udp::Udp(Instance &aInstance) otError Udp::AddReceiver(UdpReceiver &aReceiver) { + otError error = OT_ERROR_NONE; + for (UdpReceiver *cur = mReceivers; cur; cur = cur->GetNext()) { if (cur == &aReceiver) { - ExitNow(); + ExitNow(error = OT_ERROR_ALREADY); } } @@ -221,14 +223,18 @@ otError Udp::AddReceiver(UdpReceiver &aReceiver) mReceivers = &aReceiver; exit: - return OT_ERROR_NONE; + return error; } otError Udp::RemoveReceiver(UdpReceiver &aReceiver) { + otError error = OT_ERROR_NOT_FOUND; + if (mReceivers == &aReceiver) { mReceivers = mReceivers->GetNext(); + aReceiver.SetNext(NULL); + error = OT_ERROR_NONE; } else { @@ -237,14 +243,14 @@ otError Udp::RemoveReceiver(UdpReceiver &aReceiver) if (handler->GetNext() == &aReceiver) { handler->SetNext(aReceiver.GetNext()); + aReceiver.SetNext(NULL); + error = OT_ERROR_NONE; break; } } } - aReceiver.SetNext(NULL); - - return OT_ERROR_NONE; + return error; } otError Udp::AddSocket(UdpSocket &aSocket) diff --git a/src/core/net/udp6.hpp b/src/core/net/udp6.hpp index 0c51a743b..b4bd0e774 100644 --- a/src/core/net/udp6.hpp +++ b/src/core/net/udp6.hpp @@ -226,7 +226,8 @@ public: * * @param[in] aReceiver A reference to the UDP receiver. * - * @retval OT_ERROR_NONE Successfully added the UDP receiver. + * @retval OT_ERROR_NONE Successfully added the UDP receiver. + * @retval OT_ERROR_ALREADY The UDP receiver was already added. * */ otError AddReceiver(UdpReceiver &aReceiver); @@ -236,7 +237,8 @@ public: * * @param[in] aReceiver A reference to the UDP receiver. * - * @retval OT_ERROR_NONE Successfully removed the UDP receiver. + * @retval OT_ERROR_NONE Successfully removed the UDP receiver. + * @retval OT_ERROR_NOT_FOUND The UDP receiver was not added. * */ otError RemoveReceiver(UdpReceiver &aReceiver);