1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
//! Storage interface.

use std::fmt::Display;
use std::io::{Read, Write};
use std::ops::Add;
use std::path::Path;
use std::{fs, io};

use bytes::BytesMut;
use regex::Regex;
use serialport::SerialPort;

use crate::serial::{SerialCli, CLI_EOL};

const BUF_SIZE: usize = 1024;

/// Interface to Flipper device storage.
pub struct FlipperStorage {
    cli: SerialCli,
}

impl FlipperStorage {
    /// Create new [`FlipperStorage`] connected to a [`SerialPort`].
    pub fn new(port: Box<dyn SerialPort>) -> Self {
        Self {
            cli: SerialCli::new(port),
        }
    }

    /// Start serial interface.
    pub fn start(&mut self) -> io::Result<()> {
        self.cli.start()
    }

    /// Get reference to underlying [`SerialPort`].
    pub fn port(&self) -> &dyn SerialPort {
        self.cli.port()
    }

    /// Get mutable reference to underlying [`SerialPort`].
    pub fn port_mut(&mut self) -> &mut dyn SerialPort {
        self.cli.port_mut()
    }

    /// Get mutable reference to underlying [`SerialCli`].
    pub fn cli_mut(&mut self) -> &mut SerialCli {
        &mut self.cli
    }

    /// List files and directories on the device.
    pub fn list_tree(&mut self, path: &FlipperPath) -> io::Result<()> {
        // Note: The `storage list` command expects that paths do not end with a slash.
        self.cli
            .send_and_wait_eol(&format!("storage list {}", path))?;

        let data = self.cli.read_until_prompt()?;
        for line in CLI_EOL.split(&data).map(String::from_utf8_lossy) {
            let line = line.trim();
            if line.is_empty() {
                continue;
            }

            if let Some(error) = SerialCli::get_error(line) {
                eprintln!("ERROR: {error}");
                continue;
            }

            if line == "Empty" {
                continue;
            }

            if let Some((typ, info)) = line.split_once(' ') {
                match typ {
                    // Directory
                    "[D]" => {
                        let path = path.clone() + info;

                        eprintln!("{path}");
                        self.list_tree(&path)?;
                    }
                    // File
                    "[F]" => {
                        if let Some((name, size)) = info.rsplit_once(' ') {
                            let path = path.clone() + name;

                            eprintln!("{path}, size {size}");
                        }
                    }
                    // We got something unexpected, ignore it
                    _ => (),
                }
            }
        }

        Ok(())
    }

    /// Send local file to the device.
    pub fn send_file(&mut self, from: impl AsRef<Path>, to: &FlipperPath) -> io::Result<()> {
        // Try to create directory on Flipper
        if let Some(dir) = to.0.rsplit_once('/') {
            self.mkdir(&FlipperPath::from(dir.0)).ok();
        }
        self.remove(to).ok();

        let mut file = fs::File::open(from.as_ref())?;

        let mut buf = [0u8; BUF_SIZE];
        loop {
            let n = file.read(&mut buf)?;
            if n == 0 {
                break;
            }

            self.cli
                .send_and_wait_eol(&format!("storage write_chunk \"{to}\" {n}"))?;
            let line = self.cli.read_until_eol()?;
            let line = String::from_utf8_lossy(&line);

            if let Some(error) = SerialCli::get_error(&line) {
                self.cli.read_until_prompt()?;

                return Err(io::Error::new(io::ErrorKind::Other, error));
            }

            self.port_mut().write_all(&buf[..n])?;
            self.cli.read_until_prompt()?;
        }

        Ok(())
    }

    /// Receive remote file from the device.
    pub fn receive_file(&mut self, from: &FlipperPath, to: impl AsRef<Path>) -> io::Result<()> {
        let mut file = fs::File::options()
            .create(true)
            .truncate(true)
            .write(true)
            .open(to.as_ref())?;

        let data = self.read_file(from)?;
        file.write_all(&data)?;

        Ok(())
    }

    /// Read file data from the device.
    pub fn read_file(&mut self, path: &FlipperPath) -> io::Result<BytesMut> {
        self.cli
            .send_and_wait_eol(&format!("storage read_chunks \"{path}\" {}", BUF_SIZE))?;
        let line = self.cli.read_until_eol()?;
        let line = String::from_utf8_lossy(&line);

        if let Some(error) = SerialCli::get_error(&line) {
            self.cli.read_until_prompt()?;

            return Err(io::Error::new(io::ErrorKind::Other, error));
        }

        let (_, size) = line
            .split_once(": ")
            .ok_or_else(|| io::Error::new(io::ErrorKind::Other, "failed to read chunk size"))?;
        let size: usize = size
            .parse()
            .map_err(|_| io::Error::new(io::ErrorKind::Other, "failed to parse chunk size"))?;

        let mut data = BytesMut::with_capacity(BUF_SIZE);

        let mut buf = [0u8; BUF_SIZE];
        while data.len() < size {
            self.cli.read_until_ready()?;
            self.cli.send_line("y")?;

            let n = (size - data.len()).min(BUF_SIZE);
            self.port_mut().read_exact(&mut buf[..n])?;
            data.extend_from_slice(&buf[..n]);
        }

        Ok(data)
    }

