Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WiFi][Shell] Wifi Shell updates to improve shell commands. #31684

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@
#include <app/clusters/ota-requestor/OTATestEventTriggerDelegate.h>
#endif

#ifdef ENABLE_CHIP_SHELL
#include <lib/shell/commands/WiFi.h>
#endif

using namespace chip;
using namespace chip::TLV;
using namespace ::chip::Credentials;
Expand Down Expand Up @@ -202,6 +206,9 @@ CHIP_ERROR chip::NXP::App::AppTaskBase::Init()

#if CONFIG_CHIP_WIFI || CHIP_DEVICE_CONFIG_ENABLE_WPA
sNetworkCommissioningInstance.Init();
#ifdef ENABLE_CHIP_SHELL
Shell::SetWiFiDriver(chip::NXP::App::GetAppTask().GetWifiDriverInstance());
#endif
#endif
#if CONFIG_CHIP_OTA_REQUESTOR
if (err == CHIP_NO_ERROR)
Expand Down
6 changes: 6 additions & 0 deletions src/include/platform/ConnectivityManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ class ConnectivityManager
void MaintainOnDemandWiFiAP();
System::Clock::Timeout GetWiFiAPIdleTimeout();
void SetWiFiAPIdleTimeout(System::Clock::Timeout val);
CHIP_ERROR DisconnectNetwork();

// Thread Methods
bool IsThreadEnabled();
Expand Down Expand Up @@ -559,5 +560,10 @@ inline void ConnectivityManager::OnWiFiStationProvisionChange()
static_cast<ImplClass *>(this)->_OnWiFiStationProvisionChange();
}

inline CHIP_ERROR ConnectivityManager::DisconnectNetwork()
{
return static_cast<ImplClass *>(this)->_DisconnectNetwork();
}

} // namespace DeviceLayer
} // namespace chip
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class GenericConnectivityManagerImpl
void _SetUserSelectedMode(bool val);
uint16_t _GetUserSelectedModeTimeout();
void _SetUserSelectedModeTimeout(uint16_t val);
CHIP_ERROR _DisconnectNetwork();

private:
ImplClass * Impl() { return static_cast<ImplClass *>(this); }
Expand All @@ -71,6 +72,12 @@ template <class ImplClass>
inline void GenericConnectivityManagerImpl<ImplClass>::_SetUserSelectedModeTimeout(uint16_t val)
{}

template <class ImplClass>
inline CHIP_ERROR GenericConnectivityManagerImpl<ImplClass>::_DisconnectNetwork()
{
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
}

} // namespace Internal
} // namespace DeviceLayer
} // namespace chip
5 changes: 4 additions & 1 deletion src/lib/shell/commands/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ source_set("commands") {
}

if (chip_enable_wifi) {
sources += [ "WiFi.cpp" ]
sources += [
"WiFi.cpp",
"WiFi.h",
]
}

