Skip to content

Commit b050cb4

Browse files
committedApr 18, 2024
[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
1 parent 9a81bed commit b050cb4

File tree

5 files changed

+102
-10
lines changed

5 files changed

+102
-10
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

+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

0 commit comments

Comments
 (0)