X-Git-Url: https://code.octet-stream.net/m17rt/blobdiff_plain/b8702d115ebc071a9ae97e9aa9c4bc00196b5e26..608ca7e33ab51d812607ddcc3429bfa9aa3c34b0:/m17app/src/app.rs?ds=inline diff --git a/m17app/src/app.rs b/m17app/src/app.rs index 8130455..ae01976 100644 --- a/m17app/src/app.rs +++ b/m17app/src/app.rs @@ -1,7 +1,8 @@ use crate::adapter::{PacketAdapter, StreamAdapter}; +use crate::link_setup::LinkSetup; use crate::tnc::Tnc; use m17core::kiss::{KissBuffer, KissCommand, KissFrame}; -use m17core::protocol::{EncryptionType, LsfFrame, PacketType}; +use m17core::protocol::{EncryptionType, LsfFrame, PacketType, StreamFrame}; use log::debug; use std::collections::HashMap; @@ -60,14 +61,6 @@ impl M17App { } } - pub fn transmit_packet(&self, packet_type: PacketType, payload: &[u8]) { - // hang on where do we get the LSF details from? We need a destination obviously - // our source address needs to be configured here too - // also there is possible CAN, encryption, meta payload - - // we will immediately convert this into a KISS payload before sending into channel so we only need borrow on data - } - /// Create a handle that can be used to transmit data on the TNC pub fn tx(&self) -> TxHandle { TxHandle { @@ -80,6 +73,8 @@ impl M17App { } pub fn close(&self) { + // TODO: blocking function to indicate TNC has finished closing + // then we could call this in a signal handler to ensure PTT is dropped before quit let _ = self.event_tx.send(TncControlEvent::Close); } } @@ -89,13 +84,32 @@ pub struct TxHandle { } impl TxHandle { - // add more methods here for stream outgoing + pub fn transmit_packet(&self, link_setup: &LinkSetup, packet_type: PacketType, payload: &[u8]) { + let (pack_type, pack_type_len) = packet_type.as_proto(); + if pack_type_len + payload.len() > 823 { + // TODO: error for invalid transmission type + return; + } + let mut full_payload = vec![]; + full_payload.extend_from_slice(&pack_type[0..pack_type_len]); + full_payload.extend_from_slice(payload); + let crc = m17core::crc::m17_crc(&full_payload); + full_payload.extend_from_slice(&crc.to_be_bytes()); + let kiss_frame = KissFrame::new_full_packet(&link_setup.raw.0, &full_payload).unwrap(); + let _ = self.event_tx.send(TncControlEvent::Kiss(kiss_frame)); + } - pub fn transmit_stream_start(&self /* lsf?, payload? what needs to be configured ?! */) {} + pub fn transmit_stream_start(&self, link_setup: &LinkSetup) { + let kiss_frame = KissFrame::new_stream_setup(&link_setup.raw.0).unwrap(); + let _ = self.event_tx.send(TncControlEvent::Kiss(kiss_frame)); + } // as long as there is only one TNC it is implied there is only ever one stream transmission in flight - pub fn transmit_stream_next(&self, /* next payload, */ end_of_stream: bool) {} + pub fn transmit_stream_next(&self, stream: &StreamFrame) { + let kiss_frame = KissFrame::new_stream_data(stream).unwrap(); + let _ = self.event_tx.send(TncControlEvent::Kiss(kiss_frame)); + } } /// Synchronised structure for listeners subscribing to packets and streams. @@ -119,19 +133,20 @@ impl Adapters { } /// Carries a request from a method on M17App to the TNC's writer thread, which will execute it. +#[allow(clippy::large_enum_variant)] enum TncControlEvent { Kiss(KissFrame), Start, Close, } -fn spawn_reader(mut tnc: T, adapters: Arc>) { +fn spawn_reader(mut tnc: T, adapters: Arc>) { std::thread::spawn(move || { let mut kiss_buffer = KissBuffer::new(); let mut stream_running = false; loop { - let mut buf = kiss_buffer.buf_remaining(); - let n = match tnc.read(&mut buf) { + let buf = kiss_buffer.buf_remaining(); + let n = match tnc.read(buf) { Ok(n) => n, Err(_) => break, }; @@ -156,7 +171,7 @@ fn spawn_reader(mut tnc: T, adapters: Arc(mut tnc: T, adapters: Arc(mut tnc: T, adapters: Arc(mut tnc: T, adapters: Arc = adapters.read().unwrap().stream.values().cloned().collect(); for s in subs { - s.stream_began(lsf.clone()); + s.stream_began(LinkSetup::new_raw(lsf.clone())); } } else if n == 26 { if !stream_running { @@ -241,12 +256,12 @@ fn spawn_reader(mut tnc: T, adapters: Arc(mut tnc: T, event_rx: mpsc::Receiver) { +fn spawn_writer(mut tnc: T, event_rx: mpsc::Receiver) { std::thread::spawn(move || { while let Ok(ev) = event_rx.recv() { match ev { TncControlEvent::Kiss(k) => { - if let Err(e) = tnc.write_all(&k.as_bytes()) { + if let Err(e) = tnc.write_all(k.as_bytes()) { debug!("kiss send err: {:?}", e); return; }