|
| 1 | +/* |
| 2 | +; Project: Open Vehicle Monitor System |
| 3 | +; Date: 12th November 2022 |
| 4 | +; |
| 5 | +; Changes: |
| 6 | +; 1.0 Initial release - based on public domain code from Espressif (examples) |
| 7 | +; |
| 8 | +; (C) 2022 Ludovic LANGE |
| 9 | +; |
| 10 | +; Permission is hereby granted, free of charge, to any person obtaining a copy |
| 11 | +; of this software and associated documentation files (the "Software"), to deal |
| 12 | +; in the Software without restriction, including without limitation the rights |
| 13 | +; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 14 | +; copies of the Software, and to permit persons to whom the Software is |
| 15 | +; furnished to do so, subject to the following conditions: |
| 16 | +; |
| 17 | +; The above copyright notice and this permission notice shall be included in |
| 18 | +; all copies or substantial portions of the Software. |
| 19 | +; |
| 20 | +; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 | +; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 | +; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 23 | +; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 | +; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 25 | +; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 26 | +; THE SOFTWARE. |
| 27 | +*/ |
| 28 | + |
| 29 | +#include "ovms_log.h" |
| 30 | +static const char *TAG = "nvs"; |
| 31 | + |
| 32 | +#include "ovms_nvs.h" |
| 33 | +#include "nvs.h" |
| 34 | +#include "nvs_flash.h" |
| 35 | + |
| 36 | +OvmsNonVolatileStorage MyNonVolatileStorage __attribute__ ((init_priority (10000))); |
| 37 | + |
| 38 | +OvmsNonVolatileStorage::OvmsNonVolatileStorage() : m_restart_counter(0){ |
| 39 | + ESP_LOGI(TAG, "Initialising NVS (10000)"); |
| 40 | +} |
| 41 | + |
| 42 | +OvmsNonVolatileStorage::~OvmsNonVolatileStorage() { |
| 43 | + esp_err_t err = nvs_flash_deinit(); |
| 44 | + if (err != ESP_OK) { |
| 45 | + ESP_LOGE(TAG, "Error (%s) de-initialising NVS subsystem!", esp_err_to_name(err)); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +void OvmsNonVolatileStorage::Init() { |
| 50 | + esp_err_t err = nvs_flash_init(); |
| 51 | + if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) { |
| 52 | + nvs_flash_erase(); |
| 53 | + err = nvs_flash_init(); |
| 54 | + } |
| 55 | + |
| 56 | + if (err != ESP_OK) { |
| 57 | + ESP_LOGE(TAG, "Error (%s) initialising NVS subsystem!", esp_err_to_name(err)); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +void OvmsNonVolatileStorage::Start() { |
| 62 | + esp_err_t err = ESP_OK; |
| 63 | + |
| 64 | +#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 0, 0) |
| 65 | + nvs_handle_t handle; |
| 66 | +#else |
| 67 | + nvs_handle handle; |
| 68 | +#endif |
| 69 | + |
| 70 | + ESP_LOGI(TAG, "Opening Non-Volatile Storage (NVS) default partition and 'ovms' namespace"); |
| 71 | + err = nvs_open("ovms", NVS_READWRITE, &handle); |
| 72 | + if (err != ESP_OK) { |
| 73 | + ESP_LOGE(TAG, "Error (%s) opening NVS handle!", esp_err_to_name(err)); |
| 74 | + } else { |
| 75 | + err = nvs_get_u64(handle, "restart_counter", &m_restart_counter); |
| 76 | + switch (err) { |
| 77 | + case ESP_OK: |
| 78 | + ESP_LOGI(TAG, "Current restart counter: %llu", m_restart_counter); |
| 79 | + break; |
| 80 | + case ESP_ERR_NVS_NOT_FOUND: |
| 81 | + ESP_LOGW(TAG, "The restart counter is not initialized yet"); |
| 82 | + break; |
| 83 | + default : |
| 84 | + ESP_LOGE(TAG, "Error (%s) while reading restart counter in NVS!", esp_err_to_name(err)); |
| 85 | + } |
| 86 | + |
| 87 | + // Write |
| 88 | + ESP_LOGD(TAG, "Updating restart counter in NVS"); |
| 89 | + m_restart_counter++; |
| 90 | + // To ease the display on 8 decimal digits, we wrap after 99.999.999 restarts. |
| 91 | + // If we restart every minute, we'll reach this time in 190 years. |
| 92 | + if (m_restart_counter > 99999999) { |
| 93 | + m_restart_counter = 0; |
| 94 | + } |
| 95 | + err = nvs_set_u64(handle, "restart_counter", m_restart_counter); |
| 96 | + if (err != ESP_OK) { |
| 97 | + ESP_LOGE(TAG, "Error (%s) while updating restart counter in NVS!", esp_err_to_name(err)); |
| 98 | + } |
| 99 | + |
| 100 | + // Commit written value. |
| 101 | + // After setting any values, nvs_commit() must be called to ensure changes are written |
| 102 | + // to flash storage. Implementations may write to storage at other times, |
| 103 | + // but this is not guaranteed. |
| 104 | + err = nvs_commit(handle); |
| 105 | + if (err != ESP_OK) { |
| 106 | + ESP_LOGE(TAG, "Error (%s) while committing changes in NVS!", esp_err_to_name(err)); |
| 107 | + } |
| 108 | + nvs_close(handle); |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +uint64_t OvmsNonVolatileStorage::GetRestartCount() { |
| 113 | + return m_restart_counter; |
| 114 | +} |
0 commit comments