From a1b6af43e60ec552ad815a732b602cf7201df72d Mon Sep 17 00:00:00 2001 From: pankore <86098180+pankore@users.noreply.github.com> Date: Wed, 12 Apr 2023 21:05:02 +0800 Subject: [PATCH] [Ameba] Wifi connectivity update (#25955) * [wifi] store wifi info into kvs after commissioning complete - for onnetwork devices, wifi info not stored - cleanup connectivitymgr * [wifi] make ChangeWiFiStationState accessible from NetworkCommissioningDriver * fix dhcp issue * restyle * add comments --- .../ameba/main/DeviceCallbacks.cpp | 2 + .../ameba/main/DeviceCallbacks.cpp | 2 + .../ameba/main/DeviceCallbacks.cpp | 2 + .../ameba/main/DeviceCallbacks.cpp | 2 + src/platform/Ameba/AmebaUtils.cpp | 51 ++++++++++++++++++- src/platform/Ameba/AmebaUtils.h | 4 +- .../Ameba/ConnectivityManagerImpl.cpp | 50 ++++++++++-------- src/platform/Ameba/ConnectivityManagerImpl.h | 4 +- .../Ameba/NetworkCommissioningWiFiDriver.cpp | 24 ++------- 9 files changed, 96 insertions(+), 45 deletions(-) diff --git a/examples/all-clusters-app/ameba/main/DeviceCallbacks.cpp b/examples/all-clusters-app/ameba/main/DeviceCallbacks.cpp index a60b60520c6c46..b31d2629af14aa 100644 --- a/examples/all-clusters-app/ameba/main/DeviceCallbacks.cpp +++ b/examples/all-clusters-app/ameba/main/DeviceCallbacks.cpp @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -103,6 +104,7 @@ void DeviceCallbacks::DeviceEventCallback(const ChipDeviceEvent * event, intptr_ case DeviceEventType::kCommissioningComplete: ChipLogProgress(DeviceLayer, "Commissioning Complete"); + chip::DeviceLayer::Internal::AmebaUtils::SetCurrentProvisionedNetwork(); break; } } diff --git a/examples/light-switch-app/ameba/main/DeviceCallbacks.cpp b/examples/light-switch-app/ameba/main/DeviceCallbacks.cpp index 6d72c6aba85d36..b5fbaf1ece36c4 100644 --- a/examples/light-switch-app/ameba/main/DeviceCallbacks.cpp +++ b/examples/light-switch-app/ameba/main/DeviceCallbacks.cpp @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include @@ -103,6 +104,7 @@ void DeviceCallbacks::DeviceEventCallback(const ChipDeviceEvent * event, intptr_ case DeviceEventType::kCommissioningComplete: ChipLogProgress(DeviceLayer, "Commissioning Complete"); + chip::DeviceLayer::Internal::AmebaUtils::SetCurrentProvisionedNetwork(); break; } } diff --git a/examples/lighting-app/ameba/main/DeviceCallbacks.cpp b/examples/lighting-app/ameba/main/DeviceCallbacks.cpp index 6ed98af023d157..abc6472d2c72b9 100644 --- a/examples/lighting-app/ameba/main/DeviceCallbacks.cpp +++ b/examples/lighting-app/ameba/main/DeviceCallbacks.cpp @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -101,6 +102,7 @@ void DeviceCallbacks::DeviceEventCallback(const ChipDeviceEvent * event, intptr_ case DeviceEventType::kCommissioningComplete: ChipLogProgress(DeviceLayer, "Commissioning Complete"); + chip::DeviceLayer::Internal::AmebaUtils::SetCurrentProvisionedNetwork(); break; } } diff --git a/examples/ota-requestor-app/ameba/main/DeviceCallbacks.cpp b/examples/ota-requestor-app/ameba/main/DeviceCallbacks.cpp index d62996bf369d71..67cb1ac8b9d747 100644 --- a/examples/ota-requestor-app/ameba/main/DeviceCallbacks.cpp +++ b/examples/ota-requestor-app/ameba/main/DeviceCallbacks.cpp @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -103,6 +104,7 @@ void DeviceCallbacks::DeviceEventCallback(const ChipDeviceEvent * event, intptr_ case DeviceEventType::kCommissioningComplete: ChipLogProgress(DeviceLayer, "Commissioning Complete"); + chip::DeviceLayer::Internal::AmebaUtils::SetCurrentProvisionedNetwork(); break; } } diff --git a/src/platform/Ameba/AmebaUtils.cpp b/src/platform/Ameba/AmebaUtils.cpp index 72d0e7fda0c4cc..578ffab2cd5816 100644 --- a/src/platform/Ameba/AmebaUtils.cpp +++ b/src/platform/Ameba/AmebaUtils.cpp @@ -136,7 +136,7 @@ CHIP_ERROR AmebaUtils::WiFiDisconnect(void) return err; } -CHIP_ERROR AmebaUtils::WiFiConnect(void) +CHIP_ERROR AmebaUtils::WiFiConnectProvisionedNetwork(void) { CHIP_ERROR err = CHIP_NO_ERROR; rtw_wifi_config_t * config = (rtw_wifi_config_t *) pvPortMalloc(sizeof(rtw_wifi_config_t)); @@ -144,9 +144,56 @@ CHIP_ERROR AmebaUtils::WiFiConnect(void) GetWiFiConfig(config); ChipLogProgress(DeviceLayer, "Connecting to AP : [%s]", (char *) config->ssid); int ameba_err = matter_wifi_connect((char *) config->ssid, RTW_SECURITY_WPA_WPA2_MIXED, (char *) config->password, - strlen((const char *) config->ssid), strlen((const char *) config->password), 0, NULL); + strlen((const char *) config->ssid), strlen((const char *) config->password), 0, nullptr); vPortFree(config); err = (ameba_err == RTW_SUCCESS) ? CHIP_NO_ERROR : CHIP_ERROR_INTERNAL; return err; } + +CHIP_ERROR AmebaUtils::WiFiConnect(const char * ssid, const char * password) +{ + CHIP_ERROR err = CHIP_NO_ERROR; + ChipLogProgress(DeviceLayer, "Connecting to AP : [%s]", (char *) ssid); + int ameba_err = matter_wifi_connect((char *) ssid, RTW_SECURITY_WPA_WPA2_MIXED, (char *) password, strlen(ssid), + strlen(password), 0, nullptr); + err = (ameba_err == RTW_SUCCESS) ? CHIP_NO_ERROR : CHIP_ERROR_INTERNAL; + return err; +} + +CHIP_ERROR AmebaUtils::SetCurrentProvisionedNetwork() +{ + CHIP_ERROR err = CHIP_NO_ERROR; + rtw_wifi_setting_t pSetting; + int ret = matter_get_sta_wifi_info(&pSetting); + if (ret < 0) + { + ChipLogProgress(DeviceLayer, "STA No Wi-Fi Info"); + goto exit; + } + else + { + rtw_wifi_config_t config = { 0 }; + GetWiFiConfig(&config); + if (!memcmp(config.ssid, pSetting.ssid, strlen((const char *) pSetting.ssid) + 1)) + { + ChipLogProgress(DeviceLayer, "STA Wi-Fi Info exist, do nothing"); + goto exit; + } + else + { + ChipLogProgress(DeviceLayer, "STA Wi-Fi Info "); + + memcpy(config.ssid, pSetting.ssid, strlen((const char *) pSetting.ssid) + 1); + memcpy(config.password, pSetting.password, strlen((const char *) pSetting.password) + 1); + err = SetWiFiConfig(&config); + if (err != CHIP_NO_ERROR) + { + ChipLogError(DeviceLayer, "SetWiFiConfig() failed"); + goto exit; + } + } + } +exit: + return err; +} diff --git a/src/platform/Ameba/AmebaUtils.h b/src/platform/Ameba/AmebaUtils.h index b2f68f38dad1ef..5fcc5c9c2ef487 100644 --- a/src/platform/Ameba/AmebaUtils.h +++ b/src/platform/Ameba/AmebaUtils.h @@ -36,7 +36,9 @@ class AmebaUtils static CHIP_ERROR GetWiFiConfig(rtw_wifi_config_t * config); static CHIP_ERROR ClearWiFiConfig(void); static CHIP_ERROR WiFiDisconnect(void); - static CHIP_ERROR WiFiConnect(void); + static CHIP_ERROR WiFiConnectProvisionedNetwork(void); + static CHIP_ERROR WiFiConnect(const char * ssid, const char * password); + static CHIP_ERROR SetCurrentProvisionedNetwork(void); }; } // namespace Internal diff --git a/src/platform/Ameba/ConnectivityManagerImpl.cpp b/src/platform/Ameba/ConnectivityManagerImpl.cpp index 670c060e95b94f..ba289b115c0bab 100644 --- a/src/platform/Ameba/ConnectivityManagerImpl.cpp +++ b/src/platform/Ameba/ConnectivityManagerImpl.cpp @@ -83,7 +83,7 @@ CHIP_ERROR ConnectivityManagerImpl::_Init() chip_connmgr_set_callback_func((chip_connmgr_callback)(conn_callback_dispatcher), this); // Register WiFi event handlers - wifi_reg_event_handler(WIFI_EVENT_CONNECT, ConnectivityManagerImpl::RtkWiFiStationConnectedHandler, NULL); + wifi_reg_event_handler(WIFI_EVENT_FOURWAY_HANDSHAKE_DONE, ConnectivityManagerImpl::RtkWiFiStationConnectedHandler, NULL); wifi_reg_event_handler(WIFI_EVENT_DISCONNECT, ConnectivityManagerImpl::RtkWiFiStationDisconnectedHandler, NULL); err = Internal::AmebaUtils::StartWiFi(); @@ -148,13 +148,16 @@ void ConnectivityManagerImpl::_OnPlatformEvent(const ChipDeviceEvent * event) if (event->Type == DeviceEventType::kRtkWiFiStationConnectedEvent) { ChipLogProgress(DeviceLayer, "WiFiStationConnected"); + // Only do DHCP when connecting to wifi via matter provisioning + // External wifi provisioning will do DHCP on their own if (mWiFiStationState == kWiFiStationState_Connecting) { - ChangeWiFiStationState(kWiFiStationState_Connecting_Succeeded); + DHCPProcess(); } - if (rtw_join_status & JOIN_HANDSHAKE_DONE) + // Allow external wifi provisioning methods by allowing NotConnected states to advance to Connecting_succeed + if ((mWiFiStationState == kWiFiStationState_Connecting) || (mWiFiStationState == kWiFiStationState_NotConnected)) { - DHCPProcess(); + ChangeWiFiStationState(kWiFiStationState_Connecting_Succeeded); } DriveStationState(); } @@ -166,6 +169,10 @@ void ConnectivityManagerImpl::_OnPlatformEvent(const ChipDeviceEvent * event) { ChangeWiFiStationState(kWiFiStationState_Connecting_Failed); } + if (mWiFiStationState == kWiFiStationState_Connected) + { + ChangeWiFiStationState(kWiFiStationState_Disconnecting); + } DriveStationState(); } if (event->Type == DeviceEventType::kRtkWiFiScanCompletedEvent) @@ -204,7 +211,8 @@ CHIP_ERROR ConnectivityManagerImpl::_SetWiFiStationMode(WiFiStationMode val) if (mWiFiStationMode != val) { - ChipLogProgress(DeviceLayer, "WiFi station mode change: %d -> %d", (mWiFiStationMode), (val)); + ChipLogProgress(DeviceLayer, "WiFi station mode change: %s -> %s", WiFiStationModeToStr(mWiFiStationMode), + WiFiStationModeToStr(val)); } mWiFiStationMode = val; @@ -221,9 +229,7 @@ bool ConnectivityManagerImpl::_IsWiFiStationProvisioned(void) void ConnectivityManagerImpl::_ClearWiFiStationProvision(void) { // Clear Ameba WiFi station config - rtw_wifi_config_t wifiConfig; - memset(&wifiConfig, 0, sizeof(wifiConfig)); - Internal::AmebaUtils::SetWiFiConfig(&wifiConfig); + Internal::AmebaUtils::ClearWiFiConfig(); } CHIP_ERROR ConnectivityManagerImpl::_SetWiFiAPMode(WiFiAPMode val) @@ -496,9 +502,9 @@ void ConnectivityManagerImpl::DriveStationState() // If the station interface is currently connected ... if (stationConnected) { - // Advance the station state to Connected if it was previously NotConnected or + // Advance the station state to Connected if // a previously initiated connect attempt succeeded. - if (mWiFiStationState == kWiFiStationState_NotConnected || mWiFiStationState == kWiFiStationState_Connecting_Succeeded) + if (mWiFiStationState == kWiFiStationState_Connecting_Succeeded) { ChangeWiFiStationState(kWiFiStationState_Connected); ChipLogProgress(DeviceLayer, "WiFi station interface connected"); @@ -512,23 +518,23 @@ void ConnectivityManagerImpl::DriveStationState() { System::Clock::Timestamp now = System::SystemClock().GetMonotonicTimestamp(); - // Advance the station state to NotConnected if it was previously Connected or Disconnecting, + // Advance the station state to NotConnected if it was previously Disconnecting, // or if a previous initiated connect attempt failed. - if (mWiFiStationState == kWiFiStationState_Connected || mWiFiStationState == kWiFiStationState_Disconnecting || - mWiFiStationState == kWiFiStationState_Connecting_Failed) + if (mWiFiStationState == kWiFiStationState_Disconnecting || mWiFiStationState == kWiFiStationState_Connecting_Failed) { WiFiStationState prevState = mWiFiStationState; ChangeWiFiStationState(kWiFiStationState_NotConnected); if (prevState != kWiFiStationState_Connecting_Failed) { - ChipLogProgress(DeviceLayer, "WiFi station interface disconnected"); - mLastStationConnectFailTime = System::Clock::kZero; - OnStationDisconnected(); + ChipLogProgress(DeviceLayer, "WiFi station failed to connect"); + // TODO: check retry count if exceeded, then clearwificonfig } else { - mLastStationConnectFailTime = now; + ChipLogProgress(DeviceLayer, "WiFi station disconnected"); } + mLastStationConnectFailTime = now; + OnStationDisconnected(); } // If the WiFi station interface is now enabled and provisioned (and by implication, // not presently under application control), AND the system is not in the process of @@ -541,14 +547,13 @@ void ConnectivityManagerImpl::DriveStationState() now >= mLastStationConnectFailTime + mWiFiStationReconnectInterval) { ChipLogProgress(DeviceLayer, "Attempting to connect WiFi station interface"); - err = Internal::AmebaUtils::WiFiConnect(); + ChangeWiFiStationState(kWiFiStationState_Connecting); + err = Internal::AmebaUtils::WiFiConnectProvisionedNetwork(); if (err != CHIP_NO_ERROR) { - ChipLogError(DeviceLayer, "WiFiConnect() failed: %s", chip::ErrorStr(err)); + ChipLogError(DeviceLayer, "WiFiConnectProvisionedNetwork() failed: %s", chip::ErrorStr(err)); } SuccessOrExit(err); - - ChangeWiFiStationState(kWiFiStationState_Connecting); } // Otherwise arrange another connection attempt at a suitable point in the future. @@ -644,7 +649,8 @@ void ConnectivityManagerImpl::ChangeWiFiStationState(WiFiStationState newState) { if (mWiFiStationState != newState) { - ChipLogProgress(DeviceLayer, "WiFi station state change: %d -> %d", (mWiFiStationState), (newState)); + ChipLogProgress(DeviceLayer, "WiFi station state change: %s -> %s", WiFiStationStateToStr(mWiFiStationState), + WiFiStationStateToStr(newState)); mWiFiStationState = newState; SystemLayer().ScheduleLambda([]() { NetworkCommissioning::AmebaWiFiDriver::GetInstance().OnNetworkStatusChange(); }); } diff --git a/src/platform/Ameba/ConnectivityManagerImpl.h b/src/platform/Ameba/ConnectivityManagerImpl.h index 9863c47dec88d8..c4c04c5346e482 100644 --- a/src/platform/Ameba/ConnectivityManagerImpl.h +++ b/src/platform/Ameba/ConnectivityManagerImpl.h @@ -84,6 +84,9 @@ class ConnectivityManagerImpl final : public ConnectivityManager, // the implementation methods provided by this class. friend class ConnectivityManager; +public: + void ChangeWiFiStationState(WiFiStationState newState); + private: CHIP_ERROR _Init(void); void _OnPlatformEvent(const ChipDeviceEvent * event); @@ -130,7 +133,6 @@ class ConnectivityManagerImpl final : public ConnectivityManager, void DriveStationState(void); void OnStationConnected(void); void OnStationDisconnected(void); - void ChangeWiFiStationState(WiFiStationState newState); static void DriveStationState(::chip::System::Layer * aLayer, void * aAppState); void DriveAPState(void); diff --git a/src/platform/Ameba/NetworkCommissioningWiFiDriver.cpp b/src/platform/Ameba/NetworkCommissioningWiFiDriver.cpp index e89bc635377215..38be3f2080f0b5 100644 --- a/src/platform/Ameba/NetworkCommissioningWiFiDriver.cpp +++ b/src/platform/Ameba/NetworkCommissioningWiFiDriver.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -145,25 +146,10 @@ CHIP_ERROR AmebaWiFiDriver::ConnectWiFiNetwork(const char * ssid, uint8_t ssidLe } } - ReturnErrorOnFailure(ConnectivityMgr().SetWiFiStationMode(ConnectivityManager::kWiFiStationMode_Disabled)); + ConnectivityMgrImpl().ChangeWiFiStationState(ConnectivityManager::kWiFiStationState_Connecting); - rtw_wifi_config_t wifiConfig; - - // Set the wifi configuration - memset(&wifiConfig, 0, sizeof(wifiConfig)); - memcpy(wifiConfig.ssid, ssid, ssidLen + 1); - memcpy(wifiConfig.password, key, keyLen + 1); - - // Configure the WiFi interface. - err = chip::DeviceLayer::Internal::AmebaUtils::SetWiFiConfig(&wifiConfig); - if (err != CHIP_NO_ERROR) - { - ChipLogError(DeviceLayer, "SetWiFiConfig() failed"); - return err; - } - - ReturnErrorOnFailure(ConnectivityMgr().SetWiFiStationMode(ConnectivityManager::kWiFiStationMode_Disabled)); - return ConnectivityMgr().SetWiFiStationMode(ConnectivityManager::kWiFiStationMode_Enabled); + err = chip::DeviceLayer::Internal::AmebaUtils::WiFiConnect(ssid, key); + return err; } void AmebaWiFiDriver::OnConnectWiFiNetwork() @@ -318,7 +304,7 @@ void AmebaWiFiDriver::ScanNetworks(ByteSpan ssid, WiFiDriver::ScanCallback * cal CHIP_ERROR AmebaWiFiDriver::SetLastDisconnectReason(const ChipDeviceEvent * event) { VerifyOrReturnError(event->Type == DeviceEventType::kRtkWiFiStationDisconnectedEvent, CHIP_ERROR_INVALID_ARGUMENT); - mLastDisconnectedReason = wifi_get_last_error(); + mLastDisconnectedReason = wifi_get_last_error(); // TODO: change this to wrapper return CHIP_NO_ERROR; }