Skip to content

Commit 570a7cb

Browse files
committed
[WiFi][Shell] Wifi Shell updates to improve shell commands.
- "wifi connect" command was improved to work with preset credentials or with credentials entered after - "wifi add" command was created to add network credentials - "wifi remove" command was created to remove network credentials - "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 943b870 commit 570a7cb

File tree

4 files changed

+235
-5
lines changed

4 files changed

+235
-5
lines changed

src/include/platform/ConnectivityManager.h

+25-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <lib/support/CodeUtils.h>
3131
#include <platform/CHIPDeviceConfig.h>
3232
#include <platform/CHIPDeviceEvent.h>
33+
#include <platform/NetworkCommissioning.h>
3334

3435
#include <app/util/basic-types.h>
3536

@@ -162,6 +163,9 @@ class ConnectivityManager
162163
void SetDelegate(ConnectivityManagerDelegate * delegate) { mDelegate = delegate; }
163164
ConnectivityManagerDelegate * GetDelegate() const { return mDelegate; }
164165

166+
void SetDriver(chip::DeviceLayer::NetworkCommissioning::WiFiDriver * driver) { mDriver = driver; }
167+
chip::DeviceLayer::NetworkCommissioning::WiFiDriver * GetDriver() const { return mDriver; }
168+
165169
chip::Inet::EndPointManager<Inet::UDPEndPoint> & UDPEndPointManager();
166170

167171
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
@@ -190,6 +194,10 @@ class ConnectivityManager
190194
void MaintainOnDemandWiFiAP();
191195
System::Clock::Timeout GetWiFiAPIdleTimeout();
192196
void SetWiFiAPIdleTimeout(System::Clock::Timeout val);
197+
CHIP_ERROR GetNetworkSSID(char * ssid);
198+
CHIP_ERROR GetNetworkPassword(char * credentials);
199+
CHIP_ERROR DisconnectNetwork(void);
200+
193201

194202
// Thread Methods
195203
ThreadMode GetThreadMode();
@@ -236,7 +244,8 @@ class ConnectivityManager
236244
static const char * CHIPoBLEServiceModeToStr(CHIPoBLEServiceMode mode);
237245

238246
private:
239-
ConnectivityManagerDelegate * mDelegate = nullptr;
247+
ConnectivityManagerDelegate * mDelegate = nullptr;
248+
chip::DeviceLayer::NetworkCommissioning::WiFiDriver * mDriver = nullptr;
240249

241250
// ===== Members for internal use by the following friends.
242251

@@ -606,5 +615,20 @@ inline void ConnectivityManager::OnWiFiStationProvisionChange()
606615
static_cast<ImplClass *>(this)->_OnWiFiStationProvisionChange();
607616
}
608617

618+
inline CHIP_ERROR ConnectivityManager::GetNetworkSSID(char * ssid)
619+
{
620+
return static_cast<ImplClass *>(this)->_GetNetworkSSID(ssid);
621+
}
622+
623+
inline CHIP_ERROR ConnectivityManager::GetNetworkPassword(char * credentials)
624+
{
625+
return static_cast<ImplClass *>(this)->_GetNetworkPassword(credentials);
626+
}
627+
628+
inline CHIP_ERROR ConnectivityManager::DisconnectNetwork(void)
629+
{
630+
return static_cast<ImplClass *>(this)->_DisconnectNetwork();
631+
}
632+
609633
} // namespace DeviceLayer
610634
} // namespace chip

src/include/platform/internal/GenericConnectivityManagerImpl_NoWiFi.h

+21
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ class GenericConnectivityManagerImpl_NoWiFi
7474
bool _CanStartWiFiScan();
7575
void _OnWiFiScanDone();
7676
void _OnWiFiStationProvisionChange();
77+
CHIP_ERROR _GetNetworkSSID(char * ssid);
78+
CHIP_ERROR _GetNetworkPassword(char * credentials);
79+
CHIP_ERROR _DisconnectNetwork(void);
7780
static const char * _WiFiStationModeToStr(ConnectivityManager::WiFiStationMode mode);
7881
static const char * _WiFiAPModeToStr(ConnectivityManager::WiFiAPMode mode);
7982
static const char * _WiFiStationStateToStr(ConnectivityManager::WiFiStationState state);
@@ -221,6 +224,24 @@ inline const char * GenericConnectivityManagerImpl_NoWiFi<ImplClass>::_WiFiAPSta
221224
return nullptr;
222225
}
223226

