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

569 lines
21 KiB
Rust
Raw Normal View History

use crate::command::Command;
use crate::serial::SerialIO;
use crate::types::*;
2022-07-06 20:33:43 +02:00
use anyhow;
use std::{
error::Error,
io::{Read, Write},
thread,
2022-11-29 15:19:42 +01:00
time::Duration,
sync::mpsc::Receiver,
};
2022-07-06 20:33:43 +02:00
use esp_idf_hal::prelude::*;
use esp_idf_hal::serial::{self, Rx, Tx};
2022-06-11 16:49:16 +02:00
2022-06-13 00:55:08 +02:00
use embedded_hal::digital::v2::OutputPin;
2022-07-06 20:33:43 +02:00
use mqtt::packet::{ConnectPacket, PublishPacket, QoSWithPacketIdentifier, VariablePacket};
use mqtt::{Encodable, Decodable, TopicName};
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> {
serial: SerialIO<UART>,
2022-06-11 16:49:16 +02:00
}
2022-06-13 00:55:08 +02:00
#[derive(Debug)]
pub enum ModemError {
CommandError(String),
2022-06-13 00:55:08 +02:00
SetupError(String),
2022-11-26 18:59:33 +01:00
SendDataError(String),
ReadError(String),
2022-06-18 01:39:38 +02:00
TimeoutError,
}
2022-06-11 16:49:16 +02:00
2022-06-16 01:39:16 +02:00
impl Error for ModemError {}
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
}
}
impl<UART: serial::Uart> Modem<UART> {
pub fn new(tx: Tx<UART>, rx: Rx<UART>) -> Self {
Self {
serial: SerialIO::new(tx, rx),
}
2022-06-18 01:39:38 +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 for sim module to come online ...");
thread::sleep(Duration::from_millis(3000));
loop {
match self.send_command(Command::probe()) {
Ok(_) => break,
_ => {
thread::sleep(Duration::from_millis(2000));
continue
},
}
}
Ok(())
2022-06-18 01:39:38 +02:00
}
2022-07-04 17:04:09 +02:00
/// Reads the serial RX until the `contains` string is encoutered if `contains` is Some(s), if
/// None, then the first line is returned. If a timeout is reached. The timeout is provided on
/// input via the `timeout` argument. The first argument `contains` is checked against every
/// line in the response.
fn command_read_response(&mut self, contains: Option<String>) -> Result<String> {
let mut response = String::new();
2022-06-18 01:39:38 +02:00
loop {
2022-11-29 15:19:42 +01:00
let mut buf = vec![0; 1024];
let num_bytes = self.serial
.read(buf.as_mut_slice())
.map_err(|err| ModemError::ReadError(format!("Error in serial.read(buf) ({:?})", err)))?;
2022-11-29 15:19:42 +01:00
response.push_str(std::str::from_utf8(&buf[0..num_bytes])
.map_err(|err| ModemError::ReadError(format!("Error in str::from_utf8 ({:?})", err)))?);
2022-11-29 15:19:42 +01:00
if num_bytes < buf.len() {
break
}
2022-06-18 01:39:38 +02:00
}
2022-11-29 15:19:42 +01:00
print!("Read {} bytes from serial: {}", response.len(), response);
if let Some(c) = contains {
if response.contains(&c) {
Ok(response)
} else {
Err(ModemError::CommandError(format!("Didn't get expected ({}) from modem.", c)))
}
} else {
Ok(response)
}
2022-06-11 16:49:16 +02:00
}
fn send_command(&mut self, cmd: Command) -> Result<String> {
println!("-----------------------------------------------------------");
println!("Sending {} ...", cmd.text);
2022-11-29 15:19:42 +01:00
let _ = self.serial
2022-11-29 15:19:42 +01:00
.write_bytes(cmd.text.as_bytes())
.map_err(|_| ModemError::SendDataError(format!("Error in send_command({})", cmd.text)))?;
let _ = self.serial
.write(&['\r' as u8])
.map_err(|_| ModemError::SendDataError(format!("Error in send_command({})", cmd.text)))?;
self.command_read_response(cmd.contains)
2022-11-29 15:19:42 +01:00
}
fn handle_prompt(&mut self) -> Result<()> {
let mut prompt_buf = vec![0; 256];
let prompt_len = self.serial.read(&mut prompt_buf)
.map_err(|err| ModemError::ReadError(format!("Error in handle_prompt() ({:?})", err)))?;
let prompt = String::from_utf8(prompt_buf[0..prompt_len].to_vec())
.unwrap_or("".to_string())
.trim()
.to_string();
println!("Prompt is: ({})", prompt);
2022-11-29 15:19:42 +01:00
if prompt != ">" {
let msg = format!("Prompt error, expected (>), got ({})", prompt);
2022-11-29 15:19:42 +01:00
Err(ModemError::SendDataError(msg))
} else {
Ok(())
}
}
2022-11-26 18:59:33 +01:00
fn tcp_manual_send_data(&mut self, buf: &[u8]) -> Result<String> {
println!("Sending AT+CIPSEND to serial TX!");
let _ = self.serial
.write("AT+CIPSEND\r".as_bytes())
2022-11-26 18:59:33 +01:00
.map_err(|_| ModemError::SendDataError("Error in tcp_manual_send_data ... AT_CIPSEND\\r".to_string()))?;
2022-11-29 15:19:42 +01:00
let _ = self.handle_prompt()?;
println!("Handled prompt OK!!");
println!("Writing bytes in serial TX! ({:?})", String::from_utf8(buf.to_vec()));
self.serial
2022-11-29 15:19:42 +01:00
.write_bytes(buf)
2022-11-26 18:59:33 +01:00
.map_err(|err| ModemError::SendDataError(format!("{:?}", err)))?;
2022-11-29 15:19:42 +01:00
self.serial
.write(&[26_u8]) // 26_u8 = Ctrl+z - to end sending data
.map_err(|err| ModemError::SendDataError(format!("{:?}", err)))?;
println!("DONE Writing bytes in serial TX!");
thread::sleep(Duration::from_millis(500));
println!("Reading bytes in serial RX!");
for _ in 0..3 {
let res = self.command_read_response(Some("SEND OK".into()));
if res.is_ok() {
return res;
}
thread::sleep(Duration::from_millis(1000))
}
Err(ModemError::ReadError(format!("ReadError: cannot read serial RX!")))
2022-06-18 01:39:38 +02:00
}
pub fn gprs_status(&mut self) -> Result<String> {
self.send_command(Command::gprs_bearer_status())
}
pub fn gprs_attach_ap(&mut self, apn: &str, username: &str, password: &str)-> Result<()> {
2022-06-18 01:39:38 +02:00
println!("init gprs ...");
let _ = self.send_command(Command::gprs_init())?;
2022-06-18 01:39:38 +02:00
println!("setting up gprs credentials for apn {}, {}:{})", apn, username, password);
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
Ok(())
}
pub fn gprs_connect(&mut self)-> Result<()> {
2022-06-18 01:39:38 +02:00
println!("open gprs ...");
2022-07-21 16:24:14 +02:00
self.send_command(Command::gprs_bearer_open())
.map(|_| ())
2022-06-13 00:55:08 +02:00
}
2022-06-18 01:39:38 +02:00
pub fn is_gprs_attached(&mut self)-> Result<bool> {
let res = self.send_command(Command::is_gprs_attached())?;
Ok(res.contains("+CGATT: 1"))
}
2022-11-26 18:59:33 +01:00
fn try_connect_gprs(&mut self) -> Result<()> {
let mut retries = 0;
loop {
if self.is_gprs_attached()? {
let _ = self.gprs_connect()?;
thread::sleep(Duration::from_millis(1000));
let ip_addr = self.gprs_status()?;
if ip_addr.contains("0.0.0.0") && retries < 5 {
thread::sleep(Duration::from_millis(1000));
retries += 1;
continue
} else if retries < 5 {
break Ok(())
} else {
break Err(ModemError::SetupError(format!("Cannot connect to GPRS after {} retries ... bailing!", retries)));
}
}
}
}
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<()> {
2022-07-21 16:24:14 +02:00
self.send_command(Command::tcp_ssl_set(false))
.map(|_| ())
}
pub fn tcp_ssl_enable(&mut self) -> Result<()> {
2022-07-21 16:24:14 +02:00
self.send_command(Command::tcp_ssl_set(true))
.map(|_| ())
}
pub fn tcp_connect(&mut self, addr: &str, port: u16) -> Result<()> {
let _ = self.send_command(Command::tcp_connect(addr, port))?;
for _ in 0..3 {
if let Ok(reply) = self.command_read_response(Some("CONNECT_OK".to_string())) {
println!("TCP connect replied with {}", reply);
break
}
thread::sleep(Duration::from_millis(500));
}
Ok(())
}
pub fn tcp_set_quick_mode(&mut self, mode: bool) -> Result<()> {
2022-07-21 16:24:14 +02:00
self.send_command(Command::tcp_set_quick_mode(mode))
.map(|_| ())
}
2022-07-04 17:04:09 +02:00
pub fn tcp_set_manual_receive(&mut self, is_manual: bool) -> Result<()> {
2022-07-21 16:24:14 +02:00
self.send_command(Command::tcp_set_manual_receive(is_manual))
.map(|_| ())
2022-06-21 02:30:00 +02:00
}
2022-11-26 18:59:33 +01:00
pub fn tcp_manual_send(&mut self, buf: &[u8]) -> Result<()> {
self.tcp_manual_send_data(buf)
2022-07-21 16:24:14 +02:00
.map(|_| ())
}
2022-06-21 02:30:00 +02:00
fn tcp_parse_response_size(&mut self, reply_line: &str) -> Result<usize> {
2022-06-22 19:35:01 +02:00
reply_line.split(',')
.into_iter()
.last()
.unwrap_or("x")
.parse::<usize>()
.map_err(|_| ModemError::CommandError(format!("response size should be a number, got {}", reply_line)))
}
pub fn tcp_receive_reply_len(&mut self) -> Result<usize> {
let reply = self.send_command(Command::tcp_receive_reply_len())?;
println!("Receiving TCP reply length!");
let res = reply.lines()
.filter(|line| line.contains("+CIPRXGET: 4"))
.next()
.ok_or(ModemError::CommandError("reply body missing :/".to_string()))
.and_then(|line| self.tcp_parse_response_size(line))
.map_err(|_| ModemError::CommandError(format!("received 0 elements from parsing")));
println!("Received ({:?})", res);
res
2022-06-22 19:35:01 +02:00
}
pub fn tcp_receive(&mut self, buf: &mut [u8]) -> Result<usize> {
let mut size = 0;
loop {
let reply = self.send_command(Command::tcp_receive(buf.len()))
2022-07-04 17:04:09 +02:00
.map(|reply| {
// TODO: parse the response properly
// 1. the first line is \r\n
// 2. next is the +CIPRXGET: 2,X,Y where X is the number of bytes read and Y is
// the number of bytes left to be read
// 3. immediately after this the payload is returned (with size X)
// 4. OK
2022-07-04 17:04:09 +02:00
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())
})?;
2022-07-04 17:04:09 +02:00
match reply {
Some(0) | None => {
break Ok(size)
},
Some(x) => {
size += x;
continue
},
}
}
2022-06-21 02:30:00 +02:00
}
pub fn tcp_close_connection(&mut self) -> Result<String> {
self.send_command(Command::tcp_close())
}
pub fn http_post(&mut self, url: &str, token: &str, content: &[u8]) -> Result<String> {
let _ = self.send_command(Command::http_init());
let _ = self.send_command(Command::http_set_cid());
2022-07-04 17:04:09 +02:00
let _ = self.send_command(Command::http_set_url(url));
let _ = self.send_command(Command::http_set_header("X-Secret", token));
let _ = self.send_command(Command::http_set_header("X-Topic", "device-dev"));
let _ = self.send_command(Command::http_set_content("application/json"));
2022-07-04 17:04:09 +02:00
let _ = self.send_command(Command::http_set_ssl(true));
let _ = self.send_command(Command::http_post_len(content.len(), 100000));
2022-11-29 15:19:42 +01:00
let _ = self.serial.write_bytes(content);
let _ = self.serial.write(&[26_u8]);
let _ = self.send_command(Command::http_post());
2022-07-04 17:04:09 +02:00
self.send_command(Command::http_read_response())
}
pub fn http_get(&mut self, url: &str) -> Result<String> {
let _ = self.send_command(Command::http_init());
let _ = self.send_command(Command::http_set_cid());
let _ = self.send_command(Command::http_set_url(url));
let _ = self.send_command(Command::http_set_redirect(true));
let _ = self.send_command(Command::http_set_ssl(true));
let _ = self.send_command(Command::http_get());
self.send_command(Command::http_read_response())
}
pub fn http_close(&mut self) -> Result<()> {
2022-07-21 16:24:14 +02:00
self.send_command(Command::http_close())
.map(|_| ())
}
pub fn chip_info(&mut self) -> Result<()> {
let _ = self.send_command(Command::manufacturer_id())?;
thread::sleep(Duration::from_millis(1000));
let _ = self.send_command(Command::model_id())?;
thread::sleep(Duration::from_millis(1000));
let _ = self.send_command(Command::release_id())?;
Ok(())
}
pub fn location(&mut self) -> Result<()> {
2022-07-21 16:24:14 +02:00
self.send_command(Command::get_location())
.map(|_| ())
}
pub fn ssl_opt(&mut self) -> Result<()> {
2022-07-21 16:24:14 +02:00
self.send_command(Command::ssl_opt())
.map(|_| ())
}
2022-07-04 17:04:09 +02:00
fn file_write(&mut self, buf: &[u8], path: &str, append: bool, input_time_sec: usize) -> Result<()> {
let cmd = Command::fs_file_write(path, append, buf.len(), input_time_sec);
let _ = self.serial
2022-11-29 15:19:42 +01:00
.write(cmd.text.as_bytes())
2022-11-26 18:59:33 +01:00
.map_err(|err| ModemError::SendDataError(format!("File write error ({:?})", err)))?;
2022-07-04 17:04:09 +02:00
2022-11-29 15:19:42 +01:00
let _ = self.handle_prompt()?;
2022-07-04 17:04:09 +02:00
self.serial
2022-11-29 15:19:42 +01:00
.write(buf)
2022-11-26 18:59:33 +01:00
.map_err(|err| ModemError::SendDataError(format!("Error sending bytes via serial ({:?})", err)))?;
let _ = self.command_read_response(None);
2022-07-04 17:04:09 +02:00
Ok(())
}
pub fn upload_cert(&mut self, path: &str, cert: &[u8]) -> Result<()> {
let _ = self.send_command(Command::fs_file_create(path))?;
let _ = self.file_write(cert, path, false, 20000)?;
Ok(())
}
pub fn fs_list(&mut self, path: &str) -> Result<()> {
2022-07-21 16:24:14 +02:00
self.send_command(Command::fs_list(path))
.map(|_| ())
2022-07-04 17:04:09 +02:00
}
pub fn fs_free_space(&mut self) -> Result<()> {
2022-07-21 16:24:14 +02:00
self.send_command(Command::fs_free_size())
.map(|_| ())
2022-07-04 17:04:09 +02:00
}
pub fn ssl_set_client_cert(&mut self, path: &str, password: &str) -> Result<()> {
2022-07-21 16:24:14 +02:00
self.send_command(Command::ssl_set_client_cert(path, password))
.map(|_| ())
2022-07-04 17:04:09 +02:00
}
pub fn ssl_set_root_cert(&mut self, path: &str, filesize: usize) -> Result<()> {
2022-07-21 16:24:14 +02:00
self.send_command(Command::ssl_set_root_cert(path, filesize))
.map(|_| ())
2022-07-04 17:04:09 +02:00
}
2022-07-06 20:33:43 +02:00
fn mqtt_receive_reply(&mut self) -> Result<VariablePacket> {
for _ in 0..3 {
let size = self.tcp_receive_reply_len()?;
println!("received reply len({}) ...", size);
if size == 0 {
println!("retrying ...");
continue
} else {
let mut reply = vec![0 as u8; size];
println!("receiving mqtt reply ...");
let _ = self.tcp_receive(&mut reply);
let reply = std::str::from_utf8(&reply).unwrap_or("");
println!("received mqtt reply ({})", reply);
return VariablePacket::decode(&mut reply.as_bytes())
.map_err(|err| ModemError::CommandError(format!("Undecodable MQTT message. ({:?})", err)));
}
}
Err(ModemError::ReadError("TCP server didn't respond!".into()))
2022-07-06 20:33:43 +02:00
}
fn mqtt_connect(&mut self, device_id: &str) -> anyhow::Result<()> {
2022-07-06 20:33:43 +02:00
let mut buf = Vec::new();
let mut conn = ConnectPacket::new(device_id);
conn.set_clean_session(true);
2022-11-26 18:59:33 +01:00
conn.set_keep_alive(100);
2022-07-06 20:33:43 +02:00
let _ = conn.encode(&mut buf)?;
self.tcp_manual_send(&mut buf).ok();
2022-07-06 20:33:43 +02:00
let reply = self.mqtt_receive_reply()?;
println!("mqtt decoded packet: ({:?})", reply);
2022-07-06 20:33:43 +02:00
match reply {
VariablePacket::ConnackPacket(_) => Ok(()),
_ => Err(anyhow::Error::msg("Invalid MQTT reply ... expected CONNACK!"))
}
2022-07-06 20:33:43 +02:00
}
fn mqtt_publish(&mut self, _device_id: &str, message: &str) -> anyhow::Result<()> {
2022-07-06 20:33:43 +02:00
println!("entered mqtt publish ...");
let mut buf = Vec::new();
let packet = PublishPacket::new(
TopicName::new(format!("bajsevi/location")).unwrap(),
QoSWithPacketIdentifier::Level0,
message.as_bytes(),
);
let _ = packet.encode(&mut buf)?;
println!("created mqtt publish packet ... ({})",
std::str::from_utf8(buf.as_slice()).unwrap_or(""));
self.tcp_manual_send(&mut buf).ok();
2022-07-06 20:33:43 +02:00
Ok(())
}
}
impl<UART: serial::Uart> std::io::Read for Modem<UART> {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
self.tcp_receive(buf).map_err(|_| std::io::Error::from(std::io::ErrorKind::ConnectionAborted))
}
}
2022-07-06 20:33:43 +02:00
pub fn main<T: Sync + Send>(
rx: esp_idf_hal::gpio::Gpio26<T>,
tx: esp_idf_hal::gpio::Gpio27<T>,
uart: serial::UART1,
pwrkey: esp_idf_hal::gpio::Gpio4<esp_idf_hal::gpio::Output>,
rst: esp_idf_hal::gpio::Gpio5<esp_idf_hal::gpio::Output>,
power: esp_idf_hal::gpio::Gpio23<esp_idf_hal::gpio::Output>,
receiver: Receiver<Msg>,
) -> std::result::Result<(), anyhow::Error> {
let serial_pins = serial::Pins {
tx,
rx,
cts: None,
rts: None,
};
let serial: serial::Serial<serial::UART1, _, _> = serial::Serial::new(
uart,
serial_pins,
serial::config::Config::default().baudrate(Hertz(115200)),
)?;
let (tx, rx) = serial.split();
let mut mdm = Modem::new(tx, rx);
mdm.init(pwrkey, rst, power)?;
2022-11-26 18:59:33 +01:00
// thread::sleep(Duration::from_millis(500));
2022-07-06 20:33:43 +02:00
//println!("setting up client TLS cert");
//let client_cert = include_bytes!("../certs/full-bin.p12");
//let client_cert_path = "C:\\USER\\fullchain.pem";
//let _ = mdm.upload_cert(client_cert_path, client_cert)?;
//let _ = mdm.ssl_set_client_cert(client_cert_path, "t")?;
//let _ = mdm.fs_list("C:\\USER\\")?;
2022-11-26 18:59:33 +01:00
loop {
if !mdm.is_gprs_attached()? {
let _ = mdm.gprs_attach_ap(
crate::config::A1_GPRS_AP.apn,
crate::config::A1_GPRS_AP.username,
crate::config::A1_GPRS_AP.password,
)?;
2022-07-06 20:33:43 +02:00
}
2022-11-26 18:59:33 +01:00
if let Ok(()) = mdm.try_connect_gprs() {
let device_id = "c36a72df-5bd6-4f9b-995d-059433bc3267";
let _ = mdm.tcp_set_quick_mode(false);
let _ = mdm.tcp_set_manual_receive(true);
let _ = mdm.tcp_connect("51.158.66.64", 7887)?;
let _ = mdm.mqtt_connect(device_id)?;
println!("entering queue receive loop ...");
let mut err_count = 0;
let _ = loop {
match receiver.recv() {
Ok(Msg::Gps(solution)) => {
println!("received GPS solution {:?} | sending to mqtt ...", solution);
let _ = mdm.mqtt_publish(device_id, &format!("{:?}", solution))?;
err_count = 0;
},
Ok(Msg::Accelerometer(acc)) => {
println!("received accel {} | sending to mqtt ...", acc);
let _ = mdm.mqtt_publish(device_id, &format!("{:?}", acc))?;
err_count = 0;
}
2022-11-26 18:59:33 +01:00
Err(e) => {
if err_count < 10 {
err_count += 1;
println!("received error {} | NOT sending to mqtt ...", e);
}
else {
break
}
}
}
2022-11-26 18:59:33 +01:00
};
2022-07-06 20:33:43 +02:00
2022-11-26 18:59:33 +01:00
let _ = mdm.tcp_close_connection()?;
}
2022-07-06 20:33:43 +02:00
}
}