Skip to content

Commit

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

## [Unreleased]

## [1.3.3] - 2023-02-03

### Fixed

- On-change callbacks not being called when restoring settings.

## [1.3.2] - 2023-02-01

### Added
Expand Down Expand Up @@ -63,7 +69,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.2...HEAD
[Unreleased]: https://github.com/IRNAS/irnas-usersettings-lib/compare/v1.3.3...HEAD

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

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

Expand Down
11 changes: 5 additions & 6 deletions library/include/user_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,7 @@ void user_settings_set_global_on_change_cb(user_settings_on_change_t on_change_c
* @param[in] on_change_cb The callback function. NULL to disable the
* notification.
*/
void user_settings_set_on_change_cb_with_key(const char *key,
user_settings_on_change_t on_change_cb);
void user_settings_set_on_change_cb_with_key(char *key, user_settings_on_change_t on_change_cb);

/**
* @brief Set the on change callback for changes to a specific setting
Expand Down Expand Up @@ -410,7 +409,7 @@ bool user_settings_has_default_with_id(uint16_t id);
*
* @return uint16_t The id of setting with the provided key
*/
uint16_t user_settings_key_to_id(const char *key);
uint16_t user_settings_key_to_id(char *key);

/**
* @brief Convert user settings id to a key
Expand All @@ -423,7 +422,7 @@ uint16_t user_settings_key_to_id(const char *key);
*
* @return char* The key of setting with the provided id
*/
const char *user_settings_id_to_key(uint16_t id);
char *user_settings_id_to_key(uint16_t id);

/**
* @brief Get maximal length of the setting
Expand All @@ -436,7 +435,7 @@ const char *user_settings_id_to_key(uint16_t id);
*
* @return The length of the setting value.
*/
size_t user_settings_get_max_len_with_key(const char *key);
size_t user_settings_get_max_len_with_key(char *key);

/**
* @brief Get maximal length of the setting
Expand All @@ -462,7 +461,7 @@ size_t user_settings_get_max_len_with_id(uint16_t id);
*
* @return The type of the setting value.
*/
enum user_setting_type user_settings_get_type_with_key(const char *key);
enum user_setting_type user_settings_get_type_with_key(char *key);

/**
* @brief Get the type of the setting
Expand Down
2 changes: 1 addition & 1 deletion library/protocol/binary/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ GET FULL: 03 00 68 65 79 00 02 00 02 39 05 02
SET: /
SET DEFAULT: 06 03 00 02 39 05
SETTING: id: 4, key: "yo", value: 7654321, default: 123456
SETTING: id: 4, key: "yo", value: 7654321, default: 1234567 (TYPE_U32)
GET: 04 00 79 6F 00 03 04 B1 CB 74 00
GET FULL: 04 00 79 6F 00 03 04 B1 CB 74 00 04 87 D6 12 00 04
SET: 05 04 00 04 B1 CB 74 00
Expand Down
21 changes: 12 additions & 9 deletions library/user_settings/user_settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,13 @@ static int prv_user_settings_set(struct user_setting *s, void *data, size_t len)

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;
/* if value in not set, do nothing */
if (!setting->is_set) {
return;
}

/* Set the setting to the same value as default */
prv_user_settings_set(setting, setting->default_data, setting->default_data_len);
}

void user_settings_restore_defaults(void)
Expand Down Expand Up @@ -468,8 +472,7 @@ void user_settings_set_global_on_change_cb(user_settings_on_change_t on_change_c
prv_global_on_change_cb = on_change_cb;
}

void user_settings_set_on_change_cb_with_key(const char *key,
user_settings_on_change_t on_change_cb)
void user_settings_set_on_change_cb_with_key(char *key, user_settings_on_change_t on_change_cb)
{
__ASSERT(prv_is_inited, INIT_ASSERT_TEXT);

Expand Down Expand Up @@ -531,7 +534,7 @@ bool user_settings_has_default_with_id(uint16_t id)
return s->default_is_set;
}

uint16_t user_settings_key_to_id(const char *key)
uint16_t user_settings_key_to_id(char *key)
{
__ASSERT(prv_is_loaded, LOAD_ASSERT_TEXT);

Expand All @@ -541,7 +544,7 @@ uint16_t user_settings_key_to_id(const char *key)
return s->id;
}

const char *user_settings_id_to_key(uint16_t id)
char *user_settings_id_to_key(uint16_t id)
{
__ASSERT(prv_is_loaded, LOAD_ASSERT_TEXT);

Expand All @@ -551,7 +554,7 @@ const char *user_settings_id_to_key(uint16_t id)
return s->key;
}

