Skip to content

Commit

Permalink
Merge pull request #28 from IRNAS/release/v1.3.2
Browse files Browse the repository at this point in the history
Release v1.3.2
  • Loading branch information
TjazVracko authored Feb 1, 2023
2 parents f036d9f + 19a6007 commit 9598f8d
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 10 deletions.
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

## [Unreleased]

## [1.3.2] - 2023-02-01

### Added

- Get default functions.
- Restore single setting functions.

## [1.3.1] - 2023-01-30

### Changed
Expand Down Expand Up @@ -56,7 +63,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- Basic sample.
- Callbacks sample.

[Unreleased]: https://github.com/IRNAS/irnas-usersettings-lib/compare/v1.3.1...HEAD
[Unreleased]: https://github.com/IRNAS/irnas-usersettings-lib/compare/v1.3.2...HEAD

[1.3.2]: https://github.com/IRNAS/irnas-usersettings-lib/compare/v1.3.1...v1.3.2

[1.3.1]: https://github.com/IRNAS/irnas-usersettings-lib/compare/v1.3.0...v1.3.1

Expand Down
65 changes: 65 additions & 0 deletions library/include/user_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,32 @@ int user_settings_set_default_with_id(uint16_t id, void *data, size_t len);
*/
void user_settings_restore_defaults(void);

/**
* @brief Set setting value, defined by key to it's default value
*
* This will reset key specific setting value to its default and store it to NVS.
* If no default exists for a setting, the value is still deleted. Calls to
* @user_settings_get_with_*() will return NULL.
*
* @param[in] key The key of the setting to set
*
* @retval 0 on success
* @retval -EIO if no default exists for a setting
*/
int user_settings_restore_default_with_key(char *key);

/**
* @brief Set setting value, defined by id to it's default value
*
* See @user_settings_restore_default_with_key()
*
* @param[in] id The ID of the setting to set
*
* @retval 0 on success
* @retval -EIO if no default exists for a setting
*/
int user_settings_restore_default_with_id(uint16_t id);

/**
* @brief Check if a user setting with the provided key exists
*
Expand Down Expand Up @@ -229,6 +255,45 @@ void *user_settings_get_with_key(char *key, size_t *len);
*/
void *user_settings_get_with_id(uint16_t id, size_t *len);

/**
* @brief Get a settings default value
*
* If no default value is set, return NULL.
*
* The out parameter @p len is usefull for the string and bytes setting types,
* when a consumer of the setting value might not know the length of the array.
*
* Will assert of a setting with this key does not exist.
* If the key input for this function is unknown to the application (i.e. parsed from user), then
* it should first be checked with user_settings_exists_with_key().
*
* @param[in] key The key of the setting to get
* @param[out] len The length of the setting default value. Can be NULL
*
* @return void* A pointer to the settings default value. The consumer of the value is
* expected to know the type of the setting in order to be able to cast the pointer to the correct
* type.
*/
void *user_settings_get_default_with_key(char *key, size_t *len);

/**
* @brief GGet a settings default value
*
* See user_settings_get_default_with_key()
*
* Will assert of a setting with this ID does not exist.
* If the ID input for this function is unknown to the application (i.e. parsed from user), then
* it should first be checked with user_settings_exists_with_id().
*
* @param[in] id The ID of the setting to get
* @param[out] len The length of the setting default value. Can be NULL
*
* @return void* A pointer to the settings default value. The consumer of the value is
* expected to know the type of the setting in order to be able to cast the pointer to the correct
* type.
*/
void *user_settings_get_default_with_id(uint16_t id, size_t *len);

/**
* @brief Set the on change callback for changes on all settings
*
Expand Down
66 changes: 62 additions & 4 deletions library/user_settings/user_settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,13 @@ static int prv_user_settings_set(struct user_setting *s, void *data, size_t len)
return 0;
}

static void prv_settings_restore(struct user_setting *setting)
{
/* Clear the value. This will cause the default to be used on subsequent reads */
memset(setting->data, 0, setting->data_len);
setting->is_set = false;
}

