Skip to content

Commit 08234fe

Browse files
mkardous-silabsgmarcosb
authored andcommitted
[Silabs] Finish WifiInterface function API cleanup (project-chip#37746)
* Finish WifiInterface cleanup * Use C-Like Array * Update impl files * Update NetworkCommissioningWifiDriver for C-Like arrays * fix copy string * Fix has ipv6 address check
1 parent 38f5dea commit 08234fe

10 files changed

+334
-371
lines changed

src/platform/silabs/ConfigurationManagerImpl.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ void ConfigurationManagerImpl::DoFactoryReset(intptr_t arg)
297297
}
298298

299299
ChipLogProgress(DeviceLayer, "Clearing WiFi provision");
300-
wfx_clear_wifi_provision();
300+
ClearWifiCredentials();
301301
#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI_STATION
302302

303303
// Restart the system.

src/platform/silabs/ConnectivityManagerImpl_WIFI.cpp

+5-17
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,7 @@ ConnectivityManager::WiFiStationMode ConnectivityManagerImpl::_GetWiFiStationMod
143143

144144
bool ConnectivityManagerImpl::_IsWiFiStationProvisioned(void)
145145
{
146-
wfx_wifi_provision_t wifiConfig;
147-
if (wfx_get_wifi_provision(&wifiConfig))
148-
{
149-
return (wifiConfig.ssid[0] != 0);
150-
}
151-
return false;
146+
return IsWifiProvisioned();
152147
}
153148

