#include "store.h" #include #include #include #include "esp_check.h" #include "esp_heap_caps.h" #include "esp_littlefs.h" #include "esp_log.h" #include "freertos/FreeRTOS.h" #include "freertos/semphr.h" static const char *TAG = "store"; static const char *const NAMES[] = { "device", "zone", "preset", "profile", "palette", "pattern", "sequence", "scene", "group", "output", "settings", }; #define N_COLL (sizeof(NAMES) / sizeof(NAMES[0])) static cJSON *s_coll[N_COLL]; static SemaphoreHandle_t s_lock; static int index_of(const char *name) { for (size_t i = 0; i < N_COLL; i++) { if (strcmp(NAMES[i], name) == 0) { return (int)i; } } return -1; } static esp_err_t mount(const char *label, const char *base) { esp_vfs_littlefs_conf_t conf = { .base_path = base, .partition_label = label, .format_if_mount_failed = true, .grow_on_mount = true, // the db partition spans the rest of the flash; an older, smaller filesystem is extended in place }; return esp_vfs_littlefs_register(&conf); } static cJSON *load_file(const char *name) { char path[48]; snprintf(path, sizeof(path), "/db/%s.json", name); FILE *f = fopen(path, "rb"); if (!f) { return cJSON_CreateObject(); } fseek(f, 0, SEEK_END); long n = ftell(f); fseek(f, 0, SEEK_SET); char *buf = malloc(n + 1); cJSON *obj = NULL; if (buf) { buf[fread(buf, 1, n, f)] = 0; obj = cJSON_Parse(buf); free(buf); } fclose(f); if (!cJSON_IsObject(obj)) { ESP_LOGW(TAG, "%s: unreadable, starting empty", path); cJSON_Delete(obj); obj = cJSON_CreateObject(); } return obj; } // JSON nodes are small, so plain malloc would put the whole store in internal RAM, which the mic, // Wi-Fi and lwIP need. They go to PSRAM instead (free() releases either kind). static void *json_malloc(size_t size) { void *p = heap_caps_malloc(size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); return p ? p : malloc(size); } esp_err_t store_init(void) { cJSON_Hooks hooks = { .malloc_fn = json_malloc, .free_fn = free }; cJSON_InitHooks(&hooks); s_lock = xSemaphoreCreateRecursiveMutex(); ESP_RETURN_ON_FALSE(s_lock, ESP_ERR_NO_MEM, TAG, "mutex"); ESP_RETURN_ON_ERROR(mount("db", "/db"), TAG, "mount db"); for (size_t i = 0; i < N_COLL; i++) { s_coll[i] = load_file(NAMES[i]); ESP_LOGI(TAG, "%s: %d records", NAMES[i], cJSON_GetArraySize(s_coll[i])); } return ESP_OK; } bool store_known(const char *name) { return index_of(name) >= 0; } cJSON *store_coll(const char *name) { int i = index_of(name); return i < 0 ? NULL : s_coll[i]; } esp_err_t store_save(const char *name) { int i = index_of(name); ESP_RETURN_ON_FALSE(i >= 0, ESP_ERR_NOT_FOUND, TAG, "unknown collection"); char *json = cJSON_PrintUnformatted(s_coll[i]); ESP_RETURN_ON_FALSE(json, ESP_ERR_NO_MEM, TAG, "print"); char tmp[48], path[48]; snprintf(tmp, sizeof(tmp), "/db/%s.tmp", name); snprintf(path, sizeof(path), "/db/%s.json", name); FILE *f = fopen(tmp, "wb"); esp_err_t ret = ESP_FAIL; if (f) { size_t len = strlen(json); bool ok = fwrite(json, 1, len, f) == len; ok = fclose(f) == 0 && ok; if (ok) { remove(path); // littlefs rename-over is not guaranteed on every version ok = rename(tmp, path) == 0; } ret = ok ? ESP_OK : ESP_FAIL; } free(json); if (ret != ESP_OK) { ESP_LOGE(TAG, "saving %s failed", name); } return ret; } esp_err_t store_replace(const char *name, cJSON *obj) { int i = index_of(name); if (i < 0 || !cJSON_IsObject(obj)) { cJSON_Delete(obj); return ESP_ERR_INVALID_ARG; } cJSON_Delete(s_coll[i]); s_coll[i] = obj; return store_save(name); } void store_next_id(const char *name, char *buf, size_t len) { long max = 0; cJSON *c = store_coll(name), *it; cJSON_ArrayForEach(it, c) { char *end; long v = strtol(it->string, &end, 10); if (*end == 0 && v > max) { max = v; } } snprintf(buf, len, "%ld", max + 1); } cJSON *store_settings(void) { return store_coll("settings"); } void store_lock(void) { xSemaphoreTakeRecursive(s_lock, portMAX_DELAY); } void store_unlock(void) { xSemaphoreGiveRecursive(s_lock); }