matc/clusters/codec/
diagnostics_ethernet.rs

1//! Generated Matter TLV encoders and decoders for Ethernet Network Diagnostics Cluster
2//! Cluster ID: 0x0037
3//! 
4//! This file is automatically generated from DiagnosticsEthernet.xml
5
6use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11// Command encoders
12
13// Attribute decoders
14
15/// Decode PHYRate attribute (0x0000)
16pub 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
24/// Decode FullDuplex attribute (0x0001)
25pub 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
33/// Decode PacketRxCount attribute (0x0002)
34pub 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
42/// Decode PacketTxCount attribute (0x0003)
43pub 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
51/// Decode TxErrCount attribute (0x0004)
52pub 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
60/// Decode CollisionCount attribute (0x0005)
61pub 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
69/// Decode OverrunCount attribute (0x0006)
70pub 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
78/// Decode CarrierDetect attribute (0x0007)
79pub 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
87/// Decode TimeSinceReset attribute (0x0008)
88pub 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
97// JSON dispatcher function
98
99/// Decode attribute value and return as JSON string
100/// 
101/// # Parameters
102/// * `cluster_id` - The cluster identifier
103/// * `attribute_id` - The attribute identifier
104/// * `tlv_value` - The TLV value to decode
105/// 
106/// # Returns
107/// JSON string representation of the decoded value or error
108pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
109    // Verify this is the correct cluster
110    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
173/// Get list of all attributes supported by this cluster
174/// 
175/// # Returns
176/// Vector of tuples containing (attribute_id, attribute_name)
177pub 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