X-Git-Url: https://code.octet-stream.net/m17rt/blobdiff_plain/7b7b572052951018da4d6d0f5753d627836236ad..cd3124ca701db72364554f91fdb6a119faa876ec:/m17core/src/tnc.rs diff --git a/m17core/src/tnc.rs b/m17core/src/tnc.rs index 26f8f4a..f3622e5 100644 --- a/m17core/src/tnc.rs +++ b/m17core/src/tnc.rs @@ -1,4 +1,5 @@ use crate::kiss::{KissBuffer, KissFrame}; +use crate::modem::ModulatorFrame; use crate::protocol::{Frame, LichCollection, LsfFrame, Mode, PacketFrameCounter}; /// Handles the KISS protocol and frame management for `SoftModulator` and `SoftDemodulator`. @@ -26,7 +27,7 @@ impl SoftTnc { } /// Process an individual `Frame` that has been decoded by the modem. - pub fn handle_frame(&mut self, frame: Frame) -> Result<(), SoftTncError> { + pub fn handle_frame(&mut self, frame: Frame) { match frame { Frame::Lsf(lsf) => { // A new LSF implies a clean slate. @@ -65,6 +66,7 @@ impl SoftTnc { let end = start + payload_len; rx.packet[start..(start + payload_len)] .copy_from_slice(&packet.payload); + // TODO: compatible packets should be sent on port 0 too let kiss = KissFrame::new_full_packet(&rx.lsf.0, &rx.packet[0..end]) .unwrap(); @@ -126,25 +128,28 @@ impl SoftTnc { } } } - Ok(()) } - /// Update the number of samples that have been received by the incoming stream, as a form of timekeeping - pub fn advance_samples(&mut self, _samples: u64) {} - pub fn set_data_carrier_detect(&mut self, _dcd: bool) {} + + pub fn set_now(&mut self, samples: u64) {} + + pub fn set_tx_end_time(&mut self, in_samples: usize) { + // This is a relative time from now, expressed in samples + // Use the time from set_now() to decide when to drop PTT + } - pub fn read_tx_frame(&mut self) -> Result, SoftTncError> { - // yes we want to deal with Frames here + pub fn read_tx_frame(&mut self) -> Option { + // yes we want to deal with frames here // it's important to establish successful decode that SoftDemodulator is aware of the frame innards - Ok(None) + None } /// Read KISS message to be sent to host. /// /// After each frame input, this should be consumed in a loop until length 0 is returned. /// This component will never block. Upstream interface can provide blocking `read()` if desired. - pub fn read_kiss(&mut self, target_buf: &mut [u8]) -> Result { + pub fn read_kiss(&mut self, target_buf: &mut [u8]) -> usize { match self.outgoing_kiss.as_mut() { Some(outgoing) => { let n = (outgoing.kiss_frame.len - outgoing.sent).min(target_buf.len()); @@ -154,13 +159,13 @@ impl SoftTnc { if outgoing.sent == outgoing.kiss_frame.len { self.outgoing_kiss = None; } - Ok(n) + n } - None => Ok(0), + None => 0, } } - pub fn write_kiss(&mut self, buf: &[u8]) -> Result { + pub fn write_kiss(&mut self, buf: &[u8]) -> usize { let target_buf = self.kiss_buffer.buf_remaining(); let n = buf.len().min(target_buf.len()); target_buf[0..n].copy_from_slice(&buf[0..n]); @@ -168,7 +173,7 @@ impl SoftTnc { while let Some(_kiss_frame) = self.kiss_buffer.next_frame() { // TODO: handle host-to-TNC message } - Ok(n) + n } fn kiss_to_host(&mut self, kiss_frame: KissFrame) { @@ -271,10 +276,10 @@ mod tests { }; let mut tnc = SoftTnc::new(); let mut kiss = KissFrame::new_empty(); - assert_eq!(tnc.read_kiss(&mut kiss.data), Ok(0)); + assert_eq!(tnc.read_kiss(&mut kiss.data), 0); - tnc.handle_frame(Frame::Lsf(lsf)).unwrap(); - kiss.len = tnc.read_kiss(&mut kiss.data).unwrap(); + tnc.handle_frame(Frame::Lsf(lsf)); + kiss.len = tnc.read_kiss(&mut kiss.data); assert_eq!(kiss.command().unwrap(), KissCommand::DataFrame); assert_eq!(kiss.port().unwrap(), PORT_STREAM); @@ -282,16 +287,16 @@ mod tests { let n = kiss.decode_payload(&mut payload_buf).unwrap(); assert_eq!(n, 30); - tnc.handle_frame(Frame::Stream(stream1)).unwrap(); - kiss.len = tnc.read_kiss(&mut kiss.data).unwrap(); + tnc.handle_frame(Frame::Stream(stream1)); + kiss.len = tnc.read_kiss(&mut kiss.data); assert_eq!(kiss.command().unwrap(), KissCommand::DataFrame); assert_eq!(kiss.port().unwrap(), PORT_STREAM); let n = kiss.decode_payload(&mut payload_buf).unwrap(); assert_eq!(n, 26); - tnc.handle_frame(Frame::Stream(stream2)).unwrap(); - kiss.len = tnc.read_kiss(&mut kiss.data).unwrap(); + tnc.handle_frame(Frame::Stream(stream2)); + kiss.len = tnc.read_kiss(&mut kiss.data); assert_eq!(kiss.command().unwrap(), KissCommand::DataFrame); assert_eq!(kiss.port().unwrap(), PORT_STREAM); @@ -361,9 +366,9 @@ mod tests { let mut tnc = SoftTnc::new(); let mut kiss = KissFrame::new_empty(); for f in frames { - tnc.handle_frame(Frame::Stream(f)).unwrap(); + tnc.handle_frame(Frame::Stream(f)); } - kiss.len = tnc.read_kiss(&mut kiss.data).unwrap(); + kiss.len = tnc.read_kiss(&mut kiss.data); let mut payload_buf = [0u8; 2048]; let n = kiss.decode_payload(&mut payload_buf).unwrap(); assert_eq!(n, 30); @@ -402,10 +407,10 @@ mod tests { }; let mut tnc = SoftTnc::new(); let mut kiss = KissFrame::new_empty(); - assert_eq!(tnc.read_kiss(&mut kiss.data), Ok(0)); + assert_eq!(tnc.read_kiss(&mut kiss.data), 0); - tnc.handle_frame(Frame::Lsf(lsf)).unwrap(); - kiss.len = tnc.read_kiss(&mut kiss.data).unwrap(); + tnc.handle_frame(Frame::Lsf(lsf)); + kiss.len = tnc.read_kiss(&mut kiss.data); assert_eq!(kiss.command().unwrap(), KissCommand::DataFrame); assert_eq!(kiss.port().unwrap(), PORT_STREAM); @@ -413,16 +418,16 @@ mod tests { let n = kiss.decode_payload(&mut payload_buf).unwrap(); assert_eq!(n, 30); - tnc.handle_frame(Frame::Stream(stream1)).unwrap(); - kiss.len = tnc.read_kiss(&mut kiss.data).unwrap(); + tnc.handle_frame(Frame::Stream(stream1)); + kiss.len = tnc.read_kiss(&mut kiss.data); assert_eq!(kiss.command().unwrap(), KissCommand::DataFrame); assert_eq!(kiss.port().unwrap(), PORT_STREAM); let n = kiss.decode_payload(&mut payload_buf).unwrap(); assert_eq!(n, 26); - tnc.handle_frame(Frame::Stream(stream3)).unwrap(); - kiss.len = tnc.read_kiss(&mut kiss.data).unwrap(); + tnc.handle_frame(Frame::Stream(stream3)); + kiss.len = tnc.read_kiss(&mut kiss.data); assert_eq!(kiss.command().unwrap(), KissCommand::DataFrame); assert_eq!(kiss.port().unwrap(), PORT_STREAM);