e-bike-tracker-device/src/modem.rs

111 lines
3.7 KiB
Rust
Raw Normal View History

2022-06-13 00:55:08 +02:00
use std::fmt::Write;
use std::thread;
use std::time::Duration;
2022-06-11 16:49:16 +02:00
2022-06-13 00:55:08 +02:00
use embedded_hal::digital::v2::OutputPin;
use embedded_hal::serial::Read;
use esp_idf_hal::serial::{self, Rx, Tx};
pub fn init(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_owned()))?;
rst.set_high().map_err(|_| ModemError::SetupError("Error setting RST to high.".to_owned()))?;
// 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_owned()))?;
thread::sleep(Duration::from_millis(100));
pwrkey.set_low().map_err(|_| ModemError::SetupError("Error setting PWRKEY to low.".to_owned()))?;
thread::sleep(Duration::from_millis(1000));
pwrkey.set_high().map_err(|_| ModemError::SetupError("Error setting PWRKEY to high.".to_owned()))?;
println!("Waiting 5s for sim module to come online ...");
thread::sleep(Duration::from_millis(5000));
Ok(())
}
pub struct Modem<UART: serial::Uart> {
2022-06-11 16:49:16 +02:00
is_connected: bool,
2022-06-13 00:55:08 +02:00
rx: Rx<UART>,
tx: Tx<UART>,
2022-06-11 16:49:16 +02:00
}
2022-06-13 00:55:08 +02:00
#[derive(Debug)]
pub enum ModemError {
GprsAPConnectionError(String),
ATCommandError(String),
CommandError(String),
2022-06-13 00:55:08 +02:00
SetupError(String),
}
2022-06-11 16:49:16 +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
}
}
pub type Result<T> = std::result::Result<T, ModemError>;
2022-06-11 16:49:16 +02:00
2022-06-13 00:55:08 +02:00
impl<UART: serial::Uart> Modem<UART> {
pub fn new(tx: Tx<UART>, rx: Rx<UART>) -> Self {
2022-06-11 16:49:16 +02:00
Self {
is_connected: false,
rx,
tx,
}
}
fn at_command(&mut self, cmd: &str) -> Result<String> {
let mut msg = "AT+".to_owned();
msg.push_str(cmd);
2022-06-13 00:55:08 +02:00
self.send_command(&msg)
.map_err(|err| ModemError::ATCommandError(format!("{}", err)))
}
2022-06-13 00:55:08 +02:00
fn read_response(&mut self) -> Result<String> {
let len = self.rx.count()
.map_err(|_| ModemError::CommandError("Error getting RX fifo length".to_owned()))?;
if cfg!(debug_assertions) {
println!("Reading {} bytes from serial RX ...", len);
}
let mut response = String::new();
2022-06-13 00:55:08 +02:00
for _ in 0..len {
nb::block!(self.rx.read())
.map(|b| response.push(b as char))
.map_err(|_| ModemError::CommandError("Error reading from RX".to_owned()))?;
}
response = response.trim().to_owned();
if cfg!(debug_assertions) {
2022-06-13 00:55:08 +02:00
println!("Received: {}", response);
2022-06-11 16:49:16 +02:00
}
Ok(response)
2022-06-11 16:49:16 +02:00
}
2022-06-13 00:55:08 +02:00
fn send_command(&mut self, cmd: &str) -> Result<String> {
writeln!(self.tx, "{}", cmd)
.map_err(|_| ModemError::CommandError("error writing to serial".to_owned()))?;
self.read_response()
}
pub fn connect_to_gprs_ap(&mut self, apn: &str, username: &str, password: &str)-> Result<String> {
println!("connecting to {} with {}:{}", apn, username, password);
self.at_command("CGATTCGATT=1")
2022-06-13 00:55:08 +02:00
.map(|response| {
println!("connected to {} with response: {}", apn, response);
self.is_connected = true;
2022-06-13 00:55:08 +02:00
response
})
.map_err(|err| ModemError::GprsAPConnectionError(format!("{}", err)))
}
pub fn test_modem(&mut self)-> Result<String> {
println!("testing modem with AP command");
self.at_command("AT")
.map(|response| {
println!("modem responded with: {}", response);
response
})
.map_err(|err| ModemError::GprsAPConnectionError(format!("{}", err)))
2022-06-11 16:49:16 +02:00
}
2022-06-10 01:32:52 +02:00
}