dot15d4_frame/repr/
mod.rs

1use crate::FrameType;
2
3use super::{Error, Frame, Result};
4
5mod addressing;
6pub use addressing::AddressingFieldsRepr;
7
8mod frame_control;
9pub use frame_control::FrameControlRepr;
10
11mod ie;
12pub use ie::*;
13
14mod builder;
15pub use builder::FrameBuilder;
16
17/// A high-level representation of an IEEE 802.15.4 frame.
18#[derive(Debug)]
19#[cfg_attr(feature = "fuzz", derive(arbitrary::Arbitrary))]
20pub struct FrameRepr<'p> {
21    /// The frame control field.
22    pub frame_control: FrameControlRepr,
23    /// The sequence number.
24    pub sequence_number: Option<u8>,
25    /// The addressing fields.
26    pub addressing_fields: Option<AddressingFieldsRepr>,
27    /// The information elements.
28    pub information_elements: Option<InformationElementsRepr>,
29    /// The payload.
30    pub payload: Option<&'p [u8]>,
31}
32
33impl<'f> FrameRepr<'f> {
34    /// Parse an IEEE 802.15.4 frame.
35    pub fn parse(reader: &Frame<&'f [u8]>) -> Result<Self> {
36        let frame_control = FrameControlRepr::parse(reader.frame_control())?;
37        let addressing_fields = reader
38            .addressing()
39            .map(|af| AddressingFieldsRepr::parse(af));
40        let information_elements = reader
41            .information_elements()
42            .map(InformationElementsRepr::parse)
43            .transpose()?;
44
45        Ok(Self {
46            frame_control,
47            sequence_number: reader.sequence_number(),
48            addressing_fields,
49            information_elements,
50            payload: reader.payload(),
51        })
52    }
53
54    /// Validate the frame.
55    pub fn validate(&self) -> Result<()> {
56        // If the frame type is data, then the addressing fields must be present.
57        if self.frame_control.frame_type == FrameType::Data {
58            if self.addressing_fields.is_none() {
59                return Err(Error);
60            }
61
62            if self.payload.is_none() {
63                return Err(Error);
64            }
65        }
66
67        // If the addressing fields are present, they must be valid.
68        if let Some(af) = &self.addressing_fields {
69            af.validate(&self.frame_control)?;
70        }
71
72        // If the payload is present, it must not be empty.
73        if let Some(payload) = self.payload {
74            if payload.is_empty() {
75                return Err(Error);
76            }
77        }
78
79        Ok(())
80    }
81
82    /// Return the length of the frame when emitted into a buffer.
83    pub fn buffer_len(&self) -> usize {
84        let mut len = 2; // Frame control
85
86        if self.sequence_number.is_some() {
87            len += 1;
88        }
89
90        if let Some(af) = &self.addressing_fields {
91            len += af.buffer_len(&self.frame_control);
92        }
93
94        if let Some(ie) = &self.information_elements {
95            len += ie.buffer_len(self.payload.is_some());
96        }
97
98        if let Some(payload) = self.payload {
99            len += payload.len();
100        }
101
102        len
103    }
104
105    /// Emit the frame into a buffer.
106    pub fn emit(&self, frame: &mut Frame<&'_ mut [u8]>) {
107        frame.set_frame_control(&self.frame_control);
108
109        if let Some(sequence_number) = self.sequence_number {
110            frame.set_sequence_number(sequence_number);
111        }
112
113        if let Some(af) = &self.addressing_fields {
114            frame.set_addressing_fields(af);
115        }
116
117        if let Some(ie) = &self.information_elements {
118            frame.set_information_elements(ie, self.payload.is_some());
119        }
120
121        if let Some(payload) = self.payload {
122            frame.set_payload(payload);
123        }
124    }
125}