implement modem on a Read/Write structure (rx, tx)
This commit is contained in:
parent
fbe132295e
commit
e3c3aa7391
2 changed files with 81 additions and 21 deletions
31
src/main.rs
31
src/main.rs
|
@ -1,10 +1,35 @@
|
||||||
mod modem;
|
mod modem;
|
||||||
|
|
||||||
|
use std;
|
||||||
|
use std::io::{BufReader, BufWriter, Cursor};
|
||||||
|
use std::io::{Read, Write};
|
||||||
use esp_idf_sys as _; // If using the `binstart` feature of `esp-idf-sys`, always keep this module imported
|
use esp_idf_sys as _; // If using the `binstart` feature of `esp-idf-sys`, always keep this module imported
|
||||||
|
|
||||||
|
fn start_loop<R: Read, W: Write>(_m: &mut modem::Modem<R, W>) -> () {
|
||||||
|
println!("Starting rx/tx loop (not implemented yet) ...");
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Ap<'a> {
|
||||||
|
apn: &'a str,
|
||||||
|
username: &'a str,
|
||||||
|
password: &'a str,
|
||||||
|
}
|
||||||
|
|
||||||
|
const A1: Ap = Ap {
|
||||||
|
apn: "internet",
|
||||||
|
username: "internet",
|
||||||
|
password: "internet",
|
||||||
|
};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Starting GPRS ...");
|
println!("Starting GPRS ...");
|
||||||
let sim800l = modem::Modem::new();
|
|
||||||
sim800l.connect_to_gprs_ap("internet", "internet", "internet");
|
let rx = Cursor::new(Vec::new());
|
||||||
println!("GPRS started ...");
|
let tx = Cursor::new(Vec::new());
|
||||||
|
let mut sim800l = modem::Modem::new(BufReader::new(rx), BufWriter::new(tx));
|
||||||
|
|
||||||
|
match sim800l.connect_to_gprs_ap(A1.apn, A1.username, A1.password) {
|
||||||
|
Ok(_) => start_loop(&mut sim800l),
|
||||||
|
Err(e) => println!("{}", e),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
71
src/modem.rs
71
src/modem.rs
|
@ -1,29 +1,64 @@
|
||||||
use std;
|
use std;
|
||||||
|
use std::io::{Read, Write};
|
||||||
|
|
||||||
pub struct Modem {
|
pub struct Modem<R: Read, W: Write> {
|
||||||
is_connected: bool,
|
is_connected: bool,
|
||||||
|
rx: R,
|
||||||
|
tx: W,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
pub enum ModemError {
|
||||||
pub struct ModemConnectionError;
|
GprsAPConnectionError(String),
|
||||||
|
ATCommandError(String),
|
||||||
|
CommandError(String),
|
||||||
|
}
|
||||||
|
|
||||||
impl std::fmt::Display for ModemConnectionError {
|
impl std::fmt::Display for ModemError {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||||
write!(f, "gprs connection error")
|
match self {
|
||||||
}
|
Self::GprsAPConnectionError(msg) => write!(f, "gprs connection error {}", msg),
|
||||||
}
|
Self::ATCommandError(msg) => write!(f, "AT command error {}", msg),
|
||||||
|
Self::CommandError(msg) => write!(f, "Modem command error {}", msg),
|
||||||
type ModemConnection = Result<Modem, ModemConnectionError>;
|
|
||||||
|
|
||||||
impl Modem {
|
|
||||||
pub fn new() -> Self {
|
|
||||||
Self {
|
|
||||||
is_connected: false,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn connect_to_gprs_ap(mut self, apn: &str, username: &str, password: &str)-> ModemConnection {
|
}
|
||||||
// TODO: set AT command for connecting to gprs
|
|
||||||
self.is_connected = true;
|
pub type Result<T> = std::result::Result<T, ModemError>;
|
||||||
Ok(self)
|
|
||||||
|
impl<R: Read, W: Write> Modem<R, W> {
|
||||||
|
pub fn new(rx: R, tx: W) -> Self {
|
||||||
|
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);
|
||||||
|
self.send_command(msg.to_owned())
|
||||||
|
.map_err(|err| ModemError::ATCommandError(format!("{}", err)))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn send_command(&mut self, cmd: String) -> Result<String> {
|
||||||
|
self.tx.write(cmd.as_bytes())
|
||||||
|
.map_err(|err| ModemError::CommandError(format!("{}", err)))?;
|
||||||
|
let mut response = String::new();
|
||||||
|
self.rx.read_to_string(&mut response)
|
||||||
|
.map_err(|err| ModemError::CommandError(format!("{}", err)))?;
|
||||||
|
if cfg!(debug_assertions) {
|
||||||
|
println!("{} = {}", cmd, response);
|
||||||
|
}
|
||||||
|
Ok(response)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn connect_to_gprs_ap(&mut self, apn: &str, username: &str, password: &str)-> Result<()> {
|
||||||
|
self.at_command("CGATTCGATT=1")
|
||||||
|
.map(|_| {
|
||||||
|
println!("connecting to {} with {}:{}", apn, username, password);
|
||||||
|
self.is_connected = true;
|
||||||
|
})
|
||||||
|
.map_err(|err| ModemError::GprsAPConnectionError(format!("{}", err)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue