Skip to content

Commit 70e9ad0

Browse files
committed
config: Cleanup config file initialization
There was duplicated code for config base on file system for littlefs and nffs. Now there is common initialization function for both file systems. Additionally other file system can be used for config storage without need to modify initialization code since it used mynewt fs api and nothing specific to littlefs of nffs. It is needed if FATFS (sdcard) is to be used for config storage Signed-off-by: Jerzy Kasenberg <[email protected]>
1 parent f9ded5d commit 70e9ad0

File tree

4 files changed

+53
-46
lines changed

4 files changed

+53
-46
lines changed

sys/config/include/config/config_file.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ struct conf_file {
3939
int conf_file_src(struct conf_file *); /* register file to be source of cfg */
4040
int conf_file_dst(struct conf_file *); /* cfg saves go to a file */
4141

42+
/**
43+
* @brief Initialize config with syscfg values
44+
*
45+
* Function registers config source and destination stores.
46+
*/
47+
void config_file_init(void);
48+
4249
#ifdef __cplusplus
4350
}
4451
#endif

sys/config/src/config_file.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
#include "os/mynewt.h"
2121

22-
#if MYNEWT_VAL(CONFIG_NFFS) || MYNEWT_VAL(CONFIG_LITTLEFS)
22+
#if MYNEWT_VAL(CONFIG_FILE)
2323

2424
#include <string.h>
2525
#include <assert.h>
@@ -35,7 +35,7 @@ static int conf_file_load(struct conf_store *, conf_store_load_cb cb,
3535
static int conf_file_save(struct conf_store *, const char *name,
3636
const char *value);
3737

38-
static struct conf_store_itf conf_file_itf = {
38+
static const struct conf_store_itf conf_file_itf = {
3939
.csi_load = conf_file_load,
4040
.csi_save = conf_file_save,
4141
};
@@ -283,4 +283,20 @@ conf_file_save(struct conf_store *cs, const char *name, const char *value)
283283
return rc;
284284
}
285285

286+
static struct conf_file config_init_conf = {
287+
.cf_name = MYNEWT_VAL(CONFIG_FILE_FILE_NAME),
288+
.cf_maxlines = MYNEWT_VAL(CONFIG_FILE_MAX_LINES)
289+
};
290+
291+
void
292+
config_file_init(void)
293+
{
294+
int rc;
295+
296+
rc = conf_file_src(&config_init_conf);
297+
SYSINIT_PANIC_ASSERT(rc == 0);
298+
rc = conf_file_dst(&config_init_conf);
299+
SYSINIT_PANIC_ASSERT(rc == 0);
300+
}
301+
286302
#endif

sys/config/src/config_init.c

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -27,46 +27,7 @@
2727

2828
#if MYNEWT_VAL(CONFIG_AUTO_INIT)
2929

30-
#if MYNEWT_VAL(CONFIG_LITTLEFS)
31-
#include "fs/fs.h"
32-
33-
static struct conf_file config_init_conf_littlefs = {
34-
.cf_name = MYNEWT_VAL(CONFIG_LITTLEFS_FILE),
35-
.cf_maxlines = MYNEWT_VAL(CONFIG_LITTLEFS_MAX_LINES)
36-
};
37-
38-
static void
39-
config_init_littlefs(void)
40-
{
41-
int rc;
42-
43-
rc = conf_file_src(&config_init_conf_littlefs);
44-
SYSINIT_PANIC_ASSERT(rc == 0);
45-
rc = conf_file_dst(&config_init_conf_littlefs);
46-
SYSINIT_PANIC_ASSERT(rc == 0);
47-
}
48-
49-
#elif MYNEWT_VAL(CONFIG_NFFS)
50-
51-
#include "fs/fs.h"
52-
53-
static struct conf_file config_init_conf_file = {
54-
.cf_name = MYNEWT_VAL(CONFIG_NFFS_FILE),
55-
.cf_maxlines = MYNEWT_VAL(CONFIG_NFFS_MAX_LINES)
56-
};
57-
58-
static void
59-
config_init_fs(void)
60-
{
61-
int rc;
62-
63-
rc = conf_file_src(&config_init_conf_file);
64-
SYSINIT_PANIC_ASSERT(rc == 0);
65-
rc = conf_file_dst(&config_init_conf_file);
66-
SYSINIT_PANIC_ASSERT(rc == 0);
67-
}
68-
69-
#elif MYNEWT_VAL(CONFIG_FCB)
30+
#if MYNEWT_VAL(CONFIG_FCB)
7031

7132
#include "config/config_fcb.h"
7233

@@ -157,10 +118,8 @@ config_pkg_init(void)
157118
conf_init();
158119

159120
#if MYNEWT_VAL(CONFIG_AUTO_INIT)
160-
#if MYNEWT_VAL(CONFIG_LITTLEFS)
161-
config_init_littlefs();
162-
#elif MYNEWT_VAL(CONFIG_NFFS)
163-
config_init_fs();
121+
#if MYNEWT_VAL(CONFIG_FILE)
122+
config_file_init();
164123
#elif MYNEWT_VAL(CONFIG_FCB)
165124
config_init_fcb();
166125
#elif MYNEWT_VAL(CONFIG_FCB2)

sys/config/syscfg.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ syscfg.defs:
4848
- '!CONFIG_FCB'
4949
- '!CONFIG_FCB2'
5050

51+
CONFIG_FILE:
52+
description: 'Config default storage is in file system file'
53+
value: 0
54+
restrictions:
55+
- '!CONFIG_FCB'
56+
- '!CONFIG_FCB2'
57+
5158
CONFIG_MGMT:
5259
description: 'SMP access to config'
5360
value: 0
@@ -143,8 +150,26 @@ syscfg.defs.CONFIG_LITTLEFS:
143150
description: 'Limit how many items stored in file before compressing'
144151
value: 32
145152

153+
syscfg.defs.CONFIG_FILE:
154+
CONFIG_FILE_FILE_NAME:
155+
description: 'Name for the default config file'
156+
value: '"cfg"'
157+
CONFIG_FILE_MAX_LINES:
158+
description: 'Limit how many items stored in file before compressing'
159+
value: 32
160+
146161
syscfg.vals.CONFIG_FCB2:
147162
CONFIG_FCB_MAGIC: 0xd0b1d0b1
148163

149164
syscfg.vals.CONFIG_NEWTMGR:
150165
CONFIG_MGMT: MYNEWT_VAL(CONFIG_NEWTMGR)
166+
167+
syscfg.vals.CONFIG_LITTLEFS:
168+
CONFIG_FILE: 1
169+
CONFIG_FILE_FILE_NAME: MYNEWT_VAL(CONFIG_LITTLEFS_FILE)
170+
CONFIG_FILE_MAX_LINES: MYNEWT_VAL(CONFIG_LITTLEFS_MAX_LINES)
171+
172+
syscfg.vals.CONFIG_NFFS:
173+
CONFIG_FILE: 1
174+
CONFIG_FILE_FILE_NAME: MYNEWT_VAL(CONFIG_NFFS_FILE)
175+
CONFIG_FILE_MAX_LINES: MYNEWT_VAL(CONFIG_NFFS_MAX_LINES)

0 commit comments

Comments
 (0)