diff options
| author | Ulf Lilleengen <[email protected]> | 2024-09-04 11:04:36 +0200 |
|---|---|---|
| committer | Ulf Lilleengen <[email protected]> | 2024-09-04 11:04:36 +0200 |
| commit | a6db8678eb5a7c9ebcf6449799b4ff525fe52d5f (patch) | |
| tree | 8a044238a1c0ac0c0606aa5acd7f0af7068399e4 /examples | |
| parent | 86a45b47e529864ac5a203b4ad26b9ee62127a1e (diff) | |
Add utility for setting configuration for a context
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/nrf9160/src/bin/modem_tcp_client.rs | 64 |
1 files changed, 22 insertions, 42 deletions
diff --git a/examples/nrf9160/src/bin/modem_tcp_client.rs b/examples/nrf9160/src/bin/modem_tcp_client.rs index b1dac18a1..817ad17c7 100644 --- a/examples/nrf9160/src/bin/modem_tcp_client.rs +++ b/examples/nrf9160/src/bin/modem_tcp_client.rs | |||
| @@ -2,14 +2,15 @@ | |||
| 2 | #![no_main] | 2 | #![no_main] |
| 3 | 3 | ||
| 4 | use core::mem::MaybeUninit; | 4 | use core::mem::MaybeUninit; |
| 5 | use core::net::IpAddr; | ||
| 5 | use core::ptr::addr_of_mut; | 6 | use core::ptr::addr_of_mut; |
| 6 | use core::str::FromStr; | 7 | use core::str::FromStr; |
| 7 | use core::{slice, str}; | 8 | use core::{slice, str}; |
| 8 | 9 | ||
| 9 | use defmt::{assert, *}; | 10 | use defmt::{assert, info, warn, unwrap}; |
| 10 | use embassy_executor::Spawner; | 11 | use embassy_executor::Spawner; |
| 11 | use embassy_net::{Ipv4Address, Ipv4Cidr, Stack, StackResources}; | 12 | use embassy_net::{Ipv4Address, Ipv4Cidr, Stack, StackResources}; |
| 12 | use embassy_net_nrf91::{Runner, State}; | 13 | use embassy_net_nrf91::{Runner, State, context}; |
| 13 | use embassy_nrf::buffered_uarte::{self, BufferedUarteTx}; | 14 | use embassy_nrf::buffered_uarte::{self, BufferedUarteTx}; |
| 14 | use embassy_nrf::gpio::{AnyPin, Level, Output, OutputDrive, Pin}; | 15 | use embassy_nrf::gpio::{AnyPin, Level, Output, OutputDrive, Pin}; |
| 15 | use embassy_nrf::uarte::Baudrate; | 16 | use embassy_nrf::uarte::Baudrate; |
| @@ -63,9 +64,9 @@ async fn blink_task(pin: AnyPin) { | |||
| 63 | let mut led = Output::new(pin, Level::Low, OutputDrive::Standard); | 64 | let mut led = Output::new(pin, Level::Low, OutputDrive::Standard); |
| 64 | loop { | 65 | loop { |
| 65 | led.set_high(); | 66 | led.set_high(); |
| 66 | Timer::after_millis(100).await; | 67 | Timer::after_millis(1000).await; |
| 67 | led.set_low(); | 68 | led.set_low(); |
| 68 | Timer::after_millis(100).await; | 69 | Timer::after_millis(1000).await; |
| 69 | } | 70 | } |
| 70 | } | 71 | } |
| 71 | 72 | ||
| @@ -123,51 +124,30 @@ async fn main(spawner: Spawner) { | |||
| 123 | 124 | ||
| 124 | unwrap!(spawner.spawn(net_task(stack))); | 125 | unwrap!(spawner.spawn(net_task(stack))); |
| 125 | 126 | ||
| 126 | control.wait_init().await; | 127 | let control = context::Control::new(control, 0).await; |
| 127 | info!("INIT OK"); | ||
| 128 | 128 | ||
| 129 | let mut buf = [0u8; 256]; | 129 | unwrap!(control.configure(context::Config { |
| 130 | 130 | gateway: "iot.nat.es", | |
| 131 | let n = control.at_command(b"AT+CFUN?", &mut buf).await; | 131 | auth_prot: context::AuthProt::Pap, |
| 132 | info!("AT resp: '{}'", unsafe { str::from_utf8_unchecked(&buf[..n]) }); | 132 | auth: Some(("orange", "orange")), |
| 133 | 133 | }).await); | |
| 134 | let n = control | ||
| 135 | .at_command(b"AT+CGDCONT=0,\"IP\",\"iot.nat.es\"", &mut buf) | ||
| 136 | .await; | ||
| 137 | info!("AT resp: '{}'", unsafe { str::from_utf8_unchecked(&buf[..n]) }); | ||
| 138 | let n = control | ||
| 139 | .at_command(b"AT+CGAUTH=0,1,\"orange\",\"orange\"", &mut buf) | ||
| 140 | .await; | ||
| 141 | info!("AT resp: '{}'", unsafe { str::from_utf8_unchecked(&buf[..n]) }); | ||
| 142 | |||
| 143 | let n = control.at_command(b"AT+CFUN=1", &mut buf).await; | ||
| 144 | info!("AT resp: '{}'", unsafe { str::from_utf8_unchecked(&buf[..n]) }); | ||
| 145 | 134 | ||
| 146 | info!("waiting for attach..."); | 135 | info!("waiting for attach..."); |
| 147 | loop { | ||
| 148 | Timer::after_millis(500).await; | ||
| 149 | let n = control.at_command(b"AT+CGATT?", &mut buf).await; | ||
| 150 | let mut res = &buf[..n]; | ||
| 151 | pop_prefix(&mut res, b"+CGATT: "); | ||
| 152 | let res = split_field(&mut res); | ||
| 153 | info!("AT resp field: '{}'", unsafe { str::from_utf8_unchecked(res) }); | ||
| 154 | if res == b"1" { | ||
| 155 | break; | ||
| 156 | } | ||
| 157 | } | ||
| 158 | 136 | ||
| 159 | let n = control.at_command(b"AT+CGPADDR=0", &mut buf).await; | 137 | let mut status = unwrap!(control.status().await); |
| 160 | let mut res = &buf[..n]; | 138 | while !status.attached && status.ip.is_none() { |
| 161 | pop_prefix(&mut res, b"+CGPADDR: 0,"); | 139 | Timer::after_millis(1000).await; |
| 162 | let ip = split_field(&mut res); | 140 | status = unwrap!(control.status().await); |
| 163 | let ip = Ipv4Address::from_str(unsafe { str::from_utf8_unchecked(ip) }).unwrap(); | 141 | info!("STATUS: {:?}", status); |
| 164 | info!("IP: '{}'", ip); | 142 | } |
| 165 | 143 | ||
| 166 | info!("============== OPENING SOCKET"); | 144 | let Some(IpAddr::V4(addr)) = status.ip else { |
| 167 | control.open_raw_socket().await; | 145 | panic!("Unexpected IP address"); |
| 146 | }; | ||
| 147 | let addr = Ipv4Address(addr.octets()); | ||
| 168 | 148 | ||
| 169 | stack.set_config_v4(embassy_net::ConfigV4::Static(embassy_net::StaticConfigV4 { | 149 | stack.set_config_v4(embassy_net::ConfigV4::Static(embassy_net::StaticConfigV4 { |
| 170 | address: Ipv4Cidr::new(ip, 32), | 150 | address: Ipv4Cidr::new(addr, 32), |
| 171 | gateway: None, | 151 | gateway: None, |
| 172 | dns_servers: Default::default(), | 152 | dns_servers: Default::default(), |
| 173 | })); | 153 | })); |
