Skip to content

Commit b34948a

Browse files
Address review comments and other small tweaks
- Factor out HaveNetworkCredentials() helper and use it consistently - Validate WPA credential in SetNetworkCredentials() - Don't emit an SSID change when only the passphrase changes - Use CHIPSafeCasts - Avoid std::bind (and we can't use std::bind_front yet) - Add a destructor that unregisters handlers
1 parent 9f1a3dd commit b34948a

File tree

3 files changed

+45
-14
lines changed

3 files changed

+45
-14
lines changed

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include <AppMain.h>
1919
#include <app/clusters/wifi-network-management-server/wifi-network-management-server.h>
20+
#include <lib/core/CHIPSafeCasts.h>
2021
#include <lib/support/Span.h>
2122

2223
using namespace chip;
@@ -28,7 +29,7 @@ void ApplicationShutdown() {}
2829

2930
ByteSpan ByteSpanFromCharSpan(CharSpan span)
3031
{
31-
return ByteSpan(reinterpret_cast<const uint8_t *>(span.data()), span.size());
32+
return ByteSpan(Uint8::from_const_char(span.data()), span.size());
3233
}
3334

3435
int main(int argc, char * argv[])
@@ -39,7 +40,7 @@ int main(int argc, char * argv[])
3940
}
4041

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

4445
ChipLinuxAppMainLoop();
4546
return 0;

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

+39-12
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
#include <lib/core/Global.h>
2525
#include <lib/support/CodeUtils.h>
2626

27-
#include <functional>
27+
#include <algorithm>
28+
#include <cctype>
2829

2930
using namespace chip;
3031
using namespace chip::app;
@@ -46,6 +47,12 @@ Server & Server::Instance()
4647

4748
Server::Server() : AttributeAccessInterface(NullOptional, Id), CommandHandlerInterface(NullOptional, Id) {}
4849

50+
Server::~Server()
51+
{
52+
unregisterAttributeAccessOverride(this);
53+
InteractionModelEngine::GetInstance()->UnregisterCommandHandler(this);
54+
}
55+
4956
CHIP_ERROR Server::Init(EndpointId endpoint)
5057
{
5158
VerifyOrReturnError(endpoint != kInvalidEndpointId, CHIP_ERROR_INVALID_ARGUMENT);
@@ -59,39 +66,58 @@ CHIP_ERROR Server::Init(EndpointId endpoint)
5966

6067
CHIP_ERROR Server::ClearNetworkCredentials()
6168
{
62-
VerifyOrReturnError(!SsidSpan().empty() || !PassphraseSpan().empty(), CHIP_NO_ERROR);
69+
VerifyOrReturnError(HaveNetworkCredentials(), CHIP_NO_ERROR);
6370

6471
mSsidLen = 0;
6572
mPassphrase.SetLength(0);
6673
MatterReportingAttributeChangeCallback(mEndpointId, Id, Ssid::Id);
6774
return CHIP_NO_ERROR;
6875
}
6976

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(), std::isxdigit);
88+
}
89+
return false;
90+
}
91+
7092
CHIP_ERROR Server::SetNetworkCredentials(ByteSpan ssid, ByteSpan passphrase)
7193
{
7294
VerifyOrReturnError(1 <= ssid.size() && ssid.size() <= sizeof(mSsid), CHIP_ERROR_INVALID_ARGUMENT);
73-
VerifyOrReturnError(1 <= passphrase.size() && passphrase.size() <= mPassphrase.Capacity(), CHIP_ERROR_INVALID_ARGUMENT);
95+
VerifyOrReturnError(IsValidWpaPersonalCredential(passphrase), CHIP_ERROR_INVALID_ARGUMENT);
7496

75-
VerifyOrReturnError(!SsidSpan().data_equal(ssid) || !PassphraseSpan().data_equal(passphrase), CHIP_NO_ERROR);
97+
bool ssidChanged = !SsidSpan().data_equal(ssid);
98+
bool passphraseChanged = !PassphraseSpan().data_equal(passphrase);
99+
VerifyOrReturnError(ssidChanged || passphraseChanged, CHIP_NO_ERROR);
76100

77101
memcpy(mSsid, ssid.data(), ssid.size());
78102
mSsidLen = static_cast<decltype(mSsidLen)>(ssid.size());
79103

80-
ReturnErrorOnFailure(mPassphrase.SetLength(passphrase.size()));
104+
VerifyOrDie(mPassphrase.SetLength(passphrase.size()) == CHIP_NO_ERROR);
81105
memcpy(mPassphrase.Bytes(), passphrase.data(), passphrase.size());
82106

83-
MatterReportingAttributeChangeCallback(mEndpointId, Id, Ssid::Id); // report SSID change even if only passphrase changed
107+
// Note: The spec currently defines no way to signal a passphrase change
108+
if (ssidChanged)
109+
{
110+
MatterReportingAttributeChangeCallback(mEndpointId, Id, Ssid::Id);
111+
}
84112
return CHIP_NO_ERROR;
85113
}
86114

