Skip to content

Commit

Permalink
[Silabs] Finish WifiInterface function API cleanup (#37746)
Browse files Browse the repository at this point in the history
* Finish WifiInterface cleanup

* Use C-Like Array

* Update impl files

* Update NetworkCommissioningWifiDriver for C-Like arrays

* fix copy string

* Fix has ipv6 address check
  • Loading branch information
mkardous-silabs authored Feb 24, 2025
1 parent 1a36b6b commit 0201fc5
Show file tree
Hide file tree
Showing 10 changed files with 334 additions and 371 deletions.
2 changes: 1 addition & 1 deletion src/platform/silabs/ConfigurationManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ void ConfigurationManagerImpl::DoFactoryReset(intptr_t arg)
}

ChipLogProgress(DeviceLayer, "Clearing WiFi provision");
wfx_clear_wifi_provision();
ClearWifiCredentials();
#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI_STATION

// Restart the system.
Expand Down
22 changes: 5 additions & 17 deletions src/platform/silabs/ConnectivityManagerImpl_WIFI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,7 @@ ConnectivityManager::WiFiStationMode ConnectivityManagerImpl::_GetWiFiStationMod

bool ConnectivityManagerImpl::_IsWiFiStationProvisioned(void)
{
wfx_wifi_provision_t wifiConfig;
if (wfx_get_wifi_provision(&wifiConfig))
{
return (wifiConfig.ssid[0] != 0);
}
return false;
return IsWifiProvisioned();
}

