Save and restore from power off

This commit is contained in:
Manuel Bl
2021-09-29 16:48:59 +02:00
parent 0130928601
commit f433e826a7
9 changed files with 402 additions and 1 deletions

View File

@ -75,6 +75,7 @@ static spi_transaction_t spi_transaction;
static SemaphoreHandle_t mutex;
static esp_timer_handle_t timer;
static int64_t time_offset;
static int32_t initial_time_offset;
static int64_t next_alarm;
static volatile bool run_background_task;
static volatile wait_kind_e current_wait_kind;
@ -306,11 +307,25 @@ void init_timer(void)
struct timeval now;
gettimeofday(&now, NULL);
time_offset = (int64_t)now.tv_sec * 1000000;
time_offset += (int64_t)now.tv_sec * 1000000;
initial_time_offset = 0;
ESP_LOGI(TAG, "Timer initialized");
}
uint32_t hal_esp32_get_time(void)
{
struct timeval now;
gettimeofday(&now, NULL);
return now.tv_sec + initial_time_offset;
}
void hal_esp32_set_time(uint32_t time_val)
{
initial_time_offset = time_val;
time_offset = (int64_t)time_val * 1000000;
}
void set_next_alarm(int64_t time)
{
next_alarm = time;

View File

@ -35,6 +35,26 @@ void hal_esp32_set_rssi_cal(int8_t rssi_cal);
TickType_t hal_esp32_get_timer_duration(void);
/**
* Gets the time.
*
* The time is relative to boot time of the
* run when the device joined the TTN network.
*
* @return time (in seconds)
*/
uint32_t hal_esp32_get_time(void);
/**
* Sets the time.
*
* The time is relative to boot time of the
* run when the device joined the TTN network.
*
* @param time_val time (in seconds)
*/
void hal_esp32_set_time(uint32_t time_val);
#ifdef __cplusplus
}

View File

