use crate::l2cap_classic::ClassicL2capChannel;
use futures_core::Stream;
use webbluetooth_core::{PanControlMessage, PanFrame, Result};
pub struct PanSession {
channel: ClassicL2capChannel,
}
impl PanSession {
pub fn new(channel: ClassicL2capChannel) -> Self {
Self { channel }
}
pub fn send(&self, frame: &PanFrame) -> Result<()> {
let bytes = frame
.encode()
.map_err(|error| webbluetooth_core::Error::InvalidModification(error.to_string()))?;
self.channel.send(&bytes)
}
pub fn send_control_message(&self, message: &PanControlMessage) -> Result<()> {
let mut bytes = vec![0x10];
bytes.extend(
message.encode().map_err(|error| {
webbluetooth_core::Error::InvalidModification(error.to_string())
})?,
);
self.channel.send(&bytes)
}
pub fn parse_control_message(&self, bytes: &[u8]) -> Result<PanControlMessage> {
if bytes.first().copied() != Some(0x10) {
return Err(webbluetooth_core::Error::InvalidModification(
"not a BNEP control packet".into(),
));
}
PanControlMessage::decode(&bytes[1..])
.map_err(|error| webbluetooth_core::Error::InvalidModification(error.to_string()))
}
pub fn take_incoming(&self) -> Option<impl Stream<Item = Vec<u8>>> {
self.channel.take_incoming()
}
pub fn channel(&self) -> &ClassicL2capChannel {
&self.channel
}
pub fn close(&self) {
self.channel.close();
}
}