use crate::address::{Address, Callsign};
-use crate::kiss::{KissBuffer, KissFrame, PORT_PACKET_BASIC, PORT_PACKET_FULL, PORT_STREAM};
+use crate::kiss::{
+ KissBuffer, KissCommand, KissFrame, PORT_PACKET_BASIC, PORT_PACKET_FULL, PORT_STREAM,
+};
use crate::modem::ModulatorFrame;
use crate::protocol::{
Frame, LichCollection, LsfFrame, Mode, PacketFrame, PacketFrameCounter, StreamFrame,
/// Should PTT be on right now? Polled by external
ptt: bool,
+
+ /// TxDelay raw value, number of 10ms units. We will optimistically start with default 0.
+ tx_delay: u8,
+
+ /// This is a full duplex channel so we do not need to monitor DCD or use CSMA. Default false.
+ full_duplex: bool,
}
impl SoftTnc {
stream_curr: 0,
stream_full: false,
ptt: false,
+ tx_delay: 0,
+ full_duplex: false,
}
}
Mode::Stream => {
let kiss = KissFrame::new_stream_setup(&lsf.0).unwrap();
self.kiss_to_host(kiss);
- self.state = State::RxStream(RxStreamState { lsf, index: 0 });
+ self.state = State::RxStream(RxStreamState {
+ _lsf: lsf,
+ index: 0,
+ });
}
}
}
let start = 25 * rx.count;
let end = start + payload_len;
rx.packet[start..(start + payload_len)]
- .copy_from_slice(&packet.payload);
+ .copy_from_slice(&packet.payload[0..payload_len]);
// TODO: compatible packets should be sent on port 0 too
let kiss =
KissFrame::new_full_packet(&rx.lsf.0, &rx.packet[0..end])
// TODO: avoid discarding the first data payload here
// need a queue depth of 2 for outgoing kiss
self.state = State::RxStream(RxStreamState {
- lsf,
+ _lsf: lsf,
index: stream.frame_number + 1,
});
}
}
// We have something we might send if the channel is free
- match self.next_csma_check {
- None => {
- if self.dcd {
- self.next_csma_check = Some(self.now + 1920);
- return None;
- } else {
- // channel is idle at the moment we get a frame to send
- // go right ahead
- }
- }
- Some(at_time) => {
- if self.now < at_time {
- return None;
+
+ // TODO: Proper full duplex support
+ // A true full duplex TNC should be able to rx and tx concurrently, implying
+ // separate states.
+ if !self.full_duplex {
+ match self.next_csma_check {
+ None => {
+ if self.dcd {
+ self.next_csma_check = Some(self.now + 1920);
+ return None;
+ } else {
+ // channel is idle at the moment we get a frame to send
+ // go right ahead
+ }
}
- // 25% chance that we'll transmit this slot.
- // Using self.now as random is probably fine so long as it's not being set in
- // a lumpy manner. m17app's soundmodem should be fine.
- // TODO: bring in prng to help in cases where `now` never ends in 0b11
- let p1_4 = (self.now & 3) == 3;
- if !self.dcd || !p1_4 {
- self.next_csma_check = Some(self.now + 1920);
- return None;
- } else {
- self.next_csma_check = None;
+ Some(at_time) => {
+ if self.now < at_time {
+ return None;
+ }
+ // 25% chance that we'll transmit this slot.
+ // Using self.now as random is probably fine so long as it's not being set in
+ // a lumpy manner. m17app's soundmodem should be fine.
+ // TODO: bring in prng to help in cases where `now` never ends in 0b11
+ let p1_4 = (self.now & 3) == 3;
+ if !self.dcd || !p1_4 {
+ self.next_csma_check = Some(self.now + 1920);
+ return None;
+ } else {
+ self.next_csma_check = None;
+ }
}
}
}
self.state = State::TxPacket;
}
self.ptt = true;
- // TODO: true txdelay
- Some(ModulatorFrame::Preamble { tx_delay: 0 })
+ Some(ModulatorFrame::Preamble {
+ tx_delay: self.tx_delay,
+ })
}
State::TxStream => {
if !self.stream_full && self.stream_next == self.stream_curr {
let Ok(port) = kiss_frame.port() else {
continue;
};
+ let Ok(command) = kiss_frame.command() else {
+ continue;
+ };
+ if port != PORT_PACKET_BASIC && port != PORT_PACKET_FULL && port != PORT_STREAM {
+ continue;
+ }
+ if command == KissCommand::TxDelay {
+ let mut new_delay = [0u8; 1];
+ if kiss_frame.decode_payload(&mut new_delay) == Ok(1) {
+ self.tx_delay = new_delay[0];
+ }
+ continue;
+ }
+ if command == KissCommand::FullDuplex {
+ let mut new_duplex = [0u8; 1];
+ if kiss_frame.decode_payload(&mut new_duplex) == Ok(1) {
+ self.full_duplex = new_duplex[0] != 0;
+ }
+ continue;
+ }
+ if command != KissCommand::DataFrame {
+ // Not supporting any other settings yet
+ // TODO: allow adjusting P persistence parameter for CSMA
+ continue;
+ }
if port == PORT_PACKET_BASIC {
if self.packet_full {
continue;
}
pending.lsf = Some(lsf);
let app_data_len = len - 30;
- pending.app_data[0..app_data_len].copy_from_slice(&payload[30..]);
+ pending.app_data[0..app_data_len].copy_from_slice(&payload[30..len]);
pending.app_data_len = app_data_len;
self.packet_queue[self.packet_next] = pending;
self.packet_next = (self.packet_next + 1) % 4;
struct RxStreamState {
/// Track identifying information for this transmission so we can tell if it changes.
- lsf: LsfFrame,
+ _lsf: LsfFrame,
/// Expected next frame number. Allowed to skip values on RX, but not go backwards.
index: u16,
)
};
let mut payload = [0u8; 25];
- payload.copy_from_slice(
+ payload[0..data_len].copy_from_slice(
&self.app_data[self.app_data_transmitted..(self.app_data_transmitted + data_len)],
);
self.app_data_transmitted += data_len;