This is the documentation for the latest (main) development branch of Zephyr. If you are looking for the documentation of previous releases, use the drop-down menu on the left and select the desired version.

MCUmgr Callbacks

Overview

MCUmgr has a customisable callback/notification system that allows application (and module) code to receive callbacks for MCUmgr events that they are interested in and react to them or return a status code to the calling function that provides control over if the action should be allowed or not. An example of this is with the fs_mgmt group, whereby file access can be gated, the callback allows the application to inspect the request path and allow or deny access to said file, or it can rewrite the provided path to a different path for transparent file redirection support.

Implementation

Enabling

The base callback/notification system can be enabled using CONFIG_MCUMGR_MGMT_NOTIFICATION_HOOKS which will compile the registration and notification system into the code. This will not provide any callbacks by default as the callbacks that are supported by a build must also be selected by enabling the Kconfig’s for the required callbacks (see Events for further details). A callback function with the mgmt_cb type definition can then be declared and registered by calling mgmt_callback_register() for the desired event inside of a :c:struct`mgmt_callback` structure. Handlers are called in the order that they were registered.

With the system enabled, a basic handler can be set up and defined in application code as per:

#include <zephyr/kernel.h>
#include <zephyr/mgmt/mcumgr/mgmt/mgmt.h>
#include <zephyr/mgmt/mcumgr/mgmt/callbacks.h>

struct mgmt_callback my_callback;

int32_t my_function(uint32_t event, int32_t rc, bool *abort_more, void *data,
                    size_t data_size)
{
    if (event == MGMT_EVT_OP_CMD_DONE) {
        /* This is the event we registered for */
    }

    /* Return OK status code to continue with acceptance to underlying handler */
    return MGMT_ERR_EOK;
}

void main()
{
    my_callback.callback = my_function;
    my_callback.event_id = MGMT_EVT_OP_CMD_DONE;
    mgmt_callback_register(&my_callback);
}

This code registers a handler for the MGMT_EVT_OP_CMD_DONE event, which will be called after a MCUmgr command has been processed and output generated, note that this requires that CONFIG_MCUMGR_SMP_COMMAND_STATUS_HOOKS be enabled to receive this callback.

Multiple callbacks can be setup to use a single function as a common callback, and many different functions can be used for each event by registering each group once, or all notifications for a whole group can be enabled by using one of the MGMT_EVT_OP_*_ALL events, alternatively a handler can setup for every notification by using MGMT_EVT_OP_ALL. When setting up handlers, events can be combined that are in the same group only, for example 5 img_mgmt callbacks can be setup with a single registration call, but to also setup a callback for an os_mgmt callback, this must be done as a separate registration. Group IDs are numerical increments, event IDs are bitmask values, hence the restriction.

As an example, the following registration is allowed, which will register for 3 SMP events with a single callback function in a single registration:

my_callback.callback = my_function;
my_callback.event_id = (MGMT_EVT_OP_CMD_RECV |
                        MGMT_EVT_OP_CMD_STATUS |
                        MGMT_EVT_OP_CMD_DONE);
mgmt_callback_register(&my_callback);

The following code is not allowed, and will cause undefined operation, because it mixes the IMG management group with the OS management group whereby the group is not a bitmask value, only the event is:

my_callback.callback = my_function;
my_callback.event_id = (MGMT_EVT_OP_IMG_MGMT_DFU_STARTED |
                        MGMT_EVT_OP_OS_MGMT_RESET);
mgmt_callback_register(&my_callback);

Events

Events can be selected by enabling their corresponding Kconfig option:

Actions

Some callbacks expect a return status to either allow or disallow an operation, an example is the fs_mgmt access hook which allows for access to files to be allowed or denied. With these handlers, the first non-OK error code returned by a handler will be returned to the MCUmgr client.

An example of selectively denying file access:

#include <zephyr/kernel.h>
#include <zephyr/mgmt/mcumgr/mgmt/mgmt.h>
#include <zephyr/mgmt/mcumgr/mgmt/callbacks.h>
#include <string.h>

struct mgmt_callback my_callback;

