matc/clusters/codec/
web_rtc_requestor.rs1use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11pub 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
26pub 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
38pub 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
49pub 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
61pub 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
77pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
89 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
105pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
110 vec![
111 (0x0000, "CurrentSessions"),
112 ]
113}
114