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,38 @@ 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 )
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 );
111
135
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 ();
114
144
}
115
145
116
146
static CHIP_ERROR WiFiDispatch (int argc, char ** argv)
@@ -122,13 +152,24 @@ static CHIP_ERROR WiFiDispatch(int argc, char ** argv)
122
152
return sShellWiFiSubCommands .ExecCommand (argc, argv);
123
153
}
124
154
155
+ void SetWiFiDriver (WiFiDriver * driver)
156
+ {
157
+ sDriver = driver;
158
+ }
159
+
160
+ WiFiDriver * GetWiFiDriver ()
161
+ {
162
+ return sDriver ;
163
+ }
164
+
125
165
void RegisterWiFiCommands ()
126
166
{
127
167
// / Subcommands for root command: `device <subcommand>`
128
168
static const shell_command_t sWiFiSubCommands [] = {
129
169
{ &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" },
132
173
};
133
174
static const shell_command_t sWiFiCommand = { &WiFiDispatch, " wifi" , " Usage: wifi <subcommand>" };
134
175
0 commit comments