Skip to content

Commit

Permalink
fixup! fix(coap): Apply clippy fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
chrysn committed Oct 30, 2024
1 parent a5e234b commit 2122040
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 33 deletions.
2 changes: 1 addition & 1 deletion clippy.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Require SAFETY docs, as well as a few other lints, for private items
check-private-items = true

doc-valid-idents = ["MHz", "GHz", "THz", ".."]
doc-valid-idents = ["CoAP", "MHz", "GHz", "THz", ".."]
12 changes: 7 additions & 5 deletions src/riot-rs-coap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use coap_handler_implementations::ReportingHandlerBuilder;
use coapcore::seccontext;
use embassy_net::udp::{PacketMetadata, UdpSocket};
use embassy_sync::once_lock::OnceLock;
use riot_rs_debug::log::*;
use riot_rs_debug::log::info;
use riot_rs_embassy::sendcell::SendCell;
use static_cell::StaticCell;

Expand All @@ -34,6 +34,11 @@ static CLIENT: OnceLock<
///
/// This can only be run once, as it sets up a system wide CoAP handler.
pub async fn coap_run(handler: impl coap_handler::Handler + coap_handler::Reporting) -> ! {
use hexlit::hex;
const R: &[u8] = &hex!("72cc4761dbd4c78f758931aa589d348d1ef874a7e303ede2f140dcf3e6aa4aac");

static COAP: StaticCell<embedded_nal_coap::CoAPShared<CONCURRENT_REQUESTS>> = StaticCell::new();

let stack = riot_rs_embassy::network::network_stack().await.unwrap();

// FIXME trim to CoAP requirements
Expand All @@ -59,8 +64,6 @@ pub async fn coap_run(handler: impl coap_handler::Handler + coap_handler::Report
.await
.unwrap();

use hexlit::hex;
const R: &[u8] = &hex!("72cc4761dbd4c78f758931aa589d348d1ef874a7e303ede2f140dcf3e6aa4aac");
let own_identity = (
&lakers::CredentialRPK::new(lakers::EdhocMessageBuffer::new_from_slice(&hex!("A2026008A101A5010202410A2001215820BBC34960526EA4D32E940CAD2A234148DDC21791A12AFBCBAC93622046DD44F02258204519E257236B2A0CE2023F0931F1F386CA7AFDA64FCDE0108C224C51EABF6072")).expect("Credential should be small enough")).expect("Credential should be processable"),
R,
Expand All @@ -75,8 +78,7 @@ pub async fn coap_run(handler: impl coap_handler::Handler + coap_handler::Report

info!("Server is ready.");

static COAP: StaticCell<embedded_nal_coap::CoAPShared<CONCURRENT_REQUESTS>> = StaticCell::new();
let coap = COAP.init_with(|| embedded_nal_coap::CoAPShared::new());
let coap = COAP.init_with(embedded_nal_coap::CoAPShared::new);
let (client, server) = coap.split();
CLIENT
.init(SendCell::new_async(client).await)
Expand Down
40 changes: 22 additions & 18 deletions src/riot-rs-coap/src/udp_nal/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
//! UDP sockets usable through [embedded_nal_async]
//! UDP sockets usable through [`embedded_nal_async`]
//!
//! The full [embedded_nal_async::UdpStack] is *not* implemented at the moment: As its API allows
//! arbitrary creation of movable sockets, embassy's [udp::UdpSocket] type could only be crated if
//! The full [`embedded_nal_async::UdpStack`] is *not* implemented at the moment: As its API allows
//! arbitrary creation of movable sockets, embassy's [`udp::UdpSocket`] type could only be crated if
//! the NAL stack had a pre-allocated pool of sockets with their respective buffers. Nothing rules
//! out such a type, but at the moment, only the bound or connected socket types are implemented
//! with their own constructors from an embassy [crate::Stack] -- for many applications, those are
//! with their own constructors from an embassy [`crate::Stack`] -- for many applications, those are
//! useful enough. (FIXME: Given we construct from Socket, Stack could really be implemented on
//! `Cell<Option<Socket>>` by `.take()`ing, couldn't it?)
//!
//! The constructors of the various socket types mimic the UdpStack's socket creation functions,
//! The constructors of the various socket types mimic the [`UdpStack`]'s socket creation functions,
//! but take an owned (uninitialized) Socket instead of a shared stack.
//!
//! No `bind_single` style constructor is currently provided. FIXME: Not sure we have all the
Expand Down Expand Up @@ -42,22 +42,24 @@ pub struct ConnectedUdp<'a> {

/// A UDP socket that has been bound locally (either to a unique address or just to a port)
///
/// Its operations are accessible through the [nal::UnconnectedUdp] trait.
/// Its operations are accessible through the [`nal::UnconnectedUdp`] trait.
pub struct UnconnectedUdp<'a> {
socket: udp::UdpSocket<'a>,
}

#[allow(
dead_code,
clippy::unused_async,
clippy::missing_errors_doc,
reason = "pub item is being prepared for embedded-nal-async where it will be reachable publicly"
)]
impl<'a> ConnectedUdp<'a> {
/// Create a ConnectedUdp by assigning it a remote and a concrete local address
/// Create a [`ConnectedUdp`] by assigning it a remote and a concrete local address
///
/// ## Prerequisites
///
/// The `socket` must be open (in the sense of smoltcp's `.is_open()`) -- unbound and
/// unconnected.
#[expect(
dead_code,
reason = "pub item is being prepared for embedded-nal-async where it will be reachable publicly"
)]
pub async fn connect_from(
mut socket: udp::UdpSocket<'a>,
local: nal::SocketAddr,
Expand All @@ -75,25 +77,27 @@ impl<'a> ConnectedUdp<'a> {
})
}

