Skip to content

Commit

Permalink
Move the old ieee802154 crate into the project.
Browse files Browse the repository at this point in the history
Add stricter fmt rules.
  • Loading branch information
diondokter committed Feb 14, 2025
1 parent e13e2d5 commit ad89067
Show file tree
Hide file tree
Showing 49 changed files with 4,898 additions and 196 deletions.
14 changes: 5 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ embassy-sync = { git = "https://github.com/embassy-rs/embassy.git", tag = "embas

arrayvec = { version = "0.7.6", default-features = false }
critical-section = "1.2.0"
grounded = { version = "0.2.0", features = ["cas"] }
ieee802154 = { version = "0.7.0", git = "https://github.com/rust-iot/rust-ieee802.15.4.git", rev = "769a33e8d14062f0d42e250fbbe80ecff0c99f96" } # https://github.com/rust-iot/rust-ieee802.15.4/pull/69
maitake-sync = { version = "0.1.2", default-features = false }
static_cell = "2.1.0"
dw1000 = { git = "https://github.com/jkelleyrtp/dw1000-rs", rev = "698987dbfebb2db16bf20c166bf0d75c16f982c1" }
embedded-hal = "1.0.0"
embedded-hal-async = "1.0.0"
Expand All @@ -32,19 +29,18 @@ byte = "0.2.7"
rand = { version = "0.8.5", optional = true }
futures = { version = "0.3.31", default-features = false, features = ["async-await"] }

[dependencies.tokio]
version = "1.41.0"
optional = true
default-features = false
features = ["time", "test-util"]
heapless = "0.8.0"
ccm = { version = "0.4.0", default-features = false }
cipher = { version = "0.3.0", default-features = false }
tokio = { version = "1.41.0", optional = true, default-features = false, features = ["time", "test-util"] }

[dev-dependencies]
test-log = "0.2.16"
futures-test = "0.3.31"
critical-section = { version = "1.1.3", features = ["std"] }
tokio = { version = "1.41.0", features = ["test-util", "macros"] }
rand = "0.8.5"
pretty_assertions = "1.4.1"
aes = { version = "0.7.0", default-features = false }

[features]
default = ["test_helpers"]
Expand Down
2 changes: 2 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
group_imports = "StdExternalCrate"
imports_granularity = "Crate"
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
extern crate alloc;
extern crate core;

use ieee802154::mac::{ExtendedAddress, ShortAddress};
use crate::wire::{ExtendedAddress, ShortAddress};

// This must go FIRST so that all the other modules see its macros.
mod fmt;
Expand All @@ -18,6 +18,7 @@ pub mod sap;
#[cfg(feature = "test_helpers")]
pub mod test_helpers;
pub mod time;
pub mod wire;

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DeviceAddress {
Expand Down
3 changes: 1 addition & 2 deletions src/mac/callback.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use super::{commander::RequestResponder, state::MacState};
use crate::{
phy::{Phy, SendResult},
pib::MacPib,
sap::start::StartRequest,
};

use super::{commander::RequestResponder, state::MacState};

/// A callback that will be ran when a message has been sent.
pub enum SendCallback<'a> {
StartProcedure(RequestResponder<'a, StartRequest>),
Expand Down
3 changes: 1 addition & 2 deletions src/mac/mlme_get.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::{commander::RequestResponder, MacError};
use crate::{
phy::Phy,
pib::{MacPib, PibValue},
Expand All @@ -7,8 +8,6 @@ use crate::{
},
};

use super::{commander::RequestResponder, MacError};

pub async fn process_get_request(
phy: &mut impl Phy,
mac_pib: &MacPib,
Expand Down
18 changes: 9 additions & 9 deletions src/mac/mlme_reset.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#[allow(unused_imports)]
use micromath::F32Ext;
use rand_core::RngCore;

use super::{commander::RequestResponder, state::MacState, MacConfig, MacError};
use crate::{
consts::MAX_BEACON_PAYLOAD_LENGTH,
phy::Phy,
pib::{MacPib, MacPibWrite, SequenceNumber},
sap::reset::{ResetConfirm, ResetRequest},
time::DelayNsExt,
wire::{
beacon::{BeaconOrder, SuperframeOrder},
ExtendedAddress, PanId, ShortAddress,
},
};

use super::{commander::RequestResponder, state::MacState, MacConfig, MacError};
use ieee802154::mac::{
beacon::{BeaconOrder, SuperframeOrder},
ExtendedAddress, PanId, ShortAddress,
};
#[allow(unused_imports)]
use micromath::F32Ext;
use rand_core::RngCore;

pub async fn process_reset_request<P: Phy, Rng: RngCore, Delay: DelayNsExt>(
phy: &mut P,
mac_pib: &mut MacPib,
Expand Down
6 changes: 2 additions & 4 deletions src/mac/mlme_scan.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use ieee802154::mac::{Frame, FrameContent, PanId};

use super::{commander::RequestResponder, state::MacState, MacHandler};
use crate::{
consts::BASE_SUPERFRAME_DURATION,
phy::Phy,
Expand All @@ -10,11 +9,10 @@ use crate::{
PanDescriptor, SecurityInfo, Status,
},
time::{DelayNsExt, Duration, Instant},
wire::{Frame, FrameContent, PanId},
ChannelPage,
};

use super::{commander::RequestResponder, state::MacState, MacHandler};

pub async fn process_scan_request<'a>(
phy: &mut impl Phy,
mac_pib: &mut MacPib,
Expand Down
3 changes: 1 addition & 2 deletions src/mac/mlme_set.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::{commander::RequestResponder, MacError};
use crate::{
phy::Phy,
pib::{MacPibWrite, PibValue},
Expand All @@ -7,8 +8,6 @@ use crate::{
},
};

use super::{commander::RequestResponder, MacError};

pub async fn process_set_request(
phy: &mut impl Phy,
mac_pib_write: &mut MacPibWrite,
Expand Down
20 changes: 10 additions & 10 deletions src/mac/mlme_start.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use ieee802154::mac::beacon::{BeaconOrder, SuperframeOrder};
use ieee802154::mac::ShortAddress;

use super::{
commander::RequestResponder,
state::{BeaconMode, MacState},
MacError,
};
use crate::{
consts,
mac::callback::SendCallback,
Expand All @@ -10,12 +12,10 @@ use crate::{
start::{StartConfirm, StartRequest},
Status,
},
};

use super::{
commander::RequestResponder,
state::{BeaconMode, MacState},
MacError,
wire::{
beacon::{BeaconOrder, SuperframeOrder},
ShortAddress,
},
};

pub async fn process_start_request<'a>(
Expand Down Expand Up @@ -44,7 +44,7 @@ pub async fn process_start_request<'a>(
}

if responder.request.coord_realignment {
use ieee802154::mac::{
use crate::wire::{
command::{Command, CoordinatorRealignmentData},
Address, Frame, FrameContent, FrameType, FrameVersion, Header, PanId,
};
Expand Down
33 changes: 17 additions & 16 deletions src/mac/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use commander::MacHandler;
pub use commander::{IndicationResponder, MacCommander};
use embassy_futures::select::{select, Either};
use futures::FutureExt;
use ieee802154::mac::{ExtendedAddress, Frame, FrameContent, PanId, ShortAddress};
use mlme_get::process_get_request;
use mlme_reset::process_reset_request;
use mlme_scan::{process_scan_request, ScanAction};
Expand All @@ -29,6 +28,8 @@ use mlme_start::process_start_request;
use rand_core::RngCore;
use state::{BeaconMode, MacState};

use crate::wire::{ExtendedAddress, Frame, FrameContent, PanId, ShortAddress};

const BEACON_PLANNING_HEADROOM: Duration = Duration::from_millis(20);

/// Run the MAC layer of the IEEE protocol.
Expand Down Expand Up @@ -257,7 +258,7 @@ async fn perform_scan_action(
mac_state: &mut MacState<'_>,
mac_pib: &mut MacPib,
) {
use ieee802154::mac;
use crate::wire;

match scan_action {
action @ ScanAction::StartScan {
Expand Down Expand Up @@ -293,24 +294,24 @@ async fn perform_scan_action(
}
ScanType::Active => {
let data = mac_state.serialize_frame(Frame {
header: mac::Header {
frame_type: mac::FrameType::MacCommand,
header: wire::Header {
frame_type: wire::FrameType::MacCommand,
frame_pending: false,
ack_request: false,
pan_id_compress: false,
seq_no_suppress: false,
ie_present: false,
version: mac::FrameVersion::Ieee802154_2003,
version: wire::FrameVersion::Ieee802154_2003,
seq: 0,
destination: Some(mac::Address::Short(
destination: Some(wire::Address::Short(
PanId::broadcast(),
ShortAddress::BROADCAST,
)),
source: None,
auxiliary_security_header: None,
},
content: mac::FrameContent::Command(
mac::command::Command::BeaconRequest,
content: wire::FrameContent::Command(
wire::command::Command::BeaconRequest,
),
payload: &[],
footer: [0, 0],
Expand Down Expand Up @@ -395,13 +396,13 @@ async fn own_superframe_start(
phy: &mut impl Phy,
start_time: Instant,
) {
use ieee802154::mac;
use crate::wire;

let has_broadcast_scheduled = mac_state.message_scheduler.has_broadcast_scheduled();
mac_state.own_superframe_active = true;
let beacon_frame = mac::Frame {
header: mac::Header {
frame_type: mac::FrameType::Beacon,
let beacon_frame = wire::Frame {
header: wire::Header {
frame_type: wire::FrameType::Beacon,
frame_pending: has_broadcast_scheduled,
ack_request: false,
pan_id_compress: false,
Expand All @@ -411,14 +412,14 @@ async fn own_superframe_start(
seq: mac_pib.bsn.increment(),
destination: None,
source: Some(if mac_pib.short_address == ShortAddress(0xFFFE) {
mac::Address::Extended(mac_pib.pan_id, mac_pib.extended_address)
wire::Address::Extended(mac_pib.pan_id, mac_pib.extended_address)
} else {
mac::Address::Short(mac_pib.pan_id, mac_pib.short_address)
wire::Address::Short(mac_pib.pan_id, mac_pib.short_address)
}),
auxiliary_security_header: mac_state.beacon_security_info.into(),
},
content: mac::FrameContent::Beacon(mac::beacon::Beacon {
superframe_spec: mac::beacon::SuperframeSpecification {
content: wire::FrameContent::Beacon(wire::beacon::Beacon {
superframe_spec: wire::beacon::SuperframeSpecification {
beacon_order: mac_pib.beacon_order,
superframe_order: mac_pib.superframe_order,
final_cap_slot: (crate::consts::NUM_SUPERFRAME_SLOTS
Expand Down
22 changes: 12 additions & 10 deletions src/mac/state.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
use arraydeque::ArrayDeque;
use arrayvec::ArrayVec;
use ieee802154::mac::{
beacon::{GuaranteedTimeSlotInformation, PendingAddress},
security::{default::Unimplemented, SecurityContext},
FooterMode, FrameSerDesContext,
};
use rand_core::RngCore;

use crate::{sap::SecurityInfo, time::DelayNsExt};

use super::{callback::SendCallback, mlme_scan::ScanProcess, MacConfig};
use crate::{
sap::SecurityInfo,
time::DelayNsExt,
wire::{
beacon::{GuaranteedTimeSlotInformation, PendingAddress},
security::{default::Unimplemented, SecurityContext},
FooterMode, FrameSerDesContext,
},
};

pub struct MacState<'a> {
pub message_scheduler: MessageScheduler<'a>,
Expand Down Expand Up @@ -54,7 +56,7 @@ impl MacState<'_> {

pub fn serialize_frame(
&mut self,
frame: ieee802154::mac::Frame<'_>,
frame: crate::wire::Frame<'_>,
) -> ArrayVec<u8, { crate::consts::MAX_PHY_PACKET_SIZE }> {
use byte::TryWrite;

Expand All @@ -70,8 +72,8 @@ impl MacState<'_> {
pub fn deserialize_frame<'data>(
&mut self,
data: &'data mut [u8],
) -> Option<ieee802154::mac::Frame<'data>> {
match ieee802154::mac::Frame::try_read_and_unsecure(
) -> Option<crate::wire::Frame<'data>> {
match crate::wire::Frame::try_read_and_unsecure(
data,
&mut self.frame_ser_des_context(),
&mut Unimplemented,
Expand Down
6 changes: 2 additions & 4 deletions src/phy/dw1000.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use core::fmt::{Debug, Display};

pub use dw1000;
use dw1000::{
configs::PulseRepetitionFrequency, AutoDoubleBufferReceiving, Ready, RxConfig, TxConfig,
};
Expand All @@ -9,6 +10,7 @@ use embedded_hal_async::{delay::DelayNs, digital::Wait};
#[allow(unused_imports)]
use micromath::F32Ext;

use super::{ModulationType, Phy, ReceivedMessage};
use crate::{
phy::SendContinuation,
pib::{
Expand All @@ -19,10 +21,6 @@ use crate::{
ChannelPage,
};

use super::{ModulationType, Phy, ReceivedMessage};

pub use dw1000;

const TIME_CHECK_INTERVAL_MILLIS: u32 = 5000;
const TIME_CHECK_MILLIS_PER_DELAY: u32 = 100;

Expand Down
7 changes: 4 additions & 3 deletions src/pib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use core::num::{NonZero, NonZeroU32};

use ieee802154::mac::beacon::{BeaconOrder, SuperframeOrder};
use ieee802154::mac::{ExtendedAddress, PanId, ShortAddress};

use crate::{
consts::{MAX_BEACON_PAYLOAD_LENGTH, TURNAROUND_TIME, UNIT_BACKOFF_PERIOD},
sap::Status,
wire::{
beacon::{BeaconOrder, SuperframeOrder},
ExtendedAddress, PanId, ShortAddress,
},
ChannelPage,
};

Expand Down
9 changes: 4 additions & 5 deletions src/sap/associate.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use ieee802154::mac::{
command::{AssociationStatus, CapabilityInformation},
Address, ExtendedAddress, ShortAddress,
};

use super::{
ConfirmValue, Indication, IndicationValue, Request, RequestValue, ResponseValue, SecurityInfo,
Status,
};
use crate::wire::{
command::{AssociationStatus, CapabilityInformation},
Address, ExtendedAddress, ShortAddress,
};

/// The MLME-ASSOCIATE.request primitive is used by a device to request an association with a coordinator.
///
Expand Down
4 changes: 1 addition & 3 deletions src/sap/beacon_notify.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use arrayvec::ArrayVec;
use ieee802154::mac::beacon::PendingAddress;

use crate::consts::MAX_BEACON_PAYLOAD_LENGTH;

use super::{Indication, IndicationValue, PanDescriptor};
use crate::{consts::MAX_BEACON_PAYLOAD_LENGTH, wire::beacon::PendingAddress};

/// The MLME-BEACON-NOTIFY.indication primitive is used to send parameters contained within a beacon
/// frame received by the MAC sublayer to the next higher layer when either `macAutoRequest` is set to FALSE
Expand Down
Loading

0 comments on commit ad89067

Please sign in to comment.