Bluenet  5.7.0
Bluenet, firmware for nRF52 smart home devices
Loading...
Searching...
No Matches
cs_BehaviourStore.h
Go to the documentation of this file.
1/*
2 * Author: Crownstone Team
3 * Copyright: Crownstone (https://crownstone.rocks)
4 * Date: Sep 23, 2019
5 * License: LGPLv3+, Apache License 2.0, and/or MIT (triple-licensed)
6 */
7
8#pragma once
9
13#include <common/cs_Component.h>
16#include <test/cs_TestAccess.h>
17
18#include <array>
19#include <optional>
20#include <vector>
21
25class BehaviourStore : public EventListener, public Component {
26 friend class TestAccess<BehaviourStore>;
27
28public:
29 static constexpr size_t MaxBehaviours = 50;
30
31private:
32 std::array<Behaviour*, MaxBehaviours> activeBehaviours = {};
33
34public:
35 // delete copy and move constructor to prevent double deletions etc.
37 BehaviourStore(BehaviourStore&& other) = delete;
38 // default is not implicitly generated when copy constructors are deleted.
39 BehaviourStore() = default;
40
44 virtual void handleEvent(event_t& evt);
45
46 /*****************************
47 * NOTE: to loop over a specific type of behaviours simply do:
48 *
49 * for(auto b : getActiveBehaviours()){
50 * if(auto switchbehave = dynamic_cast<SwitchBehaviour*>(b)){
51 * // work with switchbehave
52 * }
53 * }
54 */
55 inline std::array<Behaviour*, MaxBehaviours>& getActiveBehaviours() { return activeBehaviours; }
56
60 cs_ret_code_t init() override;
61
62 virtual ~BehaviourStore();
63
73
80 ErrorCodesGeneral replaceBehaviour(uint8_t index, Behaviour* behaviour);
81
82 Behaviour* getBehaviour(uint8_t index);
83
87 uint8_t findEmptyIndex();
88
89private:
100 ErrorCodesGeneral addBehaviour(uint8_t* buf, cs_buffer_size_t bufSize, uint8_t& index);
101
107
114
119
124
132
143
144 // utilities
148 void storeUpdate(uint8_t index, SwitchBehaviour::Type type, uint8_t* buf, cs_buffer_size_t bufSize);
152 void storeUpdate(uint8_t index, Behaviour* behaviour);
153
162 Behaviour* allocateBehaviour(uint8_t index, SwitchBehaviour::Type type, uint8_t* buf, cs_buffer_size_t bufSize);
163
167 void assignBehaviour(uint8_t index, Behaviour* behaviour);
168
175
180
182
183 // checks intermediate state of handleReplaceBehaviour for consistency.
185
186 // loads the behaviours from state into the 'activeBehaviours' array.
187 // BehaviourType must match BehaviourCsType.
188 template <class BehaviourType>
189 void LoadBehavioursFromMemory(CS_TYPE BehaviourCsType);
190};
Describes a recent change event of the behaviour store.
Definition: cs_BehaviourMutation.h:14
Keeps track of the behaviours that are active on this crownstone.
Definition: cs_BehaviourStore.h:25
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.
ErrorCodesGeneral removeBehaviour(uint8_t index)
Remove the behaviour at [index].
virtual void handleEvent(event_t &evt)
handles events concerning updates of the active behaviours on this crownstone.
BehaviourMutation handleSaveBehaviour(event_t &evt)
Deserializes the behaviour from event data, and allocate new instance of correct type on the heap.
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: E...
void assignBehaviour(uint8_t index, Behaviour *behaviour)
Assign it to activateBehaviour[index] and print it.
void LoadBehavioursFromMemory(CS_TYPE BehaviourCsType)
BehaviourMutation handleReplaceBehaviour(event_t &evt)
Deserializes the behaviour from event data, and allocate new instance of correct type on the heap.
std::array< Behaviour *, MaxBehaviours > & getActiveBehaviours()
Definition: cs_BehaviourStore.h:55
uint8_t findEmptyIndex()
returns MaxBehaviours if not found.
ErrorCodesGeneral replaceBehaviour(uint8_t index, Behaviour *behaviour)
Add behaviour to the ActiveBehaviours if there is space.
uint32_t calculateMasterHash()
Calculate the hash over all behaviours.
void handleGetBehaviourIndices(event_t &evt)
void handleGetBehaviour(event_t &evt)
cs_ret_code_t init() override
Initialize store from flash.
Behaviour * getBehaviour(uint8_t index)
void dispatchBehaviourMutationEvent(BehaviourMutation mutation)
void clearActiveBehavioursArray()
Clear all behaviours - usefull for a clean start of the test suite.
size_t getBehaviourSize(SwitchBehaviour::Type type)
returns 0 for unknown types, else the obvious.
void storeUpdate(uint8_t index, Behaviour *behaviour)
call state store for the given behaviour and update masterhash.
void storeMasterHash()
Calculate master hash and store it in State.
static constexpr size_t MaxBehaviours
Definition: cs_BehaviourStore.h:29
ErrorCodesGeneral addBehaviour(Behaviour *behaviour)
Add behaviour to the ActiveBehaviours if there is space.
std::array< Behaviour *, MaxBehaviours > activeBehaviours
Definition: cs_BehaviourStore.h:32
BehaviourStore(BehaviourStore &&other)=delete
ErrorCodesGeneral addBehaviour(uint8_t *buf, cs_buffer_size_t bufSize, uint8_t &index)
Add behaviour to a new index.
BehaviourMutation handleRemoveBehaviour(event_t &evt)
ErrorCodesGeneral replaceParameterValidation(event_t &evt, uint8_t index, SwitchBehaviour::Type type)
BehaviourStore(BehaviourStore &other)=delete
BehaviourStore()=default
Behaviour * allocateBehaviour(uint8_t index, SwitchBehaviour::Type type, uint8_t *buf, cs_buffer_size_t bufSize)
Heap allocate an instance of given type from the buffer.
virtual ~BehaviourStore()
Class to derrive behaviours from, centralizing common variables such as from and until times.
Definition: cs_Behaviour.h:20
Type
Definition: cs_Behaviour.h:22
Helper class to manage decoupling of components.
Definition: cs_Component.h:35
Event listener.
Definition: cs_EventListener.h:17
Definition: cs_TestAccess.h:11
Author: Crownstone Team Copyright: Crownstone (https://crownstone.rocks) Date: Oct 9,...
Definition: cs_Event.h:26
ErrorCodesGeneral
Author: Crownstone Team Copyright: Crownstone (https://crownstone.rocks) Date: Jun 17,...
Definition: cs_ErrorCodes.h:9
uint16_t cs_buffer_size_t
Definition: cs_Typedefs.h:20
uint16_t cs_ret_code_t
Definition: cs_Typedefs.h:21
CS_TYPE
Types:
Definition: cs_Types.h:79