Skip to content

Commit

Permalink
[border-agent] add logging for border agent events (openthread#6481)
Browse files Browse the repository at this point in the history
This commit includes several small changes:
- Add logs for Border Agent Start and Stop events to help analysis
  commissioning issues such as openthread/ot-commissioner#193
  (comment).
- Start Border Agent when the native commissioner is stopped.
- Add CLI command ba state to get current Border Agent state.
  • Loading branch information
wgtdkp authored and duckhorn committed Oct 6, 2021
1 parent 4539b2a commit 1cae6eb
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 21 deletions.
16 changes: 15 additions & 1 deletion src/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Done

## OpenThread Command List

- [ba](#ba-port)
- [ba](#ba)
- [bbr](#bbr)
- [br](#br)
- [bufferinfo](#bufferinfo)
Expand Down Expand Up @@ -317,6 +317,10 @@ Set jitter (in seconds) for Backbone Router registration for Thread 1.2 FTD.
Done
```

### ba

Show current Border Agent information.

### ba port

Print border agent service port.
Expand All @@ -327,6 +331,16 @@ Print border agent service port.
Done
```

### ba state

Print border agent state.

```bash
> ba state
Started
Done
```

### br

Enbale/disable the Border Routing functionality.
Expand Down
32 changes: 30 additions & 2 deletions src/cli/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,37 @@ otError Interpreter::ProcessBorderAgent(uint8_t aArgsLength, char *aArgs[])
{
otError error = OT_ERROR_NONE;

VerifyOrExit(aArgsLength == 1 && strcmp(aArgs[0], "port") == 0, error = OT_ERROR_INVALID_COMMAND);
VerifyOrExit(aArgsLength == 1, error = OT_ERROR_INVALID_ARGS);

OutputLine("%hu", otBorderAgentGetUdpPort(mInstance));
if (strcmp(aArgs[0], "port") == 0)
{
OutputLine("%hu", otBorderAgentGetUdpPort(mInstance));
}
else if (strcmp(aArgs[0], "state") == 0)
{
const char *state;

switch (otBorderAgentGetState(mInstance))
{
case OT_BORDER_AGENT_STATE_STOPPED:
state = "Stopped";
break;
case OT_BORDER_AGENT_STATE_STARTED:
state = "Started";
break;
case OT_BORDER_AGENT_STATE_ACTIVE:
state = "Active";
break;
default:
state = "Unknown";
break;
}
OutputLine(state);
}
else
{
ExitNow(error = OT_ERROR_INVALID_COMMAND);
}

exit:
return error;
Expand Down
22 changes: 14 additions & 8 deletions src/core/meshcop/border_agent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,11 @@ void BorderAgent::HandleNotifierEvents(Events aEvents)

if (Get<Mle::MleRouter>().IsAttached())
{
IgnoreError(Start());
Start();
}
else
{
IgnoreError(Stop());
Stop();
}

exit:
Expand Down Expand Up @@ -579,7 +579,7 @@ uint16_t BorderAgent::GetUdpPort(void) const
return Get<Coap::CoapSecure>().GetUdpPort();
}

Error BorderAgent::Start(void)
void BorderAgent::Start(void)
{
Error error;
Coap::CoapSecure &coaps = Get<Coap::CoapSecure>();
Expand All @@ -606,8 +606,13 @@ Error BorderAgent::Start(void)
mState = kStateStarted;
mUdpProxyPort = 0;

otLogInfoMeshCoP("Border Agent start listening on port %d", kBorderAgentUdpPort);

exit:
return error;
if (error != kErrorNone)
{
otLogWarnMeshCoP("failed to start Border Agent on port %d: %s", kBorderAgentUdpPort, ErrorToString(error));
}
}

void BorderAgent::HandleTimeout(Timer &aTimer)
Expand All @@ -624,12 +629,11 @@ void BorderAgent::HandleTimeout(void)
}
}

Error BorderAgent::Stop(void)
void BorderAgent::Stop(void)
{
Error error = kErrorNone;
Coap::CoapSecure &coaps = Get<Coap::CoapSecure>();

VerifyOrExit(mState != kStateStopped, error = kErrorAlready);
VerifyOrExit(mState != kStateStopped);

mTimer.Stop();

Expand All @@ -651,8 +655,10 @@ Error BorderAgent::Stop(void)
mState = kStateStopped;
mUdpProxyPort = 0;

otLogInfoMeshCoP("Border Agent stopped");

exit:
return error;
return;
}

void BorderAgent::ApplyMeshLocalPrefix(void)
Expand Down
10 changes: 2 additions & 8 deletions src/core/meshcop/border_agent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,14 @@ class BorderAgent : public InstanceLocator, private NonCopyable
/**
* This method starts the Border Agent service.
*
* @retval kErrorNone Successfully started the Border Agent service.
* @retval kErrorAlready Border Agent is already started.
*
*/
Error Start(void);
void Start(void);

/**
* This method stops the Border Agent service.
*
* @retval kErrorNone Successfully stopped the Border Agent service.
* @retval kErrorAlready Border Agent is already stopped.
*
*/
Error Stop(void);
void Stop(void);

/**
* This method gets the state of the Border Agent service.
Expand Down
7 changes: 5 additions & 2 deletions src/core/meshcop/commissioner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,7 @@ Error Commissioner::Start(otCommissionerStateCallback aStateCallback,
VerifyOrExit(mState == kStateDisabled, error = kErrorAlready);

#if OPENTHREAD_CONFIG_BORDER_AGENT_ENABLE
error = Get<MeshCoP::BorderAgent>().Stop();
VerifyOrExit(error == kErrorNone || error == kErrorAlready);
Get<MeshCoP::BorderAgent>().Stop();
#endif

SuccessOrExit(error = Get<Coap::CoapSecure>().Start(SendRelayTransmit, this));
Expand Down Expand Up @@ -359,6 +358,10 @@ Error Commissioner::Stop(bool aResign)
SendKeepAlive();
}

#if OPENTHREAD_CONFIG_BORDER_AGENT_ENABLE
Get<MeshCoP::BorderAgent>().Start();
#endif

exit:
LogError("stop commissioner", error);
return error;
Expand Down
3 changes: 3 additions & 0 deletions tests/scripts/expect/cli-misc.exp
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,7 @@ expect ": InvalidState"
send "ba port\n"
expect "Done"

send "ba state\n"
expect "Done"

dispose_all

0 comments on commit 1cae6eb

Please sign in to comment.