Skip to content

Move mcumgr functionality as internal Zephyr service #36092

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

Merged
3 changes: 3 additions & 0 deletions subsys/mgmt/mcumgr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ zephyr_library_sources_ifdef(CONFIG_MCUMGR_SMP_SHELL smp_shell.c)
zephyr_library_sources_ifdef(CONFIG_MCUMGR_SMP_UART smp_uart.c)
zephyr_library_sources_ifdef(CONFIG_MCUMGR_SMP_UDP smp_udp.c)
add_subdirectory_ifdef(CONFIG_MCUMGR_GRP_ZEPHYR_BASIC zephyr_grp)

add_subdirectory(lib)
zephyr_library_link_libraries(MCUMGR)

if (CONFIG_MCUMGR_SMP_SHELL OR CONFIG_MCUMGR_SMP_UART)
zephyr_library_sources(serial_util.c)

endif ()
16 changes: 16 additions & 0 deletions subsys/mgmt/mcumgr/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ config MGMT_CBORATTR_MAX_SIZE
help
The maximum size of a CBOR attribute during decoding

config MGMT_CBORATTR_FLOAT_SUPPORT
bool "Enable support for float"
help
The option enables float processing within CBOR attributes.
If your code requires processing of float numbers by CBOR,
the option needs to be enabled.
Disabling the option slightly reduces code footprint.

menu "Command Handlers"
menuconfig MCUMGR_CMD_FS_MGMT
bool "Enable mcumgr handlers for file management (insecure)"
Expand Down Expand Up @@ -201,6 +209,14 @@ config IMG_MGMT_REJECT_DIRECT_XIP_MISMATCHED_SLOT
The base address can be set, to an image binary header, with imgtool,
using the --rom-fixed command line option.

config IMG_MGMT_FRUGAL_LIST
bool "Omit zero, empty or false values from status list"
help
The status list send back from the device will only be filled with data that is non-zero,
non-empty or true. This option slightly reduces number of bytes transferred back from
a device but requires support in client software, which has to default omitted values.
Works correctly with mcumgr-cli.

endif


Expand Down
20 changes: 20 additions & 0 deletions subsys/mgmt/mcumgr/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#
# Copyright (c) 2018-2021 mcumgr authors
#
# SPDX-License-Identifier: Apache-2.0
#

zephyr_interface_library_named(MCUMGR)

target_include_directories(MCUMGR INTERFACE
mgmt/include
cborattr/include
util/include
smp/include
)

add_subdirectory(cborattr)
add_subdirectory(cmd)
add_subdirectory(mgmt)
add_subdirectory(smp)
add_subdirectory(util)
141 changes: 141 additions & 0 deletions subsys/mgmt/mcumgr/lib/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
# mcumgr

This is mcumgr, version 0.2.0

mcumgr is a management library for 32-bit MCUs. The goal of mcumgr is to
define a common management infrastructure with pluggable transport and encoding
components. In addition, mcumgr provides definitions and handlers for some
core commands: image management, file system management, and OS management.

mcumgr is operating system and hardware independent. It relies on hardware
porting layers from the operating system it runs on.

## Configuration

The `samples/smp_svr/zephyr/prj.conf` file provides a good starting point for
configuring an application to use *mcumgr*. The major configuration settings
are described below:

| Setting | Description | Default |
| ------------- | ------------- | ------- |
| `CONFIG_MCUMGR` | Enable the mcumgr management library. | n |
| `CONFIG_MCUMGR_CMD_FS_MGMT` | Enable mcumgr handlers for file management | n |
| `CONFIG_MCUMGR_CMD_IMG_MGMT` | Enable mcumgr handlers for image management | n |
| `CONFIG_MCUMGR_CMD_OS_MGMT` | Enable mcumgr handlers for OS management | n |
| `CONFIG_MCUMGR_CMD_STAT_MGMT` | Enable mcumgr handlers for statistics | n |
| `CONFIG_MCUMGR_GRP_ZEPHYR_BASIC` | Enable mcumgr basic commands group | n |

