Skip to content

Commit 8403f81

Browse files
[nxp fromtree][WiFi][Shell] Wifi Shell updates to improve shell commands. (project-chip#31684)
* [WiFi][Shell] Wifi Shell updates to improve shell commands. - "wifi connect" command was improved to work with credentials entered from shell - "wifi disconnect" command was created to disconnect from connected WiFi network * [NXP][platform][common] Update RemoveNetwork and Disconnect functions and enable CHIP_DEVICE_CONFIG_ENABLE_WIFI_STATION for all NXP WiFi platforms * Restyled by clang-format --------- Co-authored-by: Restyled.io <commits@restyled.io>
1 parent 574ed96 commit 8403f81

File tree

11 files changed

+168
-46
lines changed

11 files changed

+168
-46
lines changed

examples/platform/nxp/common/app_task/source/AppTaskBase.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@
6363
#include <app/clusters/ota-requestor/OTATestEventTriggerDelegate.h>
6464
#endif
6565

66+
#ifdef ENABLE_CHIP_SHELL
67+
#include <lib/shell/commands/WiFi.h>
68+
#endif
69+
6670
using namespace chip;
6771
using namespace chip::TLV;
6872
using namespace ::chip::Credentials;
@@ -214,6 +218,9 @@ CHIP_ERROR chip::NXP::App::AppTaskBase::Init()
214218

215219
#if CONFIG_CHIP_WIFI || CHIP_DEVICE_CONFIG_ENABLE_WPA
216220
sNetworkCommissioningInstance.Init();
221+
#ifdef ENABLE_CHIP_SHELL
222+
Shell::SetWiFiDriver(chip::NXP::App::GetAppTask().GetWifiDriverInstance());
223+
#endif
217224
#endif
218225
#if CHIP_DEVICE_CONFIG_ENABLE_OTA_REQUESTOR
219226
if (err == CHIP_NO_ERROR)

src/include/platform/ConnectivityManager.h

+6
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ class ConnectivityManager
190190
void MaintainOnDemandWiFiAP();
191191
System::Clock::Timeout GetWiFiAPIdleTimeout();
192192
void SetWiFiAPIdleTimeout(System::Clock::Timeout val);
193+
CHIP_ERROR DisconnectNetwork();
193194

194195
// Thread Methods
195196
ThreadMode GetThreadMode();
@@ -586,5 +587,10 @@ inline void ConnectivityManager::OnWiFiStationProvisionChange()
586587
static_cast<ImplClass *>(this)->_OnWiFiStationProvisionChange();
587588
}
588589

590+
inline CHIP_ERROR ConnectivityManager::DisconnectNetwork()
591+
{
592+
return static_cast<ImplClass *>(this)->_DisconnectNetwork();
593+
}
594+
589595
} // namespace DeviceLayer
590596
} // namespace chip

src/include/platform/internal/GenericConnectivityManagerImpl.h

+7
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class GenericConnectivityManagerImpl
4646
void _SetUserSelectedMode(bool val);
4747
uint16_t _GetUserSelectedModeTimeout();
4848
void _SetUserSelectedModeTimeout(uint16_t val);
49+
CHIP_ERROR _DisconnectNetwork();
4950

5051
private:
5152
ImplClass * Impl() { return static_cast<ImplClass *>(this); }
@@ -71,6 +72,12 @@ template <class ImplClass>
7172
inline void GenericConnectivityManagerImpl<ImplClass>::_SetUserSelectedModeTimeout(uint16_t val)
7273
{}
7374

75+
template <class ImplClass>
76+
inline CHIP_ERROR GenericConnectivityManagerImpl<ImplClass>::_DisconnectNetwork()
77+
{
78+
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
79+
}
80+
7481
} // namespace Internal
7582
} // namespace DeviceLayer
7683
} // namespace chip

src/lib/shell/commands/BUILD.gn

+4-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ source_set("commands") {
3737
}
3838

3939
if (chip_enable_wifi) {
40-
sources += [ "WiFi.cpp" ]
40+
sources += [
41+
"WiFi.cpp",
42+
"WiFi.h",
43+
]
4144
}
4245

