Skip to content

Commit

Permalink
[SL-ONLY] Add ApplicationSleepManager structure for the selective lis…
Browse files Browse the repository at this point in the history
…tenning implementation (project-chip#131)

Co-authored-by: Ricardo Casallas <77841255+rcasallas-silabs@users.noreply.github.com>
  • Loading branch information
mkardous-silabs and rcasallas-silabs authored Nov 27, 2024
1 parent 575d44c commit c16843b
Show file tree
Hide file tree
Showing 9 changed files with 298 additions and 2 deletions.
19 changes: 19 additions & 0 deletions examples/platform/silabs/MatterConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
#endif // CHIP_CONFIG_ENABLE_ICD_SERVER
#endif /* SL_WIFI */

#if SL_MATTER_ENABLE_APP_SLEEP_MANAGER
#include "ApplicationSleepManager.h"
#endif // SL_MATTER_ENABLE_APP_SLEEP_MANAGER

#if PW_RPC_ENABLED
#include "Rpc.h"
#endif
Expand Down Expand Up @@ -333,6 +337,21 @@ CHIP_ERROR SilabsMatterConfig::InitMatter(const char * appName)
// Init Matter Server and Start Event Loop
err = chip::Server::GetInstance().Init(initParams);

// [sl-only]: Configure Wi-Fi App Sleep Manager
#if SL_MATTER_ENABLE_APP_SLEEP_MANAGER
err = app::Silabs::ApplicationSleepManager::GetInstance()
.SetFabricTable(&Server::GetInstance().GetFabricTable())
.SetSubscriptionInfoProvider(app::InteractionModelEngine::GetInstance())
.SetCommissioningWindowManager(&Server::GetInstance().GetCommissioningWindowManager())
.SetWifiSleepManager(&WifiSleepManager::GetInstance())
.Init();
VerifyOrReturnError(err == CHIP_NO_ERROR, err, ChipLogError(DeviceLayer, "ApplicationSleepManager init failed"));

// Register ReadHandler::ApplicationCallback
app::InteractionModelEngine::GetInstance()->RegisterReadHandlerAppCallback(
&app::Silabs::ApplicationSleepManager::GetInstance());
#endif // SL_MATTER_ENABLE_APP_SLEEP_MANAGER

#if MATTER_TRACING_ENABLED
static Tracing::Silabs::BackendImpl backend;
Tracing::Register(backend);
Expand Down
5 changes: 5 additions & 0 deletions examples/platform/silabs/SiWx917/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import("//build_overrides/chip.gni")
import("//build_overrides/efr32_sdk.gni")
import("${chip_root}/examples/common/pigweed/pigweed_rpcs.gni")
import("${chip_root}/examples/platform/silabs/args.gni")
import("${chip_root}/src/app/icd/icd.gni")
import("${chip_root}/src/lib/lib.gni")
import("${chip_root}/src/platform/device.gni")
import("${chip_root}/src/platform/silabs/wifi/args.gni")
Expand Down Expand Up @@ -204,6 +205,10 @@ source_set("siwx917-common") {
]
}

if (chip_enable_icd_server) {
public_deps += [ "${silabs_common_plat_dir}/wifi/icd:app-sleep-manager" ]
}

if (app_data_model != "") {
public_deps += [ app_data_model ]
}
Expand Down
82 changes: 82 additions & 0 deletions examples/platform/silabs/wifi/icd/ApplicationSleepManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*******************************************************************************
* @file ApplicationSleepManager.cpp
* @brief Implementation for the buisness logic around Optimizing Wi-Fi sleep states
*******************************************************************************
* # License
* <b>Copyright 2024 Silicon Laboratories Inc. www.silabs.com</b>
*******************************************************************************
*
* The licensor of this software is Silicon Laboratories Inc. Your use of this
* software is governed by the terms of Silicon Labs Master Software License
* Agreement (MSLA) available at
* www.silabs.com/about-us/legal/master-software-license-agreement. This
* software is distributed to you in Source Code format and is governed by the
* sections of the MSLA applicable to Source Code.
*
******************************************************************************/

#include "ApplicationSleepManager.h"
#include <lib/support/logging/CHIPLogging.h>

