diff options
| author | Dario Nieuwenhuis <[email protected]> | 2022-05-23 03:50:43 +0200 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2022-05-25 19:56:22 +0200 |
| commit | a5aea995a802fea8fc1b3e4b5fe47bd6d1fca2a4 (patch) | |
| tree | 0fcb4c01914347eff5b3be44b284aa9432e28678 /embassy-net/src/config | |
| parent | 36a1f203648dcb402727ea3eb5d30cf1f6993795 (diff) | |
WIP embassy-net v2
Diffstat (limited to 'embassy-net/src/config')
| -rw-r--r-- | embassy-net/src/config/dhcp.rs | 55 | ||||
| -rw-r--r-- | embassy-net/src/config/mod.rs | 35 | ||||
| -rw-r--r-- | embassy-net/src/config/statik.rs | 29 |
3 files changed, 0 insertions, 119 deletions
diff --git a/embassy-net/src/config/dhcp.rs b/embassy-net/src/config/dhcp.rs deleted file mode 100644 index 298657ed6..000000000 --- a/embassy-net/src/config/dhcp.rs +++ /dev/null | |||
| @@ -1,55 +0,0 @@ | |||
| 1 | use heapless::Vec; | ||
| 2 | use smoltcp::iface::SocketHandle; | ||
| 3 | use smoltcp::socket::{Dhcpv4Event, Dhcpv4Socket}; | ||
| 4 | use smoltcp::time::Instant; | ||
| 5 | |||
| 6 | use super::*; | ||
| 7 | use crate::device::LinkState; | ||
| 8 | use crate::Interface; | ||
| 9 | |||
| 10 | pub struct DhcpConfigurator { | ||
| 11 | handle: Option<SocketHandle>, | ||
| 12 | } | ||
| 13 | |||
| 14 | impl DhcpConfigurator { | ||
| 15 | pub fn new() -> Self { | ||
| 16 | Self { handle: None } | ||
| 17 | } | ||
| 18 | } | ||
| 19 | |||
| 20 | impl Configurator for DhcpConfigurator { | ||
| 21 | fn poll(&mut self, iface: &mut Interface, _timestamp: Instant) -> Event { | ||
| 22 | if self.handle.is_none() { | ||
| 23 | let handle = iface.add_socket(Dhcpv4Socket::new()); | ||
| 24 | self.handle = Some(handle) | ||
| 25 | } | ||
| 26 | |||
| 27 | let link_up = iface.device_mut().device.link_state() == LinkState::Up; | ||
| 28 | |||
| 29 | let socket = iface.get_socket::<Dhcpv4Socket>(self.handle.unwrap()); | ||
| 30 | |||
| 31 | if !link_up { | ||
| 32 | socket.reset(); | ||
| 33 | return Event::Deconfigured; | ||
| 34 | } | ||
| 35 | |||
| 36 | match socket.poll() { | ||
| 37 | None => Event::NoChange, | ||
| 38 | Some(Dhcpv4Event::Deconfigured) => Event::Deconfigured, | ||
| 39 | Some(Dhcpv4Event::Configured(config)) => { | ||
| 40 | let mut dns_servers = Vec::new(); | ||
| 41 | for s in &config.dns_servers { | ||
| 42 | if let Some(addr) = s { | ||
| 43 | dns_servers.push(addr.clone()).unwrap(); | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | Event::Configured(Config { | ||
| 48 | address: config.address, | ||
| 49 | gateway: config.router, | ||
| 50 | dns_servers, | ||
| 51 | }) | ||
| 52 | } | ||
| 53 | } | ||
| 54 | } | ||
| 55 | } | ||
diff --git a/embassy-net/src/config/mod.rs b/embassy-net/src/config/mod.rs deleted file mode 100644 index eb1b6636a..000000000 --- a/embassy-net/src/config/mod.rs +++ /dev/null | |||
| @@ -1,35 +0,0 @@ | |||
| 1 | use heapless::Vec; | ||
| 2 | use smoltcp::time::Instant; | ||
| 3 | use smoltcp::wire::{Ipv4Address, Ipv4Cidr}; | ||
| 4 | |||
| 5 | use crate::Interface; | ||
| 6 | |||
| 7 | mod statik; | ||
| 8 | pub use statik::StaticConfigurator; | ||
| 9 | |||
| 10 | #[cfg(feature = "dhcpv4")] | ||
| 11 | mod dhcp; | ||
| 12 | #[cfg(feature = "dhcpv4")] | ||
| 13 | pub use dhcp::DhcpConfigurator; | ||
| 14 | |||
| 15 | /// Return value for the `Configurator::poll` function | ||
| 16 | #[derive(Debug, Clone)] | ||
| 17 | pub enum Event { | ||
| 18 | /// No change has occured to the configuration. | ||
| 19 | NoChange, | ||
| 20 | /// Configuration has been lost (for example, DHCP lease has expired) | ||
| 21 | Deconfigured, | ||
| 22 | /// Configuration has been newly acquired, or modified. | ||
| 23 | Configured(Config), | ||
| 24 | } | ||
| 25 | |||
| 26 | #[derive(Debug, Clone, PartialEq, Eq)] | ||
| 27 | pub struct Config { | ||
| 28 | pub address: Ipv4Cidr, | ||
| 29 | pub gateway: Option<Ipv4Address>, | ||
| 30 | pub dns_servers: Vec<Ipv4Address, 3>, | ||
| 31 | } | ||
| 32 | |||
| 33 | pub trait Configurator { | ||
| 34 | fn poll(&mut self, iface: &mut Interface, timestamp: Instant) -> Event; | ||
| 35 | } | ||
diff --git a/embassy-net/src/config/statik.rs b/embassy-net/src/config/statik.rs deleted file mode 100644 index e614db73b..000000000 --- a/embassy-net/src/config/statik.rs +++ /dev/null | |||
| @@ -1,29 +0,0 @@ | |||
| 1 | use smoltcp::time::Instant; | ||
| 2 | |||
| 3 | use super::*; | ||
| 4 | use crate::Interface; | ||
| 5 | |||
| 6 | pub struct StaticConfigurator { | ||
| 7 | config: Config, | ||
| 8 | returned: bool, | ||
| 9 | } | ||
| 10 | |||
| 11 | impl StaticConfigurator { | ||
| 12 | pub fn new(config: Config) -> Self { | ||
| 13 | Self { | ||
| 14 | config, | ||
| 15 | returned: false, | ||
| 16 | } | ||
| 17 | } | ||
| 18 | } | ||
| 19 | |||
| 20 | impl Configurator for StaticConfigurator { | ||
| 21 | fn poll(&mut self, _iface: &mut Interface, _timestamp: Instant) -> Event { | ||
| 22 | if self.returned { | ||
| 23 | Event::NoChange | ||
| 24 | } else { | ||
| 25 | self.returned = true; | ||
| 26 | Event::Configured(self.config.clone()) | ||
| 27 | } | ||
| 28 | } | ||
| 29 | } | ||
