Bluenet  5.7.0
Bluenet, firmware for nRF52 smart home devices
Loading...
Searching...
No Matches
MicroappStorage Class Reference

Class to store microapps on flash. More...

#include <cs_MicroappStorage.h>

Public Member Functions

cs_ret_code_t init ()
 Initialize fstorage, allocate buffer. More...
 
void getAppHeader (uint8_t appIndex, microapp_binary_header_t &header)
 Get a copy of the microapp binary header. More...
 
uint32_t getStartInstructionAddress (uint8_t appIndex)
 Get the address in flash, where the microapp program starts. More...
 
bool isErased (uint8_t appIndex)
 Checks if storage space of this microapp is erased. More...
 
cs_ret_code_t erase (uint8_t appIndex)
 Erases storage space of given app. More...
 
cs_ret_code_t writeChunk (uint8_t appIndex, uint16_t offset, const uint8_t *data, uint16_t size)
 Write a chunk to flash. More...
 
cs_ret_code_t validateApp (uint8_t appIndex)
 Validate the overall binary, this goes through flash and checks it completely. More...
 
void printHeader (uint8_t logLevel, microapp_binary_header_t &header)
 
void handleFileStorageEvent (nrf_fstorage_evt_t *evt)
 Internal usage: when fstorage is done, this function will be called (indirectly through app_scheduler). More...
 

Static Public Member Functions

static MicroappStoragegetInstance ()
 

Private Member Functions

 MicroappStorage ()
 Singleton, constructor, also copy constructor, is private. More...
 
 MicroappStorage (MicroappStorage const &)=delete
 
void operator= (MicroappStorage const &)=delete
 
cs_ret_code_t writeNextChunkPart ()
 Write the next part of a chunk. More...
 
cs_ret_code_t write (uint32_t flashAddress, const uint8_t *data, uint16_t size)
 Write to flash. More...
 
void onFlashWritten (cs_ret_code_t retCode)
 Called when data has been written to flash. More...
 
void resetChunkVars ()
 To be called when a chunk has been written (or when it failed to write). More...
 
bool isErased (uint32_t flashAddress, uint16_t size)
 Reads flash, and checks if it's erased. More...
 

Private Attributes

bool _writing = false
 Keep up whether or not we are currently writing to (or erasing) flash. More...
 
uint8_t _writeBuffer [MICROAPP_STORAGE_BUF_SIZE]
 The buffer is required to perform writes to flash, as the data has to be aligned, and stay in memory until the write is done. More...
 
const uint8_t * _chunkData = nullptr
 When writing a chunk of data, it will be done in parts. More...
 
uint16_t _chunkSize = 0
 
uint16_t _chunkWritten = 0
 
uint32_t _chunkFlashAddress = 0
 

Detailed Description

Class to store microapps on flash.

Constructor & Destructor Documentation

◆ MicroappStorage() [1/2]

Singleton, constructor, also copy constructor, is private.

◆ MicroappStorage() [2/2]

Member Function Documentation

◆ erase()

cs_ret_code_t MicroappStorage::erase ( uint8_t  appIndex)

Erases storage space of given app.

First checks if storage is already erased.

Parameters
[in]appIndexIndex of the microapp, validity is not checked.
Returns
ERR_SUCCESS The storage is already erased.
ERR_WAIT_FOR_SUCCESS The storage will be written erased, wait for CMD_RESOLVE_ASYNC_CONTROL_COMMAND.
ERR_BUSY Retry again later.

◆ getAppHeader()

void MicroappStorage::getAppHeader ( uint8_t  appIndex,
microapp_binary_header_t header 
)

Get a copy of the microapp binary header.

Parameters
[in]appIndexIndex of the microapp, validity is not checked.
[out]headerHeader to read to.

◆ getInstance()

static MicroappStorage & MicroappStorage::getInstance ( )
inlinestatic

◆ getStartInstructionAddress()

uint32_t MicroappStorage::getStartInstructionAddress ( uint8_t  appIndex)

Get the address in flash, where the microapp program starts.

Parameters
[in]appIndexIndex of the microapp, validity is not checked.

◆ handleFileStorageEvent()

