mqtt continues

This commit is contained in:
Vladan Popovic 2022-06-26 00:56:59 +02:00
parent 8d7d4d898d
commit edf427dcb1
3 changed files with 79 additions and 59 deletions

View file

@ -164,17 +164,14 @@ impl<UART: serial::Uart> Modem<UART> {
}
}
fn send(&mut self, b: u8) -> Result<()> {
nb::block!(self.tx.write(b))
.map_err(|_| ModemError::CommandError(format!("error writing {} to serial", b)))?;
Ok(())
}
#[inline(always)]
fn send_bytes(&mut self, payload: &[u8], eos: char) -> Result<()> {
for b in payload.iter() {
self.send(*b)?;
nb::block!(self.tx.write(*b))
.map_err(|_| ModemError::CommandError(format!("error writing {} to serial", b)))?;
}
self.send(eos as u8)?;
nb::block!(self.tx.write(eos as u8))
.map_err(|_| ModemError::CommandError(format!("error writing {} to serial", eos)))?;
Ok(())
}
@ -290,22 +287,25 @@ impl<UART: serial::Uart> Modem<UART> {
pub fn tcp_receive(&mut self, buf: &mut [u8]) -> Result<usize> {
let mut size = 0;
loop {
let reply = self.send_command(Command::tcp_receive(MAX_TCP_MANUAL_REPLY_SIZE))
.map(|reply| {
reply
.split("\r\n")
.filter(|line| line.len() > 2 && !line.contains("+CIPRXGET: 2,"))
.next()
.map(|line| line.chars().enumerate().map(|(idx, c)| buf[size + idx] = c as u8).count())
let reply: usize = self.send_command(Command::tcp_receive(MAX_TCP_MANUAL_REPLY_SIZE))
.map(|reply: String| {
reply.lines()
.map(|line| {
if !line.starts_with("\r") && !line.contains("+CIPRXGET: 2,") && !line.contains("OK") {
line.chars().enumerate().map(|(idx, c)| { buf[size + idx] = c as u8; }).count()
}
else {
0
}
})
.sum()
})?;
match reply {
Some(0) | None => {
break Ok(size)
},
Some(x) => {
size += x;
continue
},
if reply == 0 {
break Ok(size)
}
else {
size += reply;
continue
}
}
}