Skip to content

Commit 9c882cf

Browse files
authored
Merge pull request #907 from quartiq/feature/serial-settings-save-individual-values
Feature/serial settings save individual values
2 parents 6b28ad1 + c8beb2e commit 9c882cf

2 files changed

Lines changed: 58 additions & 19 deletions

File tree

serial-settings/src/lib.rs

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
//! list
2323
//! get <item>
2424
//! set <item> <value>
25-
//! clear
25+
//! save [item]
26+
//! clear [item]
2627
//! platform <cmd>
2728
//! help [ <command> ]
2829
//!
@@ -83,6 +84,7 @@ pub trait Platform<const Y: usize>: Sized {
8384
fn save(
8485
&mut self,
8586
buffer: &mut [u8],
87+
key: Option<&str>,
8688
settings: &Self::Settings,
8789
) -> Result<(), Self::Error>;
8890

@@ -195,7 +197,8 @@ impl<'a, P: Platform<Y>, const Y: usize> Interface<'a, P, Y> {
195197
interface: &mut Self,
196198
settings: &mut P::Settings,
197199
) {
198-
if let Some(key) = menu::argument_finder(item, args, "item").unwrap() {
200+
let maybe_key = menu::argument_finder(item, args, "item").unwrap();
201+
if let Some(key) = maybe_key {
199202
let mut defaults = settings.clone();
200203
defaults.reset();
201204

@@ -222,9 +225,9 @@ impl<'a, P: Platform<Y>, const Y: usize> Interface<'a, P, Y> {
222225
writeln!(interface, "All settings cleared").unwrap();
223226
}
224227

225-
match interface.platform.save(interface.buffer, settings) {
228+
match interface.platform.save(interface.buffer, maybe_key, settings) {
226229
Ok(_) => {
227-
writeln!(interface, "Settings saved. Reboot device (`platform reboot`) to apply.")
230+
writeln!(interface, "Settings saved. You may need to reboot for the settings to be applied")
228231
}
229232
Err(e) => {
230233
writeln!(interface, "Failed to clear settings: {e:?}")
@@ -256,6 +259,25 @@ impl<'a, P: Platform<Y>, const Y: usize> Interface<'a, P, Y> {
256259
.unwrap();
257260
}
258261

262+
fn handle_save(
263+
_menu: &menu::Menu<Self, P::Settings>,
264+
item: &menu::Item<Self, P::Settings>,
265+
args: &[&str],
266+
interface: &mut Self,
267+
settings: &mut P::Settings,
268+
) {
269+
match interface.platform.save(interface.buffer, menu::argument_finder(item, args, "item").unwrap(), settings) {
270+
Ok(_) => writeln!(
271+
interface,
272+
"Settings saved. You may need to reboot for the settings to be applied"
273+
)
274+
.unwrap(),
275+
Err(e) => {
276+
writeln!(interface, "Failed to save settings: {e:?}").unwrap()
277+
}
278+
}
279+
}
280+
259281
fn handle_set(
260282
_menu: &menu::Menu<Self, P::Settings>,
261283
item: &menu::Item<Self, P::Settings>,
@@ -268,22 +290,14 @@ impl<'a, P: Platform<Y>, const Y: usize> Interface<'a, P, Y> {
268290
menu::argument_finder(item, args, "value").unwrap().unwrap();
269291

270292
// Now, write the new value into memory.
271-
// TODO: Validate it first?
272293
match settings.set_json(key, value.as_bytes())
273294
{
274295
Ok(_) => {
275296
interface.updated = true;
276-
match interface.platform.save(interface.buffer, settings) {
277-
Ok(_) => {
278-
writeln!(
279-
interface,
280-
"Settings saved. Reboot device (`platform reboot`) to apply."
281-
)
282-
}
283-
Err(e) => {
284-
writeln!(interface, "Failed to save settings: {e:?}")
285-
}
286-
}
297+
writeln!(
298+
interface,
299+
"Settings updated. You may need to reboot for the setting to be applied"
300+
)
287301
},
288302
Err(e) => {
289303
writeln!(interface, "Failed to update {key}: {e:?}")
@@ -331,6 +345,19 @@ impl<'a, P: Platform<Y>, const Y: usize> Interface<'a, P, Y> {
331345
]
332346
},
333347
},
348+
&menu::Item {
349+
command: "save",
350+
help: Some("Save settings to the device."),
351+
item_type: menu::ItemType::Callback {
352+
function: Self::handle_save,
353+
parameters: &[
354+
menu::Parameter::Optional {
355+
parameter_name: "item",
356+
help: Some("The name of the setting to clear."),
357+
},
358+
]
359+
},
360+
},
334361
&menu::Item {
335362
command: "clear",
336363
help: Some("Clear the device settings to default values."),

src/settings.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,12 @@ where
167167
fn save(
168168
&mut self,
169169
buf: &mut [u8],
170+
key: Option<&str>,
170171
settings: &Self::Settings,
171172
) -> Result<(), Self::Error> {
172-
for path in Self::Settings::iter_paths::<String<64>>("/") {
173+
let mut save_setting = |path| -> Result<(), Self::Error> {
173174
let mut item = SettingsItem {
174-
path: path.unwrap(),
175+
path,
175176
..Default::default()
176177
};
177178

@@ -187,7 +188,7 @@ where
187188
"Failed to save `{}` to flash: {e:?}",
188189
item.path
189190
);
190-
continue;
191+
return Ok(());
191192
}
192193
Ok(slice) => slice.len(),
193194
};
@@ -210,6 +211,17 @@ where
210211
log::info!("Storing `{}` to flash", item.path);
211212
map::store_item(&mut self.storage, range, buf, item).unwrap();
212213
}
214+
215+
Ok(())
216+
};
217+
218+
if let Some(key) = key {
219+
save_setting(heapless::String::from(key))?;
220+
} else {
221+
for path in Self::Settings::iter_paths::<heapless::String<64>>("/")
222+
{
223+
save_setting(path.unwrap())?;
224+
}
213225
}
214226

215227
Ok(())

0 commit comments

Comments
 (0)