Fork me on GitHub

Configuration infrastructure

The detailed description of the configuration infrastrucuture is given in the dedicated documentation section . This document covers configuration API for both C/C++ and JavaScript.

C API Reference mgos_config.h

#include "mgos_config.h"

See on GitHub: mgos_config.h, mgos_config.c

The usage pattern is this:

  1. Create an empty config struct at the beginning.
  2. Load the defaults.
  3. Then, apply overrides.

When override is applied, previously allocated values are freed.


bool mgos_conf_check_access(const struct mg_str key, const char *acl);

enum mgos_conf_type {
  CONF_TYPE_INT = 0,
  CONF_TYPE_BOOL = 1,
  CONF_TYPE_DOUBLE = 2,
  CONF_TYPE_STRING = 3,
  CONF_TYPE_OBJECT = 4,
};

struct mgos_conf_entry {
  enum mgos_conf_type type;
  const char *key;
  union {
    int offset;
    int num_desc;
  };

bool mgos_conf_parse(const struct mg_str json, const char *acl,
                     const struct mgos_conf_entry *schema, void *cfg);

Parses config in 'json' into 'cfg' according to rules defined in 'schema' and checking keys against 'acl'.

typedef void (*mgos_conf_emit_cb_t)(struct mbuf *data, void *param);

Emit config in 'cfg' according to rules in 'schema'. Keys are only emitted if their values are different from 'base'. If 'base' is NULL then all keys are emitted.

void mgos_conf_emit_cb(const void *cfg, const void *base,
                       const struct mgos_conf_entry *schema, bool pretty,
                       struct mbuf *out, mgos_conf_emit_cb_t cb,
                       void *cb_param);

bool mgos_conf_emit_f(const void *cfg, const void *base,
                      const struct mgos_conf_entry *schema, bool pretty,
                      const char *fname);

void mgos_conf_free(const struct mgos_conf_entry *schema, void *cfg);

Frees any resources allocated in 'cfg'.

const struct mgos_conf_entry *mgos_conf_find_schema_entry(
    const char *path, const struct mgos_conf_entry *obj);

const struct mgos_conf_entry *mgos_conf_find_schema_entry_s(
    const struct mg_str path, const struct mgos_conf_entry *obj);

void mgos_conf_set_str(char **vp, const char *v);

enum mgos_conf_type mgos_conf_value_type(struct mgos_conf_entry *e);

Returns a type of the value (this function is primarily for FFI)

const char *mgos_conf_value_string(const void *cfg,
                                   const struct mgos_conf_entry *e);

Returns a string value from the config entry. If the value is empty, returns NULL.

const char *mgos_conf_value_string_nonnull(const void *cfg,
                                           const struct mgos_conf_entry *e);

Same as mgos_conf_value_string(), but returns an empty string instead of NULL when the value is empty.

int mgos_conf_value_int(const void *cfg, const struct mgos_conf_entry *e);

Returns an int or bool value from the config entry

double mgos_conf_value_double(const void *cfg, const struct mgos_conf_entry *e);

Returns a double value from the config entry

JAVASCRIPT API Reference api_config.js

load("api_config.js");


Cfg.get(path)

Get the config value by the configuration variable. Currently, only simple types are returned: strings, ints, booleans, doubles. Objects are not yet supported.

Examples:

load('api_config.js');
Cfg.get('device.id');        // returns a string
Cfg.get('debug.level');      // returns an integer
Cfg.get('wifi.sta.enable');  // returns a boolean