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

Keeps up the asset filters. More...

#include <cs_AssetFilterStore.h>

Inheritance diagram for AssetFilterStore:
Collaboration diagram for AssetFilterStore:

Public Member Functions

cs_ret_code_t init ()
 set _filterModificationInProgress to false. More...
 
bool isReady ()
 Returns true if this object is ready to be used. More...
 
bool isInProgress ()
 Whether changes are in progress. More...
 
uint8_t getFilterCount ()
 Get the number of filters. More...
 
AssetFilter getFilter (uint8_t index)
 Get the Nth filter. More...
 
std::optional< uint8_t > findFilterIndex (uint8_t filterId)
 Returns the index of the filter with given filterId, if any. More...
 
uint16_t getMasterVersion ()
 Get the current master version. More...
 
uint32_t getMasterCrc ()
 Get the current master CRC. More...
 
void handleEvent (event_t &evt)
 Internal usage. More...
 
- Public Member Functions inherited from EventListener
 EventListener ()
 
virtual ~EventListener ()
 unregisters the listener. More...
 
virtual void handleEvent (event_t &event)=0
 Handle events. More...
 
void listen ()
 Registers this with the EventDispatcher. More...
 
- Public Member Functions inherited from Component
template<class T >
T * getComponent (Component *requester=nullptr)
 Returns a component of type T* from _parent->children(), If not found try again with ancestors: _parent-> ... ->_parent->children(). More...
 
virtual cs_ret_code_t init ()
 Components can implement this if they need to get references to sibling or if they need to do specific initialization. More...
 
void parentAllChildren ()
 utility that loops over all elements of getChildren() and setParent on the non-nullptr ones. More...
 
virtual ~Component ()=default
 

Static Public Attributes

static constexpr uint8_t MAX_FILTER_IDS = 8
 Max number of filters. More...
 
static constexpr size_t FILTER_BUFFER_SIZE = 520
 Max total size that the filters take up in RAM. More...
 
static constexpr int MODIFICATION_IN_PROGRESS_TIMEOUT_SECONDS = 20
 Time after last edit command (upload, remove), until "modification in progress" times out. More...
 

Private Member Functions

uint8_t * allocateFilter (uint8_t filterId, size_t stateDataSize)
 Allocates RAM for a filter of given size, and adds it to the filters array. More...
 
bool deallocateFilter (uint8_t filterId)
 Same as deallocateFilterByIndex, but looks up the filter by the filterId. More...
 
void deallocateFilterByIndex (uint8_t parsingFilterIndex)
 Deallocates the filter at given index in the filters array. More...
 
CS_TYPE getStateType (uint16_t filterDataSize)
 Get the State type, given the filter data size. More...
 
uint16_t getStateSize (uint16_t filterDataSize)
 Get the size required for State, given the filter data size. More...
 
CS_TYPE getNthStateType (uint8_t index)
 Used to loop over all asset filter state types. More...
 
uint8_t * findFilter (uint8_t filterId)
 Returns a pointer to the filter with given filterId, or nullptr if not found. More...
 
size_t getTotalHeapAllocatedSize ()
 Returns the total amount of heap allocated for the filters. More...
 
cs_ret_code_t handleUploadFilterCommand (const asset_filter_cmd_upload_filter_t &cmdData)
 Handle an upload command. More...
 
cs_ret_code_t handleRemoveFilterCommand (const asset_filter_cmd_remove_filter_t &cmdData)
 Removes given filter immediately. More...
 
cs_ret_code_t handleCommitFilterChangesCommand (const asset_filter_cmd_commit_filter_changes_t &cmdData)
 Commit the filters. More...
 
void handleGetFilterSummariesCommand (cs_result_t &result)
 Writes the filter summary in the result. More...
 
void onTick ()
 
void startInProgress ()
 To be called when about to modify filters. More...
 
