matc/clusters/codec/
media_input.rs

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