From 96daebb94678058ed000d0108f8c2e70dd10b3ad Mon Sep 17 00:00:00 2001 From: XuGuohui Date: Tue, 26 Dec 2023 16:20:19 +0800 Subject: [PATCH] [msom] enable session resumption --- hal/src/rtl872x/backup_ram_hal.cpp | 34 ++++++++++++++++--- hal/src/rtl872x/core_hal.c | 1 + platform/MCU/rtl872x/inc/hw_config.h | 2 ++ .../MCU/rtl872x/inc/platform_system_flags.h | 2 ++ system/src/system_cloud_internal.cpp | 5 --- 5 files changed, 35 insertions(+), 9 deletions(-) diff --git a/hal/src/rtl872x/backup_ram_hal.cpp b/hal/src/rtl872x/backup_ram_hal.cpp index 09dc8711d5..0130118805 100644 --- a/hal/src/rtl872x/backup_ram_hal.cpp +++ b/hal/src/rtl872x/backup_ram_hal.cpp @@ -29,6 +29,7 @@ #include "timer_hal.h" #include "static_recursive_mutex.h" #include "scope_guard.h" +#include "dtls_session_persist.h" // NOTE: we are using a dedicated flash page for this as timings for writing // into fs vs raw page are about 10x @@ -40,6 +41,8 @@ extern uintptr_t platform_backup_ram_persisted_flash_start; extern uintptr_t platform_backup_ram_persisted_flash_end; extern uintptr_t platform_backup_ram_persisted_flash_size; +extern SessionPersistDataOpaque session; + namespace { system_tick_t lastSyncTimeMs = 0; @@ -102,12 +105,32 @@ int hal_backup_ram_init(void) { g_backupRamValidMarker = HAL_BACKUP_RAM_VALID_VALUE; }); if ((g_backupRamValidMarker != HAL_BACKUP_RAM_VALID_VALUE) && - ((BOOT_Reason() & BIT_BOOT_DSLP_RESET_HAPPEN) || SYSTEM_FLAG(restore_backup_ram) == 1 || dctFlags.restore_backup_ram == 1)) { - SYSTEM_FLAG(restore_backup_ram) = 0; + ((BOOT_Reason() & BIT_BOOT_DSLP_RESET_HAPPEN) || + (SYSTEM_FLAG(restore_backup_ram) & SYSTEM_FLAG_RESTOR_BACKUP_RAM_MASK) || + (dctFlags.restore_backup_ram & SYSTEM_FLAG_RESTOR_BACKUP_RAM_MASK))) { + SYSTEM_FLAG(restore_backup_ram) &= ~SYSTEM_FLAG_RESTOR_BACKUP_RAM_MASK; dct_write_app_data(&system_flags, DCT_SYSTEM_FLAGS_OFFSET, DCT_SYSTEM_FLAGS_SIZE); // Woke up from deep sleep CHECK(hal_flash_read((uintptr_t)&platform_backup_ram_persisted_flash_start, (uint8_t*)&platform_backup_ram_all_start, (size_t)&platform_backup_ram_persisted_flash_size)); + + if ((BOOT_Reason() & BIT_BOOT_DSLP_RESET_HAPPEN) && !(SYSTEM_FLAG(restore_backup_ram) & SYSTEM_FLAG_SESSION_DATA_STALE_MASK)) { + // The session info restored out of flash is valid + // Invalidate the session info in flash, cos it's probably stale from now on + auto s = new SessionPersistDataOpaque(); + if (s) { + std::swap(*s, session); + memset(&session, 0, sizeof(session)); + hal_backup_ram_sync(nullptr); + std::swap(*s, session); + delete s; + } + } else { + // Other cases: + // 1. Pin reset / PoR / BoR + // 2. Waking up from hibernate mode, but the session info is stale + memset(&session, 0, sizeof(session)); + } } return SYSTEM_ERROR_NONE; } @@ -116,14 +139,17 @@ int hal_backup_ram_sync(void* reserved) { BackupRamLock lk; if (memcmp((void*)&platform_backup_ram_all_start, backupRamShadow, sizeof(backupRamShadow))) { memcpy(backupRamShadow, (void*)&platform_backup_ram_all_start, sizeof(backupRamShadow)); - if (SYSTEM_FLAG(restore_backup_ram) != 1) { - SYSTEM_FLAG(restore_backup_ram) = 1; + if (!(SYSTEM_FLAG(restore_backup_ram) & SYSTEM_FLAG_RESTOR_BACKUP_RAM_MASK)) { + SYSTEM_FLAG(restore_backup_ram) |= SYSTEM_FLAG_RESTOR_BACKUP_RAM_MASK; dct_write_app_data(&system_flags, DCT_SYSTEM_FLAGS_OFFSET, DCT_SYSTEM_FLAGS_SIZE); } CHECK(hal_flash_erase_sector((uintptr_t)&platform_backup_ram_persisted_flash_start, CEIL_DIV((uintptr_t)&platform_backup_ram_persisted_flash_size, INTERNAL_FLASH_PAGE_SIZE))); CHECK(hal_flash_write((uintptr_t)&platform_backup_ram_persisted_flash_start, backupRamShadow, sizeof(backupRamShadow))); + + // We don't care about this bit in DCT + SYSTEM_FLAG(restore_backup_ram) &= ~SYSTEM_FLAG_SESSION_DATA_STALE_MASK; } return SYSTEM_ERROR_NONE; } diff --git a/hal/src/rtl872x/core_hal.c b/hal/src/rtl872x/core_hal.c index af5e11abef..e3dbf4b42f 100644 --- a/hal/src/rtl872x/core_hal.c +++ b/hal/src/rtl872x/core_hal.c @@ -942,6 +942,7 @@ retained_system SessionPersistDataOpaque session; int HAL_System_Backup_Save(size_t offset, const void* buffer, size_t length, void* reserved) { + SYSTEM_FLAG(restore_backup_ram) |= SYSTEM_FLAG_SESSION_DATA_STALE_MASK; if (offset==0 && length==sizeof(SessionPersistDataOpaque)) { memcpy(&session, buffer, length); diff --git a/platform/MCU/rtl872x/inc/hw_config.h b/platform/MCU/rtl872x/inc/hw_config.h index 7d8855304a..11e55ad4be 100644 --- a/platform/MCU/rtl872x/inc/hw_config.h +++ b/platform/MCU/rtl872x/inc/hw_config.h @@ -26,6 +26,8 @@ extern "C" { extern uint8_t USE_SYSTEM_FLAGS; extern uint16_t tempFlag; +#define SYSTEM_FLAG_RESTOR_BACKUP_RAM_MASK 0x0001 +#define SYSTEM_FLAG_SESSION_DATA_STALE_MASK 0x0002 #define SYSTEM_FLAG(x) (system_flags.x) void Load_SystemFlags(void); diff --git a/platform/MCU/rtl872x/inc/platform_system_flags.h b/platform/MCU/rtl872x/inc/platform_system_flags.h index 4e3b669b1e..d763154616 100644 --- a/platform/MCU/rtl872x/inc/platform_system_flags.h +++ b/platform/MCU/rtl872x/inc/platform_system_flags.h @@ -36,6 +36,8 @@ typedef struct __attribute__((packed)) platform_system_flags { uint8_t FeaturesEnabled_SysFlag; // default is 0xFF all features enabled. If any bits are cleared in the bottom 4-bits, then the upper 4 bits should be the logical inverse of these. // This is to prevent against corrupted data causing the bootloader to be unavailable. uint32_t RCC_CSR_SysFlag; + // bit 0: 1: restore backup ram from flash + // bit 1: 1: session data in flash is stale uint16_t restore_backup_ram; uint16_t reserved[3]; } platform_system_flags_t; diff --git a/system/src/system_cloud_internal.cpp b/system/src/system_cloud_internal.cpp index 1046b4466d..52903bc019 100644 --- a/system/src/system_cloud_internal.cpp +++ b/system/src/system_cloud_internal.cpp @@ -522,16 +522,11 @@ int Spark_Save(const void* buffer, size_t length, uint8_t type, void* reserved) int Spark_Restore(void* buffer, size_t max_length, uint8_t type, void* reserved) { -#if PLATFORM_ID == PLATFORM_MSOM - // FIXME: Force new session handshake for now - return 0; -#else size_t length = 0; int error = HAL_System_Backup_Restore(0, buffer, max_length, &length, nullptr); if (error) length = 0; return length; -#endif } void update_persisted_state(std::function fn)