Skip to content

Commit

Permalink
Macro Updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
Frostie314159 committed Sep 30, 2023
1 parent 7cdc3f7 commit f47bce8
Show file tree
Hide file tree
Showing 16 changed files with 440 additions and 761 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ harness = false

[dependencies]
bin-utils = "0.2.1"
heapless = "0.7.16"
mac-parser = { version = "0.1.2", default-features = false }
macro-bits = "0.1.1"
num-integer = { version = "0.1.45", default-features = false }
tlv-rs = { version = "0.2.1", default-features = false }
try_take = "0.1.0"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Do note please, that although the parsers are not yet present, the features are.
## no_std
The library is built with the `alloc` crate, which makes using it in a no_std environment with an allocator possible.
## Credits
Although the actual parser was written by me, the reverse engineering of the AWDL protocol was conducted by Milan Stute and SeeMoo-Lab. So kudos to them..
Although the actual parser was written by me, the reverse engineering of the AWDL protocol was conducted by Milan Stute and SeeMoo-Lab. So kudos to them...
- https://tuprints.ulb.tu-darmstadt.de/11457/1/dissertation_milan-stute_2020.pdf
- https://github.com/seemoo-lab/owl
- https://owlink.org/
82 changes: 23 additions & 59 deletions src/common/awdl_dns_compression.rs
Original file line number Diff line number Diff line change
@@ -1,77 +1,41 @@
use bin_utils::*;
use macro_bits::serializable_enum;

#[non_exhaustive]
#[cfg_attr(feature = "debug", derive(Debug))]
#[derive(Clone, Copy, Default, PartialEq, Eq)]
/// Compressed dns-sd domains/services. Compression might be the wrong word though.
pub enum AWDLDnsCompression {
Null,
serializable_enum! {
#[cfg_attr(feature = "debug", derive(Debug))]
#[derive(Clone, Copy, Default, PartialEq, Eq)]
/// Compressed dns-sd domains/services. Compression might be the wrong word though.
pub enum AWDLDnsCompression: u16 {
Null => 0xC000,

AirPlayTcpLocal,
AirPlayTcpLocal => 0xC001,

AirPlayUdpLocal,
AirPlayUdpLocal => 0xC002,

AirPlay,
AirPlay => 0xC003,

RaopTcpLocal,
RaopTcpLocal => 0xC004,

RaopUdpLocal,
RaopUdpLocal => 0xC005,

Raop,
Raop => 0xC006,

AirDropTcpLocal,
AirDropTcpLocal => 0xC007,

AirDropUdpLocal,
AirDropUdpLocal => 0xC008,

AirDrop,
AirDrop => 0xC009,

TcpLocal,
TcpLocal => 0xC00A,

UdpLocal,
UdpLocal => 0xC00B,

#[default]
Local,
#[default]
Local => 0xC00C,

Ip6Arpa,
Ip6Arpa => 0xC00D,

Ip4Arpa,

Unknown(u16),
}
enum_to_int! {
u16,
AWDLDnsCompression,

0xC000,
AWDLDnsCompression::Null,
0xC001,
AWDLDnsCompression::AirPlayTcpLocal,
0xC002,
AWDLDnsCompression::AirPlayUdpLocal,
0xC003,
AWDLDnsCompression::AirPlay,
0xC004,
AWDLDnsCompression::RaopTcpLocal,
0xC005,
AWDLDnsCompression::RaopUdpLocal,
0xC006,
AWDLDnsCompression::Raop,
0xC007,
AWDLDnsCompression::AirDropTcpLocal,
0xC008,
AWDLDnsCompression::AirDropUdpLocal,
0xC009,
AWDLDnsCompression::AirDrop,
0xC00A,
AWDLDnsCompression::TcpLocal,
0xC00B,
AWDLDnsCompression::UdpLocal,
0xC00C,
AWDLDnsCompression::Local,
0xC00D,
AWDLDnsCompression::Ip6Arpa,
0xC00E,
AWDLDnsCompression::Ip4Arpa
Ip4Arpa => 0xC00E
}
}
impl AWDLDnsCompression {
pub fn to_string(&self) -> &'static str {
Expand Down
35 changes: 11 additions & 24 deletions src/common/awdl_version.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
#[cfg(feature = "debug")]
use core::fmt::Debug;

#[derive(Clone, Copy, PartialEq, Eq)]
/// A version in AWDL format.
pub struct AWDLVersion {
/// The major version.
pub major: u8,
use macro_bits::bitfield;

/// The minor version.
pub minor: u8,
bitfield! {
#[derive(Clone, Copy, PartialEq, Eq)]
/// A version in AWDL format.
pub struct AWDLVersion: u8 {
/// The major version.
pub major: u8 => 0xf0,

/// The minor version.
pub minor: u8 => 0x0f
}
}
impl PartialOrd for AWDLVersion {
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
Expand All @@ -27,20 +31,3 @@ impl Debug for AWDLVersion {
f.write_str(&alloc::format!("{}.{}", self.major, self.minor))
}
}
#[cfg(feature = "read")]
impl From<u8> for AWDLVersion {
#[inline]
fn from(value: u8) -> Self {
Self {
major: (value >> 4) & 0xf,
minor: value & 0xf,
}
}
}
#[cfg(feature = "write")]
impl From<AWDLVersion> for u8 {
#[inline]
fn from(value: AWDLVersion) -> Self {
(value.major << 4) | value.minor
}
}
24 changes: 0 additions & 24 deletions src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,3 @@ where
})
.zip(0..)
}
macro_rules! bit {
($index:expr) => {
(1 << $index)
};
($($index:expr), +) => {
$(bit!($index) | )+ 0
};
}
macro_rules! check_bit {
($flags:expr, $mask:expr) => {
($flags & $mask != 0x0)
};
}
macro_rules! set_bit {
($flags:expr, $mask:expr) => {
$flags |= $mask
};
($flags:expr, $mask:expr, $value:expr) => {
if $value {
set_bit!($flags, $mask);
}
};
}
pub(crate) use {bit, check_bit, set_bit};
14 changes: 14 additions & 0 deletions src/data_frame.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use alloc::vec::Vec;

