From ee3575b6114ef19bf3baae80a5b28b1e69213313 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Pol=C3=A1=C4=8Dek?= Date: Sun, 10 Nov 2024 17:48:19 +0100 Subject: [PATCH] Merge _dispatch_response into `reader` --- nrf52840/src/at_utils.rs | 40 ++++++++++++++++++++++------------------ nrf52840/src/bg77.rs | 2 +- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/nrf52840/src/at_utils.rs b/nrf52840/src/at_utils.rs index dba6e4b..cf2ff71 100644 --- a/nrf52840/src/at_utils.rs +++ b/nrf52840/src/at_utils.rs @@ -22,7 +22,10 @@ static CHANNEL: Channel, Erro Channel::new(); #[embassy_executor::task] -async fn reader(mut rx: UarteRxWithIdle<'static, UARTE1, TIMER0>) { +async fn reader( + mut rx: UarteRxWithIdle<'static, UARTE1, TIMER0>, + callback_dispatcher: fn(&str, &str) -> bool, +) { const AT_BUF_SIZE: usize = 300; let mut buf = [0; AT_BUF_SIZE]; loop { @@ -35,9 +38,23 @@ async fn reader(mut rx: UarteRxWithIdle<'static, UARTE1, TIMER0>) { Ok(len) => { let lines = split_lines(&buf[..len]).unwrap(); for line in lines { - CHANNEL - .send(String::from_str(line).map_err(|_| Error::StringEncodingError)) - .await; + let mut is_callback = false; + if line.starts_with('+') { + let prefix_len = line.find(": "); + if let Some(prefix_len) = prefix_len { + let prefix = &line[1..prefix_len]; + let rest = &line[prefix_len + 2..]; + if (callback_dispatcher)(prefix, rest) { + is_callback = true; + } + } + } + + if !is_callback { + CHANNEL + .send(String::from_str(line).map_err(|_| Error::StringEncodingError)) + .await; + } } CHANNEL.send(Ok(String::new())).await; // Stop transmission } @@ -52,7 +69,7 @@ impl AtUart { callback_dispatcher: fn(&str, &str) -> bool, spawner: &Spawner, ) -> Self { - unwrap!(spawner.spawn(reader(rx))); + unwrap!(spawner.spawn(reader(rx, callback_dispatcher))); Self { tx, callback_dispatcher, @@ -87,19 +104,6 @@ impl AtUart { self.read(timeout).await } - - async fn _dispatch_response(self, line: &str) { - if line.starts_with('+') { - let prefix_len = line.find(": "); - if let Some(prefix_len) = prefix_len { - let prefix = &line[1..prefix_len]; - let rest = &line[prefix_len..]; - if !(self.callback_dispatcher)(prefix, rest) { - // TODO: forward - } - } - } - } } fn readline(s: &str) -> (Option<&str>, &str) { diff --git a/nrf52840/src/bg77.rs b/nrf52840/src/bg77.rs index a0daf8c..eef62d8 100644 --- a/nrf52840/src/bg77.rs +++ b/nrf52840/src/bg77.rs @@ -10,7 +10,7 @@ use embassy_time::{Duration, Timer}; use heapless::format; static MINIMUM_TIMEOUT: Duration = Duration::from_millis(300); -const CLIENT_ID: u32 = 1; +const CLIENT_ID: u32 = 0; pub struct BG77 { uart1: AtUart,