matc/clusters/codec/
diagnostics_software.rs1#![allow(clippy::too_many_arguments)]
7
8use crate::tlv;
9use anyhow;
10use serde_json;
11
12
13use crate::clusters::helpers::{serialize_opt_bytes_as_hex};
15
16#[derive(Debug, serde::Serialize)]
19pub struct ThreadMetrics {
20 pub id: Option<u64>,
21 pub name: Option<String>,
22 pub stack_free_current: Option<u32>,
23 pub stack_free_minimum: Option<u32>,
24 pub stack_size: Option<u32>,
25}
26
27pub fn decode_thread_metrics(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<ThreadMetrics>> {
33 let mut res = Vec::new();
34 if let tlv::TlvItemValue::List(v) = inp {
35 for item in v {
36 res.push(ThreadMetrics {
37 id: item.get_int(&[0]),
38 name: item.get_string_owned(&[1]),
39 stack_free_current: item.get_int(&[2]).map(|v| v as u32),
40 stack_free_minimum: item.get_int(&[3]).map(|v| v as u32),
41 stack_size: item.get_int(&[4]).map(|v| v as u32),
42 });
43 }
44 }
45 Ok(res)
46}
47
48pub fn decode_current_heap_free(inp: &tlv::TlvItemValue) -> anyhow::Result<u64> {
50 if let tlv::TlvItemValue::Int(v) = inp {
51 Ok(*v)
52 } else {
53 Err(anyhow::anyhow!("Expected UInt64"))
54 }
55}
56
57pub fn decode_current_heap_used(inp: &tlv::TlvItemValue) -> anyhow::Result<u64> {
59 if let tlv::TlvItemValue::Int(v) = inp {
60 Ok(*v)
61 } else {
62 Err(anyhow::anyhow!("Expected UInt64"))
63 }
64}
65
66pub fn decode_current_heap_high_watermark(inp: &tlv::TlvItemValue) -> anyhow::Result<u64> {
68 if let tlv::TlvItemValue::Int(v) = inp {
69 Ok(*v)
70 } else {
71 Err(anyhow::anyhow!("Expected UInt64"))
72 }
73}
74
75
76pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
88 if cluster_id != 0x0034 {
90 return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0034, got {}\"}}", cluster_id);
91 }
92
93 match attribute_id {
94 0x0000 => {
95 match decode_thread_metrics(tlv_value) {
96 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
97 Err(e) => format!("{{\"error\": \"{}\"}}", e),
98 }
99 }
100 0x0001 => {
101 match decode_current_heap_free(tlv_value) {
102 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
103 Err(e) => format!("{{\"error\": \"{}\"}}", e),
104 }
105 }
106 0x0002 => {
107 match decode_current_heap_used(tlv_value) {
108 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
109 Err(e) => format!("{{\"error\": \"{}\"}}", e),
110 }
111 }
112 0x0003 => {
113 match decode_current_heap_high_watermark(tlv_value) {
114 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
115 Err(e) => format!("{{\"error\": \"{}\"}}", e),
116 }
117 }
118 _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
119 }
120}
121
122pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
127 vec![
128 (0x0000, "ThreadMetrics"),
129 (0x0001, "CurrentHeapFree"),
130 (0x0002, "CurrentHeapUsed"),
131 (0x0003, "CurrentHeapHighWatermark"),
132 ]
133}
134
135pub fn get_command_list() -> Vec<(u32, &'static str)> {
138 vec![
139 (0x00, "ResetWatermarks"),
140 ]
141}
142
143pub fn get_command_name(cmd_id: u32) -> Option<&'static str> {
144 match cmd_id {
145 0x00 => Some("ResetWatermarks"),
146 _ => None,
147 }
148}
149
150pub fn get_command_schema(cmd_id: u32) -> Option<Vec<crate::clusters::codec::CommandField>> {
151 match cmd_id {
152 0x00 => Some(vec![]),
153 _ => None,
154 }
155}
156
157pub fn encode_command_json(cmd_id: u32, _args: &serde_json::Value) -> anyhow::Result<Vec<u8>> {
158 match cmd_id {
159 0x00 => Ok(vec![]),
160 _ => Err(anyhow::anyhow!("unknown command ID: 0x{:02X}", cmd_id)),
161 }
162}
163
164pub async fn reset_watermarks(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<()> {
168 conn.invoke_request(endpoint, crate::clusters::defs::CLUSTER_ID_SOFTWARE_DIAGNOSTICS, crate::clusters::defs::CLUSTER_SOFTWARE_DIAGNOSTICS_CMD_ID_RESETWATERMARKS, &[]).await?;
169 Ok(())
170}
171
172pub async fn read_thread_metrics(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Vec<ThreadMetrics>> {
174 let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_SOFTWARE_DIAGNOSTICS, crate::clusters::defs::CLUSTER_SOFTWARE_DIAGNOSTICS_ATTR_ID_THREADMETRICS).await?;
175 decode_thread_metrics(&tlv)
176}
177
178pub async fn read_current_heap_free(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<u64> {
180 let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_SOFTWARE_DIAGNOSTICS, crate::clusters::defs::CLUSTER_SOFTWARE_DIAGNOSTICS_ATTR_ID_CURRENTHEAPFREE).await?;
181 decode_current_heap_free(&tlv)
182}
183
184pub async fn read_current_heap_used(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<u64> {
186 let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_SOFTWARE_DIAGNOSTICS, crate::clusters::defs::CLUSTER_SOFTWARE_DIAGNOSTICS_ATTR_ID_CURRENTHEAPUSED).await?;
187 decode_current_heap_used(&tlv)
188}
189
190pub async fn read_current_heap_high_watermark(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<u64> {
192 let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_SOFTWARE_DIAGNOSTICS, crate::clusters::defs::CLUSTER_SOFTWARE_DIAGNOSTICS_ATTR_ID_CURRENTHEAPHIGHWATERMARK).await?;
193 decode_current_heap_high_watermark(&tlv)
194}
195
196#[derive(Debug, serde::Serialize)]
197pub struct SoftwareFaultEvent {
198 pub id: Option<u64>,
199 pub name: Option<String>,
200 #[serde(serialize_with = "serialize_opt_bytes_as_hex")]
201 pub fault_recording: Option<Vec<u8>>,
202}
203
204pub fn decode_software_fault_event(inp: &tlv::TlvItemValue) -> anyhow::Result<SoftwareFaultEvent> {
208 if let tlv::TlvItemValue::List(_fields) = inp {
209 let item = tlv::TlvItem { tag: 0, value: inp.clone() };
210 Ok(SoftwareFaultEvent {
211 id: item.get_int(&[0]),
212 name: item.get_string_owned(&[1]),
213 fault_recording: item.get_octet_string_owned(&[2]),
214 })
215 } else {
216 Err(anyhow::anyhow!("Expected struct fields"))
217 }
218}
219