Skip to content
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

Update to embassy-net 0.5 #50

Merged
merged 3 commits into from
Jan 2, 2025
Merged
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
2 changes: 1 addition & 1 deletion clippy.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
future-size-threshold = 200
future-size-threshold = 300
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 edge_nal::Dns for Dns<'_> {
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
47 changes: 4 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 All @@ -25,6 +25,7 @@ pub(crate) struct Pool<T, const N: usize> {
impl<T, const N: usize> Pool<T, N> {
#[allow(clippy::declare_interior_mutable_const)]
const VALUE: Cell<bool> = Cell::new(false);
#[allow(clippy::declare_interior_mutable_const)]
const UNINIT: UnsafeCell<MaybeUninit<T>> = UnsafeCell::new(MaybeUninit::uninit());

const fn new() -> Self {
Expand Down Expand Up @@ -59,7 +60,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 +72,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"),
}
}
Loading
Loading