diff options
| author | Dion Dokter <[email protected]> | 2025-11-20 13:22:38 +0100 |
|---|---|---|
| committer | Dion Dokter <[email protected]> | 2025-11-20 13:22:38 +0100 |
| commit | 4f2c36e447455e8d33607d586859d3d075cabf1d (patch) | |
| tree | 003cd822d688acd7c074dd229663b4648d100f71 /examples/std | |
| parent | 663732d85abbae400f2dbab2c411802a5b60e9b1 (diff) | |
| parent | 661874d11de7d93ed52e08e020a9d4c7ee11122d (diff) | |
Merge branch 'main' into u0-lcd
Diffstat (limited to 'examples/std')
| -rw-r--r-- | examples/std/Cargo.toml | 20 | ||||
| -rw-r--r-- | examples/std/README.md | 125 | ||||
| -rw-r--r-- | examples/std/src/bin/net.rs | 30 | ||||
| -rw-r--r-- | examples/std/src/bin/net_dns.rs | 22 | ||||
| -rw-r--r-- | examples/std/src/bin/net_ppp.rs | 85 | ||||
| -rw-r--r-- | examples/std/src/bin/net_udp.rs | 22 | ||||
| -rw-r--r-- | examples/std/src/bin/serial.rs | 2 | ||||
| -rw-r--r-- | examples/std/src/bin/tcp_accept.rs | 39 | ||||
| -rw-r--r-- | examples/std/src/bin/tick.rs | 2 | ||||
| -rw-r--r-- | examples/std/tap.sh | 7 |
10 files changed, 192 insertions, 162 deletions
diff --git a/examples/std/Cargo.toml b/examples/std/Cargo.toml index 58ea894f3..6dc6a353d 100644 --- a/examples/std/Cargo.toml +++ b/examples/std/Cargo.toml | |||
| @@ -1,16 +1,17 @@ | |||
| 1 | [package] | 1 | [package] |
| 2 | edition = "2021" | 2 | edition = "2024" |
| 3 | name = "embassy-std-examples" | 3 | name = "embassy-std-examples" |
| 4 | version = "0.1.0" | 4 | version = "0.1.0" |
| 5 | license = "MIT OR Apache-2.0" | 5 | license = "MIT OR Apache-2.0" |
| 6 | publish = false | ||
| 6 | 7 | ||
| 7 | [dependencies] | 8 | [dependencies] |
| 8 | embassy-sync = { version = "0.6.0", path = "../../embassy-sync", features = ["log"] } | 9 | embassy-sync = { version = "0.7.2", path = "../../embassy-sync", features = ["log"] } |
| 9 | embassy-executor = { version = "0.5.0", path = "../../embassy-executor", features = ["task-arena-size-32768", "arch-std", "executor-thread", "log", "integrated-timers"] } | 10 | embassy-executor = { version = "0.9.0", path = "../../embassy-executor", features = ["arch-std", "executor-thread", "log"] } |
| 10 | embassy-time = { version = "0.3.1", path = "../../embassy-time", features = ["log", "std", ] } | 11 | embassy-time = { version = "0.5.0", path = "../../embassy-time", features = ["log", "std", ] } |
| 11 | embassy-net = { version = "0.4.0", path = "../../embassy-net", features=[ "std", "log", "medium-ethernet", "medium-ip", "tcp", "udp", "dns", "dhcpv4", "proto-ipv6"] } | 12 | embassy-net = { version = "0.7.1", path = "../../embassy-net", features=[ "log", "medium-ethernet", "medium-ip", "tcp", "udp", "dns", "dhcpv4", "proto-ipv6"] } |
| 12 | embassy-net-tuntap = { version = "0.1.0", path = "../../embassy-net-tuntap" } | 13 | embassy-net-tuntap = { version = "0.1.0", path = "../../embassy-net-tuntap" } |
| 13 | embassy-net-ppp = { version = "0.1.0", path = "../../embassy-net-ppp", features = ["log"]} | 14 | embassy-net-ppp = { version = "0.2.1", path = "../../embassy-net-ppp", features = ["log"]} |
| 14 | embedded-io-async = { version = "0.6.1" } | 15 | embedded-io-async = { version = "0.6.1" } |
| 15 | embedded-io-adapters = { version = "0.6.1", features = ["futures-03"] } | 16 | embedded-io-adapters = { version = "0.6.1", features = ["futures-03"] } |
| 16 | critical-section = { version = "1.1", features = ["std"] } | 17 | critical-section = { version = "1.1", features = ["std"] } |
| @@ -21,9 +22,14 @@ futures = { version = "0.3.17" } | |||
| 21 | log = "0.4.14" | 22 | log = "0.4.14" |
| 22 | nix = "0.26.2" | 23 | nix = "0.26.2" |
| 23 | clap = { version = "3.0.0-beta.5", features = ["derive"] } | 24 | clap = { version = "3.0.0-beta.5", features = ["derive"] } |
| 24 | rand_core = { version = "0.6.3", features = ["std"] } | 25 | rand_core = { version = "0.9.1", features = ["std", "os_rng"] } |
| 25 | heapless = { version = "0.8", default-features = false } | 26 | heapless = { version = "0.8", default-features = false } |
| 26 | static_cell = "2" | 27 | static_cell = "2" |
| 27 | 28 | ||
| 28 | [profile.release] | 29 | [profile.release] |
| 29 | debug = 2 | 30 | debug = 2 |
| 31 | |||
| 32 | [package.metadata.embassy] | ||
| 33 | build = [ | ||
| 34 | { artifact-dir = "out/examples/std" } | ||
| 35 | ] | ||
diff --git a/examples/std/README.md b/examples/std/README.md index e3a59d6ea..ac2c2a1a6 100644 --- a/examples/std/README.md +++ b/examples/std/README.md | |||
| @@ -1,23 +1,128 @@ | |||
| 1 | 1 | ||
| 2 | ## Running the `embassy-net` examples | 2 | ## Running the `embassy-net` examples |
| 3 | 3 | ||
| 4 | First, create the tap0 interface. You only need to do this once. | 4 | To run `net`, `tcp_accept`, `net_udp` and `net_dns` examples you will need a tap interface. Before running these examples, create the tap99 interface. (The number was chosen to |
| 5 | hopefully not collide with anything.) You only need to do this once every time you reboot your computer. | ||
| 5 | 6 | ||
| 6 | ```sh | 7 | ```sh |
| 7 | sudo ip tuntap add name tap0 mode tap user $USER | 8 | cd $EMBASSY_ROOT/examples/std/ |
| 8 | sudo ip link set tap0 up | 9 | sudo sh tap.sh |
| 9 | sudo ip addr add 192.168.69.100/24 dev tap0 | ||
| 10 | sudo ip -6 addr add fe80::100/64 dev tap0 | ||
| 11 | sudo ip -6 addr add fdaa::100/64 dev tap0 | ||
| 12 | sudo ip -6 route add fe80::/64 dev tap0 | ||
| 13 | sudo ip -6 route add fdaa::/64 dev tap0 | ||
| 14 | ``` | 10 | ``` |
| 15 | 11 | ||
| 16 | Second, have something listening there. For example `nc -lp 8000` | 12 | The example `net_ppp` requires different steps that are detailed in its section. |
| 13 | |||
| 14 | ### `net` example | ||
| 15 | |||
| 16 | For this example, you need to have something listening in the correct port. For example `nc -lp 8000`. | ||
| 17 | 17 | ||
| 18 | Then run the example located in the `examples` folder: | 18 | Then run the example located in the `examples` folder: |
| 19 | 19 | ||
| 20 | ```sh | 20 | ```sh |
| 21 | cd $EMBASSY_ROOT/examples/std/ | 21 | cd $EMBASSY_ROOT/examples/std/ |
| 22 | cargo run --bin net -- --static-ip | 22 | cargo run --bin net -- --tap tap99 --static-ip |
| 23 | ``` | ||
| 24 | ### `tcp_accept` example | ||
| 25 | |||
| 26 | This example listen for a tcp connection. | ||
| 27 | |||
| 28 | First run the example located in the `examples` folder: | ||
| 29 | |||
| 30 | ```sh | ||
| 31 | cd $EMBASSY_ROOT/examples/std/ | ||
| 32 | cargo run --bin tcp_accept -- --tap tap99 --static-ip | ||
| 33 | ``` | ||
| 34 | |||
| 35 | Then open a connection to the port. For example `nc 192.168.69.2 9999`. | ||
| 36 | |||
| 37 | ### `net_udp` example | ||
| 38 | |||
| 39 | This example listen for a udp connection. | ||
| 40 | |||
| 41 | First run the example located in the `examples` folder: | ||
| 42 | |||
| 43 | ```sh | ||
| 44 | cd $EMBASSY_ROOT/examples/std/ | ||
| 45 | cargo run --bin net_udp -- --tap tap99 --static-ip | ||
| 46 | ``` | ||
| 47 | |||
| 48 | Then open a connection to the port. For example `nc -u 192.168.69.2 9400`. | ||
| 49 | |||
| 50 | ### `net_dns` example | ||
| 51 | |||
| 52 | This example queries a `DNS` for the IP address of `www.example.com`. | ||
| 53 | |||
| 54 | In order to achieve this, the `tap99` interface requires configuring tap99 as a gateway device temporarily. | ||
| 55 | |||
| 56 | For example, in Ubuntu you can do this by: | ||
| 57 | |||
| 58 | 1. Identifying your default route device. In the next example `eth0` | ||
| 59 | |||
| 60 | ```sh | ||
| 61 | ip r | grep "default" | ||
| 62 | default via 192.168.2.1 dev eth0 proto kernel metric 35 | ||
| 63 | ``` | ||
| 64 | |||
| 65 | 2. Enabling temporarily IP Forwarding: | ||
| 66 | |||
| 67 | ```sh | ||
| 68 | sudo sysctl -w net.ipv4.ip_forward=1 | ||
| 69 | ``` | ||
| 70 | |||
| 71 | 3. Configuring NAT to mascarade traffic from `tap99` to `eth0` | ||
| 72 | |||
| 73 | ```sh | ||
| 74 | sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE | ||
| 75 | sudo iptables -A FORWARD -i tap99 -j ACCEPT | ||
| 76 | sudo iptables -A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT | ||
| 77 | ``` | ||
| 78 | |||
| 79 | 4. Then you can run the example located in the `examples` folder: | ||
| 80 | |||
| 81 | ```sh | ||
| 82 | cd $EMBASSY_ROOT/examples/std/ | ||
| 83 | cargo run --bin net_dns -- --tap tap99 --static-ip | ||
| 84 | ``` | ||
| 85 | |||
| 86 | ### `net_ppp` example | ||
| 87 | |||
| 88 | This example establish a Point-to-Point Protocol (PPP) connection that can be used, for example, for connecting to internet through a 4G modem via a serial channel. | ||
| 89 | |||
| 90 | The example creates a PPP bridge over a virtual serial channel between `pty1` and `pty2` for the example code and a PPP server running on the same computer. | ||
| 91 | |||
| 92 | To run this example you will need: | ||
| 93 | - ppp (pppd server) | ||
| 94 | - socat (socket CAT) | ||
| 95 | |||
| 96 | To run the examples you may follow the next steps: | ||
| 97 | |||
| 98 | 1. Save the PPP server configuration: | ||
| 99 | ```sh | ||
| 100 | sudo sh -c 'echo "myuser $(hostname) mypass 192.168.7.10" >> /etc/ppp/pap-secrets' | ||
| 101 | ``` | ||
| 102 | |||
| 103 | 2. Create a files `pty1` and `pty2` and link them | ||
| 104 | ```sh | ||
| 105 | cd $EMBASSY_ROOT/examples/std/ | ||
| 106 | socat -v -x PTY,link=pty1,rawer PTY,link=pty2,rawer | ||
| 107 | ``` | ||
| 108 | |||
| 109 | 3. open a second terminal and start the PPP server: | ||
| 110 | ```sh | ||
| 111 | cd $EMBASSY_ROOT/examples/std/ | ||
| 112 | sudo pppd $PWD/pty1 115200 192.168.7.1: ms-dns 8.8.4.4 ms-dns 8.8.8.8 nodetach debug local persist silent | ||
| 113 | ``` | ||
| 114 | |||
| 115 | 4. Open a third terminal and run the example | ||
| 116 | ```sh | ||
| 117 | cd $EMBASSY_ROOT/examples/std/ | ||
| 118 | RUST_LOG=trace cargo run --bin net_ppp -- --device pty2 | ||
| 119 | ``` | ||
| 120 | 5. Observe the output in the second and third terminal | ||
| 121 | 6. Open one last terminal to interact with `net_ppp` example through the PPP connection | ||
| 122 | ```sh | ||
| 123 | # ping the net_ppp client | ||
| 124 | ping 192.168.7.10 | ||
| 125 | # open an tcp connection | ||
| 126 | nc 192.168.7.10 1234 | ||
| 127 | # Type anything and observe the output in the different terminals | ||
| 23 | ``` | 128 | ``` |
diff --git a/examples/std/src/bin/net.rs b/examples/std/src/bin/net.rs index 59813d8cb..fd7b6c930 100644 --- a/examples/std/src/bin/net.rs +++ b/examples/std/src/bin/net.rs | |||
| @@ -1,13 +1,15 @@ | |||
| 1 | use core::fmt::Write as _; | ||
| 2 | |||
| 1 | use clap::Parser; | 3 | use clap::Parser; |
| 2 | use embassy_executor::{Executor, Spawner}; | 4 | use embassy_executor::{Executor, Spawner}; |
| 3 | use embassy_net::tcp::TcpSocket; | 5 | use embassy_net::tcp::TcpSocket; |
| 4 | use embassy_net::{Config, Ipv4Address, Ipv4Cidr, Stack, StackResources}; | 6 | use embassy_net::{Config, Ipv4Address, Ipv4Cidr, StackResources}; |
| 5 | use embassy_net_tuntap::TunTapDevice; | 7 | use embassy_net_tuntap::TunTapDevice; |
| 6 | use embassy_time::Duration; | 8 | use embassy_time::Duration; |
| 7 | use embedded_io_async::Write; | 9 | use embedded_io_async::Write; |
| 8 | use heapless::Vec; | 10 | use heapless::Vec; |
| 9 | use log::*; | 11 | use log::*; |
| 10 | use rand_core::{OsRng, RngCore}; | 12 | use rand_core::{OsRng, TryRngCore}; |
| 11 | use static_cell::StaticCell; | 13 | use static_cell::StaticCell; |
| 12 | 14 | ||
| 13 | #[derive(Parser)] | 15 | #[derive(Parser)] |
| @@ -22,8 +24,8 @@ struct Opts { | |||
| 22 | } | 24 | } |
| 23 | 25 | ||
| 24 | #[embassy_executor::task] | 26 | #[embassy_executor::task] |
| 25 | async fn net_task(stack: &'static Stack<TunTapDevice>) -> ! { | 27 | async fn net_task(mut runner: embassy_net::Runner<'static, TunTapDevice>) -> ! { |
| 26 | stack.run().await | 28 | runner.run().await |
| 27 | } | 29 | } |
| 28 | 30 | ||
| 29 | #[embassy_executor::task] | 31 | #[embassy_executor::task] |
| @@ -46,21 +48,15 @@ async fn main_task(spawner: Spawner) { | |||
| 46 | 48 | ||
| 47 | // Generate random seed | 49 | // Generate random seed |
| 48 | let mut seed = [0; 8]; | 50 | let mut seed = [0; 8]; |
| 49 | OsRng.fill_bytes(&mut seed); | 51 | OsRng.try_fill_bytes(&mut seed).unwrap(); |
| 50 | let seed = u64::from_le_bytes(seed); | 52 | let seed = u64::from_le_bytes(seed); |
| 51 | 53 | ||
| 52 | // Init network stack | 54 | // Init network stack |
| 53 | static STACK: StaticCell<Stack<TunTapDevice>> = StaticCell::new(); | ||
| 54 | static RESOURCES: StaticCell<StackResources<3>> = StaticCell::new(); | 55 | static RESOURCES: StaticCell<StackResources<3>> = StaticCell::new(); |
| 55 | let stack = &*STACK.init(Stack::new( | 56 | let (stack, runner) = embassy_net::new(device, config, RESOURCES.init(StackResources::new()), seed); |
| 56 | device, | ||
| 57 | config, | ||
| 58 | RESOURCES.init(StackResources::<3>::new()), | ||
| 59 | seed, | ||
| 60 | )); | ||
| 61 | 57 | ||
| 62 | // Launch network task | 58 | // Launch network task |
| 63 | spawner.spawn(net_task(stack)).unwrap(); | 59 | spawner.spawn(net_task(runner).unwrap()); |
| 64 | 60 | ||
| 65 | // Then we can use it! | 61 | // Then we can use it! |
| 66 | let mut rx_buffer = [0; 4096]; | 62 | let mut rx_buffer = [0; 4096]; |
| @@ -77,8 +73,10 @@ async fn main_task(spawner: Spawner) { | |||
| 77 | return; | 73 | return; |
| 78 | } | 74 | } |
| 79 | info!("connected!"); | 75 | info!("connected!"); |
| 80 | loop { | 76 | for i in 0.. { |
| 81 | let r = socket.write_all(b"Hello!\n").await; | 77 | let mut buf = heapless::String::<100>::new(); |
| 78 | write!(buf, "Hello! ({})\r\n", i).unwrap(); | ||
| 79 | let r = socket.write_all(buf.as_bytes()).await; | ||
| 82 | if let Err(e) = r { | 80 | if let Err(e) = r { |
| 83 | warn!("write error: {:?}", e); | 81 | warn!("write error: {:?}", e); |
| 84 | return; | 82 | return; |
| @@ -97,6 +95,6 @@ fn main() { | |||
| 97 | 95 | ||
| 98 | let executor = EXECUTOR.init(Executor::new()); | 96 | let executor = EXECUTOR.init(Executor::new()); |
| 99 | executor.run(|spawner| { | 97 | executor.run(|spawner| { |
| 100 | spawner.spawn(main_task(spawner)).unwrap(); | 98 | spawner.spawn(main_task(spawner).unwrap()); |
| 101 | }); | 99 | }); |
| 102 | } | 100 | } |
diff --git a/examples/std/src/bin/net_dns.rs b/examples/std/src/bin/net_dns.rs index 3b6a3de37..dff704b86 100644 --- a/examples/std/src/bin/net_dns.rs +++ b/examples/std/src/bin/net_dns.rs | |||
| @@ -1,11 +1,11 @@ | |||
| 1 | use clap::Parser; | 1 | use clap::Parser; |
| 2 | use embassy_executor::{Executor, Spawner}; | 2 | use embassy_executor::{Executor, Spawner}; |
| 3 | use embassy_net::dns::DnsQueryType; | 3 | use embassy_net::dns::DnsQueryType; |
| 4 | use embassy_net::{Config, Ipv4Address, Ipv4Cidr, Stack, StackResources}; | 4 | use embassy_net::{Config, Ipv4Address, Ipv4Cidr, StackResources}; |
| 5 | use embassy_net_tuntap::TunTapDevice; | 5 | use embassy_net_tuntap::TunTapDevice; |
| 6 | use heapless::Vec; | 6 | use heapless::Vec; |
| 7 | use log::*; | 7 | use log::*; |
| 8 | use rand_core::{OsRng, RngCore}; | 8 | use rand_core::{OsRng, TryRngCore}; |
| 9 | use static_cell::StaticCell; | 9 | use static_cell::StaticCell; |
| 10 | 10 | ||
| 11 | #[derive(Parser)] | 11 | #[derive(Parser)] |
| @@ -20,8 +20,8 @@ struct Opts { | |||
| 20 | } | 20 | } |
| 21 | 21 | ||
| 22 | #[embassy_executor::task] | 22 | #[embassy_executor::task] |
| 23 | async fn net_task(stack: &'static Stack<TunTapDevice>) -> ! { | 23 | async fn net_task(mut runner: embassy_net::Runner<'static, TunTapDevice>) -> ! { |
| 24 | stack.run().await | 24 | runner.run().await |
| 25 | } | 25 | } |
| 26 | 26 | ||
| 27 | #[embassy_executor::task] | 27 | #[embassy_executor::task] |
| @@ -45,21 +45,15 @@ async fn main_task(spawner: Spawner) { | |||
| 45 | 45 | ||
| 46 | // Generate random seed | 46 | // Generate random seed |
| 47 | let mut seed = [0; 8]; | 47 | let mut seed = [0; 8]; |
| 48 | OsRng.fill_bytes(&mut seed); | 48 | OsRng.try_fill_bytes(&mut seed).unwrap(); |
| 49 | let seed = u64::from_le_bytes(seed); | 49 | let seed = u64::from_le_bytes(seed); |
| 50 | 50 | ||
| 51 | // Init network stack | 51 | // Init network stack |
| 52 | static STACK: StaticCell<Stack<TunTapDevice>> = StaticCell::new(); | ||
| 53 | static RESOURCES: StaticCell<StackResources<3>> = StaticCell::new(); | 52 | static RESOURCES: StaticCell<StackResources<3>> = StaticCell::new(); |
| 54 | let stack: &Stack<_> = &*STACK.init(Stack::new( | 53 | let (stack, runner) = embassy_net::new(device, config, RESOURCES.init(StackResources::new()), seed); |
| 55 | device, | ||
| 56 | config, | ||
| 57 | RESOURCES.init(StackResources::<3>::new()), | ||
| 58 | seed, | ||
| 59 | )); | ||
| 60 | 54 | ||
| 61 | // Launch network task | 55 | // Launch network task |
| 62 | spawner.spawn(net_task(stack)).unwrap(); | 56 | spawner.spawn(net_task(runner).unwrap()); |
| 63 | 57 | ||
| 64 | let host = "example.com"; | 58 | let host = "example.com"; |
| 65 | info!("querying host {:?}...", host); | 59 | info!("querying host {:?}...", host); |
| @@ -84,6 +78,6 @@ fn main() { | |||
| 84 | 78 | ||
| 85 | let executor = EXECUTOR.init(Executor::new()); | 79 | let executor = EXECUTOR.init(Executor::new()); |
| 86 | executor.run(|spawner| { | 80 | executor.run(|spawner| { |
| 87 | spawner.spawn(main_task(spawner)).unwrap(); | 81 | spawner.spawn(main_task(spawner).unwrap()); |
| 88 | }); | 82 | }); |
| 89 | } | 83 | } |
diff --git a/examples/std/src/bin/net_ppp.rs b/examples/std/src/bin/net_ppp.rs index 9ec0ea91f..82272c798 100644 --- a/examples/std/src/bin/net_ppp.rs +++ b/examples/std/src/bin/net_ppp.rs | |||
| @@ -16,14 +16,14 @@ use async_io::Async; | |||
| 16 | use clap::Parser; | 16 | use clap::Parser; |
| 17 | use embassy_executor::{Executor, Spawner}; | 17 | use embassy_executor::{Executor, Spawner}; |
| 18 | use embassy_net::tcp::TcpSocket; | 18 | use embassy_net::tcp::TcpSocket; |
| 19 | use embassy_net::{Config, ConfigV4, Ipv4Address, Ipv4Cidr, Stack, StackResources}; | 19 | use embassy_net::{Config, ConfigV4, Ipv4Cidr, Stack, StackResources}; |
| 20 | use embassy_net_ppp::Runner; | 20 | use embassy_net_ppp::Runner; |
| 21 | use embedded_io_async::Write; | 21 | use embedded_io_async::Write; |
| 22 | use futures::io::BufReader; | 22 | use futures::io::BufReader; |
| 23 | use heapless::Vec; | 23 | use heapless::Vec; |
| 24 | use log::*; | 24 | use log::*; |
| 25 | use nix::sys::termios; | 25 | use nix::sys::termios; |
| 26 | use rand_core::{OsRng, RngCore}; | 26 | use rand_core::{OsRng, TryRngCore}; |
| 27 | use static_cell::StaticCell; | 27 | use static_cell::StaticCell; |
| 28 | 28 | ||
| 29 | use crate::serial_port::SerialPort; | 29 | use crate::serial_port::SerialPort; |
| @@ -37,19 +37,15 @@ struct Opts { | |||
| 37 | } | 37 | } |
| 38 | 38 | ||
| 39 | #[embassy_executor::task] | 39 | #[embassy_executor::task] |
| 40 | async fn net_task(stack: &'static Stack<embassy_net_ppp::Device<'static>>) -> ! { | 40 | async fn net_task(mut runner: embassy_net::Runner<'static, embassy_net_ppp::Device<'static>>) -> ! { |
| 41 | stack.run().await | 41 | runner.run().await |
| 42 | } | 42 | } |
| 43 | 43 | ||
| 44 | #[embassy_executor::task] | 44 | #[embassy_executor::task] |
| 45 | async fn ppp_task( | 45 | async fn ppp_task(stack: Stack<'static>, mut runner: Runner<'static>, port: SerialPort) -> ! { |
| 46 | stack: &'static Stack<embassy_net_ppp::Device<'static>>, | ||
| 47 | mut runner: Runner<'static>, | ||
| 48 | port: SerialPort, | ||
| 49 | ) -> ! { | ||
| 50 | let port = Async::new(port).unwrap(); | 46 | let port = Async::new(port).unwrap(); |
| 51 | let port = BufReader::new(port); | 47 | let port = BufReader::new(port); |
| 52 | let port = adapter::FromFutures::new(port); | 48 | let port = embedded_io_adapters::futures_03::FromFutures::new(port); |
| 53 | 49 | ||
| 54 | let config = embassy_net_ppp::Config { | 50 | let config = embassy_net_ppp::Config { |
| 55 | username: b"myuser", | 51 | username: b"myuser", |
| @@ -64,10 +60,10 @@ async fn ppp_task( | |||
| 64 | }; | 60 | }; |
| 65 | let mut dns_servers = Vec::new(); | 61 | let mut dns_servers = Vec::new(); |
| 66 | for s in ipv4.dns_servers.iter().flatten() { | 62 | for s in ipv4.dns_servers.iter().flatten() { |
| 67 | let _ = dns_servers.push(Ipv4Address::from_bytes(&s.0)); | 63 | let _ = dns_servers.push(*s); |
| 68 | } | 64 | } |
| 69 | let config = ConfigV4::Static(embassy_net::StaticConfigV4 { | 65 | let config = ConfigV4::Static(embassy_net::StaticConfigV4 { |
| 70 | address: Ipv4Cidr::new(Ipv4Address::from_bytes(&addr.0), 0), | 66 | address: Ipv4Cidr::new(addr, 0), |
| 71 | gateway: None, | 67 | gateway: None, |
| 72 | dns_servers, | 68 | dns_servers, |
| 73 | }); | 69 | }); |
| @@ -93,22 +89,21 @@ async fn main_task(spawner: Spawner) { | |||
| 93 | 89 | ||
| 94 | // Generate random seed | 90 | // Generate random seed |
| 95 | let mut seed = [0; 8]; | 91 | let mut seed = [0; 8]; |
| 96 | OsRng.fill_bytes(&mut seed); | 92 | OsRng.try_fill_bytes(&mut seed).unwrap(); |
| 97 | let seed = u64::from_le_bytes(seed); | 93 | let seed = u64::from_le_bytes(seed); |
| 98 | 94 | ||
| 99 | // Init network stack | 95 | // Init network stack |
| 100 | static STACK: StaticCell<Stack<embassy_net_ppp::Device<'static>>> = StaticCell::new(); | ||
| 101 | static RESOURCES: StaticCell<StackResources<3>> = StaticCell::new(); | 96 | static RESOURCES: StaticCell<StackResources<3>> = StaticCell::new(); |
| 102 | let stack = &*STACK.init(Stack::new( | 97 | let (stack, net_runner) = embassy_net::new( |
| 103 | device, | 98 | device, |
| 104 | Config::default(), // don't configure IP yet | 99 | Config::default(), // don't configure IP yet |
| 105 | RESOURCES.init(StackResources::<3>::new()), | 100 | RESOURCES.init(StackResources::new()), |
| 106 | seed, | 101 | seed, |
| 107 | )); | 102 | ); |
| 108 | 103 | ||
| 109 | // Launch network task | 104 | // Launch network task |
| 110 | spawner.spawn(net_task(stack)).unwrap(); | 105 | spawner.spawn(net_task(net_runner).unwrap()); |
| 111 | spawner.spawn(ppp_task(stack, runner, port)).unwrap(); | 106 | spawner.spawn(ppp_task(stack, runner, port).unwrap()); |
| 112 | 107 | ||
| 113 | // Then we can use it! | 108 | // Then we can use it! |
| 114 | let mut rx_buffer = [0; 4096]; | 109 | let mut rx_buffer = [0; 4096]; |
| @@ -165,56 +160,6 @@ fn main() { | |||
| 165 | 160 | ||
| 166 | let executor = EXECUTOR.init(Executor::new()); | 161 | let executor = EXECUTOR.init(Executor::new()); |
| 167 | executor.run(|spawner| { | 162 | executor.run(|spawner| { |
| 168 | spawner.spawn(main_task(spawner)).unwrap(); | 163 | spawner.spawn(main_task(spawner).unwrap()); |
| 169 | }); | 164 | }); |
| 170 | } | 165 | } |
| 171 | |||
| 172 | mod adapter { | ||
| 173 | use core::future::poll_fn; | ||
| 174 | use core::pin::Pin; | ||
| 175 | |||
| 176 | use futures::AsyncBufReadExt; | ||
| 177 | |||
| 178 | /// Adapter from `futures::io` traits. | ||
| 179 | #[derive(Clone)] | ||
| 180 | pub struct FromFutures<T: ?Sized> { | ||
| 181 | inner: T, | ||
| 182 | } | ||
| 183 | |||
| 184 | impl<T> FromFutures<T> { | ||
| 185 | /// Create a new adapter. | ||
| 186 | pub fn new(inner: T) -> Self { | ||
| 187 | Self { inner } | ||
| 188 | } | ||
| 189 | } | ||
| 190 | |||
| 191 | impl<T: ?Sized> embedded_io_async::ErrorType for FromFutures<T> { | ||
| 192 | type Error = std::io::Error; | ||
| 193 | } | ||
| 194 | |||
| 195 | impl<T: futures::io::AsyncRead + Unpin + ?Sized> embedded_io_async::Read for FromFutures<T> { | ||
| 196 | async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> { | ||
| 197 | poll_fn(|cx| Pin::new(&mut self.inner).poll_read(cx, buf)).await | ||
| 198 | } | ||
| 199 | } | ||
| 200 | |||
| 201 | impl<T: futures::io::AsyncBufRead + Unpin + ?Sized> embedded_io_async::BufRead for FromFutures<T> { | ||
| 202 | async fn fill_buf(&mut self) -> Result<&[u8], Self::Error> { | ||
| 203 | self.inner.fill_buf().await | ||
| 204 | } | ||
| 205 | |||
| 206 | fn consume(&mut self, amt: usize) { | ||
| 207 | Pin::new(&mut self.inner).consume(amt) | ||
| 208 | } | ||
| 209 | } | ||
| 210 | |||
| 211 | impl<T: futures::io::AsyncWrite + Unpin + ?Sized> embedded_io_async::Write for FromFutures<T> { | ||
| 212 | async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> { | ||
| 213 | poll_fn(|cx| Pin::new(&mut self.inner).poll_write(cx, buf)).await | ||
| 214 | } | ||
| 215 | |||
| 216 | async fn flush(&mut self) -> Result<(), Self::Error> { | ||
| 217 | poll_fn(|cx| Pin::new(&mut self.inner).poll_flush(cx)).await | ||
| 218 | } | ||
| 219 | } | ||
| 220 | } | ||
diff --git a/examples/std/src/bin/net_udp.rs b/examples/std/src/bin/net_udp.rs index bee91990d..c5c4da65f 100644 --- a/examples/std/src/bin/net_udp.rs +++ b/examples/std/src/bin/net_udp.rs | |||
| @@ -1,11 +1,11 @@ | |||
| 1 | use clap::Parser; | 1 | use clap::Parser; |
| 2 | use embassy_executor::{Executor, Spawner}; | 2 | use embassy_executor::{Executor, Spawner}; |
| 3 | use embassy_net::udp::{PacketMetadata, UdpSocket}; | 3 | use embassy_net::udp::{PacketMetadata, UdpSocket}; |
| 4 | use embassy_net::{Config, Ipv4Address, Ipv4Cidr, Stack, StackResources}; | 4 | use embassy_net::{Config, Ipv4Address, Ipv4Cidr, StackResources}; |
| 5 | use embassy_net_tuntap::TunTapDevice; | 5 | use embassy_net_tuntap::TunTapDevice; |
| 6 | use heapless::Vec; | 6 | use heapless::Vec; |
| 7 | use log::*; | 7 | use log::*; |
| 8 | use rand_core::{OsRng, RngCore}; | 8 | use rand_core::{OsRng, TryRngCore}; |
| 9 | use static_cell::StaticCell; | 9 | use static_cell::StaticCell; |
| 10 | 10 | ||
| 11 | #[derive(Parser)] | 11 | #[derive(Parser)] |
| @@ -20,8 +20,8 @@ struct Opts { | |||
| 20 | } | 20 | } |
| 21 | 21 | ||
| 22 | #[embassy_executor::task] | 22 | #[embassy_executor::task] |
| 23 | async fn net_task(stack: &'static Stack<TunTapDevice>) -> ! { | 23 | async fn net_task(mut runner: embassy_net::Runner<'static, TunTapDevice>) -> ! { |
| 24 | stack.run().await | 24 | runner.run().await |
| 25 | } | 25 | } |
| 26 | 26 | ||
| 27 | #[embassy_executor::task] | 27 | #[embassy_executor::task] |
| @@ -44,21 +44,15 @@ async fn main_task(spawner: Spawner) { | |||
| 44 | 44 | ||
| 45 | // Generate random seed | 45 | // Generate random seed |
| 46 | let mut seed = [0; 8]; | 46 | let mut seed = [0; 8]; |
| 47 | OsRng.fill_bytes(&mut seed); | 47 | OsRng.try_fill_bytes(&mut seed).unwrap(); |
| 48 | let seed = u64::from_le_bytes(seed); | 48 | let seed = u64::from_le_bytes(seed); |
| 49 | 49 | ||
| 50 | // Init network stack | 50 | // Init network stack |
| 51 | static STACK: StaticCell<Stack<TunTapDevice>> = StaticCell::new(); | ||
| 52 | static RESOURCES: StaticCell<StackResources<3>> = StaticCell::new(); | 51 | static RESOURCES: StaticCell<StackResources<3>> = StaticCell::new(); |
| 53 | let stack = &*STACK.init(Stack::new( | 52 | let (stack, runner) = embassy_net::new(device, config, RESOURCES.init(StackResources::new()), seed); |
| 54 | device, | ||
| 55 | config, | ||
| 56 | RESOURCES.init(StackResources::<3>::new()), | ||
| 57 | seed, | ||
| 58 | )); | ||
| 59 | 53 | ||
| 60 | // Launch network task | 54 | // Launch network task |
| 61 | spawner.spawn(net_task(stack)).unwrap(); | 55 | spawner.spawn(net_task(runner).unwrap()); |
| 62 | 56 | ||
| 63 | // Then we can use it! | 57 | // Then we can use it! |
| 64 | let mut rx_meta = [PacketMetadata::EMPTY; 16]; | 58 | let mut rx_meta = [PacketMetadata::EMPTY; 16]; |
| @@ -92,6 +86,6 @@ fn main() { | |||
| 92 | 86 | ||
| 93 | let executor = EXECUTOR.init(Executor::new()); | 87 | let executor = EXECUTOR.init(Executor::new()); |
| 94 | executor.run(|spawner| { | 88 | executor.run(|spawner| { |
| 95 | spawner.spawn(main_task(spawner)).unwrap(); | 89 | spawner.spawn(main_task(spawner).unwrap()); |
| 96 | }); | 90 | }); |
| 97 | } | 91 | } |
diff --git a/examples/std/src/bin/serial.rs b/examples/std/src/bin/serial.rs index 10c85511d..1ed9997c4 100644 --- a/examples/std/src/bin/serial.rs +++ b/examples/std/src/bin/serial.rs | |||
| @@ -50,6 +50,6 @@ fn main() { | |||
| 50 | 50 | ||
| 51 | let executor = EXECUTOR.init(Executor::new()); | 51 | let executor = EXECUTOR.init(Executor::new()); |
| 52 | executor.run(|spawner| { | 52 | executor.run(|spawner| { |
| 53 | spawner.spawn(run()).unwrap(); | 53 | spawner.spawn(run().unwrap()); |
| 54 | }); | 54 | }); |
| 55 | } | 55 | } |
diff --git a/examples/std/src/bin/tcp_accept.rs b/examples/std/src/bin/tcp_accept.rs index e8b6eaa6c..77886f471 100644 --- a/examples/std/src/bin/tcp_accept.rs +++ b/examples/std/src/bin/tcp_accept.rs | |||
| @@ -1,15 +1,13 @@ | |||
| 1 | use core::fmt::Write as _; | ||
| 2 | |||
| 3 | use clap::Parser; | 1 | use clap::Parser; |
| 4 | use embassy_executor::{Executor, Spawner}; | 2 | use embassy_executor::{Executor, Spawner}; |
| 5 | use embassy_net::tcp::TcpSocket; | 3 | use embassy_net::tcp::TcpSocket; |
| 6 | use embassy_net::{Config, Ipv4Address, Ipv4Cidr, Stack, StackResources}; | 4 | use embassy_net::{Config, Ipv4Address, Ipv4Cidr, StackResources}; |
| 7 | use embassy_net_tuntap::TunTapDevice; | 5 | use embassy_net_tuntap::TunTapDevice; |
| 8 | use embassy_time::{Duration, Timer}; | 6 | use embassy_time::{Duration, Timer}; |
| 9 | use embedded_io_async::Write as _; | 7 | use embedded_io_async::Write as _; |
| 10 | use heapless::Vec; | 8 | use heapless::Vec; |
| 11 | use log::*; | 9 | use log::*; |
| 12 | use rand_core::{OsRng, RngCore}; | 10 | use rand_core::{OsRng, TryRngCore}; |
| 13 | use static_cell::StaticCell; | 11 | use static_cell::StaticCell; |
| 14 | 12 | ||
| 15 | #[derive(Parser)] | 13 | #[derive(Parser)] |
| @@ -24,18 +22,8 @@ struct Opts { | |||
| 24 | } | 22 | } |
| 25 | 23 | ||
| 26 | #[embassy_executor::task] | 24 | #[embassy_executor::task] |
| 27 | async fn net_task(stack: &'static Stack<TunTapDevice>) -> ! { | 25 | async fn net_task(mut runner: embassy_net::Runner<'static, TunTapDevice>) -> ! { |
| 28 | stack.run().await | 26 | runner.run().await |
| 29 | } | ||
| 30 | |||
| 31 | #[derive(Default)] | ||
| 32 | struct StrWrite(pub heapless::Vec<u8, 30>); | ||
| 33 | |||
| 34 | impl core::fmt::Write for StrWrite { | ||
| 35 | fn write_str(&mut self, s: &str) -> Result<(), core::fmt::Error> { | ||
| 36 | self.0.extend_from_slice(s.as_bytes()).unwrap(); | ||
| 37 | Ok(()) | ||
| 38 | } | ||
| 39 | } | 27 | } |
| 40 | 28 | ||
| 41 | #[embassy_executor::task] | 29 | #[embassy_executor::task] |
| @@ -58,21 +46,15 @@ async fn main_task(spawner: Spawner) { | |||
| 58 | 46 | ||
| 59 | // Generate random seed | 47 | // Generate random seed |
| 60 | let mut seed = [0; 8]; | 48 | let mut seed = [0; 8]; |
| 61 | OsRng.fill_bytes(&mut seed); | 49 | OsRng.try_fill_bytes(&mut seed).unwrap(); |
| 62 | let seed = u64::from_le_bytes(seed); | 50 | let seed = u64::from_le_bytes(seed); |
| 63 | 51 | ||
| 64 | // Init network stack | 52 | // Init network stack |
| 65 | static STACK: StaticCell<Stack<TunTapDevice>> = StaticCell::new(); | ||
| 66 | static RESOURCES: StaticCell<StackResources<3>> = StaticCell::new(); | 53 | static RESOURCES: StaticCell<StackResources<3>> = StaticCell::new(); |
| 67 | let stack = &*STACK.init(Stack::new( | 54 | let (stack, runner) = embassy_net::new(device, config, RESOURCES.init(StackResources::new()), seed); |
| 68 | device, | ||
| 69 | config, | ||
| 70 | RESOURCES.init(StackResources::<3>::new()), | ||
| 71 | seed, | ||
| 72 | )); | ||
| 73 | 55 | ||
| 74 | // Launch network task | 56 | // Launch network task |
| 75 | spawner.spawn(net_task(stack)).unwrap(); | 57 | spawner.spawn(net_task(runner).unwrap()); |
| 76 | 58 | ||
| 77 | // Then we can use it! | 59 | // Then we can use it! |
| 78 | let mut rx_buffer = [0; 4096]; | 60 | let mut rx_buffer = [0; 4096]; |
| @@ -91,9 +73,8 @@ async fn main_task(spawner: Spawner) { | |||
| 91 | 73 | ||
| 92 | // Write some quick output | 74 | // Write some quick output |
| 93 | for i in 1..=5 { | 75 | for i in 1..=5 { |
| 94 | let mut w = StrWrite::default(); | 76 | let s = format!("{}! ", i); |
| 95 | write!(w, "{}! ", i).unwrap(); | 77 | let r = socket.write_all(s.as_bytes()).await; |
| 96 | let r = socket.write_all(&w.0).await; | ||
| 97 | if let Err(e) = r { | 78 | if let Err(e) = r { |
| 98 | warn!("write error: {:?}", e); | 79 | warn!("write error: {:?}", e); |
| 99 | return; | 80 | return; |
| @@ -120,6 +101,6 @@ fn main() { | |||
| 120 | 101 | ||
| 121 | let executor = EXECUTOR.init(Executor::new()); | 102 | let executor = EXECUTOR.init(Executor::new()); |
| 122 | executor.run(|spawner| { | 103 | executor.run(|spawner| { |
| 123 | spawner.spawn(main_task(spawner)).unwrap(); | 104 | spawner.spawn(main_task(spawner).unwrap()); |
| 124 | }); | 105 | }); |
| 125 | } | 106 | } |
diff --git a/examples/std/src/bin/tick.rs b/examples/std/src/bin/tick.rs index f23cf3549..16b82c82b 100644 --- a/examples/std/src/bin/tick.rs +++ b/examples/std/src/bin/tick.rs | |||
| @@ -17,5 +17,5 @@ async fn main(spawner: Spawner) { | |||
| 17 | .format_timestamp_nanos() | 17 | .format_timestamp_nanos() |
| 18 | .init(); | 18 | .init(); |
| 19 | 19 | ||
| 20 | spawner.spawn(run()).unwrap(); | 20 | spawner.spawn(run().unwrap()); |
| 21 | } | 21 | } |
diff --git a/examples/std/tap.sh b/examples/std/tap.sh new file mode 100644 index 000000000..fb89d2381 --- /dev/null +++ b/examples/std/tap.sh | |||
| @@ -0,0 +1,7 @@ | |||
| 1 | ip tuntap add name tap99 mode tap user $SUDO_USER | ||
| 2 | ip link set tap99 up | ||
| 3 | ip addr add 192.168.69.100/24 dev tap99 | ||
| 4 | ip -6 addr add fe80::100/64 dev tap99 | ||
| 5 | ip -6 addr add fdaa::100/64 dev tap99 | ||
| 6 | ip -6 route add fe80::/64 dev tap99 | ||
| 7 | ip -6 route add fdaa::/64 dev tap99 | ||
