1 // Copyright 2022, The Android Open Source Project 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // TODO(b/290018030): Remove this and add proper safety comments. 16 #![allow(clippy::undocumented_unsafe_blocks)] 17 18 use crate::core::{start, stop}; 19 20 use cxx::{type_id, ExternType}; 21 pub use inner::*; 22 23 // SAFETY: `GattServerCallbacks` can be passed between threads. 24 unsafe impl Send for GattServerCallbacks {} 25 26 // SAFETY: `future_t` can be passed between threads. 27 unsafe impl Send for Future {} 28 29 unsafe impl ExternType for Uuid { 30 type Id = type_id!("bluetooth::Uuid"); 31 type Kind = cxx::kind::Trivial; 32 } 33 34 unsafe impl ExternType for AddressWithType { 35 type Id = type_id!("bluetooth::core::AddressWithType"); 36 type Kind = cxx::kind::Trivial; 37 } 38 39 #[allow(dead_code, missing_docs, unsafe_op_in_unsafe_fn)] 40 #[cxx::bridge] 41 mod inner { 42 #[derive(Debug)] 43 pub enum AddressTypeForFFI { 44 Public, 45 Random, 46 } 47 48 unsafe extern "C++" { 49 include!("osi/include/future.h"); 50 include!("src/core/ffi/module.h"); 51 52 #[cxx_name = "future_t"] 53 type Future; 54 55 #[namespace = "bluetooth::rust_shim"] 56 #[cxx_name = "FutureReady"] future_ready(future: Pin<&mut Future>)57 fn future_ready(future: Pin<&mut Future>); 58 } 59 60 #[namespace = "bluetooth::core"] 61 extern "C++" { 62 include!("src/core/ffi/types.h"); 63 type AddressWithType = crate::core::address::AddressWithType; 64 } 65 66 #[namespace = "bluetooth"] 67 extern "C++" { 68 include!("bluetooth/uuid.h"); 69 type Uuid = crate::core::uuid::Uuid; 70 } 71 72 #[namespace = "bluetooth::gatt"] 73 unsafe extern "C++" { 74 include!("src/gatt/ffi/gatt_shim.h"); 75 type GattServerCallbacks = crate::gatt::GattServerCallbacks; 76 } 77 78 #[namespace = "bluetooth::connection"] 79 unsafe extern "C++" { 80 include!("src/connection/ffi/connection_shim.h"); 81 type LeAclManagerShim = crate::connection::LeAclManagerShim; 82 } 83 84 #[namespace = "bluetooth::rust_shim"] 85 extern "Rust" { start( gatt_server_callbacks: UniquePtr<GattServerCallbacks>, le_acl_manager: UniquePtr<LeAclManagerShim>, on_started: Pin<&'static mut Future>, )86 fn start( 87 gatt_server_callbacks: UniquePtr<GattServerCallbacks>, 88 le_acl_manager: UniquePtr<LeAclManagerShim>, 89 on_started: Pin<&'static mut Future>, 90 ); 91 stop()92 fn stop(); 93 } 94 } 95