matc/clusters/codec/
target_navigator.rs1use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11#[derive(Debug, serde::Serialize)]
14pub struct TargetInfo {
15 pub identifier: Option<u8>,
16 pub name: Option<String>,
17}
18
19pub fn encode_navigate_target(target: u8, data: String) -> anyhow::Result<Vec<u8>> {
23 let tlv = tlv::TlvItemEnc {
24 tag: 0,
25 value: tlv::TlvItemValueEnc::StructInvisible(vec![
26 (0, tlv::TlvItemValueEnc::UInt8(target)).into(),
27 (1, tlv::TlvItemValueEnc::String(data)).into(),
28 ]),
29 };
30 Ok(tlv.encode()?)
31}
32
33pub fn decode_target_list(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<TargetInfo>> {
37 let mut res = Vec::new();
38 if let tlv::TlvItemValue::List(v) = inp {
39 for item in v {
40 res.push(TargetInfo {
41 identifier: item.get_int(&[0]).map(|v| v as u8),
42 name: item.get_string_owned(&[1]),
43 });
44 }
45 }
46 Ok(res)
47}
48
49pub fn decode_current_target(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
51 if let tlv::TlvItemValue::Int(v) = inp {
52 Ok(*v as u8)
53 } else {
54 Err(anyhow::anyhow!("Expected Integer"))
55 }
56}
57
58
59pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
71 if cluster_id != 0x0505 {
73 return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0505, got {}\"}}", cluster_id);
74 }
75
76 match attribute_id {
77 0x0000 => {
78 match decode_target_list(tlv_value) {
79 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
80 Err(e) => format!("{{\"error\": \"{}\"}}", e),
81 }
82 }
83 0x0001 => {
84 match decode_current_target(tlv_value) {
85 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
86 Err(e) => format!("{{\"error\": \"{}\"}}", e),
87 }
88 }
89 _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
90 }
91}
92
93pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
98 vec![
99 (0x0000, "TargetList"),
100 (0x0001, "CurrentTarget"),
101 ]
102}
103