nrfcxx  0.1.0
C++-17 Framework for Nordic nRF5 Devices
Data Structures | Public Member Functions | Static Public Attributes | Protected Member Functions | Protected Attributes
nrfcxx::utility::Persist Class Reference

Class supporting persistence of tagged records to non-volatile memory. More...

#include <nrfcxx/utility.hpp>

Data Structures

struct  header_type
 Header identifying record content and its span. More...
 

Public Member Functions

constexpr Persist (FailSafeCode code=FailSafeCode::PERSIST_VIOLATION)
 Construct the wrapper class. More...
 
constexpr Persist (unsigned int code)
 Alternative constructor for uncast failsafe codes.
 
int configure (const uint32_t *begin, const uint32_t *end)
 Configure the instance to operate on a specific persisted region. More...
 
const header_typefind (uint16_t tag)
 Find the record with the given tag, if it is present. More...
 
int erase (uint16_t tag)
 Remove the record with the given tag. More...
 
const header_typeaddReplace (uint16_t tag, const void *data, size_t count)
 Add or replace the record with the given tag. More...
 
ptrdiff_t extract (header_type *buffer=nullptr, size_t length=0) const
 Extract the active records from the region. More...
 
int restore (const header_type *buffer, size_t length)
 Replay extracted records into the persisted region. More...
 
int gc ()
 Extract, clear, and restore the page contents. More...
 
bool is_cleared () const
 Return true iff the region is configured and starts with a used record.
 
size_t available () const
 Return the number of bytes available for payload in a new record. More...
 
size_t erased () const
 Return the number of bytes that can be reclaimed if the region is extracted and restored.
 
void clear ()
 Clear all data from the persisted region.
 

Static Public Attributes

static constexpr uint16_t TAG_ERASED = 0
 header_type::tag value for a record that has been erased.
 
static constexpr uint16_t TAG_UNUSED = 0xFFFF
 header_type::tag value for the placeholder header of the next record to be written.
 
static constexpr const uint32_t * REGION_BEGIN = &__PersistBase
 pointer to the first word of the PERSIST memory region. More...
 
static constexpr const uint32_t * REGION_END = &__PersistLimit
 pointer to the first word after the PERSIST memory region. More...
 
static constexpr unsigned int PAGE_SIZE = 1024
 Number of bytes in a flash page. More...
 
static constexpr unsigned int BLOCK_SIZE = PAGE_SIZE
 Number of bytes in a flash block. More...
 

Protected Member Functions

void erase_ (const header_type *hp)
 Mark an existing record as erased.
 
const header_typefind_ (uint16_t tag)
 Locate a record by tag.
 

Protected Attributes

const FailSafeCode code_
 Code used in failsafe resets related to persistence failures.
 
const header_typebegin_
 Inclusive lower bound of the persisted region.
 
const header_typeend_
 Exclusive upper bound of the persisted region.
 
const header_typefirst_
 Pointer to the first used and unerased record in the region. More...
 
const header_typeunused_
 Pointer to the region's unused record. More...
 
unsigned int erased_
 Number of words in the region covered by erased records. More...
 

Detailed Description

Class supporting persistence of tagged records to non-volatile memory.

Page-aligned regions of flash memory may be reserved by defining a non-empty PERSIST region in the linker memory map and defining objects that are placed in the .persist (or a .persist.*) section. Instantiating an instance of Persist and configuring it to reference one of those regions allows tagged records to be stored in flash and retrieved by their tag.

Constructor & Destructor Documentation

◆ Persist()

constexpr nrfcxx::utility::Persist::Persist ( FailSafeCode  code = FailSafeCode::PERSIST_VIOLATION)
inlineconstexpr

Construct the wrapper class.

Parameters
codethe failsafe code to be generated when an unrecoverable error is detected using the Persist API.

Member Function Documentation

◆ addReplace()

const header_type* nrfcxx::utility::Persist::addReplace ( uint16_t  tag,
const void *  data,
size_t  count 
)

Add or replace the record with the given tag.

Parameters
tagthe header_type::tag value for the record. If a record with this tag is already present it will be erased prior to creating the new record.
datapointer to the data to be associated with the record. If a null pointer is passed the record will have no data.
countthe number of bytes at data that should be preserved at header_type::data in the resulting record.
Returns
a null pointer if the record could not be created, otherwise a pointer to the new record. If a null pointer is returned any previous record with tag is left unmodified.

