use crate::{Error, Message, MessageType, MESSAGE_SIZE, PROTOCOL_VERSION};
pub const PACK_MAX_MESSAGES: usize = 9;
pub const PACK_MAX_SIZE: usize = 3 + PACK_MAX_MESSAGES * MESSAGE_SIZE;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MessagePack<'a> {
protocol_version: u8,
body: &'a [u8],
}
impl<'a> MessagePack<'a> {
pub fn parse(data: &'a [u8]) -> Result<Self, Error> {
if data.len() < 3 {
return Err(Error::TooShort {
need: 3,
got: data.len(),
});
}
if data[0] >> 4 != u8::from(MessageType::MessagePack) {
return Err(Error::WrongMessageType {
expected: 0xF,
found: data[0] >> 4,
});
}
if data[1] as usize != MESSAGE_SIZE {
return Err(Error::InvalidPack("single message size is not 25"));
}
let count = data[2] as usize;
if count == 0 {
return Err(Error::InvalidPack("pack holds no messages"));
}
if count > PACK_MAX_MESSAGES {
return Err(Error::InvalidPack("pack holds more than 9 messages"));
}
let need = 3 + count * MESSAGE_SIZE;
if data.len() < need {
return Err(Error::TooShort {
need,
got: data.len(),
});
}
let body = &data[3..need];
let mut seen = [0u8; 6];
for chunk in body.chunks_exact(MESSAGE_SIZE) {
let t = chunk[0] >> 4;
if t > 5 {
return Err(Error::InvalidPack(
"pack contains a pack or an unknown message type",
));
}
seen[t as usize] += 1;
}
if seen[1] > 1 || seen[3] > 1 || seen[4] > 1 || seen[5] > 1 {
return Err(Error::InvalidPack(
"more than one Location, Self ID, System or Operator ID message",
));
}
if seen[0] as usize > crate::uas::BASIC_ID_SLOTS {
return Err(Error::InvalidPack(
"more Basic ID messages than the standard allows",
));
}
Ok(MessagePack {
protocol_version: data[0] & 0x0F,
body,
})
}
pub fn protocol_version(&self) -> u8 {
self.protocol_version
}
pub fn len(&self) -> usize {
self.body.len() / MESSAGE_SIZE
}
pub fn is_empty(&self) -> bool {
self.body.is_empty()
}
pub fn encoded_len(&self) -> usize {
3 + self.body.len()
}
pub fn raw_messages(&self) -> impl Iterator<Item = &'a [u8; MESSAGE_SIZE]> + 'a {
self.body
.chunks_exact(MESSAGE_SIZE)
.map(|c| c.try_into().expect("chunk is 25 bytes"))
}
pub fn messages(&self) -> impl Iterator<Item = Result<Message, Error>> + 'a {
self.raw_messages().map(|raw| Message::decode(raw))
}
}
#[derive(Debug, Clone)]
pub struct MessagePackBuilder {
buf: [u8; PACK_MAX_SIZE],
count: usize,
}
impl Default for MessagePackBuilder {
fn default() -> Self {
Self::new()
}
}
impl MessagePackBuilder {
pub fn new() -> Self {
let mut buf = [0u8; PACK_MAX_SIZE];
buf[0] = 0xF0 | PROTOCOL_VERSION;
buf[1] = MESSAGE_SIZE as u8;
MessagePackBuilder { buf, count: 0 }
}
pub fn push(&mut self, msg: &Message) -> Result<(), Error> {
self.push_raw(&msg.encode()?)
}
pub fn push_raw(&mut self, raw: &[u8; MESSAGE_SIZE]) -> Result<(), Error> {
if self.count == PACK_MAX_MESSAGES {
return Err(Error::InvalidPack("pack is full"));
}
let at = 3 + self.count * MESSAGE_SIZE;
self.buf[at..at + MESSAGE_SIZE].copy_from_slice(raw);
self.count += 1;
self.buf[2] = self.count as u8;
Ok(())
}
pub fn len(&self) -> usize {
self.count
}
pub fn is_empty(&self) -> bool {
self.count == 0
}
pub fn as_bytes(&self) -> &[u8] {
&self.buf[..3 + self.count * MESSAGE_SIZE]
}
}