Skip to content

Commit dfa0987

Browse files
rosahay-silabsbrosahayjmartinez-silabs
authored
[Silabs] Cleanup of logs and checks for WiFi products (#34430)
* Clean up of logs and added checks * Apply suggestions from code review Co-authored-by: Junior Martinez <67972863+jmartinez-silabs@users.noreply.github.com> * Added changes from code review * Replace with chip::platform memory functions * Reduce logs * chore: Fix logging statements and improve error handling in BaseApplication.cpp * Sanitize provisioning WiFi network credentials * Sanitize SSID and SSID length * use CodeUtils min functions * refactor: add void parameter to functions * refactor: fix logging format in wfx_rsi_join_fail_cb and wfx_rsi_init functions * Simplify SSID processing and improve memory handling --------- Co-authored-by: brosahay <3526930+brosahay@users.noreply.github.com> Co-authored-by: Junior Martinez <67972863+jmartinez-silabs@users.noreply.github.com>
1 parent 0140a72 commit dfa0987

13 files changed

+437
-504
lines changed

examples/platform/silabs/BaseApplication.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ void BaseApplication::CancelFactoryResetSequence()
621621
if (sIsFactoryResetTriggered)
622622
{
623623
sIsFactoryResetTriggered = false;
624-
ChipLogProgress(AppServer, "Factory Reset has been Canceled");
624+
ChipLogProgress(AppServer, "Factory Reset has been cancelled");
625625
}
626626
}
627627

@@ -854,7 +854,7 @@ void BaseApplication::OnPlatformEvent(const ChipDeviceEvent * event, intptr_t)
854854
VerifyOrReturn(event->InternetConnectivityChange.IPv4 == kConnectivity_Established);
855855
if (DIC_OK != dic_init(dic::control::subscribeCB))
856856
{
857-
SILABS_LOG("Failed to initialize DIC module\n");
857+
ChipLogError(AppServer, "dic_init failed");
858858
}
859859
#endif // DIC_ENABLE
860860
#ifdef DISPLAY_ENABLED

examples/platform/silabs/SiWx917/SiWx917/sl_wifi_if.cpp

+44-35
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,12 @@
3636
#include "task.h"
3737
#include "wfx_host_events.h"
3838
#include "wfx_rsi.h"
39+
3940
#include <app/icd/server/ICDServerConfig.h>
4041
#include <inet/IPAddress.h>
42+
#include <lib/support/CHIPMem.h>
43+
#include <lib/support/CHIPMemString.h>
44+
#include <lib/support/CodeUtils.h>
4145
#include <lib/support/logging/CHIPLogging.h>
4246

4347
extern "C" {
@@ -120,7 +124,7 @@ static void DHCPTimerEventHandler(void * arg)
120124
WfxPostEvent(&event);
121125
}
122126

