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

Commit 093d5818 authored by Katherine Lai's avatar Katherine Lai
Browse files

Floss: Add server_read_phy topshim in GattServer

This adds server_read_phy and its callback to topshim with
CXX since we can't pass base::Callback with bindgen

Bug: 193685149
Tag: #floss
Test: emerge floss
Change-Id: Ic4924f8f465bf9c76d7110eb5d4f869e3e14b002
parent 43f6a451
Loading
Loading
Loading
Loading
+15 −0
Original line number Diff line number Diff line
@@ -32,6 +32,11 @@ void ReadPhyCallback(int client_if, RawAddress addr, uint8_t tx_phy, uint8_t rx_
  bluetooth::topshim::rust::read_phy_callback(client_if, addr, tx_phy, rx_phy, status);
}

void ServerReadPhyCallback(
    int server_if, RawAddress addr, uint8_t tx_phy, uint8_t rx_phy, uint8_t status) {
  bluetooth::topshim::rust::server_read_phy_callback(server_if, addr, tx_phy, rx_phy, status);
}

}  // namespace internal

int GattClientIntf::read_phy(int client_if, RawAddress addr) {
@@ -42,6 +47,16 @@ std::unique_ptr<GattClientIntf> GetGattClientProfile(const unsigned char* gatt_i
  return std::make_unique<GattClientIntf>(reinterpret_cast<const btgatt_interface_t*>(gatt_intf)->client);
}

int GattServerIntf::server_read_phy(int server_if, RawAddress addr) {
  return server_intf_->read_phy(
      addr, base::Bind(&internal::ServerReadPhyCallback, server_if, addr));
}

std::unique_ptr<GattServerIntf> GetGattServerProfile(const unsigned char* gatt_intf) {
  return std::make_unique<GattServerIntf>(
      reinterpret_cast<const btgatt_interface_t*>(gatt_intf)->server);
}

}  // namespace rust
}  // namespace topshim
}  // namespace bluetooth
+13 −0
Original line number Diff line number Diff line
@@ -39,6 +39,19 @@ class GattClientIntf {

std::unique_ptr<GattClientIntf> GetGattClientProfile(const unsigned char* gatt_intf);

class GattServerIntf {
 public:
  GattServerIntf(const btgatt_server_interface_t* server_intf) : server_intf_(server_intf){};
  ~GattServerIntf() = default;

  int server_read_phy(int server_if, RawAddress bt_addr);

 private:
  const btgatt_server_interface_t* server_intf_;
};

std::unique_ptr<GattServerIntf> GetGattServerProfile(const unsigned char* gatt_intf);

}  // namespace rust
}  // namespace topshim
}  // namespace bluetooth
+31 −1
Original line number Diff line number Diff line
@@ -133,11 +133,29 @@ pub mod ffi {
        unsafe fn GetGattClientProfile(btif: *const u8) -> UniquePtr<GattClientIntf>;

        fn read_phy(self: Pin<&mut GattClientIntf>, client_if: i32, bt_addr: RawAddress) -> i32;

        type GattServerIntf;

        unsafe fn GetGattServerProfile(btif: *const u8) -> UniquePtr<GattServerIntf>;

        fn server_read_phy(
            self: Pin<&mut GattServerIntf>,
            server_if: i32,
            bt_addr: RawAddress,
        ) -> i32;
    }

    extern "Rust" {
        // Generated by cb_variant! below.
        fn read_phy_callback(client_if: i32, addr: RawAddress, tx_phy: u8, rx_phy: u8, status: u8);

        fn server_read_phy_callback(
            server_if: i32,
            addr: RawAddress,
            tx_phy: u8,
            rx_phy: u8,
            status: u8,
        );
    }

    unsafe extern "C++" {
@@ -599,6 +617,7 @@ pub enum GattServerCallbacks {
    MtuChanged(i32, i32),
    PhyUpdated(i32, u8, u8, u8),
    ConnUpdated(i32, u16, u16, u16, u8),
    ReadPhy(i32, RawAddress, u8, u8, GattStatus),
}

pub struct GattClientCallbacksDispatcher {
@@ -855,6 +874,11 @@ cb_variant!(
    i32, u16, u16, u16, u8, {}
);

cb_variant!(
    GattServerCb,
    server_read_phy_callback -> GattServerCallbacks::ReadPhy,
    i32, RawAddress, u8, u8, u8 -> GattStatus);

/// Scanning callbacks used by the GD implementation of BleScannerInterface.
/// These callbacks should be registered using |RegisterCallbacks| on
/// `BleScannerInterface`.
@@ -1129,6 +1153,7 @@ unsafe impl Send for RawBleAdvertiserWrapper {}
unsafe impl Send for btgatt_callbacks_t {}
unsafe impl Send for GattClient {}
unsafe impl Send for GattClientCallbacks {}
unsafe impl Send for GattServer {}
unsafe impl Send for BleScanner {}
unsafe impl Send for BleAdvertiser {}

@@ -1333,6 +1358,7 @@ impl GattClient {

pub struct GattServer {
    internal: RawGattServerWrapper,
    internal_cxx: cxx::UniquePtr<ffi::GattServerIntf>,
}

impl GattServer {
@@ -1412,7 +1438,9 @@ impl GattServer {
        BtStatus::from(ccall!(self, set_preferred_phy, addr, tx_phy, rx_phy, phy_options))
    }

    // TODO(b/193916778): Figure out how to shim read_phy which accepts base::Callback
    pub fn read_phy(&mut self, server_if: i32, addr: &RawAddress) -> BtStatus {
        BtStatus::from_i32(mutcxxcall!(self, server_read_phy, server_if, *addr)).unwrap()
    }
}

pub struct BleScanner {
@@ -1686,6 +1714,7 @@ impl Gatt {
        }

        let gatt_client_intf = unsafe { ffi::GetGattClientProfile(r as *const u8) };
        let gatt_server_intf = unsafe { ffi::GetGattServerProfile(r as *const u8) };
        let gatt_scanner_intf = unsafe { ffi::GetBleScannerIntf(r as *const u8) };
        let gatt_advertiser_intf = unsafe { ffi::GetBleAdvertiserIntf(r as *const u8) };

@@ -1708,6 +1737,7 @@ impl Gatt {
                            as *const btgatt_server_interface_t
                    },
                },
                internal_cxx: gatt_server_intf,
            },
            scanner: BleScanner::new(r as *const btgatt_interface_t, gatt_scanner_intf),
            advertiser: BleAdvertiser::new(r as *const btgatt_interface_t, gatt_advertiser_intf),