diff options
| author | Ulf Lilleengen <[email protected]> | 2024-09-04 18:47:26 +0200 |
|---|---|---|
| committer | Ulf Lilleengen <[email protected]> | 2024-09-04 18:47:26 +0200 |
| commit | 372e45dabc0cfd3eb495e902665bb752a67aa804 (patch) | |
| tree | 2d8b5aa066c37b73c857a2aa15f8c47dea06edef /examples/nrf9160 | |
| parent | 49881f6fd1e3d77d63dea2313afb5201eca8ebd9 (diff) | |
Add context run task
Diffstat (limited to 'examples/nrf9160')
| -rw-r--r-- | examples/nrf9160/src/bin/modem_tcp_client.rs | 93 |
1 files changed, 50 insertions, 43 deletions
diff --git a/examples/nrf9160/src/bin/modem_tcp_client.rs b/examples/nrf9160/src/bin/modem_tcp_client.rs index c65e6e153..a6f42eb3b 100644 --- a/examples/nrf9160/src/bin/modem_tcp_client.rs +++ b/examples/nrf9160/src/bin/modem_tcp_client.rs | |||
| @@ -50,6 +50,43 @@ async fn net_task(stack: &'static Stack<embassy_net_nrf91::NetDriver<'static>>) | |||
| 50 | } | 50 | } |
| 51 | 51 | ||
| 52 | #[embassy_executor::task] | 52 | #[embassy_executor::task] |
| 53 | async fn control_task( | ||
| 54 | control: &'static context::Control<'static>, | ||
| 55 | config: context::Config<'static>, | ||
| 56 | stack: &'static Stack<embassy_net_nrf91::NetDriver<'static>>, | ||
| 57 | ) { | ||
| 58 | unwrap!( | ||
| 59 | control | ||
| 60 | .run(&config, |status| { | ||
| 61 | let Some(IpAddr::V4(addr)) = status.ip else { | ||
| 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 | }) | ||
| 85 | .await | ||
| 86 | ); | ||
| 87 | } | ||
| 88 | |||
| 89 | #[embassy_executor::task] | ||
| 53 | async fn blink_task(pin: AnyPin) { | 90 | async fn blink_task(pin: AnyPin) { |
| 54 | let mut led = Output::new(pin, Level::Low, OutputDrive::Standard); | 91 | let mut led = Output::new(pin, Level::Low, OutputDrive::Standard); |
| 55 | loop { | 92 | loop { |
| @@ -117,50 +154,20 @@ async fn main(spawner: Spawner) { | |||
| 117 | 154 | ||
| 118 | unwrap!(spawner.spawn(net_task(stack))); | 155 | unwrap!(spawner.spawn(net_task(stack))); |
| 119 | 156 | ||
| 120 | let control = context::Control::new(control, 0).await; | 157 | static CONTROL: StaticCell<context::Control<'static>> = StaticCell::new(); |
| 158 | let control = CONTROL.init(context::Control::new(control, 0).await); | ||
| 121 | 159 | ||
| 122 | unwrap!( | 160 | unwrap!(spawner.spawn(control_task( |
| 123 | control | 161 | control, |
| 124 | .configure(context::Config { | 162 | context::Config { |
| 125 | apn: "iot.nat.es", | 163 | apn: "iot.nat.es", |
| 126 | auth_prot: context::AuthProt::Pap, | 164 | auth_prot: context::AuthProt::Pap, |
| 127 | auth: Some(("orange", "orange")), | 165 | auth: Some(("orange", "orange")), |
| 128 | }) | 166 | }, |
| 129 | .await | 167 | stack |
| 130 | ); | 168 | ))); |
| 131 | 169 | ||
| 132 | info!("waiting for attach..."); | 170 | stack.wait_config_up().await; |
| 133 | |||
| 134 | let mut status = unwrap!(control.status().await); | ||
| 135 | while !status.attached && status.ip.is_none() { | ||
| 136 | Timer::after_millis(1000).await; | ||
| 137 | status = unwrap!(control.status().await); | ||
| 138 | info!("STATUS: {:?}", status); | ||
| 139 | } | ||
| 140 | |||
| 141 | let Some(IpAddr::V4(addr)) = status.ip else { | ||
| 142 | panic!("Unexpected IP address"); | ||
| 143 | }; | ||
| 144 | let addr = Ipv4Address(addr.octets()); | ||
| 145 | |||
| 146 | let gateway = if let Some(IpAddr::V4(addr)) = status.gateway { | ||
| 147 | Some(Ipv4Address(addr.octets())) | ||
| 148 | } else { | ||
| 149 | None | ||
| 150 | }; | ||
| 151 | |||
| 152 | let mut dns_servers = Vec::new(); | ||
| 153 | for dns in status.dns { | ||
| 154 | if let IpAddr::V4(ip) = dns { | ||
| 155 | unwrap!(dns_servers.push(Ipv4Address(ip.octets()))); | ||
| 156 | } | ||
| 157 | } | ||
| 158 | |||
| 159 | stack.set_config_v4(embassy_net::ConfigV4::Static(embassy_net::StaticConfigV4 { | ||
| 160 | address: Ipv4Cidr::new(addr, 32), | ||
| 161 | gateway, | ||
| 162 | dns_servers, | ||
| 163 | })); | ||
| 164 | 171 | ||
| 165 | let mut rx_buffer = [0; 4096]; | 172 | let mut rx_buffer = [0; 4096]; |
| 166 | let mut tx_buffer = [0; 4096]; | 173 | let mut tx_buffer = [0; 4096]; |
| @@ -172,7 +179,7 @@ async fn main(spawner: Spawner) { | |||
| 172 | let host_addr = embassy_net::Ipv4Address::from_str("45.79.112.203").unwrap(); | 179 | let host_addr = embassy_net::Ipv4Address::from_str("45.79.112.203").unwrap(); |
| 173 | if let Err(e) = socket.connect((host_addr, 4242)).await { | 180 | if let Err(e) = socket.connect((host_addr, 4242)).await { |
| 174 | warn!("connect error: {:?}", e); | 181 | warn!("connect error: {:?}", e); |
| 175 | Timer::after_secs(1).await; | 182 | Timer::after_secs(10).await; |
| 176 | continue; | 183 | continue; |
| 177 | } | 184 | } |
| 178 | info!("Connected to {:?}", socket.remote_endpoint()); | 185 | info!("Connected to {:?}", socket.remote_endpoint()); |
