Skip to main content

parse

Function parse 

Source
pub fn parse(buffer: &[u8]) -> Result<(Packet<'_>, usize), RxError>
Expand description

Reads the one packet that starts at the front of these bytes.

§Arguments

  • buffer - the bytes read out of the receive buffer.

§Returns

The packet, and how many bytes of the buffer it took.

§Errors

Returns RxError::NotAPacket when the sync word is missing, and RxError::Truncated when the buffer ends before the packet does.

§Examples

use pamoja_radios::sx1302::rx::{parse, Modem};

// Head metadata, payload, then the tail the chip writes after it.
let mut buffer = vec![0xa5, 0xc0, 4, 2, 0b0111_0001, 3, 0, 0, 0];
buffer.extend_from_slice(b"21.5");
buffer.extend_from_slice(&[0u8; 14]);

let (packet, took) = parse(&buffer).expect("it begins with the sync word");
assert_eq!(packet.payload, b"21.5");
assert_eq!(packet.channel, 2);
assert_eq!(packet.modem, Modem::LoraMulti(3));
assert_eq!(packet.datarate, 7);
assert!(packet.crc_enabled);
assert_eq!(took, buffer.len());