Skip to content

Commit

Permalink
[Silabs] Clean up WifiInterface public APIs (project-chip#37035)
Browse files Browse the repository at this point in the history
* Refactor wifi task start API

* Clean up function signatures

* Fix incorrect macro

* Add wifi suffix to output directory

* fix function signature

* Improve output build directory

* restyle

* remove hardcode string

* clean up string

* Restyled by whitespace

---------

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
mkardous-silabs and restyled-commits authored Jan 15, 2025
1 parent b5313a7 commit 26a867c
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 161 deletions.
21 changes: 14 additions & 7 deletions scripts/examples/gn_silabs_example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ DOTFILE=".gn"
SILABS_THREAD_TARGET=\""../silabs:ot-efr32-cert"\"
USAGE="./scripts/examples/gn_silabs_example.sh <AppRootFolder> <outputFolder> <silabs_board_name> [<Build options>]"

PROTOCOL_DIR_SUFFIX="thread"
NCP_DIR_SUFFIX=""

if [ "$#" == "0" ]; then
echo "Build script for EFR32 Matter apps
Format:
Expand Down Expand Up @@ -188,6 +191,7 @@ else
echo "--wifi requires rs9116 or SiWx917 or wf200"
exit 1
fi

if [ "$2" = "rs9116" ]; then
optArgs+="use_rs9116=true "
elif [ "$2" = "SiWx917" ]; then
Expand All @@ -198,6 +202,8 @@ else
echo "Wifi usage: --wifi rs9116|SiWx917|wf200"
exit 1
fi

NCP_DIR_SUFFIX="/"$2
USE_WIFI=true
optArgs+="chip_device_platform =\"efr32\" chip_crypto_keystore=\"psa\""
shift
Expand Down Expand Up @@ -321,20 +327,21 @@ else
source "$CHIP_ROOT/scripts/activate.sh"
fi

if [ "$USE_WIFI" == true ]; then
DOTFILE="$ROOT/build_for_wifi_gnfile.gn"
PROTOCOL_DIR_SUFFIX="wifi"
else
DOTFILE="$ROOT/openthread.gn"
fi

PYTHON_PATH="$(which python3)"
BUILD_DIR=$OUTDIR/$SILABS_BOARD
BUILD_DIR=$OUTDIR/$PROTOCOL_DIR_SUFFIX/$SILABS_BOARD$NCP_DIR_SUFFIX
echo BUILD_DIR="$BUILD_DIR"

if [ "$DIR_CLEAN" == true ]; then
rm -rf "$BUILD_DIR"
fi

if [ "$USE_WIFI" == true ]; then
DOTFILE="$ROOT/build_for_wifi_gnfile.gn"
else
DOTFILE="$ROOT/openthread.gn"
fi

if [ "$USE_DOCKER" == true ] && [ "$USE_WIFI" == false ]; then
echo "Switching OpenThread ROOT"
optArgs+="openthread_root=\"$GSDK_ROOT/util/third_party/openthread\" "
Expand Down
42 changes: 22 additions & 20 deletions src/platform/silabs/ConnectivityManagerImpl_WIFI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ CHIP_ERROR ConnectivityManagerImpl::_Init()
// TODO Initialize the Chip Addressing and Routing Module.

// Ensure that station mode is enabled.
wfx_enable_sta_mode();
ConfigureStationMode();

err = DeviceLayer::SystemLayer().ScheduleWork(DriveStationState, NULL);

Expand Down Expand Up @@ -153,7 +153,7 @@ bool ConnectivityManagerImpl::_IsWiFiStationProvisioned(void)

bool ConnectivityManagerImpl::_IsWiFiStationEnabled(void)
{
return wfx_is_sta_mode_enabled();
return IsStationModeEnabled();
}

CHIP_ERROR ConnectivityManagerImpl::_SetWiFiStationMode(ConnectivityManager::WiFiStationMode val)
Expand Down Expand Up @@ -218,26 +218,23 @@ CHIP_ERROR ConnectivityManagerImpl::_SetPollingInterval(System::Clock::Milliseco

void ConnectivityManagerImpl::DriveStationState()
{
sl_status_t serr;
bool stationConnected;
bool stationConnected = false;

// Refresh the current station mode.
GetWiFiStationMode();

// If the station interface is NOT under application control...
if (mWiFiStationMode != kWiFiStationMode_ApplicationControlled)
{
// Ensure that the WFX is started.
if ((serr = wfx_wifi_start()) != SL_STATUS_OK)
{
ChipLogError(DeviceLayer, "wfx_wifi_start() failed: %lx", serr);
return;
}
// Ensure that station mode is enabled in the WFX WiFi layer.
wfx_enable_sta_mode();
// Ensure that the Wifi task is started.
CHIP_ERROR error = StartWifiTask();
VerifyOrReturn(error == CHIP_NO_ERROR, ChipLogError(DeviceLayer, "StartWifiTask() failed: %s", ErrorStr(error)));

// Ensure that station mode is enabled in the WiFi layer.
ConfigureStationMode();
}

stationConnected = wfx_is_sta_connected();
stationConnected = IsStationConnected();

// If the station interface is currently connected ...
if (stationConnected)
Expand All @@ -262,12 +259,14 @@ void ConnectivityManagerImpl::DriveStationState()
(mWiFiStationMode != kWiFiStationMode_Enabled && !IsWiFiStationProvisioned()))
{
ChipLogProgress(DeviceLayer, "Disconnecting WiFi station interface");
serr = sl_matter_wifi_disconnect();
if (serr != SL_STATUS_OK)
sl_status_t status = sl_matter_wifi_disconnect();
if (status != SL_STATUS_OK)
{
ChipLogError(DeviceLayer, "wfx_wifi_disconnect() failed: %lx", serr);
ChipLogError(DeviceLayer, "wfx_wifi_disconnect() failed: %lx", status);

// TODO: Clean the function up to remove the usage of goto
goto exit;
}
SuccessOrExit(serr);

ChangeWiFiStationState(kWiFiStationState_Disconnecting);
}
Expand Down Expand Up @@ -310,11 +309,14 @@ void ConnectivityManagerImpl::DriveStationState()
if (mWiFiStationState != kWiFiStationState_Connecting)
{
ChipLogProgress(DeviceLayer, "Attempting to connect WiFi");
if ((serr = wfx_connect_to_ap()) != SL_STATUS_OK)

sl_status_t status = wfx_connect_to_ap();
if (status != SL_STATUS_OK)
{
ChipLogError(DeviceLayer, "wfx_connect_to_ap() failed: %" PRId32, serr);
ChipLogError(DeviceLayer, "wfx_connect_to_ap() failed: %" PRId32, status);
// TODO: Clean the function up to remove the usage of goto
goto exit;
}
SuccessOrExit(serr);

ChangeWiFiStationState(kWiFiStationState_Connecting);
}
Expand Down
4 changes: 2 additions & 2 deletions src/platform/silabs/NetworkCommissioningWiFiDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ void SlWiFiDriver::UpdateNetworkingStatus()
}

ByteSpan networkId = ByteSpan((const unsigned char *) mStagingNetwork.ssid, mStagingNetwork.ssidLen);
if (!wfx_is_sta_connected())
if (!IsStationConnected())
{
// TODO: https://github.com/project-chip/connectedhomeip/issues/26861
mpStatusChangeCallback->OnNetworkingStatusChange(Status::kUnknownError, MakeOptional(networkId),
Expand Down Expand Up @@ -336,7 +336,7 @@ CHIP_ERROR GetConnectedNetwork(Network & network)
network.connected = false;
// we are able to fetch the wifi provision data and STA should be connected
VerifyOrReturnError(wfx_get_wifi_provision(&wifiConfig), CHIP_ERROR_UNINITIALIZED);
VerifyOrReturnError(wfx_is_sta_connected(), CHIP_ERROR_NOT_CONNECTED);
VerifyOrReturnError(IsStationConnected(), CHIP_ERROR_NOT_CONNECTED);
network.connected = true;
uint8_t length = strnlen(wifiConfig.ssid, DeviceLayer::Internal::kMaxWiFiSSIDLength);
VerifyOrReturnError(length < sizeof(network.networkID), CHIP_ERROR_BUFFER_TOO_SMALL);
Expand Down
2 changes: 1 addition & 1 deletion src/platform/silabs/wifi/SiWx/WifiInterfaceImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ void ProcessEvent(WifiPlatformEvent event)
/*********************************************************************************
* @fn void sl_matter_wifi_task(void *arg)
* @brief
* The main WLAN task - started by wfx_wifi_start() that interfaces with RSI.
* The main WLAN task - started by StartWifiTask() that interfaces with RSI.
* The rest of RSI stuff come in call-backs.
* The initialization has been already done.
* @param[in] arg:
Expand Down
36 changes: 31 additions & 5 deletions src/platform/silabs/wifi/WifiInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ enum class WifiState : uint16_t
kStationMode = (1 << 7), /* Enable Station Mode */
kAPMode = (1 << 8), /* Enable AP Mode */
kStationReady = (kStationConnected | kStationDhcpDone),
kStationStarted = (1 << 9), /* RSI task started */
kStationStarted = (1 << 9),
kScanStarted = (1 << 10), /* Scan Started */
};

Expand Down Expand Up @@ -249,20 +249,46 @@ CHIP_ERROR GetMacAddress(sl_wfx_interface_t interface, chip::MutableByteSpan & a
*/
CHIP_ERROR StartNetworkScan(chip::ByteSpan ssid, ScanCallback callback);

/**
* @brief Creates and starts the WiFi task that processes Wifi events and operations
*
* @return CHIP_ERROR CHIP_NO_ERROR if the task was successfully started and initialized
* CHIP_ERROR_NO_MEMORY if the task failed to be created
* CHIP_ERROR_INTERNAL if software or hardware initialization failed
*/
CHIP_ERROR StartWifiTask();

/**
* @brief Configures the Wi-Fi devices as a Wi-Fi station
*/
void ConfigureStationMode();

/**
* @brief Returns the state of the Wi-Fi connection
*
* @return true, if the Wi-Fi device is connected to an AP
* false, otherwise
*/
bool IsStationConnected();

/**
* @brief Returns the state of the Wi-Fi Station configuration of the Wi-Fi device
*
* @return true, if the Wi-Fi Station mode is enabled
* false, otherwise
*/
bool IsStationModeEnabled(void);

/* Function to update */

sl_status_t wfx_wifi_start(void);
void wfx_enable_sta_mode(void);
void wfx_set_wifi_provision(wfx_wifi_provision_t * wifiConfig);
bool wfx_get_wifi_provision(wfx_wifi_provision_t * wifiConfig);
bool wfx_is_sta_mode_enabled(void);
int32_t wfx_get_ap_info(wfx_wifi_scan_result_t * ap);
int32_t wfx_get_ap_ext(wfx_wifi_scan_ext_t * extra_info);
int32_t wfx_reset_counts();
void wfx_clear_wifi_provision(void);
sl_status_t wfx_connect_to_ap(void);
void wfx_setup_ip6_link_local(sl_wfx_interface_t);
bool wfx_is_sta_connected(void);
sl_status_t sl_matter_wifi_disconnect(void);

#if CHIP_DEVICE_CONFIG_ENABLE_IPV4
Expand Down
2 changes: 1 addition & 1 deletion src/platform/silabs/wifi/rs911x/WifiInterfaceImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ void ProcessEvent(WifiPlatformEvent event)
/*********************************************************************************
* @fn void sl_matter_wifi_task(void *arg)
* @brief
* The main WLAN task - started by wfx_wifi_start () that interfaces with RSI.
* The main WLAN task - started by StartWifiTask () that interfaces with RSI.
* The rest of RSI stuff come in call-backs.
* The initialization has been already done.
* @param[in] arg:
Expand Down
Loading

0 comments on commit 26a867c

Please sign in to comment.