Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement nwk layer #4

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 137 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "zigbee-rs"
version = "0.1.0"
authors = ["Stürmer, Benjamin <[email protected]>"]
edition = "2018"
rust-version = "1.31"
rust-version = "1.50"

description = "ZigBee protocol stack based on the [ZigBee specification 22 1.0](https://csa-iot.org/wp-content/uploads/2022/01/docs-05-3474-22-0csg-zigbee-specification-1.pdf)"
documentation = "https://docs.rs/zigbee-rs"
Expand All @@ -16,4 +16,9 @@ license = "MIT OR Apache-2.0"


[dependencies]
heapless = "0.8.0"
packed_struct = { version = "0.10", default-features = false }

[dev-dependencies]
hex = "0.4.3"

7 changes: 4 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
//!
//! ZigBee is a protocol stack based on the ZigBee specification 22 1.0
//!
mod lib {

}
#![no_std]
mod spec;
pub use spec::*;

pub(crate) mod parse;
95 changes: 95 additions & 0 deletions src/parse/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
use heapless::Vec;

pub trait PackBytes
where
Self: Sized,
{
fn unpack_from_iter(src: impl IntoIterator<Item = u8>) -> Option<Self>;
fn unpack_from_slice(src: &[u8]) -> Option<Self> {
Self::unpack_from_iter(src.iter().cloned())
}
}

impl PackBytes for u8 {
fn unpack_from_iter(src: impl IntoIterator<Item = u8>) -> Option<Self> {
src.into_iter().next()
}
}

impl PackBytes for i8 {
fn unpack_from_iter(src: impl IntoIterator<Item = u8>) -> Option<Self> {
src.into_iter().next().map(|b| b as i8)
}
}

macro_rules! impl_primitive {
($ty:ty, $sz:literal) => {
impl PackBytes for $ty {
fn unpack_from_iter(src: impl IntoIterator<Item = u8>) -> Option<Self> {
let buf: Vec<u8, $sz> = src.into_iter().take($sz).collect();
Some(<$ty>::from_be_bytes(buf.into_array().unwrap()))
}
}
};
}

impl_primitive!(u16, 2);
impl_primitive!(u32, 4);
impl_primitive!(u64, 8);
impl_primitive!(i16, 2);
impl_primitive!(i32, 4);
impl_primitive!(i64, 8);

#[macro_export]
macro_rules! impl_pack_bytes {
(
$(#[$m:meta])*
$v:vis struct $name:ident($ty:ty);
) => {
$(#[$m])*
$v struct $name($ty);

impl $crate::parse::PackBytes for $name {
fn unpack_from_iter(src: impl IntoIterator<Item = u8>) -> Option<Self> {
Some(Self(PackBytes::unpack_from_iter(src)?))
}
}
};
(
$(#[$m:meta])*
$v:vis struct $name:ident {
$(
$(#[doc = $doc:literal])*
$(#[transparent($tp_ty:ty)])?
$(#[collect($cl_ty:ty)])?
$vf:vis $field_name:ident: $field_ty:ty
),+
$(,)?
}
) => {
$(#[$m])*
$v struct $name{
$(
$(#[doc = $doc])*
$vf $field_name: $field_ty
),+
}

impl $crate::parse::PackBytes for $name {
fn unpack_from_iter(src: impl IntoIterator<Item = u8>) -> Option<Self> {
use core::iter::FromIterator;
let mut src = src.into_iter();
Some(Self {
$(
$(
$field_name: <$tp_ty>::unpack_from_iter(&mut src)?
)?
$(
$field_name: <$cl_ty>::from_iter(src)
)?
),+
})
}
}
}
}
1 change: 1 addition & 0 deletions src/spec/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod nwk;
Loading