Skip to content

Commit 9284b2a

Browse files
korken89ryankurte
andauthored
Feature/802.15.4 2015 (#44)
Add defmt support, extend types for 802.15.4-2015 Co-authored-by: ryan <ryan@kurte.nz>
1 parent 4d24735 commit 9284b2a

File tree

10 files changed

+96
-3
lines changed

10 files changed

+96
-3
lines changed

Cargo.toml

Lines changed: 4 additions & 2 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" }
@@ -21,6 +23,7 @@ travis-ci = { repository = "braun-robotics/ieee802154" }
2123
hash32 = "0.2.1"
2224
hash32-derive = "0.1"
2325
byte = "0.2.4"
26+
defmt = { version = ">=0.2.0,<0.4", optional = true }
2427

2528
[dependencies.ccm]
2629
version = "0.4.0"
@@ -30,10 +33,9 @@ default-features = false
3033
version = "0.3.0"
3134
default-features = false
3235

33-
3436
[dev-dependencies]
3537
rand = "0.8.3"
3638

3739
[dev-dependencies.aes]
3840
version = "0.7.0"
39-
default-features = false
41+
default-features = false

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
///
@@ -152,6 +155,7 @@ impl TryWrite for SuperframeSpecification {
152155

153156
/// Direction of data
154157
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
158+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
155159
enum Direction {
156160
/// Receive data
157161
Receive,
@@ -161,6 +165,7 @@ enum Direction {
161165

162166
/// Descriptor of the guaranteed time slots (GTSs)
163167
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
168+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
164169
pub struct GuaranteedTimeSlotDescriptor {
165170
/// Device short address used by this slot
166171
short_address: ShortAddress,
@@ -230,6 +235,7 @@ const PERMIT: u8 = 0b1000_0000;
230235

231236
/// Information of the guaranteed time slots (GTSs)
232237
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
238+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
233239
pub struct GuaranteedTimeSlotInformation {
234240
/// Permit GTS
235241
pub permit: bool,
@@ -348,6 +354,7 @@ const EXTENDED_MASK: u8 = 0b0111_0000;
348354
/// 0 - 2 3 4 - 6 7 bit
349355
/// ```
350356
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
357+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
351358
pub struct PendingAddress {
352359
short_address_count: usize,
353360
short_addresses: [ShortAddress; 7],
@@ -433,6 +440,7 @@ impl TryWrite for PendingAddress {
433440

434441
/// Beacon frame
435442
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
443+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
436444
pub struct Beacon {
437445
/// Superframe specification
438446
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.
@@ -125,6 +126,7 @@ extended_enum!(
125126
///
126127
/// Changes to the PAN sent by the coordinator.
127128
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
129+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
128130
pub struct CoordinatorRealignmentData {
129131
/// PAN id that the coordinator will use
130132
pub pan_id: PanId,
@@ -185,6 +187,7 @@ const GTSC_ALLOCATION: u8 = 0x20;
185187
///
186188
/// GTS configuration requested with the guaranteed time slot request command.
187189
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
190+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
188191
pub struct GuaranteedTimeSlotCharacteristics {
189192
/// Number of slots requested
190193
pub count: u8,
@@ -221,6 +224,7 @@ impl From<GuaranteedTimeSlotCharacteristics> for u8 {
221224

222225
/// MAC commands
223226
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
227+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
224228
pub enum Command {
225229
/// Association request, request association to a PAN
226230
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: 22 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

@@ -122,8 +129,14 @@ impl TryRead<'_> for Header {
122129
let pan_id_compress =
123130
((bits & mask::PAN_ID_COMPRESS) >> offset::PAN_ID_COMPRESS) as u8;
124131

132+
let seq_no_suppress =
133+
((bits & mask::SEQ_NO_SUPPRESS) >> offset::SEQ_NO_SUPPRESS) as u8;
134+
let ie_present =
135+
((bits & mask::IE_PRESENT) >> offset::IE_PRESENT) as u8;
136+
125137
let dest_addr_mode =
126138
((bits & mask::DEST_ADDR_MODE) >> offset::DEST_ADDR_MODE) as u8;
139+
127140
let version = ((bits & mask::VERSION) >> offset::VERSION) as u8;
128141
let src_addr_mode =
129142
((bits & mask::SRC_ADDR_MODE) >> offset::SRC_ADDR_MODE) as u8;
@@ -140,6 +153,8 @@ impl TryRead<'_> for Header {
140153
let frame_pending = frame_pending > 0;
141154
let ack_request = ack_request > 0;
142155
let pan_id_compress = pan_id_compress > 0;
156+
let seq_no_suppress = seq_no_suppress > 0;
157+
let ie_present = ie_present > 0;
143158

144159
/* Decode header depending on Frame Control Fields */
145160

@@ -202,6 +217,8 @@ impl TryRead<'_> for Header {
202217
frame_pending,
203218
ack_request,
204219
pan_id_compress,
220+
seq_no_suppress,
221+
ie_present,
205222
version,
206223
seq,
207224
destination,
@@ -295,6 +312,7 @@ where
295312
/// let pan_id = PanId(0x0123);
296313
/// ```
297314
#[derive(Clone, Copy, Debug, Eq, Hash, Hash32, PartialEq)]
315+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
298316
pub struct PanId(pub u16);
299317

300318
impl PanId {
@@ -332,6 +350,7 @@ impl TryRead<'_> for PanId {
332350
/// let short_address = ShortAddress(0x0123);
333351
/// ```
334352
#[derive(Clone, Copy, Debug, Eq, Hash, Hash32, PartialEq)]
353+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
335354
pub struct ShortAddress(pub u16);
336355

337356
impl ShortAddress {
@@ -371,6 +390,7 @@ impl TryRead<'_> for ShortAddress {
371390
/// let ext_address = ExtendedAddress(0x0123456789abcdef);
372391
/// ```
373392
#[derive(Clone, Copy, Debug, Eq, Hash, Hash32, PartialEq)]
393+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
374394
pub struct ExtendedAddress(pub u64);
375395

376396
impl ExtendedAddress {
@@ -400,6 +420,7 @@ impl TryRead<'_> for ExtendedAddress {
400420

401421
/// An address that might contain an PAN ID and address
402422
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
423+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
403424
pub enum Address {
404425
/// Short (16-bit) address and PAN ID (16-bit)
405426
Short(PanId, ShortAddress),
@@ -408,6 +429,7 @@ pub enum Address {
408429
}
409430

410431
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
432+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
411433
enum AddressEncoding {
412434
Normal,
413435
Compressed,

0 commit comments

Comments
 (0)