settings: Moving initialization code to proper backend sources

Backend initialization code has been moved from common settings_init.c
to proper backend source files. Missing static specifiers have been
added.
Minor cleanup has been done to source files: exported functions have
been moved to the end of source files and definitions of static
variables, that are used by only a single function, have been moved from
global scope into functions that use them.

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
This commit is contained in:
Dominik Ermel 2019-11-25 12:14:32 +00:00 committed by Carles Cufí
parent a6f6179cd9
commit 220f7607da
7 changed files with 178 additions and 183 deletions

View file

@ -11,3 +11,4 @@ zephyr_sources_ifdef(CONFIG_SETTINGS_RUNTIME settings_runtime.c)
zephyr_sources_ifdef(CONFIG_SETTINGS_FS settings_file.c)
zephyr_sources_ifdef(CONFIG_SETTINGS_FCB settings_fcb.c)
zephyr_sources_ifdef(CONFIG_SETTINGS_NVS settings_nvs.c)
zephyr_sources_ifdef(CONFIG_SETTINGS_NONE settings_none.c)

View file

@ -20,6 +20,9 @@ LOG_MODULE_DECLARE(settings, CONFIG_SETTINGS_LOG_LEVEL);
#define SETTINGS_FCB_VERS 1
int settings_backend_init(void);
void settings_mount_fcb_backend(struct settings_fcb *cf);
static int settings_fcb_load(struct settings_store *cs,
const struct settings_load_arg *arg);
static int settings_fcb_save(struct settings_store *cs, const char *name,
@ -369,3 +372,58 @@ void settings_mount_fcb_backend(struct settings_fcb *cf)
settings_line_io_init(read_handler, write_handler, get_len_cb, rbs);
}
int settings_backend_init(void)
{
static struct flash_sector
settings_fcb_area[CONFIG_SETTINGS_FCB_NUM_AREAS + 1];
static struct settings_fcb config_init_settings_fcb = {
.cf_fcb.f_magic = CONFIG_SETTINGS_FCB_MAGIC,
.cf_fcb.f_sectors = settings_fcb_area,
};
u32_t cnt = sizeof(settings_fcb_area) /
sizeof(settings_fcb_area[0]);
int rc;
const struct flash_area *fap;
rc = flash_area_get_sectors(DT_FLASH_AREA_STORAGE_ID, &cnt,
settings_fcb_area);
if (rc == -ENODEV) {
return rc;
} else if (rc != 0 && rc != -ENOMEM) {
k_panic();
}
config_init_settings_fcb.cf_fcb.f_sector_cnt = cnt;
rc = settings_fcb_src(&config_init_settings_fcb);
if (rc != 0) {
rc = flash_area_open(DT_FLASH_AREA_STORAGE_ID, &fap);
if (rc == 0) {
rc = flash_area_erase(fap, 0, fap->fa_size);
flash_area_close(fap);
}
if (rc != 0) {
k_panic();
} else {
rc = settings_fcb_src(&config_init_settings_fcb);
}
}
if (rc != 0) {
k_panic();
}
rc = settings_fcb_dst(&config_init_settings_fcb);
if (rc != 0) {
k_panic();
}
settings_mount_fcb_backend(&config_init_settings_fcb);
return rc;
}

View file

