1// Copyright 2024 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4use 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;
1112// Watches the sensor service for new providers of fuchsia.hardware.sensors.Driver and connects to
13// them. If the connection is successful, it hands the proxy to the SensorManager to manage
14// their configurations and events.
15pub(crate) async fn watch_service_directory(
16 sensor_service: fclient::Service<ServiceMarker>,
17 manager: Arc<Mutex<SensorManager>>,
18) -> Result<(), Error> {
19let mut watcher = sensor_service.watch().await?;
20while let Ok(Some(instance)) = watcher.try_next().await {
21let instance_name = instance.instance_name();
22log::info!("Found new sensor service provider: {}", instance_name);
23let driver_proxy_res = instance.connect_to_driver();
24let driver_proxy: DriverProxy = if let Ok(proxy) = driver_proxy_res {
25 proxy
26 } else {
27log::error!("Failed to connect to driver instance: {:#?}", driver_proxy_res);
28continue;
29 };
30let mut sm = manager.lock().await;
31 sm.add_instance(driver_proxy).await;
32 }
33Ok(())
34}