namespace chip {
namespace app {
namespace Silabs {

ApplicationSleepManager ApplicationSleepManager::mInstance;

CHIP_ERROR ApplicationSleepManager::Init()
{
VerifyOrReturnError(mFabricTable != nullptr, CHIP_ERROR_INVALID_ARGUMENT, ChipLogError(AppServer, "FabricTable is null"));
VerifyOrReturnError(mSubscriptionsInfoProvider != nullptr, CHIP_ERROR_INVALID_ARGUMENT,
ChipLogError(AppServer, "SubscriptionsInfoProvider is null"));
VerifyOrReturnError(mCommissioningWindowManager != nullptr, CHIP_ERROR_INVALID_ARGUMENT,
ChipLogError(AppServer, "CommissioningWindowManager is null"));
VerifyOrReturnError(mWifiSleepManager != nullptr, CHIP_ERROR_INVALID_ARGUMENT,
ChipLogError(AppServer, "WifiSleepManager is null"));

ReturnErrorOnFailure(mFabricTable->AddFabricDelegate(this));

// Register WifiSleepManager::ApplicationCallback
mWifiSleepManager->SetApplicationCallback(this);

return CHIP_NO_ERROR;
}

void ApplicationSleepManager::OnSubscriptionEstablished(chip::app::ReadHandler & aReadHandler)
{
mWifiSleepManager->VerifyAndTransitionToLowPowerMode();
}

void ApplicationSleepManager::OnSubscriptionTerminated(chip::app::ReadHandler & aReadHandler)
{
mWifiSleepManager->VerifyAndTransitionToLowPowerMode();
}

CHIP_ERROR ApplicationSleepManager::OnSubscriptionRequested(chip::app::ReadHandler & aReadHandler,
chip::Transport::SecureSession & aSecureSession)
{
// Nothing to execute for the ApplicationSleepManager
return CHIP_NO_ERROR;
}

void ApplicationSleepManager::OnFabricRemoved(const chip::FabricTable & fabricTable, chip::FabricIndex fabricIndex)
{
mWifiSleepManager->VerifyAndTransitionToLowPowerMode();
}

void ApplicationSleepManager::OnFabricCommitted(const chip::FabricTable & fabricTable, chip::FabricIndex fabricIndex)
{
mWifiSleepManager->VerifyAndTransitionToLowPowerMode();
}

bool ApplicationSleepManager::CanGoToLIBasedSleep()
{
// TODO: Implement your logic here

ChipLogProgress(AppServer, "CanGoToLIBasedSleep was called!");
return false;
}

} // namespace Silabs
} // namespace app
} // namespace chip
138 changes: 138 additions & 0 deletions examples/platform/silabs/wifi/icd/ApplicationSleepManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*******************************************************************************
* @file ApplicationSleepManager.h
* @brief Header for the buisness logic around Optimizing Wi-Fi sleep states
*******************************************************************************
* # License
* <b>Copyright 2024 Silicon Laboratories Inc. www.silabs.com</b>
*******************************************************************************
*
* The licensor of this software is Silicon Laboratories Inc. Your use of this
* software is governed by the terms of Silicon Labs Master Software License
* Agreement (MSLA) available at
* www.silabs.com/about-us/legal/master-software-license-agreement. This
* software is distributed to you in Source Code format and is governed by the
* sections of the MSLA applicable to Source Code.
*
******************************************************************************/

#pragma once

#include <app/ReadHandler.h>
#include <app/SubscriptionsInfoProvider.h>
#include <app/server/CommissioningWindowManager.h>
#include <credentials/FabricTable.h>
#include <platform/silabs/wifi/icd/WifiSleepManager.h>

