Skip to content

Commit 26a97d2

Browse files
[SL-TEMP] Fixing the scan for the WiFi devices (#81)
Co-authored-by: Junior Martinez <67972863+jmartinez-silabs@users.noreply.github.com>
1 parent 9e3f034 commit 26a97d2

File tree

4 files changed

+20
-17
lines changed

4 files changed

+20
-17
lines changed

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -545,11 +545,12 @@ sl_status_t show_scan_results(sl_wifi_scan_result_t * scan_result)
545545

546546
cur_scan_result.ssid_length = strnlen((char *) scan_result->scan_info[idx].ssid,
547547
chip::min<size_t>(sizeof(scan_result->scan_info[idx].ssid), WFX_MAX_SSID_LENGTH));
548-
chip::Platform::CopyString(cur_scan_result.ssid, cur_scan_result.ssid_length, (char *) scan_result->scan_info[idx].ssid);
548+
// cur_scan_result.ssid is of size WFX_MAX_SSID_LENGTH+1, we are safe with the cur_scan_result.ssid_length calculated above
549+
chip::Platform::CopyString(cur_scan_result.ssid, cur_scan_result.ssid_length + 1, (char *) scan_result->scan_info[idx].ssid); // +1 for null termination
549550

550551
// if user has provided ssid, then check if the current scan result ssid matches the user provided ssid
551552
if (wfx_rsi.scan_ssid != NULL &&
552-
(strncmp(wfx_rsi.scan_ssid, cur_scan_result.ssid, MIN(strlen(wfx_rsi.scan_ssid), strlen(cur_scan_result.ssid))) ==
553+
(strncmp(wfx_rsi.scan_ssid, cur_scan_result.ssid, MIN(strlen(wfx_rsi.scan_ssid), strlen(cur_scan_result.ssid))) !=
553554
CMP_SUCCESS))
554555
{
555556
continue;
@@ -603,8 +604,8 @@ static void wfx_rsi_save_ap_info(void) // translation
603604
#endif
604605
sl_wifi_ssid_t ssid_arg;
605606
memset(&ssid_arg, 0, sizeof(ssid_arg));
606-
ssid_arg.length = wfx_rsi.sec.ssid_length;
607-
chip::Platform::CopyString((char *) &ssid_arg.value[0], ssid_arg.length, wfx_rsi.sec.ssid);
607+
ssid_arg.length = chip::min<size_t>(wfx_rsi.sec.ssid_length, sizeof(ssid_arg.value) - 1);
608+
chip::Platform::CopyString((char *) &ssid_arg.value[0], ssid_arg.length + 1, wfx_rsi.sec.ssid); // +1 for null termination
608609
sl_wifi_set_scan_callback(scan_callback_handler, NULL);
609610
scan_results_complete = false;
610611
#ifndef EXP_BOARD

examples/platform/silabs/efr32/rs911x/rsi_if.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,8 @@ void ProcessEvent(WfxEvent_t inEvent)
757757
memset(&ap, 0, sizeof(ap));
758758
ap.ssid_length =
759759
strnlen(reinterpret_cast<char *>(scan->ssid), chip::min<size_t>(sizeof(scan->ssid), WFX_MAX_SSID_LENGTH));
760-
chip::Platform::CopyString(ap.ssid, ap.ssid_length, reinterpret_cast<char *>(scan->ssid));
760+
// ap.ssid is of size WFX_MAX_SSID_LENGTH+1, we are safe with the ap.ssid_length calculated above
761+
chip::Platform::CopyString(ap.ssid, ap.ssid_length + 1, reinterpret_cast<char *>(scan->ssid)); // +1 for null termination
761762

762763
// check if the scanned ssid is the one we are looking for
763764
if (wfx_rsi.scan_ssid_length != 0 && strncmp(wfx_rsi.scan_ssid, ap.ssid, WFX_MAX_SSID_LENGTH) != CMP_SUCCESS)

examples/platform/silabs/efr32/wf200/host_if.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,8 @@ int32_t wfx_get_ap_info(wfx_wifi_scan_result_t * ap)
737737
int32_t signal_strength;
738738

739739
ap->ssid_length = strnlen(ap_info.ssid, chip::min<size_t>(sizeof(ap_info.ssid), WFX_MAX_SSID_LENGTH));
740-
chip::Platform::CopyString(ap->ssid, ap->ssid_length, ap_info.ssid);
740+
// ap->ssid is of size WFX_MAX_SSID_LENGTH+1, we are safe with the ap->ssid_length calculated above
741+
chip::Platform::CopyString(ap->ssid, ap->ssid_length + 1, ap_info.ssid); // +1 for null termination
741742
memcpy(ap->bssid, ap_info.bssid, sizeof(ap_info.bssid));
742743
ap->security = ap_info.security;
743744
ap->chan = ap_info.chan;
@@ -1184,10 +1185,10 @@ bool wfx_start_scan(char * ssid, void (*callback)(wfx_wifi_scan_result_t *))
11841185
VerifyOrReturnError(scan_cb != nullptr, false);
11851186
if (ssid)
11861187
{
1187-
scan_ssid_length = strnlen(ssid, WFX_MAX_SSID_LENGTH);
1188-
scan_ssid = reinterpret_cast<char *>(chip::Platform::MemoryAlloc(scan_ssid_length));
1188+
scan_ssid_length = strnlen(ssid, chip::min<size_t>(sizeof(ssid), WFX_MAX_SSID_LENGTH));
1189+
scan_ssid = reinterpret_cast<char *>(chip::Platform::MemoryAlloc(scan_ssid_length + 1));
11891190
VerifyOrReturnError(scan_ssid != nullptr, false);
1190-
Platform::CopyString(scan_ssid, scan_ssid_length, ssid);
1191+
Platform::CopyString(scan_ssid, scan_ssid_length + 1, ssid);
11911192
}
11921193
scan_cb = callback;
11931194
xEventGroupSetBits(sl_wfx_event_group, SL_WFX_SCAN_START);

examples/platform/silabs/wifi/wfx_rsi_host.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -365,15 +365,15 @@ int32_t wfx_reset_counts(void)
365365
bool wfx_start_scan(char * ssid, void (*callback)(wfx_wifi_scan_result_t *))
366366
{
367367
// check if already in progress
368-
VerifyOrReturnError(wfx_rsi.scan_cb != nullptr, false);
368+
VerifyOrReturnError(wfx_rsi.scan_cb == nullptr, false);
369369
wfx_rsi.scan_cb = callback;
370-
371-
VerifyOrReturnError(ssid != nullptr, false);
372-
wfx_rsi.scan_ssid_length = strnlen(ssid, chip::min<size_t>(sizeof(ssid), WFX_MAX_SSID_LENGTH));
373-
wfx_rsi.scan_ssid = reinterpret_cast<char *>(chip::Platform::MemoryAlloc(wfx_rsi.scan_ssid_length));
374-
VerifyOrReturnError(wfx_rsi.scan_ssid != nullptr, false);
375-
chip::Platform::CopyString(wfx_rsi.scan_ssid, wfx_rsi.scan_ssid_length, ssid);
376-
370+
// if SSID is provided to scan only that SSID
371+
if(ssid) {
372+
wfx_rsi.scan_ssid_length = strnlen(ssid, chip::min<size_t>(sizeof(ssid), WFX_MAX_SSID_LENGTH));
373+
wfx_rsi.scan_ssid = reinterpret_cast<char *>(chip::Platform::MemoryAlloc(wfx_rsi.scan_ssid_length + 1));
374+
VerifyOrReturnError(wfx_rsi.scan_ssid != nullptr, false);
375+
chip::Platform::CopyString(wfx_rsi.scan_ssid, wfx_rsi.scan_ssid_length + 1, ssid);
376+
}
377377
WfxEvent_t event;
378378
event.eventType = WFX_EVT_SCAN;
379379
WfxPostEvent(&event);

0 commit comments

Comments
 (0)