Skip to content

Commit 176fec5

Browse files
committed
Add defmt support, extend types for 802.15.4-2015
1 parent 44f7296 commit 176fec5

File tree

10 files changed

+79
-1
lines changed

10 files changed

+79
-1
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ readme = "README.md"
1212
categories = ["embedded", "network-programming", "no-std"]
1313
keywords = ["WPAN"]
1414

15+
[features]
16+
1517

1618
[badges]
1719
travis-ci = { repository = "braun-robotics/ieee802154" }
@@ -23,7 +25,9 @@ hash32-derive = "0.1"
2325
byte = "0.2.4"
2426
cipher = { version = "0.2", default-features = false }
2527
ccm = {version = "0.3.0", default-features = false}
28+
defmt = { version = "0.2.1", optional = true }
2629

2730
[dev-dependencies]
2831
aes-soft = {version = "0.6.4", default-features = false}
2932
rand = "0.8.3"
33+

src/mac/beacon.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::mac::{ExtendedAddress, ShortAddress};
1010

1111
/// Beacon order is used to calculate the beacon interval
1212
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
13+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
1314
pub enum BeaconOrder {
1415
/// Used to calculate at which interval beacons are sent
1516
///
@@ -41,6 +42,7 @@ impl From<BeaconOrder> for u8 {
4142

4243
/// Superframe order, amount of time during wich this superframe is active
4344
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
45+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
4446
pub enum SuperframeOrder {
4547
/// Ammount of time that the superframe is active
4648
///
@@ -75,6 +77,7 @@ impl From<SuperframeOrder> for u8 {
7577
/// The superframe specification describes the organisation of frames in the
7678
/// air when using superframes and/or periodical beacons.
7779
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
80+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
7881
pub struct SuperframeSpecification {
7982
/// Beacon order, 0-15, where 15 is on demand.
8083
///
@@ -150,6 +153,7 @@ impl TryWrite for SuperframeSpecification {
150153

151154
/// Direction of data
152155
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
156+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
153157
enum Direction {
154158
/// Receive data
155159
Receive,
@@ -159,6 +163,7 @@ enum Direction {
159163

160164
/// Descriptor of the guaranteed time slots (GTSs)
161165
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
166+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
162167
pub struct GuaranteedTimeSlotDescriptor {
163168
/// Device short address used by this slot
164169
short_address: ShortAddress,
@@ -228,6 +233,7 @@ const PERMIT: u8 = 0b1000_0000;
228233

229234
/// Information of the guaranteed time slots (GTSs)
230235
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
236+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
231237
pub struct GuaranteedTimeSlotInformation {
232238
/// Permit GTS
233239
pub permit: bool,
@@ -345,6 +351,7 @@ const EXTENDED_MASK: u8 = 0b0111_0000;
345351
/// 0 - 2 3 4 - 6 7 bit
346352
/// ```
347353
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
354+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
348355
pub struct PendingAddress {
349356
short_address_count: usize,
350357
short_addresses: [ShortAddress; 7],
@@ -429,6 +436,7 @@ impl TryWrite for PendingAddress {
429436

430437
/// Beacon frame
431438
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
439+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
432440
pub struct Beacon {
433441
/// Superframe specification
434442
pub superframe_spec: SuperframeSpecification,

src/mac/command.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const CAP_ALLOCATE_ADDRESS: u8 = 0x80;
4242
///
4343
/// Sent with association request to report the capabilities of the device.
4444
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
45+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
4546
pub struct CapabilityInformation {
4647
/// Full-function device (FFD) or a reduced-function device (RFD)
4748
/// RFD and FFD have different function sets.
@@ -123,6 +124,7 @@ extended_enum!(
123124
///
124125
/// Changes to the PAN sent by the coordinator.
125126
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
127+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
126128
pub struct CoordinatorRealignmentData {
127129
/// PAN id that the coordinator will use
128130
pub pan_id: PanId,
@@ -183,6 +185,7 @@ const GTSC_ALLOCATION: u8 = 0x20;
183185
///
184186
/// GTS configuration requested with the guaranteed time slot request command.
185187
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
188+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
186189
pub struct GuaranteedTimeSlotCharacteristics {
187190
/// Number of slots requested
188191
pub count: u8,
@@ -219,6 +222,7 @@ impl From<GuaranteedTimeSlotCharacteristics> for u8 {
219222

220223
/// MAC commands
221224
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
225+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
222226
pub enum Command {
223227
/// Association request, request association to a PAN
224228
AssociationRequest(CapabilityInformation),

src/mac/frame/frame_control.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use super::DecodeError;
88
///
99
/// [`Header`]: super::header::Header
1010
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
11+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
1112
pub enum FrameType {
1213
/// Beacon
1314
Beacon = 0b000,
@@ -20,6 +21,15 @@ pub enum FrameType {
2021

2122
/// MAC command
2223
MacCommand = 0b011,
24+
25+
/// Multipurpose
26+
Multipurpose = 0b101,
27+
28+
/// Fragment of Fragment Ack
29+
FragOrFragAck = 0b110,
30+
31+
/// Extended
32+
Extended = 0b111,
2333
}
2434

2535
impl FrameType {
@@ -41,13 +51,17 @@ impl FrameType {
4151
0b001 => Some(FrameType::Data),
4252
0b010 => Some(FrameType::Acknowledgement),
4353
0b011 => Some(FrameType::MacCommand),
54+
0b101 => Some(FrameType::Multipurpose),
55+
0b110 => Some(FrameType::FragOrFragAck),
56+
0b111 => Some(FrameType::Extended),
4457
_ => None,
4558
}
4659
}
4760
}
4861

4962
/// Defines version information for a frame
5063
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
64+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
5165
pub enum FrameVersion {
5266
/// A frame conforming to the 802.15.4-2003 standard
5367
Ieee802154_2003 = 0b00,
@@ -92,6 +106,7 @@ impl FrameVersion {
92106
/// assert_eq!(address_mode, AddressMode::Short);
93107
/// ```
94108
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
109+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
95110
pub enum AddressMode {
96111
/// PAN identifier and address field are not present
97112
None = 0b00,
@@ -150,6 +165,8 @@ pub mod offset {
150165
pub const PENDING: u16 = 4;
151166
pub const ACK: u16 = 5;
152167
pub const PAN_ID_COMPRESS: u16 = 6;
168+
pub const SEQ_NO_SUPPRESS: u16 = 8;
169+
pub const IE_PRESENT: u16 = 9;
153170
pub const DEST_ADDR_MODE: u16 = 10;
154171
pub const VERSION: u16 = 12;
155172
pub const SRC_ADDR_MODE: u16 = 14u16;
@@ -161,6 +178,8 @@ pub mod mask {
161178
pub const PENDING: u16 = 0x0010;
162179
pub const ACK: u16 = 0x0020;
163180
pub const PAN_ID_COMPRESS: u16 = 0x0040;
181+
pub const SEQ_NO_SUPPRESS: u16 = 0x0100;
182+
pub const IE_PRESENT: u16 = 0x0200;
164183
pub const DEST_ADDR_MODE: u16 = 0x0C00;
165184
pub const VERSION: u16 = 0x3000;
166185
pub const SRC_ADDR_MODE: u16 = 0xC000;

src/mac/frame/header.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use super::{security::AuxiliarySecurityHeader, EncodeError};
2222
///
2323
/// [MAC frame format start at 5.2]: http://ecee.colorado.edu/~liue/teaching/comm_standards/2015S_zigbee/802.15.4-2011.pdf
2424
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
25+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
2526
pub struct Header {
2627
// * Frame Control Field * /
2728
/// Frame Type
@@ -51,6 +52,12 @@ pub struct Header {
5152
/// present.
5253
pub pan_id_compress: bool,
5354

55+
/// Suppress sequence number
56+
pub seq_no_suppress: bool,
57+
58+
/// Information element present
59+
pub ie_present: bool,
60+
5461
/// Frame version
5562
pub version: FrameVersion,
5663

@@ -120,6 +127,9 @@ impl TryRead<'_> for Header {
120127
let ack_request = ((bits & mask::ACK) >> offset::ACK) as u8;
121128
let pan_id_compress = ((bits & mask::PAN_ID_COMPRESS) >> offset::PAN_ID_COMPRESS) as u8;
122129

130+
let seq_no_suppress = ((bits & mask::SEQ_NO_SUPPRESS) >> offset::SEQ_NO_SUPPRESS) as u8;
131+
let ie_present = ((bits & mask::IE_PRESENT) >> offset::IE_PRESENT) as u8;
132+
123133
let dest_addr_mode = ((bits & mask::DEST_ADDR_MODE) >> offset::DEST_ADDR_MODE) as u8;
124134
let version = ((bits & mask::VERSION) >> offset::VERSION) as u8;
125135
let src_addr_mode = ((bits & mask::SRC_ADDR_MODE) >> offset::SRC_ADDR_MODE) as u8;
@@ -136,6 +146,8 @@ impl TryRead<'_> for Header {
136146
let frame_pending = frame_pending > 0;
137147
let ack_request = ack_request > 0;
138148
let pan_id_compress = pan_id_compress > 0;
149+
let seq_no_suppress = seq_no_suppress > 0;
150+
let ie_present = ie_present > 0;
139151

140152
/* Decode header depending on Frame Control Fields */
141153

@@ -189,6 +201,8 @@ impl TryRead<'_> for Header {
189201
frame_pending,
190202
ack_request,
191203
pan_id_compress,
204+
seq_no_suppress,
205+
ie_present,
192206
version,
193207
seq,
194208
destination,
@@ -278,6 +292,7 @@ where
278292
/// let pan_id = PanId(0x0123);
279293
/// ```
280294
#[derive(Clone, Copy, Debug, Eq, Hash, Hash32, PartialEq)]
295+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
281296
pub struct PanId(pub u16);
282297

283298
impl PanId {
@@ -315,6 +330,7 @@ impl TryRead<'_> for PanId {
315330
/// let short_address = ShortAddress(0x0123);
316331
/// ```
317332
#[derive(Clone, Copy, Debug, Eq, Hash, Hash32, PartialEq)]
333+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
318334
pub struct ShortAddress(pub u16);
319335

320336
impl ShortAddress {
@@ -354,6 +370,7 @@ impl TryRead<'_> for ShortAddress {
354370
/// let ext_address = ExtendedAddress(0x0123456789abcdef);
355371
/// ```
356372
#[derive(Clone, Copy, Debug, Eq, Hash, Hash32, PartialEq)]
373+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
357374
pub struct ExtendedAddress(pub u64);
358375

359376
impl ExtendedAddress {
@@ -383,6 +400,7 @@ impl TryRead<'_> for ExtendedAddress {
383400

384401
/// An address that might contain an PAN ID and address
385402
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
403+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
386404
pub enum Address {
387405
/// Short (16-bit) address and PAN ID (16-bit)
388406
Short(PanId, ShortAddress),
@@ -391,6 +409,7 @@ pub enum Address {
391409
}
392410

393411
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
412+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
394413
enum AddressEncoding {
395414
Normal,
396415
Compressed,

src/mac/frame/mod.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ use self::security::{
150150
/// [decode]: #method.try_read
151151
/// [encode]: #method.try_write
152152
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
153+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
153154
pub struct Frame<'p> {
154155
/// Header
155156
pub header: Header,
@@ -367,6 +368,7 @@ impl<'a> TryRead<'a, FooterMode> for Frame<'a> {
367368
///
368369
/// [`Frame::try_write`](Frame::try_write)
369370
#[derive(Clone, Copy)]
371+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
370372
pub enum FooterMode {
371373
/// Don't read/write the footer
372374
None,
@@ -382,6 +384,7 @@ impl Default for FooterMode {
382384

383385
/// Content of a frame
384386
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
387+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
385388
pub enum FrameContent {
386389
/// Beacon frame content
387390
Beacon(Beacon),
@@ -391,15 +394,23 @@ pub enum FrameContent {
391394
Acknowledgement,
392395
/// MAC command frame
393396
Command(Command),
397+
/// Multipurpose frame
398+
Multipurpose,
399+
400+
/// Fragment of Fragment Ack frame
401+
FragOrFragAck,
402+
403+
/// Extended frame
404+
Extended,
394405
}
395406

396407
impl TryWrite for FrameContent {
397408
fn try_write(self, bytes: &mut [u8], _ctx: ()) -> byte::Result<usize> {
398409
let offset = &mut 0;
399410
match self {
400411
FrameContent::Beacon(beacon) => bytes.write(offset, beacon)?,
401-
FrameContent::Data | FrameContent::Acknowledgement => (),
402412
FrameContent::Command(command) => bytes.write(offset, command)?,
413+
_ => (),
403414
};
404415
Ok(*offset)
405416
}
@@ -414,6 +425,9 @@ impl TryRead<'_, &Header> for FrameContent {
414425
FrameType::Data => FrameContent::Data,
415426
FrameType::Acknowledgement => FrameContent::Acknowledgement,
416427
FrameType::MacCommand => FrameContent::Command(bytes.read(offset)?),
428+
FrameType::Multipurpose => FrameContent::Multipurpose,
429+
FrameType::FragOrFragAck => FrameContent::FragOrFragAck,
430+
FrameType::Extended => FrameContent::Extended,
417431
},
418432
*offset,
419433
))
@@ -422,6 +436,7 @@ impl TryRead<'_, &Header> for FrameContent {
422436

423437
/// Signals an error that occured while decoding bytes
424438
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
439+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
425440
pub enum DecodeError {
426441
/// Buffer does not contain enough bytes
427442
NotEnoughBytes,
@@ -491,6 +506,7 @@ impl From<DecodeError> for byte::Error {
491506

492507
/// Errors that can occur while securing or unsecuring a frame
493508
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
509+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
494510
pub enum EncodeError {
495511
/// Something went wrong while writing a frame's bytes to the destination
496512
WriteError,

src/mac/frame/security/auxiliary_security_header.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use cipher::{consts::U16, BlockCipher, NewBlockCipher};
88
///
99
/// See: section 7.4
1010
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
11+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
1112
pub struct AuxiliarySecurityHeader {
1213
/// The control field in the Auxiliary Security Header
1314
pub control: SecurityControl,
@@ -147,6 +148,7 @@ where
147148

148149
/// A key identifier
149150
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
151+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
150152
pub struct KeyIdentifier {
151153
/// The key source to be used for determining a key from this key identifier (if any)
152154
pub key_source: Option<KeySource>,
@@ -173,6 +175,7 @@ impl TryWrite for KeyIdentifier {
173175

174176
/// A key source
175177
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
178+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
176179
pub enum KeySource {
177180
/// A key source that is 4 octets long
178181
Short(u32),

src/mac/frame/security/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ pub enum AddressingMode {
191191
}
192192

193193
#[derive(Clone)]
194+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
194195
/// A partial device descriptor
195196
pub struct DeviceDescriptor {
196197
/// The frame counter associated with this device

0 commit comments

Comments
 (0)