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

@ -239,9 +239,9 @@ impl Command {
} }
} }
pub fn tcp_ssl_disable() -> Command { pub fn tcp_ssl_set(enabled: bool) -> Command {
Command { Command {
text: "AT+CIPSSL=0".to_string(), text: format!("AT+CIPSSL={}", if enabled { 1 } else { 0 }),
timeout: Duration::from_millis(3000), timeout: Duration::from_millis(3000),
contains: Some("OK".to_string()), 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 { Command {
text: "AT+CIPRXGET=4".to_string(), text: "AT+CIPRXGET=4".to_string(),
timeout: Duration::from_millis(3000), timeout: Duration::from_millis(3000),

View file

@ -13,7 +13,7 @@ use esp_idf_hal::serial;
fn main() -> anyhow::Result<()> { fn main() -> anyhow::Result<()> {
esp_idf_sys::link_patches(); 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. // LilyGo TTGO T-Call sim800l board serial pins.
let serial_rx = dp.pins.gpio26; let serial_rx = dp.pins.gpio26;
@ -32,31 +32,36 @@ fn main() -> anyhow::Result<()> {
dp.uart1, dp.uart1,
serial_pins, serial_pins,
serial::config::Config::default().baudrate(Hertz(115200)), serial::config::Config::default().baudrate(Hertz(115200)),
).expect("Failed to create serial ... :("); )?;
let (tx, rx) = serial.split(); let (tx, rx) = serial.split();
let mut mdm = modem::Modem::new(tx, rx); 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()?;
let modem_pwrkey = dp.pins.gpio4.into_output().unwrap(); let modem_rst = dp.pins.gpio5.into_output()?;
let modem_rst = dp.pins.gpio5.into_output().unwrap(); let modem_power = dp.pins.gpio23.into_output()?;
let modem_power = dp.pins.gpio23.into_output().unwrap();
mdm.init(modem_pwrkey, modem_rst, modem_power)?; mdm.init(modem_pwrkey, modem_rst, modem_power)?;
if !mdm.is_gprs_attached()? {
let _ = mdm.connect_to_gprs_ap( let _ = mdm.connect_to_gprs_ap(
config::A1_GPRS_AP.apn, config::A1_GPRS_AP.apn,
config::A1_GPRS_AP.username, config::A1_GPRS_AP.username,
config::A1_GPRS_AP.password, 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()?; let _ = mdm.get_ip_addr()?;
println!("connecting to server!"); //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_quick_mode(false);
let _ = mdm.tcp_set_manual_receive()?; let _ = mdm.tcp_set_manual_receive()?;
let _ = mdm.tcp_connect("51.158.66.64", 9988)?; let _ = mdm.tcp_connect("51.158.66.64", 9988)?;
@ -64,21 +69,18 @@ fn main() -> anyhow::Result<()> {
thread::sleep(Duration::from_millis(1000)); thread::sleep(Duration::from_millis(1000));
println!("+++++++++++++++++++++++++++++++++"); println!("+++++++++++++++++++++++++++++++++");
let size = mdm.tcp_receive_reply_len()?; let size = mdm.tcp_receive_reply_len()?;
println!("{}", size);
let mut reply = vec![0 as u8; size];
let received_size = mdm.tcp_receive(&mut reply)?;
println!("expected: {} / received: {}", size, received_size);
println!("+++++++++++++++++++++++++++++++++"); println!("+++++++++++++++++++++++++++++++++");
let mut reply = String::new(); println!("REPLY({}) = {}", reply.len(), reply.iter().map(|b| char::from(*b)).collect::<String>());
loop {
let s = mdm.tcp_receive_payload(300)?;
if &s == "" {
break;
}
else {
reply.push_str(&s);
}
};
println!("REPLY({}) = {}", reply.len(), reply);
println!("+++++++++++++++++++++++++++++++++"); println!("+++++++++++++++++++++++++++++++++");
let _ = mdm.tcp_close_connection()?; let _ = mdm.tcp_close_connection()?;
break;
}
println!("!!!!!!!!!!!!GPRS NOT ATTACHED!!!!!!!!!!!");
} }
Ok(()) Ok(())

View file

@ -8,6 +8,8 @@ use embedded_hal::serial::{Read, Write};
use embedded_hal::digital::v2::OutputPin; use embedded_hal::digital::v2::OutputPin;
use esp_idf_hal::serial::{self, Rx, Tx}; 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 type Result<T> = std::result::Result<T, ModemError>;
pub struct Modem<UART: serial::Uart> { pub struct Modem<UART: serial::Uart> {
@ -237,7 +239,12 @@ impl<UART: serial::Uart> Modem<UART> {
} }
pub fn tcp_ssl_disable(&mut self) -> Result<()> { 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(()) Ok(())
} }
@ -271,7 +278,7 @@ impl<UART: serial::Uart> Modem<UART> {
} }
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_reply_len())?;
reply.lines() reply.lines()
.filter(|line| line.contains("+CIPRXGET: 4")) .filter(|line| line.contains("+CIPRXGET: 4"))
.next() .next()
@ -280,16 +287,27 @@ impl<UART: serial::Uart> Modem<UART> {
.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> { pub fn tcp_receive(&mut self, buf: &mut [u8]) -> Result<usize> {
self.send_command(Command::tcp_receive(size)) let mut size = 0;
loop {
let reply = self.send_command(Command::tcp_receive(MAX_TCP_MANUAL_REPLY_SIZE))
.map(|reply| { .map(|reply| {
reply reply
.split("\r\n") .split("\r\n")
.filter(|line| line.len() > 2 && !line.contains("+CIPRXGET: 2,")) .filter(|line| line.len() > 2 && !line.contains("+CIPRXGET: 2,"))
.last() .next()
.map(String::from) .map(|line| line.chars().enumerate().map(|(idx, c)| buf[size + idx] = c as u8).count())
.unwrap_or("".to_string()) })?;
}) match reply {
Some(0) | None => {
break Ok(size)
},
Some(x) => {
size += x;
continue
},
}
}
} }
pub fn tcp_close_connection(&mut self) -> Result<String> { pub fn tcp_close_connection(&mut self) -> Result<String> {