matc/clusters/codec/
audio_output.rs

1//! Generated Matter TLV encoders and decoders for Audio Output Cluster
2//! Cluster ID: 0x050B
3//! 
4//! This file is automatically generated from AudioOutput.xml
5
6use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11// Struct definitions
12
13#[derive(Debug, serde::Serialize)]
14pub struct OutputInfo {
15    pub index: Option<u8>,
16    pub output_type: Option<u8>,
17    pub name: Option<String>,
18}
19
20// Command encoders
21
22/// Encode SelectOutput command (0x00)
23pub fn encode_select_output(index: u8) -> anyhow::Result<Vec<u8>> {
24    let tlv = tlv::TlvItemEnc {
25        tag: 0,
26        value: tlv::TlvItemValueEnc::StructInvisible(vec![
27        (0, tlv::TlvItemValueEnc::UInt8(index)).into(),
28        ]),
29    };
30    Ok(tlv.encode()?)
31}
32
33/// Encode RenameOutput command (0x01)
34pub fn encode_rename_output(index: u8, name: String) -> anyhow::Result<Vec<u8>> {
35    let tlv = tlv::TlvItemEnc {
36        tag: 0,
37        value: tlv::TlvItemValueEnc::StructInvisible(vec![
38        (0, tlv::TlvItemValueEnc::UInt8(index)).into(),
39        (1, tlv::TlvItemValueEnc::String(name)).into(),
40        ]),
41    };
42    Ok(tlv.encode()?)
43}
44
45// Attribute decoders
46
47/// Decode OutputList attribute (0x0000)
48pub fn decode_output_list(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<OutputInfo>> {
49    let mut res = Vec::new();
50    if let tlv::TlvItemValue::List(v) = inp {
51        for item in v {
52            res.push(OutputInfo {
53                index: item.get_int(&[0]).map(|v| v as u8),
54                output_type: item.get_int(&[1]).map(|v| v as u8),
55                name: item.get_string_owned(&[2]),
56            });
57        }
58    }
59    Ok(res)
60}
61
62/// Decode CurrentOutput attribute (0x0001)
63pub fn decode_current_output(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
64    if let tlv::TlvItemValue::Int(v) = inp {
65        Ok(*v as u8)
66    } else {
67        Err(anyhow::anyhow!("Expected Integer"))
68    }
69}
70
71
72// JSON dispatcher function
73
74/// Decode attribute value and return as JSON string
75/// 
76/// # Parameters
77/// * `cluster_id` - The cluster identifier
78/// * `attribute_id` - The attribute identifier
79/// * `tlv_value` - The TLV value to decode
80/// 
81/// # Returns
82/// JSON string representation of the decoded value or error
83pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
84    // Verify this is the correct cluster
85    if cluster_id != 0x050B {
86        return format!("{{\"error\": \"Invalid cluster ID. Expected 0x050B, got {}\"}}", cluster_id);
87    }
88    
89    match attribute_id {
90        0x0000 => {
91            match decode_output_list(tlv_value) {
92                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
93                Err(e) => format!("{{\"error\": \"{}\"}}", e),
94            }
95        }
96        0x0001 => {
97            match decode_current_output(tlv_value) {
98                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
99                Err(e) => format!("{{\"error\": \"{}\"}}", e),
100            }
101        }
102        _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
103    }
104}
105
106/// Get list of all attributes supported by this cluster
107/// 
108/// # Returns
109/// Vector of tuples containing (attribute_id, attribute_name)
110pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
111    vec![
112        (0x0000, "OutputList"),
113        (0x0001, "CurrentOutput"),
114    ]
115}
116