aboutsummaryrefslogtreecommitdiff
path: root/examples/std/src
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2022-05-23 03:50:43 +0200
committerDario Nieuwenhuis <[email protected]>2022-05-25 19:56:22 +0200
commita5aea995a802fea8fc1b3e4b5fe47bd6d1fca2a4 (patch)
tree0fcb4c01914347eff5b3be44b284aa9432e28678 /examples/std/src
parent36a1f203648dcb402727ea3eb5d30cf1f6993795 (diff)
WIP embassy-net v2
Diffstat (limited to 'examples/std/src')
-rw-r--r--examples/std/src/bin/net.rs51
-rw-r--r--examples/std/src/tuntap.rs2
2 files changed, 28 insertions, 25 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;
4use embassy::executor::{Executor, Spawner}; 4use embassy::executor::{Executor, Spawner};
5use embassy::util::Forever; 5use embassy::util::Forever;
6use embassy_net::tcp::TcpSocket; 6use embassy_net::tcp::TcpSocket;
7use embassy_net::{ 7use embassy_net::{ConfigStrategy, Ipv4Address, Ipv4Cidr, Stack, StackResources};
8 Config, Configurator, DhcpConfigurator, Ipv4Address, Ipv4Cidr, StackResources,
9 StaticConfigurator,
10};
11use embedded_io::asynch::Write; 8use embedded_io::asynch::Write;
12use heapless::Vec; 9use heapless::Vec;
13use log::*; 10use log::*;
11use rand_core::{OsRng, RngCore};
14 12
15#[path = "../tuntap.rs"] 13#[path = "../tuntap.rs"]
16mod tuntap; 14mod tuntap;
17 15
18use crate::tuntap::TunTapDevice; 16use crate::tuntap::TunTapDevice;
19 17
20static DEVICE: Forever<TunTapDevice> = Forever::new(); 18macro_rules! forever {
21static CONFIG_STATIC: Forever<StaticConfigurator> = Forever::new(); 19 ($val:expr) => {{
22static CONFIG_DYNAMIC: Forever<DhcpConfigurator> = Forever::new(); 20 type T = impl Sized;
23static 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]
37async fn net_task() { 38async 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]
92fn _embassy_rand(buf: &mut [u8]) {
93 use rand_core::{OsRng, RngCore};
94 OsRng.fill_bytes(buf);
95}
96
97static EXECUTOR: Forever<Executor> = Forever::new(); 100static EXECUTOR: Forever<Executor> = Forever::new();
98 101
99fn main() { 102fn main() {
diff --git a/examples/std/src/tuntap.rs b/examples/std/src/tuntap.rs
index 09a4be070..b70767a3a 100644
--- a/examples/std/src/tuntap.rs
+++ b/examples/std/src/tuntap.rs
@@ -209,7 +209,7 @@ impl Device for TunTapDevice {
209 } 209 }
210 } 210 }
211 211
212 fn capabilities(&mut self) -> DeviceCapabilities { 212 fn capabilities(&self) -> DeviceCapabilities {
213 let mut caps = DeviceCapabilities::default(); 213 let mut caps = DeviceCapabilities::default();
214 caps.max_transmission_unit = self.device.get_ref().mtu; 214 caps.max_transmission_unit = self.device.get_ref().mtu;
215 caps 215 caps