void endInProgress (uint16_t newMasterVersion, uint32_t newMasterCrc)
 To be called when filters are no longer being modified. More...
 
void inProgressTimeout ()
 Called when the modification in progress is timed out. More...
 
void sendInProgressStatus ()
 Send an internal event when isInProgress() may have changed. More...
 
cs_ret_code_t commit (uint16_t masterVersion, uint32_t masterCrc, bool store)
 Commit current filters. More...
 
uint32_t computeMasterCrc ()
 Calculate the master CRC and return it (not stored). More...
 
bool validateFilters ()
 Checks for all filters if the allocated filter data size is equal to the computed size based on its contents. More...
 
void computeFilterCrcs ()
 Computes the CRC of the filters, and sets it in the filter. More...
 
void storeFilters ()
 Store filters to flash. More...
 
void loadFromFlash ()
 Load filters and version from flash. More...
 
void loadFromFlash (CS_TYPE type)
 Load filters of a specific type (size) from flash. More...
 
void markFiltersCommitted ()
 Sets flags.committed to true. More...
 

Private Attributes

uint8_t * _filters [MAX_FILTER_IDS] = {}
 List of pointers to the allocated buffers for the filters. More...
 
uint8_t _filtersCount = 0
 Number of allocated filters in the filters array. More...
 
uint16_t _masterVersion = 0
 Keeps track of the version of the filters. More...
 
uint32_t _masterCrc
 CRC over all the filter IDs and CRCs. More...
 
uint16_t _modificationInProgressCountdown = 0
 When this value is not 0, the filters are being modified. More...
 

Additional Inherited Members

- Protected Member Functions inherited from Component
virtual std::vector< Component * > getChildren ()
 Components with children can override this method to return them. More...
 
cs_ret_code_t initChildren ()
 
void setParent (Component *p)
 Children that are instantiated later can also be added individually. More...
 

Detailed Description

Keeps up the asset filters.

  • Stores filters in flash, and reads them on init.
  • Allocates RAM for the filters.
  • Handles commands that modify the filters.
  • Keeps up the master version and CRC.
  • Keeps up "modification in progress".

Member Function Documentation

◆ allocateFilter()

uint8_t * AssetFilterStore::allocateFilter ( uint8_t  filterId,
size_t  stateDataSize 
)
private

Allocates RAM for a filter of given size, and adds it to the filters array.

  • Does NOT check if filterId is already in the list.
  • Adds size of runtime data.
  • Checks max filters (MAX_FILTER_IDS).
  • Checks max ram size (FILTER_BUFFER_SIZE).
Parameters
[in]filterIdID of the filter.
[in]stateDataSizeSize of the filter data used in State, result of getStateSize(filter data size).

Returns nullptr on failure, pointer to the buffer on success.

◆ commit()

cs_ret_code_t AssetFilterStore::commit ( uint16_t  masterVersion,
uint32_t  masterCrc,
bool  store 
)
private

Commit current filters.

  • Checks the filter data validity.
  • Computes CRCs and checks it against the given CRC.

When all checks pass:

  • Stores the filters, if store == true.
  • Marks filters as committed.
  • Unsets "modification in progress".
  • Sets master version.

◆ computeFilterCrcs()

void AssetFilterStore::computeFilterCrcs ( )
private

Computes the CRC of the filters, and sets it in the filter.

  • Skips filters of which the CRC have been calculated before (flags.crcCalculated == true).
  • Sets flags.crcCalculated to true.

◆ computeMasterCrc()

uint32_t AssetFilterStore::computeMasterCrc ( )
private

Calculate the master CRC and return it (not stored).

Assumes filter CRCs are already calculated.

◆ deallocateFilter()

bool AssetFilterStore::deallocateFilter ( uint8_t  filterId)
private

Same as deallocateFilterByIndex, but looks up the filter by the filterId.

Returns true when id is found and filter is deallocated, false else.

◆ deallocateFilterByIndex()

