@@ -172,11 +172,11 @@ impl Emitable for Nl80211Element {
172172 v. as_slice ( ) . iter ( ) . map ( |v| u8:: from ( * v) ) . collect ( ) ;
173173 payload. copy_from_slice ( raw. as_slice ( ) ) ;
174174 }
175- Self :: Channel ( v) => buffer [ 0 ] = * v,
176- Self :: Country ( v) => v. emit ( buffer ) ,
177- Self :: Rsn ( v) => v. emit ( buffer ) ,
178- Self :: Vendor ( v) => buffer [ ..v. len ( ) ] . copy_from_slice ( v. as_slice ( ) ) ,
179- Self :: HtCapability ( v) => v. emit ( buffer ) ,
175+ Self :: Channel ( v) => payload [ 0 ] = * v,
176+ Self :: Country ( v) => v. emit ( payload ) ,
177+ Self :: Rsn ( v) => v. emit ( payload ) ,
178+ Self :: Vendor ( v) => payload [ ..v. len ( ) ] . copy_from_slice ( v. as_slice ( ) ) ,
179+ Self :: HtCapability ( v) => v. emit ( payload ) ,
180180 Self :: Other ( _, data) => {
181181 payload. copy_from_slice ( data. as_slice ( ) ) ;
182182 }
@@ -193,9 +193,9 @@ const BSS_MEMBERSHIP_SELECTOR_HT_PHY: u8 = 127;
193193#[ derive( Debug , PartialEq , Eq , Clone , Copy ) ]
194194#[ non_exhaustive]
195195pub enum Nl80211RateAndSelector {
196- /// BSS basic rate set in Mb/s .
196+ /// BSS basic rate in units of 500 kb/s, if necessary rounded up to the next 500 kbs .
197197 BssBasicRateSet ( u8 ) ,
198- /// Rate in Mb/s .
198+ /// Rate in units of 500 kb/s, if necessary rounded up to the next 500 kbs .
199199 Rate ( u8 ) ,
200200 SelectorHt ,
201201 SelectorVht ,
@@ -216,43 +216,45 @@ pub enum Nl80211RateAndSelector {
216216
217217impl From < u8 > for Nl80211RateAndSelector {
218218 fn from ( d : u8 ) -> Self {
219- let msb: bool = ( d & 1 << 7 ) > 0 ;
220- let value = d & 0b01111111 ;
219+ const MSB_MASK : u8 = 0b1000_0000 ;
220+ let msb: bool = ( d & MSB_MASK ) == MSB_MASK ;
221+ let value = d & !MSB_MASK ;
221222 if msb {
222223 match value {
223224 BSS_MEMBERSHIP_SELECTOR_SAE_HASH => Self :: SelectorSaeHash ,
224225 BSS_MEMBERSHIP_SELECTOR_EPD => Self :: SelectorEpd ,
225226 BSS_MEMBERSHIP_SELECTOR_GLK => Self :: SelectorGlk ,
226227 BSS_MEMBERSHIP_SELECTOR_VHT_PHY => Self :: SelectorVht ,
227228 BSS_MEMBERSHIP_SELECTOR_HT_PHY => Self :: SelectorHt ,
228- _ => Self :: BssBasicRateSet ( value / 2 ) ,
229+ _ => Self :: BssBasicRateSet ( value) ,
229230 }
230231 } else {
231- Self :: Rate ( value / 2 )
232+ Self :: Rate ( value)
232233 }
233234 }
234235}
235236
236237impl From < Nl80211RateAndSelector > for u8 {
237238 fn from ( v : Nl80211RateAndSelector ) -> u8 {
239+ const MSB : u8 = 0b1000_0000 ;
238240 match v {
239- Nl80211RateAndSelector :: BssBasicRateSet ( r) => ( r * 2 ) & 1 << 7 ,
241+ Nl80211RateAndSelector :: BssBasicRateSet ( r) => r & ! MSB | MSB ,
240242 Nl80211RateAndSelector :: SelectorHt => {
241- BSS_MEMBERSHIP_SELECTOR_HT_PHY & 1 << 7
243+ BSS_MEMBERSHIP_SELECTOR_HT_PHY | MSB
242244 }
243245 Nl80211RateAndSelector :: SelectorVht => {
244- BSS_MEMBERSHIP_SELECTOR_VHT_PHY & 1 << 7
246+ BSS_MEMBERSHIP_SELECTOR_VHT_PHY | MSB
245247 }
246248 Nl80211RateAndSelector :: SelectorGlk => {
247- BSS_MEMBERSHIP_SELECTOR_GLK & 1 << 7
249+ BSS_MEMBERSHIP_SELECTOR_GLK | MSB
248250 }
249251 Nl80211RateAndSelector :: SelectorEpd => {
250- BSS_MEMBERSHIP_SELECTOR_EPD & 1 << 7
252+ BSS_MEMBERSHIP_SELECTOR_EPD | MSB
251253 }
252254 Nl80211RateAndSelector :: SelectorSaeHash => {
253- BSS_MEMBERSHIP_SELECTOR_SAE_HASH & 1 << 7
255+ BSS_MEMBERSHIP_SELECTOR_SAE_HASH | MSB
254256 }
255- Nl80211RateAndSelector :: Rate ( r) => r * 2 ,
257+ Nl80211RateAndSelector :: Rate ( r) => r,
256258 }
257259 }
258260}
@@ -312,7 +314,7 @@ impl Emitable for Nl80211ElementCountry {
312314 buffer[ 0 ] = self . country . as_bytes ( ) [ 0 ] ;
313315 buffer[ 1 ] = self . country . as_bytes ( ) [ 1 ] ;
314316 }
315- buffer[ 3 ] = self . environment . into ( ) ;
317+ buffer[ 2 ] = self . environment . into ( ) ;
316318 for ( i, triplet) in self . triplets . as_slice ( ) . iter ( ) . enumerate ( ) {
317319 triplet. emit ( & mut buffer[ ( i + 1 ) * 3 ..( i + 2 ) * 3 ] ) ;
318320 }
@@ -991,3 +993,46 @@ impl Nl80211Pmkid {
991993 }
992994 }
993995}
996+
997+ #[ cfg( test) ]
998+ mod test {
999+ use super :: * ;
1000+ use crate :: macros:: test:: roundtrip_emit_parse_test;
1001+
1002+ roundtrip_emit_parse_test ! (
1003+ ssid,
1004+ Nl80211Element ,
1005+ Nl80211Element :: Ssid ( "test-ssid" . to_owned( ) ) ,
1006+ ) ;
1007+ roundtrip_emit_parse_test ! (
1008+ rates_and_selectors,
1009+ Nl80211Element ,
1010+ Nl80211Element :: SupportedRatesAndSelectors ( vec![
1011+ Nl80211RateAndSelector :: BssBasicRateSet ( 1 ) ,
1012+ Nl80211RateAndSelector :: Rate ( 1 ) ,
1013+ Nl80211RateAndSelector :: SelectorHt ,
1014+ Nl80211RateAndSelector :: SelectorVht ,
1015+ Nl80211RateAndSelector :: SelectorGlk ,
1016+ ] )
1017+ ) ;
1018+ roundtrip_emit_parse_test ! (
1019+ channel,
1020+ Nl80211Element ,
1021+ Nl80211Element :: Channel ( 7 )
1022+ ) ;
1023+ roundtrip_emit_parse_test ! (
1024+ country,
1025+ Nl80211Element ,
1026+ Nl80211Element :: Country ( Nl80211ElementCountry {
1027+ country: "DE" . to_owned( ) ,
1028+ environment: Nl80211ElementCountryEnvironment :: IndoorAndOutdoor ,
1029+ triplets: vec![ Nl80211ElementCountryTriplet :: Subband (
1030+ Nl80211ElementSubBand {
1031+ channel_start: 1 ,
1032+ channel_count: 13 ,
1033+ max_power_level: 20 ,
1034+ }
1035+ ) ] ,
1036+ } ) ,
1037+ ) ;
1038+ }
0 commit comments