Skip to main content

input_pipeline/
incoming.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
5use crate::Transport;
6use anyhow::Context;
7use fidl_fuchsia_io as fio;
8use fuchsia_component::client::Connect;
9use fuchsia_component::directory::{AsRefDirectory, Directory};
10
11#[cfg(feature = "dso")]
12pub use dso::*;
13
14#[cfg(not(feature = "dso"))]
15pub use elf::*;
16
17mod dso {
18    #![cfg(feature = "dso")]
19
20    use super::*;
21
22    #[derive(Clone)]
23    pub struct Incoming(std::sync::Arc<fdf_component::Incoming>);
24
25    impl Incoming {
26        pub fn new(incoming: std::sync::Arc<fdf_component::Incoming>) -> Self {
27            Self(incoming)
28        }
29
30        pub fn connect_protocol<T: Connect>(&self) -> Result<T, anyhow::Error> {
31            self.0.connect_protocol().context("connect_protocol")
32        }
33
34        pub fn connect_protocol_next<P: fidl_next::Discoverable>(
35            &self,
36        ) -> Result<fidl_next::ClientEnd<P, Transport>, anyhow::Error> {
37            self.0.connect_protocol_next().context("connect_protocol_next")
38        }
39
40        pub fn connect_protocol_next_at<P: fidl_next::Discoverable>(
41            dir: &impl AsRefDirectory,
42            path: &str,
43        ) -> Result<fidl_next::ClientEnd<P, Transport>, anyhow::Error> {
44            fdf_component::Incoming::connect_protocol_next_at(dir, path)
45                .context("connect_protocol_next_at")
46        }
47    }
48
49    impl Directory for Incoming {
50        fn open(
51            &self,
52            path: &str,
53            flags: fio::Flags,
54            server_end: zx::Channel,
55        ) -> Result<(), anyhow::Error> {
56            self.0.open(path, flags, server_end)
57        }
58    }
59}
60
61mod elf {
62    #![cfg(not(feature = "dso"))]
63
64    use super::*;
65    use fuchsia_component::client::connect;
66
67    #[derive(Clone)]
68    pub struct Incoming;
69
70    impl Incoming {
71        pub fn new() -> Self {
72            Self {}
73        }
74
75        pub fn connect_protocol<T: Connect>(&self) -> Result<T, anyhow::Error> {
76            connect::connect_to_protocol::<T>()
77        }
78
79        pub fn connect_protocol_next<P: fidl_next::Discoverable>(
80            &self,
81        ) -> Result<fidl_next::ClientEnd<P, Transport>, anyhow::Error> {
82            let (client_end, server_end) = zx::Channel::create();
83            fdio::service_connect(&format!("/svc/{}", P::PROTOCOL_NAME), server_end)
84                .context("connect_protocol_next")?;
85            Ok(fidl_next::ClientEnd::<P, zx::Channel>::from_untyped(client_end))
86        }
87
88        pub fn connect_protocol_next_at<P: fidl_next::Discoverable>(
89            dir: &impl AsRefDirectory,
90            path: &str,
91        ) -> Result<fidl_next::ClientEnd<P, Transport>, anyhow::Error> {
92            let (client_end, server_end) = zx::Channel::create();
93            dir.as_ref_directory().open(path, fio::Flags::PROTOCOL_SERVICE, server_end)?;
94            Ok(fidl_next::ClientEnd::<P, zx::Channel>::from_untyped(client_end))
95        }
96    }
97
98    impl Directory for Incoming {
99        fn open(
100            &self,
101            path: &str,
102            flags: fio::Flags,
103            server_end: zx::Channel,
104        ) -> Result<(), anyhow::Error> {
105            fdio::open(path, flags, server_end).context("Directory::open")
106        }
107    }
108}
109
110impl AsRefDirectory for Incoming {
111    fn as_ref_directory(&self) -> &dyn fuchsia_component::directory::Directory {
112        self
113    }
114}