19
19
#include < lib/shell/Engine.h>
20
20
#include < lib/shell/commands/Help.h>
21
21
#include < lib/shell/streamer.h>
22
+ #include < lib/support/Span.h>
22
23
#include < platform/CHIPDeviceLayer.h>
23
24
#include < platform/ConnectivityManager.h>
25
+ #include < platform/NetworkCommissioning.h>
24
26
25
27
using chip::DeviceLayer::ConnectivityManager;
26
28
using chip::DeviceLayer::ConnectivityMgr;
29
+ using namespace chip ::DeviceLayer::NetworkCommissioning;
27
30
28
31
namespace chip {
29
32
namespace Shell {
@@ -102,15 +105,172 @@ static CHIP_ERROR WiFiModeHandler(int argc, char ** argv)
102
105
return SetWiFiMode (argv[0 ]);
103
106
}
104
107
108
+ static CHIP_ERROR WifiAddNetwork (char * ssid, char * password)
109
+ {
110
+ if (ConnectivityMgr ().GetDriver () == nullptr )
111
+ {
112
+ return CHIP_ERROR_NOT_IMPLEMENTED;
113
+ }
114
+
115
+ uint8_t networkIndex;
116
+ chip::MutableCharSpan debugText;
117
+ chip::ByteSpan ssidSpan;
118
+ chip::ByteSpan passwordSpan;
119
+
120
+ ssidSpan = chip::ByteSpan (reinterpret_cast <const uint8_t *>(ssid), strlen (ssid));
121
+ passwordSpan = chip::ByteSpan (reinterpret_cast <const uint8_t *>(password), strlen (password));
122
+
123
+ if (IsSpanUsable (ssidSpan) && IsSpanUsable (passwordSpan))
124
+ {
125
+ ChipLogProgress (DeviceLayer, " [Shell] Adding/Updating network %s" , ssidSpan.data ());
126
+
127
+ /* AddOrUpdateNetwork() checks ssid length and password length. The network info is not persistent. */
128
+ ConnectivityMgr ().GetDriver ()->AddOrUpdateNetwork (ssidSpan, passwordSpan, debugText, networkIndex);
129
+
130
+ return CHIP_NO_ERROR;
131
+ }
132
+ else
133
+ {
134
+ return CHIP_ERROR_INVALID_ARGUMENT;
135
+ }
136
+ }
137
+
105
138
static CHIP_ERROR WiFiConnectHandler (int argc, char ** argv)
139
+ {
140
+ if (ConnectivityMgr ().GetDriver () == nullptr )
141
+ {
142
+ return CHIP_ERROR_NOT_IMPLEMENTED;
143
+ }
144
+
145
+ /* Command accepts running either without arguments or either with SSID and password */
146
+ /* Running without arguments implies that "wifi add ssid password" was executed before */
147
+ if ((argc != 0 ) && (argc != 2 ))
148
+ {
149
+ return CHIP_ERROR_INVALID_ARGUMENT;
150
+ }
151
+
152
+ chip::ByteSpan ssidSpan;
153
+ bool connectToNetwork = false ;
154
+
155
+ if (argc == 0 )
156
+ {
157
+ /* Retrieve previously added SSID */
158
+ char ssid[DeviceLayer::Internal::kMaxWiFiSSIDLength ] = { 0 };
159
+
160
+ ConnectivityMgr ().GetNetworkSSID ((char *) &ssid);
161
+ ssidSpan = chip::ByteSpan (reinterpret_cast <const uint8_t *>(ssid), strlen (ssid));
162
+
163
+ if (IsSpanUsable (ssidSpan))
164
+ {
165
+ connectToNetwork = true ;
166
+ }
167
+ else
168
+ {
169
+ ChipLogError (
170
+ DeviceLayer,
171
+ " [Shell] No network credentials found! Please add using <wifi add ssid password> or <wifi connect ssid password>" );
172
+ return CHIP_ERROR_INVALID_ARGUMENT;
173
+ }
174
+ }
175
+ else if (argc == 2 )
176
+ {
177
+ ssidSpan = chip::ByteSpan (reinterpret_cast <const uint8_t *>(argv[0 ]), strlen (argv[0 ]));
178
+
179
+ if ((IsSpanUsable (ssidSpan)) && (WifiAddNetwork (argv[0 ], argv[1 ]) == CHIP_NO_ERROR))
180
+ {
181
+ connectToNetwork = true ;
182
+ }
183
+ else
184
+ {
185
+ ChipLogError (DeviceLayer, " [Shell] Failed to add network credentials!" );
186
+ return CHIP_ERROR_INVALID_ARGUMENT;
187
+ }
188
+ }
189
+
190
+ if (connectToNetwork == true )
191
+ {
192
+ ChipLogProgress (DeviceLayer, " [Shell] Progress: Connecting to network" );
193
+ /* Connection event will be returned in OnWiFiConnectivityChange from DeviceCallbacks.cpp */
194
+ ConnectivityMgr ().GetDriver ()->ConnectNetwork (ssidSpan, nullptr );
195
+ }
196
+
197
+ return CHIP_NO_ERROR;
198
+ }
199
+
200
+ static CHIP_ERROR WiFiDisconnectHandler (int argc, char ** argv)
201
+ {
202
+ if (argc != 0 )
203
+ {
204
+ return CHIP_ERROR_INVALID_ARGUMENT;
205
+ }
206
+
207
+ return ConnectivityMgr ().DisconnectNetwork ();
208
+ }
209
+
210
+ static CHIP_ERROR WiFiAddNwkHandler (int argc, char ** argv)
106
211
{
107
212
if (argc != 2 )
108
213
{
109
214
return CHIP_ERROR_INVALID_ARGUMENT;
110
215
}
111
216
112
- // TODO:Provision WiFi using WirelessDriver
113
- return CHIP_ERROR_NOT_IMPLEMENTED;
217
+ return WifiAddNetwork (argv[0 ], argv[1 ]);
218
+ }
219
+
220
+ static CHIP_ERROR WiFiRemoveNwkHandler (int argc, char ** argv)
221
+ {
222
+ if (ConnectivityMgr ().GetDriver () == nullptr )
223
+ {
224
+ return CHIP_ERROR_NOT_IMPLEMENTED;
225
+ }
226
+
227
+ chip::DeviceLayer::NetworkCommissioning::Status status;
228
+ uint8_t networkIndex;
229
+ chip::ByteSpan ssidSpan;
230
+ char ssid[DeviceLayer::Internal::kMaxWiFiSSIDLength ] = { 0 };
231
+ chip::MutableCharSpan debugText;
232
+
233
+ ConnectivityMgr ().GetNetworkSSID ((char *) &ssid);
234
+ ssidSpan = chip::ByteSpan (reinterpret_cast <const uint8_t *>(ssid), strlen (ssid));
235
+
236
+ status = ConnectivityMgr ().GetDriver ()->RemoveNetwork (ssidSpan, debugText, networkIndex);
237
+
238
+ if (status != chip::DeviceLayer::NetworkCommissioning::Status::kSuccess )
239
+ {
240
+ ChipLogError (DeviceLayer, " [Shell] Error: RemoveNetwork: %u" , (uint8_t ) status);
241
+ }
242
+
243
+ return CHIP_NO_ERROR;
244
+ }
245
+
246
+ static CHIP_ERROR WiFiScanHandler (int argc, char ** argv)
247
+ {
248
+ if (ConnectivityMgr ().GetDriver () == nullptr )
249
+ {
250
+ return CHIP_ERROR_NOT_IMPLEMENTED;
251
+ }
252
+
253
+ /* Command accepts running either without arguments or either with ssid */
254
+ /* Running with argument ssid shall restrict the scan to the interested ssid */
255
+ if ((argc != 0 ) && (argc != 1 ))
256
+ {
257
+ return CHIP_ERROR_INVALID_ARGUMENT;
258
+ }
259
+
260
+ chip::ByteSpan ssidSpan;
261
+
262
+ if (argc == 0 )
263
+ {
264
+ ssidSpan = chip::ByteSpan ();
265
+ }
266
+ else if (argc == 1 )
267
+ {
268
+ ssidSpan = chip::ByteSpan (reinterpret_cast <const uint8_t *>(argv[0 ]), strlen (argv[0 ]));
269
+ }
270
+
271
+ ConnectivityMgr ().GetDriver ()->ScanNetworks (ssidSpan, nullptr );
272
+
273
+ return CHIP_NO_ERROR;
114
274
}
115
275
116
276
static CHIP_ERROR WiFiDispatch (int argc, char ** argv)
@@ -127,8 +287,12 @@ void RegisterWiFiCommands()
127
287
// / Subcommands for root command: `device <subcommand>`
128
288
static const shell_command_t sWiFiSubCommands [] = {
129
289
{ &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." },
290
+ { &WiFiModeHandler, " mode" , " Get/Set wifi mode. Usage: wifi mode [disable|ap|sta]" },
291
+ { &WiFiConnectHandler, " connect" , " Connect to AP. Usage: wifi connect <ssid> <psk>. ssid and psk are optional." },
292
+ { &WiFiDisconnectHandler, " disconnect" , " Disconnect device from AP. Usage: wifi disconnect" },
293
+ { &WiFiAddNwkHandler, " add" , " Add credentials for Wi-Fi network. Usage: wifi add <ssid> <psk>" },
294
+ { &WiFiRemoveNwkHandler, " remove" , " Remove credentials for Wi-Fi network. Usage: wifi remove <ssid>" },
295
+ { &WiFiScanHandler, " scan" , " Scan for Wi-Fi networks. Usage: wifi scan <ssid>. ssid is optional." },
132
296
};
133
297
static const shell_command_t sWiFiCommand = { &WiFiDispatch, " wifi" , " Usage: wifi <subcommand>" };
134
298
0 commit comments