Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MSoM: enable session resumption #2722

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions hal/src/rtl872x/backup_ram_hal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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);
scott-brust marked this conversation as resolved.
Show resolved Hide resolved
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;
}
Expand All @@ -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);
scott-brust marked this conversation as resolved.
Show resolved Hide resolved
}
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;
}
Expand Down
1 change: 1 addition & 0 deletions hal/src/rtl872x/core_hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions platform/MCU/rtl872x/inc/hw_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions platform/MCU/rtl872x/inc/platform_system_flags.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 0 additions & 5 deletions system/src/system_cloud_internal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<void(SessionPersistOpaque&)> fn)
Expand Down