Skip to content

Commit

Permalink
Better callback detection
Browse files Browse the repository at this point in the history
  • Loading branch information
lukipuki committed Nov 10, 2024
1 parent 414a684 commit 5745684
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
9 changes: 7 additions & 2 deletions nrf52840/src/at_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ async fn reader(
Err(err) => CHANNEL.send(Err(err)).await,
Ok(len) => {
let lines = split_lines(&buf[..len]).unwrap();
let mut lines_count = 0;
for (idx, line) in lines.iter().enumerate() {
let is_callback = split_at_response(line)
.map(|(prefix, rest)| (callback_dispatcher)(prefix, rest))
Expand All @@ -72,14 +73,19 @@ async fn reader(
CHANNEL
.send(String::from_str(line).map_err(|_| Error::StringEncodingError))
.await;
debug!("Read: {}", line);
lines_count += 1;
if (*line == "OK" || *line == "ERROR") && idx + 1 < lines.len() {
CHANNEL.send(Ok(String::new())).await; // Mark a finished command
lines_count = 0;
}
} else {
info!("CALLBACK! {}", line);
}
}
CHANNEL.send(Ok(String::new())).await; // Stop transmission
if lines_count > 0 {
CHANNEL.send(Ok(String::new())).await; // Stop transmission
}
}
}
}
Expand Down Expand Up @@ -115,7 +121,6 @@ impl AtUart {
break;
}

debug!("Read {}", line.as_str());
res.push(line).map_err(|_| Error::BufferTooSmallError)?;
}

Expand Down
11 changes: 10 additions & 1 deletion nrf52840/src/bg77.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ pub struct BG77 {
activation_timeout: Duration,
}

fn callback_dispatcher(prefix: &str, _rest: &str) -> bool {
fn callback_dispatcher(prefix: &str, rest: &str) -> bool {
// TODO: Improve this
match prefix {
"QMTSTAT" => true,
"QMTPUB" => true,
"QMTCONN" => rest.len() >= 4 && rest.as_bytes()[1] == 0x2c && rest.as_bytes()[3] == 0x2c,
"QMTOPEN" => rest.as_bytes()[1] == 0x2c && rest.len() == 3,
_ => false,
}
}
Expand Down Expand Up @@ -93,6 +97,10 @@ impl BG77 {

pub async fn mqtt_disconnect(&mut self) -> Result<(), Error> {
let command = format!(50; "AT+QMTDISC={CLIENT_ID}").unwrap();
self.uart1
.call_with_response(&command, MINIMUM_TIMEOUT, self.pkt_timeout)
.await?;
let command = format!(50; "AT+QMTCLOSE={CLIENT_ID}").unwrap();
self.uart1.call(&command, MINIMUM_TIMEOUT).await?;
Ok(())
}
Expand All @@ -106,6 +114,7 @@ impl BG77 {
}

pub async fn experiment(&mut self) {
//self._turn_on().await;
unwrap!(self.config().await);
let _ = self.mqtt_connect().await;

Expand Down

0 comments on commit 5745684

Please sign in to comment.