ieee80211/
bssid.rs

1// Copyright 2021 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use crate::mac_addr::{MacAddr, MacAddrByteArray, MacAddrBytes, MacFmt, OuiFmt};
6use std::fmt;
7use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout, Unaligned};
8
9// Bssid is a newtype to wrap MacAddrBytes where a BSSID is explicitly required.
10#[repr(transparent)]
11#[derive(
12    KnownLayout,
13    FromBytes,
14    IntoBytes,
15    Immutable,
16    Unaligned,
17    Clone,
18    Copy,
19    PartialEq,
20    Eq,
21    PartialOrd,
22    Ord,
23    Hash,
24)]
25pub struct Bssid(pub(crate) MacAddrByteArray);
26
27impl MacAddrBytes for Bssid {
28    fn to_array(&self) -> MacAddrByteArray {
29        self.0
30    }
31
32    fn as_array(&self) -> &MacAddrByteArray {
33        &self.0
34    }
35}
36
37impl MacFmt for Bssid {}
38impl OuiFmt for Bssid {}
39
40impl From<MacAddrByteArray> for Bssid {
41    fn from(bytes: MacAddrByteArray) -> Bssid {
42        Bssid(bytes)
43    }
44}
45
46impl From<MacAddr> for Bssid {
47    fn from(mac: MacAddr) -> Bssid {
48        Bssid(mac.0)
49    }
50}
51
52impl fmt::Display for Bssid {
53    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54        write!(f, "{}", self.to_mac_string())
55    }
56}
57
58impl fmt::Debug for Bssid {
59    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
60        write!(f, "Bssid({})", self)
61    }
62}
63
64pub const WILDCARD_BSSID: Bssid = Bssid([0xff, 0xff, 0xff, 0xff, 0xff, 0xff]);
65
66#[cfg(test)]
67mod tests {
68    use super::*;
69
70    #[test]
71    fn format_bssid_as_mac_string() {
72        let bssid = Bssid::from([0x01, 0x02, 0x01, 0x02, 0x01, 0x02]);
73        assert_eq!("01:02:01:02:01:02", &format!("{}", bssid));
74    }
75
76    #[test]
77    fn format_bssid_as_mac_debug_string() {
78        let bssid = Bssid::from([0x01, 0x02, 0x01, 0x02, 0x01, 0x02]);
79        assert_eq!("Bssid(01:02:01:02:01:02)", &format!("{:?}", bssid));
80    }
81}