18
18
#include < lib/shell/Commands.h>
19
19
#include < lib/shell/Engine.h>
20
20
#include < lib/shell/commands/Help.h>
21
+ #include < lib/shell/commands/WiFi.h>
21
22
#include < lib/shell/streamer.h>
23
+ #include < lib/support/Span.h>
22
24
#include < platform/CHIPDeviceLayer.h>
23
25
#include < platform/ConnectivityManager.h>
26
+ #include < platform/NetworkCommissioning.h>
24
27
25
28
using chip::DeviceLayer::ConnectivityManager;
26
29
using chip::DeviceLayer::ConnectivityMgr;
30
+ using namespace chip ::DeviceLayer::NetworkCommissioning;
27
31
28
32
namespace chip {
29
33
namespace Shell {
30
34
31
- static chip::Shell::Engine sShellWiFiSubCommands ;
35
+ static Shell::Engine sShellWiFiSubCommands ;
36
+ static DeviceLayer::NetworkCommissioning::WiFiDriver * sDriver ;
32
37
33
38
static CHIP_ERROR WiFiHelpHandler (int argc, char ** argv)
34
39
{
@@ -104,13 +109,59 @@ static CHIP_ERROR WiFiModeHandler(int argc, char ** argv)
104
109
105
110
static CHIP_ERROR WiFiConnectHandler (int argc, char ** argv)
106
111
{
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 )
108
158
{
109
- return CHIP_ERROR_INVALID_ARGUMENT ;
159
+ ssidSpan = ByteSpan ( reinterpret_cast < const uint8_t *>(argv[ 0 ]), strlen (argv[ 0 ])) ;
110
160
}
111
161
112
- // TODO:Provision WiFi using WirelessDriver
113
- return CHIP_ERROR_NOT_IMPLEMENTED;
162
+ GetWiFiDriver ()->ScanNetworks (ssidSpan, nullptr );
163
+
164
+ return CHIP_NO_ERROR;
114
165
}
115
166
116
167
static CHIP_ERROR WiFiDispatch (int argc, char ** argv)
@@ -122,13 +173,25 @@ static CHIP_ERROR WiFiDispatch(int argc, char ** argv)
122
173
return sShellWiFiSubCommands .ExecCommand (argc, argv);
123
174
}
124
175
176
+ void SetWiFiDriver (WiFiDriver * driver)
177
+ {
178
+ sDriver = driver;
179
+ }
180
+
181
+ WiFiDriver * GetWiFiDriver ()
182
+ {
183
+ return sDriver ;
184
+ }
185
+
125
186
void RegisterWiFiCommands ()
126
187
{
127
188
// / Subcommands for root command: `device <subcommand>`
128
189
static const shell_command_t sWiFiSubCommands [] = {
129
190
{ &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" },
132
195
};
133
196
static const shell_command_t sWiFiCommand = { &WiFiDispatch, " wifi" , " Usage: wifi <subcommand>" };
134
197
0 commit comments