Skip to content

Commit 46b1b90

Browse files
Address review comments
- Rename Server -> WiFiNetworkManagementServer and move it up a namespace - Move internal definitions into an anonymous namespace
1 parent 6c59f6a commit 46b1b90

File tree

3 files changed

+52
-46
lines changed

3 files changed

+52
-46
lines changed

examples/network-manager-app/linux/main.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ int main(int argc, char * argv[])
3939
return -1;
4040
}
4141

42-
WiFiNetworkManagement::Server::Instance().SetNetworkCredentials(ByteSpanFromCharSpan("MatterAP"_span),
43-
ByteSpanFromCharSpan("Setec Astronomy"_span));
42+
WiFiNetworkManagementServer::Instance().SetNetworkCredentials(ByteSpanFromCharSpan("MatterAP"_span),
43+
ByteSpanFromCharSpan("Setec Astronomy"_span));
4444

4545
ChipLinuxAppMainLoop();
4646
return 0;

src/app/clusters/wifi-network-management-server/wifi-network-management-server.cpp

+41-34
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,51 @@ using namespace chip;
3131
using namespace chip::app;
3232
using namespace chip::app::Clusters;
3333
using namespace chip::app::Clusters::WiFiNetworkManagement::Attributes;
34+
using namespace chip::app::Clusters::WiFiNetworkManagement::Commands;
3435
using namespace std::placeholders;
3536

