Skip to content

Commit d57d003

Browse files
committed
store counter of restarts in NVS (Non-Volatile Storage)
In preparation of #748, this counter will serve as part of an "unique" token in the log file name ; ensuring that after a reboot we will not overwrite an existing log file. The counter is stored in NVS, and is incremented at each class instanciation; which should roughly translate to every reboot. (Don't know about the deepsleep for the moment). The counter will reset to 1 after 99.999.999 restarts, which should gives us plenty of time. Signed-off-by: Ludovic LANGE <llange@users.noreply.github.com>
1 parent e19f95c commit d57d003

3 files changed

Lines changed: 176 additions & 6 deletions

File tree

vehicle/OVMS.V3/main/ovms_main.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ static const char *TAG = "ovms_main";
66
#include "freertos/FreeRTOS.h"
77
#include "freertos/task.h"
88
#include "esp_system.h"
9-
#include "nvs_flash.h"
109
#include <stdio.h>
1110
#include <string.h>
1211
#include "ovms.h"
1312
#include "ovms_peripherals.h"
1413
#include "ovms_housekeeping.h"
1514
#include "ovms_events.h"
1615
#include "ovms_config.h"
16+
#include "ovms_nvs.h"
1717
#include "ovms_module.h"
1818
#include <esp_task_wdt.h>
1919

@@ -45,15 +45,17 @@ Peripherals* MyPeripherals = NULL;
4545

4646
void app_main(void)
4747
{
48-
if (nvs_flash_init() == ESP_ERR_NVS_NO_FREE_PAGES)
49-
{
50-
nvs_flash_erase();
51-
nvs_flash_init();
52-
}
48+
49+
// This must be the first call, it initializes the NVS subsystem which is
50+
// used by other components - among which the Wifi
51+
MyNonVolatileStorage.Init();
5352

5453
ESP_LOGI(TAG, "Executing on CPU core %d",xPortGetCoreID());
5554
AddTaskToMap(xTaskGetCurrentTaskHandle());
5655

56+
ESP_LOGI(TAG, "Handling Non-Volatile stored values...");
57+
MyNonVolatileStorage.Start();
58+
5759
ESP_LOGI(TAG, "Mounting CONFIG...");
5860
MyConfig.mount();
5961

vehicle/OVMS.V3/main/ovms_nvs.cpp

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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+
}

vehicle/OVMS.V3/main/ovms_nvs.h

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
#ifndef __ONVS_H__
30+
#define __ONVS_H__
31+
32+
#include <stdio.h>
33+
#include "nvs.h"
34+
#include "nvs_flash.h"
35+
#include "esp_idf_version.h"
36+
37+
class OvmsNonVolatileStorage
38+
{
39+
public:
40+
OvmsNonVolatileStorage();
41+
~OvmsNonVolatileStorage();
42+
43+
public:
44+
void Init();
45+
void Start();
46+
uint64_t GetRestartCount();
47+
48+
private:
49+
uint64_t m_restart_counter;
50+
};
51+
52+
extern OvmsNonVolatileStorage MyNonVolatileStorage;
53+
54+
#endif //#ifndef __ONVS_H__

0 commit comments

Comments
 (0)