Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions src/platform/silabs/DiagnosticDataProviderImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <platform/internal/CHIPDeviceLayerInternal.h>

#include <platform/DiagnosticDataProvider.h>
#include <platform/NetworkCommissioning.h>
#include <platform/silabs/DiagnosticDataProviderImpl.h>
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
#include <platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.h>
Expand All @@ -35,6 +36,7 @@
#include <sl_cmsis_os2_common.h>

using namespace ::chip::app::Clusters::GeneralDiagnostics;
using namespace ::chip::DeviceLayer::NetworkCommissioning;

namespace chip {
namespace DeviceLayer {
Expand Down Expand Up @@ -335,15 +337,15 @@ void DiagnosticDataProviderImpl::ReleaseNetworkInterfaces(NetworkInterface * net
#if defined(SL_WIFI) && SL_WIFI
CHIP_ERROR DiagnosticDataProviderImpl::GetWiFiBssId(MutableByteSpan & BssId)
{
constexpr size_t bssIdSize = 6;
wfx_wifi_scan_result_t ap = { 0 };
using namespace ::chip::DeviceLayer::Internal;
WiFiScanResponse ap = {};

VerifyOrReturnError(BssId.size() >= bssIdSize, CHIP_ERROR_BUFFER_TOO_SMALL);
VerifyOrReturnError(BssId.size() >= kWiFiBSSIDLength, CHIP_ERROR_BUFFER_TOO_SMALL);

if (Silabs::WifiInterface::GetInstance().GetAccessPointInfo(ap) == CHIP_NO_ERROR)
{
memcpy(BssId.data(), ap.bssid, bssIdSize);
BssId.reduce_size(bssIdSize);
memcpy(BssId.data(), ap.bssid, kWiFiBSSIDLength);
BssId.reduce_size(kWiFiBSSIDLength);
return CHIP_NO_ERROR;
}

Expand All @@ -355,8 +357,8 @@ CHIP_ERROR DiagnosticDataProviderImpl::GetWiFiSecurityType(app::Clusters::WiFiNe
using app::Clusters::NetworkCommissioning::WiFiSecurityBitmap;
using app::Clusters::WiFiNetworkDiagnostics::SecurityTypeEnum;

wfx_wifi_scan_result_t ap = { 0 };
CHIP_ERROR error = Silabs::WifiInterface::GetInstance().GetAccessPointInfo(ap);
WiFiScanResponse ap = {};
CHIP_ERROR error = Silabs::WifiInterface::GetInstance().GetAccessPointInfo(ap);
VerifyOrReturnError(error == CHIP_NO_ERROR, error);

// Map Matter WiFiSecurityBitmap to WiFiNetworkDiagnostics SecurityTypeEnum (prefer highest)
Expand Down Expand Up @@ -395,23 +397,23 @@ CHIP_ERROR DiagnosticDataProviderImpl::GetWiFiVersion(app::Clusters::WiFiNetwork

CHIP_ERROR DiagnosticDataProviderImpl::GetWiFiChannelNumber(uint16_t & channelNumber)
{
wfx_wifi_scan_result_t ap = { 0 };
CHIP_ERROR error = Silabs::WifiInterface::GetInstance().GetAccessPointInfo(ap);
WiFiScanResponse ap = {};
CHIP_ERROR error = Silabs::WifiInterface::GetInstance().GetAccessPointInfo(ap);
if (error == CHIP_NO_ERROR)
{
channelNumber = ap.chan;
channelNumber = ap.channel;
return CHIP_NO_ERROR;
}
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
}

CHIP_ERROR DiagnosticDataProviderImpl::GetWiFiRssi(int8_t & rssi)
{
wfx_wifi_scan_result_t ap = { 0 };
CHIP_ERROR error = Silabs::WifiInterface::GetInstance().GetAccessPointInfo(ap);
WiFiScanResponse ap = {};
CHIP_ERROR error = Silabs::WifiInterface::GetInstance().GetAccessPointInfo(ap);
if (error == CHIP_NO_ERROR)
{
rssi = ap.rssi;
rssi = ap.signal.strength;
return CHIP_NO_ERROR;
}
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
Expand Down
15 changes: 2 additions & 13 deletions src/platform/silabs/NetworkCommissioningWiFiDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ bool SlWiFiDriver::StartScanWiFiNetworks(ByteSpan ssid)
return true;
}

void SlWiFiDriver::OnScanWiFiNetworkDone(wfx_wifi_scan_result_t * aScanResult)
void SlWiFiDriver::OnScanWiFiNetworkDone(NetworkCommissioning::WiFiScanResponse * aScanResult)
{
SlWiFiDriver * nwDriver = NetworkCommissioning::SlWiFiDriver::GetInstance();
// Cannot use the driver if the instance is not initialized.
Expand Down Expand Up @@ -279,18 +279,7 @@ void SlWiFiDriver::OnScanWiFiNetworkDone(wfx_wifi_scan_result_t * aScanResult)
}
else
{
NetworkCommissioning::WiFiScanResponse scanResponse = {};

scanResponse.security = aScanResult->security;
scanResponse.channel = aScanResult->chan;
scanResponse.signal.type = NetworkCommissioning::WirelessSignalType::kdBm;
scanResponse.signal.strength = aScanResult->rssi;
scanResponse.ssidLen = aScanResult->ssid_length;
memcpy(scanResponse.ssid, aScanResult->ssid, scanResponse.ssidLen);
memcpy(scanResponse.bssid, aScanResult->bssid, sizeof(scanResponse.bssid));
scanResponse.wiFiBand = aScanResult->wiFiBand;

mScanResponseIter.Add(&scanResponse);
mScanResponseIter.Add(aScanResult);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/platform/silabs/NetworkCommissioningWiFiDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class SlWiFiDriver final : public WiFiDriver
private:
bool NetworkMatch(const Silabs::WifiInterface::WiFiCredentials & network, ByteSpan networkId);
bool StartScanWiFiNetworks(ByteSpan ssid);
static void OnScanWiFiNetworkDone(wfx_wifi_scan_result_t * aScanResult);
static void OnScanWiFiNetworkDone(NetworkCommissioning::WiFiScanResponse * aScanResult);

static SlWiFiDriver * mDriver;
Silabs::WifiInterface::WiFiCredentials mSavedNetwork = {};
Expand Down
100 changes: 55 additions & 45 deletions src/platform/silabs/wifi/SiWx/WifiInterfaceImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,22 @@
#include "sl_matter_wifi_config.h"
#endif // SL_MATTER_GN_BUILD

#include <cmsis_os2.h>
#include <sl_cmsis_os2_common.h>

#include <algorithm>

#include "ble_config.h"
#include "sl_status.h"
#include "sl_wifi_device.h"

#include <app/icd/server/ICDServerConfig.h>
#include <cmsis_os2.h>
#include <inet/IPAddress.h>
#include <lib/support/CHIPMem.h>
#include <lib/support/CHIPMemString.h>
#include <lib/support/logging/CHIPLogging.h>
#include <platform/NetworkCommissioning.h>
#include <platform/silabs/wifi/SiWx/WifiInterfaceImpl.h>
#include <sl_cmsis_os2_common.h>

extern "C" {
#include "sl_si91x_driver.h"
Expand Down Expand Up @@ -255,60 +260,62 @@ sl_status_t BackgroundScanCallback(sl_wifi_event_t event, sl_wifi_scan_result_t
VerifyOrReturnError(result != nullptr, SL_STATUS_NULL_POINTER);
VerifyOrReturnError(wfx_rsi.scan_cb != nullptr, SL_STATUS_INVALID_HANDLE);

sl_wifi_ssid_t * requestedSsidPtr = nullptr;
chip::ByteSpan requestedSsidSpan;
// arg is the requested SSID pointer passed during sl_wifi_set_scan_callback
chip::ByteSpan requestedSsidSpan = {};

// arg is set to requested SSID if provided in sl_wifi_set_scan_callback
if (arg != nullptr)
{
requestedSsidPtr = reinterpret_cast<sl_wifi_ssid_t *>(arg);
sl_wifi_ssid_t * requestedSsidPtr = static_cast<sl_wifi_ssid_t *>(arg);
VerifyOrReturnError((std::is_same_v<decltype(requestedSsidPtr), sl_wifi_ssid_t *>), SL_STATUS_INVALID_HANDLE,
ChipLogDetail(DeviceLayer, "Expected sl_wifi_ssid_t *"));
Comment on lines +269 to +270
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This type check using std::is_same_v is redundant. The variable requestedSsidPtr is already declared as sl_wifi_ssid_t *, so decltype(requestedSsidPtr) will always resolve to sl_wifi_ssid_t *. The check is equivalent to std::is_same_v<sl_wifi_ssid_t *, sl_wifi_ssid_t *>, which is always true. These lines can be safely removed.

requestedSsidSpan = chip::ByteSpan(requestedSsidPtr->value, requestedSsidPtr->length);
}

uint32_t nbreResults = result->scan_count;
for (uint32_t i = 0; i < nbreResults; i++)
{
wfx_wifi_scan_result_t currentScanResult = { 0 };

// Length excludes null-character
size_t scannedSsidLength = strnlen(reinterpret_cast<char *>(result->scan_info[i].ssid), kMaxWiFiSSIDLength);
chip::ByteSpan scannedSsidSpan(result->scan_info[i].ssid, scannedSsidLength);

// Copy the scanned SSID to the current scan ssid buffer that will be forwarded to the callback
chip::MutableByteSpan currentScanSsid(currentScanResult.ssid, kMaxWiFiSSIDLength);
ReturnValueOnFailure(chip::CopySpanToMutableSpan(scannedSsidSpan, currentScanSsid),
SL_STATUS_SI91X_MEMORY_IS_NOT_SUFFICIENT);
currentScanResult.ssid_length = currentScanSsid.size();

chip::ByteSpan inBssid(result->scan_info[i].bssid, kWiFiBSSIDLength);
chip::MutableByteSpan outBssid(currentScanResult.bssid, kWiFiBSSIDLength);
ReturnValueOnFailure(chip::CopySpanToMutableSpan(inBssid, outBssid), SL_STATUS_SI91X_MEMORY_IS_NOT_SUFFICIENT);

currentScanResult.security =
ConvertSlWifiSecurityToBitmap(static_cast<sl_wifi_security_t>(result->scan_info[i].security_mode));
currentScanResult.rssi = (-1) * result->scan_info[i].rssi_val; // The returned value is positive - we need to flip it
currentScanResult.chan = result->scan_info[i].rf_channel;
// TODO: change this when SDK provides values
currentScanResult.wiFiBand = WiFiBandEnum::k2g4;

// if user has provided ssid, check if the current scan result ssid matches the user provided ssid
// NOTE: background scan does not filter by ssid, so we need to do it here
if (!requestedSsidSpan.empty())
{
if (requestedSsidSpan.data_equal(currentScanSsid))
{
wfx_rsi.scan_cb(&currentScanResult);
}
}
else // No ssid was provide - forward all results
size_t ssidLen = strnlen(reinterpret_cast<char *>(result->scan_info[i].ssid), kMaxWiFiSSIDLength);
chip::ByteSpan ssidSpan(result->scan_info[i].ssid, ssidLen);

if (requestedSsidSpan.empty() || requestedSsidSpan.data_equal(ssidSpan))
{

// Create a new scan response for the current scan result
chip::DeviceLayer::NetworkCommissioning::WiFiScanResponse currentScanResult = {};

// Copy the scanned SSID to the scan response
chip::MutableByteSpan responseSsidSpan(currentScanResult.ssid, kMaxWiFiSSIDLength);
VerifyOrReturnError(chip::CopySpanToMutableSpan(ssidSpan, responseSsidSpan) == CHIP_NO_ERROR,
SL_STATUS_SI91X_MEMORY_IS_NOT_SUFFICIENT);
currentScanResult.ssidLen = static_cast<uint8_t>(ssidLen);

// Copy the BSSID to the scan response
chip::ByteSpan bssidSpan(result->scan_info[i].bssid, kWiFiBSSIDLength);
chip::MutableByteSpan responseBssidSpan(currentScanResult.bssid, kWiFiBSSIDLength);
VerifyOrReturnError(chip::CopySpanToMutableSpan(bssidSpan, responseBssidSpan) == CHIP_NO_ERROR,
SL_STATUS_SI91X_MEMORY_IS_NOT_SUFFICIENT);

// Convert the RSSI to a int8_t value
int16_t rssi = std::clamp(((-1) * result->scan_info[i].rssi_val), INT8_MIN, INT8_MAX);

currentScanResult.signal.strength = static_cast<int8_t>(rssi);
currentScanResult.signal.type = chip::DeviceLayer::NetworkCommissioning::WirelessSignalType::kdBm;

currentScanResult.channel = static_cast<uint16_t>(result->scan_info[i].rf_channel);
currentScanResult.wiFiBand = WiFiBandEnum::k2g4;

currentScanResult.security =
ConvertSlWifiSecurityToBitmap(static_cast<sl_wifi_security_t>(result->scan_info[i].security_mode));

wfx_rsi.scan_cb(&currentScanResult);
}
}

// null callback to indicate that the scan is complete
wfx_rsi.scan_cb(nullptr);
// cleanup and return
wfx_rsi.scan_cb = nullptr;

// cleanup the callback
wfx_rsi.scan_cb = nullptr;
wfx_rsi.dev_state.Clear(WifiInterface::WifiState::kScanStarted);
osSemaphoreRelease(sScanCompleteSemaphore);

Expand Down Expand Up @@ -782,7 +789,7 @@ sl_status_t WifiInterfaceImpl::JoinCallback(sl_wifi_event_t event, char * result
return status;
}

CHIP_ERROR WifiInterfaceImpl::GetAccessPointInfo(wfx_wifi_scan_result_t & info)
CHIP_ERROR WifiInterfaceImpl::GetAccessPointInfo(NetworkCommissioning::WiFiScanResponse & info)
{
// TODO: Convert this to a int8
int32_t rssi = 0;
Expand Down Expand Up @@ -811,19 +818,22 @@ CHIP_ERROR WifiInterfaceImpl::GetAccessPointInfo(wfx_wifi_scan_result_t & info)
}

info.security = wfx_rsi.credentials.security;
info.chan = wfx_rsi.ap_chan;
info.channel = wfx_rsi.ap_chan;
info.wiFiBand = WiFiBandEnum::k2g4;

chip::MutableByteSpan output(info.ssid, kMaxWiFiSSIDLength);
chip::ByteSpan ssid(wfx_rsi.credentials.ssid, wfx_rsi.credentials.ssidLen);
ReturnErrorOnFailure(chip::CopySpanToMutableSpan(ssid, output));
info.ssid_length = output.size();
info.ssidLen = static_cast<uint8_t>(output.size());
chip::ByteSpan apBssidSpan(wfx_rsi.ap_bssid.data(), wfx_rsi.ap_bssid.size());
chip::MutableByteSpan bssidSpan(info.bssid, kWiFiBSSIDLength);
ReturnErrorOnFailure(chip::CopySpanToMutableSpan(apBssidSpan, bssidSpan));

// TODO: add error processing
sl_wifi_get_signal_strength(SL_WIFI_CLIENT_INTERFACE, &(rssi));
info.rssi = rssi;
info.signal.strength = static_cast<int8_t>(
std::clamp(rssi, static_cast<int32_t>(INT8_MIN), static_cast<int32_t>(INT8_MAX)));
info.signal.type = NetworkCommissioning::WirelessSignalType::kdBm;
return CHIP_NO_ERROR;
}

Expand Down
2 changes: 1 addition & 1 deletion src/platform/silabs/wifi/SiWx/WifiInterfaceImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class WifiInterfaceImpl final : public WifiInterface
void CancelScanNetworks() override;
bool IsWifiProvisioned() override;
CHIP_ERROR InitWiFiStack(void) override;
CHIP_ERROR GetAccessPointInfo(wfx_wifi_scan_result_t & info) override;
CHIP_ERROR GetAccessPointInfo(chip::DeviceLayer::NetworkCommissioning::WiFiScanResponse & info) override;
CHIP_ERROR GetAccessPointExtendedInfo(wfx_wifi_scan_ext_t & info) override;
CHIP_ERROR ResetCounters() override;

Expand Down
16 changes: 3 additions & 13 deletions src/platform/silabs/wifi/WifiInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,7 @@ constexpr size_t kWiFiMacAddressLength = 6;
/* Defines to update */
#define MAX_JOIN_RETRIES_COUNT (5)

typedef struct wfx_wifi_scan_result
{
uint8_t ssid[chip::DeviceLayer::Internal::kMaxWiFiSSIDLength]; // excludes null-character
size_t ssid_length;
chip::BitFlags<chip::app::Clusters::NetworkCommissioning::WiFiSecurityBitmap> security;
uint8_t bssid[chip::DeviceLayer::Internal::kWiFiBSSIDLength];
uint8_t chan;
int16_t rssi; /* I suspect this is in dBm - so signed */
chip::app::Clusters::NetworkCommissioning::WiFiBandEnum wiFiBand;
} wfx_wifi_scan_result_t;
using ScanCallback = void (*)(wfx_wifi_scan_result_t *);
using ScanCallback = void (*)(chip::DeviceLayer::NetworkCommissioning::WiFiScanResponse *);

typedef struct wfx_wifi_scan_ext
{
Expand Down Expand Up @@ -246,14 +236,14 @@ class WifiInterface : public WifiStateProvider, public PowerSaveInterface

/**
* @brief Gets the connected access point information.
* See @wfx_wifi_scan_result_t for the information that is returned by the function.
* See @NetworkCommissioning::WiFiScanResponse for the information that is returned by the function.
*
* @param[out] info AP information
*
* @return CHIP_ERROR CHIP_NO_ERROR, device has succesfully pulled all the AP information
* CHIP_ERROR_INTERNAL, otherwise. If the function returns an error, the data in ap cannot be used.
*/
virtual CHIP_ERROR GetAccessPointInfo(wfx_wifi_scan_result_t & info) = 0;
virtual CHIP_ERROR GetAccessPointInfo(chip::DeviceLayer::NetworkCommissioning::WiFiScanResponse & info) = 0;

/**
* @brief Gets the connected access point extended information.
Expand Down
Loading