Skip to content

Commit

Permalink
Update to embassy-net 0.5 (#49)
Browse files Browse the repository at this point in the history
* Adapt changes to upstream embassy-net

* Remove leftover imports

And use the now release embassy-net.

* Implement Readable for embassy-net 0.5

* Simplify the internal helpers

---------

Co-authored-by: Shaw Drastin <168159404+showier-drastic@users.noreply.github.com>
  • Loading branch information
2 people authored and ivmarkov committed Dec 11, 2024
1 parent 4c0940b commit a252419
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 153 deletions.
14 changes: 11 additions & 3 deletions edge-nal-embassy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,21 @@ categories = [
"embedded",
"no-std::no-alloc",
"asynchronous",
"network-programming"
"network-programming",
]

[dependencies]
embedded-io-async = { workspace = true }
edge-nal = { workspace = true }
heapless = { workspace = true }
# TODO: Do not require these features and conditionalize the code instead
embassy-net = { version = "0.4", features = ["tcp", "udp", "dns", "proto-ipv6", "medium-ethernet", "proto-ipv4", "igmp"] }
# Do not require these features and conditionalize the code instead
embassy-net = { version = "0.5", features = [
"tcp",
"udp",
"dns",
"proto-ipv6",
"medium-ethernet",
"proto-ipv4",
"multicast",
] }
embassy-futures = { workspace = true }
3 changes: 1 addition & 2 deletions edge-nal-embassy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ All traits except `Readable` which - while implemented - panics if called.

### UDP

* All traits except `UdpConnect`.
* All traits except `UdpConnect`.
* `MulticastV6` - while implemented - panics if `join_v6` / `leave_v6` are called.
* `Readable` - while implemented - panics if called.

### Raw sockets

Expand Down
24 changes: 6 additions & 18 deletions edge-nal-embassy/src/dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,25 @@ use edge_nal::AddrType;

use embassy_net::{
dns::{DnsQueryType, Error},
driver::Driver,
Stack,
};
use embedded_io_async::ErrorKind;

use crate::to_net_addr;

/// A struct that implements the `Dns` trait from `edge-nal`
pub struct Dns<'a, D>
where
D: Driver + 'static,
{
stack: &'a Stack<D>,
pub struct Dns<'a> {
stack: Stack<'a>,
}

impl<'a, D> Dns<'a, D>
where
D: Driver + 'static,
{
impl<'a> Dns<'a> {
/// Create a new `Dns` instance for the provided Embassy networking stack
///
/// NOTE: If using DHCP, make sure it has reconfigured the stack to ensure the DNS servers are updated
pub fn new(stack: &'a Stack<D>) -> Self {
pub fn new(stack: Stack<'a>) -> Self {
Self { stack }
}
}

impl<'a, D> edge_nal::Dns for Dns<'a, D>
where
D: Driver + 'static,
{
impl<'a> edge_nal::Dns for Dns<'a> {
type Error = DnsError;

async fn get_host_by_name(
Expand All @@ -48,7 +36,7 @@ where
};
let addrs = self.stack.dns_query(host, qtype).await?;
if let Some(first) = addrs.first() {
Ok(to_net_addr(*first))
Ok((*first).into())
} else {
Err(Error::Failed.into())
}
Expand Down
46 changes: 3 additions & 43 deletions edge-nal-embassy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

use core::cell::{Cell, UnsafeCell};
use core::mem::MaybeUninit;
use core::net::{IpAddr, SocketAddr};
use core::net::SocketAddr;
use core::ptr::NonNull;

use embassy_net::{IpAddress, IpEndpoint, IpListenEndpoint};
use embassy_net::IpEndpoint;

pub use dns::*;
pub use tcp::*;
Expand Down Expand Up @@ -59,7 +59,7 @@ impl<T, const N: usize> Pool<T, N> {
}

pub(crate) fn to_net_socket(socket: IpEndpoint) -> SocketAddr {
SocketAddr::new(to_net_addr(socket.addr), socket.port)
SocketAddr::new(socket.addr.into(), socket.port)
}

// pub(crate) fn to_net_socket2(socket: IpListenEndpoint) -> SocketAddr {
Expand All @@ -71,43 +71,3 @@ pub(crate) fn to_net_socket(socket: IpEndpoint) -> SocketAddr {
// socket.port,
// )
// }

pub(crate) fn to_emb_socket(socket: SocketAddr) -> IpEndpoint {
IpEndpoint {
addr: to_emb_addr(socket.ip()),
port: socket.port(),
}
}

pub(crate) fn to_emb_bind_socket(socket: SocketAddr) -> IpListenEndpoint {
IpListenEndpoint {
addr: (!socket.ip().is_unspecified()).then(|| to_emb_addr(socket.ip())),
port: socket.port(),
}
}

pub(crate) fn to_net_addr(addr: IpAddress) -> IpAddr {
match addr {
//#[cfg(feature = "proto-ipv4")]
IpAddress::Ipv4(addr) => addr.0.into(),
// #[cfg(not(feature = "proto-ipv4"))]
// IpAddr::V4(_) => panic!("ipv4 support not enabled"),
//#[cfg(feature = "proto-ipv6")]
IpAddress::Ipv6(addr) => addr.0.into(),
// #[cfg(not(feature = "proto-ipv6"))]
// IpAddr::V6(_) => panic!("ipv6 support not enabled"),
}
}

pub(crate) fn to_emb_addr(addr: IpAddr) -> IpAddress {
match addr {
//#[cfg(feature = "proto-ipv4")]
IpAddr::V4(addr) => IpAddress::Ipv4(embassy_net::Ipv4Address::from_bytes(&addr.octets())),
// #[cfg(not(feature = "proto-ipv4"))]
// IpAddr::V4(_) => panic!("ipv4 support not enabled"),
//#[cfg(feature = "proto-ipv6")]
IpAddr::V6(addr) => IpAddress::Ipv6(embassy_net::Ipv6Address::from_bytes(&addr.octets())),
// #[cfg(not(feature = "proto-ipv6"))]
// IpAddr::V6(_) => panic!("ipv6 support not enabled"),
}
}
50 changes: 20 additions & 30 deletions edge-nal-embassy/src/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,33 @@ use edge_nal::{Close, Readable, TcpBind, TcpConnect, TcpShutdown, TcpSplit};

use embassy_futures::join::join;

use embassy_net::driver::Driver;
use embassy_net::tcp::{AcceptError, ConnectError, Error, TcpReader, TcpWriter};
use embassy_net::Stack;

use embedded_io_async::{ErrorKind, ErrorType, Read, Write};

use crate::{to_emb_bind_socket, to_emb_socket, to_net_socket, Pool};
use crate::{to_net_socket, Pool};

/// A struct that implements the `TcpConnect` and `TcpBind` factory traits from `edge-nal`
/// Capable of managing up to N concurrent connections with TX and RX buffers according to TX_SZ and RX_SZ.
pub struct Tcp<'d, D: Driver, const N: usize, const TX_SZ: usize = 1024, const RX_SZ: usize = 1024>
{
stack: &'d Stack<D>,
pub struct Tcp<'d, const N: usize, const TX_SZ: usize = 1024, const RX_SZ: usize = 1024> {
stack: Stack<'d>,
buffers: &'d TcpBuffers<N, TX_SZ, RX_SZ>,
}

impl<'d, D: Driver, const N: usize, const TX_SZ: usize, const RX_SZ: usize>
Tcp<'d, D, N, TX_SZ, RX_SZ>
{
impl<'d, const N: usize, const TX_SZ: usize, const RX_SZ: usize> Tcp<'d, N, TX_SZ, RX_SZ> {
/// Create a new `Tcp` instance for the provided Embassy networking stack, using the provided TCP buffers
///
/// Ensure that the number of buffers `N` fits within StackResources<N> of
/// [embassy_net::Stack], while taking into account the sockets used for DHCP, DNS, etc. else
/// [smoltcp::iface::SocketSet] will panic with `adding a socket to a full SocketSet`.
pub fn new(stack: &'d Stack<D>, buffers: &'d TcpBuffers<N, TX_SZ, RX_SZ>) -> Self {
pub fn new(stack: Stack<'d>, buffers: &'d TcpBuffers<N, TX_SZ, RX_SZ>) -> Self {
Self { stack, buffers }
}
}

impl<'d, D: Driver, const N: usize, const TX_SZ: usize, const RX_SZ: usize> TcpConnect
for Tcp<'d, D, N, TX_SZ, RX_SZ>
impl<'d, const N: usize, const TX_SZ: usize, const RX_SZ: usize> TcpConnect
for Tcp<'d, N, TX_SZ, RX_SZ>
{
type Error = TcpError;

Expand All @@ -48,19 +44,19 @@ impl<'d, D: Driver, const N: usize, const TX_SZ: usize, const RX_SZ: usize> TcpC
async fn connect(&self, remote: SocketAddr) -> Result<Self::Socket<'_>, Self::Error> {
let mut socket = TcpSocket::new(self.stack, self.buffers)?;

socket.socket.connect(to_emb_socket(remote)).await?;
socket.socket.connect(remote).await?;

Ok(socket)
}
}

impl<'d, D: Driver, const N: usize, const TX_SZ: usize, const RX_SZ: usize> TcpBind
for Tcp<'d, D, N, TX_SZ, RX_SZ>
impl<'d, const N: usize, const TX_SZ: usize, const RX_SZ: usize> TcpBind
for Tcp<'d, N, TX_SZ, RX_SZ>
{
type Error = TcpError;

type Accept<'a>
= TcpAccept<'a, D, N, TX_SZ, RX_SZ>
= TcpAccept<'a, N, TX_SZ, RX_SZ>
where
Self: 'a;

Expand All @@ -70,19 +66,13 @@ impl<'d, D: Driver, const N: usize, const TX_SZ: usize, const RX_SZ: usize> TcpB
}

/// Represents an acceptor for incoming TCP client connections. Implements the `TcpAccept` factory trait from `edge-nal`
pub struct TcpAccept<
'd,
D: Driver,
const N: usize,
const TX_SZ: usize = 1024,
const RX_SZ: usize = 1024,
> {
stack: &'d Tcp<'d, D, N, TX_SZ, RX_SZ>,
pub struct TcpAccept<'d, const N: usize, const TX_SZ: usize = 1024, const RX_SZ: usize = 1024> {
stack: &'d Tcp<'d, N, TX_SZ, RX_SZ>,
local: SocketAddr,
}

impl<'d, D: Driver, const N: usize, const TX_SZ: usize, const RX_SZ: usize> edge_nal::TcpAccept
for TcpAccept<'d, D, N, TX_SZ, RX_SZ>
impl<'d, const N: usize, const TX_SZ: usize, const RX_SZ: usize> edge_nal::TcpAccept
for TcpAccept<'d, N, TX_SZ, RX_SZ>
{
type Error = TcpError;

Expand All @@ -94,7 +84,7 @@ impl<'d, D: Driver, const N: usize, const TX_SZ: usize, const RX_SZ: usize> edge
async fn accept(&self) -> Result<(SocketAddr, Self::Socket<'_>), Self::Error> {
let mut socket = TcpSocket::new(self.stack.stack, self.stack.buffers)?;

socket.socket.accept(to_emb_bind_socket(self.local)).await?;
socket.socket.accept(self.local).await?;

let local_endpoint = socket.socket.local_endpoint().unwrap();

Expand All @@ -111,8 +101,8 @@ pub struct TcpSocket<'d, const N: usize, const TX_SZ: usize, const RX_SZ: usize>
}

impl<'d, const N: usize, const TX_SZ: usize, const RX_SZ: usize> TcpSocket<'d, N, TX_SZ, RX_SZ> {
fn new<D: Driver>(
stack: &'d Stack<D>,
fn new(
stack: Stack<'d>,
stack_buffers: &'d TcpBuffers<N, TX_SZ, RX_SZ>,
) -> Result<Self, TcpError> {
let mut socket_buffers = stack_buffers.pool.alloc().ok_or(TcpError::NoBuffers)?;
Expand Down Expand Up @@ -214,7 +204,7 @@ impl<'d, const N: usize, const TX_SZ: usize, const RX_SZ: usize> Readable
for TcpSocket<'d, N, TX_SZ, RX_SZ>
{
async fn readable(&mut self) -> Result<(), Self::Error> {
panic!("Not implemented yet")
Ok(self.socket.wait_read_ready().await)
}
}

Expand Down Expand Up @@ -246,7 +236,7 @@ impl<'a> Read for TcpSocketRead<'a> {

impl<'a> Readable for TcpSocketRead<'a> {
async fn readable(&mut self) -> Result<(), Self::Error> {
panic!("Not implemented yet")
Ok(self.0.wait_read_ready().await)
}
}

Expand Down
Loading

0 comments on commit a252419

Please sign in to comment.