use crate::rfcomm::RfcommChannel;
use futures_core::Stream;
use webbluetooth_core::{AtLine, AtParser, CallCommand, CallInfo, CallState, Result};
pub struct HfpSession {
channel: RfcommChannel,
parser: AtParser,
}
impl HfpSession {
pub fn new(channel: RfcommChannel) -> Self {
Self {
channel,
parser: AtParser::default(),
}
}
pub fn send_command(&self, command: &str) -> Result<()> {
let bytes = webbluetooth_core::encode_command(command)
.map_err(|error| webbluetooth_core::Error::InvalidModification(error.to_string()))?;
self.channel.send(&bytes)
}
pub fn send_call_command(&self, command: CallCommand) -> Result<()> {
self.channel.send(
&command.encode().map_err(|error| {
webbluetooth_core::Error::InvalidModification(error.to_string())
})?,
)
}
pub fn parse_bytes(&mut self, bytes: &[u8]) -> Vec<AtLine> {
self.parser.feed(bytes)
}
pub fn parse_call_list(&mut self, bytes: &[u8]) -> Vec<CallInfo> {
self.parse_bytes(bytes)
.into_iter()
.filter_map(|line| match line {
AtLine::CurrentCall(call) => Some(call),
_ => None,
})
.collect()
}
pub fn call_state(&self) -> CallState {
self.parser.call_state()
}
pub fn channel(&self) -> &RfcommChannel {
&self.channel
}
pub fn take_incoming(&self) -> Option<impl Stream<Item = Vec<u8>>> {
self.channel.take_incoming()
}
pub fn close(&self) {
self.channel.close();
}
}