- // I want to call tnc.read() here
- // Probably these needs a helper in m17core::kiss? It will be common to both TNC and host
-
- // After a read...
- // if this does not start with FEND, forget all data up until first FEND
- // if we start with a FEND, see if there is another FEND with at least one byte between
- // for each such case, turn that FEND..=FEND slice into a KissFrame and attempt to parse it
- // once all such pairs have been handled...
- // move the last FEND onwards back to the start of the buffer
- // - if there is no room to do so, this is an oversize frame. purge everything and carry on.
- // perform next read from end
+ let mut buf = kiss_buffer.buf_remaining();
+ let n = match tnc.read(&mut buf) {
+ Ok(n) => n,
+ Err(_) => break,
+ };
+ kiss_buffer.did_write(n);
+ while let Some(frame) = kiss_buffer.next_frame() {
+ if frame.command() != Ok(KissCommand::DataFrame) {
+ continue;
+ }
+ match frame.port() {
+ Ok(m17core::kiss::PORT_PACKET_BASIC) => {
+ // no action
+ // we will handle the more full-featured version from from port 1
+ }
+ Ok(m17core::kiss::PORT_PACKET_FULL) => {
+ let mut payload = [0u8; 855]; // 30 byte LSF + 825 byte packet including CRC
+ let Ok(n) = frame.decode_payload(&mut payload) else {
+ debug!("failed to decode payload from KISS frame");
+ continue;
+ };
+ if n < 33 {
+ debug!("unusually short full packet frame");
+ continue;
+ }
+ let lsf = LsfFrame(payload[0..30].try_into().unwrap());
+ if lsf.check_crc() != 0 {
+ debug!("LSF in full packet frame did not pass CRC");
+ continue;
+ }
+ if lsf.encryption_type() != EncryptionType::None {
+ debug!("we only understand None encryption for now - skipping packet");
+ continue;
+ }
+ let Some((packet_type, type_len)) = PacketType::from_proto(&payload[30..n])
+ else {
+ debug!("failed to decode packet type");
+ continue;
+ };
+ if (n - 30 - type_len) < 2 {
+ debug!("packet payload too small to provide CRC");
+ continue;
+ }
+ let packet_crc = m17core::crc::m17_crc(&payload[30..n]);
+ if packet_crc != 0 {
+ debug!("packet CRC does not pass");
+ continue;
+ }
+ let packet_payload: Arc<[u8]> =
+ Arc::from(&payload[(30 + type_len)..(n - 2)]);
+
+ let subs: Vec<_> =
+ adapters.read().unwrap().packet.values().cloned().collect();
+ for s in subs {
+ s.packet_received(
+ lsf.clone(),
+ packet_type.clone(),
+ packet_payload.clone(),
+ );
+ }
+ }
+ Ok(m17core::kiss::PORT_STREAM) => {
+ let mut payload = [0u8; 32];
+ let Ok(n) = frame.decode_payload(&mut payload) else {
+ debug!("failed to decode stream payload from KISS frame");
+ continue;
+ };
+ if n == 30 {
+ let lsf = LsfFrame(payload[0..30].try_into().unwrap());
+ if lsf.check_crc() != 0 {
+ debug!("initial LSF in stream did not pass CRC");
+ continue;
+ }
+ stream_running = true;
+ let subs: Vec<_> =
+ adapters.read().unwrap().stream.values().cloned().collect();
+ for s in subs {
+ s.stream_began(lsf.clone());
+ }
+ } else if n == 26 {
+ if !stream_running {
+ debug!("ignoring stream data as we didn't get a valid LSF first");
+ continue;
+ }
+ // TODO: parse LICH and handle the different changing subvalues META could have
+ if m17core::crc::m17_crc(&payload[6..n]) != 0 {
+ debug!("stream data CRC mismatch");
+ continue;
+ }
+ let mut frame_number = u16::from_be_bytes([payload[6], payload[7]]);
+ let is_final = (frame_number & 0x8000) > 0;
+ frame_number &= 0x7fff;
+ let data: [u8; 16] = payload[8..24].try_into().unwrap();
+ let data = Arc::new(data);
+ if is_final {
+ stream_running = false;
+ }
+ let subs: Vec<_> =
+ adapters.read().unwrap().stream.values().cloned().collect();
+ for s in subs {
+ s.stream_data(frame_number, is_final, data.clone());
+ }
+ }
+ }
+ _ => (),
+ }
+ }