Skip to content

Commit

Permalink
chore: Patch edge-net revision and implement new traits + refactor. (#53
Browse files Browse the repository at this point in the history
)

- 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.
  • Loading branch information
AnthonyGrondin authored Oct 16, 2024
1 parent e6e698f commit f292457
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 20 deletions.
30 changes: 22 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand All @@ -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"
Expand All @@ -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 = [
Expand Down Expand Up @@ -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" }
21 changes: 20 additions & 1 deletion esp-mbedtls/src/compat/edge_nal_compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<
Expand Down Expand Up @@ -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<<T as embedded_io::ErrorType>::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
Expand Down
9 changes: 8 additions & 1 deletion esp-mbedtls/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ impl embedded_io::Error for TlsError {
}
}

#[cfg(feature = "edge-nal")]
impl From<edge_nal_embassy::TcpError> 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"))]
Expand Down Expand Up @@ -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,
Expand Down
23 changes: 18 additions & 5 deletions examples/edge_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) => {
Expand All @@ -193,15 +202,19 @@ where
{
type Error = Error<<T as ErrorType>::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?;
Expand Down
10 changes: 5 additions & 5 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit f292457

Please sign in to comment.