aboutsummaryrefslogtreecommitdiff
path: root/examples/std
diff options
context:
space:
mode:
author1-rafael-1 <[email protected]>2025-09-15 20:07:18 +0200
committer1-rafael-1 <[email protected]>2025-09-15 20:07:18 +0200
commit6bb3d2c0720fa082f27d3cdb70f516058497ec87 (patch)
tree5a1e255cff999b00800f203b91a759c720c973e5 /examples/std
parenteb685574601d98c44faed9a3534d056199b46e20 (diff)
parent92a6fd2946f2cbb15359290f68aa360953da2ff7 (diff)
Merge branch 'main' into rp2040-rtc-alarm
Diffstat (limited to 'examples/std')
-rw-r--r--examples/std/Cargo.toml18
-rw-r--r--examples/std/README.md119
-rw-r--r--examples/std/src/bin/net.rs8
-rw-r--r--examples/std/src/bin/net_dns.rs8
-rw-r--r--examples/std/src/bin/net_ppp.rs10
-rw-r--r--examples/std/src/bin/net_udp.rs8
-rw-r--r--examples/std/src/bin/serial.rs2
-rw-r--r--examples/std/src/bin/tcp_accept.rs8
-rw-r--r--examples/std/src/bin/tick.rs2
-rw-r--r--examples/std/tap.sh2
10 files changed, 150 insertions, 35 deletions
diff --git a/examples/std/Cargo.toml b/examples/std/Cargo.toml
index f00953167..449c5ddca 100644
--- a/examples/std/Cargo.toml
+++ b/examples/std/Cargo.toml
@@ -3,14 +3,15 @@ edition = "2021"
3name = "embassy-std-examples" 3name = "embassy-std-examples"
4version = "0.1.0" 4version = "0.1.0"
5license = "MIT OR Apache-2.0" 5license = "MIT OR Apache-2.0"
6publish = false
6 7
7[dependencies] 8[dependencies]
8embassy-sync = { version = "0.6.2", path = "../../embassy-sync", features = ["log"] } 9embassy-sync = { version = "0.7.2", path = "../../embassy-sync", features = ["log"] }
9embassy-executor = { version = "0.7.0", path = "../../embassy-executor", features = ["arch-std", "executor-thread", "log"] } 10embassy-executor = { version = "0.9.0", path = "../../embassy-executor", features = ["arch-std", "executor-thread", "log"] }
10embassy-time = { version = "0.4.0", path = "../../embassy-time", features = ["log", "std", ] } 11embassy-time = { version = "0.5.0", path = "../../embassy-time", features = ["log", "std", ] }
11embassy-net = { version = "0.7.0", path = "../../embassy-net", features=[ "log", "medium-ethernet", "medium-ip", "tcp", "udp", "dns", "dhcpv4", "proto-ipv6"] } 12embassy-net = { version = "0.7.1", path = "../../embassy-net", features=[ "log", "medium-ethernet", "medium-ip", "tcp", "udp", "dns", "dhcpv4", "proto-ipv6"] }
12embassy-net-tuntap = { version = "0.1.0", path = "../../embassy-net-tuntap" } 13embassy-net-tuntap = { version = "0.1.0", path = "../../embassy-net-tuntap" }
13embassy-net-ppp = { version = "0.2.0", path = "../../embassy-net-ppp", features = ["log"]} 14embassy-net-ppp = { version = "0.2.1", path = "../../embassy-net-ppp", features = ["log"]}
14embedded-io-async = { version = "0.6.1" } 15embedded-io-async = { version = "0.6.1" }
15embedded-io-adapters = { version = "0.6.1", features = ["futures-03"] } 16embedded-io-adapters = { version = "0.6.1", features = ["futures-03"] }
16critical-section = { version = "1.1", features = ["std"] } 17critical-section = { version = "1.1", features = ["std"] }
@@ -21,9 +22,14 @@ futures = { version = "0.3.17" }
21log = "0.4.14" 22log = "0.4.14"
22nix = "0.26.2" 23nix = "0.26.2"
23clap = { version = "3.0.0-beta.5", features = ["derive"] } 24clap = { version = "3.0.0-beta.5", features = ["derive"] }
24rand_core = { version = "0.6.3", features = ["std"] } 25rand_core = { version = "0.9.1", features = ["std", "os_rng"] }
25heapless = { version = "0.8", default-features = false } 26heapless = { version = "0.8", default-features = false }
26static_cell = "2" 27static_cell = "2"
27 28
28[profile.release] 29[profile.release]
29debug = 2 30debug = 2
31
32[package.metadata.embassy]
33build = [
34 { artifact-dir = "out/examples/std" }
35]
diff --git a/examples/std/README.md b/examples/std/README.md
index dcc152fc2..ac2c2a1a6 100644
--- a/examples/std/README.md
+++ b/examples/std/README.md
@@ -1,19 +1,128 @@
1 1
2## Running the `embassy-net` examples 2## Running the `embassy-net` examples
3 3
4First, create the tap99 interface. (The number was chosen to 4To 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
5hopefully not collide with anything.) You only need to do 5hopefully not collide with anything.) You only need to do this once every time you reboot your computer.
6this once.
7 6
8```sh 7```sh
8cd $EMBASSY_ROOT/examples/std/
9sudo sh tap.sh 9sudo sh tap.sh
10``` 10```
11 11
12Second, have something listening there. For example `nc -lp 8000` 12The example `net_ppp` requires different steps that are detailed in its section.
13
14### `net` example
15
16For this example, you need to have something listening in the correct port. For example `nc -lp 8000`.
13 17
14Then run the example located in the `examples` folder: 18Then run the example located in the `examples` folder:
15 19
16```sh 20```sh
17cd $EMBASSY_ROOT/examples/std/ 21cd $EMBASSY_ROOT/examples/std/
18sudo cargo run --bin net -- --tap tap99 --static-ip 22cargo run --bin net -- --tap tap99 --static-ip
23```
24### `tcp_accept` example
25
26This example listen for a tcp connection.
27
28First run the example located in the `examples` folder:
29
30```sh
31cd $EMBASSY_ROOT/examples/std/
32cargo run --bin tcp_accept -- --tap tap99 --static-ip
33```
34
35Then open a connection to the port. For example `nc 192.168.69.2 9999`.
36
37### `net_udp` example
38
39This example listen for a udp connection.
40
41First run the example located in the `examples` folder:
42
43```sh
44cd $EMBASSY_ROOT/examples/std/
45cargo run --bin net_udp -- --tap tap99 --static-ip
46```
47
48Then open a connection to the port. For example `nc -u 192.168.69.2 9400`.
49
50### `net_dns` example
51
52This example queries a `DNS` for the IP address of `www.example.com`.
53
54In order to achieve this, the `tap99` interface requires configuring tap99 as a gateway device temporarily.
55
56For example, in Ubuntu you can do this by:
57
581. Identifying your default route device. In the next example `eth0`
59
60```sh
61ip r | grep "default"
62default via 192.168.2.1 dev eth0 proto kernel metric 35
63```
64
652. Enabling temporarily IP Forwarding:
66
67```sh
68sudo sysctl -w net.ipv4.ip_forward=1
69```
70
713. Configuring NAT to mascarade traffic from `tap99` to `eth0`
72
73```sh
74sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
75sudo iptables -A FORWARD -i tap99 -j ACCEPT
76sudo iptables -A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
77```
78
794. Then you can run the example located in the `examples` folder:
80
81```sh
82cd $EMBASSY_ROOT/examples/std/
83cargo run --bin net_dns -- --tap tap99 --static-ip
84```
85
86### `net_ppp` example
87
88This 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
90The 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
92To run this example you will need:
93- ppp (pppd server)
94- socat (socket CAT)
95
96To run the examples you may follow the next steps:
97
981. Save the PPP server configuration:
99```sh
100sudo sh -c 'echo "myuser $(hostname) mypass 192.168.7.10" >> /etc/ppp/pap-secrets'
101```
102
1032. Create a files `pty1` and `pty2` and link them
104```sh
105cd $EMBASSY_ROOT/examples/std/
106socat -v -x PTY,link=pty1,rawer PTY,link=pty2,rawer
107```
108
1093. open a second terminal and start the PPP server:
110```sh
111cd $EMBASSY_ROOT/examples/std/
112sudo 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
1154. Open a third terminal and run the example
116```sh
117cd $EMBASSY_ROOT/examples/std/
118RUST_LOG=trace cargo run --bin net_ppp -- --device pty2
119```
1205. Observe the output in the second and third terminal
1216. Open one last terminal to interact with `net_ppp` example through the PPP connection
122```sh
123# ping the net_ppp client
124ping 192.168.7.10
125# open an tcp connection
126nc 192.168.7.10 1234
127# Type anything and observe the output in the different terminals
19``` 128```
diff --git a/examples/std/src/bin/net.rs b/examples/std/src/bin/net.rs
index 6e50b1a01..fd7b6c930 100644
--- a/examples/std/src/bin/net.rs
+++ b/examples/std/src/bin/net.rs
@@ -9,7 +9,7 @@ use embassy_time::Duration;
9use embedded_io_async::Write; 9use embedded_io_async::Write;
10use heapless::Vec; 10use heapless::Vec;
11use log::*; 11use log::*;
12use rand_core::{OsRng, RngCore}; 12use rand_core::{OsRng, TryRngCore};
13use static_cell::StaticCell; 13use static_cell::StaticCell;
14 14
15#[derive(Parser)] 15#[derive(Parser)]
@@ -48,7 +48,7 @@ async fn main_task(spawner: Spawner) {
48 48
49 // Generate random seed 49 // Generate random seed
50 let mut seed = [0; 8]; 50 let mut seed = [0; 8];
51 OsRng.fill_bytes(&mut seed); 51 OsRng.try_fill_bytes(&mut seed).unwrap();
52 let seed = u64::from_le_bytes(seed); 52 let seed = u64::from_le_bytes(seed);
53 53
54 // Init network stack 54 // Init network stack
@@ -56,7 +56,7 @@ async fn main_task(spawner: Spawner) {
56 let (stack, runner) = embassy_net::new(device, config, RESOURCES.init(StackResources::new()), seed); 56 let (stack, runner) = embassy_net::new(device, config, RESOURCES.init(StackResources::new()), seed);
57 57
58 // Launch network task 58 // Launch network task
59 spawner.spawn(net_task(runner)).unwrap(); 59 spawner.spawn(net_task(runner).unwrap());
60 60
61 // Then we can use it! 61 // Then we can use it!
62 let mut rx_buffer = [0; 4096]; 62 let mut rx_buffer = [0; 4096];
@@ -95,6 +95,6 @@ fn main() {
95 95
96 let executor = EXECUTOR.init(Executor::new()); 96 let executor = EXECUTOR.init(Executor::new());
97 executor.run(|spawner| { 97 executor.run(|spawner| {
98 spawner.spawn(main_task(spawner)).unwrap(); 98 spawner.spawn(main_task(spawner).unwrap());
99 }); 99 });
100} 100}
diff --git a/examples/std/src/bin/net_dns.rs b/examples/std/src/bin/net_dns.rs
index a42c5dbb7..dff704b86 100644
--- a/examples/std/src/bin/net_dns.rs
+++ b/examples/std/src/bin/net_dns.rs
@@ -5,7 +5,7 @@ use embassy_net::{Config, Ipv4Address, Ipv4Cidr, StackResources};
5use embassy_net_tuntap::TunTapDevice; 5use embassy_net_tuntap::TunTapDevice;
6use heapless::Vec; 6use heapless::Vec;
7use log::*; 7use log::*;
8use rand_core::{OsRng, RngCore}; 8use rand_core::{OsRng, TryRngCore};
9use static_cell::StaticCell; 9use static_cell::StaticCell;
10 10
11#[derive(Parser)] 11#[derive(Parser)]
@@ -45,7 +45,7 @@ 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
@@ -53,7 +53,7 @@ async fn main_task(spawner: Spawner) {
53 let (stack, runner) = embassy_net::new(device, config, RESOURCES.init(StackResources::new()), seed); 53 let (stack, runner) = embassy_net::new(device, config, RESOURCES.init(StackResources::new()), seed);
54 54
55 // Launch network task 55 // Launch network task
56 spawner.spawn(net_task(runner)).unwrap(); 56 spawner.spawn(net_task(runner).unwrap());
57 57
58 let host = "example.com"; 58 let host = "example.com";
59 info!("querying host {:?}...", host); 59 info!("querying host {:?}...", host);
@@ -78,6 +78,6 @@ fn main() {
78 78
79 let executor = EXECUTOR.init(Executor::new()); 79 let executor = EXECUTOR.init(Executor::new());
80 executor.run(|spawner| { 80 executor.run(|spawner| {
81 spawner.spawn(main_task(spawner)).unwrap(); 81 spawner.spawn(main_task(spawner).unwrap());
82 }); 82 });
83} 83}
diff --git a/examples/std/src/bin/net_ppp.rs b/examples/std/src/bin/net_ppp.rs
index f667e8d4c..82272c798 100644
--- a/examples/std/src/bin/net_ppp.rs
+++ b/examples/std/src/bin/net_ppp.rs
@@ -23,7 +23,7 @@ use futures::io::BufReader;
23use heapless::Vec; 23use heapless::Vec;
24use log::*; 24use log::*;
25use nix::sys::termios; 25use nix::sys::termios;
26use rand_core::{OsRng, RngCore}; 26use rand_core::{OsRng, TryRngCore};
27use static_cell::StaticCell; 27use static_cell::StaticCell;
28 28
29use crate::serial_port::SerialPort; 29use crate::serial_port::SerialPort;
@@ -89,7 +89,7 @@ async fn main_task(spawner: Spawner) {
89 89
90 // Generate random seed 90 // Generate random seed
91 let mut seed = [0; 8]; 91 let mut seed = [0; 8];
92 OsRng.fill_bytes(&mut seed); 92 OsRng.try_fill_bytes(&mut seed).unwrap();
93 let seed = u64::from_le_bytes(seed); 93 let seed = u64::from_le_bytes(seed);
94 94
95 // Init network stack 95 // Init network stack
@@ -102,8 +102,8 @@ async fn main_task(spawner: Spawner) {
102 ); 102 );
103 103
104 // Launch network task 104 // Launch network task
105 spawner.spawn(net_task(net_runner)).unwrap(); 105 spawner.spawn(net_task(net_runner).unwrap());
106 spawner.spawn(ppp_task(stack, runner, port)).unwrap(); 106 spawner.spawn(ppp_task(stack, runner, port).unwrap());
107 107
108 // Then we can use it! 108 // Then we can use it!
109 let mut rx_buffer = [0; 4096]; 109 let mut rx_buffer = [0; 4096];
@@ -160,6 +160,6 @@ fn main() {
160 160
161 let executor = EXECUTOR.init(Executor::new()); 161 let executor = EXECUTOR.init(Executor::new());
162 executor.run(|spawner| { 162 executor.run(|spawner| {
163 spawner.spawn(main_task(spawner)).unwrap(); 163 spawner.spawn(main_task(spawner).unwrap());
164 }); 164 });
165} 165}
diff --git a/examples/std/src/bin/net_udp.rs b/examples/std/src/bin/net_udp.rs
index 02d4d3efb..c5c4da65f 100644
--- a/examples/std/src/bin/net_udp.rs
+++ b/examples/std/src/bin/net_udp.rs
@@ -5,7 +5,7 @@ use embassy_net::{Config, Ipv4Address, Ipv4Cidr, StackResources};
5use embassy_net_tuntap::TunTapDevice; 5use embassy_net_tuntap::TunTapDevice;
6use heapless::Vec; 6use heapless::Vec;
7use log::*; 7use log::*;
8use rand_core::{OsRng, RngCore}; 8use rand_core::{OsRng, TryRngCore};
9use static_cell::StaticCell; 9use static_cell::StaticCell;
10 10
11#[derive(Parser)] 11#[derive(Parser)]
@@ -44,7 +44,7 @@ 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
@@ -52,7 +52,7 @@ async fn main_task(spawner: Spawner) {
52 let (stack, runner) = embassy_net::new(device, config, RESOURCES.init(StackResources::new()), seed); 52 let (stack, runner) = embassy_net::new(device, config, RESOURCES.init(StackResources::new()), seed);
53 53
54 // Launch network task 54 // Launch network task
55 spawner.spawn(net_task(runner)).unwrap(); 55 spawner.spawn(net_task(runner).unwrap());
56 56
57 // Then we can use it! 57 // Then we can use it!
58 let mut rx_meta = [PacketMetadata::EMPTY; 16]; 58 let mut rx_meta = [PacketMetadata::EMPTY; 16];
@@ -86,6 +86,6 @@ fn main() {
86 86
87 let executor = EXECUTOR.init(Executor::new()); 87 let executor = EXECUTOR.init(Executor::new());
88 executor.run(|spawner| { 88 executor.run(|spawner| {
89 spawner.spawn(main_task(spawner)).unwrap(); 89 spawner.spawn(main_task(spawner).unwrap());
90 }); 90 });
91} 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 18646a083..77886f471 100644
--- a/examples/std/src/bin/tcp_accept.rs
+++ b/examples/std/src/bin/tcp_accept.rs
@@ -7,7 +7,7 @@ use embassy_time::{Duration, Timer};
7use embedded_io_async::Write as _; 7use embedded_io_async::Write as _;
8use heapless::Vec; 8use heapless::Vec;
9use log::*; 9use log::*;
10use rand_core::{OsRng, RngCore}; 10use rand_core::{OsRng, TryRngCore};
11use static_cell::StaticCell; 11use static_cell::StaticCell;
12 12
13#[derive(Parser)] 13#[derive(Parser)]
@@ -46,7 +46,7 @@ async fn main_task(spawner: Spawner) {
46 46
47 // Generate random seed 47 // Generate random seed
48 let mut seed = [0; 8]; 48 let mut seed = [0; 8];
49 OsRng.fill_bytes(&mut seed); 49 OsRng.try_fill_bytes(&mut seed).unwrap();
50 let seed = u64::from_le_bytes(seed); 50 let seed = u64::from_le_bytes(seed);
51 51
52 // Init network stack 52 // Init network stack
@@ -54,7 +54,7 @@ async fn main_task(spawner: Spawner) {
54 let (stack, runner) = embassy_net::new(device, config, RESOURCES.init(StackResources::new()), seed); 54 let (stack, runner) = embassy_net::new(device, config, RESOURCES.init(StackResources::new()), seed);
55 55
56 // Launch network task 56 // Launch network task
57 spawner.spawn(net_task(runner)).unwrap(); 57 spawner.spawn(net_task(runner).unwrap());
58 58
59 // Then we can use it! 59 // Then we can use it!
60 let mut rx_buffer = [0; 4096]; 60 let mut rx_buffer = [0; 4096];
@@ -101,6 +101,6 @@ fn main() {
101 101
102 let executor = EXECUTOR.init(Executor::new()); 102 let executor = EXECUTOR.init(Executor::new());
103 executor.run(|spawner| { 103 executor.run(|spawner| {
104 spawner.spawn(main_task(spawner)).unwrap(); 104 spawner.spawn(main_task(spawner).unwrap());
105 }); 105 });
106} 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
index 39d92a099..fb89d2381 100644
--- a/examples/std/tap.sh
+++ b/examples/std/tap.sh
@@ -1,4 +1,4 @@
1ip tuntap add name tap99 mode tap user $USER 1ip tuntap add name tap99 mode tap user $SUDO_USER
2ip link set tap99 up 2ip link set tap99 up
3ip addr add 192.168.69.100/24 dev tap99 3ip addr add 192.168.69.100/24 dev tap99
4ip -6 addr add fe80::100/64 dev tap99 4ip -6 addr add fe80::100/64 dev tap99