Mintor fixes after testing re-attach

This commit is contained in:
Ulf Lilleengen 2024-09-05 10:02:45 +02:00
parent ccfa6264b0
commit 836e8add1b
2 changed files with 41 additions and 33 deletions

View File

@ -275,24 +275,28 @@ impl<'a> Control<'a> {
})
}
/// Run a control loop for this context, ensuring that reaattach is handled.
pub async fn run<F: Fn(&Status)>(&self, config: &Config<'_>, reattach: F) -> Result<(), Error> {
self.configure(config).await?;
async fn wait_attached(&self) -> Result<Status, Error> {
while !self.attached().await? {
Timer::after(Duration::from_secs(1)).await;
}
let status = self.status().await?;
Ok(status)
}
/// Run a control loop for this context, ensuring that reaattach is handled.
pub async fn run<F: Fn(&Status)>(&self, config: &Config<'_>, reattach: F) -> Result<(), Error> {
self.configure(config).await?;
let status = self.wait_attached().await?;
let mut fd = self.control.open_raw_socket().await;
reattach(&status);
loop {
if !self.attached().await? {
trace!("detached");
self.control.close_raw_socket(fd).await;
self.attach().await?;
while !self.attached().await? {
Timer::after(Duration::from_secs(1)).await;
}
let status = self.status().await?;
let status = self.wait_attached().await?;
trace!("attached");
fd = self.control.open_raw_socket().await;
reattach(&status);
}

View File

@ -10,6 +10,7 @@ use core::str::FromStr;
use defmt::{info, unwrap, warn};
use embassy_executor::Spawner;
use embassy_net::{Ipv4Address, Ipv4Cidr, Stack, StackResources};
use embassy_net_nrf91::context::Status;
use embassy_net_nrf91::{context, Runner, State, TraceBuffer, TraceReader};
use embassy_nrf::buffered_uarte::{self, BufferedUarteTx};
use embassy_nrf::gpio::{AnyPin, Level, Output, OutputDrive, Pin};
@ -58,34 +59,38 @@ async fn control_task(
unwrap!(
control
.run(&config, |status| {
let Some(IpAddr::V4(addr)) = status.ip else {
panic!("Unexpected IP address");
};
let addr = Ipv4Address(addr.octets());
let gateway = if let Some(IpAddr::V4(addr)) = status.gateway {
Some(Ipv4Address(addr.octets()))
} else {
None
};
let mut dns_servers = Vec::new();
for dns in status.dns.iter() {
if let IpAddr::V4(ip) = dns {
unwrap!(dns_servers.push(Ipv4Address(ip.octets())));
}
}
stack.set_config_v4(embassy_net::ConfigV4::Static(embassy_net::StaticConfigV4 {
address: Ipv4Cidr::new(addr, 32),
gateway,
dns_servers,
}));
stack.set_config_v4(status_to_config(status));
})
.await
);
}
fn status_to_config(status: &Status) -> embassy_net::ConfigV4 {
let Some(IpAddr::V4(addr)) = status.ip else {
panic!("Unexpected IP address");
};
let addr = Ipv4Address(addr.octets());
let gateway = if let Some(IpAddr::V4(addr)) = status.gateway {
Some(Ipv4Address(addr.octets()))
} else {
None
};
let mut dns_servers = Vec::new();
for dns in status.dns.iter() {
if let IpAddr::V4(ip) = dns {
unwrap!(dns_servers.push(Ipv4Address(ip.octets())));
}
}
embassy_net::ConfigV4::Static(embassy_net::StaticConfigV4 {
address: Ipv4Cidr::new(addr, 32),
gateway,
dns_servers,
})
}
#[embassy_executor::task]
async fn blink_task(pin: AnyPin) {
let mut led = Output::new(pin, Level::Low, OutputDrive::Standard);
@ -193,7 +198,6 @@ async fn main(spawner: Spawner) {
info!("txd: {}", core::str::from_utf8(msg).unwrap());
Timer::after_secs(1).await;
}
// Test auto-attach
unwrap!(control.detach().await);
Timer::after_secs(4).await;
}
}