bt_rfcomm/lib.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
// Copyright 2021 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/// The direct link connection identifier (DLCI) definitions.
mod dlci;
pub use dlci::{ServerChannel, DLCI};
/// The error type used throughout this library.
mod error;
pub use error::Error as RfcommError;
/// The definitions for RFCOMM frames - the basic unit of data in RFCOMM.
pub mod frame;
/// Convenience helpers for the `bredr.Profile` API RFCOMM operations.
pub mod profile;
/// The Role assigned to a device in an RFCOMM Session.
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Role {
/// RFCOMM Session has not started up the start-up procedure.
Unassigned,
/// The start-up procedure is in progress, and so the role is being negotiated.
Negotiating,
/// The device that starts up the multiplexer control channel is considered
/// the initiator.
Initiator,
/// The device that responds to the start-up procedure.
Responder,
}
impl Role {
/// Returns the Role opposite to the current Role.
pub fn opposite_role(&self) -> Self {
match self {
Role::Initiator => Role::Responder,
Role::Responder => Role::Initiator,
role => *role,
}
}
/// Returns true if the multiplexer has started - namely, a role has been assigned.
pub fn is_multiplexer_started(&self) -> bool {
*self == Role::Initiator || *self == Role::Responder
}
}
/// Returns the maximum RFCOMM packet size that can be used for the provided L2CAP `mtu`.
/// It is assumed that `mtu` is a valid L2CAP MTU.
pub fn max_packet_size_from_l2cap_mtu(mtu: u16) -> u16 {
mtu - crate::frame::MAX_RFCOMM_HEADER_SIZE as u16
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_convert_to_opposite_role() {
let role = Role::Initiator;
assert_eq!(role.opposite_role(), Role::Responder);
let role = Role::Responder;
assert_eq!(role.opposite_role(), Role::Initiator);
let role = Role::Unassigned;
assert_eq!(role.opposite_role(), Role::Unassigned);
let role = Role::Negotiating;
assert_eq!(role.opposite_role(), Role::Negotiating);
}
}