Skip to content

Commit

Permalink
Use ScheduleLambda in several places in the app to schedule work on M…
Browse files Browse the repository at this point in the history
…atter stack (#26177)
  • Loading branch information
lucicop authored May 18, 2023
1 parent 36a57fe commit c520917
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 59 deletions.
81 changes: 46 additions & 35 deletions examples/lighting-app/qpg/src/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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(); });
}
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -697,7 +708,7 @@ static void NextCountdown(void)
}
else
{
ConfigurationMgr().InitiateFactoryReset();
SystemLayer().ScheduleLambda([] { ConfigurationMgr().InitiateFactoryReset(); });
}
}

Expand Down
58 changes: 34 additions & 24 deletions examples/lock-app/qpg/src/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit c520917

Please sign in to comment.