Skip to main content

mcumgr_toolkit/commands/
settings.rs

1use serde::{Deserialize, Serialize};
2
3use crate::commands::macros::{
4    impl_deserialize_from_empty_map_and_into_unit, impl_serialize_as_empty_map,
5};
6
7/// [Read Setting](https://docs.zephyrproject.org/latest/services/device_mgmt/smp_groups/smp_group_3.html#read-setting-request) command
8#[derive(Clone, Debug, Serialize, Eq, PartialEq)]
9pub struct ReadSetting<'a> {
10    /// string of the setting to retrieve
11    pub name: &'a str,
12    /// optional maximum size of data to return
13    #[serde(skip_serializing_if = "Option::is_none")]
14    pub max_size: Option<u32>,
15}
16
17/// Response for [`ReadSetting`] command
18#[derive(Clone, Debug, Deserialize, Eq, PartialEq)]
19pub struct ReadSettingResponse {
20    /// The returned data.
21    ///
22    /// Note that the underlying data type cannot be specified through this and must be known by the client.
23    pub val: Vec<u8>,
24    /// Will be set if the maximum supported data size is smaller than the
25    /// maximum requested data size, and contains the maximum data size
26    /// which the device supports
27    pub max_size: Option<u32>,
28}
29
30/// [Write Setting](https://docs.zephyrproject.org/latest/services/device_mgmt/smp_groups/smp_group_3.html#write-setting-request) command
31#[derive(Clone, Debug, Serialize, Eq, PartialEq)]
32pub struct WriteSetting<'a, 'b> {
33    /// string of the setting to update/set
34    pub name: &'a str,
35    /// value to set the setting to
36    #[serde(with = "serde_bytes")]
37    pub val: &'b [u8],
38}
39
40/// Response for [`WriteSetting`] command
41#[derive(Clone, Default, Debug, Eq, PartialEq)]
42pub struct WriteSettingResponse;
43impl_deserialize_from_empty_map_and_into_unit!(WriteSettingResponse);
44
45/// [Delete Setting](https://docs.zephyrproject.org/latest/services/device_mgmt/smp_groups/smp_group_3.html#delete-setting-command) command
46#[derive(Clone, Debug, Serialize, Eq, PartialEq)]
47pub struct DeleteSetting<'a> {
48    /// string of the setting to delete
49    pub name: &'a str,
50}
51
52/// Response for [`DeleteSetting`] command
53#[derive(Clone, Default, Debug, Eq, PartialEq)]
54pub struct DeleteSettingResponse;
55impl_deserialize_from_empty_map_and_into_unit!(DeleteSettingResponse);
56
57/// [Commit settings](https://docs.zephyrproject.org/latest/services/device_mgmt/smp_groups/smp_group_3.html#commit-settings-command) command
58#[derive(Clone, Debug, Eq, PartialEq)]
59pub struct CommitSettings;
60impl_serialize_as_empty_map!(CommitSettings);
61
62/// Response for [`CommitSettings`] command
63#[derive(Clone, Default, Debug, Eq, PartialEq)]
64pub struct CommitSettingsResponse;
65impl_deserialize_from_empty_map_and_into_unit!(CommitSettingsResponse);
66
67/// [Load settings](https://docs.zephyrproject.org/latest/services/device_mgmt/smp_groups/smp_group_3.html#load-settings-request) command
68#[derive(Clone, Debug, Eq, PartialEq)]
69pub struct LoadSettings;
70impl_serialize_as_empty_map!(LoadSettings);
71
72/// Response for [`LoadSettings`] command
73#[derive(Clone, Default, Debug, Eq, PartialEq)]
74pub struct LoadSettingsResponse;
75impl_deserialize_from_empty_map_and_into_unit!(LoadSettingsResponse);
76
77/// [Save Settings](https://docs.zephyrproject.org/latest/services/device_mgmt/smp_groups/smp_group_3.html#save-settings-request) command
78#[derive(Clone, Debug, Serialize, Eq, PartialEq)]
79pub struct SaveSettings<'a> {
80    /// if provided, contains the settings subtree name to save, if not then will save all settings.
81    #[serde(skip_serializing_if = "Option::is_none")]
82    pub name: Option<&'a str>,
83}
84
85/// Response for [`SaveSettings`] command
86#[derive(Clone, Default, Debug, Eq, PartialEq)]
87pub struct SaveSettingsResponse;
88impl_deserialize_from_empty_map_and_into_unit!(SaveSettingsResponse);
89
90#[cfg(test)]
91mod tests {
92    use super::super::macros::command_encode_decode_test;
93    use super::*;
94    use ciborium::cbor;
95
96    command_encode_decode_test! {
97        read_setting,
98        (0, 3, 0),
99        ReadSetting {
100            name: "foo/bar",
101            max_size: None
102        },
103        cbor!({
104            "name" => "foo/bar",
105        }),
106        cbor!({
107            "val" => ciborium::Value::Bytes(vec![1,2,3,4,5]),
108        }),
109        ReadSettingResponse{
110            val: vec![1,2,3,4,5],
111            max_size: None,
112        },
113    }
114    command_encode_decode_test! {
115        read_setting_with_max_size,
116        (0, 3, 0),
117        ReadSetting {
118            name: "foo/bar",
119            max_size: Some(42),
120        },
121        cbor!({
122            "name" => "foo/bar",
123            "max_size" => 42,
124        }),
125        cbor!({
126            "val" => ciborium::Value::Bytes(vec![1,2,3,4,5]),
127            "max_size" => 36,
128        }),
129        ReadSettingResponse{
130            val: vec![1,2,3,4,5],
131            max_size: Some(36),
132        },
133    }
134
135    command_encode_decode_test! {
136        write_setting,
137        (2, 3, 0),
138        WriteSetting {
139            name: "foo",
140            val: &[1,2,3,4,5],
141        },
142        cbor!({
143            "name" => "foo",
144            "val" => ciborium::Value::Bytes(vec![1,2,3,4,5]),
145        }),
146        cbor!({}),
147        WriteSettingResponse,
148    }
149
150    command_encode_decode_test! {
151        delete_setting,
152        (2, 3, 1),
153        DeleteSetting {
154            name: "bar",
155        },
156        cbor!({
157            "name" => "bar",
158        }),
159        cbor!({}),
160        DeleteSettingResponse,
161    }
162
163    command_encode_decode_test! {
164        commit_settings,
165        (2, 3, 2),
166        CommitSettings,
167        cbor!({}),
168        cbor!({}),
169        CommitSettingsResponse,
170    }
171
172    command_encode_decode_test! {
173        load_settings,
174        (0, 3, 3),
175        LoadSettings,
176        cbor!({}),
177        cbor!({}),
178        LoadSettingsResponse,
179    }
180
181    command_encode_decode_test! {
182        save_settings,
183        (2, 3, 3),
184        SaveSettings{
185            name: None,
186        },
187        cbor!({}),
188        cbor!({}),
189        SaveSettingsResponse,
190    }
191
192    command_encode_decode_test! {
193        save_settings_with_name,
194        (2, 3, 3),
195        SaveSettings{
196            name: Some("foo"),
197        },
198        cbor!({
199            "name" => "foo",
200        }),
201        cbor!({}),
202        SaveSettingsResponse,
203    }
204}