Skip to content

Commit d5a6b34

Browse files
Updated logic to schedulelambda from apptask and remove unnessary chiplocks
1 parent 02e8979 commit d5a6b34

File tree

3 files changed

+67
-74
lines changed

3 files changed

+67
-74
lines changed

examples/closure-app/silabs/include/ClosureManager.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,6 @@ class ClosureManager
156156
*/
157157
const Action_t & GetCurrentAction() const { return mCurrentAction; }
158158

159-
/**
160-
* @brief Retrieves the complete current state of the closure control endpoint.
161-
*
162-
* @param currentState Reference to store the retrieved current state.
163-
* @return CHIP_ERROR Returns CHIP_NO_ERROR on success, or an error code if retrieval fails.
164-
*/
165-
CHIP_ERROR GetClosureControlCurrentState(
166-
chip::app::DataModel::Nullable<chip::app::Clusters::ClosureControl::GenericOverallCurrentState> & currentState);
167159

168160
/**
169161
* @brief Checks if a MoveTo action is currently in progress.
@@ -172,6 +164,16 @@ class ClosureManager
172164
*/
173165
bool IsClosureControlMotionInProgress() const;
174166

167+
/**
168+
* @brief Gets the ClosureControlEndpoint logic for direct access.
169+
*
170+
* This allows direct access to the cluster logic methods (e.g., HandleMoveTo)
171+
* when called from chip task context (e.g., via ScheduleWork).
172+
*
173+
* @return Reference to the ClosureControlEndpoint logic.
174+
*/
175+
chip::app::Clusters::ClosureControl::ClusterLogic & GetClosureControlLogic() { return mClosureEndpoint1.GetLogic(); }
176+
175177
/**
176178
* @brief Sets the initial state for the ClosureControlEndpoint.
177179
*

examples/closure-app/silabs/src/AppTask.cpp

Lines changed: 57 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -154,66 +154,75 @@ void AppTask::ButtonEventHandler(uint8_t button, uint8_t btnAction)
154154

155155
void AppTask::ClosureButtonActionEventHandler(AppEvent * aEvent)
156156
{
157-
CHIP_ERROR err = CHIP_NO_ERROR;
158-
159157
if (aEvent->Type == AppEvent::kEventType_Button)
160158
{
161-
// Check if an action is already in progress
162-
if (ClosureManager::GetInstance().IsClosureControlMotionInProgress())
163-
{
164-
// Stop the current action
165-
auto status = ClosureManager::GetInstance().OnStopCommand();
166-
if (status != Protocols::InteractionModel::Status::Success)
159+
// Schedule work on the chip stack thread to ensure all CHIP API calls are safe
160+
chip::DeviceLayer::PlatformMgr().ScheduleWork([](intptr_t) {
161+
// Check if an action is already in progress
162+
if (ClosureManager::GetInstance().IsClosureControlMotionInProgress())
167163
{
168-
ChipLogError(AppServer, "Failed to stop closure action");
164+
// Stop the current action
165+
auto status = ClosureManager::GetInstance().GetClosureControlLogic().HandleStop();
166+
if (status != Protocols::InteractionModel::Status::Success)
167+
{
168+
ChipLogError(AppServer, "Failed to stop closure action: %u", to_underlying(status));
169+
}
169170
}
170-
}
171-
else
172-
{
173-
// Fetch the complete current state of closure with proper locking
174-
chip::DeviceLayer::PlatformMgr().LockChipStack();
175-
DataModel::Nullable<ClosureControl::GenericOverallCurrentState> currentState;
176-
CHIP_ERROR err = ClosureManager::GetInstance().GetClosureControlCurrentState(currentState);
177-
chip::DeviceLayer::PlatformMgr().UnlockChipStack();
178-
179-
if (err != CHIP_NO_ERROR || currentState.IsNull() || !currentState.Value().position.HasValue() ||
180-
currentState.Value().position.Value().IsNull())
171+
else
181172
{
182-
ChipLogError(AppServer, "Failed to get current closure state");
183-
return;
184-
}
185-
186-
// Get current position and determine target position (toggle)
187-
auto currentPosition = currentState.Value().position.Value().Value();
188-
ClosureControl::TargetPositionEnum targetPosition =
173+
DataModel::Nullable<ClosureControl::GenericOverallCurrentState> currentState;
174+
CHIP_ERROR err = ClosureManager::GetInstance().GetClosureControlLogic().GetOverallCurrentState(currentState);
175+
176+
if (err != CHIP_NO_ERROR)
177+
{
178+
ChipLogError(AppServer, "Failed to get current closure state: %s", chip::ErrorStr(err));
179+
return;
180+
}
181+
if (currentState.IsNull())
182+
{
183+
ChipLogError(AppServer, "Failed to get current closure state: currentState is null");
184+
return;
185+
}
186+
if (!currentState.Value().position.HasValue() || currentState.Value().position.Value().IsNull())
187+
{
188+
ChipLogError(AppServer, "Failed to get current closure state: position is null");
189+
return;
190+
}
191+
192+
// Get current position and determine target position (toggle)
193+
auto currentPosition = currentState.Value().position.Value().Value();
194+
ChipLogProgress(AppServer, "Current state - Position: %d", to_underlying(currentPosition));
195+
196+
ClosureControl::TargetPositionEnum targetPosition =
189197
(currentPosition == ClosureControl::CurrentPositionEnum::kFullyOpened)
190198
? ClosureControl::TargetPositionEnum::kMoveToFullyClosed
191199
: ClosureControl::TargetPositionEnum::kMoveToFullyOpen;
192-
193-
// Get latch and speed from current state to preserve them in target state
194-
Optional<bool> latch = chip::NullOptional;
195-
if (currentState.Value().latch.HasValue() && !currentState.Value().latch.Value().IsNull())
196-
{
197-
latch = MakeOptional(currentState.Value().latch.Value().Value());
198-
}
199-
200-
Optional<Globals::ThreeLevelAutoEnum> speed = NullOptional;
201-
if (currentState.Value().speed.HasValue())
202-
{
203-
speed = chip::MakeOptional(currentState.Value().speed.Value());
200+
ChipLogProgress(AppServer, "Target position: %d", to_underlying(targetPosition));
201+
202+
Optional<bool> latch = chip::NullOptional;
203+
if (currentState.Value().latch.HasValue() && !currentState.Value().latch.Value().IsNull())
204+
{
205+
latch = MakeOptional(false);
206+
}
207+
208+
Optional<Globals::ThreeLevelAutoEnum> speed = NullOptional;
209+
if (currentState.Value().speed.HasValue())
210+
{
211+
speed = chip::MakeOptional(currentState.Value().speed.Value());
212+
}
213+
214+
// Move to the target position with latch set to false and preserved speed value
215+
auto status = ClosureManager::GetInstance().GetClosureControlLogic().HandleMoveTo(
216+
MakeOptional(targetPosition), latch, speed);
217+
if (status != Protocols::InteractionModel::Status::Success)
218+
{
219+
ChipLogError(AppServer, "Failed to move closure to target position: %u", to_underlying(status));
220+
}
204221
}
205-
206-
// Move to the target position with preserved latch and speed values
207-
auto status = ClosureManager::GetInstance().OnMoveToCommand(MakeOptional(targetPosition), latch, speed);
208-
if (status != Protocols::InteractionModel::Status::Success)
209-
{
210-
ChipLogError(AppServer, "Failed to move closure to target position");
211-
}
212-
}
222+
}, 0);
213223
}
214224
else
215225
{
216-
err = APP_ERROR_UNHANDLED_EVENT;
217226
ChipLogError(AppServer, "Unhandled event type in ClosureButtonActionEventHandler");
218227
}
219228
}

examples/closure-app/silabs/src/ClosureManager.cpp

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -632,26 +632,20 @@ chip::Protocols::InteractionModel::Status ClosureManager::OnMoveToCommand(const
632632
}
633633
}
634634

635-
// Lock the chip stack before calling SetTargetState to prevent assertion failures
636-
DeviceLayer::PlatformMgr().LockChipStack();
637-
638635
if (mClosurePanelEndpoint2.GetLogic().SetTargetState(DataModel::MakeNullable(mClosurePanelEndpoint2Target)) != CHIP_NO_ERROR)
639636
{
640-
DeviceLayer::PlatformMgr().UnlockChipStack();
641637
ChipLogError(AppServer, "Failed to set target for Endpoint 2");
642638
return Status::Failure;
643639
}
644640

645641
if (mClosurePanelEndpoint3.GetLogic().SetTargetState(DataModel::MakeNullable(mClosurePanelEndpoint3Target)) != CHIP_NO_ERROR)
646642
{
647-
DeviceLayer::PlatformMgr().UnlockChipStack();
648643
ChipLogError(AppServer, "Failed to set target for Endpoint 3");
649644
return Status::Failure;
650645
}
651646

652647
if (mClosureEndpoint1.GetLogic().SetCountdownTimeFromDelegate(kDefaultCountdownTimeSeconds) != CHIP_NO_ERROR)
653648
{
654-
DeviceLayer::PlatformMgr().UnlockChipStack();
655649
ChipLogError(AppServer, "Failed to set countdown time for move to command on Endpoint 1");
656650
return Status::Failure;
657651
}
@@ -668,7 +662,6 @@ chip::Protocols::InteractionModel::Status ClosureManager::OnMoveToCommand(const
668662
SetCurrentAction(MOVE_TO_ACTION);
669663
}
670664
mIsMoveToInProgress = true;
671-
DeviceLayer::PlatformMgr().UnlockChipStack();
672665

673666
// Post an event to initiate the move to action asynchronously.
674667
// MoveTo Command can only be initiated from Closure Control Endpoint (Endpoint 1).
@@ -827,18 +820,13 @@ chip::Protocols::InteractionModel::Status ClosureManager::OnSetTargetCommand(con
827820
const Optional<Globals::ThreeLevelAutoEnum> & speed,
828821
const chip::EndpointId endpointId)
829822
{
830-
// Lock chip stack to safely access cluster state
831-
DeviceLayer::PlatformMgr().LockChipStack();
832-
833823
MainStateEnum mClosureEndpoint1MainState;
834824
if (mClosureEndpoint1.GetLogic().GetMainState(mClosureEndpoint1MainState) != CHIP_NO_ERROR)
835825
{
836-
DeviceLayer::PlatformMgr().UnlockChipStack();
837826
ChipLogError(AppServer, "Failed to get main state for SetTarget command on Endpoint 1");
838827
return Status::Failure;
839828
}
840829

841-
DeviceLayer::PlatformMgr().UnlockChipStack();
842830

843831
// If this command is received while the MainState attribute is currently either in Disengaged, Protected, Calibrating,
844832
// SetupRequired or Error, then a status code of INVALID_IN_STATE shall be returned.
@@ -1315,12 +1303,6 @@ bool ClosureManager::GetPanelNextPosition(const GenericDimensionStateStruct & cu
13151303
}
13161304
return true;
13171305
}
1318-
1319-
CHIP_ERROR ClosureManager::GetClosureControlCurrentState(DataModel::Nullable<GenericOverallCurrentState> & currentState)
1320-
{
1321-
return mClosureEndpoint1.GetLogic().GetOverallCurrentState(currentState);
1322-
}
1323-
13241306
bool ClosureManager::IsClosureControlMotionInProgress() const
13251307
{
13261308
return mIsMoveToInProgress;

0 commit comments

Comments
 (0)