Loading system/gd/hal/hci_hal_host.cc +2 −0 Original line number Diff line number Diff line Loading @@ -362,6 +362,8 @@ class HciHalHost : public HciHal { ASSERT_LOG(received_size != -1, "Can't receive from socket: %s", strerror(errno)); if (received_size == 0) { LOG_WARN("Can't read H4 header. EOF received"); // First close sock fd before raising sigint close(sock_fd_); raise(SIGINT); return; } Loading system/gd/rust/linux/service/Cargo.toml +2 −0 Original line number Diff line number Diff line Loading @@ -16,7 +16,9 @@ dbus-crossroads = "0.4.0" dbus-tokio = "0.7.3" env_logger = "0.8.3" futures = "0.3.13" lazy_static = "1.4" log = "0.4.14" nix = "0.19" num-traits = "*" tokio = { version = "1", features = ['bytes', 'fs', 'io-util', 'libc', 'macros', 'memchr', 'mio', 'net', 'num_cpus', 'rt', 'rt-multi-thread', 'sync', 'time', 'tokio-macros'] } syslog = "4.0" Loading system/gd/rust/linux/service/src/main.rs +47 −2 Original line number Diff line number Diff line extern crate clap; #[macro_use] extern crate lazy_static; use clap::{App, AppSettings, Arg}; use dbus::{channel::MatchingReceiver, message::MatchRule}; Loading @@ -6,8 +8,9 @@ use dbus_crossroads::Crossroads; use dbus_tokio::connection; use futures::future; use log::LevelFilter; use nix::sys::signal; use std::error::Error; use std::sync::{Arc, Mutex}; use std::sync::{Arc, Condvar, Mutex}; use syslog::{BasicLogger, Facility, Formatter3164}; use bt_topshim::{btif::get_btinterface, topstack}; Loading @@ -19,9 +22,10 @@ use btstack::{ bluetooth_media::BluetoothMedia, socket_manager::BluetoothSocketManager, suspend::Suspend, Stack, Message, Stack, }; use dbus_projection::DisconnectWatcher; use tokio::sync::mpsc::Sender; mod dbus_arg; mod iface_battery_manager; Loading Loading @@ -100,6 +104,7 @@ fn main() -> Result<(), Box<dyn Error>> { } let (tx, rx) = Stack::create_channel(); let sig_notifier = Arc::new((Mutex::new(false), Condvar::new())); let intf = Arc::new(Mutex::new(get_btinterface().unwrap())); let bluetooth_gatt = Loading @@ -112,6 +117,7 @@ fn main() -> Result<(), Box<dyn Error>> { tx.clone(), intf.clone(), bluetooth_media.clone(), sig_notifier.clone(), )))); let suspend = Arc::new(Mutex::new(Box::new(Suspend::new( bluetooth.clone(), Loading Loading @@ -274,6 +280,19 @@ fn main() -> Result<(), Box<dyn Error>> { bluetooth_gatt.lock().unwrap().init_profiles(tx.clone(), adapter.clone()); bt_sock_mgr.lock().unwrap().initialize(intf.clone()); // Install SIGTERM handler so that we can properly shutdown *SIG_DATA.lock().unwrap() = Some((tx.clone(), sig_notifier.clone())); let sig_action = signal::SigAction::new( signal::SigHandler::Handler(handle_sigterm), signal::SaFlags::empty(), signal::SigSet::empty(), ); unsafe { signal::sigaction(signal::SIGTERM, &sig_action).unwrap(); } } // Serve clients forever. Loading @@ -281,3 +300,29 @@ fn main() -> Result<(), Box<dyn Error>> { unreachable!() }) } lazy_static! { /// Data needed for signal handling. static ref SIG_DATA: Mutex<Option<(Sender<Message>, Arc<(Mutex<bool>, Condvar)>)>> = Mutex::new(None); } extern "C" fn handle_sigterm(_signum: i32) { let guard = SIG_DATA.lock().unwrap(); if let Some((tx, notifier)) = guard.as_ref() { log::debug!("Handling SIGTERM by disabling the adapter!"); let txl = tx.clone(); tokio::spawn(async move { // Send the shutdown message here. let _ = txl.send(Message::Shutdown).await; }); let guard = notifier.0.lock().unwrap(); if *guard { log::debug!("Waiting for stack to turn off for 2s"); let _ = notifier.1.wait_timeout(guard, std::time::Duration::from_millis(2000)); } } log::debug!("Sigterm completed"); std::process::exit(0); } system/gd/rust/linux/stack/src/bluetooth.rs +14 −2 Original line number Diff line number Diff line Loading @@ -22,8 +22,7 @@ use log::{debug, error, warn}; use num_traits::cast::ToPrimitive; use std::collections::HashMap; use std::collections::VecDeque; use std::sync::Arc; use std::sync::Mutex; use std::sync::{Arc, Condvar, Mutex}; use std::time::Duration; use std::time::Instant; use tokio::sync::mpsc::Sender; Loading Loading @@ -352,6 +351,9 @@ pub struct Bluetooth { // Internal API members internal_le_rand_queue: VecDeque<OneShotSender<u64>>, discoverable_timeout: Option<JoinHandle<()>>, /// Used to notify signal handler that we have turned off the stack. sig_notifier: Arc<(Mutex<bool>, Condvar)>, } impl Bluetooth { Loading @@ -360,6 +362,7 @@ impl Bluetooth { tx: Sender<Message>, intf: Arc<Mutex<BluetoothInterface>>, bluetooth_media: Arc<Mutex<Box<BluetoothMedia>>>, sig_notifier: Arc<(Mutex<bool>, Condvar)>, ) -> Bluetooth { Bluetooth { bonded_devices: HashMap::new(), Loading Loading @@ -387,6 +390,7 @@ impl Bluetooth { // Internal API members internal_le_rand_queue: VecDeque::<OneShotSender<u64>>::new(), discoverable_timeout: None, sig_notifier, } } Loading Loading @@ -697,6 +701,10 @@ impl BtifBluetoothCallbacks for Bluetooth { if self.state == BtState::Off { self.properties.clear(); // Let the signal notifier know we are turned off. *self.sig_notifier.0.lock().unwrap() = false; self.sig_notifier.1.notify_all(); } else { // Trigger properties update self.intf.lock().unwrap().get_adapter_properties(); Loading @@ -706,6 +714,10 @@ impl BtifBluetoothCallbacks for Bluetooth { // Ensure device is connectable so that disconnected device can reconnect self.set_connectable(true); // Notify the signal notifier that we are turned on. *self.sig_notifier.0.lock().unwrap() = true; self.sig_notifier.1.notify_all(); } } Loading system/gd/rust/linux/stack/src/lib.rs +8 −1 Original line number Diff line number Diff line Loading @@ -22,7 +22,7 @@ use std::sync::{Arc, Mutex}; use tokio::sync::mpsc::channel; use tokio::sync::mpsc::{Receiver, Sender}; use crate::bluetooth::Bluetooth; use crate::bluetooth::{Bluetooth, IBluetooth}; use crate::bluetooth_gatt::BluetoothGatt; use crate::bluetooth_media::{BluetoothMedia, MediaActions}; use crate::socket_manager::{BluetoothSocketManager, SocketActions}; Loading @@ -39,6 +39,9 @@ use bt_topshim::{ /// Message types that are sent to the stack main dispatch loop. pub enum Message { // Shuts down the stack. Shutdown, // Callbacks from libbluetooth A2dp(A2dpCallbacks), Avrcp(AvrcpCallbacks), Loading Loading @@ -119,6 +122,10 @@ impl Stack { } match m.unwrap() { Message::Shutdown => { bluetooth.lock().unwrap().disable(); } Message::A2dp(a) => { bluetooth_media.lock().unwrap().dispatch_a2dp_callbacks(a); } Loading Loading
system/gd/hal/hci_hal_host.cc +2 −0 Original line number Diff line number Diff line Loading @@ -362,6 +362,8 @@ class HciHalHost : public HciHal { ASSERT_LOG(received_size != -1, "Can't receive from socket: %s", strerror(errno)); if (received_size == 0) { LOG_WARN("Can't read H4 header. EOF received"); // First close sock fd before raising sigint close(sock_fd_); raise(SIGINT); return; } Loading
system/gd/rust/linux/service/Cargo.toml +2 −0 Original line number Diff line number Diff line Loading @@ -16,7 +16,9 @@ dbus-crossroads = "0.4.0" dbus-tokio = "0.7.3" env_logger = "0.8.3" futures = "0.3.13" lazy_static = "1.4" log = "0.4.14" nix = "0.19" num-traits = "*" tokio = { version = "1", features = ['bytes', 'fs', 'io-util', 'libc', 'macros', 'memchr', 'mio', 'net', 'num_cpus', 'rt', 'rt-multi-thread', 'sync', 'time', 'tokio-macros'] } syslog = "4.0" Loading
system/gd/rust/linux/service/src/main.rs +47 −2 Original line number Diff line number Diff line extern crate clap; #[macro_use] extern crate lazy_static; use clap::{App, AppSettings, Arg}; use dbus::{channel::MatchingReceiver, message::MatchRule}; Loading @@ -6,8 +8,9 @@ use dbus_crossroads::Crossroads; use dbus_tokio::connection; use futures::future; use log::LevelFilter; use nix::sys::signal; use std::error::Error; use std::sync::{Arc, Mutex}; use std::sync::{Arc, Condvar, Mutex}; use syslog::{BasicLogger, Facility, Formatter3164}; use bt_topshim::{btif::get_btinterface, topstack}; Loading @@ -19,9 +22,10 @@ use btstack::{ bluetooth_media::BluetoothMedia, socket_manager::BluetoothSocketManager, suspend::Suspend, Stack, Message, Stack, }; use dbus_projection::DisconnectWatcher; use tokio::sync::mpsc::Sender; mod dbus_arg; mod iface_battery_manager; Loading Loading @@ -100,6 +104,7 @@ fn main() -> Result<(), Box<dyn Error>> { } let (tx, rx) = Stack::create_channel(); let sig_notifier = Arc::new((Mutex::new(false), Condvar::new())); let intf = Arc::new(Mutex::new(get_btinterface().unwrap())); let bluetooth_gatt = Loading @@ -112,6 +117,7 @@ fn main() -> Result<(), Box<dyn Error>> { tx.clone(), intf.clone(), bluetooth_media.clone(), sig_notifier.clone(), )))); let suspend = Arc::new(Mutex::new(Box::new(Suspend::new( bluetooth.clone(), Loading Loading @@ -274,6 +280,19 @@ fn main() -> Result<(), Box<dyn Error>> { bluetooth_gatt.lock().unwrap().init_profiles(tx.clone(), adapter.clone()); bt_sock_mgr.lock().unwrap().initialize(intf.clone()); // Install SIGTERM handler so that we can properly shutdown *SIG_DATA.lock().unwrap() = Some((tx.clone(), sig_notifier.clone())); let sig_action = signal::SigAction::new( signal::SigHandler::Handler(handle_sigterm), signal::SaFlags::empty(), signal::SigSet::empty(), ); unsafe { signal::sigaction(signal::SIGTERM, &sig_action).unwrap(); } } // Serve clients forever. Loading @@ -281,3 +300,29 @@ fn main() -> Result<(), Box<dyn Error>> { unreachable!() }) } lazy_static! { /// Data needed for signal handling. static ref SIG_DATA: Mutex<Option<(Sender<Message>, Arc<(Mutex<bool>, Condvar)>)>> = Mutex::new(None); } extern "C" fn handle_sigterm(_signum: i32) { let guard = SIG_DATA.lock().unwrap(); if let Some((tx, notifier)) = guard.as_ref() { log::debug!("Handling SIGTERM by disabling the adapter!"); let txl = tx.clone(); tokio::spawn(async move { // Send the shutdown message here. let _ = txl.send(Message::Shutdown).await; }); let guard = notifier.0.lock().unwrap(); if *guard { log::debug!("Waiting for stack to turn off for 2s"); let _ = notifier.1.wait_timeout(guard, std::time::Duration::from_millis(2000)); } } log::debug!("Sigterm completed"); std::process::exit(0); }
system/gd/rust/linux/stack/src/bluetooth.rs +14 −2 Original line number Diff line number Diff line Loading @@ -22,8 +22,7 @@ use log::{debug, error, warn}; use num_traits::cast::ToPrimitive; use std::collections::HashMap; use std::collections::VecDeque; use std::sync::Arc; use std::sync::Mutex; use std::sync::{Arc, Condvar, Mutex}; use std::time::Duration; use std::time::Instant; use tokio::sync::mpsc::Sender; Loading Loading @@ -352,6 +351,9 @@ pub struct Bluetooth { // Internal API members internal_le_rand_queue: VecDeque<OneShotSender<u64>>, discoverable_timeout: Option<JoinHandle<()>>, /// Used to notify signal handler that we have turned off the stack. sig_notifier: Arc<(Mutex<bool>, Condvar)>, } impl Bluetooth { Loading @@ -360,6 +362,7 @@ impl Bluetooth { tx: Sender<Message>, intf: Arc<Mutex<BluetoothInterface>>, bluetooth_media: Arc<Mutex<Box<BluetoothMedia>>>, sig_notifier: Arc<(Mutex<bool>, Condvar)>, ) -> Bluetooth { Bluetooth { bonded_devices: HashMap::new(), Loading Loading @@ -387,6 +390,7 @@ impl Bluetooth { // Internal API members internal_le_rand_queue: VecDeque::<OneShotSender<u64>>::new(), discoverable_timeout: None, sig_notifier, } } Loading Loading @@ -697,6 +701,10 @@ impl BtifBluetoothCallbacks for Bluetooth { if self.state == BtState::Off { self.properties.clear(); // Let the signal notifier know we are turned off. *self.sig_notifier.0.lock().unwrap() = false; self.sig_notifier.1.notify_all(); } else { // Trigger properties update self.intf.lock().unwrap().get_adapter_properties(); Loading @@ -706,6 +714,10 @@ impl BtifBluetoothCallbacks for Bluetooth { // Ensure device is connectable so that disconnected device can reconnect self.set_connectable(true); // Notify the signal notifier that we are turned on. *self.sig_notifier.0.lock().unwrap() = true; self.sig_notifier.1.notify_all(); } } Loading
system/gd/rust/linux/stack/src/lib.rs +8 −1 Original line number Diff line number Diff line Loading @@ -22,7 +22,7 @@ use std::sync::{Arc, Mutex}; use tokio::sync::mpsc::channel; use tokio::sync::mpsc::{Receiver, Sender}; use crate::bluetooth::Bluetooth; use crate::bluetooth::{Bluetooth, IBluetooth}; use crate::bluetooth_gatt::BluetoothGatt; use crate::bluetooth_media::{BluetoothMedia, MediaActions}; use crate::socket_manager::{BluetoothSocketManager, SocketActions}; Loading @@ -39,6 +39,9 @@ use bt_topshim::{ /// Message types that are sent to the stack main dispatch loop. pub enum Message { // Shuts down the stack. Shutdown, // Callbacks from libbluetooth A2dp(A2dpCallbacks), Avrcp(AvrcpCallbacks), Loading Loading @@ -119,6 +122,10 @@ impl Stack { } match m.unwrap() { Message::Shutdown => { bluetooth.lock().unwrap().disable(); } Message::A2dp(a) => { bluetooth_media.lock().unwrap().dispatch_a2dp_callbacks(a); } Loading