use bytes::Bytes;
use crate::{
client::ObexError,
headers::Header,
packet::{OpCode, Packet, PacketExtra},
};
const OBEX_VERSION: u8 = 0x10;
const OBEX_FLAGS: u8 = 0x00;
const OBEX_MAX_PACKET: u16 = 0xFFFF;
pub struct ObexServer {
next_conn_id: u32,
}
impl ObexServer {
#[must_use]
pub const fn new() -> Self {
Self { next_conn_id: 1 }
}
pub fn handle_connect(
&mut self,
data: &[u8],
who_uuid: &[u8; 16],
) -> Result<(Packet, Bytes), ObexError> {
let packet = Packet::decode(data)?;
let conn_id = self.next_conn_id;
self.next_conn_id = self.next_conn_id.saturating_add(1);
let rsp = Packet {
opcode: OpCode::Ok,
extra: PacketExtra::Connect {
version: OBEX_VERSION,
flags: OBEX_FLAGS,
max_packet: OBEX_MAX_PACKET,
},
headers: vec![
Header::ConnectionId(conn_id),
Header::Who(Bytes::copy_from_slice(who_uuid)),
],
}
.encode()?;
Ok((packet, rsp))
}
pub fn handle_put(data: &[u8]) -> Result<(Option<Bytes>, Bytes), ObexError> {
let packet = Packet::decode(data)?;
let body = packet.body_payload().map(Bytes::copy_from_slice);
Ok((body, Self::ok_response()))
}
#[must_use]
pub const fn ok_response() -> Bytes {
Bytes::from_static(&[0xA0, 0x00, 0x03])
}
#[must_use]
pub const fn bad_request_response() -> Bytes {
Bytes::from_static(&[0xC0, 0x00, 0x03])
}
}
impl Default for ObexServer {
fn default() -> Self {
Self::new()
}
}