reachability_core/
inspect.rs

1// Copyright 2020 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::{LinkState, Proto};
6use fuchsia_inspect::Node;
7use fuchsia_inspect_contrib::inspect_log;
8use fuchsia_inspect_contrib::nodes::BoundedListNode;
9
10// Keep only the 50 most recent events.
11static INSPECT_LOG_WINDOW_SIZE: usize = 50;
12
13/// Maintains the Inspect information.
14pub(crate) struct InspectInfo {
15    _node: Node,
16    v4: BoundedListNode,
17    v6: BoundedListNode,
18}
19
20impl InspectInfo {
21    /// Create inspect node `id` rooted at `n`.
22    pub(crate) fn new(n: &Node, id: &str, name: &str) -> Self {
23        let node = n.create_child(id);
24        node.record_string("name", name);
25        let mut v4 = BoundedListNode::new(node.create_child("IPv4"), INSPECT_LOG_WINDOW_SIZE);
26        inspect_log!(v4, state: "None");
27        let mut v6 = BoundedListNode::new(node.create_child("IPv6"), INSPECT_LOG_WINDOW_SIZE);
28        inspect_log!(v6, state: "None");
29
30        InspectInfo { _node: node, v4, v6 }
31    }
32    pub(crate) fn log_link_state(&mut self, proto: Proto, link_state: LinkState) {
33        match proto {
34            Proto::IPv4 => inspect_log!(self.v4, state: format!("{:?}", link_state)),
35            Proto::IPv6 => inspect_log!(self.v6, state: format!("{:?}", link_state)),
36        }
37    }
38}
39#[cfg(test)]
40mod tests {
41    use super::*;
42    use diagnostics_assertions::assert_data_tree;
43    use fuchsia_inspect::Inspector;
44
45    #[test]
46    fn test_log_state() {
47        let _executor = fuchsia_async::TestExecutor::new();
48        let inspector = Inspector::default();
49        let mut i = InspectInfo::new(inspector.root(), "id", "myname");
50        assert_data_tree!(inspector, root: contains {
51            id: {
52                name:"myname",
53                IPv4:{"0": contains {
54                    state: "None"
55                }
56                },
57                IPv6:{"0": contains {
58                    state: "None"
59                }
60                }
61            }
62        });
63
64        i.log_link_state(Proto::IPv4, LinkState::Internet);
65        assert_data_tree!(inspector, root: contains {
66            id: {
67                name:"myname",
68                IPv4:{"0": contains {
69                    state: "None"
70                },
71                "1": contains {
72                    state: "Internet"
73                }
74                },
75                IPv6:{"0": contains {
76                    state: "None"
77                }
78                }
79            }
80        });
81        i.log_link_state(Proto::IPv4, LinkState::Gateway);
82        i.log_link_state(Proto::IPv6, LinkState::Local);
83        assert_data_tree!(inspector, root: contains {
84            id: {
85                name:"myname",
86                IPv4:{"0": contains {
87                    state: "None"
88                },
89                "1": contains {
90                    state: "Internet"
91                },
92                "2": contains {
93                    state: "Gateway"
94                }
95                },
96                IPv6:{"0": contains {
97                    state: "None"
98                },
99                "1": contains {
100                    state: "Local"
101                }
102                }
103            }
104        });
105    }
106}