4346
if (chip_enable_ble && chip_device_platform != "none") {

src/lib/shell/commands/WiFi.cpp

+50-9
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,22 @@
1818
#include <lib/shell/Commands.h>
1919
#include <lib/shell/Engine.h>
2020
#include <lib/shell/commands/Help.h>
21+
#include <lib/shell/commands/WiFi.h>
2122
#include <lib/shell/streamer.h>
23+
#include <lib/support/Span.h>
2224
#include <platform/CHIPDeviceLayer.h>
2325
#include <platform/ConnectivityManager.h>
26+
#include <platform/NetworkCommissioning.h>
2427

2528
using chip::DeviceLayer::ConnectivityManager;
2629
using chip::DeviceLayer::ConnectivityMgr;
30+
using namespace chip::DeviceLayer::NetworkCommissioning;
2731

2832
namespace chip {
2933
namespace Shell {
3034

31-
static chip::Shell::Engine sShellWiFiSubCommands;
35+
static Shell::Engine sShellWiFiSubCommands;
36+
static DeviceLayer::NetworkCommissioning::WiFiDriver * sDriver;
3237

3338
static CHIP_ERROR WiFiHelpHandler(int argc, char ** argv)
3439
{
@@ -104,13 +109,38 @@ static CHIP_ERROR WiFiModeHandler(int argc, char ** argv)
104109

105110
static CHIP_ERROR WiFiConnectHandler(int argc, char ** argv)
106111
{
107-
if (argc != 2)
108-
{
109-
return CHIP_ERROR_INVALID_ARGUMENT;
110-
}
112+
CHIP_ERROR error = CHIP_NO_ERROR;
113+
uint8_t networkIndex;
114+
char debugBuffer[CHIP_CONFIG_NETWORK_COMMISSIONING_DEBUG_TEXT_BUFFER_SIZE];
115+
MutableCharSpan debugText(debugBuffer);
116+
117+
VerifyOrReturnError(GetWiFiDriver() != nullptr, CHIP_ERROR_NOT_IMPLEMENTED);
118+
119+
/* Command accepts running with SSID and password as parameters */
120+
VerifyOrReturnError((argc == 2), CHIP_ERROR_INVALID_ARGUMENT);
121+
122+
ByteSpan ssidSpan = ByteSpan(Uint8::from_const_char(argv[0]), strlen(argv[0]));
123+
ByteSpan passwordSpan = ByteSpan(Uint8::from_const_char(argv[1]), strlen(argv[1]));
124+
125+
VerifyOrReturnError(IsSpanUsable(ssidSpan) && IsSpanUsable(passwordSpan), CHIP_ERROR_INVALID_ARGUMENT);
126+
127+
ChipLogProgress(Shell, "Adding/Updating network %s", argv[0]);
128+
129+
/* AddOrUpdateNetwork() checks ssid length and password length. The network info is not persistent. */
130+
GetWiFiDriver()->AddOrUpdateNetwork(ssidSpan, passwordSpan, debugText, networkIndex);
131+
132+
ChipLogProgress(Shell, "Connecting to network");
133+
/* Connection event will be returned in OnWiFiConnectivityChange from DeviceCallbacks.cpp */
134+
GetWiFiDriver()->ConnectNetwork(ssidSpan, nullptr);
111135

112-
// TODO:Provision WiFi using WirelessDriver
113-
return CHIP_ERROR_NOT_IMPLEMENTED;
136+
return error;
137+
}
138+
139+
static CHIP_ERROR WiFiDisconnectHandler(int argc, char ** argv)
140+
{
141+
VerifyOrReturnError((argc == 0), CHIP_ERROR_INVALID_ARGUMENT);
142+
143+
return ConnectivityMgr().DisconnectNetwork();
114144
}
115145

116146
static CHIP_ERROR WiFiDispatch(int argc, char ** argv)
@@ -122,13 +152,24 @@ static CHIP_ERROR WiFiDispatch(int argc, char ** argv)
122152
return sShellWiFiSubCommands.ExecCommand(argc, argv);
123153
}
124154

155+
void SetWiFiDriver(WiFiDriver * driver)
156+
{
157+
sDriver = driver;
158+
}
159+
160+
WiFiDriver * GetWiFiDriver()
161+
{
162+
return sDriver;
163+
}
164+
125165
void RegisterWiFiCommands()
126166
{
127167
/// Subcommands for root command: `device <subcommand>`
128168
static const shell_command_t sWiFiSubCommands[] = {
129169
{ &WiFiHelpHandler, "help", "" },
130-
{ &WiFiModeHandler, "mode", "Get/Set wifi mode. Usage: wifi mode [disable|ap|sta]." },
131-
{ &WiFiConnectHandler, "connect", "Connect to AP. Usage: wifi connect ssid psk." },
170+
{ &WiFiModeHandler, "mode", "Get/Set wifi mode. Usage: wifi mode [disable|ap|sta]" },
171+
{ &WiFiConnectHandler, "connect", "Connect to AP. Usage: wifi connect <ssid> <psk>" },
172+
{ &WiFiDisconnectHandler, "disconnect", "Disconnect device from AP. Usage: wifi disconnect" },
132173
};
133174
static const shell_command_t sWiFiCommand = { &WiFiDispatch, "wifi", "Usage: wifi <subcommand>" };
134175

src/lib/shell/commands/WiFi.h

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
*
3+
* Copyright (c) 2024 Project CHIP Authors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* @file
20+
* Header that defines default shell commands for CHIP examples
21+
*/
22+
23+
#pragma once
24+
25+
#include <lib/shell/Engine.h>
26+
#include <platform/NetworkCommissioning.h>
27+
28+
namespace chip {
29+
namespace Shell {
30+
31+
void SetWiFiDriver(DeviceLayer::NetworkCommissioning::WiFiDriver * driver);
32+
DeviceLayer::NetworkCommissioning::WiFiDriver * GetWiFiDriver();
33+
34+
} // namespace Shell
35+
} // namespace chip

src/platform/nxp/common/ConnectivityManagerImpl.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,34 @@ CHIP_ERROR ConnectivityManagerImpl::_SetPollingInterval(System::Clock::Milliseco
605605
return CHIP_NO_ERROR;
606606
}
607607
#endif // CHIP_CONFIG_ENABLE_ICD_SERVER
608+
609+
/* Can be used to disconnect from WiFi network.
610+
*/
611+
CHIP_ERROR ConnectivityManagerImpl::_DisconnectNetwork(void)
612+
{
613+
int ret = 0;
614+
CHIP_ERROR err = CHIP_NO_ERROR;
615+
616+
if (ConnectivityMgrImpl().IsWiFiStationConnected())
617+
{
618+
ChipLogProgress(NetworkProvisioning, "Disconnecting from WiFi network.");
619+
620+
ret = wlan_disconnect();
621+
622+
if (ret != WM_SUCCESS)
623+
{
624+
ChipLogError(NetworkProvisioning, "Failed to disconnect from network with error: %u", (uint8_t) ret);
625+
err = CHIP_ERROR_UNEXPECTED_EVENT;
626+
}
627+
}
628+
else
629+
{
630+
ChipLogError(NetworkProvisioning, "Error: WiFi not connected!");
631+
err = CHIP_ERROR_INCORRECT_STATE;
632+
}
633+
634+
return err;
635+
}
608636
#endif
609637

610638
} // namespace DeviceLayer

src/platform/nxp/common/ConnectivityManagerImpl.h

+1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ class ConnectivityManagerImpl final : public ConnectivityManager,
118118
bool _IsWiFiStationEnabled();
119119
bool _IsWiFiStationConnected();
120120
bool _IsWiFiStationApplicationControlled();
121+
CHIP_ERROR _DisconnectNetwork(void);
121122
#endif /* CHIP_DEVICE_CONFIG_ENABLE_WPA */
122123

123124
// ===== Members for internal use by the following friends.

src/platform/nxp/common/NetworkCommissioningDriver.h

-4
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,6 @@ class NXPWiFiDriver final : public WiFiDriver
7272
Status ReorderNetwork(ByteSpan networkId, uint8_t index, MutableCharSpan & outDebugText) override;
7373
void ConnectNetwork(ByteSpan networkId, ConnectCallback * callback) override;
7474

75-
/* Can be used to disconnect from WiFi network.
76-
*/
77-
int DisconnectNetwork();
78-
7975
/* Returns the network SSID. User needs to allocate a buffer of size >= DeviceLayer::Internal::kMaxWiFiSSIDLength.
8076
* ssid - pointer to the returned SSID
8177
*/

src/platform/nxp/common/NetworkCommissioningWiFiDriver.cpp

+29-27
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,39 @@ Status NXPWiFiDriver::AddOrUpdateNetwork(ByteSpan ssid, ByteSpan credentials, Mu
142142

143143
Status NXPWiFiDriver::RemoveNetwork(ByteSpan networkId, MutableCharSpan & outDebugText, uint8_t & outNetworkIndex)
144144
{
145+
int err_code = 0;
146+
145147
outDebugText.reduce_size(0);
146148
outNetworkIndex = 0;
147149
VerifyOrReturnError(NetworkMatch(mStagingNetwork, networkId), Status::kNetworkIDNotFound);
148150

149-
// Use empty ssid for representing invalid network
150-
mStagingNetwork.ssidLen = 0;
151-
memset(mStagingNetwork.ssid, 0, DeviceLayer::Internal::kMaxWiFiSSIDLength);
152-
memset(mStagingNetwork.credentials, 0, DeviceLayer::Internal::kMaxWiFiKeyLength);
151+
err_code = wlan_remove_network((char *) networkId.data());
152+
153+
switch (err_code)
154+
{
155+
case -WM_E_INVAL:
156+
ChipLogError(DeviceLayer, "Error: Network not found");
157+
break;
158+
159+
case WM_SUCCESS:
160+
/* Use empty ssid for representing invalid network */
161+
mStagingNetwork.ssidLen = 0;
162+
memset(mStagingNetwork.ssid, 0, DeviceLayer::Internal::kMaxWiFiSSIDLength);
163+
memset(mStagingNetwork.credentials, 0, DeviceLayer::Internal::kMaxWiFiKeyLength);
164+
/* Save to persistent memory */
165+
CommitConfiguration();
166+
ChipLogProgress(DeviceLayer, "Successfully removed network");
167+
break;
168+
169+
case WLAN_ERROR_STATE:
170+
ChipLogError(DeviceLayer, "Error: Can't remove network in this state");
171+
break;
172+
173+
default:
174+
ChipLogError(DeviceLayer, "Error: Unable to remove network");
175+
break;
176+
}
177+
153178
return Status::kSuccess;
154179
}
155180

@@ -238,29 +263,6 @@ void NXPWiFiDriver::ConnectNetwork(ByteSpan networkId, ConnectCallback * callbac
238263
}
239264
}
240265

241-
int NXPWiFiDriver::DisconnectNetwork(void)
242-
{
243-
int ret = 0;
244-
245-
if (ConnectivityMgrImpl().IsWiFiStationConnected())
246-
{
247-
ChipLogProgress(NetworkProvisioning, "Disconnecting from WiFi network.");
248-
249-
ret = wlan_disconnect();
250-
251-
if (ret != WM_SUCCESS)
252-
{
253-
ChipLogError(NetworkProvisioning, "Failed to disconnect from network with error: %u", (uint8_t) ret);
254-
}
255-
}
256-
else
257-
{
258-
ChipLogError(NetworkProvisioning, "Error: WiFi not connected!");
259-
}
260-
261-
return ret;
262-
}
263-
264266
CHIP_ERROR NXPWiFiDriver::StartScanWiFiNetworks(ByteSpan ssid)
265267
{
266268
wlan_scan_params_v2_t wlan_scan_param;

third_party/nxp/rt_sdk/rt_sdk.gni

+1-5
Original file line numberDiff line numberDiff line change
@@ -611,11 +611,7 @@ template("rt_sdk") {
611611
}
612612

613613
if (chip_enable_wifi) {
614-
if (!w8801_transceiver) {
615-
defines += [ "CHIP_DEVICE_CONFIG_ENABLE_WIFI_STATION=1" ]
616-
} else {
617-
defines += [ "CHIP_DEVICE_CONFIG_ENABLE_WIFI_STATION=0" ]
618-
}
614+
defines += [ "CHIP_DEVICE_CONFIG_ENABLE_WIFI_STATION=1" ]
619615
}
620616

621617
# Now add our "system-header" include dirs

0 commit comments

Comments
 (0)