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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// Copyright 2018 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use anyhow::{format_err, Context as _, Error};
use fidl::endpoints::Proxy as _;
use fidl_fuchsia_bluetooth_test::{
    EmulatorError, EmulatorSettings, HciEmulatorMarker, HciEmulatorProxy,
};
use fidl_fuchsia_device::{ControllerMarker, ControllerProxy};
use fidl_fuchsia_hardware_bluetooth::{
    EmulatorMarker, EmulatorProxy, HostMarker, HostProxy, VirtualControllerMarker,
};
use fidl_fuchsia_io::DirectoryProxy;
use fuchsia_async::{DurationExt as _, TimeoutExt as _};
use fuchsia_bluetooth::constants::{
    DEV_DIR, HCI_DEVICE_DIR, HOST_DEVICE_DIR, INTEGRATION_TIMEOUT as WATCH_TIMEOUT,
};
use fuchsia_zircon as zx;
use futures::TryFutureExt as _;
use tracing::error;

pub mod types;

const EMULATOR_DEVICE_DIR: &str = "class/bt-emulator";

/// Represents a bt-hci device emulator. Instances of this type can be used manage the
/// bt-hci-emulator driver within the test device hierarchy. The associated driver instance gets
/// unbound and all bt-hci and bt-emulator device instances destroyed when
/// `destroy_and_wait()` resolves successfully.
/// `destroy_and_wait()` MUST be called for proper clean up of the emulator device.
pub struct Emulator {
    /// This will have a value when the emulator is instantiated and will be reset to None
    /// in `destroy_and_wait()`. This is so the destructor can assert that the TestDevice has been
    /// destroyed.
    dev: Option<TestDevice>,
    hci_emulator: HciEmulatorProxy,
}

impl Emulator {
    /// Returns the default settings.
    // TODO(armansito): Consider defining a library type for EmulatorSettings.
    pub fn default_settings() -> EmulatorSettings {
        EmulatorSettings {
            address: None,
            hci_config: None,
            extended_advertising: None,
            acl_buffer_settings: None,
            le_acl_buffer_settings: None,
            ..Default::default()
        }
    }

    /// Publish a new bt-emulator device and return a handle to it. No corresponding bt-hci device
    /// will be published; to do so it must be explicitly configured and created with a call to
    /// `publish()`. If `realm` is present, the device will be created inside it, otherwise it will
    /// be created using the `/dev` directory in the component's namespace.
    pub async fn create(dev_directory: DirectoryProxy) -> Result<Emulator, Error> {
        let (dev, hci_emulator) = TestDevice::create(dev_directory)
            .await
            .context(format!("Error creating test device"))?;
        Ok(Emulator { dev: Some(dev), hci_emulator })
    }

    /// Publish a bt-emulator and a bt-hci device using the default emulator settings. If `realm`
    /// is present, the device will be created inside it, otherwise it will be created using the
    /// `/dev` directory in the component's namespace.
    pub async fn create_and_publish(dev_directory: DirectoryProxy) -> Result<Emulator, Error> {
        let fake_dev = Self::create(dev_directory).await?;
        fake_dev.publish(Self::default_settings()).await?;
        Ok(fake_dev)
    }

    /// Sends a publish message to the emulator. This is a convenience method that internally
    /// handles the FIDL binding error.
    pub async fn publish(&self, settings: EmulatorSettings) -> Result<(), Error> {
        self.emulator()
            .publish(&settings)
            .await
            .context("publish transport")?
            .map_err(|e: EmulatorError| format_err!("failed to publish bt-hci device: {:#?}", e))
    }

    /// Sends a publish message emulator and returns a Future that resolves when a bt-host device is
    /// published. Note that this requires the bt-host driver to be installed. On success, returns a
    /// proxy to the bt-host device.
    pub async fn publish_and_wait_for_host(
        &self,
        settings: EmulatorSettings,
    ) -> Result<HostProxy, Error> {
        let () = self.publish(settings).await?;
        let dev = self.dev.as_ref().expect("emulator device accessed after it was destroyed!");
        let topo = dev.get_topological_path().await?;
        let TestDevice { dev_directory, controller: _, emulator: _ } = dev;
        let host_dir = fuchsia_fs::directory::open_directory_no_describe(
            dev_directory,
            HOST_DEVICE_DIR,
            fuchsia_fs::OpenFlags::empty(),
        )?;
        let host = device_watcher::wait_for_device_with(
            &host_dir,
            |device_watcher::DeviceInfo { filename, topological_path }| {
                topological_path.starts_with(&topo).then(|| {
                    fuchsia_component::client::connect_to_named_protocol_at_dir_root::<HostMarker>(
                        &host_dir, filename,
                    )
                })
            },
        )
        .on_timeout(WATCH_TIMEOUT, || Err(format_err!("timed out waiting for device to appear")))
        .await??;
        Ok(host)
    }