## Dependencies

To use mcumgr's image management support, your device must be running version
1.1.0 or later of the [MCUboot boot
loader](https://github.com/mcu-tools/mcuboot). The other mcumgr features do
not require MCUboot.

## Command line tool

The `mcumgr` command line tool is available at:
https://github.com/apache/mynewt-mcumgr-cli. The command line tool requires [Go
1.12 or later](https://golang.org/dl/). Once Go is installed and set up on your
system, you can install the mcumgr CLI tool by issuing the following `go get`
command:

```
$ go get github.com/apache/mynewt-mcumgr-cli/mcumgr
```

The `mcumgr` tool allows you to manage devices running an mcumgr server.

## Architecture

The mcumgr stack has the following layout:

```
+---------------------+---------------------+
| <command handlers> |
+---------------------+---------------------+
| mgmt |
+---------------------+---------------------+
| <transfer encoding(s)> |
+---------------------+---------------------+
| <transport(s)> |
+---------------------+---------------------+
```

Items enclosed in angled brackets represent generic components that can be plugged into mcumgr. The items in this stack diagram are defined below:
* *Command handler*: Processes incoming mcumgr requests and generates corresponding responses. A command handler is associated with a single command type, defined by a (group ID, command ID) pair.
* *mgmt*: The core of mcumgr; facilitates the passing of requests and responses between the generic command handlers and the concrete transports and transfer encodings.
* *Transfer encoding*: Defines how mcumgr requests and responses are encoded on the wire.
* *Transport*: Sends and receives mcumgr packets over a particular medium.

Each transport is configured with a single transfer encoding.

As an example, the sample application `smp_svr` uses the following components:

* Command handlers:
* Image management (`img_mgmt`)
* File system management (`fs_mgmt`)
* OS management (`os_mgmt`)
* Transfer/Transports protocols:
* SMP/Bluetooth
* SMP/Shell

yielding the following stack diagram:

```
+----------+----------+----------+----------+
| img_mgmt | fs_mgmt | os_mgmt | ... |
+----------+----------+----------+----------+
| mgmt |
+---------------------+---------------------+
| SMP | SMP |
+---------------------+---------------------+
| Bluetooth | Shell |
+---------------------+---------------------+
```

## Command definition

An mcumgr request or response consists of the following two components:
* mcumgr header
* CBOR key-value map

How these two components are encoded and parsed depends on the transfer
encoding used.

The mcumgr header structure is defined in `mgmt/include/mgmt/mgmt.h` as
`struct mgmt_hdr`.

The contents of the CBOR key-value map are specified per command type.

## Supported transfer encodings

Mcumgr comes with one built-in transfer encoding: Simple Management Protocol
(SMP). SMP requests and responses have a very basic structure. For details,
see the comments at the top of `smp/include/smp/smp.h`.

## Supported transports

The mcumgr project defines two transports:
* [SMP/Console](transport/smp-console.md)
* [SMP/Bluetooth](transport/smp-bluetooth.md)

Implementations, being hardware- and OS-specific, are not included.

## Browsing

Information and documentation for mcumgr is stored within the source.

For more information in the source, here are some pointers:

- [cborattr](cborattr): Used for parsing incoming mcumgr requests. Destructures mcumgr packets and populates corresponding field variables.
- [cmd](cmd): Built-in command handlers for the core mcumgr commands.
- [ext](ext): Third-party libraries that mcumgr depends on.
- [mgmt](mgmt): Code implementing the `mgmt` layer of mcumgr.
- [smp](smp): The built-in transfer encoding: Simple management protocol.

## Joining

Developers welcome!

* Discord mcumgr channel: https://discord.com/invite/Ck7jw53nU2
13 changes: 13 additions & 0 deletions subsys/mgmt/mcumgr/lib/cborattr/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#
# Copyright (c) 2018-2021 mcumgr authors
#
# SPDX-License-Identifier: Apache-2.0
#

target_include_directories(MCUMGR INTERFACE
include
)

zephyr_library_sources(
src/cborattr.c
)
144 changes: 144 additions & 0 deletions subsys/mgmt/mcumgr/lib/cborattr/include/cborattr/cborattr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
* Copyright (c) 2018-2021 mcumgr authors
*
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef CBORATTR_H
#define CBORATTR_H

#include <stdbool.h>
#include <stdint.h>
#include <ctype.h>
#include <stdio.h>
#include <sys/types.h>
#include "tinycbor/cbor.h"

#ifdef __cplusplus
extern "C" {
#endif

/* This library wraps the tinycbor decoder with a attribute based decoder
* suitable for decoding a binary version of json. Specifically, the
* contents of the cbor contains pairs of attributes. where the attribute
* is a key/value pair. keys are always text strings, but values can be
* many different things (enumerated below)
*/

enum CborAttrType {
CborAttrIntegerType = 1,
CborAttrUnsignedIntegerType,
CborAttrByteStringType,
CborAttrTextStringType,
CborAttrBooleanType,
CborAttrHalfFloatType,
CborAttrFloatType,
CborAttrDoubleType,
CborAttrArrayType,
CborAttrObjectType,
CborAttrStructObjectType,
CborAttrNullType,
};

struct cbor_attr_t;

struct cbor_enum_t {
char *name;
long long value;
};

struct cbor_array_t {
enum CborAttrType element_type;
union {
struct {
const struct cbor_attr_t *subtype;
char *base;
size_t stride;
} objects;
struct {
char **ptrs;
char *store;
int storelen;
} strings;
struct {
long long *store;
} integers;
struct {
unsigned long long *store;
} uintegers;
struct {
double *store;
} reals;
struct{
uint16_t *store;
} halffloats;
struct {
bool *store;
} booleans;
} arr;
int *count;
int maxlen;
};

struct cbor_attr_t {
char *attribute;
enum CborAttrType type;
union {
long long *integer;
unsigned long long *uinteger;
uint16_t *halffloat;
double *real;
float *fval;
char *string;
bool *boolean;
struct byte_string {
uint8_t *data;
size_t *len;
} bytestring;
struct cbor_array_t array;
size_t offset;
struct cbor_attr_t *obj;
} addr;
union {
long long integer;
double real;
bool boolean;
float fval;
uint16_t halffloat;
} dflt;
size_t len;
bool nodefault;
};

/*
* Use the following macros to declare template initializers for
* CborAttrStructObjectType arrays. Writing the equivalents out by hand is
* error-prone.
*
* CBOR_STRUCT_OBJECT takes a structure name s, and a fieldname f in s.
*
* CBOR_STRUCT_ARRAY takes the name of a structure array, a pointer to a an
* initializer defining the subobject type, and the address of an integer to
* store the length in.
*/
#define CBORATTR_STRUCT_OBJECT(s, f) .addr.offset = offsetof(s, f)
#define CBORATTR_STRUCT_ARRAY(a, e, n) \
.addr.array.element_type = CborAttrStructObjectType, \
.addr.array.arr.objects.subtype = e, \
.addr.array.arr.objects.base = (char *)a, \
.addr.array.arr.objects.stride = sizeof(a[0]), \
.addr.array.count = n, \
.addr.array.maxlen = ARRAY_SIZE(a)

#define CBORATTR_ATTR_UNNAMED (char *)(-1)

int cbor_read_object(struct CborValue *val, const struct cbor_attr_t *attr);
int cbor_read_array(struct CborValue *val, const struct cbor_array_t *arr);

int cbor_read_flat_attrs(const uint8_t *data, int len,
const struct cbor_attr_t *attrs);
#ifdef __cplusplus
}
#endif

#endif /* CBORATTR_H */
Loading