Skip to content

Update to embassy-net 0.5 #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
20 changes: 5 additions & 15 deletions edge-nal-embassy/src/dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,27 @@ 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 Down
8 changes: 4 additions & 4 deletions edge-nal-embassy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ pub(crate) fn to_emb_bind_socket(socket: SocketAddr) -> IpListenEndpoint {
pub(crate) fn to_net_addr(addr: IpAddress) -> IpAddr {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this function even necessary anymore given that now smoltcp (and embassy-net) did switch to core::net for IP addresses?

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

match addr {
//#[cfg(feature = "proto-ipv4")]
IpAddr::V4(addr) => IpAddress::Ipv4(embassy_net::Ipv4Address::from_bytes(&addr.octets())),
IpAddr::V4(addr) => IpAddress::Ipv4(addr),
// #[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())),
IpAddr::V6(addr) => IpAddress::Ipv6(addr),
// #[cfg(not(feature = "proto-ipv6"))]
// IpAddr::V6(_) => panic!("ipv6 support not enabled"),
}
Expand Down
44 changes: 17 additions & 27 deletions edge-nal-embassy/src/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ 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;

Expand All @@ -16,27 +15,24 @@ use crate::{to_emb_bind_socket, to_emb_socket, 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 @@ -54,13 +50,13 @@ impl<'d, D: Driver, const N: usize, const TX_SZ: usize, const RX_SZ: usize> TcpC
}
}

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 Down Expand Up @@ -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
Loading