Skip to main content

mcumgr_toolkit/commands/
enum.rs

1use serde::{Deserialize, Serialize};
2
3use super::macros::impl_serialize_as_empty_map;
4
5/// [Count of supported groups](https://docs.zephyrproject.org/latest/services/device_mgmt/smp_groups/smp_group_10.html#count-of-supported-groups-command) command
6#[derive(Clone, Debug, Eq, PartialEq)]
7pub struct GroupCount;
8impl_serialize_as_empty_map!(GroupCount);
9
10/// Response for [`GroupCount`] command
11#[derive(Clone, Debug, Deserialize, Eq, PartialEq)]
12pub struct GroupCountResponse {
13    /// the total number of supported MCUmgr groups on the device
14    pub count: u16,
15}
16
17/// [List supported groups](https://docs.zephyrproject.org/latest/services/device_mgmt/smp_groups/smp_group_10.html#list-supported-groups-command) command
18#[derive(Clone, Debug, Eq, PartialEq)]
19pub struct ListGroups;
20impl_serialize_as_empty_map!(ListGroups);
21
22/// Response for [`ListGroups`] command
23#[derive(Clone, Debug, Deserialize, Eq, PartialEq)]
24pub struct ListGroupsResponse {
25    /// list of the supported MCUmgr group IDs on the device
26    pub groups: Vec<u16>,
27}
28
29/// [Fetch single group ID](https://docs.zephyrproject.org/latest/services/device_mgmt/smp_groups/smp_group_10.html#fetch-single-group-id-command) command
30#[derive(Clone, Debug, Serialize, Eq, PartialEq)]
31pub struct GroupId {
32    /// contains the (0-based) index of the group to return information on, can be omitted to return the first group’s details.
33    #[serde(skip_serializing_if = "Option::is_none")]
34    pub index: Option<u16>,
35}
36
37/// Response for [`GroupId`] command
38#[derive(Clone, Debug, Deserialize, Eq, PartialEq)]
39pub struct GroupIdResponse {
40    /// the group ID
41    pub group: u16,
42    /// true if the listed group is the final supported group on the device
43    #[serde(default)]
44    pub end: bool,
45}
46
47/// [Details on supported groups](https://docs.zephyrproject.org/latest/services/device_mgmt/smp_groups/smp_group_10.html#details-on-supported-groups-command) command
48#[derive(Clone, Debug, Serialize, Eq, PartialEq)]
49pub struct GroupDetails<'a> {
50    /// list of the MCUmgr group IDs to fetch details on.
51    ///
52    /// fetch all groups if `None`
53    #[serde(skip_serializing_if = "Option::is_none")]
54    pub groups: Option<&'a [u16]>,
55}
56
57/// Details about a group
58#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
59pub struct GroupDetailsEntry {
60    /// the group ID of the MCUmgr command group
61    pub group: u16,
62    /// the name of the MCUmgr command group
63    pub name: Option<String>,
64    /// the number of handlers that the MCUmgr command group supports
65    pub handlers: Option<u8>,
66}
67
68/// Response for [`GroupDetails`] command
69#[derive(Clone, Debug, Deserialize, Eq, PartialEq)]
70pub struct GroupDetailsResponse {
71    /// list of group details
72    pub groups: Vec<GroupDetailsEntry>,
73}
74
75#[cfg(test)]
76mod tests {
77    use super::super::macros::command_encode_decode_test;
78    use super::*;
79    use ciborium::cbor;
80
81    command_encode_decode_test! {
82        group_count,
83        (0, 10, 0),
84        GroupCount,
85        cbor!({}),
86        cbor!({
87            "count" => 42,
88        }),
89        GroupCountResponse{
90            count: 42,
91        },
92    }
93
94    command_encode_decode_test! {
95        list_groups,
96        (0, 10, 1),
97        ListGroups,
98        cbor!({}),
99        cbor!({"groups" => [5,69,65535]}),
100        ListGroupsResponse{ groups: vec![5,69,u16::MAX] },
101    }
102
103    command_encode_decode_test! {
104        get_group_id_empty,
105        (0, 10, 2),
106        GroupId{index: None},
107        cbor!({}),
108        cbor!({
109            "group" => 42,
110        }),
111        GroupIdResponse{
112            group: 42,
113            end: false,
114        },
115    }
116
117    command_encode_decode_test! {
118        get_group_id_full,
119        (0, 10, 2),
120        GroupId{index: Some(69)},
121        cbor!({
122            "index" => 69,
123        }),
124        cbor!({
125            "group" => 65535,
126            "end" => true,
127        }),
128        GroupIdResponse{
129            group: u16::MAX,
130            end: true,
131        },
132    }
133
134    command_encode_decode_test! {
135        get_group_details,
136        (0, 10, 3),
137        GroupDetails{
138            groups: None,
139        },
140        cbor!({}),
141        cbor!({
142            "groups" => [
143                {
144                    "group" => 69,
145                },
146                {
147                    "group" => 42,
148                    "name" => "answer",
149                    "handlers" => 133,
150                },
151            ]
152    }),
153        GroupDetailsResponse{
154            groups: vec![
155                GroupDetailsEntry{
156                    group: 69,
157                    name: None,
158                    handlers: None,
159                },
160                GroupDetailsEntry{
161                    group: 42,
162                    name: Some("answer".into()),
163                    handlers: Some(133),
164                },
165            ]
166        },
167    }
168
169    command_encode_decode_test! {
170        get_group_details_2,
171        (0, 10, 3),
172        GroupDetails{
173            groups: Some(&[42, 69]),
174        },
175        cbor!({
176            "groups" => [42, 69],
177        }),
178        cbor!({
179            "groups" => []
180    }),
181        GroupDetailsResponse{
182            groups: vec![]
183        },
184    }
185}