matc/clusters/codec/
wake_on_lan.rs1#![allow(clippy::too_many_arguments)]
7
8use crate::tlv;
9use anyhow;
10use serde_json;
11
12
13pub 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
24pub 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
34pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
46 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
68pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
73 vec![
74 (0x0000, "MACAddress"),
75 (0x0001, "LinkLocalAddress"),
76 ]
77}
78
79pub 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
87pub 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