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, InterruptRef, 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    /// Parses the `interrupts` property into interrupt references.
43    pub fn interrupts(&self) -> Vec<InterruptRef> {
44        self.inner.interrupts()
45    }
46}
47
48impl<'a> ViewOp<'a> for NodeGeneric<'a> {
49    fn as_view(&self) -> NodeView<'a> {
50        self.inner
51    }
52}
53
54// ---------------------------------------------------------------------------
55// GenericNodeViewMut
56// ---------------------------------------------------------------------------
57
58/// Mutable view for generic nodes.
59pub struct NodeGenericMut<'a> {
60    pub(super) inner: NodeView<'a>,
61}
62
63impl<'a> ViewOp<'a> for NodeGenericMut<'a> {
64    fn as_view(&self) -> NodeView<'a> {
65        self.inner
66    }
67}
68
69impl<'a> ViewMutOp<'a> for NodeGenericMut<'a> {
70    fn new(node: NodeGenericMut<'a>) -> Self {
71        Self { inner: node.inner }
72    }
73}
74
75impl<'a> NodeGenericMut<'a> {
76    pub fn id(&self) -> NodeId {
77        self.inner.id()
78    }
79
80    pub fn path(&self) -> String {
81        self.inner.path()
82    }
83
84    pub fn set_regs(&mut self, regs: &[RegInfo]) {
85        self.inner.set_regs(regs);
86    }
87
88    pub fn add_child_generic(&mut self, name: &str) -> NodeGenericMut<'a> {
89        let node = Node::new(name);
90        let new_id = self.inner.fdt_mut().add_node(self.inner.id(), node);
91        let new_view = NodeView::new(self.inner.fdt(), new_id);
92        NodeGenericMut { inner: new_view }
93    }
94
95    pub(crate) fn add_child<T: ViewMutOp<'a>>(&mut self, name: &str) -> T {
96        let generic_child = self.add_child_generic(name);
97        T::new(generic_child)
98    }
99}