summaryrefslogtreecommitdiff
path: root/system/gd/rust/linux/service/src/main.rs
blob: e749843e140112085c3c0b0a5f4b04ab91a6f61e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
use dbus::{channel::MatchingReceiver, message::MatchRule};
use dbus_crossroads::Crossroads;
use dbus_tokio::connection;
use futures::future;
use log::LevelFilter;
use std::error::Error;
use std::sync::{Arc, Mutex};
use syslog::{BasicLogger, Facility, Formatter3164};

use bt_topshim::{btif::get_btinterface, topstack};
use btstack::{
    bluetooth::{get_bt_dispatcher, Bluetooth, IBluetooth},
    bluetooth_gatt::BluetoothGatt,
    bluetooth_media::BluetoothMedia,
    suspend::Suspend,
    Stack,
};
use dbus_projection::DisconnectWatcher;

mod dbus_arg;
mod iface_bluetooth;
mod iface_bluetooth_gatt;
mod iface_bluetooth_media;
mod iface_suspend;

const DBUS_SERVICE_NAME: &str = "org.chromium.bluetooth";

/// Check command line arguments for target hci adapter (--hci=N). If no adapter
/// is set, default to 0.
fn get_adapter_index(args: &Vec<String>) -> i32 {
    for arg in args {
        if arg.starts_with("--hci=") {
            let num = (&arg[6..]).parse::<i32>();
            if num.is_ok() {
                return num.unwrap();
            }
        }
    }

    0
}

fn make_object_name(idx: i32, name: &str) -> String {
    String::from(format!("/org/chromium/bluetooth/hci{}/{}", idx, name))
}

/// Runs the Bluetooth daemon serving D-Bus IPC.
fn main() -> Result<(), Box<dyn Error>> {
    let formatter = Formatter3164 {
        facility: Facility::LOG_USER,
        hostname: None,
        process: "btadapterd".into(),
        pid: 0,
    };

    let logger = syslog::unix(formatter).expect("could not connect to syslog");
    let _ = log::set_boxed_logger(Box::new(BasicLogger::new(logger)))
        .map(|()| log::set_max_level(LevelFilter::Info));

    let (tx, rx) = Stack::create_channel();

    let intf = Arc::new(Mutex::new(get_btinterface().unwrap()));
    let suspend = Arc::new(Mutex::new(Box::new(Suspend::new(tx.clone()))));
    let bluetooth_gatt = Arc::new(Mutex::new(Box::new(BluetoothGatt::new(intf.clone()))));
    let bluetooth_media =
        Arc::new(Mutex::new(Box::new(BluetoothMedia::new(tx.clone(), intf.clone()))));
    let bluetooth = Arc::new(Mutex::new(Box::new(Bluetooth::new(
        tx.clone(),
        intf.clone(),
        bluetooth_media.clone(),
    ))));

    // Args don't include arg[0] which is the binary name
    let all_args = std::env::args().collect::<Vec<String>>();
    let args = all_args[1..].to_vec();

    let adapter_index = get_adapter_index(&args);

    topstack::get_runtime().block_on(async {
        // Connect to D-Bus system bus.
        let (resource, conn) = connection::new_system_sync()?;

        // The `resource` is a task that should be spawned onto a tokio compatible
        // reactor ASAP. If the resource ever finishes, we lost connection to D-Bus.
        tokio::spawn(async {
            let err = resource.await;
            panic!("Lost connection to D-Bus: {}", err);
        });

        // Request a service name and quit if not able to.
        conn.request_name(DBUS_SERVICE_NAME, false, true, false).await?;

        // Prepare D-Bus interfaces.
        let mut cr = Crossroads::new();
        cr.set_async_support(Some((
            conn.clone(),
            Box::new(|x| {
                tokio::spawn(x);
            }),
        )));

        // Run the stack main dispatch loop.
        topstack::get_runtime().spawn(Stack::dispatch(
            rx,
            bluetooth.clone(),
            bluetooth_gatt.clone(),
            bluetooth_media.clone(),
            suspend.clone(),
        ));

        // Set up the disconnect watcher to monitor client disconnects.
        let disconnect_watcher = Arc::new(Mutex::new(DisconnectWatcher::new()));
        disconnect_watcher.lock().unwrap().setup_watch(conn.clone()).await;

        // Register D-Bus method handlers of IBluetooth.
        iface_bluetooth::export_bluetooth_dbus_obj(
            make_object_name(adapter_index, "adapter"),
            conn.clone(),
            &mut cr,
            bluetooth.clone(),
            disconnect_watcher.clone(),
        );
        // Register D-Bus method handlers of IBluetoothGatt.
        iface_bluetooth_gatt::export_bluetooth_gatt_dbus_obj(
            make_object_name(adapter_index, "gatt"),
            conn.clone(),
            &mut cr,
            bluetooth_gatt.clone(),
            disconnect_watcher.clone(),
        );

        iface_bluetooth_media::export_bluetooth_media_dbus_obj(
            make_object_name(adapter_index, "media"),
            conn.clone(),
            &mut cr,
            bluetooth_media.clone(),
            disconnect_watcher.clone(),
        );

        iface_suspend::export_suspend_dbus_obj(
            make_object_name(adapter_index, "suspend"),
            conn.clone(),
            &mut cr,
            suspend,
            disconnect_watcher.clone(),
        );

        // Hold locks and initialize all interfaces. This must be done AFTER DBus is
        // initialized so DBus can properly enforce user policies.
        {
            intf.lock().unwrap().initialize(get_bt_dispatcher(tx.clone()), args);

            bluetooth_media.lock().unwrap().set_adapter(bluetooth.clone());

            let mut bluetooth = bluetooth.lock().unwrap();
            bluetooth.init_profiles();
            bluetooth.enable();

            bluetooth_gatt.lock().unwrap().init_profiles(tx.clone());
        }

        // Start listening on DBus after exporting interfaces and initializing
        // all bluetooth objects.
        conn.start_receive(
            MatchRule::new_method_call(),
            Box::new(move |msg, conn| {
                cr.handle_message(msg, conn).unwrap();
                true
            }),
        );

        // Serve clients forever.
        future::pending::<()>().await;
        unreachable!()
    })
}

#[cfg(test)]
mod tests {
    use crate::get_adapter_index;

    #[test]
    fn device_index_parsed() {
        // A few failing cases
        assert_eq!(get_adapter_index(&vec! {}), 0);
        assert_eq!(get_adapter_index(&vec! {"--bar".to_string(), "--hci".to_string()}), 0);
        assert_eq!(get_adapter_index(&vec! {"--hci=foo".to_string()}), 0);
        assert_eq!(get_adapter_index(&vec! {"--hci=12t".to_string()}), 0);

        // Some passing cases
        assert_eq!(get_adapter_index(&vec! {"--hci=12".to_string()}), 12);
        assert_eq!(get_adapter_index(&vec! {"--hci=1".to_string(), "--hci=2".to_string()}), 1);
    }
}