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.rs27
1 files changed, 16 insertions, 11 deletions
diff --git a/examples/std/src/bin/net.rs b/examples/std/src/bin/net.rs
index 344a6e4d3..73c8e1494 100644
--- a/examples/std/src/bin/net.rs
+++ b/examples/std/src/bin/net.rs
@@ -18,7 +18,8 @@ mod tuntap;
18use crate::tuntap::TunTapDevice; 18use crate::tuntap::TunTapDevice;
19 19
20static DEVICE: Forever<TunTapDevice> = Forever::new(); 20static DEVICE: Forever<TunTapDevice> = Forever::new();
21static CONFIG: Forever<DhcpConfigurator> = Forever::new(); 21static CONFIG_STATIC: Forever<StaticConfigurator> = Forever::new();
22static CONFIG_DYNAMIC: Forever<DhcpConfigurator> = Forever::new();
22static NET_RESOURCES: Forever<StackResources<1, 2, 8>> = Forever::new(); 23static NET_RESOURCES: Forever<StackResources<1, 2, 8>> = Forever::new();
23 24
24#[derive(Clap)] 25#[derive(Clap)]
@@ -28,6 +29,9 @@ struct Opts {
28 /// TAP device name 29 /// TAP device name
29 #[clap(long, default_value = "tap0")] 30 #[clap(long, default_value = "tap0")]
30 tap: String, 31 tap: String,
32 /// use a static IP instead of DHCP
33 #[clap(long)]
34 static_ip: bool,
31} 35}
32 36
33#[embassy::task] 37#[embassy::task]
@@ -42,20 +46,21 @@ async fn main_task(spawner: Spawner) {
42 // Init network device 46 // Init network device
43 let device = TunTapDevice::new(&opts.tap).unwrap(); 47 let device = TunTapDevice::new(&opts.tap).unwrap();
44 48
45 // Static IP configuration 49 // Choose between dhcp or static ip
46 let config = StaticConfigurator::new(Config { 50 let config: &'static mut dyn Configurator = if opts.static_ip {
47 address: Ipv4Cidr::new(Ipv4Address::new(192, 168, 69, 2), 24), 51 CONFIG_STATIC.put(StaticConfigurator::new(Config {
48 dns_servers: Vec::new(), 52 address: Ipv4Cidr::new(Ipv4Address::new(192, 168, 69, 2), 24),
49 gateway: Some(Ipv4Address::new(192, 168, 69, 1)), 53 dns_servers: Vec::new(),
50 }); 54 gateway: Some(Ipv4Address::new(192, 168, 69, 1)),
51 55 }))
52 // DHCP configruation 56 } else {
53 let config = DhcpConfigurator::new(); 57 CONFIG_DYNAMIC.put(DhcpConfigurator::new())
58 };
54 59
55 let net_resources = StackResources::new(); 60 let net_resources = StackResources::new();
56 61
57 // Init network stack 62 // Init network stack
58 embassy_net::init(DEVICE.put(device), CONFIG.put(config), NET_RESOURCES.put(net_resources)); 63 embassy_net::init(DEVICE.put(device), config, NET_RESOURCES.put(net_resources));
59 64
60 // Launch network task 65 // Launch network task
61 spawner.spawn(net_task()).unwrap(); 66 spawner.spawn(net_task()).unwrap();