227+
template <class ImplClass>
228+
inline CHIP_ERROR GenericConnectivityManagerImpl_NoWiFi<ImplClass>::_GetNetworkSSID(char * ssid)
229+
{
230+
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
231+
}
232+
233+
template <class ImplClass>
234+
inline CHIP_ERROR GenericConnectivityManagerImpl_NoWiFi<ImplClass>::_GetNetworkPassword(char * credentials)
235+
{
236+
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
237+
}
238+
239+
template <class ImplClass>
240+
inline CHIP_ERROR GenericConnectivityManagerImpl_NoWiFi<ImplClass>::_DisconnectNetwork(void)
241+
{
242+
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
243+
}
244+
224245
} // namespace Internal
225246
} // namespace DeviceLayer
226247
} // namespace chip

src/include/platform/internal/GenericConnectivityManagerImpl_WiFi.h

+21
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ class GenericConnectivityManagerImpl_WiFi
8080
#if CHIP_CONFIG_ENABLE_ICD_SERVER && !CHIP_DEVICE_CONFIG_ENABLE_THREAD
8181
CHIP_ERROR _SetPollingInterval(System::Clock::Milliseconds32 pollingInterval);
8282
#endif
83+
CHIP_ERROR _GetNetworkSSID(char * ssid);
84+
CHIP_ERROR _GetNetworkPassword(char * credentials);
85+
CHIP_ERROR _DisconnectNetwork(void);
8386
static const char * _WiFiStationModeToStr(ConnectivityManager::WiFiStationMode mode);
8487
static const char * _WiFiAPModeToStr(ConnectivityManager::WiFiAPMode mode);
8588
static const char * _WiFiStationStateToStr(ConnectivityManager::WiFiStationState state);
@@ -193,6 +196,24 @@ inline CHIP_ERROR GenericConnectivityManagerImpl_WiFi<ImplClass>::_SetPollingInt
193196
}
194197
#endif
195198

199+
template <class ImplClass>
200+
inline CHIP_ERROR GenericConnectivityManagerImpl_WiFi<ImplClass>::_GetNetworkSSID(char * ssid)
201+
{
202+
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
203+
}
204+
205+
template <class ImplClass>
206+
inline CHIP_ERROR GenericConnectivityManagerImpl_WiFi<ImplClass>::_GetNetworkPassword(char * credentials)
207+
{
208+
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
209+
}
210+
211+
template <class ImplClass>
212+
inline CHIP_ERROR GenericConnectivityManagerImpl_WiFi<ImplClass>::_DisconnectNetwork(void)
213+
{
214+
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
215+
}
216+
196217
} // namespace Internal
197218
} // namespace DeviceLayer
198219
} // namespace chip

src/lib/shell/commands/WiFi.cpp

+168-4
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@
1919
#include <lib/shell/Engine.h>
2020
#include <lib/shell/commands/Help.h>
2121
#include <lib/shell/streamer.h>
22+
#include <lib/support/Span.h>
2223
#include <platform/CHIPDeviceLayer.h>
2324
#include <platform/ConnectivityManager.h>
25+
#include <platform/NetworkCommissioning.h>
2426

2527
using chip::DeviceLayer::ConnectivityManager;
2628
using chip::DeviceLayer::ConnectivityMgr;
29+
using namespace chip::DeviceLayer::NetworkCommissioning;
2730

2831
namespace chip {
2932
namespace Shell {
@@ -102,15 +105,172 @@ static CHIP_ERROR WiFiModeHandler(int argc, char ** argv)
102105
return SetWiFiMode(argv[0]);
103106
}
104107

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+
105138
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)
106211
{
107212
if (argc != 2)
108213
{
109214
return CHIP_ERROR_INVALID_ARGUMENT;
110215
}
111216

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;
114274
}
115275

116276
static CHIP_ERROR WiFiDispatch(int argc, char ** argv)
@@ -127,8 +287,12 @@ void RegisterWiFiCommands()
127287
/// Subcommands for root command: `device <subcommand>`
128288
static const shell_command_t sWiFiSubCommands[] = {
129289
{ &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." },
132296
};
133297
static const shell_command_t sWiFiCommand = { &WiFiDispatch, "wifi", "Usage: wifi <subcommand>" };
134298

0 commit comments

Comments
 (0)