summaryrefslogtreecommitdiff
path: root/embassy-net/src/config/dhcp.rs
blob: 8bbcd8176a914d0eb8ab72a13d7353b50fc1f134 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use heapless::Vec;
use smoltcp::socket::{Dhcpv4Event, Dhcpv4Socket, SocketHandle};
use smoltcp::time::Instant;

use super::*;
use crate::device::LinkState;
use crate::{Interface, SocketSet};

pub struct DhcpConfigurator {
    handle: Option<SocketHandle>,
}

impl DhcpConfigurator {
    pub fn new() -> Self {
        Self { handle: None }
    }
}

impl Configurator for DhcpConfigurator {
    fn poll(
        &mut self,
        iface: &mut Interface,
        sockets: &mut SocketSet,
        _timestamp: Instant,
    ) -> Event {
        if self.handle.is_none() {
            let handle = sockets.add(Dhcpv4Socket::new());
            self.handle = Some(handle)
        }

        let mut socket = sockets.get::<Dhcpv4Socket>(self.handle.unwrap());

        let link_up = iface.device_mut().device.link_state() == LinkState::Up;
        if !link_up {
            socket.reset();
            return Event::Deconfigured;
        }

        match socket.poll() {
            None => Event::NoChange,
            Some(Dhcpv4Event::Deconfigured) => Event::Deconfigured,
            Some(Dhcpv4Event::Configured(config)) => {
                let mut dns_servers = Vec::new();
                for s in &config.dns_servers {
                    if let Some(addr) = s {
                        dns_servers.push(addr.clone()).unwrap();
                    }
                }

                Event::Configured(Config {
                    address: config.address,
                    gateway: config.router,
                    dns_servers,
                })
            }
        }
    }
}