fuchsia_audio_dai/
discover.rsuse anyhow::Error;
use fidl_fuchsia_io as fio;
use std::path::Path;
use crate::DigitalAudioInterface;
const DAI_DEVICE_DIR: &str = "/dev/class/dai";
pub async fn find_devices() -> Result<Vec<DigitalAudioInterface>, Error> {
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");
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");
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");
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");
}
}