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

Keeps track of the behaviours that are active on this crownstone. More...

#include <cs_BehaviourStore.h>

Inheritance diagram for BehaviourStore:
Collaboration diagram for BehaviourStore:

Public Member Functions

 BehaviourStore (BehaviourStore &other)=delete
 
 BehaviourStore (BehaviourStore &&other)=delete
 
 BehaviourStore ()=default
 
virtual void handleEvent (event_t &evt)
 handles events concerning updates of the active behaviours on this crownstone. More...
 
std::array< Behaviour *, MaxBehaviours > & getActiveBehaviours ()
 
cs_ret_code_t init () override
 Initialize store from flash. More...
 
virtual ~BehaviourStore ()
 
ErrorCodesGeneral addBehaviour (Behaviour *behaviour)
 Add behaviour to the ActiveBehaviours if there is space. More...
 
ErrorCodesGeneral replaceBehaviour (uint8_t index, Behaviour *behaviour)
 Add behaviour to the ActiveBehaviours if there is space. More...
 
BehaviourgetBehaviour (uint8_t index)
 
uint8_t findEmptyIndex ()
 returns MaxBehaviours if not found. 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 size_t MaxBehaviours = 50
 

Private Member Functions

ErrorCodesGeneral addBehaviour (uint8_t *buf, cs_buffer_size_t bufSize, uint8_t &index)
 Add behaviour to a new index. More...
 
ErrorCodesGeneral removeBehaviour (uint8_t index)
 Remove the behaviour at [index]. More...
 
void clearActiveBehavioursArray ()
 Clear all behaviours - usefull for a clean start of the test suite. More...
 
uint32_t calculateMasterHash ()
 Calculate the hash over all behaviours. More...
 
void storeMasterHash ()
 Calculate master hash and store it in State. More...
 
BehaviourMutation handleSaveBehaviour (event_t &evt)
 Deserializes the behaviour from event data, and allocate new instance of correct type on the heap. More...
 
BehaviourMutation handleReplaceBehaviour (event_t &evt)
 Deserializes the behaviour from event data, and allocate new instance of correct type on the heap. More...
 
BehaviourMutation handleRemoveBehaviour (event_t &evt)
 
void handleGetBehaviour (event_t &evt)
 
void handleGetBehaviourIndices (event_t &evt)
 
void storeUpdate (uint8_t index, SwitchBehaviour::Type type, uint8_t *buf, cs_buffer_size_t bufSize)
 call state store for the given switch type and update masterhash. More...
 
void storeUpdate (uint8_t index, Behaviour *behaviour)
 call state store for the given behaviour and update masterhash. More...
 
BehaviourallocateBehaviour (uint8_t index, SwitchBehaviour::Type type, uint8_t *buf, cs_buffer_size_t bufSize)
 Heap allocate an instance of given type from the buffer. More...
 
void assignBehaviour (uint8_t index, Behaviour *behaviour)
 Assign it to activateBehaviour[index] and print it. More...
 
ErrorCodesGeneral checkSizeAndType (SwitchBehaviour::Type type, cs_buffer_size_t bufSize)
 If type is not a valid behaviour type: ERR_WRONG_PARAMETER If bufSize does not match expected size: ERR_WRONG_PAYLOAD_LENGTH Else: ERR_SUCCESS. More...
 
size_t getBehaviourSize (SwitchBehaviour::Type type)
 returns 0 for unknown types, else the obvious. More...
 
void dispatchBehaviourMutationEvent (BehaviourMutation mutation)
 
ErrorCodesGeneral replaceParameterValidation (event_t &evt, uint8_t index, SwitchBehaviour::Type type)
 
template<class BehaviourType >
void LoadBehavioursFromMemory (CS_TYPE BehaviourCsType)
 

Private Attributes

std::array< Behaviour *, MaxBehavioursactiveBehaviours = {}
 

Friends

class TestAccess< BehaviourStore >
 

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 track of the behaviours that are active on this crownstone.

Constructor & Destructor Documentation

◆ BehaviourStore() [1/3]

◆ BehaviourStore() [2/3]

◆ BehaviourStore() [3/3]

◆ ~BehaviourStore()

virtual BehaviourStore::~BehaviourStore ( )
virtual

Member Function Documentation

◆ addBehaviour() [1/2]

ErrorCodesGeneral BehaviourStore::addBehaviour ( Behaviour behaviour)

Add behaviour to the ActiveBehaviours if there is space.

If null, nothing happens.

The object pointed to by behaviour must to be allocated on the heap. Its lifetime will be managed by BehaviourStore and will be destroyed and deleted if/when necessary, for example when replaceBehaviour is called.

◆ addBehaviour() [2/2]

ErrorCodesGeneral BehaviourStore::addBehaviour ( uint8_t *  buf,
cs_buffer_size_t  bufSize,
uint8_t &  index 
)
private

Add behaviour to a new index.

Does not check if similar behaviour already exists.

Parameters
[in]bufBuffer with the behaviour.
[in]bufSizeSize of the buffer.
[out]indexIndex at which the behaviour was added, only set on success.
Returns
error code.

◆ allocateBehaviour()

