Skip to content

Commit

Permalink
Add unsigned int config type ("ui")
Browse files Browse the repository at this point in the history
```
["myapp.test_ui", "ui", 4294967295, {}],
```

#525

CL: Add unsigned int config type ("ui")
  • Loading branch information
rojer9-fb committed Jan 10, 2020
1 parent 4d80044 commit cabac8c
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 10 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_DOUBLE = 2,
CONF_TYPE_STRING = 3,
CONF_TYPE_OBJECT = 4,
CONF_TYPE_UNSIGNED_INT = 5,
};

/* Configuration entry */
Expand Down
30 changes: 26 additions & 4 deletions src/mgos_config_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ void mgos_conf_parse_cb(void *data, const char *name, size_t name_len,
/* fall through */
#endif
case CONF_TYPE_INT:
/* fall through */
case CONF_TYPE_UNSIGNED_INT:
if (tok->type != JSON_TYPE_NUMBER) {
LOG(LL_ERROR, ("[%s] is not a number", path));
ctx->result = false;
Expand All @@ -134,6 +136,9 @@ void mgos_conf_parse_cb(void *data, const char *name, size_t name_len,
/* NB: Using base 0 to accept hex numbers. */
*((int *) vp) = strtol(tok->ptr, &endptr, 0);
break;
case CONF_TYPE_UNSIGNED_INT:
*((unsigned int *) vp) = strtoul(tok->ptr, &endptr, 0);
break;
#ifndef MGOS_BOOT_BUILD
case CONF_TYPE_DOUBLE:
*((double *) vp) = strtod(tok->ptr, &endptr);
Expand Down Expand Up @@ -239,7 +244,10 @@ static bool mgos_conf_value_eq(const void *cfg, const void *base,
char *bvp = (((char *) base) + e->offset);
switch (e->type) {
case CONF_TYPE_INT:
/* fall through */
case CONF_TYPE_BOOL:
/* fall through */
case CONF_TYPE_UNSIGNED_INT:
return *((int *) vp) == *((int *) bvp);
case CONF_TYPE_DOUBLE:
return *((double *) vp) == *((double *) bvp);
Expand Down Expand Up @@ -273,8 +281,9 @@ static void mgos_conf_emit_entry(struct emit_ctx *ctx,
char buf[40];
int len;
switch (e->type) {
case CONF_TYPE_INT: {
len = snprintf(buf, sizeof(buf), "%d",
case CONF_TYPE_INT:
case CONF_TYPE_UNSIGNED_INT: {
len = snprintf(buf, sizeof(buf), (e->type == CONF_TYPE_INT ? "%d" : "%u"),
*((int *) (((char *) ctx->cfg) + e->offset)));
mbuf_append(ctx->out, buf, len);
break;
Expand Down Expand Up @@ -465,7 +474,8 @@ const char *mgos_conf_value_string_nonnull(const void *cfg,

int mgos_conf_value_int(const void *cfg, const struct mgos_conf_entry *e) {
char *vp = (((char *) cfg) + e->offset);
if (e->type == CONF_TYPE_INT || e->type == CONF_TYPE_BOOL) {
if (e->type == CONF_TYPE_INT || e->type == CONF_TYPE_UNSIGNED_INT ||
e->type == CONF_TYPE_BOOL) {
return *((int *) vp);
}
return 0;
Expand All @@ -490,7 +500,10 @@ bool mgos_config_get(const struct mg_str key, struct mg_str *value,
cp = (char **) &value->p;
switch (e->type) {
case CONF_TYPE_INT:
value->len = mg_asprintf(cp, 0, "%d", mgos_conf_value_int(cfg, e));
/* fall through */
case CONF_TYPE_UNSIGNED_INT:
value->len = mg_asprintf(cp, 0, (e->type == CONF_TYPE_INT ? "%d" : "%u"),
mgos_conf_value_int(cfg, e));
break;
case CONF_TYPE_BOOL:
value->len = mg_asprintf(
Expand Down Expand Up @@ -538,6 +551,15 @@ bool mgos_config_set(const struct mg_str key, const struct mg_str value,
ret = true;
break;
}
case CONF_TYPE_UNSIGNED_INT: {
unsigned int *vp = (unsigned int *) (((char *) cfg) + e->offset);
char *endptr;
value_nul = mg_strdup_nul(value);
*vp = strtoul(value_nul.p, &endptr, 10);
if (endptr != value_nul.p + value_nul.len) goto out;
ret = true;
break;
}
case CONF_TYPE_BOOL: {
int *vp = (int *) (((char *) cfg) + e->offset);
if (mg_vcmp(&value, "true") == 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ INCS = -I$(REPO_ROOT)/src \
-I. \
$(CFLAGS_EXTRA)

CFLAGS = -W -Wall -Werror -g -O0 -Wno-multichar -I$(BUILD_DIR) $(INCS)
CFLAGS = -W -Wall -Wextra -Werror -g -O0 -Wno-multichar -I$(BUILD_DIR) $(INCS)

all: $(BUILD_DIR) $(PROG)
./$(PROG)
Expand Down
18 changes: 15 additions & 3 deletions src/test/data/golden/mgos_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

#include "mgos_config_util.h"

const struct mgos_conf_entry mgos_config_schema_[26] = {
{.type = CONF_TYPE_OBJECT, .key = "", .offset = 0, .num_desc = 25},
const struct mgos_conf_entry mgos_config_schema_[27] = {
{.type = CONF_TYPE_OBJECT, .key = "", .offset = 0, .num_desc = 26},
{.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 @@ -25,11 +25,12 @@ const struct mgos_conf_entry mgos_config_schema_[26] = {
{.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 = 4},
{.type = CONF_TYPE_OBJECT, .key = "debug", .offset = offsetof(struct mgos_config, debug), .num_desc = 5},
{.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_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_UNSIGNED_INT, .key = "test_ui", .offset = offsetof(struct mgos_config, debug.test_ui)},
{.type = CONF_TYPE_OBJECT, .key = "test", .offset = offsetof(struct mgos_config, test), .num_desc = 6},
{.type = CONF_TYPE_OBJECT, .key = "bar", .offset = offsetof(struct mgos_config, test.bar), .num_desc = 2},
{.type = CONF_TYPE_BOOL, .key = "enable", .offset = offsetof(struct mgos_config, test.bar.enable)},
Expand Down Expand Up @@ -59,6 +60,7 @@ const struct mgos_config mgos_config_defaults = {
.debug.dest = "uart1",
.debug.test_d1 = 2.0,
.debug.test_d2 = 0.0,
.debug.test_ui = 4294967295,
.test.bar.enable = 0,
.test.bar.param1 = 111,
.test.bar1.enable = 0,
Expand Down Expand Up @@ -230,6 +232,16 @@ void mgos_config_set_debug_test_d2(struct mgos_config *cfg, double v) {
cfg->debug.test_d2 = v;
}

/* debug.test_ui */
#define MGOS_CONFIG_HAVE_DEBUG_TEST_UI
#define MGOS_SYS_CONFIG_HAVE_DEBUG_TEST_UI
unsigned int mgos_config_get_debug_test_ui(struct mgos_config *cfg) {
return cfg->debug.test_ui;
}
void mgos_config_set_debug_test_ui(struct mgos_config *cfg, unsigned int v) {
cfg->debug.test_ui = v;
}

/* test */
#define MGOS_CONFIG_HAVE_TEST
#define MGOS_SYS_CONFIG_HAVE_TEST
Expand Down
9 changes: 9 additions & 0 deletions src/test/data/golden/mgos_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ struct mgos_config_debug {
const char * dest;
double test_d1;
double test_d2;
unsigned int test_ui;
};

struct mgos_config_test_bar {
Expand Down Expand Up @@ -200,6 +201,14 @@ static inline double mgos_sys_config_get_debug_test_d2(void) { return mgos_confi
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_ui */
#define MGOS_CONFIG_HAVE_DEBUG_TEST_UI
#define MGOS_SYS_CONFIG_HAVE_DEBUG_TEST_UI
unsigned int mgos_config_get_debug_test_ui(struct mgos_config *cfg);
static inline unsigned int mgos_sys_config_get_debug_test_ui(void) { return mgos_config_get_debug_test_ui(&mgos_sys_config); }
void mgos_config_set_debug_test_ui(struct mgos_config *cfg, unsigned int v);
static inline void mgos_sys_config_set_debug_test_ui(unsigned int v) { mgos_config_set_debug_test_ui(&mgos_sys_config, v); }

/* test */
#define MGOS_CONFIG_HAVE_TEST
#define MGOS_SYS_CONFIG_HAVE_TEST
Expand Down
1 change: 1 addition & 0 deletions src/test/data/golden/mgos_config_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
["debug.dest", "s", {"title": "Where to send debug"}],
["debug.test_d1", "d", {"title": "Test doubles 1"}],
["debug.test_d2", "d", {}],
["debug.test_ui", "ui", {}],
["test", "o", {}],
["test.bar", "o", {}],
["test.bar.enable", "b", {}],
Expand Down
1 change: 1 addition & 0 deletions src/test/data/sys_conf_debug.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
["debug.dest", "s", "uart1", {title: "Where to send debug"}],
["debug.test_d1", "d", 0.123, {title: "Test doubles 1"}],
["debug.test_d2", "d", 0, {}],
["debug.test_ui", "ui", 4294967295, {}],
["test.bar", "o", {}],
["test.bar.enable", "b", {}],
["test.bar.param1", "i", 111, {}],
Expand Down
12 changes: 11 additions & 1 deletion src/test/unit_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,14 @@ static const char *test_config(void) {
ASSERT(conf.wifi.ap.pass == NULL); /* Reset string - set to NULL */
ASSERT_EQ(conf.http.enable, 0); /* Override boolean */

/* Test global accessors */
/* Test accessors */
ASSERT_EQ(mgos_config_get_wifi_ap_channel(&conf), 6);
ASSERT_EQ(mgos_config_get_debug_test_ui(&conf), 4294967295);

/* Test global accessors */
ASSERT_EQ(mgos_sys_config_get_wifi_ap_channel(), 0);
mgos_sys_config_set_wifi_ap_channel(123);
ASSERT_EQ(mgos_sys_config_get_wifi_ap_channel(), 123);

mgos_conf_free(schema, &conf);

Expand All @@ -54,6 +60,10 @@ static const char *test_config(void) {
return NULL;
}

#ifndef MGOS_CONFIG_HAVE_DEBUG_LEVEL
#error MGOS_CONFIG_HAVE_xxx must be defined
#endif

static const char *test_json_scanf(void) {
int a = 0;
bool b = false;
Expand Down
13 changes: 12 additions & 1 deletion tools/mgos_gen_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@

class SchemaEntry(object):
V_INT = "i"
V_UNSIGNED_INT = "ui"
V_BOOL = "b"
V_DOUBLE = "d"
V_STRING = "s"
Expand All @@ -124,6 +125,8 @@ def __init__(self, e):
self.default = False
elif self.vtype == SchemaEntry.V_INT:
self.default = 0
elif self.vtype == SchemaEntry.V_UNSIGNED_INT:
self.default = 0
elif self.vtype == SchemaEntry.V_DOUBLE:
self.default = 0.0
elif self.vtype == SchemaEntry.V_STRING:
Expand Down Expand Up @@ -157,26 +160,33 @@ def __init__(self, e):
raise TypeError("%s: Invalid params" % self.path)

def IsPrimitiveType(self):
return self.vtype in (self.V_OBJECT, self.V_BOOL, self.V_INT, self.V_DOUBLE, self.V_STRING)
return self.vtype in (
self.V_OBJECT, self.V_BOOL, self.V_INT, self.V_UNSIGNED_INT,
self.V_DOUBLE, self.V_STRING)


def ValidateDefault(self):
if self.vtype == SchemaEntry.V_DOUBLE and type(self.default) is int:
self.default = float(self.default)
if (self.vtype == SchemaEntry.V_BOOL and not isinstance(self.default, bool) or
self.vtype == SchemaEntry.V_INT and not isinstance(self.default, int) or
self.vtype == SchemaEntry.V_UNSIGNED_INT and not isinstance(self.default, int) or
# In Python, boolvalue is an instance of int, but we don't want that.
self.vtype == SchemaEntry.V_INT and isinstance(self.default, bool) or
self.vtype == SchemaEntry.V_DOUBLE and not isinstance(self.default, float) or
self.vtype == SchemaEntry.V_STRING and not isinstance(self.default, str)):
raise TypeError("%s: Invalid default value type (%s)" % (self.path, type(self.default)))
if self.vtype == SchemaEntry.V_UNSIGNED_INT and self.default < 0:
raise TypeError("%s: Invalid default unsigned value (%d)" % (self.path, self.default))

def GetIdentifierName(self):
return self.path.replace(".", "_")

def GetCType(self, struct_prefix):
if self.vtype in (SchemaEntry.V_BOOL, SchemaEntry.V_INT):
return "int"
elif self.vtype == SchemaEntry.V_UNSIGNED_INT:
return "unsigned int"
elif self.vtype == SchemaEntry.V_DOUBLE:
return "double"
elif self.vtype == SchemaEntry.V_STRING:
Expand Down Expand Up @@ -553,6 +563,7 @@ def __str__(self):
class CWriter(object):
_CONF_TYPES = {
SchemaEntry.V_INT: "CONF_TYPE_INT",
SchemaEntry.V_UNSIGNED_INT: "CONF_TYPE_UNSIGNED_INT",
SchemaEntry.V_BOOL: "CONF_TYPE_BOOL",
SchemaEntry.V_DOUBLE: "CONF_TYPE_DOUBLE",
SchemaEntry.V_STRING: "CONF_TYPE_STRING",
Expand Down

0 comments on commit cabac8c

Please sign in to comment.