Skip to content

Commit b1878fd

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 b1878fd

File tree

5 files changed

+122
-8
lines changed

5 files changed

+122
-8
lines changed

src/include/platform/ConnectivityManager.h

+6
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ class ConnectivityManager
181181
void MaintainOnDemandWiFiAP();
182182
System::Clock::Timeout GetWiFiAPIdleTimeout();
183183
void SetWiFiAPIdleTimeout(System::Clock::Timeout val);
184+
CHIP_ERROR DisconnectNetwork();
184185

185186
// Thread Methods
186187
bool IsThreadEnabled();
@@ -559,5 +560,10 @@ inline void ConnectivityManager::OnWiFiStationProvisionChange()
559560
static_cast<ImplClass *>(this)->_OnWiFiStationProvisionChange();
560561
}
561562

563+
inline CHIP_ERROR ConnectivityManager::DisconnectNetwork()
564+
{
565+
return static_cast<ImplClass *>(this)->_DisconnectNetwork();
566+
}
567+
562568
} // namespace DeviceLayer
563569
} // 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
@@ -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

+70-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,59 @@ 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, CHIP_ERROR_NOT_IMPLEMENTED);
119+
120+
/* Command accepts running with SSID and password as parameters */
121+
VerifyOrReturnError((argc == 2), 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+
VerifyOrReturnError(IsSpanUsable(ssidSpan) && IsSpanUsable(passwordSpan), CHIP_ERROR_INVALID_ARGUMENT);
127+
128+
ChipLogProgress(Shell, "Adding/Updating network %s", argv[0]);
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(Shell, "Connecting to network");
134+
/* Connection event will be returned in OnWiFiConnectivityChange from DeviceCallbacks.cpp */
135+
GetWiFiDriver()->ConnectNetwork(ssidSpan, nullptr);
136+
137+
return error;
138+
}
139+
140+
static CHIP_ERROR WiFiDisconnectHandler(int argc, char ** argv)
141+
{
142+
VerifyOrReturnError((argc == 0), CHIP_ERROR_INVALID_ARGUMENT);
143+
144+
return ConnectivityMgr().DisconnectNetwork();
145+
}
146+
147+
static CHIP_ERROR WiFiScanHandler(int argc, char ** argv)
148+
{
149+
ByteSpan ssidSpan;
150+
151+
VerifyOrReturnError(GetWiFiDriver() != nullptr, CHIP_ERROR_NOT_IMPLEMENTED);
152+
153+
/* Command accepts running either without arguments or either with ssid */
154+
/* Running with argument ssid shall restrict the scan to the interested ssid */
155+
VerifyOrReturnError((argc == 0) || (argc == 1), CHIP_ERROR_INVALID_ARGUMENT);
156+
157+
if (argc == 1)
108158
{
109-
return CHIP_ERROR_INVALID_ARGUMENT;
159+
ssidSpan = ByteSpan(reinterpret_cast<const uint8_t *>(argv[0]), strlen(argv[0]));
110160
}
111161

112-
// TODO:Provision WiFi using WirelessDriver
113-
return CHIP_ERROR_NOT_IMPLEMENTED;
162+
GetWiFiDriver()->ScanNetworks(ssidSpan, nullptr);
163+
164+
return CHIP_NO_ERROR;
114165
}
115166

116167
static CHIP_ERROR WiFiDispatch(int argc, char ** argv)
@@ -122,13 +173,25 @@ static CHIP_ERROR WiFiDispatch(int argc, char ** argv)
122173
return sShellWiFiSubCommands.ExecCommand(argc, argv);
123174
}
124175

176+
void SetWiFiDriver(WiFiDriver * driver)
177+
{
178+
sDriver = driver;
179+
}
180+
181+
WiFiDriver * GetWiFiDriver()
182+
{
183+
return sDriver;
184+
}
185+
125186
void RegisterWiFiCommands()
126187
{
127188
/// Subcommands for root command: `device <subcommand>`
128189
static const shell_command_t sWiFiSubCommands[] = {
129190
{ &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." },
191+
{ &WiFiModeHandler, "mode", "Get/Set wifi mode. Usage: wifi mode [disable|ap|sta]" },
192+
{ &WiFiConnectHandler, "connect", "Connect to AP. Usage: wifi connect <ssid> <psk>" },
193+
{ &WiFiDisconnectHandler, "disconnect", "Disconnect device from AP. Usage: wifi disconnect" },
194+
{ &WiFiScanHandler, "scan", "Scan for Wi-Fi networks. Usage: wifi scan <ssid>. ssid is optional" },
132195
};
133196
static const shell_command_t sWiFiCommand = { &WiFiDispatch, "wifi", "Usage: wifi <subcommand>" };
134197

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

0 commit comments

Comments
 (0)