Skip to content

Commit 318d81e

Browse files
[NXP] allow user to connect devices to open network (project-chip#42138)
* [NXP] allow user to connect devices to open network Signed-off-by: Martin Girardot <[email protected]> * Restyled by whitespace * Restyled by clang-format * [NXP] Fix PR comment Signed-off-by: Martin Girardot <[email protected]> --------- Signed-off-by: Martin Girardot <[email protected]> Co-authored-by: Restyled.io <[email protected]>
1 parent c1fe27b commit 318d81e

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

src/lib/shell/commands/WiFi.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,22 @@ static CHIP_ERROR WiFiConnectHandler(int argc, char ** argv)
109109

110110
VerifyOrReturnError(GetWiFiDriver() != nullptr, CHIP_ERROR_NOT_IMPLEMENTED);
111111

112-
/* Command accepts running with SSID and password as parameters */
113-
VerifyOrReturnError((argc == 2), CHIP_ERROR_INVALID_ARGUMENT);
112+
/* Command accepts running with SSID and password (optional) as parameters */
113+
VerifyOrReturnError((argc == 1 || argc == 2), CHIP_ERROR_INVALID_ARGUMENT);
114114

115-
ByteSpan ssidSpan = ByteSpan(Uint8::from_const_char(argv[0]), strlen(argv[0]));
116-
ByteSpan passwordSpan = ByteSpan(Uint8::from_const_char(argv[1]), strlen(argv[1]));
117-
118-
VerifyOrReturnError(IsSpanUsable(ssidSpan) && IsSpanUsable(passwordSpan), CHIP_ERROR_INVALID_ARGUMENT);
115+
ByteSpan ssidSpan = ByteSpan(Uint8::from_const_char(argv[0]), strlen(argv[0]));
116+
VerifyOrReturnError(!ssidSpan.empty(), CHIP_ERROR_INVALID_ARGUMENT);
117+
ByteSpan passwordSpan;
118+
if (argc == 2)
119+
{
120+
passwordSpan = ByteSpan(Uint8::from_const_char(argv[1]), strlen(argv[1]));
121+
VerifyOrReturnError(!passwordSpan.empty(), CHIP_ERROR_INVALID_ARGUMENT);
122+
}
123+
else
124+
{
125+
// If no password is provided, use an empty password
126+
passwordSpan = ByteSpan();
127+
}
119128

120129
ChipLogProgress(Shell, "Adding/Updating network %s", argv[0]);
121130

@@ -150,7 +159,7 @@ void RegisterWiFiCommands()
150159
{
151160
static constexpr Command subCommands[] = {
152161
{ &WiFiModeHandler, "mode", "Get/Set wifi mode. Usage: wifi mode [disable|ap|sta]" },
153-
{ &WiFiConnectHandler, "connect", "Connect to AP. Usage: wifi connect <ssid> <psk>" },
162+
{ &WiFiConnectHandler, "connect", "Connect to AP. Usage: wifi connect <ssid> [<psk>]" },
154163
{ &WiFiDisconnectHandler, "disconnect", "Disconnect device from AP. Usage: wifi disconnect" },
155164
};
156165

src/platform/nxp/common/NetworkCommissioningWiFiDriver.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ CHIP_ERROR NXPWiFiDriver::Init(NetworkStatusChangeCallback * networkStatusChange
8080

8181
err = PersistedStorage::KeyValueStoreMgr().Get(kWiFiCredentialsKeyName, mSavedNetwork.credentials,
8282
sizeof(mSavedNetwork.credentials), &credentialsLen);
83-
if (err != CHIP_NO_ERROR)
83+
/* Password could be empty, do not return error if key not found,
84+
password is written in flash before SSID, if SSID is present but not password, it is not an error */
85+
if (err != CHIP_NO_ERROR && err != CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND)
8486
{
8587
ChipLogProgress(DeviceLayer, "WiFi network credentials not retrieved from persisted storage: %" CHIP_ERROR_FORMAT,
8688
err.Format());
@@ -117,9 +119,9 @@ void NXPWiFiDriver::Shutdown()
117119

118120
CHIP_ERROR NXPWiFiDriver::CommitConfiguration()
119121
{
120-
ReturnErrorOnFailure(PersistedStorage::KeyValueStoreMgr().Put(kWiFiSSIDKeyName, mStagingNetwork.ssid, mStagingNetwork.ssidLen));
121122
ReturnErrorOnFailure(PersistedStorage::KeyValueStoreMgr().Put(kWiFiCredentialsKeyName, mStagingNetwork.credentials,
122123
mStagingNetwork.credentialsLen));
124+
ReturnErrorOnFailure(PersistedStorage::KeyValueStoreMgr().Put(kWiFiSSIDKeyName, mStagingNetwork.ssid, mStagingNetwork.ssidLen));
123125
mSavedNetwork = mStagingNetwork;
124126

125127
return CHIP_NO_ERROR;

0 commit comments

Comments
 (0)