void MicroappStorage::handleFileStorageEvent ( nrf_fstorage_evt_t *  evt)

Internal usage: when fstorage is done, this function will be called (indirectly through app_scheduler).

◆ init()

cs_ret_code_t MicroappStorage::init ( )

Initialize fstorage, allocate buffer.

◆ isErased() [1/2]

bool MicroappStorage::isErased ( uint32_t  flashAddress,
uint16_t  size 
)
private

Reads flash, and checks if it's erased.

◆ isErased() [2/2]

bool MicroappStorage::isErased ( uint8_t  appIndex)

Checks if storage space of this microapp is erased.

Parameters
[in]appIndexIndex of the microapp, validity is not checked.
Returns
true Storage space of this app is erased, and thus ready to be written.

◆ onFlashWritten()

void MicroappStorage::onFlashWritten ( cs_ret_code_t  retCode)
private

Called when data has been written to flash.

◆ operator=()

void MicroappStorage::operator= ( MicroappStorage const &  )
privatedelete

◆ printHeader()

void MicroappStorage::printHeader ( uint8_t  logLevel,
microapp_binary_header_t header 
)

◆ resetChunkVars()

void MicroappStorage::resetChunkVars ( )
private

To be called when a chunk has been written (or when it failed to write).

Resets all variables.

◆ validateApp()

cs_ret_code_t MicroappStorage::validateApp ( uint8_t  appIndex)

Validate the overall binary, this goes through flash and checks it completely.

All flash write operations have to have finished before.

Parameters
[in]appIndexIndex of the microapp, validity is not checked.
Returns
ERR_SUCCESS The app binary header is valid, and the checksums match.

◆ write()

cs_ret_code_t MicroappStorage::write ( uint32_t  flashAddress,
const uint8_t *  data,
uint16_t  size 
)
private

Write to flash.

Returns
ERR_SUCCESS The data will be written to flash.

◆ writeChunk()

cs_ret_code_t MicroappStorage::writeChunk ( uint8_t  appIndex,
uint16_t  offset,
const uint8_t *  data,
uint16_t  size 
)

Write a chunk to flash.

Parameters
[in]appIndexIndex of the microapp, validity is not checked.
[in]offsetOffset of the data in bytes from the start of the app storage space.
[in]dataPointer to the data to be written. Must remain valid until the write is finished.
[in]sizeSize of the data to be written, must be a multiple of 4.
Returns
ERR_SUCCESS_NO_CHANGE The data is already on flash.
ERR_WAIT_FOR_SUCCESS The data will be written to flash, wait for CMD_RESOLVE_ASYNC_CONTROL_COMMAND.
ERR_NO_SPACE Data would go outside the app storage space.
ERR_WRONG_PAYLOAD_LENGTH Data size is not a multiple of 4.
ERR_WRITE_DISABLED App storage space is not erased.
ERR_BUSY Another chunk is being written already.

◆ writeNextChunkPart()

cs_ret_code_t MicroappStorage::writeNextChunkPart ( )
private

Write the next part of a chunk.

Called first time from command, and every time when a flash write is done.

Returns
ERR_SUCCESS Complete chunk has been written to flash.
ERR_WAIT_FOR_SUCCESS The data will be written to flash.

Member Data Documentation

◆ _chunkData

const uint8_t* MicroappStorage::_chunkData = nullptr
private

When writing a chunk of data, it will be done in parts.

These variable are needed to know what to write where to.

◆ _chunkFlashAddress

uint32_t MicroappStorage::_chunkFlashAddress = 0
private

◆ _chunkSize

uint16_t MicroappStorage::_chunkSize = 0
private

◆ _chunkWritten

uint16_t MicroappStorage::_chunkWritten = 0
private

◆ _writeBuffer

uint8_t MicroappStorage::_writeBuffer[MICROAPP_STORAGE_BUF_SIZE]
private

The buffer is required to perform writes to flash, as the data has to be aligned, and stay in memory until the write is done.

◆ _writing

bool MicroappStorage::_writing = false
private

Keep up whether or not we are currently writing to (or erasing) flash.

We tried using nrf_fstorage_is_busy() instead, but that seems to return busy when State is writing (because State uses FDS, which uses fstorage).


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