size_t user_settings_get_max_len_with_key(const char *key)
size_t user_settings_get_max_len_with_key(char *key)
{
__ASSERT(prv_is_loaded, LOAD_ASSERT_TEXT);

Expand All @@ -571,7 +574,7 @@ size_t user_settings_get_max_len_with_id(uint16_t id)
return s->max_size;
}

enum user_setting_type user_settings_get_type_with_key(const char *key)
enum user_setting_type user_settings_get_type_with_key(char *key)
{
__ASSERT(prv_is_loaded, LOAD_ASSERT_TEXT);

Expand Down
19 changes: 18 additions & 1 deletion library/user_settings/user_settings_shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,21 @@ static int cmd_restore(const struct shell *shell_ptr, size_t argc, char *argv[])
return 0;
}

static int cmd_restore_one(const struct shell *shell_ptr, size_t argc, char *argv[])
{
char *key = argv[1];

struct user_setting *s = user_settings_list_get_by_key(key);
if (!s) {
shell_error(shell_ptr, "Setting with this key not found: %s", key);
return -ENOENT;
}

user_settings_restore_default_with_key(key);

return 0;
}

/**
* @brief Get user setting at position @p idx in the list
*
Expand Down Expand Up @@ -278,7 +293,9 @@ SHELL_STATIC_SUBCMD_SET_CREATE(
SHELL_CMD_ARG(set_default, &dsub_setting_key,
"<name> <value> Set the default value for one user setting", cmd_set_default,
3, 0),
SHELL_CMD_ARG(restore, NULL, "Set all user settings to default values", cmd_restore, 1, 0),
SHELL_CMD_ARG(restore, NULL, "Restore all settings to default values", cmd_restore, 1, 0),
SHELL_CMD_ARG(restore_one, NULL, "Restore one setting to its default value",
cmd_restore_one, 2, 0),
SHELL_SUBCMD_SET_END);

static int cmd_settings(const struct shell *shell_ptr, size_t argc, char **argv)
Expand Down
25 changes: 25 additions & 0 deletions samples/internal/binary_encoding/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# COPYRIGHT NOTICE: (c) 2022 Irnas. All rights reserved.

cmake_minimum_required(VERSION 3.20)

# create compile_commands.json for clang
set(CMAKE_EXPORT_COMPILE_COMMANDS on)

find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})

project(user-settings-sample-basic)

zephyr_compile_options(-fdiagnostics-color=always)

zephyr_include_directories(src)
target_sources(app PRIVATE
src/main.c
)

# Set CMake path variables for convenience

set(LIB_DIR ../../../library)
target_include_directories(app PRIVATE ${LIB_DIR}/user_settings)
target_include_directories(app PRIVATE ${LIB_DIR}/protocol/binary)
target_include_directories(app PRIVATE ${LIB_DIR}/protocol)
target_sources(app PRIVATE ${LIB_DIR}/protocol/binary/user_settings_protocol_binary.c)
10 changes: 10 additions & 0 deletions samples/internal/binary_encoding/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# INTERNAL

THIS IS AN "INTERNAL" SAMPLE. It demostrates some bit of the internals of the user-settings libary.
This means that is includes source and header files from within the library with CMake.
The functionality here can not be used via the library API. If this is ever required, the library must be refactored.

## Binary Encoding

This sample adds a setting of each type and gives them default values and values.
Then, it encodes each one using the binary encoder and prints the encoded buffer.
34 changes: 34 additions & 0 deletions samples/internal/binary_encoding/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
CONFIG_ASSERT=y
CONFIG_DEBUG=y

# log
CONFIG_LOG=y

# reboot
CONFIG_REBOOT=y

# shell
CONFIG_SHELL=y
CONFIG_SHELL_LOG_BACKEND=y # pass logs into the shell

# enable internal flash driver and flash map for NVS
CONFIG_FLASH=y
CONFIG_FLASH_MAP=y
CONFIG_NVS=y

# SETTINGS
CONFIG_SETTINGS=y
CONFIG_SETTINGS_RUNTIME=y
# NOTE: settings NVS is not required by the libraray.
# Any settings backend will do. So you could use FCB, FS, ...,
# but NVS is recomended by Zephyr for the settings module.
CONFIG_SETTINGS_NVS=y

# disable the settings shell to not confuse the user with
# 2 similar top level commands
CONFIG_SETTINGS_SHELL=y

# enable user settings
CONFIG_USER_SETTINGS=y
CONFIG_USER_SETTINGS_LOG_LEVEL_DBG=y
CONFIG_USER_SETTINGS_SHELL=y
134 changes: 134 additions & 0 deletions samples/internal/binary_encoding/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
#include <zephyr/kernel.h>

#include <user_settings.h>
#include <user_settings_list.h>
#include <user_settings_protocol_binary.h>

#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(main);

/* Use this instead of LOG_HEXDUMP to a get a more compact output */
void print_buf(uint8_t *buf, size_t len, char *text)
{
LOG_PRINTK("%s: ", text);
for (int i = 0; i < len; i++) {
LOG_PRINTK("%02X ", buf[i]);
}
LOG_PRINTK("\n");
}

