Python script for code generation from JSON file (JSON -> user settings)
Opened this issue · 0 comments
TjazVracko commented
Required steps / Implementation details
- Add a
library/codegen
folder and add the script to it - The script:
- takes the json file path as a cmd line argument (use the argparse lib to parse)
- read and validate the json file (a json schema might be useful here)
- Generate a .c and .h pair of files with code that will add each setting to the lib and set the default values. See here for what Eva has done on Bbraun. Let's have a discussion on how exactly we want this to look in the end. I wrote a short example at the end here.
- Write a "wrapping" .c file that: has a
SYS_INIT
call that will:- initialize the user settings lib
- call the generated "add" function
- load the settings
- call the generated "set defaults" function
Definition of Done
Code is written, tested and reviewed.
Additional context - JSON FORMAT
The JSON format is defined as follows
{
"version": 1, // denotes the version of the user-settings JSON format
"settings": {
"<name_of_setting_1>": {
"id": <number>, // required
"type": <string (valid type)>, // required
"size": <integer>, // required for string and bytes type, otherwise must not be present
"default": <default value in specified type>
},
"<name_of_setting_2>": {
// ...
}
// ...
}
}
For example:
{
"version": 1,
"settings": {
"first_name": {
"id": 1,
"type": "string",
"size": 50,
"default": "Luka"
},
"lorawan_class": {
"id": 2,
"type": "uint8_t",
"default": 0
}
}
}
Full example: https://github.com/IRNAS/bbraun-gravityplus-firmware/blob/dev/scripts/settings/settings.json
Additional context - Generated code
This is a stub of the generated code. The full generated files require all the docstrings, ifdefs and everything that is expected in the c header and source files.
/* h file */
enum user_settings_gen_id {
// this contains a list of IDs with enum "names" being all-caps names of the settings
USER_SETTINGS_ID_FIRST_NAME = 1,
USER_SETTINGS_ID_LORAWAN_CLASS = 2,
};
/* final name of function pending */
void user_settings_gen_add(void);
/* final name of function pending */
void user_settings_gen_set_defaults(void);
/* c file */
/* add each setting */
void user_settings_gen_add(void)
{
user_settings_add_sized(1, "first_name", USER_SETTINGS_TYPE_STR, 50);
user_settings_add(2, "lorawan_class", USER_SETTINGS_TYPE_U8);
// ...
}
/* check the default and set the default value for each setting */
/* NOTE: it would be nice to check the default for each setting before setting it, and asserting if there is already an existing default with a different value - the device flash must be deleted in this case. */
void user_settings_gen_set_defaults(void)
{
user_settings_set_default_with_id(1, "Luka", 5);
uint8_t lorawan_class = 1;
user_settings_set_default_with_id(2, &lorawan_class, 1);
}