Skip to content

Fix emit inconsitencies and add tests #30

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

Open
wants to merge 1 commit 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
84 changes: 65 additions & 19 deletions src/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,11 @@ impl Emitable for Nl80211Element {
v.as_slice().iter().map(|v| u8::from(*v)).collect();
payload.copy_from_slice(raw.as_slice());
}
Self::Channel(v) => buffer[0] = *v,
Self::Country(v) => v.emit(buffer),
Self::Rsn(v) => v.emit(buffer),
Self::Vendor(v) => buffer[..v.len()].copy_from_slice(v.as_slice()),
Self::HtCapability(v) => v.emit(buffer),
Self::Channel(v) => payload[0] = *v,
Self::Country(v) => v.emit(payload),
Self::Rsn(v) => v.emit(payload),
Self::Vendor(v) => payload[..v.len()].copy_from_slice(v.as_slice()),
Self::HtCapability(v) => v.emit(payload),
Self::Other(_, data) => {
payload.copy_from_slice(data.as_slice());
}
Expand All @@ -193,9 +193,10 @@ const BSS_MEMBERSHIP_SELECTOR_HT_PHY: u8 = 127;
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[non_exhaustive]
pub enum Nl80211RateAndSelector {
/// BSS basic rate set in Mb/s.
/// BSS basic rate in units of 500 kb/s, if necessary rounded up to the
/// next 500 kbs.
BssBasicRateSet(u8),
/// Rate in Mb/s.
/// Rate in units of 500 kb/s, if necessary rounded up to the next 500 kbs.
Rate(u8),
SelectorHt,
SelectorVht,
Expand All @@ -216,43 +217,45 @@ pub enum Nl80211RateAndSelector {

impl From<u8> for Nl80211RateAndSelector {
fn from(d: u8) -> Self {
let msb: bool = (d & 1 << 7) > 0;
let value = d & 0b01111111;
const MSB_MASK: u8 = 0b1000_0000;
let msb: bool = (d & MSB_MASK) == MSB_MASK;
let value = d & !MSB_MASK;
if msb {
match value {
BSS_MEMBERSHIP_SELECTOR_SAE_HASH => Self::SelectorSaeHash,
BSS_MEMBERSHIP_SELECTOR_EPD => Self::SelectorEpd,
BSS_MEMBERSHIP_SELECTOR_GLK => Self::SelectorGlk,
BSS_MEMBERSHIP_SELECTOR_VHT_PHY => Self::SelectorVht,
BSS_MEMBERSHIP_SELECTOR_HT_PHY => Self::SelectorHt,
_ => Self::BssBasicRateSet(value / 2),
_ => Self::BssBasicRateSet(value),
}
} else {
Self::Rate(value / 2)
Self::Rate(value)
}
}
}

impl From<Nl80211RateAndSelector> for u8 {
fn from(v: Nl80211RateAndSelector) -> u8 {
const MSB: u8 = 0b1000_0000;
match v {
Nl80211RateAndSelector::BssBasicRateSet(r) => (r * 2) & 1 << 7,
Nl80211RateAndSelector::BssBasicRateSet(r) => r & !MSB | MSB,
Nl80211RateAndSelector::SelectorHt => {
BSS_MEMBERSHIP_SELECTOR_HT_PHY & 1 << 7
BSS_MEMBERSHIP_SELECTOR_HT_PHY | MSB
}
Nl80211RateAndSelector::SelectorVht => {
BSS_MEMBERSHIP_SELECTOR_VHT_PHY & 1 << 7
BSS_MEMBERSHIP_SELECTOR_VHT_PHY | MSB
}
Nl80211RateAndSelector::SelectorGlk => {
BSS_MEMBERSHIP_SELECTOR_GLK & 1 << 7
BSS_MEMBERSHIP_SELECTOR_GLK | MSB
}
Nl80211RateAndSelector::SelectorEpd => {
BSS_MEMBERSHIP_SELECTOR_EPD & 1 << 7
BSS_MEMBERSHIP_SELECTOR_EPD | MSB
}
Nl80211RateAndSelector::SelectorSaeHash => {
BSS_MEMBERSHIP_SELECTOR_SAE_HASH & 1 << 7
BSS_MEMBERSHIP_SELECTOR_SAE_HASH | MSB
}
Nl80211RateAndSelector::Rate(r) => r * 2,
Nl80211RateAndSelector::Rate(r) => r,
}
}
}
Expand Down Expand Up @@ -312,7 +315,7 @@ impl Emitable for Nl80211ElementCountry {
buffer[0] = self.country.as_bytes()[0];
buffer[1] = self.country.as_bytes()[1];
}
buffer[3] = self.environment.into();
buffer[2] = self.environment.into();
for (i, triplet) in self.triplets.as_slice().iter().enumerate() {
triplet.emit(&mut buffer[(i + 1) * 3..(i + 2) * 3]);
}
Expand Down Expand Up @@ -991,3 +994,46 @@ impl Nl80211Pmkid {
}
}
}

