From 10781157860bd01dbe396f3d625954d10981036e Mon Sep 17 00:00:00 2001 From: lucicop Date: Thu, 18 May 2023 16:28:43 +0300 Subject: [PATCH] Use ScheduleLambda in several places in the app to schedule work on Matter stack (#26177) --- examples/lighting-app/qpg/src/AppTask.cpp | 81 +++++++++++++---------- examples/lock-app/qpg/src/AppTask.cpp | 58 +++++++++------- 2 files changed, 80 insertions(+), 59 deletions(-) diff --git a/examples/lighting-app/qpg/src/AppTask.cpp b/examples/lighting-app/qpg/src/AppTask.cpp index ba75fa7fbf4b0e..a1b8db694c1b81 100644 --- a/examples/lighting-app/qpg/src/AppTask.cpp +++ b/examples/lighting-app/qpg/src/AppTask.cpp @@ -120,16 +120,21 @@ void OnTriggerIdentifyEffect(Identify * identify) case EMBER_ZCL_IDENTIFY_EFFECT_IDENTIFIER_BLINK: case EMBER_ZCL_IDENTIFY_EFFECT_IDENTIFIER_BREATHE: case EMBER_ZCL_IDENTIFY_EFFECT_IDENTIFIER_OKAY: - (void) chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Seconds16(5), OnTriggerIdentifyEffectCompleted, - identify); + SystemLayer().ScheduleLambda([identify] { + (void) chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Seconds16(5), OnTriggerIdentifyEffectCompleted, + identify); + }); break; case EMBER_ZCL_IDENTIFY_EFFECT_IDENTIFIER_FINISH_EFFECT: - (void) chip::DeviceLayer::SystemLayer().CancelTimer(OnTriggerIdentifyEffectCompleted, identify); - (void) chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Seconds16(1), OnTriggerIdentifyEffectCompleted, - identify); + SystemLayer().ScheduleLambda([identify] { + (void) chip::DeviceLayer::SystemLayer().CancelTimer(OnTriggerIdentifyEffectCompleted, identify); + (void) chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Seconds16(1), OnTriggerIdentifyEffectCompleted, + identify); + }); break; case EMBER_ZCL_IDENTIFY_EFFECT_IDENTIFIER_STOP_EFFECT: - (void) chip::DeviceLayer::SystemLayer().CancelTimer(OnTriggerIdentifyEffectCompleted, identify); + SystemLayer().ScheduleLambda( + [identify] { (void) chip::DeviceLayer::SystemLayer().CancelTimer(OnTriggerIdentifyEffectCompleted, identify); }); sIdentifyEffect = EMBER_ZCL_IDENTIFY_EFFECT_IDENTIFIER_STOP_EFFECT; break; default: @@ -438,7 +443,7 @@ void AppTask::FunctionTimerEventHandler(AppEvent * aEvent) { // Actually trigger Factory Reset sAppTask.mFunction = kFunction_NoneSelected; - chip::Server::GetInstance().ScheduleFactoryReset(); + SystemLayer().ScheduleLambda([] { chip::Server::GetInstance().ScheduleFactoryReset(); }); } } @@ -515,24 +520,28 @@ void AppTask::FunctionHandler(AppEvent * aEvent) void AppTask::CancelTimer() { - chip::DeviceLayer::SystemLayer().CancelTimer(TimerEventHandler, this); - mFunctionTimerActive = false; + SystemLayer().ScheduleLambda([this] { + chip::DeviceLayer::SystemLayer().CancelTimer(TimerEventHandler, this); + this->mFunctionTimerActive = false; + }); } void AppTask::StartTimer(uint32_t aTimeoutInMs) { - CHIP_ERROR err; - - chip::DeviceLayer::SystemLayer().CancelTimer(TimerEventHandler, this); - err = chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Milliseconds32(aTimeoutInMs), TimerEventHandler, this); - SuccessOrExit(err); - - mFunctionTimerActive = true; -exit: - if (err != CHIP_NO_ERROR) - { - ChipLogError(NotSpecified, "StartTimer failed %s: ", chip::ErrorStr(err)); - } + SystemLayer().ScheduleLambda([aTimeoutInMs, this] { + CHIP_ERROR err; + chip::DeviceLayer::SystemLayer().CancelTimer(TimerEventHandler, this); + err = + chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Milliseconds32(aTimeoutInMs), TimerEventHandler, this); + SuccessOrExit(err); + + this->mFunctionTimerActive = true; + exit: + if (err != CHIP_NO_ERROR) + { + ChipLogError(NotSpecified, "StartTimer failed %s: ", chip::ErrorStr(err)); + } + }); } void AppTask::ActionInitiated(LightingManager::Action_t aAction) @@ -599,22 +608,24 @@ void AppTask::DispatchEvent(AppEvent * aEvent) */ void AppTask::UpdateClusterState(void) { - ChipLogProgress(NotSpecified, "UpdateClusterState"); + SystemLayer().ScheduleLambda([] { + ChipLogProgress(NotSpecified, "UpdateClusterState"); - // Write the new on/off value - EmberAfStatus status = Clusters::OnOff::Attributes::OnOff::Set(QPG_LIGHT_ENDPOINT_ID, LightingMgr().IsTurnedOn()); + // Write the new on/off value + EmberAfStatus status = Clusters::OnOff::Attributes::OnOff::Set(QPG_LIGHT_ENDPOINT_ID, LightingMgr().IsTurnedOn()); - if (status != EMBER_ZCL_STATUS_SUCCESS) - { - ChipLogError(NotSpecified, "ERR: updating on/off %x", status); - } + if (status != EMBER_ZCL_STATUS_SUCCESS) + { + ChipLogError(NotSpecified, "ERR: updating on/off %x", status); + } - // Write new level value - status = Clusters::LevelControl::Attributes::CurrentLevel::Set(QPG_LIGHT_ENDPOINT_ID, LightingMgr().GetLevel()); - if (status != EMBER_ZCL_STATUS_SUCCESS) - { - ChipLogError(NotSpecified, "ERR: updating level %x", status); - } + // Write new level value + status = Clusters::LevelControl::Attributes::CurrentLevel::Set(QPG_LIGHT_ENDPOINT_ID, LightingMgr().GetLevel()); + if (status != EMBER_ZCL_STATUS_SUCCESS) + { + ChipLogError(NotSpecified, "ERR: updating level %x", status); + } + }); } void AppTask::UpdateLEDs(void) @@ -697,7 +708,7 @@ static void NextCountdown(void) } else { - ConfigurationMgr().InitiateFactoryReset(); + SystemLayer().ScheduleLambda([] { ConfigurationMgr().InitiateFactoryReset(); }); } } diff --git a/examples/lock-app/qpg/src/AppTask.cpp b/examples/lock-app/qpg/src/AppTask.cpp index dc00f672a41758..7ffd23a34d83c7 100644 --- a/examples/lock-app/qpg/src/AppTask.cpp +++ b/examples/lock-app/qpg/src/AppTask.cpp @@ -427,10 +427,14 @@ void AppTask::FunctionHandler(AppEvent * aEvent) if (!ConnectivityMgr().IsThreadProvisioned()) { // Enable BLE advertisements and pairing window - if (chip::Server::GetInstance().GetCommissioningWindowManager().OpenBasicCommissioningWindow() == CHIP_NO_ERROR) - { - ChipLogProgress(NotSpecified, "BLE advertising started. Waiting for Pairing."); - } + SystemLayer().ScheduleLambda([] { + CHIP_ERROR err; + if ((err = chip::Server::GetInstance().GetCommissioningWindowManager().OpenBasicCommissioningWindow()) == + CHIP_NO_ERROR) + { + ChipLogProgress(NotSpecified, "BLE advertising started. Waiting for Pairing."); + } + }); } else { @@ -466,24 +470,28 @@ void AppTask::FunctionHandler(AppEvent * aEvent) void AppTask::CancelTimer() { - chip::DeviceLayer::SystemLayer().CancelTimer(TimerEventHandler, this); - mFunctionTimerActive = false; + SystemLayer().ScheduleLambda([this] { + chip::DeviceLayer::SystemLayer().CancelTimer(TimerEventHandler, this); + this->mFunctionTimerActive = false; + }); } void AppTask::StartTimer(uint32_t aTimeoutInMs) { - CHIP_ERROR err; - - chip::DeviceLayer::SystemLayer().CancelTimer(TimerEventHandler, this); - err = chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Milliseconds32(aTimeoutInMs), TimerEventHandler, this); - SuccessOrExit(err); - - mFunctionTimerActive = true; -exit: - if (err != CHIP_NO_ERROR) - { - ChipLogError(NotSpecified, "StartTimer failed %s: ", chip::ErrorStr(err)); - } + SystemLayer().ScheduleLambda([aTimeoutInMs, this] { + CHIP_ERROR err; + chip::DeviceLayer::SystemLayer().CancelTimer(TimerEventHandler, this); + err = + chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Milliseconds32(aTimeoutInMs), TimerEventHandler, this); + SuccessOrExit(err); + + this->mFunctionTimerActive = true; + exit: + if (err != CHIP_NO_ERROR) + { + ChipLogError(NotSpecified, "StartTimer failed %s: ", chip::ErrorStr(err)); + } + }); } void AppTask::ActionInitiated(BoltLockManager::Action_t aAction, int32_t aActor) @@ -577,14 +585,16 @@ void AppTask::UpdateClusterState(void) using namespace chip::app::Clusters; auto newValue = BoltLockMgr().IsUnlocked() ? DoorLock::DlLockState::kUnlocked : DoorLock::DlLockState::kLocked; - ChipLogProgress(NotSpecified, "UpdateClusterState"); + SystemLayer().ScheduleLambda([newValue] { + ChipLogProgress(NotSpecified, "UpdateClusterState"); - EmberAfStatus status = DoorLock::Attributes::LockState::Set(QPG_LOCK_ENDPOINT_ID, newValue); + EmberAfStatus status = DoorLock::Attributes::LockState::Set(QPG_LOCK_ENDPOINT_ID, newValue); - if (status != EMBER_ZCL_STATUS_SUCCESS) - { - ChipLogError(NotSpecified, "ERR: updating DoorLock %x", status); - } + if (status != EMBER_ZCL_STATUS_SUCCESS) + { + ChipLogError(NotSpecified, "ERR: updating DoorLock %x", status); + } + }); } void AppTask::UpdateLEDs(void)