Skip to content
This repository was archived by the owner on Aug 1, 2024. It is now read-only.

Commit 09887d8

Browse files
committed
Working end to end
1 parent f712acd commit 09887d8

File tree

2 files changed

+89
-81
lines changed

2 files changed

+89
-81
lines changed

sdkconfig.defaults

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Necessary for the LED screen demos
22
CONFIG_ESP_MAIN_TASK_STACK_SIZE=20000
33

4+
# Necessary for async-io
5+
CONFIG_PTHREAD_TASK_STACK_SIZE_DEFAULT=4096
6+
47
CONFIG_ESP_SYSTEM_EVENT_TASK_STACK_SIZE=4096
58

69
# NAPT demo (router)

src/main.rs

Lines changed: 86 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use std::{env, sync::Arc, thread, time::*};
3636

3737
use anyhow::{bail, Result};
3838

39-
use async_io::Async;
39+
use async_io::{Async, Timer};
4040
use esp_idf_svc::http::server::{EspHttpConnection, Request};
4141
use esp_idf_svc::io::Write;
4242
use log::*;
@@ -312,6 +312,18 @@ fn main() -> Result<()> {
312312

313313
let _timer = test_timer(eventloop, mqtt_client)?;
314314

315+
#[allow(clippy::needless_update)]
316+
{
317+
esp_idf_svc::sys::esp!(unsafe {
318+
esp_idf_svc::sys::esp_vfs_eventfd_register(
319+
&esp_idf_svc::sys::esp_vfs_eventfd_config_t {
320+
max_fds: 5,
321+
..Default::default()
322+
},
323+
)
324+
})?;
325+
}
326+
315327
#[cfg(not(esp_idf_version = "4.3"))]
316328
test_tcp_bind_async()?;
317329

@@ -730,18 +742,6 @@ fn test_tcp_bind_async() -> anyhow::Result<()> {
730742

731743
info!("About to bind a simple echo service to port 8081 using async (with async-io)!");
732744

733-
#[allow(clippy::needless_update)]
734-
{
735-
esp_idf_svc::sys::esp!(unsafe {
736-
esp_idf_svc::sys::esp_vfs_eventfd_register(
737-
&esp_idf_svc::sys::esp_vfs_eventfd_config_t {
738-
max_fds: 5,
739-
..Default::default()
740-
},
741-
)
742-
})?;
743-
}
744-
745745
thread::Builder::new().stack_size(20000).spawn(move || {
746746
let executor = LocalExecutor::new();
747747

@@ -755,6 +755,78 @@ fn test_tcp_bind_async() -> anyhow::Result<()> {
755755

756756
fn test_https_client() -> anyhow::Result<()> {
757757
async fn test() -> anyhow::Result<()> {
758+
// Implement `esp_idf_svc::tls::PollableSocket` for async-io sockets
759+
////////////////////////////////////////////////////////////////////
760+
761+
pub struct EspTlsSocket(Option<async_io::Async<TcpStream>>);
762+
763+
impl EspTlsSocket {
764+
pub const fn new(socket: async_io::Async<TcpStream>) -> Self {
765+
Self(Some(socket))
766+
}
767+
768+
pub fn handle(&self) -> i32 {
769+
self.0.as_ref().unwrap().as_raw_fd()
770+
}
771+
772+
pub fn poll_readable(
773+
&self,
774+
ctx: &mut core::task::Context,
775+
) -> core::task::Poll<Result<(), esp_idf_svc::sys::EspError>> {
776+
self.0
777+
.as_ref()
778+
.unwrap()
779+
.poll_readable(ctx)
780+
.map_err(|_| EspError::from_infallible::<{ esp_idf_svc::sys::ESP_FAIL }>())
781+
}
782+
783+
pub fn poll_writeable(
784+
&self,
785+
ctx: &mut core::task::Context,
786+
) -> core::task::Poll<Result<(), esp_idf_svc::sys::EspError>> {
787+
self.0
788+
.as_ref()
789+
.unwrap()
790+
.poll_writable(ctx)
791+
.map_err(|_| EspError::from_infallible::<{ esp_idf_svc::sys::ESP_FAIL }>())
792+
}
793+
794+
fn release(&mut self) -> Result<(), esp_idf_svc::sys::EspError> {
795+
let socket = self.0.take().unwrap();
796+
socket.into_inner().unwrap().into_raw_fd();
797+
798+
Ok(())
799+
}
800+
}
801+
802+
impl esp_idf_svc::tls::Socket for EspTlsSocket {
803+
fn handle(&self) -> i32 {
804+
EspTlsSocket::handle(self)
805+
}
806+
807+
fn release(&mut self) -> Result<(), esp_idf_svc::sys::EspError> {
808+
EspTlsSocket::release(self)
809+
}
810+
}
811+
812+
impl esp_idf_svc::tls::PollableSocket for EspTlsSocket {
813+
fn poll_readable(
814+
&self,
815+
ctx: &mut core::task::Context,
816+
) -> core::task::Poll<Result<(), esp_idf_svc::sys::EspError>> {
817+
EspTlsSocket::poll_readable(self, ctx)
818+
}
819+
820+
fn poll_writable(
821+
&self,
822+
ctx: &mut core::task::Context,
823+
) -> core::task::Poll<Result<(), esp_idf_svc::sys::EspError>> {
824+
EspTlsSocket::poll_writeable(self, ctx)
825+
}
826+
}
827+
828+
////////////////////////////////////////////////////////////////////
829+
758830
let addr = "google.com:443".to_socket_addrs()?.next().unwrap();
759831
let socket = Async::<TcpStream>::connect(addr).await?;
760832

@@ -780,7 +852,7 @@ fn test_https_client() -> anyhow::Result<()> {
780852
}
781853

782854
let th = thread::Builder::new()
783-
.stack_size(15000)
855+
.stack_size(20000)
784856
.spawn(move || async_io::block_on(test()))?;
785857

786858
th.join().unwrap()
@@ -1473,70 +1545,3 @@ fn waveshare_epd_hello_world(
14731545

14741546
Ok(())
14751547
}
1476-
1477-
pub struct EspTlsSocket(Option<async_io::Async<TcpStream>>);
1478-
1479-
impl EspTlsSocket {
1480-
pub const fn new(socket: async_io::Async<TcpStream>) -> Self {
1481-
Self(Some(socket))
1482-
}
1483-
1484-
pub fn handle(&self) -> i32 {
1485-
self.0.as_ref().unwrap().as_raw_fd()
1486-
}
1487-
1488-
pub fn poll_readable(
1489-
&self,
1490-
ctx: &mut core::task::Context,
1491-
) -> core::task::Poll<Result<(), esp_idf_svc::sys::EspError>> {
1492-
self.0
1493-
.as_ref()
1494-
.unwrap()
1495-
.poll_readable(ctx)
1496-
.map_err(|_| EspError::from_infallible::<{ esp_idf_svc::sys::ESP_FAIL }>())
1497-
}
1498-
1499-
pub fn poll_writeable(
1500-
&self,
1501-
ctx: &mut core::task::Context,
1502-
) -> core::task::Poll<Result<(), esp_idf_svc::sys::EspError>> {
1503-
self.0
1504-
.as_ref()
1505-
.unwrap()
1506-
.poll_writable(ctx)
1507-
.map_err(|_| EspError::from_infallible::<{ esp_idf_svc::sys::ESP_FAIL }>())
1508-
}
1509-
1510-
fn release(&mut self) -> Result<(), esp_idf_svc::sys::EspError> {
1511-
let socket = self.0.take().unwrap();
1512-
socket.into_inner().unwrap().into_raw_fd();
1513-
1514-
Ok(())
1515-
}
1516-
}
1517-
1518-
impl esp_idf_svc::tls::Socket for EspTlsSocket {
1519-
fn handle(&self) -> i32 {
1520-
EspTlsSocket::handle(self)
1521-
}
1522-
1523-
fn release(&mut self) -> Result<(), esp_idf_svc::sys::EspError> {
1524-
EspTlsSocket::release(self)
1525-
}
1526-
}
1527-
1528-
impl esp_idf_svc::tls::PollableSocket for EspTlsSocket {
1529-
fn poll_readable(
1530-
&self,
1531-
ctx: &mut core::task::Context,
1532-
) -> core::task::Poll<Result<(), esp_idf_svc::sys::EspError>> {
1533-
EspTlsSocket::poll_readable(self, ctx)
1534-
}
1535-
1536-
fn poll_writable(
1537-
&self,
1538-
ctx: &mut core::task::Context,
1539-
) -> core::task::Poll<Result<(), esp_idf_svc::sys::EspError>> {
1540-
EspTlsSocket::poll_writeable(self, ctx)
1541-
}
1542-
}

0 commit comments

Comments
 (0)