@ -18,6 +18,7 @@
#include "lmic/lmic.h"
#include "ttn_logging.h"
#include "ttn_provisioning.h"
#include "ttn_nvs.h"
#include "ttn_rtc.h"
#define TAG "ttn"
@ -236,6 +237,29 @@ bool ttn_resume_after_deep_sleep(void)
return true;
}
bool ttn_resume_after_power_off(int off_duration)
{
if (!ttn_provisioning_have_keys())
{
if (!ttn_provisioning_restore_keys(false))
return false;
}
if (!ttn_provisioning_have_keys())
{
ESP_LOGW(TAG, "DevEUI, AppEUI/JoinEUI and/or AppKey have not been provided");
return false;
}
start();
if (!ttn_nvs_restore(off_duration))
return false;
has_joined = true;
return true;
}
// Called immediately before sending join request message
void config_rf_params(void)
{
@ -340,6 +364,11 @@ void ttn_prepare_for_deep_sleep(void)
stop();
}
void ttn_prepare_for_power_off(void)
{
ttn_nvs_save();
stop();
}
void ttn_wait_for_idle(void)
{

123
src/ttn_nvs.c Normal file
View File

@ -0,0 +1,123 @@
/*******************************************************************************
*
* ttn-esp32 - The Things Network device library for ESP-IDF / SX127x
*
* Copyright (c) 2018-2021 Manuel Bleichenbacher
*
* Licensed under MIT License
* https://opensource.org/licenses/MIT
*
* Functions for storing and retrieving TTN communication state from NVS.
*******************************************************************************/
#include "esp_log.h"
#include "esp_system.h"
#include "hal/hal_esp32.h"
#include "lmic/lmic.h"
#include "nvs_flash.h"
#include "ttn_rtc.h"
#include <string.h>
#define LMIC_OFFSET(field) __builtin_offsetof(struct lmic_t, field)
#define LMIC_DIST(field1, field2) (LMIC_OFFSET(field2) - LMIC_OFFSET(field1))
#define TAG "ttn_nvs"
#define NVS_FLASH_PARTITION "ttn"
#define NVS_FLASH_KEY_CHUNK_1 "chunk1"
#define NVS_FLASH_KEY_CHUNK_2 "chunk2"
#define NVS_FLASH_KEY_CHUNK_3 "chunk3"
#define NVS_FLASH_KEY_TIME "time"
void ttn_nvs_save()
{
nvs_handle handle = 0;
esp_err_t res = nvs_open(NVS_FLASH_PARTITION, NVS_READWRITE, &handle);
if (res != ESP_OK)
goto done;
// Save LMIC struct except client, osjob, pendTxData and frame
size_t len1 = LMIC_DIST(radio, pendTxData);
res = nvs_set_blob(handle, NVS_FLASH_KEY_CHUNK_1, &LMIC.radio, len1);
if (res != ESP_OK)
goto done;
size_t len2 = LMIC_DIST(pendTxData, frame) - MAX_LEN_PAYLOAD;
res = nvs_set_blob(handle, NVS_FLASH_KEY_CHUNK_2, (u1_t *)&LMIC.pendTxData + MAX_LEN_PAYLOAD, len2);
if (res != ESP_OK)
goto done;
size_t len3 = sizeof(struct lmic_t) - LMIC_OFFSET(frame) - MAX_LEN_FRAME;
res = nvs_set_blob(handle, NVS_FLASH_KEY_CHUNK_3, (u1_t *)&LMIC.frame + MAX_LEN_FRAME, len3);
if (res != ESP_OK)
goto done;
res = nvs_set_u32(handle, NVS_FLASH_KEY_TIME, hal_esp32_get_time());
if (res != ESP_OK)
goto done;
res = nvs_commit(handle);
if (res != ESP_OK)
goto done;
done:
nvs_close(handle);
if (res == ESP_ERR_NVS_NOT_INITIALIZED)
{
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)
{
nvs_handle handle = 0;
esp_err_t res = nvs_open(NVS_FLASH_PARTITION, NVS_READWRITE, &handle);
if (res != ESP_OK)
goto done;
uint32_t time_val;
res = nvs_get_u32(handle, NVS_FLASH_KEY_TIME, &time_val);
if (res != ESP_OK)
goto done;
size_t len1 = LMIC_DIST(radio, pendTxData);
res = nvs_get_blob(handle, NVS_FLASH_KEY_CHUNK_1, &LMIC.radio, &len1);
if (res != ESP_OK)
goto done;
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);
if (res != ESP_OK)
goto done;
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);
if (res != ESP_OK)
goto done;
// invalidate data
res = nvs_erase_key(handle, NVS_FLASH_KEY_TIME);
if (res != ESP_OK)
goto done;
res = nvs_commit(handle);
if (res != ESP_OK)
goto done;
if (off_duration != 0)
hal_esp32_set_time(time_val + off_duration * 60);
done:
nvs_close(handle);
if (res == ESP_ERR_NVS_NOT_INITIALIZED)
{
ESP_LOGW(TAG, "NVS storage is not initialized. Call 'nvs_flash_init()' first.");
}
return res == ESP_OK;
}

30
src/ttn_nvs.h Normal file
View File

@ -0,0 +1,30 @@
/*******************************************************************************
*
* ttn-esp32 - The Things Network device library for ESP-IDF / SX127x
*
* Copyright (c) 2018-2021 Manuel Bleichenbacher
*
* Licensed under MIT License
* https://opensource.org/licenses/MIT
*
* Functions for storing and retrieving TTN communication state from NVS.
*******************************************************************************/
#ifndef TTN_NVS_H
#define TTN_NVS_H
#include <stdbool.h>
#ifdef __cplusplus
extern "C"
{
#endif
void ttn_nvs_save();
bool ttn_nvs_restore(int off_duration);
#ifdef __cplusplus
}
#endif
#endif