Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add static IP address as a build feature #509

Merged
merged 4 commits into from
Feb 15, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ branch = "feature/assume-init"
[features]
nightly = ["cortex-m/inline-asm"]
pounder_v1_1 = [ ]
static_ip = [ ]

[profile.dev]
codegen-units = 1
Expand Down
45 changes: 36 additions & 9 deletions src/hardware/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,20 @@ const NUM_TCP_SOCKETS: usize = 4;
const NUM_UDP_SOCKETS: usize = 1;
const NUM_SOCKETS: usize = NUM_UDP_SOCKETS + NUM_TCP_SOCKETS;

const IPADDR: [u8; 4] = [192, 168, 137, 17];
ryan-summers marked this conversation as resolved.
Show resolved Hide resolved
ryan-summers marked this conversation as resolved.
Show resolved Hide resolved

// Omit DHCP socket, if static IP is used
#[cfg(feature = "static_ip")]
const NUM_DHCP_SOCKETS: usize = 0;
#[cfg(not(feature = "static_ip"))]
const NUM_DHCP_SOCKETS: usize = 1;

pub struct NetStorage {
pub ip_addrs: [smoltcp::wire::IpCidr; 1],

// Note: There is an additional socket set item required for the DHCP socket.
pub sockets: [smoltcp::iface::SocketStorage<'static>; NUM_SOCKETS + 1],
pub sockets: [smoltcp::iface::SocketStorage<'static>;
ryan-summers marked this conversation as resolved.
Show resolved Hide resolved
NUM_SOCKETS + NUM_DHCP_SOCKETS],
pub tcp_socket_storage: [TcpSocketStorage; NUM_TCP_SOCKETS],
pub udp_socket_storage: [UdpSocketStorage; NUM_UDP_SOCKETS],
pub neighbor_cache:
Expand Down Expand Up @@ -84,7 +93,8 @@ impl Default for NetStorage {
)],
neighbor_cache: [None; 8],
routes_cache: [None; 8],
sockets: [smoltcp::iface::SocketStorage::EMPTY; NUM_SOCKETS + 1],
sockets: [smoltcp::iface::SocketStorage::EMPTY;
NUM_SOCKETS + NUM_DHCP_SOCKETS],
tcp_socket_storage: [TcpSocketStorage::new(); NUM_TCP_SOCKETS],
udp_socket_storage: [UdpSocketStorage::new(); NUM_UDP_SOCKETS],
}
Expand Down Expand Up @@ -688,17 +698,32 @@ pub fn setup(

unsafe { ethernet::enable_interrupt() };

// Configure IP address according to DHCP socket availability
let ip_addrs: smoltcp::wire::IpCidr;
if NUM_DHCP_SOCKETS == 1 {
ip_addrs = smoltcp::wire::IpCidr::new(
smoltcp::wire::IpAddress::Ipv4(
smoltcp::wire::Ipv4Address::UNSPECIFIED,
),
0,
);
} else {
ip_addrs = smoltcp::wire::IpCidr::new(
smoltcp::wire::IpAddress::Ipv4(
smoltcp::wire::Ipv4Address::new(
IPADDR[0], IPADDR[1], IPADDR[2], IPADDR[3],
),
),
24,
);
}

// Note(unwrap): The hardware configuration function is only allowed to be called once.
// Unwrapping is intended to panic if called again to prevent re-use of global memory.
let store =
cortex_m::singleton!(: NetStorage = NetStorage::default()).unwrap();

store.ip_addrs[0] = smoltcp::wire::IpCidr::new(
smoltcp::wire::IpAddress::Ipv4(
smoltcp::wire::Ipv4Address::UNSPECIFIED,
),
0,
);
store.ip_addrs[0] = ip_addrs;
ryan-summers marked this conversation as resolved.
Show resolved Hide resolved

let mut routes =
smoltcp::iface::Routes::new(&mut store.routes_cache[..]);
Expand All @@ -719,7 +744,9 @@ pub fn setup(
.routes(routes)
.finalize();

interface.add_socket(smoltcp::socket::Dhcpv4Socket::new());
if NUM_DHCP_SOCKETS == 1 {
interface.add_socket(smoltcp::socket::Dhcpv4Socket::new());
ryan-summers marked this conversation as resolved.
Show resolved Hide resolved
}

for storage in store.tcp_socket_storage[..].iter_mut() {
let tcp_socket = {
Expand Down