]> code.octet-stream.net Git - m17rt/commitdiff
Make Input RRC creation fallible and read samples upfront
authorThomas Karpiniec <tom.karpiniec@outlook.com>
Sat, 1 Feb 2025 06:51:02 +0000 (17:51 +1100)
committerThomas Karpiniec <tom.karpiniec@outlook.com>
Sat, 1 Feb 2025 06:51:02 +0000 (17:51 +1100)
m17app/src/error.rs
m17app/src/soundmodem.rs

index acbf397f28c1d4a6f247a59b6cf8e1c0833d0ab2..c1bcac1e2950da9b5cdffb1e58e6f6cf1df87007 100644 (file)
@@ -1,3 +1,5 @@
+use std::path::PathBuf;
+
 use thiserror::Error;
 
 #[derive(Debug, Error, PartialEq, Eq, Clone)]
@@ -21,4 +23,10 @@ pub enum M17Error {
         "provided packet payload is too large: provided {provided} bytes, capacity {capacity}"
     )]
     PacketTooLarge { provided: usize, capacity: usize },
+
+    #[error("provided path to RRC file could not be opened: {0}")]
+    InvalidRrcPath(PathBuf),
+
+    #[error("failed to read from RRC file: {0}")]
+    RrcReadFailed(PathBuf),
 }
index 391bfadde1b66b2ee33c1b7a336991aabe126e19..16bb97642900e86139c5d22aab31a681b0b53c2d 100644 (file)
@@ -1,3 +1,4 @@
+use crate::error::M17Error;
 use crate::tnc::{Tnc, TncError};
 use log::debug;
 use m17core::kiss::MAX_FRAME_LEN;
@@ -240,29 +241,28 @@ pub trait InputSource: Send + Sync + 'static {
 }
 
 pub struct InputRrcFile {
-    path: PathBuf,
+    baseband: Arc<[u8]>,
     end_tx: Mutex<Option<Sender<()>>>,
 }
 
 impl InputRrcFile {
-    pub fn new(path: PathBuf) -> Self {
-        Self {
-            path,
+    pub fn new(path: PathBuf) -> Result<Self, M17Error> {
+        let mut file = File::open(&path).map_err(|_| M17Error::InvalidRrcPath(path.clone()))?;
+        let mut baseband = vec![];
+        file.read_to_end(&mut baseband)
+            .map_err(|_| M17Error::RrcReadFailed(path))?;
+        Ok(Self {
+            baseband: baseband.into(),
             end_tx: Mutex::new(None),
-        }
+        })
     }
 }
 
 impl InputSource for InputRrcFile {
     fn start(&self, samples: SyncSender<SoundmodemEvent>) {
         let (end_tx, end_rx) = channel();
-        let path = self.path.clone();
+        let baseband = self.baseband.clone();
         std::thread::spawn(move || {
-            // TODO: error handling
-            let mut file = File::open(path).unwrap();
-            let mut baseband = vec![];
-            file.read_to_end(&mut baseband).unwrap();
-
             // assuming 48 kHz for now
             const TICK: Duration = Duration::from_millis(25);
             const SAMPLES_PER_TICK: usize = 1200;