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

Commit 4f27716b authored by Sonny Sasaka's avatar Sonny Sasaka Committed by Gerrit Code Review
Browse files

Merge "floss: Add read_phy topshim in GattClient"

parents 29f90aae 69960a9c
Loading
Loading
Loading
Loading
+5 −2
Original line number Diff line number Diff line
@@ -49,6 +49,7 @@ cc_library_static {
        "btav/btav_shim.cc",
        "btav_sink/btav_sink_shim.cc",
        "btif/btif_shim.cc",
        "gatt/gatt_shim.cc",
    ],
    generated_headers: ["libbt_topshim_bridge_header", "cxx-bridge-header"],
    generated_sources: ["libbt_topshim_bridge_code"],
@@ -71,7 +72,8 @@ gensrcs {
    srcs: [
        "src/btif.rs",
        "src/profiles/a2dp.rs",
        "src/profiles/avrcp.rs"
        "src/profiles/avrcp.rs",
        "src/profiles/gatt.rs",
    ],
    output_extension: "rs.h",
    export_include_dirs: ["."],
@@ -84,7 +86,8 @@ gensrcs {
    srcs: [
        "src/btif.rs",
        "src/profiles/a2dp.rs",
        "src/profiles/avrcp.rs"
        "src/profiles/avrcp.rs",
        "src/profiles/gatt.rs",
    ],
    output_extension: "cc",
    export_include_dirs: ["."],
+6 −3
Original line number Diff line number Diff line
@@ -26,7 +26,8 @@ cxxbridge_header("btif_bridge_header") {
  sources = [
    "src/btif.rs",
    "src/profiles/a2dp.rs",
    "src/profiles/avrcp.rs"
    "src/profiles/avrcp.rs",
    "src/profiles/gatt.rs",
  ]
  all_dependent_configs = [ ":rust_topshim_config" ]
  deps = [":cxxlibheader"]
@@ -36,7 +37,8 @@ cxxbridge_cc("btif_bridge_code") {
  sources = [
    "src/btif.rs",
    "src/profiles/a2dp.rs",
    "src/profiles/avrcp.rs"
    "src/profiles/avrcp.rs",
    "src/profiles/gatt.rs",
  ]
  deps = [":btif_bridge_header"]
  configs = [ "//bt/gd:gd_defaults" ]
@@ -46,7 +48,8 @@ source_set("btif_cxx_bridge_code") {
  sources = [
    "btif/btif_shim.cc",
    "btav/btav_shim.cc",
    "btav_sink/btav_sink_shim.cc"
    "btav_sink/btav_sink_shim.cc",
    "gatt/gatt_shim.cc",
  ]

  deps = [":btif_bridge_header"]
+59 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "gd/rust/topshim/gatt/gatt_shim.h"

#include "base/bind.h"
#include "base/callback.h"
#include "rust/cxx.h"
#include "src/profiles/gatt.rs.h"

namespace bluetooth {
namespace topshim {
namespace rust {

namespace internal {

static RustRawAddress ToRustAddress(const RawAddress& address) {
  RustRawAddress raddr;
  std::copy(std::begin(address.address), std::end(address.address), std::begin(raddr.address));
  return raddr;
}

static RawAddress FromRustAddress(const RustRawAddress& raddr) {
  RawAddress addr;
  addr.FromOctets(raddr.address.data());
  return addr;
}

void ReadPhyCallback(int client_if, RawAddress address, uint8_t tx_phy, uint8_t rx_phy, uint8_t status) {
  bluetooth::topshim::rust::read_phy_callback(client_if, ToRustAddress(address), tx_phy, rx_phy, status);
}

}  // namespace internal

int GattClientIntf::read_phy(int client_if, RustRawAddress addr) {
  RawAddress address = internal::FromRustAddress(addr);
  return client_intf_->read_phy(address, base::Bind(&internal::ReadPhyCallback, client_if, address));
}

std::unique_ptr<GattClientIntf> GetGattClientProfile(const unsigned char* gatt_intf) {
  return std::make_unique<GattClientIntf>(reinterpret_cast<const btgatt_interface_t*>(gatt_intf)->client);
}

}  // namespace rust
}  // namespace topshim
}  // namespace bluetooth
+48 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifndef GD_RUST_TOPSHIM_GATT_GATT_SHIM_H
#define GD_RUST_TOPSHIM_GATT_GATT_SHIM_H

#include <memory>

#include "include/hardware/bt_gatt.h"

#include "rust/cxx.h"

namespace bluetooth {
namespace topshim {
namespace rust {

struct RustRawAddress;

class GattClientIntf {
 public:
  GattClientIntf(const btgatt_client_interface_t* client_intf) : client_intf_(client_intf){};
  ~GattClientIntf() = default;

  int read_phy(int client_if, RustRawAddress bt_addr);

 private:
  const btgatt_client_interface_t* client_intf_;
};

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

}  // namespace rust
}  // namespace topshim
}  // namespace bluetooth

#endif  // GD_RUST_TOPSHIM_GATT_GATT_SHIM_H
+54 −1
Original line number Diff line number Diff line
@@ -10,6 +10,8 @@ use crate::profiles::gatt::bindings::{
use crate::topstack::get_dispatchers;
use crate::{cast_to_ffi_address, ccall, deref_ffi_address};

use num_traits::cast::FromPrimitive;

use std::sync::{Arc, Mutex};

use topshim_macros::cb_variant;
@@ -34,6 +36,36 @@ impl std::fmt::Debug for BtGattNotifyParams {
    }
}

#[cxx::bridge(namespace = bluetooth::topshim::rust)]
pub mod ffi {
    #[derive(Debug, Copy, Clone)]
    pub struct RustRawAddress {
        address: [u8; 6],
    }

    unsafe extern "C++" {
        include!("gatt/gatt_shim.h");

        type GattClientIntf;

        unsafe fn GetGattClientProfile(btif: *const u8) -> UniquePtr<GattClientIntf>;

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

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

#[derive(Debug, FromPrimitive, PartialEq, PartialOrd)]
#[repr(u32)]
pub enum GattStatus {
@@ -109,6 +141,7 @@ pub enum GattClientCallbacks {
    PhyUpdated(i32, u8, u8, u8),
    ConnUpdated(i32, u16, u16, u16, u8),
    ServiceChanged(i32),
    ReadPhy(i32, RawAddress, u8, u8, u8),
}

#[derive(Debug)]
@@ -280,6 +313,14 @@ cb_variant!(
    i32, {}
);

cb_variant!(
    GattClientCb,
    read_phy_callback -> GattClientCallbacks::ReadPhy,
    i32, ffi::RustRawAddress -> RawAddress, u8, u8, u8, {
        let _1 = RawAddress { val: _1.address };
    }
);

cb_variant!(
    GattServerCb,
    gs_register_server_cb -> GattServerCallbacks::RegisterServer,
@@ -421,9 +462,11 @@ unsafe impl Send for RawGattServerWrapper {}
unsafe impl Send for RawBleScannerWrapper {}
unsafe impl Send for RawBleAdvertiserWrapper {}
unsafe impl Send for btgatt_callbacks_t {}
unsafe impl Send for GattClient {}

pub struct GattClient {
    internal: RawGattClientWrapper,
    internal_cxx: cxx::UniquePtr<ffi::GattClientIntf>,
}

impl GattClient {
@@ -613,7 +656,14 @@ impl GattClient {
        BtStatus::from(ccall!(self, set_preferred_phy, ffi_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, client_if: i32, addr: &RawAddress) -> BtStatus {
        BtStatus::from_i32(
            self.internal_cxx
                .pin_mut()
                .read_phy(client_if, ffi::RustRawAddress { address: addr.val }),
        )
        .unwrap()
    }

    pub fn test_command(&self, command: i32, params: &BtGattTestParams) -> BtStatus {
        BtStatus::from(ccall!(self, test_command, command, params))
@@ -743,6 +793,8 @@ impl Gatt {
            return None;
        }

        let gatt_client_intf = unsafe { ffi::GetGattClientProfile(r as *const u8) };

        Some(Gatt {
            internal: RawGattWrapper { raw: r as *const btgatt_interface_t },
            is_init: false,
@@ -753,6 +805,7 @@ impl Gatt {
                            as *const btgatt_client_interface_t
                    },
                },
                internal_cxx: gatt_client_intf,
            },
            server: GattServer {
                internal: RawGattServerWrapper {