common: Implement the "clear_storage" shell command.
[gps-watch.git] / src / common / shell.rs
index 09c79079d62178444b6d1f0dd82553dd4b616a92..ab814962ad0ea83368caec4546a5f2f7195f8a18 100644 (file)
@@ -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<u8> {
     }
 }
 
+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();
+    }
 }