Feature: JSON encode / decode support.
Closed this issue · 1 comments
TjazVracko commented
Context
Support is required for being able to set settings from a JSON input and also to serialize the settings into a JSON structure.
cJSON will be used as the JSON parsing lib, since it is available in zephyr.
Required steps / Implementation details
- Add
changed
flag tostruct user_setting
that is set totrue
when a setting value is changed (prv_user_settings_set
). - Add iteration functions to existing
user_settings.c/.h
(See below). - Add a function to reset a single changed flag and all changed flags (See below).
- Add tests to
tests/user_settings
to verify this functionality.
Additions to user_settings.h
:
/**
* @brief Start iteration over all user settings
*
* Call this before getting elements with user_settings_list_iter_next.
* This will reset an in-progress iteration.
*/
void user_settings_iter_start(void);
/**
* @brief Get next settings ID and KEY in the iteration
*
* @param[out] key The key of the next settings. NULL if no more element exist.
* @param[out] id The ID of the next settings.NULL if no more element exist.
*/
void user_settings_list_iter_next(char *key, uint16_t* id);
/**
* @brief Get next settings ID and KEY in the iteration
*
* Only returns settings wil a set changed flag.
*
* @param[out] key The key of the next changed settings. NULL if no more element exist.
* @param[out] id The ID of the next changed settings.NULL if no more element exist.
*/
void user_settings_list_iter_next_changed(char *key, uint16_t* id);
/* Write docstring here pls. should be obvious */
void user_settings_clear_changed_by_key(char *key);
void user_settings_clear_changed_by_id(uint16_t id);
/**
* @brief Clears changed on all settings
*/
void user_settings_clear_changed(void);
- Add a new submodule
user_settings_json.h/.c
- Implement bellow API.
- Add Kconfig options to enable this module (something like CONFIG_USER_SETTINGS_JSON).
- Add appropriate tests to tests/user_settings_json`
/**
* @brief Set settings from a flat json structure
*
* Does not modify the passed in JSON.
*
* Expected structure looks like:
* {
* "s_key_1": <value>,
* "s_key_2": <value>,
* // ...
* }
*
* @param[in] settings The settings to apply
*
* @return int Can this fail? Figure out if any errors can be returned or if this can be void.
*/
int user_settings_set_from_json(const cJSON *settings);
/**
* @brief Create a JSON with containing only settings marked changed.
*
* The caller is expected to free the created cJSON structure.
*
* @param[out] settings Created json
* @return int This can probably fail with allocation errors. Be thorough please.
*/
int settings user_settings_get_changed_json(cJSON **settings);
/**
* @brief Create a JSON with containing all settings.
*
* The caller is expected to free the created cJSON structure.
*
* @param[out] settings Created json
* @return int This can probably fail with allocation errors. Be thorough please.
*/
int settings user_settings_get_all_json(cJSON **settings);
Definition of Done
TjazVracko commented
If you see that some API function would make more sense in another form, change it!