Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Mcumgr handlers for group enumeration #70704

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions include/zephyr/mgmt/mcumgr/mgmt/mgmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@
*/
void mgmt_unregister_group(struct mgmt_group *group);

/**
* @brief Get list of registered groups
*
* @return Pointer to list of registered groups
*/
sys_slist_t *mgmt_get_group_list();

Check failure on line 128 in include/zephyr/mgmt/mcumgr/mgmt/mgmt.h

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

FUNCTION_WITHOUT_ARGS

include/zephyr/mgmt/mcumgr/mgmt/mgmt.h:128 Bad function definition - sys_slist_t *mgmt_get_group_list() should probably be sys_slist_t *mgmt_get_group_list(void)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a function that calls a callback multiple times, once per group, and returns the group object, it should not be just returning a list directly or using sys_slist_t when the type should be the mgmt_group type (should also be const in the callback)


/**
* @brief Finds a registered command handler.
*
Expand Down
1 change: 1 addition & 0 deletions subsys/mgmt/mcumgr/grp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ add_subdirectory_ifdef(CONFIG_MCUMGR_GRP_STAT stat_mgmt)
add_subdirectory_ifdef(CONFIG_MCUMGR_GRP_SHELL shell_mgmt)
add_subdirectory_ifdef(CONFIG_MCUMGR_GRP_ZBASIC zephyr_basic)
add_subdirectory_ifdef(CONFIG_MCUMGR_GRP_SETTINGS settings_mgmt)
add_subdirectory_ifdef(CONFIG_MCUMGR_GRP_ENUM enum_mgmt)
2 changes: 2 additions & 0 deletions subsys/mgmt/mcumgr/grp/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ rsource "stat_mgmt/Kconfig"
rsource "zephyr_basic/Kconfig"

rsource "settings_mgmt/Kconfig"

rsource "enum_mgmt/Kconfig"
12 changes: 12 additions & 0 deletions subsys/mgmt/mcumgr/grp/enum_mgmt/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#
# Copyright (c) 2018-2021 mcumgr authors
# Copyright (c) 2024 Extreme Engineering Solutions
#
# SPDX-License-Identifier: Apache-2.0

# Enumeration management group public API is exported by MCUmgr interface API,
# when Enumeration Management is enabled.
zephyr_library(mgmt_mcumgr_grp_enum)
zephyr_library_sources(src/enum_mgmt.c)

zephyr_library_include_directories(include)
9 changes: 9 additions & 0 deletions subsys/mgmt/mcumgr/grp/enum_mgmt/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Copyright (c) 2024 Extreme Engineering Solutions, Inc.
# SPDX-License-Identifier: Apache-2.0
#

config MCUMGR_GRP_ENUM
bool "MCUmgr handlers for enumeration management"
help
Enables MCUmgr handlers for enumeration management. This includes
a single command which returns a list of all registered groups.
72 changes: 72 additions & 0 deletions subsys/mgmt/mcumgr/grp/enum_mgmt/src/enum_mgmt.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* Copyright (c) - 2024 Extreme Engineering Solutions
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/sys/util.h>
#include <zephyr/kernel.h>
#include <zephyr/sys/slist.h>
#include <zephyr/mgmt/mcumgr/mgmt/mgmt.h>
#include <zephyr/mgmt/mcumgr/smp/smp.h>
#include <zephyr/mgmt/mcumgr/mgmt/handlers.h>
#include <zephyr/logging/log.h>
#include <assert.h>
#include <string.h>
#include <stdio.h>

#include <zcbor_common.h>
#include <zcbor_encode.h>
#include <zcbor_decode.h>

#include <mgmt/mcumgr/util/zcbor_bulk.h>

#define ENUMERATION_MGMT_LIST 0
#define ENUMERATION_MGMT_GP_ID 10

Comment on lines +24 to +26
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.h file

static bool encode_id(sys_snode_t *group_node, zcbor_state_t *zse) {

Check failure on line 27 in subsys/mgmt/mcumgr/grp/enum_mgmt/src/enum_mgmt.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

OPEN_BRACE

subsys/mgmt/mcumgr/grp/enum_mgmt/src/enum_mgmt.c:27 open brace '{' following function definitions go on the next line
struct mgmt_group *group = CONTAINER_OF(group_node, struct mgmt_group, node);
return zcbor_uint32_put(group->mg_group_id);

Check warning on line 29 in subsys/mgmt/mcumgr/grp/enum_mgmt/src/enum_mgmt.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

LINE_SPACING

subsys/mgmt/mcumgr/grp/enum_mgmt/src/enum_mgmt.c:29 Missing a blank line after declarations
}

static int enumeration_mgmt_list(struct smp_streamer *ctxt)
{
zcbor_state_t *zse = ctxt->writer->zs;
sys_slist_t *groups;
sys_snode_t *snp, *sns;
int count;

groups = mgmt_get_group_list();
count = sys_slist_len(groups);

ok = zcbor_tstr_put_lit(zse, "groups") &&
zcbor_list_start_encode(zse, count);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

count needs to be more e.g. if canonical mode is enabled


SYS_SLIST_FOR_EACH_NODE_SAFE(&groups, snp, sns) {
ok = encode_id(sns, zse);
if (!ok) {
return MGMT_ERR_EBADSTATE;
}
}

ok = zcbor_list_end_encode(zse, count);

return ok ? MGMT_ERR_EOK : MGMT_ERR_ECORRUPT;
}

static const struct mgmt_handler enumeration_mgmt_group_handlers[] = {
[ENUMERATION_MGMT_LIST] = {enumeration_mgmt_list, NULL},
};

static struct mgmt_group enumeration_mgmt_group = {
.mg_handlers = enumeration_mgmt_group_handlers,
.mg_handlers_count = ARRAY_SIZE(enumeration_mgmt_group_handlers),
.mg_group_id = ENUMERATION_MGMT_GP_ID,
};

static void enumeration_mgmt_register_group(void)
{
mgmt_register_group(&enumeration_mgmt_group);
}

MCUMGR_HANDLER_DEFINE(enumerationg_mgmt, enumeration_mgmt_register_group);
8 changes: 7 additions & 1 deletion subsys/mgmt/mcumgr/mgmt/src/mgmt.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
* Copyright (c) 2018-2021 mcumgr authors
* Copyright (c) 2022-2023 Nordic Semiconductor ASA
* Copyright (c) 2024 Extreme Engineering Solutions
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -29,7 +30,7 @@
void
mgmt_unregister_group(struct mgmt_group *group)
{
(void)sys_slist_find_and_remove(&mgmt_group_list, &group->node);
sys_slist_find_and_remove(&mgmt_group_list, &group->node);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

??

}

const struct mgmt_handler *
Expand Down Expand Up @@ -133,6 +134,11 @@
sys_slist_append(&mgmt_group_list, &group->node);
}

sys_slist_t *mgmt_get_group_list()

Check failure on line 137 in subsys/mgmt/mcumgr/mgmt/src/mgmt.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

FUNCTION_WITHOUT_ARGS

subsys/mgmt/mcumgr/mgmt/src/mgmt.c:137 Bad function definition - sys_slist_t *mgmt_get_group_list() should probably be sys_slist_t *mgmt_get_group_list(void)
{
return &mgmt_group_list;
}

#if defined(CONFIG_MCUMGR_MGMT_NOTIFICATION_HOOKS)
void mgmt_callback_register(struct mgmt_callback *callback)
{
Expand Down
Loading