Files
bolar_1/main/memory_cache.cc

147 lines
4.4 KiB
C++

#include "memory_cache.h"
#include <esp_log.h>
#include <esp_partition.h>
#include <cstring>
#include <vector>
#define TAG "MemoryCache"
MemoryCache& MemoryCache::GetInstance()
{
static MemoryCache instance;
return instance;
}
bool MemoryCache::Load()
{
const esp_partition_t* partition = esp_partition_find_first(
ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_SPIFFS, kPartitionLabel);
if (!partition) {
ESP_LOGW(TAG, "Partition '%s' not found", kPartitionLabel);
return false;
}
// Read header: [4B magic] [4B data_len]
uint32_t header[2] = {0, 0};
esp_err_t err = esp_partition_read(partition, 0, header, sizeof(header));
if (err != ESP_OK) {
ESP_LOGW(TAG, "Failed to read header: %s", esp_err_to_name(err));
return false;
}
if (header[0] != kMagic) {
ESP_LOGI(TAG, "No cached data (magic mismatch)");
return false;
}
uint32_t data_len = header[1];
if (data_len == 0 || data_len > 64 * 1024) {
ESP_LOGW(TAG, "Invalid data length: %lu", data_len);
return false;
}
// Read JSON data
std::vector<char> buffer(data_len + 1, 0);
err = esp_partition_read(partition, sizeof(header), buffer.data(), data_len);
if (err != ESP_OK) {
ESP_LOGW(TAG, "Failed to read data: %s", esp_err_to_name(err));
return false;
}
cJSON* root = cJSON_Parse(buffer.data());
if (!root) {
ESP_LOGW(TAG, "Failed to parse cached JSON");
return false;
}
auto* brain = cJSON_GetObjectItem(root, "brain");
if (cJSON_IsString(brain)) profile_text_ = brain->valuestring;
auto* name = cJSON_GetObjectItem(root, "name");
if (cJSON_IsString(name)) child_name_ = name->valuestring;
auto* count = cJSON_GetObjectItem(root, "count");
if (cJSON_IsNumber(count)) interaction_count_ = count->valueint;
cJSON_Delete(root);
ESP_LOGI(TAG, "Loaded: name='%s', interactions=%d, profile=%zuB",
child_name_.c_str(), interaction_count_, profile_text_.size());
return true;
}
bool MemoryCache::Save()
{
const esp_partition_t* partition = esp_partition_find_first(
ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_SPIFFS, kPartitionLabel);
if (!partition) {
ESP_LOGW(TAG, "Partition '%s' not found", kPartitionLabel);
return false;
}
// Build JSON
cJSON* root = cJSON_CreateObject();
cJSON_AddStringToObject(root, "brain", profile_text_.c_str());
cJSON_AddStringToObject(root, "name", child_name_.c_str());
cJSON_AddNumberToObject(root, "count", interaction_count_);
char* json_str = cJSON_PrintUnformatted(root);
cJSON_Delete(root);
if (!json_str) return false;
uint32_t data_len = strlen(json_str);
// Erase first sector (4KB minimum erase unit)
uint32_t erase_size = ((sizeof(uint32_t) * 2 + data_len + 4095) / 4096) * 4096;
esp_err_t err = esp_partition_erase_range(partition, 0, erase_size);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to erase: %s", esp_err_to_name(err));
cJSON_free(json_str);
return false;
}
// Write header
uint32_t header[2] = {kMagic, data_len};
err = esp_partition_write(partition, 0, header, sizeof(header));
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to write header: %s", esp_err_to_name(err));
cJSON_free(json_str);
return false;
}
// Write JSON data
err = esp_partition_write(partition, sizeof(header), json_str, data_len);
cJSON_free(json_str);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to write data: %s", esp_err_to_name(err));
return false;
}
ESP_LOGI(TAG, "Saved %lu bytes to Flash", data_len);
return true;
}
void MemoryCache::UpdateFromServerSync(const cJSON* memory_sync)
{
if (!memory_sync) return;
auto* brain = cJSON_GetObjectItem(memory_sync, "brain_text");
if (cJSON_IsString(brain)) {
profile_text_ = brain->valuestring;
}
auto* name = cJSON_GetObjectItem(memory_sync, "child_name");
if (cJSON_IsString(name)) {
child_name_ = name->valuestring;
}
auto* count = cJSON_GetObjectItem(memory_sync, "interaction_count");
if (cJSON_IsNumber(count)) {
interaction_count_ = count->valueint;
}
Save();
ESP_LOGI(TAG, "Synced from server: name='%s', interactions=%d",
child_name_.c_str(), interaction_count_);
}