mgmt: mcumgr: Add iterable section to register MCUmgr handlers

This replaces the requirement for applications to manually
register MCUmgr handlers by having an iterable section which
then automatically registers the handlers at boot time.

Signed-off-by: Jamie McCrae <jamie.mccrae@nordicsemi.no>
This commit is contained in:
Jamie McCrae 2022-12-09 09:42:17 +00:00 committed by Carles Cufí
parent f7ab9a8c52
commit d7557102c0
17 changed files with 104 additions and 92 deletions

View file

@ -146,6 +146,10 @@ if(CONFIG_SENSOR_INFO)
zephyr_iterable_section(NAME sensor_info KVMA RAM_REGION GROUP RODATA_REGION SUBALIGN 4)
endif()
if(CONFIG_MCUMGR)
zephyr_iterable_section(NAME mcumgr_handler KVMA RAM_REGION GROUP RODATA_REGION SUBALIGN 4)
endif()
zephyr_iterable_section(NAME k_p4wq_initparam KVMA RAM_REGION GROUP RODATA_REGION SUBALIGN 4)
if(CONFIG_EMUL)

View file

@ -12,6 +12,10 @@
ITERABLE_SECTION_ROM(sensor_info, 4)
#endif
#if defined(CONFIG_MCUMGR)
ITERABLE_SECTION_ROM(mcumgr_handler, 4)
#endif
#if defined(CONFIG_EMUL)
SECTION_DATA_PROLOGUE(emulators_section,,)
{

View file

@ -0,0 +1,55 @@
/*
* Copyright (c) 2022 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef H_MCUMGR_MGMT_HANDLERS_
#define H_MCUMGR_MGMT_HANDLERS_
#include <zephyr/kernel.h>
#include <zephyr/sys/util_macro.h>
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
/**
* @brief MCUmgr handler registration API
* @defgroup mcumgr_handler_api MCUmgr handler API
* @ingroup mcumgr
* @{
*/
/** Type definition for a MCUmgr handler initialisation function */
typedef void (*mcumgr_handler_init_t)(void);
/** @cond INTERNAL_HIDDEN */
struct mcumgr_handler {
/** Initialisation function to be called */
const mcumgr_handler_init_t init;
};
/** @endcond */
/**
* @brief Define a MCUmgr handler to register.
*
* This adds a new entry to the iterable section linker list of MCUmgr handers.
*
* @param name Name of the MCUmgr handler to registger.
* @param _init Init function to be called (mcumgr_handler_init_t).
*/
#define MCUMGR_HANDLER_DEFINE(name, _init) \
STRUCT_SECTION_ITERABLE(mcumgr_handler, name) = { \
.init = _init, \
}
#ifdef __cplusplus
}
#endif /* __cplusplus */
/**
* @}
*/
#endif /* H_MCUMGR_MGMT_HANDLERS_ */

View file

@ -11,15 +11,6 @@
#include <string.h>
#include <zephyr/kernel.h>
#ifdef CONFIG_MCUMGR_CMD_FS_MGMT
#include <zephyr/mgmt/mcumgr/grp/fs_mgmt/fs_mgmt.h>
#endif
#ifdef CONFIG_MCUMGR_CMD_IMG_MGMT
#include <zephyr/mgmt/mcumgr/grp/img_mgmt/img_mgmt.h>
#endif
#ifdef CONFIG_MCUMGR_CMD_OS_MGMT
#include <zephyr/mgmt/mcumgr/grp/os_mgmt/os_mgmt.h>
#endif
#ifdef CONFIG_MCUMGR_CMD_STAT_MGMT
#include <zephyr/mgmt/mcumgr/grp/stat_mgmt/stat_mgmt.h>
#endif
@ -43,20 +34,6 @@ void smp_svr_init(void)
rc = STATS_INIT_AND_REG(smp_svr_stats, STATS_SIZE_32, "smp_svr_stats");
__ASSERT_NO_MSG(rc == 0);
/* Register the built-in mcumgr command handlers. */
#ifdef CONFIG_MCUMGR_CMD_FS_MGMT
fs_mgmt_register_group();
#endif
#ifdef CONFIG_MCUMGR_CMD_OS_MGMT
os_mgmt_register_group();
#endif
#ifdef CONFIG_MCUMGR_CMD_IMG_MGMT
img_mgmt_register_group();
#endif
#ifdef CONFIG_MCUMGR_CMD_STAT_MGMT
stat_mgmt_register_group();
#endif
}
static void smp_svr_timer_handler(struct k_timer *dummy)

View file

