+ Start,
+ Close,
+}
+
+fn spawn_soundmodem_worker(
+ event_tx: SyncSender<SoundmodemEvent>,
+ event_rx: Receiver<SoundmodemEvent>,
+ kiss_out_tx: SyncSender<Arc<[u8]>>,
+ input: Box<dyn InputSource>,
+) {
+ std::thread::spawn(move || {
+ // TODO: should be able to provide a custom Demodulator for a soundmodem
+ let mut demod = SoftDemodulator::new();
+ let mut tnc = SoftTnc::new();
+ let mut buf = [0u8; MAX_FRAME_LEN];
+ while let Ok(ev) = event_rx.recv() {
+ match ev {
+ SoundmodemEvent::Kiss(k) => {
+ let _n = tnc.write_kiss(&k);
+ // TODO: what does it mean if we fail to write it all?
+ // Probably we have to read frames for tx first - revisit this during tx
+ }
+ SoundmodemEvent::BasebandInput(b) => {
+ for sample in &*b {
+ if let Some(frame) = demod.demod(*sample) {
+ tnc.handle_frame(frame);
+ loop {
+ let n = tnc.read_kiss(&mut buf);
+ if n > 0 {
+ let _ = kiss_out_tx.try_send(buf[0..n].into());
+ } else {
+ break;
+ }
+ }
+ }
+ }
+ }
+ SoundmodemEvent::Start => input.start(event_tx.clone()),
+ SoundmodemEvent::Close => break,
+ }
+ }
+ });