forked from nrfconnect/sdk-nrf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpersistent_storage.h
183 lines (152 loc) · 5.54 KB
/
persistent_storage.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
* Copyright (c) 2024 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/
#pragma once
#include "persistent_storage_common.h"
namespace Nrf
{
class PersistentStorageImpl;
/**
* @brief Interface class to manage persistent storage using generic PersistentStorageNode data unit.
*
* PersistentStorage interface consists of NonSecure* and Secure* methods that implement the persistence related
* operations. The NonSecure* prefix indicates that the method does not provide additional security layer and keeps the
* raw material in a persistent storage. Secure* prefix indicates that the method shall leverage the additional security
* layer, like PSA Certified API, that typically causes extra computational overhead and memory usage.
*/
class PersistentStorage {
public:
/**
* @brief Perform the initialization required before using the storage.
*
* @param prefix storage key prefix
* @return true if success.
* @return false otherwise.
*/
PSErrorCode NonSecureInit(const char *prefix);
/**
* @brief Store data into the persistent storage.
*
* @param node address of the tree node containing information about the key.
* @param data data to store into a specific key.
* @param dataSize a size of input buffer.
* @return true if key has been written successfully.
* @return false an error occurred.
*/
PSErrorCode NonSecureStore(PersistentStorageNode *node, const void *data, size_t dataSize);
/**
* @brief Load data from the persistent storage.
*
* @param node address of the tree node containing information about the key.
* @param data data buffer to load from a specific key.
* @param dataMaxSize a size of data buffer to load.
* @param outSize an actual size of read data.
* @return true if key has been loaded successfully.
* @return false an error occurred.
*/
PSErrorCode NonSecureLoad(PersistentStorageNode *node, void *data, size_t dataMaxSize, size_t &outSize);
/**
* @brief Check if given key entry exists in the persistent storage.
*
* @param node address of the tree node containing information about the key.
* @return true if key entry exists.
* @return false if key entry does not exist.
*/
PSErrorCode NonSecureHasEntry(PersistentStorageNode *node);
/**
* @brief Remove given key entry from the persistent storage.
*
* @param node address of the tree node containing information about the key.
* @return true if key entry has been removed successfully.
* @return false an error occurred.
*/
PSErrorCode NonSecureRemove(PersistentStorageNode *node);
/**
* @brief Perform factory reset and remove all keys.
*
* @return true if subtree has been removed successfully.
* @return false an error occurred.
*/
PSErrorCode NonSecureFactoryReset();
/* Secure storage API counterparts.*/
PSErrorCode SecureInit(const char *prefix);
PSErrorCode SecureStore(PersistentStorageNode *node, const void *data, size_t dataSize);
PSErrorCode SecureLoad(PersistentStorageNode *node, void *data, size_t dataMaxSize, size_t &outSize);
PSErrorCode SecureHasEntry(PersistentStorageNode *node);
PSErrorCode SecureRemove(PersistentStorageNode *node);
PSErrorCode SecureFactoryReset();
protected:
PersistentStorage() = default;
~PersistentStorage() = default;
PersistentStorage(const PersistentStorage &) = delete;
PersistentStorage(PersistentStorage &&) = delete;
PersistentStorage &operator=(const PersistentStorage &) = delete;
PersistentStorage &operator=(PersistentStorage &&) = delete;
private:
PersistentStorageImpl *Impl();
};
/* This should be defined by the implementation class. */
extern PersistentStorage &GetPersistentStorage();
} /* namespace Nrf */
#include "persistent_storage_impl.h"
namespace Nrf
{
inline PersistentStorageImpl *PersistentStorage::Impl()
{
return static_cast<PersistentStorageImpl *>(this);
}
/* Non secure storage API. */
inline PSErrorCode PersistentStorage::NonSecureInit(const char *prefix)
{
return Impl()->_NonSecureInit(prefix);
};
inline PSErrorCode PersistentStorage::NonSecureStore(PersistentStorageNode *node, const void *data, size_t dataSize)
{
return Impl()->_NonSecureStore(node, data, dataSize);
}
inline PSErrorCode PersistentStorage::NonSecureLoad(PersistentStorageNode *node, void *data, size_t dataMaxSize,
size_t &outSize)
{
return Impl()->_NonSecureLoad(node, data, dataMaxSize, outSize);
}
inline PSErrorCode PersistentStorage::NonSecureHasEntry(PersistentStorageNode *node)
{
return Impl()->_NonSecureHasEntry(node);
}
inline PSErrorCode PersistentStorage::NonSecureRemove(PersistentStorageNode *node)
{
return Impl()->_NonSecureRemove(node);
}
inline PSErrorCode PersistentStorage::NonSecureFactoryReset()
{
return Impl()->_NonSecureFactoryReset();
}
/* Secure storage API. */
inline PSErrorCode PersistentStorage::SecureInit(const char *prefix)
{
return Impl()->_SecureInit(prefix);
};
inline PSErrorCode PersistentStorage::SecureStore(PersistentStorageNode *node, const void *data, size_t dataSize)
{
return Impl()->_SecureStore(node, data, dataSize);
}
inline PSErrorCode PersistentStorage::SecureLoad(PersistentStorageNode *node, void *data, size_t dataMaxSize,
size_t &outSize)
{
return Impl()->_SecureLoad(node, data, dataMaxSize, outSize);
}
inline PSErrorCode PersistentStorage::SecureHasEntry(PersistentStorageNode *node)
{
return Impl()->_SecureHasEntry(node);
}
inline PSErrorCode PersistentStorage::SecureRemove(PersistentStorageNode *node)
{
return Impl()->_SecureRemove(node);
}
inline PSErrorCode PersistentStorage::SecureFactoryReset()
{
return Impl()->_SecureFactoryReset();
}
} /* namespace Nrf */