use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum CallState {
#[default]
Idle,
Incoming,
Outgoing,
Connected,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AtLine {
Command(String),
Ok,
Error,
Ring,
CallState(CallState),
CurrentCall(CallInfo),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CallInfo {
pub index: u8,
pub outgoing: bool,
pub status: u8,
pub multiparty: bool,
pub number: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CallCommand {
Dial(String),
Answer,
HangUp,
QueryCurrentCalls,
}
impl CallCommand {
pub fn encode(&self) -> Result<Vec<u8>, AtError> {
match self {
Self::Dial(number) => encode_command(&format!("ATD{number};")),
Self::Answer => encode_command("ATA"),
Self::HangUp => encode_command("AT+CHUP"),
Self::QueryCurrentCalls => encode_command("AT+CLCC"),
}
}
}
#[derive(Debug, Default)]
pub struct AtParser {
buffer: Vec<u8>,
call_state: CallState,
}
impl AtParser {
pub fn feed(&mut self, bytes: &[u8]) -> Vec<AtLine> {
self.buffer.extend_from_slice(bytes);
let mut lines = Vec::new();
while let Some(position) = self.buffer.iter().position(|&byte| byte == b'\n') {
let line: Vec<u8> = self.buffer.drain(..=position).collect();
let text = String::from_utf8_lossy(&line).trim().to_owned();
if !text.is_empty() {
lines.push(self.parse_line(&text));
}
}
lines
}
pub fn call_state(&self) -> CallState {
self.call_state
}
fn parse_line(&mut self, line: &str) -> AtLine {
match line {
"OK" => AtLine::Ok,
"ERROR" => AtLine::Error,
"RING" => {
self.call_state = CallState::Incoming;
AtLine::Ring
}
value if value.starts_with("+CIEV:") => {
let state = value
.split(',')
.nth(1)
.and_then(|part| part.trim().parse::<u8>().ok())
.map(|state| match state {
0 => CallState::Idle,
1 => CallState::Incoming,
2 => CallState::Outgoing,
_ => CallState::Connected,
})
.unwrap_or(self.call_state);
self.call_state = state;
AtLine::CallState(state)
}
value if value.starts_with("+CLCC:") => {
let fields: Vec<_> = value[6..].trim().split(',').collect();
if fields.len() < 5 {
return AtLine::Command(value.to_owned());
}
let parsed = fields[0]
.parse::<u8>()
.ok()
.zip(fields[1].parse::<u8>().ok())
.zip(fields[2].parse::<u8>().ok())
.zip(fields[3].parse::<u8>().ok())
.map(|(((index, direction), status), multiparty)| CallInfo {
index,
outgoing: direction == 0,
status,
multiparty: multiparty != 0,
number: fields
.get(5)
.map(|number| number.trim_matches('"').to_owned()),
});
parsed
.map(AtLine::CurrentCall)
.unwrap_or_else(|| AtLine::Command(value.to_owned()))
}
value => AtLine::Command(value.to_owned()),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AtError {
InvalidCommand,
}
impl fmt::Display for AtError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("invalid HFP/HSP AT command")
}
}
impl std::error::Error for AtError {}
pub fn encode_command(command: &str) -> Result<Vec<u8>, AtError> {
if command.contains('\r') || command.contains('\n') {
return Err(AtError::InvalidCommand);
}
let mut bytes = command.as_bytes().to_vec();
bytes.extend_from_slice(b"\r\n");
Ok(bytes)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parses_fragmented_ring_and_call_state() {
let mut parser = AtParser::default();
assert!(parser.feed(b"RI").is_empty());
assert_eq!(
parser.feed(b"NG\r\n+CIEV: 2,3\r\n"),
vec![AtLine::Ring, AtLine::CallState(CallState::Connected)]
);
assert_eq!(parser.call_state(), CallState::Connected);
let call = parser.feed(b"+CLCC: 1,0,0,0,0,\"555\"\r\n");
assert_eq!(
call[0],
AtLine::CurrentCall(CallInfo {
index: 1,
outgoing: true,
status: 0,
multiparty: false,
number: Some("555".into()),
})
);
}
#[test]
fn encodes_and_validates_commands() {
assert_eq!(encode_command("AT+CHUP").unwrap(), b"AT+CHUP\r\n");
assert_eq!(encode_command("AT\n").unwrap_err(), AtError::InvalidCommand);
assert_eq!(CallCommand::Answer.encode().unwrap(), b"ATA\r\n");
assert_eq!(CallCommand::HangUp.encode().unwrap(), b"AT+CHUP\r\n");
}
}