From 03f139f7f17a77b85cf5b8153a54aec8163ab93a Mon Sep 17 00:00:00 2001 From: Anthony Grondin <104731965+AnthonyGrondin@users.noreply.github.com> Date: Sun, 13 Oct 2024 00:40:22 -0400 Subject: [PATCH] chore: Patch edge-net revision and implement new traits + refactor. - Implements the new TcpShutdown trait for closing connections. - Refactor the handler code to handle the new timeout mecanism. - Add helper feature to build edge_server example. --- Cargo.toml | 30 +++++++++++++++++------ esp-mbedtls/src/compat/edge_nal_compat.rs | 21 +++++++++++++++- esp-mbedtls/src/lib.rs | 9 ++++++- examples/edge_server.rs | 23 +++++++++++++---- justfile | 10 ++++---- 5 files changed, 73 insertions(+), 20 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d863496..f329dff 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,13 +8,15 @@ rust-version = "1.75" [profile.release] debug = true -lto = false +lto = "fat" +opt-level = "s" + [profile.release.package.esp-wifi] opt-level = 3 [profile.dev] -lto = false +lto = "fat" [profile.dev.package.esp-wifi] opt-level = 3 @@ -42,10 +44,7 @@ embassy-net = { version = "0.4.0", features = [ ], optional = true } -esp-wifi = { version = "0.10.1", features = [ - "phy-enable-usb", - "wifi-default", -] } +esp-wifi = { version = "0.10.1", features = ["phy-enable-usb", "wifi-default"] } smoltcp = { version = "0.11.0", default-features = false, features = [ "proto-igmp", "proto-ipv4", @@ -67,6 +66,7 @@ static_cell = { version = "2.1", features = ["nightly"] } esp-mbedtls = { path = "./esp-mbedtls" } edge-http = { version = "0.3.0", optional = true } +edge-nal = { version = "0.3.0", optional = true } edge-nal-embassy = { version = "0.3.0", optional = true } cfg-if = "1.0.0" esp-alloc = "0.5.0" @@ -93,7 +93,7 @@ required-features = ["async"] [[example]] name = "edge_server" -required-features = ["async", "esp-hal-embassy", "edge-nal-embassy", "edge-http", "esp-mbedtls/edge-nal"] +required-features = ["async", "edge-server"] [features] esp32 = [ @@ -136,5 +136,19 @@ async = [ "embassy-time", "dep:embedded-io-async", "esp-mbedtls/async", - "esp-hal-embassy" + "esp-hal-embassy", ] + +# Helper feature to enable all dependencies required for edge-net +edge-server = [ + "edge-nal", + "edge-nal-embassy", + "edge-http", + "esp-mbedtls/edge-nal", +] + +# Patch until new release +[patch.crates-io] +edge-http = { git = "https://github.com/ivmarkov/edge-net", rev = "f90468953aec1d476ba52fe3b63f392a07bb9daa" } +edge-nal = { git = "https://github.com/ivmarkov/edge-net", rev = "f90468953aec1d476ba52fe3b63f392a07bb9daa" } +edge-nal-embassy = { git = "https://github.com/ivmarkov/edge-net", rev = "f90468953aec1d476ba52fe3b63f392a07bb9daa" } diff --git a/esp-mbedtls/src/compat/edge_nal_compat.rs b/esp-mbedtls/src/compat/edge_nal_compat.rs index 938ad22..6056bd8 100644 --- a/esp-mbedtls/src/compat/edge_nal_compat.rs +++ b/esp-mbedtls/src/compat/edge_nal_compat.rs @@ -7,7 +7,7 @@ use core::{ ptr::NonNull, }; -use edge_nal::TcpBind; +use edge_nal::{Close, TcpBind}; use edge_nal_embassy::{Tcp, TcpAccept, TcpSocket}; pub struct TlsAcceptor< @@ -99,6 +99,25 @@ where } } +impl<'a, T, const RX_SIZE: usize, const TX_SIZE: usize> edge_nal::TcpShutdown + for AsyncConnectedSession<'a, T, RX_SIZE, TX_SIZE> +where + T: embedded_io_async::Read + embedded_io_async::Write + edge_nal::TcpShutdown, + TlsError: From<::Error>, +{ + async fn close(&mut self, what: Close) -> Result<(), Self::Error> { + self.session + .stream + .close(what) + .await + .map_err(TlsError::from) + } + + async fn abort(&mut self) -> Result<(), Self::Error> { + self.session.stream.abort().await.map_err(TlsError::from) + } +} + impl<'d, D, const N: usize, const RX_SZ: usize, const TX_SZ: usize> edge_nal::TcpAccept for TlsAcceptor<'d, D, N, RX_SZ, TX_SZ> where diff --git a/esp-mbedtls/src/lib.rs b/esp-mbedtls/src/lib.rs index 3191332..4415855 100644 --- a/esp-mbedtls/src/lib.rs +++ b/esp-mbedtls/src/lib.rs @@ -107,6 +107,13 @@ impl embedded_io::Error for TlsError { } } +#[cfg(feature = "edge-nal")] +impl From for TlsError { + fn from(value: edge_nal_embassy::TcpError) -> Self { + TlsError::TcpError(value) + } +} + #[allow(unused)] pub fn set_debug(level: u32) { #[cfg(not(target_arch = "xtensa"))] @@ -682,7 +689,7 @@ pub mod asynch { pub use crate::compat::edge_nal_compat::*; pub struct Session<'a, T, const RX_SIZE: usize = 4096, const TX_SIZE: usize = 4096> { - stream: T, + pub(crate) stream: T, drbg_context: *mut mbedtls_ctr_drbg_context, ssl_context: *mut mbedtls_ssl_context, ssl_config: *mut mbedtls_ssl_config, diff --git a/examples/edge_server.rs b/examples/edge_server.rs index def8bd4..4e3d8cb 100644 --- a/examples/edge_server.rs +++ b/examples/edge_server.rs @@ -50,6 +50,9 @@ const SSID: &str = env!("SSID"); const PASSWORD: &str = env!("PASSWORD"); /// Number of sockets used for the HTTPS server +#[cfg(feature = "esp32")] +const SERVER_SOCKETS: usize = 1; +#[cfg(not(feature = "esp32"))] const SERVER_SOCKETS: usize = 2; /// Total number of sockets used for the application @@ -171,9 +174,15 @@ async fn main(spawner: Spawner) -> ! { ) .await .with_hardware_rsa(&mut peripherals.RSA); - match server.run(tls_acceptor, HttpHandler, Some(15_000)).await { + match server + .run( + edge_nal::WithTimeout::new(15_000, tls_acceptor), + HttpHandler, + ) + .await + { Ok(_) => {} - Err(Error::Io(TlsError::MbedTlsError(-30592))) => { + Err(Error::Io(edge_nal::WithTimeoutError::Error(TlsError::MbedTlsError(-30592)))) => { println!("Fatal message: Please enable the exception for a self-signed certificate in your browser"); } Err(error) => { @@ -193,15 +202,19 @@ where { type Error = Error<::Error>; - async fn handle(&self, connection: &mut Connection<'b, T, N>) -> Result<(), Self::Error> { + async fn handle( + &self, + _task_id: impl core::fmt::Display + Copy, + connection: &mut Connection<'b, T, N>, + ) -> Result<(), Self::Error> { println!("Got new connection"); let headers = connection.headers()?; - if !matches!(headers.method, Some(Method::Get)) { + if headers.method != Method::Get { connection .initiate_response(405, Some("Method Not Allowed"), &[]) .await?; - } else if !matches!(headers.path, Some("/")) { + } else if headers.path != "/" { connection .initiate_response(404, Some("Not Found"), &[]) .await?; diff --git a/justfile b/justfile index 45258b1..b51fef2 100644 --- a/justfile +++ b/justfile @@ -8,11 +8,11 @@ all: (check "esp32" "esp") (check "esp32s3" "esp") (check "esp32c3" "nightly-202 check arch toolchain: cargo +{{ toolchain }} b{{ arch }} --example sync_client cargo +{{ toolchain }} b{{ arch }} --example sync_client_mTLS - cargo +{{ toolchain }} b{{ arch }} --example async_client --features="async,esp-hal-embassy" - cargo +{{ toolchain }} b{{ arch }} --example async_client_mTLS --features="async,esp-hal-embassy" + cargo +{{ toolchain }} b{{ arch }} --example async_client --features="async" + cargo +{{ toolchain }} b{{ arch }} --example async_client_mTLS --features="async" cargo +{{ toolchain }} b{{ arch }} --example sync_server cargo +{{ toolchain }} b{{ arch }} --example sync_server_mTLS - cargo +{{ toolchain }} b{{ arch }} --example async_server --features="async,esp-hal-embassy" - cargo +{{ toolchain }} b{{ arch }} --example async_server_mTLS --features="async,esp-hal-embassy" - cargo +{{ toolchain }} b{{ arch }} --example edge_server --features="async,esp-hal-embassy,edge-nal-embassy,edge-http,esp-mbedtls/edge-nal" + cargo +{{ toolchain }} b{{ arch }} --example async_server --features="async" + cargo +{{ toolchain }} b{{ arch }} --example async_server_mTLS --features="async" + cargo +{{ toolchain }} b{{ arch }} --example edge_server --features="async,edge-server" cargo +{{ toolchain }} fmt --all -- --check