aboutsummaryrefslogtreecommitdiff
path: root/examples/std/src/bin/net.rs
diff options
context:
space:
mode:
Diffstat (limited to 'examples/std/src/bin/net.rs')
-rw-r--r--examples/std/src/bin/net.rs26
1 files changed, 10 insertions, 16 deletions
diff --git a/examples/std/src/bin/net.rs b/examples/std/src/bin/net.rs
index 9b1450b72..3aadb029d 100644
--- a/examples/std/src/bin/net.rs
+++ b/examples/std/src/bin/net.rs
@@ -1,28 +1,22 @@
1#![feature(type_alias_impl_trait)] 1#![feature(type_alias_impl_trait)]
2 2
3use std::default::Default;
4
3use clap::Parser; 5use clap::Parser;
4use embassy_executor::{Executor, Spawner}; 6use embassy_executor::{Executor, Spawner};
5use embassy_net::tcp::TcpSocket; 7use embassy_net::tcp::TcpSocket;
6use embassy_net::{ConfigStrategy, Ipv4Address, Ipv4Cidr, Stack, StackResources}; 8use embassy_net::{Config, Ipv4Address, Ipv4Cidr, Stack, StackResources};
9use embassy_time::Duration;
7use embedded_io::asynch::Write; 10use embedded_io::asynch::Write;
8use heapless::Vec; 11use heapless::Vec;
9use log::*; 12use log::*;
10use rand_core::{OsRng, RngCore}; 13use rand_core::{OsRng, RngCore};
11use static_cell::StaticCell; 14use static_cell::{make_static, StaticCell};
12 15
13#[path = "../tuntap.rs"] 16#[path = "../tuntap.rs"]
14mod tuntap; 17mod tuntap;
15 18
16use crate::tuntap::TunTapDevice; 19use crate::tuntap::TunTapDevice;
17
18macro_rules! singleton {
19 ($val:expr) => {{
20 type T = impl Sized;
21 static STATIC_CELL: StaticCell<T> = StaticCell::new();
22 STATIC_CELL.init_with(move || $val)
23 }};
24}
25
26#[derive(Parser)] 20#[derive(Parser)]
27#[clap(version = "1.0")] 21#[clap(version = "1.0")]
28struct Opts { 22struct Opts {
@@ -48,13 +42,13 @@ async fn main_task(spawner: Spawner) {
48 42
49 // Choose between dhcp or static ip 43 // Choose between dhcp or static ip
50 let config = if opts.static_ip { 44 let config = if opts.static_ip {
51 ConfigStrategy::Static(embassy_net::Config { 45 Config::ipv4_static(embassy_net::StaticConfigV4 {
52 address: Ipv4Cidr::new(Ipv4Address::new(192, 168, 69, 2), 24), 46 address: Ipv4Cidr::new(Ipv4Address::new(192, 168, 69, 2), 24),
53 dns_servers: Vec::new(), 47 dns_servers: Vec::new(),
54 gateway: Some(Ipv4Address::new(192, 168, 69, 1)), 48 gateway: Some(Ipv4Address::new(192, 168, 69, 1)),
55 }) 49 })
56 } else { 50 } else {
57 ConfigStrategy::Dhcp 51 Config::dhcpv4(Default::default())
58 }; 52 };
59 53
60 // Generate random seed 54 // Generate random seed
@@ -63,10 +57,10 @@ async fn main_task(spawner: Spawner) {
63 let seed = u64::from_le_bytes(seed); 57 let seed = u64::from_le_bytes(seed);
64 58
65 // Init network stack 59 // Init network stack
66 let stack = &*singleton!(Stack::new( 60 let stack = &*make_static!(Stack::new(
67 device, 61 device,
68 config, 62 config,
69 singleton!(StackResources::<1, 2, 8>::new()), 63 make_static!(StackResources::<3>::new()),
70 seed 64 seed
71 )); 65 ));
72 66
@@ -78,7 +72,7 @@ async fn main_task(spawner: Spawner) {
78 let mut tx_buffer = [0; 4096]; 72 let mut tx_buffer = [0; 4096];
79 let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer); 73 let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer);
80 74
81 socket.set_timeout(Some(embassy_net::SmolDuration::from_secs(10))); 75 socket.set_timeout(Some(Duration::from_secs(10)));
82 76
83 let remote_endpoint = (Ipv4Address::new(192, 168, 69, 100), 8000); 77 let remote_endpoint = (Ipv4Address::new(192, 168, 69, 100), 8000);
84 info!("connecting to {:?}...", remote_endpoint); 78 info!("connecting to {:?}...", remote_endpoint);