void main(void)
{
/* This sleep is only here for native_posix build so that all log messages will actually be
* printed. if you don't sleep a bit, they get skipped */
#ifdef CONFIG_BOARD_NATIVE_POSIX
k_sleep(K_SECONDS(1));
#endif

LOG_INF("Sample for setting binary encoding");

/* Initialize settings defaults */
int err = user_settings_init();
if (err) {
LOG_ERR("err: %d", err);
}

/* Provide all application settings - one setting of each type is set here for demonstration
* purposes */
user_settings_add(1, "enabled", USER_SETTINGS_TYPE_BOOL);
user_settings_add(2, "number", USER_SETTINGS_TYPE_U8);
user_settings_add(3, "hey", USER_SETTINGS_TYPE_U16);
user_settings_add(4, "yo", USER_SETTINGS_TYPE_U32);
user_settings_add(5, "lets", USER_SETTINGS_TYPE_U64);
user_settings_add(6, "go", USER_SETTINGS_TYPE_I8);
user_settings_add(7, "t7", USER_SETTINGS_TYPE_I16);
user_settings_add(8, "t8", USER_SETTINGS_TYPE_I32);
user_settings_add(9, "ttt", USER_SETTINGS_TYPE_I64);
user_settings_add_sized(10, "text", USER_SETTINGS_TYPE_STR, 10);
user_settings_add_sized(11, "secret", USER_SETTINGS_TYPE_BYTES, 8);

/* Load settings - this will set every setting to its value from NVS, or a default value (if
* it is set)
*/
user_settings_load();

/* vars to hold data */
// bool b;
uint8_t u8;
uint16_t u16;
uint32_t u32;
uint64_t u64;
int8_t s8;
int16_t s16;
int32_t s32;
int64_t s64;
char str[] = "banana";
uint8_t bytes[] = {1, 2, 3, 4, 5, 6};

/* set default values */
// b = true;
// user_settings_set_default_with_id(1, &b, sizeof(b));
u8 = 13;
user_settings_set_default_with_id(2, &u8, sizeof(u8));
u16 = 1337;
user_settings_set_default_with_id(3, &u16, sizeof(u16));
u32 = 1234567;
user_settings_set_default_with_id(4, &u32, sizeof(u32));
// u64 = 1234567654321;
// user_settings_set_default_with_id(5, &u64, sizeof(u64));

s8 = -1;
user_settings_set_default_with_id(6, &s8, sizeof(s8));
s16 = 505;
user_settings_set_default_with_id(7, &s16, sizeof(s16));
// s32 = -1234567;
// user_settings_set_default_with_id(8, &s32, sizeof(s32));
s64 = 65432123456;
user_settings_set_default_with_id(9, &s64, sizeof(s64));

user_settings_set_default_with_id(10, str, strlen(str) + 1);
user_settings_set_default_with_id(11, bytes, sizeof(bytes));

/* set values */
// b = false;
// user_settings_set_with_id(1, &b, sizeof(b));
u8 = 69;
user_settings_set_with_id(2, &u8, sizeof(u8));
// u16 = 420;
// user_settings_set_with_id(3, &u16, sizeof(u16));
u32 = 7654321;
user_settings_set_with_id(4, &u32, sizeof(u32));
u64 = 1;
user_settings_set_with_id(5, &u64, sizeof(u64));

s8 = -2;
user_settings_set_with_id(6, &s8, sizeof(s8));
s16 = 202;
user_settings_set_with_id(7, &s16, sizeof(s16));
s32 = -7654321;
user_settings_set_with_id(8, &s32, sizeof(s32));
s64 = 987656789;
user_settings_set_with_id(9, &s64, sizeof(s64));

strcpy(str, "apple");
user_settings_set_with_id(10, str, strlen(str) + 1);

for (int i = 0; i < sizeof(bytes); i++) {
bytes[i] += 7;
}
user_settings_set_with_id(11, bytes, sizeof(bytes));

/* encode each setting */
uint8_t buffer[256];
int ret;
for (int i = 1; i <= 11; i++) {
struct user_setting *s = user_settings_list_get_by_id(i);
LOG_PRINTK("ID: %d\n", s->id);
ret = user_settings_protocol_binary_encode(s, buffer, sizeof(buffer));
print_buf(buffer, ret, "GET");
ret = user_settings_protocol_binary_encode_full(s, buffer, sizeof(buffer));
print_buf(buffer, ret, "GET FULL");
}

k_sleep(K_FOREVER);
}
Loading

0 comments on commit 9cf6a37

Please sign in to comment.