use crate::rfcomm::RfcommChannel;
use futures_core::Stream;
use webbluetooth_core::{parse_obex_header, ObexHeader, ObexPacket, Result};
pub struct ObexSession {
channel: RfcommChannel,
}
impl ObexSession {
pub fn new(channel: RfcommChannel) -> Self {
Self { channel }
}
pub fn send(&self, packet: &ObexPacket) -> Result<()> {
self.channel.send(packet.as_bytes())
}
pub fn connect(&self, version: u8, flags: u8, max_packet: u16) -> Result<()> {
self.send(&ObexPacket::connect(version, flags, max_packet))
}
pub fn get(&self, body: &[u8]) -> Result<()> {
let packet = ObexPacket::get(body)
.map_err(|error| webbluetooth_core::Error::InvalidModification(error.to_string()))?;
self.send(&packet)
}
pub fn put(&self, body: &[u8]) -> Result<()> {
let packet = ObexPacket::put(body)
.map_err(|error| webbluetooth_core::Error::InvalidModification(error.to_string()))?;
self.send(&packet)
}
pub fn disconnect(&self) -> Result<()> {
self.send(&ObexPacket::disconnect())
}
pub fn parse_response_header(&self, bytes: &[u8]) -> Result<ObexHeader> {
parse_obex_header(bytes)
.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) -> &RfcommChannel {
&self.channel
}
pub fn close(&self) {
self.channel.close();
}
}