Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python script for code generation from JSON file (JSON -> user settings) #21

Open
Tracked by #4
TjazVracko opened this issue Jan 27, 2023 · 0 comments
Open
Tracked by #4
Labels
type: feature request New feature or request.

Comments

@TjazVracko
Copy link
Collaborator

TjazVracko commented Jan 27, 2023

Required steps / Implementation details

  1. Add a library/codegen folder and add the script to it
  2. The script:
    1. takes the json file path as a cmd line argument (use the argparse lib to parse)
    2. read and validate the json file (a json schema might be useful here)
    3. 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.
    4. Write a "wrapping" .c file that: has a SYS_INIT call that will:
      1. initialize the user settings lib
      2. call the generated "add" function
      3. load the settings
      4. 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); 
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature request New feature or request.
Projects
None yet
Development

No branches or pull requests

1 participant