Skip to main content

matc/clusters/codec/
account_login.rs

1//! Matter TLV encoders and decoders for Account Login Cluster
2//! Cluster ID: 0x050E
3//!
4//! This file is automatically generated from AccountLogin.xml
5
6#![allow(clippy::too_many_arguments)]
7
8use crate::tlv;
9use anyhow;
10use serde_json;
11
12
13// Command encoders
14
15/// Encode GetSetupPIN command (0x00)
16pub fn encode_get_setup_pin(temp_account_identifier: String) -> anyhow::Result<Vec<u8>> {
17    let tlv = tlv::TlvItemEnc {
18        tag: 0,
19        value: tlv::TlvItemValueEnc::StructInvisible(vec![
20        (0, tlv::TlvItemValueEnc::String(temp_account_identifier)).into(),
21        ]),
22    };
23    Ok(tlv.encode()?)
24}
25
26/// Encode Login command (0x02)
27pub fn encode_login(temp_account_identifier: String, setup_pin: String, node: Option<u64>) -> anyhow::Result<Vec<u8>> {
28    let mut tlv_fields: Vec<tlv::TlvItemEnc> = Vec::new();
29    tlv_fields.push((0, tlv::TlvItemValueEnc::String(temp_account_identifier)).into());
30    tlv_fields.push((1, tlv::TlvItemValueEnc::String(setup_pin)).into());
31    if let Some(x) = node { tlv_fields.push((2, tlv::TlvItemValueEnc::UInt64(x)).into()); }
32    let tlv = tlv::TlvItemEnc {
33        tag: 0,
34        value: tlv::TlvItemValueEnc::StructInvisible(tlv_fields),
35    };
36    Ok(tlv.encode()?)
37}
38
39/// Encode Logout command (0x03)
40pub fn encode_logout(node: Option<u64>) -> anyhow::Result<Vec<u8>> {
41    let mut tlv_fields: Vec<tlv::TlvItemEnc> = Vec::new();
42    if let Some(x) = node { tlv_fields.push((0, tlv::TlvItemValueEnc::UInt64(x)).into()); }
43    let tlv = tlv::TlvItemEnc {
44        tag: 0,
45        value: tlv::TlvItemValueEnc::StructInvisible(tlv_fields),
46    };
47    Ok(tlv.encode()?)
48}
49
50// Command listing
51
52pub fn get_command_list() -> Vec<(u32, &'static str)> {
53    vec![
54        (0x00, "GetSetupPIN"),
55        (0x02, "Login"),
56        (0x03, "Logout"),
57    ]
58}
59
60pub fn get_command_name(cmd_id: u32) -> Option<&'static str> {
61    match cmd_id {
62        0x00 => Some("GetSetupPIN"),
63        0x02 => Some("Login"),
64        0x03 => Some("Logout"),
65        _ => None,
66    }
67}
68
69pub fn get_command_schema(cmd_id: u32) -> Option<Vec<crate::clusters::codec::CommandField>> {
70    match cmd_id {
71        0x00 => Some(vec![
72            crate::clusters::codec::CommandField { tag: 0, name: "temp_account_identifier", kind: crate::clusters::codec::FieldKind::String, optional: false, nullable: false },
73        ]),
74        0x02 => Some(vec![
75            crate::clusters::codec::CommandField { tag: 0, name: "temp_account_identifier", kind: crate::clusters::codec::FieldKind::String, optional: false, nullable: false },
76            crate::clusters::codec::CommandField { tag: 1, name: "setup_pin", kind: crate::clusters::codec::FieldKind::String, optional: false, nullable: false },
77            crate::clusters::codec::CommandField { tag: 2, name: "node", kind: crate::clusters::codec::FieldKind::U64, optional: true, nullable: false },
78        ]),
79        0x03 => Some(vec![
80            crate::clusters::codec::CommandField { tag: 0, name: "node", kind: crate::clusters::codec::FieldKind::U64, optional: true, nullable: false },
81        ]),
82        _ => None,
83    }
84}
85
86pub fn encode_command_json(cmd_id: u32, args: &serde_json::Value) -> anyhow::Result<Vec<u8>> {
87    match cmd_id {
88        0x00 => {
89        let temp_account_identifier = crate::clusters::codec::json_util::get_string(args, "temp_account_identifier")?;
90        encode_get_setup_pin(temp_account_identifier)
91        }
92        0x02 => {
93        let temp_account_identifier = crate::clusters::codec::json_util::get_string(args, "temp_account_identifier")?;
94        let setup_pin = crate::clusters::codec::json_util::get_string(args, "setup_pin")?;
95        let node = crate::clusters::codec::json_util::get_opt_u64(args, "node")?;
96        encode_login(temp_account_identifier, setup_pin, node)
97        }
98        0x03 => {
99        let node = crate::clusters::codec::json_util::get_opt_u64(args, "node")?;
100        encode_logout(node)
101        }
102        _ => Err(anyhow::anyhow!("unknown command ID: 0x{:02X}", cmd_id)),
103    }
104}
105
106#[derive(Debug, serde::Serialize)]
107pub struct GetSetupPINResponse {
108    pub setup_pin: Option<String>,
109}
110
111// Command response decoders
112
113/// Decode GetSetupPINResponse command response (01)
114pub fn decode_get_setup_pin_response(inp: &tlv::TlvItemValue) -> anyhow::Result<GetSetupPINResponse> {
115    if let tlv::TlvItemValue::List(_fields) = inp {
116        let item = tlv::TlvItem { tag: 0, value: inp.clone() };
117        Ok(GetSetupPINResponse {
118                setup_pin: item.get_string_owned(&[0]),
119        })
120    } else {
121        Err(anyhow::anyhow!("Expected struct fields"))
122    }
123}
124
125// Typed facade (invokes + reads)
126
127/// Invoke `GetSetupPIN` command on cluster `Account Login`.
128pub async fn get_setup_pin(conn: &crate::controller::Connection, endpoint: u16, temp_account_identifier: String) -> anyhow::Result<GetSetupPINResponse> {
129    let tlv = conn.invoke_request2(endpoint, crate::clusters::defs::CLUSTER_ID_ACCOUNT_LOGIN, crate::clusters::defs::CLUSTER_ACCOUNT_LOGIN_CMD_ID_GETSETUPPIN, &encode_get_setup_pin(temp_account_identifier)?).await?;
130    decode_get_setup_pin_response(&tlv)
131}
132
133/// Invoke `Login` command on cluster `Account Login`.
134pub async fn login(conn: &crate::controller::Connection, endpoint: u16, temp_account_identifier: String, setup_pin: String, node: Option<u64>) -> anyhow::Result<()> {
135    conn.invoke_request(endpoint, crate::clusters::defs::CLUSTER_ID_ACCOUNT_LOGIN, crate::clusters::defs::CLUSTER_ACCOUNT_LOGIN_CMD_ID_LOGIN, &encode_login(temp_account_identifier, setup_pin, node)?).await?;
136    Ok(())
137}
138
139/// Invoke `Logout` command on cluster `Account Login`.
140pub async fn logout(conn: &crate::controller::Connection, endpoint: u16, node: Option<u64>) -> anyhow::Result<()> {
141    conn.invoke_request(endpoint, crate::clusters::defs::CLUSTER_ID_ACCOUNT_LOGIN, crate::clusters::defs::CLUSTER_ACCOUNT_LOGIN_CMD_ID_LOGOUT, &encode_logout(node)?).await?;
142    Ok(())
143}
144
145#[derive(Debug, serde::Serialize)]
146pub struct LoggedOutEvent {
147    pub node: Option<u64>,
148}
149
150// Event decoders
151
152/// Decode LoggedOut event (0x00, priority: critical)
153pub fn decode_logged_out_event(inp: &tlv::TlvItemValue) -> anyhow::Result<LoggedOutEvent> {
154    if let tlv::TlvItemValue::List(_fields) = inp {
155        let item = tlv::TlvItem { tag: 0, value: inp.clone() };
156        Ok(LoggedOutEvent {
157                                node: item.get_int(&[0]),
158        })
159    } else {
160        Err(anyhow::anyhow!("Expected struct fields"))
161    }
162}
163
164
165// Event JSON dispatcher
166
167/// Decode event value and return as JSON string
168pub fn decode_event_json(cluster_id: u32, event_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
169    if cluster_id != 0x050E {
170        return format!("{{\"error\": \"Invalid cluster ID. Expected 0x050E, got {}\"}}", cluster_id);
171    }
172
173    match event_id {
174        0x00 => {
175            match decode_logged_out_event(tlv_value) {
176                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
177                Err(e) => format!("{{\"error\": \"{}\"}}", e),
178            }
179        }
180        _ => format!("{{\"error\": \"Unknown event ID: {}\"}}", event_id),
181    }
182}
183
184/// Get list of all events supported by this cluster
185///
186/// # Returns
187/// Vector of tuples containing (event_id, event_name)
188pub fn get_event_list() -> Vec<(u32, &'static str)> {
189    vec![
190        (0x00, "LoggedOut"),
191    ]
192}
193