Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/serial settings save individual values #907

Merged
merged 7 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 43 additions & 16 deletions serial-settings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
//! list
//! get <item>
//! set <item> <value>
//! clear
//! save [item]
//! clear [item]
//! platform <cmd>
//! help [ <command> ]
//!
Expand Down Expand Up @@ -83,6 +84,7 @@ pub trait Platform<const Y: usize>: Sized {
fn save(
&mut self,
buffer: &mut [u8],
key: Option<&str>,
settings: &Self::Settings,
) -> Result<(), Self::Error>;

Expand Down Expand Up @@ -195,7 +197,8 @@ impl<'a, P: Platform<Y>, 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();

Expand All @@ -222,9 +225,9 @@ impl<'a, P: Platform<Y>, 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. 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:?}")
Expand Down Expand Up @@ -256,6 +259,25 @@ impl<'a, P: Platform<Y>, const Y: usize> Interface<'a, P, Y> {
.unwrap();
}

fn handle_save(
_menu: &menu::Menu<Self, P::Settings>,
item: &menu::Item<Self, P::Settings>,
args: &[&str],
interface: &mut Self,
settings: &mut P::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"
)
.unwrap(),
Err(e) => {
writeln!(interface, "Failed to save settings: {e:?}").unwrap()
}
}
}

fn handle_set(
_menu: &menu::Menu<Self, P::Settings>,
item: &menu::Item<Self, P::Settings>,
Expand All @@ -268,22 +290,14 @@ impl<'a, P: Platform<Y>, 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(_) => {
interface.updated = true;
match interface.platform.save(interface.buffer, settings) {
Ok(_) => {
writeln!(
interface,
"Settings saved. Reboot device (`platform reboot`) to apply."
)
}
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:?}")
Expand Down Expand Up @@ -331,6 +345,19 @@ impl<'a, P: Platform<Y>, const Y: usize> Interface<'a, P, Y> {
]
},
},
&menu::Item {
command: "save",
help: Some("Save settings to the device."),
item_type: menu::ItemType::Callback {
function: Self::handle_save,
parameters: &[
menu::Parameter::Optional {
parameter_name: "item",
help: Some("The name of the setting to clear."),
},
]
},
},
&menu::Item {
command: "clear",
help: Some("Clear the device settings to default values."),
Expand Down
18 changes: 15 additions & 3 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,12 @@ where
fn save(
&mut self,
buf: &mut [u8],
key: Option<&str>,
settings: &Self::Settings,
) -> Result<(), Self::Error> {
for path in Self::Settings::iter_paths::<String<64>>("/") {
let mut save_setting = |path| -> Result<(), Self::Error> {
let mut item = SettingsItem {
path: path.unwrap(),
path,
..Default::default()
};

Expand All @@ -187,7 +188,7 @@ where
"Failed to save `{}` to flash: {e:?}",
item.path
);
continue;
return Ok(());
}
Ok(slice) => slice.len(),
};
Expand All @@ -210,6 +211,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::<heapless::String<64>>("/")
{
save_setting(path.unwrap())?;
}
}

Ok(())
Expand Down
Loading