From 64ea28af15a933145dd6e7179cac9f5823d64371 Mon Sep 17 00:00:00 2001 From: Ryan Summers Date: Tue, 18 Jun 2024 14:33:53 +0200 Subject: [PATCH 1/7] Updating serial-settings to save individual values --- serial-settings/src/lib.rs | 31 ++++++++++++++++++++++++++++++- src/settings.rs | 30 +++++++++++++++++++++++++----- 2 files changed, 55 insertions(+), 6 deletions(-) diff --git a/serial-settings/src/lib.rs b/serial-settings/src/lib.rs index 7c41ddf6c..49424dd00 100644 --- a/serial-settings/src/lib.rs +++ b/serial-settings/src/lib.rs @@ -22,6 +22,7 @@ //! list //! get //! set +//! save //! clear //! platform //! help [ ] @@ -83,6 +84,7 @@ pub trait Platform: Sized { fn save( &mut self, buffer: &mut [u8], + key: Option<&str>, settings: &Self::Settings, ) -> Result<(), Self::Error>; @@ -256,6 +258,25 @@ impl<'a, P: Platform, const Y: usize> Interface<'a, P, Y> { .unwrap(); } + fn handle_save( + _menu: &menu::Menu, + _item: &menu::Item, + _args: &[&str], + interface: &mut Self, + settings: &mut P::Settings, + ) { + match interface.platform.save(interface.buffer, None, settings) { + Ok(_) => writeln!( + interface, + "Settings saved. Reboot device (`platform reboot`) to apply." + ) + .unwrap(), + Err(e) => { + writeln!(interface, "Failed to save settings: {e:?}").unwrap() + } + } + } + fn handle_set( _menu: &menu::Menu, item: &menu::Item, @@ -273,7 +294,7 @@ impl<'a, P: Platform, const Y: usize> Interface<'a, P, Y> { { Ok(_) => { interface.updated = true; - match interface.platform.save(interface.buffer, settings) { + match interface.platform.save(interface.buffer, Some(key), settings) { Ok(_) => { writeln!( interface, @@ -331,6 +352,14 @@ impl<'a, P: Platform, const Y: usize> Interface<'a, P, Y> { ] }, }, + &menu::Item { + command: "save", + help: Some("Save all current settings to the device."), + item_type: menu::ItemType::Callback { + function: Self::handle_save, + parameters: &[], + }, + }, &menu::Item { command: "clear", help: Some("Clear the device settings to default values."), diff --git a/src/settings.rs b/src/settings.rs index eaeb8b7da..a994b2edc 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -167,15 +167,24 @@ where fn save( &mut self, buf: &mut [u8], + key: Option<&str>, settings: &Self::Settings, ) -> Result<(), Self::Error> { - for path in Self::Settings::iter_paths::>("/") { - let mut item = SettingsItem { - path: path.unwrap(), - ..Default::default() + let mut save_setting = |path| -> Result<(), Self::Error> { + let path = SettingsKey(path); + let mut data = heapless::Vec::new(); + data.resize(data.capacity(), 0).unwrap(); + + let mut serializer = postcard::Serializer { + output: postcard::ser_flavors::Slice::new(&mut data), }; - item.data.resize(item.data.capacity(), 0).unwrap(); + if let Err(e) = settings + .serialize_by_key(path.0.split('/').skip(1), &mut serializer) + { + log::warn!("Failed to save `{}` to flash: {e:?}", path.0); + return Ok(()); + } let flavor = postcard::ser_flavors::Slice::new(&mut item.data); @@ -210,6 +219,17 @@ where log::info!("Storing `{}` to flash", item.path); map::store_item(&mut self.storage, range, buf, item).unwrap(); } + + Ok(()) + }; + + if let Some(key) = key { + save_setting(heapless::String::from(key))?; + } else { + for path in Self::Settings::iter_paths::>("/") + { + save_setting(path.unwrap())?; + } } Ok(()) From 596110d1057f56dc74a77a725cee04169309bd8d Mon Sep 17 00:00:00 2001 From: Ryan Summers Date: Tue, 18 Jun 2024 14:36:16 +0200 Subject: [PATCH 2/7] Updating messages in serial-settings --- serial-settings/src/lib.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/serial-settings/src/lib.rs b/serial-settings/src/lib.rs index 49424dd00..7e1300007 100644 --- a/serial-settings/src/lib.rs +++ b/serial-settings/src/lib.rs @@ -226,7 +226,7 @@ impl<'a, P: Platform, const Y: usize> Interface<'a, P, Y> { match interface.platform.save(interface.buffer, settings) { Ok(_) => { - writeln!(interface, "Settings saved. Reboot device (`platform reboot`) to apply.") + writeln!(interface, "Settings saved. You may need to reboot for the settings to be applied") } Err(e) => { writeln!(interface, "Failed to clear settings: {e:?}") @@ -268,7 +268,7 @@ impl<'a, P: Platform, const Y: usize> Interface<'a, P, Y> { match interface.platform.save(interface.buffer, None, settings) { Ok(_) => writeln!( interface, - "Settings saved. Reboot device (`platform reboot`) to apply." + "Settings saved. You may need to reboot for the settings to be applied" ) .unwrap(), Err(e) => { @@ -289,7 +289,6 @@ impl<'a, P: Platform, const Y: usize> Interface<'a, P, Y> { menu::argument_finder(item, args, "value").unwrap().unwrap(); // Now, write the new value into memory. - // TODO: Validate it first? match settings.set_json(key, value.as_bytes()) { Ok(_) => { @@ -298,7 +297,7 @@ impl<'a, P: Platform, const Y: usize> Interface<'a, P, Y> { Ok(_) => { writeln!( interface, - "Settings saved. Reboot device (`platform reboot`) to apply." + "Settings saved. You may need to reboot for the settings to be applied" ) } Err(e) => { From 193497e95b891e1699b8ea7bb1e5ae13fbc27e2c Mon Sep 17 00:00:00 2001 From: Ryan Summers Date: Tue, 18 Jun 2024 14:42:01 +0200 Subject: [PATCH 3/7] Fixing save semantics --- serial-settings/src/lib.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/serial-settings/src/lib.rs b/serial-settings/src/lib.rs index 7e1300007..7dc574003 100644 --- a/serial-settings/src/lib.rs +++ b/serial-settings/src/lib.rs @@ -197,7 +197,8 @@ impl<'a, P: Platform, const Y: usize> Interface<'a, P, Y> { interface: &mut Self, settings: &mut P::Settings, ) { - if let Some(key) = menu::argument_finder(item, args, "item").unwrap() { + let maybe_key = menu::argument_finder(item, args, "item").unwrap(); + if let Some(key) = maybe_key { let mut defaults = settings.clone(); defaults.reset(); @@ -224,7 +225,7 @@ impl<'a, P: Platform, const Y: usize> Interface<'a, P, Y> { writeln!(interface, "All settings cleared").unwrap(); } - match interface.platform.save(interface.buffer, settings) { + match interface.platform.save(interface.buffer, maybe_key, settings) { Ok(_) => { writeln!(interface, "Settings saved. You may need to reboot for the settings to be applied") } From 41f98b4d48275a17324fc73490cd2033277d0b56 Mon Sep 17 00:00:00 2001 From: Ryan Summers Date: Tue, 18 Jun 2024 14:50:34 +0200 Subject: [PATCH 4/7] Fixing save semantics --- src/settings.rs | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/src/settings.rs b/src/settings.rs index a994b2edc..0838a9ed2 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -171,20 +171,12 @@ where settings: &Self::Settings, ) -> Result<(), Self::Error> { let mut save_setting = |path| -> Result<(), Self::Error> { - let path = SettingsKey(path); - let mut data = heapless::Vec::new(); - data.resize(data.capacity(), 0).unwrap(); - - let mut serializer = postcard::Serializer { - output: postcard::ser_flavors::Slice::new(&mut data), + let mut item = SettingsItem { + path, + ..Default::default() }; - if let Err(e) = settings - .serialize_by_key(path.0.split('/').skip(1), &mut serializer) - { - log::warn!("Failed to save `{}` to flash: {e:?}", path.0); - return Ok(()); - } + item.data.resize(item.data.capacity(), 0).unwrap(); let flavor = postcard::ser_flavors::Slice::new(&mut item.data); @@ -196,7 +188,7 @@ where "Failed to save `{}` to flash: {e:?}", item.path ); - continue; + return Ok(()); } Ok(slice) => slice.len(), }; From 60ffea60fe7b4933eff5faa2c75bf947ddbfe2c6 Mon Sep 17 00:00:00 2001 From: Ryan Summers Date: Tue, 18 Jun 2024 14:59:18 +0200 Subject: [PATCH 5/7] Removed save operation from settings update --- serial-settings/src/lib.rs | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/serial-settings/src/lib.rs b/serial-settings/src/lib.rs index 7dc574003..f89db543e 100644 --- a/serial-settings/src/lib.rs +++ b/serial-settings/src/lib.rs @@ -294,17 +294,10 @@ impl<'a, P: Platform, const Y: usize> Interface<'a, P, Y> { { Ok(_) => { interface.updated = true; - match interface.platform.save(interface.buffer, Some(key), settings) { - Ok(_) => { - writeln!( - interface, - "Settings saved. You may need to reboot for the settings to be applied" - ) - } - Err(e) => { - writeln!(interface, "Failed to save settings: {e:?}") - } - } + writeln!( + interface, + "Settings updated. You may need to reboot for the setting to be applied" + ) }, Err(e) => { writeln!(interface, "Failed to update {key}: {e:?}") From cd4999c39f7727198e689b63895c588613cc0320 Mon Sep 17 00:00:00 2001 From: Ryan Summers Date: Tue, 18 Jun 2024 15:19:03 +0200 Subject: [PATCH 6/7] Updating save to allow specific items --- serial-settings/src/lib.rs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/serial-settings/src/lib.rs b/serial-settings/src/lib.rs index f89db543e..d91b18171 100644 --- a/serial-settings/src/lib.rs +++ b/serial-settings/src/lib.rs @@ -22,8 +22,8 @@ //! list //! get //! set -//! save -//! clear +//! save [item] +//! clear [item] //! platform //! help [ ] //! @@ -266,7 +266,7 @@ impl<'a, P: Platform, const Y: usize> Interface<'a, P, Y> { interface: &mut Self, settings: &mut P::Settings, ) { - match interface.platform.save(interface.buffer, None, settings) { + match interface.platform.save(interface.buffer, menu::argument_finder(item, args, "item").unwrap(), settings) { Ok(_) => writeln!( interface, "Settings saved. You may need to reboot for the settings to be applied" @@ -347,10 +347,15 @@ impl<'a, P: Platform, const Y: usize> Interface<'a, P, Y> { }, &menu::Item { command: "save", - help: Some("Save all current settings to the device."), + help: Some("Save settings to the device."), item_type: menu::ItemType::Callback { function: Self::handle_save, - parameters: &[], + parameters: &[ + menu::Parameter::Optional { + parameter_name: "item", + help: Some("The name of the setting to clear."), + }, + ] }, }, &menu::Item { From c8beb2eeeb2ec0eb097c186db0492129df521064 Mon Sep 17 00:00:00 2001 From: Ryan Summers Date: Tue, 18 Jun 2024 15:23:20 +0200 Subject: [PATCH 7/7] Fixing build --- serial-settings/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/serial-settings/src/lib.rs b/serial-settings/src/lib.rs index d91b18171..9d381a63a 100644 --- a/serial-settings/src/lib.rs +++ b/serial-settings/src/lib.rs @@ -261,8 +261,8 @@ impl<'a, P: Platform, const Y: usize> Interface<'a, P, Y> { fn handle_save( _menu: &menu::Menu, - _item: &menu::Item, - _args: &[&str], + item: &menu::Item, + args: &[&str], interface: &mut Self, settings: &mut P::Settings, ) {