void user_settings_restore_defaults(void)
{
__ASSERT(prv_is_loaded, LOAD_ASSERT_TEXT);
Expand All @@ -321,13 +328,32 @@ void user_settings_restore_defaults(void)
user_settings_list_iter_start();
struct user_setting *setting;
while ((setting = user_settings_list_iter_next()) != NULL) {
if (setting->default_is_set) {
prv_user_settings_set(setting, setting->default_data,
setting->default_data_len);
}
prv_settings_restore(setting);
}
}

int user_settings_restore_default_with_key(char *key)
{
__ASSERT(prv_is_loaded, LOAD_ASSERT_TEXT);

struct user_setting *setting = user_settings_list_get_by_key(key);
__ASSERT(setting, "Key does not exists: %s", key);

prv_settings_restore(setting);
return 0;
}

int user_settings_restore_default_with_id(uint16_t id)
{
__ASSERT(prv_is_loaded, LOAD_ASSERT_TEXT);

struct user_setting *setting = user_settings_list_get_by_id(id);
__ASSERT(setting, "ID does not exists: %d", id);

prv_settings_restore(setting);
return 0;
}

bool user_settings_exists_with_key(char *key)
{
__ASSERT(prv_is_loaded, LOAD_ASSERT_TEXT);
Expand Down Expand Up @@ -403,6 +429,38 @@ void *user_settings_get_with_id(uint16_t id, size_t *len)
return prv_user_setting_get(s, len);
}

static void *prv_user_setting_get_default(struct user_setting *s, size_t *len)
{
if (s->default_is_set) {
if (len) {
*len = s->default_data_len;
}
return s->default_data;
}

return NULL;
}

void *user_settings_get_default_with_key(char *key, size_t *len)
{
__ASSERT(prv_is_loaded, LOAD_ASSERT_TEXT);

struct user_setting *s = user_settings_list_get_by_key(key);
__ASSERT(s, "Key does not exists: %s", key);

return prv_user_setting_get_default(s, len);
}

void *user_settings_get_default_with_id(uint16_t id, size_t *len)
{
__ASSERT(prv_is_loaded, LOAD_ASSERT_TEXT);

struct user_setting *s = user_settings_list_get_by_id(id);
__ASSERT(s, "ID does not exists: %d", id);

return prv_user_setting_get_default(s, len);
}

void user_settings_set_global_on_change_cb(user_settings_on_change_t on_change_cb)
{
__ASSERT(prv_is_inited, INIT_ASSERT_TEXT);
Expand Down
30 changes: 25 additions & 5 deletions tests/user_settings/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,17 +117,37 @@ ZTEST(user_settings_suite, test_settings_default_value)
uint32_t default_value = 69;
user_settings_set_default_with_id(2, &default_value, 4);

/* Get default should return what was set above */
size_t len_out;
uint32_t val_get = *(uint32_t *)user_settings_get_default_with_id(2, &len_out);
zassert_equal(len_out, sizeof(default_value), "default value should be %d",
sizeof(default_value));
zassert_equal(val_get, default_value, "default value should be %d", default_value);

/* value should be unchanged */
uint32_t value_out;
value_out = *(uint32_t *)user_settings_get_with_id(2, NULL);
zassert_equal(value_out, value, "Value should be unchanged");
}

/* restore defaults */
user_settings_restore_defaults();
ZTEST(user_settings_suite, test_settings_restore_one)
{
/* set value */
int8_t default_value = 0, value = -1;
user_settings_set_with_id(3, &value, 1);
user_settings_set_default_with_id(3, &default_value, 1);

/* value should now be same as default value */
value_out = *(uint32_t *)user_settings_get_with_id(2, NULL);
zassert_equal(value_out, default_value, "Value should be same as default value");
/* value should be what was set */
int8_t value_out = *(int8_t *)user_settings_get_with_id(3, NULL);
zassert_equal(value_out, value, "Value should be %d before restore", value);

// /* restore default for this setting */
user_settings_restore_default_with_id(3);

/* value should be default */
int8_t value_out_after_restore = *(int8_t *)user_settings_get_with_id(3, NULL);
zassert_equal(value_out_after_restore, default_value, "Value should be %d after restore",
default_value);
}

static uint32_t on_change_id_store;
Expand Down

0 comments on commit 9598f8d

Please sign in to comment.