use crate::error::LorawanError;
pub const MAX_FRAME: usize = 256;
pub const MAX_PAYLOAD: usize = MAX_FRAME - 13;
pub(crate) const MTYPE_JOIN_REQUEST: u8 = 0x00;
pub(crate) const MTYPE_JOIN_ACCEPT: u8 = 0x20;
pub(crate) const MTYPE_UNCONFIRMED_UP: u8 = 0x40;
pub(crate) const MTYPE_UNCONFIRMED_DOWN: u8 = 0x60;
pub(crate) const MTYPE_CONFIRMED_UP: u8 = 0x80;
pub(crate) const MTYPE_CONFIRMED_DOWN: u8 = 0xA0;
pub(crate) const MTYPE_MASK: u8 = 0xE0;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Direction {
Uplink,
Downlink,
}
impl Direction {
pub(crate) fn bit(self) -> u8 {
match self {
Direction::Uplink => 0,
Direction::Downlink => 1,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct PhyPayload {
bytes: [u8; MAX_FRAME],
len: usize,
}
impl PhyPayload {
pub(crate) fn new(bytes: &[u8]) -> Result<Self, LorawanError> {
if bytes.len() > MAX_FRAME {
return Err(LorawanError::PayloadTooLong);
}
let mut buf = [0u8; MAX_FRAME];
buf[..bytes.len()].copy_from_slice(bytes);
Ok(PhyPayload {
bytes: buf,
len: bytes.len(),
})
}
pub fn as_bytes(&self) -> &[u8] {
&self.bytes[..self.len]
}
}