@@ -25,74 +25,8 @@ pub use header::Header;
2525/// Represents a MAC frame. Can be used to [decode] a frame from bytes, or
2626/// [encode] a frame to bytes.
2727///
28- /// [decode]: #method.decode
29- /// [encode]: #method.encode
30- #[ derive( Clone , Copy , Debug , Eq , Hash , PartialEq ) ]
31- pub struct Frame < ' p > {
32- /// Header
33- pub header : Header ,
34-
35- /// Content
36- pub content : FrameContent ,
37-
38- /// Payload
39- pub payload : & ' p [ u8 ] ,
40-
41- /// Footer
42- ///
43- /// This is a 2-byte CRC checksum.
44- ///
45- /// When creating an instance of this struct for encoding, you don't
46- /// necessarily need to write an actual CRC checksum here. [`Frame::encode`]
47- /// can omit writing this checksum, for example if the transceiver hardware
48- /// automatically adds the checksum for you.
49- pub footer : [ u8 ; 2 ] ,
50- }
51-
52- impl TryWrite < FooterMode > for Frame < ' _ > {
53- fn try_write ( self , bytes : & mut [ u8 ] , mode : FooterMode ) -> byte:: Result < usize > {
54- let offset = & mut 0 ;
55- bytes. write ( offset, self . header ) ?;
56- bytes. write ( offset, self . content ) ?;
57- bytes. write ( offset, self . payload ) ?;
58- match mode {
59- FooterMode :: None => { }
60- FooterMode :: Explicit => bytes. write ( offset, & self . footer [ ..] ) ?,
61- }
62- Ok ( * offset)
63- }
64- }
65-
66- impl < ' a > TryRead < ' a , FooterMode > for Frame < ' a > {
67- fn try_read ( bytes : & ' a [ u8 ] , mode : FooterMode ) -> byte:: Result < ( Self , usize ) > {
68- let offset = & mut 0 ;
69- let header = bytes. read ( offset) ?;
70- let content = bytes. read_with ( offset, & header) ?;
71- let ( payload, footer) = match mode {
72- FooterMode :: None => (
73- bytes. read_with ( offset, Bytes :: Len ( bytes. len ( ) - * offset) ) ?,
74- 0u16 ,
75- ) ,
76- FooterMode :: Explicit => (
77- bytes. read_with ( offset, Bytes :: Len ( bytes. len ( ) - * offset - 2 ) ) ?,
78- bytes. read_with ( offset, LE ) ?,
79- ) ,
80- } ;
81- Ok ( (
82- Frame {
83- header : header,
84- content : content,
85- payload,
86- footer : footer. to_le_bytes ( ) ,
87- } ,
88- * offset,
89- ) )
90- }
91- }
92-
93- /// Decodes a frame from a byte buffer
9428///
95- /// # Errors
29+ /// # Decode Errors
9630///
9731/// This function returns an error, if the bytes either don't encode a valid
9832/// IEEE 802.15.4 frame, or encode a frame that is not fully supported by
@@ -205,7 +139,73 @@ impl<'a> TryRead<'a, FooterMode> for Frame<'a> {
205139/// assert_eq!(bytes[..len], expected_bytes);
206140/// ```
207141///
208- /// Tells [`Frame::encode`] whether to read/write the footer
142+ /// [decode]: #method.try_read
143+ /// [encode]: #method.try_write
144+ #[ derive( Clone , Copy , Debug , Eq , Hash , PartialEq ) ]
145+ pub struct Frame < ' p > {
146+ /// Header
147+ pub header : Header ,
148+
149+ /// Content
150+ pub content : FrameContent ,
151+
152+ /// Payload
153+ pub payload : & ' p [ u8 ] ,
154+
155+ /// Footer
156+ ///
157+ /// This is a 2-byte CRC checksum.
158+ ///
159+ /// When creating an instance of this struct for encoding, you don't
160+ /// necessarily need to write an actual CRC checksum here. [`Frame::encode`]
161+ /// can omit writing this checksum, for example if the transceiver hardware
162+ /// automatically adds the checksum for you.
163+ pub footer : [ u8 ; 2 ] ,
164+ }
165+
166+ impl TryWrite < FooterMode > for Frame < ' _ > {
167+ fn try_write ( self , bytes : & mut [ u8 ] , mode : FooterMode ) -> byte:: Result < usize > {
168+ let offset = & mut 0 ;
169+ bytes. write ( offset, self . header ) ?;
170+ bytes. write ( offset, self . content ) ?;
171+ bytes. write ( offset, self . payload ) ?;
172+ match mode {
173+ FooterMode :: None => { }
174+ FooterMode :: Explicit => bytes. write ( offset, & self . footer [ ..] ) ?,
175+ }
176+ Ok ( * offset)
177+ }
178+ }
179+
180+ impl < ' a > TryRead < ' a , FooterMode > for Frame < ' a > {
181+ fn try_read ( bytes : & ' a [ u8 ] , mode : FooterMode ) -> byte:: Result < ( Self , usize ) > {
182+ let offset = & mut 0 ;
183+ let header = bytes. read ( offset) ?;
184+ let content = bytes. read_with ( offset, & header) ?;
185+ let ( payload, footer) = match mode {
186+ FooterMode :: None => (
187+ bytes. read_with ( offset, Bytes :: Len ( bytes. len ( ) - * offset) ) ?,
188+ 0u16 ,
189+ ) ,
190+ FooterMode :: Explicit => (
191+ bytes. read_with ( offset, Bytes :: Len ( bytes. len ( ) - * offset - 2 ) ) ?,
192+ bytes. read_with ( offset, LE ) ?,
193+ ) ,
194+ } ;
195+ Ok ( (
196+ Frame {
197+ header : header,
198+ content : content,
199+ payload,
200+ footer : footer. to_le_bytes ( ) ,
201+ } ,
202+ * offset,
203+ ) )
204+ }
205+ }
206+
207+ ///
208+ /// Controls whether the footer is read/written with the frame
209209///
210210/// Eventually, this should support three options:
211211/// 1. Don't read or write the footer
@@ -214,7 +214,7 @@ impl<'a> TryRead<'a, FooterMode> for Frame<'a> {
214214///
215215/// For now, only 1 and 3 are supported.
216216///
217- /// [`Frame::encode `](Frame::encode )
217+ /// [`Frame::try_write `](Frame::try_write )
218218pub enum FooterMode {
219219 /// Don't read/write the footer
220220 None ,
0 commit comments