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, {}]
...
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?
Comments
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_sub
for this matter. BTW it would be much simpler do handle this in MJS.Підтримки масивів немає.
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.
@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?
mos config-get app
@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.
@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.