/** * @file nvs_driver.c * @brief Non-Volatile Storage implementation * * Implements persistent state management using ESP-IDF NVS API. */ #include "drivers/nvs_driver.h" #include "esp_log.h" #include "nvs.h" #include "nvs_flash.h" static const char *TAG = "nvs_driver"; static const char *NAMESPACE = "storage"; static const char *KEY_PROVISIONED = "is_prov"; static const char *KEY_BOOT_COUNT = "boots"; bool nvsDriverInit(void) { ESP_LOGI(TAG, "Initializing NVS"); esp_err_t ret = nvs_flash_init(); if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { // NVS partition was truncated and needs to be erased // Retry nvs_flash_init ESP_LOGW(TAG, "Erasing NVS partition due to corruption/version mismatch"); ESP_ERROR_CHECK(nvs_flash_erase()); ret = nvs_flash_init(); } if (ret != ESP_OK) { ESP_LOGE(TAG, "NVS Init failed: %s", esp_err_to_name(ret)); return false; } return true; } bool nvsDriverIsFirstBoot(void) { nvs_handle_t handle; esp_err_t err = nvs_open(NAMESPACE, NVS_READONLY, &handle); if (err != ESP_OK) { // If namespace doesn't exist, it's definitely first boot return true; } uint8_t provisioned = 0; err = nvs_get_u8(handle, KEY_PROVISIONED, &provisioned); nvs_close(handle); // If key not found (ESP_ERR_NVS_NOT_FOUND), it's first boot if (err == ESP_ERR_NVS_NOT_FOUND) { return true; } return (provisioned == 0); } bool nvsDriverMarkProvisioned(void) { nvs_handle_t handle; esp_err_t err = nvs_open(NAMESPACE, NVS_READWRITE, &handle); if (err != ESP_OK) return false; err = nvs_set_u8(handle, KEY_PROVISIONED, 1); if (err == ESP_OK) { err = nvs_commit(handle); } nvs_close(handle); if (err == ESP_OK) { ESP_LOGI(TAG, "Device marked as provisioned"); } else { ESP_LOGE(TAG, "Failed to write provision flag: %s", esp_err_to_name(err)); } return (err == ESP_OK); } uint32_t nvsDriverGetBootCount(void) { nvs_handle_t handle; if (nvs_open(NAMESPACE, NVS_READONLY, &handle) != ESP_OK) return 1; uint32_t count = 0; esp_err_t err = nvs_get_u32(handle, KEY_BOOT_COUNT, &count); nvs_close(handle); return (err == ESP_OK) ? count : 1; } void nvsDriverIncrementBootCount(void) { nvs_handle_t handle; if (nvs_open(NAMESPACE, NVS_READWRITE, &handle) != ESP_OK) return; uint32_t count = 0; nvs_get_u32(handle, KEY_BOOT_COUNT, &count); count++; nvs_set_u32(handle, KEY_BOOT_COUNT, count); nvs_commit(handle); nvs_close(handle); ESP_LOGI(TAG, "Boot count updated: %ld", (long)count); } // IMU calibration keys static const char *KEY_IMU_CAL_X = "imu_cal_x"; static const char *KEY_IMU_CAL_Y = "imu_cal_y"; static const char *KEY_IMU_CAL_Z = "imu_cal_z"; bool nvsDriverSaveImuCalibration(float x, float y, float z) { nvs_handle_t handle; esp_err_t err = nvs_open(NAMESPACE, NVS_READWRITE, &handle); if (err != ESP_OK) { ESP_LOGE(TAG, "Failed to open NVS for calibration save"); return false; } // NVS doesn't have native float support, so we store as blobs bool success = true; if (nvs_set_blob(handle, KEY_IMU_CAL_X, &x, sizeof(float)) != ESP_OK) { success = false; } if (nvs_set_blob(handle, KEY_IMU_CAL_Y, &y, sizeof(float)) != ESP_OK) { success = false; } if (nvs_set_blob(handle, KEY_IMU_CAL_Z, &z, sizeof(float)) != ESP_OK) { success = false; } if (success) { err = nvs_commit(handle); if (err != ESP_OK) { success = false; } } nvs_close(handle); if (success) { ESP_LOGI(TAG, "IMU calibration saved: x=%.3f y=%.3f z=%.3f", x, y, z); } else { ESP_LOGE(TAG, "Failed to save IMU calibration"); } return success; } bool nvsDriverLoadImuCalibration(float *x, float *y, float *z) { if (!x || !y || !z) { return false; } nvs_handle_t handle; esp_err_t err = nvs_open(NAMESPACE, NVS_READONLY, &handle); if (err != ESP_OK) { return false; } size_t size = sizeof(float); bool success = true; if (nvs_get_blob(handle, KEY_IMU_CAL_X, x, &size) != ESP_OK) { success = false; } size = sizeof(float); if (nvs_get_blob(handle, KEY_IMU_CAL_Y, y, &size) != ESP_OK) { success = false; } size = sizeof(float); if (nvs_get_blob(handle, KEY_IMU_CAL_Z, z, &size) != ESP_OK) { success = false; } nvs_close(handle); if (success) { ESP_LOGI(TAG, "IMU calibration loaded: x=%.3f y=%.3f z=%.3f", *x, *y, *z); } return success; }