Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 926389a

Browse files
authored
Merge pull request #428 from andreagilardoni/kvstore
[C33] Kvstore
2 parents c081119 + d71dc88 commit 926389a

File tree

9 files changed

+3266
-1
lines changed

9 files changed

+3266
-1
lines changed

‎.github/workflows/compile-examples.yml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ jobs:
7676
- libraries/RTC/examples/RTC_NTPSync
7777
- libraries/RTC/examples/RTC_Alarm
7878
- libraries/SFU
79+
- libraries/KVStore/examples/StartCounter
7980
- board:
8081
fqbn: "arduino-git:renesas:portenta_c33"
8182
additional-sketch-paths: |
@@ -91,6 +92,7 @@ jobs:
9192
- libraries/RTC/examples/RTC_NTPSync
9293
- libraries/RTC/examples/RTC_Alarm
9394
- libraries/SFU
95+
- libraries/KVStore/examples/StartCounter
9496
- board:
9597
fqbn: "arduino:renesas_uno:unor4wifi"
9698
additional-sketch-paths: |

‎libraries/KVStore/.portenta_only‎

Whitespace-only changes.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Microcontroller startup counter example with Portenta c33 kvstore library
3+
* This simple example demonstrates using the KVStore library to store how many times
4+
* the microcontroller has booted. The KVStore library is based on mbed OS KVStore library
5+
*
6+
* This example is based on Martin Sloup (Arcao) StartCounter example for arduino-esp32
7+
*/
8+
9+
#include <KVStore.h>
10+
#include <TDBStore.h>
11+
#include <MBRBlockDevice.h>
12+
13+
auto root = BlockDevice::get_default_instance();
14+
MBRBlockDevice bd(root, 3);
15+
TDBStore kvstore(&bd);
16+
17+
void setup() {
18+
Serial.begin(115200);
19+
Serial.println();
20+
21+
while(!Serial);
22+
23+
// Init KVStore
24+
if (kvstore.init() != KVSTORE_SUCCESS) {
25+
Serial.println("Cannot initialize kvstore");
26+
while(1) {};
27+
}
28+
29+
// Remove all values stored in the kvstore
30+
// kvstore.reset();
31+
32+
// Or remove the counter key only
33+
// kvstore.remove("counter");
34+
35+
// Get the counter value, if it doesn't exist it returns KVSTORE_ERROR_ITEM_NOT_FOUND
36+
unsigned int counter;
37+
auto res = kvstore.get("counter", (void*)&counter, sizeof(counter));
38+
39+
if (res == KVSTORE_ERROR_ITEM_NOT_FOUND) {
40+
counter = 0;
41+
} else if (res == KVSTORE_SUCCESS) {
42+
// Increase counter by 1
43+
counter++;
44+
} else {
45+
Serial.print("Error getting counter from kvstore: ");
46+
Serial.println(res);
47+
}
48+
49+
// Print the counter to Serial Monitor
50+
Serial.print("Current counter value: ");
51+
Serial.println(counter);
52+
53+
// Store the updated counter value to the kvstore
54+
if (kvstore.set("counter",(void*)&counter, sizeof(counter), 0) != KVSTORE_SUCCESS) {
55+
Serial.println("Error setting counter from kvstore");
56+
}
57+
58+
// Close the kvstore
59+
if (kvstore.deinit() != KVSTORE_SUCCESS) {
60+
Serial.println("Cannot deinitialize kvstore");
61+
while(1) {};
62+
}
63+
64+
// Wait 10 seconds
65+
Serial.println("Restarting in 10 seconds...");
66+
delay(10000);
67+
68+
// Reset
69+
NVIC_SystemReset();
70+
}
71+
72+
void loop() {}

‎libraries/KVStore/library.properties‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=KVStore
2+
version=1.0.0
3+
author=Arduino
4+
maintainer=Arduino <info@arduino.cc>
5+
sentence=KVStore for arduino core renesas
6+
paragraph=
7+
category=Storage
8+
url=https://github.com/arduino/ArduinoCore-renesas/tree/master/libraries/KVStore
9+
architectures=renesas,renesas_portenta

‎libraries/KVStore/src/KVStore.h‎

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

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /