+ InvalidState,
+}
+
+struct OutgoingKiss {
+ kiss_frame: KissFrame,
+ sent: usize,
+}
+
+enum State {
+ /// Nothing happening. We may have TX data queued but we won't act on it until CSMA opens up.
+ Idle,
+
+ /// We received some stream data but missed the leading LSF so we are trying to assemble from LICH.
+ RxAcquiringStream(RxAcquiringStreamState),
+
+ /// We have acquired an identified stream transmission and are sending data payloads to the host.
+ RxStream(RxStreamState),
+
+ /// We are receiving a packet. All is well so far, and there is more data to come before we tell the host.
+ RxPacket(RxPacketState),
+
+ /// PTT is on and this is a stream-type transmission. New data may be added.
+ TxStream,
+
+ /// We have delivered the last frame in the current stream
+ TxStreamSentEndOfStream,
+
+ /// PTT is on and this is a packet-type transmission. New packets may be enqueued.
+ TxPacket,
+
+ /// We gave modulator an EndOfTransmission. PTT is still on, waiting for modulator to advise end time.
+ TxEnding,
+
+ /// Ending transmission, PTT remains on, but we know the timestamp at which we should disengage it.
+ TxEndingAtTime(u64),
+}
+
+struct RxAcquiringStreamState {
+ /// Partial assembly of LSF by accumulating LICH fields.
+ lich: LichCollection,
+}
+
+struct RxStreamState {
+ /// Track identifying information for this transmission so we can tell if it changes.
+ lsf: LsfFrame,
+
+ /// Expected next frame number. Allowed to skip values on RX, but not go backwards.
+ index: u16,
+}
+
+struct RxPacketState {
+ /// Initial LSF
+ lsf: LsfFrame,
+
+ /// Accumulation of packet data that we have received so far.
+ packet: [u8; 825],
+
+ /// Number of payload frames we have received. If we are stably in the RxPacket state,
+ /// this will be between 0 and 32 inclusive.
+ count: usize,
+}
+
+struct PendingPacket {
+ lsf: Option<LsfFrame>,
+
+ app_data: [u8; 825],
+ app_data_len: usize,
+ app_data_transmitted: usize,
+}
+
+impl PendingPacket {
+ fn new() -> Self {
+ Self {
+ lsf: None,
+ app_data: [0u8; 825],
+ app_data_len: 0,
+ app_data_transmitted: 0,
+ }
+ }
+
+ /// Returns next frame, not including preamble or EOT.
+ ///
+ /// False means all data frames have been sent.
+ fn next_frame(&mut self) -> Option<ModulatorFrame> {
+ if let Some(lsf) = self.lsf.take() {
+ return Some(ModulatorFrame::Lsf(lsf));
+ }
+ if self.app_data_len == self.app_data_transmitted {
+ return None;
+ }
+ let remaining = self.app_data_len - self.app_data_transmitted;
+ let (counter, data_len) = if remaining <= 25 {
+ (
+ PacketFrameCounter::FinalFrame {
+ payload_len: remaining,
+ },
+ remaining,
+ )
+ } else {
+ (
+ PacketFrameCounter::Frame {
+ index: self.app_data_transmitted / 25,
+ },
+ 25,
+ )
+ };
+ let mut payload = [0u8; 25];
+ payload.copy_from_slice(
+ &self.app_data[self.app_data_transmitted..(self.app_data_transmitted + data_len)],
+ );
+ self.app_data_transmitted += data_len;
+ Some(ModulatorFrame::Packet(PacketFrame { payload, counter }))
+ }
+}
+
+impl Default for PendingPacket {
+ fn default() -> Self {
+ Self {
+ lsf: None,
+ app_data: [0u8; 825],
+ app_data_len: 0,
+ app_data_transmitted: 0,
+ }
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::kiss::{KissCommand, PORT_STREAM};
+ use crate::protocol::StreamFrame;
+
+ // TODO: finish all handle_frame tests as below
+ // this will be much more straightforward when we have a way to create LSFs programatically
+
+ // receiving a single-frame packet
+
+ // receiving a multi-frame packet
+
+ // part of one packet and then another
+
+ #[test]
+ fn tnc_receive_stream() {
+ let lsf = LsfFrame([
+ 255, 255, 255, 255, 255, 255, 0, 0, 0, 159, 221, 81, 5, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 131, 53,
+ ]);
+ let stream1 = StreamFrame {
+ lich_idx: 0,
+ lich_part: [255, 255, 255, 255, 255],
+ frame_number: 0,
+ end_of_stream: false,
+ stream_data: [
+ 128, 0, 119, 115, 220, 252, 41, 235, 8, 0, 116, 195, 94, 244, 45, 75,
+ ],
+ };
+ let stream2 = StreamFrame {
+ lich_idx: 1,
+ lich_part: [255, 0, 0, 0, 159],
+ frame_number: 1,
+ end_of_stream: true,
+ stream_data: [
+ 17, 0, 94, 82, 216, 135, 181, 15, 30, 0, 125, 195, 152, 183, 41, 57,
+ ],
+ };
+ let mut tnc = SoftTnc::new();
+ let mut kiss = KissFrame::new_empty();
+ assert_eq!(tnc.read_kiss(&mut kiss.data), 0);
+
+ 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);
+
+ let mut payload_buf = [0u8; 2048];
+ let n = kiss.decode_payload(&mut payload_buf).unwrap();
+ assert_eq!(n, 30);
+
+ 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));
+ 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);
+ }
+
+ #[test]
+ fn tnc_acquire_stream() {
+ let frames = [
+ StreamFrame {
+ lich_idx: 0,
+ lich_part: [255, 255, 255, 255, 255],
+ frame_number: 0,
+ end_of_stream: false,
+ stream_data: [
+ 128, 0, 119, 115, 220, 252, 41, 235, 8, 0, 116, 195, 94, 244, 45, 75,
+ ],
+ },
+ StreamFrame {
+ lich_idx: 1,
+ lich_part: [255, 0, 0, 0, 159],
+ frame_number: 1,
+ end_of_stream: false,
+ stream_data: [
+ 17, 0, 94, 82, 216, 135, 181, 15, 30, 0, 125, 195, 152, 183, 41, 57,
+ ],
+ },
+ StreamFrame {
+ lich_idx: 2,
+ lich_part: [221, 81, 5, 5, 0],
+ frame_number: 2,
+ end_of_stream: false,
+ stream_data: [
+ 17, 128, 93, 74, 154, 167, 169, 11, 20, 0, 116, 91, 158, 220, 45, 111,
+ ],
+ },
+ StreamFrame {
+ lich_idx: 3,
+ lich_part: [0, 0, 0, 0, 0],
+ frame_number: 3,
+ end_of_stream: false,
+ stream_data: [
+ 15, 128, 114, 83, 218, 252, 59, 111, 31, 128, 116, 91, 84, 231, 45, 105,
+ ],
+ },
+ StreamFrame {
+ lich_idx: 4,
+ lich_part: [0, 0, 0, 0, 0],
+ frame_number: 4,
+ end_of_stream: false,
+ stream_data: [
+ 9, 128, 119, 115, 220, 220, 57, 15, 48, 128, 124, 83, 158, 236, 181, 91,
+ ],
+ },
+ StreamFrame {
+ lich_idx: 5,
+ lich_part: [0, 0, 0, 131, 53],
+ frame_number: 5,
+ end_of_stream: false,
+ stream_data: [
+ 52, 0, 116, 90, 152, 167, 225, 216, 32, 0, 116, 83, 156, 212, 33, 216,
+ ],
+ },
+ ];
+
+ let mut tnc = SoftTnc::new();
+ let mut kiss = KissFrame::new_empty();
+ for f in frames {
+ tnc.handle_frame(Frame::Stream(f));
+ }
+ 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);
+ assert_eq!(
+ &payload_buf[0..30],
+ [
+ 255, 255, 255, 255, 255, 255, 0, 0, 0, 159, 221, 81, 5, 5, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 131, 53,
+ ]
+ );
+ }
+
+ #[test]
+ fn tnc_handle_skipped_stream_frame() {
+ let lsf = LsfFrame([
+ 255, 255, 255, 255, 255, 255, 0, 0, 0, 159, 221, 81, 5, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 131, 53,
+ ]);
+ let stream1 = StreamFrame {
+ lich_idx: 0,
+ lich_part: [255, 255, 255, 255, 255],
+ frame_number: 0,
+ end_of_stream: false,
+ stream_data: [
+ 128, 0, 119, 115, 220, 252, 41, 235, 8, 0, 116, 195, 94, 244, 45, 75,
+ ],
+ };
+ let stream3 = StreamFrame {
+ lich_idx: 2,
+ lich_part: [221, 81, 5, 5, 0],
+ frame_number: 2,
+ end_of_stream: false,
+ stream_data: [
+ 17, 128, 93, 74, 154, 167, 169, 11, 20, 0, 116, 91, 158, 220, 45, 111,
+ ],
+ };
+ let mut tnc = SoftTnc::new();
+ let mut kiss = KissFrame::new_empty();
+ assert_eq!(tnc.read_kiss(&mut kiss.data), 0);
+
+ 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);
+
+ let mut payload_buf = [0u8; 2048];
+ let n = kiss.decode_payload(&mut payload_buf).unwrap();
+ assert_eq!(n, 30);
+
+ 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));
+ 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);
+ }