diff --git a/Cargo.toml b/Cargo.toml index 867adc5..91f3c34 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,5 +2,5 @@ resolver = "2" -members = ["dot15d4", "dot15d4-cat", "dot15d4-macros"] +members = ["dot15d4", "dot15d4-cat", "dot15d4-frame", "dot15d4-macros"] exclude = ["ensure-no-std", "dot15d4-fuzz"] diff --git a/dot15d4-cat/Cargo.toml b/dot15d4-cat/Cargo.toml index 4590246..cdc56e1 100644 --- a/dot15d4-cat/Cargo.toml +++ b/dot15d4-cat/Cargo.toml @@ -7,7 +7,7 @@ license = "MIT OR Apache-2.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -dot15d4 = { path = "../dot15d4" } +dot15d4-frame = { path = "../dot15d4-frame" } colored = "2" clap = { version = "4.5.1", features = ["derive"] } diff --git a/dot15d4-cat/src/main.rs b/dot15d4-cat/src/main.rs index cd631cf..48f42dc 100644 --- a/dot15d4-cat/src/main.rs +++ b/dot15d4-cat/src/main.rs @@ -1,6 +1,6 @@ use clap::Parser; use colored::*; -use dot15d4::frame::*; +use dot15d4_frame::*; struct Writer { indent: usize, diff --git a/dot15d4-frame/Cargo.toml b/dot15d4-frame/Cargo.toml new file mode 100644 index 0000000..c656e74 --- /dev/null +++ b/dot15d4-frame/Cargo.toml @@ -0,0 +1,23 @@ +[package] +name = "dot15d4-frame" +version = "0.1.0" +edition = "2021" +license = "MIT OR Apache-2.0" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +dot15d4-macros = { path = "../dot15d4-macros" } + +bitflags = "2.4.2" +heapless = "0.8.0" + +arbitrary = { version = "1.3.2", features = ["derive"], optional = true } + +[dev-dependencies] +env_logger = "0.11.3" +log = "0.4.21" + +[features] +std = [] +fuzz = ["arbitrary"] diff --git a/dot15d4/examples/parsing.rs b/dot15d4-frame/examples/parsing.rs similarity index 97% rename from dot15d4/examples/parsing.rs rename to dot15d4-frame/examples/parsing.rs index 9ad19c8..5d4808b 100644 --- a/dot15d4/examples/parsing.rs +++ b/dot15d4-frame/examples/parsing.rs @@ -1,4 +1,4 @@ -use dot15d4::frame::*; +use dot15d4_frame::*; fn main() { env_logger::init(); diff --git a/dot15d4/src/frame/addressing.rs b/dot15d4-frame/src/addressing.rs similarity index 100% rename from dot15d4/src/frame/addressing.rs rename to dot15d4-frame/src/addressing.rs diff --git a/dot15d4/src/frame/aux_sec_header.rs b/dot15d4-frame/src/aux_sec_header.rs similarity index 100% rename from dot15d4/src/frame/aux_sec_header.rs rename to dot15d4-frame/src/aux_sec_header.rs diff --git a/dot15d4/src/frame/frame_control.rs b/dot15d4-frame/src/frame_control.rs similarity index 100% rename from dot15d4/src/frame/frame_control.rs rename to dot15d4-frame/src/frame_control.rs diff --git a/dot15d4/src/frame/ie/headers.rs b/dot15d4-frame/src/ie/headers.rs similarity index 99% rename from dot15d4/src/frame/ie/headers.rs rename to dot15d4-frame/src/ie/headers.rs index 8ca2243..e1a58ba 100644 --- a/dot15d4/src/frame/ie/headers.rs +++ b/dot15d4-frame/src/ie/headers.rs @@ -1,7 +1,7 @@ //! IEEE 802.15.4 Header Information Element reader and writers. -use crate::frame::{Error, Result}; use crate::time::Duration; +use crate::{Error, Result}; use dot15d4_macros::frame; /// A reader/writer for the IEEE 802.15.4 Header Information Elements diff --git a/dot15d4/src/frame/ie/mod.rs b/dot15d4-frame/src/ie/mod.rs similarity index 100% rename from dot15d4/src/frame/ie/mod.rs rename to dot15d4-frame/src/ie/mod.rs diff --git a/dot15d4/src/frame/ie/nested.rs b/dot15d4-frame/src/ie/nested.rs similarity index 100% rename from dot15d4/src/frame/ie/nested.rs rename to dot15d4-frame/src/ie/nested.rs diff --git a/dot15d4/src/frame/ie/payloads.rs b/dot15d4-frame/src/ie/payloads.rs similarity index 100% rename from dot15d4/src/frame/ie/payloads.rs rename to dot15d4-frame/src/ie/payloads.rs diff --git a/dot15d4/src/frame/mod.rs b/dot15d4-frame/src/lib.rs similarity index 99% rename from dot15d4/src/frame/mod.rs rename to dot15d4-frame/src/lib.rs index d3aac7c..7d6f1b6 100644 --- a/dot15d4/src/frame/mod.rs +++ b/dot15d4-frame/src/lib.rs @@ -19,7 +19,7 @@ //! ## Reading a frame //! For an incoming frame, use the [`Frame`] structure to read its content. //! ``` -//! # use dot15d4::frame::{ +//! # use dot15d4_frame::{ //! # Frame, //! # FrameControl, //! # FrameType, @@ -175,9 +175,13 @@ //! [`payload_information_elements`]: InformationElements::payload_information_elements //! [`nested_information_elements`]: PayloadInformationElement::nested_information_elements +#![cfg_attr(not(any(test, feature = "std")), no_std)] + #[cfg(test)] mod tests; +mod time; + mod frame_control; pub use frame_control::*; diff --git a/dot15d4/src/frame/repr/addressing.rs b/dot15d4-frame/src/repr/addressing.rs similarity index 100% rename from dot15d4/src/frame/repr/addressing.rs rename to dot15d4-frame/src/repr/addressing.rs diff --git a/dot15d4/src/frame/repr/aux_sec_header.rs b/dot15d4-frame/src/repr/aux_sec_header.rs similarity index 100% rename from dot15d4/src/frame/repr/aux_sec_header.rs rename to dot15d4-frame/src/repr/aux_sec_header.rs diff --git a/dot15d4/src/frame/repr/builder.rs b/dot15d4-frame/src/repr/builder.rs similarity index 99% rename from dot15d4/src/frame/repr/builder.rs rename to dot15d4-frame/src/repr/builder.rs index 81dd6d2..1b561dd 100644 --- a/dot15d4/src/frame/repr/builder.rs +++ b/dot15d4-frame/src/repr/builder.rs @@ -1,6 +1,6 @@ use super::*; -use crate::frame::{Address, AddressingMode, FrameType, FrameVersion}; -use crate::frame::{Error, Result}; +use crate::{Address, AddressingMode, FrameType, FrameVersion}; +use crate::{Error, Result}; pub struct Beacon; pub struct EnhancedBeacon; diff --git a/dot15d4/src/frame/repr/frame_control.rs b/dot15d4-frame/src/repr/frame_control.rs similarity index 100% rename from dot15d4/src/frame/repr/frame_control.rs rename to dot15d4-frame/src/repr/frame_control.rs diff --git a/dot15d4/src/frame/repr/ie/headers.rs b/dot15d4-frame/src/repr/ie/headers.rs similarity index 100% rename from dot15d4/src/frame/repr/ie/headers.rs rename to dot15d4-frame/src/repr/ie/headers.rs diff --git a/dot15d4/src/frame/repr/ie/mod.rs b/dot15d4-frame/src/repr/ie/mod.rs similarity index 100% rename from dot15d4/src/frame/repr/ie/mod.rs rename to dot15d4-frame/src/repr/ie/mod.rs diff --git a/dot15d4/src/frame/repr/ie/nested.rs b/dot15d4-frame/src/repr/ie/nested.rs similarity index 100% rename from dot15d4/src/frame/repr/ie/nested.rs rename to dot15d4-frame/src/repr/ie/nested.rs diff --git a/dot15d4/src/frame/repr/ie/payloads.rs b/dot15d4-frame/src/repr/ie/payloads.rs similarity index 100% rename from dot15d4/src/frame/repr/ie/payloads.rs rename to dot15d4-frame/src/repr/ie/payloads.rs diff --git a/dot15d4/src/frame/repr/mod.rs b/dot15d4-frame/src/repr/mod.rs similarity index 100% rename from dot15d4/src/frame/repr/mod.rs rename to dot15d4-frame/src/repr/mod.rs diff --git a/dot15d4/src/frame/tests.rs b/dot15d4-frame/src/tests.rs similarity index 100% rename from dot15d4/src/frame/tests.rs rename to dot15d4-frame/src/tests.rs diff --git a/dot15d4-frame/src/time.rs b/dot15d4-frame/src/time.rs new file mode 100644 index 0000000..b0656bd --- /dev/null +++ b/dot15d4-frame/src/time.rs @@ -0,0 +1,102 @@ +//! Time structures. +//! +//! - [`Instant`] is used to represent a point in time. +//! - [`Duration`] is used to represent a duration of time. + +#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Eq, Ord)] +#[cfg_attr(feature = "fuzz", derive(arbitrary::Arbitrary))] +pub struct Instant { + us: i64, +} + +impl Instant { + pub const fn from_us(us: i64) -> Self { + Self { us } + } + + pub const fn as_us(&self) -> i64 { + self.us + } +} + +impl core::fmt::Display for Instant { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + write!(f, "{:.2}ms", self.as_us() as f32 / 1000.0) + } +} + +#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Eq, Ord)] +#[cfg_attr(feature = "fuzz", derive(arbitrary::Arbitrary))] +pub struct Duration(i64); + +impl Duration { + pub const fn from_us(us: i64) -> Self { + Self(us) + } + + pub const fn as_us(&self) -> i64 { + self.0 + } +} + +impl core::fmt::Display for Duration { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + write!(f, "{:.2}ms", self.as_us() as f32 / 1000.0) + } +} + +impl core::ops::Sub for Instant { + type Output = Self; + + fn sub(self, rhs: Instant) -> Self::Output { + Self::from_us(self.as_us() - rhs.as_us()) + } +} + +impl core::ops::Sub for Instant { + type Output = Self; + + fn sub(self, rhs: Duration) -> Self::Output { + Self::from_us(self.us - rhs.as_us()) + } +} + +impl core::ops::Sub for Duration { + type Output = Self; + + fn sub(self, rhs: Duration) -> Self::Output { + Self::from_us(self.as_us() - rhs.as_us()) + } +} + +impl core::ops::Mul for Duration { + type Output = Self; + + fn mul(self, rhs: usize) -> Self::Output { + Self::from_us(self.as_us() * rhs as i64) + } +} + +impl core::ops::Add for Instant { + type Output = Self; + + fn add(self, rhs: Duration) -> Self::Output { + Self::from_us(self.us + rhs.as_us()) + } +} + +impl core::ops::Div for Duration { + type Output = Self; + + fn div(self, rhs: usize) -> Self::Output { + Self::from_us(self.as_us() / rhs as i64) + } +} + +impl core::ops::Add for Duration { + type Output = Self; + + fn add(self, rhs: Duration) -> Self::Output { + Self::from_us(self.as_us() + rhs.as_us()) + } +} diff --git a/dot15d4-fuzz/Cargo.toml b/dot15d4-fuzz/Cargo.toml index c7da94b..e0bf6f9 100644 --- a/dot15d4-fuzz/Cargo.toml +++ b/dot15d4-fuzz/Cargo.toml @@ -9,4 +9,4 @@ license = "MIT OR Apache-2.0" [dependencies] afl = "0.15.3" arbitrary = { version = "1.3.2", features = ["derive"] } -dot15d4 = { path = "../dot15d4", features = ["std", "fuzz"] } +dot15d4-frame = { path = "../dot15d4-frame", features = ["std", "fuzz"] } diff --git a/dot15d4-fuzz/src/bin/frame.rs b/dot15d4-fuzz/src/bin/frame.rs index fb2b1bc..d996128 100644 --- a/dot15d4-fuzz/src/bin/frame.rs +++ b/dot15d4-fuzz/src/bin/frame.rs @@ -1,5 +1,5 @@ use afl::*; -use dot15d4::frame::{Frame, FrameRepr}; +use dot15d4_frame::{Frame, FrameRepr}; fn main() { fuzz!(|data: &[u8]| { diff --git a/dot15d4-fuzz/src/bin/repr.rs b/dot15d4-fuzz/src/bin/repr.rs index d339976..2b253f2 100644 --- a/dot15d4-fuzz/src/bin/repr.rs +++ b/dot15d4-fuzz/src/bin/repr.rs @@ -1,5 +1,5 @@ use afl::*; -use dot15d4::frame::{Frame, FrameRepr}; +use dot15d4_frame::{Frame, FrameRepr}; fn main() { fuzz!(|repr: FrameRepr| { diff --git a/dot15d4/Cargo.toml b/dot15d4/Cargo.toml index 4f56568..5696243 100644 --- a/dot15d4/Cargo.toml +++ b/dot15d4/Cargo.toml @@ -7,19 +7,15 @@ license = "MIT OR Apache-2.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -dot15d4-macros = { path = "../dot15d4-macros" } +dot15d4-frame = { path = "../dot15d4-frame" } log = { version = "0.4.21", optional = true } defmt = { version = "0.3", optional = true } -bitflags = "2.4.2" -heapless = "0.8.0" critical-section = "1.1" rand_core = { version = "0.6.4", default-features = false } embedded-hal-async = { version = "1.0.0" } -arbitrary = { version = "1.3.2", features = ["derive"], optional = true } - [dev-dependencies] critical-section = { version = "1.1", features = ["std"] } env_logger = "0.11.3" @@ -35,5 +31,3 @@ default = ["std"] log = ["dep:log"] ## Use defmt for logging defmt = ["dep:defmt"] - -fuzz = ["std", "arbitrary"] diff --git a/dot15d4/src/csma/mod.rs b/dot15d4/src/csma/mod.rs index 76ea835..e395bef 100644 --- a/dot15d4/src/csma/mod.rs +++ b/dot15d4/src/csma/mod.rs @@ -9,7 +9,6 @@ use rand_core::RngCore; use user_configurable_constants::*; use crate::{ - frame::{Address, Frame, FrameBuilder, FrameType}, phy::{ config::{RxConfig, TxConfig}, driver::{self, Driver, FrameBuffer}, @@ -28,6 +27,7 @@ use crate::{ }, time::Duration, }; +use dot15d4_frame::{Address, Frame, FrameBuilder, FrameType}; #[cfg_attr(feature = "defmt", derive(defmt::Format))] #[derive(Debug, Clone, Copy)] diff --git a/dot15d4/src/lib.rs b/dot15d4/src/lib.rs index 5ce8654..8cfb8c1 100644 --- a/dot15d4/src/lib.rs +++ b/dot15d4/src/lib.rs @@ -3,8 +3,9 @@ #[macro_use] pub(crate) mod utils; +pub use dot15d4_frame as frame; + pub mod csma; -pub mod frame; pub mod phy; pub mod sync; pub mod time;