matc/clusters/codec/
audio_output.rs1use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11#[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
20pub 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
33pub 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
45pub 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
62pub 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
72pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
84 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
106pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
111 vec![
112 (0x0000, "OutputList"),
113 (0x0001, "CurrentOutput"),
114 ]
115}
116