]> code.octet-stream.net Git - m17rt/blobdiff - m17app/src/adapter.rs
Simplify adapter lifecycle and introduce a lot of error propagation
[m17rt] / m17app / src / adapter.rs
index 57e01bbbabae6e35947a07ceacdb50219446e67b..0e23924690fe695d74808ef7f724e748adb36efd 100644 (file)
@@ -1,28 +1,74 @@
-use crate::{app::TxHandle, link_setup::LinkSetup};
+use crate::{app::TxHandle, error::AdapterError, link_setup::LinkSetup};
 use m17core::protocol::PacketType;
 use std::sync::Arc;
 
+/// Can be connected to an `M17App` to receive incoming packet data.
+///
+/// The `packet_received` callback will be fired once for each incoming packet type. Any filtering
+/// must be done by the receiver. There are also some lifecycle callbacks, one of which will provide
+/// a `TxHandle` when the adapter is first added to the app. This means the adapter can transmit as
+/// well as receive.
 pub trait PacketAdapter: Send + Sync + 'static {
-    fn adapter_registered(&self, _id: usize, _handle: TxHandle) {}
-    fn adapter_removed(&self) {}
-    fn tnc_started(&self) {}
-    fn tnc_closed(&self) {}
-    fn packet_received(
-        &self,
-        _link_setup: LinkSetup,
-        _packet_type: PacketType,
-        _content: Arc<[u8]>,
-    ) {
+    /// TNC is online. New packets may now be received and it is okay to transmit.
+    fn start(&self, handle: TxHandle) -> Result<(), AdapterError> {
+        let _ = handle;
+        Ok(())
+    }
+
+    /// This adapter or the whole TNC has been shut down. There will be no more tx/rx. This is a
+    /// permanent operation.
+    fn close(&self) -> Result<(), AdapterError> {
+        Ok(())
+    }
+
+    /// A packet has been received and assembled by the radio.
+    fn packet_received(&self, link_setup: LinkSetup, packet_type: PacketType, content: Arc<[u8]>) {
+        let _ = link_setup;
+        let _ = packet_type;
+        let _ = content;
     }
 }
 
+/// Can be connected to an `M17App` to receive incoming streams (voice or data).
+///
+/// Once an incoming stream has been acquired (either by receiving a Link Setup Frame or by decoding
+/// an ongoing LICH), all stream frames will be provided to this adapter.
+///
+/// There are also some lifecycle callbacks, one of which will provide a `TxHandle` when the adapter
+/// is first added to the app. This means the adapter can transmit as well as receive.
 pub trait StreamAdapter: Send + Sync + 'static {
-    fn adapter_registered(&self, _id: usize, _handle: TxHandle) {}
-    fn adapter_removed(&self) {}
-    fn tnc_started(&self) {}
-    fn tnc_closed(&self) {}
-    fn stream_began(&self, _link_setup: LinkSetup) {}
-    fn stream_data(&self, _frame_number: u16, _is_final: bool, _data: Arc<[u8; 16]>) {}
+    /// TNC is online. New streams may now be received and it is okay to transmit.
+    fn start(&self, handle: TxHandle) -> Result<(), AdapterError> {
+        let _ = handle;
+        Ok(())
+    }
+
+    /// This adapter or the whole TNC has been shut down. There will be no more tx/rx. This is a
+    /// permanent operation.
+    fn close(&self) -> Result<(), AdapterError> {
+        Ok(())
+    }
+
+    /// A new incoming stream has begun.
+    ///
+    /// If we did not receive the end of the previous stream, this may occur even there was never a
+    /// `stream_data` where `is_final` is true.
+    fn stream_began(&self, link_setup: LinkSetup) {
+        let _ = link_setup;
+    }
+
+    /// A frame has been received for an ongoing incoming stream.
+    ///
+    /// It is not guaranteed to receive every frame. Frame numbers may not start from 0, and they will
+    /// wrap around to 0 after 0x7fff. If we receive an indication that the frame is the final one then
+    /// `is_final` is set. If the transmitter never sends that frame or we fail to receive it then the
+    /// stream may trail off without that being set. Implementors should consider setting an appropriate
+    /// timeout to consider a stream "dead" and wait for the next `stream_began`.
+    fn stream_data(&self, frame_number: u16, is_final: bool, data: Arc<[u8; 16]>) {
+        let _ = frame_number;
+        let _ = is_final;
+        let _ = data;
+    }
 
     // TODO
     // fn stream_lost(&self);