_zircon_transport_rust_child_rustc_static/
lib.rs1use fdf_component::{driver_register, Driver, DriverContext, Node, NodeBuilder};
6use fidl_fuchsia_hardware_i2c as i2c;
7use log::{error, info};
8use zx::Status;
9
10#[allow(unused)]
12struct ZirconChildDriver {
13 node: Node,
16}
17
18driver_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}