Skip to content

Commit

Permalink
[border-agent] add api to disconnect from secure sessions (openthread…
Browse files Browse the repository at this point in the history
  • Loading branch information
sunytt authored Oct 8, 2024
1 parent a8bf8e7 commit be74a3c
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 1 deletion.
13 changes: 13 additions & 0 deletions include/openthread/border_agent.h
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,19 @@ void otBorderAgentSetEphemeralKeyCallback(otInstance *aIns
otBorderAgentEphemeralKeyCallback aCallback,
void *aContext);

/**
* Disconnects the Border Agent from any active secure sessions.
*
* If Border Agent is connected to a commissioner candidate with ephemeral key, calling this API
* will cause the ephemeral key to be cleared after the session is disconnected.
*
* The Border Agent state may not change immediately upon calling this method. The state will be
* updated when the connection update is notified with a delay.
*
* @param[in] aInstance The OpenThread instance.
*/
void otBorderAgentDisconnect(otInstance *aInstance);

/**
* @}
*/
Expand Down
2 changes: 1 addition & 1 deletion include/openthread/instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ extern "C" {
*
* @note This number versions both OpenThread platform and user APIs.
*/
#define OPENTHREAD_API_VERSION (452)
#define OPENTHREAD_API_VERSION (453)

/**
* @addtogroup api-instance
Expand Down
9 changes: 9 additions & 0 deletions src/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,15 @@ Started
Done
```

### ba disconnect

Disconnects border agent from any active secure sessions.

```bash
> ba disconnect
Done
```

### ba ephemeralkey

Indicates if an ephemeral key is active.
Expand Down
14 changes: 14 additions & 0 deletions src/cli/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,20 @@ template <> otError Interpreter::Process<Cmd("ba")>(Arg aArgs[])

OutputLine("%s", Stringify(otBorderAgentGetState(GetInstancePtr()), kStateStrings));
}
/**
* @cli ba disconnect
* @code
* ba disconnect
* Done
* @endcode
* @par
* Disconnects the Border Agent from any active secure sessions
* @sa otBorderAgentDisconnect
*/
else if (aArgs[0] == "disconnect")
{
otBorderAgentDisconnect(GetInstancePtr());
}
#if OPENTHREAD_CONFIG_BORDER_AGENT_ID_ENABLE
/**
* @cli ba id (get,set)
Expand Down
2 changes: 2 additions & 0 deletions src/core/api/border_agent_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,6 @@ const otBorderAgentCounters *otBorderAgentGetCounters(otInstance *aInstance)
return AsCoreType(aInstance).Get<MeshCoP::BorderAgent>().GetCounters();
}

void otBorderAgentDisconnect(otInstance *aInstance) { AsCoreType(aInstance).Get<MeshCoP::BorderAgent>().Disconnect(); }

#endif // OPENTHREAD_CONFIG_BORDER_AGENT_ENABLE
10 changes: 10 additions & 0 deletions src/core/meshcop/border_agent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,16 @@ void BorderAgent::Stop(void)
return;
}

void BorderAgent::Disconnect(void)
{
VerifyOrExit(mState == kStateConnected || mState == kStateAccepted);

Get<Tmf::SecureAgent>().Disconnect();

exit:
return;
}

#if OPENTHREAD_CONFIG_BORDER_AGENT_EPHEMERAL_KEY_ENABLE

Error BorderAgent::SetEphemeralKey(const char *aKeyString, uint32_t aTimeout, uint16_t aUdpPort)
Expand Down
11 changes: 11 additions & 0 deletions src/core/meshcop/border_agent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,17 @@ class BorderAgent : public InstanceLocator, private NonCopyable
*/
State GetState(void) const { return mState; }

/**
* Disconnects the Border Agent from any active secure sessions.
*
* If Border Agent is connected to a commissioner candidate with ephemeral key, calling this API
* will cause the ephemeral key to be cleared after the session is disconnected.
*
* The Border Agent state may not change immediately upon calling this method, the state will be
* updated when the connection update is notified by `HandleConnected()`.
*/
void Disconnect(void);

#if OPENTHREAD_CONFIG_BORDER_AGENT_EPHEMERAL_KEY_ENABLE
/**
* Sets the ephemeral key for a given timeout duration.
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 @@ -184,6 +184,9 @@ expect "Done"
send "ba state\n"
expect "Done"

send "ba disconnect\n"
expect "Done"

send "prefix meshlocal fd00:dead:beef:cafe::/96\n"
expect_line "Error 7: InvalidArgs"
send "prefix meshlocal fd00:dead:beef:cafe::/64\n"
Expand Down

0 comments on commit be74a3c

Please sign in to comment.