socket/dhcpv4: return owned Config in Event.

This commit is contained in:
Dario Nieuwenhuis 2021-10-21 03:14:51 +02:00
parent 214a13bca6
commit b1d6104a74
1 changed files with 6 additions and 6 deletions

View File

@ -29,7 +29,7 @@ const PARAMETER_REQUEST_LIST: &[u8] = &[
]; ];
/// IPv4 configuration data provided by the DHCP server. /// IPv4 configuration data provided by the DHCP server.
#[derive(Debug, Eq, PartialEq)] #[derive(Debug, Eq, PartialEq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))] #[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct Config { pub struct Config {
/// IP address /// IP address
@ -103,11 +103,11 @@ enum ClientState {
/// Return value for the `Dhcpv4Socket::poll` function /// Return value for the `Dhcpv4Socket::poll` function
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))] #[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Event<'a> { pub enum Event {
/// Configuration has been lost (for example, the lease has expired) /// Configuration has been lost (for example, the lease has expired)
Deconfigured, Deconfigured,
/// Configuration has been newly acquired, or modified. /// Configuration has been newly acquired, or modified.
Configured(&'a Config), Configured(Config),
} }
#[derive(Debug)] #[derive(Debug)]
@ -544,12 +544,12 @@ impl Dhcpv4Socket {
/// ///
/// The socket has an internal "configuration changed" flag. If /// The socket has an internal "configuration changed" flag. If
/// set, this function returns the configuration and resets the flag. /// set, this function returns the configuration and resets the flag.
pub fn poll(&mut self) -> Option<Event<'_>> { pub fn poll(&mut self) -> Option<Event> {
if !self.config_changed { if !self.config_changed {
None None
} else if let ClientState::Renewing(state) = &self.state { } else if let ClientState::Renewing(state) = &self.state {
self.config_changed = false; self.config_changed = false;
Some(Event::Configured(&state.config)) Some(Event::Configured(state.config))
} else { } else {
self.config_changed = false; self.config_changed = false;
Some(Event::Deconfigured) Some(Event::Deconfigured)
@ -836,7 +836,7 @@ mod test {
assert_eq!( assert_eq!(
s.poll(), s.poll(),
Some(Event::Configured(&Config { Some(Event::Configured(Config {
address: Ipv4Cidr::new(MY_IP, 24), address: Ipv4Cidr::new(MY_IP, 24),
dns_servers: DNS_IPS, dns_servers: DNS_IPS,
router: Some(SERVER_IP), router: Some(SERVER_IP),