_zircon_transport_rust_child_rustc_static/
lib.rs

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.
4
5use fdf_component::{driver_register, Driver, DriverContext, Node, NodeBuilder};
6use fidl_fuchsia_hardware_i2c as i2c;
7use log::{error, info};
8use zx::Status;
9
10/// 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.
15    node: Node,
16}
17
18// 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);
21
22impl Driver for ZirconChildDriver {
23    const NAME: &str = "zircon_child_rust_driver";
24
25    async fn start(mut context: DriverContext) -> Result<Self, Status> {
26        info!("Binding node client. Every driver needs to do this for the driver to be considered loaded.");
27        let node = context.take_node()?;
28
29        let device = get_i2c_device(&context).unwrap();
30        let device_name = device.get_name().await.unwrap().unwrap();
31        info!("i2c device name: {device_name}");
32
33        info!("Adding child node with i2c device name as a property value");
34        let 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?;
38
39        Ok(Self { node })
40    }
41
42    async fn stop(&self) {
43        info!("ZirconChildDriver::stop() was invoked. Use this function to do any cleanup needed.");
44    }
45}
46
47fn get_i2c_device(context: &DriverContext) -> Result<i2c::DeviceProxy, Status> {
48    let service_proxy = context.incoming.service_marker(i2c::ServiceMarker).connect()?;
49
50    service_proxy.connect_to_device().map_err(|err| {
51        error!("Error connecting to i2c device proxy at driver startup: {err}");
52        Status::INTERNAL
53    })
54}