Skip to content

Commit

Permalink
Add float config value type ("f")
Browse files Browse the repository at this point in the history
  • Loading branch information
rojer committed Feb 12, 2021
1 parent b46f39e commit 9a1170d
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 23 deletions.
1 change: 1 addition & 0 deletions include/mgos_config_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ enum mgos_conf_type {
CONF_TYPE_STRING = 3,
CONF_TYPE_OBJECT = 4,
CONF_TYPE_UNSIGNED_INT = 5,
CONF_TYPE_FLOAT = 6,
};

/* Configuration entry */
Expand Down
40 changes: 38 additions & 2 deletions src/mgos_config_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ void mgos_conf_parse_cb(void *data, const char *name, size_t name_len,
#endif
char *vp = (((char *) ctx->cfg) + e->offset - ctx->offset_adj);
switch (e->type) {
case CONF_TYPE_FLOAT:
/* fall through */
case CONF_TYPE_DOUBLE:
#ifdef MGOS_BOOT_BUILD
ctx->result = false;
Expand All @@ -143,6 +145,9 @@ void mgos_conf_parse_cb(void *data, const char *name, size_t name_len,
*((unsigned int *) vp) = strtoul(tok->ptr, &endptr, 0);
break;
#ifndef MGOS_BOOT_BUILD
case CONF_TYPE_FLOAT:
*((float *) vp) = strtof(tok->ptr, &endptr);
break;
case CONF_TYPE_DOUBLE:
*((double *) vp) = strtod(tok->ptr, &endptr);
break;
Expand Down Expand Up @@ -273,6 +278,8 @@ static bool mgos_conf_value_eq(const void *cfg, const void *base,
/* fall through */
case CONF_TYPE_UNSIGNED_INT:
return *((int *) vp) == *((int *) bvp);
case CONF_TYPE_FLOAT:
return *((float *) vp) == *((float *) bvp);
case CONF_TYPE_DOUBLE:
return *((double *) vp) == *((double *) bvp);
case CONF_TYPE_STRING: {
Expand Down Expand Up @@ -325,9 +332,13 @@ static void mgos_conf_emit_entry(struct emit_ctx *ctx,
mbuf_append(ctx->out, s, len);
break;
}
case CONF_TYPE_FLOAT:
/* fall through */
case CONF_TYPE_DOUBLE: {
len = snprintf(buf, sizeof(buf), "%lf",
*((double *) (((char *) ctx->cfg) + e->offset)));
char *ptr = (((char *) ctx->cfg) + e->offset);
double v =
(e->type == CONF_TYPE_DOUBLE ? *((double *) ptr) : *((float *) ptr));
len = snprintf(buf, sizeof(buf), "%lf", v);
mbuf_append(ctx->out, buf, len);
break;
}
Expand Down Expand Up @@ -450,6 +461,11 @@ bool mgos_conf_copy(const struct mgos_conf_entry *schema, const void *src,
case CONF_TYPE_UNSIGNED_INT:
*((int *) dvp) = *((const int *) svp);
break;
case CONF_TYPE_FLOAT:
#ifndef MGOS_BOOT_BUILD
*((float *) dvp) = *((const float *) svp);
#endif
break;
case CONF_TYPE_DOUBLE:
#ifndef MGOS_BOOT_BUILD
*((double *) dvp) = *((const double *) svp);
Expand Down Expand Up @@ -539,6 +555,14 @@ int mgos_conf_value_int(const void *cfg, const struct mgos_conf_entry *e) {
return 0;
}

float mgos_conf_value_float(const void *cfg, const struct mgos_conf_entry *e) {
char *vp = (((char *) cfg) + e->offset);
if (e->type == CONF_TYPE_FLOAT) {
return *((float *) vp);
}
return 0;
}

double mgos_conf_value_double(const void *cfg,
const struct mgos_conf_entry *e) {
char *vp = (((char *) cfg) + e->offset);
Expand Down Expand Up @@ -567,6 +591,9 @@ bool mgos_config_get(const struct mg_str key, struct mg_str *value,
value->len = mg_asprintf(
cp, 0, "%s", (mgos_conf_value_int(cfg, e) ? "true" : "false"));
break;
case CONF_TYPE_FLOAT:
value->len = mg_asprintf(cp, 0, "%f", mgos_conf_value_float(cfg, e));
break;
case CONF_TYPE_DOUBLE:
value->len = mg_asprintf(cp, 0, "%lf", mgos_conf_value_double(cfg, e));
break;
Expand Down Expand Up @@ -630,6 +657,15 @@ bool mgos_config_set(const struct mg_str key, const struct mg_str value,
ret = true;
break;
}
case CONF_TYPE_FLOAT: {
float *vp = (float *) (((char *) cfg) + e->offset);
char *endptr;
value_nul = mg_strdup_nul(value);
*vp = strtof(value_nul.p, &endptr);
if (endptr != value_nul.p + value_nul.len) goto out;
ret = true;
break;
}
case CONF_TYPE_DOUBLE: {
double *vp = (double *) (((char *) cfg) + e->offset);
char *endptr;
Expand Down
42 changes: 28 additions & 14 deletions src/test/data/golden/mgos_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

/* struct mgos_config */
static const struct mgos_conf_entry mgos_config_schema_[] = {
{.type = CONF_TYPE_OBJECT, .key = "", .offset = 0, .num_desc = 38},
{.type = CONF_TYPE_OBJECT, .key = "", .offset = 0, .num_desc = 40},
{.type = CONF_TYPE_OBJECT, .key = "wifi", .offset = offsetof(struct mgos_config, wifi), .num_desc = 8},
{.type = CONF_TYPE_OBJECT, .key = "sta", .offset = offsetof(struct mgos_config, wifi.sta), .num_desc = 2},
{.type = CONF_TYPE_STRING, .key = "ssid", .offset = offsetof(struct mgos_config, wifi.sta.ssid)},
Expand All @@ -27,12 +27,14 @@ static const struct mgos_conf_entry mgos_config_schema_[] = {
{.type = CONF_TYPE_OBJECT, .key = "http", .offset = offsetof(struct mgos_config, http), .num_desc = 2},
{.type = CONF_TYPE_BOOL, .key = "enable", .offset = offsetof(struct mgos_config, http.enable)},
{.type = CONF_TYPE_INT, .key = "port", .offset = offsetof(struct mgos_config, http.port)},
{.type = CONF_TYPE_OBJECT, .key = "debug", .offset = offsetof(struct mgos_config, debug), .num_desc = 7},
{.type = CONF_TYPE_OBJECT, .key = "debug", .offset = offsetof(struct mgos_config, debug), .num_desc = 9},
{.type = CONF_TYPE_INT, .key = "level", .offset = offsetof(struct mgos_config, debug.level)},
{.type = CONF_TYPE_STRING, .key = "dest", .offset = offsetof(struct mgos_config, debug.dest)},
{.type = CONF_TYPE_STRING, .key = "file_level", .offset = offsetof(struct mgos_config, debug.file_level)},
{.type = CONF_TYPE_DOUBLE, .key = "test_d1", .offset = offsetof(struct mgos_config, debug.test_d1)},
{.type = CONF_TYPE_DOUBLE, .key = "test_d2", .offset = offsetof(struct mgos_config, debug.test_d2)},
{.type = CONF_TYPE_FLOAT, .key = "test_f1", .offset = offsetof(struct mgos_config, debug.test_f1)},
{.type = CONF_TYPE_FLOAT, .key = "test_f2", .offset = offsetof(struct mgos_config, debug.test_f2)},
{.type = CONF_TYPE_UNSIGNED_INT, .key = "test_ui", .offset = offsetof(struct mgos_config, debug.test_ui)},
{.type = CONF_TYPE_OBJECT, .key = "empty", .offset = offsetof(struct mgos_config, debug.empty), .num_desc = 0},
{.type = CONF_TYPE_OBJECT, .key = "test", .offset = offsetof(struct mgos_config, test), .num_desc = 16},
Expand Down Expand Up @@ -107,7 +109,7 @@ void mgos_config_http_set_defaults(struct mgos_config_http *cfg) {

/* struct mgos_config_debug_empty */
const struct mgos_conf_entry *mgos_config_debug_empty_get_schema(void) {
return &mgos_config_schema_[21];
return &mgos_config_schema_[23];
}

void mgos_config_debug_empty_set_defaults(struct mgos_config_debug_empty *cfg) {
Expand All @@ -125,13 +127,15 @@ void mgos_config_debug_set_defaults(struct mgos_config_debug *cfg) {
cfg->file_level = "mg_foo.c=4";
cfg->test_d1 = 2.0;
cfg->test_d2 = 0.0;
cfg->test_f1 = 0.123;
cfg->test_f2 = 123.0;
cfg->test_ui = 4294967295;
mgos_config_debug_empty_set_defaults(&cfg->empty);
}

/* struct mgos_config_baz */
const struct mgos_conf_entry *mgos_config_baz_get_schema(void) {
return &mgos_config_schema_[37];
return &mgos_config_schema_[39];
}

void mgos_config_baz_set_defaults(struct mgos_config_baz *cfg) {
Expand All @@ -140,7 +144,7 @@ void mgos_config_baz_set_defaults(struct mgos_config_baz *cfg) {

/* struct mgos_config_bar_inner */
const struct mgos_conf_entry *mgos_config_bar_inner_get_schema(void) {
return &mgos_config_schema_[34];
return &mgos_config_schema_[36];
}

void mgos_config_bar_inner_set_defaults(struct mgos_config_bar_inner *cfg) {
Expand All @@ -150,7 +154,7 @@ void mgos_config_bar_inner_set_defaults(struct mgos_config_bar_inner *cfg) {

/* struct mgos_config_baz */
const struct mgos_conf_entry *mgos_config_bar_baz_get_schema(void) {
return &mgos_config_schema_[37];
return &mgos_config_schema_[39];
}

void mgos_config_bar_baz_set_defaults(struct mgos_config_baz *cfg) {
Expand All @@ -159,7 +163,7 @@ void mgos_config_bar_baz_set_defaults(struct mgos_config_baz *cfg) {

/* struct mgos_config_bar */
const struct mgos_conf_entry *mgos_config_bar_get_schema(void) {
return &mgos_config_schema_[31];
return &mgos_config_schema_[33];
}

void mgos_config_bar_set_defaults(struct mgos_config_bar *cfg) {
Expand All @@ -171,7 +175,7 @@ void mgos_config_bar_set_defaults(struct mgos_config_bar *cfg) {

/* struct mgos_config_bar_inner */
const struct mgos_conf_entry *mgos_config_test_bar1_inner_get_schema(void) {
return &mgos_config_schema_[34];
return &mgos_config_schema_[36];
}

void mgos_config_test_bar1_inner_set_defaults(struct mgos_config_bar_inner *cfg) {
Expand All @@ -181,7 +185,7 @@ void mgos_config_test_bar1_inner_set_defaults(struct mgos_config_bar_inner *cfg)

/* struct mgos_config_baz */
const struct mgos_conf_entry *mgos_config_test_bar1_baz_get_schema(void) {
return &mgos_config_schema_[37];
return &mgos_config_schema_[39];
}

void mgos_config_test_bar1_baz_set_defaults(struct mgos_config_baz *cfg) {
Expand All @@ -190,7 +194,7 @@ void mgos_config_test_bar1_baz_set_defaults(struct mgos_config_baz *cfg) {

/* struct mgos_config_bar */
const struct mgos_conf_entry *mgos_config_test_bar1_get_schema(void) {
return &mgos_config_schema_[31];
return &mgos_config_schema_[33];
}

void mgos_config_test_bar1_set_defaults(struct mgos_config_bar *cfg) {
Expand All @@ -202,7 +206,7 @@ void mgos_config_test_bar1_set_defaults(struct mgos_config_bar *cfg) {

/* struct mgos_config_bar_inner */
const struct mgos_conf_entry *mgos_config_test_bar2_inner_get_schema(void) {
return &mgos_config_schema_[34];
return &mgos_config_schema_[36];
}

void mgos_config_test_bar2_inner_set_defaults(struct mgos_config_bar_inner *cfg) {
Expand All @@ -212,7 +216,7 @@ void mgos_config_test_bar2_inner_set_defaults(struct mgos_config_bar_inner *cfg)

/* struct mgos_config_baz */
const struct mgos_conf_entry *mgos_config_test_bar2_baz_get_schema(void) {
return &mgos_config_schema_[37];
return &mgos_config_schema_[39];
}

void mgos_config_test_bar2_baz_set_defaults(struct mgos_config_baz *cfg) {
Expand All @@ -221,7 +225,7 @@ void mgos_config_test_bar2_baz_set_defaults(struct mgos_config_baz *cfg) {

/* struct mgos_config_bar */
const struct mgos_conf_entry *mgos_config_test_bar2_get_schema(void) {
return &mgos_config_schema_[31];
return &mgos_config_schema_[33];
}

void mgos_config_test_bar2_set_defaults(struct mgos_config_bar *cfg) {
Expand All @@ -233,7 +237,7 @@ void mgos_config_test_bar2_set_defaults(struct mgos_config_bar *cfg) {

/* struct mgos_config_test */
const struct mgos_conf_entry *mgos_config_test_get_schema(void) {
return &mgos_config_schema_[22];
return &mgos_config_schema_[24];
}

void mgos_config_test_set_defaults(struct mgos_config_test *cfg) {
Expand Down Expand Up @@ -364,6 +368,16 @@ double mgos_config_get_debug_test_d2(const struct mgos_config *cfg) { return cfg
double mgos_config_get_default_debug_test_d2(void) { return 0.0; }
void mgos_config_set_debug_test_d2(struct mgos_config *cfg, double v) { cfg->debug.test_d2 = v; }

/* debug.test_f1 */
float mgos_config_get_debug_test_f1(const struct mgos_config *cfg) { return cfg->debug.test_f1; }
float mgos_config_get_default_debug_test_f1(void) { return 0.123; }
void mgos_config_set_debug_test_f1(struct mgos_config *cfg, float v) { cfg->debug.test_f1 = v; }

/* debug.test_f2 */
float mgos_config_get_debug_test_f2(const struct mgos_config *cfg) { return cfg->debug.test_f2; }
float mgos_config_get_default_debug_test_f2(void) { return 123.0; }
void mgos_config_set_debug_test_f2(struct mgos_config *cfg, float v) { cfg->debug.test_f2 = v; }

/* debug.test_ui */
unsigned int mgos_config_get_debug_test_ui(const struct mgos_config *cfg) { return cfg->debug.test_ui; }
unsigned int mgos_config_get_default_debug_test_ui(void) { return 4294967295; }
Expand Down
22 changes: 22 additions & 0 deletions src/test/data/golden/mgos_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ struct mgos_config_debug {
const char * file_level;
double test_d1;
double test_d2;
float test_f1;
float test_f2;
unsigned int test_ui;
struct mgos_config_debug_empty empty;
};
Expand Down Expand Up @@ -650,6 +652,26 @@ static inline double mgos_sys_config_get_default_debug_test_d2(void) { return mg
void mgos_config_set_debug_test_d2(struct mgos_config *cfg, double v);
static inline void mgos_sys_config_set_debug_test_d2(double v) { mgos_config_set_debug_test_d2(&mgos_sys_config, v); }

/* debug.test_f1 */
#define MGOS_CONFIG_HAVE_DEBUG_TEST_F1
#define MGOS_SYS_CONFIG_HAVE_DEBUG_TEST_F1
float mgos_config_get_debug_test_f1(const struct mgos_config *cfg);
float mgos_config_get_default_debug_test_f1(void);
static inline float mgos_sys_config_get_debug_test_f1(void) { return mgos_config_get_debug_test_f1(&mgos_sys_config); }
static inline float mgos_sys_config_get_default_debug_test_f1(void) { return mgos_config_get_default_debug_test_f1(); }
void mgos_config_set_debug_test_f1(struct mgos_config *cfg, float v);
static inline void mgos_sys_config_set_debug_test_f1(float v) { mgos_config_set_debug_test_f1(&mgos_sys_config, v); }

/* debug.test_f2 */
#define MGOS_CONFIG_HAVE_DEBUG_TEST_F2
#define MGOS_SYS_CONFIG_HAVE_DEBUG_TEST_F2
float mgos_config_get_debug_test_f2(const struct mgos_config *cfg);
float mgos_config_get_default_debug_test_f2(void);
static inline float mgos_sys_config_get_debug_test_f2(void) { return mgos_config_get_debug_test_f2(&mgos_sys_config); }
static inline float mgos_sys_config_get_default_debug_test_f2(void) { return mgos_config_get_default_debug_test_f2(); }
void mgos_config_set_debug_test_f2(struct mgos_config *cfg, float v);
static inline void mgos_sys_config_set_debug_test_f2(float v) { mgos_config_set_debug_test_f2(&mgos_sys_config, v); }

/* debug.test_ui */
#define MGOS_CONFIG_HAVE_DEBUG_TEST_UI
#define MGOS_SYS_CONFIG_HAVE_DEBUG_TEST_UI
Expand Down
4 changes: 3 additions & 1 deletion src/test/data/golden/mgos_config_pretty.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
"dest": "uart1",
"file_level": "mgos_bar=1",
"test_d1": 2.000000,
"test_d2": 0.000000,
"test_d2": 111.000000,
"test_f1": 0.123000,
"test_f2": 11.500000,
"test_ui": 4294967295,
"empty": {
}
Expand Down
2 changes: 2 additions & 0 deletions src/test/data/golden/mgos_config_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
["debug.file_level", "s", {"title": "File level"}],
["debug.test_d1", "d", {"title": "Test doubles 1"}],
["debug.test_d2", "d", {}],
["debug.test_f1", "f", {"title": "Test float 1"}],
["debug.test_f2", "f", {}],
["debug.test_ui", "ui", {}],
["debug.empty", "o", {"title": "Empty object with no fields"}],
["test", "o", {}],
Expand Down
4 changes: 3 additions & 1 deletion src/test/data/overrides.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
},
"debug": {
"level": 1,
"file_level": "mgos_bar=1"
"file_level": "mgos_bar=1",
"test_d2": 111,
"test_f2": 11.5
}
}
2 changes: 2 additions & 0 deletions src/test/data/sys_conf_debug.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
["debug.file_level", "s", "mg_foo.c=4", {title: "File level"}],
["debug.test_d1", "d", 0.123, {title: "Test doubles 1"}],
["debug.test_d2", "d", 0, {}],
["debug.test_f1", "f", 0.123, {title: "Test float 1"}],
["debug.test_f2", "f", 123, {}],
["debug.test_ui", "ui", 4294967295, {}],
["debug.empty", "o", {title: "Empty object with no fields"}],

Expand Down
5 changes: 5 additions & 0 deletions src/test/unit_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ static const char *test_config(void) {
ASSERT_EQ(conf.debug.level, 1); /* Override integer */
ASSERT(conf.wifi.ap.pass == NULL); /* Reset string - set to NULL */
ASSERT_EQ(conf.http.enable, 0); /* Override boolean */
ASSERT_EQ(conf.debug.test_d2, 111.0);
ASSERT_EQ(sizeof(conf.debug.test_d2), sizeof(double));
ASSERT_EQ(conf.debug.test_f2, 11.5);
ASSERT_EQ(sizeof(conf.debug.test_f2), sizeof(float));

ASSERT_EQ(conf.test.bar1.param1, 1111);
ASSERT_EQ(conf.test.bar2.param1, 2222);
Expand All @@ -70,6 +74,7 @@ static const char *test_config(void) {
ASSERT_STREQ(conf.debug.file_level, conf_debug.file_level);
ASSERT_EQ(conf.debug.level, conf_debug.level);
ASSERT_EQ(conf.debug.test_d1, conf_debug.test_d1);
ASSERT_EQ(conf.debug.test_f1, conf_debug.test_f1);

mgos_config_debug_free(&conf_debug);

Expand Down
Loading

0 comments on commit 9a1170d

Please sign in to comment.