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.
45use fdf_component::{driver_register, Driver, DriverContext, Node, NodeBuilder};
6use fidl_fuchsia_hardware_i2c as i2c;
7use log::{error, info};
8use zx::Status;
910/// The implementation of our driver will live in this object, which implements [`Driver`].
11#[allow(unused)]
12struct ZirconChildDriver {
13/// The [`Node`] is our handle to the node we bound to. We need to keep this handle
14 /// open to keep the node around.
15node: Node,
16}
1718// This creates the exported driver registration structures that allow the driver host to
19// find and run the start and stop methods on our `ZirconChildDriver`.
20driver_register!(ZirconChildDriver);
2122impl Driver for ZirconChildDriver {
23const NAME: &str = "zircon_child_rust_driver";
2425async fn start(mut context: DriverContext) -> Result<Self, Status> {
26info!("Binding node client. Every driver needs to do this for the driver to be considered loaded.");
27let node = context.take_node()?;
2829let device = get_i2c_device(&context).unwrap();
30let device_name = device.get_name().await.unwrap().unwrap();
31info!("i2c device name: {device_name}");
3233info!("Adding child node with i2c device name as a property value");
34let child_node = NodeBuilder::new("transport-child")
35 .add_property(bind_fuchsia_test::TEST_CHILD, device_name)
36 .build();
37 node.add_child(child_node).await?;
3839Ok(Self { node })
40 }
4142async fn stop(&self) {
43info!("ZirconChildDriver::stop() was invoked. Use this function to do any cleanup needed.");
44 }
45}
4647fn get_i2c_device(context: &DriverContext) -> Result<i2c::DeviceProxy, Status> {
48let service_proxy = context.incoming.service_marker(i2c::ServiceMarker).connect()?;
4950 service_proxy.connect_to_device().map_err(|err| {
51error!("Error connecting to i2c device proxy at driver startup: {err}");
52 Status::INTERNAL
53 })
54}