diff --git a/src/border_agent/border_agent.cpp b/src/border_agent/border_agent.cpp index 20fd22edca5..ce620d116c1 100644 --- a/src/border_agent/border_agent.cpp +++ b/src/border_agent/border_agent.cpp @@ -250,10 +250,10 @@ void BorderAgent::SetEphemeralKeyEnabled(bool aIsEnabled) if (!mIsEphemeralKeyEnabled) { - // If the ePSKc feature is enabled, we call the clear function which + // If the ePSKc feature is enabled, we call the stop function which // will wait for the session to close if it is in active use before // removing ephemeral key and unpublishing the service. - otBorderAgentClearEphemeralKey(mHost.GetInstance()); + otBorderAgentEphemeralKeyStop(mHost.GetInstance()); } UpdateMeshCopService(); @@ -281,7 +281,7 @@ void BorderAgent::Start(void) mServiceInstanceName = GetServiceInstanceNameWithExtAddr(mBaseServiceInstanceName); UpdateMeshCopService(); - otBorderAgentSetEphemeralKeyCallback(mHost.GetInstance(), BorderAgent::HandleEpskcStateChanged, this); + otBorderAgentEphemeralKeySetCallback(mHost.GetInstance(), BorderAgent::HandleEpskcStateChanged, this); } void BorderAgent::Stop(void) @@ -292,18 +292,25 @@ void BorderAgent::Stop(void) void BorderAgent::HandleEpskcStateChanged(void *aContext) { - BorderAgent *borderAgent = static_cast(aContext); + static_cast(aContext)->HandleEpskcStateChanged(); +} - if (otBorderAgentIsEphemeralKeyActive(borderAgent->mHost.GetInstance())) - { - borderAgent->PublishEpskcService(); - } - else +void BorderAgent::HandleEpskcStateChanged(void) +{ + switch (otBorderAgentEphemeralKeyGetState(mHost.GetInstance())) { - borderAgent->UnpublishEpskcService(); + case OT_BORDER_AGENT_STATE_STARTED: + case OT_BORDER_AGENT_STATE_CONNECTED: + case OT_BORDER_AGENT_STATE_ACCEPTED: + PublishEpskcService(); + break; + case OT_BORDER_AGENT_STATE_DISABLED: + case OT_BORDER_AGENT_STATE_STOPPED: + UnpublishEpskcService(); + break; } - for (auto &ephemeralKeyCallback : borderAgent->mEphemeralKeyChangedCallbacks) + for (auto &ephemeralKeyCallback : mEphemeralKeyChangedCallbacks) { ephemeralKeyCallback(); } @@ -312,7 +319,7 @@ void BorderAgent::HandleEpskcStateChanged(void *aContext) void BorderAgent::PublishEpskcService() { otInstance *instance = mHost.GetInstance(); - int port = otBorderAgentGetUdpPort(instance); + int port = otBorderAgentEphemeralKeyGetUdpPort(instance); otbrLogInfo("Publish meshcop-e service %s.%s.local. port %d", mServiceInstanceName.c_str(), kBorderAgentEpskcServiceType, port); @@ -583,7 +590,7 @@ void BorderAgent::PublishMeshCopService(void) AppendVendorTxtEntries(mMeshCopTxtUpdate, txtList); - if (otBorderAgentGetState(instance) != OT_BORDER_AGENT_STATE_STOPPED) + if (otBorderAgentIsActive(instance)) { port = otBorderAgentGetUdpPort(instance); } diff --git a/src/border_agent/border_agent.hpp b/src/border_agent/border_agent.hpp index bf5a9275c6d..ece64d135c0 100644 --- a/src/border_agent/border_agent.hpp +++ b/src/border_agent/border_agent.hpp @@ -177,6 +177,7 @@ class BorderAgent : public Mdns::StateObserver, private NonCopyable std::string GetAlternativeServiceInstanceName() const; static void HandleEpskcStateChanged(void *aContext); + void HandleEpskcStateChanged(void); void PublishEpskcService(void); void UnpublishEpskcService(void); diff --git a/src/dbus/server/dbus_thread_object_rcp.cpp b/src/dbus/server/dbus_thread_object_rcp.cpp index 3130f3a3e37..4ee4e2ea5f2 100644 --- a/src/dbus/server/dbus_thread_object_rcp.cpp +++ b/src/dbus/server/dbus_thread_object_rcp.cpp @@ -2014,11 +2014,25 @@ void DBusThreadObjectRcp::DeactivateEphemeralKeyModeHandler(DBusRequest &aReques VerifyOrExit(mBorderAgent.GetEphemeralKeyEnabled(), error = OT_ERROR_NOT_CAPABLE); SuccessOrExit(DBusMessageToTuple(*aRequest.GetMessage(), args), error = OT_ERROR_INVALID_ARGS); - if (!retain_active_session) + + // Stop the ephemeral key use if + // - there is no active session, or + // - there is a connected session and we should not `retain_active_session`. + + swicth (otBorderAgentEphemeralKeyGetState(threadHelper->GetInstance())) { - otBorderAgentDisconnect(threadHelper->GetInstance()); + case OT_BORDER_AGENT_STATE_STARTED: + break; + case OT_BORDER_AGENT_STATE_CONNECTED: + case OT_BORDER_AGENT_STATE_ACCEPTED: + VerifyOrExit(!retain_active_session); + break; + case OT_BORDER_AGENT_STATE_DISABLED: + case OT_BORDER_AGENT_STATE_STOPPED: + ExitNow(); } - otBorderAgentClearEphemeralKey(threadHelper->GetInstance()); + + otBorderAgentEphemeralKeyStop(threadHelper->GetInstance()); exit: aRequest.ReplyOtResult(error); @@ -2040,8 +2054,8 @@ void DBusThreadObjectRcp::ActivateEphemeralKeyModeHandler(DBusRequest &aRequest) SuccessOrExit(mBorderAgent.CreateEphemeralKey(ePskc), error = OT_ERROR_INVALID_ARGS); otbrLogInfo("Created Ephemeral Key: %s", ePskc.c_str()); - SuccessOrExit(error = otBorderAgentSetEphemeralKey(threadHelper->GetInstance(), ePskc.c_str(), lifetime, - OTBR_CONFIG_BORDER_AGENT_MESHCOP_E_UDP_PORT)); + SuccessOrExit(error = otBorderAgentEphemeralKeyStart(threadHelper->GetInstance(), ePskc.c_str(), lifetime, + OTBR_CONFIG_BORDER_AGENT_MESHCOP_E_UDP_PORT)); exit: if (error == OT_ERROR_NONE)