@ -13,20 +13,10 @@
#include <zephyr/device.h>
#include <zephyr/fs/fs.h>
#include <zephyr/fs/littlefs.h>
#include <zephyr/mgmt/mcumgr/grp/fs_mgmt/fs_mgmt.h>
#endif
#ifdef CONFIG_MCUMGR_CMD_OS_MGMT
#include <zephyr/mgmt/mcumgr/grp/os_mgmt/os_mgmt.h>
#endif
#ifdef CONFIG_MCUMGR_CMD_IMG_MGMT
#include <zephyr/mgmt/mcumgr/grp/img_mgmt/img_mgmt.h>
#endif
#ifdef CONFIG_MCUMGR_CMD_STAT_MGMT
#include <zephyr/mgmt/mcumgr/grp/stat_mgmt/stat_mgmt.h>
#endif
#ifdef CONFIG_MCUMGR_CMD_SHELL_MGMT
#include <zephyr/mgmt/mcumgr/grp/shell_mgmt/shell_mgmt.h>
#endif
#define LOG_LEVEL LOG_LEVEL_DBG
#include <zephyr/logging/log.h>
@ -75,20 +65,6 @@ void main(void)
if (rc < 0) {
LOG_ERR("Error mounting littlefs [%d]", rc);
}
fs_mgmt_register_group();
#endif
#ifdef CONFIG_MCUMGR_CMD_OS_MGMT
os_mgmt_register_group();
#endif
#ifdef CONFIG_MCUMGR_CMD_IMG_MGMT
img_mgmt_register_group();
#endif
#ifdef CONFIG_MCUMGR_CMD_STAT_MGMT
stat_mgmt_register_group();
#endif
#ifdef CONFIG_MCUMGR_CMD_SHELL_MGMT
shell_mgmt_register_group();
#endif
#ifdef CONFIG_MCUMGR_SMP_BT
start_smp_bluetooth();

View file

@ -11,6 +11,7 @@
#include <zephyr/fs/fs.h>
#include <zephyr/mgmt/mcumgr/mgmt/mgmt.h>
#include <zephyr/mgmt/mcumgr/smp/smp.h>
#include <zephyr/mgmt/mcumgr/mgmt/handlers.h>
#include <zephyr/mgmt/mcumgr/grp/fs_mgmt/fs_mgmt.h>
#include <zephyr/mgmt/mcumgr/grp/fs_mgmt/fs_mgmt_hash_checksum.h>
#include <assert.h>
@ -688,3 +689,5 @@ void fs_mgmt_register_group(void)
#endif
#endif
}
MCUMGR_HANDLER_DEFINE(fs_mgmt, fs_mgmt_register_group);

View file

@ -17,6 +17,7 @@
#include <zephyr/mgmt/mcumgr/mgmt/mgmt.h>
#include <zephyr/mgmt/mcumgr/smp/smp.h>
#include <zephyr/mgmt/mcumgr/mgmt/handlers.h>
#include <zephyr/mgmt/mcumgr/grp/img_mgmt/img_mgmt.h>
#include <zephyr/mgmt/mcumgr/grp/img_mgmt/image.h>
@ -611,14 +612,14 @@ static struct mgmt_group img_mgmt_group = {
};
void
img_mgmt_register_group(void)
void img_mgmt_register_group(void)
{
mgmt_register_group(&img_mgmt_group);
}
void
img_mgmt_unregister_group(void)
void img_mgmt_unregister_group(void)
{
mgmt_unregister_group(&img_mgmt_group);
}
MCUMGR_HANDLER_DEFINE(img_mgmt, img_mgmt_register_group);

View file

@ -12,6 +12,7 @@
#include <zephyr/kernel_structs.h>
#include <zephyr/mgmt/mcumgr/mgmt/mgmt.h>
#include <zephyr/mgmt/mcumgr/smp/smp.h>
#include <zephyr/mgmt/mcumgr/mgmt/handlers.h>
#include <zephyr/mgmt/mcumgr/grp/os_mgmt/os_mgmt.h>
#include <assert.h>
#include <string.h>
@ -680,7 +681,4 @@ void os_mgmt_register_group(void)
mgmt_register_group(&os_mgmt_group);
}
void os_mgmt_module_init(void)
{
os_mgmt_register_group();
}
MCUMGR_HANDLER_DEFINE(os_mgmt, os_mgmt_register_group);

View file

@ -7,6 +7,7 @@
#include <zephyr/sys/util.h>
#include <zephyr/shell/shell_dummy.h>
#include <zephyr/mgmt/mcumgr/mgmt/mgmt.h>
#include <zephyr/mgmt/mcumgr/mgmt/handlers.h>
#include <zephyr/mgmt/mcumgr/smp/smp.h>
#include <zephyr/mgmt/mcumgr/grp/shell_mgmt/shell_mgmt.h>
#include <string.h>
@ -135,8 +136,9 @@ static struct mgmt_group shell_mgmt_group = {
};
void
shell_mgmt_register_group(void)
void shell_mgmt_register_group(void)
{
mgmt_register_group(&shell_mgmt_group);
}
MCUMGR_HANDLER_DEFINE(shell_mgmt, shell_mgmt_register_group);

View file

