diff --git a/examples/platform/nxp/common/app_task/source/AppTaskBase.cpp b/examples/platform/nxp/common/app_task/source/AppTaskBase.cpp index e03634e9807aa5..2f0bbcbf85b653 100644 --- a/examples/platform/nxp/common/app_task/source/AppTaskBase.cpp +++ b/examples/platform/nxp/common/app_task/source/AppTaskBase.cpp @@ -62,6 +62,10 @@ #include #endif +#ifdef ENABLE_CHIP_SHELL +#include +#endif + using namespace chip; using namespace chip::TLV; using namespace ::chip::Credentials; @@ -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) diff --git a/src/include/platform/ConnectivityManager.h b/src/include/platform/ConnectivityManager.h index 90bab2a5ba5e76..cb3ad0a41d0fed 100644 --- a/src/include/platform/ConnectivityManager.h +++ b/src/include/platform/ConnectivityManager.h @@ -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(); @@ -559,5 +560,10 @@ inline void ConnectivityManager::OnWiFiStationProvisionChange() static_cast(this)->_OnWiFiStationProvisionChange(); } +inline CHIP_ERROR ConnectivityManager::DisconnectNetwork() +{ + return static_cast(this)->_DisconnectNetwork(); +} + } // namespace DeviceLayer } // namespace chip diff --git a/src/include/platform/internal/GenericConnectivityManagerImpl.h b/src/include/platform/internal/GenericConnectivityManagerImpl.h index a56e92d17d0d8d..c2aa896bf06411 100644 --- a/src/include/platform/internal/GenericConnectivityManagerImpl.h +++ b/src/include/platform/internal/GenericConnectivityManagerImpl.h @@ -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(this); } @@ -71,6 +72,12 @@ template inline void GenericConnectivityManagerImpl::_SetUserSelectedModeTimeout(uint16_t val) {} +template +inline CHIP_ERROR GenericConnectivityManagerImpl::_DisconnectNetwork() +{ + return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE; +} + } // namespace Internal } // namespace DeviceLayer } // namespace chip diff --git a/src/lib/shell/commands/BUILD.gn b/src/lib/shell/commands/BUILD.gn index d4dd5820ad7f7a..f3da2760f641b1 100644 --- a/src/lib/shell/commands/BUILD.gn +++ b/src/lib/shell/commands/BUILD.gn @@ -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") { diff --git a/src/lib/shell/commands/WiFi.cpp b/src/lib/shell/commands/WiFi.cpp index 9bd891f20430da..35ab5e26ae23a2 100644 --- a/src/lib/shell/commands/WiFi.cpp +++ b/src/lib/shell/commands/WiFi.cpp @@ -18,17 +18,22 @@ #include #include #include +#include #include +#include #include #include +#include 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) { @@ -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); + + 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) @@ -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 ` 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 " }, + { &WiFiDisconnectHandler, "disconnect", "Disconnect device from AP. Usage: wifi disconnect" }, }; static const shell_command_t sWiFiCommand = { &WiFiDispatch, "wifi", "Usage: wifi " }; diff --git a/src/lib/shell/commands/WiFi.h b/src/lib/shell/commands/WiFi.h new file mode 100644 index 00000000000000..858a960f5b11d0 --- /dev/null +++ b/src/lib/shell/commands/WiFi.h @@ -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 +#include + +namespace chip { +namespace Shell { + +void SetWiFiDriver(DeviceLayer::NetworkCommissioning::WiFiDriver * driver); +DeviceLayer::NetworkCommissioning::WiFiDriver * GetWiFiDriver(); + +} // namespace Shell +} // namespace chip diff --git a/src/platform/nxp/common/ConnectivityManagerImpl.cpp b/src/platform/nxp/common/ConnectivityManagerImpl.cpp index 2753451c5308ff..c9d2861686b143 100644 --- a/src/platform/nxp/common/ConnectivityManagerImpl.cpp +++ b/src/platform/nxp/common/ConnectivityManagerImpl.cpp @@ -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 diff --git a/src/platform/nxp/common/ConnectivityManagerImpl.h b/src/platform/nxp/common/ConnectivityManagerImpl.h index 806826daf0f7d3..49e4f66a13197b 100644 --- a/src/platform/nxp/common/ConnectivityManagerImpl.h +++ b/src/platform/nxp/common/ConnectivityManagerImpl.h @@ -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. diff --git a/src/platform/nxp/common/NetworkCommissioningDriver.h b/src/platform/nxp/common/NetworkCommissioningDriver.h index 5757771476e057..33d7d5bc1ed3af 100644 --- a/src/platform/nxp/common/NetworkCommissioningDriver.h +++ b/src/platform/nxp/common/NetworkCommissioningDriver.h @@ -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 */ diff --git a/src/platform/nxp/common/NetworkCommissioningWiFiDriver.cpp b/src/platform/nxp/common/NetworkCommissioningWiFiDriver.cpp index d697c75d1042d7..c201dad7a6661c 100644 --- a/src/platform/nxp/common/NetworkCommissioningWiFiDriver.cpp +++ b/src/platform/nxp/common/NetworkCommissioningWiFiDriver.cpp @@ -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; } @@ -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; diff --git a/third_party/nxp/rt_sdk/rt_sdk.gni b/third_party/nxp/rt_sdk/rt_sdk.gni index 9063361f7b9d4d..eb44f01802ce14 100644 --- a/third_party/nxp/rt_sdk/rt_sdk.gni +++ b/third_party/nxp/rt_sdk/rt_sdk.gni @@ -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