if (chip_enable_ble && chip_device_platform != "none") {
Expand Down
59 changes: 50 additions & 9 deletions src/lib/shell/commands/WiFi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,22 @@
#include <lib/shell/Commands.h>
#include <lib/shell/Engine.h>
#include <lib/shell/commands/Help.h>
#include <lib/shell/commands/WiFi.h>
#include <lib/shell/streamer.h>
#include <lib/support/Span.h>
#include <platform/CHIPDeviceLayer.h>
#include <platform/ConnectivityManager.h>
#include <platform/NetworkCommissioning.h>

using chip::DeviceLayer::ConnectivityManager;
using chip::DeviceLayer::ConnectivityMgr;
using namespace chip::DeviceLayer::NetworkCommissioning;

namespace chip {
namespace Shell {

static chip::Shell::Engine sShellWiFiSubCommands;
static Shell::Engine sShellWiFiSubCommands;
static DeviceLayer::NetworkCommissioning::WiFiDriver * sDriver;

static CHIP_ERROR WiFiHelpHandler(int argc, char ** argv)
{
Expand Down Expand Up @@ -104,13 +109,38 @@ static CHIP_ERROR WiFiModeHandler(int argc, char ** argv)

static CHIP_ERROR WiFiConnectHandler(int argc, char ** argv)
{
if (argc != 2)
{
return CHIP_ERROR_INVALID_ARGUMENT;
}
CHIP_ERROR error = CHIP_NO_ERROR;
uint8_t networkIndex;
char debugBuffer[CHIP_CONFIG_NETWORK_COMMISSIONING_DEBUG_TEXT_BUFFER_SIZE];
MutableCharSpan debugText(debugBuffer);

VerifyOrReturnError(GetWiFiDriver() != nullptr, CHIP_ERROR_NOT_IMPLEMENTED);

/* Command accepts running with SSID and password as parameters */
VerifyOrReturnError((argc == 2), CHIP_ERROR_INVALID_ARGUMENT);

ByteSpan ssidSpan = ByteSpan(Uint8::from_const_char(argv[0]), strlen(argv[0]));
ByteSpan passwordSpan = ByteSpan(Uint8::from_const_char(argv[1]), strlen(argv[1]));

VerifyOrReturnError(IsSpanUsable(ssidSpan) && IsSpanUsable(passwordSpan), CHIP_ERROR_INVALID_ARGUMENT);

ChipLogProgress(Shell, "Adding/Updating network %s", argv[0]);

/* AddOrUpdateNetwork() checks ssid length and password length. The network info is not persistent. */
GetWiFiDriver()->AddOrUpdateNetwork(ssidSpan, passwordSpan, debugText, networkIndex);
mihai-ignat marked this conversation as resolved.
Show resolved Hide resolved

ChipLogProgress(Shell, "Connecting to network");
/* Connection event will be returned in OnWiFiConnectivityChange from DeviceCallbacks.cpp */
GetWiFiDriver()->ConnectNetwork(ssidSpan, nullptr);

// TODO:Provision WiFi using WirelessDriver
return CHIP_ERROR_NOT_IMPLEMENTED;
return error;
}

static CHIP_ERROR WiFiDisconnectHandler(int argc, char ** argv)
{
VerifyOrReturnError((argc == 0), CHIP_ERROR_INVALID_ARGUMENT);

return ConnectivityMgr().DisconnectNetwork();
}

static CHIP_ERROR WiFiDispatch(int argc, char ** argv)
Expand All @@ -122,13 +152,24 @@ static CHIP_ERROR WiFiDispatch(int argc, char ** argv)
return sShellWiFiSubCommands.ExecCommand(argc, argv);
}

void SetWiFiDriver(WiFiDriver * driver)
{
sDriver = driver;
}

WiFiDriver * GetWiFiDriver()
{
return sDriver;
}

void RegisterWiFiCommands()
{
/// Subcommands for root command: `device <subcommand>`
static const shell_command_t sWiFiSubCommands[] = {
{ &WiFiHelpHandler, "help", "" },
{ &WiFiModeHandler, "mode", "Get/Set wifi mode. Usage: wifi mode [disable|ap|sta]." },
{ &WiFiConnectHandler, "connect", "Connect to AP. Usage: wifi connect ssid psk." },
{ &WiFiModeHandler, "mode", "Get/Set wifi mode. Usage: wifi mode [disable|ap|sta]" },
{ &WiFiConnectHandler, "connect", "Connect to AP. Usage: wifi connect <ssid> <psk>" },
{ &WiFiDisconnectHandler, "disconnect", "Disconnect device from AP. Usage: wifi disconnect" },
};
static const shell_command_t sWiFiCommand = { &WiFiDispatch, "wifi", "Usage: wifi <subcommand>" };

Expand Down
35 changes: 35 additions & 0 deletions src/lib/shell/commands/WiFi.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
*
* Copyright (c) 2024 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* @file
* Header that defines default shell commands for CHIP examples
*/

#pragma once

#include <lib/shell/Engine.h>
#include <platform/NetworkCommissioning.h>

namespace chip {
namespace Shell {

void SetWiFiDriver(DeviceLayer::NetworkCommissioning::WiFiDriver * driver);
DeviceLayer::NetworkCommissioning::WiFiDriver * GetWiFiDriver();

} // namespace Shell
} // namespace chip
28 changes: 28 additions & 0 deletions src/platform/nxp/common/ConnectivityManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,34 @@ void ConnectivityManagerImpl::ConnectNetworkTimerHandler(::chip::System::Layer *
PlatformMgr().UnlockChipStack();
}
}

/* Can be used to disconnect from WiFi network.
*/
CHIP_ERROR ConnectivityManagerImpl::_DisconnectNetwork(void)
{
int ret = 0;
CHIP_ERROR err = CHIP_NO_ERROR;

if (ConnectivityMgrImpl().IsWiFiStationConnected())
{
ChipLogProgress(NetworkProvisioning, "Disconnecting from WiFi network.");

ret = wlan_disconnect();

if (ret != WM_SUCCESS)
{
ChipLogError(NetworkProvisioning, "Failed to disconnect from network with error: %u", (uint8_t) ret);
err = CHIP_ERROR_UNEXPECTED_EVENT;
}
}
else
{
ChipLogError(NetworkProvisioning, "Error: WiFi not connected!");
err = CHIP_ERROR_INCORRECT_STATE;
}

return err;
}
#endif

} // namespace DeviceLayer
Expand Down
1 change: 1 addition & 0 deletions src/platform/nxp/common/ConnectivityManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ class ConnectivityManagerImpl final : public ConnectivityManager,
bool _IsWiFiStationEnabled();
bool _IsWiFiStationConnected();
bool _IsWiFiStationApplicationControlled();
CHIP_ERROR _DisconnectNetwork(void);
#endif /* CHIP_DEVICE_CONFIG_ENABLE_WPA */

// ===== Members for internal use by the following friends.
Expand Down
4 changes: 0 additions & 4 deletions src/platform/nxp/common/NetworkCommissioningDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,6 @@ class NXPWiFiDriver final : public WiFiDriver
Status ReorderNetwork(ByteSpan networkId, uint8_t index, MutableCharSpan & outDebugText) override;
void ConnectNetwork(ByteSpan networkId, ConnectCallback * callback) override;

/* Can be used to disconnect from WiFi network.
*/
int DisconnectNetwork();

/* Returns the network SSID. User needs to allocate a buffer of size >= DeviceLayer::Internal::kMaxWiFiSSIDLength.
* ssid - pointer to the returned SSID
*/
Expand Down
56 changes: 29 additions & 27 deletions src/platform/nxp/common/NetworkCommissioningWiFiDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,39 @@ Status NXPWiFiDriver::AddOrUpdateNetwork(ByteSpan ssid, ByteSpan credentials, Mu

Status NXPWiFiDriver::RemoveNetwork(ByteSpan networkId, MutableCharSpan & outDebugText, uint8_t & outNetworkIndex)
{
int err_code = 0;

outDebugText.reduce_size(0);
outNetworkIndex = 0;
VerifyOrReturnError(NetworkMatch(mStagingNetwork, networkId), Status::kNetworkIDNotFound);

// Use empty ssid for representing invalid network
mStagingNetwork.ssidLen = 0;
memset(mStagingNetwork.ssid, 0, DeviceLayer::Internal::kMaxWiFiSSIDLength);
memset(mStagingNetwork.credentials, 0, DeviceLayer::Internal::kMaxWiFiKeyLength);
err_code = wlan_remove_network((char *) networkId.data());

switch (err_code)
{
case -WM_E_INVAL:
ChipLogError(DeviceLayer, "Error: Network not found");
break;

case WM_SUCCESS:
/* Use empty ssid for representing invalid network */
mStagingNetwork.ssidLen = 0;
memset(mStagingNetwork.ssid, 0, DeviceLayer::Internal::kMaxWiFiSSIDLength);
memset(mStagingNetwork.credentials, 0, DeviceLayer::Internal::kMaxWiFiKeyLength);
/* Save to persistent memory */
CommitConfiguration();
ChipLogProgress(DeviceLayer, "Successfully removed network");
break;

case WLAN_ERROR_STATE:
ChipLogError(DeviceLayer, "Error: Can't remove network in this state");
break;

default:
ChipLogError(DeviceLayer, "Error: Unable to remove network");
break;
}

return Status::kSuccess;
}

Expand Down Expand Up @@ -238,29 +263,6 @@ void NXPWiFiDriver::ConnectNetwork(ByteSpan networkId, ConnectCallback * callbac
}
}

int NXPWiFiDriver::DisconnectNetwork(void)
{
int ret = 0;

if (ConnectivityMgrImpl().IsWiFiStationConnected())
{
ChipLogProgress(NetworkProvisioning, "Disconnecting from WiFi network.");

ret = wlan_disconnect();

if (ret != WM_SUCCESS)
{
ChipLogError(NetworkProvisioning, "Failed to disconnect from network with error: %u", (uint8_t) ret);
}
}
else
{
ChipLogError(NetworkProvisioning, "Error: WiFi not connected!");
}

return ret;
}

CHIP_ERROR NXPWiFiDriver::StartScanWiFiNetworks(ByteSpan ssid)
{
wlan_scan_params_v2_t wlan_scan_param;
Expand Down
6 changes: 1 addition & 5 deletions third_party/nxp/rt_sdk/rt_sdk.gni
Original file line number Diff line number Diff line change
Expand Up @@ -562,11 +562,7 @@ template("rt_sdk") {
}

if (chip_enable_wifi) {
if (!w8801_transceiver) {
defines += [ "CHIP_DEVICE_CONFIG_ENABLE_WIFI_STATION=1" ]
} else {
defines += [ "CHIP_DEVICE_CONFIG_ENABLE_WIFI_STATION=0" ]
}
defines += [ "CHIP_DEVICE_CONFIG_ENABLE_WIFI_STATION=1" ]
}

# Now add our "system-header" include dirs
Expand Down
Loading