◆ available()

size_t nrfcxx::utility::Persist::available ( ) const
inline

Return the number of bytes available for payload in a new record.

Note
This is the maximum payload length for a single record. It does not include the length of the headers for additional records.

◆ configure()

int nrfcxx::utility::Persist::configure ( const uint32_t *  begin,
const uint32_t *  end 
)

Configure the instance to operate on a specific persisted region.

An instance can be successfully configured at most once.

Parameters
beginpage-aligned pointer to NVMC data.
endpage-aligned pointer to NVMC data, at or above begin
Returns
zero if the configuration was acceptable, otherwise a negative error code.

◆ erase()

int nrfcxx::utility::Persist::erase ( uint16_t  tag)
inline

Remove the record with the given tag.

Parameters
tagthe header_type::tag value for a matching record.
Returns
zero if the record was found and removed, or a negative error code.

◆ extract()

ptrdiff_t nrfcxx::utility::Persist::extract ( header_type buffer = nullptr,
size_t  length = 0 
) const

Extract the active records from the region.

Parameters
bufferwhere to store the record data. If a null pointer is passed the records will not be extracted, but the return value will indicate the buffer length required to successfully extract the data.
lengththe number of words that may be stored in buffer when that is a non-null pointer.
Returns
the number of words stored in buffer (or that would be stored in buffer if it were not null), or a negative error code if the provided non-null buffer and length cannot accommodate all records.

◆ find()

const header_type* nrfcxx::utility::Persist::find ( uint16_t  tag)
inline

Find the record with the given tag, if it is present.

Parameters
tagthe header_type::tag value for a matching record.
Returns
a pointer to the record if it is found, otherwise a null pointer.

◆ gc()

int nrfcxx::utility::Persist::gc ( )

Extract, clear, and restore the page contents.

This uses the stack to store the extracted information.

Returns
as with restore().

◆ restore()

int nrfcxx::utility::Persist::restore ( const header_type buffer,
size_t  length 
)

Replay extracted records into the persisted region.

If a record has the same tag as a record in the region, it supersedes the existing record as with addReplace().

Parameters
bufferpointer to a record set extracted from a Persist instance using extract().
lengththe non-negative size returned from the extract() call that filled buffer.
Returns
A non-negative count of the number of records restored, if all records were restored. A negative error code indicates that the region did not have sufficient unused space to store the records.

Field Documentation

◆ BLOCK_SIZE

constexpr unsigned int nrfcxx::utility::Persist::BLOCK_SIZE = PAGE_SIZE
staticconstexpr

Number of bytes in a flash block.

Note
This constant is nRF5 series-specific; this value is for nRF51.

◆ erased_

unsigned int nrfcxx::utility::Persist::erased_
protected

Number of words in the region covered by erased records.

This includes the header word as well as record content.

◆ first_

const header_type* nrfcxx::utility::Persist::first_
protected

Pointer to the first used and unerased record in the region.

If the region has never had a record added the pointer is null.

◆ PAGE_SIZE

constexpr unsigned int nrfcxx::utility::Persist::PAGE_SIZE = 1024
staticconstexpr

Number of bytes in a flash page.

Note
This constant is nRF5 series-specific; this value is for nRF51.

◆ REGION_BEGIN

constexpr const uint32_t* nrfcxx::utility::Persist::REGION_BEGIN = &__PersistBase
staticconstexpr

pointer to the first word of the PERSIST memory region.

This may be used as the begin parameter to configure() when a single persist instance that covers the region is desired.

◆ REGION_END

constexpr const uint32_t* nrfcxx::utility::Persist::REGION_END = &__PersistLimit
staticconstexpr

pointer to the first word after the PERSIST memory region.

This may be used as the end parameter to configure() when a single persist instance that covers the region is desired.

◆ unused_

const header_type* nrfcxx::utility::Persist::unused_
protected

Pointer to the region's unused record.

If there is no unused space in the region the pointer is null.

Note
An unused record has tag TAG_UNUSED and span of 0xFFFF. There is at most one of these in the region, and it should generally not be exposed to the application.

The documentation for this class was generated from the following file: