Skip to content

Commit

Permalink
revert: Changes Session back to own the socket (#25)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
AnthonyGrondin authored Feb 14, 2024
1 parent 749a0cc commit 360bf59
Show file tree
Hide file tree
Showing 13 changed files with 85 additions and 137 deletions.
34 changes: 0 additions & 34 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ rustflags = [
"-C", "link-arg=-Tlinkall.x",
"-C", "link-arg=-nostartfiles",
"-C", "link-arg=-Trom_functions.x",
"-C", "target-feature=-loop",
]

[target.riscv32imc-unknown-none-elf]
Expand All @@ -18,22 +17,6 @@ rustflags = [
# Required to obtain backtraces (e.g. when using the "esp-backtrace" crate.)
# NOTE: May negatively impact performance of produced code
"-C", "force-frame-pointers",

# comment the cfgs below if you do _not_ wish to emulate atomics.
# enable the atomic codegen option for RISCV
"-C", "target-feature=+a",
# tell the core library have atomics even though it's not specified in the target definition
"--cfg", "target_has_atomic_load_store",
"--cfg", 'target_has_atomic_load_store="8"',
"--cfg", 'target_has_atomic_load_store="16"',
"--cfg", 'target_has_atomic_load_store="32"',
"--cfg", 'target_has_atomic_load_store="ptr"',
# enable cas
"--cfg", "target_has_atomic",
"--cfg", 'target_has_atomic="8"',
"--cfg", 'target_has_atomic="16"',
"--cfg", 'target_has_atomic="32"',
"--cfg", 'target_has_atomic="ptr"',
]

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

"-C", "link-arg=-Tlinkall.x",
"-C", "link-arg=-Trom_functions.x",

# Enable the atomic codegen option for Xtensa
"-C", "target-feature=+s32c1i",

# tell the core library have atomics even though it's not specified in the target definition
"--cfg", "target_has_atomic_load_store",
"--cfg", 'target_has_atomic_load_store="8"',
"--cfg", 'target_has_atomic_load_store="16"',
"--cfg", 'target_has_atomic_load_store="32"',
"--cfg", 'target_has_atomic_load_store="ptr"',
# enable cas
"--cfg", "target_has_atomic",
"--cfg", 'target_has_atomic="8"',
"--cfg", 'target_has_atomic="16"',
"--cfg", 'target_has_atomic="32"',
"--cfg", 'target_has_atomic="ptr"',
]

[target.xtensa-esp32s3-none-elf]
Expand All @@ -69,7 +36,6 @@ rustflags = [
"-C", "link-arg=-Tlinkall.x",
"-C", "link-arg=-nostartfiles",
"-C", "link-arg=-Trom_functions.x",
"-C", "target-feature=-loop",
]

[unstable]
Expand Down
32 changes: 15 additions & 17 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,24 @@ lto = false
opt-level = 3