154149
bool ConnectivityManagerImpl::_IsWiFiStationEnabled(void)
@@ -181,7 +176,7 @@ void ConnectivityManagerImpl::_ClearWiFiStationProvision(void)
181176
{
182177
if (mWiFiStationMode != kWiFiStationMode_ApplicationControlled)
183178
{
184-
wfx_clear_wifi_provision();
179+
ClearWifiCredentials();
185180

186181
DeviceLayer::SystemLayer().ScheduleWork(DriveStationState, NULL);
187182
}
@@ -304,14 +299,7 @@ void ConnectivityManagerImpl::DriveStationState()
304299
if (mWiFiStationState != kWiFiStationState_Connecting)
305300
{
306301
ChipLogProgress(DeviceLayer, "Attempting to connect WiFi");
307-
308-
sl_status_t status = wfx_connect_to_ap();
309-
if (status != SL_STATUS_OK)
310-
{
311-
ChipLogError(DeviceLayer, "wfx_connect_to_ap() failed: %" PRId32, status);
312-
// TODO: Clean the function up to remove the usage of goto
313-
goto exit;
314-
}
302+
SuccessOrExitAction(ConnectToAccessPoint(), ChipLogError(DeviceLayer, "ConnectToAccessPoint() failed"));
315303

316304
ChangeWiFiStationState(kWiFiStationState_Connecting);
317305
}
@@ -388,9 +376,9 @@ void ConnectivityManagerImpl::UpdateInternetConnectivityState(void)
388376
if (mWiFiStationState == kWiFiStationState_Connected)
389377
{
390378
#if CHIP_DEVICE_CONFIG_ENABLE_IPV4
391-
haveIPv4Conn = wfx_have_ipv4_addr(SL_WFX_STA_INTERFACE);
379+
haveIPv4Conn = HasAnIPv4Address();
392380
#endif /* CHIP_DEVICE_CONFIG_ENABLE_IPV4 */
393-
haveIPv6Conn = wfx_have_ipv6_addr(SL_WFX_STA_INTERFACE);
381+
haveIPv6Conn = HasAnIPv6Address();
394382
}
395383

396384
// If the internet connectivity state has changed...

src/platform/silabs/NetworkCommissioningWiFiDriver.cpp

+16-11
Original file line numberDiff line numberDiff line change
@@ -144,22 +144,22 @@ CHIP_ERROR SlWiFiDriver::ConnectWiFiNetwork(const char * ssid, uint8_t ssidLen,
144144
ReturnErrorOnFailure(ConnectivityMgr().SetWiFiStationMode(ConnectivityManager::kWiFiStationMode_Disabled));
145145

146146
// Set the wifi configuration
147-
wfx_wifi_provision_t wifiConfig;
148-
memset(&wifiConfig, 0, sizeof(wifiConfig));
147+
WifiCredentials wifiConfig;
148+
wifiConfig.Clear();
149149

150150
VerifyOrReturnError(ssidLen <= WFX_MAX_SSID_LENGTH, CHIP_ERROR_BUFFER_TOO_SMALL);
151151
memcpy(wifiConfig.ssid, ssid, ssidLen);
152-
wifiConfig.ssid_length = ssidLen;
152+
wifiConfig.ssidLength = ssidLen;
153153

154154
VerifyOrReturnError(keyLen < WFX_MAX_PASSKEY_LENGTH, CHIP_ERROR_BUFFER_TOO_SMALL);
155155
memcpy(wifiConfig.passkey, key, keyLen);
156-
wifiConfig.passkey_length = keyLen;
156+
wifiConfig.passkeyLength = keyLen;
157157

158158
wifiConfig.security = WFX_SEC_WPA2;
159159

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

327327
CHIP_ERROR GetConnectedNetwork(Network & network)
328328
{
329-
wfx_wifi_provision_t wifiConfig;
329+
WifiCredentials wifiConfig;
330330
network.networkIDLen = 0;
331331
network.connected = false;
332+
332333
// we are able to fetch the wifi provision data and STA should be connected
333-
VerifyOrReturnError(wfx_get_wifi_provision(&wifiConfig), CHIP_ERROR_UNINITIALIZED);
334334
VerifyOrReturnError(IsStationConnected(), CHIP_ERROR_NOT_CONNECTED);
335+
ReturnErrorOnFailure(GetWifiCredentials(wifiConfig));
336+
VerifyOrReturnError(wifiConfig.ssidLength < NetworkCommissioning::kMaxNetworkIDLen, CHIP_ERROR_BUFFER_TOO_SMALL);
337+
335338
network.connected = true;
336-
uint8_t length = strnlen(wifiConfig.ssid, DeviceLayer::Internal::kMaxWiFiSSIDLength);
337-
VerifyOrReturnError(length < sizeof(network.networkID), CHIP_ERROR_BUFFER_TOO_SMALL);
338-
memcpy(network.networkID, wifiConfig.ssid, length);
339-
network.networkIDLen = length;
339+
340+
ByteSpan ssidSpan(wifiConfig.ssid, wifiConfig.ssidLength);
341+
MutableByteSpan networkIdSpan(network.networkID, NetworkCommissioning::kMaxNetworkIDLen);
342+
343+
ReturnErrorOnFailure(CopySpanToMutableSpan(ssidSpan, networkIdSpan));
344+
network.networkIDLen = networkIdSpan.size();
340345

341346
return CHIP_NO_ERROR;
342347
}

src/platform/silabs/wifi/SiWx/WifiInterfaceImpl.cpp

+21-21
Original file line numberDiff line numberDiff line change
@@ -346,16 +346,16 @@ sl_status_t ScanCallback(sl_wifi_event_t event, sl_wifi_scan_result_t * scan_res
346346

347347
sl_status_t InitiateScan()
348348
{
349-
sl_status_t status = SL_STATUS_OK;
350-
351-
sl_wifi_ssid_t ssid = { 0 };
352-
349+
sl_status_t status = SL_STATUS_OK;
350+
sl_wifi_ssid_t ssid = { 0 };
353351
sl_wifi_scan_configuration_t wifi_scan_configuration = default_wifi_scan_configuration;
354352

355-
ssid.length = wfx_rsi.sec.ssid_length;
353+
ssid.length = wfx_rsi.credentials.ssidLength;
354+
355+
chip::ByteSpan requestedSsidSpan(wfx_rsi.credentials.ssid, wfx_rsi.credentials.ssidLength);
356+
chip::MutableByteSpan ssidSpan(ssid.value, ssid.length);
357+
chip::CopySpanToMutableSpan(requestedSsidSpan, ssidSpan);
356358

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

361361
osSemaphoreAcquire(sScanInProgressSemaphore, osWaitForever);
@@ -394,19 +394,20 @@ sl_status_t SetWifiConfigurations()
394394
VerifyOrReturnError(status == SL_STATUS_OK, status);
395395
#endif // CHIP_CONFIG_ENABLE_ICD_SERVER
396396

397-
if (wfx_rsi.sec.passkey_length != 0)
397+
if (wfx_rsi.credentials.passkeyLength != 0)
398398
{
399-
status = sl_net_set_credential(SL_NET_DEFAULT_WIFI_CLIENT_CREDENTIAL_ID, SL_NET_WIFI_PSK, &wfx_rsi.sec.passkey[0],
400-
wfx_rsi.sec.passkey_length);
399+
status = sl_net_set_credential(SL_NET_DEFAULT_WIFI_CLIENT_CREDENTIAL_ID, SL_NET_WIFI_PSK, &wfx_rsi.credentials.passkey[0],
400+
wfx_rsi.credentials.passkeyLength);
401401
VerifyOrReturnError(status == SL_STATUS_OK, status,
402402
ChipLogError(DeviceLayer, "sl_net_set_credential failed: 0x%lx", status));
403403
}
404404

405405
sl_net_wifi_client_profile_t profile = {
406406
.config = {
407407
.ssid = {
408+
.value = { 0 },
408409
//static cast because the types dont match
409-
.length = static_cast<uint8_t>(wfx_rsi.sec.ssid_length),
410+
.length = static_cast<uint8_t>(wfx_rsi.credentials.ssidLength),
410411
},
411412
.channel = {
412413
.channel = SL_WIFI_AUTO_CHANNEL,
@@ -427,8 +428,10 @@ sl_status_t SetWifiConfigurations()
427428
.ip = {{{0}}},
428429
}
429430
};
430-
// TODO: memcpy for now since the types dont match
431-
memcpy((char *) &profile.config.ssid.value, wfx_rsi.sec.ssid, wfx_rsi.sec.ssid_length);
431+
432+
chip::MutableByteSpan output(profile.config.ssid.value, WFX_MAX_SSID_LENGTH);
433+
chip::ByteSpan input(wfx_rsi.credentials.ssid, wfx_rsi.credentials.ssidLength);
434+
chip::CopySpanToMutableSpan(input, output);
432435

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

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

506509
wfx_rsi.dev_state.Clear(WifiState::kStationConnecting).Clear(WifiState::kStationConnected);
507-
508-
ChipLogProgress(DeviceLayer, "Connection retry attempt %d", wfx_rsi.join_retries);
509-
wfx_retry_connection(++wfx_rsi.join_retries);
510+
ScheduleConnectionAttempt();
510511

511512
return status;
512513
}
@@ -522,12 +523,11 @@ CHIP_ERROR GetAccessPointInfo(wfx_wifi_scan_result_t & info)
522523
{
523524
// TODO: Convert this to a int8
524525
int32_t rssi = 0;
525-
info.security = wfx_rsi.sec.security;
526+
info.security = wfx_rsi.credentials.security;
526527
info.chan = wfx_rsi.ap_chan;
527528

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

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

796-
sl_matter_wifi_task_started();
796+
NotifyWifiTaskInitialized();
797797

798798
ChipLogDetail(DeviceLayer, "MatterWifiTask: starting event loop");
799799
for (;;)

src/platform/silabs/wifi/WifiInterface.cpp

+10-27
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,14 @@ bool hasNotifiedIPV4 = false;
5656
*/
5757
void RetryConnectionTimerHandler(void * arg)
5858
{
59-
if (wfx_connect_to_ap() != SL_STATUS_OK)
59+
if (ConnectToAccessPoint() != CHIP_NO_ERROR)
6060
{
61-
ChipLogError(DeviceLayer, "wfx_connect_to_ap() failed.");
61+
ChipLogError(DeviceLayer, "ConnectToAccessPoint() failed.");
6262
}
6363
}
6464

6565
} // namespace
6666

67-
/* Updated functions */
68-
6967
void NotifyIPv6Change(bool gotIPv6Addr)
7068
{
7169
hasNotifiedIPV6 = gotIPv6Addr;
@@ -132,19 +130,12 @@ void ResetIPNotificationStates()
132130
hasNotifiedIPV4 = false;
133131
#endif // CHIP_DEVICE_CONFIG_ENABLE_IPV4
134132
}
135-
/* Function to update */
136-
137-
/***********************************************************************************
138-
* @fn sl_matter_wifi_task_started(void)
139-
* @brief
140-
* Wifi device started notification
141-
* @param[in]: None
142-
* @return None
143-
*************************************************************************************/
144-
void sl_matter_wifi_task_started(void)
133+
134+
void NotifyWifiTaskInitialized(void)
145135
{
146136
sl_wfx_startup_ind_t evt = {};
147137

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

169-
/**************************************************************************************
170-
* @fn void wfx_retry_connection(uint16_t retryAttempt)
171-
* @brief
172-
* During commissioning, we retry to join the network MAX_JOIN_RETRIES_COUNT times.
173-
* If DUT is disconnected from the AP or device is power cycled, then retry connection
174-
* with AP continously after a certain time interval.
175-
* @param[in] retryAttempt
176-
* @return None
177-
*************************************************************************************/
178-
void wfx_retry_connection(uint16_t retryAttempt)
160+
// TODO: The retry stategy needs to be re-worked
161+
void ScheduleConnectionAttempt()
179162
{
180163
if (retryInterval > kWlanMaxRetryIntervalsInSec)
181164
{
@@ -185,9 +168,9 @@ void wfx_retry_connection(uint16_t retryAttempt)
185168
{
186169
ChipLogProgress(DeviceLayer, "Failed to start retry timer");
187170
// Sending the join command if retry timer failed to start
188-
if (wfx_connect_to_ap() != SL_STATUS_OK)
171+
if (ConnectToAccessPoint() != CHIP_NO_ERROR)
189172
{
190-
ChipLogError(DeviceLayer, "wfx_connect_to_ap() failed.");
173+
ChipLogError(DeviceLayer, "ConnectToAccessPoint() failed.");
191174
}
192175

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

204-
ChipLogProgress(DeviceLayer, "wfx_retry_connection : Next attempt after %d Seconds", retryInterval);
187+
ChipLogProgress(DeviceLayer, "ScheduleConnectionAttempt : Next attempt after %d Seconds", retryInterval);
205188
retryInterval += retryInterval;
206189
}

0 commit comments

Comments
 (0)