matc/clusters/codec/
thread_border_router_management.rs

1//! Generated Matter TLV encoders and decoders for Thread Border Router Management Cluster
2//! Cluster ID: 0x0452
3//! 
4//! This file is automatically generated from ThreadBorderRouterManagement.xml
5
6use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11// Command encoders
12
13/// Encode SetActiveDatasetRequest command (0x03)
14pub fn encode_set_active_dataset_request(active_dataset: Vec<u8>, breadcrumb: u64) -> anyhow::Result<Vec<u8>> {
15    let tlv = tlv::TlvItemEnc {
16        tag: 0,
17        value: tlv::TlvItemValueEnc::StructInvisible(vec![
18        (0, tlv::TlvItemValueEnc::OctetString(active_dataset)).into(),
19        (1, tlv::TlvItemValueEnc::UInt64(breadcrumb)).into(),
20        ]),
21    };
22    Ok(tlv.encode()?)
23}
24
25/// Encode SetPendingDatasetRequest command (0x04)
26pub fn encode_set_pending_dataset_request(pending_dataset: Vec<u8>) -> anyhow::Result<Vec<u8>> {
27    let tlv = tlv::TlvItemEnc {
28        tag: 0,
29        value: tlv::TlvItemValueEnc::StructInvisible(vec![
30        (0, tlv::TlvItemValueEnc::OctetString(pending_dataset)).into(),
31        ]),
32    };
33    Ok(tlv.encode()?)
34}
35
36// Attribute decoders
37
38/// Decode BorderRouterName attribute (0x0000)
39pub fn decode_border_router_name(inp: &tlv::TlvItemValue) -> anyhow::Result<String> {
40    if let tlv::TlvItemValue::String(v) = inp {
41        Ok(v.clone())
42    } else {
43        Err(anyhow::anyhow!("Expected String"))
44    }
45}
46
47/// Decode BorderAgentID attribute (0x0001)
48pub fn decode_border_agent_id(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<u8>> {
49    if let tlv::TlvItemValue::OctetString(v) = inp {
50        Ok(v.clone())
51    } else {
52        Err(anyhow::anyhow!("Expected OctetString"))
53    }
54}
55
56/// Decode ThreadVersion attribute (0x0002)
57pub fn decode_thread_version(inp: &tlv::TlvItemValue) -> anyhow::Result<u16> {
58    if let tlv::TlvItemValue::Int(v) = inp {
59        Ok(*v as u16)
60    } else {
61        Err(anyhow::anyhow!("Expected Integer"))
62    }
63}
64
65/// Decode InterfaceEnabled attribute (0x0003)
66pub fn decode_interface_enabled(inp: &tlv::TlvItemValue) -> anyhow::Result<bool> {
67    if let tlv::TlvItemValue::Bool(v) = inp {
68        Ok(*v)
69    } else {
70        Err(anyhow::anyhow!("Expected Bool"))
71    }
72}
73
74/// Decode ActiveDatasetTimestamp attribute (0x0004)
75pub fn decode_active_dataset_timestamp(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u64>> {
76    if let tlv::TlvItemValue::Int(v) = inp {
77        Ok(Some(*v))
78    } else {
79        Ok(None)
80    }
81}
82
83/// Decode PendingDatasetTimestamp attribute (0x0005)
84pub fn decode_pending_dataset_timestamp(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u64>> {
85    if let tlv::TlvItemValue::Int(v) = inp {
86        Ok(Some(*v))
87    } else {
88        Ok(None)
89    }
90}
91
92
93// JSON dispatcher function
94
95/// Decode attribute value and return as JSON string
96/// 
97/// # Parameters
98/// * `cluster_id` - The cluster identifier
99/// * `attribute_id` - The attribute identifier
100/// * `tlv_value` - The TLV value to decode
101/// 
102/// # Returns
103/// JSON string representation of the decoded value or error
104pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
105    // Verify this is the correct cluster
106    if cluster_id != 0x0452 {
107        return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0452, got {}\"}}", cluster_id);
108    }
109    
110    match attribute_id {
111        0x0000 => {
112            match decode_border_router_name(tlv_value) {
113                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
114                Err(e) => format!("{{\"error\": \"{}\"}}", e),
115            }
116        }
117        0x0001 => {
118            match decode_border_agent_id(tlv_value) {
119                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
120                Err(e) => format!("{{\"error\": \"{}\"}}", e),
121            }
122        }
123        0x0002 => {
124            match decode_thread_version(tlv_value) {
125                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
126                Err(e) => format!("{{\"error\": \"{}\"}}", e),
127            }
128        }
129        0x0003 => {
130            match decode_interface_enabled(tlv_value) {
131                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
132                Err(e) => format!("{{\"error\": \"{}\"}}", e),
133            }
134        }
135        0x0004 => {
136            match decode_active_dataset_timestamp(tlv_value) {
137                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
138                Err(e) => format!("{{\"error\": \"{}\"}}", e),
139            }
140        }
141        0x0005 => {
142            match decode_pending_dataset_timestamp(tlv_value) {
143                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
144                Err(e) => format!("{{\"error\": \"{}\"}}", e),
145            }
146        }
147        _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
148    }
149}
150
151/// Get list of all attributes supported by this cluster
152/// 
153/// # Returns
154/// Vector of tuples containing (attribute_id, attribute_name)
155pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
156    vec![
157        (0x0000, "BorderRouterName"),
158        (0x0001, "BorderAgentID"),
159        (0x0002, "ThreadVersion"),
160        (0x0003, "InterfaceEnabled"),
161        (0x0004, "ActiveDatasetTimestamp"),
162        (0x0005, "PendingDatasetTimestamp"),
163    ]
164}
165