#![deny(unsafe_code)]
use puns::{
Error, Pun, from_head,
num::{u8c, u16be, u16bec, u16le, u16lec, u32le, u64le},
try_pun_ref,
};
#[derive(Pun)]
#[repr(C)]
#[pun(assert(frame_control & 0x000f == 0x0008))]
#[pun(assert(crc32(0, size_of_val(self) - 4) == le(size_of_val(self) - 4, 4)))]
#[pun(get(
subtype: u8 = (frame_control >> 4) & 0xf,
to_ds: bool = frame_control & 0x0100 != 0,
from_ds: bool = frame_control & 0x0200 != 0,
retry: bool = frame_control & 0x0800 != 0,
protected: bool = frame_control & 0x4000 != 0,
qos: bool = frame_control & 0x0080 != 0,
sequence: u16 = sequence_control >> 4,
fragment: u8 = sequence_control & 0xf,
))]
pub struct DataFrame {
pub frame_control: u16le,
pub duration: u16le,
pub address1: [u8; 6],
pub address2: [u8; 6],
pub address3: [u8; 6],
pub sequence_control: u16le,
#[pun(regions(
// A fourth address when both "to DS" and "from DS" are set.
address4 = if frame_control & 0x0300 == 0x0300 { 6 } else { 0 },
// The QoS subtypes: bit 3 of the subtype.
qos_control = if frame_control & 0x0080 != 0 { 2 } else { 0 },
// HT control: the order bit, in a QoS frame.
ht_control = if frame_control & 0x8080 == 0x8080 { 4 } else { 0 },
body = size_of_val(self) - 24 - 4
- (if frame_control & 0x0300 == 0x0300 { 6 } else { 0 })
- (if frame_control & 0x0080 != 0 { 2 } else { 0 })
- (if frame_control & 0x8080 == 0x8080 { 4 } else { 0 }),
fcs,
))]
pub data: [u8],
}
#[derive(Pun)]
#[repr(C)]
#[pun(assert(crc32(0, size_of_val(self) - 4) == le(size_of_val(self) - 4, 4)))]
#[pun(assert(walk(
36,
|at| at < size_of_val(self) - 4,
|at| at + 2 + be(at + 1, 1),
) == size_of_val(self) - 4))]
#[pun(get(ess: bool = capability & 1 != 0, privacy: bool = capability & 0x10 != 0))]
pub struct Beacon {
pub frame_control: u16lec<0x0080>,
pub duration: u16le,
pub destination: [u8; 6],
pub source: [u8; 6],
pub bssid: [u8; 6],
pub sequence_control: u16le,
pub timestamp: u64le,
pub interval: u16le,
pub capability: u16le,
#[pun(regions(elements = size_of_val(self) - 36 - 4, fcs))]
pub data: [u8],
}
#[derive(Pun)]
#[repr(C)]
#[pun(size = length + 2)]
pub struct Element {
pub id: u8,
pub length: u8,
pub data: [u8],
}
#[derive(Pun)]
#[repr(C)]
pub struct Ssid {
pub id: u8c<0>,
#[pun(valid(..=32))]
pub length: u8,
#[pun(len = length)]
pub name: [u8],
}
#[derive(Pun)]
#[repr(C)]
#[pun(assert(crc32(0, 10) == fcs))]
pub struct Ack {
pub frame_control: u16lec<0x00d4>,
pub duration: u16le,
pub receiver: [u8; 6],
pub fcs: u32le,
}
#[derive(Pun)]
#[repr(C)]
#[pun(size = length + 4)]
#[pun(get(ogf: u8 = opcode >> 10, ocf: u16 = opcode & 0x3ff))]
pub struct Command {
pub indicator: u8c<0x01>,
pub opcode: u16le,
pub length: u8,
pub parameters: [u8],
}
#[derive(Pun)]
#[repr(C)]
#[pun(size = length + 5)]
#[pun(get(handle: u16 = handle_flags & 0x0fff, boundary: u8 = (handle_flags >> 12) & 3))]
#[pun(assert((handle_flags >> 12) & 3 != 3 && handle_flags >> 14 != 3))]
pub struct Acl<P: ?Sized = [u8]> {
pub indicator: u8c<0x02>,
pub handle_flags: u16le,
pub length: u16le,
pub data: P,
}
#[derive(Pun)]
#[repr(C)]
#[pun(size = length + 3)]
pub struct Event {
pub indicator: u8c<0x04>,
pub code: u8,
pub length: u8,
pub parameters: [u8],
}
#[derive(Pun)]
#[repr(C)]
#[pun(size = length + 3)]
#[pun(assert(length >= 3))]
pub struct CommandComplete {
pub indicator: u8c<0x04>,
pub code: u8c<0x0e>,
pub length: u8,
pub packets: u8,
pub opcode: u16le,
pub parameters: [u8],
}
#[derive(Pun)]
#[repr(C)]
#[pun(size = length + 4)]
pub struct L2cap {
pub length: u16le,
#[pun(valid(1..))]
pub channel: u16le,
pub payload: [u8],
}
#[derive(Pun)]
pub enum Packet<'a> {
Complete(&'a CommandComplete),
Command(&'a Command),
Acl(&'a Acl<L2cap>),
Event(&'a Event),
}
#[derive(Pun, Debug, Clone, Copy)]
#[repr(C)]
#[pun(assert(can_id & 0x8000_0000 != 0 || can_id & 0x1fff_f800 == 0))]
#[pun(assert(len8_dlc == 0 || len == 8))]
#[pun(get(
extended: bool = can_id & 0x8000_0000 != 0,
remote: bool = can_id & 0x4000_0000 != 0,
error: bool = can_id & 0x2000_0000 != 0,
id: u32 = can_id & 0x1fff_ffff,
))]
pub struct CanFrame {
pub can_id: u32le,
#[pun(valid(0..=8))]
pub len: u8,
pub pad: u8,
pub res0: u8,
#[pun(valid(0, 9..=15))]
pub len8_dlc: u8,
pub data: [u8; 8],
}
#[derive(Pun, Debug, Clone, Copy)]
#[repr(C)]
#[pun(assert(can_id & 0x8000_0000 != 0 || can_id & 0x1fff_f800 == 0))]
#[pun(assert(can_id & 0x4000_0000 == 0))]
#[pun(get(bit_rate_switch: bool = flags & 1 != 0))]
pub struct CanFdFrame {
pub can_id: u32le,
#[pun(valid(0..=8, 12, 16, 20, 24, 32, 48, 64))]
pub len: u8,
#[pun(valid(0..=7))]
pub flags: u8,
pub res0: u8c<0>,
pub res1: u8c<0>,
pub data: [u8; 64],
}
#[derive(Pun)]
#[repr(C)]
#[pun(assert(size_of_val(self) >= 4 && size_of_val(self) <= 256))]
#[pun(assert(
crc::<16, 0x8005, 0xffff, true, 0>(0, size_of_val(self) - 2) == le(size_of_val(self) - 2, 2)
))]
#[pun(get(exception: bool = function & 0x80 != 0))]
pub struct RtuFrame {
#[pun(valid(0..=247))]
pub address: u8,
pub function: u8,
#[pun(regions(data = size_of_val(self) - 4, crc))]
pub rest: [u8],
}
#[derive(Pun)]
#[repr(C)]
#[pun(size = length + 6)]
#[pun(assert(length >= 2 && length <= 254))]
pub struct TcpAdu {
pub transaction: u16be,
pub protocol: u16bec<0>,
pub length: u16be,
pub unit: u8,
pub function: u8,
pub data: [u8],
}
#[derive(Pun)]
#[repr(C)]
#[pun(assert(count >= 1 && count <= 125))]
pub struct ReadHoldingRegisters {
pub start: u16be,
pub count: u16be,
}
#[derive(Pun)]
#[repr(C)]
#[pun(assert(byte_count % 2 == 0))]
pub struct RegistersResponse {
pub byte_count: u8,
#[pun(len = byte_count / 2)]
pub registers: [u16be],
}
#[derive(Pun)]
#[repr(transparent)]
#[pun(assert(size_of_val(self) >= 1 && be(size_of_val(self) - 1, 1) == 0xc0))]
#[pun(assert(walk(
0,
|at| at < size_of_val(self) - 1,
|at| if be(at, 1) == 0xc0 {
1 / 0
} else if be(at, 1) == 0xdb {
if be(at + 1, 1) == 0xdc || be(at + 1, 1) == 0xdd { at + 2 } else { 1 / 0 }
} else {
at + 1
},
) == size_of_val(self) - 1))]
pub struct Slip(pub [u8]);
#[derive(Pun)]
#[repr(transparent)]
#[pun(assert(size_of_val(self) >= 6 && be(0, 1) == 0x7e && be(size_of_val(self) - 1, 1) == 0x7e))]
#[pun(assert(be(1, 1) == 0xff && be(2, 2) == 0x7d23))]
#[pun(assert(walk(
1,
|at| at < size_of_val(self) - 1,
|at| if be(at, 1) == 0x7e {
1 / 0
} else if be(at, 1) == 0x7d {
if be(at + 1, 1) == 0x7e { 1 / 0 } else { at + 2 }
} else {
at + 1
},
) == size_of_val(self) - 1))]
pub struct Ppp(pub [u8]);
const QOS_DATA: [u8; 38] = [
0x88, 0x01, 0x2c, 0x00, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb,
0x00, 0x11, 0x22, 0x33, 0x44, 0x56, 0x30, 0x01, 0x05, 0x00, 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00,
0x08, 0x00, 0x8f, 0x7a, 0x3a, 0x83,
];
const MESH_DATA: [u8; 44] = [
0x88, 0x03, 0x2c, 0x00, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb,
0x00, 0x11, 0x22, 0x33, 0x44, 0x56, 0x30, 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x05, 0x00,
0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00, 0x08, 0x00, 0x9a, 0xf7, 0x70, 0x9a,
];
const BEACON: [u8; 50] = [
0x80, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x01,
0x02, 0x00, 0x00, 0x00, 0x00, 0x01, 0x10, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x64, 0x00, 0x11, 0x00, 0x00, 0x04, b't', b'e', b's', b't', 0x01, 0x02, 0x82, 0x84, 0xeb, 0xbe,
0xbe, 0xaf,
];
const ACK: [u8; 14] =
[0xd4, 0x00, 0x00, 0x00, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x71, 0xea, 0xf2, 0x4b];
const HCI: [u8; 23] = [
0x01, 0x03, 0x0c, 0x00, 0x04, 0x0e, 0x04, 0x01, 0x03, 0x0c, 0x00, 0x02, 0x40, 0x20, 0x07, 0x00,
0x03, 0x00, 0x04, 0x00, 0x0a, 0x03, 0x00,
];
const CAN: [u8; 16] = [0x23, 0x01, 0, 0, 2, 0, 0, 0, 0xde, 0xad, 0, 0, 0, 0, 0, 0];
const READ: [u8; 8] = [0x01, 0x03, 0x00, 0x00, 0x00, 0x0a, 0xc5, 0xcd];
const REGISTERS: [u8; 11] = [0x11, 0x03, 0x06, 0x02, 0x2b, 0x00, 0x00, 0x00, 0x64, 0xc8, 0xba];
const ADU: [u8; 13] =
[0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x11, 0x03, 0x00, 0x6b, 0x00, 0x03, 0xee];
const SLIP: [u8; 8] = [0x45, 0xdb, 0xdc, 0x01, 0xdb, 0xdd, 0x02, 0xc0];
const LCP: [u8; 17] = [
0x7e, 0xff, 0x7d, 0x23, 0xc0, 0x21, 0x7d, 0x21, 0x7d, 0x21, 0x7d, 0x20, 0x7d, 0x24, 0xd1, 0xb5,
0x7e,
];
pub fn main() {
let frame: &DataFrame = try_pun_ref(&QOS_DATA[..]).unwrap();
assert_eq!(
(frame.subtype(), frame.to_ds(), frame.from_ds(), frame.qos()),
(8, true, false, true)
);
assert_eq!(
(frame.sequence(), frame.fragment(), frame.retry(), frame.protected()),
(19, 0, false, false)
);
assert_eq!(
(frame.address4().len(), frame.qos_control(), frame.ht_control().len()),
(0, &[5, 0][..], 0)
);
assert_eq!((frame.body(), frame.fcs().len()), (&[0xaa, 0xaa, 0x03, 0, 0, 0, 0x08, 0x00][..], 4));
let mesh: &DataFrame = try_pun_ref(&MESH_DATA[..]).unwrap();
assert_eq!((mesh.address4(), mesh.body().len()), (&[2, 0, 0, 0, 0, 0x0c][..], 8));
for at in [0, 1, 10, 30, 43] {
let mut bytes = MESH_DATA;
bytes[at] ^= 0x04;
assert_eq!(try_pun_ref::<_, DataFrame>(&bytes[..]).err(), Some(Error::Validity));
}
let beacon: &Beacon = try_pun_ref(&BEACON[..]).unwrap();
assert_eq!((beacon.interval.get(), beacon.ess(), beacon.privacy()), (100, true, true));
let (element, rest) = from_head::<Element>(beacon.elements()).unwrap();
let ssid: &Ssid = try_pun_ref(&beacon.elements()[..6]).unwrap();
assert_eq!((element.id, &ssid.name, rest), (0, &b"test"[..], &[1, 2, 0x82, 0x84][..]));
let mut bytes = BEACON[..46].to_vec();
bytes[37] = 5;
bytes.extend(puns::crc32(&bytes).to_le_bytes());
assert_eq!(try_pun_ref::<_, Beacon>(&bytes[..]).err(), Some(Error::Validity));
let long = [&[0, 33][..], &[b'x'; 33]].concat();
assert_eq!(try_pun_ref::<_, Ssid>(&long[..]).err(), Some(Error::Validity));
assert!(try_pun_ref::<_, Ack>(&ACK[..]).is_ok());
let mut bytes = ACK;
bytes[5] = 0;
assert_eq!(try_pun_ref::<_, Ack>(&bytes[..]).err(), Some(Error::Validity));
let (packet, rest) = Packet::from_head(&HCI).unwrap();
let Packet::Command(command) = packet else { panic!() };
assert_eq!((command.ogf(), command.ocf(), command.parameters.len()), (3, 3, 0));
let (packet, rest) = Packet::from_head(rest).unwrap();
let Packet::Complete(complete) = packet else { panic!() };
assert_eq!(
(complete.packets, complete.opcode.get(), &complete.parameters),
(1, 0x0c03, &[0][..])
);
let (packet, rest) = Packet::from_head(rest).unwrap();
let Packet::Acl(acl) = packet else { panic!() };
assert_eq!(
(acl.handle(), acl.boundary(), acl.data.channel.get(), &acl.data.payload),
(0x40, 2, 4, &[0x0a, 3, 0][..])
);
assert_eq!(rest.len(), 0);
let Ok(Packet::Event(event)) = Packet::from_bytes(&[0x04, 0x13, 0x01, 0x00]) else { panic!() };
assert_eq!((event.code, &event.parameters), (0x13, &[0][..]));
assert!(from_head::<Event>(&HCI[4..]).is_ok());
for (at, value) in [(16, 4), (13, 0x30), (18, 0)] {
let mut bytes = HCI;
bytes[at] = value;
assert_eq!(Packet::from_bytes(&bytes[11..]).err(), Some(Error::Validity));
}
assert_eq!(Packet::from_bytes(&[0x05, 0, 0, 0]).err(), Some(Error::Validity));
let can: &CanFrame = try_pun_ref(&CAN).unwrap();
assert_eq!((can.id(), can.extended(), can.remote(), can.error()), (0x123, false, false, false));
assert_eq!((can.len, &can.data[..2]), (2, &[0xde, 0xad][..]));
for (at, value) in [(4, 9), (1, 0x08), (7, 9)] {
let mut bytes = CAN;
bytes[at] = value;
assert_eq!(try_pun_ref::<_, CanFrame>(&bytes).err(), Some(Error::Validity));
}
let mut extended = CAN;
extended[1] = 0x08;
extended[3] = 0x80;
let can: &CanFrame = try_pun_ref(&extended).unwrap();
assert_eq!((can.extended(), can.id()), (true, 0x823));
let mut fd = [0u8; 72];
fd[4] = 48;
fd[5] = 1;
let frame: &CanFdFrame = try_pun_ref(&fd).unwrap();
assert_eq!((frame.len, frame.bit_rate_switch()), (48, true));
fd[4] = 40;
assert_eq!(try_pun_ref::<_, CanFdFrame>(&fd).err(), Some(Error::Validity));
fd[4] = 48;
fd[3] = 0x40;
assert_eq!(try_pun_ref::<_, CanFdFrame>(&fd).err(), Some(Error::Validity));
let frame: &RtuFrame = try_pun_ref(&READ[..]).unwrap();
assert_eq!(
(frame.address, frame.function, frame.exception(), frame.crc()),
(1, 3, false, &[0xc5, 0xcd][..])
);
let request: &ReadHoldingRegisters = try_pun_ref(frame.data()).unwrap();
assert_eq!((request.start.get(), request.count.get()), (0, 10));
for at in 0..READ.len() {
for bit in 0..8 {
let mut bytes = READ;
bytes[at] ^= 1 << bit;
assert_eq!(try_pun_ref::<_, RtuFrame>(&bytes[..]).err(), Some(Error::Validity));
}
}
let frame: &RtuFrame = try_pun_ref(®ISTERS[..]).unwrap();
let registers: &RegistersResponse = try_pun_ref(frame.data()).unwrap();
let values: Vec<u16> = registers.registers.iter().map(|register| register.get()).collect();
assert_eq!((frame.address, values), (17, vec![0x22b, 0, 0x64]));
let mut largest = vec![1u8; 254];
largest.extend(0x454f_u16.to_le_bytes());
assert!(try_pun_ref::<_, RtuFrame>(&largest[..]).is_ok());
largest.insert(0, 1);
assert_eq!(try_pun_ref::<_, RtuFrame>(&largest[..]).err(), Some(Error::Validity));
assert_eq!(try_pun_ref::<_, RtuFrame>(&READ[..3]).err(), Some(Error::Validity));
let (adu, rest) = from_head::<TcpAdu>(&ADU).unwrap();
assert_eq!((adu.transaction.get(), adu.unit, adu.function, rest), (1, 0x11, 3, &[0xee][..]));
let request: &ReadHoldingRegisters = try_pun_ref(&adu.data).unwrap();
assert_eq!((request.start.get(), request.count.get()), (0x6b, 3));
for (at, value, error) in [(3, 1, Error::Validity), (5, 1, Error::Validity), (5, 9, Error::Size)]
{
let mut bytes = ADU;
bytes[at] = value;
assert_eq!(from_head::<TcpAdu>(&bytes).err(), Some(error));
}
let mut bytes = ADU;
bytes[11] = 0;
let (adu, _) = from_head::<TcpAdu>(&bytes).unwrap();
assert_eq!(try_pun_ref::<_, ReadHoldingRegisters>(&adu.data).err(), Some(Error::Validity));
assert!(try_pun_ref::<_, Slip>(&SLIP[..]).is_ok());
for (at, value) in [(2, 0xc0), (2, 0x01), (7, 0x00)] {
let mut bytes = SLIP;
bytes[at] = value;
assert_eq!(try_pun_ref::<_, Slip>(&bytes[..]).err(), Some(Error::Validity));
}
assert_eq!(try_pun_ref::<_, Slip>(&[1u8, 0xdb, 0xc0][..]).err(), Some(Error::Validity));
assert!(try_pun_ref::<_, Ppp>(&LCP[..]).is_ok());
for (at, value) in [(1, 0xfe), (5, 0x7e)] {
let mut bytes = LCP;
bytes[at] = value;
assert_eq!(try_pun_ref::<_, Ppp>(&bytes[..]).err(), Some(Error::Validity));
}
let mut bytes = LCP;
bytes[15] ^= 0x01;
assert!(try_pun_ref::<_, Ppp>(&bytes[..]).is_ok());
println!("ok");
}