[dependencies]
esp32-hal = { version = "0.16.0", optional = true }
esp32c3-hal = { version = "0.13.0", optional = true }
esp32s2-hal = { version = "0.13.0", optional = true }
esp32s3-hal = { version = "0.13.0", optional = true }
esp-backtrace = { version = "0.9.0", features = [
esp32-hal = { version = "0.18.0", optional = true }
esp32c3-hal = { version = "0.15.0", optional = true }
esp32s2-hal = { version = "0.15.0", optional = true }
esp32s3-hal = { version = "0.15.0", optional = true }
esp-backtrace = { version = "0.11.0", features = [
"panic-handler",
"print-uart",
"println",
"exception-handler",
] }
esp-println = { version = "0.7.0", features = ["log"] }
esp-println = { version = "0.9.0", features = ["log"] }

embassy-time = { version = "0.1.3", features = ["nightly"], optional = true }
embassy-executor = { version = "0.3.0", package = "embassy-executor", features = [
embassy-time = { version = "0.3.0", optional = true }
embassy-executor = { version = "0.5.0", package = "embassy-executor", features = [
"nightly",
"executor-thread",
"integrated-timers",
], optional = true }
embassy-net = { version = "0.2.1", features = [
"nightly",
embassy-net = { version = "0.4.0", features = [
"tcp",
"udp",
"dhcpv4",
Expand All @@ -47,9 +46,9 @@ embassy-net = { version = "0.2.1", features = [
esp-wifi = { git = "https://github.com/esp-rs/esp-wifi.git", features = [
"phy-enable-usb",
"embedded-svc",
"wifi",
], rev = "516608916e71671e99c234511df93801fde2d3a3" }
smoltcp = { version = "0.10.0", default-features = false, features = [
"wifi-default",
], rev = "e6c58b67569c290f27e38846fcf628d893e562c1" }
smoltcp = { version = "0.11.0", default-features = false, features = [
"proto-igmp",
"proto-ipv4",
"socket-tcp",
Expand All @@ -60,12 +59,11 @@ smoltcp = { version = "0.10.0", default-features = false, features = [
"socket-raw",
"socket-dhcpv4",
] }
embedded-svc = { version = "0.26.2", default-features = false }
log = "0.4.16"
embedded-hal = "0.2"
embedded-io = "0.6.1"
embedded-io-async = { version = "0.6.0", optional = true }
heapless = "0.7.16"
heapless = "0.8.0"
critical-section = "1.0.1"
static_cell = { version = "=1.2", features = ["nightly"] }

Expand Down Expand Up @@ -101,7 +99,6 @@ esp32c3 = [
"esp32c3-hal/embassy-time-timg0",
"esp-backtrace/esp32c3",
"esp-println/esp32c3",
"embassy-executor?/arch-riscv32",
"esp-wifi/esp32c3",
"esp-mbedtls/esp32c3",
]
Expand Down Expand Up @@ -135,6 +132,7 @@ async = [
"esp32-hal?/async",
"esp32s2-hal?/async",
"esp32s3-hal?/async",
"esp32c3-hal?/embassy-executor-thread",
"esp32-hal?/embassy-executor-thread",
"esp32s2-hal?/embassy-executor-thread",
"esp32s3-hal?/embassy-executor-thread",
Expand Down
9 changes: 5 additions & 4 deletions esp-mbedtls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
name = "esp-mbedtls"
version = "0.1.0"
edition = "2021"
rust-version = "1.74"

[dependencies]
esp-mbedtls-sys = { path = "../esp-mbedtls-sys" }
log = "0.4.17"
embedded-io = { version = "0.6.1" }
embedded-io-async = { version = "0.6.0", optional = true }
esp32-hal = { version = "0.16.0", optional = true }
esp32c3-hal = { version = "0.13.0", optional = true }
esp32s2-hal = { version = "0.13.0", optional = true }
esp32s3-hal = { version = "0.13.0", optional = true }
esp32-hal = { version = "0.18.0", optional = true }
esp32c3-hal = { version = "0.15.0", optional = true }
esp32s2-hal = { version = "0.15.0", optional = true }
esp32s3-hal = { version = "0.15.0", optional = true }

[features]
async = ["dep:embedded-io-async"]
Expand Down
5 changes: 0 additions & 5 deletions esp-mbedtls/src/compat.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
use core::ffi::VaListImpl;
use core::fmt::Write;

#[no_mangle]
extern "C" fn putchar() {
todo!()
}

#[no_mangle]
pub unsafe extern "C" fn snprintf(dst: *mut u8, n: u32, format: *const u8, args: ...) -> i32 {
vsnprintf(dst, n, format, args)
Expand Down
54 changes: 25 additions & 29 deletions esp-mbedtls/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#![no_std]
#![feature(c_variadic)]
#![feature(async_fn_in_trait)]
#![feature(impl_trait_projections)]
#![allow(incomplete_features)]

use embedded_io::ErrorType;
Expand Down Expand Up @@ -376,18 +374,18 @@ impl<'a> Certificates<'a> {
}
}

pub struct Session<'a, T> {
stream: &'a mut T,
pub struct Session<T> {
stream: T,
ssl_context: *mut mbedtls_ssl_context,
ssl_config: *mut mbedtls_ssl_config,
crt: *mut mbedtls_x509_crt,
client_crt: *mut mbedtls_x509_crt,
private_key: *mut mbedtls_pk_context,
}

impl<'a, T> Session<'a, T> {
impl<T> Session<T> {
pub fn new(
stream: &'a mut T,
stream: T,
servername: &str,
mode: Mode,
min_version: TlsVersion,
Expand All @@ -406,11 +404,11 @@ impl<'a, T> Session<'a, T> {
}
}

impl<'a, T> Session<'a, T>
impl<T> Session<T>
where
T: Read + Write,
{
pub fn connect<'b>(self) -> Result<ConnectedSession<'a, T>, TlsError> {
pub fn connect<'b>(self) -> Result<ConnectedSession<T>, TlsError> {
unsafe {
mbedtls_ssl_set_bio(
self.ssl_context,
Expand Down Expand Up @@ -509,7 +507,7 @@ where
}
}

impl<'a, T> Drop for Session<'a, T> {
impl<T> Drop for Session<T> {
fn drop(&mut self) {
log::debug!("session dropped - freeing memory");
unsafe {
Expand All @@ -528,21 +526,21 @@ impl<'a, T> Drop for Session<'a, T> {
}
}

pub struct ConnectedSession<'a, T>
pub struct ConnectedSession<T>
where
T: Read + Write,
{
session: Session<'a, T>,
session: Session<T>,
}

impl<'a, T> ErrorType for ConnectedSession<'a, T>
impl<T> ErrorType for ConnectedSession<T>
where
T: Read + Write,
{
type Error = TlsError;
}

impl<'a, T> Read for ConnectedSession<'a, T>
impl<T> Read for ConnectedSession<T>
where
T: Read + Write,
{
Expand All @@ -560,7 +558,7 @@ where
}
}

impl<'a, T> Write for ConnectedSession<'a, T>
impl<T> Write for ConnectedSession<T>
where
T: Read + Write,
{
Expand All @@ -578,8 +576,8 @@ where
pub mod asynch {
use super::*;

pub struct Session<'a, T, const BUFFER_SIZE: usize = 4096> {
stream: &'a mut T,
pub struct Session<T, const BUFFER_SIZE: usize = 4096> {
stream: T,
ssl_context: *mut mbedtls_ssl_context,
ssl_config: *mut mbedtls_ssl_config,
crt: *mut mbedtls_x509_crt,
Expand All @@ -590,9 +588,9 @@ pub mod asynch {
rx_buffer: BufferedBytes<BUFFER_SIZE>,
}

impl<'a, T, const BUFFER_SIZE: usize> Session<'a, T, BUFFER_SIZE> {
impl<T, const BUFFER_SIZE: usize> Session<T, BUFFER_SIZE> {
pub fn new(
stream: &'a mut T,
stream: T,
servername: &str,
mode: Mode,
min_version: TlsVersion,
Expand All @@ -614,7 +612,7 @@ pub mod asynch {
}
}

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

impl<'a, T, const BUFFER_SIZE: usize> Session<'a, T, BUFFER_SIZE>
impl<T, const BUFFER_SIZE: usize> Session<T, BUFFER_SIZE>
where
T: embedded_io_async::Read + embedded_io_async::Write,
{
pub async fn connect<'b>(
mut self,
) -> Result<AsyncConnectedSession<'a, T, BUFFER_SIZE>, TlsError> {
) -> Result<AsyncConnectedSession<T, BUFFER_SIZE>, TlsError> {
unsafe {
mbedtls_ssl_set_bio(
self.ssl_context,
Expand Down Expand Up @@ -841,23 +839,22 @@ pub mod asynch {
}
}

pub struct AsyncConnectedSession<'a, T, const BUFFER_SIZE: usize>
pub struct AsyncConnectedSession<T, const BUFFER_SIZE: usize>
where
T: embedded_io_async::Read + embedded_io_async::Write,
{
pub(crate) session: Session<'a, T, BUFFER_SIZE>,
pub(crate) session: Session<T, BUFFER_SIZE>,
}

impl<'a, T, const BUFFER_SIZE: usize> embedded_io_async::ErrorType
for AsyncConnectedSession<'a, T, BUFFER_SIZE>
impl<T, const BUFFER_SIZE: usize> embedded_io_async::ErrorType
for AsyncConnectedSession<T, BUFFER_SIZE>
where
T: embedded_io_async::Read + embedded_io_async::Write,
{
type Error = TlsError;
}

impl<'a, T, const BUFFER_SIZE: usize> embedded_io_async::Read
for AsyncConnectedSession<'a, T, BUFFER_SIZE>
impl<T, const BUFFER_SIZE: usize> embedded_io_async::Read for AsyncConnectedSession<T, BUFFER_SIZE>
where
T: embedded_io_async::Read + embedded_io_async::Write,
{
Expand All @@ -879,8 +876,7 @@ pub mod asynch {
}
}

impl<'a, T, const BUFFER_SIZE: usize> embedded_io_async::Write
for AsyncConnectedSession<'a, T, BUFFER_SIZE>
impl<T, const BUFFER_SIZE: usize> embedded_io_async::Write for AsyncConnectedSession<T, BUFFER_SIZE>
where
T: embedded_io_async::Read + embedded_io_async::Write,
{
Expand Down
12 changes: 7 additions & 5 deletions examples/async_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ use embassy_net::{Config, Ipv4Address, Stack, StackResources};

use embassy_executor::Spawner;
use embassy_time::{Duration, Timer};
use embedded_svc::wifi::{ClientConfiguration, Configuration, Wifi};
use esp_backtrace as _;
use esp_mbedtls::X509;
use esp_mbedtls::{asynch::Session, set_debug, Certificates, Mode, TlsVersion};
use esp_println::logger::init_logger;
use esp_println::{print, println};
use esp_wifi::wifi::{WifiController, WifiDevice, WifiEvent, WifiStaDevice, WifiState};
use esp_wifi::wifi::{
ClientConfiguration, Configuration, WifiController, WifiDevice, WifiEvent, WifiStaDevice,
WifiState,
};
use esp_wifi::{initialize, EspWifiInitFor};
use hal::clock::ClockControl;
use hal::Rng;
Expand Down Expand Up @@ -64,7 +66,7 @@ async fn main(spawner: Spawner) -> ! {
esp_wifi::wifi::new_with_mode(&init, wifi, WifiStaDevice).unwrap();

let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks);
embassy::init(&clocks, timer_group0.timer0);
embassy::init(&clocks, timer_group0);

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

Expand Down Expand Up @@ -179,8 +181,8 @@ async fn connection(mut controller: WifiController<'static>) {
}
if !matches!(controller.is_started(), Ok(true)) {
let client_config = Configuration::Client(ClientConfiguration {
ssid: SSID.into(),
password: PASSWORD.into(),
ssid: SSID.try_into().unwrap(),
password: PASSWORD.try_into().unwrap(),
..Default::default()
});
controller.set_configuration(&client_config).unwrap();
Expand Down
Loading

0 comments on commit 360bf59

Please sign in to comment.