Bluenet  5.7.0
Bluenet, firmware for nRF52 smart home devices
Loading...
Searching...
No Matches
cs_State.h
Go to the documentation of this file.
1/*
2 * Author: Crownstone Team
3 * Copyright: Crownstone (https://crownstone.rocks)
4 * Date: May 11, 2015
5 * License: LGPLv3+, Apache License 2.0, and/or MIT (triple-licensed)
6 */
7#pragma once
8
9#include <cfg/cs_Boards.h>
10#include <common/cs_BaseClass.h>
11#include <common/cs_Types.h>
12#include <drivers/cs_Storage.h>
13#include <drivers/cs_Timer.h>
16
17#include <vector>
18
23constexpr const char* operationModeName(OperationMode const& mode) {
24 switch (mode) {
25 case OperationMode::OPERATION_MODE_SETUP: return "SETUP";
26 case OperationMode::OPERATION_MODE_DFU: return "DFU";
28 case OperationMode::OPERATION_MODE_NORMAL: return "NORMAL";
30 }
31 // should never be reached
32 return "UNKNOWN";
33}
34
35constexpr int ValidMode(OperationMode const& mode) {
36 switch (mode) {
42 }
43 return 0;
44}
45
46// TODO: what to do when for invalid operation mode?
47// TODO: how can we use TYPIFY() as parameter type?
48constexpr OperationMode getOperationMode(uint8_t mode) {
49 OperationMode opMode = static_cast<OperationMode>(mode);
50 if (ValidMode(opMode)) {
51 return opMode;
52 }
54}
55
57
58#define FACTORY_RESET_STATE_NORMAL 0
59#define FACTORY_RESET_STATE_LOWTX 1
60#define FACTORY_RESET_STATE_RESET 2
61
67};
68
79struct __attribute__((__packed__)) cs_state_store_queue_t {
83 uint32_t counter; // Uint32, so it can fit 24h.
84 uint32_t init_counter;
85 bool execute;
86};
87
88const uint32_t CS_STATE_QUEUE_DELAY_SECONDS_MAX = 0xFFFFFFFF / 1000;
89
92 std::vector<cs_state_id_t>* ids;
93 cs_id_list_t(CS_TYPE type, std::vector<cs_state_id_t>* ids) : type(type), ids(ids) {}
94};
95
145class State : public BaseClass<>, EventListener {
146public:
150 static State& getInstance() {
151 static State instance;
152 return instance;
153 }
154
155 virtual ~State();
156
162 void init(boards_config_t* boardsConfig);
163
172 cs_ret_code_t get(cs_state_data_t& data, const PersistenceMode mode = PersistenceMode::STRATEGY1);
173
179 cs_ret_code_t get(const CS_TYPE type, void* value, size16_t size);
180
189 bool isTrue(CS_TYPE type, const PersistenceMode mode = PersistenceMode::STRATEGY1);
190
204 cs_ret_code_t getIds(CS_TYPE type, std::vector<cs_state_id_t>*& ids);
205
220 cs_ret_code_t set(const cs_state_data_t& data, PersistenceMode mode = PersistenceMode::STRATEGY1);
221
227 cs_ret_code_t set(const CS_TYPE type, void* value, size16_t size);
228
240 cs_ret_code_t setDelayed(const cs_state_data_t& data, uint8_t delay);
241
256 cs_ret_code_t setThrottled(const cs_state_data_t& data, uint32_t period);
257
265
273
282 const CS_TYPE& type, cs_state_id_t id, const PersistenceMode mode = PersistenceMode::STRATEGY1);
283
288
297
307 cs_ret_code_t getViaPointer(cs_state_data_t& data, const PersistenceMode mode = PersistenceMode::STRATEGY1);
308
319
329
334
338 void handleEvent(event_t& event);
339
340protected:
342
344
354 cs_ret_code_t findInRam(const CS_TYPE& type, cs_state_id_t id, size16_t& index_in_ram);
355
366
371
385
396
406
414 cs_ret_code_t compareWithRam(const cs_state_data_t& data, uint32_t& cmp_result);
415
425
436
450 StateQueueOp operation, const CS_TYPE& type, cs_state_id_t id, uint32_t delayMs, const StateQueueMode mode);
451
453
455
459 std::vector<cs_state_data_t> _ram_data_register;
460
464 std::vector<cs_id_list_t> _idsCache;
465
469 std::vector<cs_state_store_queue_t> _store_queue;
470
472
474
475private:
478
480 State(State const&);
481
483 void operator=(State const&);
484
486
488
495
497
505 cs_ret_code_t getIdsFromFlash(const CS_TYPE& type, std::vector<cs_state_id_t>*& ids);
506
513
518};
BaseClass.
Definition: cs_BaseClass.h:15
Event listener.
Definition: cs_EventListener.h:17
Stores state values in RAM and/or FLASH.
Definition: cs_State.h:145
cs_ret_code_t loadFromRam(cs_state_data_t &data)
Copies from ram to target buffer.
cs_ret_code_t storeInRam(const cs_state_data_t &data, size16_t &index_in_ram)
Stores state variable in ram.
cs_ret_code_t getViaPointer(cs_state_data_t &data, const PersistenceMode mode=PersistenceMode::STRATEGY1)
Get pointer to state value.
bool handleFactoryResetResult(cs_ret_code_t retCode)
Handle factory reset result.
State(State const &)
State copy constructor, singleton, thus made private.
cs_ret_code_t removeFromFlash(const CS_TYPE &type, const cs_state_id_t id)
Remove given id of given type from flash.
void startWritesToFlash()
After calling this, state is allowed to write to flash.
cs_ret_code_t findInRam(const CS_TYPE &type, cs_state_id_t id, size16_t &index_in_ram)
Find given type in ram.
cs_ret_code_t set(const CS_TYPE type, void *value, size16_t size)
Convenience function for set().
cs_state_data_t & addToRam(const CS_TYPE &type, cs_state_id_t id, size16_t size)
Adds a new state_data struct to ram.
cs_ret_code_t setDelayed(const cs_state_data_t &data, uint8_t delay)
Set the state to a new value, but delay the write to flash.
cs_ret_code_t verifySizeForSet(const cs_state_data_t &data)
Verify size of user data for setting a state.
cs_ret_code_t removeInternal(const CS_TYPE &type, cs_state_id_t id, const PersistenceMode mode)
std::vector< cs_id_list_t > _idsCache
Stores list of existing ids for certain types.
Definition: cs_State.h:464
cs_ret_code_t cleanUp()
Clean up persistent storage.
cs_ret_code_t get(cs_state_data_t &data, const PersistenceMode mode=PersistenceMode::STRATEGY1)
Get copy of a state value.
bool _startedWritingToFlash
Definition: cs_State.h:471
cs_ret_code_t get(const CS_TYPE type, void *value, size16_t size)
Convenience function for get() with id 0.
cs_ret_code_t addToQueue(StateQueueOp operation, const CS_TYPE &type, cs_state_id_t id, uint32_t delayMs, const StateQueueMode mode)
Add an operation to queue.
cs_ret_code_t verifySizeForGet(const cs_state_data_t &data)
Verify size of user data for getting a state.
void handleEvent(event_t &event)
Handle (crownstone) events.
cs_ret_code_t set(const cs_state_data_t &data, PersistenceMode mode=PersistenceMode::STRATEGY1)
Set state to new value, via copy.
void handleStorageError(cs_storage_operation_t operation, CS_TYPE type, cs_state_id_t id)
Internal usage.
cs_ret_code_t removeFromRam(const CS_TYPE &type, cs_state_id_t id)
Removed a state variable from ram.
cs_ret_code_t remId(const CS_TYPE &type, cs_state_id_t id)
Remove ID from list of cached IDs.
virtual ~State()
cs_ret_code_t storeInFlash(size16_t &index_in_ram)
Writes state variable in ram to flash.
cs_ret_code_t setViaPointer(CS_TYPE type)
Update state value.
void operator=(State const &)
Assignment operator, singleton, thus made private.
cs_ret_code_t addId(const CS_TYPE &type, cs_state_id_t id)
Add ID to list of cached IDs.
void factoryReset()
Erase all used persistent storage.
State()
State constructor, singleton, thus made private.
void delayedStoreTick()
boards_config_t * _boardsConfig
Definition: cs_State.h:343
cs_ret_code_t compareWithRam(const cs_state_data_t &data, uint32_t &cmp_result)
Check a particular value with the value currently in ram.
cs_ret_code_t getDefaultValue(cs_state_data_t &data)
std::vector< cs_state_data_t > _ram_data_register
Stores state data structs with pointers to state data.
Definition: cs_State.h:459
static State & getInstance()
Get a reference to the State object.
Definition: cs_State.h:150
cs_ret_code_t storeInRam(const cs_state_data_t &data)
Convenience function, in case you're not interested in index in ram.
void init(boards_config_t *boardsConfig)
Initialize the State object with the board configuration.
cs_ret_code_t setThrottled(const cs_state_data_t &data, uint32_t period)
Write variable to flash in a throttled mode.
cs_ret_code_t remove(const CS_TYPE &type, cs_state_id_t id, const PersistenceMode mode=PersistenceMode::STRATEGY1)
Remove a state variable.
Storage * _storage
Definition: cs_State.h:341
bool _performingFactoryReset
Definition: cs_State.h:473
cs_ret_code_t getIds(CS_TYPE type, std::vector< cs_state_id_t > *&ids)
Get a list of IDs for given type.
cs_ret_code_t setInternal(const cs_state_data_t &data, PersistenceMode mode)
cs_ret_code_t getIdsFromFlash(const CS_TYPE &type, std::vector< cs_state_id_t > *&ids)
Get and cache all IDs with given type from flash.
cs_ret_code_t allocate(cs_state_data_t &data)
bool isTrue(CS_TYPE type, const PersistenceMode mode=PersistenceMode::STRATEGY1)
Shorthand for get() for boolean data types, and id 0.
std::vector< cs_state_store_queue_t > _store_queue
Stores the queue of flash operations.
Definition: cs_State.h:469
Class to store items persistently in flash (persistent) memory.
Definition: cs_Storage.h:57
Author: Crownstone Team Copyright: Crownstone (https://crownstone.rocks) Date: Oct 9,...
Definition: cs_Event.h:26
PersistenceMode
Store values in FLASH or RAM.
Definition: cs_StateData.h:70
constexpr int ValidMode(OperationMode const &mode)
Definition: cs_State.h:35
constexpr const char * operationModeName(OperationMode const &mode)
These strings end up in the firmware, even with binary logging.
Definition: cs_State.h:23
StateQueueOp
Definition: cs_State.h:62
@ CS_STATE_QUEUE_OP_GC
Definition: cs_State.h:66
@ CS_STATE_QUEUE_OP_REM_ONE_ID_OF_TYPE
Definition: cs_State.h:64
@ CS_STATE_QUEUE_OP_WRITE
Definition: cs_State.h:63
@ CS_STATE_QUEUE_OP_FACTORY_RESET
Definition: cs_State.h:65
constexpr OperationMode getOperationMode(uint8_t mode)
Definition: cs_State.h:48
const uint32_t CS_STATE_QUEUE_DELAY_SECONDS_MAX
Definition: cs_State.h:88
StateQueueMode
Definition: cs_State.h:56
cs_storage_operation_t
Definition: cs_Storage.h:18
uint16_t size16_t
Definition: cs_Typedefs.h:25
uint8_t cs_state_id_t
Definition: cs_Typedefs.h:28
uint16_t cs_ret_code_t
Definition: cs_Typedefs.h:21
OperationMode
Definition: cs_Types.h:440
@ OPERATION_MODE_FACTORY_RESET
@ OPERATION_MODE_UNINITIALIZED
CS_TYPE
Types:
Definition: cs_Types.h:79
Board configuration.
Definition: cs_Boards.h:169
Definition: cs_State.h:90
cs_id_list_t(CS_TYPE type, std::vector< cs_state_id_t > *ids)
Definition: cs_State.h:93
std::vector< cs_state_id_t > * ids
Definition: cs_State.h:92
CS_TYPE type
Definition: cs_State.h:91
Author: Crownstone Team Copyright: Crownstone (https://crownstone.rocks) Date: Oct 9,...
Definition: cs_StateData.h:22
Struct for queuing operations.
Definition: cs_State.h:79
uint32_t counter
Definition: cs_State.h:83
uint32_t init_counter
Definition: cs_State.h:84
CS_TYPE type
Definition: cs_State.h:81
cs_state_id_t id
Definition: cs_State.h:82
StateQueueOp operation
Definition: cs_State.h:80
bool execute
Definition: cs_State.h:85