/// Create a ConnectedUdp by assigning it a remote and a local address (the latter may happen
/// lazily)
/// Create a [`ConnectedUdp`] by assigning it a remote and a local address (the latter may
/// happen lazily)
///
/// ## Prerequisites
///
/// The `socket` must be open (in the sense of smoltcp's `.is_open()`) -- unbound and
/// unconnected.
#[expect(
dead_code,
reason = "pub item is being prepared for embedded-nal-async where it will be reachable publicly"
)]
pub async fn connect(socket: udp::UdpSocket<'a>, /*, ... */) -> Result<Self, udp::BindError> {
// This is really just a copy of the provided `embedded_nal::udp::UdpStack::connect` method
todo!("use {:p}", &socket)
}
}

#[allow(
dead_code,
clippy::unused_async,
clippy::missing_errors_doc,
reason = "pub item is being prepared for embedded-nal-async where it will be reachable publicly"
)]
impl<'a> UnconnectedUdp<'a> {
/// Create an UnconnectedUdp.
/// Create an [`UnconnectedUdp`].
///
/// The `local` address may be anything from fully specified (address and port) to fully
/// unspecified (port 0, all-zeros address).
Expand Down
29 changes: 20 additions & 9 deletions src/riot-rs-coap/src/udp_nal/util.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
//! Helpers for udp_nal -- conversion and error types
//! Helpers for [`udp_nal`] -- conversion and error types
use embassy_net::{udp, IpAddress, IpEndpoint};
use embedded_nal_async as nal;

/// Converts socket address types between [`embedded_nal_async`] and [`embassy_net`] (internally
/// `smol`).
///
/// # Errors
///
/// This produces an error if an address family is unavailable.
#[allow(
clippy::unnecessary_wraps,
reason = "errors are currently only impossible because for crate feature synchronization reasons, all cfg handling is commented out"
)]
pub(super) fn sockaddr_nal2smol(sockaddr: nal::SocketAddr) -> Result<IpEndpoint, Error> {
match sockaddr {
#[allow(unused)]
Expand Down Expand Up @@ -44,17 +54,21 @@ pub(super) fn sockaddr_smol2nal(endpoint: IpEndpoint) -> nal::SocketAddr {

/// Is the IP address in this type the unspecified address?
///
/// FIXME: What of ::ffff:0.0.0.0? Is that expected to bind to all v4 addresses?
/// FIXME: What of `::ffff:0.0.0.0`? Is that expected to bind to all v4 addresses?
pub(super) fn is_unspec_ip(addr: nal::SocketAddr) -> bool {
match addr {
nal::SocketAddr::V4(sockaddr) => sockaddr.ip().octets() == [0; 4],
nal::SocketAddr::V6(sockaddr) => sockaddr.ip().octets() == [0; 16],
}
}

/// Unified error type for [embedded_nal_async] operations on UDP sockets
/// Unified error type for [`embedded_nal_async`] operations on UDP sockets
#[derive(Debug)]
#[non_exhaustive]
#[allow(
clippy::enum_variant_names,
reason = "false positive -- they're not called SomethingError because they are a Self (which is named Error), but because they contain a type SomethingError"
)]
pub enum Error {
/// Error stemming from failure to send
RecvError(udp::RecvError),
Expand All @@ -71,16 +85,13 @@ pub enum Error {
impl embedded_io_async::Error for Error {
fn kind(&self) -> embedded_io_async::ErrorKind {
match self {
Self::SendError(udp::SendError::NoRoute) => {
embedded_io_async::ErrorKind::AddrNotAvailable
}
Self::BindError(udp::BindError::NoRoute) => {
Self::SendError(udp::SendError::NoRoute) | Self::BindError(udp::BindError::NoRoute) => {
embedded_io_async::ErrorKind::AddrNotAvailable
}
Self::AddressFamilyUnavailable => embedded_io_async::ErrorKind::AddrNotAvailable,
// These should not happen b/c our sockets are typestated.
Self::SendError(udp::SendError::SocketNotBound) => embedded_io_async::ErrorKind::Other,
Self::BindError(udp::BindError::InvalidState) => embedded_io_async::ErrorKind::Other,
Self::SendError(udp::SendError::SocketNotBound) |
Self::BindError(udp::BindError::InvalidState) |
// This should not happen b/c in embedded_nal_async this is not expressed through an
// error.
// FIXME we're not there in this impl yet.
Expand Down

0 comments on commit 2122040

Please sign in to comment.