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

Commit

Permalink
Working end to end
Browse files Browse the repository at this point in the history
  • Loading branch information
ivmarkov committed Oct 19, 2023
1 parent f712acd commit 09887d8
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 81 deletions.
3 changes: 3 additions & 0 deletions sdkconfig.defaults
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
167 changes: 86 additions & 81 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -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()?;

Expand Down Expand Up @@ -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();

Expand All @@ -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<async_io::Async<TcpStream>>);

impl EspTlsSocket {
pub const fn new(socket: async_io::Async<TcpStream>) -> 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<Result<(), esp_idf_svc::sys::EspError>> {
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<Result<(), esp_idf_svc::sys::EspError>> {
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<Result<(), esp_idf_svc::sys::EspError>> {
EspTlsSocket::poll_readable(self, ctx)
}

fn poll_writable(
&self,
ctx: &mut core::task::Context,
) -> core::task::Poll<Result<(), esp_idf_svc::sys::EspError>> {
EspTlsSocket::poll_writeable(self, ctx)
}
}

////////////////////////////////////////////////////////////////////

let addr = "google.com:443".to_socket_addrs()?.next().unwrap();
let socket = Async::<TcpStream>::connect(addr).await?;

Expand All @@ -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()
Expand Down Expand Up @@ -1473,70 +1545,3 @@ fn waveshare_epd_hello_world(

Ok(())
}

pub struct EspTlsSocket(Option<async_io::Async<TcpStream>>);

impl EspTlsSocket {
pub const fn new(socket: async_io::Async<TcpStream>) -> 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<Result<(), esp_idf_svc::sys::EspError>> {
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<Result<(), esp_idf_svc::sys::EspError>> {
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<Result<(), esp_idf_svc::sys::EspError>> {
EspTlsSocket::poll_readable(self, ctx)
}

fn poll_writable(
&self,
ctx: &mut core::task::Context,
) -> core::task::Poll<Result<(), esp_idf_svc::sys::EspError>> {
EspTlsSocket::poll_writeable(self, ctx)
}
}

0 comments on commit 09887d8

Please sign in to comment.