namespace chip {
namespace app {
namespace Silabs {

class ApplicationSleepManager : public chip::app::ReadHandler::ApplicationCallback,
public chip::DeviceLayer::Silabs::WifiSleepManager::ApplicationCallback,
public chip::FabricTable::Delegate
{
public:
static ApplicationSleepManager & GetInstance() { return mInstance; }

/**
* @brief Init function validates that the necessary pointers where correctly set
* before registering the object with the FabricTable and the WifiSleepManager.
*
* Init function does not register with the InteractionModelEngine since depending on the whole interation model engine
* complexifies unit testing when we can use the SubscriptionInfoProvider which provides the necessary APIs.
*
*
* @return CHIP_ERROR CHIP_NO_ERROR if the init succeed
* CHIP_ERROR_INVALID_ARGUMENT, if the fabricTable, subscriptionsInfoProvider or commissioningWindowManager,
* wifiSleepManager were not set correctly
* other, if the FabricTable::AddFabricDelegate failed
*/
CHIP_ERROR Init();

ApplicationSleepManager & SetFabricTable(chip::FabricTable * fabricTable)
{
mFabricTable = fabricTable;
return *this;
}

ApplicationSleepManager & SetSubscriptionInfoProvider(chip::app::SubscriptionsInfoProvider * subscriptionsInfoProvider)
{
mSubscriptionsInfoProvider = subscriptionsInfoProvider;
return *this;
}

ApplicationSleepManager & SetCommissioningWindowManager(chip::CommissioningWindowManager * commissioningWindowManager)
{
mCommissioningWindowManager = commissioningWindowManager;
return *this;
}

ApplicationSleepManager & SetWifiSleepManager(chip::DeviceLayer::Silabs::WifiSleepManager * wifiSleepManager)
{
mWifiSleepManager = wifiSleepManager;
return *this;
}

// ReadHandler::ApplicationCallback implementation

/**
* @brief Calls the WifiSleepManager VerifyAndTransitionToLowPowerMode.
* The VerifyAndTransitionToLowPowerMode function is responsible of then queriyng the ApplicationSleepManager to
* determine in which low power state the Wi-Fi device can transition to.
*/
void OnSubscriptionTerminated(chip::app::ReadHandler & aReadHandler);

/**
* @brief Calls the WifiSleepManager VerifyAndTransitionToLowPowerMode.
* The VerifyAndTransitionToLowPowerMode function is responsible of then queriyng the ApplicationSleepManager to
* determine in which low power state the Wi-Fi device can transition to.
*/
void OnSubscriptionEstablished(chip::app::ReadHandler & aReadHandler);

CHIP_ERROR OnSubscriptionRequested(chip::app::ReadHandler & aReadHandler, chip::Transport::SecureSession & aSecureSession);

// WifiSleepManager::ApplicationCallback implementation

/**
* @brief TODO
*
* @return true
* @return false
*/
bool CanGoToLIBasedSleep() override;

// FabricTable::Delegate implementation

/**
* @brief Calls the WifiSleepManager VerifyAndTransitionToLowPowerMode.
* The VerifyAndTransitionToLowPowerMode function is responsible of queriyng the ApplicationSleepManager to
* determine in which low power state the Wi-Fi device can transition to.
*/
void OnFabricRemoved(const chip::FabricTable & fabricTable, chip::FabricIndex fabricIndex) override;

/**
* @brief Calls the WifiSleepManager VerifyAndTransitionToLowPowerMode.
* The VerifyAndTransitionToLowPowerMode function is responsible of queriyng the ApplicationSleepManager to
* determine in which low power state the Wi-Fi device can transition to.
*/
void OnFabricCommitted(const chip::FabricTable & fabricTable, chip::FabricIndex fabricIndex) override;

void OnFabricUpdated(const chip::FabricTable & fabricTable, chip::FabricIndex fabricIndex) override {}

private:
ApplicationSleepManager() = default;
~ApplicationSleepManager() = default;

ApplicationSleepManager(const ApplicationSleepManager &) = delete;
ApplicationSleepManager & operator=(const ApplicationSleepManager &) = delete;

static ApplicationSleepManager mInstance;
chip::FabricTable * mFabricTable = nullptr;
chip::app::SubscriptionsInfoProvider * mSubscriptionsInfoProvider = nullptr;
chip::CommissioningWindowManager * mCommissioningWindowManager = nullptr;
chip::DeviceLayer::Silabs::WifiSleepManager * mWifiSleepManager = nullptr;
};

} // namespace Silabs
} // namespace app
} // namespace chip
39 changes: 39 additions & 0 deletions examples/platform/silabs/wifi/icd/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#******************************************************************************
# @file BUILD.gn
# @brief BUILD.gn to build the Application Sleep Manager implementation
#******************************************************************************
# # License
# <b>Copyright 2024 Silicon Laboratories Inc. www.silabs.com</b>
#******************************************************************************
#
# The licensor of this software is Silicon Laboratories Inc. Your use of this
# software is governed by the terms of Silicon Labs Master Software License
# Agreement (MSLA) available at
# www.silabs.com/about-us/legal/master-software-license-agreement. This
# software is distributed to you in Source Code format and is governed by the
# sections of the MSLA applicable to Source Code.
#
#*****************************************************************************/