#[cfg(test)]
mod test {
use super::*;
use crate::macros::test::roundtrip_emit_parse_test;

roundtrip_emit_parse_test!(
ssid,
Nl80211Element,
Nl80211Element::Ssid("test-ssid".to_owned()),
);
roundtrip_emit_parse_test!(
rates_and_selectors,
Nl80211Element,
Nl80211Element::SupportedRatesAndSelectors(vec![
Nl80211RateAndSelector::BssBasicRateSet(1),
Nl80211RateAndSelector::Rate(1),
Nl80211RateAndSelector::SelectorHt,
Nl80211RateAndSelector::SelectorVht,
Nl80211RateAndSelector::SelectorGlk,
])
);
roundtrip_emit_parse_test!(
channel,
Nl80211Element,
Nl80211Element::Channel(7)
);
roundtrip_emit_parse_test!(
country,
Nl80211Element,
Nl80211Element::Country(Nl80211ElementCountry {
country: "DE".to_owned(),
environment: Nl80211ElementCountryEnvironment::IndoorAndOutdoor,
triplets: vec![Nl80211ElementCountryTriplet::Subband(
Nl80211ElementSubBand {
channel_start: 1,
channel_count: 13,
max_power_level: 20,
}
)],
}),
);
}
39 changes: 39 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,42 @@ macro_rules! try_nl80211 {
}
}};
}

#[cfg(test)]
pub(crate) mod test {
#[macro_export]
macro_rules! roundtrip_emit_parse_test {
($name:ident, $ty:ty, $new:expr$(,)?) => {
#[test]
fn $name() {
let val: $ty = $new;

// to check if the type can be emitted to a buffer greater than
// the needed size
let mut buffer = vec![0; val.buffer_len() + 1];
val.emit(buffer.as_mut_slice());

assert_eq!(
<$ty>::parse(&buffer[0..val.buffer_len()]).unwrap(),
val,
);
}
};
}

macro_rules! roundtrip_from_test {
($name:ident, $from:ty => $into:ty, $new:expr$(,)?) => {
#[test]
fn $name() {
let val: $from = $new;

let into: $into = val.into();

assert_eq!(<$from>::from(into), val,);
}
};
}

pub(crate) use roundtrip_emit_parse_test;
pub(crate) use roundtrip_from_test;
}
102 changes: 95 additions & 7 deletions src/wifi4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ impl Emitable for Nl80211HtCaps {
}

fn emit(&self, buffer: &mut [u8]) {
buffer.copy_from_slice(&self.bits().to_ne_bytes())
buffer[0..self.buffer_len()].copy_from_slice(&self.bits().to_ne_bytes())
}
}

Expand Down Expand Up @@ -470,9 +470,9 @@ impl From<Nl80211HtExtendedCap> for [u8; 2] {
fn from(v: Nl80211HtExtendedCap) -> [u8; 2] {
[
v.pco as u8 | (v.pco_trans_time << 1) | (v.mcs_feedback & 0b1) << 7,
((v.mcs_feedback & 0b10) >> 1)
| ((v.support_ht_control as u8) << 1)
| ((v.rd_responder as u8) << 2),
(v.mcs_feedback & 0b11)
| ((v.support_ht_control as u8) << 2)
| ((v.rd_responder as u8) << 3),
]
}
}
Expand Down Expand Up @@ -627,7 +627,7 @@ impl Emitable for Nl80211HtTransmitBeamformingCaps {
}

