matc/clusters/codec/
web_rtc_requestor.rs

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