    pub async fn publish_and_wait_for_device_path(
        &self,
        settings: EmulatorSettings,
    ) -> Result<String, Error> {
        let () = self.publish(settings).await?;
        let dev = self.dev.as_ref().expect("emulator device accessed after it was destroyed!");
        let topo = dev.get_topological_path().await?;
        let TestDevice { dev_directory, controller: _, emulator: _ } = dev;
        let hci_dir = fuchsia_fs::directory::open_directory_no_describe(
            dev_directory,
            HCI_DEVICE_DIR,
            fuchsia_fs::OpenFlags::empty(),
        )?;

        let hci_device_path = device_watcher::wait_for_device_with(
            &hci_dir,
            |device_watcher::DeviceInfo { filename, topological_path }| {
                topological_path.starts_with(&topo).then(|| filename.to_string())
            },
        )
        .on_timeout(WATCH_TIMEOUT, || Err(format_err!("timed out waiting for device to appear")))
        .await?;

        Ok(hci_device_path)
    }

    /// Sends the test device a destroy message which will unbind the driver.
    /// This will wait for the test device to be unpublished from devfs.
    pub async fn destroy_and_wait(&mut self) -> Result<(), Error> {
        self.dev
            .take()
            .expect("attempted to destroy an already destroyed emulator device")
            .destroy_and_wait()
            .await
    }

    pub async fn get_topological_path(&self) -> Result<String, Error> {
        let dev = self.dev.as_ref().expect("emulator device accessed after it was destroyed!");
        dev.get_topological_path().await
    }

    /// Returns a reference to the fuchsia.bluetooth.test.HciEmulator protocol proxy.
    pub fn emulator(&self) -> &HciEmulatorProxy {
        &self.hci_emulator
    }
}

impl Drop for Emulator {
    fn drop(&mut self) {
        if self.dev.is_some() {
            error!("Did not call destroy() on Emulator");
        }
    }
}

// Represents the test device. `destroy()` MUST be called explicitly to remove the device.
// The device will be removed asynchronously so the caller cannot rely on synchronous
// execution of destroy() to know about device removal. Instead, the caller should watch for the
// device path to be removed.
struct TestDevice {
    dev_directory: DirectoryProxy,
    controller: ControllerProxy,
    emulator: EmulatorProxy,
}

impl TestDevice {
    // Creates a new device as a child of the emulator controller device and obtain the HciEmulator
    // protocol channel.
    async fn create(
        dev_directory: DirectoryProxy,
    ) -> Result<(TestDevice, HciEmulatorProxy), Error> {
        // 0x30 => fuchsia.platform.BIND_PLATFORM_DEV_DID.BT_HCI_EMULATOR
        const CONTROL_DEVICE: &str = "sys/platform/00:00:30/bt_hci_virtual";

        let controller = device_watcher::recursive_wait_and_open::<VirtualControllerMarker>(
            &dev_directory,
            CONTROL_DEVICE,
        )
        .await
        .with_context(|| format!("failed to open {}", CONTROL_DEVICE))?;
        let name = controller
            .create_emulator()
            .map_err(Error::from)
            .on_timeout(WATCH_TIMEOUT.after_now(), || {
                Err(format_err!("timed out waiting for emulator to create test device"))
            })
            .await?
            .map_err(zx::Status::from_raw)?
            .ok_or_else(|| {
                format_err!("name absent from EmulatorController::Create FIDL response")
            })?;

        let emulator_dir = fuchsia_fs::directory::open_directory_no_describe(
            &dev_directory,
            EMULATOR_DEVICE_DIR,
            fuchsia_fs::OpenFlags::empty(),
        )?;

        // Wait until a bt-emulator device gets published under our test device.
        let directory = device_watcher::wait_for_device_with(
            &emulator_dir,
            |device_watcher::DeviceInfo { filename, topological_path }| {
                let topological_path = topological_path.strip_prefix(DEV_DIR)?;
                let topological_path = topological_path.strip_prefix('/')?;
                let topological_path = topological_path.strip_prefix(CONTROL_DEVICE)?;
                let topological_path = topological_path.strip_prefix('/')?;
                let topological_path = topological_path.strip_prefix(&name)?;
                let _: &str = topological_path;
                Some(fuchsia_fs::directory::open_directory_no_describe(
                    &emulator_dir,
                    filename,
                    fuchsia_fs::OpenFlags::empty(),
                ))
            },
        )
        .on_timeout(WATCH_TIMEOUT, || Err(format_err!("timed out waiting for device to appear")))
        .await??;

        let controller = fuchsia_component::client::connect_to_named_protocol_at_dir_root::<
            ControllerMarker,
        >(&directory, fidl_fuchsia_device_fs::DEVICE_CONTROLLER_NAME)?;
        let emulator = fuchsia_component::client::connect_to_named_protocol_at_dir_root::<
            EmulatorMarker,
        >(&directory, fidl_fuchsia_device_fs::DEVICE_PROTOCOL_NAME)?;

        // Open a HciEmulator protocol channel.
        let (proxy, server_end) = fidl::endpoints::create_proxy::<HciEmulatorMarker>()?;
        let () = emulator.open(server_end)?;
        Ok((Self { dev_directory, controller, emulator }, proxy))
    }

