From 05c623454b67f265be954117ab492901fad41a77 Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Mon, 28 Apr 2025 20:52:49 -0700 Subject: [PATCH] [border-agent] shorten method and variable names (#11456) This commit updates `BorderAgent` method and variable names to use shorter forms where possible. Specifically, the term `MeshCoP` is removed from many variable and method names, as the `BorderAgent` class itself is already defined within the `MeshCoP` namespace. This commit is purely a style and naming change and contains no modification to the code logic. --- src/core/api/border_agent_api.cpp | 4 +- src/core/border_router/routing_manager.cpp | 2 +- src/core/meshcop/border_agent.cpp | 46 ++++++------- src/core/meshcop/border_agent.hpp | 40 +++++------ tests/nexus/test_border_agent.cpp | 77 +++++++++++----------- 5 files changed, 85 insertions(+), 84 deletions(-) diff --git a/src/core/api/border_agent_api.cpp b/src/core/api/border_agent_api.cpp index 4a83ed6d6..0fd74d231 100644 --- a/src/core/api/border_agent_api.cpp +++ b/src/core/api/border_agent_api.cpp @@ -80,12 +80,12 @@ void otBorderAgentSetMeshCoPServiceChangedCallback(otInstance otBorderAgentMeshCoPServiceChangedCallback aCallback, void *aContext) { - AsCoreType(aInstance).Get().SetMeshCoPServiceChangedCallback(aCallback, aContext); + AsCoreType(aInstance).Get().SetServiceChangedCallback(aCallback, aContext); } otError otBorderAgentGetMeshCoPServiceTxtData(otInstance *aInstance, otBorderAgentMeshCoPServiceTxtData *aTxtData) { - return AsCoreType(aInstance).Get().GetMeshCoPServiceTxtData(*aTxtData); + return AsCoreType(aInstance).Get().PrepareServiceTxtData(*aTxtData); } const otBorderAgentCounters *otBorderAgentGetCounters(otInstance *aInstance) diff --git a/src/core/border_router/routing_manager.cpp b/src/core/border_router/routing_manager.cpp index 508cdc17d..7797082f7 100644 --- a/src/core/border_router/routing_manager.cpp +++ b/src/core/border_router/routing_manager.cpp @@ -2634,7 +2634,7 @@ void RoutingManager::OmrPrefixManager::SetFavordPrefix(const OmrPrefix &aOmrPref if (oldFavoredPrefix != mFavoredPrefix) { #if OPENTHREAD_CONFIG_BORDER_AGENT_ENABLE - Get().PostNotifyMeshCoPServiceChangedTask(); + Get().HandleFavoredOmrPrefixChanged(); #endif LogInfo("Favored OMR prefix: %s -> %s", FavoredToString(oldFavoredPrefix).AsCString(), FavoredToString(mFavoredPrefix).AsCString()); diff --git a/src/core/meshcop/border_agent.cpp b/src/core/meshcop/border_agent.cpp index 3d15a4039..547da903e 100644 --- a/src/core/meshcop/border_agent.cpp +++ b/src/core/meshcop/border_agent.cpp @@ -52,7 +52,7 @@ BorderAgent::BorderAgent(Instance &aInstance) #if OPENTHREAD_CONFIG_BORDER_AGENT_ID_ENABLE , mIdInitialized(false) #endif - , mNotifyMeshCoPServiceChangedTask(aInstance) + , mServiceTask(aInstance) #if OPENTHREAD_CONFIG_BORDER_AGENT_EPHEMERAL_KEY_ENABLE , mEphemeralKeyManager(aInstance) #endif @@ -115,7 +115,7 @@ void BorderAgent::Start(void) pskc.Clear(); mIsRunning = true; - PostNotifyMeshCoPServiceChangedTask(); + PostServiceTask(); LogInfo("Border Agent start listening on port %u", GetUdpPort()); @@ -134,7 +134,7 @@ void BorderAgent::Stop(void) mDtlsTransport.Close(); mIsRunning = false; - PostNotifyMeshCoPServiceChangedTask(); + PostServiceTask(); LogInfo("Border Agent stopped"); @@ -144,18 +144,18 @@ exit: uint16_t BorderAgent::GetUdpPort(void) const { return mDtlsTransport.GetUdpPort(); } -void BorderAgent::SetMeshCoPServiceChangedCallback(MeshCoPServiceChangedCallback aCallback, void *aContext) +void BorderAgent::SetServiceChangedCallback(ServiceChangedCallback aCallback, void *aContext) { - mMeshCoPServiceChangedCallback.Set(aCallback, aContext); + mServiceChangedCallback.Set(aCallback, aContext); - mNotifyMeshCoPServiceChangedTask.Post(); + PostServiceTask(); } -Error BorderAgent::GetMeshCoPServiceTxtData(MeshCoPServiceTxtData &aTxtData) const +Error BorderAgent::PrepareServiceTxtData(ServiceTxtData &aTxtData) const { - MeshCoPTxtEncoder meshCoPTxtEncoder(GetInstance(), aTxtData); + TxtEncoder encoder(GetInstance(), aTxtData); - return meshCoPTxtEncoder.EncodeTxtData(); + return encoder.EncodeTxtData(); } void BorderAgent::HandleNotifierEvents(Events aEvents) @@ -175,7 +175,7 @@ void BorderAgent::HandleNotifierEvents(Events aEvents) if (aEvents.ContainsAny(kEventThreadRoleChanged | kEventThreadExtPanIdChanged | kEventThreadNetworkNameChanged | kEventThreadBackboneRouterStateChanged | kEventActiveDatasetChanged)) { - PostNotifyMeshCoPServiceChangedTask(); + PostServiceTask(); } if (aEvents.ContainsAny(kEventPskcChanged)) @@ -347,20 +347,20 @@ exit: FreeMessageOnError(message, error); } -void BorderAgent::NotifyMeshCoPServiceChanged(void) { mMeshCoPServiceChangedCallback.InvokeIfSet(); } +void BorderAgent::HandleServiceTask(void) { mServiceChangedCallback.InvokeIfSet(); } -void BorderAgent::PostNotifyMeshCoPServiceChangedTask(void) +void BorderAgent::PostServiceTask(void) { - if (mMeshCoPServiceChangedCallback.IsSet()) + if (mServiceChangedCallback.IsSet()) { - mNotifyMeshCoPServiceChangedTask.Post(); + mServiceTask.Post(); } } //---------------------------------------------------------------------------------------------------------------------- -// BorderAgent::MeshCoPTxtEncoder +// BorderAgent::TxtEncoder -Error BorderAgent::MeshCoPTxtEncoder::AppendTxtEntry(const char *aKey, const void *aValue, uint16_t aValueLength) +Error BorderAgent::TxtEncoder::AppendTxtEntry(const char *aKey, const void *aValue, uint16_t aValueLength) { Dns::TxtEntry txtEntry; @@ -368,12 +368,12 @@ Error BorderAgent::MeshCoPTxtEncoder::AppendTxtEntry(const char *aKey, const voi return txtEntry.AppendTo(mAppender); } -template <> Error BorderAgent::MeshCoPTxtEncoder::AppendTxtEntry(const char *aKey, const NameData &aObject) +template <> Error BorderAgent::TxtEncoder::AppendTxtEntry(const char *aKey, const NameData &aObject) { return AppendTxtEntry(aKey, aObject.GetBuffer(), aObject.GetLength()); } -Error BorderAgent::MeshCoPTxtEncoder::EncodeTxtData(void) +Error BorderAgent::TxtEncoder::EncodeTxtData(void) { Error error = kErrorNone; #if OPENTHREAD_CONFIG_BORDER_AGENT_ID_ENABLE @@ -418,7 +418,7 @@ exit: } #if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE -Error BorderAgent::MeshCoPTxtEncoder::AppendBbrTxtEntry(StateBitmap aState) +Error BorderAgent::TxtEncoder::AppendBbrTxtEntry(StateBitmap aState) { Error error = kErrorNone; const DomainName &domainName = Get().GetDomainName(); @@ -441,7 +441,7 @@ exit: #endif // OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE #if OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE -Error BorderAgent::MeshCoPTxtEncoder::AppendOmrTxtEntry(void) +Error BorderAgent::TxtEncoder::AppendOmrTxtEntry(void) { Error error = kErrorNone; Ip6::Prefix prefix; @@ -462,7 +462,7 @@ exit: } #endif -BorderAgent::MeshCoPTxtEncoder::StateBitmap BorderAgent::MeshCoPTxtEncoder::GetStateBitmap(void) +BorderAgent::TxtEncoder::StateBitmap BorderAgent::TxtEncoder::GetStateBitmap(void) { StateBitmap state; @@ -561,14 +561,14 @@ void BorderAgent::EphemeralKeyManager::SetEnabled(bool aEnabled) { VerifyOrExit(mState == kStateDisabled); SetState(kStateStopped); - Get().PostNotifyMeshCoPServiceChangedTask(); + Get().PostServiceTask(); } else { VerifyOrExit(mState != kStateDisabled); Stop(); SetState(kStateDisabled); - Get().PostNotifyMeshCoPServiceChangedTask(); + Get().PostServiceTask(); } exit: diff --git a/src/core/meshcop/border_agent.hpp b/src/core/meshcop/border_agent.hpp index 1b036d5e3..d95eb371d 100644 --- a/src/core/meshcop/border_agent.hpp +++ b/src/core/meshcop/border_agent.hpp @@ -77,9 +77,11 @@ class BorderAgent : public InstanceLocator, private NonCopyable class CoapDtlsSession; public: - typedef otBorderAgentId Id; ///< Border Agent ID. - typedef otBorderAgentCounters Counters; ///< Border Agent Counters. - typedef otBorderAgentSessionInfo SessionInfo; ///< A session info. + typedef otBorderAgentId Id; ///< Border Agent ID. + typedef otBorderAgentCounters Counters; ///< Border Agent Counters. + typedef otBorderAgentSessionInfo SessionInfo; ///< A session info. + typedef otBorderAgentMeshCoPServiceChangedCallback ServiceChangedCallback; ///< Service changed callback. + typedef otBorderAgentMeshCoPServiceTxtData ServiceTxtData; ///< Service TXT data. /** * Represents an iterator for secure sessions. @@ -163,8 +165,6 @@ public: */ bool IsRunning(void) const { return mIsRunning; } - typedef otBorderAgentMeshCoPServiceChangedCallback MeshCoPServiceChangedCallback; - /** * Sets the callback function used by the Border Agent to notify any changes on the MeshCoP service TXT values. * @@ -178,19 +178,17 @@ public: * @param[in] aCallback The callback to invoke when there are any changes of the MeshCoP service. * @param[in] aContext A pointer to application-specific context. */ - void SetMeshCoPServiceChangedCallback(MeshCoPServiceChangedCallback aCallback, void *aContext); - - typedef otBorderAgentMeshCoPServiceTxtData MeshCoPServiceTxtData; + void SetServiceChangedCallback(ServiceChangedCallback aCallback, void *aContext); /** - * Gets the MeshCoP service TXT data. + * Prepares the MeshCoP service TXT data. * * @param[out] aTxtData A reference to a MeshCoP Service TXT data struct to get the data. * * @retval kErrorNone If successfully retrieved the Border Agent MeshCoP Service TXT data. * @retval kErrorNoBufs If the buffer in @p aTxtData doesn't have enough size. */ - Error GetMeshCoPServiceTxtData(MeshCoPServiceTxtData &aTxtData) const; + Error PrepareServiceTxtData(ServiceTxtData &aTxtData) const; #if OPENTHREAD_CONFIG_BORDER_AGENT_EPHEMERAL_KEY_ENABLE /** @@ -434,10 +432,10 @@ private: uint64_t mAllocationTime; }; - class MeshCoPTxtEncoder : public InstanceLocator + class TxtEncoder : public InstanceLocator { public: - MeshCoPTxtEncoder(Instance &aInstance, MeshCoPServiceTxtData &aTxtData) + TxtEncoder(Instance &aInstance, ServiceTxtData &aTxtData) : InstanceLocator(aInstance) , mTxtData(aTxtData) , mAppender(mTxtData.mData, sizeof(mTxtData.mData)) @@ -532,8 +530,8 @@ private: StateBitmap GetStateBitmap(void); - MeshCoPServiceTxtData &mTxtData; - Appender mAppender; + ServiceTxtData &mTxtData; + Appender mAppender; }; void Start(void); @@ -554,10 +552,14 @@ private: static Coap::Message::Code CoapCodeFromError(Error aError); - void PostNotifyMeshCoPServiceChangedTask(void); - void NotifyMeshCoPServiceChanged(void); + void PostServiceTask(void); + void HandleServiceTask(void); +#if OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE + // Callback from `RoutingManager` + void HandleFavoredOmrPrefixChanged(void) { PostServiceTask(); } +#endif - using NotifyMeshCoPServiceChangedTask = TaskletIn; + using ServiceTask = TaskletIn; bool mIsRunning; Dtls::Transport mDtlsTransport; @@ -565,8 +567,8 @@ private: Id mId; bool mIdInitialized; #endif - Callback mMeshCoPServiceChangedCallback; - NotifyMeshCoPServiceChangedTask mNotifyMeshCoPServiceChangedTask; + Callback mServiceChangedCallback; + ServiceTask mServiceTask; #if OPENTHREAD_CONFIG_BORDER_AGENT_EPHEMERAL_KEY_ENABLE EphemeralKeyManager mEphemeralKeyManager; #endif diff --git a/tests/nexus/test_border_agent.cpp b/tests/nexus/test_border_agent.cpp index d7d09e512..625e9e2ce 100644 --- a/tests/nexus/test_border_agent.cpp +++ b/tests/nexus/test_border_agent.cpp @@ -742,21 +742,25 @@ void TestBorderAgentEphemeralKey(void) VerifyOrQuit(node0.Get().GetCounters().mEpskcInvalidArgsErrors == 2); } -class MeshCoPServiceTester +//---------------------------------------------------------------------------------------------------------------------- + +class TxtDataTester { public: - MeshCoPServiceTester(BorderAgent &aBorderAgent) + TxtDataTester(BorderAgent &aBorderAgent) : mBorderAgent(aBorderAgent) , mIsRunning(false) , mUdpPort(0) { } - void HandleMeshCoPServiceChanged(void) + static void HandleServiceChanged(void *aContext) { static_cast(aContext)->HandleServiceChanged(); } + + void HandleServiceChanged(void) { mIsRunning = mBorderAgent.IsRunning(); mUdpPort = mBorderAgent.GetUdpPort(); - SuccessOrQuit(mBorderAgent.GetMeshCoPServiceTxtData(mTxtData)); + SuccessOrQuit(mBorderAgent.PrepareServiceTxtData(mTxtData)); } bool FindTxtEntry(const char *aKey, TxtEntry &aTxtEntry) @@ -777,17 +781,12 @@ public: return found; } - BorderAgent &mBorderAgent; - otBorderAgentMeshCoPServiceTxtData mTxtData; - bool mIsRunning; - uint16_t mUdpPort; + BorderAgent &mBorderAgent; + BorderAgent::ServiceTxtData mTxtData; + bool mIsRunning; + uint16_t mUdpPort; }; -static void HandleMeshCoPServiceChanged(void *aContext) -{ - static_cast(aContext)->HandleMeshCoPServiceChanged(); -} - template bool CheckObjectSameAsTxtEntryData(const TxtEntry &aTxtEntry, const ObjectType &aObject) { static_assert(!TypeTraits::IsPointer::kValue, "ObjectType must not be a pointer"); @@ -801,47 +800,47 @@ template <> bool CheckObjectSameAsTxtEntryData(const TxtEntry &aTxtEnt memcmp(aTxtEntry.mValue, aNameData.GetBuffer(), aNameData.GetLength()) == 0; } -void TestBorderAgentMeshCoPServiceChangedCallback(void) +void TestBorderAgentTxtDataCallback(void) { Core nexus; Node &node0 = nexus.CreateNode(); Log("------------------------------------------------------------------------------------------------------"); - Log("TestBorderAgentMeshCoPServiceChangedCallback"); + Log("TestBorderAgentTxtDataCallback"); nexus.AdvanceTime(0); - MeshCoPServiceTester meshCoPServiceTester(node0.Get()); - TxtEntry txtEntry; + TxtDataTester txtDataTester(node0.Get()); + TxtEntry txtEntry; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // 1. Set MeshCoP service change callback. Will get initial values. Log("Set MeshCoP service change callback and check initial values"); - node0.Get().SetMeshCoPServiceChangedCallback(HandleMeshCoPServiceChanged, &meshCoPServiceTester); + node0.Get().SetServiceChangedCallback(TxtDataTester::HandleServiceChanged, &txtDataTester); nexus.AdvanceTime(1); // 1.1 Check the initial TXT entries #if OPENTHREAD_CONFIG_BORDER_AGENT_ID_ENABLE - VerifyOrQuit(meshCoPServiceTester.FindTxtEntry("id", txtEntry)); + VerifyOrQuit(txtDataTester.FindTxtEntry("id", txtEntry)); BorderAgent::Id id; VerifyOrQuit(node0.Get().GetId(id) == kErrorNone); VerifyOrQuit(CheckObjectSameAsTxtEntryData(txtEntry, id)); #endif - VerifyOrQuit(meshCoPServiceTester.FindTxtEntry("nn", txtEntry)); + VerifyOrQuit(txtDataTester.FindTxtEntry("nn", txtEntry)); VerifyOrQuit(CheckObjectSameAsTxtEntryData(txtEntry, node0.Get().GetNetworkName().GetAsData())); - VerifyOrQuit(meshCoPServiceTester.FindTxtEntry("xp", txtEntry)); + VerifyOrQuit(txtDataTester.FindTxtEntry("xp", txtEntry)); VerifyOrQuit(CheckObjectSameAsTxtEntryData(txtEntry, node0.Get().GetExtPanId())); - VerifyOrQuit(meshCoPServiceTester.FindTxtEntry("tv", txtEntry)); + VerifyOrQuit(txtDataTester.FindTxtEntry("tv", txtEntry)); VerifyOrQuit(CheckObjectSameAsTxtEntryData(txtEntry, NameData(kThreadVersionString, strlen(kThreadVersionString)))); - VerifyOrQuit(meshCoPServiceTester.FindTxtEntry("xa", txtEntry)); + VerifyOrQuit(txtDataTester.FindTxtEntry("xa", txtEntry)); VerifyOrQuit(CheckObjectSameAsTxtEntryData(txtEntry, node0.Get().GetExtAddress())); - VerifyOrQuit(meshCoPServiceTester.FindTxtEntry("sb", txtEntry)); + VerifyOrQuit(txtDataTester.FindTxtEntry("sb", txtEntry)); - VerifyOrQuit(meshCoPServiceTester.FindTxtEntry("pt", txtEntry) == false); - VerifyOrQuit(meshCoPServiceTester.FindTxtEntry("at", txtEntry) == false); + VerifyOrQuit(txtDataTester.FindTxtEntry("pt", txtEntry) == false); + VerifyOrQuit(txtDataTester.FindTxtEntry("at", txtEntry) == false); // 1.2 Check the Border Agent state - VerifyOrQuit(meshCoPServiceTester.mIsRunning == false); - VerifyOrQuit(meshCoPServiceTester.mUdpPort == 0); + VerifyOrQuit(txtDataTester.mIsRunning == false); + VerifyOrQuit(txtDataTester.mUdpPort == 0); // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // 2. Join Thread network and check updated values and states. @@ -851,28 +850,28 @@ void TestBorderAgentMeshCoPServiceChangedCallback(void) // 2.1 Check the initial TXT entries #if OPENTHREAD_CONFIG_BORDER_AGENT_ID_ENABLE - VerifyOrQuit(meshCoPServiceTester.FindTxtEntry("id", txtEntry)); + VerifyOrQuit(txtDataTester.FindTxtEntry("id", txtEntry)); VerifyOrQuit(node0.Get().GetId(id) == kErrorNone); VerifyOrQuit(CheckObjectSameAsTxtEntryData(txtEntry, id)); #endif - VerifyOrQuit(meshCoPServiceTester.FindTxtEntry("nn", txtEntry)); + VerifyOrQuit(txtDataTester.FindTxtEntry("nn", txtEntry)); VerifyOrQuit(CheckObjectSameAsTxtEntryData(txtEntry, node0.Get().GetNetworkName().GetAsData())); - VerifyOrQuit(meshCoPServiceTester.FindTxtEntry("xp", txtEntry)); + VerifyOrQuit(txtDataTester.FindTxtEntry("xp", txtEntry)); VerifyOrQuit(CheckObjectSameAsTxtEntryData(txtEntry, node0.Get().GetExtPanId())); - VerifyOrQuit(meshCoPServiceTester.FindTxtEntry("tv", txtEntry)); + VerifyOrQuit(txtDataTester.FindTxtEntry("tv", txtEntry)); VerifyOrQuit(CheckObjectSameAsTxtEntryData(txtEntry, NameData(kThreadVersionString, strlen(kThreadVersionString)))); - VerifyOrQuit(meshCoPServiceTester.FindTxtEntry("xa", txtEntry)); + VerifyOrQuit(txtDataTester.FindTxtEntry("xa", txtEntry)); VerifyOrQuit(CheckObjectSameAsTxtEntryData(txtEntry, node0.Get().GetExtAddress())); - VerifyOrQuit(meshCoPServiceTester.FindTxtEntry("sb", txtEntry)); - VerifyOrQuit(meshCoPServiceTester.FindTxtEntry("pt", txtEntry)); + VerifyOrQuit(txtDataTester.FindTxtEntry("sb", txtEntry)); + VerifyOrQuit(txtDataTester.FindTxtEntry("pt", txtEntry)); VerifyOrQuit(CheckObjectSameAsTxtEntryData( txtEntry, BigEndian::HostSwap32(node0.Get().GetLeaderData().GetPartitionId()))); - VerifyOrQuit(meshCoPServiceTester.FindTxtEntry("at", txtEntry)); + VerifyOrQuit(txtDataTester.FindTxtEntry("at", txtEntry)); VerifyOrQuit(CheckObjectSameAsTxtEntryData(txtEntry, node0.Get().GetTimestamp())); // 2.2 Check the Border Agent state - VerifyOrQuit(meshCoPServiceTester.mIsRunning == true); - VerifyOrQuit(meshCoPServiceTester.mUdpPort != 0); + VerifyOrQuit(txtDataTester.mIsRunning == true); + VerifyOrQuit(txtDataTester.mUdpPort != 0); } } // namespace Nexus @@ -882,7 +881,7 @@ int main(void) { ot::Nexus::TestBorderAgent(); ot::Nexus::TestBorderAgentEphemeralKey(); - ot::Nexus::TestBorderAgentMeshCoPServiceChangedCallback(); + ot::Nexus::TestBorderAgentTxtDataCallback(); printf("All tests passed\n"); return 0; }