OSDN Git Service

rusty-gd: implement controller shim
authorZach Johnson <zachoverflow@google.com>
Tue, 22 Dec 2020 07:16:48 +0000 (23:16 -0800)
committerZach Johnson <zachoverflow@google.com>
Wed, 13 Jan 2021 23:13:58 +0000 (15:13 -0800)
Bug: 171749953
Tag: #gd-refactor
Test: gd/cert/run --rhost
Change-Id: I847253d5b217295a8ae72e124b25a647dc0d96e1

gd/rust/hci/src/controller.rs
gd/rust/hci/src/lib.rs
gd/rust/shim/Android.bp
gd/rust/shim/src/controller.rs [new file with mode: 0644]
gd/rust/shim/src/lib.rs
gd/rust/shim/src/stack.rs
main/shim/controller.cc
main/shim/stack.cc
main/shim/stack.h

index ddf8227..cada68c 100644 (file)
@@ -15,11 +15,12 @@ use bt_packets::hci::{
 use gddi::{module, provides, Stoppable};
 use num_traits::ToPrimitive;
 use std::convert::TryFrom;
+use std::sync::Arc;
 
 module! {
     controller_module,
     providers {
-        ControllerExports => provide_controller,
+        Arc<ControllerExports> => provide_controller,
     },
 }
 
@@ -33,7 +34,7 @@ macro_rules! assert_success {
 }
 
 #[provides]
-async fn provide_controller(mut hci: HciExports) -> ControllerExports {
+async fn provide_controller(mut hci: HciExports) -> Arc<ControllerExports> {
     assert_success!(hci.send(LeSetEventMaskBuilder { le_event_mask: 0x0000000000021e7f }));
     assert_success!(hci.send(SetEventMaskBuilder { event_mask: 0x3dbfffffffffffff }));
     assert_success!(
@@ -54,7 +55,7 @@ async fn provide_controller(mut hci: HciExports) -> ControllerExports {
             .get_supported_commands(),
     };
 
-    let lmp_features = read_lmp_features(&mut hci).await;
+    let features = read_features(&mut hci).await;
 
     let buffer_size = assert_success!(hci.send(ReadBufferSizeBuilder {}));
     let acl_buffer_length = buffer_size.get_acl_data_packet_length();
@@ -86,8 +87,9 @@ async fn provide_controller(mut hci: HciExports) -> ControllerExports {
         le_buffer_length = acl_buffer_length;
     }
 
-    let le_features =
-        assert_success!(hci.send(LeReadLocalSupportedFeaturesBuilder {})).get_le_features();
+    let le_features = SupportedLeFeatures::new(
+        assert_success!(hci.send(LeReadLocalSupportedFeaturesBuilder {})).get_le_features(),
+    );
     let le_supported_states =
         assert_success!(hci.send(LeReadSupportedStatesBuilder {})).get_le_states();
     let le_connect_list_size =
@@ -139,12 +141,12 @@ async fn provide_controller(mut hci: HciExports) -> ControllerExports {
 
     let address = assert_success!(hci.send(ReadBdAddrBuilder {})).get_bd_addr();
 
-    ControllerExports {
+    Arc::new(ControllerExports {
         name,
         address,
         version_info,
         commands,
-        lmp_features,
+        features,
         acl_buffer_length,
         acl_buffers,
         sco_buffer_length: buffer_size.get_synchronous_data_packet_length(),
@@ -162,10 +164,10 @@ async fn provide_controller(mut hci: HciExports) -> ControllerExports {
         le_max_advertising_data_length,
         le_supported_advertising_sets,
         le_periodic_advertiser_list_size,
-    }
+    })
 }
 
-async fn read_lmp_features(hci: &mut HciExports) -> Vec<u64> {
+async fn read_features(hci: &mut HciExports) -> SupportedFeatures {
     let mut features = Vec::new();
     let mut page_number: u8 = 0;
     let mut max_page_number: u8 = 1;
@@ -176,34 +178,35 @@ async fn read_lmp_features(hci: &mut HciExports) -> Vec<u64> {
         page_number += 1;
     }
 
-    features
+    SupportedFeatures::new(features)
 }
 
 /// Controller interface
 #[derive(Clone, Stoppable)]
+#[allow(missing_docs)]
 pub struct ControllerExports {
-    name: String,
-    address: Address,
-    version_info: LocalVersionInformation,
-    commands: SupportedCommands,
-    lmp_features: Vec<u64>,
-    acl_buffer_length: u16,
-    acl_buffers: u16,
-    sco_buffer_length: u8,
-    sco_buffers: u16,
-    le_buffer_length: u16,
-    le_buffers: u8,
-    iso_buffer_length: u16,
-    iso_buffers: u8,
-    le_features: u64,
-    le_supported_states: u64,
-    le_connect_list_size: u8,
-    le_resolving_list_size: u8,
-    le_max_data_length: LeMaximumDataLength,
-    le_suggested_default_data_length: u16,
-    le_max_advertising_data_length: u16,
-    le_supported_advertising_sets: u8,
-    le_periodic_advertiser_list_size: u8,
+    pub name: String,
+    pub address: Address,
+    pub version_info: LocalVersionInformation,
+    pub commands: SupportedCommands,
+    pub features: SupportedFeatures,
+    pub acl_buffer_length: u16,
+    pub acl_buffers: u16,
+    pub sco_buffer_length: u8,
+    pub sco_buffers: u16,
+    pub le_buffer_length: u16,
+    pub le_buffers: u8,
+    pub iso_buffer_length: u16,
+    pub iso_buffers: u8,
+    pub le_features: SupportedLeFeatures,
+    pub le_supported_states: u64,
+    pub le_connect_list_size: u8,
+    pub le_resolving_list_size: u8,
+    pub le_max_data_length: LeMaximumDataLength,
+    pub le_suggested_default_data_length: u16,
+    pub le_max_advertising_data_length: u16,
+    pub le_supported_advertising_sets: u8,
+    pub le_periodic_advertiser_list_size: u8,
 }
 
 /// Convenience struct for checking what commands are supported
@@ -233,6 +236,94 @@ impl SupportedCommands {
     }
 }
 
+macro_rules! supported_features {
+    ($($id:ident => $page:literal : $bit:literal),*) => {
+        /// Convenience struct for checking what features are supported
+        #[derive(Clone)]
+        #[allow(missing_docs)]
+        pub struct SupportedFeatures {
+            $(pub $id: bool,)*
+        }
+
+        impl SupportedFeatures {
+            fn new(supported: Vec<u64>) -> Self {
+                Self {
+                    $($id: *supported.get($page).unwrap_or(&0) & (1 << $bit) != 0,)*
+                }
+            }
+        }
+    }
+}
+
+supported_features! {
+    three_slot_packets => 0:0,
+    five_slot_packets => 0:1,
+    role_switch => 0:5,
+    hold_mode => 0:6,
+    sniff_mode => 0:7,
+    park_mode => 0:8,
+    sco => 0:11,
+    hv2_packets => 0:12,
+    hv3_packets => 0:13,
+    classic_2m_phy => 0:25,
+    classic_3m_phy => 0:26,
+    interlaced_inquiry_scan => 0:28,
+    rssi_with_inquiry_results => 0:30,
+    ev3_packets => 0:31,
+    ev4_packets => 0:32,
+    ev5_packets => 0:33,
+    ble => 0:38,
+    three_slot_edr_packets => 0:39,
+    five_slot_edr_packets => 0:40,
+    sniff_subrating => 0:41,
+    encryption_pause => 0:42,
+    esco_2m_phy => 0:45,
+    esco_3m_phy => 0:46,
+    three_slot_esco_edr_packets => 0:47,
+    extended_inquiry_response => 0:48,
+    simultaneous_le_bredr => 0:49,
+    simple_pairing => 0:51,
+    non_flushable_pb => 0:54,
+    secure_connections => 2:8
+}
+
+macro_rules! supported_le_features {
+    ($($id:ident => $bit:literal),*) => {
+        /// Convenience struct for checking what features are supported
+        #[derive(Clone)]
+        #[allow(missing_docs)]
+        pub struct SupportedLeFeatures {
+            $(pub $id: bool,)*
+        }
+
+        impl SupportedLeFeatures {
+            fn new(supported: u64) -> Self {
+                Self {
+                    $($id: supported & (1 << $bit) != 0,)*
+                }
+            }
+        }
+    }
+}
+
+supported_le_features! {
+    connection_parameter_request => 1,
+    connection_parameters_request => 2,
+    peripheral_initiated_feature_exchange => 3,
+    packet_extension => 5,
+    privacy => 6,
+    ble_2m_phy => 8,
+    ble_coded_phy => 11,
+    extended_advertising => 12,
+    periodic_advertising => 13,
+    periodic_advertising_sync_transfer_sender => 24,
+    periodic_advertising_sync_transfer_recipient => 25,
+    connected_iso_stream_central => 28,
+    connected_iso_stream_peripheral => 29,
+    iso_broadcaster => 30,
+    synchronized_receiver => 31
+}
+
 fn null_terminated_to_string(slice: &[u8]) -> String {
     let temp = std::str::from_utf8(slice).unwrap();
     temp[0..temp.find('\0').unwrap()].to_string()
index 57fad97..16da2a0 100644 (file)
@@ -8,6 +8,7 @@ pub mod error;
 pub mod facade;
 
 pub use bt_hci_custom_types::*;
+pub use controller::ControllerExports;
 
 use bt_common::time::Alarm;
 use bt_hal::HalExports;
index 6f7b7ad..6ae4f4f 100644 (file)
@@ -35,6 +35,9 @@ rust_ffi_static {
         "libnix",
         "liblog_rust",
     ],
+    proc_macros: [
+        "libpaste",
+    ],
     static_libs: [
         "libbt_callbacks_cxx",
     ],
@@ -47,6 +50,7 @@ cc_library_static {
         "libbt_init_flags_bridge_header",
         "libbt_shim_bridge_header",
         "libbt_hci_bridge_header",
+        "libbt_controller_bridge_header",
         "libbt_message_loop_thread_bridge_header",
         "cxx-bridge-header",
     ],
@@ -54,12 +58,14 @@ cc_library_static {
         "libbt_init_flags_bridge_code",
         "libbt_shim_bridge_code",
         "libbt_hci_bridge_code",
+        "libbt_controller_bridge_code",
         "libbt_message_loop_thread_bridge_code",
     ],
     export_generated_headers: [
         "libbt_init_flags_bridge_header",
         "libbt_shim_bridge_header",
         "libbt_hci_bridge_header",
+        "libbt_controller_bridge_header",
         "libbt_message_loop_thread_bridge_header",
         "cxx-bridge-header",
     ],
@@ -162,3 +168,19 @@ genrule {
     srcs: ["src/message_loop_thread.rs"],
     out: ["message_loop_thread.cc"],
 }
+
+genrule {
+    name: "libbt_controller_bridge_header",
+    tools: ["cxxbridge"],
+    cmd: "$(location cxxbridge) $(in) --header > $(out)",
+    srcs: ["src/controller.rs"],
+    out: ["src/controller.rs.h"],
+}
+
+genrule {
+    name: "libbt_controller_bridge_code",
+    tools: ["cxxbridge"],
+    cmd: "$(location cxxbridge) $(in) >> $(out)",
+    srcs: ["src/controller.rs"],
+    out: ["controller.cc"],
+}
diff --git a/gd/rust/shim/src/controller.rs b/gd/rust/shim/src/controller.rs
new file mode 100644 (file)
index 0000000..70036eb
--- /dev/null
@@ -0,0 +1,213 @@
+//! Controller shim
+
+use bt_hci::ControllerExports;
+use bt_packets::hci::OpCode;
+use paste::paste;
+use std::sync::Arc;
+
+#[cxx::bridge(namespace = bluetooth::shim::rust)]
+mod ffi {
+    extern "Rust" {
+        type Controller;
+
+        fn controller_supports_simple_pairing(c: &Controller) -> bool;
+        fn controller_supports_secure_connections(c: &Controller) -> bool;
+        fn controller_supports_simultaneous_le_bredr(c: &Controller) -> bool;
+        fn controller_supports_interlaced_inquiry_scan(c: &Controller) -> bool;
+        fn controller_supports_rssi_with_inquiry_results(c: &Controller) -> bool;
+        fn controller_supports_extended_inquiry_response(c: &Controller) -> bool;
+        fn controller_supports_role_switch(c: &Controller) -> bool;
+        fn controller_supports_three_slot_packets(c: &Controller) -> bool;
+        fn controller_supports_five_slot_packets(c: &Controller) -> bool;
+        fn controller_supports_classic_2m_phy(c: &Controller) -> bool;
+        fn controller_supports_classic_3m_phy(c: &Controller) -> bool;
+        fn controller_supports_three_slot_edr_packets(c: &Controller) -> bool;
+        fn controller_supports_five_slot_edr_packets(c: &Controller) -> bool;
+        fn controller_supports_sco(c: &Controller) -> bool;
+        fn controller_supports_hv2_packets(c: &Controller) -> bool;
+        fn controller_supports_hv3_packets(c: &Controller) -> bool;
+        fn controller_supports_ev3_packets(c: &Controller) -> bool;
+        fn controller_supports_ev4_packets(c: &Controller) -> bool;
+        fn controller_supports_ev5_packets(c: &Controller) -> bool;
+        fn controller_supports_esco_2m_phy(c: &Controller) -> bool;
+        fn controller_supports_esco_3m_phy(c: &Controller) -> bool;
+        fn controller_supports_three_slot_esco_edr_packets(c: &Controller) -> bool;
+        fn controller_supports_hold_mode(c: &Controller) -> bool;
+        fn controller_supports_sniff_mode(c: &Controller) -> bool;
+        fn controller_supports_park_mode(c: &Controller) -> bool;
+        fn controller_supports_non_flushable_pb(c: &Controller) -> bool;
+        fn controller_supports_sniff_subrating(c: &Controller) -> bool;
+        fn controller_supports_encryption_pause(c: &Controller) -> bool;
+        fn controller_supports_ble(c: &Controller) -> bool;
+
+        fn controller_supports_privacy(c: &Controller) -> bool;
+        fn controller_supports_packet_extension(c: &Controller) -> bool;
+        fn controller_supports_connection_parameters_request(c: &Controller) -> bool;
+        fn controller_supports_ble_2m_phy(c: &Controller) -> bool;
+        fn controller_supports_ble_coded_phy(c: &Controller) -> bool;
+        fn controller_supports_extended_advertising(c: &Controller) -> bool;
+        fn controller_supports_periodic_advertising(c: &Controller) -> bool;
+        fn controller_supports_peripheral_initiated_feature_exchange(c: &Controller) -> bool;
+        fn controller_supports_connection_parameter_request(c: &Controller) -> bool;
+        fn controller_supports_periodic_advertising_sync_transfer_sender(c: &Controller) -> bool;
+        fn controller_supports_periodic_advertising_sync_transfer_recipient(c: &Controller)
+            -> bool;
+        fn controller_supports_connected_iso_stream_central(c: &Controller) -> bool;
+        fn controller_supports_connected_iso_stream_peripheral(c: &Controller) -> bool;
+        fn controller_supports_iso_broadcaster(c: &Controller) -> bool;
+        fn controller_supports_synchronized_receiver(c: &Controller) -> bool;
+
+        fn controller_supports_reading_remote_extended_features(c: &Controller) -> bool;
+        fn controller_supports_enhanced_setup_synchronous_connection(c: &Controller) -> bool;
+        fn controller_supports_enhanced_accept_synchronous_connection(c: &Controller) -> bool;
+        fn controller_supports_ble_set_privacy_mode(c: &Controller) -> bool;
+
+        fn controller_get_acl_buffer_length(c: &Controller) -> u16;
+        fn controller_get_le_buffer_length(c: &Controller) -> u16;
+        fn controller_get_iso_buffer_length(c: &Controller) -> u16;
+        fn controller_get_le_suggested_default_data_length(c: &Controller) -> u16;
+        fn controller_get_le_maximum_tx_data_length(c: &Controller) -> u16;
+        fn controller_get_le_max_advertising_data_length(c: &Controller) -> u16;
+        fn controller_get_le_supported_advertising_sets(c: &Controller) -> u8;
+        fn controller_get_le_periodic_advertiser_list_size(c: &Controller) -> u8;
+        fn controller_get_acl_buffers(c: &Controller) -> u16;
+        fn controller_get_le_buffers(c: &Controller) -> u8;
+        fn controller_get_iso_buffers(c: &Controller) -> u8;
+        fn controller_get_le_connect_list_size(c: &Controller) -> u8;
+        fn controller_get_le_resolving_list_size(c: &Controller) -> u8;
+        fn controller_get_le_supported_states(c: &Controller) -> u64;
+
+        fn controller_get_address(c: &Controller) -> String;
+    }
+}
+
+pub type Controller = Arc<ControllerExports>;
+
+macro_rules! feature_getters {
+    ($($id:ident),*) => {
+        paste! {
+            $(
+                fn [<controller_supports_ $id>](c: &Controller) -> bool {
+                    c.features.$id
+                }
+            )*
+        }
+    }
+}
+
+feature_getters! {
+    simple_pairing,
+    secure_connections,
+    simultaneous_le_bredr,
+    interlaced_inquiry_scan,
+    rssi_with_inquiry_results,
+    extended_inquiry_response,
+    role_switch,
+    three_slot_packets,
+    five_slot_packets,
+    classic_2m_phy,
+    classic_3m_phy,
+    three_slot_edr_packets,
+    five_slot_edr_packets,
+    sco,
+    hv2_packets,
+    hv3_packets,
+    ev3_packets,
+    ev4_packets,
+    ev5_packets,
+    esco_2m_phy,
+    esco_3m_phy,
+    three_slot_esco_edr_packets,
+    hold_mode,
+    sniff_mode,
+    park_mode,
+    non_flushable_pb,
+    sniff_subrating,
+    encryption_pause,
+    ble
+}
+
+macro_rules! le_feature_getters {
+    ($($id:ident),*) => {
+        paste! {
+            $(
+                fn [<controller_supports_ $id>](c: &Controller) -> bool {
+                    c.le_features.$id
+                }
+            )*
+        }
+    }
+}
+
+le_feature_getters! {
+    privacy,
+    packet_extension,
+    connection_parameters_request,
+    ble_2m_phy,
+    ble_coded_phy,
+    extended_advertising,
+    periodic_advertising,
+    peripheral_initiated_feature_exchange,
+    connection_parameter_request,
+    periodic_advertising_sync_transfer_sender,
+    periodic_advertising_sync_transfer_recipient,
+    connected_iso_stream_central,
+    connected_iso_stream_peripheral,
+    iso_broadcaster,
+    synchronized_receiver
+}
+
+macro_rules! opcode_getters {
+    ($($id:ident => $opcode:path),*) => {
+        paste! {
+            $(
+                fn [<controller_supports_ $id>](c: &Controller) -> bool {
+                    c.commands.is_supported($opcode)
+                }
+            )*
+        }
+    }
+}
+
+opcode_getters! {
+    reading_remote_extended_features => OpCode::ReadRemoteSupportedFeatures,
+    enhanced_setup_synchronous_connection => OpCode::EnhancedSetupSynchronousConnection,
+    enhanced_accept_synchronous_connection => OpCode::EnhancedAcceptSynchronousConnection,
+    ble_set_privacy_mode => OpCode::LeSetPrivacyMode
+}
+
+macro_rules! field_getters {
+    ($($id:ident : $type:ty),*) => {
+        paste! {
+            $(
+                fn [<controller_get_ $id>](c: &Controller) -> $type {
+                    c.$id
+                }
+            )*
+        }
+    }
+}
+
+field_getters! {
+    acl_buffer_length: u16,
+    le_buffer_length: u16,
+    iso_buffer_length: u16,
+    le_suggested_default_data_length: u16,
+    le_max_advertising_data_length: u16,
+    le_supported_advertising_sets: u8,
+    le_periodic_advertiser_list_size: u8,
+    acl_buffers: u16,
+    le_buffers: u8,
+    iso_buffers: u8,
+    le_connect_list_size: u8,
+    le_resolving_list_size: u8,
+    le_supported_states: u64
+}
+
+fn controller_get_le_maximum_tx_data_length(c: &Controller) -> u16 {
+    c.le_max_data_length.supported_max_tx_octets
+}
+
+fn controller_get_address(c: &Controller) -> String {
+    c.address.to_string()
+}
index 65754b0..afe4e45 100644 (file)
@@ -2,6 +2,7 @@
 #[macro_use]
 extern crate lazy_static;
 
+mod controller;
 mod hci;
 mod init_flags;
 mod message_loop_thread;
index 7c06452..3e28d6d 100644 (file)
@@ -1,5 +1,6 @@
 //! Stack management
 
+use crate::controller::Controller;
 use crate::hci::Hci;
 use bt_common::init_flags;
 use bt_main::Stack;
@@ -17,12 +18,14 @@ mod ffi {
     extern "Rust" {
         type Stack;
         type Hci;
+        type Controller;
 
         fn stack_create() -> Box<Stack>;
         fn stack_start(stack: &mut Stack);
         fn stack_stop(stack: &mut Stack);
 
         fn get_hci(stack: &mut Stack) -> Box<Hci>;
+        fn get_controller(stack: &mut Stack) -> Box<Controller>;
     }
 }
 
@@ -58,3 +61,10 @@ pub fn get_hci(stack: &mut Stack) -> Box<Hci> {
 
     Box::new(Hci::new(stack.get_runtime(), stack.get_blocking::<bt_hci::HciExports>()))
 }
+
+pub fn get_controller(stack: &mut Stack) -> Box<Controller> {
+    assert!(init_flags::gd_rust_is_enabled());
+    assert!(init_flags::gd_controller_is_enabled());
+
+    Box::new(stack.get_blocking::<Controller>())
+}
index a903003..ce05b0a 100644 (file)
 
 #include "main/shim/controller.h"
 #include "btcore/include/module.h"
+#include "gd/common/init_flags.h"
 #include "main/shim/entry.h"
 #include "main/shim/shim.h"
+#include "main/shim/stack.h"
 #include "osi/include/future.h"
 #include "osi/include/log.h"
 
 #include "hci/controller.h"
+#include "src/controller.rs.h"
 
+using ::bluetooth::common::init_flags::gd_rust_is_enabled;
 using ::bluetooth::shim::GetController;
 
-constexpr uint8_t kMaxFeaturePage = 3;
-
 constexpr int kMaxSupportedCodecs = 8;  // MAX_LOCAL_SUPPORTED_CODECS_SIZE
 
 constexpr uint8_t kPhyLe1M = 0x01;
@@ -57,8 +59,6 @@ EXPORT_SYMBOL extern const module_t gd_controller_module = {
 
 struct {
   bool ready;
-  uint64_t feature[kMaxFeaturePage];
-  uint64_t le_feature[kMaxFeaturePage];
   RawAddress raw_address;
   bt_version_t bt_version;
   uint8_t local_supported_codecs[kMaxSupportedCodecs];
@@ -71,13 +71,28 @@ static future_t* start_up(void) {
   LOG_INFO("%s Starting up", __func__);
   data_.ready = true;
 
-  std::string string_address = GetController()->GetMacAddress().ToString();
-  RawAddress::FromString(string_address, data_.raw_address);
+  if (gd_rust_is_enabled()) {
+    auto controller =
+        bluetooth::shim::Stack::GetInstance()->GetRustController();
+    auto rust_string_address =
+        bluetooth::shim::rust::controller_get_address(**controller);
+    auto string_address =
+        std::string(rust_string_address.data(), rust_string_address.length());
+    RawAddress::FromString(string_address, data_.raw_address);
+
+    data_.le_supported_states =
+        bluetooth::shim::rust::controller_get_le_supported_states(**controller);
 
-  data_.le_supported_states =
-      bluetooth::shim::GetController()->GetLeSupportedStates();
+    LOG_INFO("Mac address:%s", string_address.c_str());
+  } else {
+    std::string string_address = GetController()->GetMacAddress().ToString();
+    RawAddress::FromString(string_address, data_.raw_address);
 
-  LOG_INFO("Mac address:%s", string_address.c_str());
+    data_.le_supported_states =
+        bluetooth::shim::GetController()->GetLeSupportedStates();
+
+    LOG_INFO("Mac address:%s", string_address.c_str());
+  }
 
   data_.phy = kPhyLe1M;
 
@@ -112,8 +127,15 @@ static const uint8_t* get_ble_supported_states(void) {
   return (const uint8_t*)&data_.le_supported_states;
 }
 
-#define MAP_TO_GD(legacy, gd) \
-  static bool legacy(void) { return GetController()->gd(); }
+#define MAP_TO_GD(legacy, gd)                                            \
+  static bool legacy(void) {                                             \
+    if (gd_rust_is_enabled()) {                                          \
+      return bluetooth::shim::rust::controller_##legacy(                 \
+          **bluetooth::shim::Stack::GetInstance()->GetRustController()); \
+    } else {                                                             \
+      return GetController()->gd();                                      \
+    }                                                                    \
+  }
 
 MAP_TO_GD(supports_simple_pairing, SupportsSimplePairing)
 MAP_TO_GD(supports_secure_connections, SupportsSecureConnections)
@@ -121,13 +143,12 @@ MAP_TO_GD(supports_simultaneous_le_bredr, SupportsSimultaneousLeBrEdr)
 MAP_TO_GD(supports_interlaced_inquiry_scan, SupportsInterlacedInquiryScan)
 MAP_TO_GD(supports_rssi_with_inquiry_results, SupportsRssiWithInquiryResults)
 MAP_TO_GD(supports_extended_inquiry_response, SupportsExtendedInquiryResponse)
-MAP_TO_GD(supports_central_peripheral_role_switch, SupportsRoleSwitch)
-MAP_TO_GD(supports_3_slot_packets, Supports3SlotPackets)
-MAP_TO_GD(supports_5_slot_packets, Supports5SlotPackets)
+MAP_TO_GD(supports_three_slot_packets, Supports3SlotPackets)
+MAP_TO_GD(supports_five_slot_packets, Supports5SlotPackets)
 MAP_TO_GD(supports_classic_2m_phy, SupportsClassic2mPhy)
 MAP_TO_GD(supports_classic_3m_phy, SupportsClassic3mPhy)
-MAP_TO_GD(supports_3_slot_edr_packets, Supports3SlotEdrPackets)
-MAP_TO_GD(supports_5_slot_edr_packets, Supports5SlotEdrPackets)
+MAP_TO_GD(supports_three_slot_edr_packets, Supports3SlotEdrPackets)
+MAP_TO_GD(supports_five_slot_edr_packets, Supports5SlotEdrPackets)
 MAP_TO_GD(supports_sco, SupportsSco)
 MAP_TO_GD(supports_hv2_packets, SupportsHv2Packets)
 MAP_TO_GD(supports_hv3_packets, SupportsHv3Packets)
@@ -136,7 +157,7 @@ MAP_TO_GD(supports_ev4_packets, SupportsEv4Packets)
 MAP_TO_GD(supports_ev5_packets, SupportsEv5Packets)
 MAP_TO_GD(supports_esco_2m_phy, SupportsEsco2mPhy)
 MAP_TO_GD(supports_esco_3m_phy, SupportsEsco3mPhy)
-MAP_TO_GD(supports_3_slot_esco_edr_packets, Supports3SlotEscoEdrPackets)
+MAP_TO_GD(supports_three_slot_esco_edr_packets, Supports3SlotEscoEdrPackets)
 MAP_TO_GD(supports_role_switch, SupportsRoleSwitch)
 MAP_TO_GD(supports_hold_mode, SupportsHoldMode)
 MAP_TO_GD(supports_sniff_mode, SupportsSniffMode)
@@ -146,116 +167,116 @@ MAP_TO_GD(supports_sniff_subrating, SupportsSniffSubrating)
 MAP_TO_GD(supports_encryption_pause, SupportsEncryptionPause)
 
 MAP_TO_GD(supports_ble, SupportsBle)
-MAP_TO_GD(supports_ble_privacy, SupportsBlePrivacy)
-MAP_TO_GD(supports_ble_packet_extension, SupportsBlePacketExtension)
-MAP_TO_GD(supports_ble_connection_parameters_request,
+MAP_TO_GD(supports_privacy, SupportsBlePrivacy)
+MAP_TO_GD(supports_packet_extension, SupportsBlePacketExtension)
+MAP_TO_GD(supports_connection_parameters_request,
           SupportsBleConnectionParametersRequest)
 MAP_TO_GD(supports_ble_2m_phy, SupportsBle2mPhy)
 MAP_TO_GD(supports_ble_coded_phy, SupportsBleCodedPhy)
-MAP_TO_GD(supports_ble_extended_advertising, SupportsBleExtendedAdvertising)
-MAP_TO_GD(supports_ble_periodic_advertising, SupportsBlePeriodicAdvertising)
-MAP_TO_GD(supports_ble_peripheral_initiated_feature_exchange,
+MAP_TO_GD(supports_extended_advertising, SupportsBleExtendedAdvertising)
+MAP_TO_GD(supports_periodic_advertising, SupportsBlePeriodicAdvertising)
+MAP_TO_GD(supports_peripheral_initiated_feature_exchange,
           SupportsBlePeripheralInitiatedFeatureExchange)
-MAP_TO_GD(supports_ble_connection_parameter_request,
+MAP_TO_GD(supports_connection_parameter_request,
           SupportsBleConnectionParameterRequest)
 
-MAP_TO_GD(supports_ble_periodic_advertising_sync_transfer_sender,
+MAP_TO_GD(supports_periodic_advertising_sync_transfer_sender,
           SupportsBlePeriodicAdvertisingSyncTransferSender)
-MAP_TO_GD(supports_ble_periodic_advertising_sync_transfer_recipient,
+MAP_TO_GD(supports_periodic_advertising_sync_transfer_recipient,
           SupportsBlePeriodicAdvertisingSyncTransferRecipient)
-MAP_TO_GD(supports_ble_connected_isochronous_stream_central,
+MAP_TO_GD(supports_connected_iso_stream_central,
           SupportsBleConnectedIsochronousStreamCentral)
-MAP_TO_GD(supports_ble_connected_isochronous_stream_peripheral,
+MAP_TO_GD(supports_connected_iso_stream_peripheral,
           SupportsBleConnectedIsochronousStreamPeripheral)
-MAP_TO_GD(supports_ble_isochronous_broadcaster,
-          SupportsBleIsochronousBroadcaster)
-MAP_TO_GD(supports_ble_synchronized_receiver, SupportsBleSynchronizedReceiver)
-
-static bool supports_reading_remote_extended_features(void) {
-  return GetController()->IsSupported(
-      (bluetooth::hci::OpCode)kReadRemoteExtendedFeatures);
-}
-
-static bool supports_enhanced_setup_synchronous_connection(void) {
-  return GetController()->IsSupported(
-      (bluetooth::hci::OpCode)kEnhancedSetupSynchronousConnection);
-}
-
-static bool supports_enhanced_accept_synchronous_connection(void) {
-  return GetController()->IsSupported(
-      (bluetooth::hci::OpCode)kEnhancedAcceptSynchronousConnection);
-}
-
-static bool supports_ble_set_privacy_mode() {
-  return GetController()->IsSupported(
-      (bluetooth::hci::OpCode)kLeSetPrivacyMode);
-}
-
-static uint16_t get_acl_data_size_classic(void) {
-  return GetController()->GetAclPacketLength();
-}
+MAP_TO_GD(supports_iso_broadcaster, SupportsBleIsochronousBroadcaster)
+MAP_TO_GD(supports_synchronized_receiver, SupportsBleSynchronizedReceiver)
+
+#define FORWARD_IF_RUST(legacy, gd)                                      \
+  static bool legacy(void) {                                             \
+    if (gd_rust_is_enabled()) {                                          \
+      return bluetooth::shim::rust::controller_##legacy(                 \
+          **bluetooth::shim::Stack::GetInstance()->GetRustController()); \
+    } else {                                                             \
+      return gd;                                                         \
+    }                                                                    \
+  }
 
-static uint16_t get_acl_data_size_ble(void) {
-  return GetController()->GetLeBufferSize().le_data_packet_length_;
-}
+FORWARD_IF_RUST(supports_reading_remote_extended_features,
+                GetController()->IsSupported((bluetooth::hci::OpCode)
+                                                 kReadRemoteExtendedFeatures))
+FORWARD_IF_RUST(supports_enhanced_setup_synchronous_connection,
+                GetController()->IsSupported((
+                    bluetooth::hci::OpCode)kEnhancedSetupSynchronousConnection))
+FORWARD_IF_RUST(
+    supports_enhanced_accept_synchronous_connection,
+    GetController()->IsSupported((bluetooth::hci::OpCode)
+                                     kEnhancedAcceptSynchronousConnection))
+FORWARD_IF_RUST(
+    supports_ble_set_privacy_mode,
+    GetController()->IsSupported((bluetooth::hci::OpCode)kLeSetPrivacyMode))
+
+#define FORWARD_GETTER_IF_RUST(type, legacy, gd)                         \
+  static type legacy(void) {                                             \
+    if (gd_rust_is_enabled()) {                                          \
+      return bluetooth::shim::rust::controller_##legacy(                 \
+          **bluetooth::shim::Stack::GetInstance()->GetRustController()); \
+    } else {                                                             \
+      return gd;                                                         \
+    }                                                                    \
+  }
 
-static uint16_t get_iso_data_size(void) {
-  return GetController()->GetControllerIsoBufferSize().le_data_packet_length_;
-}
+FORWARD_GETTER_IF_RUST(uint16_t, get_acl_buffer_length,
+                       GetController()->GetAclPacketLength())
+FORWARD_GETTER_IF_RUST(
+    uint16_t, get_le_buffer_length,
+    GetController()->GetLeBufferSize().le_data_packet_length_)
+FORWARD_GETTER_IF_RUST(
+    uint16_t, get_iso_buffer_length,
+    GetController()->GetControllerIsoBufferSize().le_data_packet_length_)
 
 static uint16_t get_acl_packet_size_classic(void) {
-  return get_acl_data_size_classic() + kHciDataPreambleSize;
+  return get_acl_buffer_length() + kHciDataPreambleSize;
 }
 
 static uint16_t get_acl_packet_size_ble(void) {
-  return get_acl_data_size_ble() + kHciDataPreambleSize;
+  return get_le_buffer_length() + kHciDataPreambleSize;
 }
 
 static uint16_t get_iso_packet_size(void) {
-  return get_iso_data_size() + kHciDataPreambleSize;
-}
-
-static uint16_t get_ble_suggested_default_data_length(void) {
-  return GetController()->GetLeSuggestedDefaultDataLength();
-}
-
-static uint16_t get_ble_maximum_tx_data_length(void) {
-  ::bluetooth::hci::LeMaximumDataLength le_maximum_data_length =
-      GetController()->GetLeMaximumDataLength();
-  return le_maximum_data_length.supported_max_tx_octets_;
-}
-
-static uint16_t get_ble_maxium_advertising_data_length(void) {
-  return GetController()->GetLeMaximumAdvertisingDataLength();
-}
-
-static uint8_t get_ble_number_of_supported_advertising_sets(void) {
-  return GetController()->GetLeNumberOfSupportedAdverisingSets();
-}
-
-static uint8_t get_ble_periodic_advertiser_list_size(void) {
-  return GetController()->GetLePeriodicAdvertiserListSize();
-}
-
-static uint16_t get_acl_buffer_count_classic(void) {
-  return GetController()->GetNumAclPacketBuffers();
-}
-
-static uint8_t get_acl_buffer_count_ble(void) {
-  return GetController()->GetLeBufferSize().total_num_le_packets_;
+  return get_iso_buffer_length() + kHciDataPreambleSize;
 }
 
-static uint8_t get_iso_buffer_count(void) {
-  return GetController()->GetControllerIsoBufferSize().total_num_le_packets_;
+FORWARD_GETTER_IF_RUST(uint16_t, get_le_suggested_default_data_length,
+                       GetController()->GetLeSuggestedDefaultDataLength())
+
+static uint16_t get_le_maximum_tx_data_length(void) {
+  if (gd_rust_is_enabled()) {
+    return bluetooth::shim::rust::controller_get_le_maximum_tx_data_length(
+        **bluetooth::shim::Stack::GetInstance()->GetRustController());
+  } else {
+    ::bluetooth::hci::LeMaximumDataLength le_maximum_data_length =
+        GetController()->GetLeMaximumDataLength();
+    return le_maximum_data_length.supported_max_tx_octets_;
+  }
 }
 
-static uint8_t get_ble_connect_list_size(void) {
-  return GetController()->GetLeConnectListSize();
-}
-
-static uint8_t get_ble_resolving_list_max_size(void) {
-  return GetController()->GetLeResolvingListSize();
-}
+FORWARD_GETTER_IF_RUST(uint16_t, get_le_max_advertising_data_length,
+                       GetController()->GetLeMaximumAdvertisingDataLength())
+FORWARD_GETTER_IF_RUST(uint8_t, get_le_supported_advertising_sets,
+                       GetController()->GetLeNumberOfSupportedAdverisingSets())
+FORWARD_GETTER_IF_RUST(uint8_t, get_le_periodic_advertiser_list_size,
+                       GetController()->GetLePeriodicAdvertiserListSize())
+FORWARD_GETTER_IF_RUST(uint16_t, get_acl_buffers,
+                       GetController()->GetNumAclPacketBuffers())
+FORWARD_GETTER_IF_RUST(uint8_t, get_le_buffers,
+                       GetController()->GetLeBufferSize().total_num_le_packets_)
+FORWARD_GETTER_IF_RUST(
+    uint8_t, get_iso_buffers,
+    GetController()->GetControllerIsoBufferSize().total_num_le_packets_)
+FORWARD_GETTER_IF_RUST(uint8_t, get_le_connect_list_size,
+                       GetController()->GetLeConnectListSize())
+FORWARD_GETTER_IF_RUST(uint8_t, get_le_resolving_list_size,
+                       GetController()->GetLeResolvingListSize())
 
 static void set_ble_resolving_list_max_size(int resolving_list_max_size) {
   LOG_WARN("%s TODO Unimplemented", __func__);
@@ -278,15 +299,15 @@ static const controller_t interface = {
     supports_interlaced_inquiry_scan,
     supports_rssi_with_inquiry_results,
     supports_extended_inquiry_response,
-    supports_central_peripheral_role_switch,
+    supports_role_switch,
     supports_enhanced_setup_synchronous_connection,
     supports_enhanced_accept_synchronous_connection,
-    supports_3_slot_packets,
-    supports_5_slot_packets,
+    supports_three_slot_packets,
+    supports_five_slot_packets,
     supports_classic_2m_phy,
     supports_classic_3m_phy,
-    supports_3_slot_edr_packets,
-    supports_5_slot_edr_packets,
+    supports_three_slot_edr_packets,
+    supports_five_slot_edr_packets,
     supports_sco,
     supports_hv2_packets,
     supports_hv3_packets,
@@ -295,7 +316,7 @@ static const controller_t interface = {
     supports_ev5_packets,
     supports_esco_2m_phy,
     supports_esco_3m_phy,
-    supports_3_slot_esco_edr_packets,
+    supports_three_slot_esco_edr_packets,
     supports_role_switch,
     supports_hold_mode,
     supports_sniff_mode,
@@ -305,43 +326,43 @@ static const controller_t interface = {
     supports_encryption_pause,
 
     supports_ble,
-    supports_ble_packet_extension,
-    supports_ble_connection_parameters_request,
-    supports_ble_privacy,
+    supports_packet_extension,
+    supports_connection_parameters_request,
+    supports_privacy,
     supports_ble_set_privacy_mode,
     supports_ble_2m_phy,
     supports_ble_coded_phy,
-    supports_ble_extended_advertising,
-    supports_ble_periodic_advertising,
-    supports_ble_peripheral_initiated_feature_exchange,
-    supports_ble_connection_parameter_request,
-    supports_ble_periodic_advertising_sync_transfer_sender,
-    supports_ble_periodic_advertising_sync_transfer_recipient,
-    supports_ble_connected_isochronous_stream_central,
-    supports_ble_connected_isochronous_stream_peripheral,
-    supports_ble_isochronous_broadcaster,
-    supports_ble_synchronized_receiver,
-
-    get_acl_data_size_classic,
-    get_acl_data_size_ble,
-    get_iso_data_size,
+    supports_extended_advertising,
+    supports_periodic_advertising,
+    supports_peripheral_initiated_feature_exchange,
+    supports_connection_parameter_request,
+    supports_periodic_advertising_sync_transfer_sender,
+    supports_periodic_advertising_sync_transfer_recipient,
+    supports_connected_iso_stream_central,
+    supports_connected_iso_stream_peripheral,
+    supports_iso_broadcaster,
+    supports_synchronized_receiver,
+
+    get_acl_buffer_length,
+    get_le_buffer_length,
+    get_iso_buffer_length,
 
     get_acl_packet_size_classic,
     get_acl_packet_size_ble,
     get_iso_packet_size,
-    get_ble_suggested_default_data_length,
-    get_ble_maximum_tx_data_length,
-    get_ble_maxium_advertising_data_length,
-    get_ble_number_of_supported_advertising_sets,
-    get_ble_periodic_advertiser_list_size,
+    get_le_suggested_default_data_length,
+    get_le_maximum_tx_data_length,
+    get_le_max_advertising_data_length,
+    get_le_supported_advertising_sets,
+    get_le_periodic_advertiser_list_size,
 
-    get_acl_buffer_count_classic,
-    get_acl_buffer_count_ble,
-    get_iso_buffer_count,
+    get_acl_buffers,
+    get_le_buffers,
+    get_iso_buffers,
 
-    get_ble_connect_list_size,
+    get_le_connect_list_size,
 
-    get_ble_resolving_list_max_size,
+    get_le_resolving_list_size,
     set_ble_resolving_list_max_size,
     get_local_supported_codecs,
     get_le_all_initiating_phys};
index b59b666..d5fe1a2 100644 (file)
@@ -77,6 +77,10 @@ void Stack::StartEverything() {
     if (common::init_flags::gd_hci_is_enabled()) {
       rust_hci_ = new ::rust::Box<rust::Hci>(rust::get_hci(**rust_stack_));
     }
+    if (common::init_flags::gd_controller_is_enabled()) {
+      rust_controller_ = new ::rust::Box<rust::Controller>(
+          rust::get_controller(**rust_stack_));
+    }
     return;
   }
 
index 37f6f7b..a981e63 100644 (file)
@@ -57,6 +57,9 @@ class Stack {
   os::Handler* GetHandler();
 
   ::rust::Box<rust::Hci>* GetRustHci() { return rust_hci_; }
+  ::rust::Box<rust::Controller>* GetRustController() {
+    return rust_controller_;
+  }
 
   DISALLOW_COPY_AND_ASSIGN(Stack);
 
@@ -70,6 +73,7 @@ class Stack {
   Btm* btm_ = nullptr;
   ::rust::Box<rust::Stack>* rust_stack_ = nullptr;
   ::rust::Box<rust::Hci>* rust_hci_ = nullptr;
+  ::rust::Box<rust::Controller>* rust_controller_ = nullptr;
 
   void Start(ModuleList* modules);
 };