[border-agent] border agent state event (#3186)

This commit is contained in:
Yakun Xu
2018-10-22 22:47:59 -07:00
committed by Jonathan Hui
parent 04a2577cc6
commit c58d9cc0b1
6 changed files with 170 additions and 11 deletions
+1
View File
@@ -56,6 +56,7 @@ openthread_headers = \
commissioner.h \
config.h \
crypto.h \
border_agent.h \
border_router.h \
dataset.h \
dataset_ftd.h \
+83
View File
@@ -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 <openthread/instance.h>
#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_
+1
View File
@@ -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
};
/**
+51
View File
@@ -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 <openthread/border_agent.h>
#include "common/instance.hpp"
#if OPENTHREAD_ENABLE_BORDER_AGENT
using namespace ot;
otBorderAgentState otBorderAgentGetState(otInstance *aInstance)
{
Instance &instance = *static_cast<Instance *>(aInstance);
return instance.GetThreadNetif().GetBorderAgent().GetState();
}
#endif // OPENTHREAD_ENABLE_BORDER_AGENT
+16 -5
View File
@@ -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
+18 -6
View File
@@ -36,6 +36,8 @@
#include "openthread-core-config.h"
#include <openthread/border_agent.h>
#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