matc 0.1.3

Matter protocol library (controller side)
Documentation
//! Matter TLV encoders and decoders for WebRTC Transport Requestor Cluster
//! Cluster ID: 0x0554
//!
//! This file is automatically generated from WebRTC_Requestor.xml

#![allow(clippy::too_many_arguments)]

use crate::tlv;
use anyhow;
use serde_json;


// Command encoders

/// Encode Offer command (0x00)
pub fn encode_offer(web_rtc_session_id: u8, sdp: String, ice_transport_policy: Option<String>) -> anyhow::Result<Vec<u8>> {
    let mut tlv_fields: Vec<tlv::TlvItemEnc> = Vec::new();
    tlv_fields.push((0, tlv::TlvItemValueEnc::UInt8(web_rtc_session_id)).into());
    tlv_fields.push((1, tlv::TlvItemValueEnc::String(sdp)).into());
    if let Some(x) = ice_transport_policy { tlv_fields.push((3, tlv::TlvItemValueEnc::String(x)).into()); }
    let tlv = tlv::TlvItemEnc {
        tag: 0,
        value: tlv::TlvItemValueEnc::StructInvisible(tlv_fields),
    };
    Ok(tlv.encode()?)
}

/// Encode Answer command (0x01)
pub fn encode_answer(web_rtc_session_id: u8, sdp: String) -> anyhow::Result<Vec<u8>> {
    let tlv = tlv::TlvItemEnc {
        tag: 0,
        value: tlv::TlvItemValueEnc::StructInvisible(vec![
        (0, tlv::TlvItemValueEnc::UInt8(web_rtc_session_id)).into(),
        (1, tlv::TlvItemValueEnc::String(sdp)).into(),
        ]),
    };
    Ok(tlv.encode()?)
}

/// Encode ICECandidates command (0x02)
pub fn encode_ice_candidates(web_rtc_session_id: u8) -> anyhow::Result<Vec<u8>> {
    let tlv = tlv::TlvItemEnc {
        tag: 0,
        value: tlv::TlvItemValueEnc::StructInvisible(vec![
        (0, tlv::TlvItemValueEnc::UInt8(web_rtc_session_id)).into(),
        ]),
    };
    Ok(tlv.encode()?)
}

/// Encode End command (0x03)
pub fn encode_end(web_rtc_session_id: u8, reason: u8) -> anyhow::Result<Vec<u8>> {
    let tlv = tlv::TlvItemEnc {
        tag: 0,
        value: tlv::TlvItemValueEnc::StructInvisible(vec![
        (0, tlv::TlvItemValueEnc::UInt8(web_rtc_session_id)).into(),
        (1, tlv::TlvItemValueEnc::UInt8(reason)).into(),
        ]),
    };
    Ok(tlv.encode()?)
}

// Attribute decoders

/// Decode CurrentSessions attribute (0x0000)
pub fn decode_current_sessions(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<u8>> {
    let mut res = Vec::new();
    if let tlv::TlvItemValue::List(v) = inp {
        for item in v {
            if let tlv::TlvItemValue::Int(i) = &item.value {
                res.push(*i as u8);
            }
        }
    }
    Ok(res)
}


// JSON dispatcher function

/// Decode attribute value and return as JSON string
///
/// # Parameters
/// * `cluster_id` - The cluster identifier
/// * `attribute_id` - The attribute identifier
/// * `tlv_value` - The TLV value to decode
///
/// # Returns
/// JSON string representation of the decoded value or error
pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
    // Verify this is the correct cluster
    if cluster_id != 0x0554 {
        return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0554, got {}\"}}", cluster_id);
    }

    match attribute_id {
        0x0000 => {
            match decode_current_sessions(tlv_value) {
                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
                Err(e) => format!("{{\"error\": \"{}\"}}", e),
            }
        }
        _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
    }
}

/// Get list of all attributes supported by this cluster
///
/// # Returns
/// Vector of tuples containing (attribute_id, attribute_name)
pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
    vec![
        (0x0000, "CurrentSessions"),
    ]
}

// Command listing

pub fn get_command_list() -> Vec<(u32, &'static str)> {
    vec![
        (0x00, "Offer"),
        (0x01, "Answer"),
        (0x02, "ICECandidates"),
        (0x03, "End"),
    ]
}

pub fn get_command_name(cmd_id: u32) -> Option<&'static str> {
    match cmd_id {
        0x00 => Some("Offer"),
        0x01 => Some("Answer"),
        0x02 => Some("ICECandidates"),
        0x03 => Some("End"),
        _ => None,
    }
}