@ -14,6 +14,7 @@
#include <zcbor_encode.h>
#include <zephyr/mgmt/mcumgr/mgmt/mgmt.h>
#include <zephyr/mgmt/mcumgr/mgmt/handlers.h>
#include <zephyr/mgmt/mcumgr/smp/smp.h>
#include <zephyr/mgmt/mcumgr/grp/stat_mgmt/stat_mgmt.h>
@ -237,8 +238,9 @@ static struct mgmt_group stat_mgmt_group = {
.mg_group_id = MGMT_GROUP_ID_STAT,
};
void
stat_mgmt_register_group(void)
void stat_mgmt_register_group(void)
{
mgmt_register_group(&stat_mgmt_group);
}
MCUMGR_HANDLER_DEFINE(stat_mgmt, stat_mgmt_register_group);

View file

@ -10,6 +10,7 @@
#include <zephyr/storage/flash_map.h>
#include <zephyr/mgmt/mcumgr/mgmt/mgmt.h>
#include <zephyr/mgmt/mcumgr/mgmt/handlers.h>
#include <zephyr/mgmt/mcumgr/grp/zephyr/zephyr_basic.h>
LOG_MODULE_REGISTER(mcumgr_zephyr_grp);
@ -59,13 +60,9 @@ static struct mgmt_group zephyr_basic_mgmt_group = {
.mg_group_id = (ZEPHYR_MGMT_GRP_BASIC),
};
static int zephyr_basic_mgmt_init(const struct device *dev)
void zephyr_basic_mgmt_init(void)
{
ARG_UNUSED(dev);
LOG_INF("Registering Zephyr basic mgmt group");
mgmt_register_group(&zephyr_basic_mgmt_group);
return 0;
}
SYS_INIT(zephyr_basic_mgmt_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY);
MCUMGR_HANDLER_DEFINE(zephyr_basic_mgmt, zephyr_basic_mgmt_init);

View file

@ -7,7 +7,9 @@
#include <zephyr/sys/slist.h>
#include <zephyr/sys/byteorder.h>
#include <zephyr/device.h>
#include <zephyr/mgmt/mcumgr/mgmt/mgmt.h>
#include <zephyr/mgmt/mcumgr/mgmt/handlers.h>
#include <string.h>
#ifdef CONFIG_MCUMGR_MGMT_NOTIFICATION_HOOKS
@ -123,3 +125,19 @@ int32_t mgmt_callback_notify(uint32_t event, void *data, size_t data_size)
return return_rc;
}
#endif
/* Processes all registered MCUmgr handlers at start up and registers them */
static int mcumgr_handlers_init(const struct device *dev)
{
ARG_UNUSED(dev);
STRUCT_SECTION_FOREACH(mcumgr_handler, handler) {
if (handler->init) {
handler->init();
}
}
return 0;
}
SYS_INIT(mcumgr_handlers_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY);

View file

@ -56,9 +56,6 @@ ZTEST(fs_mgmt_hash_supported, test_supported)
#endif
};
/* Register os_mgmt mcumgr group */
fs_mgmt_register_group();
/* Enable dummy SMP backend and ready for usage */
smp_dummy_enable();
smp_dummy_clear_state();

View file

@ -38,9 +38,6 @@ ZTEST(os_mgmt_echo, test_echo)
{
struct net_buf *nb;
/* Register os_mgmt mcumgr group */
os_mgmt_register_group();
/* Enable dummy SMP backend and ready for usage */
smp_dummy_enable();
smp_dummy_clear_state();

View file

@ -246,14 +246,6 @@ ZTEST(os_mgmt_info_build_date, test_info_build_date_2_all)
abs(expected_time_seconds - received_time_seconds));
}
static void *setup_tests(void)
{
/* Register os_mgmt mcumgr group */
os_mgmt_register_group();
return NULL;
}
static void cleanup_test(void *p)
{
if (nb != NULL) {
@ -263,6 +255,6 @@ static void cleanup_test(void *p)
}
/* Build date/time test set */
ZTEST_SUITE(os_mgmt_info_build_date, NULL, setup_tests, NULL, cleanup_test, NULL);
ZTEST_SUITE(os_mgmt_info_build_date, NULL, NULL, NULL, cleanup_test, NULL);
#endif

View file

@ -165,14 +165,6 @@ ZTEST(os_mgmt_info_limited, test_info_2_all)
rc);
}
static void *setup_tests(void)
{
/* Register os_mgmt mcumgr group */
os_mgmt_register_group();
return NULL;
}
static void cleanup_test(void *p)
{
if (nb != NULL) {
@ -182,6 +174,6 @@ static void cleanup_test(void *p)
}
/* Limited size buffer test set */
ZTEST_SUITE(os_mgmt_info_limited, NULL, setup_tests, NULL, cleanup_test, NULL);
ZTEST_SUITE(os_mgmt_info_limited, NULL, NULL, NULL, cleanup_test, NULL);
#endif

View file

@ -1497,9 +1497,6 @@ static void cleanup_test(void *p)
void test_main(void)
{
/* Register os_mgmt mcumgr group */
os_mgmt_register_group();
while (test_state.test_set < OS_MGMT_TEST_SET_COUNT) {
ztest_run_all(&test_state);
++test_state.test_set;