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

Commit 594b9161 authored by Rahul Arya's avatar Rahul Arya
Browse files

[Connection Manager] Expose AddressWithType over FFI

We create a simple AddressWithType object shared between C++ and Rust.
We can't reuse any of the existing types, since the GD AddressWithType
has a vtable and so does not have Rust-compatible layout, and the
legacy tBLE_BD_ADDR has the wrong endianness.

Bug: 272572974
Test: compiles
Change-Id: I399bb1f232aeec4e7f2b635aef0f050bf5efb356
parent 1dd7ce0b
Loading
Loading
Loading
Loading
+26 −0
Original line number Diff line number Diff line
//! An address with type (public / random)

#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
#[repr(C)]
/// The type of an LE address (see: 5.3 Vol 6B 1.3 Device Axddress)
pub enum AddressType {
    /// A public address
    Public = 0x0,
    /// A random address (either random static or private)
    Random = 0x1,
}

/// An LE address
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
#[repr(C)]
pub struct AddressWithType {
    /// The 6 address bytes stored in little-endian format
    pub address: [u8; 6],
    /// The address type, either public or random
    pub address_type: AddressType,
}

impl AddressWithType {
    /// An empty/invalid address
    pub const EMPTY: Self = Self { address: [0, 0, 0, 0, 0, 0], address_type: AddressType::Public };
}
+17 −0
Original line number Diff line number Diff line
@@ -24,9 +24,26 @@ unsafe impl ExternType for Uuid {
    type Kind = cxx::kind::Trivial;
}

unsafe impl ExternType for AddressWithType {
    type Id = type_id!("bluetooth::core::AddressWithType");
    type Kind = cxx::kind::Trivial;
}

#[allow(dead_code, missing_docs)]
#[cxx::bridge]
mod inner {
    #[derive(Debug)]
    pub enum AddressTypeForFFI {
        Public,
        Random,
    }

    #[namespace = "bluetooth::core"]
    extern "C++" {
        include!("src/core/ffi/types.h");
        type AddressWithType = crate::core::address::AddressWithType;
    }

    #[namespace = "bluetooth"]
    extern "C++" {
        include!("bluetooth/uuid.h");
+36 −0
Original line number Diff line number Diff line
// Copyright 2023, 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.

#pragma once

#include <stdint.h>

#include <array>

namespace bluetooth {
namespace core {

enum class AddressType : uint8_t {
  Public = 0x0,
  Random = 0x1,
};

struct AddressWithType {
  /// Stored in little-endian format
  std::array<uint8_t, 6> address;
  AddressType address_type;
};

}  // namespace core
}  // namespace bluetooth
+1 −0
Original line number Diff line number Diff line
//! Shared data-types and utility methods go here.

pub mod address;
mod ffi;
pub mod shared_box;
pub mod shared_mutex;