Skip to content

Commit

Permalink
Merge pull request #18 from thvdveld/dot15d4-frame
Browse files Browse the repository at this point in the history
Move frame logic in own crate
  • Loading branch information
thvdveld authored Mar 14, 2024
2 parents ebc64ad + fe6e087 commit 3973869
Show file tree
Hide file tree
Showing 30 changed files with 144 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
2 changes: 1 addition & 1 deletion dot15d4-cat/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
2 changes: 1 addition & 1 deletion dot15d4-cat/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clap::Parser;
use colored::*;
use dot15d4::frame::*;
use dot15d4_frame::*;

struct Writer {
indent: usize,
Expand Down
23 changes: 23 additions & 0 deletions dot15d4-frame/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"]
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use dot15d4::frame::*;
use dot15d4_frame::*;

fn main() {
env_logger::init();
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -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
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 5 additions & 1 deletion dot15d4/src/frame/mod.rs → dot15d4-frame/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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::*;

Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
102 changes: 102 additions & 0 deletions dot15d4-frame/src/time.rs
Original file line number Diff line number Diff line change
@@ -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<Duration> 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<usize> 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<Duration> for Instant {
type Output = Self;

fn add(self, rhs: Duration) -> Self::Output {
Self::from_us(self.us + rhs.as_us())
}
}

impl core::ops::Div<usize> 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<Duration> for Duration {
type Output = Self;

fn add(self, rhs: Duration) -> Self::Output {
Self::from_us(self.as_us() + rhs.as_us())
}
}
2 changes: 1 addition & 1 deletion dot15d4-fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
2 changes: 1 addition & 1 deletion dot15d4-fuzz/src/bin/frame.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use afl::*;
use dot15d4::frame::{Frame, FrameRepr};
use dot15d4_frame::{Frame, FrameRepr};

fn main() {
fuzz!(|data: &[u8]| {
Expand Down
2 changes: 1 addition & 1 deletion dot15d4-fuzz/src/bin/repr.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use afl::*;
use dot15d4::frame::{Frame, FrameRepr};
use dot15d4_frame::{Frame, FrameRepr};

fn main() {
fuzz!(|repr: FrameRepr| {
Expand Down
8 changes: 1 addition & 7 deletions dot15d4/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -35,5 +31,3 @@ default = ["std"]
log = ["dep:log"]
## Use defmt for logging
defmt = ["dep:defmt"]

fuzz = ["std", "arbitrary"]
2 changes: 1 addition & 1 deletion dot15d4/src/csma/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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)]
Expand Down
3 changes: 2 additions & 1 deletion dot15d4/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 3973869

Please sign in to comment.