int32_t my_function(uint32_t event, int32_t rc, bool *abort_more, void *data,
                    size_t data_size)
{
    /* Only run this handler if a previous handler has not failed */
    if (event == MGMT_EVT_OP_FS_MGMT_FILE_ACCESS && rc == MGMT_ERR_EOK) {
        struct fs_mgmt_file_access *fs_data = (struct fs_mgmt_file_access *)data;

        /* Check if this is an upload and deny access if it is, otherwise check the
         * the path and deny if is matches a name
         */
        if (fs_data->upload == true) {
            /* Return an access denied error code to the client and abort calling
             * further handlers
             */
            *abort_more = true;
            return MGMT_ERR_EACCESSDENIED;
        } else if (strcmp(fs_data->filename, "/lfs1/false_deny.txt") == 0) {
            /* Return a no entry error code to the client, call additional handlers
             * (which will have failed set to true)
             */
            return MGMT_ERR_ENOENT;
        }
    }

    /* Return OK status code to continue with acceptance to underlying handler */
    return MGMT_ERR_EOK;
}

void main()
{
    my_callback.callback = my_function;
    my_callback.event_id = MGMT_EVT_OP_FS_MGMT_FILE_ACCESS;
    mgmt_callback_register(&my_callback);
}

This code registers a handler for the MGMT_EVT_OP_FS_MGMT_FILE_ACCESS event, which will be called after a fs_mgmt file read/write command has been received to check if access to the file should be allowed or not, note that this requires that CONFIG_MCUMGR_GRP_FS_FILE_ACCESS_HOOK be enabled to receive this callback.

MCUmgr Command Callback Usage/Adding New Event Types

To add a callback to a MCUmgr command, mgmt_callback_notify() can be called with the event ID and, optionally, a data struct to pass to the callback (which can be modified by handlers). If no data needs to be passed back, NULL can be used instead, and size of the data set to 0.

An example MCUmgr command handler:

#include <zephyr/kernel.h>
#include <zcbor_common.h>
#include <zcbor_encode.h>
#include <zephyr/mgmt/mcumgr/mgmt/mgmt.h>
#include <zephyr/mgmt/mcumgr/mgmt/callbacks.h>

#define MGMT_EVT_GRP_USER_ONE MGMT_EVT_GRP_USER_CUSTOM_START

enum user_one_group_events {
    /** Callback on first post, data is test_struct. */
    MGMT_EVT_OP_USER_ONE_FIRST  = MGMT_DEF_EVT_OP_ID(MGMT_EVT_GRP_USER_ONE, 0),

    /** Callback on second post, data is test_struct. */
    MGMT_EVT_OP_USER_ONE_SECOND = MGMT_DEF_EVT_OP_ID(MGMT_EVT_GRP_USER_ONE, 1),

    /** Used to enable all user_one events. */
    MGMT_EVT_OP_USER_ONE_ALL    = MGMT_DEF_EVT_OP_ALL(MGMT_EVT_GRP_USER_ONE),
};

struct test_struct {
    uint8_t some_value;
};

static int test_command(struct mgmt_ctxt *ctxt)
{
    int rc;
    zcbor_state_t *zse = ctxt->cnbe->zs;
    bool ok;
    struct test_struct test_data = {
        .some_value = 8,
    };

    rc = mgmt_callback_notify(MGMT_EVT_OP_USER_ONE_FIRST, &test_data,
                              sizeof(test_data));

    if (rc != MGMT_ERR_EOK) {
        /* A handler returned a failure code */
        return rc;
    }

    /* All handlers returned success codes */

    ok = zcbor_tstr_put_lit(zse, "output_value") &&
         zcbor_int32_put(zse, 1234);

    if (!ok) {
            return MGMT_ERR_EMSGSIZE;
    }

    return MGMT_ERR_EOK;
}

If no response is required for the callback, the function call be called and casted to void.

Migration

If there is existing code using the previous callback system(s) in Zephyr 3.2 or earlier, then it will need to be migrated to the new system. To migrate code, the following callback registration functions will need to be migrated to register for callbacks using mgmt_callback_register() (note that CONFIG_MCUMGR_MGMT_NOTIFICATION_HOOKS will need to be set to enable the new notification system in addition to any migrations):

API Reference

group mcumgr_callback_api

MCUmgr callback API.

Defines

MGMT_EVT_GET_GROUP(event)

Get group from event.

MGMT_EVT_GET_ID(event)

