Skip to content

Commit

Permalink
[Ameba] Wifi connectivity update (#25955)
Browse files Browse the repository at this point in the history
* [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
  • Loading branch information
pankore authored and pull[bot] committed Oct 11, 2023
1 parent 81023fa commit a1b6af4
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 45 deletions.
2 changes: 2 additions & 0 deletions examples/all-clusters-app/ameba/main/DeviceCallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <app/util/basic-types.h>
#include <app/util/util.h>
#include <lib/dnssd/Advertiser.h>
#include <platform/Ameba/AmebaUtils.h>
#include <route_hook/ameba_route_hook.h>
#include <support/CodeUtils.h>
#include <support/logging/CHIPLogging.h>
Expand Down Expand Up @@ -103,6 +104,7 @@ void DeviceCallbacks::DeviceEventCallback(const ChipDeviceEvent * event, intptr_

case DeviceEventType::kCommissioningComplete:
ChipLogProgress(DeviceLayer, "Commissioning Complete");
chip::DeviceLayer::Internal::AmebaUtils::SetCurrentProvisionedNetwork();
break;
}
}
Expand Down
2 changes: 2 additions & 0 deletions examples/light-switch-app/ameba/main/DeviceCallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <app/util/basic-types.h>
#include <app/util/util.h>
#include <lib/dnssd/Advertiser.h>
#include <platform/Ameba/AmebaUtils.h>
#include <route_hook/ameba_route_hook.h>
#include <support/CodeUtils.h>
#include <support/logging/CHIPLogging.h>
Expand Down Expand Up @@ -103,6 +104,7 @@ void DeviceCallbacks::DeviceEventCallback(const ChipDeviceEvent * event, intptr_

case DeviceEventType::kCommissioningComplete:
ChipLogProgress(DeviceLayer, "Commissioning Complete");
chip::DeviceLayer::Internal::AmebaUtils::SetCurrentProvisionedNetwork();
break;
}
}
Expand Down
2 changes: 2 additions & 0 deletions examples/lighting-app/ameba/main/DeviceCallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <app/util/basic-types.h>
#include <app/util/util.h>
#include <lib/dnssd/Advertiser.h>
#include <platform/Ameba/AmebaUtils.h>
#include <route_hook/ameba_route_hook.h>
#include <support/CodeUtils.h>
#include <support/logging/CHIPLogging.h>
Expand Down Expand Up @@ -101,6 +102,7 @@ void DeviceCallbacks::DeviceEventCallback(const ChipDeviceEvent * event, intptr_

case DeviceEventType::kCommissioningComplete:
ChipLogProgress(DeviceLayer, "Commissioning Complete");
chip::DeviceLayer::Internal::AmebaUtils::SetCurrentProvisionedNetwork();
break;
}
}
Expand Down
2 changes: 2 additions & 0 deletions examples/ota-requestor-app/ameba/main/DeviceCallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <app/util/basic-types.h>
#include <app/util/util.h>
#include <lib/dnssd/Advertiser.h>
#include <platform/Ameba/AmebaUtils.h>
#include <route_hook/ameba_route_hook.h>
#include <support/CodeUtils.h>
#include <support/logging/CHIPLogging.h>
Expand Down Expand Up @@ -103,6 +104,7 @@ void DeviceCallbacks::DeviceEventCallback(const ChipDeviceEvent * event, intptr_

case DeviceEventType::kCommissioningComplete:
ChipLogProgress(DeviceLayer, "Commissioning Complete");
chip::DeviceLayer::Internal::AmebaUtils::SetCurrentProvisionedNetwork();
break;
}
}
Expand Down
51 changes: 49 additions & 2 deletions src/platform/Ameba/AmebaUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,64 @@ 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));
memset(config, 0, sizeof(rtw_wifi_config_t));
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;
}
4 changes: 3 additions & 1 deletion src/platform/Ameba/AmebaUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 28 additions & 22 deletions src/platform/Ameba/ConnectivityManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
}
Expand All @@ -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)
Expand Down Expand Up @@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -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");
Expand All @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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(); });
}
Expand Down
4 changes: 3 additions & 1 deletion src/platform/Ameba/ConnectivityManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
24 changes: 5 additions & 19 deletions src/platform/Ameba/NetworkCommissioningWiFiDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <lib/support/CodeUtils.h>
#include <lib/support/SafeInt.h>
#include <platform/Ameba/AmebaUtils.h>
#include <platform/Ameba/ConnectivityManagerImpl.h>
#include <platform/Ameba/NetworkCommissioningDriver.h>
#include <platform/CHIPDeviceLayer.h>

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit a1b6af4

Please sign in to comment.