diff --git a/src/command.rs b/src/command.rs index 2672aeb..f44a914 100644 --- a/src/command.rs +++ b/src/command.rs @@ -239,9 +239,9 @@ impl Command { } } - pub fn tcp_ssl_disable() -> Command { + pub fn tcp_ssl_set(enabled: bool) -> Command { Command { - text: "AT+CIPSSL=0".to_string(), + text: format!("AT+CIPSSL={}", if enabled { 1 } else { 0 }), timeout: Duration::from_millis(3000), contains: Some("OK".to_string()), } @@ -319,7 +319,7 @@ impl Command { } } - pub fn tcp_receive_query_len() -> Command { + pub fn tcp_receive_reply_len() -> Command { Command { text: "AT+CIPRXGET=4".to_string(), timeout: Duration::from_millis(3000), diff --git a/src/main.rs b/src/main.rs index 444f41c..11c0f6c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,7 +13,7 @@ use esp_idf_hal::serial; fn main() -> anyhow::Result<()> { esp_idf_sys::link_patches(); - let dp = Peripherals::take().unwrap(); + let dp = Peripherals::take().expect("error taking peripherals"); // LilyGo TTGO T-Call sim800l board serial pins. let serial_rx = dp.pins.gpio26; @@ -32,53 +32,55 @@ fn main() -> anyhow::Result<()> { dp.uart1, serial_pins, serial::config::Config::default().baudrate(Hertz(115200)), - ).expect("Failed to create serial ... :("); + )?; let (tx, rx) = serial.split(); let mut mdm = modem::Modem::new(tx, rx); - // Unwrap all of these, without them we can't do anything anyways. - let modem_pwrkey = dp.pins.gpio4.into_output().unwrap(); - let modem_rst = dp.pins.gpio5.into_output().unwrap(); - let modem_power = dp.pins.gpio23.into_output().unwrap(); + let modem_pwrkey = dp.pins.gpio4.into_output()?; + let modem_rst = dp.pins.gpio5.into_output()?; + let modem_power = dp.pins.gpio23.into_output()?; mdm.init(modem_pwrkey, modem_rst, modem_power)?; - let _ = mdm.connect_to_gprs_ap( - config::A1_GPRS_AP.apn, - config::A1_GPRS_AP.username, - config::A1_GPRS_AP.password, - )?; + if !mdm.is_gprs_attached()? { + let _ = mdm.connect_to_gprs_ap( + config::A1_GPRS_AP.apn, + config::A1_GPRS_AP.username, + config::A1_GPRS_AP.password, + )?; + } - if mdm.is_gprs_attached()? { - if mdm.tcp_is_ssl_enabled()? { - mdm.tcp_ssl_disable()?; + loop { + if mdm.is_gprs_attached()? { + let _ = mdm.get_ip_addr()?; + + //println!("connecting to server!"); + //if !mdm.tcp_is_ssl_enabled()? { + // let _ = mdm.tcp_ssl_enable()?; + //} + if mdm.tcp_is_ssl_enabled()? { + let _ = mdm.tcp_ssl_disable()?; + } + let _ = mdm.tcp_set_quick_mode(false); + let _ = mdm.tcp_set_manual_receive()?; + let _ = mdm.tcp_connect("51.158.66.64", 9988)?; + let _ = mdm.tcp_send("aaaaa")?; + thread::sleep(Duration::from_millis(1000)); + println!("+++++++++++++++++++++++++++++++++"); + let size = mdm.tcp_receive_reply_len()?; + + let mut reply = vec![0 as u8; size]; + let received_size = mdm.tcp_receive(&mut reply)?; + + println!("expected: {} / received: {}", size, received_size); + println!("+++++++++++++++++++++++++++++++++"); + println!("REPLY({}) = {}", reply.len(), reply.iter().map(|b| char::from(*b)).collect::()); + println!("+++++++++++++++++++++++++++++++++"); + let _ = mdm.tcp_close_connection()?; + break; } - let _ = mdm.get_ip_addr()?; - - println!("connecting to server!"); - let _ = mdm.tcp_set_quick_mode(false); - let _ = mdm.tcp_set_manual_receive()?; - let _ = mdm.tcp_connect("51.158.66.64", 9988)?; - let _ = mdm.tcp_send("aaaaa")?; - thread::sleep(Duration::from_millis(1000)); - println!("+++++++++++++++++++++++++++++++++"); - let size = mdm.tcp_receive_reply_len()?; - println!("{}", size); - 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()?; + println!("!!!!!!!!!!!!GPRS NOT ATTACHED!!!!!!!!!!!"); } Ok(()) diff --git a/src/modem.rs b/src/modem.rs index 0f48c00..6c2390a 100644 --- a/src/modem.rs +++ b/src/modem.rs @@ -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 = std::result::Result; pub struct Modem { @@ -237,7 +239,12 @@ impl Modem { } 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 Modem { } pub fn tcp_receive_reply_len(&mut self) -> Result { - 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 Modem { .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_receive(&mut self, buf: &mut [u8]) -> Result { + 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 {