diff options
| author | Dario Nieuwenhuis <[email protected]> | 2022-05-23 03:50:43 +0200 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2022-05-25 19:56:22 +0200 |
| commit | a5aea995a802fea8fc1b3e4b5fe47bd6d1fca2a4 (patch) | |
| tree | 0fcb4c01914347eff5b3be44b284aa9432e28678 /examples/std/src/bin | |
| parent | 36a1f203648dcb402727ea3eb5d30cf1f6993795 (diff) | |
WIP embassy-net v2
Diffstat (limited to 'examples/std/src/bin')
| -rw-r--r-- | examples/std/src/bin/net.rs | 51 |
1 files changed, 27 insertions, 24 deletions
diff --git a/examples/std/src/bin/net.rs b/examples/std/src/bin/net.rs index daedffb0f..74073ee81 100644 --- a/examples/std/src/bin/net.rs +++ b/examples/std/src/bin/net.rs | |||
| @@ -4,23 +4,24 @@ use clap::Parser; | |||
| 4 | use embassy::executor::{Executor, Spawner}; | 4 | use embassy::executor::{Executor, Spawner}; |
| 5 | use embassy::util::Forever; | 5 | use embassy::util::Forever; |
| 6 | use embassy_net::tcp::TcpSocket; | 6 | use embassy_net::tcp::TcpSocket; |
| 7 | use embassy_net::{ | 7 | use embassy_net::{ConfigStrategy, Ipv4Address, Ipv4Cidr, Stack, StackResources}; |
| 8 | Config, Configurator, DhcpConfigurator, Ipv4Address, Ipv4Cidr, StackResources, | ||
| 9 | StaticConfigurator, | ||
| 10 | }; | ||
| 11 | use embedded_io::asynch::Write; | 8 | use embedded_io::asynch::Write; |
| 12 | use heapless::Vec; | 9 | use heapless::Vec; |
| 13 | use log::*; | 10 | use log::*; |
| 11 | use rand_core::{OsRng, RngCore}; | ||
| 14 | 12 | ||
| 15 | #[path = "../tuntap.rs"] | 13 | #[path = "../tuntap.rs"] |
| 16 | mod tuntap; | 14 | mod tuntap; |
| 17 | 15 | ||
| 18 | use crate::tuntap::TunTapDevice; | 16 | use crate::tuntap::TunTapDevice; |
| 19 | 17 | ||
| 20 | static DEVICE: Forever<TunTapDevice> = Forever::new(); | 18 | macro_rules! forever { |
| 21 | static CONFIG_STATIC: Forever<StaticConfigurator> = Forever::new(); | 19 | ($val:expr) => {{ |
| 22 | static CONFIG_DYNAMIC: Forever<DhcpConfigurator> = Forever::new(); | 20 | type T = impl Sized; |
| 23 | static NET_RESOURCES: Forever<StackResources<1, 2, 8>> = Forever::new(); | 21 | static FOREVER: Forever<T> = Forever::new(); |
| 22 | FOREVER.put_with(move || $val) | ||
| 23 | }}; | ||
| 24 | } | ||
| 24 | 25 | ||
| 25 | #[derive(Parser)] | 26 | #[derive(Parser)] |
| 26 | #[clap(version = "1.0")] | 27 | #[clap(version = "1.0")] |
| @@ -34,8 +35,8 @@ struct Opts { | |||
| 34 | } | 35 | } |
| 35 | 36 | ||
| 36 | #[embassy::task] | 37 | #[embassy::task] |
| 37 | async fn net_task() { | 38 | async fn net_task(stack: &'static Stack<TunTapDevice>) -> ! { |
| 38 | embassy_net::run().await | 39 | stack.run().await |
| 39 | } | 40 | } |
| 40 | 41 | ||
| 41 | #[embassy::task] | 42 | #[embassy::task] |
| @@ -46,28 +47,36 @@ async fn main_task(spawner: Spawner) { | |||
| 46 | let device = TunTapDevice::new(&opts.tap).unwrap(); | 47 | let device = TunTapDevice::new(&opts.tap).unwrap(); |
| 47 | 48 | ||
| 48 | // Choose between dhcp or static ip | 49 | // Choose between dhcp or static ip |
| 49 | let config: &'static mut dyn Configurator = if opts.static_ip { | 50 | let config = if opts.static_ip { |
| 50 | CONFIG_STATIC.put(StaticConfigurator::new(Config { | 51 | ConfigStrategy::Static(embassy_net::Config { |
| 51 | address: Ipv4Cidr::new(Ipv4Address::new(192, 168, 69, 2), 24), | 52 | address: Ipv4Cidr::new(Ipv4Address::new(192, 168, 69, 2), 24), |
| 52 | dns_servers: Vec::new(), | 53 | dns_servers: Vec::new(), |
| 53 | gateway: Some(Ipv4Address::new(192, 168, 69, 1)), | 54 | gateway: Some(Ipv4Address::new(192, 168, 69, 1)), |
| 54 | })) | 55 | }) |
| 55 | } else { | 56 | } else { |
| 56 | CONFIG_DYNAMIC.put(DhcpConfigurator::new()) | 57 | ConfigStrategy::Dhcp |
| 57 | }; | 58 | }; |
| 58 | 59 | ||
| 59 | let net_resources = StackResources::new(); | 60 | // Generate random seed |
| 61 | let mut seed = [0; 8]; | ||
| 62 | OsRng.fill_bytes(&mut seed); | ||
| 63 | let seed = u64::from_le_bytes(seed); | ||
| 60 | 64 | ||
| 61 | // Init network stack | 65 | // Init network stack |
| 62 | embassy_net::init(DEVICE.put(device), config, NET_RESOURCES.put(net_resources)); | 66 | let stack = &*forever!(Stack::new( |
| 67 | device, | ||
| 68 | config, | ||
| 69 | forever!(StackResources::<1, 2, 8>::new()), | ||
| 70 | seed | ||
| 71 | )); | ||
| 63 | 72 | ||
| 64 | // Launch network task | 73 | // Launch network task |
| 65 | spawner.spawn(net_task()).unwrap(); | 74 | spawner.spawn(net_task(stack)).unwrap(); |
| 66 | 75 | ||
| 67 | // Then we can use it! | 76 | // Then we can use it! |
| 68 | let mut rx_buffer = [0; 4096]; | 77 | let mut rx_buffer = [0; 4096]; |
| 69 | let mut tx_buffer = [0; 4096]; | 78 | let mut tx_buffer = [0; 4096]; |
| 70 | let mut socket = TcpSocket::new(&mut rx_buffer, &mut tx_buffer); | 79 | let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer); |
| 71 | 80 | ||
| 72 | socket.set_timeout(Some(embassy_net::SmolDuration::from_secs(10))); | 81 | socket.set_timeout(Some(embassy_net::SmolDuration::from_secs(10))); |
| 73 | 82 | ||
| @@ -88,12 +97,6 @@ async fn main_task(spawner: Spawner) { | |||
| 88 | } | 97 | } |
| 89 | } | 98 | } |
| 90 | 99 | ||
| 91 | #[no_mangle] | ||
| 92 | fn _embassy_rand(buf: &mut [u8]) { | ||
| 93 | use rand_core::{OsRng, RngCore}; | ||
| 94 | OsRng.fill_bytes(buf); | ||
| 95 | } | ||
| 96 | |||
| 97 | static EXECUTOR: Forever<Executor> = Forever::new(); | 100 | static EXECUTOR: Forever<Executor> = Forever::new(); |
| 98 | 101 | ||
| 99 | fn main() { | 102 | fn main() { |
