Skip to content

Commit 360bf59

Browse files
revert: Changes Session back to own the socket (#25)
* revert: Changes Session back to own the socket This was done as a fix for the server, but it seems that letting T and simply passing &mut T fixes the original issue this PR was intended to fix. Other libraries like embedded_tls take ownership of the socket. This allows to move session around without having to keep a reference to the socket alive. Ref: c094cfc * chore(update): Update esp-hal, esp-wifi to latest version.
1 parent 749a0cc commit 360bf59

13 files changed

+85
-137
lines changed

.cargo/config.toml

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ rustflags = [
55
"-C", "link-arg=-Tlinkall.x",
66
"-C", "link-arg=-nostartfiles",
77
"-C", "link-arg=-Trom_functions.x",
8-
"-C", "target-feature=-loop",
98
]
109

1110
[target.riscv32imc-unknown-none-elf]
@@ -18,22 +17,6 @@ rustflags = [
1817
# Required to obtain backtraces (e.g. when using the "esp-backtrace" crate.)
1918
# NOTE: May negatively impact performance of produced code
2019
"-C", "force-frame-pointers",
21-
22-
# comment the cfgs below if you do _not_ wish to emulate atomics.
23-
# enable the atomic codegen option for RISCV
24-
"-C", "target-feature=+a",
25-
# tell the core library have atomics even though it's not specified in the target definition
26-
"--cfg", "target_has_atomic_load_store",
27-
"--cfg", 'target_has_atomic_load_store="8"',
28-
"--cfg", 'target_has_atomic_load_store="16"',
29-
"--cfg", 'target_has_atomic_load_store="32"',
30-
"--cfg", 'target_has_atomic_load_store="ptr"',
31-
# enable cas
32-
"--cfg", "target_has_atomic",
33-
"--cfg", 'target_has_atomic="8"',
34-
"--cfg", 'target_has_atomic="16"',
35-
"--cfg", 'target_has_atomic="32"',
36-
"--cfg", 'target_has_atomic="ptr"',
3720
]
3821

3922
[target.xtensa-esp32s2-none-elf]
@@ -44,22 +27,6 @@ rustflags = [
4427

4528
"-C", "link-arg=-Tlinkall.x",
4629
"-C", "link-arg=-Trom_functions.x",
47-
48-
# Enable the atomic codegen option for Xtensa
49-
"-C", "target-feature=+s32c1i",
50-
51-
# tell the core library have atomics even though it's not specified in the target definition
52-
"--cfg", "target_has_atomic_load_store",
53-
"--cfg", 'target_has_atomic_load_store="8"',
54-
"--cfg", 'target_has_atomic_load_store="16"',
55-
"--cfg", 'target_has_atomic_load_store="32"',
56-
"--cfg", 'target_has_atomic_load_store="ptr"',
57-
# enable cas
58-
"--cfg", "target_has_atomic",
59-
"--cfg", 'target_has_atomic="8"',
60-
"--cfg", 'target_has_atomic="16"',
61-
"--cfg", 'target_has_atomic="32"',
62-
"--cfg", 'target_has_atomic="ptr"',
6330
]
6431

6532
[target.xtensa-esp32s3-none-elf]
@@ -69,7 +36,6 @@ rustflags = [
6936
"-C", "link-arg=-Tlinkall.x",
7037
"-C", "link-arg=-nostartfiles",
7138
"-C", "link-arg=-Trom_functions.x",
72-
"-C", "target-feature=-loop",
7339
]
7440

7541
[unstable]

Cargo.toml

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,24 @@ lto = false
1919
opt-level = 3
2020

2121
[dependencies]
22-
esp32-hal = { version = "0.16.0", optional = true }
23-
esp32c3-hal = { version = "0.13.0", optional = true }
24-
esp32s2-hal = { version = "0.13.0", optional = true }
25-
esp32s3-hal = { version = "0.13.0", optional = true }
26-
esp-backtrace = { version = "0.9.0", features = [
22+
esp32-hal = { version = "0.18.0", optional = true }
23+
esp32c3-hal = { version = "0.15.0", optional = true }
24+
esp32s2-hal = { version = "0.15.0", optional = true }
25+
esp32s3-hal = { version = "0.15.0", optional = true }
26+
esp-backtrace = { version = "0.11.0", features = [
2727
"panic-handler",
28-
"print-uart",
28+
"println",
2929
"exception-handler",
3030
] }
31-
esp-println = { version = "0.7.0", features = ["log"] }
31+
esp-println = { version = "0.9.0", features = ["log"] }
3232

33-
embassy-time = { version = "0.1.3", features = ["nightly"], optional = true }
34-
embassy-executor = { version = "0.3.0", package = "embassy-executor", features = [
33+
embassy-time = { version = "0.3.0", optional = true }
34+
embassy-executor = { version = "0.5.0", package = "embassy-executor", features = [
3535
"nightly",
3636
"executor-thread",
3737
"integrated-timers",
3838
], optional = true }
39-
embassy-net = { version = "0.2.1", features = [
40-
"nightly",
39+
embassy-net = { version = "0.4.0", features = [
4140
"tcp",
4241
"udp",
4342
"dhcpv4",
@@ -47,9 +46,9 @@ embassy-net = { version = "0.2.1", features = [
4746
esp-wifi = { git = "https://github.com/esp-rs/esp-wifi.git", features = [
4847
"phy-enable-usb",
4948
"embedded-svc",
50-
"wifi",
51-
], rev = "516608916e71671e99c234511df93801fde2d3a3" }
52-
smoltcp = { version = "0.10.0", default-features = false, features = [
49+
"wifi-default",
50+
], rev = "e6c58b67569c290f27e38846fcf628d893e562c1" }
51+
smoltcp = { version = "0.11.0", default-features = false, features = [
5352
"proto-igmp",
5453
"proto-ipv4",
5554
"socket-tcp",
@@ -60,12 +59,11 @@ smoltcp = { version = "0.10.0", default-features = false, features = [
6059
"socket-raw",
6160
"socket-dhcpv4",
6261
] }
63-
embedded-svc = { version = "0.26.2", default-features = false }
6462
log = "0.4.16"
6563
embedded-hal = "0.2"
6664
embedded-io = "0.6.1"
6765
embedded-io-async = { version = "0.6.0", optional = true }
68-
heapless = "0.7.16"
66+
heapless = "0.8.0"
6967
critical-section = "1.0.1"
7068
static_cell = { version = "=1.2", features = ["nightly"] }
7169

@@ -101,7 +99,6 @@ esp32c3 = [
10199
"esp32c3-hal/embassy-time-timg0",
102100
"esp-backtrace/esp32c3",
103101
"esp-println/esp32c3",
104-
"embassy-executor?/arch-riscv32",
105102
"esp-wifi/esp32c3",
106103
"esp-mbedtls/esp32c3",
107104
]
@@ -135,6 +132,7 @@ async = [
135132
"esp32-hal?/async",
136133
"esp32s2-hal?/async",
137134
"esp32s3-hal?/async",
135+
"esp32c3-hal?/embassy-executor-thread",
138136
"esp32-hal?/embassy-executor-thread",
139137
"esp32s2-hal?/embassy-executor-thread",
140138
"esp32s3-hal?/embassy-executor-thread",

esp-mbedtls/Cargo.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@
22
name = "esp-mbedtls"
33
version = "0.1.0"
44
edition = "2021"
5+
rust-version = "1.74"
56

67
[dependencies]
78
esp-mbedtls-sys = { path = "../esp-mbedtls-sys" }
89
log = "0.4.17"
910
embedded-io = { version = "0.6.1" }
1011
embedded-io-async = { version = "0.6.0", optional = true }
11-
esp32-hal = { version = "0.16.0", optional = true }
12-
esp32c3-hal = { version = "0.13.0", optional = true }
13-
esp32s2-hal = { version = "0.13.0", optional = true }
14-
esp32s3-hal = { version = "0.13.0", optional = true }
12+
esp32-hal = { version = "0.18.0", optional = true }
13+
esp32c3-hal = { version = "0.15.0", optional = true }
14+
esp32s2-hal = { version = "0.15.0", optional = true }
15+
esp32s3-hal = { version = "0.15.0", optional = true }
1516

1617
[features]
1718
async = ["dep:embedded-io-async"]

esp-mbedtls/src/compat.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
use core::ffi::VaListImpl;
22
use core::fmt::Write;
33

4-
#[no_mangle]
5-
extern "C" fn putchar() {
6-
todo!()
7-
}
8-
94
#[no_mangle]
105
pub unsafe extern "C" fn snprintf(dst: *mut u8, n: u32, format: *const u8, args: ...) -> i32 {
116
vsnprintf(dst, n, format, args)

esp-mbedtls/src/lib.rs

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#![no_std]
22
#![feature(c_variadic)]
3-
#![feature(async_fn_in_trait)]
4-
#![feature(impl_trait_projections)]
53
#![allow(incomplete_features)]
64

75
use embedded_io::ErrorType;
@@ -376,18 +374,18 @@ impl<'a> Certificates<'a> {
376374
}
377375
}
378376

379-
pub struct Session<'a, T> {
380-
stream: &'a mut T,
377+
pub struct Session<T> {
378+
stream: T,
381379
ssl_context: *mut mbedtls_ssl_context,
382380
ssl_config: *mut mbedtls_ssl_config,
383381
crt: *mut mbedtls_x509_crt,
384382
client_crt: *mut mbedtls_x509_crt,
385383
private_key: *mut mbedtls_pk_context,
386384
}
387385

388-
impl<'a, T> Session<'a, T> {
386+
impl<T> Session<T> {
389387
pub fn new(
390-
stream: &'a mut T,
388+
stream: T,
391389
servername: &str,
392390
mode: Mode,
393391
min_version: TlsVersion,
@@ -406,11 +404,11 @@ impl<'a, T> Session<'a, T> {
406404
}
407405
}
408406

409-
impl<'a, T> Session<'a, T>
407+
impl<T> Session<T>
410408
where
411409
T: Read + Write,
412410
{
413-
pub fn connect<'b>(self) -> Result<ConnectedSession<'a, T>, TlsError> {
411+
pub fn connect<'b>(self) -> Result<ConnectedSession<T>, TlsError> {
414412
unsafe {
415413
mbedtls_ssl_set_bio(
416414
self.ssl_context,
@@ -509,7 +507,7 @@ where
509507
}
510508
}
511509

512-
impl<'a, T> Drop for Session<'a, T> {
510+
impl<T> Drop for Session<T> {
513511
fn drop(&mut self) {
514512
log::debug!("session dropped - freeing memory");
515513
unsafe {
@@ -528,21 +526,21 @@ impl<'a, T> Drop for Session<'a, T> {
528526
}
529527
}
530528

531-
pub struct ConnectedSession<'a, T>
529+
pub struct ConnectedSession<T>
532530
where
533531
T: Read + Write,
534532
{
535-
session: Session<'a, T>,
533+
session: Session<T>,
536534
}
537535

538-
impl<'a, T> ErrorType for ConnectedSession<'a, T>
536+
impl<T> ErrorType for ConnectedSession<T>
539537
where
540538
T: Read + Write,
541539
{
542540
type Error = TlsError;
543541
}
544542

545-
impl<'a, T> Read for ConnectedSession<'a, T>
543+
impl<T> Read for ConnectedSession<T>
546544
where
547545
T: Read + Write,
548546
{
@@ -560,7 +558,7 @@ where
560558
}
561559
}
562560

563-
impl<'a, T> Write for ConnectedSession<'a, T>
561+
impl<T> Write for ConnectedSession<T>
564562
where
565563
T: Read + Write,
566564
{
@@ -578,8 +576,8 @@ where
578576
pub mod asynch {
579577
use super::*;
580578

581-
pub struct Session<'a, T, const BUFFER_SIZE: usize = 4096> {
582-
stream: &'a mut T,
579+
pub struct Session<T, const BUFFER_SIZE: usize = 4096> {
580+
stream: T,
583581
ssl_context: *mut mbedtls_ssl_context,
584582
ssl_config: *mut mbedtls_ssl_config,
585583
crt: *mut mbedtls_x509_crt,
@@ -590,9 +588,9 @@ pub mod asynch {
590588
rx_buffer: BufferedBytes<BUFFER_SIZE>,
591589
}
592590

593-
impl<'a, T, const BUFFER_SIZE: usize> Session<'a, T, BUFFER_SIZE> {
591+
impl<T, const BUFFER_SIZE: usize> Session<T, BUFFER_SIZE> {
594592
pub fn new(
595-
stream: &'a mut T,
593+
stream: T,
596594
servername: &str,
597595
mode: Mode,
598596
min_version: TlsVersion,
@@ -614,7 +612,7 @@ pub mod asynch {
614612
}
615613
}
616614

617-
impl<'a, T, const BUFFER_SIZE: usize> Drop for Session<'a, T, BUFFER_SIZE> {
615+
impl<T, const BUFFER_SIZE: usize> Drop for Session<T, BUFFER_SIZE> {
618616
fn drop(&mut self) {
619617
log::debug!("session dropped - freeing memory");
620618
unsafe {
@@ -633,13 +631,13 @@ pub mod asynch {
633631
}
634632
}
635633

636-
impl<'a, T, const BUFFER_SIZE: usize> Session<'a, T, BUFFER_SIZE>
634+
impl<T, const BUFFER_SIZE: usize> Session<T, BUFFER_SIZE>
637635
where
638636
T: embedded_io_async::Read + embedded_io_async::Write,
639637
{
640638
pub async fn connect<'b>(
641639
mut self,
642-
) -> Result<AsyncConnectedSession<'a, T, BUFFER_SIZE>, TlsError> {
640+
) -> Result<AsyncConnectedSession<T, BUFFER_SIZE>, TlsError> {
643641
unsafe {
644642
mbedtls_ssl_set_bio(
645643
self.ssl_context,
@@ -841,23 +839,22 @@ pub mod asynch {
841839
}
842840
}
843841

844-
pub struct AsyncConnectedSession<'a, T, const BUFFER_SIZE: usize>
842+
pub struct AsyncConnectedSession<T, const BUFFER_SIZE: usize>
845843
where
846844
T: embedded_io_async::Read + embedded_io_async::Write,
847845
{
848-
pub(crate) session: Session<'a, T, BUFFER_SIZE>,
846+
pub(crate) session: Session<T, BUFFER_SIZE>,
849847
}
850848

851-
impl<'a, T, const BUFFER_SIZE: usize> embedded_io_async::ErrorType
852-
for AsyncConnectedSession<'a, T, BUFFER_SIZE>
849+
impl<T, const BUFFER_SIZE: usize> embedded_io_async::ErrorType
850+
for AsyncConnectedSession<T, BUFFER_SIZE>
853851
where
854852
T: embedded_io_async::Read + embedded_io_async::Write,
855853
{
856854
type Error = TlsError;
857855
}
858856

859-
impl<'a, T, const BUFFER_SIZE: usize> embedded_io_async::Read
860-
for AsyncConnectedSession<'a, T, BUFFER_SIZE>
857+
impl<T, const BUFFER_SIZE: usize> embedded_io_async::Read for AsyncConnectedSession<T, BUFFER_SIZE>
861858
where
862859
T: embedded_io_async::Read + embedded_io_async::Write,
863860
{
@@ -879,8 +876,7 @@ pub mod asynch {
879876
}
880877
}
881878

882-
impl<'a, T, const BUFFER_SIZE: usize> embedded_io_async::Write
883-
for AsyncConnectedSession<'a, T, BUFFER_SIZE>
879+
impl<T, const BUFFER_SIZE: usize> embedded_io_async::Write for AsyncConnectedSession<T, BUFFER_SIZE>
884880
where
885881
T: embedded_io_async::Read + embedded_io_async::Write,
886882
{

examples/async_client.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@ use embassy_net::{Config, Ipv4Address, Stack, StackResources};
2222

2323
use embassy_executor::Spawner;
2424
use embassy_time::{Duration, Timer};
25-
use embedded_svc::wifi::{ClientConfiguration, Configuration, Wifi};
2625
use esp_backtrace as _;
2726
use esp_mbedtls::X509;
2827
use esp_mbedtls::{asynch::Session, set_debug, Certificates, Mode, TlsVersion};
2928
use esp_println::logger::init_logger;
3029
use esp_println::{print, println};
31-
use esp_wifi::wifi::{WifiController, WifiDevice, WifiEvent, WifiStaDevice, WifiState};
30+
use esp_wifi::wifi::{
31+
ClientConfiguration, Configuration, WifiController, WifiDevice, WifiEvent, WifiStaDevice,
32+
WifiState,
33+
};
3234
use esp_wifi::{initialize, EspWifiInitFor};
3335
use hal::clock::ClockControl;
3436
use hal::Rng;
@@ -64,7 +66,7 @@ async fn main(spawner: Spawner) -> ! {
6466
esp_wifi::wifi::new_with_mode(&init, wifi, WifiStaDevice).unwrap();
6567

6668
let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks);
67-
embassy::init(&clocks, timer_group0.timer0);
69+
embassy::init(&clocks, timer_group0);
6870

6971
let config = Config::dhcpv4(Default::default());
7072

@@ -179,8 +181,8 @@ async fn connection(mut controller: WifiController<'static>) {
179181
}
180182
if !matches!(controller.is_started(), Ok(true)) {
181183
let client_config = Configuration::Client(ClientConfiguration {
182-
ssid: SSID.into(),
183-
password: PASSWORD.into(),
184+
ssid: SSID.try_into().unwrap(),
185+
password: PASSWORD.try_into().unwrap(),
184186
..Default::default()
185187
});
186188
controller.set_configuration(&client_config).unwrap();

0 commit comments

Comments
 (0)