bool ConnectivityManagerImpl::_IsWiFiStationEnabled(void)
Expand Down Expand Up @@ -181,7 +176,7 @@ void ConnectivityManagerImpl::_ClearWiFiStationProvision(void)
{
if (mWiFiStationMode != kWiFiStationMode_ApplicationControlled)
{
wfx_clear_wifi_provision();
ClearWifiCredentials();

DeviceLayer::SystemLayer().ScheduleWork(DriveStationState, NULL);
}
Expand Down Expand Up @@ -304,14 +299,7 @@ void ConnectivityManagerImpl::DriveStationState()
if (mWiFiStationState != kWiFiStationState_Connecting)
{
ChipLogProgress(DeviceLayer, "Attempting to connect WiFi");

sl_status_t status = wfx_connect_to_ap();
if (status != SL_STATUS_OK)
{
ChipLogError(DeviceLayer, "wfx_connect_to_ap() failed: %" PRId32, status);
// TODO: Clean the function up to remove the usage of goto
goto exit;
}
SuccessOrExitAction(ConnectToAccessPoint(), ChipLogError(DeviceLayer, "ConnectToAccessPoint() failed"));

ChangeWiFiStationState(kWiFiStationState_Connecting);
}
Expand Down Expand Up @@ -388,9 +376,9 @@ void ConnectivityManagerImpl::UpdateInternetConnectivityState(void)
if (mWiFiStationState == kWiFiStationState_Connected)
{
#if CHIP_DEVICE_CONFIG_ENABLE_IPV4
haveIPv4Conn = wfx_have_ipv4_addr(SL_WFX_STA_INTERFACE);
haveIPv4Conn = HasAnIPv4Address();
#endif /* CHIP_DEVICE_CONFIG_ENABLE_IPV4 */
haveIPv6Conn = wfx_have_ipv6_addr(SL_WFX_STA_INTERFACE);
haveIPv6Conn = HasAnIPv6Address();
}

// If the internet connectivity state has changed...
Expand Down
27 changes: 16 additions & 11 deletions src/platform/silabs/NetworkCommissioningWiFiDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,22 +144,22 @@ CHIP_ERROR SlWiFiDriver::ConnectWiFiNetwork(const char * ssid, uint8_t ssidLen,
ReturnErrorOnFailure(ConnectivityMgr().SetWiFiStationMode(ConnectivityManager::kWiFiStationMode_Disabled));

// Set the wifi configuration
wfx_wifi_provision_t wifiConfig;
memset(&wifiConfig, 0, sizeof(wifiConfig));
WifiCredentials wifiConfig;
wifiConfig.Clear();

VerifyOrReturnError(ssidLen <= WFX_MAX_SSID_LENGTH, CHIP_ERROR_BUFFER_TOO_SMALL);
memcpy(wifiConfig.ssid, ssid, ssidLen);
wifiConfig.ssid_length = ssidLen;
wifiConfig.ssidLength = ssidLen;

VerifyOrReturnError(keyLen < WFX_MAX_PASSKEY_LENGTH, CHIP_ERROR_BUFFER_TOO_SMALL);
memcpy(wifiConfig.passkey, key, keyLen);
wifiConfig.passkey_length = keyLen;
wifiConfig.passkeyLength = keyLen;

wifiConfig.security = WFX_SEC_WPA2;

ChipLogProgress(NetworkProvisioning, "Setting up connection for WiFi SSID: %.*s", static_cast<int>(ssidLen), ssid);
// Configure the WFX WiFi interface.
wfx_set_wifi_provision(&wifiConfig);
SetWifiCredentials(wifiConfig);
ReturnErrorOnFailure(ConnectivityMgr().SetWiFiStationMode(ConnectivityManager::kWiFiStationMode_Disabled));
ReturnErrorOnFailure(ConnectivityMgr().SetWiFiStationMode(ConnectivityManager::kWiFiStationMode_Enabled));
return CHIP_NO_ERROR;
Expand Down Expand Up @@ -326,17 +326,22 @@ void SlWiFiDriver::ScanNetworks(ByteSpan ssid, WiFiDriver::ScanCallback * callba

CHIP_ERROR GetConnectedNetwork(Network & network)
{
wfx_wifi_provision_t wifiConfig;
WifiCredentials wifiConfig;
network.networkIDLen = 0;
network.connected = false;

// we are able to fetch the wifi provision data and STA should be connected
VerifyOrReturnError(wfx_get_wifi_provision(&wifiConfig), CHIP_ERROR_UNINITIALIZED);
VerifyOrReturnError(IsStationConnected(), CHIP_ERROR_NOT_CONNECTED);
ReturnErrorOnFailure(GetWifiCredentials(wifiConfig));
VerifyOrReturnError(wifiConfig.ssidLength < NetworkCommissioning::kMaxNetworkIDLen, CHIP_ERROR_BUFFER_TOO_SMALL);

network.connected = true;
uint8_t length = strnlen(wifiConfig.ssid, DeviceLayer::Internal::kMaxWiFiSSIDLength);
VerifyOrReturnError(length < sizeof(network.networkID), CHIP_ERROR_BUFFER_TOO_SMALL);
memcpy(network.networkID, wifiConfig.ssid, length);
network.networkIDLen = length;

ByteSpan ssidSpan(wifiConfig.ssid, wifiConfig.ssidLength);
MutableByteSpan networkIdSpan(network.networkID, NetworkCommissioning::kMaxNetworkIDLen);

ReturnErrorOnFailure(CopySpanToMutableSpan(ssidSpan, networkIdSpan));
network.networkIDLen = networkIdSpan.size();

return CHIP_NO_ERROR;
}
Expand Down
42 changes: 21 additions & 21 deletions src/platform/silabs/wifi/SiWx/WifiInterfaceImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,16 +346,16 @@ sl_status_t ScanCallback(sl_wifi_event_t event, sl_wifi_scan_result_t * scan_res

sl_status_t InitiateScan()
{
sl_status_t status = SL_STATUS_OK;

sl_wifi_ssid_t ssid = { 0 };

sl_status_t status = SL_STATUS_OK;
sl_wifi_ssid_t ssid = { 0 };
sl_wifi_scan_configuration_t wifi_scan_configuration = default_wifi_scan_configuration;

ssid.length = wfx_rsi.sec.ssid_length;
ssid.length = wfx_rsi.credentials.ssidLength;

chip::ByteSpan requestedSsidSpan(wfx_rsi.credentials.ssid, wfx_rsi.credentials.ssidLength);
chip::MutableByteSpan ssidSpan(ssid.value, ssid.length);
chip::CopySpanToMutableSpan(requestedSsidSpan, ssidSpan);

// TODO: workaround because the string management with the null termination is flawed
chip::Platform::CopyString((char *) &ssid.value[0], ssid.length + 1, wfx_rsi.sec.ssid);
sl_wifi_set_scan_callback(ScanCallback, NULL);

osSemaphoreAcquire(sScanInProgressSemaphore, osWaitForever);
Expand Down Expand Up @@ -394,19 +394,20 @@ sl_status_t SetWifiConfigurations()
VerifyOrReturnError(status == SL_STATUS_OK, status);
#endif // CHIP_CONFIG_ENABLE_ICD_SERVER

if (wfx_rsi.sec.passkey_length != 0)
if (wfx_rsi.credentials.passkeyLength != 0)
{
status = sl_net_set_credential(SL_NET_DEFAULT_WIFI_CLIENT_CREDENTIAL_ID, SL_NET_WIFI_PSK, &wfx_rsi.sec.passkey[0],
wfx_rsi.sec.passkey_length);
status = sl_net_set_credential(SL_NET_DEFAULT_WIFI_CLIENT_CREDENTIAL_ID, SL_NET_WIFI_PSK, &wfx_rsi.credentials.passkey[0],
wfx_rsi.credentials.passkeyLength);
VerifyOrReturnError(status == SL_STATUS_OK, status,
ChipLogError(DeviceLayer, "sl_net_set_credential failed: 0x%lx", status));
}

sl_net_wifi_client_profile_t profile = {
.config = {
.ssid = {
.value = { 0 },
//static cast because the types dont match
.length = static_cast<uint8_t>(wfx_rsi.sec.ssid_length),
.length = static_cast<uint8_t>(wfx_rsi.credentials.ssidLength),
},
.channel = {
.channel = SL_WIFI_AUTO_CHANNEL,
Expand All @@ -427,8 +428,10 @@ sl_status_t SetWifiConfigurations()
.ip = {{{0}}},
}
};
// TODO: memcpy for now since the types dont match
memcpy((char *) &profile.config.ssid.value, wfx_rsi.sec.ssid, wfx_rsi.sec.ssid_length);

chip::MutableByteSpan output(profile.config.ssid.value, WFX_MAX_SSID_LENGTH);
chip::ByteSpan input(wfx_rsi.credentials.ssid, wfx_rsi.credentials.ssidLength);
chip::CopySpanToMutableSpan(input, output);

status = sl_net_set_profile(SL_NET_WIFI_CLIENT_INTERFACE, SL_NET_DEFAULT_WIFI_CLIENT_PROFILE_ID, &profile);
VerifyOrReturnError(status == SL_STATUS_OK, status, ChipLogError(DeviceLayer, "sl_net_set_profile failed: 0x%lx", status));
Expand Down Expand Up @@ -460,7 +463,7 @@ sl_status_t JoinCallback(sl_wifi_event_t event, char * result, uint32_t resultLe
status = *reinterpret_cast<sl_status_t *>(result);
ChipLogError(DeviceLayer, "JoinCallback: failed: 0x%lx", status);
wfx_rsi.dev_state.Clear(WifiState::kStationConnected);
wfx_retry_connection(++wfx_rsi.join_retries);
ScheduleConnectionAttempt();
}

return status;
Expand Down Expand Up @@ -504,9 +507,7 @@ sl_status_t JoinWifiNetwork(void)
ChipLogError(DeviceLayer, "sl_wifi_connect failed: 0x%lx", static_cast<uint32_t>(status));

wfx_rsi.dev_state.Clear(WifiState::kStationConnecting).Clear(WifiState::kStationConnected);

ChipLogProgress(DeviceLayer, "Connection retry attempt %d", wfx_rsi.join_retries);
wfx_retry_connection(++wfx_rsi.join_retries);
ScheduleConnectionAttempt();

return status;
}
Expand All @@ -522,12 +523,11 @@ CHIP_ERROR GetAccessPointInfo(wfx_wifi_scan_result_t & info)
{
// TODO: Convert this to a int8
int32_t rssi = 0;
info.security = wfx_rsi.sec.security;
info.security = wfx_rsi.credentials.security;
info.chan = wfx_rsi.ap_chan;

chip::MutableByteSpan output(info.ssid, WFX_MAX_SSID_LENGTH);
// Cast is a workaround until the wfx_rsi structure is refactored
chip::ByteSpan ssid(reinterpret_cast<uint8_t *>(wfx_rsi.sec.ssid), wfx_rsi.sec.ssid_length);
chip::ByteSpan ssid(wfx_rsi.credentials.ssid, wfx_rsi.credentials.ssidLength);
chip::CopySpanToMutableSpan(ssid, output);
info.ssid_length = output.size();

Expand Down Expand Up @@ -793,7 +793,7 @@ void MatterWifiTask(void * arg)
VerifyOrReturn(status == SL_STATUS_OK,
ChipLogError(DeviceLayer, "MatterWifiTask: sl_wifi_siwx917_init failed: 0x%lx", static_cast<uint32_t>(status)));

sl_matter_wifi_task_started();
NotifyWifiTaskInitialized();

ChipLogDetail(DeviceLayer, "MatterWifiTask: starting event loop");
for (;;)
Expand Down
37 changes: 10 additions & 27 deletions src/platform/silabs/wifi/WifiInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,14 @@ bool hasNotifiedIPV4 = false;
*/
void RetryConnectionTimerHandler(void * arg)
{
if (wfx_connect_to_ap() != SL_STATUS_OK)
if (ConnectToAccessPoint() != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "wfx_connect_to_ap() failed.");
ChipLogError(DeviceLayer, "ConnectToAccessPoint() failed.");
}
}

} // namespace

/* Updated functions */

void NotifyIPv6Change(bool gotIPv6Addr)
{
hasNotifiedIPV6 = gotIPv6Addr;
Expand Down Expand Up @@ -132,19 +130,12 @@ void ResetIPNotificationStates()
hasNotifiedIPV4 = false;
#endif // CHIP_DEVICE_CONFIG_ENABLE_IPV4
}
/* Function to update */

/***********************************************************************************
* @fn sl_matter_wifi_task_started(void)
* @brief
* Wifi device started notification
* @param[in]: None
* @return None
*************************************************************************************/
void sl_matter_wifi_task_started(void)

void NotifyWifiTaskInitialized(void)
{
sl_wfx_startup_ind_t evt = {};

// TODO: We should move this to the init function and not the notification function
// Creating a timer which will be used to retry connection with AP
sRetryTimer = osTimerNew(RetryConnectionTimerHandler, osTimerOnce, NULL, NULL);
VerifyOrReturn(sRetryTimer != NULL);
Expand All @@ -166,16 +157,8 @@ void sl_matter_wifi_task_started(void)
HandleWFXSystemEvent((sl_wfx_generic_message_t *) &evt);
}

/**************************************************************************************
* @fn void wfx_retry_connection(uint16_t retryAttempt)
* @brief
* During commissioning, we retry to join the network MAX_JOIN_RETRIES_COUNT times.
* If DUT is disconnected from the AP or device is power cycled, then retry connection
* with AP continously after a certain time interval.
* @param[in] retryAttempt
* @return None
*************************************************************************************/
void wfx_retry_connection(uint16_t retryAttempt)
// TODO: The retry stategy needs to be re-worked
void ScheduleConnectionAttempt()
{
if (retryInterval > kWlanMaxRetryIntervalsInSec)
{
Expand All @@ -185,9 +168,9 @@ void wfx_retry_connection(uint16_t retryAttempt)
{
ChipLogProgress(DeviceLayer, "Failed to start retry timer");
// Sending the join command if retry timer failed to start
if (wfx_connect_to_ap() != SL_STATUS_OK)
if (ConnectToAccessPoint() != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "wfx_connect_to_ap() failed.");
ChipLogError(DeviceLayer, "ConnectToAccessPoint() failed.");
}

#if CHIP_CONFIG_ENABLE_ICD_SERVER
Expand All @@ -201,6 +184,6 @@ void wfx_retry_connection(uint16_t retryAttempt)
Silabs::WifiSleepManager::GetInstance().RemoveHighPerformanceRequest();
#endif // CHIP_CONFIG_ENABLE_ICD_SERVER

ChipLogProgress(DeviceLayer, "wfx_retry_connection : Next attempt after %d Seconds", retryInterval);
ChipLogProgress(DeviceLayer, "ScheduleConnectionAttempt : Next attempt after %d Seconds", retryInterval);
retryInterval += retryInterval;
}
Loading

0 comments on commit 0201fc5

Please sign in to comment.