fuchsia_audio_dai/
discover.rs

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
// Copyright 2021 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::Error;
use fidl_fuchsia_io as fio;
use std::path::Path;

use crate::DigitalAudioInterface;

const DAI_DEVICE_DIR: &str = "/dev/class/dai";

/// Finds any DAI devices, connects to any that are available and provides
pub async fn find_devices() -> Result<Vec<DigitalAudioInterface>, Error> {
    // Connect to the component's environment.
    let directory_proxy =
        fuchsia_fs::directory::open_in_namespace(DAI_DEVICE_DIR, fio::Flags::empty())?;
    find_devices_internal(directory_proxy).await
}

async fn find_devices_internal(
    directory_proxy: fio::DirectoryProxy,
) -> Result<Vec<DigitalAudioInterface>, Error> {
    let files = fuchsia_fs::directory::readdir(&directory_proxy).await?;

    let paths: Vec<_> =
        files.iter().map(|file| Path::new(DAI_DEVICE_DIR).join(&file.name)).collect();
    let devices = paths.iter().map(|path| DigitalAudioInterface::new(&path)).collect();

    Ok(devices)
}

#[cfg(test)]
mod tests {
    use fuchsia_component_test::{
        Capability, ChildOptions, LocalComponentHandles, RealmBuilder, Route,
    };
    use futures::channel::mpsc;
    use futures::{SinkExt, StreamExt};
    use realmbuilder_mock_helpers::mock_dev;

    use super::*;
    use crate::test::mock_dai_dev_with_io_devices;

    #[fuchsia::test]
    async fn test_env_dir_is_not_found() {
        let _ = find_devices().await.expect_err("find devices okay");
    }

    async fn mock_client(
        handles: LocalComponentHandles,
        mut sender: mpsc::Sender<()>,
    ) -> Result<(), Error> {
        let proxy = handles.clone_from_namespace("dev/class/dai")?;
        let devices = find_devices_internal(proxy).await.expect("should find devices");
        assert_eq!(devices.len(), 2);
        let _ = sender.send(()).await.unwrap();
        Ok(())
    }

    #[fuchsia::test]
    async fn devices_found_from_env() {
        let (device_sender, mut device_receiver) = mpsc::channel(0);
        let builder = RealmBuilder::new().await.expect("Failed to create test realm builder");

        // Add a mock that provides the dev/ directory with one input and output device.
        let mock_dev = builder
            .add_local_child(
                "mock-dev",
                move |handles: LocalComponentHandles| {
                    Box::pin(mock_dev(
                        handles,
                        mock_dai_dev_with_io_devices("input1".to_string(), "output1".to_string()),
                    ))
                },
                ChildOptions::new().eager(),
            )
            .await
            .expect("Failed adding mock /dev provider to topology");

        // Add a mock that represents a client trying to discover DAI devices.
        let mock_client = builder
            .add_local_child(
                "mock-client",
                move |handles: LocalComponentHandles| {
                    let s = device_sender.clone();
                    Box::pin(mock_client(handles, s.clone()))
                },
                ChildOptions::new().eager(),
            )
            .await
            .expect("Failed adding mock client to topology");

        // Give client access to dev/
        builder
            .add_route(
                Route::new()
                    .capability(
                        Capability::directory("dev-dai")
                            .path(DAI_DEVICE_DIR)
                            .rights(fio::R_STAR_DIR),
                    )
                    .from(&mock_dev)
                    .to(&mock_client),
            )
            .await
            .expect("Failed adding route for dai device directory");

        let _test_topology = builder.build().await.unwrap();

        let _ = device_receiver.next().await.expect("should receive devices");
    }
}