2020-09-09 17:56:50 +08:00
|
|
|
use core::fmt;
|
|
|
|
|
|
|
|
use libboard_zynq::smoltcp::wire::{EthernetAddress, IpAddress};
|
|
|
|
|
|
|
|
use super::Config;
|
|
|
|
|
|
|
|
pub struct NetAddresses {
|
|
|
|
pub hardware_addr: EthernetAddress,
|
|
|
|
pub ipv4_addr: IpAddress,
|
|
|
|
#[cfg(feature = "ipv6")]
|
|
|
|
pub ipv6_ll_addr: IpAddress,
|
|
|
|
#[cfg(feature = "ipv6")]
|
|
|
|
pub ipv6_addr: Option<IpAddress>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for NetAddresses {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "MAC={} IPv4={} ",
|
|
|
|
self.hardware_addr, self.ipv4_addr)?;
|
|
|
|
|
|
|
|
#[cfg(feature = "ipv6")]
|
|
|
|
{
|
|
|
|
write!(f, "IPv6-LL={}", self.ipv6_ll_addr)?;
|
|
|
|
match self.ipv6_addr {
|
|
|
|
Some(addr) => write!(f, " {}", addr)?,
|
|
|
|
None => write!(f, " IPv6: no configured address")?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-07 18:01:44 +08:00
|
|
|
#[cfg(feature = "target_kasli_soc")]
|
|
|
|
fn get_address_from_eeprom() -> EthernetAddress {
|
|
|
|
use libboard_zynq::i2c::{I2c, eeprom};
|
|
|
|
|
|
|
|
let mut i2c = I2c::i2c0();
|
|
|
|
i2c.init().unwrap();
|
|
|
|
let mut eeprom = eeprom::EEPROM::new(&mut i2c, 16);
|
|
|
|
let address = eeprom.read_eui48().unwrap_or([0x02, 0x00, 0x00, 0x00, 0x00, 0x56]);
|
|
|
|
|
|
|
|
EthernetAddress(address)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_addresses(cfg: &Config) -> NetAddresses {
|
2020-10-14 12:53:32 +08:00
|
|
|
#[cfg(feature = "target_zc706")]
|
2020-09-09 17:56:50 +08:00
|
|
|
let mut hardware_addr = EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x52]);
|
2020-10-14 12:53:32 +08:00
|
|
|
#[cfg(feature = "target_zc706")]
|
2020-09-09 17:56:50 +08:00
|
|
|
let mut ipv4_addr = IpAddress::v4(192, 168, 1, 52);
|
2020-11-14 00:56:47 +08:00
|
|
|
#[cfg(feature = "target_coraz7")]
|
2020-10-14 12:53:32 +08:00
|
|
|
let mut hardware_addr = EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x54]);
|
2020-11-14 00:56:47 +08:00
|
|
|
#[cfg(feature = "target_coraz7")]
|
2020-10-14 12:53:32 +08:00
|
|
|
let mut ipv4_addr = IpAddress::v4(192, 168, 1, 54);
|
|
|
|
#[cfg(feature = "target_redpitaya")]
|
|
|
|
let mut hardware_addr = EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x55]);
|
|
|
|
#[cfg(feature = "target_redpitaya")]
|
|
|
|
let mut ipv4_addr = IpAddress::v4(192, 168, 1, 55);
|
2020-11-20 02:41:38 +08:00
|
|
|
#[cfg(feature = "target_kasli_soc")]
|
2022-03-07 18:01:44 +08:00
|
|
|
let mut hardware_addr = get_address_from_eeprom();
|
2020-11-20 02:41:38 +08:00
|
|
|
#[cfg(feature = "target_kasli_soc")]
|
|
|
|
let mut ipv4_addr = IpAddress::v4(192, 168, 1, 56);
|
2020-09-09 17:56:50 +08:00
|
|
|
|
|
|
|
if let Ok(Ok(addr)) = cfg.read_str("mac").map(|s| s.parse()) {
|
|
|
|
hardware_addr = addr;
|
|
|
|
}
|
|
|
|
if let Ok(Ok(addr)) = cfg.read_str("ip").map(|s| s.parse()) {
|
|
|
|
ipv4_addr = addr;
|
|
|
|
}
|
|
|
|
#[cfg(feature = "ipv6")]
|
2020-11-18 10:47:48 +08:00
|
|
|
let ipv6_addr = cfg.read_str("ip6").ok().and_then(|s| s.parse().ok());
|
2020-09-09 17:56:50 +08:00
|
|
|
|
|
|
|
#[cfg(feature = "ipv6")]
|
|
|
|
let ipv6_ll_addr = IpAddress::v6(
|
|
|
|
0xfe80, 0x0000, 0x0000, 0x0000,
|
|
|
|
(((hardware_addr.0[0] ^ 0x02) as u16) << 8) | (hardware_addr.0[1] as u16),
|
|
|
|
((hardware_addr.0[2] as u16) << 8) | 0x00ff,
|
|
|
|
0xfe00 | (hardware_addr.0[3] as u16),
|
|
|
|
((hardware_addr.0[4] as u16) << 8) | (hardware_addr.0[5] as u16));
|
|
|
|
|
|
|
|
NetAddresses {
|
|
|
|
hardware_addr,
|
|
|
|
ipv4_addr,
|
|
|
|
#[cfg(feature = "ipv6")]
|
|
|
|
ipv6_ll_addr,
|
|
|
|
#[cfg(feature = "ipv6")]
|
|
|
|
ipv6_addr
|
|
|
|
}
|
|
|
|
}
|