Skip to content

Commit

Permalink
Fixing after testing
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-summers committed Nov 13, 2023
1 parent 8532886 commit 5e59ea6
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 18 deletions.
26 changes: 18 additions & 8 deletions src/hardware/flash.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use embedded_storage::nor_flash::{ReadNorFlash, NorFlash};
use stm32h7xx_hal::flash::{LockedFlashBank, UnlockedFlashBank};
use core::fmt::Write;
use embedded_storage::nor_flash::{NorFlash, ReadNorFlash};
use stm32h7xx_hal::flash::{LockedFlashBank, UnlockedFlashBank};

#[derive(miniconf::Tree)]
pub struct Settings {
Expand All @@ -26,15 +26,19 @@ pub struct FlashSettings {
}

impl FlashSettings {
pub fn new(mut flash: LockedFlashBank, mac: smoltcp_nal::smoltcp::wire::EthernetAddress) -> Self {
pub fn new(
mut flash: LockedFlashBank,
mac: smoltcp_nal::smoltcp::wire::EthernetAddress,
) -> Self {
let mut settings = Settings::new(mac);
let mut buffer = [0u8; 256];
let mut offset: usize = 0;

// We iteratively read the settings from flash to allow for easy expansion of the settings
// without losing data in the future when new fields are added.
flash.read(offset as u32, &mut buffer[..]).unwrap();
let len = buffer.iter().skip(1).position(|x| x == &b'"').unwrap_or(0) + 2;
let len =
buffer.iter().skip(1).position(|x| x == &b'"').unwrap_or(0) + 2;
settings.broker = {
match serde_json_core::from_slice(&buffer[..len]) {
Ok((item, size)) => {
Expand All @@ -49,11 +53,11 @@ impl FlashSettings {
};

flash.read(offset as u32, &mut buffer[..]).unwrap();
let len = buffer.iter().skip(1).position(|x| x == &b'"').unwrap_or(0) + 2;
let len =
buffer.iter().skip(1).position(|x| x == &b'"').unwrap_or(0) + 2;
settings.id = {
match serde_json_core::from_slice(&buffer[..len]) {
Ok((item, size)) => {
offset += size;
item
}
Err(e) => {
Expand All @@ -70,8 +74,14 @@ impl FlashSettings {
let mut bank = self.flash.unlocked();
let mut data = [0; 512];
let mut offset: usize = 0;
offset += serde_json_core::to_slice(&self.settings.broker, &mut data[offset..]).unwrap();
offset += serde_json_core::to_slice(&self.settings.id, &mut data[offset..]).unwrap();
offset += serde_json_core::to_slice(
&self.settings.broker,
&mut data[offset..],
)
.unwrap();
offset +=
serde_json_core::to_slice(&self.settings.id, &mut data[offset..])
.unwrap();

bank.erase(0, UnlockedFlashBank::ERASE_SIZE as u32).unwrap();
bank.write(0, &data[..offset]).unwrap();
Expand Down
13 changes: 11 additions & 2 deletions src/hardware/serial_terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,15 @@ fn handle_property_read(

let mut buf = [0u8; 256];
for path in props {
let len = context.flash.settings.get_json(path, &mut buf).unwrap();
let len = match context.flash.settings.get_json(path, &mut buf) {
Err(e) => {
writeln!(&mut context.output, "Failed to read {path}: {e}")
.unwrap();
return;
}
Ok(len) => len,
};

let stringified = core::str::from_utf8(&buf[..len]).unwrap();
writeln!(&mut context.output, "{path}: {stringified}").unwrap();
}
Expand Down Expand Up @@ -149,7 +157,8 @@ fn handle_property_write(
.unwrap();
}
Err(e) => {
writeln!(&mut context.output, "Failed to update {property}: {e:?}").unwrap();
writeln!(&mut context.output, "Failed to update {property}: {e:?}")
.unwrap();
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/hardware/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1072,7 +1072,8 @@ pub fn setup(

let (_, flash_bank2) = device.FLASH.split();

let settings = FlashSettings::new(flash_bank2.unwrap(), network_devices.mac_address.clone());
let settings =
FlashSettings::new(flash_bank2.unwrap(), network_devices.mac_address);

let stabilizer = StabilizerDevices {
systick,
Expand Down
9 changes: 2 additions & 7 deletions src/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ pub mod data_stream;
pub mod network_processor;
pub mod telemetry;

use crate::hardware::{
EthernetPhy, NetworkManager, NetworkStack, SystemTimer,
};
use crate::hardware::{EthernetPhy, NetworkManager, NetworkStack, SystemTimer};
use data_stream::{DataStream, FrameGenerator};
use network_processor::NetworkProcessor;
use telemetry::TelemetryClient;
Expand Down Expand Up @@ -224,10 +222,7 @@ where
///
/// # Returns
/// A client ID that may be used for MQTT client identification.
fn get_client_id(
id: &str,
mode: &str,
) -> String<64> {
fn get_client_id(id: &str, mode: &str) -> String<64> {
let mut identifier = String::new();
write!(&mut identifier, "{id}-{mode}").unwrap();
identifier
Expand Down

0 comments on commit 5e59ea6

Please sign in to comment.