Skip to main content

fdf_resource/
lib.rs

1// Copyright 2026 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
5#![deny(missing_docs)]
6//! Helper library for connecting to common driver resources using `fidl_next`.
7
8use fdf_component::{DriverContext, DriverError};
9use fidl_next::Client;
10use fidl_next_fuchsia_hardware_clock as fclock;
11use fidl_next_fuchsia_hardware_gpio as fgpio;
12use fidl_next_fuchsia_hardware_i2c as fi2c;
13use fidl_next_fuchsia_hardware_mailbox as fmailbox;
14use fidl_next_fuchsia_hardware_pci as fpci;
15use fidl_next_fuchsia_hardware_reset as freset;
16use fidl_next_fuchsia_hardware_spi as fspi;
17
18/// Extension trait for [`DriverContext`] to simplify connecting to clock resources.
19pub trait ClockExt {
20    /// Connects to a clock resource with the given instance name.
21    fn connect_to_clock(&self, instance: &str) -> Result<Client<fclock::Clock>, DriverError>;
22}
23
24impl ClockExt for DriverContext {
25    fn connect_to_clock(&self, instance: &str) -> Result<Client<fclock::Clock>, DriverError> {
26        let service = self
27            .incoming
28            .service::<fdf_component::ServiceInstance<fclock::Service>>()
29            .instance(instance)
30            .connect_next()?;
31        let (client, server) = fidl_next::fuchsia::create_channel();
32        service.clock(server)?;
33        Ok(client.spawn())
34    }
35}
36
37/// Extension trait for [`DriverContext`] to simplify connecting to GPIO resources.
38pub trait GpioExt {
39    /// Connects to a GPIO resource with the given instance name.
40    fn connect_to_gpio(&self, instance: &str) -> Result<Client<fgpio::Gpio>, DriverError>;
41}
42
43impl GpioExt for DriverContext {
44    fn connect_to_gpio(&self, instance: &str) -> Result<Client<fgpio::Gpio>, DriverError> {
45        let service = self
46            .incoming
47            .service::<fdf_component::ServiceInstance<fgpio::Service>>()
48            .instance(instance)
49            .connect_next()?;
50        let (client, server) = fidl_next::fuchsia::create_channel();
51        service.device(server)?;
52        Ok(client.spawn())
53    }
54}
55
56/// Extension trait for [`DriverContext`] to simplify connecting to I2C resources.
57pub trait I2cExt {
58    /// Connects to an I2C resource with the given instance name.
59    fn connect_to_i2c(&self, instance: &str) -> Result<Client<fi2c::Device>, DriverError>;
60}
61
62impl I2cExt for DriverContext {
63    fn connect_to_i2c(&self, instance: &str) -> Result<Client<fi2c::Device>, DriverError> {
64        let service = self
65            .incoming
66            .service::<fdf_component::ServiceInstance<fi2c::Service>>()
67            .instance(instance)
68            .connect_next()?;
69        let (client, server) = fidl_next::fuchsia::create_channel();
70        service.device(server)?;
71        Ok(client.spawn())
72    }
73}
74
75/// Extension trait for [`DriverContext`] to simplify connecting to mailbox resources.
76pub trait MailboxExt {
77    /// Connects to a mailbox resource with the given instance name.
78    fn connect_to_mailbox(&self, instance: &str) -> Result<Client<fmailbox::Channel>, DriverError>;
79}
80
81impl MailboxExt for DriverContext {
82    fn connect_to_mailbox(&self, instance: &str) -> Result<Client<fmailbox::Channel>, DriverError> {
83        let service = self
84            .incoming
85            .service::<fdf_component::ServiceInstance<fmailbox::Service>>()
86            .instance(instance)
87            .connect_next()?;
88        let (client, server) = fidl_next::fuchsia::create_channel();
89        service.channel(server)?;
90        Ok(client.spawn())
91    }
92}
93
94/// Extension trait for [`DriverContext`] to simplify connecting to reset resources.
95pub trait ResetExt {
96    /// Connects to a reset resource with the given instance name.
97    fn connect_to_reset(&self, instance: &str) -> Result<Client<freset::Reset>, DriverError>;
98}
99
100impl ResetExt for DriverContext {
101    fn connect_to_reset(&self, instance: &str) -> Result<Client<freset::Reset>, DriverError> {
102        let service = self
103            .incoming
104            .service::<fdf_component::ServiceInstance<freset::Service>>()
105            .instance(instance)
106            .connect_next()?;
107        let (client, server) = fidl_next::fuchsia::create_channel();
108        service.reset(server)?;
109        Ok(client.spawn())
110    }
111}
112
113/// Extension trait for [`DriverContext`] to simplify connecting to SPI resources.
114pub trait SpiExt {
115    /// Connects to a SPI resource with the given instance name.
116    fn connect_to_spi(&self, instance: &str) -> Result<Client<fspi::Device>, DriverError>;
117}
118
119impl SpiExt for DriverContext {
120    fn connect_to_spi(&self, instance: &str) -> Result<Client<fspi::Device>, DriverError> {
121        let service = self
122            .incoming
123            .service::<fdf_component::ServiceInstance<fspi::Service>>()
124            .instance(instance)
125            .connect_next()?;
126        let (client, server) = fidl_next::fuchsia::create_channel();
127        service.device(server)?;
128        Ok(client.spawn())
129    }
130}
131
132/// Extension trait for [`DriverContext`] to simplify connecting to PCI
133/// resources.
134pub trait PciExt {
135    /// Connects to a PCI resource with the given instance name.
136    fn connect_to_pci(&self, instance: &str) -> Result<Client<fpci::Device>, DriverError>;
137}
138
139impl PciExt for DriverContext {
140    fn connect_to_pci(&self, instance: &str) -> Result<Client<fpci::Device>, DriverError> {
141        let service = self
142            .incoming
143            .service::<fdf_component::ServiceInstance<fpci::Service>>()
144            .instance(instance)
145            .connect_next()?;
146        let (client, server) = fidl_next::fuchsia::create_channel();
147        service.device(server)?;
148        Ok(client.spawn())
149    }
150}