Skip to main content

fdt_edit/node/view/
generic.rs

1//! Generic node view specialization.
2
3use alloc::{string::String, vec::Vec};
4use fdt_raw::{Phandle, RegInfo};
5
6use super::NodeView;
7use crate::{ClockRef, Node, NodeId, RegFixed, ViewMutOp, ViewOp};
8
9// ---------------------------------------------------------------------------
10// GenericNodeView
11// ---------------------------------------------------------------------------
12
13/// A generic node view with no extra specialization.
14#[derive(Clone, Copy)]
15pub struct NodeGeneric<'a> {
16    pub(super) inner: NodeView<'a>,
17}
18
19impl<'a> NodeGeneric<'a> {
20    pub fn id(&self) -> NodeId {
21        self.inner.id()
22    }
23
24    pub fn path(&self) -> String {
25        self.inner.path()
26    }
27
28    pub fn regs(&self) -> Vec<RegFixed> {
29        self.inner.regs()
30    }
31
32    /// Returns the effective `interrupt-parent`, inheriting from ancestors.
33    pub fn interrupt_parent(&self) -> Option<Phandle> {
34        self.inner.interrupt_parent()
35    }
36
37    /// Parses the `clocks` property into clock references.
38    pub fn clocks(&self) -> Vec<ClockRef> {
39        self.inner.clocks()
40    }
41}
42
43impl<'a> ViewOp<'a> for NodeGeneric<'a> {
44    fn as_view(&self) -> NodeView<'a> {
45        self.inner
46    }
47}
48
49// ---------------------------------------------------------------------------
50// GenericNodeViewMut
51// ---------------------------------------------------------------------------
52
53/// Mutable view for generic nodes.
54pub struct NodeGenericMut<'a> {
55    pub(super) inner: NodeView<'a>,
56}
57
58impl<'a> ViewOp<'a> for NodeGenericMut<'a> {
59    fn as_view(&self) -> NodeView<'a> {
60        self.inner
61    }
62}
63
64impl<'a> ViewMutOp<'a> for NodeGenericMut<'a> {
65    fn new(node: NodeGenericMut<'a>) -> Self {
66        Self { inner: node.inner }
67    }
68}
69
70impl<'a> NodeGenericMut<'a> {
71    pub fn id(&self) -> NodeId {
72        self.inner.id()
73    }
74
75    pub fn path(&self) -> String {
76        self.inner.path()
77    }
78
79    pub fn set_regs(&mut self, regs: &[RegInfo]) {
80        self.inner.set_regs(regs);
81    }
82
83    pub fn add_child_generic(&mut self, name: &str) -> NodeGenericMut<'a> {
84        let node = Node::new(name);
85        let new_id = self.inner.fdt_mut().add_node(self.inner.id(), node);
86        let new_view = NodeView::new(self.inner.fdt(), new_id);
87        NodeGenericMut { inner: new_view }
88    }
89
90    pub(crate) fn add_child<T: ViewMutOp<'a>>(&mut self, name: &str) -> T {
91        let generic_child = self.add_child_generic(name);
92        T::new(generic_child)
93    }
94}