Get event ID from event.

Typedefs

typedef int32_t (*mgmt_cb)(uint32_t event, int32_t rc, bool *abort_more, void *data, size_t data_size)

Function to be called on MGMT notification/event.

This callback function is used to notify an application or system about a mcumgr mgmt event.

Param event

mcumgr_op_t.

Param rc

mcumgr_err_t of the previous handler calls, if it is an error then it will be the first error that was returned by a handler (i.e. this handler is being called for a notification only, the return code will be ignored).

Param abort_more

Set to true to abort further processing by additional handlers.

Param data

Optional event argument.

Param data_size

Size of optional event argument (0 if no data is provided).

Return

mcumgr_err_t of the status to return to the calling code (only checked when failed is false).

Enums

enum mgmt_cb_groups

MGMT event callback group IDs. Note that this is not a 1:1 mapping with mcumgr_group_t values.

Values:

enumerator MGMT_EVT_GRP_ALL = 0
enumerator MGMT_EVT_GRP_SMP
enumerator MGMT_EVT_GRP_OS
enumerator MGMT_EVT_GRP_IMG
enumerator MGMT_EVT_GRP_FS
enumerator MGMT_EVT_GRP_USER_CUSTOM_START = MGMT_GROUP_ID_PERUSER
enum smp_all_events

MGMT event opcodes for all command processing.

Values:

enumerator MGMT_EVT_OP_ALL = MGMT_DEF_EVT_OP_ALL(MGMT_EVT_GRP_ALL)

Used to enable all events.

enum smp_group_events

MGMT event opcodes for base SMP command processing.

Values:

enumerator MGMT_EVT_OP_CMD_RECV = MGMT_DEF_EVT_OP_ID(MGMT_EVT_GRP_SMP, 0)

Callback when a command is received, data is mgmt_evt_op_cmd_arg().

enumerator MGMT_EVT_OP_CMD_STATUS = MGMT_DEF_EVT_OP_ID(MGMT_EVT_GRP_SMP, 1)

Callback when a a status is updated, data is mgmt_evt_op_cmd_arg().

enumerator MGMT_EVT_OP_CMD_DONE = MGMT_DEF_EVT_OP_ID(MGMT_EVT_GRP_SMP, 2)

Callback when a command has been processed, data is mgmt_evt_op_cmd_arg().

enumerator MGMT_EVT_OP_CMD_ALL = MGMT_DEF_EVT_OP_ALL(MGMT_EVT_GRP_SMP)

Used to enable all smp_group events.

enum fs_mgmt_group_events

MGMT event opcodes for filesystem management group.

Values:

enumerator MGMT_EVT_OP_FS_MGMT_FILE_ACCESS = MGMT_DEF_EVT_OP_ID(MGMT_EVT_GRP_FS, 0)

Callback when a file has been accessed, data is fs_mgmt_file_access().

enumerator MGMT_EVT_OP_FS_MGMT_ALL = MGMT_DEF_EVT_OP_ALL(MGMT_EVT_GRP_FS)

Used to enable all fs_mgmt_group events.

enum img_mgmt_group_events

MGMT event opcodes for image management group.

Values:

enumerator MGMT_EVT_OP_IMG_MGMT_DFU_CHUNK = MGMT_DEF_EVT_OP_ID(MGMT_EVT_GRP_IMG, 0)

Callback when a client sends a file upload chunk, data is img_mgmt_upload_check().

enumerator MGMT_EVT_OP_IMG_MGMT_DFU_STOPPED = MGMT_DEF_EVT_OP_ID(MGMT_EVT_GRP_IMG, 1)

Callback when a DFU operation is stopped.

enumerator MGMT_EVT_OP_IMG_MGMT_DFU_STARTED = MGMT_DEF_EVT_OP_ID(MGMT_EVT_GRP_IMG, 2)

Callback when a DFU operation is started.

enumerator MGMT_EVT_OP_IMG_MGMT_DFU_PENDING = MGMT_DEF_EVT_OP_ID(MGMT_EVT_GRP_IMG, 3)

Callback when a DFU operation has finished being transferred.

enumerator MGMT_EVT_OP_IMG_MGMT_DFU_CONFIRMED = MGMT_DEF_EVT_OP_ID(MGMT_EVT_GRP_IMG, 4)

