2022-06-16 01:06:32 +02:00
|
|
|
use crate::command::Command;
|
|
|
|
|
2022-06-13 00:55:08 +02:00
|
|
|
use std::thread;
|
2022-06-16 01:39:16 +02:00
|
|
|
use std::error::Error;
|
2022-06-16 01:06:32 +02:00
|
|
|
use std::time::{Duration, Instant};
|
2022-06-11 16:49:16 +02:00
|
|
|
|
2022-06-16 01:06:32 +02:00
|
|
|
use embedded_hal::serial::{Read, Write};
|
2022-06-13 00:55:08 +02:00
|
|
|
use embedded_hal::digital::v2::OutputPin;
|
2022-06-19 00:33:32 +02:00
|
|
|
use esp_idf_hal::serial::{self, Rx, Tx};
|
2022-06-13 00:55:08 +02:00
|
|
|
|
2022-06-18 01:39:38 +02:00
|
|
|
pub type Result<T> = std::result::Result<T, ModemError>;
|
2022-06-13 00:55:08 +02:00
|
|
|
|
|
|
|
pub struct Modem<UART: serial::Uart> {
|
2022-06-19 00:33:32 +02:00
|
|
|
rx: Rx<UART>,
|
2022-06-13 00:55:08 +02:00
|
|
|
tx: Tx<UART>,
|
2022-06-11 16:49:16 +02:00
|
|
|
}
|
|
|
|
|
2022-06-13 00:55:08 +02:00
|
|
|
#[derive(Debug)]
|
2022-06-12 12:02:59 +02:00
|
|
|
pub enum ModemError {
|
|
|
|
CommandError(String),
|
2022-06-13 00:55:08 +02:00
|
|
|
SetupError(String),
|
2022-06-19 00:33:32 +02:00
|
|
|
ReadError,
|
2022-06-18 01:39:38 +02:00
|
|
|
TimeoutError,
|
2022-06-12 12:02:59 +02:00
|
|
|
}
|
2022-06-11 16:49:16 +02:00
|
|
|
|
2022-06-16 01:39:16 +02:00
|
|
|
impl Error for ModemError {}
|
|
|
|
|
2022-06-12 12:02:59 +02:00
|
|
|
impl std::fmt::Display for ModemError {
|
2022-06-11 16:49:16 +02:00
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
2022-06-13 00:55:08 +02:00
|
|
|
write!(f, "{:?}", self)
|
2022-06-11 16:49:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-19 00:33:32 +02:00
|
|
|
impl<UART: serial::Uart> Modem<UART> {
|
|
|
|
pub fn new(tx: Tx<UART>, rx: Rx<UART>) -> Self {
|
|
|
|
Self {
|
|
|
|
rx,
|
|
|
|
tx,
|
|
|
|
}
|
2022-06-18 01:39:38 +02:00
|
|
|
}
|
|
|
|
|
2022-06-19 00:33:32 +02:00
|
|
|
/// Initialize the modem (sim800l in this case). The initialization process sets all pins in the
|
|
|
|
/// required state so that the modem is turned on, then resets it a couple of times (beats me) and
|
|
|
|
/// sleeps for 3 seconds, which is enough for the modem to come online.
|
|
|
|
///
|
|
|
|
/// Below is an example for sim800l pins on a LilyGo TTGO T-Call.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// 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();
|
|
|
|
///
|
|
|
|
/// modem::init(modem_pwrkey, modem_rst, modem_power);
|
|
|
|
/// ```
|
|
|
|
pub fn init(&mut self, mut pwrkey: impl OutputPin, mut rst: impl OutputPin, mut power: impl OutputPin) -> Result<()> {
|
|
|
|
println!("Turning SIM800L on ...");
|
|
|
|
power.set_high().map_err(|_| ModemError::SetupError("Error setting POWER to high.".to_string()))?;
|
|
|
|
rst.set_high().map_err(|_| ModemError::SetupError("Error setting RST to high.".to_string()))?;
|
|
|
|
// Pull down PWRKEY for more than 1 second according to manual requirements
|
|
|
|
pwrkey.set_high().map_err(|_| ModemError::SetupError("Error setting PWRKEY to high.".to_string()))?;
|
|
|
|
thread::sleep(Duration::from_millis(100));
|
|
|
|
pwrkey.set_low().map_err(|_| ModemError::SetupError("Error setting PWRKEY to low.".to_string()))?;
|
|
|
|
thread::sleep(Duration::from_millis(1000));
|
|
|
|
pwrkey.set_high().map_err(|_| ModemError::SetupError("Error setting PWRKEY to high.".to_string()))?;
|
|
|
|
println!("Waiting 3s for sim module to come online ...");
|
|
|
|
thread::sleep(Duration::from_millis(3000));
|
|
|
|
Ok(())
|
2022-06-18 01:39:38 +02:00
|
|
|
}
|
|
|
|
|
2022-06-19 00:33:32 +02:00
|
|
|
/// Reads a whole line (that ends with \\n) within the given `timeout` passed on input.
|
|
|
|
/// `nb` returns Ok(byte), or one of Err(WouldBlock) and Err(Other) which isn't of anyone's
|
|
|
|
/// interest, so the retry mechanism is triggered on _any_ error every 200ms until a byte is
|
|
|
|
/// received, or the timeout is reached.
|
|
|
|
///
|
|
|
|
fn read_line(&mut self, timeout: Duration) -> Result<String> {
|
|
|
|
let mut buf = String::new();
|
|
|
|
let start = Instant::now();
|
|
|
|
|
2022-06-18 01:39:38 +02:00
|
|
|
loop {
|
2022-06-19 00:33:32 +02:00
|
|
|
match self.rx.read() {
|
|
|
|
Ok(b) => {
|
|
|
|
print!("{}, ", b);
|
|
|
|
buf.push(b as char);
|
|
|
|
if b == '\n' as u8 {
|
|
|
|
return Ok(buf);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
if Instant::now() > start + timeout {
|
|
|
|
return Err(ModemError::ReadError);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
thread::sleep(Duration::from_millis(200));
|
|
|
|
}
|
|
|
|
},
|
2022-06-18 01:39:38 +02:00
|
|
|
}
|
2022-06-12 12:02:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-19 00:33:32 +02:00
|
|
|
/// Reads the serial RX until a \\n char is encoutered, or a timeout is reached. The timeout is
|
|
|
|
/// provided on input via the `timeout` argument. The first argument `contains` is checked
|
|
|
|
/// against a line in the response, if it's there the reading stops.
|
2022-06-16 01:06:32 +02:00
|
|
|
///
|
2022-06-19 00:33:32 +02:00
|
|
|
/// If `contains` is `None`, the first line only is returned in the response. If it's
|
|
|
|
/// `Some(match_txt)`, then the end of the response is matched against `match_txt`.
|
2022-06-18 01:39:38 +02:00
|
|
|
fn read_response(&mut self, contains: Option<String>, timeout: Duration) -> Result<String> {
|
2022-06-12 12:02:59 +02:00
|
|
|
let mut response = String::new();
|
2022-06-19 00:33:32 +02:00
|
|
|
let start = Instant::now();
|
2022-06-18 01:39:38 +02:00
|
|
|
let match_text: String = contains.unwrap_or("".to_string());
|
|
|
|
|
2022-06-16 01:06:32 +02:00
|
|
|
loop {
|
2022-06-19 00:33:32 +02:00
|
|
|
if let Ok(line) = self.read_line(start + timeout - Instant::now()) {
|
2022-06-18 01:39:38 +02:00
|
|
|
response.push_str(&line);
|
2022-06-19 00:33:32 +02:00
|
|
|
if line.contains("ERROR") || line.contains(&match_text) {
|
2022-06-18 01:39:38 +02:00
|
|
|
println!("Found match {} for line {} ... exiting response reader now!", match_text, line);
|
|
|
|
println!("-----------------------------------------------------------");
|
|
|
|
break;
|
|
|
|
}
|
2022-06-19 00:33:32 +02:00
|
|
|
} else {
|
|
|
|
if Instant::now() > start + timeout {
|
|
|
|
return Err(ModemError::TimeoutError);
|
|
|
|
}
|
|
|
|
println!("got into retry loop in read_response");
|
|
|
|
thread::sleep(Duration::from_millis(200));
|
2022-06-16 01:06:32 +02:00
|
|
|
}
|
2022-06-18 01:39:38 +02:00
|
|
|
}
|
|
|
|
Ok(response.to_string())
|
2022-06-11 16:49:16 +02:00
|
|
|
}
|
2022-06-12 12:02:59 +02:00
|
|
|
|
2022-06-19 00:33:32 +02:00
|
|
|
fn send(&mut self, b: u8) -> Result<()> {
|
|
|
|
nb::block!(self.tx.write(b))
|
|
|
|
.map_err(|_| ModemError::CommandError(format!("error writing {} to serial", b)))?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn send_bytes(&mut self, payload: &[u8]) -> Result<()> {
|
|
|
|
for b in payload.iter() {
|
|
|
|
self.send(*b)?;
|
2022-06-16 01:06:32 +02:00
|
|
|
}
|
2022-06-19 00:33:32 +02:00
|
|
|
self.send('\r' as u8)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn send_command(&mut self, cmd: Command) -> Result<String> {
|
|
|
|
println!("-----------------------------------------------------------");
|
|
|
|
println!("Sending {} ...", cmd.text);
|
|
|
|
let _ = self.send_bytes(cmd.text.as_bytes())?;
|
2022-06-18 01:39:38 +02:00
|
|
|
self.read_response(cmd.contains.clone(), cmd.timeout.clone())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_ip_addr(&mut self) -> Result<String> {
|
2022-06-19 00:33:32 +02:00
|
|
|
self.send_command(Command::getbear())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_local_ip_addr(&mut self) -> Result<()> {
|
|
|
|
let _ = self.send_command(Command::get_local_ip_addr())?;
|
|
|
|
Ok(())
|
2022-06-13 00:55:08 +02:00
|
|
|
}
|
|
|
|
|
2022-06-16 01:06:32 +02:00
|
|
|
pub fn connect_to_gprs_ap(&mut self, apn: &str, username: &str, password: &str)-> Result<()> {
|
2022-06-18 01:39:38 +02:00
|
|
|
println!("init gprs ...");
|
2022-06-19 00:33:32 +02:00
|
|
|
let _ = self.send_command(Command::gprs_init())?;
|
2022-06-18 01:39:38 +02:00
|
|
|
|
2022-06-19 00:33:32 +02:00
|
|
|
println!("setting up gprs credentials for apn {}, {}:{})", apn, username, password);
|
2022-06-16 01:06:32 +02:00
|
|
|
|
2022-06-19 00:33:32 +02:00
|
|
|
let _ = self.send_command(Command::gprs_set_apn(apn))?;
|
|
|
|
let _ = self.send_command(Command::gprs_set_user(username))?;
|
|
|
|
let _ = self.send_command(Command::gprs_set_pwd(password))?;
|
2022-06-18 01:39:38 +02:00
|
|
|
|
|
|
|
println!("open gprs ...");
|
2022-06-19 00:33:32 +02:00
|
|
|
let _ = self.send_command(Command::gprs_open())?;
|
2022-06-16 01:06:32 +02:00
|
|
|
|
|
|
|
Ok(())
|
2022-06-13 00:55:08 +02:00
|
|
|
}
|
|
|
|
|
2022-06-18 01:39:38 +02:00
|
|
|
pub fn info(&mut self)-> Result<String> {
|
2022-06-19 00:33:32 +02:00
|
|
|
self.send_command(Command::modem_info())
|
2022-06-11 16:49:16 +02:00
|
|
|
}
|
2022-06-18 01:39:38 +02:00
|
|
|
|
|
|
|
pub fn probe(&mut self)-> Result<String> {
|
|
|
|
self.send_command(Command::probe())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_gprs_attached(&mut self)-> Result<bool> {
|
|
|
|
let res = self.send_command(Command::is_gprs_attached())?;
|
|
|
|
Ok(res.contains("+CGATT: 1"))
|
|
|
|
}
|
2022-06-16 01:06:32 +02:00
|
|
|
|
2022-06-19 00:33:32 +02:00
|
|
|
pub fn ping(&mut self, domain: &str)-> Result<()> {
|
|
|
|
self.send_command(Command::ping(domain))?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn tcp_is_ssl_enabled(&mut self) -> Result<bool> {
|
|
|
|
let res = self.send_command(Command::tcp_ssl_check())?;
|
|
|
|
Ok(res.contains("+CIPSSL: (1)"))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn tcp_ssl_disable(&mut self) -> Result<()> {
|
|
|
|
let _ = self.send_command(Command::tcp_ssl_disable())?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn tcp_connect(&mut self, addr: &str, port: u16) -> Result<()> {
|
|
|
|
self.send_command(Command::tcp_connect(addr, port))?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn tcp_set_quick_mode(&mut self, mode: bool) -> Result<()> {
|
|
|
|
self.send_command(Command::tcp_set_quick_mode(mode))?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn tcp_send(&mut self, payload: &str) -> Result<()> {
|
|
|
|
self.send_command(Command::tcp_send(payload.len()))?;
|
|
|
|
self.send_command(Command::tcp_write(payload))?;
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-06-16 01:06:32 +02:00
|
|
|
}
|