#include "schedule/alarm_schedule.h" #include #include namespace borla { namespace { bool IsBuiltIn(const AlarmEntry& alarm) { return alarm.id == kBuiltInFirstDayAlarmId; } AlarmEntry BuiltInAlarm() { AlarmEntry alarm; alarm.id = kBuiltInFirstDayAlarmId; alarm.hour = 20; alarm.minute = 30; alarm.repeat_mask = 0x7F; alarm.enabled = true; std::strncpy( alarm.label.data(), "first_day_sleep_prompt", alarm.label.size() - 1); return alarm; } } // namespace bool AlarmSchedule::Validate(const AlarmEntry& alarm) { return alarm.id != 0 && alarm.hour < 24 && alarm.minute < 60 && alarm.repeat_mask <= 0x7F && alarm.label.back() == '\0' && (alarm.repeat_mask != 0 || alarm.fire_epoch >= 0); } bool AlarmSchedule::Upsert(const AlarmEntry& alarm) { if (!Validate(alarm)) { return false; } for (size_t i = 0; i < alarms_.count; ++i) { if (alarms_.entries[i].id == alarm.id) { alarms_.entries[i] = alarm; return true; } } if (alarms_.count >= alarms_.entries.size()) { return false; } alarms_.entries[alarms_.count++] = alarm; return true; } bool AlarmSchedule::Delete(int32_t id) { for (size_t i = 0; i < alarms_.count; ++i) { if (alarms_.entries[i].id != id) { continue; } std::move( alarms_.entries.begin() + i + 1, alarms_.entries.begin() + alarms_.count, alarms_.entries.begin() + i); alarms_.entries[--alarms_.count] = {}; if (id == kBuiltInFirstDayAlarmId) { first_day_consumed_ = true; } return true; } return false; } bool AlarmSchedule::Replace(const AlarmList& alarms) { if (alarms.count > alarms.entries.size()) { return false; } AlarmList replacement; for (size_t i = 0; i < alarms.count; ++i) { if (!Validate(alarms.entries[i]) || replacement.count >= replacement.entries.size()) { return false; } bool duplicate = false; for (size_t j = 0; j < replacement.count; ++j) { duplicate |= replacement.entries[j].id == alarms.entries[i].id; } if (duplicate) { return false; } replacement.entries[replacement.count++] = alarms.entries[i]; } alarms_ = replacement; return true; } bool AlarmSchedule::EnsureBuiltInFirstDay(bool consumed) { first_day_consumed_ = consumed; if (consumed) { Delete(kBuiltInFirstDayAlarmId); first_day_consumed_ = true; return true; } // The built-in first-day prompt is optional. When the user list already // fills every slot there is no room for it, but that must not fail the // whole sync or boot; first_day_consumed_ stays false so a later sync with // free capacity can still install it. Upsert(BuiltInAlarm()); return true; } bool AlarmSchedule::Matches( const AlarmEntry& alarm, const CalendarMinute& local_time, int64_t epoch_seconds) { if (!Validate(alarm) || !alarm.enabled || alarm.hour != local_time.hour || alarm.minute != local_time.minute) { return false; } if (alarm.repeat_mask != 0) { return (alarm.repeat_mask & (1U << local_time.week_day)) != 0; } if (alarm.fire_epoch == 0) { return true; } const int64_t difference = epoch_seconds - alarm.fire_epoch; return difference >= 0 && difference < 60; } AlarmFireBatch AlarmSchedule::TakeDue( const CalendarMinute& local_time, int64_t epoch_seconds) { AlarmFireBatch result; const int64_t minute_key = epoch_seconds / 60; if (minute_key == last_fired_minute_key_) { return result; } for (size_t i = 0; i < alarms_.count; ++i) { AlarmEntry& alarm = alarms_.entries[i]; if (!Matches(alarm, local_time, epoch_seconds)) { continue; } result.ids[result.count++] = alarm.id; if (IsBuiltIn(alarm)) { result.built_in_first_day = true; first_day_consumed_ = true; alarm.enabled = false; } else { result.play_audio = true; } if (alarm.repeat_mask == 0) { alarm.enabled = false; } } if (result.count > 0) { last_fired_minute_key_ = minute_key; } return result; } } // namespace borla