matc/clusters/codec/
target_navigator.rs

1//! Generated Matter TLV encoders and decoders for Target Navigator Cluster
2//! Cluster ID: 0x0505
3//! 
4//! This file is automatically generated from TargetNavigator.xml
5
6use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11// Struct definitions
12
13#[derive(Debug, serde::Serialize)]
14pub struct TargetInfo {
15    pub identifier: Option<u8>,
16    pub name: Option<String>,
17}
18
19// Command encoders
20
21/// Encode NavigateTarget command (0x00)
22pub 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
33// Attribute decoders
34
35/// Decode TargetList attribute (0x0000)
36pub 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
49/// Decode CurrentTarget attribute (0x0001)
50pub 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
59// JSON dispatcher function
60
61/// Decode attribute value and return as JSON string
62/// 
63/// # Parameters
64/// * `cluster_id` - The cluster identifier
65/// * `attribute_id` - The attribute identifier
66/// * `tlv_value` - The TLV value to decode
67/// 
68/// # Returns
69/// JSON string representation of the decoded value or error
70pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
71    // Verify this is the correct cluster
72    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
93/// Get list of all attributes supported by this cluster
94/// 
95/// # Returns
96/// Vector of tuples containing (attribute_id, attribute_name)
97pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
98    vec![
99        (0x0000, "TargetList"),
100        (0x0001, "CurrentTarget"),
101    ]
102}
103