Mintor fixes after testing re-attach
This commit is contained in:
parent
ccfa6264b0
commit
836e8add1b
@ -275,24 +275,28 @@ impl<'a> Control<'a> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Run a control loop for this context, ensuring that reaattach is handled.
|
async fn wait_attached(&self) -> Result<Status, Error> {
|
||||||
pub async fn run<F: Fn(&Status)>(&self, config: &Config<'_>, reattach: F) -> Result<(), Error> {
|
|
||||||
self.configure(config).await?;
|
|
||||||
while !self.attached().await? {
|
while !self.attached().await? {
|
||||||
Timer::after(Duration::from_secs(1)).await;
|
Timer::after(Duration::from_secs(1)).await;
|
||||||
}
|
}
|
||||||
let status = self.status().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;
|
let mut fd = self.control.open_raw_socket().await;
|
||||||
reattach(&status);
|
reattach(&status);
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
if !self.attached().await? {
|
if !self.attached().await? {
|
||||||
|
trace!("detached");
|
||||||
|
|
||||||
self.control.close_raw_socket(fd).await;
|
self.control.close_raw_socket(fd).await;
|
||||||
self.attach().await?;
|
let status = self.wait_attached().await?;
|
||||||
while !self.attached().await? {
|
trace!("attached");
|
||||||
Timer::after(Duration::from_secs(1)).await;
|
|
||||||
}
|
|
||||||
let status = self.status().await?;
|
|
||||||
fd = self.control.open_raw_socket().await;
|
fd = self.control.open_raw_socket().await;
|
||||||
reattach(&status);
|
reattach(&status);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -10,6 +10,7 @@ use core::str::FromStr;
|
|||||||
use defmt::{info, unwrap, warn};
|
use defmt::{info, unwrap, warn};
|
||||||
use embassy_executor::Spawner;
|
use embassy_executor::Spawner;
|
||||||
use embassy_net::{Ipv4Address, Ipv4Cidr, Stack, StackResources};
|
use embassy_net::{Ipv4Address, Ipv4Cidr, Stack, StackResources};
|
||||||
|
use embassy_net_nrf91::context::Status;
|
||||||
use embassy_net_nrf91::{context, Runner, State, TraceBuffer, TraceReader};
|
use embassy_net_nrf91::{context, Runner, State, TraceBuffer, TraceReader};
|
||||||
use embassy_nrf::buffered_uarte::{self, BufferedUarteTx};
|
use embassy_nrf::buffered_uarte::{self, BufferedUarteTx};
|
||||||
use embassy_nrf::gpio::{AnyPin, Level, Output, OutputDrive, Pin};
|
use embassy_nrf::gpio::{AnyPin, Level, Output, OutputDrive, Pin};
|
||||||
@ -58,34 +59,38 @@ async fn control_task(
|
|||||||
unwrap!(
|
unwrap!(
|
||||||
control
|
control
|
||||||
.run(&config, |status| {
|
.run(&config, |status| {
|
||||||
let Some(IpAddr::V4(addr)) = status.ip else {
|
stack.set_config_v4(status_to_config(status));
|
||||||
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,
|
|
||||||
}));
|
|
||||||
})
|
})
|
||||||
.await
|
.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]
|
#[embassy_executor::task]
|
||||||
async fn blink_task(pin: AnyPin) {
|
async fn blink_task(pin: AnyPin) {
|
||||||
let mut led = Output::new(pin, Level::Low, OutputDrive::Standard);
|
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());
|
info!("txd: {}", core::str::from_utf8(msg).unwrap());
|
||||||
Timer::after_secs(1).await;
|
Timer::after_secs(1).await;
|
||||||
}
|
}
|
||||||
// Test auto-attach
|
Timer::after_secs(4).await;
|
||||||
unwrap!(control.detach().await);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user