Skip to content

[C33] Kvstore #428

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/compile-examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ jobs:
- libraries/RTC/examples/RTC_NTPSync
- libraries/RTC/examples/RTC_Alarm
- libraries/SFU
- libraries/KVStore/examples/StartCounter
- board:
fqbn: "arduino-git:renesas:portenta_c33"
additional-sketch-paths: |
Expand All @@ -91,6 +92,7 @@ jobs:
- libraries/RTC/examples/RTC_NTPSync
- libraries/RTC/examples/RTC_Alarm
- libraries/SFU
- libraries/KVStore/examples/StartCounter
- board:
fqbn: "arduino:renesas_uno:unor4wifi"
additional-sketch-paths: |
Expand Down
Empty file.
72 changes: 72 additions & 0 deletions libraries/KVStore/examples/StartCounter/StartCounter.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Microcontroller startup counter example with Portenta c33 kvstore library
* This simple example demonstrates using the KVStore library to store how many times
* the microcontroller has booted. The KVStore library is based on mbed OS KVStore library
*
* This example is based on Martin Sloup (Arcao) StartCounter example for arduino-esp32
*/

#include <KVStore.h>
#include <TDBStore.h>
#include <MBRBlockDevice.h>

auto root = BlockDevice::get_default_instance();
MBRBlockDevice bd(root, 3);
TDBStore kvstore(&bd);

void setup() {
Serial.begin(115200);
Serial.println();

while(!Serial);

// Init KVStore
if (kvstore.init() != KVSTORE_SUCCESS) {
Serial.println("Cannot initialize kvstore");
while(1) {};
}

// Remove all values stored in the kvstore
// kvstore.reset();

// Or remove the counter key only
// kvstore.remove("counter");

// Get the counter value, if it doesn't exist it returns KVSTORE_ERROR_ITEM_NOT_FOUND
unsigned int counter;
auto res = kvstore.get("counter", (void*)&counter, sizeof(counter));

if (res == KVSTORE_ERROR_ITEM_NOT_FOUND) {
counter = 0;
} else if (res == KVSTORE_SUCCESS) {
// Increase counter by 1
counter++;
} else {
Serial.print("Error getting counter from kvstore: ");
Serial.println(res);
}

// Print the counter to Serial Monitor
Serial.print("Current counter value: ");
Serial.println(counter);

// Store the updated counter value to the kvstore
if (kvstore.set("counter",(void*)&counter, sizeof(counter), 0) != KVSTORE_SUCCESS) {
Serial.println("Error setting counter from kvstore");
}

// Close the kvstore
if (kvstore.deinit() != KVSTORE_SUCCESS) {
Serial.println("Cannot deinitialize kvstore");
while(1) {};
}

// Wait 10 seconds
Serial.println("Restarting in 10 seconds...");
delay(10000);

// Reset
NVIC_SystemReset();
}