use crate::tlvs::AWDLTLV;

pub struct ExtendedHeader {
pub tlvs: Vec<AWDLTLV>,
pub header_data: Vec<u8>,
}
pub struct AWDLDataFrame {
pub sequence_number: u16,
pub header_type: u16,
pub ext_header: Option<ExtendedHeader>,
pub payload: Vec<u8>,
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ extern crate alloc;
pub mod action_frame;
/// Structures, which are shared between multiple TLVs and frame types.
pub mod common;
pub mod data_frame;
/// Every TLV currently understood.
pub mod tlvs;
pub use heapless;
46 changes: 15 additions & 31 deletions src/tlvs/data/data_path_state_tlv/misc.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
use bin_utils::*;

use crate::common::{bit, check_bit, set_bit};

pub const CHANNEL_6: u16 = bit!(0);
pub const CHANNEL_44: u16 = bit!(1);
pub const CHANNEL_149: u16 = bit!(2);
use macro_bits::{bit, bitfield};

#[cfg_attr(feature = "debug", derive(Debug))]
#[derive(Default, Clone)]
#[derive(Clone, Default, PartialEq)]
pub struct DataPathMisc {
pub msec_since_activation: u32,
pub aw_seq_counter: u32,
Expand Down Expand Up @@ -36,17 +31,20 @@ impl WriteFixed<12> for DataPathMisc {
.unwrap()
}
}
bitfield! {
#[cfg_attr(feature = "debug", derive(Debug))]
#[derive(Clone, Copy, Default, Eq, PartialEq)]
pub struct ChannelMap: u16 {
pub channel_6: bool => bit!(0),
pub channel_44: bool => bit!(1),
pub channel_149: bool => bit!(2)
}
}
#[cfg_attr(feature = "debug", derive(Debug))]
#[derive(Clone, Copy, PartialEq)]
pub enum DataPathChannel {
SingleChannel {
channel: u8,
},
ChannelMap {
channel_6: bool,
channel_44: bool,
channel_149: bool,
},
SingleChannel { channel: u8 },
ChannelMap(ChannelMap),
}
impl DataPathChannel {
pub fn from_u16(value: u16, single_channel: bool) -> Self {
Expand All @@ -55,27 +53,13 @@ impl DataPathChannel {
channel: value as u8,
}
} else {
Self::ChannelMap {
channel_6: check_bit!(value, CHANNEL_6),
channel_44: check_bit!(value, CHANNEL_44),
channel_149: check_bit!(value, CHANNEL_149),
}
Self::ChannelMap(ChannelMap::from_representation(value))
}
}
pub fn as_u16(&self) -> u16 {
match *self {
DataPathChannel::SingleChannel { channel } => channel as u16,
DataPathChannel::ChannelMap {
channel_6,
channel_44,
channel_149,
} => {
let mut channel_map = 0;
set_bit!(channel_map, CHANNEL_6, channel_6);
set_bit!(channel_map, CHANNEL_44, channel_44);
set_bit!(channel_map, CHANNEL_149, channel_149);
channel_map
}
DataPathChannel::ChannelMap(channel_map) => channel_map.to_representation(),
}
}
}
Loading

0 comments on commit f47bce8

Please sign in to comment.