matc/clusters/codec/
thread_border_router_management.rs1use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11pub 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
25pub 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
36pub 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
47pub 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
56pub 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
65pub 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
74pub 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
83pub 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
93pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
105 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
151pub 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