matc/clusters/codec/
wake_on_lan.rs

1//! Matter TLV encoders and decoders for Wake On LAN Cluster
2//! Cluster ID: 0x0503
3//!
4//! This file is automatically generated from WakeOnLAN.xml
5
6#![allow(clippy::too_many_arguments)]
7
8use crate::tlv;
9use anyhow;
10use serde_json;
11
12
13// Attribute decoders
14
15/// Decode MACAddress attribute (0x0000)
16pub fn decode_mac_address(inp: &tlv::TlvItemValue) -> anyhow::Result<String> {
17    if let tlv::TlvItemValue::String(v) = inp {
18        Ok(v.clone())
19    } else {
20        Err(anyhow::anyhow!("Expected String"))
21    }
22}
23
24/// Decode LinkLocalAddress attribute (0x0001)
25pub fn decode_link_local_address(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
26    if let tlv::TlvItemValue::Int(v) = inp {
27        Ok(*v as u8)
28    } else {
29        Err(anyhow::anyhow!("Expected UInt8"))
30    }
31}
32
33
34// JSON dispatcher function
35
36/// Decode attribute value and return as JSON string
37///
38/// # Parameters
39/// * `cluster_id` - The cluster identifier
40/// * `attribute_id` - The attribute identifier
41/// * `tlv_value` - The TLV value to decode
42///
43/// # Returns
44/// JSON string representation of the decoded value or error
45pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
46    // Verify this is the correct cluster
47    if cluster_id != 0x0503 {
48        return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0503, got {}\"}}", cluster_id);
49    }
50
51    match attribute_id {
52        0x0000 => {
53            match decode_mac_address(tlv_value) {
54                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
55                Err(e) => format!("{{\"error\": \"{}\"}}", e),
56            }
57        }
58        0x0001 => {
59            match decode_link_local_address(tlv_value) {
60                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
61                Err(e) => format!("{{\"error\": \"{}\"}}", e),
62            }
63        }
64        _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
65    }
66}
67
68/// Get list of all attributes supported by this cluster
69///
70/// # Returns
71/// Vector of tuples containing (attribute_id, attribute_name)
72pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
73    vec![
74        (0x0000, "MACAddress"),
75        (0x0001, "LinkLocalAddress"),
76    ]
77}
78
79// Typed facade (invokes + reads)
80
81/// Read `MACAddress` attribute from cluster `Wake On LAN`.
82pub async fn read_mac_address(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<String> {
83    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_WAKE_ON_LAN, crate::clusters::defs::CLUSTER_WAKE_ON_LAN_ATTR_ID_MACADDRESS).await?;
84    decode_mac_address(&tlv)
85}
86
87/// Read `LinkLocalAddress` attribute from cluster `Wake On LAN`.
88pub async fn read_link_local_address(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<u8> {
89    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_WAKE_ON_LAN, crate::clusters::defs::CLUSTER_WAKE_ON_LAN_ATTR_ID_LINKLOCALADDRESS).await?;
90    decode_link_local_address(&tlv)
91}
92