mirror of
https://github.com/embassy-rs/embassy.git
synced 2024-11-25 08:12:30 +00:00
Add UDP example app
This commit is contained in:
parent
d5ab0d3ebb
commit
865a91976c
@ -6,7 +6,7 @@ version = "0.1.0"
|
||||
[dependencies]
|
||||
embassy-util = { version = "0.1.0", path = "../../embassy-util", features = ["log"] }
|
||||
embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["log", "std", "time", "nightly"] }
|
||||
embassy-net = { version = "0.1.0", path = "../../embassy-net", features=[ "std", "log", "medium-ethernet", "tcp", "dhcpv4", "pool-16"] }
|
||||
embassy-net = { version = "0.1.0", path = "../../embassy-net", features=[ "std", "log", "medium-ethernet", "tcp", "udp", "dhcpv4", "pool-16"] }
|
||||
embedded-io = { version = "0.3.0", features = ["async", "std", "futures"] }
|
||||
|
||||
async-io = "1.6.0"
|
||||
|
109
examples/std/src/bin/net_udp.rs
Normal file
109
examples/std/src/bin/net_udp.rs
Normal file
@ -0,0 +1,109 @@
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
||||
use clap::Parser;
|
||||
use embassy_executor::executor::{Executor, Spawner};
|
||||
use embassy_net::udp::UdpSocket;
|
||||
use embassy_net::{ConfigStrategy, Ipv4Address, Ipv4Cidr, PacketMetadata, Stack, StackResources};
|
||||
use embassy_util::Forever;
|
||||
use heapless::Vec;
|
||||
use log::*;
|
||||
use rand_core::{OsRng, RngCore};
|
||||
|
||||
#[path = "../tuntap.rs"]
|
||||
mod tuntap;
|
||||
|
||||
use crate::tuntap::TunTapDevice;
|
||||
|
||||
macro_rules! forever {
|
||||
($val:expr) => {{
|
||||
type T = impl Sized;
|
||||
static FOREVER: Forever<T> = Forever::new();
|
||||
FOREVER.put_with(move || $val)
|
||||
}};
|
||||
}
|
||||
|
||||
#[derive(Parser)]
|
||||
#[clap(version = "1.0")]
|
||||
struct Opts {
|
||||
/// TAP device name
|
||||
#[clap(long, default_value = "tap0")]
|
||||
tap: String,
|
||||
/// use a static IP instead of DHCP
|
||||
#[clap(long)]
|
||||
static_ip: bool,
|
||||
}
|
||||
|
||||
#[embassy_executor::task]
|
||||
async fn net_task(stack: &'static Stack<TunTapDevice>) -> ! {
|
||||
stack.run().await
|
||||
}
|
||||
|
||||
#[embassy_executor::task]
|
||||
async fn main_task(spawner: Spawner) {
|
||||
let opts: Opts = Opts::parse();
|
||||
|
||||
// Init network device
|
||||
let device = TunTapDevice::new(&opts.tap).unwrap();
|
||||
|
||||
// Choose between dhcp or static ip
|
||||
let config = if opts.static_ip {
|
||||
ConfigStrategy::Static(embassy_net::Config {
|
||||
address: Ipv4Cidr::new(Ipv4Address::new(192, 168, 69, 2), 24),
|
||||
dns_servers: Vec::new(),
|
||||
gateway: Some(Ipv4Address::new(192, 168, 69, 1)),
|
||||
})
|
||||
} else {
|
||||
ConfigStrategy::Dhcp
|
||||
};
|
||||
|
||||
// Generate random seed
|
||||
let mut seed = [0; 8];
|
||||
OsRng.fill_bytes(&mut seed);
|
||||
let seed = u64::from_le_bytes(seed);
|
||||
|
||||
// Init network stack
|
||||
let stack = &*forever!(Stack::new(
|
||||
device,
|
||||
config,
|
||||
forever!(StackResources::<1, 2, 8>::new()),
|
||||
seed
|
||||
));
|
||||
|
||||
// Launch network task
|
||||
spawner.spawn(net_task(stack)).unwrap();
|
||||
|
||||
// Then we can use it!
|
||||
let mut rx_meta = [PacketMetadata::EMPTY; 16];
|
||||
let mut rx_buffer = [0; 4096];
|
||||
let mut tx_meta = [PacketMetadata::EMPTY; 16];
|
||||
let mut tx_buffer = [0; 4096];
|
||||
let mut buf = [0; 4096];
|
||||
|
||||
let mut socket = UdpSocket::new(stack, &mut rx_meta, &mut rx_buffer, &mut tx_meta, &mut tx_buffer);
|
||||
socket.bind(9400).unwrap();
|
||||
|
||||
loop {
|
||||
let (n, ep) = socket.recv_from(&mut buf).await.unwrap();
|
||||
if let Ok(s) = core::str::from_utf8(&buf[..n]) {
|
||||
info!("ECHO (to {}): {}", ep, s);
|
||||
} else {
|
||||
info!("ECHO (to {}): bytearray len {}", ep, n);
|
||||
}
|
||||
socket.send_to(&buf[..n], ep).await.unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
static EXECUTOR: Forever<Executor> = Forever::new();
|
||||
|
||||
fn main() {
|
||||
env_logger::builder()
|
||||
.filter_level(log::LevelFilter::Debug)
|
||||
.filter_module("async_io", log::LevelFilter::Info)
|
||||
.format_timestamp_nanos()
|
||||
.init();
|
||||
|
||||
let executor = EXECUTOR.put(Executor::new());
|
||||
executor.run(|spawner| {
|
||||
spawner.spawn(main_task(spawner)).unwrap();
|
||||
});
|
||||
}
|
Loading…
Reference in New Issue
Block a user