    /// Does the file or directory exist on the device?
    pub fn exist(&mut self, path: &FlipperPath) -> io::Result<bool> {
        let exist = match self.stat(path) {
            Err(_err) => false,
            Ok(_) => true,
        };

        Ok(exist)
    }

    /// Does the directory exist on the device?
    pub fn exist_dir(&mut self, path: &FlipperPath) -> io::Result<bool> {
        let exist = match self.stat(path) {
            Err(_err) => false,
            Ok(stat) => stat.contains("Directory") || stat.contains("Storage"),
        };

        Ok(exist)
    }

    /// Does the file exist on the device?
    pub fn exist_file(&mut self, path: &FlipperPath) -> io::Result<bool> {
        let exist = match self.stat(path) {
            Err(_err) => false,
            Ok(stat) => stat.contains("File, size:"),
        };

        Ok(exist)
    }

    /// File size in bytes
    pub fn size(&mut self, path: &FlipperPath) -> io::Result<usize> {
        let line = self.stat(path)?;

        let size = Regex::new(r"File, size: (.+)b")
            .unwrap()
            .captures(&line)
            .and_then(|m| m[1].parse::<usize>().ok())
            .ok_or_else(|| io::Error::new(io::ErrorKind::Other, "failed to parse size"))?;

        Ok(size)
    }

    /// Stat a file or directory.
    fn stat(&mut self, path: &FlipperPath) -> io::Result<String> {
        self.cli
            .send_and_wait_eol(&format!("storage stat {path}"))?;
        let line = self.cli.consume_response()?;

        Ok(line)
    }

    /// Make directory on the device.
    pub fn mkdir(&mut self, path: &FlipperPath) -> io::Result<()> {
        self.cli
            .send_and_wait_eol(&format!("storage mkdir {path}"))?;
        self.cli.consume_response()?;

        Ok(())
    }

    /// Format external storage.
    pub fn format_ext(&mut self) -> io::Result<()> {
        self.cli.send_and_wait_eol("storage format /ext")?;
        self.cli.send_and_wait_eol("y")?;
        self.cli.consume_response()?;

        Ok(())
    }

    /// Remove file or directory.
    pub fn remove(&mut self, path: &FlipperPath) -> io::Result<()> {
        self.cli
            .send_and_wait_eol(&format!("storage remove {path}"))?;
        self.cli.consume_response()?;

        Ok(())
    }

    /// Calculate MD5 hash of file.
    pub fn md5sum(&mut self, path: &FlipperPath) -> io::Result<String> {
        self.cli.send_and_wait_eol(&format!("storage md5 {path}"))?;
        let line = self.cli.consume_response()?;

        Ok(line)
    }
}

/// A path on the Flipper device.
///
/// [`FlipperPath`] maintains certain invariants:
/// - Paths are valid UTF-8
/// - Paths are always absolute (start with `/`)
/// - Paths do not end in a `/`
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FlipperPath(String);

impl FlipperPath {
    /// Create a new [`FlipperPath`].
    pub fn new() -> Self {
        Self(String::from("/"))
    }

    /// Push a path fragment to this path
    pub fn push(&mut self, path: &str) {
        let path = path.trim_end_matches('/');
        if path.starts_with('/') {
            // Absolute path
            self.0 = String::from(path);
        } else {
            // Relative path
            if !self.0.ends_with('/') {
                self.0 += "/";
            }
            self.0 += path;
        }
    }
}

impl Default for FlipperPath {
    fn default() -> Self {
        Self::new()
    }
}

impl Display for FlipperPath {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.0)
    }
}

impl AsRef<str> for FlipperPath {
    fn as_ref(&self) -> &str {
        self.0.as_str()
    }
}

impl From<String> for FlipperPath {
    fn from(mut value: String) -> Self {
        if let Some(p) = value.rfind(|c| c != '/') {
            // Drop any trailing `/`
            value.truncate(p + 1);
        }

        if !value.starts_with('/') {
            // Make path absolute
            let mut path = Self::new();
            path.0.extend([value]);

            path
        } else {
            Self(value)
        }
    }
}

impl From<&str> for FlipperPath {
    fn from(value: &str) -> Self {
        FlipperPath::from(value.to_string())
    }
}

impl Add<&str> for FlipperPath {
    type Output = Self;

    fn add(mut self, rhs: &str) -> Self::Output {
        self.push(rhs);

        self
    }
}