void loop() {}
9 changes: 9 additions & 0 deletions libraries/KVStore/library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name=KVStore
version=1.0.0
author=Arduino
maintainer=Arduino <[email protected]>
sentence=KVStore for arduino core renesas
paragraph=
category=Storage
url=https://github.com/arduino/ArduinoCore-renesas/tree/master/libraries/KVStore
architectures=renesas,renesas_portenta
229 changes: 229 additions & 0 deletions libraries/KVStore/src/KVStore.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
/*
* Copyright (c) 2018 ARM Limited. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef MBED_KVSTORE_H
#define MBED_KVSTORE_H

#include <stdint.h>
#include <string.h>

#define KVSTORE_SUCCESS 0
#define KVSTORE_ERROR_READ_FAILED 283
#define KVSTORE_ERROR_WRITE_FAILED 284
#define KVSTORE_ERROR_INVALID_DATA_DETECTED 258
#define KVSTORE_ERROR_INVALID_SIZE 261
#define KVSTORE_ERROR_INVALID_ARGUMENT 257
#define KVSTORE_ERROR_ITEM_NOT_FOUND 263
#define KVSTORE_ERROR_MEDIA_FULL 267
#define KVSTORE_ERROR_WRITE_PROTECTED 274
#define KVSTORE_ERROR_OUT_OF_RESOURCES 288
#define KVSTORE_ERROR_NOT_READY 270
#define KVSTORE_ERROR_FAILED_OPERATION 271

namespace mbed {

/** KVStore class
*
* Interface class for Key Value Storage
*/
class KVStore {
public:
enum create_flags {
WRITE_ONCE_FLAG = (1 << 0),
REQUIRE_CONFIDENTIALITY_FLAG = (1 << 1),
RESERVED_FLAG = (1 << 2),
REQUIRE_REPLAY_PROTECTION_FLAG = (1 << 3),
};

static const uint32_t MAX_KEY_SIZE = 128;

typedef struct _opaque_set_handle *set_handle_t;

typedef struct _opaque_key_iterator *iterator_t;

/**
* Holds key information
*/
typedef struct info {
/**
* The key size
*/
size_t size;
/*
* The Key flags, possible flags combination:
* WRITE_ONCE_FLAG,
* REQUIRE_CONFIDENTIALITY_FLAG,
* REQUIRE_REPLAY_PROTECTION_FLAG
*/
uint32_t flags;
} info_t;

virtual ~KVStore() {};

/**
* @brief Initialize KVStore
*
* @returns KVSTORE_SUCCESS on success or an error code on failure
*/
virtual int init() = 0;

/**
* @brief Deinitialize KVStore
*
* @returns KVSTORE_SUCCESS on success or an error code on failure
*/
virtual int deinit() = 0;


/**
* @brief Reset KVStore contents (clear all keys)
*
* @returns KVSTORE_SUCCESS on success or an error code on failure
*/
virtual int reset() = 0;

/**
* @brief Set one KVStore item, given key and value.
*
* @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
* @param[in] buffer Value data buffer.
* @param[in] size Value data size.
* @param[in] create_flags Flag mask.
*
* @returns KVSTORE_SUCCESS on success or an error code on failure
*/
virtual int set(const char *key, const void *buffer, size_t size, uint32_t create_flags) = 0;

/**
* @brief Get one KVStore item, given key.
*
* @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
* @param[in] buffer Value data buffer.
* @param[in] buffer_size Value data buffer size.
* @param[out] actual_size Actual read size (NULL to pass nothing).
* @param[in] offset Offset to read from in data.
*
* @returns KVSTORE_SUCCESS on success or an error code on failure
*/
virtual int get(const char *key, void *buffer, size_t buffer_size, size_t *actual_size = NULL, size_t offset = 0) = 0;

/**
* @brief Get information of a given key.
*
* @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
* @param[out] info Returned information structure (NULL to pass nothing).
*
* @returns KVSTORE_SUCCESS on success or an error code on failure
*/
virtual int get_info(const char *key, info_t *info = NULL) = 0;

/**
* @brief Remove a KVStore item, given key.
*
* @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
*
* @returns KVSTORE_SUCCESS on success or an error code on failure
*/
virtual int remove(const char *key) = 0;


/**
* @brief Start an incremental KVStore set sequence.
*
* @param[out] handle Returned incremental set handle.
* @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
* @param[in] final_data_size Final value data size.
* @param[in] create_flags Flag mask.
*
* @returns KVSTORE_SUCCESS on success or an error code on failure
*/
virtual int set_start(set_handle_t *handle, const char *key, size_t final_data_size, uint32_t create_flags) = 0;

/**
* @brief Add data to incremental KVStore set sequence.
*
* @param[in] handle Incremental set handle.
* @param[in] value_data Value data to add.
* @param[in] data_size Value data size.
*
* @returns KVSTORE_SUCCESS on success or an error code on failure
*/
virtual int set_add_data(set_handle_t handle, const void *value_data, size_t data_size) = 0;

/**
* @brief Finalize an incremental KVStore set sequence.
*
* @param[in] handle Incremental set handle.
*
* @returns KVSTORE_SUCCESS on success or an error code on failure
*/
virtual int set_finalize(set_handle_t handle) = 0;

/**
* @brief Start an iteration over KVStore keys.
*
* @param[out] it Returned iterator handle.
* @param[in] prefix Key prefix (null for all keys).
*
* @returns KVSTORE_SUCCESS on success or an error code on failure
*/
virtual int iterator_open(iterator_t *it, const char *prefix = NULL) = 0;

/**
* @brief Get next key in iteration.
*
* @param[in] it Iterator handle.
* @param[in] key Buffer for returned key.
* @param[in] key_size Key buffer size.
*
* @returns KVSTORE_SUCCESS on success or an error code on failure
*/
virtual int iterator_next(iterator_t it, char *key, size_t key_size) = 0;

/**
* @brief Close iteration.
*
* @param[in] it Iterator handle.
*
* @returns KVSTORE_SUCCESS on success or an error code on failure
*/
virtual int iterator_close(iterator_t it) = 0;

/** Convenience function for checking key validity.
* Key must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
*
* @param[in] key Key buffer.
*
* @returns KVSTORE_SUCCESS on success or an error code on failure
*/
bool is_valid_key(const char *key) const
{
if (!key || !strlen(key) || (strlen(key) > MAX_KEY_SIZE)) {
return false;
}

if (strpbrk(key, " */?:;\"|<>\\")) {
return false;
}
return true;
}

};
/** @}*/

} // namespace mbed

#endif
Loading
Loading