aboutsummaryrefslogtreecommitdiff
path: root/embassy-net/src/stack.rs
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2021-04-07 19:06:45 +0200
committerDario Nieuwenhuis <[email protected]>2021-04-07 19:06:45 +0200
commit9c5a8b945a743e75d586fdc2ef857d7c2a038e7d (patch)
treec7e4c88840281b3b54fbda466c717a83094f8121 /embassy-net/src/stack.rs
parent9bee576fd241f019c363919b0c29551c6b8ee4b2 (diff)
Update to latest embassy, atomic-pool, smoltcp
Diffstat (limited to 'embassy-net/src/stack.rs')
-rw-r--r--embassy-net/src/stack.rs77
1 files changed, 42 insertions, 35 deletions
diff --git a/embassy-net/src/stack.rs b/embassy-net/src/stack.rs
index f8a945a54..dc8043e67 100644
--- a/embassy-net/src/stack.rs
+++ b/embassy-net/src/stack.rs
@@ -11,14 +11,12 @@ use smoltcp::phy::Device as _;
11use smoltcp::phy::Medium; 11use smoltcp::phy::Medium;
12use smoltcp::socket::SocketSetItem; 12use smoltcp::socket::SocketSetItem;
13use smoltcp::time::Instant as SmolInstant; 13use smoltcp::time::Instant as SmolInstant;
14use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr, Ipv4Address}; 14use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr, Ipv4Address, Ipv4Cidr};
15 15
16use crate::device::{Device, DeviceAdapter}; 16use crate::config::Configurator;
17use crate::config::Event;
18use crate::device::{Device, DeviceAdapter, LinkState};
17use crate::fmt::*; 19use crate::fmt::*;
18use crate::{
19 config::{Config, Configurator},
20 device::LinkState,
21};
22use crate::{Interface, SocketSet}; 20use crate::{Interface, SocketSet};
23 21
24const ADDRESSES_LEN: usize = 1; 22const ADDRESSES_LEN: usize = 1;
@@ -68,39 +66,41 @@ impl Stack {
68 } 66 }
69 67
70 fn poll_configurator(&mut self, timestamp: SmolInstant) { 68 fn poll_configurator(&mut self, timestamp: SmolInstant) {
71 if let Some(config) = self 69 let medium = self.iface.device().capabilities().medium;
70
71 match self
72 .configurator 72 .configurator
73 .poll(&mut self.iface, &mut self.sockets, timestamp) 73 .poll(&mut self.iface, &mut self.sockets, timestamp)
74 { 74 {
75 let medium = self.iface.device().capabilities().medium; 75 Event::NoChange => {}
76 76 Event::Configured(config) => {
77 let (addr, gateway) = match config { 77 debug!("Acquired IP configuration:");
78 Config::Up(config) => (config.address.into(), Some(config.gateway)), 78
79 Config::Down => (IpCidr::new(Ipv4Address::UNSPECIFIED.into(), 32), None), 79 debug!(" IP address: {}", config.address);
80 }; 80 set_ipv4_addr(&mut self.iface, config.address);
81 81
82 self.iface.update_ip_addrs(|addrs| { 82 if medium == Medium::Ethernet {
83 let curr_addr = &mut addrs[0]; 83 if let Some(gateway) = config.gateway {
84 if *curr_addr != addr { 84 debug!(" Default gateway: {}", gateway);
85 info!("IPv4 address: {:?} -> {:?}", *curr_addr, addr); 85 self.iface
86 *curr_addr = addr; 86 .routes_mut()
87 } 87 .add_default_ipv4_route(gateway)
88 }); 88 .unwrap();
89 89 } else {
90 if medium == Medium::Ethernet { 90 debug!(" Default gateway: None");
91 self.iface.routes_mut().update(|r| { 91 self.iface.routes_mut().remove_default_ipv4_route();
92 let cidr = IpCidr::new(IpAddress::v4(0, 0, 0, 0), 0);
93 let curr_gateway = r.get(&cidr).map(|r| r.via_router);
94
95 if curr_gateway != gateway.map(|a| a.into()) {
96 info!("IPv4 gateway: {:?} -> {:?}", curr_gateway, gateway);
97 if let Some(gateway) = gateway {
98 r.insert(cidr, Route::new_ipv4_gateway(gateway)).unwrap();
99 } else {
100 r.remove(&cidr);
101 }
102 } 92 }
103 }); 93 }
94 for (i, s) in config.dns_servers.iter().enumerate() {
95 debug!(" DNS server {}: {}", i, s);
96 }
97 }
98 Event::Deconfigured => {
99 debug!("Lost IP configuration");
100 set_ipv4_addr(&mut self.iface, Ipv4Cidr::new(Ipv4Address::UNSPECIFIED, 0));
101 if medium == Medium::Ethernet {
102 self.iface.routes_mut().remove_default_ipv4_route();
103 }
104 } 104 }
105 } 105 }
106 } 106 }
@@ -143,6 +143,13 @@ impl Stack {
143 } 143 }
144} 144}
145 145
146fn set_ipv4_addr(iface: &mut Interface, cidr: Ipv4Cidr) {
147 iface.update_ip_addrs(|addrs| {
148 let dest = addrs.iter_mut().next().unwrap();
149 *dest = IpCidr::Ipv4(cidr);
150 });
151}
152
146/// Initialize embassy_net. 153/// Initialize embassy_net.
147/// This function must be called from thread mode. 154/// This function must be called from thread mode.
148pub fn init(device: &'static mut dyn Device, configurator: &'static mut dyn Configurator) { 155pub fn init(device: &'static mut dyn Device, configurator: &'static mut dyn Configurator) {