read tcp replies from server

This commit is contained in:
Vladan Popovic 2022-06-22 19:35:01 +02:00
parent 4842f18b34
commit 6ce84ddfff
2 changed files with 35 additions and 12 deletions

View file

@ -65,10 +65,19 @@ fn main() -> anyhow::Result<()> {
println!("+++++++++++++++++++++++++++++++++"); println!("+++++++++++++++++++++++++++++++++");
let size = mdm.tcp_receive_reply_len()?; let size = mdm.tcp_receive_reply_len()?;
println!("{}", size); println!("{}", size);
//println!("+++++++++++++++++++++++++++++++++"); println!("+++++++++++++++++++++++++++++++++");
//let reply = mdm.tcp_receive(size)?; let mut reply = String::new();
//println!("{}", reply); loop {
//println!("+++++++++++++++++++++++++++++++++"); let s = mdm.tcp_receive_payload(300)?;
if &s == "" {
break;
}
else {
reply.push_str(&s);
}
};
println!("REPLY({}) = {}", reply.len(), reply);
println!("+++++++++++++++++++++++++++++++++");
let _ = mdm.tcp_close_connection()?; let _ = mdm.tcp_close_connection()?;
} }

View file

@ -261,23 +261,37 @@ impl<UART: serial::Uart> Modem<UART> {
Ok(()) Ok(())
} }
fn tcp_parse_payload_size(&mut self, reply_line: &str) -> Result<usize> {
reply_line.split(',')
.into_iter()
.last()
.unwrap_or("x")
.parse::<usize>()
.map_err(|_| ModemError::CommandError(format!("response size should be a number, got {}", reply_line)))
}
pub fn tcp_receive_reply_len(&mut self) -> Result<usize> { 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_query_len())?;
reply.lines() reply.lines()
.filter(|line| line.contains("+CIPRXGET: 4")) .filter(|line| line.contains("+CIPRXGET: 4"))
.next() .next()
.ok_or(ModemError::CommandError("reply not found :/".to_string())) .ok_or(ModemError::CommandError("reply not found :/".to_string()))
.map(|line| { .map(|line| self.tcp_parse_payload_size(line))
line.split(',')
.into_iter()
.last()
.unwrap_or("x")
.parse::<usize>()
.map_err(|_| ModemError::CommandError(format!("response size should be a number, got {}", line)))
})
.unwrap_or(Err(ModemError::CommandError(format!("received 0 elements from parsing")))) .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_close_connection(&mut self) -> Result<String> { pub fn tcp_close_connection(&mut self) -> Result<String> {
self.send_command(Command::tcp_close()) self.send_command(Command::tcp_close())
} }