aboutsummaryrefslogtreecommitdiff
path: root/examples/std
diff options
context:
space:
mode:
Diffstat (limited to 'examples/std')
-rw-r--r--examples/std/Cargo.toml2
-rw-r--r--examples/std/src/bin/net_dns.rs102
2 files changed, 103 insertions, 1 deletions
diff --git a/examples/std/Cargo.toml b/examples/std/Cargo.toml
index af1481e08..8087df09a 100644
--- a/examples/std/Cargo.toml
+++ b/examples/std/Cargo.toml
@@ -8,7 +8,7 @@ license = "MIT OR Apache-2.0"
8embassy-sync = { version = "0.1.0", path = "../../embassy-sync", features = ["log"] } 8embassy-sync = { version = "0.1.0", path = "../../embassy-sync", features = ["log"] }
9embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["log", "std", "nightly", "integrated-timers"] } 9embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["log", "std", "nightly", "integrated-timers"] }
10embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["log", "std", "nightly"] } 10embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["log", "std", "nightly"] }
11embassy-net = { version = "0.1.0", path = "../../embassy-net", features=[ "std", "nightly", "log", "medium-ethernet", "tcp", "udp", "dhcpv4"] } 11embassy-net = { version = "0.1.0", path = "../../embassy-net", features=[ "std", "nightly", "log", "medium-ethernet", "tcp", "udp", "dns", "dhcpv4", "unstable-traits", "proto-ipv6"] }
12embassy-net-driver = { version = "0.1.0", path = "../../embassy-net-driver" } 12embassy-net-driver = { version = "0.1.0", path = "../../embassy-net-driver" }
13embedded-io = { version = "0.4.0", features = ["async", "std", "futures"] } 13embedded-io = { version = "0.4.0", features = ["async", "std", "futures"] }
14critical-section = { version = "1.1", features = ["std"] } 14critical-section = { version = "1.1", features = ["std"] }
diff --git a/examples/std/src/bin/net_dns.rs b/examples/std/src/bin/net_dns.rs
new file mode 100644
index 000000000..6203f8370
--- /dev/null
+++ b/examples/std/src/bin/net_dns.rs
@@ -0,0 +1,102 @@
1#![feature(type_alias_impl_trait)]
2
3use std::default::Default;
4
5use clap::Parser;
6use embassy_executor::{Executor, Spawner};
7use embassy_net::dns::{DnsQueryType, DnsSocket};
8use embassy_net::{Config, Ipv4Address, Ipv4Cidr, Stack, StackResources};
9use heapless::Vec;
10use log::*;
11use rand_core::{OsRng, RngCore};
12use static_cell::StaticCell;
13
14#[path = "../tuntap.rs"]
15mod tuntap;
16
17use crate::tuntap::TunTapDevice;
18
19macro_rules! singleton {
20 ($val:expr) => {{
21 type T = impl Sized;
22 static STATIC_CELL: StaticCell<T> = StaticCell::new();
23 STATIC_CELL.init_with(move || $val)
24 }};
25}
26
27#[derive(Parser)]
28#[clap(version = "1.0")]
29struct Opts {
30 /// TAP device name
31 #[clap(long, default_value = "tap0")]
32 tap: String,
33 /// use a static IP instead of DHCP
34 #[clap(long)]
35 static_ip: bool,
36}
37
38#[embassy_executor::task]
39async fn net_task(stack: &'static Stack<TunTapDevice>) -> ! {
40 stack.run().await
41}
42
43#[embassy_executor::task]
44async fn main_task(spawner: Spawner) {
45 let opts: Opts = Opts::parse();
46
47 // Init network device
48 let device = TunTapDevice::new(&opts.tap).unwrap();
49
50 // Choose between dhcp or static ip
51 let config = if opts.static_ip {
52 Config::Static(embassy_net::StaticConfig {
53 address: Ipv4Cidr::new(Ipv4Address::new(192, 168, 69, 1), 24),
54 dns_servers: Vec::from_slice(&[Ipv4Address::new(8, 8, 4, 4).into(), Ipv4Address::new(8, 8, 8, 8).into()])
55 .unwrap(),
56 gateway: Some(Ipv4Address::new(192, 168, 69, 100)),
57 })
58 } else {
59 Config::Dhcp(Default::default())
60 };
61
62 // Generate random seed
63 let mut seed = [0; 8];
64 OsRng.fill_bytes(&mut seed);
65 let seed = u64::from_le_bytes(seed);
66
67 // Init network stack
68 let stack = &*singleton!(Stack::new(device, config, singleton!(StackResources::<2>::new()), seed));
69
70 // Launch network task
71 spawner.spawn(net_task(stack)).unwrap();
72
73 // Then we can use it!
74
75 let mut socket = DnsSocket::new(stack, vec![]);
76
77 let host = "example.com";
78 info!("querying host {:?}...", host);
79 match socket.query(host, DnsQueryType::A).await {
80 Ok(r) => {
81 info!("query response: {:?}", r);
82 }
83 Err(e) => {
84 warn!("query error: {:?}", e);
85 }
86 };
87}
88
89static EXECUTOR: StaticCell<Executor> = StaticCell::new();
90
91fn main() {
92 env_logger::builder()
93 .filter_level(log::LevelFilter::Debug)
94 .filter_module("async_io", log::LevelFilter::Info)
95 .format_timestamp_nanos()
96 .init();
97
98 let executor = EXECUTOR.init(Executor::new());
99 executor.run(|spawner| {
100 spawner.spawn(main_task(spawner)).unwrap();
101 });
102}