    /// Sends the test device a destroy message which will unbind the driver.
    /// This will wait for the test device to be unpublished from devfs.
    pub async fn destroy_and_wait(&mut self) -> Result<(), Error> {
        let () = self.controller.schedule_unbind().await?.map_err(zx::Status::from_raw)?;
        let _: (zx::Signals, zx::Signals) = futures::future::try_join(
            self.controller.as_channel().on_closed(),
            self.emulator.as_channel().on_closed(),
        )
        .await?;
        Ok(())
    }

    pub async fn get_topological_path(&self) -> Result<String, Error> {
        self.controller
            .get_topological_path()
            .await
            .context("get topological path transport")?
            .map_err(zx::Status::from_raw)
            .context("get topological path")
    }
}

#[cfg(test)]
mod tests {
    use {
        super::*,
        fidl_fuchsia_driver_test as fdt,
        fuchsia_component_test::RealmBuilder,
        fuchsia_driver_test::{DriverTestRealmBuilder, DriverTestRealmInstance},
    };

    const HCI_DEVICE_DIR: &str = "class/bt-hci";

    fn default_settings() -> EmulatorSettings {
        EmulatorSettings {
            address: None,
            hci_config: None,
            extended_advertising: None,
            acl_buffer_settings: None,
            le_acl_buffer_settings: None,
            ..Default::default()
        }
    }

    #[fuchsia::test]
    async fn test_publish_lifecycle() {
        // We use these watchers to verify the addition and removal of these devices as tied to the
        // lifetime of the Emulator instance we create below.
        let emul_dev: EmulatorProxy;
        let hci_dev: HciEmulatorProxy;

        let realm = RealmBuilder::new().await.expect("realm builder");
        let _: &RealmBuilder =
            realm.driver_test_realm_setup().await.expect("driver test realm setup");
        let realm = realm.build().await.expect("failed to build realm");
        let args = fdt::RealmArgs {
            root_driver: Some("fuchsia-boot:///platform-bus#meta/platform-bus.cm".to_string()),
            ..Default::default()
        };
        realm.driver_test_realm_start(args).await.expect("driver test realm start");

        {
            let dev_dir = realm.driver_test_realm_connect_to_dev().unwrap();
            let mut fake_dev =
                Emulator::create(dev_dir).await.expect("Failed to construct Emulator");
            let Emulator { dev, hci_emulator: _ } = &fake_dev;
            let dev = dev.as_ref().expect("emulator device exists");
            let topo = dev
                .get_topological_path()
                .await
                .expect("Failed to obtain topological path for Emulator");
            let TestDevice { dev_directory, controller: _, emulator: _ } = dev;

            // A bt-emulator device should already exist by now.
            {
                let emulator_dir = fuchsia_fs::directory::open_directory_no_describe(
                    dev_directory,
                    EMULATOR_DEVICE_DIR,
                    fuchsia_fs::OpenFlags::empty(),
                )
                .expect("open emulator directory");
                emul_dev = device_watcher::wait_for_device_with(
                    &emulator_dir,
                    |device_watcher::DeviceInfo { filename, topological_path }| {
                        topological_path.starts_with(&topo).then(|| {
                            fuchsia_component::client::connect_to_named_protocol_at_dir_root::<
                                EmulatorMarker,
                            >(&emulator_dir, filename)
                            .expect("failed to connect to device")
                        })
                    },
                )
                .on_timeout(WATCH_TIMEOUT, || panic!("timed out waiting for device to appear"))
                .await
                .expect("failed to watch for device");
            }

            // Send a publish message to the device. This call should succeed and result in a new
            // bt-hci device.
            let () = fake_dev
                .publish(default_settings())
                .await
                .expect("Failed to send Publish message to emulator device");
            {
                let hci_dir = fuchsia_fs::directory::open_directory_no_describe(
                    dev_directory,
                    HCI_DEVICE_DIR,
                    fuchsia_fs::OpenFlags::empty(),
                )
                .expect("open hci directory");
                hci_dev = device_watcher::wait_for_device_with(
                    &hci_dir,
                    |device_watcher::DeviceInfo { filename, topological_path }| {
                        topological_path.starts_with(&topo).then(|| {
                            fuchsia_component::client::connect_to_named_protocol_at_dir_root::<
                                HciEmulatorMarker,
                            >(&hci_dir, filename)
                            .expect("failed to connect to device")
                        })
                    },
                )
                .on_timeout(WATCH_TIMEOUT, || panic!("timed out waiting for device to appear"))
                .await
                .expect("failed to watch for device");
            }

            // Once a device is published, it should not be possible to publish again while the
            // HciEmulator channel is open.
            let result = fake_dev
                .emulator()
                .publish(&default_settings())
                .await
                .expect("Failed to send second Publish message to emulator device");
            assert_eq!(Err(EmulatorError::HciAlreadyPublished), result);

            fake_dev.destroy_and_wait().await.expect("Expected test device to be removed");
        }

        // Both devices should be destroyed when `fake_dev` gets dropped.
        let _: (zx::Signals, zx::Signals) = futures::future::try_join(
            emul_dev.as_channel().on_closed(),
            hci_dev.as_channel().on_closed(),
        )
        .on_timeout(WATCH_TIMEOUT, || panic!("timed out waiting for device to close"))
        .await
        .expect("on closed");
    }
}