Skip to content

Commit

Permalink
Use manual instantiation for Wi-Fi Network Management server (#34408)
Browse files Browse the repository at this point in the history
* Use manual instantiation for Wi-Fi Network Management server

Following the same pattern as Thread Network Directory server.

* Remove unneeded chip:: prefix
  • Loading branch information
ksperling-apple authored and pull[bot] committed Oct 2, 2024
1 parent 638cebd commit 48e2761
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 57 deletions.
27 changes: 16 additions & 11 deletions examples/network-manager-app/linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<DefaultThreadNetworkDirectoryServer> gThreadNetworkDirectoryServer;
void emberAfThreadNetworkDirectoryClusterInitCallback(chip::EndpointId endpoint)
void emberAfThreadNetworkDirectoryClusterInitCallback(EndpointId endpoint)
{
VerifyOrDie(!gThreadNetworkDirectoryServer);
gThreadNetworkDirectoryServer.emplace(endpoint).Init();
}

int main(int argc, char * argv[])
std::optional<WiFiNetworkManagementServer> 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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
#include <app/AttributeAccessInterfaceRegistry.h>
#include <app/InteractionModelEngine.h>
#include <app/reporting/reporting.h>
#include <app/util/config.h>
#include <lib/core/Global.h>
#include <lib/support/CodeUtils.h>

#include <algorithm>
Expand Down Expand Up @@ -55,18 +53,11 @@ bool IsValidWpaPersonalCredential(ByteSpan credential)
return false;
}

Global<WiFiNetworkManagementServer> 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()
Expand All @@ -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;
Expand All @@ -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;
}

Expand All @@ -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;
}
Expand Down Expand Up @@ -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));
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
#include <app-common/zap-generated/cluster-objects.h>
#include <app/AttributeAccessInterface.h>
#include <app/CommandHandlerInterface.h>
#include <crypto/CHIPCryptoPAL.h>
#include <lib/core/CHIPError.h>
#include <lib/core/Global.h>
#include <lib/support/Span.h>

void emberAfWiFiNetworkManagementClusterServerInitCallback(chip::EndpointId);
Expand All @@ -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<WiFiNetworkManagementServer>;
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<decltype(mSsidLen)>::max() >= sizeof(mSsid));
Expand Down
1 change: 0 additions & 1 deletion src/app/common/templates/config-data.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ ClustersWithInitFunctions:
- Mode Select
- Sample MEI
- Scenes Management
- Wi-Fi Network Management

ClustersWithAttributeChangedFunctions:
- Bridged Device Basic
Expand Down

0 comments on commit 48e2761

Please sign in to comment.