diff --git a/bluer-tools/src/l2cat.rs b/bluer-tools/src/l2cat.rs index 0f5328c..0b9c1d1 100644 --- a/bluer-tools/src/l2cat.rs +++ b/bluer-tools/src/l2cat.rs @@ -59,7 +59,7 @@ struct ConnectOpts { /// Public Bluetooth address of target device. address: Address, /// Target PSM. - psm: u8, + psm: u16, } impl ConnectOpts { @@ -106,7 +106,7 @@ struct ListenOpts { /// PSM to listen on. /// Specify 0 to auto allocate an available PSM. /// A value below 128 is privileged. - psm: u8, + psm: u16, } impl ListenOpts { @@ -164,7 +164,7 @@ struct ServeOpts { /// PSM to listen on. /// Specify 0 to auto allocate an available PSM. /// A value below 128 is privileged. - psm: u8, + psm: u16, /// Program to execute once connection is established. command: OsString, /// Arguments to program. diff --git a/bluer/examples/l2cap.inc b/bluer/examples/l2cap.inc index d182665..b348ec7 100644 --- a/bluer/examples/l2cap.inc +++ b/bluer/examples/l2cap.inc @@ -1,3 +1,3 @@ -const PSM: u8 = PSM_DYN_START + 5; +const PSM: u16 = PSM_DYN_START + 5; const HELLO_MSG: &[u8] = b"Hello from l2cap_server!"; diff --git a/bluer/src/l2cap/mod.rs b/bluer/src/l2cap/mod.rs index 2ac85cc..027a1d8 100644 --- a/bluer/src/l2cap/mod.rs +++ b/bluer/src/l2cap/mod.rs @@ -82,12 +82,15 @@ impl Drop for OwnedFd { /// `CAP_NET_BIND_SERVICE` capability. pub const PSM_DYN_START: u8 = 0x80; +/// The highest protocol service multiplexor (PSM) for Bluetooth Low Energy. +pub const PSM_LE_MAX: u16 = 0xff; + /// An L2CAP socket address. #[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct SocketAddr { /// Device address. /// - /// Specify [SocketAddr::any] for any local adapter address. + /// Specify [Address::any] for any local adapter address. pub addr: Address, /// Device address type. pub addr_type: AddressType, @@ -95,9 +98,10 @@ pub struct SocketAddr { /// /// Listening on a PSM below [PSM_DYN_START] requires the /// `CAP_NET_BIND_SERVICE` capability. + /// The highest PSM for Bluetooth Low Energy is [PSM_LE_MAX]. /// /// Set to 0 for listening to assign an available PSM. - pub psm: u8, + pub psm: u16, /// Connection identifier (CID). /// /// Should be set to 0. @@ -106,7 +110,7 @@ pub struct SocketAddr { impl SocketAddr { /// Creates a new L2CAP socket address. - pub const fn new(addr: Address, addr_type: AddressType, psm: u8) -> Self { + pub const fn new(addr: Address, addr_type: AddressType, psm: u16) -> Self { Self { addr, addr_type, psm, cid: 0 } } @@ -120,7 +124,7 @@ impl From for sockaddr_l2 { fn from(sa: SocketAddr) -> Self { sockaddr_l2 { l2_family: AF_BLUETOOTH as _, - l2_psm: (sa.psm as u16).to_le(), + l2_psm: sa.psm.to_le(), l2_cid: sa.cid.to_le(), l2_bdaddr: sa.addr.into(), l2_bdaddr_type: sa.addr_type as _, @@ -138,9 +142,7 @@ impl TryFrom for SocketAddr { addr: Address::from(saddr.l2_bdaddr), addr_type: AddressType::from_u8(saddr.l2_bdaddr_type) .ok_or_else(|| Error::new(ErrorKind::InvalidInput, "invalid sockaddr_l2::l2_bdaddr_type"))?, - psm: u16::from_le(saddr.l2_psm) - .try_into() - .map_err(|_| Error::new(ErrorKind::InvalidInput, "invalid sockaddr_l2::l2_psm"))?, + psm: u16::from_le(saddr.l2_psm), cid: u16::from_le(saddr.l2_cid), }) } @@ -845,7 +847,7 @@ pub struct StreamListener { impl StreamListener { /// Creates a new Listener, which will be bound to the specified socket address. /// - /// Specify [Address::any] for any local adapter address. + /// Specify [SocketAddr::any] for any local adapter address. /// A PSM below [PSM_DYN_START] requires the `CAP_NET_BIND_SERVICE` capability. pub async fn bind(sa: SocketAddr) -> Result { let socket = Socket::::new_stream()?;