Skip to content

Commit

Permalink
Updating after review
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-summers committed Nov 14, 2023
1 parent b7d333e commit 6f17197
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 38 deletions.
8 changes: 8 additions & 0 deletions src/hardware/flash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ use stm32h7xx_hal::flash::{LockedFlashBank, UnlockedFlashBank};
pub struct Settings {
pub broker: heapless::String<255>,
pub id: heapless::String<23>,
#[serde(skip)]
#[tree(skip)]
pub mac: smoltcp_nal::smoltcp::wire::EthernetAddress,
}

impl Settings {
Expand All @@ -16,8 +19,13 @@ impl Settings {
Self {
broker: "mqtt".into(),
id,
mac,
}
}

pub fn reset(&mut self) {
*self = Self::new(self.mac)
}
}

pub struct FlashSettings {
Expand Down
90 changes: 52 additions & 38 deletions src/hardware/serial_terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,45 +13,53 @@ const ROOT_MENU: menu::Menu<Context> = menu::Menu {
label: "root",
items: &[
&menu::Item {
command: "reset",
help: Some("Reset Stabilizer to force new settings to take effect."),
command: "reboot",
help: Some("Reboot the device to force new settings to take effect."),
item_type: menu::ItemType::Callback {
function: handle_reset,
function: handle_device_reboot,
parameters: &[]
},
},
&menu::Item {
command: "factory-reset",
help: Some("Reset the device settings to default values."),
item_type: menu::ItemType::Callback {
function: handle_settings_reset,
parameters: &[]
},
},
&menu::Item {
command: "list",
help: Some("List all available properties."),
help: Some("List all available settings and their current values."),
item_type: menu::ItemType::Callback {
function: handle_list,
parameters: &[],
},
},
&menu::Item {
command: "read",
help: Some("Read a property from the device."),
help: Some("Read a setting_from the device."),
item_type: menu::ItemType::Callback {
function: handle_property_read,
parameters: &[menu::Parameter::Optional {
parameter_name: "property",
help: Some("The name of the property to read. If not specified, all properties are read."),
function: handle_setting_read,
parameters: &[menu::Parameter::Mandatory {
parameter_name: "item",
help: Some("The name of the setting to read."),
}]
},
},
&menu::Item {
command: "write",
help: Some("Read a property to the device."),
help: Some("Update a a setting in the device."),
item_type: menu::ItemType::Callback {
function: handle_property_write,
function: handle_setting_write,
parameters: &[
menu::Parameter::Mandatory {
parameter_name: "property",
help: Some("The name of the property to write."),
parameter_name: "item",
help: Some("The name of the setting to write."),
},
menu::Parameter::Mandatory {
parameter_name: "value",
help: Some("Specifies the value to be written to the property."),
help: Some("Specifies the value to be written. Values must be JSON-encoded"),
},
]
},
Expand Down Expand Up @@ -102,13 +110,17 @@ fn handle_list(
context: &mut Context,
) {
writeln!(context, "Available properties:").unwrap();

let mut buf = [0; 256];
for path in Settings::iter_paths::<heapless::String<32>>("/") {
let path = path.unwrap();
writeln!(context, "* {path}").unwrap();
let len = context.flash.settings.get_json(&path, &mut buf).unwrap();
let stringified = core::str::from_utf8(&buf[..len]).unwrap();
writeln!(context, "{path}: {stringified}").unwrap();
}
}

fn handle_reset(
fn handle_device_reboot(
_menu: &menu::Menu<Context>,
_item: &menu::Item<Context>,
_args: &[&str],
Expand All @@ -117,48 +129,50 @@ fn handle_reset(
cortex_m::peripheral::SCB::sys_reset();
}

fn handle_property_read(
fn handle_settings_reset(
_menu: &menu::Menu<Context>,
_item: &menu::Item<Context>,
_args: &[&str],
context: &mut Context,
) {
context.flash.settings.reset();
context.flash.save();
}

fn handle_setting_read(
_menu: &menu::Menu<Context>,
item: &menu::Item<Context>,
args: &[&str],
context: &mut Context,
) {
let mut buf = [0u8; 256];
if let Some(path) = menu::argument_finder(item, args, "property").unwrap() {
let len = match context.flash.settings.get_json(path, &mut buf) {
Err(e) => {
writeln!(context, "Failed to read {path}: {e}").unwrap();
return;
}
Ok(len) => len,
};

let stringified = core::str::from_utf8(&buf[..len]).unwrap();
writeln!(context, "{path}: {stringified}").unwrap();
let key = menu::argument_finder(item, args, "item").unwrap().unwrap();
let len = match context.flash.settings.get_json(key, &mut buf) {
Err(e) => {
writeln!(context, "Failed to read {key}: {e}").unwrap();
return;
}
Ok(len) => len,
};

for path in Settings::iter_paths::<heapless::String<32>>("/") {
let path = path.unwrap();
let len = context.flash.settings.get_json(&path, &mut buf).unwrap();
let stringified = core::str::from_utf8(&buf[..len]).unwrap();
writeln!(context, "{path}: {stringified}").unwrap();
}
let stringified = core::str::from_utf8(&buf[..len]).unwrap();
writeln!(context, "{key}: {stringified}").unwrap();
}

fn handle_property_write(
fn handle_setting_write(
_menu: &menu::Menu<Context>,
item: &menu::Item<Context>,
args: &[&str],
context: &mut Context,
) {
let property = menu::argument_finder(item, args, "property")
let key = menu::argument_finder(item, args, "item")
.unwrap()
.unwrap();
let value = menu::argument_finder(item, args, "value").unwrap().unwrap();

// Now, write the new value into memory.
// TODO: Validate it first?
match context.flash.settings.set_json(property, value.as_bytes()) {
match context.flash.settings.set_json(key, value.as_bytes()) {
Ok(_) => {
context.flash.save();
writeln!(
Expand All @@ -169,7 +183,7 @@ fn handle_property_write(
.unwrap();
}
Err(e) => {
writeln!(context, "Failed to update {property}: {e:?}").unwrap();
writeln!(context, "Failed to update {key}: {e:?}").unwrap();
}
}
}
Expand Down

0 comments on commit 6f17197

Please sign in to comment.