sensors_lib/
service_watcher.rs1use crate::sensor_manager::SensorManager;
5use anyhow::Error;
6use fidl_fuchsia_hardware_sensors::{DriverProxy, ServiceMarker};
7use fuchsia_component::client as fclient;
8use futures::lock::Mutex;
9use futures_util::TryStreamExt;
10use std::sync::Arc;
11
12pub(crate) async fn watch_service_directory(
16 sensor_service: fclient::Service<ServiceMarker>,
17 manager: Arc<Mutex<SensorManager>>,
18) -> Result<(), Error> {
19 let mut watcher = sensor_service.watch().await?;
20 while let Ok(Some(instance)) = watcher.try_next().await {
21 let instance_name = instance.instance_name();
22 log::info!("Found new sensor service provider: {}", instance_name);
23 let driver_proxy_res = instance.connect_to_driver();
24 let driver_proxy: DriverProxy = if let Ok(proxy) = driver_proxy_res {
25 proxy
26 } else {
27 log::error!("Failed to connect to driver instance: {:#?}", driver_proxy_res);
28 continue;
29 };
30 let mut sm = manager.lock().await;
31 sm.add_instance(driver_proxy).await;
32 }
33 Ok(())
34}