]>
code.octet-stream.net Git - m17rt/blob - m17app/src/app.rs
a7bb3cd291b18efbf154ec5d54807fe32faf408a
1 use crate::adapter
::{PacketAdapter
, StreamAdapter
};
2 use crate::link_setup
::LinkSetup
;
4 use m17core
::kiss
::{KissBuffer
, KissCommand
, KissFrame
};
5 use m17core
::protocol
::{EncryptionType
, LsfFrame
, PacketType
, StreamFrame
};
8 use std
::collections
::HashMap
;
10 use std
::sync
::{Arc
, RwLock
};
13 adapters
: Arc
<RwLock
<Adapters
>>,
14 event_tx
: mpsc
::SyncSender
<TncControlEvent
>,
18 pub fn new
<T
: Tnc
+ Send
+ '
static>(mut tnc
: T
) -> Self {
19 let write_tnc
= tnc
.try_clone().unwrap
();
20 let (event_tx
, event_rx
) = mpsc
::sync_channel(128);
21 let listeners
= Arc
::new(RwLock
::new(Adapters
::new()));
22 spawn_reader(tnc
, listeners
.clone());
23 spawn_writer(write_tnc
, event_rx
);
30 pub fn add_packet_adapter
<P
: PacketAdapter
+ '
static>(&self, adapter
: P
) -> usize {
31 let adapter
= Arc
::new(adapter
);
32 let mut adapters
= self.adapters
.write().unwrap
();
33 let id
= adapters
.next
;
35 adapters
.packet
.insert
(id
, adapter
.clone());
37 adapter
.adapter_registered(id
, self.tx());
41 pub fn add_stream_adapter
<S
: StreamAdapter
+ '
static>(&self, adapter
: S
) -> usize {
42 let adapter
= Arc
::new(adapter
);
43 let mut adapters
= self.adapters
.write().unwrap
();
44 let id
= adapters
.next
;
46 adapters
.stream
.insert
(id
, adapter
.clone());
48 adapter
.adapter_registered(id
, self.tx());
52 pub fn remove_packet_adapter(&self, id
: usize) {
53 if let Some(a
) = self.adapters
.write().unwrap
().packet
.remove(&id
) {
58 pub fn remove_stream_adapter(&self, id
: usize) {
59 if let Some(a
) = self.adapters
.write().unwrap
().stream
.remove(&id
) {
64 /// Create a handle that can be used to transmit data on the TNC
65 pub fn tx(&self) -> TxHandle
{
67 event_tx
: self.event_tx
.clone(),
72 let _
= self.event_tx
.send(TncControlEvent
::Start
);
76 // TODO: blocking function to indicate TNC has finished closing
77 // then we could call this in a signal handler to ensure PTT is dropped before quit
78 let _
= self.event_tx
.send(TncControlEvent
::Close
);
83 event_tx
: mpsc
::SyncSender
<TncControlEvent
>,
87 pub fn transmit_packet(
89 link_setup
: &LinkSetup
,
90 packet_type
: &PacketType
,
93 let (pack_type
, pack_type_len
) = packet_type
.as_proto();
94 if pack_type_len
+ payload
.len() > 823 {
95 // TODO: error for invalid transmission type
98 let mut full_payload
= vec
![];
99 full_payload
.extend_from_slice(&pack_type
[0..pack_type_len
]);
100 full_payload
.extend_from_slice(&payload
);
101 let crc
= m17core
::crc
::m17_crc(&full_payload
);
102 full_payload
.extend_from_slice(&crc
.to_be_bytes());
103 let kiss_frame
= KissFrame
::new_full_packet(&link_setup
.raw
.0, &full_payload
).unwrap
();
104 let _
= self.event_tx
.send(TncControlEvent
::Kiss(kiss_frame
));
107 pub fn transmit_stream_start(&self, link_setup
: &LinkSetup
) {
108 let kiss_frame
= KissFrame
::new_stream_setup(&link_setup
.raw
.0).unwrap
();
109 let _
= self.event_tx
.send(TncControlEvent
::Kiss(kiss_frame
));
112 // as long as there is only one TNC it is implied there is only ever one stream transmission in flight
114 pub fn transmit_stream_next(&self, stream
: &StreamFrame
) {
115 let kiss_frame
= KissFrame
::new_stream_data(&stream
).unwrap
();
116 let _
= self.event_tx
.send(TncControlEvent
::Kiss(kiss_frame
));
120 /// Synchronised structure for listeners subscribing to packets and streams.
122 /// Each listener will be notified in turn of each event.
124 /// Identifier to be assigned to the next listener, starting from 0
126 packet
: HashMap
<usize, Arc
<dyn PacketAdapter
>>,
127 stream
: HashMap
<usize, Arc
<dyn StreamAdapter
>>,
134 packet
: HashMap
::new(),
135 stream
: HashMap
::new(),
140 /// Carries a request from a method on M17App to the TNC's writer thread, which will execute it.
141 enum TncControlEvent
{
147 fn spawn_reader
<T
: Tnc
>(mut tnc
: T
, adapters
: Arc
<RwLock
<Adapters
>>) {
148 std
::thread
::spawn(move || {
149 let mut kiss_buffer
= KissBuffer
::new();
150 let mut stream_running
= false;
152 let mut buf
= kiss_buffer
.buf_remaining();
153 let n
= match tnc
.read(&mut buf
) {
157 kiss_buffer
.did_write(n
);
158 while let Some(frame
) = kiss_buffer
.next_frame() {
159 if frame
.command() != Ok(KissCommand
::DataFrame
) {
163 Ok(m17core
::kiss
::PORT_PACKET_BASIC
) => {
165 // we will handle the more full-featured version from from port 1
167 Ok(m17core
::kiss
::PORT_PACKET_FULL
) => {
168 let mut payload
= [0u8; 855]; // 30 byte LSF + 825 byte packet including CRC
169 let Ok(n
) = frame
.decode_payload(&mut payload
) else {
170 debug
!("failed to decode payload from KISS frame");
174 debug
!("unusually short full packet frame");
177 let lsf
= LsfFrame(payload
[0..30].try_into().unwrap
());
178 if lsf
.check_crc() != 0 {
179 debug
!("LSF in full packet frame did not pass CRC");
182 if lsf
.encryption_type() != EncryptionType
::None
{
183 debug
!("we only understand None encryption for now - skipping packet");
186 let Some((packet_type
, type_len
)) = PacketType
::from_proto(&payload
[30..n
])
188 debug
!("failed to decode packet type");
191 if (n
- 30 - type_len
) < 2 {
192 debug
!("packet payload too small to provide CRC");
195 let packet_crc
= m17core
::crc
::m17_crc(&payload
[30..n
]);
197 debug
!("packet CRC does not pass");
200 let packet_payload
: Arc
<[u8]> =
201 Arc
::from(&payload
[(30 + type_len
)..(n
- 2)]);
204 adapters
.read().unwrap
().packet
.values().cloned().collect();
209 packet_payload
.clone(),
213 Ok(m17core
::kiss
::PORT_STREAM
) => {
214 let mut payload
= [0u8; 32];
215 let Ok(n
) = frame
.decode_payload(&mut payload
) else {
216 debug
!("failed to decode stream payload from KISS frame");
220 let lsf
= LsfFrame(payload
[0..30].try_into().unwrap
());
221 if lsf
.check_crc() != 0 {
222 debug
!("initial LSF in stream did not pass CRC");
225 stream_running
= true;
227 adapters
.read().unwrap
().stream
.values().cloned().collect();
229 s
.stream_began(lsf
.clone());
233 debug
!("ignoring stream data as we didn't get a valid LSF first");
236 // TODO: parse LICH and handle the different changing subvalues META could have
237 if m17core
::crc
::m17_crc(&payload
[6..n
]) != 0 {
238 debug
!("stream data CRC mismatch");
241 let mut frame_number
= u16::from_be_bytes([payload
[6], payload
[7]]);
242 let is_final
= (frame_number
& 0x8000) > 0;
243 frame_number
&= 0x7fff;
244 let data
: [u8; 16] = payload
[8..24].try_into().unwrap
();
245 let data
= Arc
::new(data
);
247 stream_running
= false;
250 adapters
.read().unwrap
().stream
.values().cloned().collect();
252 s
.stream_data(frame_number
, is_final
, data
.clone());
263 fn spawn_writer
<T
: Tnc
>(mut tnc
: T
, event_rx
: mpsc
::Receiver
<TncControlEvent
>) {
264 std
::thread
::spawn(move || {
265 while let Ok(ev
) = event_rx
.recv() {
267 TncControlEvent
::Kiss(k
) => {
268 if let Err(e
) = tnc
.write_all(&k
.as_bytes()) {
269 debug
!("kiss send err: {:?}", e
);
273 TncControlEvent
::Start
=> {
274 if let Err(e
) = tnc
.start() {
275 debug
!("tnc start err: {:?}", e
);
279 TncControlEvent
::Close
=> {
280 if let Err(e
) = tnc
.close() {
281 debug
!("tnc close err: {:?}", e
);