Skip to content

Commit ffdeff7

Browse files
committed
[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 - "wifi scan" command was added to provide the user the available WiFi networks
1 parent 9a81bed commit ffdeff7

7 files changed

+149
-9
lines changed

src/include/platform/ConnectivityManager.h

+8-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <platform/CHIPDeviceEvent.h>
3232

3333
#include <app/util/basic-types.h>
34+
#include <lib/support/Span.h>
3435

3536
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
3637
#include <inet/TCPEndPoint.h>
@@ -181,6 +182,7 @@ class ConnectivityManager
181182
void MaintainOnDemandWiFiAP();
182183
System::Clock::Timeout GetWiFiAPIdleTimeout();
183184
void SetWiFiAPIdleTimeout(System::Clock::Timeout val);
185+
CHIP_ERROR DisconnectNetwork(void);
184186

185187
// Thread Methods
186188
bool IsThreadEnabled();
@@ -223,7 +225,7 @@ class ConnectivityManager
223225
static const char * CHIPoBLEServiceModeToStr(CHIPoBLEServiceMode mode);
224226

225227
private:
226-
ConnectivityManagerDelegate * mDelegate = nullptr;
228+
ConnectivityManagerDelegate * mDelegate = nullptr;
227229

228230
// ===== Members for internal use by the following friends.
229231

@@ -559,5 +561,10 @@ inline void ConnectivityManager::OnWiFiStationProvisionChange()
559561
static_cast<ImplClass *>(this)->_OnWiFiStationProvisionChange();
560562
}
561563

564+
inline CHIP_ERROR ConnectivityManager::DisconnectNetwork(void)
565+
{
566+
return static_cast<ImplClass *>(this)->_DisconnectNetwork();
567+
}
568+
562569
} // namespace DeviceLayer
563570
} // namespace chip

src/include/platform/internal/GenericConnectivityManagerImpl.h

+5
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(void);
4950

5051
private:
5152
ImplClass * Impl() { return static_cast<ImplClass *>(this); }
@@ -71,6 +72,10 @@ 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(void)
77+
{}
78+
7479
} // namespace Internal
7580
} // namespace DeviceLayer
7681
} // namespace chip

src/include/platform/internal/GenericConnectivityManagerImpl_NoWiFi.h

+9
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
#pragma once
2626

27+
#include <lib/support/Span.h>
28+
2729
namespace chip {
2830
namespace DeviceLayer {
2931
namespace Internal {
@@ -74,6 +76,7 @@ class GenericConnectivityManagerImpl_NoWiFi
7476
bool _CanStartWiFiScan();
7577
void _OnWiFiScanDone();
7678
void _OnWiFiStationProvisionChange();
79+
CHIP_ERROR _DisconnectNetwork(void);
7780
static const char * _WiFiStationModeToStr(ConnectivityManager::WiFiStationMode mode);
7881
static const char * _WiFiAPModeToStr(ConnectivityManager::WiFiAPMode mode);
7982
static const char * _WiFiStationStateToStr(ConnectivityManager::WiFiStationState state);
@@ -221,6 +224,12 @@ inline const char * GenericConnectivityManagerImpl_NoWiFi<ImplClass>::_WiFiAPSta
221224
return nullptr;
222225
}
223226

227+
template <class ImplClass>
228+
inline CHIP_ERROR GenericConnectivityManagerImpl_NoWiFi<ImplClass>::_DisconnectNetwork(void)
229+
{
230+
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
231+
}
232+
224233
} // namespace Internal
225234
} // namespace DeviceLayer
226235
} // namespace chip

src/include/platform/internal/GenericConnectivityManagerImpl_WiFi.h

+8
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#include <app/icd/server/ICDServerConfig.h>
2727
#include <lib/support/logging/CHIPLogging.h>
28+
#include <lib/support/Span.h>
2829
#include <platform/ConnectivityManager.h>
2930
#include <platform/internal/DeviceNetworkInfo.h>
3031

@@ -81,6 +82,7 @@ class GenericConnectivityManagerImpl_WiFi
8182
#if CHIP_CONFIG_ENABLE_ICD_SERVER && !CHIP_DEVICE_CONFIG_ENABLE_THREAD
8283
CHIP_ERROR _SetPollingInterval(System::Clock::Milliseconds32 pollingInterval);
8384
#endif
85+
CHIP_ERROR _DisconnectNetwork(void);
8486
static const char * _WiFiStationModeToStr(ConnectivityManager::WiFiStationMode mode);
8587
static const char * _WiFiAPModeToStr(ConnectivityManager::WiFiAPMode mode);
8688
static const char * _WiFiStationStateToStr(ConnectivityManager::WiFiStationState state);
@@ -194,6 +196,12 @@ inline CHIP_ERROR GenericConnectivityManagerImpl_WiFi<ImplClass>::_SetPollingInt
194196
}
195197
#endif
196198

199+
template <class ImplClass>
200+
inline CHIP_ERROR GenericConnectivityManagerImpl_WiFi<ImplClass>::_DisconnectNetwork(void)
201+
{
202+
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
203+
}
204+
197205
} // namespace Internal
198206
} // namespace DeviceLayer
199207
} // namespace chip

src/lib/shell/commands/BUILD.gn

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

3838
if (chip_enable_wifi) {
39-
sources += [ "WiFi.cpp" ]
39+
sources += [
40+
"WiFi.cpp",
41+
"WiFi.h",
42+
]
4043
}
4144

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

