From: Tilman Sauerbeck Date: Wed, 8 Jan 2020 11:44:09 +0000 (+0100) Subject: common: Implement the "dump_storage" shell command. X-Git-Url: http://git.code-monkey.de/?a=commitdiff_plain;h=b0d277d6cb2a60e8dd3ec94b6c1dae569f8070bf;p=gps-watch.git common: Implement the "dump_storage" shell command. Lists the mx25l flash's contents in the yencode format. --- diff --git a/src/common/shell.rs b/src/common/shell.rs index ab81496..730a0a8 100644 --- a/src/common/shell.rs +++ b/src/common/shell.rs @@ -23,6 +23,8 @@ use buffer::Buffer; use storage::Storage; +use yencode::Yencode; +use systick; pub struct Shell<'a> { tx_buf: &'a mut Buffer, @@ -89,6 +91,23 @@ fn read_char() -> Option { } } +fn read_char_delay_ms(limit_ms: u32) -> Option { + let mut total_delay_ms = 0u32; + + while total_delay_ms < limit_ms { + if let Some(c) = read_char() { + return Some(c); + } + + let ms = 50; + + systick::delay_ms(ms); + total_delay_ms += ms; + } + + None +} + struct Context<'a> { storage: &'a mut dyn Storage, } @@ -149,12 +168,14 @@ impl<'a> Shell<'a> { Supported commands: help Show this help message. clear_storage Fully erase the flash's contents. + dump_storage Dump the flash's contents. "; self.tx_buf.write(usage); }, Some(b"clear_storage") => self.run_clear_storage(&mut context), + Some(b"dump_storage") => self.run_dump_storage(&mut context), Some(ref other) => { self.tx_buf.write(b"unknown_command: "); @@ -170,4 +191,40 @@ Supported commands: fn run_clear_storage(&self, context: &mut Context) { context.storage.clear(); } + + fn run_dump_storage(&mut self, context: &mut Context) { + self.tx_buf.write(b"waiting for receiver to start...\n"); + self.tx_buf.flush(); + + let have_receiver = read_char_delay_ms(5000).map(|c| { + c == b'R' + }).map_or_else(|| { + false + }, |_| { + let mut yenc = Yencode::new(&mut self.tx_buf); + + yenc.start(b"gps-watch-storage.bin"); + + const CHUNK_SIZE: usize = 1024; + let num_chunks = context.storage.size() / CHUNK_SIZE; + + for i in 0..num_chunks { + let mut buf = [0u8; CHUNK_SIZE]; + + context.storage.read(i * CHUNK_SIZE, &mut buf); + + yenc.data(&buf); + } + + yenc.finish(); + + true + }); + + if !have_receiver { + self.tx_buf.write(b"no signal from receiver\n"); + } + + self.tx_buf.flush(); + } }