Skip to main content

driver_manager_node/
types.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::node::Node;
6use crate::serve::{
7    ComponentRunnerComponentControllerServerBinding, DriverHostClientBinding, NodeServerBinding,
8};
9use driver_manager_types::NodeType;
10use fidl_fuchsia_component_sandbox::CapabilityId;
11use std::rc::Weak;
12
13#[derive(Clone, Debug, PartialEq)]
14pub enum DriverState {
15    Binding,
16    Running,
17    Stopped,
18}
19
20pub enum NodeTypeVariant {
21    Normal {
22        parent: Weak<Node>,
23    },
24    Composite {
25        parents: Vec<Weak<Node>>,
26        #[allow(unused)]
27        parents_names: Vec<String>,
28        primary_index: u32,
29    },
30}
31
32impl PartialEq<NodeType> for NodeTypeVariant {
33    fn eq(&self, other: &NodeType) -> bool {
34        match self {
35            NodeTypeVariant::Normal { .. } => *other == NodeType::Normal,
36            NodeTypeVariant::Composite { .. } => *other == NodeType::Composite,
37        }
38    }
39}
40
41#[derive(Debug)]
42pub struct DriverComponent {
43    pub driver_url: String,
44    component_instance: zx::Event,
45    component_instance_koid: zx::Koid,
46    runner_component_controller: Option<ComponentRunnerComponentControllerServerBinding>,
47    node_server_binding: Option<NodeServerBinding>,
48    pub driver_client_binding: Option<DriverHostClientBinding>,
49    pub state: DriverState,
50}
51
52impl DriverComponent {
53    pub fn new(
54        driver_url: String,
55        component_instance: zx::Event,
56        component_instance_koid: zx::Koid,
57        runner_component_controller: Option<ComponentRunnerComponentControllerServerBinding>,
58        node_server_binding: Option<NodeServerBinding>,
59        driver_client_binding: Option<DriverHostClientBinding>,
60        state: DriverState,
61    ) -> Self {
62        Self {
63            driver_url,
64            component_instance,
65            component_instance_koid,
66            runner_component_controller,
67            node_server_binding,
68            driver_client_binding,
69            state,
70        }
71    }
72
73    pub fn instance_koid(&self) -> zx::Koid {
74        self.component_instance_koid
75    }
76
77    pub fn duplicate_instance_handle(&self) -> zx::Event {
78        self.component_instance.duplicate(zx::Rights::SAME_RIGHTS).unwrap()
79    }
80
81    pub fn close_node(&mut self) {
82        if let Some(binding) = self.node_server_binding.take() {
83            binding.close()
84        }
85    }
86
87    pub fn send_on_stop(&self) {
88        if let Err(e) =
89            self.runner_component_controller.as_ref().unwrap().control_handle.send_on_stop(
90                fidl_fuchsia_component_runner::ComponentStopInfo { ..Default::default() },
91            )
92        {
93            log::error!("Failed to stop driver component: {}", e);
94        }
95    }
96}
97
98#[derive(Debug)]
99pub enum NodeState {
100    Unbound,
101    Starting { driver_url: String },
102    OwnedByParent { node_server_binding: Option<NodeServerBinding> },
103    CompositeParent,
104    DriverComponent(DriverComponent),
105    Quarantined { driver_url: String },
106}
107
108#[derive(Clone)]
109pub enum NodeDictionary {
110    None,
111
112    // Node specific dictionary that contains offers that the node provides that are type
113    // |DictionaryOffer|
114    Standard(CapabilityId),
115
116    // Passed down the node tree to children, as it contains non-driver protocol
117    // capabilities for testing (the ones injected in offer_injection). This is only used for
118    // system testing at the moment through |restart_with_dictionary|.
119    Subtree(CapabilityId),
120}