Bluenet  5.7.0
Bluenet, firmware for nRF52 smart home devices
Loading...
Searching...
No Matches
cs_Storage.h
Go to the documentation of this file.
1/*
2 * Author: Crownstone Team
3 * Copyright: Crownstone (https://crownstone.rocks)
4 * Date: 24 Nov., 2014
5 * License: LGPLv3+, Apache License 2.0, and/or MIT (triple-licensed)
6 */
7#pragma once
8
9#include <ble/cs_Nordic.h>
10#include <common/cs_Types.h>
11#include <components/libraries/fds/fds.h>
13#include <util/cs_Utils.h>
14#include <test/cs_TestAccess.h>
15
16#include <vector>
17
24};
25
27
57class Storage {
58 friend class TestAccess<Storage>;
59public:
64 static Storage& getInstance() {
65 static Storage instance;
66 return instance;
67 }
68
70
71 inline bool isInitialized() { return _initialized; }
72 bool isBusy();
73
82
96
112
126
137
151
168
183
197
210
223
235
244
254
267 cs_ret_code_t erasePages(const CS_TYPE doneEvent, void* startAddress, void* endAddress);
268
275 uint8_t* allocate(size16_t& size);
276
280 void handleFileStorageEvent(fds_evt_t const* p_fds_evt);
281
286
291
292private:
293 Storage() = default;
294 Storage(Storage const&) = delete;
295 void operator=(Storage const&) = delete;
296
297 bool _initialized = false;
298 bool _registeredFds = false;
300
301 fds_find_token_t _findToken;
302 CS_TYPE _currentSearchType = CS_TYPE::CONFIG_DO_NOT_USE;
303
304 bool _collectingGarbage = false;
305 bool _removingFile = false;
307 std::vector<uint16_t> _busyRecordKeys;
308
312 uint32_t _erasePage = 0;
313
317 uint32_t _eraseEndPage = 0;
318
319 CS_TYPE _eraseDoneEvent = CS_TYPE::CONFIG_DO_NOT_USE;
320
324 cs_ret_code_t findNextInternal(uint16_t recordKey, uint16_t& fileId);
325
329 cs_ret_code_t readNextInternal(uint16_t recordKey, uint16_t& fileId, uint8_t* buf, uint16_t size);
330
336 cs_ret_code_t readRecord(fds_record_desc_t recordDesc, uint8_t* buf, uint16_t size, uint16_t& fileId);
337
341
343
345
350
359
364
368 uint16_t getFileId(cs_state_id_t valueId);
369
373 cs_state_id_t getStateId(uint16_t fileId);
374
375 bool isValidRecordKey(uint16_t recordKey);
376 bool isValidFileId(uint16_t fileId);
377
381 void initSearch(CS_TYPE type);
382
389
390 // ret_code_t exists(cs_file_id_t fileId, uint16_t recordKey, bool & result);
391
400 ret_code_t exists(cs_file_id_t file_id, uint16_t recordKey, fds_record_desc_t& record_desc, bool& result);
401
402 void setBusy(uint16_t recordKey);
403 void clearBusy(uint16_t recordKey);
404 bool isBusy(uint16_t recordKey);
405
406
408
409 void handleWriteEvent(fds_evt_t const* p_fds_evt);
410 void handleRemoveRecordEvent(fds_evt_t const* p_fds_evt);
411 void handleRemoveFileEvent(fds_evt_t const* p_fds_evt);
412 void handleGarbageCollectionEvent(fds_evt_t const* p_fds_evt);
413};
Class to store items persistently in flash (persistent) memory.
Definition: cs_Storage.h:57
CS_TYPE _currentSearchType
Definition: cs_Storage.h:302
bool isErasingPages()
bool isBusy()
cs_storage_error_callback_t _errorCallback
Definition: cs_Storage.h:299
ret_code_t writeInternal(const cs_state_data_t &data)
Write to persistent storage.
cs_ret_code_t readNext(cs_state_data_t &data)
Find and read next stored value of given type.
uint32_t _eraseEndPage
Page that should not be erased.
Definition: cs_Storage.h:317
cs_ret_code_t factoryReset()
Perform factory reset.
void handleRemoveRecordEvent(fds_evt_t const *p_fds_evt)
bool _initialized
Definition: cs_Storage.h:297
cs_ret_code_t readNextInternal(uint16_t recordKey, uint16_t &fileId, uint8_t *buf, uint16_t size)
Read next fileId for given recordKey.
void handleWriteEvent(fds_evt_t const *p_fds_evt)
static Storage & getInstance()
Returns the singleton instance of this class.
Definition: cs_Storage.h:64
cs_ret_code_t readRecord(fds_record_desc_t recordDesc, uint8_t *buf, uint16_t size, uint16_t &fileId)
Read a record: copy data to buffer, and sets fileId.
bool _collectingGarbage
Definition: cs_Storage.h:304
cs_ret_code_t remove(CS_TYPE type, cs_state_id_t id)
Remove value of given type and id.
bool _removingFile
Definition: cs_Storage.h:305
cs_ret_code_t write(const cs_state_data_t &data)
Write to persistent storage.
Storage()=default
uint32_t _erasePage
Next page to erase.
Definition: cs_Storage.h:312
std::vector< uint16_t > _busyRecordKeys
Definition: cs_Storage.h:307
void setErrorCallback(cs_storage_error_callback_t callback)
Set the callback for errors.
cs_ret_code_t continueFactoryReset()
Continue the factory reset process.
void clearBusy(uint16_t recordKey)
void handleRemoveFileEvent(fds_evt_t const *p_fds_evt)
cs_state_id_t getStateId(uint16_t fileId)
Get state value id, given file id.
fds_find_token_t _findToken
Definition: cs_Storage.h:301
cs_ret_code_t garbageCollect()
Garbage collection reclaims the flash space that is occupied by records that have been deleted,...
cs_ret_code_t findNextInternal(uint16_t recordKey, uint16_t &fileId)
Find next fileId for given recordKey.
cs_ret_code_t read(cs_state_data_t &data)
Find and read stored value of given type and id.
cs_ret_code_t readFirst(cs_state_data_t &data)
Find and read from first stored value of given type.
ret_code_t exists(cs_file_id_t file_id, uint16_t recordKey, fds_record_desc_t &record_desc, bool &result)
Check if a type of record exists and return the record descriptor.
uint16_t getFileId(cs_state_id_t valueId)
Get file id, given state value id.
uint8_t * allocate(size16_t &size)
Allocate ram that is correctly aligned and padded.
void handleGarbageCollectionEvent(fds_evt_t const *p_fds_evt)
bool isInitialized()
Definition: cs_Storage.h:71
cs_ret_code_t remove(CS_TYPE type)
Remove all values of a type.
void setBusy(uint16_t recordKey)
cs_ret_code_t readV3ResetCounter(cs_state_data_t &data)
Read the old (v3) reset counter.
bool isValidFileId(uint16_t fileId)
void initSearch(CS_TYPE type)
Start a new search, where the user wants to iterate over a certain type.
bool isBusy(uint16_t recordKey)
cs_ret_code_t findFirst(CS_TYPE type, cs_state_id_t &id)
Find first id of stored values of given type.
void handleFileStorageEvent(fds_evt_t const *p_fds_evt)
Handle FDS events.
CS_TYPE _eraseDoneEvent
Definition: cs_Storage.h:319
void operator=(Storage const &)=delete
cs_ret_code_t init()
cs_ret_code_t eraseAllPages()
Erase all flash pages used by FDS.
cs_ret_code_t erasePages(const CS_TYPE doneEvent, void *startAddress, void *endAddress)
Erase flash pages.
bool _registeredFds
Definition: cs_Storage.h:298
void handleFlashOperationSuccess()
Handle FDS SOC event NRF_EVT_FLASH_OPERATION_SUCCESS.
bool _performingFactoryReset
Definition: cs_Storage.h:306
cs_ret_code_t remove(cs_state_id_t id)
Remove all values with given id.
ret_code_t garbageCollectInternal()
void handleFlashOperationError()
Handle FDS SOC event NRF_EVT_FLASH_OPERATION_ERROR.
Storage(Storage const &)=delete
void initSearch()
Start a new search.
cs_ret_code_t getErrorCode(ret_code_t code)
cs_ret_code_t findNext(CS_TYPE type, cs_state_id_t &id)
Find next id of stored values of given type.
size16_t getPaddedSize(size16_t size)
Returns size after padding for flash.
bool isValidRecordKey(uint16_t recordKey)
void eraseNextPage()
Erase next page, started via eraseAllPages().
Definition: cs_TestAccess.h:11
uint32_t ret_code_t
Author: Crownstone Team Copyright: Crownstone (https://crownstone.rocks) Date: Apr.
Definition: cs_Nordic.h:14
void(* cs_storage_error_callback_t)(cs_storage_operation_t operation, CS_TYPE type, cs_state_id_t id)
Definition: cs_Storage.h:26
cs_storage_operation_t
Definition: cs_Storage.h:18
@ CS_STORAGE_OP_GC
Definition: cs_Storage.h:23
@ CS_STORAGE_OP_REMOVE
Definition: cs_Storage.h:21
@ CS_STORAGE_OP_WRITE
Definition: cs_Storage.h:20
@ CS_STORAGE_OP_READ
Definition: cs_Storage.h:19
@ CS_STORAGE_OP_REMOVE_ALL_VALUES_WITH_ID
Definition: cs_Storage.h:22
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
CS_TYPE
Types:
Definition: cs_Types.h:79
uint16_t cs_file_id_t
Definition: cs_Types.h:458
Author: Crownstone Team Copyright: Crownstone (https://crownstone.rocks) Date: Oct 9,...
Definition: cs_StateData.h:22