diff --git a/firmware/controllers/system/timer/scheduler.h b/firmware/controllers/system/timer/scheduler.h index 8357d16afe..965aaf5b7b 100644 --- a/firmware/controllers/system/timer/scheduler.h +++ b/firmware/controllers/system/timer/scheduler.h @@ -67,4 +67,5 @@ struct ExecutorInterface { virtual void scheduleByTimestamp(const char *msg, scheduling_s *scheduling, efitimeus_t timeUs, action_s action) = 0; virtual void scheduleByTimestampNt(const char *msg, scheduling_s *scheduling, efitime_t timeUs, action_s action) = 0; virtual void scheduleForLater(scheduling_s *scheduling, int delayUs, action_s action) = 0; + virtual void cancel(scheduling_s* scheduling) = 0; }; diff --git a/firmware/controllers/system/timer/signal_executor_sleep.cpp b/firmware/controllers/system/timer/signal_executor_sleep.cpp index c77e263644..522f442a60 100644 --- a/firmware/controllers/system/timer/signal_executor_sleep.cpp +++ b/firmware/controllers/system/timer/signal_executor_sleep.cpp @@ -91,4 +91,12 @@ void SleepExecutor::scheduleForLater(scheduling_s *scheduling, int delayUs, acti doScheduleForLater(scheduling, delayUs, action); } +void SleepExecutor::cancel(scheduling_s* s) { + if (chVTIsArmedI(&s->timer)) { + chVTResetI(&s->timer); + } + + s->action = {}; +} + #endif /* EFI_SIGNAL_EXECUTOR_SLEEP */ diff --git a/firmware/controllers/system/timer/signal_executor_sleep.h b/firmware/controllers/system/timer/signal_executor_sleep.h index 5e3d478fb0..952781a023 100644 --- a/firmware/controllers/system/timer/signal_executor_sleep.h +++ b/firmware/controllers/system/timer/signal_executor_sleep.h @@ -14,4 +14,5 @@ public: void scheduleByTimestamp(const char *msg, scheduling_s *scheduling, efitimeus_t timeUs, action_s action) override; void scheduleByTimestampNt(const char *msg, scheduling_s *scheduling, efitick_t timeNt, action_s action) override; void scheduleForLater(scheduling_s *scheduling, int delayUs, action_s action) override; + void cancel(scheduling_s* s) override; }; diff --git a/firmware/controllers/system/timer/single_timer_executor.cpp b/firmware/controllers/system/timer/single_timer_executor.cpp index 6e5298529e..303b333fa5 100644 --- a/firmware/controllers/system/timer/single_timer_executor.cpp +++ b/firmware/controllers/system/timer/single_timer_executor.cpp @@ -90,6 +90,13 @@ void SingleTimerExecutor::scheduleByTimestampNt(const char *msg, scheduling_s* s } } +void SingleTimerExecutor::cancel(scheduling_s* scheduling) { + // Lock for queue removal - we may already be locked, but that's ok + chibios_rt::CriticalSectionLocker csl; + + queue.remove(scheduling); +} + void SingleTimerExecutor::onTimerCallback() { timerCallbackCounter++; diff --git a/firmware/controllers/system/timer/single_timer_executor.h b/firmware/controllers/system/timer/single_timer_executor.h index edab40df97..8465d5d769 100644 --- a/firmware/controllers/system/timer/single_timer_executor.h +++ b/firmware/controllers/system/timer/single_timer_executor.h @@ -16,6 +16,8 @@ public: void scheduleByTimestamp(const char *msg, scheduling_s *scheduling, efitimeus_t timeUs, action_s action) override; void scheduleByTimestampNt(const char *msg, scheduling_s *scheduling, efitime_t timeNt, action_s action) override; void scheduleForLater(scheduling_s *scheduling, int delayUs, action_s action) override; + void cancel(scheduling_s* scheduling) override; + void onTimerCallback(); int timerCallbackCounter = 0; int scheduleCounter = 0; diff --git a/unit_tests/global_execution_queue.cpp b/unit_tests/global_execution_queue.cpp index 7252d59e3e..0902d3a785 100644 --- a/unit_tests/global_execution_queue.cpp +++ b/unit_tests/global_execution_queue.cpp @@ -70,6 +70,15 @@ void TestExecutor::scheduleByTimestampNt(const char *msg, scheduling_s* scheduli scheduleByTimestamp("test", scheduling, NT2US(timeNt), action); } +void TestExecutor::cancel(scheduling_s* s) { + if (m_mockExecutor) { + m_mockExecutor->cancel(s); + return; + } + + schedulingQueue.remove(s); +} + void TestExecutor::setMockExecutor(ExecutorInterface* exec) { m_mockExecutor = exec; } diff --git a/unit_tests/global_execution_queue.h b/unit_tests/global_execution_queue.h index 72e3de0873..90787ef96e 100644 --- a/unit_tests/global_execution_queue.h +++ b/unit_tests/global_execution_queue.h @@ -17,6 +17,8 @@ public: void scheduleByTimestamp(const char *msg, scheduling_s *scheduling, efitimeus_t timeUs, action_s action) override; void scheduleByTimestampNt(const char *msg, scheduling_s *scheduling, efitick_t timeNt, action_s action) override; void scheduleForLater(scheduling_s *scheduling, int delayUs, action_s action) override; + void cancel(scheduling_s* scheduling) override; + void clear(); int executeAll(efitime_t now); int size(); diff --git a/unit_tests/mocks.h b/unit_tests/mocks.h index c323411c27..39bb21596e 100644 --- a/unit_tests/mocks.h +++ b/unit_tests/mocks.h @@ -58,6 +58,7 @@ public: MOCK_METHOD(void, scheduleByTimestamp, (const char *msg, scheduling_s *scheduling, efitimeus_t timeUs, action_s action), (override)); MOCK_METHOD(void, scheduleByTimestampNt, (const char *msg, scheduling_s *scheduling, efitime_t timeUs, action_s action), (override)); MOCK_METHOD(void, scheduleForLater, (scheduling_s *scheduling, int delayUs, action_s action), (override)); + MOCK_METHOD(void, cancel, (scheduling_s*), (override)); }; class MockAirmass : public AirmassVeModelBase {