misc + implement tcp receive in buffer

needed for the TcpClientStack in minimq
This commit is contained in:
Vladan Popovic 2022-06-24 00:28:25 +02:00
parent 481805a1d1
commit 1ea0bf62e5
3 changed files with 74 additions and 54 deletions

View file

@ -8,6 +8,8 @@ use embedded_hal::serial::{Read, Write};
use embedded_hal::digital::v2::OutputPin;
use esp_idf_hal::serial::{self, Rx, Tx};
const MAX_TCP_MANUAL_REPLY_SIZE: usize = 300;
pub type Result<T> = std::result::Result<T, ModemError>;
pub struct Modem<UART: serial::Uart> {
@ -237,7 +239,12 @@ impl<UART: serial::Uart> Modem<UART> {
}
pub fn tcp_ssl_disable(&mut self) -> Result<()> {
let _ = self.send_command(Command::tcp_ssl_disable())?;
let _ = self.send_command(Command::tcp_ssl_set(false))?;
Ok(())
}
pub fn tcp_ssl_enable(&mut self) -> Result<()> {
let _ = self.send_command(Command::tcp_ssl_set(true))?;
Ok(())
}
@ -271,7 +278,7 @@ impl<UART: serial::Uart> Modem<UART> {
}
pub fn tcp_receive_reply_len(&mut self) -> Result<usize> {
let reply = self.send_command(Command::tcp_receive_query_len())?;
let reply = self.send_command(Command::tcp_receive_reply_len())?;
reply.lines()
.filter(|line| line.contains("+CIPRXGET: 4"))
.next()
@ -280,16 +287,27 @@ impl<UART: serial::Uart> Modem<UART> {
.unwrap_or(Err(ModemError::CommandError(format!("received 0 elements from parsing"))))
}
pub fn tcp_receive_payload(&mut self, size: usize) -> Result<String> {
self.send_command(Command::tcp_receive(size))
.map(|reply| {
reply
.split("\r\n")
.filter(|line| line.len() > 2 && !line.contains("+CIPRXGET: 2,"))
.last()
.map(String::from)
.unwrap_or("".to_string())
})
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())
})?;
match reply {
Some(0) | None => {
break Ok(size)
},
Some(x) => {
size += x;
continue
},
}
}
}
pub fn tcp_close_connection(&mut self) -> Result<String> {