pub fn get_command_schema(cmd_id: u32) -> Option<Vec<crate::clusters::codec::CommandField>> {
    match cmd_id {
        0x00 => Some(vec![
            crate::clusters::codec::CommandField { tag: 0, name: "web_rtc_session_id", kind: crate::clusters::codec::FieldKind::U32, optional: false, nullable: false },
            crate::clusters::codec::CommandField { tag: 1, name: "sdp", kind: crate::clusters::codec::FieldKind::String, optional: false, nullable: false },
            crate::clusters::codec::CommandField { tag: 3, name: "ice_transport_policy", kind: crate::clusters::codec::FieldKind::String, optional: true, nullable: false },
        ]),
        0x01 => Some(vec![
            crate::clusters::codec::CommandField { tag: 0, name: "web_rtc_session_id", kind: crate::clusters::codec::FieldKind::U32, optional: false, nullable: false },
            crate::clusters::codec::CommandField { tag: 1, name: "sdp", kind: crate::clusters::codec::FieldKind::String, optional: false, nullable: false },
        ]),
        0x02 => Some(vec![
            crate::clusters::codec::CommandField { tag: 0, name: "web_rtc_session_id", kind: crate::clusters::codec::FieldKind::U32, optional: false, nullable: false },
        ]),
        0x03 => Some(vec![
            crate::clusters::codec::CommandField { tag: 0, name: "web_rtc_session_id", kind: crate::clusters::codec::FieldKind::U32, optional: false, nullable: false },
            crate::clusters::codec::CommandField { tag: 1, name: "reason", kind: crate::clusters::codec::FieldKind::U8, optional: false, nullable: false },
        ]),
        _ => None,
    }
}

pub fn encode_command_json(cmd_id: u32, args: &serde_json::Value) -> anyhow::Result<Vec<u8>> {
    match cmd_id {
        0x00 => {
        let web_rtc_session_id = crate::clusters::codec::json_util::get_u8(args, "web_rtc_session_id")?;
        let sdp = crate::clusters::codec::json_util::get_string(args, "sdp")?;
        let ice_transport_policy = crate::clusters::codec::json_util::get_opt_string(args, "ice_transport_policy")?;
        encode_offer(web_rtc_session_id, sdp, ice_transport_policy)
        }
        0x01 => {
        let web_rtc_session_id = crate::clusters::codec::json_util::get_u8(args, "web_rtc_session_id")?;
        let sdp = crate::clusters::codec::json_util::get_string(args, "sdp")?;
        encode_answer(web_rtc_session_id, sdp)
        }
        0x02 => {
        let web_rtc_session_id = crate::clusters::codec::json_util::get_u8(args, "web_rtc_session_id")?;
        encode_ice_candidates(web_rtc_session_id)
        }
        0x03 => {
        let web_rtc_session_id = crate::clusters::codec::json_util::get_u8(args, "web_rtc_session_id")?;
        let reason = crate::clusters::codec::json_util::get_u8(args, "reason")?;
        encode_end(web_rtc_session_id, reason)
        }
        _ => Err(anyhow::anyhow!("unknown command ID: 0x{:02X}", cmd_id)),
    }
}

// Typed facade (invokes + reads)

/// Invoke `Offer` command on cluster `WebRTC Transport Requestor`.
pub async fn offer(conn: &crate::controller::Connection, endpoint: u16, web_rtc_session_id: u8, sdp: String, ice_transport_policy: Option<String>) -> anyhow::Result<()> {
    conn.invoke_request(endpoint, crate::clusters::defs::CLUSTER_ID_WEBRTC_TRANSPORT_REQUESTOR, crate::clusters::defs::CLUSTER_WEBRTC_TRANSPORT_REQUESTOR_CMD_ID_OFFER, &encode_offer(web_rtc_session_id, sdp, ice_transport_policy)?).await?;
    Ok(())
}

/// Invoke `Answer` command on cluster `WebRTC Transport Requestor`.
pub async fn answer(conn: &crate::controller::Connection, endpoint: u16, web_rtc_session_id: u8, sdp: String) -> anyhow::Result<()> {
    conn.invoke_request(endpoint, crate::clusters::defs::CLUSTER_ID_WEBRTC_TRANSPORT_REQUESTOR, crate::clusters::defs::CLUSTER_WEBRTC_TRANSPORT_REQUESTOR_CMD_ID_ANSWER, &encode_answer(web_rtc_session_id, sdp)?).await?;
    Ok(())
}

/// Invoke `ICECandidates` command on cluster `WebRTC Transport Requestor`.
pub async fn ice_candidates(conn: &crate::controller::Connection, endpoint: u16, web_rtc_session_id: u8) -> anyhow::Result<()> {
    conn.invoke_request(endpoint, crate::clusters::defs::CLUSTER_ID_WEBRTC_TRANSPORT_REQUESTOR, crate::clusters::defs::CLUSTER_WEBRTC_TRANSPORT_REQUESTOR_CMD_ID_ICECANDIDATES, &encode_ice_candidates(web_rtc_session_id)?).await?;
    Ok(())
}

/// Invoke `End` command on cluster `WebRTC Transport Requestor`.
pub async fn end(conn: &crate::controller::Connection, endpoint: u16, web_rtc_session_id: u8, reason: u8) -> anyhow::Result<()> {
    conn.invoke_request(endpoint, crate::clusters::defs::CLUSTER_ID_WEBRTC_TRANSPORT_REQUESTOR, crate::clusters::defs::CLUSTER_WEBRTC_TRANSPORT_REQUESTOR_CMD_ID_END, &encode_end(web_rtc_session_id, reason)?).await?;
    Ok(())
}

/// Read `CurrentSessions` attribute from cluster `WebRTC Transport Requestor`.
pub async fn read_current_sessions(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Vec<u8>> {
    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_WEBRTC_TRANSPORT_REQUESTOR, crate::clusters::defs::CLUSTER_WEBRTC_TRANSPORT_REQUESTOR_ATTR_ID_CURRENTSESSIONS).await?;
    decode_current_sessions(&tlv)
}