From 48e276140637197fd7aeee1ecd9805c4d9e904bf Mon Sep 17 00:00:00 2001 From: Karsten Sperling <113487422+ksperling-apple@users.noreply.github.com> Date: Mon, 22 Jul 2024 12:03:14 +1200 Subject: [PATCH] Use manual instantiation for Wi-Fi Network Management server (#34408) * Use manual instantiation for Wi-Fi Network Management server Following the same pattern as Thread Network Directory server. * Remove unneeded chip:: prefix --- examples/network-manager-app/linux/main.cpp | 27 +++++++------ .../wifi-network-management-server.cpp | 38 +++---------------- .../wifi-network-management-server.h | 20 ++++------ src/app/common/templates/config-data.yaml | 1 - 4 files changed, 29 insertions(+), 57 deletions(-) diff --git a/examples/network-manager-app/linux/main.cpp b/examples/network-manager-app/linux/main.cpp index 6274be9a093268..84c4a2ce88e9a5 100644 --- a/examples/network-manager-app/linux/main.cpp +++ b/examples/network-manager-app/linux/main.cpp @@ -28,31 +28,36 @@ using namespace chip; using namespace chip::app; using namespace chip::app::Clusters; -void ApplicationInit() {} -void ApplicationShutdown() {} - ByteSpan ByteSpanFromCharSpan(CharSpan span) { return ByteSpan(Uint8::from_const_char(span.data()), span.size()); } std::optional gThreadNetworkDirectoryServer; -void emberAfThreadNetworkDirectoryClusterInitCallback(chip::EndpointId endpoint) +void emberAfThreadNetworkDirectoryClusterInitCallback(EndpointId endpoint) { VerifyOrDie(!gThreadNetworkDirectoryServer); gThreadNetworkDirectoryServer.emplace(endpoint).Init(); } -int main(int argc, char * argv[]) +std::optional gWiFiNetworkManagementServer; +void emberAfWiFiNetworkManagementClusterInitCallback(EndpointId endpoint) +{ + VerifyOrDie(!gWiFiNetworkManagementServer); + gWiFiNetworkManagementServer.emplace(endpoint).Init(); +} + +void ApplicationInit() { - if (ChipLinuxAppInit(argc, argv) != 0) - { - return -1; - } + gWiFiNetworkManagementServer->SetNetworkCredentials(ByteSpanFromCharSpan("MatterAP"_span), + ByteSpanFromCharSpan("Setec Astronomy"_span)); +} - WiFiNetworkManagementServer::Instance().SetNetworkCredentials(ByteSpanFromCharSpan("MatterAP"_span), - ByteSpanFromCharSpan("Setec Astronomy"_span)); +void ApplicationShutdown() {} +int main(int argc, char * argv[]) +{ + VerifyOrReturnValue(ChipLinuxAppInit(argc, argv) == 0, -1); ChipLinuxAppMainLoop(); return 0; } diff --git a/src/app/clusters/wifi-network-management-server/wifi-network-management-server.cpp b/src/app/clusters/wifi-network-management-server/wifi-network-management-server.cpp index 1a739e354bd3df..d2815b582f5ce1 100644 --- a/src/app/clusters/wifi-network-management-server/wifi-network-management-server.cpp +++ b/src/app/clusters/wifi-network-management-server/wifi-network-management-server.cpp @@ -20,8 +20,6 @@ #include #include #include -#include -#include #include #include @@ -55,18 +53,11 @@ bool IsValidWpaPersonalCredential(ByteSpan credential) return false; } -Global gWiFiNetworkManagementServerInstance; - } // namespace -WiFiNetworkManagementServer & WiFiNetworkManagementServer::Instance() -{ - return gWiFiNetworkManagementServerInstance.get(); -} - -WiFiNetworkManagementServer::WiFiNetworkManagementServer() : - AttributeAccessInterface(NullOptional, WiFiNetworkManagement::Id), - CommandHandlerInterface(NullOptional, WiFiNetworkManagement::Id) +WiFiNetworkManagementServer::WiFiNetworkManagementServer(EndpointId endpoint) : + AttributeAccessInterface(MakeOptional(endpoint), WiFiNetworkManagement::Id), + CommandHandlerInterface(MakeOptional(endpoint), WiFiNetworkManagement::Id) {} WiFiNetworkManagementServer::~WiFiNetworkManagementServer() @@ -75,12 +66,8 @@ WiFiNetworkManagementServer::~WiFiNetworkManagementServer() InteractionModelEngine::GetInstance()->UnregisterCommandHandler(this); } -CHIP_ERROR WiFiNetworkManagementServer::Init(EndpointId endpoint) +CHIP_ERROR WiFiNetworkManagementServer::Init() { - VerifyOrReturnError(endpoint != kInvalidEndpointId, CHIP_ERROR_INVALID_ARGUMENT); - VerifyOrReturnError(mEndpointId == kInvalidEndpointId, CHIP_ERROR_INCORRECT_STATE); - - mEndpointId = endpoint; VerifyOrReturnError(registerAttributeAccessOverride(this), CHIP_ERROR_INTERNAL); ReturnErrorOnFailure(InteractionModelEngine::GetInstance()->RegisterCommandHandler(this)); return CHIP_NO_ERROR; @@ -92,7 +79,7 @@ CHIP_ERROR WiFiNetworkManagementServer::ClearNetworkCredentials() mSsidLen = 0; mPassphrase.SetLength(0); - MatterReportingAttributeChangeCallback(mEndpointId, WiFiNetworkManagement::Id, Ssid::Id); + MatterReportingAttributeChangeCallback(GetEndpointId(), WiFiNetworkManagement::Id, Ssid::Id); return CHIP_NO_ERROR; } @@ -114,7 +101,7 @@ CHIP_ERROR WiFiNetworkManagementServer::SetNetworkCredentials(ByteSpan ssid, Byt // Note: The spec currently defines no way to signal a passphrase change if (ssidChanged) { - MatterReportingAttributeChangeCallback(mEndpointId, WiFiNetworkManagement::Id, Ssid::Id); + MatterReportingAttributeChangeCallback(GetEndpointId(), WiFiNetworkManagement::Id, Ssid::Id); } return CHIP_NO_ERROR; } @@ -160,17 +147,4 @@ void WiFiNetworkManagementServer::HandleNetworkPassphraseRequest(HandlerContext } // namespace app } // namespace chip -#if defined(MATTER_DM_WIFI_NETWORK_MANAGEMENT_CLUSTER_SERVER_ENDPOINT_COUNT) && \ - MATTER_DM_WIFI_NETWORK_MANAGEMENT_CLUSTER_SERVER_ENDPOINT_COUNT > 1 -#error Only a single Wi-Fi Network Management Cluster instance is supported. -#endif - void MatterWiFiNetworkManagementPluginServerInitCallback() {} - -void emberAfWiFiNetworkManagementClusterServerInitCallback(EndpointId endpoint) -{ - // We could delay constructing the instance until this point; however it's not - // clear if this is inconvenient in terms of forcing the application to initialize - // the network credentials later than it otherwise would. - LogErrorOnFailure(chip::app::Clusters::WiFiNetworkManagementServer::Instance().Init(endpoint)); -} diff --git a/src/app/clusters/wifi-network-management-server/wifi-network-management-server.h b/src/app/clusters/wifi-network-management-server/wifi-network-management-server.h index aa3cba89636a77..ce438edf79ff73 100644 --- a/src/app/clusters/wifi-network-management-server/wifi-network-management-server.h +++ b/src/app/clusters/wifi-network-management-server/wifi-network-management-server.h @@ -20,9 +20,7 @@ #include #include #include -#include #include -#include #include void emberAfWiFiNetworkManagementClusterServerInitCallback(chip::EndpointId); @@ -34,30 +32,26 @@ namespace Clusters { class WiFiNetworkManagementServer : private AttributeAccessInterface, private CommandHandlerInterface { public: - static WiFiNetworkManagementServer & Instance(); + WiFiNetworkManagementServer(EndpointId endpoint); + ~WiFiNetworkManagementServer(); + + CHIP_ERROR Init(); CHIP_ERROR ClearNetworkCredentials(); CHIP_ERROR SetNetworkCredentials(ByteSpan ssid, ByteSpan passphrase); -private: - friend Global; - friend void ::emberAfWiFiNetworkManagementClusterServerInitCallback(chip::EndpointId); - - WiFiNetworkManagementServer(); - ~WiFiNetworkManagementServer(); - CHIP_ERROR Init(EndpointId endpoint); - WiFiNetworkManagementServer(WiFiNetworkManagementServer const &) = delete; WiFiNetworkManagementServer & operator=(WiFiNetworkManagementServer const &) = delete; +private: + EndpointId GetEndpointId() { return AttributeAccessInterface::GetEndpointId().Value(); } + CHIP_ERROR Read(const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder) override; void InvokeCommand(HandlerContext & handlerContext) override; void HandleNetworkPassphraseRequest(HandlerContext & ctx, const WiFiNetworkManagement::Commands::NetworkPassphraseRequest::DecodableType & req); - EndpointId mEndpointId = kInvalidEndpointId; - uint8_t mSsid[32]; uint8_t mSsidLen = 0; static_assert(std::numeric_limits::max() >= sizeof(mSsid)); diff --git a/src/app/common/templates/config-data.yaml b/src/app/common/templates/config-data.yaml index e7121b2757e2f3..fd060149c8ff20 100644 --- a/src/app/common/templates/config-data.yaml +++ b/src/app/common/templates/config-data.yaml @@ -59,7 +59,6 @@ ClustersWithInitFunctions: - Mode Select - Sample MEI - Scenes Management - - Wi-Fi Network Management ClustersWithAttributeChangedFunctions: - Bridged Device Basic