diff options
| -rw-r--r-- | embassy-net-nrf91/src/context.rs | 20 | ||||
| -rw-r--r-- | examples/nrf9160/src/bin/modem_tcp_client.rs | 54 |
2 files changed, 41 insertions, 33 deletions
diff --git a/embassy-net-nrf91/src/context.rs b/embassy-net-nrf91/src/context.rs index 511468316..6b841aa16 100644 --- a/embassy-net-nrf91/src/context.rs +++ b/embassy-net-nrf91/src/context.rs | |||
| @@ -275,24 +275,28 @@ impl<'a> Control<'a> { | |||
| 275 | }) | 275 | }) |
| 276 | } | 276 | } |
| 277 | 277 | ||
| 278 | /// Run a control loop for this context, ensuring that reaattach is handled. | 278 | async fn wait_attached(&self) -> Result<Status, Error> { |
| 279 | pub async fn run<F: Fn(&Status)>(&self, config: &Config<'_>, reattach: F) -> Result<(), Error> { | ||
| 280 | self.configure(config).await?; | ||
| 281 | while !self.attached().await? { | 279 | while !self.attached().await? { |
| 282 | Timer::after(Duration::from_secs(1)).await; | 280 | Timer::after(Duration::from_secs(1)).await; |
| 283 | } | 281 | } |
| 284 | let status = self.status().await?; | 282 | let status = self.status().await?; |
| 283 | Ok(status) | ||
| 284 | } | ||
| 285 | |||
| 286 | /// Run a control loop for this context, ensuring that reaattach is handled. | ||
| 287 | pub async fn run<F: Fn(&Status)>(&self, config: &Config<'_>, reattach: F) -> Result<(), Error> { | ||
| 288 | self.configure(config).await?; | ||
| 289 | let status = self.wait_attached().await?; | ||
| 285 | let mut fd = self.control.open_raw_socket().await; | 290 | let mut fd = self.control.open_raw_socket().await; |
| 286 | reattach(&status); | 291 | reattach(&status); |
| 287 | 292 | ||
| 288 | loop { | 293 | loop { |
| 289 | if !self.attached().await? { | 294 | if !self.attached().await? { |
| 295 | trace!("detached"); | ||
| 296 | |||
| 290 | self.control.close_raw_socket(fd).await; | 297 | self.control.close_raw_socket(fd).await; |
| 291 | self.attach().await?; | 298 | let status = self.wait_attached().await?; |
| 292 | while !self.attached().await? { | 299 | trace!("attached"); |
| 293 | Timer::after(Duration::from_secs(1)).await; | ||
| 294 | } | ||
| 295 | let status = self.status().await?; | ||
| 296 | fd = self.control.open_raw_socket().await; | 300 | fd = self.control.open_raw_socket().await; |
| 297 | reattach(&status); | 301 | reattach(&status); |
| 298 | } | 302 | } |
diff --git a/examples/nrf9160/src/bin/modem_tcp_client.rs b/examples/nrf9160/src/bin/modem_tcp_client.rs index fb14b746f..b7d56802d 100644 --- a/examples/nrf9160/src/bin/modem_tcp_client.rs +++ b/examples/nrf9160/src/bin/modem_tcp_client.rs | |||
| @@ -10,6 +10,7 @@ use core::str::FromStr; | |||
| 10 | use defmt::{info, unwrap, warn}; | 10 | use defmt::{info, unwrap, warn}; |
| 11 | use embassy_executor::Spawner; | 11 | use embassy_executor::Spawner; |
| 12 | use embassy_net::{Ipv4Address, Ipv4Cidr, Stack, StackResources}; | 12 | use embassy_net::{Ipv4Address, Ipv4Cidr, Stack, StackResources}; |
| 13 | use embassy_net_nrf91::context::Status; | ||
| 13 | use embassy_net_nrf91::{context, Runner, State, TraceBuffer, TraceReader}; | 14 | use embassy_net_nrf91::{context, Runner, State, TraceBuffer, TraceReader}; |
| 14 | use embassy_nrf::buffered_uarte::{self, BufferedUarteTx}; | 15 | use embassy_nrf::buffered_uarte::{self, BufferedUarteTx}; |
| 15 | use embassy_nrf::gpio::{AnyPin, Level, Output, OutputDrive, Pin}; | 16 | use embassy_nrf::gpio::{AnyPin, Level, Output, OutputDrive, Pin}; |
| @@ -58,34 +59,38 @@ async fn control_task( | |||
| 58 | unwrap!( | 59 | unwrap!( |
| 59 | control | 60 | control |
| 60 | .run(&config, |status| { | 61 | .run(&config, |status| { |
| 61 | let Some(IpAddr::V4(addr)) = status.ip else { | 62 | stack.set_config_v4(status_to_config(status)); |
| 62 | panic!("Unexpected IP address"); | ||
| 63 | }; | ||
| 64 | let addr = Ipv4Address(addr.octets()); | ||
| 65 | |||
| 66 | let gateway = if let Some(IpAddr::V4(addr)) = status.gateway { | ||
| 67 | Some(Ipv4Address(addr.octets())) | ||
| 68 | } else { | ||
| 69 | None | ||
| 70 | }; | ||
| 71 | |||
| 72 | let mut dns_servers = Vec::new(); | ||
| 73 | for dns in status.dns.iter() { | ||
| 74 | if let IpAddr::V4(ip) = dns { | ||
| 75 | unwrap!(dns_servers.push(Ipv4Address(ip.octets()))); | ||
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | stack.set_config_v4(embassy_net::ConfigV4::Static(embassy_net::StaticConfigV4 { | ||
| 80 | address: Ipv4Cidr::new(addr, 32), | ||
| 81 | gateway, | ||
| 82 | dns_servers, | ||
| 83 | })); | ||
| 84 | }) | 63 | }) |
| 85 | .await | 64 | .await |
| 86 | ); | 65 | ); |
| 87 | } | 66 | } |
| 88 | 67 | ||
| 68 | fn status_to_config(status: &Status) -> embassy_net::ConfigV4 { | ||
| 69 | let Some(IpAddr::V4(addr)) = status.ip else { | ||
| 70 | panic!("Unexpected IP address"); | ||
| 71 | }; | ||
| 72 | let addr = Ipv4Address(addr.octets()); | ||
| 73 | |||
| 74 | let gateway = if let Some(IpAddr::V4(addr)) = status.gateway { | ||
| 75 | Some(Ipv4Address(addr.octets())) | ||
| 76 | } else { | ||
| 77 | None | ||
| 78 | }; | ||
| 79 | |||
| 80 | let mut dns_servers = Vec::new(); | ||
| 81 | for dns in status.dns.iter() { | ||
| 82 | if let IpAddr::V4(ip) = dns { | ||
| 83 | unwrap!(dns_servers.push(Ipv4Address(ip.octets()))); | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | embassy_net::ConfigV4::Static(embassy_net::StaticConfigV4 { | ||
| 88 | address: Ipv4Cidr::new(addr, 32), | ||
| 89 | gateway, | ||
| 90 | dns_servers, | ||
| 91 | }) | ||
| 92 | } | ||
| 93 | |||
| 89 | #[embassy_executor::task] | 94 | #[embassy_executor::task] |
| 90 | async fn blink_task(pin: AnyPin) { | 95 | async fn blink_task(pin: AnyPin) { |
| 91 | let mut led = Output::new(pin, Level::Low, OutputDrive::Standard); | 96 | let mut led = Output::new(pin, Level::Low, OutputDrive::Standard); |
| @@ -193,7 +198,6 @@ async fn main(spawner: Spawner) { | |||
| 193 | info!("txd: {}", core::str::from_utf8(msg).unwrap()); | 198 | info!("txd: {}", core::str::from_utf8(msg).unwrap()); |
| 194 | Timer::after_secs(1).await; | 199 | Timer::after_secs(1).await; |
| 195 | } | 200 | } |
| 196 | // Test auto-attach | 201 | Timer::after_secs(4).await; |
| 197 | unwrap!(control.detach().await); | ||
| 198 | } | 202 | } |
| 199 | } | 203 | } |
