Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e6de0926 authored by Abhishek Pandit-Subedi's avatar Abhishek Pandit-Subedi
Browse files

floss: Update rustdocs in topshim + stack

Bug: 218551688
Tag: #floss
Test: ./build.py --target test && ./build.py --target docs
Change-Id: Idc9e8afdf5d7e0a5ba95180ce5dabc9f02bceb9f
parent 94764140
Loading
Loading
Loading
Loading
+3 −6
Original line number Diff line number Diff line
//! Fluoride/GD Bluetooth stack.
//! Floss Bluetooth stack.
//!
//! This crate provides the API implementation of the Fluoride/GD Bluetooth stack, independent of
//! any RPC projection.
//! This crate provides the API implementation of the Fluoride/GD Bluetooth
//! stack, independent of any RPC projection.

#[macro_use]
extern crate num_derive;
@@ -27,9 +27,6 @@ use bt_topshim::{
    },
};

/// Represents a Bluetooth address.
// TODO: Add support for LE random addresses.

#[derive(Clone, Debug)]
pub enum BluetoothCallbackType {
    Adapter,
+23 −13
Original line number Diff line number Diff line
//! Bluetooth interface shim
//! Shim for `bt_interface_t`, providing access to libbluetooth.
//!
//! This is a shim interface for calling the C++ bluetooth interface via Rust.
//!

use crate::bindings::root as bindings;
use crate::topstack::get_dispatchers;
@@ -374,10 +373,10 @@ impl BluetoothProperty {
        }
    }

    // Given a mutable array, this will copy the data to that array and return a
    // pointer to it.
    //
    // The lifetime of the returned pointer is tied to that of the slice given.
    /// Given a mutable array, this will copy the data to that array and return a
    /// pointer to it.
    ///
    /// The lifetime of the returned pointer is tied to that of the slice given.
    fn get_data_ptr<'a>(&'a self, data: &'a mut [u8]) -> *mut u8 {
        let len = self.get_len();
        match &*self {
@@ -685,6 +684,7 @@ macro_rules! cast_to_const_ffi_address {
    };
}

/// An enum representing `bt_callbacks_t` from btif.
#[derive(Clone, Debug)]
pub enum BaseCallbacks {
    AdapterState(BtState),
@@ -761,13 +761,6 @@ struct RawInterfaceWrapper {

unsafe impl Send for RawInterfaceWrapper {}

pub struct BluetoothInterface {
    internal: RawInterfaceWrapper,
    pub is_init: bool,
    // Need to take ownership of callbacks so it doesn't get freed after init
    callbacks: Option<Box<bindings::bt_callbacks_t>>,
}

/// Macro to call functions via function pointers. Expects the self object to
/// have a raw interface wrapper at `self.internal`. The actual function call is
/// marked unsafe since it will need to dereference a C object. This can cause
@@ -823,11 +816,28 @@ macro_rules! mutcxxcall {
    };
}

/// Rust wrapper around `bt_interface_t`.
pub struct BluetoothInterface {
    internal: RawInterfaceWrapper,

    /// Set to true after `initialize` is called.
    pub is_init: bool,

    // Need to take ownership of callbacks so it doesn't get freed after init
    callbacks: Option<Box<bindings::bt_callbacks_t>>,
}

impl BluetoothInterface {
    pub fn is_initialized(&self) -> bool {
        self.is_init
    }

    /// Initialize the Bluetooth interface by setting up the underlying interface.
    ///
    /// # Arguments
    ///
    /// * `callbacks` - Dispatcher struct that accepts [`BaseCallbacks`]
    /// * `init_flags` - List of flags sent to libbluetooth for init.
    pub fn initialize(
        &mut self,
        callbacks: BaseCallbacksDispatcher,
+19 −1
Original line number Diff line number Diff line
//! The main entry point for Rust to C++.
//! Topshim is the main entry point from Rust code to C++.
//!
//! The Bluetooth stack is split into two parts: libbluetooth and the framework
//! above it. Libbluetooth is a combination of C/C++ and Rust that provides the
//! core Bluetooth functionality. It exposes top level apis in `bt_interface_t`
//! which can be used to drive the underlying implementation. Topshim provides
//! Rust apis to access this C/C++ interface and other top level interfaces in
//! order to use libbluetooth.
//!
//! The expected users of Topshim:
//!     * Floss (ChromeOS + Linux Bluetooth stack; uses D-Bus)
//!     * Topshim facade (used for testing)

#[macro_use]
extern crate lazy_static;
#[macro_use]
@@ -6,8 +18,14 @@ extern crate num_derive;
#[macro_use]
extern crate bitflags;

/// Bindgen bindings for accessing libbluetooth.
pub mod bindings;

pub mod btif;

/// Helper module for the topshim facade.
pub mod controller;

pub mod profiles;

pub mod topstack;
+2 −0
Original line number Diff line number Diff line
//! Various libraries to access the profile interfaces.

pub mod a2dp;
pub mod avrcp;
pub mod gatt;
+52 −1
Original line number Diff line number Diff line
@@ -29,13 +29,53 @@ lazy_static! {
        Arc::new(Mutex::new(DispatchContainer { instances: HashMap::new() }));
}

type InstanceBox = Box<dyn Any + Send + Sync>;
/// A Box-ed struct that implements a `dispatch` fn.
///
///  Example:
///  ```
///  use std::sync::Arc;
///  use std::sync::Mutex;
///
///  #[derive(Debug)]
///  enum Foo {
///     First(i16),
///     Second(i32),
///  }
///
///  struct FooDispatcher {
///     dispatch: Box<dyn Fn(Foo) + Send>,
///  }
///
///  fn main() {
///     let foo_dispatcher = FooDispatcher {
///         dispatch: Box::new(move |value| {
///             println!("Dispatch {:?}", value);
///         })
///     };
///     let value = Arc::new(Mutex::new(foo_dispatcher));
///     let instance_box = Box::new(value);
///  }
///  ```
pub type InstanceBox = Box<dyn Any + Send + Sync>;

/// Manage enum dispatches for emulating callbacks.
///
/// Libbluetooth is highly callback based but our Rust code prefers using
/// channels. To reconcile these two systems, we pass static callbacks to
/// libbluetooth that convert callback args into an enum variant and call the
/// dispatcher for that enum. The dispatcher will then queue that enum into the
/// channel (using a captured channel tx in the closure).
pub struct DispatchContainer {
    instances: HashMap<TypeId, InstanceBox>,
}

impl DispatchContainer {
    /// Find registered dispatcher for enum specialization.
    ///
    /// # Return
    ///
    /// Returns an Option with a dispatcher object (the contents of
    /// [`InstanceBox`]).
    pub fn get<T: 'static + Clone + Send + Sync>(&self) -> Option<T> {
        let typeid = TypeId::of::<T>();

@@ -46,11 +86,22 @@ impl DispatchContainer {
        None
    }

    /// Set dispatcher for an enum specialization.
    ///
    /// # Arguments
    ///
    /// * `obj` - The contents of [`InstanceBox`], usually `Arc<Mutex<U>>`. See
    ///           the [`InstanceBox`] documentation for examples.
    ///
    /// # Returns
    ///
    /// True if this is replacing an existing enum dispatcher.
    pub fn set<T: 'static + Clone + Send + Sync>(&mut self, obj: T) -> bool {
        self.instances.insert(TypeId::of::<T>(), Box::new(obj)).is_some()
    }
}

/// Take a clone of the static dispatcher container.
pub fn get_dispatchers() -> Arc<Mutex<DispatchContainer>> {
    CB_DISPATCHER.clone()
}