Skip to content

Commit 677dcf1

Browse files
Adding KVStore api from mbedos: interface
1 parent 7e83b8b commit 677dcf1

File tree

1 file changed

+230
-0
lines changed

1 file changed

+230
-0
lines changed

libraries/KVStore/KVStore.h

+230
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
/*
2+
* Copyright (c) 2018 ARM Limited. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
* Licensed under the Apache License, Version 2.0 (the License); you may
5+
* not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef MBED_KVSTORE_H
18+
#define MBED_KVSTORE_H
19+
20+
#include <stdint.h>
21+
#include <string.h>
22+
23+
// FIXME define mbed errors adequately
24+
#define KVSTORE_SUCCESS 0
25+
#define KVSTORE_ERROR_READ_FAILED 283
26+
#define KVSTORE_ERROR_WRITE_FAILED 284
27+
#define KVSTORE_ERROR_INVALID_DATA_DETECTED 258
28+
#define KVSTORE_ERROR_INVALID_SIZE 261
29+
#define KVSTORE_ERROR_INVALID_ARGUMENT 257
30+
#define KVSTORE_ERROR_ITEM_NOT_FOUND 263
31+
#define KVSTORE_ERROR_MEDIA_FULL 267
32+
#define KVSTORE_ERROR_WRITE_PROTECTED 274
33+
#define KVSTORE_ERROR_OUT_OF_RESOURCES 288
34+
#define KVSTORE_ERROR_NOT_READY 270
35+
#define KVSTORE_ERROR_FAILED_OPERATION 271
36+
37+
namespace mbed {
38+
39+
/** KVStore class
40+
*
41+
* Interface class for Key Value Storage
42+
*/
43+
class KVStore {
44+
public:
45+
enum create_flags {
46+
WRITE_ONCE_FLAG = (1 << 0),
47+
REQUIRE_CONFIDENTIALITY_FLAG = (1 << 1),
48+
RESERVED_FLAG = (1 << 2),
49+
REQUIRE_REPLAY_PROTECTION_FLAG = (1 << 3),
50+
};
51+
52+
static const uint32_t MAX_KEY_SIZE = 128;
53+
54+
typedef struct _opaque_set_handle *set_handle_t;
55+
56+
typedef struct _opaque_key_iterator *iterator_t;
57+
58+
/**
59+
* Holds key information
60+
*/
61+
typedef struct info {
62+
/**
63+
* The key size
64+
*/
65+
size_t size;
66+
/*
67+
* The Key flags, possible flags combination:
68+
* WRITE_ONCE_FLAG,
69+
* REQUIRE_CONFIDENTIALITY_FLAG,
70+
* REQUIRE_REPLAY_PROTECTION_FLAG
71+
*/
72+
uint32_t flags;
73+
} info_t;
74+
75+
virtual ~KVStore() {};
76+
77+
/**
78+
* @brief Initialize KVStore
79+
*
80+
* @returns KVSTORE_SUCCESS on success or an error code on failure
81+
*/
82+
virtual int init() = 0;
83+
84+
/**
85+
* @brief Deinitialize KVStore
86+
*
87+
* @returns KVSTORE_SUCCESS on success or an error code on failure
88+
*/
89+
virtual int deinit() = 0;
90+
91+
92+
/**
93+
* @brief Reset KVStore contents (clear all keys)
94+
*
95+
* @returns KVSTORE_SUCCESS on success or an error code on failure
96+
*/
97+
virtual int reset() = 0;
98+
99+
/**
100+
* @brief Set one KVStore item, given key and value.
101+
*
102+
* @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
103+
* @param[in] buffer Value data buffer.
104+
* @param[in] size Value data size.
105+
* @param[in] create_flags Flag mask.
106+
*
107+
* @returns KVSTORE_SUCCESS on success or an error code on failure
108+
*/
109+
virtual int set(const char *key, const void *buffer, size_t size, uint32_t create_flags) = 0;
110+
111+
/**
112+
* @brief Get one KVStore item, given key.
113+
*
114+
* @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
115+
* @param[in] buffer Value data buffer.
116+
* @param[in] buffer_size Value data buffer size.
117+
* @param[out] actual_size Actual read size (NULL to pass nothing).
118+
* @param[in] offset Offset to read from in data.
119+
*
120+
* @returns KVSTORE_SUCCESS on success or an error code on failure
121+
*/
122+
virtual int get(const char *key, void *buffer, size_t buffer_size, size_t *actual_size = NULL, size_t offset = 0) = 0;
123+
124+
/**
125+
* @brief Get information of a given key.
126+
*
127+
* @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
128+
* @param[out] info Returned information structure (NULL to pass nothing).
129+
*
130+
* @returns KVSTORE_SUCCESS on success or an error code on failure
131+
*/
132+
virtual int get_info(const char *key, info_t *info = NULL) = 0;
133+
134+
/**
135+
* @brief Remove a KVStore item, given key.
136+
*
137+
* @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
138+
*
139+
* @returns KVSTORE_SUCCESS on success or an error code on failure
140+
*/
141+
virtual int remove(const char *key) = 0;
142+
143+
144+
/**
145+
* @brief Start an incremental KVStore set sequence.
146+
*
147+
* @param[out] handle Returned incremental set handle.
148+
* @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
149+
* @param[in] final_data_size Final value data size.
150+
* @param[in] create_flags Flag mask.
151+
*
152+
* @returns KVSTORE_SUCCESS on success or an error code on failure
153+
*/
154+
virtual int set_start(set_handle_t *handle, const char *key, size_t final_data_size, uint32_t create_flags) = 0;
155+
156+
/**
157+
* @brief Add data to incremental KVStore set sequence.
158+
*
159+
* @param[in] handle Incremental set handle.
160+
* @param[in] value_data Value data to add.
161+
* @param[in] data_size Value data size.
162+
*
163+
* @returns KVSTORE_SUCCESS on success or an error code on failure
164+
*/
165+
virtual int set_add_data(set_handle_t handle, const void *value_data, size_t data_size) = 0;
166+
167+
/**
168+
* @brief Finalize an incremental KVStore set sequence.
169+
*
170+
* @param[in] handle Incremental set handle.
171+
*
172+
* @returns KVSTORE_SUCCESS on success or an error code on failure
173+
*/
174+
virtual int set_finalize(set_handle_t handle) = 0;
175+
176+
/**
177+
* @brief Start an iteration over KVStore keys.
178+
*
179+
* @param[out] it Returned iterator handle.
180+
* @param[in] prefix Key prefix (null for all keys).
181+
*
182+
* @returns KVSTORE_SUCCESS on success or an error code on failure
183+
*/
184+
virtual int iterator_open(iterator_t *it, const char *prefix = NULL) = 0;
185+
186+
/**
187+
* @brief Get next key in iteration.
188+
*
189+
* @param[in] it Iterator handle.
190+
* @param[in] key Buffer for returned key.
191+
* @param[in] key_size Key buffer size.
192+
*
193+
* @returns KVSTORE_SUCCESS on success or an error code on failure
194+
*/
195+
virtual int iterator_next(iterator_t it, char *key, size_t key_size) = 0;
196+
197+
/**
198+
* @brief Close iteration.
199+
*
200+
* @param[in] it Iterator handle.
201+
*
202+
* @returns KVSTORE_SUCCESS on success or an error code on failure
203+
*/
204+
virtual int iterator_close(iterator_t it) = 0;
205+
206+
/** Convenience function for checking key validity.
207+
* Key must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
208+
*
209+
* @param[in] key Key buffer.
210+
*
211+
* @returns KVSTORE_SUCCESS on success or an error code on failure
212+
*/
213+
bool is_valid_key(const char *key) const
214+
{
215+
if (!key || !strlen(key) || (strlen(key) > MAX_KEY_SIZE)) {
216+
return false;
217+
}
218+
219+
if (strpbrk(key, " */?:;\"|<>\\")) {
220+
return false;
221+
}
222+
return true;
223+
}
224+
225+
};
226+
/** @}*/
227+
228+
} // namespace mbed
229+
230+
#endif

0 commit comments

Comments
 (0)