123-
static void CancelDHCPTimer()
127+
static void CancelDHCPTimer(void)
124128
{
125129
osStatus_t status;
126130

@@ -164,8 +168,10 @@ int32_t wfx_rsi_get_ap_info(wfx_wifi_scan_result_t * ap)
164168
{
165169
sl_status_t status = SL_STATUS_OK;
166170
int32_t rssi = 0;
171+
ap->ssid_length = wfx_rsi.sec.ssid_length;
167172
ap->security = wfx_rsi.sec.security;
168173
ap->chan = wfx_rsi.ap_chan;
174+
chip::Platform::CopyString(ap->ssid, ap->ssid_length, wfx_rsi.sec.ssid);
169175
memcpy(&ap->bssid[0], &wfx_rsi.ap_mac.octet[0], BSSID_LEN);
170176
sl_wifi_get_signal_strength(SL_WIFI_CLIENT_INTERFACE, &rssi);
171177
ap->rssi = rssi;
@@ -197,14 +203,14 @@ int32_t wfx_rsi_get_ap_ext(wfx_wifi_scan_ext_t * extra_info)
197203
}
198204

199205
/******************************************************************
200-
* @fn int32_t wfx_rsi_reset_count()
206+
* @fn int32_t wfx_rsi_reset_count(void)
201207
* @brief
202208
* Getting the driver reset count
203209
* @param[in] None
204210
* @return
205211
* status
206212
*********************************************************************/
207-
int32_t wfx_rsi_reset_count()
213+
int32_t wfx_rsi_reset_count(void)
208214
{
209215
sl_wifi_statistics_t test = { 0 };
210216
sl_status_t status = SL_STATUS_OK;
@@ -221,14 +227,14 @@ int32_t wfx_rsi_reset_count()
221227
}
222228

223229
/******************************************************************
224-
* @fn wfx_rsi_disconnect()
230+
* @fn wfx_rsi_disconnect(void)
225231
* @brief
226232
* Getting the driver disconnect status
227233
* @param[in] None
228234
* @return
229235
* status
230236
*********************************************************************/
231-
int32_t wfx_rsi_disconnect()
237+
int32_t wfx_rsi_disconnect(void)
232238
{
233239
return sl_wifi_disconnect(SL_WIFI_CLIENT_INTERFACE);
234240
}
@@ -251,18 +257,17 @@ sl_status_t join_callback_handler(sl_wifi_event_t event, char * result, uint32_t
251257
*/
252258
ChipLogDetail(DeviceLayer, "join_callback_handler: success");
253259
memset(&temp_reset, 0, sizeof(temp_reset));
254-
255-
WfxEvent.eventType = WFX_EVT_STA_CONN;
256-
WfxPostEvent(&WfxEvent);
257260
wfx_rsi.join_retries = 0;
258261
callback_status = SL_STATUS_OK;
262+
WfxEvent.eventType = WFX_EVT_STA_CONN;
263+
WfxPostEvent(&WfxEvent);
259264
return SL_STATUS_OK;
260265
}
261266

262267
#if CHIP_CONFIG_ENABLE_ICD_SERVER
263268
#if SLI_SI91X_MCU_INTERFACE
264269
// Required to invoke button press event during sleep as falling edge is not detected
265-
void sl_si91x_invoke_btn_press_event()
270+
void sl_si91x_invoke_btn_press_event(void)
266271
{
267272
// TODO: should be removed once we are getting the press interrupt for button 0 with sleep
268273
if (!RSI_NPSSGPIO_GetPin(SL_BUTTON_BTN0_PIN) && !btn0_pressed)
@@ -298,12 +303,12 @@ void sl_si91x_invoke_btn_press_event()
298303
#endif // SLI_SI91X_MCU_INTERFACE
299304

300305
/******************************************************************
301-
* @fn wfx_rsi_power_save()
306+
* @fn wfx_rsi_power_save(rsi_power_save_profile_mode_t sl_si91x_ble_state, sl_si91x_performance_profile_t sl_si91x_wifi_state)
302307
* @brief
303308
* Setting the RS911x in DTIM sleep based mode
304309
*
305310
* @param[in] sl_si91x_ble_state : State to set for the BLE
306-
sl_si91x_wifi_state : State to set for the WiFi
311+
* @param[in] sl_si91x_wifi_state : State to set for the WiFi
307312
* @return
308313
* None
309314
*********************************************************************/
@@ -339,7 +344,7 @@ int32_t wfx_rsi_power_save(rsi_power_save_profile_mode_t sl_si91x_ble_state, sl_
339344
*****************************************************************************************/
340345
int32_t wfx_wifi_rsi_init(void)
341346
{
342-
ChipLogDetail(DeviceLayer, "wfx_wifi_rsi_init started");
347+
ChipLogDetail(DeviceLayer, "wfx_wifi_rsi_init: started");
343348
sl_status_t status;
344349
status = sl_wifi_init(&config, NULL, sl_wifi_default_event_handler);
345350
VerifyOrReturnError(status == SL_STATUS_OK, status);
@@ -534,7 +539,10 @@ sl_status_t show_scan_results(sl_wifi_scan_result_t * scan_result)
534539
for (int idx = 0; idx < (int) scan_result->scan_count; idx++)
535540
{
536541
memset(&cur_scan_result, 0, sizeof(cur_scan_result));
537-
strncpy(cur_scan_result.ssid, (char *) &scan_result->scan_info[idx].ssid, WFX_MAX_SSID_LENGTH);
542+
543+
cur_scan_result.ssid_length = strnlen((char *) scan_result->scan_info[idx].ssid,
544+
chip::min<size_t>(sizeof(scan_result->scan_info[idx].ssid), WFX_MAX_SSID_LENGTH));
545+
chip::Platform::CopyString(cur_scan_result.ssid, cur_scan_result.ssid_length, (char *) scan_result->scan_info[idx].ssid);
538546

539547
// if user has provided ssid, then check if the current scan result ssid matches the user provided ssid
540548
if (wfx_rsi.scan_ssid != NULL &&
@@ -559,10 +567,10 @@ sl_status_t show_scan_results(sl_wifi_scan_result_t * scan_result)
559567
// cleanup and return
560568
wfx_rsi.dev_state &= ~WFX_RSI_ST_SCANSTARTED;
561569
wfx_rsi.scan_cb((wfx_wifi_scan_result_t *) 0);
562-
wfx_rsi.scan_cb = NULL;
570+
wfx_rsi.scan_cb = nullptr;
563571
if (wfx_rsi.scan_ssid)
564572
{
565-
vPortFree(wfx_rsi.scan_ssid);
573+
chip::Platform::MemoryFree(wfx_rsi.scan_ssid);
566574
wfx_rsi.scan_ssid = NULL;
567575
}
568576
return SL_STATUS_OK;
@@ -576,14 +584,14 @@ sl_status_t bg_scan_callback_handler(sl_wifi_event_t event, sl_wifi_scan_result_
576584
return SL_STATUS_OK;
577585
}
578586
/***************************************************************************************
579-
* @fn static void wfx_rsi_save_ap_info()
587+
* @fn static void wfx_rsi_save_ap_info(void)
580588
* @brief
581589
* Saving the details of the AP
582590
* @param[in] None
583591
* @return
584592
* None
585593
*******************************************************************************************/
586-
static void wfx_rsi_save_ap_info() // translation
594+
static void wfx_rsi_save_ap_info(void) // translation
587595
{
588596
sl_status_t status = SL_STATUS_OK;
589597
#ifndef EXP_BOARD
@@ -592,8 +600,8 @@ static void wfx_rsi_save_ap_info() // translation
592600
#endif
593601
sl_wifi_ssid_t ssid_arg;
594602
memset(&ssid_arg, 0, sizeof(ssid_arg));
595-
ssid_arg.length = strnlen(wfx_rsi.sec.ssid, WFX_MAX_SSID_LENGTH);
596-
strncpy((char *) &ssid_arg.value[0], wfx_rsi.sec.ssid, WFX_MAX_SSID_LENGTH);
603+
ssid_arg.length = wfx_rsi.sec.ssid_length;
604+
chip::Platform::CopyString((char *) &ssid_arg.value[0], ssid_arg.length, wfx_rsi.sec.ssid);
597605
sl_wifi_set_scan_callback(scan_callback_handler, NULL);
598606
scan_results_complete = false;
599607
#ifndef EXP_BOARD
@@ -619,7 +627,7 @@ static sl_status_t wfx_rsi_do_join(void)
619627
sl_status_t status = SL_STATUS_OK;
620628
sl_wifi_client_configuration_t ap;
621629
memset(&ap, 0, sizeof(ap));
622-
WfxEvent_t event;
630+
623631
switch (wfx_rsi.sec.security)
624632
{
625633
case WFX_SEC_WEP:
@@ -662,19 +670,17 @@ static sl_status_t wfx_rsi_do_join(void)
662670
status = sl_wifi_set_advanced_client_configuration(SL_WIFI_CLIENT_INTERFACE, &client_config);
663671
VerifyOrReturnError(status == SL_STATUS_OK, status);
664672
#endif // CHIP_CONFIG_ENABLE_ICD_SERVER
665-
size_t psk_length = strlen(wfx_rsi.sec.passkey);
666-
VerifyOrReturnError(psk_length <= SL_WIFI_MAX_PSK_LENGTH, SL_STATUS_SI91X_INVALID_PSK_LENGTH);
667673
sl_net_credential_id_t id = SL_NET_DEFAULT_WIFI_CLIENT_CREDENTIAL_ID;
668-
status = sl_net_set_credential(id, SL_NET_WIFI_PSK, &wfx_rsi.sec.passkey[0], psk_length);
674+
status = sl_net_set_credential(id, SL_NET_WIFI_PSK, &wfx_rsi.sec.passkey[0], wfx_rsi.sec.passkey_length);
669675
VerifyOrReturnError(status == SL_STATUS_OK, status);
670676

671677
uint32_t timeout_ms = 0;
672-
ap.ssid.length = strnlen(wfx_rsi.sec.ssid, WFX_MAX_SSID_LENGTH);
678+
ap.ssid.length = wfx_rsi.sec.ssid_length;
673679
ap.encryption = SL_WIFI_NO_ENCRYPTION;
674680
ap.credential_id = id;
675-
memset(&ap.ssid.value, 0, (sizeof(ap.ssid.value) / sizeof(ap.ssid.value[0])));
676-
strncpy((char *) &ap.ssid.value[0], wfx_rsi.sec.ssid, WFX_MAX_SSID_LENGTH);
681+
memcpy((char *) &ap.ssid.value[0], wfx_rsi.sec.ssid, wfx_rsi.sec.ssid_length);
677682
ChipLogDetail(DeviceLayer, "wfx_rsi_do_join: SSID: %s, SECURITY: %d(%d)", ap.ssid.value, ap.security, wfx_rsi.sec.security);
683+
678684
status = sl_wifi_connect(SL_WIFI_CLIENT_INTERFACE, &ap, timeout_ms);
679685
// sl_wifi_connect returns SL_STATUS_IN_PROGRESS if join is in progress
680686
// after the initial scan is done, the scan does not check for SSID
@@ -687,24 +693,25 @@ static sl_status_t wfx_rsi_do_join(void)
687693
wfx_rsi.dev_state &= ~(WFX_RSI_ST_STA_CONNECTING | WFX_RSI_ST_STA_CONNECTED);
688694
ChipLogProgress(DeviceLayer, "wfx_rsi_do_join: retry attempt %d", wfx_rsi.join_retries);
689695
wfx_retry_connection(++wfx_rsi.join_retries);
696+
697+
WfxEvent_t event;
690698
event.eventType = WFX_EVT_STA_START_JOIN;
691699
WfxPostEvent(&event);
700+
692701
return status;
693702
}
694703

695704
/// NotifyConnectivity
696705
/// @brief Notify the application about the connectivity status if it has not been notified yet.
697706
/// Helper function for HandleDHCPPolling.
698-
void NotifyConnectivity()
707+
void NotifyConnectivity(void)
699708
{
700-
if (!hasNotifiedWifiConnectivity)
701-
{
702-
wfx_connected_notify(CONNECTION_STATUS_SUCCESS, &wfx_rsi.ap_mac);
703-
hasNotifiedWifiConnectivity = true;
704-
}
709+
VerifyOrReturn(!hasNotifiedWifiConnectivity);
710+
wfx_connected_notify(CONNECTION_STATUS_SUCCESS, &wfx_rsi.ap_mac);
711+
hasNotifiedWifiConnectivity = true;
705712
}
706713

707-
void HandleDHCPPolling()
714+
void HandleDHCPPolling(void)
708715
{
709716
struct netif * sta_netif;
710717
WfxEvent_t event;
@@ -722,6 +729,8 @@ void HandleDHCPPolling()
722729
{
723730
wfx_dhcp_got_ipv4((uint32_t) sta_netif->ip_addr.u_addr.ip4.addr);
724731
hasNotifiedIPV4 = true;
732+
event.eventType = WFX_EVT_STA_DHCP_DONE;
733+
WfxPostEvent(&event);
725734
NotifyConnectivity();
726735
}
727736
else if (dhcp_state == DHCP_OFF)
@@ -760,7 +769,7 @@ void WfxPostEvent(WfxEvent_t * event)
760769
/// ResetDHCPNotificationFlags
761770
/// @brief Reset the flags that are used to notify the application about DHCP connectivity
762771
/// and emits a WFX_EVT_STA_DO_DHCP event to trigger DHCP polling checks. Helper function for ProcessEvent.
763-
void ResetDHCPNotificationFlags()
772+
void ResetDHCPNotificationFlags(void)
764773
{
765774
WfxEvent_t outEvent;
766775

@@ -877,7 +886,7 @@ void ProcessEvent(WfxEvent_t inEvent)
877886
/*********************************************************************************
878887
* @fn void wfx_rsi_task(void *arg)
879888
* @brief
880-
* The main WLAN task - started by wfx_wifi_start () that interfaces with RSI.
889+
* The main WLAN task - started by wfx_wifi_start() that interfaces with RSI.
881890
* The rest of RSI stuff come in call-backs.
882891
* The initialization has been already done.
883892
* @param[in] arg:

0 commit comments

Comments
 (0)