From 4842f18b347b1fd9ab2841e29dd59c6923273830 Mon Sep 17 00:00:00 2001 From: Vladan Popovic Date: Wed, 22 Jun 2022 14:37:55 +0200 Subject: [PATCH] read the TCP reply length and return a number --- src/main.rs | 10 ++++++---- src/modem.rs | 23 +++++++++++++++-------- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/main.rs b/src/main.rs index 24eb074..0f5831b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -62,11 +62,13 @@ fn main() -> anyhow::Result<()> { let _ = mdm.tcp_connect("51.158.66.64", 9988)?; let _ = mdm.tcp_send("aaaaa")?; thread::sleep(Duration::from_millis(1000)); - let _ = mdm.tcp_receive_query_len()?; - let reply = mdm.tcp_receive(6)?; - println!("+++++++++++++++++++++++++++++++++"); - println!("{}", reply); println!("+++++++++++++++++++++++++++++++++"); + let size = mdm.tcp_receive_reply_len()?; + println!("{}", size); + //println!("+++++++++++++++++++++++++++++++++"); + //let reply = mdm.tcp_receive(size)?; + //println!("{}", reply); + //println!("+++++++++++++++++++++++++++++++++"); let _ = mdm.tcp_close_connection()?; } diff --git a/src/modem.rs b/src/modem.rs index 0fa496a..3872a8e 100644 --- a/src/modem.rs +++ b/src/modem.rs @@ -256,19 +256,26 @@ impl Modem { Ok(()) } - - pub fn tcp_receive_query_len(&mut self) -> Result<()> { - self.send_command(Command::tcp_receive_query_len())?; - Ok(()) - } - pub fn tcp_send(&mut self, payload: &str) -> Result<()> { self.send_data(payload)?; Ok(()) } - pub fn tcp_receive(&mut self, size: usize) -> Result { - self.send_command(Command::tcp_receive(size)) + pub fn tcp_receive_reply_len(&mut self) -> Result { + let reply = self.send_command(Command::tcp_receive_query_len())?; + reply.lines() + .filter(|line| line.contains("+CIPRXGET: 4")) + .next() + .ok_or(ModemError::CommandError("reply not found :/".to_string())) + .map(|line| { + line.split(',') + .into_iter() + .last() + .unwrap_or("x") + .parse::() + .map_err(|_| ModemError::CommandError(format!("response size should be a number, got {}", line))) + }) + .unwrap_or(Err(ModemError::CommandError(format!("received 0 elements from parsing")))) } pub fn tcp_close_connection(&mut self) -> Result {