3637
namespace chip {
3738
namespace app {
3839
namespace Clusters {
39-
namespace WiFiNetworkManagement {
4040

41-
Global<Server> gServerInstance;
41+
namespace {
4242

43-
Server & Server::Instance()
43+
// TODO: Move this into lib/support somewhere and also use it network-commissioning.cpp
44+
bool IsValidWpaPersonalCredential(ByteSpan credential)
45+
{
46+
// As per spec section 11.9.7.3. AddOrUpdateWiFiNetwork Command
47+
if (8 <= credential.size() && credential.size() <= 63) // passphrase
48+
{
49+
return true;
50+
}
51+
if (credential.size() == 64) // raw hex psk
52+
{
53+
return std::all_of(credential.begin(), credential.end(), [](auto c) { return std::isxdigit(c); });
54+
}
55+
return false;
56+
}
57+
58+
Global<WiFiNetworkManagementServer> gWiFiNetworkManagementServerInstance;
59+
60+
} // namespace
61+
62+
WiFiNetworkManagementServer & WiFiNetworkManagementServer::Instance()
4463
{
45-
return gServerInstance.get();
64+
return gWiFiNetworkManagementServerInstance.get();
4665
}
4766

48-
Server::Server() : AttributeAccessInterface(NullOptional, Id), CommandHandlerInterface(NullOptional, Id) {}
67+
WiFiNetworkManagementServer::WiFiNetworkManagementServer() :
68+
AttributeAccessInterface(NullOptional, WiFiNetworkManagement::Id),
69+
CommandHandlerInterface(NullOptional, WiFiNetworkManagement::Id)
70+
{}
4971

50-
Server::~Server()
72+
WiFiNetworkManagementServer::~WiFiNetworkManagementServer()
5173
{
5274
unregisterAttributeAccessOverride(this);
5375
InteractionModelEngine::GetInstance()->UnregisterCommandHandler(this);
5476
}
5577

56-
CHIP_ERROR Server::Init(EndpointId endpoint)
78+
CHIP_ERROR WiFiNetworkManagementServer::Init(EndpointId endpoint)
5779
{
5880
VerifyOrReturnError(endpoint != kInvalidEndpointId, CHIP_ERROR_INVALID_ARGUMENT);
5981
VerifyOrReturnError(mEndpointId == kInvalidEndpointId, CHIP_ERROR_INCORRECT_STATE);
@@ -64,32 +86,17 @@ CHIP_ERROR Server::Init(EndpointId endpoint)
6486
return CHIP_NO_ERROR;
6587
}
6688

67-
CHIP_ERROR Server::ClearNetworkCredentials()
89+
CHIP_ERROR WiFiNetworkManagementServer::ClearNetworkCredentials()
6890
{
6991
VerifyOrReturnError(HaveNetworkCredentials(), CHIP_NO_ERROR);
7092

7193
mSsidLen = 0;
7294
mPassphrase.SetLength(0);
73-
MatterReportingAttributeChangeCallback(mEndpointId, Id, Ssid::Id);
95+
MatterReportingAttributeChangeCallback(mEndpointId, WiFiNetworkManagement::Id, Ssid::Id);
7496
return CHIP_NO_ERROR;
7597
}
7698

77-
// TODO: Move this into lib/support somewhere and also use it network-commissioning.cpp
78-
bool IsValidWpaPersonalCredential(ByteSpan credential)
79-
{
80-
// As per spec section 11.9.7.3. AddOrUpdateWiFiNetwork Command
81-
if (8 <= credential.size() && credential.size() <= 63) // passphrase
82-
{
83-
return true;
84-
}
85-
if (credential.size() == 64) // raw hex psk
86-
{
87-
return std::all_of(credential.begin(), credential.end(), [](auto c) { return std::isxdigit(c); });
88-
}
89-
return false;
90-
}
91-
92-
CHIP_ERROR Server::SetNetworkCredentials(ByteSpan ssid, ByteSpan passphrase)
99+
CHIP_ERROR WiFiNetworkManagementServer::SetNetworkCredentials(ByteSpan ssid, ByteSpan passphrase)
93100
{
94101
VerifyOrReturnError(1 <= ssid.size() && ssid.size() <= sizeof(mSsid), CHIP_ERROR_INVALID_ARGUMENT);
95102
VerifyOrReturnError(IsValidWpaPersonalCredential(passphrase), CHIP_ERROR_INVALID_ARGUMENT);
@@ -107,12 +114,12 @@ CHIP_ERROR Server::SetNetworkCredentials(ByteSpan ssid, ByteSpan passphrase)
107114
// Note: The spec currently defines no way to signal a passphrase change
108115
if (ssidChanged)
109116
{
110-
MatterReportingAttributeChangeCallback(mEndpointId, Id, Ssid::Id);
117+
MatterReportingAttributeChangeCallback(mEndpointId, WiFiNetworkManagement::Id, Ssid::Id);
111118
}
112119
return CHIP_NO_ERROR;
113120
}
114121

115-
CHIP_ERROR Server::Read(const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder)
122+
CHIP_ERROR WiFiNetworkManagementServer::Read(const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder)
116123
{
117124
switch (aPath.mAttributeId)
118125
{
@@ -122,22 +129,23 @@ CHIP_ERROR Server::Read(const ConcreteReadAttributePath & aPath, AttributeValueE
122129
return CHIP_NO_ERROR;
123130
}
124131

125-
void Server::InvokeCommand(HandlerContext & ctx)
132+
void WiFiNetworkManagementServer::InvokeCommand(HandlerContext & ctx)
126133
{
127134
switch (ctx.mRequestPath.mCommandId)
128135
{
129-
case Commands::NetworkPassphraseRequest::Id:
130-
HandleCommand<Commands::NetworkPassphraseRequest::DecodableType>(
136+
case NetworkPassphraseRequest::Id:
137+
HandleCommand<NetworkPassphraseRequest::DecodableType>(
131138
ctx, [this](HandlerContext & aCtx, const auto & req) { HandleNetworkPassphraseRequest(aCtx, req); });
132139
return;
133140
}
134141
}
135142

136-
void Server::HandleNetworkPassphraseRequest(HandlerContext & ctx, const Commands::NetworkPassphraseRequest::DecodableType & req)
143+
void WiFiNetworkManagementServer::HandleNetworkPassphraseRequest(HandlerContext & ctx,
144+
const NetworkPassphraseRequest::DecodableType & req)
137145
{
138146
if (HaveNetworkCredentials())
139147
{
140-
Commands::NetworkPassphraseResponse::Type response;
148+
NetworkPassphraseResponse::Type response;
141149
response.passphrase = mPassphrase.Span();
142150
ctx.mCommandHandler.AddResponse(ctx.mRequestPath, response);
143151
}
@@ -148,7 +156,6 @@ void Server::HandleNetworkPassphraseRequest(HandlerContext & ctx, const Commands
148156
}
149157
}
150158

151-
} // namespace WiFiNetworkManagement
152159
} // namespace Clusters
153160
} // namespace app
154161
} // namespace chip
@@ -165,5 +172,5 @@ void emberAfWiFiNetworkManagementClusterServerInitCallback(EndpointId endpoint)
165172
// We could delay constructing the instance until this point; however it's not
166173
// clear if this is inconvenient in terms of forcing the application to initialize
167174
// the network credentials later than it otherwise would.
168-
LogErrorOnFailure(chip::app::Clusters::WiFiNetworkManagement::Server::Instance().Init(endpoint));
175+
LogErrorOnFailure(chip::app::Clusters::WiFiNetworkManagementServer::Instance().Init(endpoint));
169176
}

src/app/clusters/wifi-network-management-server/wifi-network-management-server.h

+9-10
Original file line numberDiff line numberDiff line change
@@ -30,31 +30,31 @@ void emberAfWiFiNetworkManagementClusterServerInitCallback(chip::EndpointId);
3030
namespace chip {
3131
namespace app {
3232
namespace Clusters {
33-
namespace WiFiNetworkManagement {
3433

35-
class Server : private AttributeAccessInterface, private CommandHandlerInterface
34+
class WiFiNetworkManagementServer : private AttributeAccessInterface, private CommandHandlerInterface
3635
{
3736
public:
38-
static Server & Instance();
37+
static WiFiNetworkManagementServer & Instance();
3938

4039
CHIP_ERROR ClearNetworkCredentials();
4140
CHIP_ERROR SetNetworkCredentials(ByteSpan ssid, ByteSpan passphrase);
4241

4342
private:
44-
friend Global<Server>;
43+
friend Global<WiFiNetworkManagementServer>;
4544
friend void ::emberAfWiFiNetworkManagementClusterServerInitCallback(chip::EndpointId);
4645

47-
Server();
48-
~Server();
46+
WiFiNetworkManagementServer();
47+
~WiFiNetworkManagementServer();
4948
CHIP_ERROR Init(EndpointId endpoint);
5049

51-
Server(Server const &) = delete;
52-
Server & operator=(Server const &) = delete;
50+
WiFiNetworkManagementServer(WiFiNetworkManagementServer const &) = delete;
51+
WiFiNetworkManagementServer & operator=(WiFiNetworkManagementServer const &) = delete;
5352

5453
CHIP_ERROR Read(const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder) override;
5554
void InvokeCommand(HandlerContext & handlerContext) override;
5655

57-
void HandleNetworkPassphraseRequest(HandlerContext & ctx, const Commands::NetworkPassphraseRequest::DecodableType & req);
56+
void HandleNetworkPassphraseRequest(HandlerContext & ctx,
57+
const WiFiNetworkManagement::Commands::NetworkPassphraseRequest::DecodableType & req);
5858

5959
EndpointId mEndpointId = kInvalidEndpointId;
6060

@@ -69,7 +69,6 @@ class Server : private AttributeAccessInterface, private CommandHandlerInterface
6969
bool HaveNetworkCredentials() { return mSsidLen > 0; }
7070
};
7171

72-
} // namespace WiFiNetworkManagement
7372
} // namespace Clusters
7473
} // namespace app
7574
} // namespace chip

0 commit comments

Comments
 (0)