2022-11-23 08:40:38 -05:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2016-08-17 11:08:22 -04:00
|
|
|
#ifndef nvs_storage_hpp
|
|
|
|
#define nvs_storage_hpp
|
|
|
|
|
|
|
|
#include <memory>
|
2022-11-23 08:40:38 -05:00
|
|
|
#include <cstdlib>
|
2016-08-17 11:08:22 -04:00
|
|
|
#include <unordered_map>
|
|
|
|
#include "nvs.hpp"
|
|
|
|
#include "nvs_types.hpp"
|
|
|
|
#include "nvs_page.hpp"
|
|
|
|
#include "nvs_pagemanager.hpp"
|
2022-11-23 08:40:38 -05:00
|
|
|
#include "nvs_memory_management.hpp"
|
2020-04-26 20:51:31 -04:00
|
|
|
#include "partition.hpp"
|
2016-08-17 11:08:22 -04:00
|
|
|
|
|
|
|
//extern void dumpBytes(const uint8_t* data, size_t count);
|
|
|
|
|
|
|
|
namespace nvs
|
|
|
|
{
|
|
|
|
|
2022-11-23 08:40:38 -05:00
|
|
|
class Storage : public intrusive_list_node<Storage>, public ExceptionlessAllocatable
|
2016-08-17 11:08:22 -04:00
|
|
|
{
|
|
|
|
enum class StorageState : uint32_t {
|
|
|
|
INVALID,
|
|
|
|
ACTIVE,
|
|
|
|
};
|
|
|
|
|
2022-11-23 08:40:38 -05:00
|
|
|
struct NamespaceEntry : public intrusive_list_node<NamespaceEntry>, public ExceptionlessAllocatable {
|
2016-08-17 11:08:22 -04:00
|
|
|
public:
|
|
|
|
char mName[Item::MAX_KEY_LENGTH + 1];
|
|
|
|
uint8_t mIndex;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef intrusive_list<NamespaceEntry> TNamespaces;
|
|
|
|
|
2022-11-23 08:40:38 -05:00
|
|
|
struct UsedPageNode: public intrusive_list_node<UsedPageNode>, public ExceptionlessAllocatable {
|
2018-04-02 06:44:59 -04:00
|
|
|
public: Page* mPage;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef intrusive_list<UsedPageNode> TUsedPageList;
|
|
|
|
|
2022-11-23 08:40:38 -05:00
|
|
|
struct BlobIndexNode: public intrusive_list_node<BlobIndexNode>, public ExceptionlessAllocatable {
|
2018-04-02 06:44:59 -04:00
|
|
|
public:
|
|
|
|
char key[Item::MAX_KEY_LENGTH + 1];
|
|
|
|
uint8_t nsIndex;
|
2019-12-06 03:16:07 -05:00
|
|
|
uint8_t chunkCount;
|
|
|
|
VerOffset chunkStart;
|
2018-04-02 06:44:59 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef intrusive_list<BlobIndexNode> TBlobIndexList;
|
|
|
|
|
2016-08-17 11:08:22 -04:00
|
|
|
public:
|
|
|
|
~Storage();
|
|
|
|
|
2020-04-26 20:51:31 -04:00
|
|
|
Storage(Partition *partition) : mPartition(partition) {
|
|
|
|
if (partition == nullptr) {
|
|
|
|
abort();
|
|
|
|
}
|
2019-12-06 03:16:07 -05:00
|
|
|
};
|
2017-08-21 05:56:16 -04:00
|
|
|
|
2016-08-17 11:08:22 -04:00
|
|
|
esp_err_t init(uint32_t baseSector, uint32_t sectorCount);
|
|
|
|
|
2016-11-15 05:24:56 -05:00
|
|
|
bool isValid() const;
|
|
|
|
|
2016-08-17 11:08:22 -04:00
|
|
|
esp_err_t createOrOpenNamespace(const char* nsName, bool canCreate, uint8_t& nsIndex);
|
|
|
|
|
|
|
|
esp_err_t writeItem(uint8_t nsIndex, ItemType datatype, const char* key, const void* data, size_t dataSize);
|
|
|
|
|
|
|
|
esp_err_t readItem(uint8_t nsIndex, ItemType datatype, const char* key, void* data, size_t dataSize);
|
|
|
|
|
|
|
|
esp_err_t getItemDataSize(uint8_t nsIndex, ItemType datatype, const char* key, size_t& dataSize);
|
|
|
|
|
|
|
|
esp_err_t eraseItem(uint8_t nsIndex, ItemType datatype, const char* key);
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
esp_err_t writeItem(uint8_t nsIndex, const char* key, const T& value)
|
|
|
|
{
|
|
|
|
return writeItem(nsIndex, itemTypeOf(value), key, &value, sizeof(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
esp_err_t readItem(uint8_t nsIndex, const char* key, T& value)
|
|
|
|
{
|
|
|
|
return readItem(nsIndex, itemTypeOf(value), key, &value, sizeof(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t eraseItem(uint8_t nsIndex, const char* key)
|
|
|
|
{
|
2016-09-22 12:36:53 -04:00
|
|
|
return eraseItem(nsIndex, ItemType::ANY, key);
|
2016-08-17 11:08:22 -04:00
|
|
|
}
|
2019-12-06 03:16:07 -05:00
|
|
|
|
2016-09-22 12:36:53 -04:00
|
|
|
esp_err_t eraseNamespace(uint8_t nsIndex);
|
2016-09-22 09:05:47 -04:00
|
|
|
|
2020-04-26 20:51:31 -04:00
|
|
|
const Partition *getPart() const
|
|
|
|
{
|
|
|
|
return mPartition;
|
|
|
|
}
|
|
|
|
|
2017-08-21 05:56:16 -04:00
|
|
|
const char *getPartName() const
|
|
|
|
{
|
2020-04-26 20:51:31 -04:00
|
|
|
return mPartition->get_partition_name();
|
2017-08-21 05:56:16 -04:00
|
|
|
}
|
2020-04-26 20:51:31 -04:00
|
|
|
|
2018-07-02 07:10:43 -04:00
|
|
|
uint32_t getBaseSector()
|
|
|
|
{
|
|
|
|
return mPageManager.getBaseSector();
|
|
|
|
}
|
2017-08-21 05:56:16 -04:00
|
|
|
|
2018-04-02 06:44:59 -04:00
|
|
|
esp_err_t writeMultiPageBlob(uint8_t nsIndex, const char* key, const void* data, size_t dataSize, VerOffset chunkStart);
|
|
|
|
|
|
|
|
esp_err_t readMultiPageBlob(uint8_t nsIndex, const char* key, void* data, size_t dataSize);
|
|
|
|
|
2019-04-15 15:06:57 -04:00
|
|
|
esp_err_t cmpMultiPageBlob(uint8_t nsIndex, const char* key, const void* data, size_t dataSize);
|
|
|
|
|
2018-04-02 06:44:59 -04:00
|
|
|
esp_err_t eraseMultiPageBlob(uint8_t nsIndex, const char* key, VerOffset chunkStart = VerOffset::VER_ANY);
|
|
|
|
|
2016-08-21 23:46:44 -04:00
|
|
|
void debugDump();
|
2019-12-06 03:16:07 -05:00
|
|
|
|
2016-08-21 23:46:44 -04:00
|
|
|
void debugCheck();
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2018-02-20 02:11:56 -05:00
|
|
|
esp_err_t fillStats(nvs_stats_t& nvsStats);
|
|
|
|
|
|
|
|
esp_err_t calcEntriesInNamespace(uint8_t nsIndex, size_t& usedEntries);
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2018-11-05 03:03:04 -05:00
|
|
|
bool findEntry(nvs_opaque_iterator_t*, const char* name);
|
|
|
|
|
|
|
|
bool nextEntry(nvs_opaque_iterator_t* it);
|
|
|
|
|
2016-08-17 11:08:22 -04:00
|
|
|
protected:
|
|
|
|
|
|
|
|
Page& getCurrentPage()
|
|
|
|
{
|
|
|
|
return mPageManager.back();
|
|
|
|
}
|
|
|
|
|
|
|
|
void clearNamespaces();
|
|
|
|
|
2019-12-06 01:36:28 -05:00
|
|
|
esp_err_t populateBlobIndices(TBlobIndexList&);
|
2018-04-02 06:44:59 -04:00
|
|
|
|
|
|
|
void eraseOrphanDataBlobs(TBlobIndexList&);
|
|
|
|
|
2018-11-05 03:03:04 -05:00
|
|
|
void fillEntryInfo(Item &item, nvs_entry_info_t &info);
|
2018-04-02 06:44:59 -04:00
|
|
|
|
|
|
|
esp_err_t findItem(uint8_t nsIndex, ItemType datatype, const char* key, Page* &page, Item& item, uint8_t chunkIdx = Page::CHUNK_ANY, VerOffset chunkStart = VerOffset::VER_ANY);
|
2016-08-17 11:08:22 -04:00
|
|
|
|
|
|
|
protected:
|
2020-04-26 20:51:31 -04:00
|
|
|
Partition *mPartition;
|
2016-08-17 11:08:22 -04:00
|
|
|
size_t mPageCount;
|
|
|
|
PageManager mPageManager;
|
|
|
|
TNamespaces mNamespaces;
|
|
|
|
CompressedEnumTable<bool, 1, 256> mNamespaceUsage;
|
|
|
|
StorageState mState = StorageState::INVALID;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace nvs
|
|
|
|
|
2018-11-05 03:03:04 -05:00
|
|
|
struct nvs_opaque_iterator_t
|
|
|
|
{
|
|
|
|
nvs_type_t type;
|
|
|
|
uint8_t nsIndex;
|
|
|
|
size_t entryIndex;
|
|
|
|
nvs::Storage *storage;
|
|
|
|
intrusive_list<nvs::Page>::iterator page;
|
|
|
|
nvs_entry_info_t entry_info;
|
|
|
|
};
|
2016-08-17 11:08:22 -04:00
|
|
|
|
|
|
|
#endif /* nvs_storage_hpp */
|