Skip to content

Commit

Permalink
fix(async_internal_read): Ensure that all pending data has been proce…
Browse files Browse the repository at this point in the history
…ssed before returning `Ok(0)` (#48)

* fix(async_internal_read): Ensure that all pending data has been processed before returning `Ok(0)`

* chore: update `static_cell` to 2.1 and use mk_static to fix CI
  • Loading branch information
AnthonyGrondin committed Sep 10, 2024
1 parent 7de5d0a commit 236724d
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 49 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ embedded-io = "0.6.1"
embedded-io-async = { version = "0.6.0", optional = true }
heapless = "0.8.0"
critical-section = "1.0.1"
static_cell = { version = "=1.2", features = ["nightly"] }
static_cell = { version = "2.1", features = ["nightly"] }

esp-mbedtls = { path = "./esp-mbedtls" }

Expand Down
5 changes: 4 additions & 1 deletion esp-mbedtls/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,10 @@ pub mod asynch {
}
}

if !self.rx_buffer.empty() || mbedtls_ssl_get_bytes_avail(self.ssl_context) > 0 {
if !self.rx_buffer.empty()
|| mbedtls_ssl_check_pending(self.ssl_context) == 1
|| mbedtls_ssl_get_bytes_avail(self.ssl_context) > 0
{
log::debug!("<<< read data from mbedtls");
let res = mbedtls_ssl_read(self.ssl_context, buf.as_mut_ptr(), buf.len());
log::debug!("<<< mbedtls returned {res}");
Expand Down
31 changes: 22 additions & 9 deletions examples/async_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,16 @@ use hal::{
clock::ClockControl, peripherals::Peripherals, rng::Rng, system::SystemControl,
timer::timg::TimerGroup,
};
use static_cell::make_static;

// Patch until https://github.com/embassy-rs/static-cell/issues/16 is fixed
macro_rules! mk_static {
($t:ty,$val:expr) => {{
static STATIC_CELL: static_cell::StaticCell<$t> = static_cell::StaticCell::new();
#[deny(unused_attributes)]
let x = STATIC_CELL.uninit().write(($val));
x
}};
}

const SSID: &str = env!("SSID");
const PASSWORD: &str = env!("PASSWORD");
Expand Down Expand Up @@ -71,12 +80,15 @@ async fn main(spawner: Spawner) -> ! {
let seed = 1234; // very random, very secure seed

// Init network stack
let stack = &*make_static!(Stack::new(
wifi_interface,
config,
make_static!(StackResources::<3>::new()),
seed
));
let stack = &*mk_static!(
Stack<WifiDevice<'_, WifiStaDevice>>,
Stack::new(
wifi_interface,
config,
mk_static!(StackResources<3>, StackResources::<3>::new()),
seed
)
);

spawner.spawn(connection(controller)).ok();
spawner.spawn(net_task(&stack)).ok();
Expand Down Expand Up @@ -126,8 +138,8 @@ async fn main(spawner: Spawner) -> ! {
.ok(),
..Default::default()
},
make_static!([0; 4096]),
make_static!([0; 4096]),
mk_static!([u8; 4096], [0; 4096]),
mk_static!([u8; 4096], [0; 4096]),
)
.unwrap()
.with_hardware_rsa(peripherals.RSA);
Expand Down Expand Up @@ -163,6 +175,7 @@ async fn main(spawner: Spawner) -> ! {
print!("{}", core::str::from_utf8(&buf[..n]).unwrap());
}
println!();
println!("Done");

loop {}
}
Expand Down
30 changes: 21 additions & 9 deletions examples/async_client_mTLS.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,16 @@ use hal::{
clock::ClockControl, peripherals::Peripherals, rng::Rng, system::SystemControl,
timer::timg::TimerGroup,
};
use static_cell::make_static;

// Patch until https://github.com/embassy-rs/static-cell/issues/16 is fixed
macro_rules! mk_static {
($t:ty,$val:expr) => {{
static STATIC_CELL: static_cell::StaticCell<$t> = static_cell::StaticCell::new();
#[deny(unused_attributes)]
let x = STATIC_CELL.uninit().write(($val));
x
}};
}

const SSID: &str = env!("SSID");
const PASSWORD: &str = env!("PASSWORD");
Expand Down Expand Up @@ -71,12 +80,15 @@ async fn main(spawner: Spawner) -> ! {
let seed = 1234; // very random, very secure seed

// Init network stack
let stack = &*make_static!(Stack::new(
wifi_interface,
config,
make_static!(StackResources::<3>::new()),
seed
));
let stack = &*mk_static!(
Stack<WifiDevice<'_, WifiStaDevice>>,
Stack::new(
wifi_interface,
config,
mk_static!(StackResources<3>, StackResources::<3>::new()),
seed
)
);

spawner.spawn(connection(controller)).ok();
spawner.spawn(net_task(&stack)).ok();
Expand Down Expand Up @@ -132,8 +144,8 @@ async fn main(spawner: Spawner) -> ! {
Mode::Client,
TlsVersion::Tls1_3,
certificates,
make_static!([0; 4096]),
make_static!([0; 4096]),
mk_static!([u8; 4096], [0; 4096]),
mk_static!([u8; 4096], [0; 4096]),
)
.unwrap()
.with_hardware_rsa(peripherals.RSA);
Expand Down
30 changes: 21 additions & 9 deletions examples/async_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,16 @@ use hal::{
clock::ClockControl, peripherals::Peripherals, rng::Rng, system::SystemControl,
timer::timg::TimerGroup,
};
use static_cell::make_static;

// Patch until https://github.com/embassy-rs/static-cell/issues/16 is fixed
macro_rules! mk_static {
($t:ty,$val:expr) => {{
static STATIC_CELL: static_cell::StaticCell<$t> = static_cell::StaticCell::new();
#[deny(unused_attributes)]
let x = STATIC_CELL.uninit().write(($val));
x
}};
}

const SSID: &str = env!("SSID");
const PASSWORD: &str = env!("PASSWORD");
Expand Down Expand Up @@ -74,20 +83,23 @@ async fn main(spawner: Spawner) -> ! {
let seed = 1234; // very random, very secure seed

// Init network stack
let stack = &*make_static!(Stack::new(
wifi_interface,
config,
make_static!(StackResources::<3>::new()),
seed
));
let stack = &*mk_static!(
Stack<WifiDevice<'_, WifiStaDevice>>,
Stack::new(
wifi_interface,
config,
mk_static!(StackResources<3>, StackResources::<3>::new()),
seed
)
);

spawner.spawn(connection(controller)).ok();
spawner.spawn(net_task(&stack)).ok();

let mut rx_buffer = [0; 4096];
let mut tx_buffer = [0; 4096];
let tls_rx_buffer = make_static!([0; 4096]);
let tls_tx_buffer = make_static!([0; 2048]);
let tls_rx_buffer = mk_static!([u8; 4096], [0; 4096]);
let tls_tx_buffer = mk_static!([u8; 2048], [0; 2048]);

loop {
if stack.is_link_up() {
Expand Down
30 changes: 21 additions & 9 deletions examples/async_server_mTLS.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,16 @@ use hal::{
clock::ClockControl, peripherals::Peripherals, rng::Rng, system::SystemControl,
timer::timg::TimerGroup,
};
use static_cell::make_static;

// Patch until https://github.com/embassy-rs/static-cell/issues/16 is fixed
macro_rules! mk_static {
($t:ty,$val:expr) => {{
static STATIC_CELL: static_cell::StaticCell<$t> = static_cell::StaticCell::new();
#[deny(unused_attributes)]
let x = STATIC_CELL.uninit().write(($val));
x
}};
}

const SSID: &str = env!("SSID");
const PASSWORD: &str = env!("PASSWORD");
Expand Down Expand Up @@ -91,20 +100,23 @@ async fn main(spawner: Spawner) -> ! {
let seed = 1234; // very random, very secure seed

// Init network stack
let stack = &*make_static!(Stack::new(
wifi_interface,
config,
make_static!(StackResources::<3>::new()),
seed
));
let stack = &*mk_static!(
Stack<WifiDevice<'_, WifiStaDevice>>,
Stack::new(
wifi_interface,
config,
mk_static!(StackResources<3>, StackResources::<3>::new()),
seed
)
);

spawner.spawn(connection(controller)).ok();
spawner.spawn(net_task(&stack)).ok();

let mut rx_buffer = [0; 4096];
let mut tx_buffer = [0; 4096];
let tls_rx_buffer = make_static!([0; 4096]);
let tls_tx_buffer = make_static!([0; 2048]);
let tls_rx_buffer = mk_static!([u8; 4096], [0; 4096]);
let tls_tx_buffer = mk_static!([u8; 2048], [0; 2048]);

loop {
if stack.is_link_up() {
Expand Down
43 changes: 32 additions & 11 deletions examples/edge_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,16 @@ use hal::{
clock::ClockControl, peripherals::Peripherals, rng::Rng, system::SystemControl,
timer::timg::TimerGroup,
};
use static_cell::make_static;

// Patch until https://github.com/embassy-rs/static-cell/issues/16 is fixed
macro_rules! mk_static {
($t:ty,$val:expr) => {{
static STATIC_CELL: static_cell::StaticCell<$t> = static_cell::StaticCell::new();
#[deny(unused_attributes)]
let x = STATIC_CELL.uninit().write(($val));
x
}};
}

const SSID: &str = env!("SSID");
const PASSWORD: &str = env!("PASSWORD");
Expand Down Expand Up @@ -94,12 +103,18 @@ async fn main(spawner: Spawner) -> ! {
let seed = 1234; // very random, very secure seed

// Init network stack
let stack = &*make_static!(Stack::new(
wifi_interface,
config,
make_static!(StackResources::<SOCKET_COUNT>::new()),
seed
));
let stack = &*mk_static!(
Stack<WifiDevice<'_, WifiStaDevice>>,
Stack::new(
wifi_interface,
config,
mk_static!(
StackResources<SOCKET_COUNT>,
StackResources::<SOCKET_COUNT>::new()
),
seed
)
);

spawner.spawn(connection(controller)).ok();
spawner.spawn(net_task(&stack)).ok();
Expand All @@ -126,10 +141,16 @@ async fn main(spawner: Spawner) -> ! {

set_debug(0);

let server = make_static!(HttpsServer::new());
let buffers = make_static!(TcpBuffers::<SERVER_SOCKETS, TX_SIZE, RX_SIZE>::new());
let tls_buffers = make_static!(esp_mbedtls::asynch::TlsBuffers::<RX_SIZE, TX_SIZE>::new());
let tcp = make_static!(Tcp::new(stack, buffers));
let server = mk_static!(HttpsServer, HttpsServer::new());
let buffers = mk_static!(TcpBuffers<SERVER_SOCKETS, TX_SIZE, RX_SIZE>, TcpBuffers::<SERVER_SOCKETS, TX_SIZE, RX_SIZE>::new());
let tls_buffers = mk_static!(
esp_mbedtls::asynch::TlsBuffers::<RX_SIZE, TX_SIZE>,
esp_mbedtls::asynch::TlsBuffers::<RX_SIZE, TX_SIZE>::new()
);
let tcp = mk_static!(
Tcp<'_, WifiDevice<'_, WifiStaDevice>, SERVER_SOCKETS, TX_SIZE, RX_SIZE>,
Tcp::new(stack, buffers)
);

let certificates = Certificates {
// Use self-signed certificates
Expand Down

0 comments on commit 236724d

Please sign in to comment.