diff options
| author | Dario Nieuwenhuis <[email protected]> | 2023-02-10 23:53:25 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-02-10 23:53:25 +0100 |
| commit | e1eac15c429f88b1176109d6ce42185e2774ac86 (patch) | |
| tree | 4e0ac99c95ab37a2aecd179b09f82953edbabb69 /examples | |
| parent | 20c1dd112cd3e9b23cce853ce1f0d0d32b29a18a (diff) | |
| parent | 76642b3a3cddaa226dcd741d5f9f8e9d01c2f3ac (diff) | |
Merge pull request #1185 from embassy-rs/dns-impl
Add DNS socket to embassy-net
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/nrf52840/src/bin/usb_ethernet.rs | 23 | ||||
| -rw-r--r-- | examples/std/Cargo.toml | 2 | ||||
| -rw-r--r-- | examples/std/src/bin/net_dns.rs | 98 | ||||
| -rw-r--r-- | examples/stm32h7/Cargo.toml | 2 |
4 files changed, 123 insertions, 2 deletions
diff --git a/examples/nrf52840/src/bin/usb_ethernet.rs b/examples/nrf52840/src/bin/usb_ethernet.rs index 979780896..430468adf 100644 --- a/examples/nrf52840/src/bin/usb_ethernet.rs +++ b/examples/nrf52840/src/bin/usb_ethernet.rs | |||
| @@ -46,8 +46,31 @@ async fn net_task(stack: &'static Stack<Device<'static, MTU>>) -> ! { | |||
| 46 | stack.run().await | 46 | stack.run().await |
| 47 | } | 47 | } |
| 48 | 48 | ||
| 49 | #[inline(never)] | ||
| 50 | pub fn test_function() -> (usize, u32, [u32; 2]) { | ||
| 51 | let mut array = [3; 2]; | ||
| 52 | |||
| 53 | let mut index = 0; | ||
| 54 | let mut result = 0; | ||
| 55 | |||
| 56 | for x in [1, 2] { | ||
| 57 | if x == 1 { | ||
| 58 | array[1] = 99; | ||
| 59 | } else { | ||
| 60 | index = if x == 2 { 1 } else { 0 }; | ||
| 61 | |||
| 62 | // grabs value from array[0], not array[1] | ||
| 63 | result = array[index]; | ||
| 64 | } | ||
| 65 | } | ||
| 66 | |||
| 67 | (index, result, array) | ||
| 68 | } | ||
| 69 | |||
| 49 | #[embassy_executor::main] | 70 | #[embassy_executor::main] |
| 50 | async fn main(spawner: Spawner) { | 71 | async fn main(spawner: Spawner) { |
| 72 | info!("{:?}", test_function()); | ||
| 73 | |||
| 51 | let p = embassy_nrf::init(Default::default()); | 74 | let p = embassy_nrf::init(Default::default()); |
| 52 | let clock: pac::CLOCK = unsafe { mem::transmute(()) }; | 75 | let clock: pac::CLOCK = unsafe { mem::transmute(()) }; |
| 53 | 76 | ||
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" | |||
| 8 | embassy-sync = { version = "0.1.0", path = "../../embassy-sync", features = ["log"] } | 8 | embassy-sync = { version = "0.1.0", path = "../../embassy-sync", features = ["log"] } |
| 9 | embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["log", "std", "nightly", "integrated-timers"] } | 9 | embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["log", "std", "nightly", "integrated-timers"] } |
| 10 | embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["log", "std", "nightly"] } | 10 | embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["log", "std", "nightly"] } |
| 11 | embassy-net = { version = "0.1.0", path = "../../embassy-net", features=[ "std", "nightly", "log", "medium-ethernet", "tcp", "udp", "dhcpv4"] } | 11 | embassy-net = { version = "0.1.0", path = "../../embassy-net", features=[ "std", "nightly", "log", "medium-ethernet", "tcp", "udp", "dns", "dhcpv4", "unstable-traits", "proto-ipv6"] } |
| 12 | embassy-net-driver = { version = "0.1.0", path = "../../embassy-net-driver" } | 12 | embassy-net-driver = { version = "0.1.0", path = "../../embassy-net-driver" } |
| 13 | embedded-io = { version = "0.4.0", features = ["async", "std", "futures"] } | 13 | embedded-io = { version = "0.4.0", features = ["async", "std", "futures"] } |
| 14 | critical-section = { version = "1.1", features = ["std"] } | 14 | critical-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..e1cc45a38 --- /dev/null +++ b/examples/std/src/bin/net_dns.rs | |||
| @@ -0,0 +1,98 @@ | |||
| 1 | #![feature(type_alias_impl_trait)] | ||
| 2 | |||
| 3 | use std::default::Default; | ||
| 4 | |||
| 5 | use clap::Parser; | ||
| 6 | use embassy_executor::{Executor, Spawner}; | ||
| 7 | use embassy_net::dns::DnsQueryType; | ||
| 8 | use embassy_net::{Config, Ipv4Address, Ipv4Cidr, Stack, StackResources}; | ||
| 9 | use heapless::Vec; | ||
| 10 | use log::*; | ||
| 11 | use rand_core::{OsRng, RngCore}; | ||
| 12 | use static_cell::StaticCell; | ||
| 13 | |||
| 14 | #[path = "../tuntap.rs"] | ||
| 15 | mod tuntap; | ||
| 16 | |||
| 17 | use crate::tuntap::TunTapDevice; | ||
| 18 | |||
| 19 | macro_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")] | ||
| 29 | struct 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] | ||
| 39 | async fn net_task(stack: &'static Stack<TunTapDevice>) -> ! { | ||
| 40 | stack.run().await | ||
| 41 | } | ||
| 42 | |||
| 43 | #[embassy_executor::task] | ||
| 44 | async 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: &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 | let host = "example.com"; | ||
| 74 | info!("querying host {:?}...", host); | ||
| 75 | match stack.dns_query(host, DnsQueryType::A).await { | ||
| 76 | Ok(r) => { | ||
| 77 | info!("query response: {:?}", r); | ||
| 78 | } | ||
| 79 | Err(e) => { | ||
| 80 | warn!("query error: {:?}", e); | ||
| 81 | } | ||
| 82 | }; | ||
| 83 | } | ||
| 84 | |||
| 85 | static EXECUTOR: StaticCell<Executor> = StaticCell::new(); | ||
| 86 | |||
| 87 | fn main() { | ||
| 88 | env_logger::builder() | ||
| 89 | .filter_level(log::LevelFilter::Debug) | ||
| 90 | .filter_module("async_io", log::LevelFilter::Info) | ||
| 91 | .format_timestamp_nanos() | ||
| 92 | .init(); | ||
| 93 | |||
| 94 | let executor = EXECUTOR.init(Executor::new()); | ||
| 95 | executor.run(|spawner| { | ||
| 96 | spawner.spawn(main_task(spawner)).unwrap(); | ||
| 97 | }); | ||
| 98 | } | ||
diff --git a/examples/stm32h7/Cargo.toml b/examples/stm32h7/Cargo.toml index bcf976416..a04134789 100644 --- a/examples/stm32h7/Cargo.toml +++ b/examples/stm32h7/Cargo.toml | |||
| @@ -21,7 +21,7 @@ cortex-m-rt = "0.7.0" | |||
| 21 | embedded-hal = "0.2.6" | 21 | embedded-hal = "0.2.6" |
| 22 | embedded-hal-1 = { package = "embedded-hal", version = "=1.0.0-alpha.9" } | 22 | embedded-hal-1 = { package = "embedded-hal", version = "=1.0.0-alpha.9" } |
| 23 | embedded-hal-async = { version = "=0.2.0-alpha.0" } | 23 | embedded-hal-async = { version = "=0.2.0-alpha.0" } |
| 24 | embedded-nal-async = "0.3.0" | 24 | embedded-nal-async = "0.4.0" |
| 25 | panic-probe = { version = "0.3", features = ["print-defmt"] } | 25 | panic-probe = { version = "0.3", features = ["print-defmt"] } |
| 26 | futures = { version = "0.3.17", default-features = false, features = ["async-await"] } | 26 | futures = { version = "0.3.17", default-features = false, features = ["async-await"] } |
| 27 | heapless = { version = "0.7.5", default-features = false } | 27 | heapless = { version = "0.7.5", default-features = false } |
