matc/clusters/codec/
diagnostics_ethernet.rs1use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11pub fn decode_phy_rate(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u8>> {
17 if let tlv::TlvItemValue::Int(v) = inp {
18 Ok(Some(*v as u8))
19 } else {
20 Ok(None)
21 }
22}
23
24pub fn decode_full_duplex(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<bool>> {
26 if let tlv::TlvItemValue::Bool(v) = inp {
27 Ok(Some(*v))
28 } else {
29 Ok(None)
30 }
31}
32
33pub fn decode_packet_rx_count(inp: &tlv::TlvItemValue) -> anyhow::Result<u64> {
35 if let tlv::TlvItemValue::Int(v) = inp {
36 Ok(*v)
37 } else {
38 Err(anyhow::anyhow!("Expected Integer"))
39 }
40}
41
42pub fn decode_packet_tx_count(inp: &tlv::TlvItemValue) -> anyhow::Result<u64> {
44 if let tlv::TlvItemValue::Int(v) = inp {
45 Ok(*v)
46 } else {
47 Err(anyhow::anyhow!("Expected Integer"))
48 }
49}
50
51pub fn decode_tx_err_count(inp: &tlv::TlvItemValue) -> anyhow::Result<u64> {
53 if let tlv::TlvItemValue::Int(v) = inp {
54 Ok(*v)
55 } else {
56 Err(anyhow::anyhow!("Expected Integer"))
57 }
58}
59
60pub fn decode_collision_count(inp: &tlv::TlvItemValue) -> anyhow::Result<u64> {
62 if let tlv::TlvItemValue::Int(v) = inp {
63 Ok(*v)
64 } else {
65 Err(anyhow::anyhow!("Expected Integer"))
66 }
67}
68
69pub fn decode_overrun_count(inp: &tlv::TlvItemValue) -> anyhow::Result<u64> {
71 if let tlv::TlvItemValue::Int(v) = inp {
72 Ok(*v)
73 } else {
74 Err(anyhow::anyhow!("Expected Integer"))
75 }
76}
77
78pub fn decode_carrier_detect(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<bool>> {
80 if let tlv::TlvItemValue::Bool(v) = inp {
81 Ok(Some(*v))
82 } else {
83 Ok(None)
84 }
85}
86
87pub fn decode_time_since_reset(inp: &tlv::TlvItemValue) -> anyhow::Result<u64> {
89 if let tlv::TlvItemValue::Int(v) = inp {
90 Ok(*v)
91 } else {
92 Err(anyhow::anyhow!("Expected Integer"))
93 }
94}
95
96
97pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
109 if cluster_id != 0x0037 {
111 return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0037, got {}\"}}", cluster_id);
112 }
113
114 match attribute_id {
115 0x0000 => {
116 match decode_phy_rate(tlv_value) {
117 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
118 Err(e) => format!("{{\"error\": \"{}\"}}", e),
119 }
120 }
121 0x0001 => {
122 match decode_full_duplex(tlv_value) {
123 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
124 Err(e) => format!("{{\"error\": \"{}\"}}", e),
125 }
126 }
127 0x0002 => {
128 match decode_packet_rx_count(tlv_value) {
129 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
130 Err(e) => format!("{{\"error\": \"{}\"}}", e),
131 }
132 }
133 0x0003 => {
134 match decode_packet_tx_count(tlv_value) {
135 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
136 Err(e) => format!("{{\"error\": \"{}\"}}", e),
137 }
138 }
139 0x0004 => {
140 match decode_tx_err_count(tlv_value) {
141 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
142 Err(e) => format!("{{\"error\": \"{}\"}}", e),
143 }
144 }
145 0x0005 => {
146 match decode_collision_count(tlv_value) {
147 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
148 Err(e) => format!("{{\"error\": \"{}\"}}", e),
149 }
150 }
151 0x0006 => {
152 match decode_overrun_count(tlv_value) {
153 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
154 Err(e) => format!("{{\"error\": \"{}\"}}", e),
155 }
156 }
157 0x0007 => {
158 match decode_carrier_detect(tlv_value) {
159 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
160 Err(e) => format!("{{\"error\": \"{}\"}}", e),
161 }
162 }
163 0x0008 => {
164 match decode_time_since_reset(tlv_value) {
165 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
166 Err(e) => format!("{{\"error\": \"{}\"}}", e),
167 }
168 }
169 _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
170 }
171}
172
173pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
178 vec![
179 (0x0000, "PHYRate"),
180 (0x0001, "FullDuplex"),
181 (0x0002, "PacketRxCount"),
182 (0x0003, "PacketTxCount"),
183 (0x0004, "TxErrCount"),
184 (0x0005, "CollisionCount"),
185 (0x0006, "OverrunCount"),
186 (0x0007, "CarrierDetect"),
187 (0x0008, "TimeSinceReset"),
188 ]
189}
190