Skip to content

Commit

Permalink
[border agent] Meshcop-e service for ePSKc mode
Browse files Browse the repository at this point in the history
  • Loading branch information
yuzhyang committed May 7, 2024
1 parent c7885ed commit f7fc15c
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
69 changes: 69 additions & 0 deletions src/border_agent/border_agent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ namespace otbr {

static const char kBorderAgentServiceType[] = "_meshcop._udp"; ///< Border agent service type of mDNS
static constexpr int kBorderAgentServiceDummyPort = 49152;
#if OTBR_ENABLE_EPSKC
static const char kBorderAgentEpskcServiceType[] = "_meshcop-e._udp"; ///< Border agent ePSKc service
#endif

/**
* Locators
Expand Down Expand Up @@ -146,6 +149,10 @@ BorderAgent::BorderAgent(otbr::Ncp::ControllerOpenThread &aNcp, Mdns::Publisher
, mBaseServiceInstanceName(OTBR_MESHCOP_SERVICE_INSTANCE_NAME)
{
mNcp.AddThreadStateChangedCallback([this](otChangedFlags aFlags) { HandleThreadStateChanged(aFlags); });

#if OTBR_ENABLE_EPSKC
otBorderAgentSetEphemeralKeyCallback(mNcp.GetInstance(), BorderAgent::HandleEpskcStateChanged, this);
#endif
}

otbrError BorderAgent::SetMeshCopServiceValues(const std::string &aServiceInstanceName,
Expand Down Expand Up @@ -210,6 +217,68 @@ void BorderAgent::Stop(void)
UnpublishMeshCopService();
}

#if OTBR_ENABLE_EPSKC
void BorderAgent::HandleEpskcStateChanged(void *aContext)
{
BorderAgent *borderAgent = static_cast<BorderAgent *>(aContext);

if (otBorderAgentIsEphemeralKeyActive(borderAgent->mNcp.GetInstance()))
{
borderAgent->PublishEpskcService();
}
else
{
borderAgent->UnpublishEpskcService();
}
}

void BorderAgent::PublishEpskcService()
{
otInstance *instance = mNcp.GetInstance();
int port = otBorderAgentGetUdpPort(instance);

otbrLogInfo("Publish meshcop-e service %s.%s.local. port %d", mServiceInstanceName.c_str(),
kBorderAgentEpskcServiceType, port);

mPublisher.PublishService(/* aHostName */ "", mServiceInstanceName, kBorderAgentEpskcServiceType,
Mdns::Publisher::SubTypeList{}, port, {}, [this](otbrError aError) {
if (aError == OTBR_ERROR_ABORTED)
{
// OTBR_ERROR_ABORTED is thrown when an ongoing service registration is
// cancelled. This can happen when the meshcop-e service is being updated
// frequently. To avoid false alarms, it should not be logged like a real error.
otbrLogInfo("Cancelled previous publishing meshcop-e service %s.%s.local",
mServiceInstanceName.c_str(), kBorderAgentEpskcServiceType);
}
else
{
otbrLogResult(aError, "Result of publish meshcop-e service %s.%s.local",
mServiceInstanceName.c_str(), kBorderAgentEpskcServiceType);
}

if (aError == OTBR_ERROR_DUPLICATED)
{
// Try to unpublish current service in case we are trying to register
// multiple new services simultaneously when the original service name
// is conflicted.
UnpublishEpskcService();
mServiceInstanceName = GetAlternativeServiceInstanceName();
PublishEpskcService();
}
});
}

void BorderAgent::UnpublishEpskcService()
{
otbrLogInfo("Unpublish meshcop-e service %s.%s.local", mServiceInstanceName.c_str(), kBorderAgentEpskcServiceType);

mPublisher.UnpublishService(mServiceInstanceName, kBorderAgentEpskcServiceType, [this](otbrError aError) {
otbrLogResult(aError, "Result of unpublish meshcop-e service %s.%s.local", mServiceInstanceName.c_str(),
kBorderAgentEpskcServiceType);
});
}
#endif

void BorderAgent::HandleMdnsState(Mdns::Publisher::State aState)
{
VerifyOrExit(IsEnabled());
Expand Down
6 changes: 6 additions & 0 deletions src/border_agent/border_agent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ class BorderAgent : private NonCopyable
std::string GetServiceInstanceNameWithExtAddr(const std::string &aServiceInstanceName) const;
std::string GetAlternativeServiceInstanceName() const;

#if OTBR_ENABLE_EPSKC
static void HandleEpskcStateChanged(void *aContext);
void PublishEpskcService(void);
void UnpublishEpskcService(void);
#endif

otbr::Ncp::ControllerOpenThread &mNcp;
Mdns::Publisher &mPublisher;
bool mIsEnabled;
Expand Down

0 comments on commit f7fc15c

Please sign in to comment.