common: Implement the "dump_storage" shell command.
authorTilman Sauerbeck <tilman@code-monkey.de>
Wed, 8 Jan 2020 11:44:09 +0000 (12:44 +0100)
committerTilman Sauerbeck <tilman@code-monkey.de>
Thu, 9 Jan 2020 14:19:36 +0000 (15:19 +0100)
Lists the mx25l flash's contents in the yencode format.

src/common/shell.rs

index ab814962ad0ea83368caec4546a5f2f7195f8a18..730a0a89fd78a85131fe221d943d5f5425ef0426 100644 (file)
@@ -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<u8> {
     }
 }
 
+fn read_char_delay_ms(limit_ms: u32) -> Option<u8> {
+    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();
+    }
 }