ieee80211/
bssid.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright 2021 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use crate::mac_addr::{MacAddr, MacAddrByteArray, MacAddrBytes, MacFmt, OuiFmt};
use std::fmt;
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout, Unaligned};

// Bssid is a newtype to wrap MacAddrBytes where a BSSID is explicitly required.
#[repr(transparent)]
#[derive(
    KnownLayout,
    FromBytes,
    IntoBytes,
    Immutable,
    Unaligned,
    Clone,
    Copy,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
    Hash,
)]
pub struct Bssid(pub(crate) MacAddrByteArray);

impl MacAddrBytes for Bssid {
    fn to_array(&self) -> MacAddrByteArray {
        self.0
    }

    fn as_array(&self) -> &MacAddrByteArray {
        &self.0
    }
}

impl MacFmt for Bssid {}
impl OuiFmt for Bssid {}

impl From<MacAddrByteArray> for Bssid {
    fn from(bytes: MacAddrByteArray) -> Bssid {
        Bssid(bytes)
    }
}

impl From<MacAddr> for Bssid {
    fn from(mac: MacAddr) -> Bssid {
        Bssid(mac.0)
    }
}

impl fmt::Display for Bssid {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.to_mac_string())
    }
}

impl fmt::Debug for Bssid {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Bssid({})", self.to_string())
    }
}

pub const WILDCARD_BSSID: Bssid = Bssid([0xff, 0xff, 0xff, 0xff, 0xff, 0xff]);

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn format_bssid_as_mac_string() {
        let bssid = Bssid::from([0x01, 0x02, 0x01, 0x02, 0x01, 0x02]);
        assert_eq!("01:02:01:02:01:02", &format!("{}", bssid));
    }

    #[test]
    fn format_bssid_as_mac_debug_string() {
        let bssid = Bssid::from([0x01, 0x02, 0x01, 0x02, 0x01, 0x02]);
        assert_eq!("Bssid(01:02:01:02:01:02)", &format!("{:?}", bssid));
    }
}