Files
bolar_2/main/schedule/alarm_store.cc
2026-11-27 16:38:11 +08:00

158 lines
4.5 KiB
C++

#include "schedule/alarm_store.h"
#include "core/psram_object.h"
#include "schedule/alarm_schedule.h"
#include <cstddef>
#include <cstdint>
#include <esp_err.h>
#include <esp_log.h>
#include <nvs.h>
namespace borla {
namespace {
constexpr const char* kTag = "AlarmStore2";
constexpr const char* kNamespace = "alarm_v2";
constexpr const char* kSnapshotKey = "snapshot";
constexpr uint32_t kMagic = 0x324D5241;
constexpr uint16_t kVersion = 1;
struct PersistedAlarmSnapshot {
uint32_t magic = kMagic;
uint16_t version = kVersion;
uint16_t count = 0;
uint8_t first_day_consumed = 0;
int8_t set_tone_index = -1;
int8_t wake_tone_index = -1;
uint8_t reserved = 0;
AlarmEntry entries[kMaximumAlarmCount]{};
uint32_t checksum = 0;
};
uint32_t Checksum(
const void* data,
size_t size)
{
const auto* bytes = static_cast<const uint8_t*>(data);
uint32_t value = 2166136261U;
for (size_t i = 0; i < size; ++i) {
value ^= bytes[i];
value *= 16777619U;
}
return value;
}
bool ToneIndexValid(int8_t index)
{
return index >= -1 && index <= 15;
}
} // namespace
bool NvsAlarmStore::Load(AlarmStoreSnapshot* snapshot) const
{
if (snapshot == nullptr) {
return false;
}
*snapshot = {};
snapshot->set_tone_index = -1;
snapshot->wake_tone_index = -1;
nvs_handle_t handle = 0;
esp_err_t result =
nvs_open(kNamespace, NVS_READONLY, &handle);
if (result == ESP_ERR_NVS_NOT_FOUND) {
return true;
}
if (result != ESP_OK) {
ESP_LOGE(kTag, "open failed: %s", esp_err_to_name(result));
return false;
}
auto stored = MakePsramObject<PersistedAlarmSnapshot>();
if (!stored) {
ESP_LOGE(kTag, "PSRAM work-buffer allocation failed");
nvs_close(handle);
return false;
}
size_t size = sizeof(*stored);
result = nvs_get_blob(
handle, kSnapshotKey, stored.get(), &size);
nvs_close(handle);
if (result == ESP_ERR_NVS_NOT_FOUND) {
return true;
}
const uint32_t expected = Checksum(
stored.get(), offsetof(PersistedAlarmSnapshot, checksum));
if (result != ESP_OK || size != sizeof(*stored) ||
stored->magic != kMagic ||
stored->version != kVersion ||
stored->count > kMaximumAlarmCount ||
stored->checksum != expected ||
!ToneIndexValid(stored->set_tone_index) ||
!ToneIndexValid(stored->wake_tone_index)) {
ESP_LOGE(kTag, "invalid persisted alarm snapshot");
return false;
}
snapshot->alarms.count = stored->count;
for (size_t i = 0; i < stored->count; ++i) {
if (!AlarmSchedule::Validate(stored->entries[i])) {
ESP_LOGE(kTag, "invalid persisted alarm index=%u",
static_cast<unsigned int>(i));
return false;
}
snapshot->alarms.entries[i] = stored->entries[i];
}
snapshot->first_day_consumed =
stored->first_day_consumed != 0;
snapshot->set_tone_index = stored->set_tone_index;
snapshot->wake_tone_index = stored->wake_tone_index;
return true;
}
bool NvsAlarmStore::Save(const AlarmStoreSnapshot& snapshot)
{
if (snapshot.alarms.count > kMaximumAlarmCount ||
!ToneIndexValid(snapshot.set_tone_index) ||
!ToneIndexValid(snapshot.wake_tone_index)) {
return false;
}
auto stored = MakePsramObject<PersistedAlarmSnapshot>();
if (!stored) {
ESP_LOGE(kTag, "PSRAM work-buffer allocation failed");
return false;
}
stored->count = static_cast<uint16_t>(snapshot.alarms.count);
stored->first_day_consumed =
snapshot.first_day_consumed ? 1 : 0;
stored->set_tone_index = snapshot.set_tone_index;
stored->wake_tone_index = snapshot.wake_tone_index;
for (size_t i = 0; i < snapshot.alarms.count; ++i) {
stored->entries[i] = snapshot.alarms.entries[i];
}
stored->checksum = Checksum(
stored.get(), offsetof(PersistedAlarmSnapshot, checksum));
nvs_handle_t handle = 0;
esp_err_t result =
nvs_open(kNamespace, NVS_READWRITE, &handle);
if (result == ESP_OK) {
result = nvs_set_blob(
handle, kSnapshotKey, stored.get(), sizeof(*stored));
}
if (result == ESP_OK) {
result = nvs_commit(handle);
}
if (handle != 0) {
nvs_close(handle);
}
if (result != ESP_OK) {
ESP_LOGE(kTag, "save failed: %s", esp_err_to_name(result));
return false;
}
return true;
}
} // namespace borla