1 //! Stack management 2 3 use crate::controller::Controller; 4 use crate::hci::Hci; 5 use bluetooth_rs::hci::ControllerExports; 6 use bt_common::init_flags; 7 use std::ops::{Deref, DerefMut}; 8 use std::sync::Arc; 9 use tokio::runtime::{Builder, Runtime}; 10 11 pub struct Stack(bluetooth_rs::Stack); 12 13 impl Deref for Stack { 14 type Target = bluetooth_rs::Stack; deref(&self) -> &Self::Target15 fn deref(&self) -> &Self::Target { 16 &self.0 17 } 18 } 19 20 impl DerefMut for Stack { deref_mut(&mut self) -> &mut Self::Target21 fn deref_mut(&mut self) -> &mut Self::Target { 22 &mut self.0 23 } 24 } 25 26 lazy_static! { 27 pub static ref RUNTIME: Arc<Runtime> = Arc::new( 28 Builder::new_multi_thread() 29 .worker_threads(1) 30 .max_blocking_threads(1) 31 .enable_all() 32 .build() 33 .unwrap() 34 ); 35 } 36 stack_create() -> Box<Stack>37pub fn stack_create() -> Box<Stack> { 38 assert!(init_flags::gd_rust_is_enabled()); 39 40 let local_rt = RUNTIME.clone(); 41 RUNTIME.block_on(async move { 42 let stack = bluetooth_rs::Stack::new(local_rt).await; 43 stack.use_default_snoop().await; 44 45 Box::new(Stack(stack)) 46 }) 47 } 48 stack_start(_stack: &mut Stack)49pub fn stack_start(_stack: &mut Stack) { 50 assert!(init_flags::gd_rust_is_enabled()); 51 } 52 stack_stop(stack: &mut Stack)53pub fn stack_stop(stack: &mut Stack) { 54 assert!(init_flags::gd_rust_is_enabled()); 55 56 stack.stop_blocking(); 57 } 58 get_hci(stack: &mut Stack) -> Box<Hci>59pub fn get_hci(stack: &mut Stack) -> Box<Hci> { 60 assert!(init_flags::gd_rust_is_enabled()); 61 62 Box::new(Hci::new( 63 stack.get_runtime(), 64 stack.get_blocking::<bluetooth_rs::hci::facade::HciFacadeService>(), 65 )) 66 } 67 get_controller(stack: &mut Stack) -> Box<Controller>68pub fn get_controller(stack: &mut Stack) -> Box<Controller> { 69 assert!(init_flags::gd_rust_is_enabled()); 70 71 Box::new(Controller(stack.get_blocking::<Arc<ControllerExports>>())) 72 } 73