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

Commit 1c57cd55 authored by Rahul Arya's avatar Rahul Arya Committed by Gerrit Code Review
Browse files

Merge changes from topic "tokio-test-utils"

* changes:
  [Private GATT] Simplify datastore interface
  [Private GATT] Add support for MTU Exchange
  [Private GATT] Add support for descriptors
  [Private GATT] Add support for handle value indications
  [Private GATT] Make ATT permissions a bitmask
  [Private GATT] Refactor AttOpcode handling
  [Private GATT] Rename AttTransactionHandler
  [Private GATT] Refactor to remove Rc<>
  [Private GATT] Add support for Tokio test-utils
parents 53400db9 6c48322b
Loading
Loading
Loading
Loading
+7 −2
Original line number Diff line number Diff line
@@ -2147,8 +2147,13 @@ static void gattServerSendIndicationNative(JNIEnv* env, jobject object,
  jbyte* array = env->GetByteArrayElements(val, 0);
  int val_len = env->GetArrayLength(val);

  if (bluetooth::gatt::is_connection_isolated(conn_id)) {
    auto data = ::rust::Slice<const uint8_t>((uint8_t*)array, val_len);
    bluetooth::gatt::send_indication(server_if, attr_handle, conn_id, data);
  } else {
    sGattIf->server->send_indication(server_if, attr_handle, conn_id,
                                     /*confirm*/ 1, (uint8_t*)array, val_len);
  }

  env->ReleaseByteArrayElements(val, array, JNI_ABORT);
}
+5 −1
Original line number Diff line number Diff line
@@ -34,8 +34,8 @@ rust_defaults {
        "liblog_rust",
        "libanyhow",
        "libcxx",
        "libtokio",
        "libbt_common",
        "libbitflags",

        // needed to work around duplicate symbols
        // caused by bug in Soong
@@ -71,6 +71,9 @@ rust_ffi_static {
    name: "libbluetooth_core_rs",
    crate_name: "bluetooth_core",
    defaults: ["libbluetooth_core_rs_defaults"],
    rustlibs: [
        "libtokio",
    ],
    target: {
        android: {
            rustlibs: [
@@ -85,6 +88,7 @@ rust_test_host {
    name: "libbluetooth_core_rs_test",
    defaults: ["libbluetooth_core_rs_defaults"],
    rustlibs: [
        "libtokio_for_test",
        "libtokio_test",
    ],
}
+1 −0
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ bt_common = { path = "../gd/rust/common", default-features = false }
# External dependencies
# Note: source-of-truth is Android.bp, these are mirrored solely for IDE convenience
anyhow = "1.0"
bitflags = "1.3.2"
log = "*"
cxx = "*"
android_logger = "*"
+2 −0
Original line number Diff line number Diff line
//! Shared data-types and utility methods go here.

mod ffi;
pub mod shared_box;
pub mod shared_mutex;
pub mod uuid;

use std::{rc::Rc, thread};
+90 −0
Original line number Diff line number Diff line
//! Wrapper around Rc<> to make ownership clearer
//!
//! The idea is to have ownership represented by a SharedBox<T>.
//! Temporary ownership can be held using a WeakBox<T>, which should
//! not be held across async points. This reduces the risk of accidental
//! lifetime extension.

use std::{
    ops::Deref,
    rc::{Rc, Weak},
};

/// A Box<> where static "weak" references to the contents can be taken,
/// and fallibly upgraded at a later point. Unlike Rc<>, weak references
/// cannot be upgraded back to owning references, so ownership remains clear
/// and reference cycles avoided.
#[derive(Debug)]
pub struct SharedBox<T: ?Sized>(Rc<T>);

impl<T> SharedBox<T> {
    /// Constructor
    pub fn new(t: T) -> Self {
        Self(t.into())
    }

    /// Produce a weak reference to the contents
    pub fn downgrade(&self) -> WeakBox<T> {
        WeakBox(Rc::downgrade(&self.0))
    }

    /// Produce an upgraded weak reference to the contents
    pub fn as_ref(&self) -> WeakBoxRef<T> {
        WeakBoxRef(self.0.deref(), Rc::downgrade(&self.0))
    }
}

impl<T> From<T> for SharedBox<T> {
    fn from(value: T) -> Self {
        Self(value.into())
    }
}

impl<T> Deref for SharedBox<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        self.0.deref()
    }
}

/// A weak reference to the contents within a SharedBox<>
pub struct WeakBox<T>(Weak<T>);

impl<T> WeakBox<T> {
    /// Fallibly upgrade to a strong reference, passed into the supplied closure.
    /// The strong reference is not passed into the closure to avoid accidental
    /// lifetime extension.
    ///
    /// Note: reference-counting is used so that, if the passed-in closure drops
    /// the SharedBox<>, the strong reference remains safe. But please don't
    /// do that!
    pub fn with<U>(&self, f: impl FnOnce(Option<WeakBoxRef<T>>) -> U) -> U {
        f(self.0.upgrade().as_deref().map(|x| WeakBoxRef(x, self.0.clone())))
    }
}

impl<T> Clone for WeakBox<T> {
    fn clone(&self) -> Self {
        Self(self.0.clone())
    }
}

/// A strong reference to the contents within a SharedBox<>.
pub struct WeakBoxRef<'a, T>(&'a T, Weak<T>);

impl<'a, T> WeakBoxRef<'a, T> {
    /// Downgrade to a weak reference (with static lifetime) to the contents
    /// within the underlying SharedBox<>
    pub fn downgrade(&self) -> WeakBox<T> {
        WeakBox(self.1.clone())
    }
}

impl<'a, T> Deref for WeakBoxRef<'a, T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        self.0
    }
}
Loading