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

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

floss: Add cxxcall and mutcxxcall macros

Add new macros for calling cxx methods. Use of these macros will
standardize how we store and use these methods (making it easier to
replace or protect with guards in the future).

Bug: 189497489
Tag: #floss
Test: emerge-zork floss
Change-Id: I1ce49aa51f516eb8004da8a83b74a8d992adecf0
parent ca4fbe95
Loading
Loading
Loading
Loading
+42 −1
Original line number Diff line number Diff line
@@ -768,6 +768,14 @@ pub struct BluetoothInterface {
    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
/// segfaults if not validated beforehand.
///
/// Example:
///     ccall!(self, foobar, arg1, arg2)
///     Expands to: unsafe {((*self.internal.raw).foobar.unwrap())(arg1, arg2)}
#[macro_export]
macro_rules! ccall {
    ($self:ident,$fn_name:ident) => {
@@ -779,7 +787,40 @@ macro_rules! ccall {
        unsafe {
            ((*$self.internal.raw).$fn_name.unwrap())($($args),*)
        }
    };
}

/// Macro to call const functions via cxx. Expects the self object to have the
/// cxx object to be called at `self.internal_cxx`.
///
/// Example:
///     cxxcall!(self, foobar, arg1, arg2)
///     Expands to: self.internal_cxx.foobar(arg1, arg2)
#[macro_export]
macro_rules! cxxcall {
    ($self:expr,$fn_name:ident) => {
        $self.internal_cxx.$fn_name()
    };
    ($self:expr,$fn_name:ident, $($args:expr),*) => {
        $self.internal_cxx.$fn_name($($args),*)
    };
}

/// Macro to call mutable functions via cxx. Mutable functions are always
/// required to be defined with `self: Pin<&mut Self>`. The self object must
/// have the cxx object at `self.internal_cxx`.
///
/// Example:
///     mutcxxcall!(self, foobar, arg1, arg2)
///     Expands to: self.internal_cxx.pin_mut().foobar(arg1, arg2)
#[macro_export]
macro_rules! mutcxxcall {
    ($self:expr,$fn_name:ident) => {
        $self.internal_cxx.pin_mut().$fn_name()
    };
    ($self:expr,$fn_name:ident, $($args:expr),*) => {
        $self.internal_cxx.pin_mut().$fn_name($($args),*)
    };
}

impl BluetoothInterface {
+8 −7
Original line number Diff line number Diff line
@@ -8,7 +8,7 @@ use crate::profiles::gatt::bindings::{
    BleAdvertiserInterface, BleScannerInterface,
};
use crate::topstack::get_dispatchers;
use crate::{cast_to_ffi_address, ccall, deref_ffi_address};
use crate::{cast_to_ffi_address, ccall, deref_ffi_address, mutcxxcall};

use num_traits::cast::FromPrimitive;

@@ -761,11 +761,12 @@ impl GattClient {
    }

    pub fn read_phy(&mut self, client_if: i32, addr: &RawAddress) -> BtStatus {
        BtStatus::from_i32(
            self.internal_cxx
                .pin_mut()
                .read_phy(client_if, ffi::RustRawAddress { address: addr.val }),
        )
        BtStatus::from_i32(mutcxxcall!(
            self,
            read_phy,
            client_if,
            ffi::RustRawAddress { address: addr.val }
        ))
        .unwrap()
    }

@@ -1053,7 +1054,7 @@ impl Gatt {
        self.gatt_scanner_callbacks = Some(gatt_scanner_callbacks);

        // Register callbacks for gatt scanner
        self.scanner.internal_cxx.pin_mut().RegisterCallbacks();
        mutcxxcall!(self.scanner, RegisterCallbacks);

        return self.is_init;
    }