]> code.octet-stream.net Git - m17rt/blob - m17app/src/adapter.rs
Simplify adapter lifecycle and introduce a lot of error propagation
[m17rt] / m17app / src / adapter.rs
1 use crate::{app::TxHandle, error::AdapterError, link_setup::LinkSetup};
2 use m17core::protocol::PacketType;
3 use std::sync::Arc;
4
5 /// Can be connected to an `M17App` to receive incoming packet data.
6 ///
7 /// The `packet_received` callback will be fired once for each incoming packet type. Any filtering
8 /// must be done by the receiver. There are also some lifecycle callbacks, one of which will provide
9 /// a `TxHandle` when the adapter is first added to the app. This means the adapter can transmit as
10 /// well as receive.
11 pub trait PacketAdapter: Send + Sync + 'static {
12 /// TNC is online. New packets may now be received and it is okay to transmit.
13 fn start(&self, handle: TxHandle) -> Result<(), AdapterError> {
14 let _ = handle;
15 Ok(())
16 }
17
18 /// This adapter or the whole TNC has been shut down. There will be no more tx/rx. This is a
19 /// permanent operation.
20 fn close(&self) -> Result<(), AdapterError> {
21 Ok(())
22 }
23
24 /// A packet has been received and assembled by the radio.
25 fn packet_received(&self, link_setup: LinkSetup, packet_type: PacketType, content: Arc<[u8]>) {
26 let _ = link_setup;
27 let _ = packet_type;
28 let _ = content;
29 }
30 }
31
32 /// Can be connected to an `M17App` to receive incoming streams (voice or data).
33 ///
34 /// Once an incoming stream has been acquired (either by receiving a Link Setup Frame or by decoding
35 /// an ongoing LICH), all stream frames will be provided to this adapter.
36 ///
37 /// There are also some lifecycle callbacks, one of which will provide a `TxHandle` when the adapter
38 /// is first added to the app. This means the adapter can transmit as well as receive.
39 pub trait StreamAdapter: Send + Sync + 'static {
40 /// TNC is online. New streams may now be received and it is okay to transmit.
41 fn start(&self, handle: TxHandle) -> Result<(), AdapterError> {
42 let _ = handle;
43 Ok(())
44 }
45
46 /// This adapter or the whole TNC has been shut down. There will be no more tx/rx. This is a
47 /// permanent operation.
48 fn close(&self) -> Result<(), AdapterError> {
49 Ok(())
50 }
51
52 /// A new incoming stream has begun.
53 ///
54 /// If we did not receive the end of the previous stream, this may occur even there was never a
55 /// `stream_data` where `is_final` is true.
56 fn stream_began(&self, link_setup: LinkSetup) {
57 let _ = link_setup;
58 }
59
60 /// A frame has been received for an ongoing incoming stream.
61 ///
62 /// It is not guaranteed to receive every frame. Frame numbers may not start from 0, and they will
63 /// wrap around to 0 after 0x7fff. If we receive an indication that the frame is the final one then
64 /// `is_final` is set. If the transmitter never sends that frame or we fail to receive it then the
65 /// stream may trail off without that being set. Implementors should consider setting an appropriate
66 /// timeout to consider a stream "dead" and wait for the next `stream_began`.
67 fn stream_data(&self, frame_number: u16, is_final: bool, data: Arc<[u8; 16]>) {
68 let _ = frame_number;
69 let _ = is_final;
70 let _ = data;
71 }
72
73 // TODO
74 // fn stream_lost(&self);
75 // fn stream_assembled_text_block()
76 // fn stream_gnss_data()
77 // fn stream_extended_callsign_data()
78
79 // fn stream_tx_ended_early(&self); // underrun/overrun
80 }