Callback when an image has been confirmed.

enumerator MGMT_EVT_OP_IMG_MGMT_ALL = MGMT_DEF_EVT_OP_ALL(MGMT_EVT_GRP_IMG)

Used to enable all img_mgmt_group events.

enum os_mgmt_group_events

MGMT event opcodes for operating system management group.

Values:

enumerator MGMT_EVT_OP_OS_MGMT_RESET = MGMT_DEF_EVT_OP_ID(MGMT_EVT_GRP_OS, 0)

Callback when a reset command has been received.

enumerator MGMT_EVT_OP_OS_MGMT_INFO_CHECK = MGMT_DEF_EVT_OP_ID(MGMT_EVT_GRP_OS, 1)

Callback when an info command is processed, data is os_mgmt_info_check.

enumerator MGMT_EVT_OP_OS_MGMT_INFO_APPEND = MGMT_DEF_EVT_OP_ID(MGMT_EVT_GRP_OS, 2)

Callback when an info command needs to output data, data is os_mgmt_info_append.

enumerator MGMT_EVT_OP_OS_MGMT_ALL = MGMT_DEF_EVT_OP_ALL(MGMT_EVT_GRP_OS)

Used to enable all os_mgmt_group events.

Functions

int32_t mgmt_callback_notify(uint32_t event, void *data, size_t data_size)

This function is called to notify registered callbacks about mcumgr notifications/events.

Parameters
  • event – mcumgr_op_t.

  • data – Optional event argument.

  • data_size – Size of optional event argument (0 if none).

Returns

mcumgr_err_t either MGMT_ERR_EOK if all handlers returned it, or the error code of the first handler that returned an error.

void mgmt_callback_register(struct mgmt_callback *callback)

Register event callback function.

Parameters
  • callback – Callback struct.

void mgmt_callback_unregister(struct mgmt_callback *callback)

Unregister event callback function.

Parameters
  • callback – Callback struct.

struct mgmt_callback
#include <callbacks.h>

MGMT callback struct

Public Members

sys_snode_t node

Entry list node.

mgmt_cb callback

Callback that will be called.

uint32_t event_id

MGMT_EVT_[…] Event ID for handler to be called on. This has special meaning if MGMT_EVT_OP_ALL is used (which will cover all events for all groups), or MGMT_EVT_OP_*_MGMT_ALL (which will cover all events for a single group). For events that are part of a single group, they can be or’d together for this to have one registration trigger on multiple events, please note that this will only work for a single group, to register for events in different groups, they must be registered separately.

struct mgmt_evt_op_cmd_arg
#include <callbacks.h>

Arguments for MGMT_EVT_OP_CMD_RECV, MGMT_EVT_OP_CMD_STATUS and MGMT_EVT_OP_CMD_DONE

Public Members

uint16_t group

mcumgr_group_t

uint8_t id

Message ID within group

int err

mcumgr_err_t, used in MGMT_EVT_OP_CMD_DONE

int status

img_mgmt_id_upload_t, used in MGMT_EVT_OP_CMD_STATUS

MCUmgr fs_mgmt callback API.

struct fs_mgmt_file_access
#include <fs_mgmt_callbacks.h>

Structure provided in the MGMT_EVT_OP_FS_MGMT_FILE_ACCESS notification callback: This callback function is used to notify the application about a pending file read/write request and to authorise or deny it. Access will be allowed so long as all notification handlers return MGMT_ERR_EOK, if one returns an error then access will be denied.

Public Members

bool upload

True if the request is for uploading data to the file, otherwise false

char *filename

Path and filename of file be accesses, note that this can be changed by handlers to redirect file access if needed (as long as it does not exceed the maximum path string size).

MCUmgr img_mgmt callback API.

struct img_mgmt_upload_check
#include <img_mgmt_callbacks.h>

Structure provided in the MGMT_EVT_OP_IMG_MGMT_DFU_CHUNK notification callback: This callback function is used to notify the application about a pending firmware upload packet from a client and authorise or deny it. Upload will be allowed so long as all notification handlers return MGMT_ERR_EOK, if one returns an error then the upload will be denied.

Public Members

struct img_mgmt_upload_action *action

Action to take

struct img_mgmt_upload_req *req

Upload request information