mcumgr_toolkit
Example
from mcumgr_toolkit import MCUmgrClient
with MCUmgrClient.serial("/dev/ttyACM0") as client:
client.use_auto_frame_size()
print(client.os_echo("Hello world!"))
# Hello world!
A high-level client for Zephyr's MCUmgr SMP functionality
Creates a new serial port based Zephyr MCUmgr SMP client.
Arguments
serial- The identifier of the serial device. (Windows:COMxx, Linux:/dev/ttyXX)baud_rate- The baud rate of the serial port.timeout_ms- The communication timeout, in ms.
Creates a Zephyr MCUmgr SMP client based on a USB serial port identified by VID:PID.
Useful for programming many devices in rapid succession, as Windows usually gives each one a different COMxx identifier.
Arguments
identifier- A regex that identifies the device.baud_rate- The baud rate the port should operate at.timeout_ms- The communication timeout, in ms.
Identifier examples
1234:89AB- Vendor ID 1234, Product ID 89AB. Will fail if product has multiple serial ports.1234:89AB:12- Vendor ID 1234, Product ID 89AB, Interface 12.1234:.*:[2-3]- Vendor ID 1234, any Product Id, Interface 2 or 3.
Configures the maximum SMP frame size that we can send to the device.
Must not exceed MCUMGR_TRANSPORT_NETBUF_SIZE,
otherwise we might crash the device.
Configures the maximum SMP frame size that we can send to the device automatically
by reading the value of MCUMGR_TRANSPORT_NETBUF_SIZE
from the device.
Changes the communication timeout.
When the device does not respond to packets within the set duration, an error will be raised.
Changes the retry amount.
When the device encounters a transport error, it will retry this many times until giving up.
Checks if the device is alive and responding.
Runs a simple echo with random data and checks if the response matches.
Raises an error if the device is not alive and responding.
High-level firmware update routine.
Arguments
firmware- The firmware image data.checksum- SHA256 of the firmware image. Optional.bootloader_type- The type of bootloader. Auto-detect bootloader if missing.skip_reboot- Do not reboot device after the update.force_confirm- Skip test boot and confirm directly.upgrade_only- Prevent firmware downgrades.progress- A callback that receives progress updates.
Sends a message to the device and expects the same message back as response.
This can be used as a sanity check for whether the device is connected and responsive.
Queries live task statistics
Note
Converts stkuse and stksiz to bytes.
Zephyr originally reports them as number of 4 byte words.
Return
A map of task names with their respective statistics
Sets the RTC of the device to the given datetime.
Uses the contained local time and discards timezone information.
Retrieves the device RTC's datetime.
Will not contain timezone information.
Issues a system reset.
Arguments
force- Issues a force reset.boot_mode- Overwrites the default boot mode.
Known boot_mode values:
0- Normal system boot1- Bootloader recovery mode
Note that boot_mode only works if MCUMGR_GRP_OS_RESET_BOOT_MODE is enabled.
Fetch information on the running image
Similar to Linux's uname command.
Arguments
format- Format specifier for the returned response
For more information about the format specifier fields, see the SMP documentation.
Modify the current image state and return the new state
Arguments
hash- the hash id of the image. Seemcuboot_get_image_info.confirm- mark the given image as 'confirmed'
If confirm is false, perform a test boot with the given image and revert upon hard reset.
If confirm is true, boot to the given image and mark it as confirmed. If hash is omitted,
confirm the currently running image.
Note that hash will not be the same as the SHA256 of the whole firmware image,
it is the field in the MCUboot TLV section that contains a hash of the data
which is used for signature verification purposes.
Upload a firmware image to an image slot.
Note
This only uploads the image to a slot on the device, it has to be activated
through image_set_state for an actual update to happen.
For a full firmware update algorithm in a single step, see firmware_update.
Arguments
data- The firmware image dataimage- Selects target image on the device. Defaults to0.checksum- The SHA256 checksum of the image. If missing, will be computed from the image data.upgrade_only- If true, allow firmware upgrades only and reject downgrades.progress- A callable object that takes (transmitted, total) values as parameters. Any return value is ignored. Raising an exception aborts the operation.
Performance
Uploading files with Zephyr's default parameters is slow.
You want to increase MCUMGR_TRANSPORT_NETBUF_SIZE
to maybe 4096 and then enable larger chunking through either set_frame_size
or use_auto_frame_size.
Erase image slot on target device.
Arguments
slot- The slot ID of the image to erase. Slot1if omitted.
Query the current values of a given stats group
Arguments
name- The name of the group. Seestats_list_groups.
Load a file from the device.
Arguments
name- The full path of the file on the device.progress- A callable object that takes (transmitted, total) values as parameters. Any return value is ignored. Raising an exception aborts the operation.
Return
The file content
Performance
Downloading files with Zephyr's default parameters is slow.
You want to increase MCUMGR_TRANSPORT_NETBUF_SIZE
to maybe 4096 or larger.
Write a file to the device.
Arguments
name- The full path of the file on the device.data- The file content.progress- A callable object that takes (transmitted, total) values as parameters. Any return value is ignored. Raising an exception aborts the operation.
Performance
Uploading files with Zephyr's default parameters is slow.
You want to increase MCUMGR_TRANSPORT_NETBUF_SIZE
to maybe 4096 and then enable larger chunking through either set_frame_size
or use_auto_frame_size.
Computes the hash/checksum of a file
For available algorithms, see fs_supported_checksum_types.
Arguments
name- The absolute path of the file on the devicealgorithm- The hash/checksum algorithm to use, or default if Noneoffset- How many bytes of the file to skiplength- How many bytes to read afteroffset. None for the entire file.
Run a shell command.
Arguments
argv- The shell command to be executed.use_retries- Retry request a certain amount of times if a transport error occurs. Be aware that this might cause the command to be executed multiple times.
Return
The command output
Query how many MCUmgr groups are supported by the device.
Return
The number of MCUmgr groups the device supports.
Query all available group IDs in a single command.
Note that this might fail if the amount of groups is too large for the SMP frame. But given that the Zephyr implementation contains less than 10 groups, this is currently highly unlikely.
If it does fail, use enum_iter_group_ids to iterate
through the available group IDs one by one.
Return
A list of all MCUmgr group IDs the device supports.
Query a single group ID from the device.
Arguments
index- The index in the list of group IDs. Must be smaller thanenum_get_group_count.
Return
The group ID of the group with the given index
Iterate through all supported MCUmgr Groups.
Same as enum_get_group_ids, but does not
require large message sizes if the number of groups is large. The tradeoff is
that this function is much slower.
Query details from all available groups.
Arguments
groups- The group IDs to fetch details for. If omitted, fetch all groups.
Return
A list of details about all MCUmgr group IDs the device supports.
Execute a raw MCUmgrCommand.
Only returns if no error happened, so the
user does not need to check for an rc or err
field in the response.
Read Zephyr's SMP Protocol Specification for more information.
Arguments
write_operation- Whether the command is a read or write operation.group_id- The group ID of the commandcommand_id- The command IDdata- Anything that can be serialized as a proper packet payload.
Example
client.raw_command(True, 0, 0, {"d": "Hello!"})
# Returns: {'r': 'Hello!'}
Return value of MCUmgrClient.fs_file_checksum.
Data format of the hash/checksum type
Properties of a hash/checksum algorithm
Return value of MCUmgrClient.fs_file_status.
Details about an MCUmgr group
The state of an image slot
Return value of MCUmgrClient.os_mcumgr_parameters.
Information about a firmware image type returned by MCUmgrClient.image_slot_info
Information about a slot that can hold a firmware image
Statistics of an MCU task/thread
Information about an MCUboot firmware image
Extract information from an MCUboot image file