From 178fbaa7916b38b8722c95144d6b984929407c42 Mon Sep 17 00:00:00 2001 From: Sofia Date: Sat, 30 May 2026 17:27:27 +0300 Subject: [PATCH] Pass SimpleATCommand in Async IO instead of command --- src/async_io.rs | 21 +++++++++++++-------- src/at_commands.rs | 11 ++++++++++- src/main.rs | 4 ++-- src/states.rs | 4 +++- 4 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/async_io.rs b/src/async_io.rs index 1823758..d70ce0f 100644 --- a/src/async_io.rs +++ b/src/async_io.rs @@ -1,6 +1,7 @@ use core::{cell::RefCell, marker::PhantomData}; use alloc::{ + boxed::Box, collections::{btree_map::BTreeMap, btree_set::BTreeSet}, rc::Rc, string::String, @@ -8,7 +9,7 @@ use alloc::{ }; use critical_section::Mutex; -use crate::at_commands::{ATCommand, StubATCommand}; +use crate::at_commands::{ATCommand, SimpleATCommand, StubATCommand}; #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub enum KeypadButton { @@ -114,7 +115,8 @@ unsafe impl Sync for ConstructedATCommand {} #[derive(Clone)] pub struct AsyncIO { - at_command: Rc>>>, + at_command: Rc>>>>, + handling_at_command: Rc>>, at_response: Rc>>>, pub keypad: Keypad, } @@ -123,6 +125,7 @@ impl Default for AsyncIO { fn default() -> Self { Self { at_command: Rc::new(Mutex::new(RefCell::new(None))), + handling_at_command: Rc::new(Mutex::new(RefCell::new(false))), at_response: Rc::new(Mutex::new(RefCell::new(None))), keypad: Keypad::default(), } @@ -154,7 +157,7 @@ impl ATPromise { } impl AsyncIO { - pub fn send_at_command>( + pub fn send_at_command + 'static>( &self, command: T, ) -> Result, ()> { @@ -164,21 +167,23 @@ impl AsyncIO { return Err(()); } - *borrow = Some(command.execute()); + *borrow = Some(Box::new(command)); Ok(ATPromise { _data: PhantomData }) }) } - pub unsafe fn check_at_command(&self) -> Option { + pub unsafe fn check_at_command(self) -> Option> { critical_section::with(|cs| { - let borrow = self.at_command.borrow_ref(cs); - borrow.clone() + let mut borrow = self.at_command.borrow_ref_mut(cs); + *self.handling_at_command.borrow_ref_mut(cs) = true; + borrow.take() }) } pub unsafe fn set_at_response(&self, response_str: String) { critical_section::with(|cs| { + *self.handling_at_command.borrow_ref_mut(cs) = false; let mut command = self.at_command.borrow_ref_mut(cs); let mut response = self.at_response.borrow_ref_mut(cs); command.take(); @@ -190,7 +195,7 @@ impl AsyncIO { critical_section::with(|cs| { let command = self.at_command.borrow_ref(cs); let mut response = self.at_response.borrow_ref_mut(cs); - if command.is_some() { + if command.is_some() || *self.handling_at_command.borrow_ref(cs) { return Some(None); } if let Some(resp) = response.take() { diff --git a/src/at_commands.rs b/src/at_commands.rs index c068fa1..35564d3 100644 --- a/src/at_commands.rs +++ b/src/at_commands.rs @@ -2,7 +2,6 @@ use alloc::string::{String, ToString}; use alloc::vec::Vec; use alloc::{borrow::ToOwned, format}; use core::fmt::Write; -use core::marker::PhantomData; use esp_hal::{Blocking, delay::Delay, gpio::Output, uart::Uart}; use crate::async_io::ConstructedATCommand; @@ -173,6 +172,16 @@ pub trait ATCommand: Send + Sync { fn parse_response(text: String) -> Self::Response; } +pub trait SimpleATCommand: Send + Sync { + fn execute(&self) -> ConstructedATCommand; +} + +impl SimpleATCommand for T { + fn execute(&self) -> ConstructedATCommand { + self.execute() + } +} + #[derive(Debug, Clone)] pub struct StubATCommand(()); impl ATCommand for StubATCommand { diff --git a/src/main.rs b/src/main.rs index ee40f3a..d6f7198 100644 --- a/src/main.rs +++ b/src/main.rs @@ -227,8 +227,8 @@ fn thread_2_main( let delay = Delay::new(); loop { - if let Some(command) = unsafe { async_io.check_at_command() } { - let response = match command { + if let Some(command) = unsafe { async_io.clone().check_at_command() } { + let response = match command.execute() { async_io::ConstructedATCommand::Single(cmd) => at_commands.raw_command(cmd), async_io::ConstructedATCommand::AddInfo(cmd, add) => { at_commands.raw_two_part_command(cmd, add) diff --git a/src/states.rs b/src/states.rs index 710d2f4..8a85169 100644 --- a/src/states.rs +++ b/src/states.rs @@ -42,7 +42,9 @@ impl ATCommandState { } } -impl State for ATCommandState { +impl State + for ATCommandState +{ fn init(&mut self, data: &mut StateData) { self.promise = Some(data.io.send_at_command(self.command.clone()).unwrap()); }