diff --git a/src/main.rs b/src/main.rs index 0f5831b..db219ca 100644 --- a/src/main.rs +++ b/src/main.rs @@ -65,10 +65,19 @@ fn main() -> anyhow::Result<()> { println!("+++++++++++++++++++++++++++++++++"); let size = mdm.tcp_receive_reply_len()?; println!("{}", size); - //println!("+++++++++++++++++++++++++++++++++"); - //let reply = mdm.tcp_receive(size)?; - //println!("{}", reply); - //println!("+++++++++++++++++++++++++++++++++"); + println!("+++++++++++++++++++++++++++++++++"); + let mut reply = String::new(); + loop { + 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()?; } diff --git a/src/modem.rs b/src/modem.rs index 3872a8e..0f48c00 100644 --- a/src/modem.rs +++ b/src/modem.rs @@ -261,23 +261,37 @@ impl Modem { Ok(()) } + fn tcp_parse_payload_size(&mut self, reply_line: &str) -> Result { + reply_line.split(',') + .into_iter() + .last() + .unwrap_or("x") + .parse::() + .map_err(|_| ModemError::CommandError(format!("response size should be a number, got {}", reply_line))) + } + 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))) - }) + .map(|line| self.tcp_parse_payload_size(line)) .unwrap_or(Err(ModemError::CommandError(format!("received 0 elements from parsing")))) } + pub fn tcp_receive_payload(&mut self, size: usize) -> Result { + 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 { self.send_command(Command::tcp_close()) }