tests: mgmt: mcumgr: Add settings_mgmt test
Adds a test which checks settings_mgmt functionality. Signed-off-by: Jamie McCrae <jamie.mccrae@nordicsemi.no>
This commit is contained in:
parent
f5c25d3d65
commit
a21a80ceb5
8 changed files with 1953 additions and 0 deletions
16
tests/subsys/mgmt/mcumgr/settings_mgmt/CMakeLists.txt
Normal file
16
tests/subsys/mgmt/mcumgr/settings_mgmt/CMakeLists.txt
Normal file
|
@ -0,0 +1,16 @@
|
|||
#
|
||||
# Copyright (c) 2023 Nordic Semiconductor ASA
|
||||
#
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
#
|
||||
|
||||
cmake_minimum_required(VERSION 3.20.0)
|
||||
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
|
||||
project(settings_mgmt)
|
||||
|
||||
FILE(GLOB app_sources
|
||||
src/*.c
|
||||
)
|
||||
|
||||
target_sources(app PRIVATE ${app_sources})
|
||||
target_include_directories(app PRIVATE ${ZEPHYR_BASE}/subsys/mgmt/mcumgr/transport/include/mgmt/mcumgr/transport/)
|
24
tests/subsys/mgmt/mcumgr/settings_mgmt/prj.conf
Normal file
24
tests/subsys/mgmt/mcumgr/settings_mgmt/prj.conf
Normal file
|
@ -0,0 +1,24 @@
|
|||
#
|
||||
# Copyright (c) 2023 Nordic Semiconductor ASA
|
||||
#
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
#
|
||||
CONFIG_ZTEST=y
|
||||
CONFIG_NET_BUF=y
|
||||
CONFIG_BASE64=y
|
||||
CONFIG_ZCBOR=y
|
||||
CONFIG_CRC=y
|
||||
CONFIG_MCUMGR=y
|
||||
CONFIG_MCUMGR_TRANSPORT_DUMMY=y
|
||||
CONFIG_MCUMGR_TRANSPORT_DUMMY_RX_BUF_SIZE=256
|
||||
CONFIG_MCUMGR_GRP_SETTINGS=y
|
||||
CONFIG_MCUMGR_MGMT_NOTIFICATION_HOOKS=y
|
||||
CONFIG_MCUMGR_GRP_SETTINGS_ACCESS_HOOK=y
|
||||
CONFIG_ZTEST_NEW_API=y
|
||||
CONFIG_ZTEST_STACK_SIZE=2048
|
||||
CONFIG_SETTINGS=y
|
||||
CONFIG_SETTINGS_RUNTIME=y
|
||||
CONFIG_SETTINGS_NVS=y
|
||||
CONFIG_FLASH_MAP=y
|
||||
CONFIG_FLASH=y
|
||||
CONFIG_NVS=y
|
1606
tests/subsys/mgmt/mcumgr/settings_mgmt/src/main.c
Normal file
1606
tests/subsys/mgmt/mcumgr/settings_mgmt/src/main.c
Normal file
File diff suppressed because it is too large
Load diff
105
tests/subsys/mgmt/mcumgr/settings_mgmt/src/settings.c
Normal file
105
tests/subsys/mgmt/mcumgr/settings_mgmt/src/settings.c
Normal file
|
@ -0,0 +1,105 @@
|
|||
/*
|
||||
* Copyright (c) 2023 Nordic Semiconductor ASA
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "settings.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/settings/settings.h>
|
||||
|
||||
static bool set_called;
|
||||
static bool get_called;
|
||||
static bool export_called;
|
||||
static bool commit_called;
|
||||
static uint8_t val_aa[4];
|
||||
static uint8_t val_bb;
|
||||
|
||||
static int val_handle_set(const char *name, size_t len, settings_read_cb read_cb, void *cb_arg);
|
||||
static int val_handle_commit(void);
|
||||
static int val_handle_export(int (*cb)(const char *name, const void *value, size_t val_len));
|
||||
static int val_handle_get(const char *name, char *val, int val_len_max);
|
||||
|
||||
SETTINGS_STATIC_HANDLER_DEFINE(val, "test_val", val_handle_get, val_handle_set, val_handle_commit,
|
||||
val_handle_export);
|
||||
|
||||
static int val_handle_set(const char *name, size_t len, settings_read_cb read_cb, void *cb_arg)
|
||||
{
|
||||
const char *next;
|
||||
int rc;
|
||||
|
||||
set_called = true;
|
||||
|
||||
if (settings_name_steq(name, "aa", &next) && !next) {
|
||||
if (len != sizeof(val_aa)) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
rc = read_cb(cb_arg, val_aa, sizeof(val_aa));
|
||||
|
||||
return 0;
|
||||
} else if (settings_name_steq(name, "bb", &next) && !next) {
|
||||
if (len != sizeof(val_bb)) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
rc = read_cb(cb_arg, &val_bb, sizeof(val_bb));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
return -ENOENT;
|
||||
}
|
||||
|
||||
static int val_handle_commit(void)
|
||||
{
|
||||
commit_called = true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int val_handle_export(int (*cb)(const char *name, const void *value, size_t val_len))
|
||||
{
|
||||
export_called = true;
|
||||
|
||||
(void)cb("test_val/aa", val_aa, sizeof(val_aa));
|
||||
(void)cb("test_val/bb", &val_bb, sizeof(val_bb));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int val_handle_get(const char *name, char *val, int val_len_max)
|
||||
{
|
||||
const char *next;
|
||||
|
||||
get_called = true;
|
||||
|
||||
if (settings_name_steq(name, "aa", &next) && !next) {
|
||||
val_len_max = MIN(val_len_max, sizeof(val_aa));
|
||||
memcpy(val, val_aa, val_len_max);
|
||||
return val_len_max;
|
||||
} else if (settings_name_steq(name, "bb", &next) && !next) {
|
||||
val_len_max = MIN(val_len_max, sizeof(val_bb));
|
||||
memcpy(val, &val_bb, val_len_max);
|
||||
return val_len_max;
|
||||
}
|
||||
|
||||
return -ENOENT;
|
||||
}
|
||||
|
||||
void settings_state_reset(void)
|
||||
{
|
||||
set_called = false;
|
||||
get_called = false;
|
||||
export_called = false;
|
||||
commit_called = false;
|
||||
}
|
||||
|
||||
void settings_state_get(bool *set, bool *get, bool *export, bool *commit)
|
||||
{
|
||||
*set = set_called;
|
||||
*get = get_called;
|
||||
*export = export_called;
|
||||
*commit = commit_called;
|
||||
}
|
12
tests/subsys/mgmt/mcumgr/settings_mgmt/src/settings.h
Normal file
12
tests/subsys/mgmt/mcumgr/settings_mgmt/src/settings.h
Normal file
|
@ -0,0 +1,12 @@
|
|||
/*
|
||||
* Copyright (c) 2023 Nordic Semiconductor ASA
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
void settings_state_reset(void);
|
||||
|
||||
void settings_state_get(bool *set, bool *get, bool *export, bool *commit);
|
136
tests/subsys/mgmt/mcumgr/settings_mgmt/src/smp_test_util.c
Normal file
136
tests/subsys/mgmt/mcumgr/settings_mgmt/src/smp_test_util.c
Normal file
|
@ -0,0 +1,136 @@
|
|||
/*
|
||||
* Copyright (c) 2022-2023 Nordic Semiconductor ASA
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "smp_test_util.h"
|
||||
#include <zephyr/mgmt/mcumgr/grp/settings_mgmt/settings_mgmt.h>
|
||||
#include <zephyr/net/buf.h>
|
||||
#include <zephyr/sys/byteorder.h>
|
||||
#include <zcbor_encode.h>
|
||||
|
||||
/* SMP header function for generating MCUmgr command header with sequence number set to 1 */
|
||||
static void smp_make_hdr(struct smp_hdr *rsp_hdr, size_t len, uint8_t type, bool write)
|
||||
{
|
||||
*rsp_hdr = (struct smp_hdr) {
|
||||
.nh_len = sys_cpu_to_be16(len),
|
||||
.nh_flags = 0,
|
||||
.nh_op = (write ? MGMT_OP_WRITE : MGMT_OP_READ),
|
||||
.nh_group = sys_cpu_to_be16(MGMT_GROUP_ID_SETTINGS),
|
||||
.nh_seq = 1,
|
||||
.nh_id = type,
|
||||
.nh_version = 1,
|
||||
};
|
||||
}
|
||||
|
||||
bool create_settings_mgmt_read_packet(zcbor_state_t *zse, uint8_t *buffer, uint8_t *output_buffer,
|
||||
uint16_t *buffer_size, char *name, uint32_t max_size)
|
||||
{
|
||||
bool ok;
|
||||
|
||||
ok = zcbor_map_start_encode(zse, 2) &&
|
||||
zcbor_tstr_put_lit(zse, "name") &&
|
||||
zcbor_tstr_put_term(zse, name) &&
|
||||
(max_size == 0 || (zcbor_tstr_put_lit(zse, "max_size") &&
|
||||
zcbor_uint32_put(zse, max_size))) &&
|
||||
zcbor_map_end_encode(zse, 2);
|
||||
|
||||
*buffer_size = (zse->payload_mut - buffer);
|
||||
smp_make_hdr((struct smp_hdr *)output_buffer, *buffer_size, SETTINGS_MGMT_ID_READ_WRITE,
|
||||
false);
|
||||
memcpy(&output_buffer[sizeof(struct smp_hdr)], buffer, *buffer_size);
|
||||
*buffer_size += sizeof(struct smp_hdr);
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool create_settings_mgmt_write_packet(zcbor_state_t *zse, uint8_t *buffer, uint8_t *output_buffer,
|
||||
uint16_t *buffer_size, char *name, const uint8_t *val,
|
||||
size_t val_size)
|
||||
{
|
||||
bool ok;
|
||||
|
||||
ok = zcbor_map_start_encode(zse, 2) &&
|
||||
zcbor_tstr_put_lit(zse, "name") &&
|
||||
zcbor_tstr_put_term(zse, name) &&
|
||||
zcbor_tstr_put_lit(zse, "val") &&
|
||||
zcbor_bstr_encode_ptr(zse, val, val_size) &&
|
||||
zcbor_map_end_encode(zse, 2);
|
||||
|
||||
*buffer_size = (zse->payload_mut - buffer);
|
||||
smp_make_hdr((struct smp_hdr *)output_buffer, *buffer_size, SETTINGS_MGMT_ID_READ_WRITE,
|
||||
true);
|
||||
memcpy(&output_buffer[sizeof(struct smp_hdr)], buffer, *buffer_size);
|
||||
*buffer_size += sizeof(struct smp_hdr);
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool create_settings_mgmt_delete_packet(zcbor_state_t *zse, uint8_t *buffer,
|
||||
uint8_t *output_buffer, uint16_t *buffer_size, char *name)
|
||||
{
|
||||
bool ok;
|
||||
|
||||
ok = zcbor_map_start_encode(zse, 2) &&
|
||||
zcbor_tstr_put_lit(zse, "name") &&
|
||||
zcbor_tstr_put_term(zse, name) &&
|
||||
zcbor_map_end_encode(zse, 2);
|
||||
|
||||
*buffer_size = (zse->payload_mut - buffer);
|
||||
smp_make_hdr((struct smp_hdr *)output_buffer, *buffer_size, SETTINGS_MGMT_ID_DELETE, true);
|
||||
memcpy(&output_buffer[sizeof(struct smp_hdr)], buffer, *buffer_size);
|
||||
*buffer_size += sizeof(struct smp_hdr);
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool create_settings_mgmt_commit_packet(zcbor_state_t *zse, uint8_t *buffer,
|
||||
uint8_t *output_buffer, uint16_t *buffer_size)
|
||||
{
|
||||
bool ok;
|
||||
|
||||
ok = zcbor_map_start_encode(zse, 2) &&
|
||||
zcbor_map_end_encode(zse, 2);
|
||||
|
||||
*buffer_size = (zse->payload_mut - buffer);
|
||||
smp_make_hdr((struct smp_hdr *)output_buffer, *buffer_size, SETTINGS_MGMT_ID_COMMIT, true);
|
||||
memcpy(&output_buffer[sizeof(struct smp_hdr)], buffer, *buffer_size);
|
||||
*buffer_size += sizeof(struct smp_hdr);
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool create_settings_mgmt_load_packet(zcbor_state_t *zse, uint8_t *buffer, uint8_t *output_buffer,
|
||||
uint16_t *buffer_size)
|
||||
{
|
||||
bool ok;
|
||||
|
||||
ok = zcbor_map_start_encode(zse, 2) &&
|
||||
zcbor_map_end_encode(zse, 2);
|
||||
|
||||
*buffer_size = (zse->payload_mut - buffer);
|
||||
smp_make_hdr((struct smp_hdr *)output_buffer, *buffer_size, SETTINGS_MGMT_ID_LOAD_SAVE,
|
||||
false);
|
||||
memcpy(&output_buffer[sizeof(struct smp_hdr)], buffer, *buffer_size);
|
||||
*buffer_size += sizeof(struct smp_hdr);
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool create_settings_mgmt_save_packet(zcbor_state_t *zse, uint8_t *buffer, uint8_t *output_buffer,
|
||||
uint16_t *buffer_size)
|
||||
{
|
||||
bool ok;
|
||||
|
||||
ok = zcbor_map_start_encode(zse, 2) &&
|
||||
zcbor_map_end_encode(zse, 2);
|
||||
|
||||
*buffer_size = (zse->payload_mut - buffer);
|
||||
smp_make_hdr((struct smp_hdr *)output_buffer, *buffer_size, SETTINGS_MGMT_ID_LOAD_SAVE,
|
||||
true);
|
||||
memcpy(&output_buffer[sizeof(struct smp_hdr)], buffer, *buffer_size);
|
||||
*buffer_size += sizeof(struct smp_hdr);
|
||||
|
||||
return ok;
|
||||
}
|
40
tests/subsys/mgmt/mcumgr/settings_mgmt/src/smp_test_util.h
Normal file
40
tests/subsys/mgmt/mcumgr/settings_mgmt/src/smp_test_util.h
Normal file
|
@ -0,0 +1,40 @@
|
|||
/*
|
||||
* Copyright (c) 2023 Nordic Semiconductor ASA
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#ifndef H_SMP_TEST_UTIL_
|
||||
#define H_SMP_TEST_UTIL_
|
||||
|
||||
#include <zephyr/ztest.h>
|
||||
#include <zephyr/mgmt/mcumgr/mgmt/mgmt.h>
|
||||
#include <zcbor_common.h>
|
||||
#include <smp_internal.h>
|
||||
|
||||
/* Function for creating an settings_mgmt read command */
|
||||
bool create_settings_mgmt_read_packet(zcbor_state_t *zse, uint8_t *buffer, uint8_t *output_buffer,
|
||||
uint16_t *buffer_size, char *name, uint32_t max_size);
|
||||
|
||||
/* Function for creating an settings_mgmt write command */
|
||||
bool create_settings_mgmt_write_packet(zcbor_state_t *zse, uint8_t *buffer, uint8_t *output_buffer,
|
||||
uint16_t *buffer_size, char *name, const uint8_t *val,
|
||||
size_t val_size);
|
||||
|
||||
/* Function for creating an settings_mgmt delete command */
|
||||
bool create_settings_mgmt_delete_packet(zcbor_state_t *zse, uint8_t *buffer,
|
||||
uint8_t *output_buffer, uint16_t *buffer_size, char *name);
|
||||
|
||||
/* Function for creating an settings_mgmt commit command */
|
||||
bool create_settings_mgmt_commit_packet(zcbor_state_t *zse, uint8_t *buffer,
|
||||
uint8_t *output_buffer, uint16_t *buffer_size);
|
||||
|
||||
/* Function for creating an settings_mgmt load command */
|
||||
bool create_settings_mgmt_load_packet(zcbor_state_t *zse, uint8_t *buffer, uint8_t *output_buffer,
|
||||
uint16_t *buffer_size);
|
||||
|
||||
/* Function for creating an settings_mgmt save command */
|
||||
bool create_settings_mgmt_save_packet(zcbor_state_t *zse, uint8_t *buffer, uint8_t *output_buffer,
|
||||
uint16_t *buffer_size);
|
||||
|
||||
#endif
|
14
tests/subsys/mgmt/mcumgr/settings_mgmt/testcase.yaml
Normal file
14
tests/subsys/mgmt/mcumgr/settings_mgmt/testcase.yaml
Normal file
|
@ -0,0 +1,14 @@
|
|||
#
|
||||
# Copyright (c) 2023 Nordic Semiconductor ASA
|
||||
#
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
#
|
||||
tests:
|
||||
settings.mgmt:
|
||||
platform_allow:
|
||||
- native_posix
|
||||
- native_posix_64
|
||||
- nrf52840dk_nrf52840
|
||||
tags:
|
||||
- settings_mgmt
|
||||
- mcumgr
|
Loading…
Add table
Add a link
Reference in a new issue