@ -16,8 +16,11 @@
#include "settings_priv.h"
#include <logging/log.h>
LOG_MODULE_DECLARE(settings, CONFIG_SETTINGS_LOG_LEVEL);
int settings_backend_init(void);
void settings_mount_fs_backend(struct settings_file *cf);
static int settings_file_load(struct settings_store *cs,
const struct settings_load_arg *arg);
@ -43,6 +46,9 @@ int settings_file_src(struct settings_file *cf)
return 0;
}
/*
* Register a file to be a destination of configuration.
*/
int settings_file_dst(struct settings_file *cf)
{
if (!cf->cf_name) {
@ -59,14 +65,13 @@ int settings_file_dst(struct settings_file *cf)
*
* This function checks if there is any duplicated data further in the buffer.
*
* @param cf FCB handler
* @param entry_ctx Current entry context
* @param name The name of the current entry
*
* @retval false No duplicates found
* @retval true Duplicate found
*/
bool settings_file_check_duplicate(struct settings_file *cf,
static bool settings_file_check_duplicate(
const struct line_entry_ctx *entry_ctx,
const char * const name)
{
@ -106,7 +111,6 @@ static int settings_file_load_priv(struct settings_store *cs, line_load_cb cb,
void *cb_arg, bool filter_duplicates)
{
struct settings_file *cf = (struct settings_file *)cs;
struct fs_dirent file_info;
struct fs_file_t file;
int lines;
int rc;
@ -119,11 +123,6 @@ static int settings_file_load_priv(struct settings_store *cs, line_load_cb cb,
lines = 0;
rc = fs_stat(cf->cf_name, &file_info);
if (rc) {
return rc;
}
rc = fs_open(&file, cf->cf_name);
if (rc != 0) {
return -EINVAL;
@ -149,7 +148,7 @@ static int settings_file_load_priv(struct settings_store *cs, line_load_cb cb,
if (filter_duplicates &&
(!read_entry_len(&entry_ctx, name_len+1) ||
settings_file_check_duplicate(cf, &entry_ctx, name))) {
settings_file_check_duplicate(&entry_ctx, name))) {
pass_entry = false;
}
/*name, val-read_cb-ctx, val-off*/
@ -214,8 +213,9 @@ static int settings_file_create_or_replace(struct fs_file_t *zfp,
/*
* Try to compress configuration file by keeping unique names only.
*/
int settings_file_save_and_compress(struct settings_file *cf, const char *name,
const char *value, size_t val_len)
static int settings_file_save_and_compress(struct settings_file *cf,
const char *name, const char *value,
size_t val_len)
{
int rc, rc2;
struct fs_file_t rf;
@ -495,3 +495,39 @@ void settings_mount_fs_backend(struct settings_file *cf)
{
settings_line_io_init(read_handler, write_handler, get_len_cb, 1);
}
int settings_backend_init(void)
{
static struct settings_file config_init_settings_file = {
.cf_name = CONFIG_SETTINGS_FS_FILE,
.cf_maxlines = CONFIG_SETTINGS_FS_MAX_LINES
};
int rc;
rc = settings_file_src(&config_init_settings_file);
if (rc) {
k_panic();
}
rc = settings_file_dst(&config_init_settings_file);
if (rc) {
k_panic();
}
settings_mount_fs_backend(&config_init_settings_file);
/*
* Must be called after root FS has been initialized.
*/
rc = fs_mkdir(CONFIG_SETTINGS_FS_DIR);
/*
* The following lines mask the file exist error.
*/
if (rc == -EEXIST) {
rc = 0;
}
return rc;
}

View file

@ -22,177 +22,6 @@ void settings_init(void);
int settings_backend_init(void);
#ifdef CONFIG_SETTINGS_FS
#include <fs/fs.h>
static struct settings_file config_init_settings_file = {
.cf_name = CONFIG_SETTINGS_FS_FILE,
.cf_maxlines = CONFIG_SETTINGS_FS_MAX_LINES
};
int settings_backend_init(void)
{
int rc;
rc = settings_file_src(&config_init_settings_file);
if (rc) {
k_panic();
}
rc = settings_file_dst(&config_init_settings_file);
if (rc) {
k_panic();
}
settings_mount_fs_backend(&config_init_settings_file);
/*
* Must be called after root FS has been initialized.
*/
rc = fs_mkdir(CONFIG_SETTINGS_FS_DIR);
/*
* The following lines mask the file exist error.
*/
if (rc == -EEXIST) {
rc = 0;
}
return rc;
}
#elif defined(CONFIG_SETTINGS_FCB)
#include <fs/fcb.h>
#include "settings/settings_fcb.h"
static struct flash_sector settings_fcb_area[CONFIG_SETTINGS_FCB_NUM_AREAS + 1];
static struct settings_fcb config_init_settings_fcb = {
.cf_fcb.f_magic = CONFIG_SETTINGS_FCB_MAGIC,
.cf_fcb.f_sectors = settings_fcb_area,
};
int settings_backend_init(void)
{
u32_t cnt = CONFIG_SETTINGS_FCB_NUM_AREAS + 1;
int rc;
const struct flash_area *fap;
rc = flash_area_get_sectors(DT_FLASH_AREA_STORAGE_ID, &cnt,
settings_fcb_area);
if (rc == -ENODEV) {
return rc;
} else if (rc != 0 && rc != -ENOMEM) {
k_panic();
}
config_init_settings_fcb.cf_fcb.f_sector_cnt = cnt;
rc = settings_fcb_src(&config_init_settings_fcb);
if (rc != 0) {
rc = flash_area_open(DT_FLASH_AREA_STORAGE_ID, &fap);
if (rc == 0) {
rc = flash_area_erase(fap, 0, fap->fa_size);
flash_area_close(fap);
}
if (rc != 0) {
k_panic();
} else {
rc = settings_fcb_src(&config_init_settings_fcb);
}
}
if (rc != 0) {
k_panic();
}
rc = settings_fcb_dst(&config_init_settings_fcb);
if (rc != 0) {
k_panic();
}
settings_mount_fcb_backend(&config_init_settings_fcb);
return rc;
}
#elif defined(CONFIG_SETTINGS_NVS)
#include <device.h>
#include <storage/flash_map.h>
#include "settings/settings_nvs.h"
static struct settings_nvs default_settings_nvs;
int settings_backend_init(void)
{
int rc;
u16_t cnt = 0;
size_t nvs_sector_size, nvs_size = 0;
const struct flash_area *fa;
struct flash_sector hw_flash_sector;
u32_t sector_cnt = 1;
rc = flash_area_open(DT_FLASH_AREA_STORAGE_ID, &fa);
if (rc) {
return rc;
}
rc = flash_area_get_sectors(DT_FLASH_AREA_STORAGE_ID, &sector_cnt,
&hw_flash_sector);
if (rc == -ENODEV) {
return rc;
} else if (rc != 0 && rc != -ENOMEM) {
k_panic();
}
nvs_sector_size = CONFIG_SETTINGS_NVS_SECTOR_SIZE_MULT *
hw_flash_sector.fs_size;
if (nvs_sector_size > UINT16_MAX) {
return -EDOM;
}
while (cnt < CONFIG_SETTINGS_NVS_SECTOR_COUNT) {
nvs_size += nvs_sector_size;
if (nvs_size > fa->fa_size) {
break;
}
cnt++;
}
/* define the nvs file system using the page_info */
default_settings_nvs.cf_nvs.sector_size = nvs_sector_size;
default_settings_nvs.cf_nvs.sector_count = cnt;
default_settings_nvs.cf_nvs.offset = fa->fa_off;
default_settings_nvs.flash_dev_name = fa->fa_dev_name;
rc = settings_nvs_backend_init(&default_settings_nvs);
if (rc) {
return rc;
}
rc = settings_nvs_src(&default_settings_nvs);
if (rc) {
return rc;
}
rc = settings_nvs_dst(&default_settings_nvs);
return rc;
}
#elif defined(CONFIG_SETTINGS_NONE)
int settings_backend_init(void)
{
return 0;
}
#endif
int settings_subsys_init(void)
{

View file

@ -23,7 +23,7 @@ struct settings_io_cb_s {
int (*write_cb)(void *ctx, off_t off, char const *buf, size_t len);
size_t (*get_len_cb)(void *ctx);
u8_t rwbs;
} settings_io_cb;
} static settings_io_cb;
#define MAX_ENC_BLOCK_SIZE 4

View file

@ -0,0 +1,10 @@
/*
* Copyright (c) 2019 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
int settings_backend_init(void)
{
return 0;
}

View file

@ -11,6 +11,7 @@
#include "settings/settings.h"
#include "settings/settings_nvs.h"
#include "settings_priv.h"
#include <storage/flash_map.h>
#include <logging/log.h>
LOG_MODULE_DECLARE(settings, CONFIG_SETTINGS_LOG_LEVEL);
@ -257,3 +258,63 @@ int settings_nvs_backend_init(struct settings_nvs *cf)
LOG_DBG("Initialized");
return 0;
}
int settings_backend_init(void)
{
static struct settings_nvs default_settings_nvs;
int rc;
u16_t cnt = 0;
size_t nvs_sector_size, nvs_size = 0;
const struct flash_area *fa;
struct flash_sector hw_flash_sector;
u32_t sector_cnt = 1;
rc = flash_area_open(DT_FLASH_AREA_STORAGE_ID, &fa);
if (rc) {
return rc;
}
rc = flash_area_get_sectors(DT_FLASH_AREA_STORAGE_ID, &sector_cnt,
&hw_flash_sector);
if (rc == -ENODEV) {
return rc;
} else if (rc != 0 && rc != -ENOMEM) {
k_panic();
}
nvs_sector_size = CONFIG_SETTINGS_NVS_SECTOR_SIZE_MULT *
hw_flash_sector.fs_size;
if (nvs_sector_size > UINT16_MAX) {
return -EDOM;
}
while (cnt < CONFIG_SETTINGS_NVS_SECTOR_COUNT) {
nvs_size += nvs_sector_size;
if (nvs_size > fa->fa_size) {
break;
}
cnt++;
}
/* define the nvs file system using the page_info */
default_settings_nvs.cf_nvs.sector_size = nvs_sector_size;
default_settings_nvs.cf_nvs.sector_count = cnt;
default_settings_nvs.cf_nvs.offset = fa->fa_off;
default_settings_nvs.flash_dev_name = fa->fa_dev_name;
rc = settings_nvs_backend_init(&default_settings_nvs);
if (rc) {
return rc;
}
rc = settings_nvs_src(&default_settings_nvs);
if (rc) {
return rc;
}
rc = settings_nvs_dst(&default_settings_nvs);
return rc;
}