Copyright ©

Mongoose OS Forum

ATTENTION! This forum has moved to:

Do not post any new messages.

Store array in the config

Hi all.

I want to describe some data with same properties in config file and iterate through them from C code.
Data structure looks like next:

- ["uart_prot.registers", "o", {title: "Device registers"}]
- ["uart_prot.registers.read_temp", "o", {title: "Read Temp sensor value"}]
- ["uart_prot.registers.read_temp.reg", "i", 0x00, {}]
- ["uart_prot.registers.read_temp.type", "s", "R", {}]
- ["uart_prot.registers.read_temp.size", "i", 2, {}]
- ["uart_prot.registers.read_temp.value", "i", 0x00, {}]
- ["uart_prot.registers.read_hum", "o", {title: "Read Humidity value"}]
- ["uart_prot.registers.read_hum.reg", "i", 0x01, {}]
- ["uart_prot.registers.read_hum.type", "s", "R", {}]
- ["uart_prot.registers.read_hum.size", "i", 2, {}]
- ["uart_prot.registers.read_hum.value", "i", 0x00, {}]

and such config generates next structure in c header:

struct mgos_config_uart_prot_registers {
    struct mgos_config_uart_prot_registers_read_temp read_temp;
    struct mgos_config_uart_prot_registers_read_hum read_hum;

struct mgos_config_uart_prot_registers_read_temp {
  int reg;
  char *type;
  int size;
  int value;

struct mgos_config_uart_prot_registers_read_hum {
  int reg;
  char *type;
  int size;
  int value;

All I want is just to get mgos_config_uart_prot_registers and iterate through all registers with possibilities to get reg, type, size and value properties.
Can somebody help me with that? Or it is impossible to do from config?


  • mamuespmamuesp Germany/Northern coast
    edited September 2018

    Well, as it is plain C, you have no possibility to traverse through a struct. I don't know if X-Macros are useable under Mongoose-OS, so I solve such problems like this:
    - ["uart_prot.registers.names, "s", "temp,hum", {title: "Device register names"}]
    and the read this config entry, split the names into an array (there are several solutions also in MJS in the wild ...) an traverse this array, generate the proper keys for your config structures and load them. Look for mgos_sys_config_parse_subfor this matter. BTW it would be much simpler do handle this in MJS.

    Thanked by 1UaHeadHunter
  • SergeySergey Dublin, Ireland

    Підтримки масивів немає.
    You can simulate arrays with numbered keys, like {"myapp": {"key1": ..., "key2": ..., }}
    But there is no native support at this point.

    I think the biggest problem with arrays is how do you specify it: a structure that describes an element, and an array length.

    Thanked by 1UaHeadHunter
  • UaHeadHunterUaHeadHunter Ukraine
    edited September 2018

    @mamuesp - thank you, I will try that one.

    @Sergey - can you make an example with correct mos.yml config syntax? Cause I was trying to have a JSON Object value without any luck

  • Something like this?

     - ["debug.level", 2]
     - ["app", "o", {title: "My app settings"}]
     - ["app.key1", "o", {title: "key1"}]
     - ["app.key1.int_value", "i", 1, {title: "key1 integer"}]
     - ["app.key1.str", "s", "str1", {title: "key1 string"}]
     - ["app.key2", "o", {title: "key2"}]
     - ["app.key2.int_value", "i", 2, {title: "key2 integer"}]
     - ["app.key2.str", "s", "str2", {title: "key2 string"}]
     - ["app.key3", "o", {title: "key3"}]
     - ["app.key3.int_value", "i", 3, {title: "key3 integer"}]
     - ["app.key3.str", "s", "str3", {title: "key3 string"}]

    mos config-get app

      "key1": {
        "int_value": 1,
        "str": "str1"
      "key2": {
        "int_value": 2,
        "str": "str2"
      "key3": {
        "int_value": 3,
        "str": "str3"
    Thanked by 1Sergey
  • mamuespmamuesp Germany/Northern coast

    @UaHeadHunter - If you like, I may post you an MJS example (and how to access the result in C) - but give me sone days. MJS might be the better solution (indeed a little slower though) here, because of the higher flexibility in string handling and object access. With some FFIing you may write the traversed result in an array accessible in C, so you have the best of both worlds.

    Thanked by 1UaHeadHunter
  • @nliviu Basically, yes, but I need it in C (not mJS).
    @mamuesp Please, don't spend too much time on it if it is time-consuming. I thought maybe I'm missing something, but now I understand that it's not a trivial functional.

Sign In or Register to comment.