void AssetFilterStore::deallocateFilterByIndex ( uint8_t  parsingFilterIndex)
private

Deallocates the filter at given index in the filters array.

If a gap is created in the array, this method moves all filters with an index above the given one down one index to close this gap.

◆ endInProgress()

void AssetFilterStore::endInProgress ( uint16_t  newMasterVersion,
uint32_t  newMasterCrc 
)
private

To be called when filters are no longer being modified.

◆ findFilter()

uint8_t * AssetFilterStore::findFilter ( uint8_t  filterId)
private

Returns a pointer to the filter with given filterId, or nullptr if not found.

◆ findFilterIndex()

std::optional< uint8_t > AssetFilterStore::findFilterIndex ( uint8_t  filterId)

Returns the index of the filter with given filterId, if any.

◆ getFilter()

AssetFilter AssetFilterStore::getFilter ( uint8_t  index)

Get the Nth filter.

◆ getFilterCount()

uint8_t AssetFilterStore::getFilterCount ( )

Get the number of filters.

◆ getMasterCrc()

uint32_t AssetFilterStore::getMasterCrc ( )

Get the current master CRC.

◆ getMasterVersion()

uint16_t AssetFilterStore::getMasterVersion ( )

Get the current master version.

◆ getNthStateType()

CS_TYPE AssetFilterStore::getNthStateType ( uint8_t  index)
private

Used to loop over all asset filter state types.

Returns CONFIG_DO_NOT_USE for invalid index.

◆ getStateSize()

uint16_t AssetFilterStore::getStateSize ( uint16_t  filterDataSize)
private

Get the size required for State, given the filter data size.

◆ getStateType()

CS_TYPE AssetFilterStore::getStateType ( uint16_t  filterDataSize)
private

Get the State type, given the filter data size.

Returns CONFIG_DO_NOT_USE for invalid size.

◆ getTotalHeapAllocatedSize()

size_t AssetFilterStore::getTotalHeapAllocatedSize ( )
private

Returns the total amount of heap allocated for the filters.

◆ handleCommitFilterChangesCommand()

cs_ret_code_t AssetFilterStore::handleCommitFilterChangesCommand ( const asset_filter_cmd_commit_filter_changes_t cmdData)
private

Commit the filters.

  • Computes CRCs and checks it against the given CRC.
  • Checks the filter data validity.
  • Unsets "modification in progress" when all checks are passed.
  • Sets master version when all checks are passed.
Returns
ERR_PROTOCOL_UNSUPPORTED For an invalid protocol version.
ERR_WRONG_STATE When a filter is invalid.
ERR_MISMATCH When the computed master CRC is different from given master CRC.
ERR_SUCCESS On success.

◆ handleEvent()

void AssetFilterStore::handleEvent ( event_t evt)
virtual

Internal usage.

Implements EventListener.

◆ handleGetFilterSummariesCommand()

void AssetFilterStore::handleGetFilterSummariesCommand ( cs_result_t result)
private

Writes the filter summary in the result.

◆ handleRemoveFilterCommand()

cs_ret_code_t AssetFilterStore::handleRemoveFilterCommand ( const asset_filter_cmd_remove_filter_t cmdData)
private

Removes given filter immediately.

Flags this crownstone as 'filter modification in progress'.

Returns
ERR_PROTOCOL_UNSUPPORTED For an invalid protocol version.
ERR_SUCCESS When the filter is removed.
ERR_SUCCESS_NO_CHANGE When the filter was already removed.

◆ handleUploadFilterCommand()

cs_ret_code_t AssetFilterStore::handleUploadFilterCommand ( const asset_filter_cmd_upload_filter_t cmdData)
private

Handle an upload command.

Allocates filter if not already done. Removes existing filter if it was committed. TOOD: remove existing filter if total size is different?

