mirror of
https://github.com/manuelbl/ttn-esp32.git
synced 2025-06-15 12:24:27 +02:00
Improve robustness of restore from NVS
This commit is contained in:
parent
f433e826a7
commit
b626ccb61a
@ -32,6 +32,8 @@ void ttn_nvs_save()
|
|||||||
{
|
{
|
||||||
nvs_handle handle = 0;
|
nvs_handle handle = 0;
|
||||||
esp_err_t res = nvs_open(NVS_FLASH_PARTITION, NVS_READWRITE, &handle);
|
esp_err_t res = nvs_open(NVS_FLASH_PARTITION, NVS_READWRITE, &handle);
|
||||||
|
if (res == ESP_ERR_NVS_NOT_INITIALIZED)
|
||||||
|
ESP_LOGW(TAG, "NVS storage is not initialized. Call 'nvs_flash_init()' first.");
|
||||||
if (res != ESP_OK)
|
if (res != ESP_OK)
|
||||||
goto done;
|
goto done;
|
||||||
|
|
||||||
@ -62,20 +64,15 @@ void ttn_nvs_save()
|
|||||||
done:
|
done:
|
||||||
nvs_close(handle);
|
nvs_close(handle);
|
||||||
|
|
||||||
if (res == ESP_ERR_NVS_NOT_INITIALIZED)
|
ESP_ERROR_CHECK(res);
|
||||||
{
|
|
||||||
ESP_LOGW(TAG, "NVS storage is not initialized. Call 'nvs_flash_init()' first.");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
ESP_ERROR_CHECK(res);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ttn_nvs_restore(int off_duration)
|
bool ttn_nvs_restore(int off_duration)
|
||||||
{
|
{
|
||||||
nvs_handle handle = 0;
|
nvs_handle handle = 0;
|
||||||
esp_err_t res = nvs_open(NVS_FLASH_PARTITION, NVS_READWRITE, &handle);
|
esp_err_t res = nvs_open(NVS_FLASH_PARTITION, NVS_READWRITE, &handle);
|
||||||
|
if (res == ESP_ERR_NVS_NOT_INITIALIZED)
|
||||||
|
ESP_LOGW(TAG, "NVS storage is not initialized. Call 'nvs_flash_init()' first.");
|
||||||
if (res != ESP_OK)
|
if (res != ESP_OK)
|
||||||
goto done;
|
goto done;
|
||||||
|
|
||||||
@ -89,11 +86,15 @@ bool ttn_nvs_restore(int off_duration)
|
|||||||
if (res != ESP_OK)
|
if (res != ESP_OK)
|
||||||
goto done;
|
goto done;
|
||||||
|
|
||||||
|
memset(LMIC.pendTxData, 0, MAX_LEN_PAYLOAD);
|
||||||
|
|
||||||
size_t len2 = LMIC_DIST(pendTxData, frame) - MAX_LEN_PAYLOAD;
|
size_t len2 = LMIC_DIST(pendTxData, frame) - MAX_LEN_PAYLOAD;
|
||||||
res = nvs_get_blob(handle, NVS_FLASH_KEY_CHUNK_2, (u1_t *)&LMIC.pendTxData + MAX_LEN_PAYLOAD, &len2);
|
res = nvs_get_blob(handle, NVS_FLASH_KEY_CHUNK_2, (u1_t *)&LMIC.pendTxData + MAX_LEN_PAYLOAD, &len2);
|
||||||
if (res != ESP_OK)
|
if (res != ESP_OK)
|
||||||
goto done;
|
goto done;
|
||||||
|
|
||||||
|
memset(LMIC.frame, 0, MAX_LEN_FRAME);
|
||||||
|
|
||||||
size_t len3 = sizeof(struct lmic_t) - LMIC_OFFSET(frame) - MAX_LEN_FRAME;
|
size_t len3 = sizeof(struct lmic_t) - LMIC_OFFSET(frame) - MAX_LEN_FRAME;
|
||||||
res = nvs_get_blob(handle, NVS_FLASH_KEY_CHUNK_3, (u1_t *)&LMIC.frame + MAX_LEN_FRAME, &len3);
|
res = nvs_get_blob(handle, NVS_FLASH_KEY_CHUNK_3, (u1_t *)&LMIC.frame + MAX_LEN_FRAME, &len3);
|
||||||
if (res != ESP_OK)
|
if (res != ESP_OK)
|
||||||
@ -114,10 +115,8 @@ bool ttn_nvs_restore(int off_duration)
|
|||||||
done:
|
done:
|
||||||
nvs_close(handle);
|
nvs_close(handle);
|
||||||
|
|
||||||
if (res == ESP_ERR_NVS_NOT_INITIALIZED)
|
if (res != ESP_OK)
|
||||||
{
|
memset(&LMIC.radio, 0, sizeof(LMIC) - LMIC_OFFSET(radio));
|
||||||
ESP_LOGW(TAG, "NVS storage is not initialized. Call 'nvs_flash_init()' first.");
|
|
||||||
}
|
|
||||||
|
|
||||||
return res == ESP_OK;
|
return res == ESP_OK;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user