From aa0f0d612a03799423e87e9137765ecb90146f48 Mon Sep 17 00:00:00 2001 From: Tilman Sauerbeck Date: Wed, 8 Jan 2020 11:52:36 +0100 Subject: [PATCH] common: Implement the "clear_storage" shell command. Erases all of the mx25l flash's contents. --- src/application/main.rs | 2 +- src/common/shell.rs | 22 +++++++++++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/application/main.rs b/src/application/main.rs index cf06f64..3523b88 100644 --- a/src/application/main.rs +++ b/src/application/main.rs @@ -267,7 +267,7 @@ pub unsafe extern "C" fn _start() -> ! { } }); - shell.update(); + shell.update(&mut mx25l); if reset_requested() { nvic::system_reset(); diff --git a/src/common/shell.rs b/src/common/shell.rs index 09c7907..ab81496 100644 --- a/src/common/shell.rs +++ b/src/common/shell.rs @@ -22,6 +22,7 @@ */ use buffer::Buffer; +use storage::Storage; pub struct Shell<'a> { tx_buf: &'a mut Buffer, @@ -88,6 +89,10 @@ fn read_char() -> Option { } } +struct Context<'a> { + storage: &'a mut dyn Storage, +} + impl<'a> Shell<'a> { pub fn new(tx_buf: &mut Buffer) -> Shell { Shell { @@ -97,7 +102,11 @@ impl<'a> Shell<'a> { } } - pub fn update(&mut self) { + pub fn update(&mut self, storage: &mut dyn Storage) { + let mut context = Context { + storage: storage, + }; + while let Some(c) = read_char() { if c != b'\n' { if self.command_offset != self.command_buffer.len() { @@ -112,7 +121,7 @@ impl<'a> Shell<'a> { self.command_buffer[command_length] = b'\0'; if command_length != 0 { - self.dispatch(command_length); + self.dispatch(command_length, &mut context); } } else { self.command_buffer[self.command_offset] = b'\0'; @@ -126,7 +135,7 @@ impl<'a> Shell<'a> { } } - fn dispatch(&mut self, command_length: usize) { + fn dispatch(&mut self, command_length: usize, mut context: &mut Context) { let command : [u8; 32] = self.command_buffer; let mut args_iter = ArgumentIter { @@ -139,11 +148,14 @@ impl<'a> Shell<'a> { let usage = b"\ Supported commands: help Show this help message. + clear_storage Fully erase the flash's contents. "; self.tx_buf.write(usage); }, + Some(b"clear_storage") => self.run_clear_storage(&mut context), + Some(ref other) => { self.tx_buf.write(b"unknown_command: "); self.tx_buf.write(other); @@ -154,4 +166,8 @@ Supported commands: } } } + + fn run_clear_storage(&self, context: &mut Context) { + context.storage.clear(); + } } -- 2.30.2