Behaviour * BehaviourStore::allocateBehaviour ( uint8_t  index,
SwitchBehaviour::Type  type,
uint8_t *  buf,
cs_buffer_size_t  bufSize 
)
private

Heap allocate an instance of given type from the buffer.

Notes:

  • If bufSize is less than the WireFormat::size for the given type, a default constructed behaviour is allocated.
  • The index is not checked for preexisting data. That must be deleted before using this function.

◆ assignBehaviour()

void BehaviourStore::assignBehaviour ( uint8_t  index,
Behaviour behaviour 
)
private

Assign it to activateBehaviour[index] and print it.

◆ calculateMasterHash()

uint32_t BehaviourStore::calculateMasterHash ( )
private

Calculate the hash over all behaviours.

◆ checkSizeAndType()

ErrorCodesGeneral BehaviourStore::checkSizeAndType ( SwitchBehaviour::Type  type,
cs_buffer_size_t  bufSize 
)
private

If type is not a valid behaviour type: ERR_WRONG_PARAMETER If bufSize does not match expected size: ERR_WRONG_PAYLOAD_LENGTH Else: ERR_SUCCESS.

◆ clearActiveBehavioursArray()

void BehaviourStore::clearActiveBehavioursArray ( )
private

Clear all behaviours - usefull for a clean start of the test suite.

Does not edit the persisted values, so expect discrepancies with flash until all behaviours in flash are overwritten.

◆ dispatchBehaviourMutationEvent()

void BehaviourStore::dispatchBehaviourMutationEvent ( BehaviourMutation  mutation)
private

◆ findEmptyIndex()

uint8_t BehaviourStore::findEmptyIndex ( )

returns MaxBehaviours if not found.

◆ getActiveBehaviours()

std::array< Behaviour *, MaxBehaviours > & BehaviourStore::getActiveBehaviours ( )
inline

◆ getBehaviour()

Behaviour * BehaviourStore::getBehaviour ( uint8_t  index)

◆ getBehaviourSize()

size_t BehaviourStore::getBehaviourSize ( SwitchBehaviour::Type  type)
private

returns 0 for unknown types, else the obvious.

◆ handleEvent()

virtual void BehaviourStore::handleEvent ( event_t evt)
virtual

handles events concerning updates of the active behaviours on this crownstone.

Implements EventListener.

◆ handleGetBehaviour()

void BehaviourStore::handleGetBehaviour ( event_t evt)
private

◆ handleGetBehaviourIndices()

void BehaviourStore::handleGetBehaviourIndices ( event_t evt)
private

◆ handleRemoveBehaviour()

BehaviourMutation BehaviourStore::handleRemoveBehaviour ( event_t evt)
private

◆ handleReplaceBehaviour()

BehaviourMutation BehaviourStore::handleReplaceBehaviour ( event_t evt)
private

Deserializes the behaviour from event data, and allocate new instance of correct type on the heap.

Does not check for prior existence.

◆ handleSaveBehaviour()

BehaviourMutation BehaviourStore::handleSaveBehaviour ( event_t evt)
private

Deserializes the behaviour from event data, and allocate new instance of correct type on the heap.

Checks if an identical behaviour already exists. (I.e. serialized equality.)

◆ init()

cs_ret_code_t BehaviourStore::init ( )
overridevirtual

Initialize store from flash.

Reimplemented from Component.

◆ LoadBehavioursFromMemory()

template<class BehaviourType >
void BehaviourStore::LoadBehavioursFromMemory ( CS_TYPE  BehaviourCsType)
private

◆ removeBehaviour()

ErrorCodesGeneral BehaviourStore::removeBehaviour ( uint8_t  index)
private

Remove the behaviour at [index].

If [index] is out of bounds, or no behaviour exists at [index], false is returned. Else, true.

◆ replaceBehaviour()

ErrorCodesGeneral BehaviourStore::replaceBehaviour ( uint8_t  index,
Behaviour behaviour 
)

Add behaviour to the ActiveBehaviours if there is space.

If null, previous behaviour will be deleted. This method takes ownership over the allocated resource (removeBehaviour may delete it.)

◆ replaceParameterValidation()

ErrorCodesGeneral BehaviourStore::replaceParameterValidation ( event_t evt,
uint8_t  index,
SwitchBehaviour::Type  type 
)
private

◆ storeMasterHash()

void BehaviourStore::storeMasterHash ( )
private

Calculate master hash and store it in State.

◆ storeUpdate() [1/2]

void BehaviourStore::storeUpdate ( uint8_t  index,
Behaviour behaviour 
)
private

call state store for the given behaviour and update masterhash.

◆ storeUpdate() [2/2]

void BehaviourStore::storeUpdate ( uint8_t  index,
SwitchBehaviour::Type  type,
uint8_t *  buf,
cs_buffer_size_t  bufSize 
)
private

call state store for the given switch type and update masterhash.

Friends And Related Function Documentation

◆ TestAccess< BehaviourStore >

friend class TestAccess< BehaviourStore >
friend

Member Data Documentation

◆ activeBehaviours

std::array<Behaviour*, MaxBehaviours> BehaviourStore::activeBehaviours = {}
private

◆ MaxBehaviours

constexpr size_t BehaviourStore::MaxBehaviours = 50
staticconstexpr

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