X-Git-Url: https://code.octet-stream.net/m17rt/blobdiff_plain/7b7b572052951018da4d6d0f5753d627836236ad..d610ea7b4224b89b5a1c481697d6c9e8fa7ce87c:/m17core/src/tnc.rs?ds=sidebyside diff --git a/m17core/src/tnc.rs b/m17core/src/tnc.rs index 26f8f4a..ae95628 100644 --- a/m17core/src/tnc.rs +++ b/m17core/src/tnc.rs @@ -26,7 +26,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 +65,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,7 +127,6 @@ impl SoftTnc { } } } - Ok(()) } /// Update the number of samples that have been received by the incoming stream, as a form of timekeeping @@ -144,7 +144,7 @@ impl SoftTnc { /// /// 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 +154,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 +168,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 +271,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 +282,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 +361,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 +402,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 +413,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);