import("//build_overrides/chip.gni")

config("app-sleep-manager-config") {
include_dirs = [ "." ]
defines = [ "SL_MATTER_ENABLE_APP_SLEEP_MANAGER=1" ]
}

source_set("app-sleep-manager") {
sources = [
"ApplicationSleepManager.cpp",
"ApplicationSleepManager.h",
]

deps = [
"${chip_root}/src/app",
"${chip_root}/src/app/server:server",
"${chip_root}/src/lib/core",
"${chip_root}/src/platform/silabs/wifi:wifi-platform",
]

public_configs = [ ":app-sleep-manager-config" ]
}
11 changes: 10 additions & 1 deletion src/platform/silabs/wifi/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ declare_args() {

#default Wifi Password
chip_default_wifi_psk = ""

# Argument to enable LwIP debug logs
sl_enable_wifi_debug_logs = false
}

if (chip_enable_wifi && !wifi_soc) {
Expand All @@ -49,9 +52,15 @@ if (chip_enable_wifi && !wifi_soc) {
}

config("wifi-platform-config") {
defines = [ "WIFI_DEBUG_ENABLED=0" ]
defines = []
include_dirs = []

if (sl_enable_wifi_debug_logs) {
defines += [ "WIFI_DEBUG_ENABLED=1" ]
} else {
defines += [ "WIFI_DEBUG_ENABLED=0" ]
}

if (use_rs9116) {
# All the stuff from wiseconnect
include_dirs += rs911x_inc_plat
Expand Down
2 changes: 2 additions & 0 deletions src/platform/silabs/wifi/icd/WifiSleepManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ CHIP_ERROR WifiSleepManager::VerifyAndTransitionToLowPowerMode()
#elif RS911X_WIFI // rs9116
VerifyOrReturnError(ConfigurePowerSave() == SL_STATUS_OK, CHIP_ERROR_INTERNAL);
return CHIP_NO_ERROR;
#else // wf200
return CHIP_NO_ERROR;
#endif
}

Expand Down
3 changes: 2 additions & 1 deletion src/platform/silabs/wifi/wf200/WifiInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "sl_wfx_cmd_api.h"
#include "sl_wfx_constants.h"
#include "task.h"
#include <app/icd/server/ICDServerConfig.h>
#include <lib/support/CHIPMemString.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/logging/CHIPLogging.h>
Expand Down Expand Up @@ -729,7 +730,7 @@ static void wfx_events_task(void * p_arg)
retryJoin = 0;
wfx_lwip_set_sta_link_up();
#if CHIP_CONFIG_ENABLE_ICD_SERVER
if (!(wfx_get_wifi_state() & SL_WFX_AP_INTERFACE_UP))
if (!(wifiContext.state & SL_WFX_AP_INTERFACE_UP))
{
// Enable the power save
ChipLogProgress(DeviceLayer, "WF200 going to DTIM based sleep");
Expand Down
1 change: 1 addition & 0 deletions src/platform/silabs/wifi/wf200/platform/wf200_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ sl_status_t sl_wfx_host_reset_chip(void)
*****************************************************************************/
sl_status_t sl_wfx_host_wait_for_wake_up(void)
{
xSemaphoreTake(wfx_wakeup_sem, pdMS_TO_TICKS(0));
xSemaphoreTake(wfx_wakeup_sem, pdMS_TO_TICKS(3));

return SL_STATUS_OK;
Expand Down

0 comments on commit c16843b

Please sign in to comment.