Skip to content

Commit

Permalink
udp_async: Implement async traits of upcoming async-embedded-nal > 0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
chrysn committed Jan 17, 2023
1 parent 40bf6aa commit bcf704e
Show file tree
Hide file tree
Showing 6 changed files with 221 additions and 130 deletions.
10 changes: 7 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "std-embedded-nal"
version = "0.1.3"
authors = ["chrysn <[email protected]>"]
edition = "2018"
edition = "2021"

description = "Implementation of the `embedded-nal` traits for large devices that support the standard library"
categories = ["embedded", "hardware-support"]
Expand All @@ -12,15 +12,19 @@ repository = "https://gitlab.com/chrysn/std-embedded-nal"

[dependencies]
embedded-nal = "0.6"
embedded-nal-async = { version = "0.1", optional = true }
embedded-nal-async = { git = "https://github.com/rust-embedded-community/embedded-nal", optional = true }
embedded-io = { version = "0.4", optional = true, features = [ "std" ] }
async-std = { version = "1.12", optional = true }
# If enabled, these traits are implemented as well; they're experimental and
# will hopefully wind up in alter embedded-nal versions, so enabling this has
# no stability guarantees.
embedded-nal-tcpextensions = { version = "0.1", optional = true }

async-io = "^1.9"
nix = { version = "0.25", features = [ "socket", "net", "uio" ] }

[features]
async = [ "embedded-nal-async", "async-std" ]
async = [ "embedded-nal-async", "async-std", "embedded-io" ]

[dev-dependencies]
mio = { version = "0.8", features = [ "os-ext" ] }
Expand Down
12 changes: 6 additions & 6 deletions examples/coapclient_async.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
//! A brutally oversimplified CoAP client that GETs /.well-known/core from localhost:5683
use embedded_nal_async::UdpClientStack;
use embedded_nal_async::{UdpStack, ConnectedUdp};

async fn run<S, E>(stack: &mut S) -> Result<(), E>
where
E: core::fmt::Debug, // Might go away when MSRV goes up to 1.49, see https://github.com/rust-lang/rust/issues/80821
S: UdpClientStack<Error = E>,
S: UdpStack<Error = E>,
S::Connected: ConnectedUdp<Error = E>,
{
let target = embedded_nal::SocketAddr::new(
"::1".parse().unwrap(),
5683,
);

let mut sock = stack.socket().await?;
stack.connect(&mut sock, target).await?;
let (_local, mut sock) = stack.connect(target).await?;
// Data, V1 NON no token, GET, message ID 0x0000, 2x Uri-Path
stack.send(&mut sock, b"\x50\x01\0\0\xbb.well-known\x04core").await?;
sock.send(b"\x50\x01\0\0\xbb.well-known\x04core").await?;

let mut respbuf = [0; 1500];
let (resplen, _) = stack.receive(&mut sock, &mut respbuf).await?;
let resplen = sock.receive_into(&mut respbuf).await?;
let response = &respbuf[..resplen];

println!("Response: {}", String::from_utf8_lossy(response));
Expand Down
25 changes: 25 additions & 0 deletions src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,31 @@ impl From<embedded_nal::IpAddr> for IpAddr {
}
}

// That this is missing zone info on IPv6 probably just makes std::net::IpAddr a bad intermediate
// type.
impl From<nix::libc::in6_pktinfo> for IpAddr {
fn from(input: nix::libc::in6_pktinfo) -> Self {
// FIXME why isn't this having zone infos??
Self(input.ipi6_addr.s6_addr.into())
}
}

impl From<IpAddr> for nix::libc::in6_pktinfo {
fn from(input: IpAddr) -> nix::libc::in6_pktinfo {
let input = match input.0 {
std::net::IpAddr::V6(a) => a,
_ => panic!("IPv6 only so far"),
};
nix::libc::in6_pktinfo {
ipi6_addr: nix::libc::in6_addr {
s6_addr: input.octets(),
},
// FIXME and here it really hurts
ipi6_ifindex: 0,
}
}
}

impl From<IpAddr> for embedded_nal::IpAddr {
fn from(s: IpAddr) -> embedded_nal::IpAddr {
match s.0 {
Expand Down
4 changes: 1 addition & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
//! All implementations use `std::io::Error` as their error type.
//!
//! [embedded-nal]: https://crates.io/crates/embedded-nal
#![cfg_attr(feature = "async", feature(generic_associated_types))] // for implementing -async
#![cfg_attr(feature = "async", feature(type_alias_impl_trait))] // as async blocks
// don't provide types
#![cfg_attr(feature = "async", feature(async_fn_in_trait))]

mod conversion;
mod dns;
Expand Down
Loading

0 comments on commit bcf704e

Please sign in to comment.