- // 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.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<_> =
+ listeners.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) => {
+ // handle stream and send it to subscribers
+ }
+ _ => (),
+ }
+ }