Skip to content

Commit

Permalink
fixing the scan for the wifi
Browse files Browse the repository at this point in the history
  • Loading branch information
chirag-silabs committed Oct 30, 2024
1 parent 934ac68 commit 7c56b89
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 18 deletions.
20 changes: 15 additions & 5 deletions examples/platform/silabs/SiWx917/SiWx917/sl_wifi_if.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "silabs_utils.h"
#if (SL_MATTER_GN_BUILD == 0)
#include "sl_matter_wifi_config.h"
#endif // SL_MATTER_GN_BUILD
Expand All @@ -43,6 +43,7 @@
#include <lib/support/CHIPMemString.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/logging/CHIPLogging.h>
#include <platform/CHIPDeviceLayer.h>

extern "C" {
#include "sl_net.h"
Expand Down Expand Up @@ -543,27 +544,36 @@ sl_status_t show_scan_results(sl_wifi_scan_result_t * scan_result)
{
memset(&cur_scan_result, 0, sizeof(cur_scan_result));

cur_scan_result.ssid_length = strnlen((char *) scan_result->scan_info[idx].ssid,
chip::min<size_t>(sizeof(scan_result->scan_info[idx].ssid), WFX_MAX_SSID_LENGTH));
// cur_scan_result.ssid_length = strnlen((char *) scan_result->scan_info[idx].ssid,
// chip::min<size_t>(sizeof(scan_result->scan_info[idx].ssid) + 1, WFX_MAX_SSID_LENGTH));
cur_scan_result.ssid_length = chip::min<size_t>(strnlen((char *) scan_result->scan_info[idx].ssid, chip::DeviceLayer::Internal::kMaxWiFiSSIDLength) + 1, chip::DeviceLayer::Internal::kMaxWiFiSSIDLength); // +1 for null termination
chip::Platform::CopyString(cur_scan_result.ssid, cur_scan_result.ssid_length, (char *) scan_result->scan_info[idx].ssid);

SILABS_LOG("ssid : %s", cur_scan_result.ssid);
SILABS_LOG("scan ssid : %s", wfx_rsi.scan_ssid);
// if user has provided ssid, then check if the current scan result ssid matches the user provided ssid
if (wfx_rsi.scan_ssid != NULL &&
(strncmp(wfx_rsi.scan_ssid, cur_scan_result.ssid, MIN(strlen(wfx_rsi.scan_ssid), strlen(cur_scan_result.ssid))) ==
(strncmp(wfx_rsi.scan_ssid, cur_scan_result.ssid, MIN(strlen(wfx_rsi.scan_ssid), strlen(cur_scan_result.ssid))) !=
CMP_SUCCESS))
{
SILABS_LOG("%d",__LINE__);
continue;
}
cur_scan_result.security = static_cast<wfx_sec_t>(scan_result->scan_info[idx].security_mode);
cur_scan_result.rssi = (-1) * scan_result->scan_info[idx].rssi_val;
memcpy(cur_scan_result.bssid, scan_result->scan_info[idx].bssid, BSSID_LEN);
wfx_rsi.scan_cb(&cur_scan_result);
SILABS_LOG("%d",__LINE__);

// if user has not provided the ssid, then call the callback for each scan result
if (wfx_rsi.scan_ssid == NULL)
{
SILABS_LOG("%d",__LINE__);

continue;
}
SILABS_LOG("%d",__LINE__);

break;
}

Expand Down Expand Up @@ -604,7 +614,7 @@ static void wfx_rsi_save_ap_info(void) // translation
sl_wifi_ssid_t ssid_arg;
memset(&ssid_arg, 0, sizeof(ssid_arg));
ssid_arg.length = wfx_rsi.sec.ssid_length;
chip::Platform::CopyString((char *) &ssid_arg.value[0], ssid_arg.length, wfx_rsi.sec.ssid);
memcpy((char *) &ssid_arg.value[0], wfx_rsi.sec.ssid, ssid_arg.length);
sl_wifi_set_scan_callback(scan_callback_handler, NULL);
scan_results_complete = false;
#ifndef EXP_BOARD
Expand Down
5 changes: 3 additions & 2 deletions examples/platform/silabs/efr32/rs911x/rsi_if.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -755,8 +755,9 @@ void ProcessEvent(WfxEvent_t inEvent)
scan = &scan_rsp.scan_info[x];
// clear structure and calculate size of SSID
memset(&ap, 0, sizeof(ap));
ap.ssid_length =
strnlen(reinterpret_cast<char *>(scan->ssid), chip::min<size_t>(sizeof(scan->ssid), WFX_MAX_SSID_LENGTH));
// ap.ssid_length =
// strnlen(reinterpret_cast<char *>(scan->ssid), chip::min<size_t>(sizeof(scan->ssid) + 1, WFX_MAX_SSID_LENGTH));
ap.ssid_length = chip::min<size_t>(strnlen(ap.ssid) + 1, WFX_MAX_SSID_LENGTH); // +1 for null termination
chip::Platform::CopyString(ap.ssid, ap.ssid_length, reinterpret_cast<char *>(scan->ssid));

// check if the scanned ssid is the one we are looking for
Expand Down
5 changes: 3 additions & 2 deletions examples/platform/silabs/efr32/wf200/host_if.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,8 @@ int32_t wfx_get_ap_info(wfx_wifi_scan_result_t * ap)
{
int32_t signal_strength;

ap->ssid_length = strnlen(ap_info.ssid, chip::min<size_t>(sizeof(ap_info.ssid), WFX_MAX_SSID_LENGTH));
ap->ssid_length = chip::min<size_t>(strlen(ap_info.ssid) + 1, WFX_MAX_SSID_LENGTH); // +1 for null termination
// ap->ssid_length = strnlen(ap_info.ssid, chip::min<size_t>(sizeof(ap_info.ssid) + 1, WFX_MAX_SSID_LENGTH));
chip::Platform::CopyString(ap->ssid, ap->ssid_length, ap_info.ssid);
memcpy(ap->bssid, ap_info.bssid, sizeof(ap_info.bssid));
ap->security = ap_info.security;
Expand Down Expand Up @@ -1184,7 +1185,7 @@ bool wfx_start_scan(char * ssid, void (*callback)(wfx_wifi_scan_result_t *))
VerifyOrReturnError(scan_cb != nullptr, false);
if (ssid)
{
scan_ssid_length = strnlen(ssid, WFX_MAX_SSID_LENGTH);
scan_ssid_length = chip::min<size_t>(strnlen(ssid) + 1, WFX_MAX_SSID_LENGTH); // +1 for null termination
scan_ssid = reinterpret_cast<char *>(chip::Platform::MemoryAlloc(scan_ssid_length));
VerifyOrReturnError(scan_ssid != nullptr, false);
Platform::CopyString(scan_ssid, scan_ssid_length, ssid);
Expand Down
20 changes: 11 additions & 9 deletions examples/platform/silabs/wifi/wfx_rsi_host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#include <lib/support/CHIPMemString.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/logging/CHIPLogging.h>

#include "wfx_host_events.h"
#include "wfx_rsi.h"

Expand Down Expand Up @@ -362,18 +361,21 @@ int32_t wfx_reset_counts(void)
* @return returns ture if successful,
* false otherwise
*******************************************************************************/
#include "silabs_utils.h"
bool wfx_start_scan(char * ssid, void (*callback)(wfx_wifi_scan_result_t *))
{
// check if already in progress
VerifyOrReturnError(wfx_rsi.scan_cb != nullptr, false);
VerifyOrReturnError(wfx_rsi.scan_cb == nullptr, false);
wfx_rsi.scan_cb = callback;

VerifyOrReturnError(ssid != nullptr, false);
wfx_rsi.scan_ssid_length = strnlen(ssid, chip::min<size_t>(sizeof(ssid), WFX_MAX_SSID_LENGTH));
wfx_rsi.scan_ssid = reinterpret_cast<char *>(chip::Platform::MemoryAlloc(wfx_rsi.scan_ssid_length));
VerifyOrReturnError(wfx_rsi.scan_ssid != nullptr, false);
chip::Platform::CopyString(wfx_rsi.scan_ssid, wfx_rsi.scan_ssid_length, ssid);

// if SSID is provided to scan only that SSID
if(ssid) {
// wfx_rsi.scan_ssid_length = strnlen(ssid, chip::min<size_t>((strlen(ssid) + 1), WFX_MA X_SSID_LENGTH));
wfx_rsi.scan_ssid_length = chip::min<size_t>(strnlen(ssid, chip::DeviceLayer::Internal::kMaxWiFiSSIDLength) + 1, chip::DeviceLayer::Internal::kMaxWiFiSSIDLength);
wfx_rsi.scan_ssid = reinterpret_cast<char *>(chip::Platform::MemoryAlloc(wfx_rsi.scan_ssid_length));
VerifyOrReturnError(wfx_rsi.scan_ssid != nullptr, false);
chip::Platform::CopyString(wfx_rsi.scan_ssid, wfx_rsi.scan_ssid_length, ssid);
SILABS_LOG("%s hhhh %s %d %d", wfx_rsi.scan_ssid, ssid, wfx_rsi.scan_ssid_length, strlen(ssid));
}
WfxEvent_t event;
event.eventType = WFX_EVT_SCAN;
WfxPostEvent(&event);
Expand Down

0 comments on commit 7c56b89

Please sign in to comment.