Skip to content

Commit 19b0bb3

Browse files
committed
add MSTP address format
1 parent a542bb4 commit 19b0bb3

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

src/network_protocol/network_pdu.rs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -267,11 +267,24 @@ impl<'a> NetworkPdu<'a> {
267267

268268
#[derive(Debug, Clone)]
269269
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
270-
pub struct Addr {
271-
pub ipv4: [u8; 4],
270+
pub struct Ipv4Addr {
271+
pub addr: [u8; 4],
272272
pub port: u16,
273273
}
274274

275+
#[derive(Debug, Clone)]
276+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
277+
pub enum Addr {
278+
Ipv4(Ipv4Addr),
279+
Mac(u8),
280+
}
281+
282+
impl Addr {
283+
pub fn new_ipv4(addr: [u8; 4], port: u16) -> Self {
284+
Self::Ipv4(Ipv4Addr { addr, port })
285+
}
286+
}
287+
275288
const IPV4_ADDR_LEN: u8 = 6;
276289

277290
pub type SourceAddress = NetworkAddress;
@@ -304,9 +317,18 @@ impl NetworkAddress {
304317
writer.extend_from_slice(&self.net.to_be_bytes());
305318
match self.addr.as_ref() {
306319
Some(addr) => {
307-
writer.push(IPV4_ADDR_LEN);
308-
writer.extend_from_slice(&addr.ipv4);
309-
writer.extend_from_slice(&addr.port.to_be_bytes());
320+
match addr {
321+
Addr::Mac(mac) => {
322+
let encoded = &mac.to_be_bytes();
323+
writer.push(encoded.len() as u8);
324+
writer.extend_from_slice(encoded);
325+
},
326+
Addr::Ipv4(addr) => {
327+
writer.push(IPV4_ADDR_LEN);
328+
writer.extend_from_slice(&addr.addr);
329+
writer.extend_from_slice(&addr.port.to_be_bytes());
330+
}
331+
}
310332
}
311333
None => writer.push(0),
312334
}
@@ -322,7 +344,7 @@ impl NetworkAddress {
322344

323345
Ok(Self {
324346
net,
325-
addr: Some(Addr { ipv4, port }),
347+
addr: Some(Addr::Ipv4(Ipv4Addr { port, addr: ipv4 })),
326348
})
327349
}
328350
0 => Ok(Self { net, addr: None }),

0 commit comments

Comments
 (0)