#pragma once #include "core/task_stack_budget.h" #include "ports/event_publisher.h" #include "schedule/alarm_schedule.h" #include "schedule/alarm_store.h" #include "schedule/schedule_types.h" #include #include #include #include #include #include namespace borla { class AlarmService final { public: explicit AlarmService(IAlarmStore& store) : store_(store) {} bool Initialize( IEventPublisher& publisher, IScheduleEventSink& event_sink); void Stop(); bool Set(const AlarmEntry& alarm); void Delete(int32_t id); bool Sync(const AlarmList& alarms); void SetToneIndices( int8_t set_tone_index, int8_t wake_tone_index); int8_t wake_tone_index() const { return wake_tone_index_.load(); } uint64_t NextWakeupDelayUs() const; private: static void TaskEntry(void* context); void Run(); bool SaveLocked(); static bool MakeCalendar( int64_t epoch_seconds, CalendarMinute* calendar); IAlarmStore& store_; IEventPublisher* publisher_ = nullptr; IScheduleEventSink* event_sink_ = nullptr; mutable SemaphoreHandle_t mutex_ = nullptr; mutable StaticSemaphore_t mutex_control_{}; AlarmSchedule schedule_; std::atomic set_tone_index_{-1}; std::atomic wake_tone_index_{-1}; std::atomic running_{false}; TaskHandle_t task_ = nullptr; StaticTask_t task_control_{}; std::array< StackType_t, task_stack_budget::kAlarmServiceBytes> task_stack_{}; }; } // namespace borla