use packet id

This commit is contained in:
John Goerzen 2020-09-21 00:00:08 -05:00
parent e80f891340
commit 87549ba45b
4 changed files with 113 additions and 52 deletions

View File

@ -100,11 +100,13 @@ fn main() {
}
Command::Pipe { dest } => {
let dest_u64: u64 = u64::from_str_radix(&dest, 16).expect("Invalid destination");
let maxpacketsize = xb.maxpacketsize;
thread::spawn(move || {
pipe::stdout_processor(&mut xbreframer, &mut xb.ser_reader)
.expect("Failure in stdout_processor")
});
pipe::stdin_processor(dest_u64, 1600, xbeesender).expect("Failure in stdin_processor");
pipe::stdin_processor(dest_u64, maxpacketsize - 1, xbeesender)
.expect("Failure in stdin_processor");
// Make sure queued up data is sent
let _ = writerthread.join();
}

View File

@ -165,6 +165,7 @@ fn writerthread(
maxpacketsize: usize,
writerrx: crossbeam_channel::Receiver<XBTX>,
) {
let mut packetstream = PacketStream::new();
for item in writerrx.iter() {
match item {
XBTX::Shutdown => return,
@ -172,12 +173,17 @@ fn writerthread(
// Here we receive a block of data, which hasn't been
// packetized. Packetize it and send out the result.
match packetize_data(maxpacketsize, &dest, &data) {
match packetstream.packetize_data(maxpacketsize, &dest, &data) {
Ok(packets) => {
for packet in packets.into_iter() {
match packet.serialize() {
Ok(datatowrite) => {
trace!("TX to {:?} data {}", &dest, hex::encode(&datatowrite));
trace!(
"TX ID {} to {:?} data {}",
packet.frame_id,
&dest,
hex::encode(&datatowrite)
);
ser.swrite.write_all(&datatowrite).unwrap();
ser.swrite.flush().unwrap();
}

View File

@ -156,40 +156,63 @@ pub fn mac48to64(mac48: &[u8; 6], pattern64: u64) -> u64 {
mac64
}
/** Convert the given data into zero or more packets for transmission.
pub struct PacketStream {
/// The counter for the frame
framecounter: u8,
}
We create a leading byte that indicates how many more XBee packets are remaining
for the block. When zero, the receiver should process the accumulated data. */
pub fn packetize_data(
maxpacketsize: usize,
dest: &XBDestAddr,
data: &[u8],
) -> Result<Vec<XBTXRequest>, String> {
let mut retval = Vec::new();
if data.is_empty() {
return Ok(retval);
impl PacketStream {
pub fn new() -> Self {
PacketStream { framecounter: 1 }
}
let chunks: Vec<&[u8]> = data.chunks(maxpacketsize - 1).collect();
let mut chunks_remaining: u8 = u8::try_from(chunks.len() - 1)
.map_err(|e| String::from("More than 255 chunks to transmit"))?;
for chunk in chunks {
let mut payload = BytesMut::new();
payload.put_u8(chunks_remaining);
payload.put_slice(chunk);
let packet = XBTXRequest {
frame_id: 0,
dest_addr: dest.clone(),
broadcast_radius: 0,
transmit_options: 0,
payload: Bytes::from(payload),
};
retval.push(packet);
chunks_remaining -= 1;
pub fn get_and_incr_framecounter(&mut self) -> u8 {
let retval = self.framecounter;
if self.framecounter == std::u8::MAX {
self.framecounter = 1
} else {
self.framecounter += 1
}
retval
}
Ok(retval)
/** Convert the given data into zero or more packets for transmission.
We create a leading byte that indicates how many more XBee packets are remaining
for the block. When zero, the receiver should process the accumulated data. */
pub fn packetize_data(
&mut self,
maxpacketsize: usize,
dest: &XBDestAddr,
data: &[u8],
) -> Result<Vec<XBTXRequest>, String> {
let mut retval = Vec::new();
if data.is_empty() {
return Ok(retval);
}
let chunks: Vec<&[u8]> = data.chunks(maxpacketsize - 1).collect();
let mut chunks_remaining: u8 = u8::try_from(chunks.len() - 1)
.map_err(|e| String::from("More than 255 chunks to transmit"))?;
for chunk in chunks {
let mut payload = BytesMut::new();
payload.put_u8(chunks_remaining);
payload.put_slice(chunk);
let frame_id = self.get_and_incr_framecounter();
let packet = XBTXRequest {
frame_id,
dest_addr: dest.clone(),
broadcast_radius: 0,
transmit_options: 0,
payload: Bytes::from(payload),
};
retval.push(packet);
chunks_remaining -= 1;
}
Ok(retval)
}
}
//////////////////////////////////////////////////////////////////////
@ -204,3 +227,13 @@ pub struct RXPacket {
pub rx_options: u8,
pub payload: Bytes,
}
/** A Digi extended transmit status frame, 0x8B */
#[derive(PartialEq, Eq, Debug)]
pub struct ExtTxStatus {
pub frame_id: u8,
pub dest_addr_16: u16,
pub tx_retry_count: u8,
pub delivery_status: u8,
pub discovery_status: u8,
}

View File

@ -75,27 +75,47 @@ pub fn rxxbpacket(ser: &mut XBSerReader) -> Option<RXPacket> {
let mut inner = Bytes::from(inner);
let frametype = inner.get_u8();
if frametype != 0x90 {
debug!("SERIN: Non-0x90 frame; data: {}", hex::encode(inner));
return None;
match frametype {
0x8B => {
// Delivery status update. Log and ignore.
let frame_id = inner.get_u8();
let dest_addr_16 = inner.get_u16();
let tx_retry_count = inner.get_u8();
let delivery_status = inner.get_u8();
let discovery_status = inner.get_u8();
let txstatus = ExtTxStatus {
frame_id,
dest_addr_16,
tx_retry_count,
delivery_status,
discovery_status,
};
trace!("TX STATUS: {:?}", txstatus);
None
}
0x90 => {
let sender_addr64 = inner.get_u64();
let sender_addr16 = inner.get_u16();
let rx_options = inner.get_u8();
let payload = inner.to_bytes();
trace!(
"SERIN: packet from {} / {}, payload {}",
hex::encode(sender_addr64.to_be_bytes()),
hex::encode(sender_addr16.to_be_bytes()),
hex::encode(&payload)
);
Some(RXPacket {
sender_addr64,
sender_addr16,
rx_options,
payload,
})
}
_ => {
debug!("SERIN: Non-0x90 frame; data: {}", hex::encode(inner));
None
}
}
let sender_addr64 = inner.get_u64();
let sender_addr16 = inner.get_u16();
let rx_options = inner.get_u8();
let payload = inner.to_bytes();
trace!(
"SERIN: packet from {} / {}, payload {}",
hex::encode(sender_addr64.to_be_bytes()),
hex::encode(sender_addr16.to_be_bytes()),
hex::encode(&payload)
);
Some(RXPacket {
sender_addr64,
sender_addr16,
rx_options,
payload,
})
}
/// Like rxxbpacket, but wait until we have a valid packet.