From c58d9cc0b192a9e98a455286ec47845a6a508560 Mon Sep 17 00:00:00 2001 From: Yakun Xu Date: Tue, 23 Oct 2018 13:47:59 +0800 Subject: [PATCH] [border-agent] border agent state event (#3186) --- include/openthread/Makefile.am | 1 + include/openthread/border_agent.h | 83 +++++++++++++++++++++++++++++++ include/openthread/instance.h | 1 + src/core/api/border_agent_api.cpp | 51 +++++++++++++++++++ src/core/meshcop/border_agent.cpp | 21 ++++++-- src/core/meshcop/border_agent.hpp | 24 ++++++--- 6 files changed, 170 insertions(+), 11 deletions(-) create mode 100644 include/openthread/border_agent.h create mode 100644 src/core/api/border_agent_api.cpp diff --git a/include/openthread/Makefile.am b/include/openthread/Makefile.am index e86743c31..5b54db7d8 100644 --- a/include/openthread/Makefile.am +++ b/include/openthread/Makefile.am @@ -56,6 +56,7 @@ openthread_headers = \ commissioner.h \ config.h \ crypto.h \ + border_agent.h \ border_router.h \ dataset.h \ dataset_ftd.h \ diff --git a/include/openthread/border_agent.h b/include/openthread/border_agent.h new file mode 100644 index 000000000..eedacaab0 --- /dev/null +++ b/include/openthread/border_agent.h @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2018, The OpenThread Authors. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * @file + * @brief + * This file includes functions for the Thread Border Agent role. + */ + +#ifndef OPENTHREAD_BORDER_AGENT_H_ +#define OPENTHREAD_BORDER_AGENT_H_ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @addtogroup api-border-agent + * + * @brief + * This module includes functions for the Thread Border Agent role. + * + * @{ + * + */ + +/** + * This enumeration defines the Border Agent state. + * + */ +typedef enum otBorderAgentState { + OT_BORDER_AGENT_STATE_STOPPED = 0, ///< Border agent role is disabled. + OT_BORDER_AGENT_STATE_STARTED = 1, ///< Border agent is started. + OT_BORDER_AGENT_STATE_ACTIVE = 2, ///< Border agent is connected with external commissioner. +} otBorderAgentState; + +/** + * This function gets the state of Thread Border Agent role. + * + * @param[in] aInstance A pointer to an OpenThread instance. + * + * @returns State of the Border Agent. + * + */ +otBorderAgentState otBorderAgentGetState(otInstance *aInstance); + +/** + * @} + * + */ + +#ifdef __cplusplus +} // end of extern "C" +#endif + +#endif // OPENTHREAD_BORDER_AGENT_H_ diff --git a/include/openthread/instance.h b/include/openthread/instance.h index c1bc7b144..de843e632 100644 --- a/include/openthread/instance.h +++ b/include/openthread/instance.h @@ -284,6 +284,7 @@ enum OT_CHANGED_SECURITY_POLICY = 1 << 22, ///< Security Policy changed OT_CHANGED_CHANNEL_MANAGER_NEW_CHANNEL = 1 << 23, ///< Channel Manager new pending Thread channel changed OT_CHANGED_SUPPORTED_CHANNEL_MASK = 1 << 24, ///< Supported channel mask changed + OT_CHANGED_BORDER_AGENT_STATE = 1 << 25, ///< Border agent state changed }; /** diff --git a/src/core/api/border_agent_api.cpp b/src/core/api/border_agent_api.cpp new file mode 100644 index 000000000..110936c4f --- /dev/null +++ b/src/core/api/border_agent_api.cpp @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2018, The OpenThread Authors. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * @file + * This file implements the OpenThread Border Agent API. + */ + +#include "openthread-core-config.h" + +#include + +#include "common/instance.hpp" + +#if OPENTHREAD_ENABLE_BORDER_AGENT + +using namespace ot; + +otBorderAgentState otBorderAgentGetState(otInstance *aInstance) +{ + Instance &instance = *static_cast(aInstance); + + return instance.GetThreadNetif().GetBorderAgent().GetState(); +} + +#endif // OPENTHREAD_ENABLE_BORDER_AGENT diff --git a/src/core/meshcop/border_agent.cpp b/src/core/meshcop/border_agent.cpp index 0749956e5..0a9d8922c 100644 --- a/src/core/meshcop/border_agent.cpp +++ b/src/core/meshcop/border_agent.cpp @@ -313,7 +313,7 @@ BorderAgent::BorderAgent(Instance &aInstance) , mProxyTransmit(OT_URI_PATH_PROXY_TX, BorderAgent::HandleRequest<&BorderAgent::mProxyTransmit>, this) , mProxyReceiver(BorderAgent::HandleProxyReceive, this) , mTimer(aInstance, HandleTimeout, this) - , mIsStarted(false) + , mState(OT_BORDER_AGENT_STATE_STOPPED) { } @@ -625,6 +625,7 @@ void BorderAgent::HandleConnected(bool aConnected) if (aConnected) { otLogInfoMeshCoP(GetInstance(), "Commissioner connected"); + SetState(OT_BORDER_AGENT_STATE_ACTIVE); mTimer.Start(kKeepAliveTimeout); } else @@ -636,6 +637,7 @@ void BorderAgent::HandleConnected(bool aConnected) netif.GetIp6().GetUdp().RemoveReceiver(mProxyReceiver); netif.RemoveUnicastAddress(mCommissionerAloc); coaps.Stop(); + SetState(OT_BORDER_AGENT_STATE_STARTED); mTimer.Start(kRestartDelay); } } @@ -661,7 +663,7 @@ otError BorderAgent::Start(void) Coap::CoapSecure &coaps = netif.GetCoapSecure(); Coap::Coap & coap = netif.GetCoap(); - VerifyOrExit(!mIsStarted, error = OT_ERROR_ALREADY); + VerifyOrExit(mState == OT_BORDER_AGENT_STATE_STOPPED, error = OT_ERROR_ALREADY); SuccessOrExit(error = StartCoaps()); @@ -678,7 +680,7 @@ otError BorderAgent::Start(void) coap.AddResource(mRelayReceive); - mIsStarted = true; + SetState(OT_BORDER_AGENT_STATE_STARTED); exit: return error; @@ -720,7 +722,7 @@ otError BorderAgent::Stop(void) Coap::CoapSecure &coaps = netif.GetCoapSecure(); Coap::Coap & coap = netif.GetCoap(); - VerifyOrExit(mIsStarted, error = OT_ERROR_ALREADY); + VerifyOrExit(mState != OT_BORDER_AGENT_STATE_STOPPED, error = OT_ERROR_ALREADY); mTimer.Stop(); @@ -740,12 +742,21 @@ otError BorderAgent::Stop(void) error = coaps.Stop(); assert(error == OT_ERROR_NONE); - mIsStarted = false; + SetState(OT_BORDER_AGENT_STATE_STOPPED); exit: return error; } +void BorderAgent::SetState(otBorderAgentState aState) +{ + if (mState != aState) + { + mState = aState; + GetNotifier().Signal(OT_CHANGED_BORDER_AGENT_STATE); + } +} + } // namespace MeshCoP } // namespace ot diff --git a/src/core/meshcop/border_agent.hpp b/src/core/meshcop/border_agent.hpp index 1a73af333..8b63e53cf 100644 --- a/src/core/meshcop/border_agent.hpp +++ b/src/core/meshcop/border_agent.hpp @@ -36,6 +36,8 @@ #include "openthread-core-config.h" +#include + #include "coap/coap.hpp" #include "common/locator.hpp" #include "net/udp6.hpp" @@ -58,21 +60,29 @@ public: explicit BorderAgent(Instance &aInstance); /** - * This method starts the BorderAgent service. + * This method starts the Border Agent service. * - * @retval OT_ERROR_NONE Successfully started the BorderAgent service. + * @retval OT_ERROR_NONE Successfully started the Border Agent service. * */ otError Start(void); /** - * This method stops the BorderAgent service. + * This method stops the Border Agent service. * - * @retval OT_ERROR_NONE Successfully stopped the BorderAgent service. + * @retval OT_ERROR_NONE Successfully stopped the Border Agent service. * */ otError Stop(void); + /** + * This method gets the state of the Border Agent service. + * + * @returns The state of the the Border Agent service. + * + */ + otBorderAgentState GetState(void) const { return mState; } + private: static void HandleConnected(bool aConnected, void *aContext) { @@ -121,6 +131,8 @@ private: } bool HandleProxyReceive(const Message &aMessage, const Ip6::MessageInfo &aMessageInfo); + void SetState(otBorderAgentState aState); + enum { kBorderAgentUdpPort = 49191, ///< UDP port of border agent service. @@ -145,8 +157,8 @@ private: Ip6::UdpReceiver mProxyReceiver; ///< The UDP receiver to handle proxy packets to Commissioner Ip6::NetifUnicastAddress mCommissionerAloc; - TimerMilli mTimer; - bool mIsStarted; + TimerMilli mTimer; + otBorderAgentState mState; }; } // namespace MeshCoP