87115
CHIP_ERROR Server::Read(const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder)
88116
{
89117
switch (aPath.mAttributeId)
90118
{
91-
case Ssid::Id: {
92-
auto ssid = SsidSpan();
93-
return (ssid.empty()) ? aEncoder.EncodeNull() : aEncoder.Encode(ssid);
94-
}
119+
case Ssid::Id:
120+
return HaveNetworkCredentials() ? aEncoder.Encode(SsidSpan()) : aEncoder.EncodeNull();
95121
}
96122
return CHIP_NO_ERROR;
97123
}
@@ -102,21 +128,22 @@ void Server::InvokeCommand(HandlerContext & ctx)
102128
{
103129
case Commands::NetworkPassphraseRequest::Id:
104130
HandleCommand<Commands::NetworkPassphraseRequest::DecodableType>(
105-
ctx, std::bind(&Server::HandleNetworkPassphraseRequest, this, _1, _2));
131+
ctx, [this](HandlerContext & ctx, const auto & req) { HandleNetworkPassphraseRequest(ctx, req); });
106132
return;
107133
}
108134
}
109135

110136
void Server::HandleNetworkPassphraseRequest(HandlerContext & ctx, const Commands::NetworkPassphraseRequest::DecodableType & req)
111137
{
112-
if (mPassphrase.Length() > 0)
138+
if (HaveNetworkCredentials())
113139
{
114140
Commands::NetworkPassphraseResponse::Type response;
115141
response.passphrase = mPassphrase.Span();
116142
ctx.mCommandHandler.AddResponse(ctx.mRequestPath, response);
117143
}
118144
else
119145
{
146+
// TODO: Status code TBC: https://github.com/CHIP-Specifications/connectedhomeip-spec/issues/9234
120147
ctx.mCommandHandler.AddStatus(ctx.mRequestPath, Protocols::InteractionModel::Status::InvalidInState);
121148
}
122149
}

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

+3
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class Server : private AttributeAccessInterface, private CommandHandlerInterface
4545
friend void ::emberAfWiFiNetworkManagementClusterServerInitCallback(chip::EndpointId);
4646

4747
Server();
48+
~Server();
4849
CHIP_ERROR Init(EndpointId endpoint);
4950

5051
Server(Server const &) = delete;
@@ -64,6 +65,8 @@ class Server : private AttributeAccessInterface, private CommandHandlerInterface
6465

6566
Crypto::SensitiveDataBuffer<64> mPassphrase;
6667
ByteSpan PassphraseSpan() const { return mPassphrase.Span(); }
68+
69+
bool HaveNetworkCredentials() { return mSsidLen > 0; }
6770
};
6871

6972
} // namespace WiFiNetworkManagement

0 commit comments

Comments
 (0)