Returns
ERR_PROTOCOL_UNSUPPORTED For an invalid protocol version.
ERR_INVALID_MESSAGE When the data would go outside the total size.
ERR_WRONG_STATE When the existing, non-commited, filter is of different size.
ERR_NO_SPACE When there is no space for this filter.
ERR_SUCCESS On success.

◆ init()

cs_ret_code_t AssetFilterStore::init ( )
virtual

set _filterModificationInProgress to false.

Reimplemented from Component.

◆ inProgressTimeout()

void AssetFilterStore::inProgressTimeout ( )
private

Called when the modification in progress is timed out.

◆ isInProgress()

bool AssetFilterStore::isInProgress ( )

Whether changes are in progress.

Meaning someone is actively modifying the filters, and a commit is expected to be done soon.

◆ isReady()

bool AssetFilterStore::isReady ( )

Returns true if this object is ready to be used.

I.e. the store is not in progress of updates and master version is non-zero.

◆ loadFromFlash() [1/2]

void AssetFilterStore::loadFromFlash ( )
private

Load filters and version from flash.

◆ loadFromFlash() [2/2]

void AssetFilterStore::loadFromFlash ( CS_TYPE  type)
private

Load filters of a specific type (size) from flash.

◆ markFiltersCommitted()

void AssetFilterStore::markFiltersCommitted ( )
private

Sets flags.committed to true.

◆ onTick()

void AssetFilterStore::onTick ( )
private

◆ sendInProgressStatus()

void AssetFilterStore::sendInProgressStatus ( )
private

Send an internal event when isInProgress() may have changed.

◆ startInProgress()

void AssetFilterStore::startInProgress ( )
private

To be called when about to modify filters.

  • Sets master version to 0.

◆ storeFilters()

void AssetFilterStore::storeFilters ( )
private

Store filters to flash.

  • Skips filters that have already been committed.

◆ validateFilters()

bool AssetFilterStore::validateFilters ( )
private

Checks for all filters if the allocated filter data size is equal to the computed size based on its contents.

  • Skips filters that have already been committed.
  • Deallocates any filters failing the check.
Returns
true When all filters passed the check.

Member Data Documentation

◆ _filters

uint8_t* AssetFilterStore::_filters[MAX_FILTER_IDS] = {}
private

List of pointers to the allocated buffers for the filters.

The filters in this array are always sorted by filterId. Null pointers will always be at the back of the array, so you can stop iterating when you encounter a null pointer.

To access a filter, construct an accessor object of type TrackingFilter for the given buffer.

◆ _filtersCount

uint8_t AssetFilterStore::_filtersCount = 0
private

Number of allocated filters in the filters array.

◆ _masterCrc

uint32_t AssetFilterStore::_masterCrc
private

CRC over all the filter IDs and CRCs.

This is updated by the commit command if it matches.

◆ _masterVersion

uint16_t AssetFilterStore::_masterVersion = 0
private

Keeps track of the version of the filters.

When this value is 0, the filters are invalid.

◆ _modificationInProgressCountdown

uint16_t AssetFilterStore::_modificationInProgressCountdown = 0
private

When this value is not 0, the filters are being modified.

Reduced by 1 every tick.

◆ FILTER_BUFFER_SIZE

constexpr size_t AssetFilterStore::FILTER_BUFFER_SIZE = 520
staticconstexpr

Max total size that the filters take up in RAM.

Set to: TypeSize(CS_TYPE::STATE_ASSET_FILTER_512) + sizeof(asset_filter_runtime_data_t)

◆ MAX_FILTER_IDS

constexpr uint8_t AssetFilterStore::MAX_FILTER_IDS = 8
staticconstexpr

Max number of filters.

◆ MODIFICATION_IN_PROGRESS_TIMEOUT_SECONDS

constexpr int AssetFilterStore::MODIFICATION_IN_PROGRESS_TIMEOUT_SECONDS = 20
staticconstexpr

Time after last edit command (upload, remove), until "modification in progress" times out.


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