#include "schedule/alarm_service.h" #include "core/psram_object.h" #include #include #include namespace borla { namespace { constexpr const char* kTag = "AlarmService2"; constexpr int64_t kMinimumValidEpoch = 1700000000; constexpr int64_t kMaximumWakeSearchSeconds = 8LL * 24LL * 60LL * 60LL; class Lock final { public: explicit Lock(SemaphoreHandle_t mutex) : mutex_(mutex) { xSemaphoreTake(mutex_, portMAX_DELAY); } ~Lock() { xSemaphoreGive(mutex_); } private: SemaphoreHandle_t mutex_; }; bool ToneValid(int8_t index) { return index >= -1 && index <= 15; } } // namespace bool AlarmService::Initialize( IEventPublisher& publisher, IScheduleEventSink& event_sink) { if (running_.load()) { return true; } publisher_ = &publisher; event_sink_ = &event_sink; mutex_ = xSemaphoreCreateMutexStatic(&mutex_control_); if (mutex_ == nullptr) { return false; } AlarmStoreSnapshot stored; if (!store_.Load(&stored) || !schedule_.Replace(stored.alarms) || !schedule_.EnsureBuiltInFirstDay( stored.first_day_consumed)) { ESP_LOGE(kTag, "persisted alarm initialization failed"); return false; } set_tone_index_.store(stored.set_tone_index); wake_tone_index_.store(stored.wake_tone_index); if (!SaveLocked()) { return false; } running_.store(true); task_ = xTaskCreateStatic( &AlarmService::TaskEntry, "alarm2", task_stack_.size(), this, 4, task_stack_.data(), &task_control_); if (task_ == nullptr) { running_.store(false); return false; } return true; } void AlarmService::Stop() { running_.store(false); } bool AlarmService::Set(const AlarmEntry& alarm) { if (mutex_ == nullptr) { return false; } Lock lock(mutex_); return schedule_.Upsert(alarm) && SaveLocked(); } void AlarmService::Delete(int32_t id) { if (mutex_ == nullptr) { return; } Lock lock(mutex_); if (schedule_.Delete(id)) { SaveLocked(); } } bool AlarmService::Sync(const AlarmList& alarms) { if (mutex_ == nullptr) { return false; } Lock lock(mutex_); const bool consumed = schedule_.first_day_consumed(); return schedule_.Replace(alarms) && schedule_.EnsureBuiltInFirstDay(consumed) && SaveLocked(); } void AlarmService::SetToneIndices( int8_t set_tone_index, int8_t wake_tone_index) { if (mutex_ == nullptr || !ToneValid(set_tone_index) || !ToneValid(wake_tone_index)) { return; } Lock lock(mutex_); set_tone_index_.store(set_tone_index); wake_tone_index_.store(wake_tone_index); SaveLocked(); } uint64_t AlarmService::NextWakeupDelayUs() const { const int64_t now = static_cast(std::time(nullptr)); if (mutex_ == nullptr || now < kMinimumValidEpoch) { return 0; } Lock lock(mutex_); const AlarmList alarms = schedule_.list(); int64_t candidate = now - now % 60 + 60; const int64_t end = now + kMaximumWakeSearchSeconds; for (; candidate <= end; candidate += 60) { CalendarMinute calendar; if (!MakeCalendar(candidate, &calendar)) { return 0; } for (size_t i = 0; i < alarms.count; ++i) { if (AlarmSchedule::Matches( alarms.entries[i], calendar, candidate)) { return static_cast( std::max(1, candidate - now)) * 1000000ULL; } } } return 0; } void AlarmService::TaskEntry(void* context) { static_cast(context)->Run(); } void AlarmService::Run() { while (running_.load()) { const int64_t now = static_cast(std::time(nullptr)); if (now >= kMinimumValidEpoch) { CalendarMinute calendar; AlarmFireBatch due; if (MakeCalendar(now, &calendar)) { Lock lock(mutex_); due = schedule_.TakeDue(calendar, now); if (due.count > 0) { SaveLocked(); } } for (size_t i = 0; i < due.count; ++i) { if (due.ids[i] > 0 && event_sink_ != nullptr) { event_sink_->SendAlarmFired(due.ids[i], now); } } if (due.play_audio && publisher_ != nullptr) { publisher_->Publish( AppEvent::Make(AppEventType::kAlarmDue)); } else if (due.built_in_first_day && publisher_ != nullptr) { publisher_->Publish(AppEvent::Make( AppEventType::kFirstDayAlarmDue)); } } vTaskDelay(pdMS_TO_TICKS(500)); } task_ = nullptr; vTaskDelete(nullptr); } bool AlarmService::SaveLocked() { auto snapshot = MakePsramObject(); if (!snapshot) { ESP_LOGE(kTag, "PSRAM work-buffer allocation failed"); return false; } snapshot->alarms = schedule_.list(); snapshot->first_day_consumed = schedule_.first_day_consumed(); snapshot->set_tone_index = set_tone_index_.load(); snapshot->wake_tone_index = wake_tone_index_.load(); return store_.Save(*snapshot); } bool AlarmService::MakeCalendar( int64_t epoch_seconds, CalendarMinute* calendar) { if (calendar == nullptr) { return false; } const time_t value = static_cast(epoch_seconds); tm local{}; if (localtime_r(&value, &local) == nullptr) { return false; } *calendar = { .year = local.tm_year, .year_day = local.tm_yday, .week_day = local.tm_wday, .hour = local.tm_hour, .minute = local.tm_min, }; return true; } } // namespace borla