Skip to content

Commit f292457

Browse files
chore: Patch edge-net revision and implement new traits + refactor. (#53)
- 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.
1 parent e6e698f commit f292457

File tree

5 files changed

+73
-20
lines changed

5 files changed

+73
-20
lines changed

Cargo.toml

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ rust-version = "1.75"
88

99
[profile.release]
1010
debug = true
11-
lto = false
11+
lto = "fat"
12+
opt-level = "s"
13+
1214

1315
[profile.release.package.esp-wifi]
1416
opt-level = 3
1517

1618
[profile.dev]
17-
lto = false
19+
lto = "fat"
1820

1921
[profile.dev.package.esp-wifi]
2022
opt-level = 3
@@ -42,10 +44,7 @@ embassy-net = { version = "0.4.0", features = [
4244
], optional = true }
4345

4446

45-
esp-wifi = { version = "0.10.1", features = [
46-
"phy-enable-usb",
47-
"wifi-default",
48-
] }
47+
esp-wifi = { version = "0.10.1", features = ["phy-enable-usb", "wifi-default"] }
4948
smoltcp = { version = "0.11.0", default-features = false, features = [
5049
"proto-igmp",
5150
"proto-ipv4",
@@ -67,6 +66,7 @@ static_cell = { version = "2.1", features = ["nightly"] }
6766
esp-mbedtls = { path = "./esp-mbedtls" }
6867

6968
edge-http = { version = "0.3.0", optional = true }
69+
edge-nal = { version = "0.3.0", optional = true }
7070
edge-nal-embassy = { version = "0.3.0", optional = true }
7171
cfg-if = "1.0.0"
7272
esp-alloc = "0.5.0"
@@ -93,7 +93,7 @@ required-features = ["async"]
9393

9494
[[example]]
9595
name = "edge_server"
96-
required-features = ["async", "esp-hal-embassy", "edge-nal-embassy", "edge-http", "esp-mbedtls/edge-nal"]
96+
required-features = ["async", "edge-server"]
9797

9898
[features]
9999
esp32 = [
@@ -136,5 +136,19 @@ async = [
136136
"embassy-time",
137137
"dep:embedded-io-async",
138138
"esp-mbedtls/async",
139-
"esp-hal-embassy"
139+
"esp-hal-embassy",
140140
]
141+
142+
# Helper feature to enable all dependencies required for edge-net
143+
edge-server = [
144+
"edge-nal",
145+
"edge-nal-embassy",
146+
"edge-http",
147+
"esp-mbedtls/edge-nal",
148+
]
149+
150+
# Patch until new release
151+
[patch.crates-io]
152+
edge-http = { git = "https://github.com/ivmarkov/edge-net", rev = "f90468953aec1d476ba52fe3b63f392a07bb9daa" }
153+
edge-nal = { git = "https://github.com/ivmarkov/edge-net", rev = "f90468953aec1d476ba52fe3b63f392a07bb9daa" }
154+
edge-nal-embassy = { git = "https://github.com/ivmarkov/edge-net", rev = "f90468953aec1d476ba52fe3b63f392a07bb9daa" }

esp-mbedtls/src/compat/edge_nal_compat.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use core::{
77
ptr::NonNull,
88
};
99

10-
use edge_nal::TcpBind;
10+
use edge_nal::{Close, TcpBind};
1111
use edge_nal_embassy::{Tcp, TcpAccept, TcpSocket};
1212

1313
pub struct TlsAcceptor<
@@ -99,6 +99,25 @@ where
9999
}
100100
}
101101

102+
impl<'a, T, const RX_SIZE: usize, const TX_SIZE: usize> edge_nal::TcpShutdown
103+
for AsyncConnectedSession<'a, T, RX_SIZE, TX_SIZE>
104+
where
105+
T: embedded_io_async::Read + embedded_io_async::Write + edge_nal::TcpShutdown,
106+
TlsError: From<<T as embedded_io::ErrorType>::Error>,
107+
{
108+
async fn close(&mut self, what: Close) -> Result<(), Self::Error> {
109+
self.session
110+
.stream
111+
.close(what)
112+
.await
113+
.map_err(TlsError::from)
114+
}
115+
116+
async fn abort(&mut self) -> Result<(), Self::Error> {
117+
self.session.stream.abort().await.map_err(TlsError::from)
118+
}
119+
}
120+
102121
impl<'d, D, const N: usize, const RX_SZ: usize, const TX_SZ: usize> edge_nal::TcpAccept
103122
for TlsAcceptor<'d, D, N, RX_SZ, TX_SZ>
104123
where

esp-mbedtls/src/lib.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,13 @@ impl embedded_io::Error for TlsError {
107107
}
108108
}
109109

110+
#[cfg(feature = "edge-nal")]
111+
impl From<edge_nal_embassy::TcpError> for TlsError {
112+
fn from(value: edge_nal_embassy::TcpError) -> Self {
113+
TlsError::TcpError(value)
114+
}
115+
}
116+
110117
#[allow(unused)]
111118
pub fn set_debug(level: u32) {
112119
#[cfg(not(target_arch = "xtensa"))]
@@ -682,7 +689,7 @@ pub mod asynch {
682689
pub use crate::compat::edge_nal_compat::*;
683690

684691
pub struct Session<'a, T, const RX_SIZE: usize = 4096, const TX_SIZE: usize = 4096> {
685-
stream: T,
692+
pub(crate) stream: T,
686693
drbg_context: *mut mbedtls_ctr_drbg_context,
687694
ssl_context: *mut mbedtls_ssl_context,
688695
ssl_config: *mut mbedtls_ssl_config,

examples/edge_server.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ const SSID: &str = env!("SSID");
5050
const PASSWORD: &str = env!("PASSWORD");
5151

5252
/// Number of sockets used for the HTTPS server
53+
#[cfg(feature = "esp32")]
54+
const SERVER_SOCKETS: usize = 1;
55+
#[cfg(not(feature = "esp32"))]
5356
const SERVER_SOCKETS: usize = 2;
5457

5558
/// Total number of sockets used for the application
@@ -171,9 +174,15 @@ async fn main(spawner: Spawner) -> ! {
171174
)
172175
.await
173176
.with_hardware_rsa(&mut peripherals.RSA);
174-
match server.run(tls_acceptor, HttpHandler, Some(15_000)).await {
177+
match server
178+
.run(
179+
edge_nal::WithTimeout::new(15_000, tls_acceptor),
180+
HttpHandler,
181+
)
182+
.await
183+
{
175184
Ok(_) => {}
176-
Err(Error::Io(TlsError::MbedTlsError(-30592))) => {
185+
Err(Error::Io(edge_nal::WithTimeoutError::Error(TlsError::MbedTlsError(-30592)))) => {
177186
println!("Fatal message: Please enable the exception for a self-signed certificate in your browser");
178187
}
179188
Err(error) => {
@@ -193,15 +202,19 @@ where
193202
{
194203
type Error = Error<<T as ErrorType>::Error>;
195204

196-
async fn handle(&self, connection: &mut Connection<'b, T, N>) -> Result<(), Self::Error> {
205+
async fn handle(
206+
&self,
207+
_task_id: impl core::fmt::Display + Copy,
208+
connection: &mut Connection<'b, T, N>,
209+
) -> Result<(), Self::Error> {
197210
println!("Got new connection");
198211
let headers = connection.headers()?;
199212

200-
if !matches!(headers.method, Some(Method::Get)) {
213+
if headers.method != Method::Get {
201214
connection
202215
.initiate_response(405, Some("Method Not Allowed"), &[])
203216
.await?;
204-
} else if !matches!(headers.path, Some("/")) {
217+
} else if headers.path != "/" {
205218
connection
206219
.initiate_response(404, Some("Not Found"), &[])
207220
.await?;

justfile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ all: (check "esp32" "esp") (check "esp32s3" "esp") (check "esp32c3" "nightly-202
88
check arch toolchain:
99
cargo +{{ toolchain }} b{{ arch }} --example sync_client
1010
cargo +{{ toolchain }} b{{ arch }} --example sync_client_mTLS
11-
cargo +{{ toolchain }} b{{ arch }} --example async_client --features="async,esp-hal-embassy"
12-
cargo +{{ toolchain }} b{{ arch }} --example async_client_mTLS --features="async,esp-hal-embassy"
11+
cargo +{{ toolchain }} b{{ arch }} --example async_client --features="async"
12+
cargo +{{ toolchain }} b{{ arch }} --example async_client_mTLS --features="async"
1313
cargo +{{ toolchain }} b{{ arch }} --example sync_server
1414
cargo +{{ toolchain }} b{{ arch }} --example sync_server_mTLS
15-
cargo +{{ toolchain }} b{{ arch }} --example async_server --features="async,esp-hal-embassy"
16-
cargo +{{ toolchain }} b{{ arch }} --example async_server_mTLS --features="async,esp-hal-embassy"
17-
cargo +{{ toolchain }} b{{ arch }} --example edge_server --features="async,esp-hal-embassy,edge-nal-embassy,edge-http,esp-mbedtls/edge-nal"
15+
cargo +{{ toolchain }} b{{ arch }} --example async_server --features="async"
16+
cargo +{{ toolchain }} b{{ arch }} --example async_server_mTLS --features="async"
17+
cargo +{{ toolchain }} b{{ arch }} --example edge_server --features="async,edge-server"
1818
cargo +{{ toolchain }} fmt --all -- --check

0 commit comments

Comments
 (0)