From 09887d81051a5111b15e0a0fcef645c471b01a26 Mon Sep 17 00:00:00 2001 From: ivmarkov Date: Thu, 19 Oct 2023 17:22:48 +0000 Subject: [PATCH] Working end to end --- sdkconfig.defaults | 3 + src/main.rs | 167 +++++++++++++++++++++++---------------------- 2 files changed, 89 insertions(+), 81 deletions(-) diff --git a/sdkconfig.defaults b/sdkconfig.defaults index 48099ad8..7cf8073d 100644 --- a/sdkconfig.defaults +++ b/sdkconfig.defaults @@ -1,6 +1,9 @@ # Necessary for the LED screen demos CONFIG_ESP_MAIN_TASK_STACK_SIZE=20000 +# Necessary for async-io +CONFIG_PTHREAD_TASK_STACK_SIZE_DEFAULT=4096 + CONFIG_ESP_SYSTEM_EVENT_TASK_STACK_SIZE=4096 # NAPT demo (router) diff --git a/src/main.rs b/src/main.rs index 7c40b79e..da6e5813 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,7 +36,7 @@ use std::{env, sync::Arc, thread, time::*}; use anyhow::{bail, Result}; -use async_io::Async; +use async_io::{Async, Timer}; use esp_idf_svc::http::server::{EspHttpConnection, Request}; use esp_idf_svc::io::Write; use log::*; @@ -312,6 +312,18 @@ fn main() -> Result<()> { let _timer = test_timer(eventloop, mqtt_client)?; + #[allow(clippy::needless_update)] + { + esp_idf_svc::sys::esp!(unsafe { + esp_idf_svc::sys::esp_vfs_eventfd_register( + &esp_idf_svc::sys::esp_vfs_eventfd_config_t { + max_fds: 5, + ..Default::default() + }, + ) + })?; + } + #[cfg(not(esp_idf_version = "4.3"))] test_tcp_bind_async()?; @@ -730,18 +742,6 @@ fn test_tcp_bind_async() -> anyhow::Result<()> { info!("About to bind a simple echo service to port 8081 using async (with async-io)!"); - #[allow(clippy::needless_update)] - { - esp_idf_svc::sys::esp!(unsafe { - esp_idf_svc::sys::esp_vfs_eventfd_register( - &esp_idf_svc::sys::esp_vfs_eventfd_config_t { - max_fds: 5, - ..Default::default() - }, - ) - })?; - } - thread::Builder::new().stack_size(20000).spawn(move || { let executor = LocalExecutor::new(); @@ -755,6 +755,78 @@ fn test_tcp_bind_async() -> anyhow::Result<()> { fn test_https_client() -> anyhow::Result<()> { async fn test() -> anyhow::Result<()> { + // Implement `esp_idf_svc::tls::PollableSocket` for async-io sockets + //////////////////////////////////////////////////////////////////// + + pub struct EspTlsSocket(Option>); + + impl EspTlsSocket { + pub const fn new(socket: async_io::Async) -> Self { + Self(Some(socket)) + } + + pub fn handle(&self) -> i32 { + self.0.as_ref().unwrap().as_raw_fd() + } + + pub fn poll_readable( + &self, + ctx: &mut core::task::Context, + ) -> core::task::Poll> { + self.0 + .as_ref() + .unwrap() + .poll_readable(ctx) + .map_err(|_| EspError::from_infallible::<{ esp_idf_svc::sys::ESP_FAIL }>()) + } + + pub fn poll_writeable( + &self, + ctx: &mut core::task::Context, + ) -> core::task::Poll> { + self.0 + .as_ref() + .unwrap() + .poll_writable(ctx) + .map_err(|_| EspError::from_infallible::<{ esp_idf_svc::sys::ESP_FAIL }>()) + } + + fn release(&mut self) -> Result<(), esp_idf_svc::sys::EspError> { + let socket = self.0.take().unwrap(); + socket.into_inner().unwrap().into_raw_fd(); + + Ok(()) + } + } + + impl esp_idf_svc::tls::Socket for EspTlsSocket { + fn handle(&self) -> i32 { + EspTlsSocket::handle(self) + } + + fn release(&mut self) -> Result<(), esp_idf_svc::sys::EspError> { + EspTlsSocket::release(self) + } + } + + impl esp_idf_svc::tls::PollableSocket for EspTlsSocket { + fn poll_readable( + &self, + ctx: &mut core::task::Context, + ) -> core::task::Poll> { + EspTlsSocket::poll_readable(self, ctx) + } + + fn poll_writable( + &self, + ctx: &mut core::task::Context, + ) -> core::task::Poll> { + EspTlsSocket::poll_writeable(self, ctx) + } + } + + //////////////////////////////////////////////////////////////////// + let addr = "google.com:443".to_socket_addrs()?.next().unwrap(); let socket = Async::::connect(addr).await?; @@ -780,7 +852,7 @@ fn test_https_client() -> anyhow::Result<()> { } let th = thread::Builder::new() - .stack_size(15000) + .stack_size(20000) .spawn(move || async_io::block_on(test()))?; th.join().unwrap() @@ -1473,70 +1545,3 @@ fn waveshare_epd_hello_world( Ok(()) } - -pub struct EspTlsSocket(Option>); - -impl EspTlsSocket { - pub const fn new(socket: async_io::Async) -> Self { - Self(Some(socket)) - } - - pub fn handle(&self) -> i32 { - self.0.as_ref().unwrap().as_raw_fd() - } - - pub fn poll_readable( - &self, - ctx: &mut core::task::Context, - ) -> core::task::Poll> { - self.0 - .as_ref() - .unwrap() - .poll_readable(ctx) - .map_err(|_| EspError::from_infallible::<{ esp_idf_svc::sys::ESP_FAIL }>()) - } - - pub fn poll_writeable( - &self, - ctx: &mut core::task::Context, - ) -> core::task::Poll> { - self.0 - .as_ref() - .unwrap() - .poll_writable(ctx) - .map_err(|_| EspError::from_infallible::<{ esp_idf_svc::sys::ESP_FAIL }>()) - } - - fn release(&mut self) -> Result<(), esp_idf_svc::sys::EspError> { - let socket = self.0.take().unwrap(); - socket.into_inner().unwrap().into_raw_fd(); - - Ok(()) - } -} - -impl esp_idf_svc::tls::Socket for EspTlsSocket { - fn handle(&self) -> i32 { - EspTlsSocket::handle(self) - } - - fn release(&mut self) -> Result<(), esp_idf_svc::sys::EspError> { - EspTlsSocket::release(self) - } -} - -impl esp_idf_svc::tls::PollableSocket for EspTlsSocket { - fn poll_readable( - &self, - ctx: &mut core::task::Context, - ) -> core::task::Poll> { - EspTlsSocket::poll_readable(self, ctx) - } - - fn poll_writable( - &self, - ctx: &mut core::task::Context, - ) -> core::task::Poll> { - EspTlsSocket::poll_writeable(self, ctx) - } -}