Skip to content

Commit

Permalink
Parse values
Browse files Browse the repository at this point in the history
  • Loading branch information
lukipuki committed Nov 12, 2024
1 parent d057cc5 commit 611118f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
28 changes: 22 additions & 6 deletions nrf52840/src/at_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,16 @@ pub enum FromModem {
Error,
}

fn pick_values<'a>(values: &'a str, indices: &[usize]) -> Vec<String<AT_VALUE_SIZE>, 5> {
let split: Vec<&str, 10> = values.split(',').collect();
indices
.into_iter()
.map(|idx| String::from_str(split[*idx]).unwrap()) //TODO
.collect()
}

const AT_COMMAND_SIZE: usize = 100;
const AT_VALUE_SIZE: usize = 20;

static CHANNEL: Channel<ThreadModeRawMutex, Result<FromModem, Error>, 5> = Channel::new();

Expand Down Expand Up @@ -97,19 +106,25 @@ pub struct AtUart {

pub struct AtResponse {
lines: Vec<FromModem, 4>,
answer: Result<String<AT_COMMAND_SIZE>, Error>,
answer: Result<Vec<String<AT_VALUE_SIZE>, 5>, Error>,
}

impl AtResponse {
fn new(lines: Vec<FromModem, 4>, command: &str) -> Self {
fn new(lines: Vec<FromModem, 4>, command: &str, indices: &[usize]) -> Self {
let pos = command.find(['=', '?']).unwrap_or(command.len());
let prefix = &command[2..pos];
for line in &lines {
if let FromModem::Line(line) = line {
if line.starts_with(prefix) {
info!("RETURN: {}", line.as_str());
let (_, rest) = split_at_response(line).unwrap();
let values = pick_values(rest, indices);
{
let val_print: Vec<&str, AT_VALUE_SIZE> =
values.iter().map(|s| s.as_str()).collect();
info!("RETURN: {} {:?}", line.as_str(), val_print.as_slice());
}
return Self {
answer: Ok(line.clone()),
answer: Ok(values),
lines,
};
}
Expand Down Expand Up @@ -166,7 +181,7 @@ impl AtUart {
debug!("Calling {}", command);
let lines = self.read(timeout).await?;
if let Some(&FromModem::Ok) = lines.last() {
Ok(AtResponse::new(lines, command))
Ok(AtResponse::new(lines, command, &[]))
} else {
error!("Fail: {}", command);
//for line in lines {
Expand All @@ -181,6 +196,7 @@ impl AtUart {
command: &str,
call_timeout: Duration,
response_timeout: Duration,
indices: &[usize],
) -> Result<AtResponse, Error> {
self.write(command).await?;
debug!("Calling {}", command);
Expand All @@ -191,7 +207,7 @@ impl AtUart {
}
let second = self.read(response_timeout).await?;
lines.extend(second);
Ok(AtResponse::new(lines, command))
Ok(AtResponse::new(lines, command, indices))
}
}

Expand Down
11 changes: 8 additions & 3 deletions nrf52840/src/bg77.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ impl BG77 {
pub async fn mqtt_connect(&mut self) -> Result<(), Error> {
let at_command = format!(100; "AT+QMTOPEN={CLIENT_ID},\"broker.emqx.io\",1883").unwrap();
self.uart1
.call_with_response(&at_command, MINIMUM_TIMEOUT, self.activation_timeout)
.call_with_response(
&at_command,
MINIMUM_TIMEOUT,
self.activation_timeout,
&[0, 1],
)
.await?;

self.uart1.call("AT+QMTOPEN?", MINIMUM_TIMEOUT).await?;
Expand All @@ -84,7 +89,7 @@ impl BG77 {
let command = format!(50; "AT+QMTCONN={CLIENT_ID},\"client-embassy\"").unwrap();
let _ = self
.uart1
.call_with_response(&command, MINIMUM_TIMEOUT, self.pkt_timeout)
.call_with_response(&command, MINIMUM_TIMEOUT, self.pkt_timeout, &[0, 1, 2])
.await;
// +QMTCONN: <client_id>,0,0

Expand All @@ -96,7 +101,7 @@ 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)
.call_with_response(&command, MINIMUM_TIMEOUT, self.pkt_timeout, &[0, 1])
.await?;
let command = format!(50; "AT+QMTCLOSE={CLIENT_ID}").unwrap();
self.uart1.call(&command, MINIMUM_TIMEOUT).await?;
Expand Down

0 comments on commit 611118f

Please sign in to comment.