use crate::mux::{self, Frame, Reader, DISC, DM, PF, SABM, UA, UIH};
use crate::WAIT;
use std::collections::{HashMap, HashSet};
use std::sync::{Arc, Condvar, Mutex, MutexGuard};
use std::time::Instant;
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct Command {
pub dlci: Option<u8>,
pub line: String,
}
#[derive(Debug, Default)]
struct Seen {
commands: Vec<Command>,
frames: Vec<Frame>,
pdus: Vec<String>,
cld_before_ua: bool,
}
#[derive(Debug, Clone, Default)]
pub struct ModemLog(Arc<(Mutex<Seen>, Condvar)>);
impl ModemLog {
fn lock(&self) -> MutexGuard<'_, Seen> {
self.0 .0.lock().unwrap_or_else(|e| e.into_inner())
}
fn note(&self, f: impl FnOnce(&mut Seen)) {
f(&mut self.lock());
self.0 .1.notify_all();
}
fn wait<T>(&self, what: &str, found: impl Fn(&Seen) -> Option<T>) -> T {
let deadline = Instant::now() + WAIT;
let mut seen = self.lock();
loop {
if let Some(found) = found(&seen) {
return found;
}
let left = deadline.saturating_duration_since(Instant::now());
if left.is_zero() {
drop(seen);
panic!("never: {what}");
}
let woken = self.0 .1.wait_timeout(seen, left);
seen = woken.unwrap_or_else(|e| e.into_inner()).0;
}
}
pub fn commands(&self) -> Vec<Command> {
self.lock().commands.clone()
}
pub fn wait_command(&self, line: &str) -> Command {
self.wait(&format!("the modem read {line}"), |s| {
let found = |c: &&Command| c.line.eq_ignore_ascii_case(line);
s.commands.iter().find(found).cloned()
})
}
pub fn frames(&self) -> Vec<Frame> {
self.lock().frames.clone()
}
pub fn wait_frame(&self, what: &str, pred: impl Fn(&Frame) -> bool) -> Frame {
self.wait(what, |s| s.frames.iter().find(|f| pred(f)).cloned())
}
pub fn pdus(&self) -> Vec<String> {
self.lock().pdus.clone()
}
pub fn cld_before_ua(&self) -> bool {
self.lock().cld_before_ua
}
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct MuxOptions {
pub answer_cld: bool,
pub deaf: HashSet<u8>,
pub refuse: HashSet<u8>,
pub lost_first_frames: usize,
}
impl Default for MuxOptions {
fn default() -> Self {
Self {
answer_cld: true,
deaf: HashSet::new(),
refuse: HashSet::new(),
lost_first_frames: 0,
}
}
}
impl MuxOptions {
pub fn answer_cld(mut self, on: bool) -> Self {
self.answer_cld = on;
self
}
pub fn deaf(mut self, dlcis: impl IntoIterator<Item = u8>) -> Self {
self.deaf.extend(dlcis);
self
}
pub fn refuse(mut self, dlcis: impl IntoIterator<Item = u8>) -> Self {
self.refuse.extend(dlcis);
self
}
pub fn lost_first_frames(mut self, n: usize) -> Self {
self.lost_first_frames = n;
self
}
}
#[derive(Debug, Clone)]
enum Reply {
Lines(Vec<String>, String),
Raw(Vec<u8>),
Silent,
}
#[derive(Debug)]
pub struct Modem {
replies: HashMap<String, Reply>,
echo: bool,
numeric: bool,
mux: Option<MuxOptions>,
log: ModemLog,
session: Option<Session>,
lines: HashMap<Option<u8>, Vec<u8>>,
prompts: HashSet<Option<u8>>,
}
#[derive(Debug)]
struct Session {
reader: Reader,
lost: usize,
open: HashSet<u8>,
}
impl Default for Modem {
fn default() -> Self {
Self::new()
}
}
impl Modem {
pub fn new() -> Self {
let modem = Self {
replies: HashMap::new(),
echo: false,
numeric: false,
mux: None,
log: ModemLog::default(),
session: None,
lines: HashMap::new(),
prompts: HashSet::new(),
};
modem.reply("AT", &[]).reply("AT+CSQ", &["+CSQ: 20,99"])
}
fn on(mut self, command: &str, reply: Reply) -> Self {
self.replies.insert(command.to_ascii_uppercase(), reply);
self
}
pub fn reply(self, command: &str, lines: &[&str]) -> Self {
let lines = lines.iter().map(|l| l.to_string()).collect();
self.on(command, Reply::Lines(lines, "OK".into()))
}
pub fn fails(self, command: &str, result: &str) -> Self {
self.on(command, Reply::Lines(Vec::new(), result.into()))
}
pub fn raw(self, command: &str, bytes: &[u8]) -> Self {
self.on(command, Reply::Raw(bytes.to_vec()))
}
pub fn silent(self, command: &str) -> Self {
self.on(command, Reply::Silent)
}
pub fn echo(mut self) -> Self {
self.echo = true;
self
}
pub fn numeric(mut self) -> Self {
self.numeric = true;
self
}
pub fn mux(mut self, options: MuxOptions) -> Self {
self.mux = Some(options);
self
}
pub fn log(&self) -> ModemLog {
self.log.clone()
}
pub fn feed(&mut self, bytes: &[u8]) -> Vec<u8> {
self.feed_with_unread(bytes, 0)
}
pub(crate) fn feed_with_unread(&mut self, bytes: &[u8], unread: usize) -> Vec<u8> {
let mut out = Vec::new();
let mut rest = bytes.to_vec();
while !rest.is_empty() {
match &mut self.session {
None => {
let (reply, taken) = self.terminal(None, &rest);
out.extend(reply);
rest.drain(..taken);
}
Some(session) => {
let mut reader = std::mem::take(&mut session.reader);
reader.push(&std::mem::take(&mut rest));
while self.session.is_some() {
let Some(frame) = reader.next_frame() else {
break;
};
out.extend(self.frame(frame, unread));
}
match &mut self.session {
Some(session) => session.reader = reader,
None => rest = reader.into_rest(),
}
}
}
}
out
}
fn frame(&mut self, f: Frame, unread: usize) -> Vec<u8> {
let (Some(options), Some(session)) = (&self.mux, &mut self.session) else {
return Vec::new();
};
if session.lost > 0 {
session.lost -= 1;
return Vec::new();
}
self.log.note(|s| s.frames.push(f.clone()));
let ua = mux::frame(f.dlci, true, UA | PF, &[]);
let dm = mux::frame(f.dlci, true, DM | PF, &[]);
match f.kind {
SABM if options.deaf.contains(&f.dlci) => Vec::new(),
SABM if options.refuse.contains(&f.dlci) => dm,
SABM => {
session.open.insert(f.dlci);
ua
}
UA | DM => Vec::new(),
_ if !session.open.contains(&f.dlci) => dm,
DISC => {
session.open.remove(&f.dlci);
self.forget_line(f.dlci);
if f.dlci == 0 {
self.session = None;
}
ua
}
UIH if f.dlci == 0 && f.payload.first() == Some(&mux::CLD) => {
if unread > 0 {
self.log.note(|s| s.cld_before_ua = true);
}
if !options.answer_cld {
return Vec::new();
}
self.session = None;
mux::control(mux::CLD & !2, &[])
}
UIH if f.dlci != 0 => {
let (reply, _) = self.terminal(Some(f.dlci), &f.payload);
reply
.chunks(31)
.flat_map(|c| mux::data(f.dlci, c))
.collect()
}
_ => Vec::new(),
}
}
fn forget_line(&mut self, dlci: u8) {
self.lines.remove(&Some(dlci));
self.prompts.remove(&Some(dlci));
}
fn terminal(&mut self, channel: Option<u8>, bytes: &[u8]) -> (Vec<u8>, usize) {
let mut out = Vec::new();
for (i, &b) in bytes.iter().enumerate() {
if self.prompts.contains(&channel) {
match b {
0x1A => {
self.prompts.remove(&channel);
let pdu = std::mem::take(self.lines.entry(channel).or_default());
let pdu = String::from_utf8_lossy(&pdu).trim().to_string();
self.log.note(|s| s.pdus.push(pdu));
out.extend(self.lines_reply(&["+CMGS: 7".into()], "OK"));
}
0x1B => {
self.prompts.remove(&channel);
self.lines.remove(&channel);
out.extend(self.lines_reply(&[], "OK"));
}
_ => self.lines.entry(channel).or_default().push(b),
}
continue;
}
if b != b'\r' {
self.lines.entry(channel).or_default().push(b);
continue;
}
let line = std::mem::take(self.lines.entry(channel).or_default());
let command = String::from_utf8_lossy(&line).trim().to_string();
if self.echo {
out.extend(&line);
out.push(b'\r');
}
self.log.note(|s| {
s.commands.push(Command {
dlci: channel,
line: command.clone(),
})
});
out.extend(self.answer(channel, &command));
if channel.is_none() && self.session.is_some() {
return (out, i + 1);
}
}
(out, bytes.len())
}
fn answer(&mut self, channel: Option<u8>, command: &str) -> Vec<u8> {
let command = command.to_ascii_uppercase();
if let Some(reply) = self.replies.get(&command).cloned() {
return match reply {
Reply::Lines(lines, result) => self.lines_reply(&lines, &result),
Reply::Raw(bytes) => bytes,
Reply::Silent => Vec::new(),
};
}
if command.starts_with("AT+CMUX=") && channel.is_none() {
if let Some(options) = &self.mux {
self.lines.retain(|channel, _| channel.is_none());
self.prompts.clear();
self.session = Some(Session {
reader: Reader::default(),
lost: options.lost_first_frames,
open: HashSet::new(),
});
return self.lines_reply(&[], "OK");
}
}
if command.starts_with("AT+CMGS=") {
self.prompts.insert(channel);
return b"\r\n> ".to_vec();
}
self.lines_reply(&[], "ERROR")
}
fn lines_reply(&self, lines: &[String], result: &str) -> Vec<u8> {
let body: String = lines
.iter()
.map(|l| {
if self.numeric {
format!("{l}\r\n")
} else {
format!("\r\n{l}\r\n")
}
})
.collect();
let result = match (self.numeric, result) {
(true, "OK") => "0\r".to_string(),
(true, "ERROR") => "4\r".to_string(),
_ => format!("\r\n{result}\r\n"),
};
(body + &result).into_bytes()
}
}