]>
code.octet-stream.net Git - m17rt/blob - m17app/src/app.rs
1 use crate::adapter
::{PacketAdapter
, StreamAdapter
};
3 use m17core
::kiss
::{KissBuffer
, KissCommand
, KissFrame
};
4 use m17core
::protocol
::{EncryptionType
, LsfFrame
, PacketType
, StreamFrame
};
7 use std
::collections
::HashMap
;
9 use std
::sync
::{Arc
, RwLock
};
12 adapters
: Arc
<RwLock
<Adapters
>>,
13 event_tx
: mpsc
::SyncSender
<TncControlEvent
>,
17 pub fn new
<T
: Tnc
+ Send
+ '
static>(mut tnc
: T
) -> Self {
18 let write_tnc
= tnc
.try_clone().unwrap
();
19 let (event_tx
, event_rx
) = mpsc
::sync_channel(128);
20 let listeners
= Arc
::new(RwLock
::new(Adapters
::new()));
21 spawn_reader(tnc
, listeners
.clone());
22 spawn_writer(write_tnc
, event_rx
);
29 pub fn add_packet_adapter
<P
: PacketAdapter
+ '
static>(&self, adapter
: P
) -> usize {
30 let adapter
= Arc
::new(adapter
);
31 let mut adapters
= self.adapters
.write().unwrap
();
32 let id
= adapters
.next
;
34 adapters
.packet
.insert
(id
, adapter
.clone());
36 adapter
.adapter_registered(id
, self.tx());
40 pub fn add_stream_adapter
<S
: StreamAdapter
+ '
static>(&self, adapter
: S
) -> usize {
41 let adapter
= Arc
::new(adapter
);
42 let mut adapters
= self.adapters
.write().unwrap
();
43 let id
= adapters
.next
;
45 adapters
.stream
.insert
(id
, adapter
.clone());
47 adapter
.adapter_registered(id
, self.tx());
51 pub fn remove_packet_adapter(&self, id
: usize) {
52 if let Some(a
) = self.adapters
.write().unwrap
().packet
.remove(&id
) {
57 pub fn remove_stream_adapter(&self, id
: usize) {
58 if let Some(a
) = self.adapters
.write().unwrap
().stream
.remove(&id
) {
63 /// Create a handle that can be used to transmit data on the TNC
64 pub fn tx(&self) -> TxHandle
{
66 event_tx
: self.event_tx
.clone(),
71 let _
= self.event_tx
.send(TncControlEvent
::Start
);
75 let _
= self.event_tx
.send(TncControlEvent
::Close
);
80 event_tx
: mpsc
::SyncSender
<TncControlEvent
>,
84 pub fn transmit_packet(&self, packet_type
: PacketType
, payload
: &[u8]) {
85 // hang on where do we get the LSF details from? We need a destination obviously
86 // our source address needs to be configured here too
87 // also there is possible CAN, encryption, meta payload
89 // we will immediately convert this into a KISS payload before sending into channel so we only need borrow on data
92 // add more methods here for stream outgoing
94 pub fn transmit_stream_start(&self, lsf
: LsfFrame
) {
95 // TODO: is asking for an LsfFrame a good idea or unfriendly API?
96 // What I should do here is create a LinkSetup struct which wraps an LsfFrame and can be loaded with a raw one
97 let kiss_frame
= KissFrame
::new_stream_setup(&lsf
.0).unwrap
();
98 let _
= self.event_tx
.send(TncControlEvent
::Kiss(kiss_frame
));
101 // as long as there is only one TNC it is implied there is only ever one stream transmission in flight
103 pub fn transmit_stream_next(&self, stream
: StreamFrame
) {
104 let kiss_frame
= KissFrame
::new_stream_data(&stream
).unwrap
();
105 let _
= self.event_tx
.send(TncControlEvent
::Kiss(kiss_frame
));
109 /// Synchronised structure for listeners subscribing to packets and streams.
111 /// Each listener will be notified in turn of each event.
113 /// Identifier to be assigned to the next listener, starting from 0
115 packet
: HashMap
<usize, Arc
<dyn PacketAdapter
>>,
116 stream
: HashMap
<usize, Arc
<dyn StreamAdapter
>>,
123 packet
: HashMap
::new(),
124 stream
: HashMap
::new(),
129 /// Carries a request from a method on M17App to the TNC's writer thread, which will execute it.
130 enum TncControlEvent
{
136 fn spawn_reader
<T
: Tnc
>(mut tnc
: T
, adapters
: Arc
<RwLock
<Adapters
>>) {
137 std
::thread
::spawn(move || {
138 let mut kiss_buffer
= KissBuffer
::new();
139 let mut stream_running
= false;
141 let mut buf
= kiss_buffer
.buf_remaining();
142 let n
= match tnc
.read(&mut buf
) {
146 kiss_buffer
.did_write(n
);
147 while let Some(frame
) = kiss_buffer
.next_frame() {
148 if frame
.command() != Ok(KissCommand
::DataFrame
) {
152 Ok(m17core
::kiss
::PORT_PACKET_BASIC
) => {
154 // we will handle the more full-featured version from from port 1
156 Ok(m17core
::kiss
::PORT_PACKET_FULL
) => {
157 let mut payload
= [0u8; 855]; // 30 byte LSF + 825 byte packet including CRC
158 let Ok(n
) = frame
.decode_payload(&mut payload
) else {
159 debug
!("failed to decode payload from KISS frame");
163 debug
!("unusually short full packet frame");
166 let lsf
= LsfFrame(payload
[0..30].try_into().unwrap
());
167 if lsf
.check_crc() != 0 {
168 debug
!("LSF in full packet frame did not pass CRC");
171 if lsf
.encryption_type() != EncryptionType
::None
{
172 debug
!("we only understand None encryption for now - skipping packet");
175 let Some((packet_type
, type_len
)) = PacketType
::from_proto(&payload
[30..n
])
177 debug
!("failed to decode packet type");
180 if (n
- 30 - type_len
) < 2 {
181 debug
!("packet payload too small to provide CRC");
184 let packet_crc
= m17core
::crc
::m17_crc(&payload
[30..n
]);
186 debug
!("packet CRC does not pass");
189 let packet_payload
: Arc
<[u8]> =
190 Arc
::from(&payload
[(30 + type_len
)..(n
- 2)]);
193 adapters
.read().unwrap
().packet
.values().cloned().collect();
198 packet_payload
.clone(),
202 Ok(m17core
::kiss
::PORT_STREAM
) => {
203 let mut payload
= [0u8; 32];
204 let Ok(n
) = frame
.decode_payload(&mut payload
) else {
205 debug
!("failed to decode stream payload from KISS frame");
209 let lsf
= LsfFrame(payload
[0..30].try_into().unwrap
());
210 if lsf
.check_crc() != 0 {
211 debug
!("initial LSF in stream did not pass CRC");
214 stream_running
= true;
216 adapters
.read().unwrap
().stream
.values().cloned().collect();
218 s
.stream_began(lsf
.clone());
222 debug
!("ignoring stream data as we didn't get a valid LSF first");
225 // TODO: parse LICH and handle the different changing subvalues META could have
226 if m17core
::crc
::m17_crc(&payload
[6..n
]) != 0 {
227 debug
!("stream data CRC mismatch");
230 let mut frame_number
= u16::from_be_bytes([payload
[6], payload
[7]]);
231 let is_final
= (frame_number
& 0x8000) > 0;
232 frame_number
&= 0x7fff;
233 let data
: [u8; 16] = payload
[8..24].try_into().unwrap
();
234 let data
= Arc
::new(data
);
236 stream_running
= false;
239 adapters
.read().unwrap
().stream
.values().cloned().collect();
241 s
.stream_data(frame_number
, is_final
, data
.clone());
252 fn spawn_writer
<T
: Tnc
>(mut tnc
: T
, event_rx
: mpsc
::Receiver
<TncControlEvent
>) {
253 std
::thread
::spawn(move || {
254 while let Ok(ev
) = event_rx
.recv() {
256 TncControlEvent
::Kiss(k
) => {
257 if let Err(e
) = tnc
.write_all(&k
.as_bytes()) {
258 debug
!("kiss send err: {:?}", e
);
262 TncControlEvent
::Start
=> {
263 if let Err(e
) = tnc
.start() {
264 debug
!("tnc start err: {:?}", e
);
268 TncControlEvent
::Close
=> {
269 if let Err(e
) = tnc
.close() {
270 debug
!("tnc close err: {:?}", e
);