Skip to content

Commit df038d6

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 df038d6

File tree

6 files changed

+140
-9
lines changed

6 files changed

+140
-9
lines changed

src/include/platform/ConnectivityManager.h

+7-1
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(void);
184185

185186
// Thread Methods
186187
bool IsThreadEnabled();
@@ -223,7 +224,7 @@ class ConnectivityManager
223224
static const char * CHIPoBLEServiceModeToStr(CHIPoBLEServiceMode mode);
224225

225226
private:
226-
ConnectivityManagerDelegate * mDelegate = nullptr;
227+
ConnectivityManagerDelegate * mDelegate = nullptr;
227228

228229
// ===== Members for internal use by the following friends.
229230

@@ -559,5 +560,10 @@ inline void ConnectivityManager::OnWiFiStationProvisionChange()
559560
static_cast<ImplClass *>(this)->_OnWiFiStationProvisionChange();
560561
}
561562

563+
inline CHIP_ERROR ConnectivityManager::DisconnectNetwork(void)
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(void);
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(void)
77+
{
78+
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
79+
}
80+
7481
} // namespace Internal
7582
} // namespace DeviceLayer
7683
} // namespace chip

src/include/platform/internal/GenericConnectivityManagerImpl_NoWiFi.h

+7
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class GenericConnectivityManagerImpl_NoWiFi
7474
bool _CanStartWiFiScan();
7575
void _OnWiFiScanDone();
7676
void _OnWiFiStationProvisionChange();
77+
CHIP_ERROR _DisconnectNetwork(void);
7778
static const char * _WiFiStationModeToStr(ConnectivityManager::WiFiStationMode mode);
7879
static const char * _WiFiAPModeToStr(ConnectivityManager::WiFiAPMode mode);
7980
static const char * _WiFiStationStateToStr(ConnectivityManager::WiFiStationState state);
@@ -221,6 +222,12 @@ inline const char * GenericConnectivityManagerImpl_NoWiFi<ImplClass>::_WiFiAPSta
221222
return nullptr;
222223
}
223224

225+
template <class ImplClass>
226+
inline CHIP_ERROR GenericConnectivityManagerImpl_NoWiFi<ImplClass>::_DisconnectNetwork(void)
227+
{
228+
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
229+
}
230+
224231
} // namespace Internal
225232
} // namespace DeviceLayer
226233
} // 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)