fn emit(&self, buffer: &mut [u8]) {
buffer.copy_from_slice(&self.bits().to_ne_bytes())
buffer[0..self.buffer_len()].copy_from_slice(&self.bits().to_ne_bytes())
}
}

Expand Down Expand Up @@ -675,6 +675,94 @@ impl Emitable for Nl80211HtAselCaps {
}

fn emit(&self, buffer: &mut [u8]) {
buffer.copy_from_slice(&self.bits().to_ne_bytes())
}
buffer[0..self.buffer_len()].copy_from_slice(&self.bits().to_ne_bytes())
}
}

#[cfg(test)]
mod test {
use super::*;
use crate::macros::test::{roundtrip_emit_parse_test, roundtrip_from_test};

roundtrip_emit_parse_test!(caps, Nl80211HtCaps, Nl80211HtCaps::all());
roundtrip_emit_parse_test!(
asel_caps,
Nl80211HtAselCaps,
Nl80211HtAselCaps::all()
);
roundtrip_emit_parse_test!(
transmit_beamforming_cap,
Nl80211HtTransmitBeamformingCaps,
Nl80211HtTransmitBeamformingCaps::all(),
);

roundtrip_from_test!(tx_params, Nl80211HtTxParameter => u8, Nl80211HtTxParameter {
mcs_set_defined: false,
tx_rx_mcs_set_not_equal: false,
max_spatial_streams: 1,
unequal_modulation_supported: false,
});

roundtrip_from_test!(ht_wiphy_no_ht, Nl80211HtWiphyChannelType => u32, Nl80211HtWiphyChannelType::NoHt);
roundtrip_from_test!(ht_wiphy_ht_20, Nl80211HtWiphyChannelType => u32, Nl80211HtWiphyChannelType::Ht20);
roundtrip_from_test!(ht_wiphy_other, Nl80211HtWiphyChannelType => u32, Nl80211HtWiphyChannelType::Other(NL80211_CHAN_HT40PLUS + 1));

roundtrip_emit_parse_test!(
mcs_info,
Nl80211HtMcsInfo,
Nl80211HtMcsInfo {
rx_mask: [0xA5; IEEE80211_HT_MCS_MASK_LEN],
rx_highest: u16::MAX,
tx_params: Nl80211HtTxParameter {
mcs_set_defined: false,
tx_rx_mcs_set_not_equal: false,
max_spatial_streams: 1,
unequal_modulation_supported: false,
},
},
);

roundtrip_from_test!(a_mpdu_para, Nl80211HtAMpduPara => u8, Nl80211HtAMpduPara {
max_len_exponent: u8::MAX & 0b11,
min_space: u8::MAX & 0b111,
});

roundtrip_from_test!(extend_cap, Nl80211HtExtendedCap => [u8; 2], Nl80211HtExtendedCap {
pco: true,
pco_trans_time: 1,
mcs_feedback: 1,
support_ht_control: true,
rd_responder: true,
});

roundtrip_emit_parse_test!(
cap_mask,
Nl80211ElementHtCap,
Nl80211ElementHtCap {
caps: Nl80211HtCaps::all(),
a_mpdu_para: Nl80211HtAMpduPara {
max_len_exponent: 3,
min_space: 7,
},
mcs_set: Nl80211HtMcsInfo {
rx_mask: [0xA5; IEEE80211_HT_MCS_MASK_LEN],
rx_highest: u16::MAX,
tx_params: Nl80211HtTxParameter {
mcs_set_defined: false,
tx_rx_mcs_set_not_equal: false,
max_spatial_streams: 1,
unequal_modulation_supported: false,
},
},
ht_ext_cap: Nl80211HtExtendedCap {
pco: true,
pco_trans_time: 2,
mcs_feedback: 2,
support_ht_control: true,
rd_responder: true,
},
transmit_beamforming_cap: Nl80211HtTransmitBeamformingCaps::all(),
asel_cap: Nl80211HtAselCaps::all(),
},
);
}