w5500_ll/net.rs
1//! Networking data types.
2
3pub use core::net::{Ipv4Addr, SocketAddrV4};
4
5/// EUI-48 MAC address struct.
6///
7/// Can be instantiated with [`Eui48Addr::new`].
8///
9/// This is an EUI-48 [MAC address] (previously called MAC-48).
10///
11/// [MAC address]: https://en.wikipedia.org/wiki/MAC_address
12#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Hash, Default)]
13pub struct Eui48Addr {
14    /// Octets of the MAC address.
15    pub octets: [u8; 6],
16}
17
18impl Eui48Addr {
19    /// Creates a new EUI-48 MAC address from six eight-bit octets.
20    ///
21    /// The result will represent the EUI-48 MAC address
22    /// `a`:`b`:`c`:`d`:`e`:`f`.
23    ///
24    /// # Examples
25    ///
26    /// ```
27    /// use w5500_ll::net::Eui48Addr;
28    ///
29    /// let addr = Eui48Addr::new(0x00, 0x00, 0x5E, 0x00, 0x00, 0x00);
30    /// ```
31    #[allow(clippy::many_single_char_names)]
32    pub const fn new(a: u8, b: u8, c: u8, d: u8, e: u8, f: u8) -> Eui48Addr {
33        Eui48Addr {
34            octets: [a, b, c, d, e, f],
35        }
36    }
37
38    /// An EUI-48 MAC address representing an unspecified address:
39    /// 00:00:00:00:00:00
40    ///
41    /// # Examples
42    ///
43    /// ```
44    /// use w5500_ll::net::Eui48Addr;
45    ///
46    /// let addr = Eui48Addr::UNSPECIFIED;
47    /// assert_eq!(addr, Eui48Addr::new(0x00, 0x00, 0x00, 0x00, 0x00, 0x00));
48    /// ```
49    pub const UNSPECIFIED: Self = Eui48Addr::new(0, 0, 0, 0, 0, 0);
50}
51
52impl ::core::fmt::Display for Eui48Addr {
53    /// String formatter for [`Eui48Addr`] addresses.
54    fn fmt(&self, fmt: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
55        write!(
56            fmt,
57            "{:02X}:{:02X}:{:02X}:{:02X}:{:02X}:{:02X}",
58            self.octets[0],
59            self.octets[1],
60            self.octets[2],
61            self.octets[3],
62            self.octets[4],
63            self.octets[5],
64        )
65    }
66}
67
68#[cfg(feature = "defmt")]
69impl defmt::Format for Eui48Addr {
70    fn format(&self, fmt: defmt::Formatter) {
71        defmt::write!(
72            fmt,
73            "{:02X}:{:02X}:{:02X}:{:02X}:{:02X}:{:02X}",
74            self.octets[0],
75            self.octets[1],
76            self.octets[2],
77            self.octets[3],
78            self.octets[4],
79            self.octets[5],
80        )
81    }
82}
83
84impl From<[u8; 6]> for Eui48Addr {
85    /// Creates an `Eui48Addr` from a six element byte array.
86    ///
87    /// # Examples
88    ///
89    /// ```
90    /// use w5500_ll::net::Eui48Addr;
91    ///
92    /// let addr = Eui48Addr::from([0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC]);
93    /// assert_eq!(Eui48Addr::new(0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC), addr);
94    /// ```
95    fn from(octets: [u8; 6]) -> Self {
96        Eui48Addr { octets }
97    }
98}