src/lib/shell/commands/WiFi.cpp

+80-7
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,69 @@ static CHIP_ERROR WiFiModeHandler(int argc, char ** argv)
104109

105110
static CHIP_ERROR WiFiConnectHandler(int argc, char ** argv)
106111
{
107-
if (argc != 2)
112+
CHIP_ERROR error = CHIP_NO_ERROR;
113+
uint8_t networkIndex;
114+
MutableCharSpan debugText;
115+
ByteSpan ssidSpan;
116+
ByteSpan passwordSpan;
117+
118+
VerifyOrReturnError(GetWiFiDriver() != nullptr, error = CHIP_ERROR_NOT_IMPLEMENTED);
119+
120+
/* Command accepts running with SSID and password as parameters */
121+
VerifyOrReturnError((argc == 2), error = CHIP_ERROR_INVALID_ARGUMENT);
122+
123+
ssidSpan = ByteSpan(reinterpret_cast<const uint8_t *>(argv[0]), strlen(argv[0]));
124+
passwordSpan = ByteSpan(reinterpret_cast<const uint8_t *>(argv[1]), strlen(argv[1]));
125+
126+
if (IsSpanUsable(ssidSpan) && IsSpanUsable(passwordSpan))
108127
{
109-
return CHIP_ERROR_INVALID_ARGUMENT;
128+
ChipLogProgress(DeviceLayer, "[Shell] Adding/Updating network %s", ssidSpan.data());
129+
130+
/* AddOrUpdateNetwork() checks ssid length and password length. The network info is not persistent. */
131+
GetWiFiDriver()->AddOrUpdateNetwork(ssidSpan, passwordSpan, debugText, networkIndex);
132+
133+
ChipLogProgress(DeviceLayer, "[Shell] Progress: Connecting to network");
134+
/* Connection event will be returned in OnWiFiConnectivityChange from DeviceCallbacks.cpp */
135+
GetWiFiDriver()->ConnectNetwork(ssidSpan, nullptr);
136+
}
137+
else
138+
{
139+
ChipLogError(DeviceLayer, "[Shell] Failed to add network credentials!");
140+
error = CHIP_ERROR_INVALID_ARGUMENT;
141+
}
142+
143+
return error;
144+
}
145+
146+
static CHIP_ERROR WiFiDisconnectHandler(int argc, char ** argv)
147+
{
148+
VerifyOrReturnError((argc == 0), CHIP_ERROR_INVALID_ARGUMENT);
149+
150+
return ConnectivityMgr().DisconnectNetwork();
151+
}
152+
153+
static CHIP_ERROR WiFiScanHandler(int argc, char ** argv)
154+
{
155+
ByteSpan ssidSpan;
156+
157+
VerifyOrReturnError(GetWiFiDriver() != nullptr, CHIP_ERROR_NOT_IMPLEMENTED);
158+
159+
/* Command accepts running either without arguments or either with ssid */
160+
/* Running with argument ssid shall restrict the scan to the interested ssid */
161+
VerifyOrReturnError((argc == 0) || (argc == 1), CHIP_ERROR_INVALID_ARGUMENT);
162+
163+
if (argc == 0)
164+
{
165+
ssidSpan = ByteSpan();
166+
}
167+
else if (argc == 1)
168+
{
169+
ssidSpan = ByteSpan(reinterpret_cast<const uint8_t *>(argv[0]), strlen(argv[0]));
110170
}
111171

112-
// TODO:Provision WiFi using WirelessDriver
113-
return CHIP_ERROR_NOT_IMPLEMENTED;
172+
GetWiFiDriver()->ScanNetworks(ssidSpan, nullptr);
173+
174+
return CHIP_NO_ERROR;
114175
}
115176

116177
static CHIP_ERROR WiFiDispatch(int argc, char ** argv)
@@ -122,13 +183,25 @@ static CHIP_ERROR WiFiDispatch(int argc, char ** argv)
122183
return sShellWiFiSubCommands.ExecCommand(argc, argv);
123184
}
124185

186+
void SetWiFiDriver(WiFiDriver * driver)
187+
{
188+
sDriver = driver;
189+
}
190+
191+
WiFiDriver * GetWiFiDriver()
192+
{
193+
return sDriver;
194+
}
195+
125196
void RegisterWiFiCommands()
126197
{
127198
/// Subcommands for root command: `device <subcommand>`
128199
static const shell_command_t sWiFiSubCommands[] = {
129200
{ &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." },
201+
{ &WiFiModeHandler, "mode", "Get/Set wifi mode. Usage: wifi mode [disable|ap|sta]" },
202+
{ &WiFiConnectHandler, "connect", "Connect to AP. Usage: wifi connect <ssid> <psk>" },
203+
{ &WiFiDisconnectHandler, "disconnect", "Disconnect device from AP. Usage: wifi disconnect" },
204+
{ &WiFiScanHandler, "scan", "Scan for Wi-Fi networks. Usage: wifi scan <ssid>. ssid is optional" },
132205
};
133206
static const shell_command_t sWiFiCommand = { &WiFiDispatch, "wifi", "Usage: wifi <subcommand>" };
134207

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(chip::DeviceLayer::NetworkCommissioning::WiFiDriver * driver);
32+
chip::DeviceLayer::NetworkCommissioning::WiFiDriver * GetWiFiDriver();
33+
34+
} // namespace Shell
35+
} // namespace chip

0 commit comments

Comments
 (0)