-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sys/configuration: add riotconf backend
- Loading branch information
Showing
2 changed files
with
240 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
/* | ||
* Copyright (C) 2024 ML!PA Consulting Gmbh | ||
* | ||
* This file is subject to the terms and conditions of the GNU Lesser General | ||
* Public License v2.1. See the file LICENSE in the top level directory for more | ||
* details. | ||
*/ | ||
|
||
/** | ||
* @ingroup sys_configuration | ||
* @{ | ||
* | ||
* @file | ||
* @brief Implementation of the default riotconf configuration backend | ||
* | ||
* @author Fabian Hüßler <[email protected]> | ||
* | ||
* @} | ||
*/ | ||
|
||
#include <assert.h> | ||
#include <errno.h> | ||
#include <stdbool.h> | ||
#include <stddef.h> | ||
#include <stdint.h> | ||
#include <string.h> | ||
|
||
#include "configuration.h" | ||
#include "macros/utils.h" | ||
#include "mutex.h" | ||
#include "mtd_default.h" | ||
#include "riotconf/slot.h" | ||
|
||
static riotconf_slot_t _current = -EINVAL; | ||
|
||
__attribute__((weak)) | ||
int configuration_backend_riotconf_accept(const riotconf_hdr_t *hdr) | ||
{ | ||
#ifdef CONFIGURATION_BACKENT_RIOTCONF_VERSION | ||
return hdr->version == CONFIGURATION_BACKENT_RIOTCONF_VERSION; | ||
#endif | ||
(void)hdr; | ||
return 1; | ||
} | ||
|
||
__attribute__((weak)) | ||
void configuration_backend_riotconf_storage_init(void) | ||
{ | ||
} | ||
|
||
static int _be_riotconf_init(void) | ||
{ | ||
riotconf_slot_t current = riotconf_slot_highest_seq(configuration_backend_riotconf_accept); | ||
if (current < 0) { | ||
current = 0; | ||
} | ||
_current = current; | ||
return 0; | ||
} | ||
|
||
static int _be_riotconf_reset(void) | ||
{ | ||
return 0; | ||
} | ||
|
||
static int _be_riotconf_load(const struct conf_backend *be, | ||
conf_key_buf_t *key, void *val, size_t *size, | ||
size_t offset, bool *more) | ||
{ | ||
(void)be; | ||
(void)key; | ||
|
||
int ret; | ||
void *sector; | ||
size_t sector_size; | ||
if (_current < 0) { | ||
return -ENOENT; | ||
} | ||
size_t rd = *size; | ||
if (offset == 0) { | ||
riotconf_slot_start_read(_current, §or, §or_size); | ||
riotconf_hdr_t *p_hdr = sector; | ||
riotconf_hdr_t hdr = *p_hdr; | ||
riotconf_hdr_ntoh(&hdr); | ||
if (*size < hdr.size) { | ||
*size = hdr.size; | ||
*more = true; | ||
} | ||
else { | ||
rd = hdr.size; | ||
*size = rd; | ||
} | ||
} | ||
if ((ret = riotconf_slot_read(_current, val, offset, rd)) < 0) { | ||
goto fin; | ||
} | ||
if (!*more) { | ||
fin: | ||
riotconf_slot_finish_read(_current); | ||
} | ||
return ret; | ||
|
||
} | ||
|
||
static int _be_riotconf_store(const struct conf_backend *be, | ||
conf_key_buf_t *key, const void *val, size_t *size, | ||
size_t offset, bool more) | ||
{ | ||
(void)be; | ||
(void)key; | ||
if (_current < 0) { | ||
return -ENOENT; | ||
} | ||
if (offset == 0) { | ||
riotconf_slot_start_write(_current); | ||
} | ||
riotconf_slot_write(_current, val, offset, *size); | ||
if (!more) { | ||
riotconf_slot_finish_write(_current, 0, 0, offset + *size); | ||
} | ||
return 0; | ||
} | ||
|
||
static int _be_riotconf_delete(const struct conf_backend *be, conf_key_buf_t *key) | ||
{ | ||
(void)be; | ||
(void)key; | ||
if (_current < 0) { | ||
return -ENOENT; | ||
} | ||
return 0; | ||
} | ||
|
||
const conf_backend_ops_t conf_backend_riotconf_ops = { | ||
.be_load = _be_riotconf_load, | ||
.be_store = _be_riotconf_store, | ||
.be_delete = _be_riotconf_delete, | ||
}; | ||
|
||
int configuration_backend_riotconf_reset(void) | ||
{ | ||
return _be_riotconf_reset(); | ||
} | ||
|
||
int configuration_backend_riotconf_init(void) | ||
{ | ||
return _be_riotconf_init(); | ||
} | ||
|
||
void auto_init_configuration_backend_riotconf(void) | ||
{ | ||
configuration_backend_riotconf_storage_init(); | ||
int fail = configuration_backend_riotconf_init(); | ||
assert(!fail); (void)fail; | ||
} | ||
|
||
#ifndef AUTO_INIT_PRIO_MOD_CONFIGURATION_BACKEND_RIOTCONF | ||
#define AUTO_INIT_PRIO_MOD_CONFIGURATION_BACKEND_RIOTCONF 5679 | ||
#endif | ||
|
||
AUTO_INIT_CONFIGURATION(auto_init_configuration_backend_riotconf, | ||
AUTO_INIT_PRIO_MOD_CONFIGURATION_BACKEND_RIOTCONF); |
78 changes: 78 additions & 0 deletions
78
sys/configuration/include/configuration_backend_riotconf.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright (C) 2024 ML!PA Consulting Gmbh | ||
* | ||
* This file is subject to the terms and conditions of the GNU Lesser | ||
* General Public License v2.1. See the file LICENSE in the top level | ||
* directory for more details. | ||
*/ | ||
|
||
/** | ||
* @ingroup sys_configuration | ||
* | ||
* @{ | ||
* | ||
* @file | ||
* @brief Interface for riotconf as a configuration backend | ||
* | ||
* @author Fabian Hüßler <[email protected]> | ||
*/ | ||
|
||
#ifndef CONFIGURATION_BACKEND_RIOTCONF_H | ||
#define CONFIGURATION_BACKEND_RIOTCONF_H | ||
|
||
#include "board.h" | ||
#include "modules.h" | ||
#include "mtd.h" | ||
#include "configuration.h" | ||
#include "riotconf/hdr.h" | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
#if DOXYGEN | ||
/** | ||
* @brief The application may define this to set a preferred configuration version | ||
*/ | ||
#define CONFIGURATION_BACKENT_RIOTCONF_VERSION | ||
#endif | ||
|
||
/** | ||
* @brief __attribute__((weak)) function to be overwritten by the application | ||
* to set a more specific criteria for a preferred configuration slot | ||
* | ||
* @param[in] hdr The header of the configuration slot to check | ||
* | ||
* @return 0 if the slot is not accepted, 1 if the slot is accepted | ||
*/ | ||
int configuration_backend_riotconf_accept(const riotconf_hdr_t *hdr); | ||
|
||
/** | ||
* @brief __attribute__((weak)) function to be overwritten by the application | ||
* to call @ref riotconf_storage_init with board supplied MTDs | ||
*/ | ||
void configuration_backend_riotconf_storage_init(void); | ||
|
||
/** | ||
* @brief Initialize the riotconf configuration backend | ||
* | ||
* @return 0 on success, <0 on error | ||
*/ | ||
int configuration_backend_riotconf_init(void); | ||
|
||
/** | ||
* @brief Auto-initialize the riotconf configuration backend | ||
*/ | ||
void auto_init_configuration_backend_riotconf(void); | ||
|
||
/** | ||
* @brief riotconf backed operations | ||
*/ | ||
extern const conf_backend_ops_t conf_backend_riotconf_ops; | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif /* CONFIGURATION_BACKEND_RIOTCONF_H */ | ||
/** @} */ |